Share the compare API cache between requesters
What does this MR do and why?
GET /projects/:id/repository/compare caches its response for one minute, but the key included the requesting user and the raw from/to strings. The same comparison was written to Redis once per user and once per ref spelling (main, refs/heads/main, the SHA). The response does not vary by requester — can?(:read_code) runs before cache_action — so the key now holds only the resolved SHAs, straight, and unidiff.
Requests with a nil or whitespace-padded ref bypass the cache. The comparison and the key can resolve those differently (Gitlab::Git::Compare strips refs, CrossRepo does not, and a nil ref resolves to the root ref), so caching them risks serving the wrong body.
Not flagged: the body is identical and rollback is a revert, following d43fef94 on the same entity. The anonymous commit cap originally in this MR is now !268855 (merged).
References
How to set up and validate locally
-
Request the same range as two users, then with the head SHA instead of the branch name:
curl --header "PRIVATE-TOKEN: <token>" \ --url "http://gdk.test:3000/api/v4/projects/<project id>/repository/compare?from=<base>&to=<head>" -
Check Redis:
Gitlab::Redis::Cache.with { |r| r.keys('*repository_compare*') }shows one entry. Addingstraight=trueorunidiff=trueadds a second.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist.