-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: fix the wrong different view for first commit #11674
Conversation
After check, I think ``git.GetDiffShortStat`` is not necessary to be called, because it hase maken right message in ``ParsePatch``. and ``git.GetDiffShortStat`` can't get right message if beforeCommitID is empty, which is the reason for go-gitea#11650 fix go-gitea#11650 Signed-off-by: a1012112796 <1012112796@qq.com>
The results of the diffstat in ParsePatch are wrong - it's limited to 100 files. |
And this solution doesn't fix the proposed issue. Here is the correct solution: diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go
index af88ed4d4..048022e9d 100644
--- a/services/gitdiff/gitdiff.go
+++ b/services/gitdiff/gitdiff.go
@@ -664,7 +664,7 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
ctx, cancel := context.WithCancel(git.DefaultContext)
defer cancel()
var cmd *exec.Cmd
- if len(beforeCommitID) == 0 && commit.ParentCount() == 0 {
+ if (len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
cmd = exec.CommandContext(ctx, git.GitExecutable, "show", afterCommitID)
} else {
actualBeforeCommitID := beforeCommitID
@@ -711,7 +711,11 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
return nil, fmt.Errorf("Wait: %v", err)
}
- diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+"..."+afterCommitID)
+ shortstatArgs := []string{beforeCommitID + "..." + afterCommitID}
+ if len(beforeCommitID) == 0 || beforeCommitID == git.EmptySHA {
+ shortstatArgs = []string{"--cached", afterCommitID}
+ }
+ diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, shortstatArgs...)
if err != nil {
return nil, err
} |
The trick here is the As we are running these commands in a bare repository the index will always be empty. (This is as it should be - however if for some reason the index file is present this command will fail.) An empty index file is assumed to represent an empty index by git diff - going back all the way to 1.7.2 and git diff will not create an index. So ... if you wanted to clean this up and prevent any potential index file from breaking this setting the GIT_INDEX_FILE to a new clean empty - temp file or some file guaranteed to be empty would do that. (/dev/null won't work) (That is as an option to git.GetDifffShortStat(...) |
index af88ed4d4..8223ee697 100644
--- a/services/gitdiff/gitdiff.go
+++ b/services/gitdiff/gitdiff.go
@@ -711,7 +711,12 @@ func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID
return nil, fmt.Errorf("Wait: %v", err)
}
- diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+"..."+afterCommitID)
+ if beforeCommitID == "" {
+ beforeCommitID = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
+ diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+".."+afterCommitID)
+ } else {
+ diff.NumFiles, diff.TotalAddition, diff.TotalDeletion, err = git.GetDiffShortStat(repoPath, beforeCommitID+"..."+afterCommitID)
+ }
if err != nil {
return nil, err
} This issue is propose by me and i had try to fix it. @zeripath @a1012112796 please have a look. Notice: git.GetDiffShortStat(repoPath, beforeCommitID+".."+afterCommitID) |
@L0veSunshine that is not a general solution... |
does my suggested solution not work? |
I am sorry i had not try. The previous pr is yours. I believe you soultion is the best. |
4b825dc642cb6eb9a060e54bf8d69288fbee4904 is a special hash value in git which denotes an empty tree. And it does not change from repo to repo. |
Well I never would have thought of doing that! Let me adjust my PR to use that SHA as it is seriously clever. |
Ah damn git 1.7.2 doesn't allow it I'm afraid |
…an empty Tree See go-gitea#11674 (comment) Co-Authored-By: L0veSunshine <xuan199651@gmail.com> Signed-off-by: Andrew Thornton <art27@cantab.net>
but it does allow it in a different way. |
@a1012112796 would you like a co-author tag on my follow-up PR? |
@zacheryph How about this way? fc51aed |
Using the information from ParsePatch is not going to work - and in any case will disappear as soon as we start causing diffs to be rendered asynchronously - (which is part of the plan for rewriting/refactoring compare) |
Therefore we must use |
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 <art27@cantab.net> Co-Authored-By: L0veSunshine <xuan199651@gmail.com>
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 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 go-gitea#11650 Closes go-gitea#11674 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-Authored-By: L0veSunshine <xuan199651@gmail.com>
After check, I think
git.GetDiffShortStat
is not necessary to be called, because it hase maken right message inParsePatch
, andgit.GetDiffShortStat
can't get right message if beforeCommitID is empty, which is the reason for #11650@zeripath Please have a look. Maybe I forgot something. Thanks.
fix #11650