mirror of
https://github.com/go-gitea/gitea
synced 2024-11-21 22:01:18 +01:00
Move get/set default branch from git package to gitrepo package to hide repopath (#29126)
This commit is contained in:
parent
a1f5dd7677
commit
25b842df26
15 changed files with 52 additions and 63 deletions
|
@ -901,12 +901,7 @@ func PullRequestCodeOwnersReview(ctx context.Context, pull *Issue, pr *PullReque
|
||||||
}
|
}
|
||||||
defer repo.Close()
|
defer repo.Close()
|
||||||
|
|
||||||
branch, err := repo.GetDefaultBranch()
|
commit, err := repo.GetBranchCommit(pr.BaseRepo.DefaultBranch)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
commit, err := repo.GetBranchCommit(branch)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,15 +55,8 @@ func (repo *Repository) GetHEADBranch() (*Branch, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDefaultBranch sets default branch of repository.
|
func GetDefaultBranch(ctx context.Context, repoPath string) (string, error) {
|
||||||
func (repo *Repository) SetDefaultBranch(name string) error {
|
stdout, _, err := NewCommand(ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repoPath})
|
||||||
_, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").AddDynamicArguments(BranchPrefix + name).RunStdString(&RunOpts{Dir: repo.Path})
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDefaultBranch gets default branch of repository.
|
|
||||||
func (repo *Repository) GetDefaultBranch() (string, error) {
|
|
||||||
stdout, _, err := NewCommand(repo.Ctx, "symbolic-ref", "HEAD").RunStdString(&RunOpts{Dir: repo.Path})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,3 +30,20 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str
|
||||||
|
|
||||||
return gitRepo.GetBranchCommitID(branch)
|
return gitRepo.GetBranchCommitID(branch)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetDefaultBranch sets default branch of repository.
|
||||||
|
func SetDefaultBranch(ctx context.Context, repo Repository, name string) error {
|
||||||
|
_, _, err := git.NewCommand(ctx, "symbolic-ref", "HEAD").
|
||||||
|
AddDynamicArguments(git.BranchPrefix + name).
|
||||||
|
RunStdString(&git.RunOpts{Dir: repoPath(repo)})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDefaultBranch gets default branch of repository.
|
||||||
|
func GetDefaultBranch(ctx context.Context, repo Repository) (string, error) {
|
||||||
|
return git.GetDefaultBranch(ctx, repoPath(repo))
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetWikiDefaultBranch(ctx context.Context, repo Repository) (string, error) {
|
||||||
|
return git.GetDefaultBranch(ctx, wikiPath(repo))
|
||||||
|
}
|
||||||
|
|
|
@ -720,7 +720,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
|
||||||
|
|
||||||
if ctx.Repo.GitRepo == nil && !repo.IsEmpty {
|
if ctx.Repo.GitRepo == nil && !repo.IsEmpty {
|
||||||
var err error
|
var err error
|
||||||
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository)
|
ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err)
|
ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err)
|
||||||
return err
|
return err
|
||||||
|
@ -731,7 +731,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
|
||||||
// Default branch only updated if changed and exist or the repository is empty
|
// Default branch only updated if changed and exist or the repository is empty
|
||||||
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) {
|
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && (repo.IsEmpty || ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch)) {
|
||||||
if !repo.IsEmpty {
|
if !repo.IsEmpty {
|
||||||
if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
|
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, *opts.DefaultBranch); err != nil {
|
||||||
if !git.IsErrUnsupportedVersion(err) {
|
if !git.IsErrUnsupportedVersion(err) {
|
||||||
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
|
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
|
"code.gitea.io/gitea/modules/gitrepo"
|
||||||
"code.gitea.io/gitea/modules/private"
|
"code.gitea.io/gitea/modules/private"
|
||||||
gitea_context "code.gitea.io/gitea/services/context"
|
gitea_context "code.gitea.io/gitea/services/context"
|
||||||
)
|
)
|
||||||
|
@ -20,7 +21,7 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
|
||||||
branch := ctx.Params(":branch")
|
branch := ctx.Params(":branch")
|
||||||
|
|
||||||
ctx.Repo.Repository.DefaultBranch = branch
|
ctx.Repo.Repository.DefaultBranch = branch
|
||||||
if err := ctx.Repo.GitRepo.SetDefaultBranch(ctx.Repo.Repository.DefaultBranch); err != nil {
|
if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
|
||||||
if !git.IsErrUnsupportedVersion(err) {
|
if !git.IsErrUnsupportedVersion(err) {
|
||||||
ctx.JSON(http.StatusInternalServerError, private.Response{
|
ctx.JSON(http.StatusInternalServerError, private.Response{
|
||||||
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
|
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
|
||||||
|
|
|
@ -102,7 +102,7 @@ func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, err
|
||||||
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
|
commit, errCommit := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
|
||||||
if git.IsErrNotExist(errCommit) {
|
if git.IsErrNotExist(errCommit) {
|
||||||
// if the default branch recorded in database is out of sync, then re-sync it
|
// if the default branch recorded in database is out of sync, then re-sync it
|
||||||
gitRepoDefaultBranch, errBranch := wikiGitRepo.GetDefaultBranch()
|
gitRepoDefaultBranch, errBranch := gitrepo.GetWikiDefaultBranch(ctx, ctx.Repo.Repository)
|
||||||
if errBranch != nil {
|
if errBranch != nil {
|
||||||
return wikiGitRepo, nil, errBranch
|
return wikiGitRepo, nil, errBranch
|
||||||
}
|
}
|
||||||
|
|
|
@ -681,7 +681,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
|
||||||
if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
|
if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
|
||||||
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
|
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
|
||||||
} else {
|
} else {
|
||||||
ctx.Repo.BranchName, _ = gitRepo.GetDefaultBranch()
|
ctx.Repo.BranchName, _ = gitrepo.GetDefaultBranch(ctx, ctx.Repo.Repository)
|
||||||
if ctx.Repo.BranchName == "" {
|
if ctx.Repo.BranchName == "" {
|
||||||
// If it still can't get a default branch, fall back to default branch from setting.
|
// If it still can't get a default branch, fall back to default branch from setting.
|
||||||
// Something might be wrong. Either site admin should fix the repo sync or Gitea should fix a potential bug.
|
// Something might be wrong. Either site admin should fix the repo sync or Gitea should fix a potential bug.
|
||||||
|
|
|
@ -593,7 +593,7 @@ func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, gi
|
||||||
m.Repo.DefaultBranch = firstName
|
m.Repo.DefaultBranch = firstName
|
||||||
}
|
}
|
||||||
// Update the git repository default branch
|
// Update the git repository default branch
|
||||||
if err := gitRepo.SetDefaultBranch(m.Repo.DefaultBranch); err != nil {
|
if err := gitrepo.SetDefaultBranch(ctx, m.Repo, m.Repo.DefaultBranch); err != nil {
|
||||||
if !git.IsErrUnsupportedVersion(err) {
|
if !git.IsErrUnsupportedVersion(err) {
|
||||||
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
|
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
|
||||||
desc := fmt.Sprintf("Failed to update default branch of underlying git repository '%s': %v", m.Repo.RepoPath(), err)
|
desc := fmt.Sprintf("Failed to update default branch of underlying git repository '%s': %v", m.Repo.RepoPath(), err)
|
||||||
|
|
|
@ -127,24 +127,17 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
|
||||||
|
|
||||||
repo.IsEmpty = false
|
repo.IsEmpty = false
|
||||||
|
|
||||||
// Don't bother looking this repo in the context it won't be there
|
|
||||||
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("openRepository: %w", err)
|
|
||||||
}
|
|
||||||
defer gitRepo.Close()
|
|
||||||
|
|
||||||
if len(defaultBranch) > 0 {
|
if len(defaultBranch) > 0 {
|
||||||
repo.DefaultBranch = defaultBranch
|
repo.DefaultBranch = defaultBranch
|
||||||
|
|
||||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
|
||||||
return fmt.Errorf("setDefaultBranch: %w", err)
|
return fmt.Errorf("setDefaultBranch: %w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
repo.DefaultBranch, err = gitRepo.GetDefaultBranch()
|
repo.DefaultBranch, err = gitrepo.GetDefaultBranch(ctx, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
repo.DefaultBranch = setting.Repository.DefaultBranch
|
repo.DefaultBranch = setting.Repository.DefaultBranch
|
||||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
|
||||||
return fmt.Errorf("setDefaultBranch: %w", err)
|
return fmt.Errorf("setDefaultBranch: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,7 +181,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
|
||||||
repo.DefaultBranch = setting.Repository.DefaultBranch
|
repo.DefaultBranch = setting.Repository.DefaultBranch
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
|
||||||
return fmt.Errorf("setDefaultBranch: %w", err)
|
return fmt.Errorf("setDefaultBranch: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,6 +190,13 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r
|
||||||
return fmt.Errorf("updateRepository: %w", err)
|
return fmt.Errorf("updateRepository: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't bother looking this repo in the context it won't be there
|
||||||
|
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("openRepository: %w", err)
|
||||||
|
}
|
||||||
|
defer gitRepo.Close()
|
||||||
|
|
||||||
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
|
if err = repo_module.SyncReleasesWithTags(ctx, repo, gitRepo); err != nil {
|
||||||
return fmt.Errorf("SyncReleasesWithTags: %w", err)
|
return fmt.Errorf("SyncReleasesWithTags: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -375,7 +375,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
|
||||||
log.Error("CancelRunningJobs: %v", err)
|
log.Error("CancelRunningJobs: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err2 = gitRepo.SetDefaultBranch(to)
|
err2 = gitrepo.SetDefaultBranch(ctx, repo, to)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
|
@ -540,7 +540,7 @@ func SetRepoDefaultBranch(ctx context.Context, repo *repo_model.Repository, gitR
|
||||||
log.Error("CancelRunningJobs: %v", err)
|
log.Error("CancelRunningJobs: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := gitRepo.SetDefaultBranch(newBranchName); err != nil {
|
if err := gitrepo.SetDefaultBranch(ctx, repo, newBranchName); err != nil {
|
||||||
if !git.IsErrUnsupportedVersion(err) {
|
if !git.IsErrUnsupportedVersion(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -177,12 +177,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
|
||||||
|
|
||||||
if len(opts.DefaultBranch) > 0 {
|
if len(opts.DefaultBranch) > 0 {
|
||||||
repo.DefaultBranch = opts.DefaultBranch
|
repo.DefaultBranch = opts.DefaultBranch
|
||||||
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("openRepository: %w", err)
|
|
||||||
}
|
|
||||||
defer gitRepo.Close()
|
|
||||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
|
||||||
return fmt.Errorf("setDefaultBranch: %w", err)
|
return fmt.Errorf("setDefaultBranch: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -272,12 +272,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
|
||||||
repo.DefaultBranch = templateRepo.DefaultBranch
|
repo.DefaultBranch = templateRepo.DefaultBranch
|
||||||
}
|
}
|
||||||
|
|
||||||
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
|
if err = gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("openRepository: %w", err)
|
|
||||||
}
|
|
||||||
defer gitRepo.Close()
|
|
||||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
|
||||||
return fmt.Errorf("setDefaultBranch: %w", err)
|
return fmt.Errorf("setDefaultBranch: %w", err)
|
||||||
}
|
}
|
||||||
if err = UpdateRepository(ctx, repo, false); err != nil {
|
if err = UpdateRepository(ctx, repo, false); err != nil {
|
||||||
|
|
|
@ -57,14 +57,7 @@ func cloneWiki(ctx context.Context, u *user_model.User, opts migration.MigrateOp
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
wikiRepo, err := git.OpenRepository(ctx, wikiPath)
|
defaultBranch, err := git.GetDefaultBranch(ctx, wikiPath)
|
||||||
if err != nil {
|
|
||||||
cleanIncompleteWikiPath()
|
|
||||||
return "", fmt.Errorf("failed to open wiki repo %q, err: %w", wikiPath, err)
|
|
||||||
}
|
|
||||||
defer wikiRepo.Close()
|
|
||||||
|
|
||||||
defaultBranch, err := wikiRepo.GetDefaultBranch()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cleanIncompleteWikiPath()
|
cleanIncompleteWikiPath()
|
||||||
return "", fmt.Errorf("failed to get wiki repo default branch for %q, err: %w", wikiPath, err)
|
return "", fmt.Errorf("failed to get wiki repo default branch for %q, err: %w", wikiPath, err)
|
||||||
|
|
|
@ -182,7 +182,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
|
||||||
repo.DefaultBranch = refName
|
repo.DefaultBranch = refName
|
||||||
repo.IsEmpty = false
|
repo.IsEmpty = false
|
||||||
if repo.DefaultBranch != setting.Repository.DefaultBranch {
|
if repo.DefaultBranch != setting.Repository.DefaultBranch {
|
||||||
if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
if err := gitrepo.SetDefaultBranch(ctx, repo, repo.DefaultBranch); err != nil {
|
||||||
if !git.IsErrUnsupportedVersion(err) {
|
if !git.IsErrUnsupportedVersion(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -370,6 +370,14 @@ func ChangeDefaultWikiBranch(ctx context.Context, repo *repo_model.Repository, n
|
||||||
return fmt.Errorf("unable to update database: %w", err)
|
return fmt.Errorf("unable to update database: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oldDefBranch, err := gitrepo.GetWikiDefaultBranch(ctx, repo)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to get default branch: %w", err)
|
||||||
|
}
|
||||||
|
if oldDefBranch == newBranch {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
|
gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo)
|
||||||
if errors.Is(err, util.ErrNotExist) {
|
if errors.Is(err, util.ErrNotExist) {
|
||||||
return nil // no git repo on storage, no need to do anything else
|
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
|
||||||
}
|
}
|
||||||
defer gitRepo.Close()
|
defer gitRepo.Close()
|
||||||
|
|
||||||
oldDefBranch, err := gitRepo.GetDefaultBranch()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to get default branch: %w", err)
|
|
||||||
}
|
|
||||||
if oldDefBranch == newBranch {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err = gitRepo.RenameBranch(oldDefBranch, newBranch)
|
err = gitRepo.RenameBranch(oldDefBranch, newBranch)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to rename default branch: %w", err)
|
return fmt.Errorf("unable to rename default branch: %w", err)
|
||||||
|
|
Loading…
Reference in a new issue