Skip to content

Commit ea47461

Browse files
authored
set working directory to cwd by default (#8)
* set working directory to cwd by default * add flag to skip output when there are no results
1 parent 8dccf25 commit ea47461

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

.github/workflows/bazel-ci.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ jobs:
2828
# need to bootstrap
2929
- name: build
3030
run: bazel build //cli:bazel-differ
31+
- name: set bazel differ path
32+
run: echo "BAZEL_DIFFER=$(bazel info bazel-bin)/cli/bazel-differ_/bazel-differ" >> $GITHUB_ENV
3133
# This section starts an example of how to use get-targets in your CI process
3234
- name: Get revisions
3335
id: get-revisions
3436
run: GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} ./tools/diff-sha.sh
3537
- name: get-test-targets
36-
run: |
37-
$(bazel info bazel-bin)/cli/bazel-differ_/bazel-differ get-targets -w $(pwd) -s ${{ steps.get-revisions.outputs.previous_sha }} -f ${{ steps.get-revisions.outputs.current_sha }} -o test_targets.txt
38-
bash -c '[[ ! -s test_targets.txt ]] && rm test_targets.txt || true'
38+
run: $BAZEL_DIFFER get-targets -s ${{ steps.get-revisions.outputs.previous_sha }} -f ${{ steps.get-revisions.outputs.current_sha }} -o test_targets.txt -v --output-on-empty=false
3939
- name: run-test-targets
4040
if: hashFiles('test_targets.txt') != ''
4141
run: bazel test --target_pattern_file=test_targets.txt
4242
- name: get-build-targets
43-
run: |
44-
$(bazel info bazel-bin)/cli/bazel-differ_/bazel-differ get-targets -w $(pwd) -s ${{ steps.get-revisions.outputs.previous_sha }} -f ${{ steps.get-revisions.outputs.current_sha }} -o build_targets.txt -v
45-
bash -c '[[ ! -s build_targets.txt ]] && rm build_targets.txt || true'
43+
run: $BAZEL_DIFFER get-targets -s ${{ steps.get-revisions.outputs.previous_sha }} -f ${{ steps.get-revisions.outputs.current_sha }} -o build_targets.txt -v --output-on-empty=false
4644
- name: run-build-targets
4745
if: hashFiles('build_targets.txt') != ''
4846
run: bazel build --target_pattern_file=build_targets.txt

cmd/get_targets.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var finalRevision string
1313
var query string
1414
var cacheDir string
1515
var cacheDisabled bool
16+
var outputOnEmpty bool
1617

1718
// getTargets represents the get-targets command
1819
var getTargets = &cobra.Command{
@@ -43,6 +44,9 @@ $ bazel-differ get-targets -w path/to/workspace -b $(which bazel) -s START_HASH
4344
targetNames := targetHasher.GetNames(queriedTargets)
4445

4546
if Output != "" {
47+
if len(targetNames) == 0 && !outputOnEmpty {
48+
return
49+
}
4650
internal.WriteTargetsFile(targetNames, Output)
4751
}
4852

@@ -66,10 +70,11 @@ func getHashes(revision string, gitClient internal.GitClient, cacheManager cache
6670

6771
hashes, err := cacheManager.Get(context.Background(), revision)
6872
ExitIfError(err, fmt.Sprintf("Error retrieving hashes for revision %s from cache", revision))
69-
70-
if hashes == nil {
71-
hashes, err = targetHasher.HashAllBazelTargetsAndSourcefiles(seedfilePaths)
73+
if hashes != nil {
74+
return hashes
7275
}
76+
77+
hashes, err = targetHasher.HashAllBazelTargetsAndSourcefiles(seedfilePaths)
7378
ExitIfError(err, "")
7479

7580
err = cacheManager.Put(context.Background(), revision, hashes)
@@ -92,6 +97,8 @@ func init() {
9297
getTargets.PersistentFlags().StringVarP(&Output, "output", "o", "",
9398
"Filepath to write the impacted Bazel targets to, "+
9499
"newline separated")
100+
getTargets.PersistentFlags().BoolVar(&outputOnEmpty, "output-on-empty", true,
101+
"Sets whether to write a file output when there are no results; useful for skipping targets in CI")
95102
getTargets.PersistentFlags().StringVar(&cacheDir, "cache-dir", "",
96103
"Directory to cache hashes associated with commits")
97104
getTargets.PersistentFlags().BoolVar(&cacheDisabled, "nocache", false,

cmd/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ var rootCmd = &cobra.Command{
2525
}
2626

2727
func init() {
28+
cwd, err := os.Getwd()
29+
if err != nil {
30+
panic(err)
31+
}
32+
WorkspacePath = cwd
2833
rootCmd.PersistentFlags().StringVarP(&WorkspacePath, "workspacePath", "w", "", "Path to Bazel workspace directory.")
2934
rootCmd.PersistentFlags().StringVarP(&BazelPath, "bazelPath", "b", "", "Path to Bazel binary")
3035
rootCmd.PersistentFlags().StringVarP(&BazelStartupOptions, "bazelStartupOptions", "y", "",

0 commit comments

Comments
 (0)