Skip to content

Commit 4474051

Browse files
Merge pull request #72 from Tinder/remove_modified_filepaths
Remove modified filepaths and avoid query options
2 parents 742b7ab + 8931de6 commit 4474051

File tree

7 files changed

+48
-378
lines changed

7 files changed

+48
-378
lines changed

README.md

+25-63
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,22 @@ Open `bazel-diff-example.sh` to see how this is implemented. This is purely an e
6060

6161
`bazel-diff` works as follows
6262

63-
* The `modified-filepaths` command is used to fetch the modified filepaths between two Git revisions, we write this list of modified filepaths to a file.
63+
* The previous revision is checked out, then we run `generate-hashes`. This gives us the hashmap representation for the entire Bazel graph, then we write this JSON to a file.
6464

65-
* The previous revision is checked out, then we run `generate-hashes`. This gives us the hashmap representation for the entire Bazel graph, then we write this JSON to a file. We do not pass `modified-filepaths` here since it is the starting revision.
65+
* Next we checkout the initial revision, then we run `generate-hashes` and write that JSON to a file. Now we have our final hashmap representation for the Bazel graph.
6666

67-
* Next we checkout the initial revision, then we run `generate-hashes` with the file output of `modified-filepaths` and write that JSON to a file. Now we have our final hashmap representation for the Bazel graph.
68-
69-
* We run `bazel-diff` on the starting and final JSON hash filepaths to get our impacted set of targets. This impacted set of targets is written to a file. You can also pass the `-t` flag to only return tests
67+
* We run `bazel-diff` on the starting and final JSON hash filepaths to get our impacted set of targets. This impacted set of targets is written to a file.
7068

7169
## CLI Interface
7270

7371
`bazel-diff` Command
7472

7573
```terminal
76-
Usage: bazel-diff [-hV] [-aq=<avoidQuery>] -b=<bazelPath>
77-
[-co=<bazelCommandOptions>] [-fh=<finalHashesJSONPath>]
78-
[-o=<outputPath>] [-sh=<startingHashesJSONPath>]
79-
[-so=<bazelStartupOptions>] -w=<workspacePath> [COMMAND]
74+
Usage: bazel-diff [-hV] -b=<bazelPath> [-co=<bazelCommandOptions>]
75+
[-fh=<finalHashesJSONPath>] [-o=<outputPath>]
76+
[-sh=<startingHashesJSONPath>] [-so=<bazelStartupOptions>]
77+
-w=<workspacePath> [COMMAND]
8078
Writes to a file the impacted targets between two Bazel graph JSON files
81-
-aq, --avoid-query=<avoidQuery>
82-
A Bazel query string, any targets that pass this query will
83-
be removed from the returned set of targets
8479
-b, --bazelPath=<bazelPath>
8580
Path to Bazel binary
8681
-co, --bazelCommandOptions=<bazelCommandOptions>
@@ -102,71 +97,39 @@ Writes to a file the impacted targets between two Bazel graph JSON files
10297
-V, --version Print version information and exit.
10398
-w, --workspacePath=<workspacePath>
10499
Path to Bazel workspace directory.
105-
```
106-
107-
`modified-filepaths` Command
108-
109-
```terminal
110-
Usage: bazel-diff modified-filepaths [-hV] -b=<bazelPath>
111-
[-co=<bazelCommandOptions>]
112-
[-so=<bazelStartupOptions>]
113-
-w=<workspacePath> <startingGitRevision>
114-
<endingGitRevision> <outputPath>
115-
Writes to the file the modified filepaths between two revisions.
116-
<startingGitRevision> The starting Git revision, e.g. "HEAD^"
117-
<endingGitRevision> The final Git revision, e.g. "HEAD"
118-
<outputPath> Path that the list of modified files will be
119-
written to
120-
-b, --bazelPath=<bazelPath>
121-
Path to Bazel binary
122-
-co, --bazelCommandOptions=<bazelCommandOptions>
123-
Additional space separated Bazel command options
124-
used when invoking Bazel
125-
-h, --help Show this help message and exit.
126-
-so, --bazelStartupOptions=<bazelStartupOptions>
127-
Additional space separated Bazel client startup
128-
options used when invoking Bazel
129-
-V, --version Print version information and exit.
130-
-w, --workspacePath=<workspacePath>
131-
Path to Bazel workspace directory.
100+
Commands:
101+
generate-hashes Writes to a file the SHA256 hashes for each Bazel Target in
102+
the provided workspace.
132103
```
133104

134105
`generate-hashes` Command
135106

136107
```terminal
137-
Usage: bazel-diff generate-hashes [-m=<modifiedFilepaths> | -a] [-hV]
138-
-b=<bazelPath> [-co=<bazelCommandOptions>]
108+
Usage: bazel-diff generate-hashes [-hV] -b=<bazelPath>
109+
[-co=<bazelCommandOptions>]
139110
[-s=<seedFilepaths>]
140111
[-so=<bazelStartupOptions>]
141112
-w=<workspacePath> <outputPath>
142113
Writes to a file the SHA256 hashes for each Bazel Target in the provided
143114
workspace.
144-
<outputPath> The filepath to write the resulting JSON of
145-
dictionary target => SHA-256 values
146-
-a, --all-sourcefiles Experimental: Hash all sourcefile targets (instead of
147-
relying on --modifiedFilepaths), Warning:
148-
Performance may degrade from reading all source
149-
files
115+
<outputPath> The filepath to write the resulting JSON of dictionary
116+
target => SHA-256 values
150117
-b, --bazelPath=<bazelPath>
151-
Path to Bazel binary
118+
Path to Bazel binary
152119
-co, --bazelCommandOptions=<bazelCommandOptions>
153-
Additional space separated Bazel command options used
154-
when invoking Bazel
155-
-h, --help Show this help message and exit.
156-
-m, --modifiedFilepaths=<modifiedFilepaths>
157-
The path to a file containing the list of modified
158-
filepaths in the workspace, you can use the
159-
'modified-filepaths' command to get this list
120+
Additional space separated Bazel command options used when
121+
invoking Bazel
122+
-h, --help Show this help message and exit.
160123
-s, --seed-filepaths=<seedFilepaths>
161-
A text file containing a newline separated list of
162-
filepaths, each of these filepaths will be read and
163-
used as a seed for all targets.
124+
A text file containing a newline separated list of
125+
filepaths, each of these filepaths will be read and used
126+
as a seed for all targets.
164127
-so, --bazelStartupOptions=<bazelStartupOptions>
165-
Additional space separated Bazel client startup
166-
options used when invoking Bazel
167-
-V, --version Print version information and exit.
128+
Additional space separated Bazel client startup options
129+
used when invoking Bazel
130+
-V, --version Print version information and exit.
168131
-w, --workspacePath=<workspacePath>
169-
Path to Bazel workspace directory.
132+
Path to Bazel workspace directory.
170133
```
171134

172135
## Installing
@@ -205,7 +168,6 @@ now run the tool with
205168
bazel run //:bazel-diff
206169
```
207170

208-
209171
> Note, in releases prior to 2.0.0 the value for the `main_class` attribute is just `BazelDiff`
210172
211173
### Run Via JAR Release

bazel-diff-example.sh

+3-12
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,18 @@ $bazel_path run :bazel-diff $shared_flags --script_path="$bazel_diff"
2525
git -C $workspace_path checkout $previous_revision --quiet
2626

2727
echo "Generating Hashes for Revision '$previous_revision'"
28-
$bazel_diff generate-hashes -w $workspace_path -b $bazel_path $starting_hashes_json -a
28+
$bazel_diff generate-hashes -w $workspace_path -b $bazel_path $starting_hashes_json
2929

3030
git -C $workspace_path checkout $final_revision --quiet
3131

3232
echo "Generating Hashes for Revision '$final_revision'"
33-
$bazel_diff generate-hashes -w $workspace_path -b $bazel_path $final_hashes_json -a
33+
$bazel_diff generate-hashes -w $workspace_path -b $bazel_path $final_hashes_json
3434

3535
echo "Determining Impacted Targets"
36-
$bazel_diff -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_targets_path -a
37-
38-
echo "Determining Impacted Test Targets"
39-
$bazel_diff -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_test_targets_path -a --avoid-query "//... except tests(//...)"
36+
$bazel_diff -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_targets_path
4037

4138
IFS=$'\n' read -d '' -r -a impacted_targets < $impacted_targets_path
4239
formatted_impacted_targets=$(IFS=$'\n'; echo "${impacted_targets[*]}")
4340
echo "Impacted Targets between $previous_revision and $final_revision:"
4441
echo $formatted_impacted_targets
4542
echo ""
46-
47-
IFS=$'\n' read -d '' -r -a impacted_test_targets < $impacted_test_targets_path
48-
formatted_impacted_test_targets=$(IFS=$'\n'; echo "${impacted_test_targets[*]}")
49-
echo "Impacted Test Targets between $previous_revision and $final_revision:"
50-
echo $formatted_impacted_test_targets
51-
echo ""

integration/integration_test.sh

+9-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final_hashes_json="$output_dir/final_hashes.json"
1212
impacted_targets_path="$output_dir/impacted_targets.txt"
1313
shared_flags=""
1414

15-
export USE_BAZEL_VERSION=last_downstream_green
15+
export USE_BAZEL_VERSION=4.1.0
1616

1717
containsElement () {
1818
local e match="$1"
@@ -23,28 +23,19 @@ containsElement () {
2323

2424
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path $starting_hashes_json
2525

26-
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path -m $modified_filepaths_output $final_hashes_json
26+
$bazel_path run :bazel-diff $shared_flags -- generate-hashes -w $workspace_path -b $bazel_path $final_hashes_json
2727

28-
awk '{gsub(/:StringGenerator.java": \"\w+\"/,"modifiedhash");print}' $final_hashes_json > /dev/null
28+
final_tmp=$(mktemp)
29+
awk '{gsub(/32c874ad651f0dd234d712b7f1dce6b54aa36cf16380da763a8eb7bb7c1f9514/,"modifiedhash");print}' $final_hashes_json > $final_tmp
30+
cp $final_tmp $final_hashes_json
2931

30-
$bazel_path run :bazel-diff $shared_flags -- -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_targets_path -aq "attr('tags', 'manual', //...)"
32+
$bazel_path run :bazel-diff $shared_flags -- -sh $starting_hashes_json -fh $final_hashes_json -w $workspace_path -b $bazel_path -o $impacted_targets_path
3133

3234
IFS=$'\n' read -d '' -r -a impacted_targets < $impacted_targets_path
33-
target1="//test/java/com/integration:bazel-diff-integration-test-lib"
34-
target2="//src/main/java/com/integration:bazel-diff-integration-lib"
35-
target3="//test/java/com/integration:bazel-diff-integration-tests"
36-
target4="//src/main/java/com/integration/submodule:Submodule"
37-
if containsElement $target1 "${impacted_targets[@]}" && \
38-
containsElement $target2 "${impacted_targets[@]}" && \
39-
containsElement $target3 "${impacted_targets[@]}"
35+
target1="//src/main/java/com/integration:StringGenerator.java"
36+
if containsElement $target1 "${impacted_targets[@]}"
4037
then
41-
if containsElement $target4 "${impacted_targets[@]}"
42-
then
43-
echo "FAILURE Incorrect impacted targets: ${impacted_targets[@]}"
44-
exit 1
45-
else
46-
echo "SUCCESS: Correct impacted targets: ${impacted_targets[@]}"
47-
fi
38+
echo "SUCCESS: Correct impacted targets: ${impacted_targets[@]}"
4839
else
4940
echo "FAILURE: Incorrect impacted targets: ${impacted_targets[@]}"
5041
exit 1

src/main/java/com/bazel_diff/BazelClient.java

-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
interface BazelClient {
2222
List<BazelTarget> queryAllTargets() throws IOException;
23-
Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery, String universeQuery) throws IOException;
2423
Set<BazelSourceFileTarget> convertFilepathsToSourceTargets(Set<Path> filepaths) throws IOException, NoSuchAlgorithmException;
2524
Set<BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException;
2625
}
@@ -46,25 +45,6 @@ public List<BazelTarget> queryAllTargets() throws IOException {
4645
return targets.stream().map( target -> new BazelTargetImpl(target)).collect(Collectors.toList());
4746
}
4847

49-
@Override
50-
public Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery, String universeQuery) throws IOException {
51-
Set<String> impactedTargetNames = new HashSet<>();
52-
String targetQuery = impactedTargets.stream()
53-
.map(target -> String.format("'%s'", target))
54-
.collect(Collectors.joining(" + "));
55-
String query = query = String.format("rdeps(%s except '//external:all-targets', %s)", universeQuery, targetQuery);
56-
if (avoidQuery != null) {
57-
query = String.format("(%s) except (%s)", query, avoidQuery);
58-
}
59-
List<Build.Target> targets = performBazelQuery(query);
60-
for (Build.Target target : targets) {
61-
if (target.hasRule()) {
62-
impactedTargetNames.add(target.getRule().getName());
63-
}
64-
}
65-
return impactedTargetNames;
66-
}
67-
6848
@Override
6949
public Set<BazelSourceFileTarget> convertFilepathsToSourceTargets(Set<Path> filepaths) throws IOException, NoSuchAlgorithmException {
7050
Set<BazelSourceFileTarget> sourceTargets = new HashSet<>();

src/main/java/com/bazel_diff/TargetHashingClient.java

+3-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import com.google.common.primitives.Bytes;
1111

1212
interface TargetHashingClient {
13-
Map<String, String> hashAllBazelTargets(Set<Path> modifiedFilepaths, Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException;
1413
Map<String, String> hashAllBazelTargetsAndSourcefiles(Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException;
15-
Set<String> getImpactedTargets(Map<String, String> startHashes, Map<String, String> endHashes, String avoidQuery, String universeQuery, Boolean hashAllTargets) throws IOException;
14+
Set<String> getImpactedTargets(Map<String, String> startHashes, Map<String, String> endHashes) throws IOException;
1615
}
1716

1817
class TargetHashingClientImpl implements TargetHashingClient {
@@ -24,12 +23,6 @@ class TargetHashingClientImpl implements TargetHashingClient {
2423
this.files = files;
2524
}
2625

27-
@Override
28-
public Map<String, String> hashAllBazelTargets(Set<Path> modifiedFilepaths, Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException {
29-
Set<BazelSourceFileTarget> bazelSourcefileTargets = bazelClient.convertFilepathsToSourceTargets(modifiedFilepaths);
30-
return hashAllTargets(createSeedForFilepaths(seedFilepaths), bazelSourcefileTargets);
31-
}
32-
3326
@Override
3427
public Map<String, String> hashAllBazelTargetsAndSourcefiles(Set<Path> seedFilepaths) throws IOException, NoSuchAlgorithmException {
3528
Set<BazelSourceFileTarget> bazelSourcefileTargets = bazelClient.queryAllSourcefileTargets();
@@ -39,10 +32,7 @@ public Map<String, String> hashAllBazelTargetsAndSourcefiles(Set<Path> seedFilep
3932
@Override
4033
public Set<String> getImpactedTargets(
4134
Map<String, String> startHashes,
42-
Map<String, String> endHashes,
43-
String avoidQuery,
44-
String universeQuery,
45-
Boolean hashAllTargets)
35+
Map<String, String> endHashes)
4636
throws IOException {
4737
Set<String> impactedTargets = new HashSet<>();
4838
for (Map.Entry<String,String> entry : endHashes.entrySet()) {
@@ -51,10 +41,7 @@ public Set<String> getImpactedTargets(
5141
impactedTargets.add(entry.getKey());
5242
}
5343
}
54-
if (hashAllTargets != null && hashAllTargets && avoidQuery == null) {
55-
return impactedTargets;
56-
}
57-
return bazelClient.queryForImpactedTargets(impactedTargets, avoidQuery, universeQuery);
44+
return impactedTargets;
5845
}
5946

6047
private byte[] createDigestForTarget(

0 commit comments

Comments
 (0)