@@ -13,6 +13,7 @@ var finalRevision string
1313var query string
1414var cacheDir string
1515var cacheDisabled bool
16+ var outputOnEmpty bool
1617
1718// getTargets represents the get-targets command
1819var 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 ,
0 commit comments