Skip to content

Commit 808e879

Browse files
committed
Some improvements
1 parent 635e7ae commit 808e879

File tree

3 files changed

+76
-41
lines changed

3 files changed

+76
-41
lines changed

routers/private/hook_pre_receive.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"code.gitea.io/gitea/modules/gitrepo"
2222
"code.gitea.io/gitea/modules/log"
2323
"code.gitea.io/gitea/modules/private"
24+
"code.gitea.io/gitea/modules/util"
2425
"code.gitea.io/gitea/modules/web"
26+
"code.gitea.io/gitea/services/agit"
2527
gitea_context "code.gitea.io/gitea/services/context"
2628
pull_service "code.gitea.io/gitea/services/pull"
2729
)
@@ -452,40 +454,18 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) {
452454
return
453455
}
454456

455-
baseBranchName := refFullName.ForBranchName()
456-
457-
baseBranchExist, err := git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, baseBranchName)
457+
_, _, err := agit.GetAgitBranchInfo(ctx, ctx.Repo.Repository.ID, refFullName.ForBranchName())
458458
if err != nil {
459-
ctx.JSON(http.StatusInternalServerError, private.Response{
460-
Err: err.Error(),
461-
})
462-
return
463-
}
464-
465-
if !baseBranchExist {
466-
for p, v := range baseBranchName {
467-
if v != '/' || p == len(baseBranchName)-1 {
468-
continue
469-
}
470-
baseBranchExist, err = git_model.IsBranchExist(ctx, ctx.Repo.Repository.ID, baseBranchName[:p])
471-
if err != nil {
472-
ctx.JSON(http.StatusInternalServerError, private.Response{
473-
Err: err.Error(),
474-
})
475-
return
476-
}
477-
if baseBranchExist {
478-
break
479-
}
459+
if !errors.Is(err, util.ErrNotExist) {
460+
ctx.JSON(http.StatusForbidden, private.Response{
461+
UserMsg: fmt.Sprintf("Unexpected ref: %s", refFullName),
462+
})
463+
} else {
464+
ctx.JSON(http.StatusInternalServerError, private.Response{
465+
Err: err.Error(),
466+
})
480467
}
481468
}
482-
483-
if !baseBranchExist {
484-
ctx.JSON(http.StatusForbidden, private.Response{
485-
UserMsg: fmt.Sprintf("Unexpected ref: %s", refFullName),
486-
})
487-
return
488-
}
489469
}
490470

491471
func generateGitEnv(opts *private.HookOptions) (env []string) {

services/agit/agit.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package agit
66
import (
77
"context"
88
"encoding/base64"
9+
"errors"
910
"fmt"
1011
"strings"
1112

@@ -32,6 +33,34 @@ func parseAgitPushOptionValue(s string) string {
3233
return s
3334
}
3435

36+
func GetAgitBranchInfo(ctx context.Context, repoID int64, baseBranchName string) (string, string, error) {
37+
baseBranchExist, err := git_model.IsBranchExist(ctx, repoID, baseBranchName)
38+
if err != nil {
39+
return "", "", err
40+
}
41+
if baseBranchExist {
42+
return baseBranchName, "", nil
43+
}
44+
45+
// try match <target-branch>/<topic-branch>
46+
// refs/for have been trimmed to get baseBranchName
47+
for p, v := range baseBranchName {
48+
if v != '/' {
49+
continue
50+
}
51+
52+
baseBranchExist, err := git_model.IsBranchExist(ctx, repoID, baseBranchName[:p])
53+
if err != nil {
54+
return "", "", err
55+
}
56+
if baseBranchExist {
57+
return baseBranchName[:p], baseBranchName[p+1:], nil
58+
}
59+
}
60+
61+
return "", "", util.NewNotExistErrorf("base branch does not exist")
62+
}
63+
3564
// ProcReceive handle proc receive work
3665
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
3766
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
@@ -70,17 +99,19 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
7099
continue
71100
}
72101

73-
baseBranchName := opts.RefFullNames[i].ForBranchName()
74-
currentTopicBranch := ""
75-
if exist, _ := git_model.IsBranchExist(ctx, repo.ID, baseBranchName); !exist {
76-
// try match refs/for/<target-branch>/<topic-branch>
77-
for p, v := range baseBranchName {
78-
if v == '/' && gitrepo.IsBranchExist(ctx, repo, baseBranchName[:p]) && p != len(baseBranchName)-1 {
79-
currentTopicBranch = baseBranchName[p+1:]
80-
baseBranchName = baseBranchName[:p]
81-
break
82-
}
102+
baseBranchName, currentTopicBranch, err := GetAgitBranchInfo(ctx, repo.ID, opts.RefFullNames[i].ForBranchName())
103+
if err != nil {
104+
if !errors.Is(err, util.ErrNotExist) {
105+
return nil, fmt.Errorf("failed to get branch information. Error: %w", err)
83106
}
107+
// If branch does not exist, we can continue
108+
results = append(results, private.HookProcReceiveRefResult{
109+
OriginalRef: opts.RefFullNames[i],
110+
OldOID: opts.OldCommitIDs[i],
111+
NewOID: opts.NewCommitIDs[i],
112+
Err: "base-branch does not exist",
113+
})
114+
continue
84115
}
85116

86117
if len(topicBranch) == 0 && len(currentTopicBranch) == 0 {

services/agit/agit_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,35 @@ package agit
66
import (
77
"testing"
88

9+
"code.gitea.io/gitea/models/unittest"
10+
"code.gitea.io/gitea/modules/util"
11+
912
"github.com/stretchr/testify/assert"
1013
)
1114

15+
func TestMain(m *testing.M) {
16+
unittest.MainTest(m)
17+
}
18+
1219
func TestParseAgitPushOptionValue(t *testing.T) {
1320
assert.Equal(t, "a", parseAgitPushOptionValue("a"))
1421
assert.Equal(t, "a", parseAgitPushOptionValue("{base64}YQ=="))
1522
assert.Equal(t, "{base64}invalid value", parseAgitPushOptionValue("{base64}invalid value"))
1623
}
24+
25+
func TestGetAgitBranchInfo(t *testing.T) {
26+
assert.NoError(t, unittest.PrepareTestDatabase())
27+
28+
_, _, err := GetAgitBranchInfo(t.Context(), 1, "non-exist-basebranch")
29+
assert.ErrorIs(t, err, util.ErrNotExist)
30+
31+
baseBranch, currentTopicBranch, err := GetAgitBranchInfo(t.Context(), 1, "master")
32+
assert.NoError(t, err)
33+
assert.Equal(t, "master", baseBranch)
34+
assert.Empty(t, currentTopicBranch)
35+
36+
baseBranch, currentTopicBranch, err = GetAgitBranchInfo(t.Context(), 1, "master/topicbranch")
37+
assert.NoError(t, err)
38+
assert.Equal(t, "master", baseBranch)
39+
assert.Equal(t, "topicbranch", currentTopicBranch)
40+
}

0 commit comments

Comments
 (0)