Skip to content

bug: fix the wrong different view for first commit #11674

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 38 additions & 27 deletions services/gitdiff/gitdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"html"
"html/template"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
Expand Down Expand Up @@ -413,13 +412,14 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
}

leftLine, rightLine int
lineCount int
curFileLinesCount int
curFileLFSPrefix bool
)

input := bufio.NewReader(reader)
isEOF := false
diff.NumFiles = 0

for !isEOF {
var linebuf bytes.Buffer
for {
Expand All @@ -437,7 +437,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
}
if linebuf.Len() < maxLineCharacters {
linebuf.WriteByte(b)
} else if linebuf.Len() == maxLineCharacters {
} else if linebuf.Len() == maxLineCharacters && curFile != nil {
curFile.IsIncomplete = true
}
}
Expand All @@ -449,11 +449,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D

trimLine := strings.Trim(line, "+- ")

if trimLine == models.LFSMetaFileIdentifier {
if trimLine == models.LFSMetaFileIdentifier && curFile != nil {
curFileLFSPrefix = true
}

if curFileLFSPrefix && strings.HasPrefix(trimLine, models.LFSMetaFileOidPrefix) {
if curFileLFSPrefix && strings.HasPrefix(trimLine, models.LFSMetaFileOidPrefix) && curFile != nil {
oid := strings.TrimPrefix(trimLine, models.LFSMetaFileOidPrefix)

if len(oid) == 64 {
Expand All @@ -469,20 +469,25 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
}

curFileLinesCount++
lineCount++

// Diff data too large, we only show the first about maxLines lines
if curFileLinesCount >= maxLines {
if curFileLinesCount >= maxLines && curFile != nil {
curFile.IsIncomplete = true
}
switch {
case line[0] == ' ':
if curFile == nil {
continue
}
diffLine := &DiffLine{Type: DiffLinePlain, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
leftLine++
rightLine++
curSection.Lines = append(curSection.Lines, diffLine)
continue
case line[0] == '@':
if curFile == nil {
continue
}
curSection = &DiffSection{}
curFile.Sections = append(curFile.Sections, curSection)
lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1)
Expand All @@ -497,36 +502,35 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
rightLine = lineSectionInfo.RightIdx
continue
case line[0] == '+':
curFile.Addition++
diff.TotalAddition++
if curFile == nil {
continue
}
curFile.Addition++
diffLine := &DiffLine{Type: DiffLineAdd, Content: line, RightIdx: rightLine}
rightLine++
curSection.Lines = append(curSection.Lines, diffLine)
continue
case line[0] == '-':
curFile.Deletion++
diff.TotalDeletion++
if curFile == nil {
continue
}
curFile.Deletion++
diffLine := &DiffLine{Type: DiffLineDel, Content: line, LeftIdx: leftLine}
if leftLine > 0 {
leftLine++
}
curSection.Lines = append(curSection.Lines, diffLine)
case strings.HasPrefix(line, "Binary"):
curFile.IsBin = true
continue
if curFile != nil {
curFile.IsBin = true
continue
}
}

// Get new file.
if strings.HasPrefix(line, cmdDiffHead) {
if len(diff.Files) >= maxFiles {
diff.IsIncomplete = true
_, err := io.Copy(ioutil.Discard, reader)
if err != nil {
return nil, fmt.Errorf("Copy: %v", err)
}
break
}

var middle int

// Note: In case file name is surrounded by double quotes (it happens only in git-shell).
Expand Down Expand Up @@ -562,6 +566,12 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D

}

if diff.NumFiles > maxFiles {
diff.NumFiles++
curFile = nil
continue
}

curFile = &DiffFile{
Name: b,
OldName: a,
Expand All @@ -571,6 +581,7 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
IsRenamed: a != b,
}
diff.Files = append(diff.Files, curFile)
diff.NumFiles++
curFileLinesCount = 0
leftLine = 1
rightLine = 1
Expand Down Expand Up @@ -634,7 +645,11 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
}
}
}
diff.NumFiles = len(diff.Files)

if diff.NumFiles > maxFiles {
diff.IsIncomplete = true
}

return diff, nil
}

Expand All @@ -646,6 +661,7 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL
}

// GetDiffRangeWithWhitespaceBehavior builds a Diff between two commits of a repository.
// if beforeCommitID is empty, get afterCommit diff by 'git show afterCommitID'
// Passing the empty string as beforeCommitID returns a diff from the parent commit.
// The whitespaceBehavior is either an empty string or a git flag
func GetDiffRangeWithWhitespaceBehavior(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int, whitespaceBehavior string) (*Diff, error) {
Expand All @@ -664,7 +680,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 (beforeCommitID == "" || beforeCommitID == git.EmptySHA) && commit.ParentCount() == 0 {
cmd = exec.CommandContext(ctx, git.GitExecutable, "show", afterCommitID)
} else {
actualBeforeCommitID := beforeCommitID
Expand Down Expand Up @@ -711,11 +727,6 @@ 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 err != nil {
return nil, err
}

return diff, nil
}

Expand Down
59 changes: 59 additions & 0 deletions services/gitdiff/gitdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,65 @@ func TestParsePatch(t *testing.T) {
t.Errorf("ParsePatch failed: %s", err)
}
println(result)

// Test diff numbers
var diff4 = `diff --git a/README.md b/README.md
--- a/README.md
+++ b/README.md
@@ -1,3 +1,6 @@
# gitea-github-migrator
+
+ Build Status
- Latest Release
Docker Pulls
+ cut off
+ cut off
diff --git a/README1.md b/README1.md
--- a/README1.md
+++ b/README1.md
@@ -1,3 +1,6 @@
# gitea-github-migrator
+
+ Build Status
- Latest Release
Docker Pulls
+ cut off
+ cut off
diff --git a/README2.md b/README2.md
--- a/README2.md
+++ b/README2.md
@@ -1,3 +1,6 @@
# gitea-github-migrator
+
+ Build Status
- Latest Release
Docker Pulls
+ cut off
+ cut off`

result, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(diff4))
if err != nil {
t.Errorf("ParsePatch failed: %s", err)
}
println(result)

result, err = ParsePatch(2, setting.Git.MaxGitDiffLineCharacters, 1, strings.NewReader(diff4))
if err != nil {
t.Errorf("ParsePatch failed: %s", err)
}
println(result)

if result.NumFiles != 3 {
t.Errorf("result.NumFiles is not right")
}

if result.Files[0].Addition != 4 {
t.Errorf("result.Files[0].Addition is not right")
}

if result.Files[0].Deletion != 1 {
t.Errorf("result.Files[0].Deletion is not right")
}
}

func setupDefaultDiff() *Diff {
Expand Down