Skip to content

Commit 760749b

Browse files
committed
Move SetDefaultBranch to gitrepo
1 parent 18bd700 commit 760749b

File tree

12 files changed

+34
-16
lines changed

12 files changed

+34
-16
lines changed

models/repo/repo.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ func (repo *Repository) GetOwnerName() string {
223223
return repo.OwnerName
224224
}
225225

226+
func (repo *Repository) GetDefaultBranch() string {
227+
return repo.DefaultBranch
228+
}
229+
230+
func (repo *Repository) GetDefaultWikiBranch() string {
231+
return repo.DefaultWikiBranch
232+
}
233+
226234
// SanitizedOriginalURL returns a sanitized OriginalURL
227235
func (repo *Repository) SanitizedOriginalURL() string {
228236
if repo.OriginalURL == "" {

modules/gitrepo/branch.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,21 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
3131
return gitRepo.GetBranchCommitID(branch)
3232
}
3333

34-
// SetDefaultBranch sets default branch of repository.
35-
func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
34+
// SetDefaultBranchForRepo sets default branch of repository.
35+
func SetDefaultBranchForRepo(ctx context.Context, repo Repository) error {
3636
_, _, err := git.NewCommand("symbolic-ref", "HEAD").
37-
AddDynamicArguments(git.BranchPrefix+name).
37+
AddDynamicArguments(git.BranchPrefix+repo.GetDefaultBranch()).
3838
RunStdString(ctx, &git.RunOpts{Dir: repoPath(repo)})
3939
return err
4040
}
4141

42+
func SetDefaultBranchForWiki(ctx context.Context, repo Repository) error {
43+
_, _, err := git.NewCommand("symbolic-ref", "HEAD").
44+
AddDynamicArguments(git.BranchPrefix+repo.GetDefaultWikiBranch()).
45+
RunStdString(ctx, &git.RunOpts{Dir: wikiPath(repo)})
46+
return err
47+
}
48+
4249
// GetDefaultBranch gets default branch of repository.
4350
func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
4451
return git.GetDefaultBranch(ctx, repoPath(repo))

modules/gitrepo/gitrepo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
type Repository interface {
2020
GetName() string
2121
GetOwnerName() string
22+
GetDefaultBranch() string
23+
GetDefaultWikiBranch() string
2224
}
2325

2426
func absPath(owner, name string) string {

routers/api/v1/repo/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,14 +735,14 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
735735
// Default branch only updated if changed and exist or the repository is empty
736736
updateRepoLicense := false
737737
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, *opts.DefaultBranch)) {
738+
repo.DefaultBranch = *opts.DefaultBranch
738739
if !repo.IsEmpty {
739-
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
740+
if err := gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
740741
ctx.APIErrorInternal(err)
741742
return err
742743
}
743744
updateRepoLicense = true
744745
}
745-
repo.DefaultBranch = *opts.DefaultBranch
746746
}
747747

748748
if err := repo_service.UpdateRepository(ctx, repo, visibilityChanged); err != nil {

routers/private/default_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
2121
branch := ctx.PathParam("branch")
2222

2323
ctx.Repo.Repository.DefaultBranch = branch
24-
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
24+
if err := gitrepo.SetDefaultBranchForRepo(ctx, ctx.Repo.Repository); err != nil {
2525
ctx.JSON(http.StatusInternalServerError, private.Response{
2626
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
2727
})

services/mirror/mirror_pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, re
629629
m.Repo.DefaultBranch = firstName
630630
}
631631
// Update the git repository default branch
632-
if err := gitrepo.SetDefaultBranch(ctx, m.Repo, m.Repo.DefaultBranch); err != nil {
632+
if err := gitrepo.SetDefaultBranchForRepo(ctx, m.Repo); err != nil {
633633
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
634634
return false
635635
}

services/repository/adopt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
124124
if len(defaultBranch) > 0 {
125125
repo.DefaultBranch = defaultBranch
126126

127-
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
127+
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
128128
return fmt.Errorf("setDefaultBranch: %w", err)
129129
}
130130
} else {
131131
repo.DefaultBranch, err = gitrepo.GetDefaultBranch(ctx, repo)
132132
if err != nil {
133133
repo.DefaultBranch = setting.Repository.DefaultBranch
134-
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
134+
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
135135
return fmt.Errorf("setDefaultBranch: %w", err)
136136
}
137137
}
@@ -188,7 +188,7 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr
188188
repo.DefaultBranch = setting.Repository.DefaultBranch
189189
}
190190

191-
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
191+
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
192192
return fmt.Errorf("setDefaultBranch: %w", err)
193193
}
194194
}

services/repository/branch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
463463
log.Error("CancelPreviousJobs: %v", err)
464464
}
465465

466-
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
466+
// repo's default branch has been updated in git_model.RenameBranch
467+
err2 = gitrepo.SetDefaultBranchForRepo(ctx, repo)
467468
if err2 != nil {
468469
return err2
469470
}
@@ -650,7 +651,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, newB
650651
log.Error("CancelPreviousJobs: %v", err)
651652
}
652653

653-
return gitrepo.SetDefaultBranch(ctx, repo, newBranchName)
654+
return gitrepo.SetDefaultBranchForRepo(ctx, repo)
654655
}); err != nil {
655656
return err
656657
}

services/repository/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func initRepository(ctx context.Context, u *user_model.User, repo *repo_model.Re
181181

182182
if len(opts.DefaultBranch) > 0 {
183183
repo.DefaultBranch = opts.DefaultBranch
184-
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
184+
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
185185
return fmt.Errorf("setDefaultBranch: %w", err)
186186
}
187187

services/repository/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
281281
repo.DefaultBranch = templateRepo.DefaultBranch
282282
}
283283

284-
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
284+
if err = gitrepo.SetDefaultBranchForRepo(ctx, repo); err != nil {
285285
return fmt.Errorf("setDefaultBranch: %w", err)
286286
}
287287
if err = UpdateRepository(ctx, repo, false); err != nil {

0 commit comments

Comments
 (0)