-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Draft
badhezi
wants to merge
25
commits into
go-gitea:main
Choose a base branch
from
badhezi:dev/hezi/add-raw-diff-patch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
8d799c2
use the correct context data for PR link template in issue card
badhezi f2a2acf
Merge branch 'main' into main
badhezi 5cc1bda
Merge branch 'main' into main
GiteaBot 54d37d1
Merge branch 'main' into main
GiteaBot ac25150
Merge branch 'go-gitea:main' into main
badhezi e11a339
Merge branch 'go-gitea:main' into main
badhezi 104eecc
Merge branch 'go-gitea:main' into main
badhezi bcc4ade
Merge branch 'go-gitea:main' into main
badhezi a7aaa79
Merge branch 'go-gitea:main' into main
badhezi b46d314
Merge branch 'go-gitea:main' into main
badhezi 4e2434b
Merge branch 'go-gitea:main' into main
badhezi 7f72fe9
Merge branch 'go-gitea:main' into main
badhezi c6acfc1
Merge branch 'go-gitea:main' into main
badhezi 016c2f3
Merge branch 'go-gitea:main' into main
badhezi a3c2953
Merge branch 'go-gitea:main' into main
badhezi 4d7ea0d
Merge branch 'go-gitea:main' into main
badhezi e60671f
parse .raw and .diff optional compare parameters
badhezi 8a2d8ed
lint, err handle
badhezi e92013e
integration tests: WIP
badhezi 406d7d1
Add tests and RevParse() function
badhezi c626b66
formatting
badhezi 460fe62
fix timezone adjustment in TestCompareRawDiffPatch
badhezi c98fccf
support tag and branch names with ends with .diff and .patch
badhezi 72b2ff1
add download links to raw diff and patch in diff box options dropdown
badhezi afae382
fix lint and typos
badhezi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,10 +228,31 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo { | |
) | ||
|
||
infoPath = ctx.PathParam("*") | ||
|
||
var infos []string | ||
|
||
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]) | ||
Comment on lines
+243
to
+244
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks ill address that |
||
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 { | ||
|
@@ -729,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"] = ".." | ||
|
@@ -743,6 +765,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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,12 @@ 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" | ||
"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 +160,99 @@ 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, _ := 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) | ||
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) | ||
|
||
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, _ := 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 -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()) | ||
}) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is it really used except the tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks ill either move it to test files or find a workaround