Skip to content

Commit cb663ca

Browse files
committed
Fix issue with DiffIndex on initial commit
Unfortunately go-gitea#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 ensure that the index is empty by referring to a non-existent file and the create a diffstat from the difference between it and the parentless SHA. Fix go-gitea#11650 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 0d9f9f7 commit cb663ca

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

modules/git/repo_compare.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ func (repo *Repository) GetDiffNumChangedFiles(base, head string) (int, error) {
122122

123123
// GetDiffShortStat counts number of changed files, number of additions and deletions
124124
func (repo *Repository) GetDiffShortStat(base, head string) (numFiles, totalAdditions, totalDeletions int, err error) {
125-
return GetDiffShortStat(repo.Path, base+"..."+head)
125+
return GetDiffShortStat(nil, repo.Path, base+"..."+head)
126126
}
127127

128128
// GetDiffShortStat counts number of changed files, number of additions and deletions
129-
func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
129+
func GetDiffShortStat(env []string, repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
130130
// Now if we call:
131131
// $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
132132
// we get:
@@ -136,7 +136,7 @@ func GetDiffShortStat(repoPath string, args ...string) (numFiles, totalAdditions
136136
"--shortstat",
137137
}, args...)
138138

139-
stdout, err := NewCommand(args...).RunInDir(repoPath)
139+
stdout, err := NewCommand(args...).RunInDirWithEnv(repoPath, env)
140140
if err != nil {
141141
return 0, 0, 0, err
142142
}

modules/repofiles/temp_repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (*gitdiff.Diff, error) {
299299
t.repo.FullName(), err, stderr)
300300
}
301301

302-
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(t.basePath, "--cached", "HEAD")
302+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(nil, t.basePath, "--cached", "HEAD")
303303
if err != nil {
304304
return nil, err
305305
}

services/gitdiff/gitdiff.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"net/url"
1818
"os"
1919
"os/exec"
20+
"path/filepath"
2021
"sort"
2122
"strconv"
2223
"strings"
@@ -664,7 +665,7 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
664665
ctx, cancel := context.WithCancel(git.DefaultContext)
665666
defer cancel()
666667
var cmd *exec.Cmd
667-
if len(beforeCommitID) == 0 && commit.ParentCount() == 0 {
668+
if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
668669
cmd = exec.CommandContext(ctx, git.GitExecutable, "show", afterCommitID)
669670
} else {
670671
actualBeforeCommitID := beforeCommitID
@@ -711,7 +712,21 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
711712
return nil, fmt.Errorf("Wait: %v", err)
712713
}
713714

714-
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+"..."+afterCommitID)
715+
shortstatArgs := []string{beforeCommitID + "..." + afterCommitID}
716+
var env []string
717+
if len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA {
718+
shortstatArgs = []string{"--cached", afterCommitID}
719+
tmpDir, err := ioutil.TempDir("", "gitdiff")
720+
if err != nil {
721+
return nil, err
722+
}
723+
defer func() {
724+
_ = os.RemoveAll(tmpDir)
725+
}()
726+
env = append(os.Environ(), "GIT_INDEX_FILE="+filepath.Join(tmpDir, "non-existent-index"))
727+
728+
}
729+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(env, repoPath, shortstatArgs...)
715730
if err != nil {
716731
return nil, err
717732
}

0 commit comments

Comments
 (0)