From 8d799c236c854c8bb132b38c97385beaf53d9327 Mon Sep 17 00:00:00 2001 From: badhezi Date: Tue, 15 Apr 2025 19:56:19 +0300 Subject: [PATCH 01/10] 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 e60671f41299c849a1700aa883f5ce71e32fcb91 Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 12 May 2025 16:38:44 +0300 Subject: [PATCH 02/10] 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 ae4ea7ea87afc..8e8080b6225b8 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -685,7 +685,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 4d1cc2f0d8908..736f73db0e0e0 100644 --- a/routers/common/compare.go +++ b/routers/common/compare.go @@ -18,4 +18,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 8b99dd95da109..12ce37bcef7f5 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -228,7 +228,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 { @@ -743,6 +755,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 8a2d8ed56fd7a3d2f3e7cd469f9d688166f76510 Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 12 May 2025 17:07:15 +0300 Subject: [PATCH 03/10] 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 8e8080b6225b8..ae4ea7ea87afc 100644 --- a/routers/api/packages/api.go +++ b/routers/api/packages/api.go @@ -685,6 +685,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 12ce37bcef7f5..8647033923aa8 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -235,7 +235,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") @@ -756,7 +755,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 e92013e2bef386643708f56c8035401516e593b4 Mon Sep 17 00:00:00 2001 From: badhezi Date: Mon, 12 May 2025 20:29:12 +0300 Subject: [PATCH 04/10] 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 0648777fede0e..712328db86f10 100644 --- a/tests/integration/compare_test.go +++ b/tests/integration/compare_test.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models/db" "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" @@ -158,3 +159,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 406d7d1fe937aed904f053e9dde96a7b9c069b6a Mon Sep 17 00:00:00 2001 From: badhezi Date: Tue, 13 May 2025 22:30:24 +0300 Subject: [PATCH 05/10] 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 f6fdff97d0400..8e04b721fa760 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -73,3 +73,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 712328db86f10..634a6b565236d 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/db" "code.gitea.io/gitea/models/unittest" @@ -160,7 +161,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{ @@ -171,16 +172,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 @@ -191,9 +195,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 c626b667059acbd7f424479a459f25aab382863b Mon Sep 17 00:00:00 2001 From: badhezi Date: Tue, 13 May 2025 22:54:09 +0300 Subject: [PATCH 06/10] 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 8e04b721fa760..ac87e3259bb73 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -75,7 +75,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 460fe6291deaa201e29c2b69e9002995aa9c1938 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 12:22:35 +0300 Subject: [PATCH 07/10] 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 634a6b565236d..6aefbd0e40c55 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" + 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())) @@ -236,7 +236,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 c98fccf96164fd58a883a93ad1157ad14e58c788 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 13:59:51 +0300 Subject: [PATCH 08/10] 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 8647033923aa8..989691a5720ab 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -231,18 +231,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 72b2ff183964dcb8f7638ed01fff38fe8d3e771a Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 14:53:38 +0300 Subject: [PATCH 09/10] add download links to raw diff and patch in diff box options dropdown --- routers/web/repo/compare.go | 1 + templates/repo/diff/options_dropdown.tmpl | 3 +++ 2 files changed, 4 insertions(+) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 989691a5720ab..a206f8deb612f 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -750,6 +750,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"] = ".." 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 afae38244c5c5a404c1d4238c4e7638f8040f479 Mon Sep 17 00:00:00 2001 From: badhezi Date: Wed, 14 May 2025 15:18:18 +0300 Subject: [PATCH 10/10] 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 ac87e3259bb73..9135a1edc918c 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -74,7 +74,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 a206f8deb612f..351236ac29f3d 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -234,7 +234,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}}