mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	Option to set default branch at repository creation (#10803)
* Option to set default branch at repository creation * Handle template repos with non-default master branch * Add DefaultBranch handling on creation to API Fix #9542 Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		@@ -113,7 +113,8 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp
 | 
			
		||||
	// Clone to temporary path and do the init commit.
 | 
			
		||||
	templateRepoPath := templateRepo.RepoPath()
 | 
			
		||||
	if err := git.Clone(templateRepoPath, tmpDir, git.CloneRepoOptions{
 | 
			
		||||
		Depth: 1,
 | 
			
		||||
		Depth:  1,
 | 
			
		||||
		Branch: templateRepo.DefaultBranch,
 | 
			
		||||
	}); err != nil {
 | 
			
		||||
		return fmt.Errorf("git clone: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -180,7 +181,7 @@ func generateRepoCommit(repo, templateRepo, generateRepo *models.Repository, tmp
 | 
			
		||||
		return fmt.Errorf("git remote add: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return initRepoCommit(tmpDir, repo, repo.Owner)
 | 
			
		||||
	return initRepoCommit(tmpDir, repo, repo.Owner, templateRepo.DefaultBranch)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *models.Repository) (err error) {
 | 
			
		||||
@@ -204,7 +205,7 @@ func generateGitContent(ctx models.DBContext, repo, templateRepo, generateRepo *
 | 
			
		||||
		return fmt.Errorf("getRepositoryByID: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	repo.DefaultBranch = "master"
 | 
			
		||||
	repo.DefaultBranch = templateRepo.DefaultBranch
 | 
			
		||||
	if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
 | 
			
		||||
		return fmt.Errorf("updateRepository: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -98,7 +98,7 @@ func prepareRepoCommit(ctx models.DBContext, repo *models.Repository, tmpDir, re
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// initRepoCommit temporarily changes with work directory.
 | 
			
		||||
func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User) (err error) {
 | 
			
		||||
func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User, defaultBranch string) (err error) {
 | 
			
		||||
	commitTimeStr := time.Now().Format(time.RFC3339)
 | 
			
		||||
 | 
			
		||||
	sig := u.NewGitSig()
 | 
			
		||||
@@ -145,7 +145,11 @@ func initRepoCommit(tmpPath string, repo *models.Repository, u *models.User) (er
 | 
			
		||||
		return fmt.Errorf("git commit: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if stdout, err := git.NewCommand("push", "origin", "master").
 | 
			
		||||
	if len(defaultBranch) == 0 {
 | 
			
		||||
		defaultBranch = "master"
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if stdout, err := git.NewCommand("push", "origin", "master:"+defaultBranch).
 | 
			
		||||
		SetDescription(fmt.Sprintf("initRepoCommit (git push): %s", tmpPath)).
 | 
			
		||||
		RunInDirWithEnv(tmpPath, models.InternalPushingEnvironment(u, repo)); err != nil {
 | 
			
		||||
		log.Error("Failed to push back to master: Stdout: %s\nError: %v", stdout, err)
 | 
			
		||||
@@ -190,7 +194,7 @@ func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Apply changes and commit.
 | 
			
		||||
		if err = initRepoCommit(tmpDir, repo, u); err != nil {
 | 
			
		||||
		if err = initRepoCommit(tmpDir, repo, u, opts.DefaultBranch); err != nil {
 | 
			
		||||
			return fmt.Errorf("initRepoCommit: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@@ -206,6 +210,10 @@ func initRepository(ctx models.DBContext, repoPath string, u *models.User, repo
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	repo.DefaultBranch = "master"
 | 
			
		||||
	if len(opts.DefaultBranch) > 0 {
 | 
			
		||||
		repo.DefaultBranch = opts.DefaultBranch
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = models.UpdateRepositoryCtx(ctx, repo, false); err != nil {
 | 
			
		||||
		return fmt.Errorf("updateRepository: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user