Use beforeCommit instead of baseCommit (#22949) (#22996)

Backport #22949
Fixes https://github.com/go-gitea/gitea/issues/22946
Probably related to https://github.com/go-gitea/gitea/issues/19530

Co-authored-by: Jonathan Tran <jonnytran@gmail.com>
pull/23046/head
Kyle D 2 years ago committed by GitHub
parent 90982bffa5
commit 760cf419ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      models/fixtures/repo_unit.yml
  2. 11
      routers/web/repo/compare.go
  3. 2
      templates/repo/diff/box.tmpl
  4. 2
      tests/gitea-repositories-meta/user2/repo20.git/hooks/post-receive.d/gitea
  5. 2
      tests/gitea-repositories-meta/user2/repo20.git/hooks/pre-receive.d/gitea
  6. 2
      tests/gitea-repositories-meta/user2/repo20.git/hooks/update.d/gitea
  7. BIN
      tests/gitea-repositories-meta/user2/repo20.git/objects/07/0b2e783a6b3e521a23fdead377a3e41a04410d
  8. BIN
      tests/gitea-repositories-meta/user2/repo20.git/objects/79/adb592126eddce5f656f56db797910db025af0
  9. 1
      tests/gitea-repositories-meta/user2/repo20.git/objects/8b/abce967f21b9dfa6987f943b91093dac58a4f0
  10. BIN
      tests/gitea-repositories-meta/user2/repo20.git/objects/a4/202876cd8bbc3f38b7d99594edbe1bb7f97a6f
  11. BIN
      tests/gitea-repositories-meta/user2/repo20.git/objects/b0/246d5964a3630491bd06c756be46513e3d7035
  12. BIN
      tests/gitea-repositories-meta/user2/repo20.git/objects/b6/7e43a07d48243a5f670ace063acd5e13f719df
  13. 2
      tests/gitea-repositories-meta/user2/repo20.git/objects/c8/e31bc7688741a5287fcde4fbb8fc129ca07027
  14. 3
      tests/gitea-repositories-meta/user2/repo20.git/objects/cf/e3b3c1fd36fba04f9183287b106497e1afe986
  15. BIN
      tests/gitea-repositories-meta/user2/repo20.git/objects/ea/f5f7510320b6a327fb308379de2f94d8859a54
  16. 1
      tests/gitea-repositories-meta/user2/repo20.git/refs/heads/add-csv
  17. 1
      tests/gitea-repositories-meta/user2/repo20.git/refs/heads/remove-files-a
  18. 1
      tests/gitea-repositories-meta/user2/repo20.git/refs/heads/remove-files-b
  19. 78
      tests/integration/compare_test.go

@ -544,3 +544,16 @@
repo_id: 51
type: 2
created_unix: 946684810
-
id: 80
repo_id: 31
type: 1
created_unix: 946684810
-
id: 81
repo_id: 31
type: 3
config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}"
created_unix: 946684810

@ -43,8 +43,8 @@ const (
)
// setCompareContext sets context data.
func setCompareContext(ctx *context.Context, base, head *git.Commit, headOwner, headName string) {
ctx.Data["BaseCommit"] = base
func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner, headName string) {
ctx.Data["BeforeCommit"] = before
ctx.Data["HeadCommit"] = head
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,
return blob
}
setPathsCompareContext(ctx, base, head, headOwner, headName)
setPathsCompareContext(ctx, before, head, headOwner, headName)
setImageCompareContext(ctx)
setCsvCompareContext(ctx)
}
@ -629,9 +629,8 @@ func PrepareCompareDiff(
}
baseGitRepo := ctx.Repo.GitRepo
baseCommitID := ci.CompareInfo.BaseCommitID
baseCommit, err := baseGitRepo.GetCommit(baseCommitID)
beforeCommit, err := baseGitRepo.GetCommit(beforeCommitID)
if err != nil {
ctx.ServerError("GetCommit", err)
return false
@ -668,7 +667,7 @@ func PrepareCompareDiff(
ctx.Data["Username"] = ci.HeadUser.Name
ctx.Data["Reponame"] = ci.HeadRepo.Name
setCompareContext(ctx, baseCommit, headCommit, ci.HeadUser.Name, repo.Name)
setCompareContext(ctx, beforeCommit, headCommit, ci.HeadUser.Name, repo.Name)
return false
}

@ -71,7 +71,7 @@
<div id="diff-file-boxes" class="sixteen wide column">
{{range $i, $file := .Diff.Files}}
{{/*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*/}}
{{$blobBase := call $.GetBlobByPathForCommit $.BaseCommit $file.OldName}}
{{$blobBase := call $.GetBlobByPathForCommit $.BeforeCommit $file.OldName}}
{{$blobHead := call $.GetBlobByPathForCommit $.HeadCommit $file.Name}}
{{$isImage := or (call $.IsBlobAnImage $blobBase) (call $.IsBlobAnImage $blobHead)}}
{{$isCsv := (call $.IsCsvFile $file)}}

@ -1,2 +1,2 @@
#!/usr/bin/env bash
"/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
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" post-receive

@ -1,2 +1,2 @@
#!/usr/bin/env bash
"/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
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" pre-receive

@ -1,2 +1,2 @@
#!/usr/bin/env bash
"/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
"$GITEA_ROOT/gitea" hook --config="$GITEA_ROOT/$GITEA_CONF" update $1 $2 $3

@ -0,0 +1 @@
x•ŽAnÃ0 sÖ+ø<EFBFBD>”l‘ Ð[_A‹TkIJC>ø÷Õz[,f1›·Zç!øKÛÍ€“è<EFBFBD>L5[,©DÒ‰'<EFBFBD>:aˆRнe·µÁDlã È:^C<EFBFBD>g”lHƒd<EFBFBD>æ‡Â>iqr´ßm‡ïs1ø‚ÛK<EFBFBD>º­m=?Uæå3oõž˜®<EFBFBD>òð<EFBFBD>Ñõ¶¿köß<EFBFBD>{ª‚@wÔʼ˜û<EFBFBD>E]

@ -0,0 +1,2 @@
x•ÎA
Â0@Q×9Å\@™NÒ&wž"“L´Ø4Ò¦‚·×+¸ýðàÇZÊØ€ˆvm<EFBFBD>`ÉÙ!&ÇuÖŽmò¾÷FKÇl³·aÈê8t¨]¢l;ÆÀHè}g´<EFBFBD>9'2{<EFBFBD>*líQ¸}&<EFBFBD>+Ÿi+unóv¾—0N‡XË ºÁŽ,!Â{Dõ«¿»&ÿ:uI š¬ âú†<N¢¾¨qEo

@ -0,0 +1,3 @@
x•ŽAnÂ0EYűs<EFBFBD>=v2FB v=D5¶';U<EFBFBD>Tâöř
Ýţ§÷ôóZ묀ş‰€%›P(z“—ŁźŠpńDě%8¶!8[Ě/oŇrďR¦1FępŔHS.¦”┞3÷$’á]ëßďEŕ—gŮëÚ´í×{ĺy9ĺµ~<EFBFBD>{ ĎŽv°ÖôµżSůŻgn˛<EFBFBD>
¨Ľô”_Ŕ­Ŕ2·çĎc6tuI‚

@ -0,0 +1 @@
c8e31bc7688741a5287fcde4fbb8fc129ca07027

@ -5,6 +5,7 @@
package integration
import (
"fmt"
"net/http"
"strings"
"testing"
@ -41,3 +42,80 @@ func TestCompareDefault(t *testing.T) {
selection := htmlDoc.doc.Find(".choose.branch .filter.dropdown")
assert.Lenf(t, selection.Nodes, 2, "The template has changed")
}
// Ensure the comparison matches what we expect
func inspectCompare(t *testing.T, htmlDoc *HTMLDoc, diffCount int, diffChanges []string) {
selection := htmlDoc.doc.Find("#diff-file-boxes").Children()
assert.Lenf(t, selection.Nodes, diffCount, "Expected %v diffed files, found: %v", diffCount, len(selection.Nodes))
for _, diffChange := range diffChanges {
selection = htmlDoc.doc.Find(fmt.Sprintf("[data-new-filename=\"%s\"]", diffChange))
assert.Lenf(t, selection.Nodes, 1, "Expected 1 match for [data-new-filename=\"%s\"], found: %v", diffChange, len(selection.Nodes))
}
}
// Git commit graph for repo20
// * 8babce9 (origin/remove-files-b) Add a dummy file
// * b67e43a Delete test.csv and link_hi
// | * cfe3b3c (origin/remove-files-a) Delete test.csv and link_hi
// |/
// * c8e31bc (origin/add-csv) Add test csv file
// * 808038d (HEAD -> master, origin/master, origin/HEAD) Added test links
func TestCompareBranches(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
// Inderect compare remove-files-b (head) with add-csv (base) branch
//
// 'link_hi' and 'test.csv' are deleted, 'test.txt' is added
req := NewRequest(t, "GET", "/user2/repo20/compare/add-csv...remove-files-b")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
diffCount := 3
diffChanges := []string{"link_hi", "test.csv", "test.txt"}
inspectCompare(t, htmlDoc, diffCount, diffChanges)
// Inderect compare remove-files-b (head) with remove-files-a (base) branch
//
// 'link_hi' and 'test.csv' are deleted, 'test.txt' is added
req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-a...remove-files-b")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
diffCount = 3
diffChanges = []string{"link_hi", "test.csv", "test.txt"}
inspectCompare(t, htmlDoc, diffCount, diffChanges)
// Inderect compare remove-files-a (head) with remove-files-b (base) branch
//
// 'link_hi' and 'test.csv' are deleted
req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-b...remove-files-a")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
diffCount = 2
diffChanges = []string{"link_hi", "test.csv"}
inspectCompare(t, htmlDoc, diffCount, diffChanges)
// Direct compare remove-files-b (head) with remove-files-a (base) branch
//
// 'test.txt' is deleted
req = NewRequest(t, "GET", "/user2/repo20/compare/remove-files-b..remove-files-a")
resp = session.MakeRequest(t, req, http.StatusOK)
htmlDoc = NewHTMLParser(t, resp.Body)
diffCount = 1
diffChanges = []string{"test.txt"}
inspectCompare(t, htmlDoc, diffCount, diffChanges)
}

Loading…
Cancel
Save