summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2020-06-12 19:01:44 +0100
committerGitHub <noreply@github.com>2020-06-12 14:01:44 -0400
commit320031fce693ffe8869da952f3f872b46d3d6550 (patch)
tree577a6f0d379ccdc933f6955ff5b064e42a38c858 /routers
parentef2f18964e86634383e78b7960781dce950abe59 (diff)
downloadgitea-320031fce693ffe8869da952f3f872b46d3d6550.tar.gz
gitea-320031fce693ffe8869da952f3f872b46d3d6550.zip
Handle more pathological branch and tag names (#11843) (#11863)
Backport #11843 It's possible to push quite pathological appearing branch names to gitea using git push gitea reasonable-branch:refs/heads/-- at which point large parts of the UI will break. Similarly you can git push origin reasonable-tag:refs/tags/-- which wil return an error. This PR fixes the problems these cause. It also changes the code from creating branches to pushing to ensure that branch restoration has to pass hooks. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/branch.go17
-rw-r--r--routers/repo/compare.go15
-rw-r--r--routers/repo/pull.go2
3 files changed, 24 insertions, 10 deletions
diff --git a/routers/repo/branch.go b/routers/repo/branch.go
index e7eac04bce..4d8b9158fe 100644
--- a/routers/repo/branch.go
+++ b/routers/repo/branch.go
@@ -6,6 +6,7 @@
package repo
import (
+ "fmt"
"strings"
"code.gitea.io/gitea/models"
@@ -102,7 +103,11 @@ func RestoreBranchPost(ctx *context.Context) {
return
}
- if err := ctx.Repo.GitRepo.CreateBranch(deletedBranch.Name, deletedBranch.Commit); err != nil {
+ if err := git.Push(ctx.Repo.Repository.RepoPath(), git.PushOptions{
+ Remote: ctx.Repo.Repository.RepoPath(),
+ Branch: fmt.Sprintf("%s:%s%s", deletedBranch.Commit, git.BranchPrefix, deletedBranch.Name),
+ Env: models.PushingEnvironment(ctx.User, ctx.Repo.Repository),
+ }); err != nil {
if strings.Contains(err.Error(), "already exists") {
ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
return
@@ -112,12 +117,6 @@ func RestoreBranchPost(ctx *context.Context) {
return
}
- if err := ctx.Repo.Repository.RemoveDeletedBranch(deletedBranch.ID); err != nil {
- log.Error("RemoveDeletedBranch: %v", err)
- ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
- return
- }
-
// Don't return error below this
if err := repofiles.PushUpdate(
ctx.Repo.Repository,
@@ -216,7 +215,7 @@ func loadBranches(ctx *context.Context) []*Branch {
}
}
- divergence, divergenceError := repofiles.CountDivergingCommits(ctx.Repo.Repository, branchName)
+ divergence, divergenceError := repofiles.CountDivergingCommits(ctx.Repo.Repository, git.BranchPrefix+branchName)
if divergenceError != nil {
ctx.ServerError("CountDivergingCommits", divergenceError)
return nil
@@ -331,6 +330,8 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
var err error
if ctx.Repo.IsViewBranch {
err = repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
+ } else if ctx.Repo.IsViewTag {
+ err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
} else {
err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
}
diff --git a/routers/repo/compare.go b/routers/repo/compare.go
index 97bb5e6b18..3ffabc14a1 100644
--- a/routers/repo/compare.go
+++ b/routers/repo/compare.go
@@ -381,7 +381,20 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, *
return nil, nil, nil, nil, "", ""
}
- compareInfo, err := headGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranch, headBranch)
+ baseBranchRef := baseBranch
+ if baseIsBranch {
+ baseBranchRef = git.BranchPrefix + baseBranch
+ } else if baseIsTag {
+ baseBranchRef = git.TagPrefix + baseBranch
+ }
+ headBranchRef := headBranch
+ if headIsBranch {
+ headBranchRef = git.BranchPrefix + headBranch
+ } else if headIsTag {
+ headBranchRef = git.TagPrefix + headBranch
+ }
+
+ compareInfo, err := headGitRepo.GetCompareInfo(baseRepo.RepoPath(), baseBranchRef, headBranchRef)
if err != nil {
ctx.ServerError("GetCompareInfo", err)
return nil, nil, nil, nil, "", ""
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index ec82d14f18..da826026b3 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -484,7 +484,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare
}
compareInfo, err := baseGitRepo.GetCompareInfo(pull.BaseRepo.RepoPath(),
- pull.BaseBranch, pull.GetGitRefName())
+ git.BranchPrefix+pull.BaseBranch, pull.GetGitRefName())
if err != nil {
if strings.Contains(err.Error(), "fatal: Not a valid object name") {
ctx.Data["IsPullRequestBroken"] = true