]> source.dussan.org Git - gitea.git/commitdiff
Fix push multiple branches error with tests (#31151) (#31153)
authorGiteabot <teabot@gitea.io>
Thu, 30 May 2024 07:04:42 +0000 (15:04 +0800)
committerGitHub <noreply@github.com>
Thu, 30 May 2024 07:04:42 +0000 (07:04 +0000)
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>
services/repository/branch.go
tests/integration/git_helper_for_declarative_test.go
tests/integration/git_push_test.go

index d74e5819a1c453bea88623d8c2b28a40f987a35b..bc97f3f6321aabf424bfb340591344ae3d130b91 100644 (file)
@@ -332,7 +332,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
                                if _, err := git_model.UpdateBranch(ctx, repoID, pusherID, branchName, commit); err != nil {
                                        return fmt.Errorf("git_model.UpdateBranch %d:%s failed: %v", repoID, branchName, err)
                                }
-                               return nil
+                               continue
                        }
 
                        // if database have branches but not this branch, it means this is a new branch
index 77fe07128e75a4e2aacbc011aab24c3d11100fe4..d1d935da4f9d828e200d4bd593614587b7dad348 100644 (file)
@@ -160,6 +160,24 @@ func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T
        }
 }
 
+func doGitAddSomeCommits(dstPath, branch string) func(*testing.T) {
+       return func(t *testing.T) {
+               doGitCheckoutBranch(dstPath, branch)(t)
+
+               assert.NoError(t, os.WriteFile(filepath.Join(dstPath, fmt.Sprintf("file-%s.txt", branch)), []byte(fmt.Sprintf("file %s", branch)), 0o644))
+               assert.NoError(t, git.AddChanges(dstPath, true))
+               signature := git.Signature{
+                       Email: "test@test.test",
+                       Name:  "test",
+               }
+               assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
+                       Committer: &signature,
+                       Author:    &signature,
+                       Message:   fmt.Sprintf("update %s", branch),
+               }))
+       }
+}
+
 func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
        return func(t *testing.T) {
                _, _, err := git.NewCommand(git.DefaultContext, "checkout", "-b").AddDynamicArguments(branch).RunStdString(&git.RunOpts{Dir: dstPath})
index b37fb02444e7265f16e35ddc5fbcca87c5f55400..da254fc88f6536b610e546b2e6a6e9adb813ab7b 100644 (file)
@@ -37,6 +37,41 @@ func testGitPush(t *testing.T, u *url.URL) {
                })
        })
 
+       t.Run("Push branches exists", func(t *testing.T) {
+               runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
+                       for i := 0; i < 10; i++ {
+                               branchName := fmt.Sprintf("branch-%d", i)
+                               if i < 5 {
+                                       pushed = append(pushed, branchName)
+                               }
+                               doGitCreateBranch(gitPath, branchName)(t)
+                       }
+                       // only push master and the first 5 branches
+                       pushed = append(pushed, "master")
+                       args := append([]string{"origin"}, pushed...)
+                       doGitPushTestRepository(gitPath, args...)(t)
+
+                       pushed = pushed[:0]
+                       // do some changes for the first 5 branches created above
+                       for i := 0; i < 5; i++ {
+                               branchName := fmt.Sprintf("branch-%d", i)
+                               pushed = append(pushed, branchName)
+
+                               doGitAddSomeCommits(gitPath, branchName)(t)
+                       }
+
+                       for i := 5; i < 10; i++ {
+                               pushed = append(pushed, fmt.Sprintf("branch-%d", i))
+                       }
+                       pushed = append(pushed, "master")
+
+                       // push all, so that master are not chagned
+                       doGitPushTestRepository(gitPath, "origin", "--all")(t)
+
+                       return pushed, deleted
+               })
+       })
+
        t.Run("Push branches one by one", func(t *testing.T) {
                runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
                        for i := 0; i < 100; i++ {