starting on the commit logic

This commit is contained in:
2020-06-21 22:26:03 -04:00
parent 1ec7b436da
commit e4ac7f70e6
7 changed files with 90 additions and 28 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/asdine/storm"
"github.com/asdine/storm/q"
)
// ConfigureDB sets up bolt and Storm according to the path of the database
@@ -187,3 +188,39 @@ func (db *DB) UpdateDescription(patchID int, description string) error {
}
return nil
}
// FetchCommitByNumber fetches the commit number of the passed branch
func (db *DB) FetchCommitByNumber(branch string, commitNumber string) (commitResult Commit, err error) {
var commit Commit
db.Info().Msgf("fetching commit by number: %s in branch: %s", commitNumber, branch)
query := db.Select(q.And(q.Eq("Branch", branch), q.Eq("Number", commitNumber)))
err = query.Find(&commit)
if err != nil {
db.Err(err).Msgf("Failed to find commit by number: %s on branch: %s", commitNumber, branch)
return commit, err
}
return commit, nil
}
// FetchCommitByHash fetches a single commit on any branch by hash //TODO: Hash collision?
func (db *DB) FetchCommitByHash(hash string) (commitResult Commit, err error) {
var commit Commit
db.Info().Msgf("Searching for commit by hash: %s", hash)
if err := db.One("CommitHash", hash, &commit); err != nil {
db.Err(err).Msgf("Failed to find commit by hash: %s", hash)
return commit, err
}
return commit, nil
}
// FetchLastCommitOnBranch gets the latest commit to the provided branch
func (db *DB) FetchLastCommitOnBranch(branch string) (commitResult Commit, err error) {
var commit Commit
db.Info().Msgf("Fetching last commit on branch: %s", branch)
query := db.Select(q.Eq("Branch", branch)) //Selecting everything that applies to that branch
err = query.OrderBy("Number").Reverse().First(&commit) // Getting the last entry by number
if err != nil {
db.Err(err).Msgf("Failed to find last commit on branch: %s", branch)
}
return commit, nil
}