Skip to content
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

Fix verifyCommits error when push a new branch #26664

Merged
merged 22 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion models/fixtures/email_address.yml
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,12 @@
email: user2-2@example.com
lower_email: user2-2@example.com
is_activated: false
is_primary: false
is_primary: false

-
id: 36
uid: 36
email: 1290147055@qq.com
lower_email: 1290147055@qq.com
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
is_activated: true
is_primary: false
24 changes: 23 additions & 1 deletion models/fixtures/gpg_key.yml
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
[] # empty
-
id: 5
owner_id: 36
key_id: 2ECAAA351B755A40
primary_key_id:
content: xsDNBGTjB1cBDACv7YEaiA5VaoTBR0fcPRy+RHgTd88ALhklVVd54lbMKWwsHa3Ms9sCYbq8chly4g50l45o8qL0mu+zkZDvrU3EsZAwFwv/6iKltQXRIQ7j0uNKIyJi/+yyMiRJtU24ptAkc+bHhb2wi7cnJtldGyDZIoymzotTdpJupmjVFXqQtbeqMHpSKg6JdyGLK+wX+ITD7gZ6UddPPbw5Yo6N5b56w55cSIG6cDtafo+/Fc4WF0jdDDmCEyPlr3afDKEcpmnDSJsMhm7/eZ5MRn2dh9+brnEFyxkKSJYfI7ptA6trAs+DCil9FgZbXM9P/c0joVUzi1lSGSu1ryav9eAb4J5lgzl2Tad338+cVWuCHR6yTbq4qZcfgNVnFK1WMUrg9MF2A8wNLO1O9LSQpXwDSBmnM9W7TcTE6bpxbADVH7WEScCNmExKeqUnvvypaeiwmwz7Jqh/V5NhjE3ySgo3j/oBFFUp2SSkZhk7p0uP8mDLSdfPLxtBUv3EQAPs8CCD7WkAEQEAAQ==
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
verified: true
can_sign: true
can_encrypt_comms: true
can_encrypt_storage: true
can_certify: true

-
id: 6
owner_id: 36
key_id: 2FCCED20E8714BAA
primary_key_id: 2ECAAA351B755A40
content: zsDNBGTjB1cBDADm2EFZ6SzUoF5We4kQBaLYtkFNRA2yQafbu+W/AYOITgxye2xDKVcMaopV6B+vPXKyIshakWkdpK8Xw5dZGI0loOm5PXFJDI6lzWWUy76J3SevenCXzxWOCXZOtI25Zpev/b96Cu7xsRdcL6eYpGRNwVJ3bW3AhD5z2AU0z05VYKrNIzs5e7PfO9g7uGQ5DuejLr6Wp9+zHfp6ppHkHFET7x0ZrNE+hH1hPAK0yFQ75iU4zvNXxa2PKgfSBkEADre+EV0+BxlRB9c9jkLWKa/Xa4ecaiFgH/ler12gr8/NOCJww+bLQapJBTKSGycupANwIZb1lgna1r09P6PCp25MBrvVWfD9qzvVmFxpM+e8/y9D3RcJkHwJjQ9SaX/gNq/nFdvVsLpIMGGQY0KDMeJW/XkwhGNZ/tuztLgqSIcbNuVZMooOwgAT9r1Z1XpKuuh+mNTwwzEzvV5Qo6Kd/9/zPqJmoniziNxyzfEQ82HhOMZg3CJVGenPPhIHXCJAO3kAEQEAAQ==
verified: true
can_sign: true
can_encrypt_comms: true
can_encrypt_storage: true
can_certify: true
4 changes: 2 additions & 2 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@
lower_name: limited_org36
name: limited_org36
full_name: Limited Org 36
email: limited_org36@example.com
email: 1290147055@qq.com
wxiaoguang marked this conversation as resolved.
Show resolved Hide resolved
keep_email_private: false
email_notifications_preference: enabled
passwd: ZogKvWdyEx:password
Expand All @@ -1320,7 +1320,7 @@
allow_create_organization: true
prohibit_login: false
avatar: avatar22
avatar_email: limited_org36@example.com
avatar_email: 1290147055@qq.com
use_custom_avatar: false
num_followers: 0
num_following: 0
Expand Down
40 changes: 24 additions & 16 deletions routers/private/hook_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env []
_ = stdoutWriter.Close()
}()

var command *git.Command
if oldCommitID == git.EmptySHA {
// When creating a new branch, the oldCommitID is empty, by using "newCommitID --not --all":
// List commits that are reachable by following the newCommitID, exclude "all" existing heads/tags commits
// So, it only lists the new commits received, doesn't list the commits already present in the receiving repository
command = git.NewCommand(repo.Ctx, "rev-list").AddDynamicArguments(newCommitID).AddArguments("--not", "--all")
CaiCandong marked this conversation as resolved.
Show resolved Hide resolved
} else {
command = git.NewCommand(repo.Ctx, "rev-list").AddDynamicArguments(oldCommitID + "..." + newCommitID)
}
// This is safe as force pushes are already forbidden
err = git.NewCommand(repo.Ctx, "rev-list").AddDynamicArguments(oldCommitID + "..." + newCommitID).
Run(&git.RunOpts{
Env: env,
Dir: repo.Path,
Stdout: stdoutWriter,
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
_ = stdoutWriter.Close()
err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
if err != nil {
log.Error("%v", err)
cancel()
}
_ = stdoutReader.Close()
return err
},
})
err = command.Run(&git.RunOpts{
Env: env,
Dir: repo.Path,
Stdout: stdoutWriter,
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
_ = stdoutWriter.Close()
err := readAndVerifyCommitsFromShaReader(stdoutReader, repo, env)
if err != nil {
log.Error("%v", err)
cancel()
}
_ = stdoutReader.Close()
return err
},
})
if err != nil && !isErrUnverifiedCommit(err) {
log.Error("Unable to check commits from %s to %s in %s: %v", oldCommitID, newCommitID, repo.Path, err)
}
Expand Down
43 changes: 43 additions & 0 deletions routers/private/hook_verification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package private

import (
"context"
"testing"

"code.gitea.io/gitea/models/unittest"
"code.gitea.io/gitea/modules/git"

"github.com/stretchr/testify/assert"
)

var testReposDir = "tests/repos/"

func TestVerifyCommits(t *testing.T) {
unittest.PrepareTestEnv(t)

gitRepo, err := git.OpenRepository(context.Background(), testReposDir+"repo1_hook_verification")
CaiCandong marked this conversation as resolved.
Show resolved Hide resolved
defer gitRepo.Close()
assert.NoError(t, err)

testCases := []struct {
base, head string
verified bool
}{
{"7d92f8f0cf7b989acf2c56a9a4387f5b39a92c41", "64e643f86a640d97f95820b43aeb28631042c629", true},
{git.EmptySHA, "451c1608ab9c5277fdc73ea68aea711c046ac7ac", true}, // New branch with verified commit
{"e05ec071e40a53cd4785345a99fd12f5dbb6a777", "7d92f8f0cf7b989acf2c56a9a4387f5b39a92c41", false},
{git.EmptySHA, "1128cd8e82948ea10b4932b2481210610604ce7e", false}, // New branch with unverified commit
}

for _, tc := range testCases {
err = verifyCommits(tc.base, tc.head, gitRepo, nil)
if tc.verified {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
}
}
17 changes: 17 additions & 0 deletions routers/private/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package private

import (
"path/filepath"
"testing"

"code.gitea.io/gitea/models/unittest"
)

func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
GiteaRootPath: filepath.Join("..", ".."),
})
}
1 change: 1 addition & 0 deletions routers/private/tests/repos/repo1_hook_verification/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/main
6 changes: 6 additions & 0 deletions routers/private/tests/repos/repo1_hook_verification/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[core]
repositoryformatversion = 0
filemode = false
bare = true
symlinks = false
ignorecase = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64e643f86a640d97f95820b43aeb28631042c629 refs/heads/main
2 changes: 2 additions & 0 deletions routers/private/tests/repos/repo1_hook_verification/logs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 7d92f8f0cf7b989acf2c56a9a4387f5b39a92c41 Gitea <gitea@fake.local> 1692868984 +0800 push
7d92f8f0cf7b989acf2c56a9a4387f5b39a92c41 64e643f86a640d97f95820b43aeb28631042c629 Gitea <gitea@fake.local> 1692869253 +0800 push
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 7d92f8f0cf7b989acf2c56a9a4387f5b39a92c41 Gitea <gitea@fake.local> 1692868984 +0800 push
7d92f8f0cf7b989acf2c56a9a4387f5b39a92c41 64e643f86a640d97f95820b43aeb28631042c629 Gitea <gitea@fake.local> 1692869253 +0800 push
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x��Kj1��)�6��>!������[�,fd2$��n`�zԃ��ۺ.�b����Zd���0N��$�:plPR,1Gs+�n�*���z(4��c��Sɹ��J��My�߶�sY�e��]��� �G ��ߏs[Oօ�)��~B0�}]�7M�|�ݘ�r�T�����0O��N�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
64e643f86a640d97f95820b43aeb28631042c629