From cf6e770257f6138ed633c0d18743ee36d150fe42 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 19 Mar 2024 10:29:16 +0800 Subject: [PATCH 1/4] chore: improve codes --- routers/private/hook_post_receive.go | 4 +--- services/repository/branch.go | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/routers/private/hook_post_receive.go b/routers/private/hook_post_receive.go index 3dad39f7b1df7..52f111bcffe12 100644 --- a/routers/private/hook_post_receive.go +++ b/routers/private/hook_post_receive.go @@ -129,9 +129,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { commitIDs = append(commitIDs, update.NewCommitID) } - if err := repo_service.SyncBranchesToDB(ctx, repo.ID, opts.UserID, branchNames, commitIDs, func(commitID string) (*git.Commit, error) { - return gitRepo.GetCommit(commitID) - }); err != nil { + if err := repo_service.SyncBranchesToDB(ctx, repo.ID, opts.UserID, branchNames, commitIDs, gitRepo.GetCommit); err != nil { ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{ Err: fmt.Sprintf("Failed to sync branch to DB in repository: %s/%s Error: %v", ownerName, repoName, err), }) diff --git a/services/repository/branch.go b/services/repository/branch.go index 8d8cfa2d19d82..db1f695a7c5d2 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -318,11 +318,11 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames, for i, branchName := range branchNames { commitID := commitIDs[i] branch, exist := branchMap[branchName] - if exist && branch.CommitID == commitID { + if exist && branch.CommitID == commitID { // FIXME: TBD continue } - commit, err := getCommit(branchName) + commit, err := getCommit(commitID) if err != nil { return fmt.Errorf("get commit of %s failed: %v", branchName, err) } From 005a0e3c0153984543a6f97f31ed92de4c5d61c8 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 19 Mar 2024 10:43:04 +0800 Subject: [PATCH 2/4] test: push deleted branch --- tests/integration/git_push_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/integration/git_push_test.go b/tests/integration/git_push_test.go index cb2910b175e8d..0a357248077ea 100644 --- a/tests/integration/git_push_test.go +++ b/tests/integration/git_push_test.go @@ -69,6 +69,23 @@ func testGitPush(t *testing.T, u *url.URL) { return pushed, deleted }) }) + + t.Run("Push to deleted branch", func(t *testing.T) { + runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { + doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete + pushed = append(pushed, "master") + + doGitCreateBranch(gitPath, "branch-1")(t) + doGitPushTestRepository(gitPath, "origin", "branch-1")(t) + pushed = append(pushed, "branch-1") + + // delete and restore + doGitPushTestRepository(gitPath, "origin", "--delete", "branch-1")(t) + doGitPushTestRepository(gitPath, "origin", "branch-1")(t) + + return pushed, deleted + }) + }) } func runTestGitPush(t *testing.T, u *url.URL, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) { From 0fe72b5dc7acf1d8cc9563fd1df7db657e49c6c9 Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 19 Mar 2024 10:50:46 +0800 Subject: [PATCH 3/4] fix: update deleted branch --- services/repository/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index db1f695a7c5d2..db7acdb5052e6 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -318,7 +318,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames, for i, branchName := range branchNames { commitID := commitIDs[i] branch, exist := branchMap[branchName] - if exist && branch.CommitID == commitID { // FIXME: TBD + if exist && branch.CommitID == commitID && !branch.IsDeleted { continue } From 3d553590bd4f1fc3030c4a46a35220eb9d4f8bba Mon Sep 17 00:00:00 2001 From: Jason Song Date: Tue, 19 Mar 2024 11:13:47 +0800 Subject: [PATCH 4/4] chore: add comments --- routers/private/hook_post_receive.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routers/private/hook_post_receive.go b/routers/private/hook_post_receive.go index 52f111bcffe12..a09956f73865d 100644 --- a/routers/private/hook_post_receive.go +++ b/routers/private/hook_post_receive.go @@ -75,6 +75,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { updates = append(updates, option) if repo.IsEmpty && (refFullName.BranchName() == "master" || refFullName.BranchName() == "main") { // put the master/main branch first + // FIXME: It doesn't always work, since the master/main branch may not be the first batch of updates. + // If the user pushes many branches at once, the Git hook will call the internal API in batches, rather than all at once. + // See https://github.com/go-gitea/gitea/blob/cb52b17f92e2d2293f7c003649743464492bca48/cmd/hook.go#L27 + // If the user executes `git push origin --all` and pushes more than 30 branches, the master/main may not be the default branch. copy(updates[1:], updates) updates[0] = option }