Skip to content

Commit 004fe29

Browse files
author
Gusted
committed
Merge pull request '[v9.0/forgejo] fix: api repo compare with commit hashes' (go-gitea#5993) from bp-v9.0/forgejo-e434ecd-d2dc4fa-1b9d124-01c9c19-ca0cd42 into v9.0/forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5993 Reviewed-by: Gusted <[email protected]>
2 parents 5058c76 + 978542c commit 004fe29

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

routers/api/v1/repo/pull.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,10 +1086,19 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
10861086

10871087
ctx.Repo.PullRequest.SameRepo = isSameRepo
10881088
log.Trace("Repo path: %q, base branch: %q, head branch: %q", ctx.Repo.GitRepo.Path, baseBranch, headBranch)
1089+
10891090
// Check if base branch is valid.
1090-
if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) && !ctx.Repo.GitRepo.IsTagExist(baseBranch) {
1091-
ctx.NotFound("BaseNotExist")
1092-
return nil, nil, nil, "", ""
1091+
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(baseBranch)
1092+
baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(baseBranch)
1093+
baseIsTag := ctx.Repo.GitRepo.IsTagExist(baseBranch)
1094+
if !baseIsCommit && !baseIsBranch && !baseIsTag {
1095+
// Check for short SHA usage
1096+
if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(baseBranch); baseCommit != nil {
1097+
baseBranch = baseCommit.ID.String()
1098+
} else {
1099+
ctx.NotFound("BaseNotExist")
1100+
return nil, nil, nil, "", ""
1101+
}
10931102
}
10941103

10951104
// Check if current user has fork of repository or in the same repository.
@@ -1162,13 +1171,34 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
11621171
}
11631172

11641173
// Check if head branch is valid.
1165-
if !headGitRepo.IsBranchExist(headBranch) && !headGitRepo.IsTagExist(headBranch) {
1166-
headGitRepo.Close()
1167-
ctx.NotFound()
1168-
return nil, nil, nil, "", ""
1174+
headIsCommit := headGitRepo.IsBranchExist(headBranch)
1175+
headIsBranch := headGitRepo.IsTagExist(headBranch)
1176+
headIsTag := headGitRepo.IsCommitExist(baseBranch)
1177+
if !headIsCommit && !headIsBranch && !headIsTag {
1178+
// Check if headBranch is short sha commit hash
1179+
if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil {
1180+
headBranch = headCommit.ID.String()
1181+
} else {
1182+
headGitRepo.Close()
1183+
ctx.NotFound("IsRefExist", nil)
1184+
return nil, nil, nil, "", ""
1185+
}
1186+
}
1187+
1188+
baseBranchRef := baseBranch
1189+
if baseIsBranch {
1190+
baseBranchRef = git.BranchPrefix + baseBranch
1191+
} else if baseIsTag {
1192+
baseBranchRef = git.TagPrefix + baseBranch
1193+
}
1194+
headBranchRef := headBranch
1195+
if headIsBranch {
1196+
headBranchRef = headBranch
1197+
} else if headIsTag {
1198+
headBranchRef = headBranch
11691199
}
11701200

1171-
compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, false, false)
1201+
compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranchRef, headBranchRef, false, false)
11721202
if err != nil {
11731203
headGitRepo.Close()
11741204
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)

tests/integration/api_repo_compare_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,22 @@ func TestAPICompareBranches(t *testing.T) {
3636
assert.Equal(t, 2, apiResp.TotalCommits)
3737
assert.Len(t, apiResp.Commits, 2)
3838
}
39+
40+
func TestAPICompareCommits(t *testing.T) {
41+
defer tests.PrepareTestEnv(t)()
42+
43+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
44+
// Login as User2.
45+
session := loginUser(t, user.Name)
46+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
47+
48+
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo20/compare/c8e31bc...8babce9").
49+
AddTokenAuth(token)
50+
resp := MakeRequest(t, req, http.StatusOK)
51+
52+
var apiResp *api.Compare
53+
DecodeJSON(t, resp, &apiResp)
54+
55+
assert.Equal(t, 2, apiResp.TotalCommits)
56+
assert.Len(t, apiResp.Commits, 2)
57+
}

0 commit comments

Comments
 (0)