Avoid rdeps when using hash all targets · Tinder/bazel-diff@29362fb · GitHub
Skip to content

Commit 29362fb

Browse files
Avoid rdeps when using hash all targets
When using hash all targets it is overkill to use rdeps to query the impacted targets, since we know at the end of hashing what was affected.
1 parent e0a1cd0 commit 29362fb

5 files changed

Lines changed: 62 additions & 24 deletions

File tree

bazel-diff-example.sh

Lines changed: 4 additions & 13 deletions

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
interface BazelClient {
2222
List<BazelTarget> queryAllTargets() throws IOException;
23-
Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery) throws IOException;
23+
Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery, Boolean hashAllTargets) throws IOException;
2424
Set<BazelSourceFileTarget> convertFilepathsToSourceTargets(Set<Path> filepaths) throws IOException, NoSuchAlgorithmException;
2525
Set<BazelSourceFileTarget> queryAllSourcefileTargets() throws IOException, NoSuchAlgorithmException;
2626
}
@@ -47,12 +47,17 @@ public List<BazelTarget> queryAllTargets() throws IOException {
4747
}
4848

4949
@Override
50-
public Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery) throws IOException {
50+
public Set<String> queryForImpactedTargets(Set<String> impactedTargets, String avoidQuery, Boolean hashAllTargets) throws IOException {
5151
Set<String> impactedTargetNames = new HashSet<>();
5252
String targetQuery = impactedTargets.stream()
5353
.map(target -> String.format("'%s'", target))
5454
.collect(Collectors.joining(" + "));
55-
String query = String.format("rdeps(//... except '//external:all-targets', %s)", targetQuery);
55+
String query = "";
56+
if (hashAllTargets) {
57+
query = targetQuery;
58+
} else {
59+
query = String.format("rdeps(//... except '//external:all-targets', %s)", targetQuery);
60+
}
5661
if (avoidQuery != null) {
5762
query = String.format("(%s) except (%s)", query, avoidQuery);
5863
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
interface TargetHashingClient {
1111
Map<String, String> hashAllBazelTargets(Set<Path> modifiedFilepaths) throws IOException, NoSuchAlgorithmException;
1212
Map<String, String> hashAllBazelTargetsAndSourcefiles() throws IOException, NoSuchAlgorithmException;
13-
Set<String> getImpactedTargets(Map<String, String> startHashes, Map<String, String> endHashes, String avoidQuery) throws IOException;
13+
Set<String> getImpactedTargets(Map<String, String> startHashes, Map<String, String> endHashes, String avoidQuery, Boolean hashAllTargets) throws IOException;
1414
}
1515

1616
class TargetHashingClientImpl implements TargetHashingClient {
@@ -36,7 +36,8 @@ public Map<String, String> hashAllBazelTargetsAndSourcefiles() throws IOExceptio
3636
public Set<String> getImpactedTargets(
3737
Map<String, String> startHashes,
3838
Map<String, String> endHashes,
39-
String avoidQuery)
39+
String avoidQuery,
40+
Boolean hashAllTargets)
4041
throws IOException {
4142
Set<String> impactedTargets = new HashSet<>();
4243
for (Map.Entry<String,String> entry : endHashes.entrySet()) {
@@ -45,7 +46,7 @@ public Set<String> getImpactedTargets(
4546
impactedTargets.add(entry.getKey());
4647
}
4748
}
48-
return bazelClient.queryForImpactedTargets(impactedTargets, avoidQuery);
49+
return bazelClient.queryForImpactedTargets(impactedTargets, avoidQuery, hashAllTargets);
4950
}
5051

5152
private byte[] createDigestForTarget(

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ class BazelDiff implements Callable<Integer> {
158158
@Option(names = {"-o", "--output"}, scope = ScopeType.LOCAL, description = "Filepath to write the impacted Bazel targets to, newline separated")
159159
File outputPath;
160160

161+
@Option(names = {"-a", "--all-sourcefiles"}, description = "Experimental: Hash all sourcefile targets (instead of relying on --modifiedFilepaths), Warning: Performance may degrade from reading all source files")
162+
Boolean hashAllSourcefiles;
163+
161164
@Option(names = {"-aq", "--avoid-query"}, scope = ScopeType.LOCAL, description = "A Bazel query string, any targets that pass this query will be removed from the returned set of targets")
162165
String avoidQuery;
163166

@@ -207,7 +210,7 @@ public Integer call() throws IOException {
207210
Map<String, String > gsonHash = new HashMap<>();
208211
Map<String, String> startingHashes = gson.fromJson(startingFileReader, gsonHash.getClass());
209212
Map<String, String> finalHashes = gson.fromJson(finalFileReader, gsonHash.getClass());
210-
Set<String> impactedTargets = hashingClient.getImpactedTargets(startingHashes, finalHashes, avoidQuery);
213+
Set<String> impactedTargets = hashingClient.getImpactedTargets(startingHashes, finalHashes, avoidQuery, hashAllSourcefiles);
211214
try {
212215
FileWriter myWriter = new FileWriter(outputPath);
213216
myWriter.write(impactedTargets.stream().collect(Collectors.joining(System.lineSeparator())));

test/java/com/bazel_diff/TargetHashingClientImplTests.java

Lines changed: 42 additions & 4 deletions

0 commit comments

Comments
 (0)