refactor(git): clarify GetBranch behavior to make it only gets an existing branch (#38662)

`GetBranch` silently returned soft-deleted branches, contradicting
`IsBranchExist` and forcing callers to manually check `branch.IsDeleted`
everywhere.

Refactored it to `GetBranchExisting` to have a clear behavior: it only
returns the existing branch.

---------

Signed-off-by: Sudhanshu Singh <sudhanshuwriterblc@gmail.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Shudhanshu Singh
2026-07-27 23:15:27 +05:30
committed by GitHub
parent 94a2c3ec18
commit d47f9f37d6
12 changed files with 55 additions and 101 deletions
+5 -5
View File
@@ -42,7 +42,7 @@ func testScheduleUpdatePush(t *testing.T) {
doTestScheduleUpdate(t, func(t *testing.T, u *url.URL, testContext APITestContext, user *user_model.User, repo *repo_model.Repository) (commitID, expectedSpec string) {
newCron := "30 5 * * 1,3"
pushScheduleChange(t, u, repo, newCron)
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
return branch.CommitID, newCron
})
@@ -187,13 +187,13 @@ func testScheduleUpdateMirrorSync(t *testing.T) {
// update remote repo
newCron := "30 5,17 * * 2,4"
pushScheduleChange(t, u, repo, newCron)
repoDefaultBranch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
repoDefaultBranch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
// sync
ok := mirror_service.SyncPullMirror(t.Context(), mirrorRepo.ID)
assert.True(t, ok)
mirrorRepoDefaultBranch, err := git_model.GetBranch(t.Context(), mirrorRepo.ID, mirrorRepo.DefaultBranch)
mirrorRepoDefaultBranch, err := git_model.GetBranchExisting(t.Context(), mirrorRepo.ID, mirrorRepo.DefaultBranch)
assert.NoError(t, err)
assert.Equal(t, repoDefaultBranch.CommitID, mirrorRepoDefaultBranch.CommitID)
@@ -215,7 +215,7 @@ func testScheduleUpdateArchiveAndUnarchive(t *testing.T) {
doAPIEditRepository(testContext, &api.EditRepoOption{
Archived: new(false),
})(t)
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
return branch.CommitID, "@every 1m"
})
@@ -230,7 +230,7 @@ func testScheduleUpdateDisableAndEnableActionsUnit(t *testing.T) {
doAPIEditRepository(testContext, &api.EditRepoOption{
HasActions: new(true),
})(t)
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
return branch.CommitID, "@every 1m"
})
+8 -8
View File
@@ -480,7 +480,7 @@ jobs:
gitRepo, err := git.OpenRepository(repo)
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
// create a branch
@@ -964,7 +964,7 @@ jobs:
gitRepo, err := git.OpenRepository(repo)
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
values := url.Values{}
values.Set("ref", "main")
@@ -1135,7 +1135,7 @@ jobs:
gitRepo, err := git.OpenRepository(repo)
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
values := url.Values{}
values.Set("ref", "main")
@@ -1226,7 +1226,7 @@ jobs:
gitRepo, err := git.OpenRepository(repo)
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
inputs := &api.CreateActionWorkflowDispatch{
Ref: "main",
@@ -1312,7 +1312,7 @@ jobs:
gitRepo, err := git.OpenRepository(repo)
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
inputs := &api.CreateActionWorkflowDispatch{
Ref: "main",
@@ -1640,7 +1640,7 @@ jobs:
gitRepo, err := git.OpenRepository(repo)
assert.NoError(t, err)
defer gitRepo.Close()
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
inputs = &api.CreateActionWorkflowDispatch{
Ref: "main",
@@ -1811,7 +1811,7 @@ jobs:
defer gitRepo.Close()
// Get the commit ID of the default branch
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
// create a branch
@@ -1889,7 +1889,7 @@ jobs:
defer gitRepo.Close()
// Get the commit ID of the default branch
branch, err := git_model.GetBranch(t.Context(), repo.ID, repo.DefaultBranch)
branch, err := git_model.GetBranchExisting(t.Context(), repo.ID, repo.DefaultBranch)
assert.NoError(t, err)
// create a branch
+1 -1
View File
@@ -242,7 +242,7 @@ func TestPullSquashWithHeadCommitID(t *testing.T) {
resp := testPullCreate(t, session, "user1", "repo1", false, "master", "master", "This is a pull title")
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: "repo1"})
headBranch, err := git_model.GetBranch(t.Context(), repo1.ID, "master")
headBranch, err := git_model.GetBranchExisting(t.Context(), repo1.ID, "master")
assert.NoError(t, err)
assert.NotNil(t, headBranch)