Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reader/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
)

func StringToModel(message string, signature Signature, filesCount int) GitCommit {
func StringToModel(message string, signature Signature, filesCount int, isMerge bool) GitCommit {
var res = strings.SplitN(message, "\n", 2)
if len(res) == 1 {
return GitCommit{Title: message, Description: "", Signature: signature, RawMessage: message}
Expand Down
4 changes: 2 additions & 2 deletions reader/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ and continues

and continues
`
commit := StringToModel(message, Signature{}, 3)
commit := StringToModel(message, Signature{}, 3, false)
assert.Equal(t, len(strings.Split(commit.Title, "\n")), 1)
assert.Equal(t, len(strings.Split(commit.Description, "\n")), 13)
assert.Equal(t, commit.FilesCount, 3)
Expand All @@ -36,7 +36,7 @@ body continues
and continues
and continues
`
commit := StringToModel(message, Signature{}, 0)
commit := StringToModel(message, Signature{}, 0, false)
assert.Equal(t, len(strings.Split(commit.Title, "\n")), 1)
assert.Equal(t, len(strings.Split(commit.Description, "\n")), 7)
}
Expand Down
2 changes: 1 addition & 1 deletion reader/direct_input_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ func ReadDirectInput(directory string) []GitCommit {
rawAuthor := string(out)
commit, _ := ioutil.ReadFile(".git/COMMIT_EDITMSG")
rawCommit := string(commit)
return []GitCommit{StringToModel(rawCommit, GitVarToSignature(rawAuthor), changedFiles)}
return []GitCommit{StringToModel(rawCommit, GitVarToSignature(rawAuthor), changedFiles, false)}
}
6 changes: 5 additions & 1 deletion reader/directory_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func ReadGitCommitsFromDirectory(directory string, scope string) []GitCommit {

var models []GitCommit
iterateOverCommon(r, getHashFromBranchName(r, a), getHashFromBranchName(r, b), func(c *object.Commit) error {
models = append(models, StringToModel(c.Message, Signature{Email: c.Author.Email, Name: c.Author.Name}, countFilesInCommit(c)))
models = append(models, StringToModel(c.Message, Signature{Email: c.Author.Email, Name: c.Author.Name}, countFilesInCommit(c), isMergeCommit(c)))
return nil
})
return models
Expand Down Expand Up @@ -90,3 +90,7 @@ func countFilesInCommit(commit *object.Commit) int {

return count
}

func isMergeCommit(commit *object.Commit) bool {
return len(commit.ParentHashes) > 1
}
1 change: 1 addition & 0 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type GitCommit struct {
RawMessage string
Signature Signature
FilesCount int
IsMerge bool
}

type Signature struct {
Expand Down
2 changes: 2 additions & 0 deletions rules/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ProjectConfig struct {
noTrailingPunctuationInTitle bool
maxFiles int
startsWithVerb bool
forbidMerge bool
}

var k = koanf.New(".")
Expand Down Expand Up @@ -44,6 +45,7 @@ func ReadConfig(directory string) ProjectConfig {
noTrailingPunctuationInTitle: k.Bool("no_trailing_punctuation_in_title"),
maxFiles: k.Int("max_files"),
startsWithVerb: k.Bool("starts_with_verb"),
forbidMerge: k.Bool("forbid_merge"),
}

return config
Expand Down
18 changes: 18 additions & 0 deletions rules/forbid_merges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rules

import "gitlab.com/tbacompany/gitector/reader"

type ContainsMergeCommit struct{}

func ForbidMerges(isMerge bool, isEnabled bool) bool {
return isMerge && isEnabled
}

func ContainsMergeCommitError(commit reader.GitCommit) GitError {
return GitError{
ErrorType: ContainsMergeCommit{},
Title: "Commit is a merge commit",
Description: "Merge commits are disabled please use rebase instead",
Commit: commit,
}
}
4 changes: 4 additions & 0 deletions rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,9 @@ func singleCommit(description reader.GitCommit, config ProjectConfig) []GitError
errors = append(errors, StartsWithBaseVerbError(description.Title, description))
}

if ForbidMerges(description.IsMerge, config.forbidMerge) {
errors = append(errors, ContainsMergeCommitError(description))
}

return errors
}
3 changes: 2 additions & 1 deletion utils/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var defaultConfig = []byte(`
"ticket_regexp": "",
"no_trailing_punctuation_in_title": true,
"max_files": 0,
"starts_with_verb": true
"starts_with_verb": true,
"forbid_merge": false

}
`)
Expand Down