Skip to content

Commit

Permalink
set working directory to cwd by default (#8)
Browse files Browse the repository at this point in the history
* set working directory to cwd by default

* add flag to skip output when there are no results
  • Loading branch information
ewhauser authored Apr 13, 2022
1 parent 8dccf25 commit ea47461
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/bazel-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ jobs:
# need to bootstrap
- name: build
run: bazel build //cli:bazel-differ
- name: set bazel differ path
run: echo "BAZEL_DIFFER=$(bazel info bazel-bin)/cli/bazel-differ_/bazel-differ" >> $GITHUB_ENV
# This section starts an example of how to use get-targets in your CI process
- name: Get revisions
id: get-revisions
run: GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} ./tools/diff-sha.sh
- name: get-test-targets
run: |
$(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
bash -c '[[ ! -s test_targets.txt ]] && rm test_targets.txt || true'
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
- name: run-test-targets
if: hashFiles('test_targets.txt') != ''
run: bazel test --target_pattern_file=test_targets.txt
- name: get-build-targets
run: |
$(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
bash -c '[[ ! -s build_targets.txt ]] && rm build_targets.txt || true'
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
- name: run-build-targets
if: hashFiles('build_targets.txt') != ''
run: bazel build --target_pattern_file=build_targets.txt
Expand Down
13 changes: 10 additions & 3 deletions cmd/get_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var finalRevision string
var query string
var cacheDir string
var cacheDisabled bool
var outputOnEmpty bool

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

if Output != "" {
if len(targetNames) == 0 && !outputOnEmpty {
return
}
internal.WriteTargetsFile(targetNames, Output)
}

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

hashes, err := cacheManager.Get(context.Background(), revision)
ExitIfError(err, fmt.Sprintf("Error retrieving hashes for revision %s from cache", revision))

if hashes == nil {
hashes, err = targetHasher.HashAllBazelTargetsAndSourcefiles(seedfilePaths)
if hashes != nil {
return hashes
}

hashes, err = targetHasher.HashAllBazelTargetsAndSourcefiles(seedfilePaths)
ExitIfError(err, "")

err = cacheManager.Put(context.Background(), revision, hashes)
Expand All @@ -92,6 +97,8 @@ func init() {
getTargets.PersistentFlags().StringVarP(&Output, "output", "o", "",
"Filepath to write the impacted Bazel targets to, "+
"newline separated")
getTargets.PersistentFlags().BoolVar(&outputOnEmpty, "output-on-empty", true,
"Sets whether to write a file output when there are no results; useful for skipping targets in CI")
getTargets.PersistentFlags().StringVar(&cacheDir, "cache-dir", "",
"Directory to cache hashes associated with commits")
getTargets.PersistentFlags().BoolVar(&cacheDisabled, "nocache", false,
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var rootCmd = &cobra.Command{
}

func init() {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
WorkspacePath = cwd
rootCmd.PersistentFlags().StringVarP(&WorkspacePath, "workspacePath", "w", "", "Path to Bazel workspace directory.")
rootCmd.PersistentFlags().StringVarP(&BazelPath, "bazelPath", "b", "", "Path to Bazel binary")
rootCmd.PersistentFlags().StringVarP(&BazelStartupOptions, "bazelStartupOptions", "y", "",
Expand Down

0 comments on commit ea47461

Please sign in to comment.