diff --git a/models/repo/repo.go b/models/repo/repo.go index a8732f60bfff8..d0dc37572fa07 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -220,19 +220,33 @@ func RelativePath(ownerName, repoName string) string { } // RelativePath should be an unix style path like username/reponame.git -func (repo *Repository) RelativePath() string { +func (repo *Repository) GetRelativePath() string { return RelativePath(repo.OwnerName, repo.Name) } -type StorageRepo string +func (repo *Repository) GetDefaultBranch() string { + return repo.DefaultBranch +} + +type StorageRepo struct { + RelativePath string + DefaultBranch string +} // RelativePath should be an unix style path like username/reponame.git -func (sr StorageRepo) RelativePath() string { - return string(sr) +func (sr StorageRepo) GetRelativePath() string { + return sr.RelativePath +} + +func (sr StorageRepo) GetDefaultBranch() string { + return sr.DefaultBranch } func (repo *Repository) WikiStorageRepo() StorageRepo { - return StorageRepo(strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git") + return StorageRepo{ + RelativePath: strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git", + DefaultBranch: repo.DefaultWikiBranch, + } } // SanitizedOriginalURL returns a sanitized OriginalURL diff --git a/modules/gitrepo/branch.go b/modules/gitrepo/branch.go index 25ea5abfca1bd..c7550cc0cd3fd 100644 --- a/modules/gitrepo/branch.go +++ b/modules/gitrepo/branch.go @@ -32,9 +32,9 @@ func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (str } // SetDefaultBranch sets default branch of repository. -func SetDefaultBranch(ctx context.Context, repo Repository, name string) error { +func SetDefaultBranch(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 } diff --git a/modules/gitrepo/gitrepo.go b/modules/gitrepo/gitrepo.go index 5da65e2452704..c0363a88e972e 100644 --- a/modules/gitrepo/gitrepo.go +++ b/modules/gitrepo/gitrepo.go @@ -17,13 +17,14 @@ import ( // Repository represents a git repository which stored in a disk type Repository interface { - RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path + GetRelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path + GetDefaultBranch() string } // RelativePath should be an unix style path like username/reponame.git // This method should change it according to the current OS. func repoPath(repo Repository) string { - return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.RelativePath())) + return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.GetRelativePath())) } // OpenRepository opens the repository at the given relative path with the provided context. diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 3d638cb05e029..037215364c8b2 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -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.SetDefaultBranch(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 { diff --git a/routers/private/default_branch.go b/routers/private/default_branch.go index c375d70dc6f2b..2066fb267dae2 100644 --- a/routers/private/default_branch.go +++ b/routers/private/default_branch.go @@ -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.SetDefaultBranch(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), }) diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 658747e7c83f1..0410b739c99ff 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -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.SetDefaultBranch(ctx, m.Repo); err != nil { log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err) return false } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index b7321156d9da8..b8a719dc261bd 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -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.SetDefaultBranch(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.SetDefaultBranch(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.SetDefaultBranch(ctx, repo); err != nil { return fmt.Errorf("setDefaultBranch: %w", err) } } diff --git a/services/repository/branch.go b/services/repository/branch.go index 8804778bd5ebb..b870bbd54bbf0 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -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.SetDefaultBranch(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.SetDefaultBranch(ctx, repo) }); err != nil { return err } diff --git a/services/repository/create.go b/services/repository/create.go index 1a6a68b35aec5..1b8e199569672 100644 --- a/services/repository/create.go +++ b/services/repository/create.go @@ -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.SetDefaultBranch(ctx, repo); err != nil { return fmt.Errorf("setDefaultBranch: %w", err) } diff --git a/services/repository/generate.go b/services/repository/generate.go index 9d2bbb1f7fbea..1263621e400f7 100644 --- a/services/repository/generate.go +++ b/services/repository/generate.go @@ -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.SetDefaultBranch(ctx, repo); err != nil { return fmt.Errorf("setDefaultBranch: %w", err) } if err = UpdateRepository(ctx, repo, false); err != nil { diff --git a/services/repository/push.go b/services/repository/push.go index c40333f0a8b76..2fc4ad7a66b21 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -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.SetDefaultBranch(ctx, repo); err != nil { return err } } diff --git a/services/repository/transfer.go b/services/repository/transfer.go index a589bc469d87f..6ce8c51f393da 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -337,7 +337,10 @@ func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newR } if err = gitrepo.RenameRepository(ctx, repo, - repo_model.StorageRepo(repo_model.RelativePath(repo.OwnerName, newRepoName))); err != nil { + repo_model.StorageRepo{ + RelativePath: repo_model.RelativePath(repo.OwnerName, newRepoName), + DefaultBranch: repo.DefaultBranch, + }); err != nil { return fmt.Errorf("rename repository directory: %w", err) } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b21f46639dc99..a6cebb3fdbf16 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -43,7 +43,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { return fmt.Errorf("InitRepository: %w", err) } else if err = gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); 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.SetDefaultBranch(ctx, repo.WikiStorageRepo()); err != nil { return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err) } return nil