Skip to content

Commit f371f84

Browse files
authored
Restore deleted branches when syncing (#29898)
Regression of #29493. If a branch has been deleted, repushing it won't restore it. Lunny may have noticed that, but I didn't delve into the comment then overlooked it: #29493 (comment) The additional comments added are to explain the issue I found during testing, which are unrelated to the fixes.
1 parent 6ed2c29 commit f371f84

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

routers/private/hook_post_receive.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
7575
updates = append(updates, option)
7676
if repo.IsEmpty && (refFullName.BranchName() == "master" || refFullName.BranchName() == "main") {
7777
// put the master/main branch first
78+
// FIXME: It doesn't always work, since the master/main branch may not be the first batch of updates.
79+
// If the user pushes many branches at once, the Git hook will call the internal API in batches, rather than all at once.
80+
// See https://github.com/go-gitea/gitea/blob/cb52b17f92e2d2293f7c003649743464492bca48/cmd/hook.go#L27
81+
// If the user executes `git push origin --all` and pushes more than 30 branches, the master/main may not be the default branch.
7882
copy(updates[1:], updates)
7983
updates[0] = option
8084
}
@@ -129,9 +133,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
129133
commitIDs = append(commitIDs, update.NewCommitID)
130134
}
131135

132-
if err := repo_service.SyncBranchesToDB(ctx, repo.ID, opts.UserID, branchNames, commitIDs, func(commitID string) (*git.Commit, error) {
133-
return gitRepo.GetCommit(commitID)
134-
}); err != nil {
136+
if err := repo_service.SyncBranchesToDB(ctx, repo.ID, opts.UserID, branchNames, commitIDs, gitRepo.GetCommit); err != nil {
135137
ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{
136138
Err: fmt.Sprintf("Failed to sync branch to DB in repository: %s/%s Error: %v", ownerName, repoName, err),
137139
})

services/repository/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
318318
for i, branchName := range branchNames {
319319
commitID := commitIDs[i]
320320
branch, exist := branchMap[branchName]
321-
if exist && branch.CommitID == commitID {
321+
if exist && branch.CommitID == commitID && !branch.IsDeleted {
322322
continue
323323
}
324324

325-
commit, err := getCommit(branchName)
325+
commit, err := getCommit(commitID)
326326
if err != nil {
327327
return fmt.Errorf("get commit of %s failed: %v", branchName, err)
328328
}

tests/integration/git_push_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ func testGitPush(t *testing.T, u *url.URL) {
6969
return pushed, deleted
7070
})
7171
})
72+
73+
t.Run("Push to deleted branch", func(t *testing.T) {
74+
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
75+
doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
76+
pushed = append(pushed, "master")
77+
78+
doGitCreateBranch(gitPath, "branch-1")(t)
79+
doGitPushTestRepository(gitPath, "origin", "branch-1")(t)
80+
pushed = append(pushed, "branch-1")
81+
82+
// delete and restore
83+
doGitPushTestRepository(gitPath, "origin", "--delete", "branch-1")(t)
84+
doGitPushTestRepository(gitPath, "origin", "branch-1")(t)
85+
86+
return pushed, deleted
87+
})
88+
})
7289
}
7390

7491
func runTestGitPush(t *testing.T, u *url.URL, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) {

0 commit comments

Comments
 (0)