Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

a1012112796
Copy link
Member

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 #11650

@zeripath Please have a look. Maybe I forgot something. Thanks.

fix #11650

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>
@zeripath
Copy link
Contributor

The results of the diffstat in ParsePatch are wrong - it's limited to 100 files.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label May 29, 2020
@a1012112796 a1012112796 marked this pull request as draft May 29, 2020 10:04
@zeripath
Copy link
Contributor

zeripath commented May 29, 2020

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
 	}

@zeripath
Copy link
Contributor

zeripath commented May 29, 2020

The trick here is the --cached flag.

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(...)

@L0veSunshine
Copy link
Contributor

L0veSunshine commented May 29, 2020

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.
This is my solution. After testing the problem was completely solved.
Maybe you can refer to. And Writing can be simplified.

@zeripath @a1012112796 please have a look.

Notice: git.GetDiffShortStat(repoPath, beforeCommitID+".."+afterCommitID)

@zeripath
Copy link
Contributor

@L0veSunshine that is not a general solution...

@zeripath
Copy link
Contributor

does my suggested solution not work?

@L0veSunshine
Copy link
Contributor

L0veSunshine commented May 29, 2020

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.
I only comment for a new Idea.
Please feel free to refer.
Thanks.

@L0veSunshine
Copy link
Contributor

@L0veSunshine that is not a general solution...

4b825dc642cb6eb9a060e54bf8d69288fbee4904 is a special hash value in git which denotes an empty tree. And it does not change from repo to repo.
I also discovered it from Google 😀

@zeripath
Copy link
Contributor

Well I never would have thought of doing that! Let me adjust my PR to use that SHA as it is seriously clever.

@zeripath
Copy link
Contributor

Ah damn git 1.7.2 doesn't allow it I'm afraid

zeripath added a commit to zeripath/gitea that referenced this pull request May 29, 2020
…an empty Tree

See go-gitea#11674 (comment)

Co-Authored-By: L0veSunshine <xuan199651@gmail.com>
Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath
Copy link
Contributor

but it does allow it in a different way.

@zeripath
Copy link
Contributor

@a1012112796 would you like a co-author tag on my follow-up PR?

@a1012112796
Copy link
Member Author

@zacheryph How about this way? fc51aed

@a1012112796 a1012112796 marked this pull request as ready for review May 29, 2020 17:10
@zeripath
Copy link
Contributor

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)

@zeripath
Copy link
Contributor

Therefore we must use git diff --shortstat

zeripath added a commit that referenced this pull request May 29, 2020
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>
ydelafollye pushed a commit to ydelafollye/gitea that referenced this pull request Jul 31, 2020
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>
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] When view first commit it return "Diff Content Not Available"
4 participants