Provide static .patch links for merge request versions

Problem Statement

GitLab currently provides dynamic diff URLs for Merge Requests (e.g., https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/18171.diff), but these URLs always return the latest version of the MR diff. This creates issues when teams use these diff URLs in automated pipelines or for applying patches.

Current Issues with Dynamic Diff URLs

  1. Lack of Version Consistency: Teams cannot reliably reference a specific version of a patch, making it impossible to ensure that the exact reviewed and tested changes are being applied.

  2. Missing Functionality Gap: While GitLab's UI already supports version-specific diff viewing via diff_id parameters (e.g., https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/18171/diffs?diff_id=24129347), this functionality is not available for downloadable diff files.

Real-World Impact

Communities like Drupal extensively use GitLab diff URLs in their patch management workflows. Without static patch versions, they face deployment inconsistencies and inability to pin specific patch versions for testing and production.

Proposed Solution

Extend the existing diff file generation functionality to accept diff_id query parameters, enabling static patch versions:

https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/18171.diff?diff_id=24129347

This approach leverages GitLab's existing diff versioning infrastructure and provides consistency with existing UI patterns while maintaining backward compatibility with current dynamic URLs.

Implementation Approach

The implementation should modify the diff file generation endpoint to:

  1. Accept diff_id parameter: Check for the presence of diff_id in query parameters
  2. Retrieve specific diff version: When diff_id is provided, fetch the specific diff version instead of the latest
  3. Maintain backward compatibility: Continue returning latest diff when no diff_id is specified
  4. Error handling: Return appropriate errors for invalid or non-existent diff_id values

Possible Implementation

The code right now in Projects::MergeRequestsController#show_merge_request is:

send_git_patch @project.repository, @merge_request.diff_refs

So it can be something like (disclaimer: this isn't tested and only to illustrate the idea):

send_git_patch @project.repository, @merge_request.merge_request_diffs.find_by(id: params[:diff_id])&.diff_refs
Edited by 🤖 GitLab Bot 🤖