Skip to content

Commit 1611740

Browse files
committed
add context to IsCommitInBranch and CommitsBetweenIDs
1 parent 30767c3 commit 1611740

File tree

8 files changed

+20
-20
lines changed

8 files changed

+20
-20
lines changed

modules/git/commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,12 @@ func (c *Commit) CommitsBeforeLimit(ctx context.Context, num int) ([]*Commit, er
254254
}
255255

256256
// CommitsBeforeUntil returns the commits between commitID to current revision
257-
func (c *Commit) CommitsBeforeUntil(commitID string) ([]*Commit, error) {
257+
func (c *Commit) CommitsBeforeUntil(ctx context.Context, commitID string) ([]*Commit, error) {
258258
endCommit, err := c.repo.GetCommit(commitID)
259259
if err != nil {
260260
return nil, err
261261
}
262-
return c.repo.CommitsBetween(c, endCommit)
262+
return c.repo.CommitsBetween(ctx, c, endCommit)
263263
}
264264

265265
// SearchCommitsOptions specify the parameters for SearchCommits

modules/git/repo_commit.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,17 @@ func (repo *Repository) FilesCountBetween(ctx context.Context, startCommitID, en
290290

291291
// CommitsBetween returns a list that contains commits between [before, last).
292292
// If before is detached (removed by reset + push) it is not included.
293-
func (repo *Repository) CommitsBetween(last, before *Commit) ([]*Commit, error) {
293+
func (repo *Repository) CommitsBetween(ctx context.Context, last, before *Commit) ([]*Commit, error) {
294294
var stdout []byte
295295
var err error
296296
if before == nil {
297-
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
297+
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path})
298298
} else {
299-
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
299+
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String()+".."+last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path})
300300
if err != nil && strings.Contains(err.Error(), "no merge base") {
301301
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
302302
// previously it would return the results of git rev-list before last so let's try that...
303-
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
303+
stdout, _, err = NewCommand("rev-list").AddDynamicArguments(before.ID.String(), last.ID.String()).RunStdBytes(ctx, &RunOpts{Dir: repo.Path})
304304
}
305305
}
306306
if err != nil {
@@ -359,20 +359,20 @@ func (repo *Repository) CommitsBetweenNotBase(ctx context.Context, last, before
359359
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
360360
}
361361

362-
// CommitsBetweenIDs return commits between twoe commits
363-
func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error) {
362+
// CommitsBetweenIDs return commits between two commits
363+
func (repo *Repository) CommitsBetweenIDs(ctx context.Context, last, before string) ([]*Commit, error) {
364364
lastCommit, err := repo.GetCommit(last)
365365
if err != nil {
366366
return nil, err
367367
}
368368
if before == "" {
369-
return repo.CommitsBetween(lastCommit, nil)
369+
return repo.CommitsBetween(ctx, lastCommit, nil)
370370
}
371371
beforeCommit, err := repo.GetCommit(before)
372372
if err != nil {
373373
return nil, err
374374
}
375-
return repo.CommitsBetween(lastCommit, beforeCommit)
375+
return repo.CommitsBetween(ctx, lastCommit, beforeCommit)
376376
}
377377

378378
// CommitsCountBetween return numbers of commits between two commits
@@ -495,8 +495,8 @@ func (repo *Repository) GetCommitsFromIDs(commitIDs []string) []*Commit {
495495
}
496496

497497
// IsCommitInBranch check if the commit is on the branch
498-
func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err error) {
499-
stdout, _, err := NewCommand("branch", "--contains").AddDynamicArguments(commitID, branch).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
498+
func (repo *Repository) IsCommitInBranch(ctx context.Context, commitID, branch string) (r bool, err error) {
499+
stdout, _, err := NewCommand("branch", "--contains").AddDynamicArguments(commitID, branch).RunStdString(ctx, &RunOpts{Dir: repo.Path})
500500
if err != nil {
501501
return false, err
502502
}

modules/git/repo_commit_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ func TestIsCommitInBranch(t *testing.T) {
7575
assert.NoError(t, err)
7676
defer bareRepo1.Close()
7777

78-
result, err := bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1")
78+
result, err := bareRepo1.IsCommitInBranch(t.Context(), "2839944139e0de9737a044f78b0e4b40d989a9e3", "branch1")
7979
assert.NoError(t, err)
8080
assert.True(t, result)
8181

82-
result, err = bareRepo1.IsCommitInBranch("2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2")
82+
result, err = bareRepo1.IsCommitInBranch(t.Context(), "2839944139e0de9737a044f78b0e4b40d989a9e3", "branch2")
8383
assert.NoError(t, err)
8484
assert.False(t, result)
8585
}
@@ -100,7 +100,7 @@ func TestRepository_CommitsBetweenIDs(t *testing.T) {
100100
{"78a445db1eac62fe15e624e1137965969addf344", "a78e5638b66ccfe7e1b4689d3d5684e42c97d7ca", 1}, // com2 -> com2_new
101101
}
102102
for i, c := range cases {
103-
commits, err := bareRepo1.CommitsBetweenIDs(c.NewID, c.OldID)
103+
commits, err := bareRepo1.CommitsBetweenIDs(t.Context(), c.NewID, c.OldID)
104104
assert.NoError(t, err)
105105
assert.Len(t, commits, c.ExpectedCommits, "case %d", i)
106106
}

services/asymkey/sign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Loop:
388388
if err != nil {
389389
return false, "", nil, err
390390
}
391-
commitList, err := commit.CommitsBeforeUntil(mergeBaseCommit)
391+
commitList, err := commit.CommitsBeforeUntil(ctx, mergeBaseCommit)
392392
if err != nil {
393393
return false, "", nil, err
394394
}

services/mirror/mirror_pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
530530
log.Error("SyncMirrors [repo: %-v]: unable to get GetFullCommitID [%s]: %v", m.Repo, result.newCommitID, err)
531531
continue
532532
}
533-
commits, err := gitRepo.CommitsBetweenIDs(newCommitID, oldCommitID)
533+
commits, err := gitRepo.CommitsBetweenIDs(ctx, newCommitID, oldCommitID)
534534
if err != nil {
535535
log.Error("SyncMirrors [repo: %-v]: unable to get CommitsBetweenIDs [new_commit_id: %s, old_commit_id: %s]: %v", m.Repo, newCommitID, oldCommitID, err)
536536
continue

services/pull/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ func MergedManually(ctx context.Context, pr *issues_model.PullRequest, doer *use
633633
}
634634
commitID = commit.ID.String()
635635

636-
ok, err := baseGitRepo.IsCommitInBranch(commitID, pr.BaseBranch)
636+
ok, err := baseGitRepo.IsCommitInBranch(ctx, commitID, pr.BaseBranch)
637637
if err != nil {
638638
return err
639639
}

services/pull/merge_squash.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) {
3232
}
3333
defer gitRepo.Close()
3434

35-
commits, err := gitRepo.CommitsBetweenIDs(trackingBranch, "HEAD")
35+
commits, err := gitRepo.CommitsBetweenIDs(ctx, trackingBranch, "HEAD")
3636
if err != nil {
3737
log.Error("%-v Unable to get commits between: %s %s: %v", ctx.pr, "HEAD", trackingBranch, err)
3838
return nil, err

services/repository/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
201201
}
202202
notify_service.CreateRef(ctx, pusher, repo, opts.RefFullName, opts.NewCommitID)
203203
} else {
204-
l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
204+
l, err = newCommit.CommitsBeforeUntil(ctx, opts.OldCommitID)
205205
if err != nil {
206206
return fmt.Errorf("newCommit.CommitsBeforeUntil: %w", err)
207207
}

0 commit comments

Comments
 (0)