Skip to content

Commit e8c776c

Browse files
GiteaBotlunny
andauthored
Fix push multiple branches error with tests (#31151) (#31153)
Backport #31151 by @lunny Fix #31140 The previous logic is wrong when pushing multiple branches. After first branch updated, it will ignore left other branches sync operations. As a workaround for the repositories, just push a new commit after the patch applied will fix the repositories status. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent cc64d4d commit e8c776c

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

services/repository/branch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
332332
if _, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit); err != nil {
333333
return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
334334
}
335-
return nil
335+
continue
336336
}
337337

338338
// if database have branches but not this branch, it means this is a new branch

tests/integration/git_helper_for_declarative_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T
160160
}
161161
}
162162

163+
func doGitAddSomeCommits(dstPath, branch string) func(*testing.T) {
164+
return func(t *testing.T) {
165+
doGitCheckoutBranch(dstPath, branch)(t)
166+
167+
assert.NoError(t, os.WriteFile(filepath.Join(dstPath, fmt.Sprintf("file-%s.txt", branch)), []byte(fmt.Sprintf("file %s", branch)), 0o644))
168+
assert.NoError(t, git.AddChanges(dstPath, true))
169+
signature := git.Signature{
170+
Email: "test@test.test",
171+
Name: "test",
172+
}
173+
assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
174+
Committer: &signature,
175+
Author: &signature,
176+
Message: fmt.Sprintf("update %s", branch),
177+
}))
178+
}
179+
}
180+
163181
func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
164182
return func(t *testing.T) {
165183
_, _, err := git.NewCommand(git.DefaultContext, "checkout", "-b").AddDynamicArguments(branch).RunStdString(&git.RunOpts{Dir: dstPath})

tests/integration/git_push_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,41 @@ func testGitPush(t *testing.T, u *url.URL) {
3737
})
3838
})
3939

40+
t.Run("Push branches exists", func(t *testing.T) {
41+
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
42+
for i := 0; i < 10; i++ {
43+
branchName := fmt.Sprintf("branch-%d", i)
44+
if i < 5 {
45+
pushed = append(pushed, branchName)
46+
}
47+
doGitCreateBranch(gitPath, branchName)(t)
48+
}
49+
// only push master and the first 5 branches
50+
pushed = append(pushed, "master")
51+
args := append([]string{"origin"}, pushed...)
52+
doGitPushTestRepository(gitPath, args...)(t)
53+
54+
pushed = pushed[:0]
55+
// do some changes for the first 5 branches created above
56+
for i := 0; i < 5; i++ {
57+
branchName := fmt.Sprintf("branch-%d", i)
58+
pushed = append(pushed, branchName)
59+
60+
doGitAddSomeCommits(gitPath, branchName)(t)
61+
}
62+
63+
for i := 5; i < 10; i++ {
64+
pushed = append(pushed, fmt.Sprintf("branch-%d", i))
65+
}
66+
pushed = append(pushed, "master")
67+
68+
// push all, so that master are not chagned
69+
doGitPushTestRepository(gitPath, "origin", "--all")(t)
70+
71+
return pushed, deleted
72+
})
73+
})
74+
4075
t.Run("Push branches one by one", func(t *testing.T) {
4176
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
4277
for i := 0; i < 100; i++ {

0 commit comments

Comments
 (0)