Skip to content

Commit

Permalink
commit: Make the message deterministic
Browse files Browse the repository at this point in the history
It depended on the order of the directory traversal.
  • Loading branch information
vHanda committed May 15, 2022
1 parent 5a00c2b commit 9ef8e04
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
16 changes: 11 additions & 5 deletions common/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"bytes"
"os/exec"
"sort"
"strings"

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

hasChanges := false
commitMsg := ""
commitMsg := []string{}
for filePath, fileStatus := range status {
if fileStatus.Worktree == git.Unmodified && fileStatus.Staging == git.Unmodified {
continue
Expand All @@ -47,19 +48,24 @@ func commit(repoPath string) error {
return tracerr.Wrap(err)
}

msg := ""
if fileStatus.Worktree == git.Untracked && fileStatus.Staging == git.Untracked {
commitMsg += "?? "
msg += "?? "
} else {
commitMsg += " " + string(fileStatus.Worktree) + " "
msg += " " + string(fileStatus.Worktree) + " "
}
commitMsg += filePath + "\n"
msg += filePath
commitMsg = append(commitMsg, msg)
}

sort.Strings(commitMsg)
msg := strings.Join(commitMsg, "\n")

if !hasChanges {
return nil
}

_, err = GitCommand(repoPath, []string{"commit", "-m", commitMsg})
_, err = GitCommand(repoPath, []string{"commit", "-m", msg})
if err != nil {
return tracerr.Wrap(err)
}
Expand Down
5 changes: 4 additions & 1 deletion common/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@ func Test_MultipleFileChange(t *testing.T) {
err := commit(repoPath)
assert.NilError(t, err)

HasHeadCommit(t, repoPath, "7058b6b292ee3d1382670334b5f29570a1117ef1", " M 1.md\n D dirA/2.md\n?? dirB/3.md\n")
HasHeadCommit(t, repoPath, "7058b6b292ee3d1382670334b5f29570a1117ef1", ` D dirA/2.md
M 1.md
?? dirB/3.md
`)
}

0 comments on commit 9ef8e04

Please sign in to comment.