loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request 'fix: api repo compare with commit hashes' (#5991) from angelnu/forgejo:angelnu/IsCommitExist into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5991
Reviewed-by: Gusted <gusted@noreply.codeberg.org>

Gusted 8e94947e c01a03e9

+57 -8
+38 -8
routers/api/v1/repo/pull.go
··· 1110 1110 1111 1111 ctx.Repo.PullRequest.SameRepo = isSameRepo 1112 1112 log.Trace("Repo path: %q, base branch: %q, head branch: %q", ctx.Repo.GitRepo.Path, baseBranch, headBranch) 1113 + 1113 1114 // Check if base branch is valid. 1114 - if !ctx.Repo.GitRepo.IsBranchExist(baseBranch) && !ctx.Repo.GitRepo.IsTagExist(baseBranch) { 1115 - ctx.NotFound("BaseNotExist") 1116 - return nil, nil, nil, "", "" 1115 + baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(baseBranch) 1116 + baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(baseBranch) 1117 + baseIsTag := ctx.Repo.GitRepo.IsTagExist(baseBranch) 1118 + if !baseIsCommit && !baseIsBranch && !baseIsTag { 1119 + // Check for short SHA usage 1120 + if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(baseBranch); baseCommit != nil { 1121 + baseBranch = baseCommit.ID.String() 1122 + } else { 1123 + ctx.NotFound("BaseNotExist") 1124 + return nil, nil, nil, "", "" 1125 + } 1117 1126 } 1118 1127 1119 1128 // Check if current user has fork of repository or in the same repository. ··· 1186 1195 } 1187 1196 1188 1197 // Check if head branch is valid. 1189 - if !headGitRepo.IsBranchExist(headBranch) && !headGitRepo.IsTagExist(headBranch) { 1190 - headGitRepo.Close() 1191 - ctx.NotFound() 1192 - return nil, nil, nil, "", "" 1198 + headIsCommit := headGitRepo.IsBranchExist(headBranch) 1199 + headIsBranch := headGitRepo.IsTagExist(headBranch) 1200 + headIsTag := headGitRepo.IsCommitExist(baseBranch) 1201 + if !headIsCommit && !headIsBranch && !headIsTag { 1202 + // Check if headBranch is short sha commit hash 1203 + if headCommit, _ := headGitRepo.GetCommit(headBranch); headCommit != nil { 1204 + headBranch = headCommit.ID.String() 1205 + } else { 1206 + headGitRepo.Close() 1207 + ctx.NotFound("IsRefExist", nil) 1208 + return nil, nil, nil, "", "" 1209 + } 1210 + } 1211 + 1212 + baseBranchRef := baseBranch 1213 + if baseIsBranch { 1214 + baseBranchRef = git.BranchPrefix + baseBranch 1215 + } else if baseIsTag { 1216 + baseBranchRef = git.TagPrefix + baseBranch 1217 + } 1218 + headBranchRef := headBranch 1219 + if headIsBranch { 1220 + headBranchRef = headBranch 1221 + } else if headIsTag { 1222 + headBranchRef = headBranch 1193 1223 } 1194 1224 1195 - compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch, false, false) 1225 + compareInfo, err := headGitRepo.GetCompareInfo(repo_model.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranchRef, headBranchRef, false, false) 1196 1226 if err != nil { 1197 1227 headGitRepo.Close() 1198 1228 ctx.Error(http.StatusInternalServerError, "GetCompareInfo", err)
+19
tests/integration/api_repo_compare_test.go
··· 36 36 assert.Equal(t, 2, apiResp.TotalCommits) 37 37 assert.Len(t, apiResp.Commits, 2) 38 38 } 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 + }