mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-29 05:06:11 +01:00
Compare commits
6 commits
5058c76f3e
...
004fe296cc
Author | SHA1 | Date | |
---|---|---|---|
004fe296cc | |||
978542cae4 | |||
dc785fdae5 | |||
c9a3e963ec | |||
a887612b75 | |||
a27e4bb586 |
|
@ -1086,10 +1086,19 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
|
||||||
|
|
||||||
ctx.Repo.PullRequest.SameRepo = isSameRepo
|
ctx.Repo.PullRequest.SameRepo = isSameRepo
|
||||||
log.Trace("Repo path: %q, base branch: %q, head branch: %q", ctx.Repo.GitRepo.Path, baseBranch, headBranch)
|
log.Trace("Repo path: %q, base branch: %q, head branch: %q", ctx.Repo.GitRepo.Path, baseBranch, headBranch)
|
||||||
|
|
||||||
// Check if base branch is valid.
|
// Check if base branch is valid.
|
||||||
if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) && !ctx.Repo.GitRepo.IsTagExist(baseBranch) {
|
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(baseBranch)
|
||||||
ctx.NotFound("BaseNotExist")
|
baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(baseBranch)
|
||||||
return nil, nil, nil, "", ""
|
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.
|
// 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if head branch is valid.
|
// Check if head branch is valid.
|
||||||
if !headGitRepo.IsBranchExist(headBranch) && !headGitRepo.IsTagExist(headBranch) {
|
headIsCommit := headGitRepo.IsBranchExist(headBranch)
|
||||||
headGitRepo.Close()
|
headIsBranch := headGitRepo.IsTagExist(headBranch)
|
||||||
ctx.NotFound()
|
headIsTag := headGitRepo.IsCommitExist(baseBranch)
|
||||||
return nil, nil, nil, "", ""
|
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, "", ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, false, false)
|
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 {
|
if err != nil {
|
||||||
headGitRepo.Close()
|
headGitRepo.Close()
|
||||||
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
|
ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
|
||||||
|
|
|
@ -36,3 +36,22 @@ func TestAPICompareBranches(t *testing.T) {
|
||||||
assert.Equal(t, 2, apiResp.TotalCommits)
|
assert.Equal(t, 2, apiResp.TotalCommits)
|
||||||
assert.Len(t, apiResp.Commits, 2)
|
assert.Len(t, apiResp.Commits, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAPICompareCommits(t *testing.T) {
|
||||||
|
defer tests.PrepareTestEnv(t)()
|
||||||
|
|
||||||
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
|
// Login as User2.
|
||||||
|
session := loginUser(t, user.Name)
|
||||||
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
||||||
|
|
||||||
|
req := NewRequestf(t, "GET", "/api/v1/repos/user2/repo20/compare/c8e31bc...8babce9").
|
||||||
|
AddTokenAuth(token)
|
||||||
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
var apiResp *api.Compare
|
||||||
|
DecodeJSON(t, resp, &apiResp)
|
||||||
|
|
||||||
|
assert.Equal(t, 2, apiResp.TotalCommits)
|
||||||
|
assert.Len(t, apiResp.Commits, 2)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue