Skip to content

Commit f48dce3

Browse files
noerwtechknowlogickzeripath
authored
Don't return binary file changes in raw PR diffs by default (#17158)
* return diffs without binary file content change * ?binary=true option to restore old behaviour Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: zeripath <art27@cantab.net>
1 parent e8574f2 commit f48dce3

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

modules/git/repo_compare.go

+14-6
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,29 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int,
215215
}
216216

217217
// GetDiffOrPatch generates either diff or formatted patch data between given revisions
218-
func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, formatted bool) error {
219-
if formatted {
218+
func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, patch, binary bool) error {
219+
if patch {
220220
return repo.GetPatch(base, head, w)
221221
}
222+
if binary {
223+
return repo.GetDiffBinary(base, head, w)
224+
}
222225
return repo.GetDiff(base, head, w)
223226
}
224227

225-
// GetDiff generates and returns patch data between given revisions.
228+
// GetDiff generates and returns patch data between given revisions, optimized for human readability
226229
func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
230+
return NewCommand("diff", "-p", base, head).
231+
RunInDirPipeline(repo.Path, w, nil)
232+
}
233+
234+
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
235+
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
227236
return NewCommand("diff", "-p", "--binary", base, head).
228237
RunInDirPipeline(repo.Path, w, nil)
229238
}
230239

231-
// GetPatch generates and returns format-patch data between given revisions.
240+
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
232241
func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
233242
stderr := new(bytes.Buffer)
234243
err := NewCommand("format-patch", "--binary", "--stdout", base+"..."+head).
@@ -246,8 +255,7 @@ func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) err
246255
err := NewCommand("diff", "-p", "--binary", base+"..."+head).
247256
RunInDirPipeline(repo.Path, w, stderr)
248257
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
249-
return NewCommand("diff", "-p", "--binary", base, head).
250-
RunInDirPipeline(repo.Path, w, nil)
258+
return repo.GetDiffBinary(base, head, w)
251259
}
252260
return err
253261
}

routers/api/v1/repo/pull.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ func DownloadPullDiffOrPatch(ctx *context.APIContext) {
204204
// type: string
205205
// enum: [diff, patch]
206206
// required: true
207+
// - name: binary
208+
// in: query
209+
// description: whether to include binary file changes. if true, the diff is applicable with `git apply`
210+
// type: boolean
207211
// responses:
208212
// "200":
209213
// "$ref": "#/responses/string"
@@ -225,7 +229,9 @@ func DownloadPullDiffOrPatch(ctx *context.APIContext) {
225229
patch = true
226230
}
227231

228-
if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch); err != nil {
232+
binary := ctx.FormBool("binary")
233+
234+
if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch, binary); err != nil {
229235
ctx.InternalServerError(err)
230236
return
231237
}

routers/web/repo/pull.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,9 @@ func DownloadPullDiffOrPatch(ctx *context.Context, patch bool) {
13451345
}
13461346

13471347
pr := issue.PullRequest
1348+
binary := ctx.FormBool("binary")
13481349

1349-
if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch); err != nil {
1350+
if err := pull_service.DownloadDiffOrPatch(pr, ctx, patch, binary); err != nil {
13501351
ctx.ServerError("DownloadDiffOrPatch", err)
13511352
return
13521353
}

services/pull/patch.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
// DownloadDiffOrPatch will write the patch for the pr to the writer
25-
func DownloadDiffOrPatch(pr *models.PullRequest, w io.Writer, patch bool) error {
25+
func DownloadDiffOrPatch(pr *models.PullRequest, w io.Writer, patch, binary bool) error {
2626
if err := pr.LoadBaseRepo(); err != nil {
2727
log.Error("Unable to load base repository ID %d for pr #%d [%d]", pr.BaseRepoID, pr.Index, pr.ID)
2828
return err
@@ -33,7 +33,7 @@ func DownloadDiffOrPatch(pr *models.PullRequest, w io.Writer, patch bool) error
3333
return fmt.Errorf("OpenRepository: %v", err)
3434
}
3535
defer gitRepo.Close()
36-
if err := gitRepo.GetDiffOrPatch(pr.MergeBase, pr.GetGitRefName(), w, patch); err != nil {
36+
if err := gitRepo.GetDiffOrPatch(pr.MergeBase, pr.GetGitRefName(), w, patch, binary); err != nil {
3737
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
3838
return fmt.Errorf("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
3939
}
@@ -108,7 +108,7 @@ func checkConflicts(pr *models.PullRequest, gitRepo *git.Repository, tmpBasePath
108108
_ = util.Remove(tmpPatchFile.Name())
109109
}()
110110

111-
if err := gitRepo.GetDiff(pr.MergeBase, "tracking", tmpPatchFile); err != nil {
111+
if err := gitRepo.GetDiffBinary(pr.MergeBase, "tracking", tmpPatchFile); err != nil {
112112
tmpPatchFile.Close()
113113
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
114114
return false, fmt.Errorf("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)

templates/swagger/v1_json.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -7400,6 +7400,12 @@
74007400
"name": "diffType",
74017401
"in": "path",
74027402
"required": true
7403+
},
7404+
{
7405+
"type": "boolean",
7406+
"description": "whether to include binary file changes. if true, the diff is applicable with `git apply`",
7407+
"name": "binary",
7408+
"in": "query"
74037409
}
74047410
],
74057411
"responses": {

0 commit comments

Comments
 (0)