Skip to content

Add .diff and .patch support to compare #34433

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

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
8d799c2
use the correct context data for PR link template in issue card
badhezi Apr 15, 2025
f2a2acf
Merge branch 'main' into main
badhezi Apr 15, 2025
5cc1bda
Merge branch 'main' into main
GiteaBot Apr 16, 2025
54d37d1
Merge branch 'main' into main
GiteaBot Apr 16, 2025
ac25150
Merge branch 'go-gitea:main' into main
badhezi Apr 16, 2025
e11a339
Merge branch 'go-gitea:main' into main
badhezi Apr 20, 2025
104eecc
Merge branch 'go-gitea:main' into main
badhezi Apr 21, 2025
bcc4ade
Merge branch 'go-gitea:main' into main
badhezi Apr 22, 2025
a7aaa79
Merge branch 'go-gitea:main' into main
badhezi Apr 27, 2025
b46d314
Merge branch 'go-gitea:main' into main
badhezi Apr 28, 2025
4e2434b
Merge branch 'go-gitea:main' into main
badhezi Apr 29, 2025
7f72fe9
Merge branch 'go-gitea:main' into main
badhezi May 2, 2025
c6acfc1
Merge branch 'go-gitea:main' into main
badhezi May 11, 2025
016c2f3
Merge branch 'go-gitea:main' into main
badhezi May 12, 2025
a3c2953
Merge branch 'go-gitea:main' into main
badhezi May 13, 2025
4d7ea0d
Merge branch 'go-gitea:main' into main
badhezi May 20, 2025
bfa2d10
Merge branch 'go-gitea:main' into main
badhezi May 28, 2025
f86ddd5
parse .raw and .diff optional compare parameters
badhezi May 12, 2025
acb57e5
lint, err handle
badhezi May 12, 2025
180c1b0
integration tests: WIP
badhezi May 12, 2025
2bcb546
Add tests and RevParse() function
badhezi May 13, 2025
a9c8ce9
formatting
badhezi May 13, 2025
17c860e
fix timezone adjustment in TestCompareRawDiffPatch
badhezi May 14, 2025
3274b84
support tag and branch names with ends with .diff and .patch
badhezi May 14, 2025
d784a0f
add download links to raw diff and patch in diff box options dropdown
badhezi May 14, 2025
c80f0b9
fix lint and typos
badhezi May 14, 2025
e9fac73
cover all head ref format cases
badhezi May 29, 2025
2905d35
add more test cases to cover different compare patterns
badhezi May 30, 2025
688fc78
move revParse to testing code only
badhezi May 30, 2025
b9102f5
remove redundant line
badhezi May 30, 2025
f4a0fe3
Merge branch 'main' into dev/hezi/add-raw-diff-patch
badhezi May 31, 2025
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
1 change: 1 addition & 0 deletions routers/common/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ type CompareInfo struct {
BaseBranch string
HeadBranch string
DirectComparison bool
RawDiffType git.RawDiffType
}
43 changes: 40 additions & 3 deletions routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
)

infoPath = ctx.PathParam("*")

var infos []string

if infoPath == "" {
infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch}
} else {
Expand All @@ -252,7 +254,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
if len(headInfos) == 1 {
isSameRepo = true
ci.HeadUser = ctx.Repo.Owner
ci.HeadBranch = headInfos[0]
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0])
} else if len(headInfos) == 2 {
headInfosSplit := strings.Split(headInfos[0], "/")
if len(headInfosSplit) == 1 {
Expand All @@ -265,7 +267,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
}
return nil
}
ci.HeadBranch = headInfos[1]
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID
if isSameRepo {
ci.HeadRepo = baseRepo
Expand All @@ -288,14 +290,15 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
}
return nil
}
ci.HeadBranch = headInfos[1]
ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1])
ci.HeadUser = ci.HeadRepo.Owner
isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID
}
} else {
ctx.NotFound(nil)
return nil
}

ctx.Data["HeadUser"] = ci.HeadUser
ctx.Data["HeadBranch"] = ci.HeadBranch
ctx.Repo.PullRequest.SameRepo = isSameRepo
Expand Down Expand Up @@ -729,6 +732,7 @@ func CompareDiff(ctx *context.Context) {
return
}

ctx.Data["PageIsCompareDiff"] = true
ctx.Data["PullRequestWorkInProgressPrefixes"] = setting.Repository.PullRequest.WorkInProgressPrefixes
ctx.Data["DirectComparison"] = ci.DirectComparison
ctx.Data["OtherCompareSeparator"] = ".."
Expand All @@ -743,6 +747,16 @@ func CompareDiff(ctx *context.Context) {
return
}

if ci.RawDiffType != "" {
err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, ci.RawDiffType, "", ctx.Resp)
if err != nil {
ctx.ServerError("GetRepoRawDiffForFile", err)
return
}
ctx.Resp.Flush()
return
}

baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
if err != nil {
ctx.ServerError("GetTagNamesByRepoID", err)
Expand Down Expand Up @@ -984,3 +998,26 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
}
return diffLines, nil
}

func parseRefForRawDiff(ctx *context.Context, ci *common.CompareInfo, ref string) string {
if strings.HasSuffix(ref, ".diff") || strings.HasSuffix(ref, ".patch") {
var headRepo *repo_model.Repository
if ci.HeadRepo != nil {
headRepo = ci.HeadRepo
} else {
headRepo = ctx.Repo.Repository
}
ref2IsBranch := gitrepo.IsBranchExist(ctx, headRepo, ref)
ref2IsTag := gitrepo.IsTagExist(ctx, headRepo, ref)
if !ref2IsBranch && !ref2IsTag {
if strings.HasSuffix(ref, ".diff") {
ci.RawDiffType = git.RawDiffNormal
ref = strings.TrimSuffix(ref, ".diff")
} else if strings.HasSuffix(ref, ".patch") {
ci.RawDiffType = git.RawDiffPatch
ref = strings.TrimSuffix(ref, ".patch")
}
}
}
return ref
}
3 changes: 3 additions & 0 deletions templates/repo/diff/options_dropdown.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
{{else if .Commit.ID.String}}
<a class="item" href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}.patch" download="{{ShortSha .Commit.ID.String}}.patch">{{ctx.Locale.Tr "repo.diff.download_patch"}}</a>
<a class="item" href="{{$.RepoLink}}/commit/{{PathEscape .Commit.ID.String}}.diff" download="{{ShortSha .Commit.ID.String}}.diff">{{ctx.Locale.Tr "repo.diff.download_diff"}}</a>
{{else if $.PageIsCompareDiff}}
<a class="item" href="{{$.Link}}.patch" download="{{$.BaseBranch}}...{{$.HeadBranch}}.patch">{{ctx.Locale.Tr "repo.diff.download_patch"}}</a>
<a class="item" href="{{$.Link}}.diff" download="{{$.BaseBranch}}...{{$.HeadBranch}}.diff">{{ctx.Locale.Tr "repo.diff.download_diff"}}</a>
{{end}}
<a id="expand-files-btn" class="item">{{ctx.Locale.Tr "repo.pulls.expand_files"}}</a>
<a id="collapse-files-btn" class="item">{{ctx.Locale.Tr "repo.pulls.collapse_files"}}</a>
Expand Down
212 changes: 212 additions & 0 deletions tests/integration/compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import (
"net/url"
"strings"
"testing"
"time"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
git_module "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/test"
repo_service "code.gitea.io/gitea/services/repository"
"code.gitea.io/gitea/tests"
Expand Down Expand Up @@ -158,3 +161,212 @@ func TestCompareCodeExpand(t *testing.T) {
}
})
}

func TestCompareRawDiffNormal(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
Name: "test_raw_diff",
Readme: "Default",
AutoInit: true,
DefaultBranch: "main",
}, true)
assert.NoError(t, err)
session := loginUser(t, user1.Name)

r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)

oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md")

testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 2))

newRef, _ := r.GetBranchCommit(repo.DefaultBranch)
newBlobRef, _ := revParse(r, newRef.ID.String(), "README.md")

req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s.diff", oldRef.ID.String(), newRef.ID.String()))
resp := session.MakeRequest(t, req, http.StatusOK)

expected := fmt.Sprintf(`diff --git a/README.md b/README.md
index %s..%s 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# test_raw_diff
-
+a
+a
`, oldBlobRef[:7], newBlobRef[:7])
assert.Equal(t, expected, resp.Body.String())
})
}

func TestCompareRawDiffPatch(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
Name: "test_raw_diff",
Readme: "Default",
AutoInit: true,
DefaultBranch: "main",
}, true)
assert.NoError(t, err)
session := loginUser(t, user1.Name)

r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)

// Get the old commit and blob reference
oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md")

resp := testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 2))

newRef, _ := r.GetBranchCommit(repo.DefaultBranch)
newBlobRef, _ := revParse(r, newRef.ID.String(), "README.md")

// Get the last modified time from the response header
respTs, _ := time.Parse(time.RFC1123, resp.Result().Header.Get("Last-Modified"))
respTs = respTs.In(time.Local)

// Format the timestamp to match the expected format in the patch
customFormat := "Mon, 02 Jan 2006 15:04:05 -0700"
respTsStr := respTs.Format(customFormat)

req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s.patch", oldRef.ID.String(), newRef.ID.String()))
resp = session.MakeRequest(t, req, http.StatusOK)

expected := fmt.Sprintf(`From %s Mon Sep 17 00:00:00 2001
From: User One <[email protected]>
Date: %s
Subject: [PATCH] Update README.md

---
README.md | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index %s..%s 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# test_raw_diff
-
+a
+a
`, newRef.ID.String(), respTsStr, oldBlobRef[:7], newBlobRef[:7])
assert.Equal(t, expected, resp.Body.String())
})
}

func TestCompareRawDiffNormalSameOwnerDifferentRepo(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
Name: "test_raw_diff",
Readme: "Default",
AutoInit: true,
DefaultBranch: "main",
}, true)
assert.NoError(t, err)
session := loginUser(t, user1.Name)

headRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
Name: "test_raw_diff_head",
Readme: "Default",
AutoInit: true,
DefaultBranch: "main",
}, true)
assert.NoError(t, err)

r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)
hr, _ := gitrepo.OpenRepository(db.DefaultContext, headRepo)

oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md")

testEditFile(t, session, user1.Name, headRepo.Name, "main", "README.md", strings.Repeat("a\n", 2))

newRef, _ := hr.GetBranchCommit(headRepo.DefaultBranch)
newBlobRef, _ := revParse(hr, newRef.ID.String(), "README.md")

req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s/%s:%s.diff", oldRef.ID.String(), user1.LowerName, headRepo.LowerName, newRef.ID.String()))
resp := session.MakeRequest(t, req, http.StatusOK)

expected := fmt.Sprintf(`diff --git a/README.md b/README.md
index %s..%s 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# test_raw_diff
-
+a
+a
`, oldBlobRef[:7], newBlobRef[:7])
assert.Equal(t, expected, resp.Body.String())
})
}

func TestCompareRawDiffNormalAcrossForks(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})

repo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{
Name: "test_raw_diff",
Readme: "Default",
AutoInit: true,
DefaultBranch: "main",
}, true)
assert.NoError(t, err)

headRepo, err := repo_service.ForkRepository(db.DefaultContext, user2, user2, repo_service.ForkRepoOptions{
BaseRepo: repo,
Name: repo.Name,
Description: repo.Description,
SingleBranch: "",
})
assert.NoError(t, err)

session := loginUser(t, user2.Name)

r, _ := gitrepo.OpenRepository(db.DefaultContext, repo)
hr, _ := gitrepo.OpenRepository(db.DefaultContext, headRepo)

oldRef, _ := r.GetBranchCommit(repo.DefaultBranch)
oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md")

testEditFile(t, session, user2.Name, headRepo.Name, "main", "README.md", strings.Repeat("a\n", 2))

newRef, _ := hr.GetBranchCommit(headRepo.DefaultBranch)
newBlobRef, _ := revParse(hr, newRef.ID.String(), "README.md")

session = loginUser(t, user1.Name)

req := NewRequest(t, "GET", fmt.Sprintf("/user1/test_raw_diff/compare/%s...%s:%s.diff", oldRef.ID.String(), user2.LowerName, newRef.ID.String()))
resp := session.MakeRequest(t, req, http.StatusOK)

expected := fmt.Sprintf(`diff --git a/README.md b/README.md
index %s..%s 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,2 @@
-# test_raw_diff
-
+a
+a
`, oldBlobRef[:7], newBlobRef[:7])
assert.Equal(t, expected, resp.Body.String())
})
}

// helper function to use rev-parse
// revParse resolves a revision reference to other git-related objects
func revParse(repo *git_module.Repository, ref, file string) (string, error) {
stdout, _, err := git_module.NewCommand("rev-parse").
AddDynamicArguments(ref+":"+file).
RunStdString(repo.Ctx, &git_module.RunOpts{Dir: repo.Path})
if err != nil {
return "", err
}
return strings.TrimSpace(stdout), nil
}