0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-23 07:41:32 +02:00

Move SetDefaultBranch to gitrepo

This commit is contained in:
Lunny Xiao 2025-03-16 11:43:34 -07:00
parent 18bd70054b
commit 760749b6b7
12 changed files with 34 additions and 16 deletions

View File

@ -223,6 +223,14 @@ func (repo *Repository) GetOwnerName() string {
return repo.OwnerName
}
func (repo *Repository) GetDefaultBranch() string {
return repo.DefaultBranch
}
func (repo *Repository) GetDefaultWikiBranch() string {
return repo.DefaultWikiBranch
}
// SanitizedOriginalURL returns a sanitized OriginalURL
func (repo *Repository) SanitizedOriginalURL() string {
if repo.OriginalURL == "" {

View File

@ -31,14 +31,21 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
return gitRepo.GetBranchCommitID(branch)
}
// SetDefaultBranch sets default branch of repository.
func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
// SetDefaultBranchForRepo sets default branch of repository.
func SetDefaultBranchForRepo(ctx context.Context, repo Repository) error {
_, _, err := git.NewCommand("symbolic-ref", "HEAD").
AddDynamicArguments(git.BranchPrefix+name).
AddDynamicArguments(git.BranchPrefix+repo.GetDefaultBranch()).
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})
return err
}
func SetDefaultBranchForWiki(ctx context.Context, repo Repository) error {
_, _, err := git.NewCommand("symbolic-ref", "HEAD").
AddDynamicArguments(git.BranchPrefix+repo.GetDefaultWikiBranch()).
RunStdString(ctx, &git.RunOpts{Dir: wikiPath(repo)})
return err
}
// GetDefaultBranch gets default branch of repository.
func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
return git.GetDefaultBranch(ctx, repoPath(repo))

View File

@ -19,6 +19,8 @@ import (
type Repository interface {
GetName() string
GetOwnerName() string
GetDefaultBranch() string
GetDefaultWikiBranch() string
}
func absPath(owner, name string) string {

View File

@ -735,14 +735,14 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
// Default branch only updated if changed and exist or the repository is empty
updateRepoLicense := false
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, *opts.DefaultBranch)) {
repo.DefaultBranch = *opts.DefaultBranch
if !repo.IsEmpty {
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
if err := gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
ctx.APIErrorInternal(err)
return err
}
updateRepoLicense = true
}
repo.DefaultBranch = *opts.DefaultBranch
}
if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil {

View File

@ -21,7 +21,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
branch := ctx.PathParam("branch")
ctx.Repo.Repository.DefaultBranch = branch
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
if err := gitrepo.SetDefaultBranchForRepo(ctx, ctx.Repo.Repository); err != nil {
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
})

View File

@ -629,7 +629,7 @@ func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, re
m.Repo.DefaultBranch = firstName
}
// Update the git repository default branch
if err := gitrepo.SetDefaultBranch(ctx, m.Repo, m.Repo.DefaultBranch); err != nil {
if err := gitrepo.SetDefaultBranchForRepo(ctx, m.Repo); err != nil {
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
return false
}

View File

@ -124,14 +124,14 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
if len(defaultBranch) > 0 {
repo.DefaultBranch = defaultBranch
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err)
}
} else {
repo.DefaultBranch, err = gitrepo.GetDefaultBranch(ctx, repo)
if err != nil {
repo.DefaultBranch = setting.Repository.DefaultBranch
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err)
}
}
@ -188,7 +188,7 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
repo.DefaultBranch = setting.Repository.DefaultBranch
}
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err)
}
}

View File

@ -463,7 +463,8 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
log.Error("CancelPreviousJobs: %v", err)
}
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
// repo's default branch has been updated in git_model.RenameBranch
err2 = gitrepo.SetDefaultBranchForRepo(ctx, repo)
if err2 != nil {
return err2
}
@ -650,7 +651,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, newB
log.Error("CancelPreviousJobs: %v", err)
}
return gitrepo.SetDefaultBranch(ctx, repo, newBranchName)
return gitrepo.SetDefaultBranchForRepo(ctx, repo)
}); err != nil {
return err
}

View File

@ -181,7 +181,7 @@ func initRepository(ctx context.Context, u *user_model.User, repo *repo_model.Re
if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err)
}

View File

@ -281,7 +281,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
repo.DefaultBranch = templateRepo.DefaultBranch
}
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
return fmt.Errorf("setDefaultBranch: %w", err)
}
if err = UpdateRepository(ctx, repo, false); err != nil {

View File

@ -185,7 +185,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
repo.DefaultBranch = refName
repo.IsEmpty = false
if repo.DefaultBranch != setting.Repository.DefaultBranch {
if err := gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
if err := gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
return err
}
}

View File

@ -43,7 +43,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
return fmt.Errorf("InitRepository: %w", err)
} else if err = gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil {
return fmt.Errorf("createDelegateHooks: %w", err)
} else if _, _, err = git.NewCommand("symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix+repo.DefaultWikiBranch).RunStdString(ctx, &git.RunOpts{Dir: repo.WikiPath()}); err != nil {
} else if err = gitrepo.SetDefaultBranchForWiki(ctx, repo); err != nil {
return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)
}
return nil