Skip to content

Commit 95e8ee7

Browse files
KN4CK3RAbdulrhmnGhanem
authored andcommitted
Fix webhook commits wrong hash on HEAD reset (go-gitea#16283)
Use `..` instead of `...` with `rev-list`. In combination with go-gitea#16282 the receiver can get the correct commit. The behaviour is now like Github. fixes go-gitea#11802
1 parent 27520ee commit 95e8ee7

16 files changed

+48
-5
lines changed

modules/git/repo_commit.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,15 @@ func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (in
264264
return len(strings.Split(stdout, "\n")) - 1, nil
265265
}
266266

267-
// CommitsBetween returns a list that contains commits between [last, before).
267+
// CommitsBetween returns a list that contains commits between [before, last).
268+
// If before is detached (removed by reset + push) it is not included.
268269
func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error) {
269270
var stdout []byte
270271
var err error
271272
if before == nil {
272273
stdout, err = NewCommand("rev-list", last.ID.String()).RunInDirBytes(repo.Path)
273274
} else {
274-
stdout, err = NewCommand("rev-list", before.ID.String()+"..."+last.ID.String()).RunInDirBytes(repo.Path)
275+
stdout, err = NewCommand("rev-list", before.ID.String()+".."+last.ID.String()).RunInDirBytes(repo.Path)
275276
if err != nil && strings.Contains(err.Error(), "no merge base") {
276277
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
277278
// previously it would return the results of git rev-list before last so let's try that...
@@ -284,14 +285,14 @@ func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List
284285
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
285286
}
286287

287-
// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [last, before)
288+
// CommitsBetweenLimit returns a list that contains at most limit commits skipping the first skip commits between [before, last)
288289
func (repo *Repository) CommitsBetweenLimit(last *Commit, before *Commit, limit, skip int) (*list.List, error) {
289290
var stdout []byte
290291
var err error
291292
if before == nil {
292293
stdout, err = NewCommand("rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), last.ID.String()).RunInDirBytes(repo.Path)
293294
} else {
294-
stdout, err = NewCommand("rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String()+"..."+last.ID.String()).RunInDirBytes(repo.Path)
295+
stdout, err = NewCommand("rev-list", "--max-count", strconv.Itoa(limit), "--skip", strconv.Itoa(skip), before.ID.String()+".."+last.ID.String()).RunInDirBytes(repo.Path)
295296
if err != nil && strings.Contains(err.Error(), "no merge base") {
296297
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
297298
// previously it would return the results of git rev-list --max-count n before last so let's try that...
@@ -322,7 +323,7 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, erro
322323

323324
// CommitsCountBetween return numbers of commits between two commits
324325
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
325-
count, err := CommitsCountFiles(repo.Path, []string{start + "..." + end}, []string{})
326+
count, err := CommitsCountFiles(repo.Path, []string{start + ".." + end}, []string{})
326327
if err != nil && strings.Contains(err.Error(), "no merge base") {
327328
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
328329
// previously it would return the results of git rev-list before last so let's try that...

modules/git/repo_commit_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,25 @@ func TestIsCommitInBranch(t *testing.T) {
7878
assert.NoError(t, err)
7979
assert.False(t, result)
8080
}
81+
82+
func TestRepository_CommitsBetweenIDs(t *testing.T) {
83+
bareRepo1Path := filepath.Join(testReposDir, "repo4_commitsbetween")
84+
bareRepo1, err := OpenRepository(bareRepo1Path)
85+
assert.NoError(t, err)
86+
defer bareRepo1.Close()
87+
88+
cases := []struct {
89+
OldID string
90+
NewID string
91+
ExpectedCommits int
92+
}{
93+
{"fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", "78a445db1eac62fe15e624e1137965969addf344", 1}, //com1 -> com2
94+
{"78a445db1eac62fe15e624e1137965969addf344", "fdc1b615bdcff0f0658b216df0c9209e5ecb7c78", 0}, //reset HEAD~, com2 -> com1
95+
{"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, //com2 -> com2_new
96+
}
97+
for i, c := range cases {
98+
commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
99+
assert.NoError(t, err)
100+
assert.Equal(t, c.ExpectedCommits, commits.Len(), "case %d", i)
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = false
4+
bare = false
5+
logallrefupdates = true
6+
symlinks = false
7+
ignorecase = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0000000000000000000000000000000000000000 fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 KN4CK3R <admin@oldschoolhack.me> 1624915979 +0200 commit (initial): com1
2+
fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 78a445db1eac62fe15e624e1137965969addf344 KN4CK3R <admin@oldschoolhack.me> 1624915993 +0200 commit: com2
3+
78a445db1eac62fe15e624e1137965969addf344 fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 KN4CK3R <admin@oldschoolhack.me> 1624916008 +0200 reset: moving to HEAD~1
4+
fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca KN4CK3R <admin@oldschoolhack.me> 1624916029 +0200 commit: com2_new
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0000000000000000000000000000000000000000 fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 KN4CK3R <admin@oldschoolhack.me> 1624915979 +0200 commit (initial): com1
2+
fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 78a445db1eac62fe15e624e1137965969addf344 KN4CK3R <admin@oldschoolhack.me> 1624915993 +0200 commit: com2
3+
78a445db1eac62fe15e624e1137965969addf344 fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 KN4CK3R <admin@oldschoolhack.me> 1624916008 +0200 reset: moving to HEAD~1
4+
fdc1b615bdcff0f0658b216df0c9209e5ecb7c78 a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca KN4CK3R <admin@oldschoolhack.me> 1624916029 +0200 commit: com2_new
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
x��M
2+
�0@a�=E��̤I������$�Zl�����|�G���)�îm̊uO�"����&`�8Gt�I�7�#n�6%�09�)�8�F�(hl��@���MuS��\����1�y=�%?i�u�"��O
3+
��Dm�ڃ��w���ź{p�C_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca

0 commit comments

Comments
 (0)