Skip to content

Commit 84b4e6d

Browse files
authored
Several cs fixes (#9420)
- Fix grpcurl example in the README to reflect now-required replacement strategy arg - Make incremental update computation properly return an error when no changes found - Don't crash the CS CLI when failing to add a document - Expose the GitClient from github.go, it will be used by the BB CLI in a change shortly
1 parent 676c8be commit 84b4e6d

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

codesearch/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Some example queries:
6868
$ grpc_cli call --metadata x-buildbuddy-api-key:YOUR_DEV_API_KEY localhost:2633 codesearch.service.CodesearchService.Index 'git_repo:<repo_url:"https://github.com/buildbuddy-io/buildbuddy"> repo_state:<commit_sha:"master">'
6969
#
7070
# grpcurl version:
71-
$ grpcurl -plaintext -H "x-buildbuddy-api-key:YOUR_DEV_API_KEY" -d '{"git_repo": {"repo_url":"https://github.com/buildbuddy-io/buildbuddy"}, "repo_state": {"commit_sha":"master"}}' localhost:2633 codesearch.service.CodesearchService.Index
71+
$ grpcurl -plaintext -H "x-buildbuddy-api-key:YOUR_DEV_API_KEY" -d '{"git_repo": {"repo_url":"https://github.com/buildbuddy-io/buildbuddy"}, "repo_state": {"commit_sha":"master"}, "replacement_strategy": 1}' localhost:2633 codesearch.service.CodesearchService.Index
7272
```
7373

7474
## Perform a Search (server)

codesearch/cmd/cli/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func handleIndex(args []string) {
181181
}
182182

183183
if err := github.AddFileToIndex(iw, repoURL, commitSHA, path, buf); err != nil {
184-
log.Fatalf("failed to add file %s: %s", path, err)
184+
log.Infof("Skipping file %s: %s", path, err)
185185
}
186186
}
187187
return nil

codesearch/github/github.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ func validateFile(content []byte) error {
182182
return nil
183183
}
184184

185-
// This type exists to enable dependency injection for testing.
186-
type gitClient interface {
185+
type GitClient interface {
187186
ExecuteCommand(args ...string) (string, error)
188187
LoadFileContents(fileToLoad string) ([]byte, error)
189188
}
@@ -192,7 +191,7 @@ type commandLineGitClient struct {
192191
repoDir string
193192
}
194193

195-
func NewCommandLineGitClient(repoDir string) gitClient {
194+
func NewCommandLineGitClient(repoDir string) GitClient {
196195
return &commandLineGitClient{repoDir: repoDir}
197196
}
198197

@@ -215,7 +214,7 @@ func (c *commandLineGitClient) LoadFileContents(filename string) ([]byte, error)
215214
return content, nil
216215
}
217216

218-
func processDiffTreeLine(gc gitClient, line string, commit *inpb.Commit) error {
217+
func processDiffTreeLine(gc GitClient, line string, commit *inpb.Commit) error {
219218
// diff-tree lines are in the format "<src mode> <dst mode> <src sha> <dst sha> <status>\t<src path>\t<dst path(copy/rename only)>""
220219
// Documentation: https://git-scm.com/docs/git-diff-tree
221220
parts := strings.Split(line, " ")
@@ -275,7 +274,7 @@ func processDiffTreeLine(gc gitClient, line string, commit *inpb.Commit) error {
275274
// The information is extracted using the git command line client on a local clone of a repo.
276275
// The payload contains a list of commits, the file contents for each added/modified file, and a list
277276
// of deleted filenames.
278-
func ComputeIncrementalUpdate(gc gitClient, firstSha, lastSha string) (*inpb.IncrementalUpdate, error) {
277+
func ComputeIncrementalUpdate(gc GitClient, firstSha, lastSha string) (*inpb.IncrementalUpdate, error) {
279278
commitRange := fmt.Sprintf("%s..%s", firstSha, lastSha)
280279

281280
changesStr, err := gc.ExecuteCommand("whatchanged", "--first-parent", "--format=%H", "--reverse", commitRange)
@@ -287,7 +286,7 @@ func ComputeIncrementalUpdate(gc gitClient, firstSha, lastSha string) (*inpb.Inc
287286
if len(changes) > maxAllowedChanges {
288287
return nil, fmt.Errorf("too many changes in commit range %s..%s: %d", firstSha, lastSha, len(changes))
289288
}
290-
if len(changes) == 0 {
289+
if len(changes) == 0 || (len(changes) == 1 && len(changes[0]) == 0) {
291290
return nil, fmt.Errorf("no commits found between %s and %s", firstSha, lastSha)
292291
}
293292

codesearch/github/github_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,20 @@ def456
519519
},
520520
}, result)
521521
}
522+
523+
func TestComputeIncrementalUpdate_NoChanges(t *testing.T) {
524+
firstSHA := "abc123"
525+
lastSHA := "def456"
526+
527+
fakeClient := &fakeGitClient{
528+
t: t,
529+
commands: map[string]string{
530+
fmt.Sprintf("whatchanged --first-parent --format=%%H --reverse %s..%s", firstSHA, lastSHA): "\n",
531+
},
532+
files: map[string][]byte{},
533+
}
534+
535+
result, err := ComputeIncrementalUpdate(fakeClient, firstSHA, lastSHA)
536+
assert.Error(t, err)
537+
assert.Nil(t, result)
538+
}

codesearch/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ func (css *codesearchServer) Index(ctx context.Context, req *inpb.IndexRequest)
327327
unlockFn := css.repoLocks.Lock(lockKey)
328328
defer unlockFn()
329329

330-
log.Infof("Starting indexing %q@%s", repoURL, commitSHA)
330+
log.Infof("Starting indexing %s@%s", repoURL, commitSHA)
331331

332332
var err error
333333
switch req.GetReplacementStrategy() {

0 commit comments

Comments
 (0)