diff options
author | zeripath <art27@cantab.net> | 2020-06-12 00:49:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-11 19:49:47 -0400 |
commit | 0973c036019c955172ebce99d58eede2e9ac55ca (patch) | |
tree | 7c49364bba0ebfb4eb8c4bb5194f643629b929bc /modules | |
parent | 6c2a59b50c2af367281492b6c6adc9061e57b0a9 (diff) | |
download | gitea-0973c036019c955172ebce99d58eede2e9ac55ca.tar.gz gitea-0973c036019c955172ebce99d58eede2e9ac55ca.zip |
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>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo_commit.go | 2 | ||||
-rw-r--r-- | modules/repository/branch.go | 72 |
2 files changed, 7 insertions, 67 deletions
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index e282c9c674..479a0d0370 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -46,7 +46,7 @@ func (repo *Repository) GetBranchCommitID(name string) (string, error) { // GetTagCommitID returns last commit ID string of given tag. func (repo *Repository) GetTagCommitID(name string) (string, error) { - stdout, err := NewCommand("rev-list", "-n", "1", name).RunInDir(repo.Path) + stdout, err := NewCommand("rev-list", "-n", "1", TagPrefix+name).RunInDir(repo.Path) if err != nil { if strings.Contains(err.Error(), "unknown revision or path") { return "", ErrNotExist{name, ""} diff --git a/modules/repository/branch.go b/modules/repository/branch.go index 94be6f0f5a..d369a200b0 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -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) { |