Skip to content

Commit 0749bb5

Browse files
committed
Refactor getpatch/getdiff functions and remove fallback automatically
1 parent 0b8a894 commit 0749bb5

File tree

3 files changed

+73
-62
lines changed

3 files changed

+73
-62
lines changed

modules/git/repo_compare.go

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -233,72 +233,63 @@ 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)
236+
func parseCompareArgs(compareArgs string) (args []string) {
237+
parts := strings.Split(compareArgs, "...")
238+
if len(parts) == 2 {
239+
return []string{compareArgs}
240240
}
241-
if binary {
242-
return repo.GetDiffBinary(base, head, w)
241+
parts = strings.Split(compareArgs, "..")
242+
if len(parts) == 2 {
243+
return parts
243244
}
244-
return repo.GetDiff(base, head, w)
245+
parts = strings.Fields(compareArgs)
246+
if len(parts) == 2 {
247+
return parts
248+
}
249+
250+
return nil
245251
}
246252

247253
// 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 {
254+
func (repo *Repository) GetDiff(compareArgs string, w io.Writer) error {
255+
args := parseCompareArgs(compareArgs)
256+
if len(args) == 0 {
257+
return fmt.Errorf("invalid compareArgs: %s", compareArgs)
258+
}
249259
stderr := new(bytes.Buffer)
250-
err := NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(base + "..." + head).
260+
return NewCommand(repo.Ctx, "diff", "-p").AddDynamicArguments(args...).
251261
Run(&RunOpts{
252262
Dir: repo.Path,
253263
Stdout: w,
254264
Stderr: stderr,
255265
})
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
264266
}
265267

266268
// 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-
})
269+
func (repo *Repository) GetDiffBinary(compareArgs string, w io.Writer) error {
270+
args := parseCompareArgs(compareArgs)
271+
if len(args) == 0 {
272+
return fmt.Errorf("invalid compareArgs: %s", compareArgs)
281273
}
282-
return err
274+
return NewCommand(repo.Ctx, "diff", "-p", "--binary", "--histogram").AddDynamicArguments(args...).Run(&RunOpts{
275+
Dir: repo.Path,
276+
Stdout: w,
277+
})
283278
}
284279

285280
// 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 {
281+
func (repo *Repository) GetPatch(compareArgs string, w io.Writer) error {
282+
args := parseCompareArgs(compareArgs)
283+
if len(args) == 0 {
284+
return fmt.Errorf("invalid compareArgs: %s", compareArgs)
285+
}
287286
stderr := new(bytes.Buffer)
288-
err := NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(base + "..." + head).
287+
return NewCommand(repo.Ctx, "format-patch", "--binary", "--stdout").AddDynamicArguments(args...).
289288
Run(&RunOpts{
290289
Dir: repo.Path,
291290
Stdout: w,
292291
Stderr: stderr,
293292
})
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
302293
}
303294

304295
// GetFilesChangedBetween returns a list of all files that have been changed between the given commits
@@ -329,21 +320,6 @@ func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, err
329320
return split, err
330321
}
331322

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-
347323
// ReadPatchCommit will check if a diff patch exists and return stats
348324
func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
349325
// Migrated repositories download patches to "pulls" location

modules/git/repo_compare_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,31 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
)
1414

15+
func Test_parseCompareArgs(t *testing.T) {
16+
testCases := []struct {
17+
compareString string
18+
expected []string
19+
}{
20+
{
21+
"master..develop",
22+
[]string{"master", "develop"},
23+
},
24+
{
25+
"master HEAD",
26+
[]string{"master", "HEAD"},
27+
},
28+
{
29+
"HEAD...develop",
30+
[]string{"HEAD...develop"},
31+
},
32+
}
33+
34+
for _, tc := range testCases {
35+
args := parseCompareArgs(tc.compareString)
36+
assert.Equal(t, tc.expected, args)
37+
}
38+
}
39+
1540
func TestGetFormatPatch(t *testing.T) {
1641
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
1742
clonedPath, err := cloneRepo(t, bareRepo1Path)
@@ -28,7 +53,7 @@ func TestGetFormatPatch(t *testing.T) {
2853
defer repo.Close()
2954

3055
rd := &bytes.Buffer{}
31-
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
56+
err = repo.GetPatch("8d92fc95^...8d92fc95", rd)
3257
if err != nil {
3358
assert.NoError(t, err)
3459
return

services/pull/patch.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,19 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io
4242
}
4343
defer closer.Close()
4444

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

358-
if err := gitRepo.GetDiffBinary(pr.MergeBase, "tracking", tmpPatchFile); err != nil {
368+
if err := gitRepo.GetDiffBinary(pr.MergeBase+"...tracking", tmpPatchFile); err != nil {
359369
tmpPatchFile.Close()
360370
log.Error("Unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
361371
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)