mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Handle more pathological branch and tag names (#11843)
* Handle more pathological branch and tag names Signed-off-by: Andrew Thornton <art27@cantab.net> * Fix failing test Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
		@@ -9,7 +9,6 @@ import (
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// GetBranch returns a branch by its name
 | 
			
		||||
@@ -76,39 +75,9 @@ func CreateNewBranch(doer *models.User, repo *models.Repository, oldBranchName,
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	basePath, err := models.CreateTemporaryPath("branch-maker")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := models.RemoveTemporaryPath(basePath); err != nil {
 | 
			
		||||
			log.Error("CreateNewBranch: RemoveTemporaryPath: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
 | 
			
		||||
		Bare:   true,
 | 
			
		||||
		Shared: true,
 | 
			
		||||
	}); err != nil {
 | 
			
		||||
		log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
 | 
			
		||||
		return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	gitRepo, err := git.OpenRepository(basePath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
 | 
			
		||||
		return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
 | 
			
		||||
	}
 | 
			
		||||
	defer gitRepo.Close()
 | 
			
		||||
 | 
			
		||||
	if err = gitRepo.CreateBranch(branchName, oldBranchName); err != nil {
 | 
			
		||||
		log.Error("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
 | 
			
		||||
		return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, oldBranchName, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = git.Push(basePath, git.PushOptions{
 | 
			
		||||
		Remote: "origin",
 | 
			
		||||
		Branch: branchName,
 | 
			
		||||
	if err := git.Push(repo.RepoPath(), git.PushOptions{
 | 
			
		||||
		Remote: repo.RepoPath(),
 | 
			
		||||
		Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName),
 | 
			
		||||
		Env:    models.PushingEnvironment(doer, repo),
 | 
			
		||||
	}); err != nil {
 | 
			
		||||
		if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
 | 
			
		||||
@@ -126,39 +95,10 @@ func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commi
 | 
			
		||||
	if err := checkBranchName(repo, branchName); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	basePath, err := models.CreateTemporaryPath("branch-maker")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	defer func() {
 | 
			
		||||
		if err := models.RemoveTemporaryPath(basePath); err != nil {
 | 
			
		||||
			log.Error("CreateNewBranchFromCommit: RemoveTemporaryPath: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	if err := git.Clone(repo.RepoPath(), basePath, git.CloneRepoOptions{
 | 
			
		||||
		Bare:   true,
 | 
			
		||||
		Shared: true,
 | 
			
		||||
	}); err != nil {
 | 
			
		||||
		log.Error("Failed to clone repository: %s (%v)", repo.FullName(), err)
 | 
			
		||||
		return fmt.Errorf("Failed to clone repository: %s (%v)", repo.FullName(), err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	gitRepo, err := git.OpenRepository(basePath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Unable to open temporary repository: %s (%v)", basePath, err)
 | 
			
		||||
		return fmt.Errorf("Failed to open new temporary repository in: %s %v", basePath, err)
 | 
			
		||||
	}
 | 
			
		||||
	defer gitRepo.Close()
 | 
			
		||||
 | 
			
		||||
	if err = gitRepo.CreateBranch(branchName, commit); err != nil {
 | 
			
		||||
		log.Error("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
 | 
			
		||||
		return fmt.Errorf("Unable to create branch: %s from %s. (%v)", branchName, commit, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = git.Push(basePath, git.PushOptions{
 | 
			
		||||
		Remote: "origin",
 | 
			
		||||
		Branch: branchName,
 | 
			
		||||
	if err := git.Push(repo.RepoPath(), git.PushOptions{
 | 
			
		||||
		Remote: repo.RepoPath(),
 | 
			
		||||
		Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
 | 
			
		||||
		Env:    models.PushingEnvironment(doer, repo),
 | 
			
		||||
	}); err != nil {
 | 
			
		||||
		if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user