summaryrefslogtreecommitdiffstats
path: root/routers/api/v1
diff options
context:
space:
mode:
authorangelnu <angelnu@noreply.codeberg.org>2024-11-16 18:12:40 +0100
committerAngel Nunez Mencias <git@angelnu.com>2024-11-16 18:12:40 +0100
commitd2dc4fae3ae8d4810f9f9b537189c897361b58ab (patch)
tree5c960d268d17c3be9dc4439e05b73fabbebb1eec /routers/api/v1
parentcheck IsCommitExist (diff)
downloadforgejo-d2dc4fae3ae8d4810f9f9b537189c897361b58ab.tar.xz
forgejo-d2dc4fae3ae8d4810f9f9b537189c897361b58ab.zip
review changes
Diffstat (limited to 'routers/api/v1')
-rw-r--r--routers/api/v1/repo/pull.go50
1 files changed, 40 insertions, 10 deletions
diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go
index 0238e18ace..6ca23f1e6d 100644
--- a/routers/api/v1/repo/pull.go
+++ b/routers/api/v1/repo/pull.go
@@ -1110,10 +1110,19 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
ctx.Repo.PullRequest.SameRepo = isSameRepo
log.Trace("Repo path: %q, base branch: %q, head branch: %q", ctx.Repo.GitRepo.Path, baseBranch, headBranch)
+
// Check if base branch is valid.
- if !ctx.Repo.GitRepo.IsCommitExist(baseBranch) && !ctx.Repo.GitRepo.IsBranchExist(baseBranch) && !ctx.Repo.GitRepo.IsTagExist(baseBranch) {
- ctx.NotFound("BaseNotExist")
- return nil, nil, nil, "", ""
+ baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(baseBranch)
+ baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(baseBranch)
+ baseIsTag := ctx.Repo.GitRepo.IsTagExist(baseBranch)
+ if !baseIsCommit && !baseIsBranch && !baseIsTag {
+ // Check for short SHA usage
+ if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(baseBranch); baseCommit != nil {
+ baseBranch = baseCommit.ID.String()
+ } else {
+ ctx.NotFound("BaseNotExist")
+ return nil, nil, nil, "", ""
+ }
}
// Check if current user has fork of repository or in the same repository.
@@ -1186,13 +1195,34 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
}
// Check if head branch is valid.
- if !ctx.Repo.GitRepo.IsCommitExist(baseBranch) && !headGitRepo.IsBranchExist(headBranch) && !headGitRepo.IsTagExist(headBranch) {
- headGitRepo.Close()
- ctx.NotFound()
- return nil, nil, nil, "", ""
- }
-
- compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, false, false)
+ headIsCommit := headGitRepo.IsBranchExist(headBranch)
+ headIsBranch := headGitRepo.IsTagExist(headBranch)
+ headIsTag := headGitRepo.IsCommitExist(baseBranch)
+ if !headIsCommit && !headIsBranch && !headIsTag {
+ // Check if headBranch is short sha commit hash
+ if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil {
+ headBranch = headCommit.ID.String()
+ } else {
+ headGitRepo.Close()
+ ctx.NotFound("IsRefExist", nil)
+ return nil, nil, nil, "", ""
+ }
+ }
+
+ baseBranchRef := baseBranch
+ if baseIsBranch {
+ baseBranchRef = git.BranchPrefix + baseBranch
+ } else if baseIsTag {
+ baseBranchRef = git.TagPrefix + baseBranch
+ }
+ headBranchRef := headBranch
+ if headIsBranch {
+ headBranchRef = headBranch
+ } else if headIsTag {
+ headBranchRef = headBranch
+ }
+
+ compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranchRef, headBranchRef, false, false)
if err != nil {
headGitRepo.Close()
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)