Skip to content

Commit 6e44808

Browse files
Fix issue with DiffIndex on initial commit (#11677)
Unfortunately #11614 introduced a bug whereby the initial commit of a repository could not be seen due to there being no parent commit to create a clear diff from. Here we create a diffstat from the difference between the parentless SHA and the SHA of the empty tree - a constant known to git. (With thanks to @L0veSunshine for informing me of this SHA) Thanks to @a1012112796 for initial attempt to fix. Fix #11650 Closes #11674 Signed-off-by: Andrew Thornton <[email protected]> Co-Authored-By: L0veSunshine <[email protected]>
1 parent 141d52c commit 6e44808

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

modules/git/sha1.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
// EmptySHA defines empty git SHA
1818
const EmptySHA = "0000000000000000000000000000000000000000"
1919

20+
// EmptyTreeSHA is the SHA of an empty tree
21+
const EmptyTreeSHA = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
22+
2023
// SHAPattern can be used to determine if a string is an valid sha
2124
var SHAPattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
2225

services/gitdiff/gitdiff.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
664664
ctx, cancel := context.WithCancel(git.DefaultContext)
665665
defer cancel()
666666
var cmd *exec.Cmd
667-
if len(beforeCommitID) == 0 && commit.ParentCount() == 0 {
667+
if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
668668
cmd = exec.CommandContext(ctx, git.GitExecutable, "show", afterCommitID)
669669
} else {
670670
actualBeforeCommitID := beforeCommitID
@@ -711,7 +711,11 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
711711
return nil, fmt.Errorf("Wait: %v", err)
712712
}
713713

714-
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+"..."+afterCommitID)
714+
shortstatArgs := []string{beforeCommitID + "..." + afterCommitID}
715+
if len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA {
716+
shortstatArgs = []string{git.EmptyTreeSHA, afterCommitID}
717+
}
718+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...)
715719
if err != nil {
716720
return nil, err
717721
}

0 commit comments

Comments
 (0)