Skip to content

Commit d2c85c4

Browse files
committed
add context to CommitsByFileAndRange and FileChangedBetweenCommits
1 parent ea27930 commit d2c85c4

File tree

9 files changed

+16
-16
lines changed

9 files changed

+16
-16
lines changed

modules/git/commit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error)
313313

314314
// FileChangedSinceCommit Returns true if the file given has changed since the past commit
315315
// YOU MUST ENSURE THAT pastCommit is a valid commit ID.
316-
func (c *Commit) FileChangedSinceCommit(filename, pastCommit string) (bool, error) {
317-
return c.repo.FileChangedBetweenCommits(filename, pastCommit, c.ID.String())
316+
func (c *Commit) FileChangedSinceCommit(ctx context.Context, filename, pastCommit string) (bool, error) {
317+
return c.repo.FileChangedBetweenCommits(ctx, filename, pastCommit, c.ID.String())
318318
}
319319

320320
// HasFile returns true if the file given exists on this commit

modules/git/repo_commit.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ func (repo *Repository) searchCommits(ctx context.Context, id ObjectID, opts Sea
190190

191191
// FileChangedBetweenCommits Returns true if the file changed between commit IDs id1 and id2
192192
// You must ensure that id1 and id2 are valid commit ids.
193-
func (repo *Repository) FileChangedBetweenCommits(filename, id1, id2 string) (bool, error) {
194-
stdout, _, err := NewCommand("diff", "--name-only", "-z").AddDynamicArguments(id1, id2).AddDashesAndList(filename).RunStdBytes(repo.Ctx, &RunOpts{Dir: repo.Path})
193+
func (repo *Repository) FileChangedBetweenCommits(ctx context.Context, filename, id1, id2 string) (bool, error) {
194+
stdout, _, err := NewCommand("diff", "--name-only", "-z").AddDynamicArguments(id1, id2).AddDashesAndList(filename).RunStdBytes(ctx, &RunOpts{Dir: repo.Path})
195195
if err != nil {
196196
return false, err
197197
}
@@ -216,7 +216,7 @@ type CommitsByFileAndRangeOptions struct {
216216
}
217217

218218
// CommitsByFileAndRange return the commits according revision file and the page
219-
func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions) ([]*Commit, error) {
219+
func (repo *Repository) CommitsByFileAndRange(ctx context.Context, opts CommitsByFileAndRangeOptions) ([]*Commit, error) {
220220
stdoutReader, stdoutWriter := io.Pipe()
221221
defer func() {
222222
_ = stdoutReader.Close()
@@ -234,7 +234,7 @@ func (repo *Repository) CommitsByFileAndRange(opts CommitsByFileAndRangeOptions)
234234
}
235235

236236
gitCmd.AddDashesAndList(opts.File)
237-
err := gitCmd.Run(repo.Ctx, &RunOpts{
237+
err := gitCmd.Run(ctx, &RunOpts{
238238
Dir: repo.Path,
239239
Stdout: stdoutWriter,
240240
Stderr: &stderr,

modules/git/repo_commit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ func TestCommitsByFileAndRange(t *testing.T) {
140140
defer bareRepo1.Close()
141141

142142
// "foo" has 3 commits in "master" branch
143-
commits, err := bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1})
143+
commits, err := bareRepo1.CommitsByFileAndRange(t.Context(), CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 1})
144144
require.NoError(t, err)
145145
assert.Len(t, commits, 2)
146146

147-
commits, err = bareRepo1.CommitsByFileAndRange(CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2})
147+
commits, err = bareRepo1.CommitsByFileAndRange(t.Context(), CommitsByFileAndRangeOptions{Revision: "master", File: "foo", Page: 2})
148148
require.NoError(t, err)
149149
assert.Len(t, commits, 1)
150150
}

routers/api/v1/repo/commits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func GetAllCommits(ctx *context.APIContext) {
238238
return
239239
}
240240

241-
commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(
241+
commits, err = ctx.Repo.GitRepo.CommitsByFileAndRange(ctx,
242242
git.CommitsByFileAndRangeOptions{
243243
Revision: sha,
244244
File: path,

routers/api/v1/repo/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func ListPageRevisions(ctx *context.APIContext) {
440440
}
441441

442442
// get Commit Count
443-
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
443+
commitsHistory, err := wikiRepo.CommitsByFileAndRange(ctx,
444444
git.CommitsByFileAndRangeOptions{
445445
Revision: "master",
446446
File: pageFilename,

routers/web/feed/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ShowFileFeed(ctx *context.Context, repo *repo.Repository, formatType string
2222
if len(fileName) == 0 {
2323
return
2424
}
25-
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
25+
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx,
2626
git.CommitsByFileAndRangeOptions{
2727
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
2828
File: fileName,

routers/web/repo/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func FileHistory(ctx *context.Context) {
235235
page = 1
236236
}
237237

238-
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(
238+
commits, err := ctx.Repo.GitRepo.CommitsByFileAndRange(ctx,
239239
git.CommitsByFileAndRangeOptions{
240240
Revision: ctx.Repo.RefFullName.ShortName(), // FIXME: legacy code used ShortName
241241
File: fileName,

routers/web/repo/wiki.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
424424
}
425425

426426
// get Commit Count
427-
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
427+
commitsHistory, err := wikiRepo.CommitsByFileAndRange(ctx,
428428
git.CommitsByFileAndRangeOptions{
429429
Revision: ctx.Repo.Repository.DefaultWikiBranch,
430430
File: pageFilename,

services/repository/files/update.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
230230
}
231231

232232
for _, file := range opts.Files {
233-
if err := handleCheckErrors(file, commit, opts); err != nil {
233+
if err := handleCheckErrors(ctx, file, commit, opts); err != nil {
234234
return nil, err
235235
}
236236
}
@@ -362,7 +362,7 @@ func (err ErrSHAOrCommitIDNotProvided) Error() string {
362362
}
363363

364364
// handles the check for various issues for ChangeRepoFiles
365-
func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRepoFilesOptions) error {
365+
func handleCheckErrors(ctx context.Context, file *ChangeRepoFile, commit *git.Commit, opts *ChangeRepoFilesOptions) error {
366366
if file.Operation == "update" || file.Operation == "delete" {
367367
fromEntry, err := commit.GetTreeEntryByPath(file.Options.fromTreePath)
368368
if err != nil {
@@ -381,7 +381,7 @@ func handleCheckErrors(file *ChangeRepoFile, commit *git.Commit, opts *ChangeRep
381381
// If a lastCommitID was given and it doesn't match the commitID of the head of the branch throw
382382
// an error, but only if we aren't creating a new branch.
383383
if commit.ID.String() != opts.LastCommitID && opts.OldBranch == opts.NewBranch {
384-
if changed, err := commit.FileChangedSinceCommit(file.Options.treePath, opts.LastCommitID); err != nil {
384+
if changed, err := commit.FileChangedSinceCommit(ctx, file.Options.treePath, opts.LastCommitID); err != nil {
385385
return err
386386
} else if changed {
387387
return ErrCommitIDDoesNotMatch{

0 commit comments

Comments
 (0)