Skip to content

Commit 34153fd

Browse files
committed
add context to GetCommitByPath
CommitsCountBetween is dead code
1 parent 0099c82 commit 34153fd

File tree

8 files changed

+21
-19
lines changed

8 files changed

+21
-19
lines changed

modules/git/commit.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ func (c *Commit) ParentCount() int {
7777
}
7878

7979
// GetCommitByPath return the commit of relative path object.
80-
func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
80+
func (c *Commit) GetCommitByPath(ctx context.Context, relpath string) (*Commit, error) {
8181
if c.repo.LastCommitCache != nil {
82-
return c.repo.LastCommitCache.GetCommitByPath(c.ID.String(), relpath)
82+
return c.repo.LastCommitCache.GetCommitByPath(ctx, c.ID.String(), relpath)
8383
}
84-
return c.repo.getCommitByPathWithID(c.ID, relpath)
84+
return c.repo.getCommitByPathWithID(ctx, c.ID, relpath)
8585
}
8686

8787
// AddChanges marks local changes to be ready for commit.

modules/git/last_commit_cache.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package git
55

66
import (
7+
"context"
78
"crypto/sha256"
89
"fmt"
910

@@ -83,7 +84,7 @@ func (c *LastCommitCache) Get(ref, entryPath string) (*Commit, error) {
8384
}
8485

8586
// GetCommitByPath gets the last commit for the entry in the provided commit
86-
func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit, error) {
87+
func (c *LastCommitCache) GetCommitByPath(ctx context.Context, commitID, entryPath string) (*Commit, error) {
8788
sha, err := NewIDFromString(commitID)
8889
if err != nil {
8990
return nil, err
@@ -94,7 +95,7 @@ func (c *LastCommitCache) GetCommitByPath(commitID, entryPath string) (*Commit,
9495
return lastCommit, err
9596
}
9697

97-
lastCommit, err = c.repo.getCommitByPathWithID(sha, entryPath)
98+
lastCommit, err = c.repo.getCommitByPathWithID(ctx, sha, entryPath)
9899
if err != nil {
99100
return nil, err
100101
}

modules/git/repo_commit.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package git
66

77
import (
88
"bytes"
9+
"context"
910
"io"
1011
"os"
1112
"strconv"
@@ -53,13 +54,13 @@ func (repo *Repository) GetTagCommit(name string) (*Commit, error) {
5354
return repo.GetCommit(commitID)
5455
}
5556

56-
func (repo *Repository) getCommitByPathWithID(id ObjectID, relpath string) (*Commit, error) {
57+
func (repo *Repository) getCommitByPathWithID(ctx context.Context, id ObjectID, relpath string) (*Commit, error) {
5758
// File name starts with ':' must be escaped.
5859
if relpath[0] == ':' {
5960
relpath = `\` + relpath
6061
}
6162

62-
stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(repo.Ctx, &RunOpts{Dir: repo.Path})
63+
stdout, _, runErr := NewCommand("log", "-1", prettyLogFormat).AddDynamicArguments(id.String()).AddDashesAndList(relpath).RunStdString(ctx, &RunOpts{Dir: repo.Path})
6364
if runErr != nil {
6465
return nil, runErr
6566
}
@@ -375,16 +376,16 @@ func (repo *Repository) CommitsBetweenIDs(last, before string) ([]*Commit, error
375376
}
376377

377378
// CommitsCountBetween return numbers of commits between two commits
378-
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
379-
count, err := CommitsCount(repo.Ctx, CommitsCountOptions{
379+
func (repo *Repository) CommitsCountBetween(ctx context.Context, start, end string) (int64, error) {
380+
count, err := CommitsCount(ctx, CommitsCountOptions{
380381
RepoPath: repo.Path,
381382
Revision: []string{start + ".." + end},
382383
})
383384

384385
if err != nil && strings.Contains(err.Error(), "no merge base") {
385386
// future versions of git >= 2.28 are likely to return an error if before and last have become unrelated.
386387
// previously it would return the results of git rev-list before last so let's try that...
387-
return CommitsCount(repo.Ctx, CommitsCountOptions{
388+
return CommitsCount(ctx, CommitsCountOptions{
388389
RepoPath: repo.Path,
389390
Revision: []string{start, end},
390391
})

routers/web/repo/view_file.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
4646
ctx.Data["FileName"] = blob.Name()
4747
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
4848

49-
commit, err := ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
49+
commit, err := ctx.Repo.Commit.GetCommitByPath(ctx, ctx.Repo.TreePath)
5050
if err != nil {
5151
ctx.ServerError("GetCommitByPath", err)
5252
return

services/repository/files/content.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
170170
return nil, err
171171
}
172172

173-
lastCommit, err := commit.GetCommitByPath(treePath)
173+
lastCommit, err := commit.GetCommitByPath(ctx, treePath)
174174
if err != nil {
175175
return nil, err
176176
}

tests/integration/api_repo_get_contents_list_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
120120
assert.NotNil(t, contentsListResponse)
121121
branchCommit, err := gitRepo.GetBranchCommit(ref)
122122
assert.NoError(t, err)
123-
lastCommit, err = branchCommit.GetCommitByPath("README.md")
123+
lastCommit, err = branchCommit.GetCommitByPath(t.Context(), "README.md")
124124
assert.NoError(t, err)
125125
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
126126
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)
@@ -134,7 +134,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
134134
assert.NotNil(t, contentsListResponse)
135135
tagCommit, err := gitRepo.GetTagCommit(ref)
136136
assert.NoError(t, err)
137-
lastCommit, err = tagCommit.GetCommitByPath("README.md")
137+
lastCommit, err = tagCommit.GetCommitByPath(t.Context(), "README.md")
138138
assert.NoError(t, err)
139139
expectedContentsListResponse = getExpectedContentsListResponseForContents(ref, refType, lastCommit.ID.String())
140140
assert.EqualValues(t, expectedContentsListResponse, contentsListResponse)

tests/integration/api_repo_get_contents_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
121121
DecodeJSON(t, resp, &contentsResponse)
122122
assert.NotNil(t, contentsResponse)
123123
branchCommit, _ := gitRepo.GetBranchCommit(ref)
124-
lastCommit, _ = branchCommit.GetCommitByPath("README.md")
124+
lastCommit, _ = branchCommit.GetCommitByPath(t.Context(), "README.md")
125125
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
126126
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
127127

@@ -133,7 +133,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
133133
DecodeJSON(t, resp, &contentsResponse)
134134
assert.NotNil(t, contentsResponse)
135135
tagCommit, _ := gitRepo.GetTagCommit(ref)
136-
lastCommit, _ = tagCommit.GetCommitByPath("README.md")
136+
lastCommit, _ = tagCommit.GetCommitByPath(t.Context(), "README.md")
137137
expectedContentsResponse = getExpectedContentsResponseForContents(ref, refType, lastCommit.ID.String())
138138
assert.EqualValues(t, *expectedContentsResponse, contentsResponse)
139139

tests/integration/repofiles_change_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ func TestChangeRepoFilesForUpdate(t *testing.T) {
304304
defer gitRepo.Close()
305305

306306
commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
307-
lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
307+
lastCommit, _ := commit.GetCommitByPath(t.Context(), opts.Files[0].TreePath)
308308
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
309309
assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0])
310310
assert.EqualValues(t, expectedFileResponse.Commit.SHA, filesResponse.Commit.SHA)
@@ -340,7 +340,7 @@ func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) {
340340
defer gitRepo.Close()
341341

342342
commit, _ := gitRepo.GetBranchCommit(opts.NewBranch)
343-
lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
343+
lastCommit, _ := commit.GetCommitByPath(t.Context(), opts.Files[0].TreePath)
344344
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
345345
// assert that the old file no longer exists in the last commit of the branch
346346
fromEntry, err := commit.GetTreeEntryByPath(opts.Files[0].FromTreePath)
@@ -391,7 +391,7 @@ func TestChangeRepoFilesWithoutBranchNames(t *testing.T) {
391391
defer gitRepo.Close()
392392

393393
commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch)
394-
lastCommit, _ := commit.GetCommitByPath(opts.Files[0].TreePath)
394+
lastCommit, _ := commit.GetCommitByPath(git.DefaultContext, opts.Files[0].TreePath)
395395
expectedFileResponse := getExpectedFileResponseForRepofilesUpdate(commit.ID.String(), opts.Files[0].TreePath, lastCommit.ID.String())
396396
assert.EqualValues(t, expectedFileResponse.Content, filesResponse.Files[0])
397397
})

0 commit comments

Comments
 (0)