Skip to content

Commit 9ef8e04

Browse files
committed
commit: Make the message deterministic
It depended on the order of the directory traversal.
1 parent 5a00c2b commit 9ef8e04

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

common/commit.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package common
33
import (
44
"bytes"
55
"os/exec"
6+
"sort"
67
"strings"
78

89
"github.com/ztrue/tracerr"
@@ -26,7 +27,7 @@ func commit(repoPath string) error {
2627
}
2728

2829
hasChanges := false
29-
commitMsg := ""
30+
commitMsg := []string{}
3031
for filePath, fileStatus := range status {
3132
if fileStatus.Worktree == git.Unmodified && fileStatus.Staging == git.Unmodified {
3233
continue
@@ -47,19 +48,24 @@ func commit(repoPath string) error {
4748
return tracerr.Wrap(err)
4849
}
4950

51+
msg := ""
5052
if fileStatus.Worktree == git.Untracked && fileStatus.Staging == git.Untracked {
51-
commitMsg += "?? "
53+
msg += "?? "
5254
} else {
53-
commitMsg += " " + string(fileStatus.Worktree) + " "
55+
msg += " " + string(fileStatus.Worktree) + " "
5456
}
55-
commitMsg += filePath + "\n"
57+
msg += filePath
58+
commitMsg = append(commitMsg, msg)
5659
}
5760

61+
sort.Strings(commitMsg)
62+
msg := strings.Join(commitMsg, "\n")
63+
5864
if !hasChanges {
5965
return nil
6066
}
6167

62-
_, err = GitCommand(repoPath, []string{"commit", "-m", commitMsg})
68+
_, err = GitCommand(repoPath, []string{"commit", "-m", msg})
6369
if err != nil {
6470
return tracerr.Wrap(err)
6571
}

common/commit_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,8 @@ func Test_MultipleFileChange(t *testing.T) {
9999
err := commit(repoPath)
100100
assert.NilError(t, err)
101101

102-
HasHeadCommit(t, repoPath, "7058b6b292ee3d1382670334b5f29570a1117ef1", " M 1.md\n D dirA/2.md\n?? dirB/3.md\n")
102+
HasHeadCommit(t, repoPath, "7058b6b292ee3d1382670334b5f29570a1117ef1", ` D dirA/2.md
103+
M 1.md
104+
?? dirB/3.md
105+
`)
103106
}

0 commit comments

Comments
 (0)