зеркало из https://github.com/mislav/hub.git
Implement support for `core.commentchar=auto`
117ddefdb4/builtin/commit.c (L633)
This commit is contained in:
Родитель
04e43fa22f
Коммит
6da6cf59ff
19
git/git.go
19
git/git.go
|
@ -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
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче