From f3165824585b55c0bd75cb4f46120fca8b4ace9f Mon Sep 17 00:00:00 2001 From: Abheek Dhawan <67982792+ADawesomeguy@users.noreply.github.com> Date: Sat, 26 Mar 2022 21:56:28 -0500 Subject: Set the default branch for repositories generated from templates (#19136) * Set the default branch for repositories generated from templates * Allows default branch to be set through the API for repos generated from templates * Update swagger API template * Only set default branch to the one from the template if not specified * Use specified default branch if it exists while generating git commits Fix #19082 Co-authored-by: John Olheiser Co-authored-by: zeripath --- modules/repository/generate.go | 15 +++++++++++++-- modules/structs/repo.go | 2 ++ 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'modules') diff --git a/modules/repository/generate.go b/modules/repository/generate.go index d0b5fa0820..cf219f4c26 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -184,7 +184,13 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r return fmt.Errorf("git remote add: %v", err) } - return initRepoCommit(ctx, tmpDir, repo, repo.Owner, templateRepo.DefaultBranch) + // set default branch based on whether it's specified in the newly generated repo or not + defaultBranch := repo.DefaultBranch + if strings.TrimSpace(defaultBranch) == "" { + defaultBranch = templateRepo.DefaultBranch + } + + return initRepoCommit(ctx, tmpDir, repo, repo.Owner, defaultBranch) } func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) { @@ -208,7 +214,11 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r return fmt.Errorf("getRepositoryByID: %v", err) } - repo.DefaultBranch = templateRepo.DefaultBranch + // if there was no default branch supplied when generating the repo, use the default one from the template + if strings.TrimSpace(repo.DefaultBranch) == "" { + repo.DefaultBranch = templateRepo.DefaultBranch + } + gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath()) if err != nil { return fmt.Errorf("openRepository: %v", err) @@ -249,6 +259,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ Name: opts.Name, LowerName: strings.ToLower(opts.Name), Description: opts.Description, + DefaultBranch: opts.DefaultBranch, IsPrivate: opts.Private, IsEmpty: !opts.GitContent || templateRepo.IsEmpty, IsFsckEnabled: templateRepo.IsFsckEnabled, diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 27d0b4f3ba..c218796620 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -201,6 +201,8 @@ type GenerateRepoOption struct { // required: true // unique: true Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"` + // Default branch of the new repository + DefaultBranch string `json:"default_branch"` // Description of the repository to create Description string `json:"description" binding:"MaxSize(255)"` // Whether the repository is private -- cgit v1.2.3