mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	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 <john.olheiser@gmail.com> Co-authored-by: zeripath <art27@cantab.net>
This commit is contained in:
		@@ -21,6 +21,7 @@ import (
 | 
				
			|||||||
// GenerateRepoOptions contains the template units to generate
 | 
					// GenerateRepoOptions contains the template units to generate
 | 
				
			||||||
type GenerateRepoOptions struct {
 | 
					type GenerateRepoOptions struct {
 | 
				
			||||||
	Name          string
 | 
						Name          string
 | 
				
			||||||
 | 
						DefaultBranch string
 | 
				
			||||||
	Description   string
 | 
						Description   string
 | 
				
			||||||
	Private       bool
 | 
						Private       bool
 | 
				
			||||||
	GitContent    bool
 | 
						GitContent    bool
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -184,7 +184,13 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
 | 
				
			|||||||
		return fmt.Errorf("git remote add: %v", err)
 | 
							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) {
 | 
					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)
 | 
							return fmt.Errorf("getRepositoryByID: %v", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 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
 | 
							repo.DefaultBranch = templateRepo.DefaultBranch
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
 | 
						gitRepo, err := git.OpenRepositoryCtx(ctx, repo.RepoPath())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return fmt.Errorf("openRepository: %v", err)
 | 
							return fmt.Errorf("openRepository: %v", err)
 | 
				
			||||||
@@ -249,6 +259,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
 | 
				
			|||||||
		Name:          opts.Name,
 | 
							Name:          opts.Name,
 | 
				
			||||||
		LowerName:     strings.ToLower(opts.Name),
 | 
							LowerName:     strings.ToLower(opts.Name),
 | 
				
			||||||
		Description:   opts.Description,
 | 
							Description:   opts.Description,
 | 
				
			||||||
 | 
							DefaultBranch: opts.DefaultBranch,
 | 
				
			||||||
		IsPrivate:     opts.Private,
 | 
							IsPrivate:     opts.Private,
 | 
				
			||||||
		IsEmpty:       !opts.GitContent || templateRepo.IsEmpty,
 | 
							IsEmpty:       !opts.GitContent || templateRepo.IsEmpty,
 | 
				
			||||||
		IsFsckEnabled: templateRepo.IsFsckEnabled,
 | 
							IsFsckEnabled: templateRepo.IsFsckEnabled,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -201,6 +201,8 @@ type GenerateRepoOption struct {
 | 
				
			|||||||
	// required: true
 | 
						// required: true
 | 
				
			||||||
	// unique: true
 | 
						// unique: true
 | 
				
			||||||
	Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"`
 | 
						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 of the repository to create
 | 
				
			||||||
	Description string `json:"description" binding:"MaxSize(255)"`
 | 
						Description string `json:"description" binding:"MaxSize(255)"`
 | 
				
			||||||
	// Whether the repository is private
 | 
						// Whether the repository is private
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -360,6 +360,7 @@ func Generate(ctx *context.APIContext) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	opts := models.GenerateRepoOptions{
 | 
						opts := models.GenerateRepoOptions{
 | 
				
			||||||
		Name:          form.Name,
 | 
							Name:          form.Name,
 | 
				
			||||||
 | 
							DefaultBranch: form.DefaultBranch,
 | 
				
			||||||
		Description:   form.Description,
 | 
							Description:   form.Description,
 | 
				
			||||||
		Private:       form.Private,
 | 
							Private:       form.Private,
 | 
				
			||||||
		GitContent:    form.GitContent,
 | 
							GitContent:    form.GitContent,
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15412,6 +15412,11 @@
 | 
				
			|||||||
          "type": "boolean",
 | 
					          "type": "boolean",
 | 
				
			||||||
          "x-go-name": "Avatar"
 | 
					          "x-go-name": "Avatar"
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					        "default_branch": {
 | 
				
			||||||
 | 
					          "description": "Default branch of the new repository",
 | 
				
			||||||
 | 
					          "type": "string",
 | 
				
			||||||
 | 
					          "x-go-name": "DefaultBranch"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
        "description": {
 | 
					        "description": {
 | 
				
			||||||
          "description": "Description of the repository to create",
 | 
					          "description": "Description of the repository to create",
 | 
				
			||||||
          "type": "string",
 | 
					          "type": "string",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user