Implement support for `core.commentchar=auto`

117ddefdb4/builtin/commit.c (L633)
This commit is contained in:
Mislav Marohnić 2017-07-31 18:09:20 +02:00
Родитель 04e43fa22f
Коммит 6da6cf59ff
2 изменённых файлов: 46 добавлений и 3 удалений

Просмотреть файл

@ -195,10 +195,23 @@ func (r *Range) IsAncestor() bool {
func CommentChar(text string) (string, error) {
char, err := Config("core.commentchar")
if err != nil {
char = "#"
return "#", nil
} else if char == "auto" {
lines := strings.Split(text, "\n")
commentCharCandidates := strings.Split("#;@!$%^&|:", "")
candidateLoop:
for _, candidate := range commentCharCandidates {
for _, line := range lines {
if strings.HasPrefix(line, candidate) {
continue candidateLoop
}
}
return candidate, nil
}
return "", fmt.Errorf("unable to select a comment character that is not used in the current message")
} else {
return char, nil
}
}
func Show(sha string) (string, error) {

Просмотреть файл

@ -154,3 +154,33 @@ func TestRemotes(t *testing.T) {
}
}
}
func TestCommentChar(t *testing.T) {
repo := fixtures.SetupTestRepo()
defer repo.TearDown()
char, err := CommentChar("")
assert.Equal(t, nil, err)
assert.Equal(t, "#", char)
SetGlobalConfig("core.commentchar", ";")
char, err = CommentChar("")
assert.Equal(t, nil, err)
assert.Equal(t, ";", char)
SetGlobalConfig("core.commentchar", "auto")
char, err = CommentChar("")
assert.Equal(t, nil, err)
assert.Equal(t, "#", char)
char, err = CommentChar("hello\n#nice\nworld")
assert.Equal(t, nil, err)
assert.Equal(t, ";", char)
char, err = CommentChar("hello\n#nice\n;world")
assert.Equal(t, nil, err)
assert.Equal(t, "@", char)
char, err = CommentChar("#\n;\n@\n!\n$\n%\n^\n&\n|\n:")
assert.Equal(t, "unable to select a comment character that is not used in the current message", err.Error())
}