Skip to content

Commit 760cf41

Browse files
kdumontnujtran
andauthored
Use beforeCommit instead of baseCommit (#22949) (#22996)
Backport #22949 Fixes #22946 Probably related to #19530 Co-authored-by: Jonathan Tran <[email protected]>
1 parent 90982bf commit 760cf41

19 files changed

+109
-10
lines changed

models/fixtures/repo_unit.yml

+13
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,16 @@
544544
repo_id: 51
545545
type: 2
546546
created_unix: 946684810
547+
548+
-
549+
id: 80
550+
repo_id: 31
551+
type: 1
552+
created_unix: 946684810
553+
554+
-
555+
id: 81
556+
repo_id: 31
557+
type: 3
558+
config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}"
559+
created_unix: 946684810

routers/web/repo/compare.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ const (
4343
)
4444

4545
// setCompareContext sets context data.
46-
func setCompareContext(ctx *context.Context, base, head *git.Commit, headOwner, headName string) {
47-
ctx.Data["BaseCommit"] = base
46+
func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner, headName string) {
47+
ctx.Data["BeforeCommit"] = before
4848
ctx.Data["HeadCommit"] = head
4949

5050
ctx.Data["GetBlobByPathForCommit"] = func(commit *git.Commit, path string) *git.Blob {
@@ -59,7 +59,7 @@ func setCompareContext(ctx *context.Context, base, head *git.Commit, headOwner,
5959
return blob
6060
}
6161

62-
setPathsCompareContext(ctx, base, head, headOwner, headName)
62+
setPathsCompareContext(ctx, before, head, headOwner, headName)
6363
setImageCompareContext(ctx)
6464
setCsvCompareContext(ctx)
6565
}
@@ -629,9 +629,8 @@ func PrepareCompareDiff(
629629
}
630630

631631
baseGitRepo := ctx.Repo.GitRepo
632-
baseCommitID := ci.CompareInfo.BaseCommitID
633632

634-
baseCommit, err := baseGitRepo.GetCommit(baseCommitID)
633+
beforeCommit, err := baseGitRepo.GetCommit(beforeCommitID)
635634
if err != nil {
636635
ctx.ServerError("GetCommit", err)
637636
return false
@@ -668,7 +667,7 @@ func PrepareCompareDiff(
668667
ctx.Data["Username"] = ci.HeadUser.Name
669668
ctx.Data["Reponame"] = ci.HeadRepo.Name
670669

671-
setCompareContext(ctx, baseCommit, headCommit, ci.HeadUser.Name, repo.Name)
670+
setCompareContext(ctx, beforeCommit, headCommit, ci.HeadUser.Name, repo.Name)
672671

673672
return false
674673
}

templates/repo/diff/box.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<div id="diff-file-boxes" class="sixteen wide column">
7272
{{range $i, $file := .Diff.Files}}
7373
{{/*notice: the index of Diff.Files should not be used for element ID, because the index will be restarted from 0 when doing load-more for PRs with a lot of files*/}}
74-
{{$blobBase := call $.GetBlobByPathForCommit $.BaseCommit $file.OldName}}
74+
{{$blobBase := call $.GetBlobByPathForCommit $.BeforeCommit $file.OldName}}
7575
{{$blobHead := call $.GetBlobByPathForCommit $.HeadCommit $file.Name}}
7676
{{$isImage := or (call $.IsBlobAnImage $blobBase) (call $.IsBlobAnImage $blobHead)}}
7777
{{$isCsv := (call $.IsCsvFile $file)}}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"/home/tris/Projects/go/src/code.gitea.io/gitea/gitea" hook --config='/home/tris/Projects/go/src/code.gitea.io/gitea/custom/conf/app.ini' post-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"/home/tris/Projects/go/src/code.gitea.io/gitea/gitea" hook --config='/home/tris/Projects/go/src/code.gitea.io/gitea/custom/conf/app.ini' pre-receive
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/usr/bin/env bash
2-
"/home/tris/Projects/go/src/code.gitea.io/gitea/gitea" hook --config='/home/tris/Projects/go/src/code.gitea.io/gitea/custom/conf/app.ini' update $1 $2 $3
2+
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x��An�0 s�+���l� �[_A�TkIJC>���z[,f1��Z�!�K�̀��S��L5[,�D҉'�:a�Rнe���Dl� �:^C�g�lH�d���>iqr��m��s1���K���m=?U��3o������������k�ߝ{��@w�ʼ���E]

tests/gitea-repositories-meta/user2/repo20.git/objects/c8/e31bc7688741a5287fcde4fbb8fc129ca07027

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x��A
2+
�0@Q�9�\@�N�&w�"�L��4Ҧ���+�����Z�؀�vm�`��!&�u֎m��FK�l��a���8t�]�l;��H�}g��9'2}�{�*l�Q�}&�+�i+un�v��0N�X� ���,!�{D����&�:uI �����<N���qEo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��An�0EY�s�=v2FB v=D5�';U�T���
2+
������Z묀���%�P(z�����p�D�%8�!8[�/o�r�R�1F�p�HS.����3�$��]���E��g��ڴ��{�y9�~�{ ��v�����S��gn��
3+
����_���2���c6tuI�
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c8e31bc7688741a5287fcde4fbb8fc129ca07027
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cfe3b3c1fd36fba04f9183287b106497e1afe986
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8babce967f21b9dfa6987f943b91093dac58a4f0

tests/integration/compare_test.go

+78
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integration
66

77
import (
8+
"fmt"
89
"net/http"
910
"strings"
1011
"testing"
@@ -41,3 +42,80 @@ func TestCompareDefault(t *testing.T) {
4142
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
4243
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
4344
}
45+
46+
// Ensure the comparison matches what we expect
47+
func inspectCompare(t *testing.T, htmlDoc *HTMLDoc, diffCount int, diffChanges []string) {
48+
selection := htmlDoc.doc.Find("#diff-file-boxes").Children()
49+
50+
assert.Lenf(t, selection.Nodes, diffCount, "Expected %v diffed files, found: %v", diffCount, len(selection.Nodes))
51+
52+
for _, diffChange := range diffChanges {
53+
selection = htmlDoc.doc.Find(fmt.Sprintf("[data-new-filename=\"%s\"]", diffChange))
54+
assert.Lenf(t, selection.Nodes, 1, "Expected 1 match for [data-new-filename=\"%s\"], found: %v", diffChange, len(selection.Nodes))
55+
}
56+
}
57+
58+
// Git commit graph for repo20
59+
// * 8babce9 (origin/remove-files-b) Add a dummy file
60+
// * b67e43a Delete test.csv and link_hi
61+
// | * cfe3b3c (origin/remove-files-a) Delete test.csv and link_hi
62+
// |/
63+
// * c8e31bc (origin/add-csv) Add test csv file
64+
// * 808038d (HEAD -> master, origin/master, origin/HEAD) Added test links
65+
66+
func TestCompareBranches(t *testing.T) {
67+
defer tests.PrepareTestEnv(t)()
68+
69+
session := loginUser(t, "user2")
70+
71+
// Inderect compare remove-files-b (head) with add-csv (base) branch
72+
//
73+
// 'link_hi' and 'test.csv' are deleted, 'test.txt' is added
74+
req := NewRequest(t, "GET", "/user2/repo20/compare/add-csv...remove-files-b")
75+
resp := session.MakeRequest(t, req, http.StatusOK)
76+
htmlDoc := NewHTMLParser(t, resp.Body)
77+
78+
diffCount := 3
79+
diffChanges := []string{"link_hi", "test.csv", "test.txt"}
80+
81+
inspectCompare(t, htmlDoc, diffCount, diffChanges)
82+
83+
// Inderect compare remove-files-b (head) with remove-files-a (base) branch
84+
//
85+
// 'link_hi' and 'test.csv' are deleted, 'test.txt' is added
86+
87+
req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-a...remove-files-b")
88+
resp = session.MakeRequest(t, req, http.StatusOK)
89+
htmlDoc = NewHTMLParser(t, resp.Body)
90+
91+
diffCount = 3
92+
diffChanges = []string{"link_hi", "test.csv", "test.txt"}
93+
94+
inspectCompare(t, htmlDoc, diffCount, diffChanges)
95+
96+
// Inderect compare remove-files-a (head) with remove-files-b (base) branch
97+
//
98+
// 'link_hi' and 'test.csv' are deleted
99+
100+
req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-b...remove-files-a")
101+
resp = session.MakeRequest(t, req, http.StatusOK)
102+
htmlDoc = NewHTMLParser(t, resp.Body)
103+
104+
diffCount = 2
105+
diffChanges = []string{"link_hi", "test.csv"}
106+
107+
inspectCompare(t, htmlDoc, diffCount, diffChanges)
108+
109+
// Direct compare remove-files-b (head) with remove-files-a (base) branch
110+
//
111+
// 'test.txt' is deleted
112+
113+
req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-b..remove-files-a")
114+
resp = session.MakeRequest(t, req, http.StatusOK)
115+
htmlDoc = NewHTMLParser(t, resp.Body)
116+
117+
diffCount = 1
118+
diffChanges = []string{"test.txt"}
119+
120+
inspectCompare(t, htmlDoc, diffCount, diffChanges)
121+
}

0 commit comments

Comments
 (0)