|
4 | 4 | # |
5 | 5 | # This script determines which Bazel targets should be built or tested and writes them separated by newlines to stdout. |
6 | 6 | # |
7 | | -# If --base is passed only include targets with modified inputs in `git diff --name-only --merge-base $BASE $HEAD`. |
8 | | -# When --head is not provided defaults to HEAD. |
| 7 | +# If --base is passed only include targets with modified inputs in `git diff --name-only --merge-base $BASE [$HEAD]`. |
| 8 | +# where `$HEAD` is from --head if specified. |
9 | 9 | # |
10 | 10 | # If --skip_long_tests is passed, tests tagged with 'long_test' will be excluded. |
11 | 11 | # |
@@ -110,15 +110,20 @@ def load_explicit_targets() -> dict[str, Set[str]]: |
110 | 110 | return explicit_targets_dict |
111 | 111 |
|
112 | 112 |
|
113 | | -def diff_only_query(command: str, base: str, head: str, skip_long_tests: bool) -> str: |
| 113 | +def diff_only_query(command: str, base: str, head: str | None, skip_long_tests: bool) -> str: |
114 | 114 | """ |
115 | | - Return a bazel query for all targets that have modified inputs in the specified git commit range. Taking into account: |
| 115 | + Return a bazel query for all targets that have modified inputs in the specified git commit range. |
| 116 | + If `head` is not specified it diffs against the working tree which is useful for testing locally. |
| 117 | + It takes into account: |
116 | 118 | * To return all targets in case files matching ALL_TARGETS_BLOBS are modified. |
117 | 119 | * To only include test targets in case the bazel command was 'test'. |
118 | 120 | * To exclude long_tests if requested. |
119 | 121 | """ |
120 | 122 | modified_files = subprocess.run( |
121 | | - ["git", "diff", "--name-only", "--merge-base", base, head], check=True, capture_output=True, text=True |
| 123 | + ["git", "diff", "--name-only", "--merge-base", base] + ([head] if head is not None else []), |
| 124 | + check=True, |
| 125 | + capture_output=True, |
| 126 | + text=True, |
122 | 127 | ).stdout.splitlines() |
123 | 128 |
|
124 | 129 | n = len(modified_files) |
@@ -187,7 +192,7 @@ def targets( |
187 | 192 | query = ( |
188 | 193 | ("//..." + (" except attr(tags, long_test, //...)" if skip_long_tests else "")) |
189 | 194 | if base is None |
190 | | - else diff_only_query(command, base, "HEAD" if head is None else head, skip_long_tests) |
| 195 | + else diff_only_query(command, base, head, skip_long_tests) |
191 | 196 | ) |
192 | 197 |
|
193 | 198 | # Finally, exclude targets that have any of the excluded tags: |
@@ -277,7 +282,7 @@ def main(): |
277 | 282 | ) |
278 | 283 | parser.add_argument( |
279 | 284 | "--base", |
280 | | - help="Only include targets with modified inputs in `git diff --name-only --merge-base $BASE $HEAD`. When --head is not provided defaults to HEAD.", |
| 285 | + help="Only include targets with modified inputs in `git diff --name-only --merge-base $BASE [$HEAD]` where $HEAD is from --head if specified.", |
281 | 286 | ) |
282 | 287 | parser.add_argument("--head", help="See --base.") |
283 | 288 | args = parser.parse_args() |
|
0 commit comments