Load first compare diff in file-by-file mode
What does this MR do and why?
On the "Compare revisions" page, enabling the "Show one file at a time" preference left the diffs area empty on load: the file browser and view settings appeared, but no diff and no diff stats showed up, with no error.
The compare page renders zero diff files on the server (by design it streams all diffs), so in single-file mode there was no rendered file for the client to select and nothing loaded. The root cause was that neither side owned which file is current in single-file mode: the client guessed the file browser's first entry while the server rendered the first file of a separately sorted slice, and the two agreed only by coincidence.
The server now names the file it rendered (initial_file) and the client selects that exact file. In single-file mode the presenter fetches just that one file, so compare renders its first file instead of nothing, which fixes the empty page. On the lazy new-merge-request page nothing is rendered server side, so initial_file is absent and the client loads the current file itself.
The merge request and commit pages are unaffected in behavior: initial_file names the file they render and the client selects it without a double load. In single-file mode they now fetch one file rather than the first of a five-file slice, since streaming is off and only one file renders.
Behavior change to note: when a file param names a file that is not part of the compare diff, the page now renders the first file instead of staying empty. This matches what the merge request and commit pages already did, but it is a change from compare's prior behavior.
Minor logic change alongside this: the linked-file guard now treats a blank file_path param as no linked file, so a stray empty value no longer triggers a wasteful lookup for an empty path.
References
- Closes #627444 (closed)
Screenshots or screen recordings
| Before | After |
|---|---|
![]() Empty diffs area on the compare page with file-by-file enabled |
![]() First diff and its stats load |
How to set up and validate locally
- Enable "Show one file at a time" from the settings gear on any diff page (or
PUT /api/v4/user/preferenceswithview_diffs_file_by_file=true). - Visit a compare page, for example
/gitlab-org/gitlab-test/-/compare/master...spooky-stuff. - Confirm the first diff file and its diff stats now load. Before this change, the diffs area stayed empty until you toggled the Inline/Side-by-side view setting.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

