Skip to content

Commit b8b690f

Browse files
lunnywxiaoguang
andauthored
Refactor getpatch/getdiff functions and remove unnecessary fallback (#32817)
Extract from #32786 `git diff a..b` is equal to `git diff a b` which is different from `git diff a...b`. For pull request, we should always --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent 6d5aa92 commit b8b690f

File tree

3 files changed

+24
-67
lines changed

3 files changed

+24
-67
lines changed

modules/git/repo_compare.go

+9-62
Original file line numberDiff line numberDiff line change
@@ -233,72 +233,34 @@ func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int,
233233
return numFiles, totalAdditions, totalDeletions, err
234234
}
235235

236-
// GetDiffOrPatch generates either diff or formatted patch data between given revisions
237-
func (repo *Repository) GetDiffOrPatch(base, head string, w io.Writer, patch, binary bool) error {
238-
if patch {
239-
return repo.GetPatch(base, head, w)
240-
}
241-
if binary {
242-
return repo.GetDiffBinary(base, head, w)
243-
}
244-
return repo.GetDiff(base, head, w)
245-
}
246-
247236
// GetDiff generates and returns patch data between given revisions, optimized for human readability
248-
func (repo *Repository) GetDiff(base, head string, w io.Writer) error {
237+
func (repo *Repository) GetDiff(compareArg string, w io.Writer) error {
249238
stderr := new(bytes.Buffer)
250-
err := NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base + "..." + head).
239+
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(compareArg).
251240
Run(&RunOpts{
252241
Dir: repo.Path,
253242
Stdout: w,
254243
Stderr: stderr,
255244
})
256-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
257-
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base, head).
258-
Run(&RunOpts{
259-
Dir: repo.Path,
260-
Stdout: w,
261-
})
262-
}
263-
return err
264245
}
265246

266247
// GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
267-
func (repo *Repository) GetDiffBinary(base, head string, w io.Writer) error {
268-
stderr := new(bytes.Buffer)
269-
err := NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base + "..." + head).
270-
Run(&RunOpts{
271-
Dir: repo.Path,
272-
Stdout: w,
273-
Stderr: stderr,
274-
})
275-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
276-
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(base, head).
277-
Run(&RunOpts{
278-
Dir: repo.Path,
279-
Stdout: w,
280-
})
281-
}
282-
return err
248+
func (repo *Repository) GetDiffBinary(compareArg string, w io.Writer) error {
249+
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(compareArg).Run(&RunOpts{
250+
Dir: repo.Path,
251+
Stdout: w,
252+
})
283253
}
284254

285255
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
286-
func (repo *Repository) GetPatch(base, head string, w io.Writer) error {
256+
func (repo *Repository) GetPatch(compareArg string, w io.Writer) error {
287257
stderr := new(bytes.Buffer)
288-
err := NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base + "..." + head).
258+
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(compareArg).
289259
Run(&RunOpts{
290260
Dir: repo.Path,
291261
Stdout: w,
292262
Stderr: stderr,
293263
})
294-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
295-
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base, head).
296-
Run(&RunOpts{
297-
Dir: repo.Path,
298-
Stdout: w,
299-
})
300-
}
301-
return err
302264
}
303265

304266
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
@@ -329,21 +291,6 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err
329291
return split, err
330292
}
331293

332-
// GetDiffFromMergeBase generates and return patch data from merge base to head
333-
func (repo *Repository) GetDiffFromMergeBase(base, head string, w io.Writer) error {
334-
stderr := new(bytes.Buffer)
335-
err := NewCommand(repo.Ctx, "diff", "-p", "--binary").AddDynamicArguments(base + "..." + head).
336-
Run(&RunOpts{
337-
Dir: repo.Path,
338-
Stdout: w,
339-
Stderr: stderr,
340-
})
341-
if err != nil && bytes.Contains(stderr.Bytes(), []byte("no merge base")) {
342-
return repo.GetDiffBinary(base, head, w)
343-
}
344-
return err
345-
}
346-
347294
// ReadPatchCommit will check if a diff patch exists and return stats
348295
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
349296
// Migrated repositories download patches to "pulls" location

modules/git/repo_compare_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestGetFormatPatch(t *testing.T) {
2828
defer repo.Close()
2929

3030
rd := &bytes.Buffer{}
31-
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
31+
err = repo.GetPatch("8d92fc95^...8d92fc95", rd)
3232
if err != nil {
3333
assert.NoError(t, err)
3434
return

services/pull/patch.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,19 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io
4141
}
4242
defer closer.Close()
4343

44-
if err := gitRepo.GetDiffOrPatch(pr.MergeBase, pr.GetGitRefName(), w, patch, binary); err != nil {
45-
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
46-
return fmt.Errorf("Unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
44+
compareArg := pr.MergeBase + "..." + pr.GetGitRefName()
45+
switch {
46+
case patch:
47+
err = gitRepo.GetPatch(compareArg, w)
48+
case binary:
49+
err = gitRepo.GetDiffBinary(compareArg, w)
50+
default:
51+
err = gitRepo.GetDiff(compareArg, w)
52+
}
53+
54+
if err != nil {
55+
log.Error("unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
56+
return fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
4757
}
4858
return nil
4959
}
@@ -354,7 +364,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo *
354364
_ = util.Remove(tmpPatchFile.Name())
355365
}()
356366

357-
if err := gitRepo.GetDiffBinary(pr.MergeBase, "tracking", tmpPatchFile); err != nil {
367+
if err := gitRepo.GetDiffBinary(pr.MergeBase+"...tracking", tmpPatchFile); err != nil {
358368
tmpPatchFile.Close()
359369
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
360370
return false, fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)

0 commit comments

Comments
 (0)