From 8d799c236c854c8bb132b38c97385beaf53d9327 Mon Sep 17 00:00:00 2001 From: badhezi Date: Tue, 15 Apr 2025 19:56:19 +0300 Subject: [PATCH 01/23] use the correct context data for PR link template in issue card --- templates/repo/issue/card.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/repo/issue/card.tmpl b/templates/repo/issue/card.tmpl index c7bbe91885012..41fe6cea8fbae 100644 --- a/templates/repo/issue/card.tmpl +++ b/templates/repo/issue/card.tmpl @@ -45,7 +45,7 @@ {{if $.Page.LinkedPRs}} {{range index $.Page.LinkedPRs .ID}}
- + {{svg "octicon-git-merge" 16 "tw-mr-1 tw-align-middle"}} {{.Title}} #{{.Index}} From 4e10abdb91a602fa0fa79d330b5c6588fa72aff2 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 8 Oct 2025 11:38:43 +0300 Subject: [PATCH 02/23] skip email notification trigger when run is not in final state --- services/mailer/mail_workflow_run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/mailer/mail_workflow_run.go b/services/mailer/mail_workflow_run.go index 37891028121c6..ed84e621ea4f6 100644 --- a/services/mailer/mail_workflow_run.go +++ b/services/mailer/mail_workflow_run.go @@ -153,7 +153,7 @@ func MailActionsTrigger(ctx context.Context, sender *user_model.User, repo *repo if setting.MailService == nil { return nil } - if !run.Status.IsDone() || run.Status.IsSkipped() { + if !run.Status.IsDone() || run.Status.IsSkipped() || (run.Started == 0 && run.Stopped == 0) { return nil } From c0e333eaa2eefaaf86a0d170527f96420b9eda7b Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 8 Oct 2025 11:55:22 +0300 Subject: [PATCH 03/23] Revert "skip email notification trigger when run is not in final state" This reverts commit 4e10abdb91a602fa0fa79d330b5c6588fa72aff2. --- services/mailer/mail_workflow_run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/mailer/mail_workflow_run.go b/services/mailer/mail_workflow_run.go index ed84e621ea4f6..37891028121c6 100644 --- a/services/mailer/mail_workflow_run.go +++ b/services/mailer/mail_workflow_run.go @@ -153,7 +153,7 @@ func MailActionsTrigger(ctx context.Context, sender *user_model.User, repo *repo if setting.MailService == nil { return nil } - if !run.Status.IsDone() || run.Status.IsSkipped() || (run.Started == 0 && run.Stopped == 0) { + if !run.Status.IsDone() || run.Status.IsSkipped() { return nil } From 11a0410bd8c0507dfc17939fbbf39ddc44ee5369 Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 12 May 2025 16:38:44 +0300 Subject: [PATCH 04/23] parse .raw and .diff optional compare parameters --- routers/api/packages/api.go | 1 - routers/common/compare.go | 1 + routers/web/repo/compare.go | 18 ++++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/routers/api/packages/api.go b/routers/api/packages/api.go index f6ee5958b5bb9..9ad7e5a976845 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -532,7 +532,6 @@ func CommonRoutes() *web.Router { // https://github.com/opencontainers/distribution-spec/blob/main/spec.md func ContainerRoutes() *web.Router { r := web.NewRouter() - r.Use(context.PackageContexter()) verifyAuth(r, []auth.Method{ diff --git a/routers/common/compare.go b/routers/common/compare.go index fda31a07ba736..57edafa55a7d9 100644 --- a/routers/common/compare.go +++ b/routers/common/compare.go @@ -19,4 +19,5 @@ type CompareInfo struct { BaseBranch string HeadBranch string DirectComparison bool + RawDiffType git.RawDiffType } diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 9262703078a6a..dd2fb1b13372b 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -230,7 +230,19 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ) infoPath = ctx.PathParam("*") + var infos []string + + // Handle possible suffixes: .diff or .patch + if strings.HasSuffix(infoPath, ".diff") { + ci.RawDiffType = git.RawDiffNormal + infoPath = strings.TrimSuffix(infoPath, ".diff") + + } else if strings.HasSuffix(infoPath, ".patch") { + ci.RawDiffType = git.RawDiffPatch + infoPath = strings.TrimSuffix(infoPath, ".patch") + } + if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { @@ -746,6 +758,12 @@ func CompareDiff(ctx *context.Context) { return } + if ci.RawDiffType != "" { + git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, ci.RawDiffType, "", ctx.Resp) + ctx.Resp.Flush() + return + } + baseTags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID) if err != nil { ctx.ServerError("GetTagNamesByRepoID", err) From fc40f658da6a322c8d716da9a7f78b49a245f474 Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 12 May 2025 17:07:15 +0300 Subject: [PATCH 05/23] lint, err handle --- routers/api/packages/api.go | 1 + routers/web/repo/compare.go | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/routers/api/packages/api.go b/routers/api/packages/api.go index 9ad7e5a976845..f6ee5958b5bb9 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -532,6 +532,7 @@ func CommonRoutes() *web.Router { // https://github.com/opencontainers/distribution-spec/blob/main/spec.md func ContainerRoutes() *web.Router { r := web.NewRouter() + r.Use(context.PackageContexter()) verifyAuth(r, []auth.Method{ diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index dd2fb1b13372b..f125d7622c550 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -237,7 +237,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if strings.HasSuffix(infoPath, ".diff") { ci.RawDiffType = git.RawDiffNormal infoPath = strings.TrimSuffix(infoPath, ".diff") - } else if strings.HasSuffix(infoPath, ".patch") { ci.RawDiffType = git.RawDiffPatch infoPath = strings.TrimSuffix(infoPath, ".patch") @@ -759,7 +758,11 @@ func CompareDiff(ctx *context.Context) { } if ci.RawDiffType != "" { - git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, ci.RawDiffType, "", ctx.Resp) + 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 } From f531e40418fe3edf2d55f8b0a73b46589a429682 Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 12 May 2025 20:29:12 +0300 Subject: [PATCH 06/23] integration tests: WIP --- tests/integration/compare_test.go | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index a3cb538d5b045..b9724dd311d96 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" + "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" @@ -157,3 +158,41 @@ func TestCompareCodeExpand(t *testing.T) { } }) } + +func TestCompareRawDiff(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) + testEditFile(t, session, user1.Name, repo.Name, "main", "README.md", strings.Repeat("a\n", 2)) + newRef, _ := r.GetBranchCommit(repo.DefaultBranch) + fmt.Println("oldRef", oldRef.ID.String()) + fmt.Println("newRef", newRef.ID.String()) + + 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) + fmt.Println("resp", resp.Body.String()) + + 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 +`, + oldRef.ID.String()[:7], newRef.ID.String()[:7]) + + assert.Equal(t, resp.Body.String(), expected) + }) +} From 8300085cd466f7d1d877871feb43fdf85c8b182a Mon Sep 17 00:00:00 2001 From: badhezi Date: Tue, 13 May 2025 22:30:24 +0300 Subject: [PATCH 07/23] Add tests and RevParse() function --- modules/git/tree.go | 11 +++++ tests/integration/compare_test.go | 73 ++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index 9c73aec735e2a..a8e237ac85900 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -76,3 +76,14 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm } return repo.GetCommit(strings.TrimSpace(stdout)) } + +// rev-parse parses the output of `git rev-parse` command +func (repo *Repository) RevParse(ref string, file string) (string, error) { + stdout, _, err := NewCommand("rev-parse"). + AddDynamicArguments(ref+":"+file). + RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) + if err != nil { + return "", err + } + return strings.TrimSpace(stdout), nil +} diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index b9724dd311d96..8520352b8353a 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -9,6 +9,7 @@ import ( "net/url" "strings" "testing" + "time" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" @@ -159,7 +160,7 @@ func TestCompareCodeExpand(t *testing.T) { }) } -func TestCompareRawDiff(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{ @@ -170,16 +171,19 @@ func TestCompareRawDiff(t *testing.T) { }, true) assert.NoError(t, err) session := loginUser(t, user1.Name) + r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) + oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) + oldBlobRef, _ := r.RevParse(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) - fmt.Println("oldRef", oldRef.ID.String()) - fmt.Println("newRef", newRef.ID.String()) + newBlobRef, _ := r.RevParse(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) - fmt.Println("resp", resp.Body.String()) expected := fmt.Sprintf(`diff --git a/README.md b/README.md index %s..%s 100644 @@ -190,9 +194,64 @@ index %s..%s 100644 - +a +a -`, - oldRef.ID.String()[:7], newRef.ID.String()[:7]) +`, 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) - assert.Equal(t, resp.Body.String(), expected) + // Get the old commit and blob reference + oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) + oldBlobRef, _ := r.RevParse(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, _ := r.RevParse(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" + 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 +Date: %s +0300 +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()) }) } From c5932f2257169078463ae724ce34d5620933abc3 Mon Sep 17 00:00:00 2001 From: badhezi Date: Tue, 13 May 2025 22:54:09 +0300 Subject: [PATCH 08/23] formatting --- modules/git/tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index a8e237ac85900..ce2be91830d56 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -78,7 +78,7 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm } // rev-parse parses the output of `git rev-parse` command -func (repo *Repository) RevParse(ref string, file string) (string, error) { +func (repo *Repository) RevParse(ref, file string) (string, error) { stdout, _, err := NewCommand("rev-parse"). AddDynamicArguments(ref+":"+file). RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) From 4a45253e264dfbae229cd07759e9aee31ac8dc32 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 12:22:35 +0300 Subject: [PATCH 09/23] fix timezone adjustment in TestCompareRawDiffPatch --- tests/integration/compare_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 8520352b8353a..c6647277c0e09 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -227,7 +227,7 @@ func TestCompareRawDiffPatch(t *testing.T) { respTs = respTs.In(time.Local) // Format the timestamp to match the expected format in the patch - customFormat := "Mon, 02 Jan 2006 15:04:05" + 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())) @@ -235,7 +235,7 @@ func TestCompareRawDiffPatch(t *testing.T) { expected := fmt.Sprintf(`From %s Mon Sep 17 00:00:00 2001 From: User One -Date: %s +0300 +Date: %s Subject: [PATCH] Update README.md --- From 620153abf22c5eaa9a4adb95e23ecd687c143c49 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 13:59:51 +0300 Subject: [PATCH 10/23] support tag and branch names with ends with .diff and .patch --- routers/web/repo/compare.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index f125d7622c550..0d049218f4ba7 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -233,18 +233,28 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { var infos []string - // Handle possible suffixes: .diff or .patch - if strings.HasSuffix(infoPath, ".diff") { - ci.RawDiffType = git.RawDiffNormal - infoPath = strings.TrimSuffix(infoPath, ".diff") - } else if strings.HasSuffix(infoPath, ".patch") { - ci.RawDiffType = git.RawDiffPatch - infoPath = strings.TrimSuffix(infoPath, ".patch") - } - if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { + // check if head is a branch or tag on ly infoPath ends with .diff or .patch + if strings.HasSuffix(infoPath, ".diff") || strings.HasSuffix(infoPath, ".patch") { + infos = strings.SplitN(infoPath, "...", 2) + if len(infos) != 2 { + infos = strings.SplitN(infoPath, "..", 2) // match github behavior + } + ref2IsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, infos[1]) + ref2IsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, infos[1]) + if !ref2IsBranch && !ref2IsTag { + if strings.HasSuffix(infoPath, ".diff") { + ci.RawDiffType = git.RawDiffNormal + infoPath = strings.TrimSuffix(infoPath, ".diff") + } else if strings.HasSuffix(infoPath, ".patch") { + ci.RawDiffType = git.RawDiffPatch + infoPath = strings.TrimSuffix(infoPath, ".patch") + } + } + } + infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { From 35d23765e27b716abbcd696933af038bc1c97b15 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 14:53:38 +0300 Subject: [PATCH 11/23] add download links to raw diff and patch in diff box options dropdown --- templates/repo/diff/options_dropdown.tmpl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/repo/diff/options_dropdown.tmpl b/templates/repo/diff/options_dropdown.tmpl index 8d08e7ad46021..a8a6c2ac10cca 100644 --- a/templates/repo/diff/options_dropdown.tmpl +++ b/templates/repo/diff/options_dropdown.tmpl @@ -10,6 +10,9 @@ {{else if .Commit.ID.String}} {{ctx.Locale.Tr "repo.diff.download_patch"}} {{ctx.Locale.Tr "repo.diff.download_diff"}} + {{else if $.PageIsCompareDiff }} + {{ctx.Locale.Tr "repo.diff.download_patch"}} + {{ctx.Locale.Tr "repo.diff.download_diff"}} {{end}} {{ctx.Locale.Tr "repo.pulls.expand_files"}} {{ctx.Locale.Tr "repo.pulls.collapse_files"}} From ed642599e8b88337279bbe21c69f05641e7bce69 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 15:18:18 +0300 Subject: [PATCH 12/23] fix lint and typos --- modules/git/tree.go | 2 +- routers/web/repo/compare.go | 2 +- templates/repo/diff/options_dropdown.tmpl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index ce2be91830d56..b5f81ce654016 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -77,7 +77,7 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm return repo.GetCommit(strings.TrimSpace(stdout)) } -// rev-parse parses the output of `git rev-parse` command +// RevParse resolves a revision reference to other git-related objects func (repo *Repository) RevParse(ref, file string) (string, error) { stdout, _, err := NewCommand("rev-parse"). AddDynamicArguments(ref+":"+file). diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 0d049218f4ba7..e3cb5904d5e2b 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -236,7 +236,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { - // check if head is a branch or tag on ly infoPath ends with .diff or .patch + // check if head is a branch or tag only if infoPath ends with .diff or .patch if strings.HasSuffix(infoPath, ".diff") || strings.HasSuffix(infoPath, ".patch") { infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { diff --git a/templates/repo/diff/options_dropdown.tmpl b/templates/repo/diff/options_dropdown.tmpl index a8a6c2ac10cca..03519165db6dc 100644 --- a/templates/repo/diff/options_dropdown.tmpl +++ b/templates/repo/diff/options_dropdown.tmpl @@ -10,7 +10,7 @@ {{else if .Commit.ID.String}} {{ctx.Locale.Tr "repo.diff.download_patch"}} {{ctx.Locale.Tr "repo.diff.download_diff"}} - {{else if $.PageIsCompareDiff }} + {{else if $.PageIsCompareDiff}} {{ctx.Locale.Tr "repo.diff.download_patch"}} {{ctx.Locale.Tr "repo.diff.download_diff"}} {{end}} From 1b8c1467c6ba6cc0bb526346720697e2ae538458 Mon Sep 17 00:00:00 2001 From: badhezi Date: Thu, 29 May 2025 16:04:16 +0300 Subject: [PATCH 13/23] cover all head ref format cases --- routers/web/repo/compare.go | 49 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index e3cb5904d5e2b..c6dde7e592381 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -236,25 +236,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { - // check if head is a branch or tag only if infoPath ends with .diff or .patch - if strings.HasSuffix(infoPath, ".diff") || strings.HasSuffix(infoPath, ".patch") { - infos = strings.SplitN(infoPath, "...", 2) - if len(infos) != 2 { - infos = strings.SplitN(infoPath, "..", 2) // match github behavior - } - ref2IsBranch := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, infos[1]) - ref2IsTag := gitrepo.IsTagExist(ctx, ctx.Repo.Repository, infos[1]) - if !ref2IsBranch && !ref2IsTag { - if strings.HasSuffix(infoPath, ".diff") { - ci.RawDiffType = git.RawDiffNormal - infoPath = strings.TrimSuffix(infoPath, ".diff") - } else if strings.HasSuffix(infoPath, ".patch") { - ci.RawDiffType = git.RawDiffPatch - infoPath = strings.TrimSuffix(infoPath, ".patch") - } - } - } - infos = strings.SplitN(infoPath, "...", 2) if len(infos) != 2 { if infos = strings.SplitN(infoPath, "..", 2); len(infos) == 2 { @@ -275,7 +256,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 { @@ -288,7 +269,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 @@ -311,7 +292,7 @@ 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 } @@ -319,6 +300,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ctx.NotFound(nil) return nil } + ctx.Data["HeadUser"] = ci.HeadUser ctx.Data["HeadBranch"] = ci.HeadBranch ctx.Repo.PullRequest.SameRepo = isSameRepo @@ -1018,3 +1000,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 +} From b36fe3f554686b86417b53ecbf81721099112931 Mon Sep 17 00:00:00 2001 From: badhezi Date: Fri, 30 May 2025 12:58:16 +0300 Subject: [PATCH 14/23] add more test cases to cover different compare patterns --- tests/integration/compare_test.go | 102 ++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index c6647277c0e09..4883eab72b0f7 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -255,3 +255,105 @@ index %s..%s 100644 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, _ := r.RevParse(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, _ := hr.RevParse(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, _ := r.RevParse(oldRef.ID.String(), "README.md") + + testEditFile(t, session, user2.Name, headRepo.Name, "main", "README.md", strings.Repeat("a\n", 2)) + session = loginUser(t, user1.Name) + + newRef, _ := hr.GetBranchCommit(headRepo.DefaultBranch) + newBlobRef, _ := hr.RevParse(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()) + }) +} From 51f127729f9bdad7519fd288de741068a1c4a846 Mon Sep 17 00:00:00 2001 From: badhezi Date: Fri, 30 May 2025 13:05:52 +0300 Subject: [PATCH 15/23] move revParse to testing code only --- modules/git/tree.go | 11 ----------- tests/integration/compare_test.go | 29 +++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index b5f81ce654016..9c73aec735e2a 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -76,14 +76,3 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm } return repo.GetCommit(strings.TrimSpace(stdout)) } - -// RevParse resolves a revision reference to other git-related objects -func (repo *Repository) RevParse(ref, file string) (string, error) { - stdout, _, err := NewCommand("rev-parse"). - AddDynamicArguments(ref+":"+file). - RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path}) - if err != nil { - return "", err - } - return strings.TrimSpace(stdout), nil -} diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 4883eab72b0f7..6db24721dc2a3 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -13,6 +13,7 @@ import ( "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" @@ -175,12 +176,12 @@ func TestCompareRawDiffNormal(t *testing.T) { r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) - oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md") + 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, _ := r.RevParse(newRef.ID.String(), "README.md") + 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) @@ -215,12 +216,12 @@ func TestCompareRawDiffPatch(t *testing.T) { // Get the old commit and blob reference oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) - oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md") + 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, _ := r.RevParse(newRef.ID.String(), "README.md") + 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")) @@ -280,12 +281,12 @@ func TestCompareRawDiffNormalSameOwnerDifferentRepo(t *testing.T) { hr, _ := gitrepo.OpenRepository(db.DefaultContext, headRepo) oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) - oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md") + 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, _ := hr.RevParse(newRef.ID.String(), "README.md") + 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) @@ -331,13 +332,13 @@ func TestCompareRawDiffNormalAcrossForks(t *testing.T) { hr, _ := gitrepo.OpenRepository(db.DefaultContext, headRepo) oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) - oldBlobRef, _ := r.RevParse(oldRef.ID.String(), "README.md") + oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") testEditFile(t, session, user2.Name, headRepo.Name, "main", "README.md", strings.Repeat("a\n", 2)) session = loginUser(t, user1.Name) newRef, _ := hr.GetBranchCommit(headRepo.DefaultBranch) - newBlobRef, _ := hr.RevParse(newRef.ID.String(), "README.md") + newBlobRef, _ := revParse(hr, newRef.ID.String(), "README.md") session = loginUser(t, user1.Name) @@ -357,3 +358,15 @@ index %s..%s 100644 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 +} From 5764e20cc6fc9d3297a81082357072c56819e19b Mon Sep 17 00:00:00 2001 From: badhezi Date: Fri, 30 May 2025 13:17:16 +0300 Subject: [PATCH 16/23] remove redundant line --- tests/integration/compare_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 6db24721dc2a3..51a29fdc27759 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -335,7 +335,6 @@ func TestCompareRawDiffNormalAcrossForks(t *testing.T) { oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") testEditFile(t, session, user2.Name, headRepo.Name, "main", "README.md", strings.Repeat("a\n", 2)) - session = loginUser(t, user1.Name) newRef, _ := hr.GetBranchCommit(headRepo.DefaultBranch) newBlobRef, _ := revParse(hr, newRef.ID.String(), "README.md") From f39bad30d2f64da015b95e0024a630c8598ab5d5 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 1 Jun 2025 23:30:44 +0800 Subject: [PATCH 17/23] revert unnecessary change --- routers/web/repo/compare.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index c6dde7e592381..6dc842e64912b 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -230,9 +230,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ) infoPath = ctx.PathParam("*") - var infos []string - if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} } else { @@ -300,7 +298,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ctx.NotFound(nil) return nil } - ctx.Data["HeadUser"] = ci.HeadUser ctx.Data["HeadBranch"] = ci.HeadBranch ctx.Repo.PullRequest.SameRepo = isSameRepo From d386cc8f54b33a22a96f2de28be6472da52c5b5c Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 1 Jun 2025 23:48:39 +0800 Subject: [PATCH 18/23] FIXME: how to correctly choose the head repository? --- routers/web/repo/compare.go | 54 ++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 6dc842e64912b..186e78cba539b 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -223,13 +223,9 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { // base<-head: master...head:feature // same repo: master...feature - var ( - isSameRepo bool - infoPath string - err error - ) + var isSameRepo bool - infoPath = ctx.PathParam("*") + infoPath := ctx.PathParam("*") var infos []string if infoPath == "" { infos = []string{baseRepo.DefaultBranch, baseRepo.DefaultBranch} @@ -249,12 +245,14 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { ci.BaseBranch = infos[0] ctx.Data["BaseBranch"] = ci.BaseBranch - // If there is no head repository, it means compare between same repository. + var err error + + // If there is no head repository, it means compare between the same repository. headInfos := strings.Split(infos[1], ":") if len(headInfos) == 1 { isSameRepo = true ci.HeadUser = ctx.Repo.Owner - ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[0]) + ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ctx.Repo.Repository, headInfos[0]) } else if len(headInfos) == 2 { headInfosSplit := strings.Split(headInfos[0], "/") if len(headInfosSplit) == 1 { @@ -267,7 +265,8 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { } return nil } - ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1]) + // FIXME: how to correctly choose the head repository? The logic below (3-8) is quite complex, the real head repo is determined there + ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ..., headInfos[1]) isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID if isSameRepo { ci.HeadRepo = baseRepo @@ -290,7 +289,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { } return nil } - ci.HeadBranch = parseRefForRawDiff(ctx, ci, headInfos[1]) + ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ci.HeadRepo, headInfos[1]) ci.HeadUser = ci.HeadRepo.Owner isSameRepo = ci.HeadRepo.ID == ctx.Repo.Repository.ID } @@ -752,7 +751,6 @@ func CompareDiff(ctx *context.Context) { ctx.ServerError("GetRepoRawDiffForFile", err) return } - ctx.Resp.Flush() return } @@ -998,25 +996,19 @@ 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") - } - } +func parseRefForRawDiff(ctx *context.Context, refRepo *repo_model.Repository, refShortName string) (string, git.RawDiffType) { + if !strings.HasSuffix(refShortName, ".diff") && !strings.HasSuffix(refShortName, ".patch") { + return refShortName, "" + } + + if gitrepo.IsBranchExist(ctx, refRepo, refShortName) || gitrepo.IsTagExist(ctx, refRepo, refShortName) { + return refShortName, "" + } + + if s, ok := strings.CutSuffix(refShortName, ".diff"); ok { + return s, git.RawDiffNormal + } else if s, ok = strings.CutSuffix(refShortName, ".patch"); ok { + return s, git.RawDiffPatch } - return ref + return refShortName, "" } From b9c62f2412d2dd4410d9076948140134a1eaeb4f Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 2 Jun 2025 11:53:01 +0300 Subject: [PATCH 19/23] make head repo selection clearer --- routers/web/repo/compare.go | 26 ++++++++++++++++++-------- tests/integration/compare_test.go | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 186e78cba539b..8f3430f1859e1 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -249,13 +249,13 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { // If there is no head repository, it means compare between the same repository. headInfos := strings.Split(infos[1], ":") - if len(headInfos) == 1 { + if len(headInfos) == 1 { // {:headBranch} case, guaranteed baseRepo is headRepo isSameRepo = true ci.HeadUser = ctx.Repo.Owner - ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ctx.Repo.Repository, headInfos[0]) - } else if len(headInfos) == 2 { + ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, baseRepo, headInfos[0]) + } else if len(headInfos) == 2 { // {:headOwner}:{:headBranch} or {:headOwner}/{:headRepoName}:{:headBranch} case headInfosSplit := strings.Split(headInfos[0], "/") - if len(headInfosSplit) == 1 { + if len(headInfosSplit) == 1 { // {:headOwner}:{:headBranch} case, guaranteed baseRepo.Name is headRepo.Name ci.HeadUser, err = user_model.GetUserByName(ctx, headInfos[0]) if err != nil { if user_model.IsErrUserNotExist(err) { @@ -265,13 +265,23 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { } return nil } - // FIXME: how to correctly choose the head repository? The logic below (3-8) is quite complex, the real head repo is determined there - ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, ..., headInfos[1]) + + headRepo, err := repo_model.GetRepositoryByOwnerAndName(ctx, ci.HeadUser.Name, baseRepo.Name) + if err != nil { + if repo_model.IsErrRepoNotExist(err) { + ctx.NotFound(nil) + } else { + ctx.ServerError("GetRepositoryByOwnerAndName", err) + } + return nil + } + ci.HeadBranch, ci.RawDiffType = parseRefForRawDiff(ctx, headRepo, headInfos[1]) + isSameRepo = ci.HeadUser.ID == ctx.Repo.Owner.ID - if isSameRepo { + if isSameRepo { // not a fork ci.HeadRepo = baseRepo } - } else { + } else { // {:headOwner}/{:headRepoName}:{:headBranch} case, across forks ci.HeadRepo, err = repo_model.GetRepositoryByOwnerAndName(ctx, headInfosSplit[0], headInfosSplit[1]) if err != nil { if repo_model.IsErrRepoNotExist(err) { diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 51a29fdc27759..d620daf4b0495 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -228,7 +228,7 @@ func TestCompareRawDiffPatch(t *testing.T) { 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" + customFormat := "Mon, 2 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())) From 7e6d45fd72f550593e532ff2b67e4fb29c2dc9f8 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 8 Oct 2025 11:56:21 +0300 Subject: [PATCH 20/23] Update routers/web/repo/compare.go Co-authored-by: Lunny Xiao Signed-off-by: badhezi --- routers/web/repo/compare.go | 1 - 1 file changed, 1 deletion(-) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 8f3430f1859e1..16a6be1605f00 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -759,7 +759,6 @@ func CompareDiff(ctx *context.Context) { err := git.GetRepoRawDiffForFile(ci.HeadGitRepo, ci.BaseBranch, ci.HeadBranch, ci.RawDiffType, "", ctx.Resp) if err != nil { ctx.ServerError("GetRepoRawDiffForFile", err) - return } return } From ecdce1800cf7f8a64a40d814299ffd9b5c965c13 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 8 Oct 2025 11:56:32 +0300 Subject: [PATCH 21/23] Update tests/integration/compare_test.go Co-authored-by: Lunny Xiao Signed-off-by: badhezi --- tests/integration/compare_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index d620daf4b0495..49b0ede848d72 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -174,7 +174,7 @@ func TestCompareRawDiffNormal(t *testing.T) { session := loginUser(t, user1.Name) r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) - +defer r.Close() oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") From b93c0c287c07b63f597f5d1a830bdb503f27192a Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 8 Oct 2025 13:12:44 +0300 Subject: [PATCH 22/23] test compliance --- tests/integration/compare_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index 49b0ede848d72..fbcd26c6058f4 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -174,7 +174,7 @@ func TestCompareRawDiffNormal(t *testing.T) { session := loginUser(t, user1.Name) r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) -defer r.Close() + defer r.Close() oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") From d347902500c5befb6d83f0fbfdfcb2cbfdf9cd1a Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 8 Oct 2025 14:15:28 +0300 Subject: [PATCH 23/23] fix tests --- tests/integration/compare_test.go | 36 +++++++++++++++++-------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/tests/integration/compare_test.go b/tests/integration/compare_test.go index fbcd26c6058f4..6dac1eb514e98 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -14,6 +14,7 @@ import ( "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/git/gitcmd" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/test" repo_service "code.gitea.io/gitea/services/repository" @@ -164,7 +165,7 @@ 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{ + repo, err := repo_service.CreateRepositoryDirectly(t.Context(), user1, user1, repo_service.CreateRepoOptions{ Name: "test_raw_diff", Readme: "Default", AutoInit: true, @@ -173,7 +174,7 @@ func TestCompareRawDiffNormal(t *testing.T) { assert.NoError(t, err) session := loginUser(t, user1.Name) - r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) + r, _ := gitrepo.OpenRepository(t.Context(), repo) defer r.Close() oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") @@ -203,7 +204,7 @@ index %s..%s 100644 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{ + repo, err := repo_service.CreateRepositoryDirectly(t.Context(), user1, user1, repo_service.CreateRepoOptions{ Name: "test_raw_diff", Readme: "Default", AutoInit: true, @@ -212,13 +213,16 @@ func TestCompareRawDiffPatch(t *testing.T) { assert.NoError(t, err) session := loginUser(t, user1.Name) - r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) + r, _ := gitrepo.OpenRepository(t.Context(), 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)) + resp := testEditorActionEdit(t, session, user1.Name, repo.Name, "_edit", "main", "README.md", map[string]string{ + "content": strings.Repeat("a\n", 2), + "commit_choice": "direct", + }) newRef, _ := r.GetBranchCommit(repo.DefaultBranch) newBlobRef, _ := revParse(r, newRef.ID.String(), "README.md") @@ -260,7 +264,7 @@ index %s..%s 100644 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{ + repo, err := repo_service.CreateRepositoryDirectly(t.Context(), user1, user1, repo_service.CreateRepoOptions{ Name: "test_raw_diff", Readme: "Default", AutoInit: true, @@ -269,7 +273,7 @@ func TestCompareRawDiffNormalSameOwnerDifferentRepo(t *testing.T) { assert.NoError(t, err) session := loginUser(t, user1.Name) - headRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user1, user1, repo_service.CreateRepoOptions{ + headRepo, err := repo_service.CreateRepositoryDirectly(t.Context(), user1, user1, repo_service.CreateRepoOptions{ Name: "test_raw_diff_head", Readme: "Default", AutoInit: true, @@ -277,8 +281,8 @@ func TestCompareRawDiffNormalSameOwnerDifferentRepo(t *testing.T) { }, true) assert.NoError(t, err) - r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) - hr, _ := gitrepo.OpenRepository(db.DefaultContext, headRepo) + r, _ := gitrepo.OpenRepository(t.Context(), repo) + hr, _ := gitrepo.OpenRepository(t.Context(), headRepo) oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") @@ -310,7 +314,7 @@ func TestCompareRawDiffNormalAcrossForks(t *testing.T) { 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{ + repo, err := repo_service.CreateRepositoryDirectly(t.Context(), user1, user1, repo_service.CreateRepoOptions{ Name: "test_raw_diff", Readme: "Default", AutoInit: true, @@ -318,7 +322,7 @@ func TestCompareRawDiffNormalAcrossForks(t *testing.T) { }, true) assert.NoError(t, err) - headRepo, err := repo_service.ForkRepository(db.DefaultContext, user2, user2, repo_service.ForkRepoOptions{ + headRepo, err := repo_service.ForkRepository(t.Context(), user2, user2, repo_service.ForkRepoOptions{ BaseRepo: repo, Name: repo.Name, Description: repo.Description, @@ -328,8 +332,8 @@ func TestCompareRawDiffNormalAcrossForks(t *testing.T) { session := loginUser(t, user2.Name) - r, _ := gitrepo.OpenRepository(db.DefaultContext, repo) - hr, _ := gitrepo.OpenRepository(db.DefaultContext, headRepo) + r, _ := gitrepo.OpenRepository(t.Context(), repo) + hr, _ := gitrepo.OpenRepository(t.Context(), headRepo) oldRef, _ := r.GetBranchCommit(repo.DefaultBranch) oldBlobRef, _ := revParse(r, oldRef.ID.String(), "README.md") @@ -361,9 +365,9 @@ index %s..%s 100644 // 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}) + stdout, _, err := gitcmd.NewCommand("rev-parse"). + AddDynamicArguments(ref + ":" + file).WithDir(repo.Path). + RunStdString(repo.Ctx) if err != nil { return "", err }