Skip to content

Commit 05e7c0b

Browse files
lunnyGiteaBot
authored andcommitted
Remove unnecessary syncbranchToDB with tests (go-gitea#28624)
go-gitea#28361 introduced `syncBranchToDB` in `CreateNewBranchFromCommit`. This PR will revert the change because it's unnecessary. Every push will already be checked by `syncBranchToDB`. This PR also created a test to ensure it's right.
1 parent bf98373 commit 05e7c0b

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

services/repository/branch.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,28 +281,17 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo
281281
return err
282282
}
283283

284-
return db.WithTx(ctx, func(ctx context.Context) error {
285-
commit, err := gitRepo.GetCommit(commitID)
286-
if err != nil {
287-
return err
288-
}
289-
// database operation should be done before git operation so that we can rollback if git operation failed
290-
if err := syncBranchToDB(ctx, repo.ID, doer.ID, branchName, commit); err != nil {
284+
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
285+
Remote: repo.RepoPath(),
286+
Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
287+
Env: repo_module.PushingEnvironment(doer, repo),
288+
}); err != nil {
289+
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
291290
return err
292291
}
293-
294-
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
295-
Remote: repo.RepoPath(),
296-
Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
297-
Env: repo_module.PushingEnvironment(doer, repo),
298-
}); err != nil {
299-
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
300-
return err
301-
}
302-
return fmt.Errorf("push: %w", err)
303-
}
304-
return nil
305-
})
292+
return fmt.Errorf("push: %w", err)
293+
}
294+
return nil
306295
}
307296

308297
// RenameBranch rename a branch

tests/integration/api_branch_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"testing"
1010

1111
auth_model "code.gitea.io/gitea/models/auth"
12+
"code.gitea.io/gitea/models/db"
13+
git_model "code.gitea.io/gitea/models/git"
1214
api "code.gitea.io/gitea/modules/structs"
1315
"code.gitea.io/gitea/tests"
1416

@@ -212,3 +214,37 @@ func TestAPIBranchProtection(t *testing.T) {
212214
testAPIDeleteBranch(t, "master", http.StatusForbidden)
213215
testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
214216
}
217+
218+
func TestAPICreateBranchWithSyncBranches(t *testing.T) {
219+
defer tests.PrepareTestEnv(t)()
220+
221+
branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
222+
RepoID: 1,
223+
})
224+
assert.NoError(t, err)
225+
assert.Len(t, branches, 4)
226+
227+
// make a broke repository with no branch on database
228+
_, err = db.DeleteByBean(db.DefaultContext, git_model.Branch{RepoID: 1})
229+
assert.NoError(t, err)
230+
231+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
232+
ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
233+
giteaURL.Path = ctx.GitPath()
234+
235+
testAPICreateBranch(t, ctx.Session, "user2", "repo1", "", "new_branch", http.StatusCreated)
236+
})
237+
238+
branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
239+
RepoID: 1,
240+
})
241+
assert.NoError(t, err)
242+
assert.Len(t, branches, 5)
243+
244+
branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
245+
RepoID: 1,
246+
Keyword: "new_branch",
247+
})
248+
assert.NoError(t, err)
249+
assert.Len(t, branches, 1)
250+
}

0 commit comments

Comments
 (0)