From 8f8ca4e51027073555f86d4281f51877fc3104b6 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 10 Jul 2025 21:22:08 -0700 Subject: [PATCH 1/5] Fix a bug that the code diff hunk missing one at --- services/gitdiff/gitdiff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 9964329876544..555cce38285e3 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -290,7 +290,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine, loc // try to find equivalent diff line. ignore, otherwise switch diffLine.Type { case DiffLineSection: - return getLineContent(diffLine.Content[1:], locale) + return getLineContent(diffLine.Content, locale) case DiffLineAdd: compareDiffLine := diffSection.GetLine(diffLine.Match) return diffSection.getDiffLineForRender(DiffLineAdd, compareDiffLine, diffLine, locale) From 806f915b3c8dfdf14c4a1f3255d10aa5b3846bdd Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 12 Jul 2025 00:24:00 +0800 Subject: [PATCH 2/5] fix getDiffLineSectionInfo --- modules/git/diff.go | 21 ++++++++++++++------- services/gitdiff/gitdiff.go | 5 +++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/modules/git/diff.go b/modules/git/diff.go index c4df6b80633e1..5ec21761ed755 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -99,9 +99,9 @@ func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diff return nil } -// ParseDiffHunkString parse the diffhunk content and return -func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHunk int) { - ss := strings.Split(diffhunk, "@@") +// ParseDiffHunkString parse the diff hunk content and return +func ParseDiffHunkString(diffHunk string) (leftLine, leftHunk, rightLine, rightHunk int) { + ss := strings.Split(diffHunk, "@@") ranges := strings.Split(ss[1][1:], " ") leftRange := strings.Split(ranges[0], ",") leftLine, _ = strconv.Atoi(leftRange[0][1:]) @@ -112,14 +112,21 @@ func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHu rightRange := strings.Split(ranges[1], ",") rightLine, _ = strconv.Atoi(rightRange[0]) if len(rightRange) > 1 { - righHunk, _ = strconv.Atoi(rightRange[1]) + rightHunk, _ = strconv.Atoi(rightRange[1]) } } else { - log.Debug("Parse line number failed: %v", diffhunk) + log.Debug("Parse line number failed: %v", diffHunk) rightLine = leftLine - righHunk = leftHunk + rightHunk = leftHunk } - return leftLine, leftHunk, rightLine, righHunk + if rightLine == 0 { + // "git diff" outputs 2 different formats for the same change "OLD" => "A\nB\nC" + // * "@@ -1 +1,3 @@": the expected result + // * "@@ -1,1 +0,4 @@": the "0" means "insert before the first line" + rightLine++ + rightHunk-- + } + return leftLine, leftHunk, rightLine, rightHunk } // Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9] diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 555cce38285e3..0b6e6be82b778 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -179,7 +179,7 @@ func (d *DiffLine) GetExpandDirection() DiffLineExpandDirection { } func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int) *DiffLineSectionInfo { - leftLine, leftHunk, rightLine, righHunk := git.ParseDiffHunkString(line) + leftLine, leftHunk, rightLine, rightHunk := git.ParseDiffHunkString(line) return &DiffLineSectionInfo{ Path: treePath, @@ -188,7 +188,7 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int LeftIdx: leftLine, RightIdx: rightLine, LeftHunkSize: leftHunk, - RightHunkSize: righHunk, + RightHunkSize: rightHunk, } } @@ -856,6 +856,7 @@ func parseHunks(ctx context.Context, curFile *DiffFile, maxLines, maxLineCharact lastLeftIdx = -1 curFile.Sections = append(curFile.Sections, curSection) + // FIXME: the "-1" can't be right, these "line idx" are all 1-based, maybe there are other bugs that covers this bug. lineSectionInfo := getDiffLineSectionInfo(curFile.Name, line, leftLine-1, rightLine-1) diffLine := &DiffLine{ Type: DiffLineSection, From fdac8bdd17ff4504a0908666b7df99ddd8726cb3 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 12 Jul 2025 00:33:36 +0800 Subject: [PATCH 3/5] fix github migration asset ID nil panic --- services/migrations/github.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/migrations/github.go b/services/migrations/github.go index 2ce11615c6dc8..c6cd6ea173336 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -322,7 +322,10 @@ func (g *GithubDownloaderV3) convertGithubRelease(ctx context.Context, rel *gith httpClient := NewMigrationHTTPClient() for _, asset := range rel.Assets { - assetID := *asset.ID // Don't optimize this, for closure we need a local variable + assetID := asset.GetID() // Don't optimize this, for closure we need a local variable TODO: no need to do so in new Golang + if assetID == 0 { + continue + } r.Assets = append(r.Assets, &base.ReleaseAsset{ ID: asset.GetID(), Name: asset.GetName(), From 76118813ba00dd92ae234807a38a0461cf0f867d Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 12 Jul 2025 00:40:36 +0800 Subject: [PATCH 4/5] correct --- modules/git/diff.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/git/diff.go b/modules/git/diff.go index 5ec21761ed755..d4a3902727dc4 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -120,11 +120,9 @@ func ParseDiffHunkString(diffHunk string) (leftLine, leftHunk, rightLine, rightH rightHunk = leftHunk } if rightLine == 0 { - // "git diff" outputs 2 different formats for the same change "OLD" => "A\nB\nC" - // * "@@ -1 +1,3 @@": the expected result - // * "@@ -1,1 +0,4 @@": the "0" means "insert before the first line" + // FIXME: GIT-DIFF-CUT-BUG search this tag to see details + // this is only a hacky patch, the rightLine&rightHunk might still be incorrect in some cases. rightLine++ - rightHunk-- } return leftLine, leftHunk, rightLine, rightHunk } @@ -277,6 +275,11 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi oldNumOfLines++ } } + + // "git diff" outputs "@@ -1 +1,3 @@" for "OLD" => "A\nB\nC" + // FIXME: GIT-DIFF-CUT-BUG But there is a bug in CutDiffAroundLine, then the "Patch" stored in the comment model becomes "@@ -1,1 +0,4 @@" + // It may generate incorrect results for difference cases, for example: delete 2 line add 1 line, delete 2 line add 2 line etc, need to double check. + // construct the new hunk header newHunk[headerLines] = fmt.Sprintf("@@ -%d,%d +%d,%d @@", oldBegin, oldNumOfLines, newBegin, newNumOfLines) From d11faa53bb3eb6109d052f686e5304c380f6461a Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sat, 12 Jul 2025 01:24:51 +0800 Subject: [PATCH 5/5] more bug comment --- modules/git/diff.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/git/diff.go b/modules/git/diff.go index d4a3902727dc4..35d115be0e5da 100644 --- a/modules/git/diff.go +++ b/modules/git/diff.go @@ -279,6 +279,7 @@ func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLi // "git diff" outputs "@@ -1 +1,3 @@" for "OLD" => "A\nB\nC" // FIXME: GIT-DIFF-CUT-BUG But there is a bug in CutDiffAroundLine, then the "Patch" stored in the comment model becomes "@@ -1,1 +0,4 @@" // It may generate incorrect results for difference cases, for example: delete 2 line add 1 line, delete 2 line add 2 line etc, need to double check. + // For example: "L1\nL2" => "A\nB", then the patch shows "L2" as line 1 on the left (deleted part) // construct the new hunk header newHunk[headerLines] = fmt.Sprintf("@@ -%d,%d +%d,%d @@",