Skip to content

Commit 25b842d

Browse files
authored
Move get/set default branch from git package to gitrepo package to hide repopath (#29126)
1 parent a1f5dd7 commit 25b842d

File tree

15 files changed

+52
-63
lines changed

15 files changed

+52
-63
lines changed

models/issues/pull.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -901,12 +901,7 @@ func PullRequestCodeOwnersReview(ctx context.Context, pull *Issue, pr *PullReque
901901
}
902902
defer repo.Close()
903903

904-
branch, err := repo.GetDefaultBranch()
905-
if err != nil {
906-
return err
907-
}
908-
909-
commit, err := repo.GetBranchCommit(branch)
904+
commit, err := repo.GetBranchCommit(pr.BaseRepo.DefaultBranch)
910905
if err != nil {
911906
return err
912907
}

modules/git/repo_branch.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,8 @@ func (repo *Repository) GetHEADBranch() (*Branch, error) {
5555
}, nil
5656
}
5757

58-
// SetDefaultBranch sets default branch of repository.
59-
func (repo *Repository) SetDefaultBranch(name string) error {
60-
_, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").AddDynamicArguments(BranchPrefix + name).RunStdString(&RunOpts{Dir: repo.Path})
61-
return err
62-
}
63-
64-
// GetDefaultBranch gets default branch of repository.
65-
func (repo *Repository) GetDefaultBranch() (string, error) {
66-
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
58+
func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
59+
stdout, _, err := NewCommand(ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repoPath})
6760
if err != nil {
6861
return "", err
6962
}

modules/gitrepo/branch.go

+17
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,20 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
3030

3131
return gitRepo.GetBranchCommitID(branch)
3232
}
33+
34+
// SetDefaultBranch sets default branch of repository.
35+
func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
36+
_, _, err := git.NewCommand(ctx, "symbolic-ref", "HEAD").
37+
AddDynamicArguments(git.BranchPrefix + name).
38+
RunStdString(&git.RunOpts{Dir: repoPath(repo)})
39+
return err
40+
}
41+
42+
// GetDefaultBranch gets default branch of repository.
43+
func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
44+
return git.GetDefaultBranch(ctx, repoPath(repo))
45+
}
46+
47+
func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) {
48+
return git.GetDefaultBranch(ctx, wikiPath(repo))
49+
}

routers/api/v1/repo/repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
720720

721721
if ctx.Repo.GitRepo == nil && !repo.IsEmpty {
722722
var err error
723-
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
723+
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, repo)
724724
if err != nil {
725725
ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err)
726726
return err
@@ -731,7 +731,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
731731
// Default branch only updated if changed and exist or the repository is empty
732732
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) {
733733
if !repo.IsEmpty {
734-
if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
734+
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
735735
if !git.IsErrUnsupportedVersion(err) {
736736
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
737737
return err

routers/private/default_branch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
repo_model "code.gitea.io/gitea/models/repo"
1111
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/gitrepo"
1213
"code.gitea.io/gitea/modules/private"
1314
gitea_context "code.gitea.io/gitea/services/context"
1415
)
@@ -20,7 +21,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
2021
branch := ctx.Params(":branch")
2122

2223
ctx.Repo.Repository.DefaultBranch = branch
23-
if err := ctx.Repo.GitRepo.SetDefaultBranch(ctx.Repo.Repository.DefaultBranch); err != nil {
24+
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
2425
if !git.IsErrUnsupportedVersion(err) {
2526
ctx.JSON(http.StatusInternalServerError, private.Response{
2627
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),

routers/web/repo/wiki.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
102102
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
103103
if git.IsErrNotExist(errCommit) {
104104
// if the default branch recorded in database is out of sync, then re-sync it
105-
gitRepoDefaultBranch, errBranch := wikiGitRepo.GetDefaultBranch()
105+
gitRepoDefaultBranch, errBranch := gitrepo.GetWikiDefaultBranch(ctx, ctx.Repo.Repository)
106106
if errBranch != nil {
107107
return wikiGitRepo, nil, errBranch
108108
}

services/context/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
681681
if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
682682
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
683683
} else {
684-
ctx.Repo.BranchName, _ = gitRepo.GetDefaultBranch()
684+
ctx.Repo.BranchName, _ = gitrepo.GetDefaultBranch(ctx, ctx.Repo.Repository)
685685
if ctx.Repo.BranchName == "" {
686686
// If it still can't get a default branch, fall back to default branch from setting.
687687
// Something might be wrong. Either site admin should fix the repo sync or Gitea should fix a potential bug.

services/mirror/mirror_pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, gi
593593
m.Repo.DefaultBranch = firstName
594594
}
595595
// Update the git repository default branch
596-
if err := gitRepo.SetDefaultBranch(m.Repo.DefaultBranch); err != nil {
596+
if err := gitrepo.SetDefaultBranch(ctx, m.Repo, m.Repo.DefaultBranch); err != nil {
597597
if !git.IsErrUnsupportedVersion(err) {
598598
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
599599
desc := fmt.Sprintf("Failed to update default branch of underlying git repository '%s': %v", m.Repo.RepoPath(), err)

services/repository/adopt.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,17 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
127127

128128
repo.IsEmpty = false
129129

130-
// Don't bother looking this repo in the context it won't be there
131-
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
132-
if err != nil {
133-
return fmt.Errorf("openRepository: %w", err)
134-
}
135-
defer gitRepo.Close()
136-
137130
if len(defaultBranch) > 0 {
138131
repo.DefaultBranch = defaultBranch
139132

140-
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
133+
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
141134
return fmt.Errorf("setDefaultBranch: %w", err)
142135
}
143136
} else {
144-
repo.DefaultBranch, err = gitRepo.GetDefaultBranch()
137+
repo.DefaultBranch, err = gitrepo.GetDefaultBranch(ctx, repo)
145138
if err != nil {
146139
repo.DefaultBranch = setting.Repository.DefaultBranch
147-
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
140+
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
148141
return fmt.Errorf("setDefaultBranch: %w", err)
149142
}
150143
}
@@ -188,7 +181,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
188181
repo.DefaultBranch = setting.Repository.DefaultBranch
189182
}
190183

191-
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
184+
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
192185
return fmt.Errorf("setDefaultBranch: %w", err)
193186
}
194187
}
@@ -197,6 +190,13 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
197190
return fmt.Errorf("updateRepository: %w", err)
198191
}
199192

193+
// Don't bother looking this repo in the context it won't be there
194+
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
195+
if err != nil {
196+
return fmt.Errorf("openRepository: %w", err)
197+
}
198+
defer gitRepo.Close()
199+
200200
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
201201
return fmt.Errorf("SyncReleasesWithTags: %w", err)
202202
}

services/repository/branch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
375375
log.Error("CancelRunningJobs: %v", err)
376376
}
377377

378-
err2 = gitRepo.SetDefaultBranch(to)
378+
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
379379
if err2 != nil {
380380
return err2
381381
}
@@ -540,7 +540,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
540540
log.Error("CancelRunningJobs: %v", err)
541541
}
542542

543-
if err := gitRepo.SetDefaultBranch(newBranchName); err != nil {
543+
if err := gitrepo.SetDefaultBranch(ctx, repo, newBranchName); err != nil {
544544
if !git.IsErrUnsupportedVersion(err) {
545545
return err
546546
}

services/repository/create.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
177177

178178
if len(opts.DefaultBranch) > 0 {
179179
repo.DefaultBranch = opts.DefaultBranch
180-
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
181-
if err != nil {
182-
return fmt.Errorf("openRepository: %w", err)
183-
}
184-
defer gitRepo.Close()
185-
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
180+
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
186181
return fmt.Errorf("setDefaultBranch: %w", err)
187182
}
188183

services/repository/generate.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
272272
repo.DefaultBranch = templateRepo.DefaultBranch
273273
}
274274

275-
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
276-
if err != nil {
277-
return fmt.Errorf("openRepository: %w", err)
278-
}
279-
defer gitRepo.Close()
280-
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
275+
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
281276
return fmt.Errorf("setDefaultBranch: %w", err)
282277
}
283278
if err = UpdateRepository(ctx, repo, false); err != nil {

services/repository/migrate.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@ func cloneWiki(ctx context.Context, u *user_model.User, opts migration.MigrateOp
5757
return "", err
5858
}
5959

60-
wikiRepo, err := git.OpenRepository(ctx, wikiPath)
61-
if err != nil {
62-
cleanIncompleteWikiPath()
63-
return "", fmt.Errorf("failed to open wiki repo %q, err: %w", wikiPath, err)
64-
}
65-
defer wikiRepo.Close()
66-
67-
defaultBranch, err := wikiRepo.GetDefaultBranch()
60+
defaultBranch, err := git.GetDefaultBranch(ctx, wikiPath)
6861
if err != nil {
6962
cleanIncompleteWikiPath()
7063
return "", fmt.Errorf("failed to get wiki repo default branch for %q, err: %w", wikiPath, err)

services/repository/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
182182
repo.DefaultBranch = refName
183183
repo.IsEmpty = false
184184
if repo.DefaultBranch != setting.Repository.DefaultBranch {
185-
if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
185+
if err := gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
186186
if !git.IsErrUnsupportedVersion(err) {
187187
return err
188188
}

services/wiki/wiki.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,14 @@ func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, n
370370
return fmt.Errorf("unable to update database: %w", err)
371371
}
372372

373+
oldDefBranch, err := gitrepo.GetWikiDefaultBranch(ctx, repo)
374+
if err != nil {
375+
return fmt.Errorf("unable to get default branch: %w", err)
376+
}
377+
if oldDefBranch == newBranch {
378+
return nil
379+
}
380+
373381
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
374382
if errors.Is(err, util.ErrNotExist) {
375383
return nil // no git repo on storage, no need to do anything else
@@ -378,14 +386,6 @@ func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, n
378386
}
379387
defer gitRepo.Close()
380388

381-
oldDefBranch, err := gitRepo.GetDefaultBranch()
382-
if err != nil {
383-
return fmt.Errorf("unable to get default branch: %w", err)
384-
}
385-
if oldDefBranch == newBranch {
386-
return nil
387-
}
388-
389389
err = gitRepo.RenameBranch(oldDefBranch, newBranch)
390390
if err != nil {
391391
return fmt.Errorf("unable to rename default branch: %w", err)

0 commit comments

Comments
 (0)