Skip to content

Commit 162899b

Browse files
committed
add context to GetPatch
modernize test
1 parent 3631512 commit 162899b

File tree

3 files changed

+8
-16
lines changed

3 files changed

+8
-16
lines changed

modules/git/repo_compare.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ func (repo *Repository) GetDiffBinary(ctx context.Context, compareArg string, w
245245
}
246246

247247
// GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
248-
func (repo *Repository) GetPatch(compareArg string, w io.Writer) error {
248+
func (repo *Repository) GetPatch(ctx context.Context, compareArg string, w io.Writer) error {
249249
stderr := new(bytes.Buffer)
250250
return NewCommand("format-patch", "--binary", "--stdout").AddDynamicArguments(compareArg).
251-
Run(repo.Ctx, &RunOpts{
251+
Run(ctx, &RunOpts{
252252
Dir: repo.Path,
253253
Stdout: w,
254254
Stderr: stderr,

modules/git/repo_compare_test.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"testing"
1111

1212
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
1314
)
1415

1516
func TestGetFormatPatch(t *testing.T) {
@@ -21,24 +22,15 @@ func TestGetFormatPatch(t *testing.T) {
2122
}
2223

2324
repo, err := openRepositoryWithDefaultContext(clonedPath)
24-
if err != nil {
25-
assert.NoError(t, err)
26-
return
27-
}
25+
require.NoError(t, err)
2826
defer repo.Close()
2927

3028
rd := &bytes.Buffer{}
31-
err = repo.GetPatch("8d92fc95^...8d92fc95", rd)
32-
if err != nil {
33-
assert.NoError(t, err)
34-
return
35-
}
29+
err = repo.GetPatch(t.Context(), "8d92fc95^...8d92fc95", rd)
30+
require.NoError(t, err)
3631

3732
patchb, err := io.ReadAll(rd)
38-
if err != nil {
39-
assert.NoError(t, err)
40-
return
41-
}
33+
require.NoError(t, err)
4234

4335
patch := string(patchb)
4436
assert.Regexp(t, "^From 8d92fc95", patch)

services/pull/patch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io
4444
compareArg := pr.MergeBase + "..." + pr.GetGitRefName()
4545
switch {
4646
case patch:
47-
err = gitRepo.GetPatch(compareArg, w)
47+
err = gitRepo.GetPatch(ctx, compareArg, w)
4848
case binary:
4949
err = gitRepo.GetDiffBinary(ctx, compareArg, w)
5050
default:

0 commit comments

Comments
 (0)