aboutsummaryrefslogtreecommitdiffstats
path: root/services/repository/branch.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/repository/branch.go')
-rw-r--r--services/repository/branch.go37
1 files changed, 28 insertions, 9 deletions
diff --git a/services/repository/branch.go b/services/repository/branch.go
index 08c53bbb6a..dd00ca7dcd 100644
--- a/services/repository/branch.go
+++ b/services/repository/branch.go
@@ -30,6 +30,7 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook"
+ actions_service "code.gitea.io/gitea/services/actions"
notify_service "code.gitea.io/gitea/services/notify"
release_service "code.gitea.io/gitea/services/release"
files_service "code.gitea.io/gitea/services/repository/files"
@@ -302,7 +303,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
// For other batches, it will hit optimization 4.
if len(branchNames) != len(commitIDs) {
- return fmt.Errorf("branchNames and commitIDs length not match")
+ return errors.New("branchNames and commitIDs length not match")
}
return db.WithTx(ctx, func(ctx context.Context) error {
@@ -409,11 +410,11 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
return "target_exist", nil
}
- if gitRepo.IsBranchExist(to) {
+ if gitrepo.IsBranchExist(ctx, repo, to) {
return "target_exist", nil
}
- if !gitRepo.IsBranchExist(from) {
+ if !gitrepo.IsBranchExist(ctx, repo, from) {
return "from_not_exist", nil
}
@@ -452,7 +453,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
log.Error("DeleteCronTaskByRepo: %v", err)
}
// cancel running cron jobs of this repository and delete old schedules
- if err := actions_model.CancelPreviousJobs(
+ if err := actions_service.CancelPreviousJobs(
ctx,
repo.ID,
from,
@@ -617,12 +618,12 @@ func AddAllRepoBranchesToSyncQueue(ctx context.Context) error {
return nil
}
-func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, newBranchName string) error {
+func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, newBranchName string) error {
if repo.DefaultBranch == newBranchName {
return nil
}
- if !gitRepo.IsBranchExist(newBranchName) {
+ if !gitrepo.IsBranchExist(ctx, repo, newBranchName) {
return git_model.ErrBranchNotExist{
BranchName: newBranchName,
}
@@ -639,7 +640,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
log.Error("DeleteCronTaskByRepo: %v", err)
}
// cancel running cron jobs of this repository and delete old schedules
- if err := actions_model.CancelPreviousJobs(
+ if err := actions_service.CancelPreviousJobs(
ctx,
repo.ID,
oldDefaultBranchName,
@@ -662,15 +663,23 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
}
}
+ // clear divergence cache
+ if err := DelRepoDivergenceFromCache(ctx, repo.ID); err != nil {
+ log.Error("DelRepoDivergenceFromCache: %v", err)
+ }
+
notify_service.ChangeDefaultBranch(ctx, repo)
return nil
}
// BranchDivergingInfo contains the information about the divergence of a head branch to the base branch.
-// This struct is also used in templates, so it needs to search for all references before changing it.
type BranchDivergingInfo struct {
+ // whether the base branch contains new commits which are not in the head branch
BaseHasNewCommits bool
+
+ // behind/after are number of commits that the head branch is behind/after the base branch, it's 0 if it's unable to calculate.
+ // there could be a case that BaseHasNewCommits=true while the behind/after are both 0 (unable to calculate).
HeadCommitsBehind int
HeadCommitsAhead int
}
@@ -681,11 +690,20 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
if err != nil {
return nil, err
}
-
+ if headGitBranch.IsDeleted {
+ return nil, git_model.ErrBranchNotExist{
+ BranchName: headBranch,
+ }
+ }
baseGitBranch, err := git_model.GetBranch(ctx, baseRepo.ID, baseBranch)
if err != nil {
return nil, err
}
+ if baseGitBranch.IsDeleted {
+ return nil, git_model.ErrBranchNotExist{
+ BranchName: baseBranch,
+ }
+ }
info := &BranchDivergingInfo{}
if headGitBranch.CommitID == baseGitBranch.CommitID {
@@ -720,5 +738,6 @@ func GetBranchDivergingInfo(ctx reqctx.RequestContext, baseRepo *repo_model.Repo
}
info.HeadCommitsBehind, info.HeadCommitsAhead = diff.Behind, diff.Ahead
+ info.BaseHasNewCommits = info.HeadCommitsBehind > 0
return info, nil
}