Skip to content

Commit 2b3f12f

Browse files
kdumontnujtran
andauthored
Use beforeCommit instead of baseCommit (#22949)
Replaces: #22947 Fixes #22946 Probably related to #19530 Basically, many of the diffs were broken because they were comparing to the base commit, where a 3-dot diff should be comparing to the [last common ancestor](https://matthew-brett.github.io/pydagogue/git_diff_dots.html). This should have an integration test so that we don’t run into this issue again. --------- Co-authored-by: Jonathan Tran <[email protected]>
1 parent 6840a8c commit 2b3f12f

20 files changed

+110
-11
lines changed

models/db/iterate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestIterate(t *testing.T) {
2525
return nil
2626
})
2727
assert.NoError(t, err)
28-
assert.EqualValues(t, 81, repoCnt)
28+
assert.EqualValues(t, 83, repoCnt)
2929

3030
err = db.Iterate(db.DefaultContext, nil, func(ctx context.Context, repoUnit *repo_model.RepoUnit) error {
3131
reopUnit2 := repo_model.RepoUnit{ID: repoUnit.ID}

models/fixtures/repo_unit.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,16 @@
556556
repo_id: 54
557557
type: 1
558558
created_unix: 946684810
559+
560+
-
561+
id: 82
562+
repo_id: 31
563+
type: 1
564+
created_unix: 946684810
565+
566+
-
567+
id: 83
568+
repo_id: 31
569+
type: 3
570+
config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}"
571+
created_unix: 946684810

routers/web/repo/compare.go

Lines changed: 5 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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)}}
Lines changed: 1 addition & 1 deletion
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
Lines changed: 1 addition & 1 deletion
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
Lines changed: 1 addition & 1 deletion
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
Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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
Lines changed: 3 additions & 0 deletions
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�
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c8e31bc7688741a5287fcde4fbb8fc129ca07027
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cfe3b3c1fd36fba04f9183287b106497e1afe986
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
8babce967f21b9dfa6987f943b91093dac58a4f0

tests/integration/compare_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package integration
55

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

0 commit comments

Comments
 (0)