fix: Return empty keyset commits for unknown refs
Why
With commits_keyset_pagination enabled, GET /projects/:id/repository/commits?pagination=keyset returned a 500 for a ref_name that does not resolve, such as a deleted branch. The offset path returns an empty list for the same request. Left as is, these would show up as new 500s during the commits_keyset_pagination rollout.
What
Gitaly's ListCommits fails with a bare Internal "bad revision" error, with no structured detail to match on. The keyset path of the Commits API now rescues that error and returns an empty list with no next-page Link header, but only after Gitlab::Git::Commit.find_commit confirms the ref does not resolve. For a range, it checks each endpoint.
- If the lookup itself fails, the original
ListCommitserror is re-raised, so Gitaly outages and timeouts still surface. - Any failure with
all=truestill raises. - Range endpoints that
Commit.valid?rejects are treated as unknown, which matches offset pagination. - The extra lookup only runs after an error, so successful requests cost nothing more.
CommitsFinderis unchanged, so the Orbit internal API (which also uses its keyset path and returns 400 for an unknown ref) keeps its current behaviour.- The keyset path also rescues
Gitlab::Git::Repository::CommitNotFound, which is how Rails surfaces the structuredListCommitsNotFound from gitaly!9517. It returns an empty list without a ref lookup, and still raises whenall=true. TheCommandErrorpath stays as a bridge until Gitaly with that change is the minimum supported version.
The new behaviour only runs when both conditions are true:
- the
commits_keyset_paginationflag is enabled for the project, and - the request sends
pagination=keyset.
How to validate locally
1. Create a test project with a few commits. Pushing to a project that doesn't exist yet creates it as a private project:
git init keyset-validate && cd keyset-validate
for i in $(seq 1 10); do git commit -q --allow-empty -m "Commit $i"; done
git push http://<gitlab-host>/<namespace>/keyset-validate.git HEAD:main2. Enable the flag for that project in a Rails console:
Feature.enable(:commits_keyset_pagination, Project.find_by_full_path('<namespace>/keyset-validate'))3. Request commits for refs that don't exist, and for a valid range:
export TOKEN=<personal access token with read_api>
export API="http://<gitlab-host>/api/v4/projects/<namespace>%2Fkeyset-validate/repository"
for ref in does-not-exist 'main..does-not-exist' '%5Edoes-not-exist' 'main~5..main'; do
code=$(curl -s -o body.json -w '%{http_code}' -H "PRIVATE-TOKEN: $TOKEN" "$API/commits?pagination=keyset&ref_name=$ref")
echo "$ref -> $code $(jq -c 'if type == "array" then "\(length) commits" else . end' body.json)"
doneref_name |
On master |
With this MR |
|---|---|---|
does-not-exist |
500 | 200, 0 commits |
main..does-not-exist |
500 | 200, 0 commits |
^does-not-exist |
500 | 200, 0 commits |
main~5..main |
200, 5 commits | 200, 5 commits (no change) |
Repeat any request without pagination=keyset: offset pagination returns the same 200 [].
4. Optional: check that outages still raise. Stop Gitaly and repeat the does-not-exist request. It should return an error status, not 200 []. Start Gitaly again afterwards.
Related
- Rollout issue: #602360
- Epic: &21573
- Endpoint wiring: !240477 (merged)
- Sibling fix: !262927 (merged)