Skip to content

Commit affe773

Browse files
committed
Make the code simpler
1 parent 450b309 commit affe773

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

models/git/branch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Br
174174
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches)
175175
}
176176

177+
func GetExistBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) {
178+
branches := make([]*Branch, 0, len(branchNames))
179+
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).
180+
And("is_deleted=?", false).
181+
In("name", branchNames).Find(&branches)
182+
}
183+
177184
func AddBranches(ctx context.Context, branches []*Branch) error {
178185
for _, branch := range branches {
179186
if _, err := db.GetEngine(ctx).Insert(branch); err != nil {

routers/web/repo/actions/actions.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,28 +281,22 @@ func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error {
281281
refName := git.RefName(run.Ref)
282282
if refName.IsBranch() {
283283
branchName := refName.ShortName()
284+
run.IsRefDeleted = true // assume it's deleted then if it's found in the database it's not deleted
284285
branchRuns[branchName] = append(branchRuns[branchName], run)
285286
branches.Add(branchName)
286287
}
287288
}
288289
if len(branches) == 0 {
289290
return nil
290291
}
291-
var branchInfos []*git_model.Branch
292-
if err := db.GetEngine(ctx).Where("repo_id = ?", ctx.Repo.Repository.ID).
293-
In("name", branches.Values()).Find(&branchInfos); err != nil {
292+
branchInfos, err := git_model.GetExistBranches(ctx, ctx.Repo.Repository.ID, branches.Values())
293+
if err != nil {
294294
return err
295295
}
296-
for branch, branchRun := range branchRuns {
297-
exist := false
298-
for _, branchInfo := range branchInfos {
299-
if branchInfo.Name == branch {
300-
exist = !branchInfo.IsDeleted
301-
break
302-
}
303-
}
296+
for _, branchInfo := range branchInfos {
297+
branchRun := branchRuns[branchInfo.Name]
304298
for _, br := range branchRun {
305-
br.IsRefDeleted = !exist
299+
br.IsRefDeleted = false
306300
}
307301
}
308302
return nil

0 commit comments

Comments
 (0)