Skip to content

Commit 960d8d6

Browse files
Following a clever trick shown to me by @L0veSunshine use the SHA of an empty Tree
See go-gitea#11674 (comment) Co-Authored-By: L0veSunshine <xuan199651@gmail.com> Signed-off-by: Andrew Thornton <art27@cantab.net>
1 parent 51caecd commit 960d8d6

File tree

4 files changed

+9
-17
lines changed

4 files changed

+9
-17
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(nil, repo.Path, base+"..."+head)
125+
return GetDiffShortStat(repo.Path, base+"..."+head)
126126
}
127127

128128
// GetDiffShortStat counts number of changed files, number of additions and deletions
129-
func GetDiffShortStat(env []string, repoPath string, args ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
129+
func GetDiffShortStat(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(env []string, repoPath string, args ...string) (numFiles,
136136
"--shortstat",
137137
}, args...)
138138

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

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

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

services/gitdiff/gitdiff.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"net/url"
1818
"os"
1919
"os/exec"
20-
"path/filepath"
2120
"sort"
2221
"strconv"
2322
"strings"
@@ -713,20 +712,10 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
713712
}
714713

715714
shortstatArgs := []string{beforeCommitID + "..." + afterCommitID}
716-
var env []string
717715
if len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA {
718-
shortstatArgs = []string{"--cached", "-R", 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-
716+
shortstatArgs = []string{git.EmptyTreeSHA, afterCommitID}
728717
}
729-
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(env, repoPath, shortstatArgs...)
718+
diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...)
730719
if err != nil {
731720
return nil, err
732721
}

0 commit comments

Comments
 (0)