feat: add raw diff/patch endpoint for repository comparisons (#37632)

## Summary

Adds `GET
/repos/{owner}/{repo}/compare/{basehead}.{diffType:diff|patch}`,
mirroring the existing `/git/commits/{sha}.{diffType}` endpoint but for
comparisons between two arbitrary refs.

The new endpoint streams a raw unified diff or `git format-patch` output
between any two refs:

GET /repos/{owner}/{repo}/compare/main...feature.diff
GET /repos/{owner}/{repo}/compare/v1.0..v1.1.patch
GET /repos/{owner}/{repo}/compare/abc1234...def5678.diff

Resolves #5561, #13416 and #17165.

AI was used while creating this PR. Automated tests were added as per
the contribution policy.

---------

Co-authored-by: bircni <bircni@icloud.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Rafail Giavrimis
2026-06-15 17:37:15 +01:00
committed by GitHub
parent b4cb192fba
commit 052feee34a
8 changed files with 275 additions and 11 deletions
+14 -2
View File
@@ -8108,8 +8108,10 @@
},
"/repos/{owner}/{repo}/compare/{basehead}": {
"get": {
"description": "By default returns JSON commit comparison information. The raw diff or patch can be\nrequested with the `output` query parameter set to `diff` or `patch` respectively.\n",
"produces": [
"application/json"
"application/json",
"text/plain"
],
"tags": [
"repository"
@@ -8133,10 +8135,20 @@
},
{
"type": "string",
"description": "compare two branches or commits",
"description": "compare two refs as `base...head` (or `base..head`); refs may be branches, tags, full or short SHAs, including branch names that contain slashes.",
"name": "basehead",
"in": "path",
"required": true
},
{
"enum": [
"diff",
"patch"
],
"type": "string",
"description": "return the raw comparison as `diff` or `patch` instead of JSON",
"name": "output",
"in": "query"
}
],
"responses": {
+14 -1
View File
@@ -19468,6 +19468,7 @@
},
"/repos/{owner}/{repo}/compare/{basehead}": {
"get": {
"description": "By default returns JSON commit comparison information. The raw diff or patch can be\nrequested with the `output` query parameter set to `diff` or `patch` respectively.\n",
"operationId": "repoCompareDiff",
"parameters": [
{
@@ -19489,13 +19490,25 @@
}
},
{
"description": "compare two branches or commits",
"description": "compare two refs as `base...head` (or `base..head`); refs may be branches, tags, full or short SHAs, including branch names that contain slashes.",
"in": "path",
"name": "basehead",
"required": true,
"schema": {
"type": "string"
}
},
{
"description": "return the raw comparison as `diff` or `patch` instead of JSON",
"in": "query",
"name": "output",
"schema": {
"enum": [
"diff",
"patch"
],
"type": "string"
}
}
],
"responses": {