func comparisonStatus(code int) result.Result { switch code { case -1: return result.NewFailure(errors.New(upgradeToolError)) case 1: return result.NewFailure(errors.New(upgradeRepoError)) default: return result.NewSuccess(VersionStatusEqual) } }
func resolveCommitParent(commit *git.Commit) result.Result { parent := commit.Parent(0) if parent == nil { return result.NewFailure(errors.New(noParentError)) } return result.NewSuccess(parent) }
// Creates a new comment using provided content and author func NewComment(message string, commit string, fileRef *FileRef, author *Person) result.Result { const missingContentMessage = "No message content provided" const missingCommitMessage = "No commit provided" if len(message) == 0 { return result.NewFailure(errors.New(missingContentMessage)) } else if len(commit) == 0 { return result.NewFailure(errors.New(missingCommitMessage)) } return result.NewSuccess(&Comment{ author, message, author, &commit, nil, false, fileRef, }) }
func deleteReference(repo *git.Repository, comment *Comment, identifier string) error { _, err := RefPath(comment, identifier).FlatMap(func(refPath interface{}) result.Result { return result.NewResult(repo.References.Lookup(refPath.(string))) }).Analysis(func(ref interface{}) result.Result { return gg.DeleteReference(ref.(*git.Reference)) }, func(err error) result.Result { return result.NewFailure(errors.New(commentNotFoundError)) }).Dematerialize() return err }
func validatedCommitForComment(repo *git.Repository, commit string) result.Result { return gg.ResolveSingleCommitHash(repo, commit).FlatMap(func(hash interface{}) result.Result { return CommentCountOnCommit(repo, *(hash.(*string))).FlatMap(func(count interface{}) result.Result { if count.(uint16) >= maxCommentsOnCommit { return result.NewFailure(errors.New(maxCommentError)) } return result.NewSuccess(hash) }) }) }
// @return result.Result<VersionStatus, error> func compareVersion(toolVersion, repoVersion string) result.Result { errorMsg := fmt.Sprintf(toolInvalidError, toolVersion, repoVersion) invalidErr := result.NewFailure(errors.New(errorMsg)) vt := result.NewResult(semver.Make(toolVersion)).RecoverWith(invalidErr) vr := result.NewResult(semver.Make(repoVersion)).RecoverWith(invalidErr) return result.Combine(func(values ...interface{}) result.Result { vt, vr := values[0].(semver.Version), values[1].(semver.Version) return comparisonStatus(vt.Compare(vr)) }, vt, vr) }
func DeleteRemoteComment(repoPath, remoteName, commentID string) result.Result { return gg.WithRemote(repoPath, remoteName, func(remote *git.Remote) result.Result { return CreatePerson(gg.ConfiguredCommitter(repoPath)).Analysis(func(val interface{}) result.Result { sig := val.(*Person).Signature() refspec := fmt.Sprintf(":%v", commentID) return gg.Push(repoPath, remoteName, []string{refspec}, sig) }, func(err error) result.Result { return result.NewFailure(errors.New(noCommitterError)) }) }) }
// Check the version of git-comment in use against // the version in use in the repository. // @return result.Result<VersionStatus, error> func VersionCheck(repoPath, toolVersion string) result.Result { return gg.WithRepository(repoPath, func(repo *git.Repository) result.Result { return readVersion(repo).Analysis(func(version interface{}) result.Result { return compareVersion(toolVersion, version.(string)) }, func(err error) result.Result { if git.IsErrorCode(err, git.ErrNotFound) { return writeVersion(repo, toolVersion) } return result.NewFailure(err) }) }) }
// Write git object for a given comment and update the // comment refs // @return result.Result<*Comment, error> func writeCommentToDisk(repo *git.Repository, comment *Comment) result.Result { if comment.ID != nil { if err := deleteReference(repo, comment, *comment.ID); err != nil { return result.NewFailure(err) } } return gg.CreateBlob(repo, comment.Serialize()).FlatMap(func(oid interface{}) result.Result { id := fmt.Sprintf("%v", oid) return RefPath(comment, id).FlatMap(func(file interface{}) result.Result { commit := *comment.Commit message := fmt.Sprintf(defaultMessageFormat, commit[:7], id[:7]) return result.NewResult(repo.References.Create(file.(string), oid.(*git.Oid), false, message)) }).FlatMap(func(value interface{}) result.Result { comment.ID = &id return result.NewSuccess(comment) }) }) }
// Parse a property string and create a person. The expected format is: // ``` // Name <*****@*****.**> // ``` // If a valid person cannot be created, an error is returned instead // @return result.Result<*Person, error> func CreatePerson(properties string) result.Result { fullRe := regexp.MustCompile(`(?:(.*)\s)?<(.*@.*)>(?:\s([0-9]+)\s([\-+][0-9]{4}))?`) match := fullRe.FindStringSubmatch(properties) invalidErr := result.NewFailure(errors.New(invalidPersonError)) if len(match) == 0 { return invalidErr } name, email := match[1], match[2] timestamp := result.NewResult(strconv.ParseInt(match[3], 10, 64)) return timestamp.Analysis(func(value interface{}) result.Result { stamp := time.Unix(value.(int64), 0) person := &Person{match[1], match[2], stamp, match[4]} return result.NewSuccess(person) }, func(err error) result.Result { timestamp := time.Now() return result.NewSuccess(&Person{name, email, timestamp, timestamp.Format("-0700")}) }) }
// Converts an error into a failure result, // or nil into a success result func BoolResult(value bool, err error) result.Result { if err != nil { return result.NewFailure(err) } return result.NewSuccess(value) }
func resolveCommit(repo *git.Repository, object git.Object) result.Result { if object != nil && object.Type() == git.ObjectCommit { return result.NewResult(repo.LookupCommit(object.Id())) } return result.NewFailure(errors.New(noCommitError)) }