|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/ewhauser/bazel-differ/internal" |
| 7 | + "github.com/ewhauser/bazel-differ/internal/cache" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var startingRevision string |
| 12 | +var finalRevision string |
| 13 | +var query string |
| 14 | +var cacheDir string |
| 15 | +var cacheDisabled bool |
| 16 | + |
| 17 | +// getTargets represents the get-targets command |
| 18 | +var getTargets = &cobra.Command{ |
| 19 | + Use: "get-targets", |
| 20 | + Short: "Collects a set of a targets between the specified commit ranges", |
| 21 | + Long: `Collects a set of a targets between the specified commit ranges. By default, |
| 22 | +the final set of targets is run through a Bazel query "set({{.Targets}})" which can be customized by the -q parameter. |
| 23 | +If you want to query all tests impacted for the given commit range, you can do: |
| 24 | +
|
| 25 | +$ bazel-differ get-targets -w path/to/workspace -b $(which bazel) -s START_HASH -f FINAL_HASH -q 'kind(".*_test",set({{.Targets}}))'" -o test_targets.txt |
| 26 | +`, |
| 27 | + Run: func(cmd *cobra.Command, args []string) { |
| 28 | + gitClient := internal.NewGitClient(WorkspacePath) |
| 29 | + bazelClient := GetBazelClient() |
| 30 | + cacheManager, err := cache.NewHashCacheManager(!cacheDisabled, cacheDir) |
| 31 | + ExitIfError(err, "") |
| 32 | + targetHasher := internal.NewTargetHashingClient(GetBazelClient(), internal.Filesystem, |
| 33 | + internal.NewRuleProvider()) |
| 34 | + |
| 35 | + startingHashes := getHashes(startingRevision, gitClient, cacheManager, targetHasher) |
| 36 | + endingHashes := getHashes(finalRevision, gitClient, cacheManager, targetHasher) |
| 37 | + |
| 38 | + targets, err := targetHasher.GetImpactedTargets(startingHashes, endingHashes) |
| 39 | + ExitIfError(err, "") |
| 40 | + |
| 41 | + queriedTargets, err := bazelClient.QueryTarget(query, targets) |
| 42 | + ExitIfError(err, "") |
| 43 | + targetNames := targetHasher.GetNames(queriedTargets) |
| 44 | + |
| 45 | + if Output != "" { |
| 46 | + internal.WriteTargetsFile(targetNames, Output) |
| 47 | + } else { |
| 48 | + for k := range targetNames { |
| 49 | + fmt.Println(k) |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +func getHashes(revision string, gitClient internal.GitClient, cacheManager cache.HashCacheManager, |
| 56 | + targetHasher internal.TargetHashingClient) map[string]string { |
| 57 | + err := gitClient.Checkout(revision) |
| 58 | + ExitIfError(err, fmt.Sprintf("Unable to checkout revision: %s", revision)) |
| 59 | + |
| 60 | + var seedfilePaths = make(map[string]bool) |
| 61 | + if SeedFilepaths != "" { |
| 62 | + seedfilePaths = readSeedFile() |
| 63 | + } |
| 64 | + |
| 65 | + hashes, err := cacheManager.Get(context.Background(), revision) |
| 66 | + ExitIfError(err, fmt.Sprintf("Error retrieving hashes for revision %s from cache", revision)) |
| 67 | + |
| 68 | + if hashes == nil { |
| 69 | + hashes, err = targetHasher.HashAllBazelTargetsAndSourcefiles(seedfilePaths) |
| 70 | + } |
| 71 | + ExitIfError(err, "") |
| 72 | + |
| 73 | + err = cacheManager.Put(context.Background(), revision, hashes) |
| 74 | + ExitIfError(err, "") |
| 75 | + |
| 76 | + return hashes |
| 77 | +} |
| 78 | + |
| 79 | +func init() { |
| 80 | + rootCmd.AddCommand(getTargets) |
| 81 | + getTargets.PersistentFlags().StringVarP(&startingRevision, "startingRevision", "s", "", |
| 82 | + "The Git revision to generate the ending hashes") |
| 83 | + getTargets.PersistentFlags().StringVarP(&finalRevision, "finalRevision", "f", "", |
| 84 | + "The Git revision to use to generate the ending hashes") |
| 85 | + getTargets.Flags().StringVarP(&SeedFilepaths, "seed-filepaths", "", "", |
| 86 | + "A text file containing a newline separated list of filepaths, "+ |
| 87 | + "each of these filepaths will be read and used as a seed for all targets.") |
| 88 | + getTargets.PersistentFlags().StringVarP(&query, "query", "q", "", |
| 89 | + "The query template to use when querying for changed targets") |
| 90 | + getTargets.PersistentFlags().StringVarP(&Output, "output", "o", "", |
| 91 | + "Filepath to write the impacted Bazel targets to, "+ |
| 92 | + "newline separated") |
| 93 | + getTargets.PersistentFlags().StringVar(&cacheDir, "cache-dir", "", |
| 94 | + "Directory to cache hashes associated with commits") |
| 95 | + getTargets.PersistentFlags().BoolVar(&cacheDisabled, "nocache", false, |
| 96 | + "Disables hash caching") |
| 97 | +} |
0 commit comments