diff --git a/internal/batches/sources/mocks_test.go b/internal/batches/sources/mocks_test.go index 7ec80489b380..4ba2dd4f0f26 100644 --- a/internal/batches/sources/mocks_test.go +++ b/internal/batches/sources/mocks_test.go @@ -10995,9 +10995,6 @@ type MockGitserverClient struct { // CheckPerforceCredentialsFunc is an instance of a mock function object // controlling the behavior of the method CheckPerforceCredentials. CheckPerforceCredentialsFunc *GitserverClientCheckPerforceCredentialsFunc - // CommitLogFunc is an instance of a mock function object controlling - // the behavior of the method CommitLog. - CommitLogFunc *GitserverClientCommitLogFunc // CommitsFunc is an instance of a mock function object controlling the // behavior of the method Commits. CommitsFunc *GitserverClientCommitsFunc @@ -11125,11 +11122,6 @@ func NewMockGitserverClient() *MockGitserverClient { return }, }, - CommitLogFunc: &GitserverClientCommitLogFunc{ - defaultHook: func(context.Context, api.RepoName, time.Time) (r0 []gitserver.CommitLog, r1 error) { - return - }, - }, CommitsFunc: &GitserverClientCommitsFunc{ defaultHook: func(context.Context, api.RepoName, gitserver.CommitsOptions) (r0 []*gitdomain.Commit, r1 error) { return @@ -11322,11 +11314,6 @@ func NewStrictMockGitserverClient() *MockGitserverClient { panic("unexpected invocation of MockGitserverClient.CheckPerforceCredentials") }, }, - CommitLogFunc: &GitserverClientCommitLogFunc{ - defaultHook: func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error) { - panic("unexpected invocation of MockGitserverClient.CommitLog") - }, - }, CommitsFunc: &GitserverClientCommitsFunc{ defaultHook: func(context.Context, api.RepoName, gitserver.CommitsOptions) ([]*gitdomain.Commit, error) { panic("unexpected invocation of MockGitserverClient.Commits") @@ -11510,9 +11497,6 @@ func NewMockGitserverClientFrom(i gitserver.Client) *MockGitserverClient { CheckPerforceCredentialsFunc: &GitserverClientCheckPerforceCredentialsFunc{ defaultHook: i.CheckPerforceCredentials, }, - CommitLogFunc: &GitserverClientCommitLogFunc{ - defaultHook: i.CommitLog, - }, CommitsFunc: &GitserverClientCommitsFunc{ defaultHook: i.Commits, }, @@ -12167,117 +12151,6 @@ func (c GitserverClientCheckPerforceCredentialsFuncCall) Results() []interface{} return []interface{}{c.Result0} } -// GitserverClientCommitLogFunc describes the behavior when the CommitLog -// method of the parent MockGitserverClient instance is invoked. -type GitserverClientCommitLogFunc struct { - defaultHook func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error) - hooks []func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error) - history []GitserverClientCommitLogFuncCall - mutex sync.Mutex -} - -// CommitLog delegates to the next hook function in the queue and stores the -// parameter and result values of this invocation. -func (m *MockGitserverClient) CommitLog(v0 context.Context, v1 api.RepoName, v2 time.Time) ([]gitserver.CommitLog, error) { - r0, r1 := m.CommitLogFunc.nextHook()(v0, v1, v2) - m.CommitLogFunc.appendCall(GitserverClientCommitLogFuncCall{v0, v1, v2, r0, r1}) - return r0, r1 -} - -// SetDefaultHook sets function that is called when the CommitLog method of -// the parent MockGitserverClient instance is invoked and the hook queue is -// empty. -func (f *GitserverClientCommitLogFunc) SetDefaultHook(hook func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error)) { - f.defaultHook = hook -} - -// PushHook adds a function to the end of hook queue. Each invocation of the -// CommitLog method of the parent MockGitserverClient instance invokes the -// hook at the front of the queue and discards it. After the queue is empty, -// the default hook function is invoked for any future action. -func (f *GitserverClientCommitLogFunc) PushHook(hook func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error)) { - f.mutex.Lock() - f.hooks = append(f.hooks, hook) - f.mutex.Unlock() -} - -// SetDefaultReturn calls SetDefaultHook with a function that returns the -// given values. -func (f *GitserverClientCommitLogFunc) SetDefaultReturn(r0 []gitserver.CommitLog, r1 error) { - f.SetDefaultHook(func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error) { - return r0, r1 - }) -} - -// PushReturn calls PushHook with a function that returns the given values. -func (f *GitserverClientCommitLogFunc) PushReturn(r0 []gitserver.CommitLog, r1 error) { - f.PushHook(func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error) { - return r0, r1 - }) -} - -func (f *GitserverClientCommitLogFunc) nextHook() func(context.Context, api.RepoName, time.Time) ([]gitserver.CommitLog, error) { - f.mutex.Lock() - defer f.mutex.Unlock() - - if len(f.hooks) == 0 { - return f.defaultHook - } - - hook := f.hooks[0] - f.hooks = f.hooks[1:] - return hook -} - -func (f *GitserverClientCommitLogFunc) appendCall(r0 GitserverClientCommitLogFuncCall) { - f.mutex.Lock() - f.history = append(f.history, r0) - f.mutex.Unlock() -} - -// History returns a sequence of GitserverClientCommitLogFuncCall objects -// describing the invocations of this function. -func (f *GitserverClientCommitLogFunc) History() []GitserverClientCommitLogFuncCall { - f.mutex.Lock() - history := make([]GitserverClientCommitLogFuncCall, len(f.history)) - copy(history, f.history) - f.mutex.Unlock() - - return history -} - -// GitserverClientCommitLogFuncCall is an object that describes an -// invocation of method CommitLog on an instance of MockGitserverClient. -type GitserverClientCommitLogFuncCall struct { - // Arg0 is the value of the 1st argument passed to this method - // invocation. - Arg0 context.Context - // Arg1 is the value of the 2nd argument passed to this method - // invocation. - Arg1 api.RepoName - // Arg2 is the value of the 3rd argument passed to this method - // invocation. - Arg2 time.Time - // Result0 is the value of the 1st result returned from this method - // invocation. - Result0 []gitserver.CommitLog - // Result1 is the value of the 2nd result returned from this method - // invocation. - Result1 error -} - -// Args returns an interface slice containing the arguments of this -// invocation. -func (c GitserverClientCommitLogFuncCall) Args() []interface{} { - return []interface{}{c.Arg0, c.Arg1, c.Arg2} -} - -// Results returns an interface slice containing the results of this -// invocation. -func (c GitserverClientCommitLogFuncCall) Results() []interface{} { - return []interface{}{c.Result0, c.Result1} -} - // GitserverClientCommitsFunc describes the behavior when the Commits method // of the parent MockGitserverClient instance is invoked. type GitserverClientCommitsFunc struct { diff --git a/internal/gitserver/BUILD.bazel b/internal/gitserver/BUILD.bazel index 18a51449c34f..a6f900216623 100644 --- a/internal/gitserver/BUILD.bazel +++ b/internal/gitserver/BUILD.bazel @@ -80,7 +80,6 @@ go_test( "@com_github_google_go_cmp//cmp", "@com_github_google_go_cmp//cmp/cmpopts", "@com_github_sourcegraph_go_diff//diff", - "@com_github_stretchr_testify//assert", "@com_github_stretchr_testify//require", "@org_golang_google_grpc//:go_default_library", "@org_golang_google_grpc//codes", diff --git a/internal/gitserver/client.go b/internal/gitserver/client.go index b8cccb2dba8d..c0e6621c602e 100644 --- a/internal/gitserver/client.go +++ b/internal/gitserver/client.go @@ -253,14 +253,6 @@ func (o *BlameRange) Attrs() []attribute.KeyValue { } } -type CommitLog struct { - AuthorEmail string - AuthorName string - Timestamp time.Time - SHA string - ChangedFiles []string -} - // ListRefsOpts are additional options passed to ListRefs. type ListRefsOpts struct { // If true, only heads are returned. Can be combined with TagsOnly. @@ -459,11 +451,6 @@ type Client interface { // longer required. Diff(ctx context.Context, repo api.RepoName, opts DiffOptions) (*DiffFileIterator, error) - // CommitLog returns the repository commit log, including the file paths that were changed. The general approach to parsing - // is to separate the first line (the metadata line) from the remaining lines (the files), and then parse the metadata line - // into component parts separately. - CommitLog(ctx context.Context, repo api.RepoName, after time.Time) ([]CommitLog, error) - // GetCommit returns the commit with the given commit ID, or RevisionNotFoundError if no such commit // exists. GetCommit(ctx context.Context, repo api.RepoName, id api.CommitID) (*gitdomain.Commit, error) diff --git a/internal/gitserver/commands.go b/internal/gitserver/commands.go index c036db696fe5..1dd115962384 100644 --- a/internal/gitserver/commands.go +++ b/internal/gitserver/commands.go @@ -221,75 +221,6 @@ func checkSpecArgSafety(spec string) error { return nil } -// CommitLog returns the repository commit log, including the file paths that were changed. The general approach to parsing -// is to separate the first line (the metadata line) from the remaining lines (the files), and then parse the metadata line -// into component parts separately. -func (c *clientImplementor) CommitLog(ctx context.Context, repo api.RepoName, after time.Time) (_ []CommitLog, err error) { - ctx, _, endObservation := c.operations.commitLog.With(ctx, &err, observation.Args{ - MetricLabelValues: []string{c.scope}, - Attrs: []attribute.KeyValue{ - repo.Attr(), - attribute.Stringer("after", after), - }, - }) - defer endObservation(1, observation.Args{}) - - args := []string{"log", "--pretty=format:%H%ae%an%ad", "--name-only", "--topo-order", "--no-merges"} - if !after.IsZero() { - args = append(args, fmt.Sprintf("--after=%s", after.Format(time.RFC3339))) - } - - cmd := c.gitCommand(repo, args...) - out, err := cmd.CombinedOutput(ctx) - if err != nil { - return nil, errors.Wrapf(err, "gitCommand %s", string(out)) - } - - var ls []CommitLog - lines := strings.Split(string(out), "\n\n") - - for _, logOutput := range lines { - partitions := strings.Split(logOutput, "\n") - if len(partitions) < 2 { - continue - } - metaLine := partitions[0] - var changedFiles []string - for _, pt := range partitions[1:] { - if pt != "" { - changedFiles = append(changedFiles, pt) - } - } - - parts := strings.Split(metaLine, "") - if len(parts) != 4 { - continue - } - sha, authorEmail, authorName, timestamp := parts[0], parts[1], parts[2], parts[3] - t, err := parseTimestamp(timestamp) - if err != nil { - return nil, errors.Wrapf(err, "parseTimestamp %s", timestamp) - } - ls = append(ls, CommitLog{ - SHA: sha, - AuthorEmail: authorEmail, - AuthorName: authorName, - Timestamp: t, - ChangedFiles: changedFiles, - }) - } - return ls, nil -} - -func parseTimestamp(timestamp string) (time.Time, error) { - layout := "Mon Jan 2 15:04:05 2006 -0700" - t, err := time.Parse(layout, timestamp) - if err != nil { - return time.Time{}, err - } - return t, nil -} - // DevNullSHA 4b825dc642cb6eb9a060e54bf8d69288fbee4904 is `git hash-object -t // tree /dev/null`, which is used as the base when computing the `git diff` of // the root commit. diff --git a/internal/gitserver/commands_test.go b/internal/gitserver/commands_test.go index f42c3e490651..4c945a5517c4 100644 --- a/internal/gitserver/commands_test.go +++ b/internal/gitserver/commands_test.go @@ -12,7 +12,6 @@ import ( "time" "github.com/google/go-cmp/cmp/cmpopts" - "github.com/stretchr/testify/assert" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -1253,57 +1252,6 @@ func TestClient_GetCommit(t *testing.T) { }) } -func Test_CommitLog(t *testing.T) { - ClientMocks.LocalGitserver = true - defer ResetClientMocks() - - tests := map[string]struct { - extraGitCommands []string - wantFiles [][]string // put these in log reverse order - wantCommits int - wantErr string - }{ - "commit changes files": { - extraGitCommands: getGitCommandsWithFileLists([]string{"file1.txt", "file2.txt"}, []string{"file3.txt"}), - wantFiles: [][]string{{"file3.txt"}, {"file1.txt", "file2.txt"}}, - wantCommits: 2, - }, - "no commits": { - wantErr: "gitCommand fatal: your current branch 'master' does not have any commits yet: exit status 128", - }, - "one file two commits": { - extraGitCommands: getGitCommandsWithFileLists([]string{"file1.txt"}, []string{"file1.txt"}), - wantFiles: [][]string{{"file1.txt"}, {"file1.txt"}}, - wantCommits: 2, - }, - "one commit": { - extraGitCommands: getGitCommandsWithFileLists([]string{"file1.txt"}), - wantFiles: [][]string{{"file1.txt"}}, - wantCommits: 1, - }, - } - - for label, test := range tests { - t.Run(label, func(t *testing.T) { - repo := MakeGitRepository(t, test.extraGitCommands...) - logResults, err := NewClient("test").CommitLog(context.Background(), repo, time.Time{}) - if err != nil { - require.ErrorContains(t, err, test.wantErr) - } - - t.Log(test) - for i, result := range logResults { - t.Log(result) - assert.Equal(t, "a@a.com", result.AuthorEmail) - assert.Equal(t, "a", result.AuthorName) - assert.Equal(t, 40, len(result.SHA)) - assert.ElementsMatch(t, test.wantFiles[i], result.ChangedFiles) - } - assert.Equal(t, test.wantCommits, len(logResults)) - }) - } -} - func TestClient_ArchiveReader(t *testing.T) { t.Run("firstChunk memoization", func(t *testing.T) { source := NewTestClientSource(t, []string{"gitserver"}, func(o *TestClientSourceOptions) { diff --git a/internal/gitserver/mocks_temp.go b/internal/gitserver/mocks_temp.go index a6d1cb440e40..4478e345a5dc 100644 --- a/internal/gitserver/mocks_temp.go +++ b/internal/gitserver/mocks_temp.go @@ -39,9 +39,6 @@ type MockClient struct { // CheckPerforceCredentialsFunc is an instance of a mock function object // controlling the behavior of the method CheckPerforceCredentials. CheckPerforceCredentialsFunc *ClientCheckPerforceCredentialsFunc - // CommitLogFunc is an instance of a mock function object controlling - // the behavior of the method CommitLog. - CommitLogFunc *ClientCommitLogFunc // CommitsFunc is an instance of a mock function object controlling the // behavior of the method Commits. CommitsFunc *ClientCommitsFunc @@ -169,11 +166,6 @@ func NewMockClient() *MockClient { return }, }, - CommitLogFunc: &ClientCommitLogFunc{ - defaultHook: func(context.Context, api.RepoName, time.Time) (r0 []CommitLog, r1 error) { - return - }, - }, CommitsFunc: &ClientCommitsFunc{ defaultHook: func(context.Context, api.RepoName, CommitsOptions) (r0 []*gitdomain.Commit, r1 error) { return @@ -366,11 +358,6 @@ func NewStrictMockClient() *MockClient { panic("unexpected invocation of MockClient.CheckPerforceCredentials") }, }, - CommitLogFunc: &ClientCommitLogFunc{ - defaultHook: func(context.Context, api.RepoName, time.Time) ([]CommitLog, error) { - panic("unexpected invocation of MockClient.CommitLog") - }, - }, CommitsFunc: &ClientCommitsFunc{ defaultHook: func(context.Context, api.RepoName, CommitsOptions) ([]*gitdomain.Commit, error) { panic("unexpected invocation of MockClient.Commits") @@ -553,9 +540,6 @@ func NewMockClientFrom(i Client) *MockClient { CheckPerforceCredentialsFunc: &ClientCheckPerforceCredentialsFunc{ defaultHook: i.CheckPerforceCredentials, }, - CommitLogFunc: &ClientCommitLogFunc{ - defaultHook: i.CommitLog, - }, CommitsFunc: &ClientCommitsFunc{ defaultHook: i.Commits, }, @@ -1202,116 +1186,6 @@ func (c ClientCheckPerforceCredentialsFuncCall) Results() []interface{} { return []interface{}{c.Result0} } -// ClientCommitLogFunc describes the behavior when the CommitLog method of -// the parent MockClient instance is invoked. -type ClientCommitLogFunc struct { - defaultHook func(context.Context, api.RepoName, time.Time) ([]CommitLog, error) - hooks []func(context.Context, api.RepoName, time.Time) ([]CommitLog, error) - history []ClientCommitLogFuncCall - mutex sync.Mutex -} - -// CommitLog delegates to the next hook function in the queue and stores the -// parameter and result values of this invocation. -func (m *MockClient) CommitLog(v0 context.Context, v1 api.RepoName, v2 time.Time) ([]CommitLog, error) { - r0, r1 := m.CommitLogFunc.nextHook()(v0, v1, v2) - m.CommitLogFunc.appendCall(ClientCommitLogFuncCall{v0, v1, v2, r0, r1}) - return r0, r1 -} - -// SetDefaultHook sets function that is called when the CommitLog method of -// the parent MockClient instance is invoked and the hook queue is empty. -func (f *ClientCommitLogFunc) SetDefaultHook(hook func(context.Context, api.RepoName, time.Time) ([]CommitLog, error)) { - f.defaultHook = hook -} - -// PushHook adds a function to the end of hook queue. Each invocation of the -// CommitLog method of the parent MockClient instance invokes the hook at -// the front of the queue and discards it. After the queue is empty, the -// default hook function is invoked for any future action. -func (f *ClientCommitLogFunc) PushHook(hook func(context.Context, api.RepoName, time.Time) ([]CommitLog, error)) { - f.mutex.Lock() - f.hooks = append(f.hooks, hook) - f.mutex.Unlock() -} - -// SetDefaultReturn calls SetDefaultHook with a function that returns the -// given values. -func (f *ClientCommitLogFunc) SetDefaultReturn(r0 []CommitLog, r1 error) { - f.SetDefaultHook(func(context.Context, api.RepoName, time.Time) ([]CommitLog, error) { - return r0, r1 - }) -} - -// PushReturn calls PushHook with a function that returns the given values. -func (f *ClientCommitLogFunc) PushReturn(r0 []CommitLog, r1 error) { - f.PushHook(func(context.Context, api.RepoName, time.Time) ([]CommitLog, error) { - return r0, r1 - }) -} - -func (f *ClientCommitLogFunc) nextHook() func(context.Context, api.RepoName, time.Time) ([]CommitLog, error) { - f.mutex.Lock() - defer f.mutex.Unlock() - - if len(f.hooks) == 0 { - return f.defaultHook - } - - hook := f.hooks[0] - f.hooks = f.hooks[1:] - return hook -} - -func (f *ClientCommitLogFunc) appendCall(r0 ClientCommitLogFuncCall) { - f.mutex.Lock() - f.history = append(f.history, r0) - f.mutex.Unlock() -} - -// History returns a sequence of ClientCommitLogFuncCall objects describing -// the invocations of this function. -func (f *ClientCommitLogFunc) History() []ClientCommitLogFuncCall { - f.mutex.Lock() - history := make([]ClientCommitLogFuncCall, len(f.history)) - copy(history, f.history) - f.mutex.Unlock() - - return history -} - -// ClientCommitLogFuncCall is an object that describes an invocation of -// method CommitLog on an instance of MockClient. -type ClientCommitLogFuncCall struct { - // Arg0 is the value of the 1st argument passed to this method - // invocation. - Arg0 context.Context - // Arg1 is the value of the 2nd argument passed to this method - // invocation. - Arg1 api.RepoName - // Arg2 is the value of the 3rd argument passed to this method - // invocation. - Arg2 time.Time - // Result0 is the value of the 1st result returned from this method - // invocation. - Result0 []CommitLog - // Result1 is the value of the 2nd result returned from this method - // invocation. - Result1 error -} - -// Args returns an interface slice containing the arguments of this -// invocation. -func (c ClientCommitLogFuncCall) Args() []interface{} { - return []interface{}{c.Arg0, c.Arg1, c.Arg2} -} - -// Results returns an interface slice containing the results of this -// invocation. -func (c ClientCommitLogFuncCall) Results() []interface{} { - return []interface{}{c.Result0, c.Result1} -} - // ClientCommitsFunc describes the behavior when the Commits method of the // parent MockClient instance is invoked. type ClientCommitsFunc struct { diff --git a/internal/gitserver/observability.go b/internal/gitserver/observability.go index 695e8bccc511..73f2f75d0704 100644 --- a/internal/gitserver/observability.go +++ b/internal/gitserver/observability.go @@ -48,7 +48,6 @@ type operations struct { getDefaultBranch *observation.Operation logReverseEach *observation.Operation diffSymbols *observation.Operation - commitLog *observation.Operation diff *observation.Operation changedFiles *observation.Operation } @@ -134,7 +133,6 @@ func newOperations(observationCtx *observation.Context) *operations { getDefaultBranch: op("GetDefaultBranch"), logReverseEach: op("LogReverseEach"), diffSymbols: op("DiffSymbols"), - commitLog: op("CommitLog"), diff: op("Diff"), changedFiles: op("ChangedFiles"), } diff --git a/internal/own/background/BUILD.bazel b/internal/own/background/BUILD.bazel index 8bf35a7b883c..f47ba6eff461 100644 --- a/internal/own/background/BUILD.bazel +++ b/internal/own/background/BUILD.bazel @@ -69,6 +69,7 @@ go_test( "//internal/database/dbtest", "//internal/fileutil", "//internal/gitserver", + "//internal/gitserver/gitdomain", "//internal/observation", "//internal/own/types", "//internal/rcache", diff --git a/internal/own/background/recent_contributors.go b/internal/own/background/recent_contributors.go index 25cb8b30660f..847cebf7bafb 100644 --- a/internal/own/background/recent_contributors.go +++ b/internal/own/background/recent_contributors.go @@ -2,6 +2,7 @@ package background import ( "context" + "io" "time" "github.com/prometheus/client_golang/prometheus" @@ -57,9 +58,13 @@ func (r *recentContributorsIndexer) indexRepo(ctx context.Context, repoId api.Re if err != nil { return errors.Wrap(err, "repoStore.Get") } - commitLog, err := r.client.CommitLog(ctx, repo.Name, time.Now().AddDate(0, 0, -90)) + commits, err := r.client.Commits(ctx, repo.Name, gitserver.CommitsOptions{ + Order: gitserver.CommitsOrderTopoDate, + After: time.Now().AddDate(0, 0, -90).Format(time.RFC3339), + Range: "HEAD", + }) if err != nil { - return errors.Wrap(err, "CommitLog") + return errors.Wrap(err, "Commits") } store := r.db.RecentContributionSignals() @@ -68,20 +73,40 @@ func (r *recentContributorsIndexer) indexRepo(ctx context.Context, repoId api.Re return errors.Wrap(err, "ClearSignals") } - for _, commit := range commitLog { - err := store.AddCommit(ctx, database.Commit{ + inserted := 0 + for _, commit := range commits { + if len(commit.Parents) > 1 { // We don't care about merge commits. + continue + } + it, err := r.client.ChangedFiles(ctx, repo.Name, "", string(commit.ID)) + if err != nil { + return errors.Wrap(err, "ChangedFiles") + } + changedFiles := []string{} + for { + ps, err := it.Next() + if err != nil { + if errors.Is(err, io.EOF) { + break + } + return errors.Wrap(err, "ChangedFilesIterator") + } + changedFiles = append(changedFiles, ps.Path) + } + err = store.AddCommit(ctx, database.Commit{ RepoID: repoId, - AuthorName: commit.AuthorName, - AuthorEmail: commit.AuthorEmail, - Timestamp: commit.Timestamp, - CommitSHA: commit.SHA, - FilesChanged: commit.ChangedFiles, + AuthorName: commit.Author.Name, + AuthorEmail: commit.Author.Email, + Timestamp: commit.Author.Date, + CommitSHA: string(commit.ID), + FilesChanged: changedFiles, }) if err != nil { return errors.Wrapf(err, "AddCommit %v", commit) } + inserted++ } - r.logger.Info("commits inserted", logger.Int("count", len(commitLog)), logger.Int("repo_id", int(repoId))) - commitCounter.Add(float64(len(commitLog))) + r.logger.Info("commits inserted", logger.Int("count", inserted), logger.Int("repo_id", int(repoId))) + commitCounter.Add(float64(inserted)) return nil } diff --git a/internal/own/background/recent_contributors_test.go b/internal/own/background/recent_contributors_test.go index 62fd53f6cef9..e2f77c4577be 100644 --- a/internal/own/background/recent_contributors_test.go +++ b/internal/own/background/recent_contributors_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/require" "github.com/sourcegraph/sourcegraph/internal/conf" + "github.com/sourcegraph/sourcegraph/internal/gitserver/gitdomain" "github.com/sourcegraph/sourcegraph/internal/rcache" "github.com/sourcegraph/sourcegraph/schema" @@ -62,8 +63,7 @@ func Test_RecentContributorIndexFromGitserver(t *testing.T) { }, } - client := gitserver.NewMockClient() - client.CommitLogFunc.SetDefaultReturn(fakeCommitsToLog(commits), nil) + client := newMockGitserverClientWithFakeCommits(commits) indexer := newRecentContributorsIndexer(client, db, logger) checker := authz.NewMockSubRepoPermissionChecker() checker.EnabledFunc.SetDefaultReturn(true) @@ -208,8 +208,7 @@ func Test_RecentContributorIndexSkipsSubrepoPermsRepos(t *testing.T) { }, } - client := gitserver.NewMockClient() - client.CommitLogFunc.SetDefaultReturn(fakeCommitsToLog(commits), nil) + client := newMockGitserverClientWithFakeCommits(commits) indexer := newRecentContributorsIndexer(client, db, logger) checker := authz.NewMockSubRepoPermissionChecker() checker.EnabledFunc.SetDefaultReturn(true) @@ -224,19 +223,40 @@ func Test_RecentContributorIndexSkipsSubrepoPermsRepos(t *testing.T) { assert.Equal(t, 0, len(got)) } -func fakeCommitsToLog(commits []fakeCommit) (results []gitserver.CommitLog) { +func fakeCommitsToLog(commits []fakeCommit) (results []*gitdomain.Commit) { for i, commit := range commits { - results = append(results, gitserver.CommitLog{ - AuthorEmail: commit.email, - AuthorName: commit.name, - Timestamp: time.Now(), - SHA: gitSha(fmt.Sprintf("%d", i)), - ChangedFiles: commit.changedFiles, + results = append(results, &gitdomain.Commit{ + ID: api.CommitID(gitSha(fmt.Sprintf("%d", i))), + Author: gitdomain.Signature{ + Email: commit.email, + Name: commit.name, + Date: time.Now(), + }, }) } return results } +func newMockGitserverClientWithFakeCommits(commits []fakeCommit) gitserver.Client { + client := gitserver.NewMockClient() + client.CommitsFunc.SetDefaultReturn(fakeCommitsToLog(commits), nil) + client.ChangedFilesFunc.SetDefaultHook(func(_ context.Context, _ api.RepoName, _, commitID string) (gitserver.ChangedFilesIterator, error) { + ps := make([]gitdomain.PathStatus, 0, len(commits)) + for i, commit := range commits { + if gitSha(fmt.Sprintf("%d", i)) == commitID { + for _, f := range commit.changedFiles { + ps = append(ps, gitdomain.PathStatus{ + Path: f, + Status: gitdomain.StatusAdded, + }) + } + } + } + return gitserver.NewChangedFilesIteratorFromSlice(ps), nil + }) + return client +} + type fakeCommit struct { email string name string