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
+2 -6
View File
@@ -492,12 +492,8 @@ func viewSummaryBranchFromRun(ctx context.Context, run *actions_model.ActionRun,
Link: run.RefLink(),
}
if refName.IsBranch() {
b, err := git_model.GetBranch(ctx, run.RepoID, refName.ShortName())
if err != nil && !git_model.IsErrBranchNotExist(err) {
log.Error("GetBranch: %v", err)
} else if git_model.IsErrBranchNotExist(err) || (b != nil && b.IsDeleted) {
branch.IsDeleted = true
}
refBranchExists, _ := git_model.IsBranchExist(ctx, run.RepoID, refName.ShortName())
branch.IsDeleted = !refBranchExists
}
return branch
}
+3 -8
View File
@@ -173,14 +173,9 @@ func (prInfo *pullRequestViewInfo) setTemplateDataMergeTarget(ctx *context.Conte
ctx.Data["BaseTarget"] = pull.BaseBranch
headBranchLink := ""
if pull.Flow == issues_model.PullRequestFlowGithub {
b, err := git_model.GetBranch(ctx, pull.HeadRepoID, pull.HeadBranch)
switch {
case err == nil:
if !b.IsDeleted {
headBranchLink = pull.GetHeadBranchLink(ctx)
}
case !git_model.IsErrBranchNotExist(err):
log.Error("GetBranch: %v", err)
headBranchExists, _ := git_model.IsBranchExist(ctx, pull.HeadRepoID, pull.HeadBranch)
if headBranchExists {
headBranchLink = pull.GetHeadBranchLink(ctx)
}
}
ctx.Data["HeadBranchLink"] = headBranchLink
+5 -12
View File
@@ -34,23 +34,16 @@ func checkOutdatedBranch(ctx *context.Context) {
if !(ctx.Repo.Permission.IsAdmin() || ctx.Repo.Permission.IsOwner()) {
return
}
// get the head commit of the branch since ctx.Repo.CommitID is not always the head commit of `ctx.Repo.BranchName`
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx, ctx.Repo.BranchName)
if err != nil {
log.Error("GetBranchCommitID: %v", err)
// Don't return an error page, as it can be rechecked the next time the user opens the page.
if !ctx.Repo.RefFullName.IsBranch() {
return
}
dbBranch, err := git_model.GetBranch(ctx, ctx.Repo.Repository.ID, ctx.Repo.BranchName)
dbBranch, err := git_model.GetBranchExisting(ctx, ctx.Repo.Repository.ID, ctx.Repo.RefFullName.ShortName())
if err != nil {
log.Error("GetBranch: %v", err)
// Don't return an error page, as it can be rechecked the next time the user opens the page.
return
return // ignore the error which can only be "not exists"
}
if dbBranch.CommitID != commit.ID.String() {
if dbBranch.CommitID != ctx.Repo.CommitID {
ctx.Flash.Warning(ctx.Tr("repo.error.broken_git_hook", "https://docs.gitea.com/help/faq#push-hook--webhook--actions-arent-running"), true)
}
}
@@ -446,13 +439,13 @@ func Home(ctx *context.Context) {
prepareFuncs := []func(*context.Context){
prepareClonePanel,
prepareHomeSidebarRepoTopics,
checkOutdatedBranch,
prepareToRenderDirOrFile(entry),
prepareRecentlyPushedNewBranches,
}
if isTreePathRoot {
prepareFuncs = append(prepareFuncs,
checkOutdatedBranch,
prepareUpstreamDivergingInfo,
prepareHomeSidebarLicenses,
prepareHomeSidebarCitationFile(entry),