Repository navigation
Add add_prefix attribute to git_repository - #29453
willstranton wants to merge 3 commits into
Conversation
Similar to `http_archive`'s `add_prefix` attribute, this allows git repos to be cloned into a given subdirectory. Fixes bazelbuild#25453
There was a problem hiding this comment.
Pull request overview
This PR adds an add_prefix attribute to git_repository (similar to http_archive) so fetched Git repositories can be materialized under a subdirectory within the external repository, and wires the new attribute through Bzlmod’s registry parsing and repo spec construction.
Changes:
- Add
add_prefixattribute support totools/build_defs/repo/git.bzl, including handling combinedstrip_prefix+add_prefix. - Extend Bzlmod index registry parsing and
GitRepoSpecBuilderto propagateadd_prefixfromsource.jsoninto the generated repo spec. - Update shell integration tests to exercise
add_prefixandadd_prefix + strip_prefixscenarios.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/build_defs/repo/git.bzl | Implements add_prefix in git_repository and adjusts .git cleanup for prefixed checkouts. |
| src/test/shell/bazel/starlark_git_repository_test.sh | Adds coverage for add_prefix scenarios and updates label paths accordingly. |
| src/test/java/com/google/devtools/build/lib/bazel/bzlmod/IndexRegistryTest.java | Extends registry parsing test to include add_prefix. |
| src/main/java/com/google/devtools/build/lib/bazel/bzlmod/IndexRegistry.java | Adds addPrefix field to Git source JSON and forwards it into repo spec creation. |
| src/main/java/com/google/devtools/build/lib/bazel/bzlmod/GitRepoSpecBuilder.java | Adds setAddPrefix() to emit the add_prefix repo-rule attribute when present. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| root = ctx.path(".") | ||
| if ctx.attr.add_prefix: | ||
| root = root.get_child(ctx.attr.add_prefix) | ||
| directory = str(root) |
There was a problem hiding this comment.
This is actually a good one, given we are receiving similar security reports everywhere.
There was a problem hiding this comment.
Refactored out a _checkout_path() method which checks if:
path_with_added_prefix.startswith(repository_root)
I think that's sufficient - there's a new test added test_git_repository_add_prefix_prevent_uproot that shows the build failing if an uplevel reference is attempted. Please check if that's ok.
| dot_git_path = ".git" | ||
| if ctx.attr.strip_prefix: | ||
| ctx.delete(ctx.path(".tmp_git_root/.git")) | ||
| else: | ||
| ctx.delete(ctx.path(".git")) | ||
| dot_git_path = ".tmp_git_root/.git" | ||
| if ctx.attr.add_prefix: | ||
| dot_git_path = "%s/%s" % (ctx.attr.add_prefix, dot_git_path) | ||
| ctx.delete(ctx.path(dot_git_path)) |
There was a problem hiding this comment.
As above, refactored out the function _checkout_path() which checks:
path_with_added_prefix.startswith(repository_root)
and added the test test_git_repository_add_prefix_prevent_uproot to check that it works.
| [ $# -ge 2 ] && [ -n $2 ] && strip_prefix="strip_prefix=\"$2\"," | ||
| [ $# -ge 3 ] && [ -n $3 ] && shallow_since="shallow_since=\"$3\"," | ||
| [ $# -ge 4 ] && [ -n $4 ] && add_prefix="add_prefix=\"$4\"," && add_prefix_path=$4 |
| [ $# -ge 3 ] && [ -n $3 ] && strip_prefix="strip_prefix=\"$3\"," | ||
| [ $# -ge 4 ] && [ -n $4 ] && add_prefix="add_prefix=\"$4\"," && add_prefix_path="$4/" |
git_repository checks add_prefix containment with a raw string prefix. A normalized path can therefore select a sibling whose name shares the checkout-root prefix, and the worker deletes that sibling before fetching. Require a path-component boundary (or the root itself) and cover the destructive sibling case with a sentinel regression. Follow-up to bazelbuild#29453.
git_repository checks add_prefix containment with a raw string prefix. A normalized path can therefore select a sibling whose name shares the checkout-root prefix, and the worker deletes that sibling before fetching. Require a path-component boundary (or the root itself) and cover the destructive sibling case with a sentinel regression. Follow-up to bazelbuild#29453.
git_repository checks add_prefix containment with a raw string prefix. A normalized path can therefore select a sibling whose name shares the checkout-root prefix, and the worker deletes that sibling before fetching. Require a path-component boundary (or the root itself) and cover the destructive sibling case with a sentinel regression. Follow-up to #29453. Closes #30309. PiperOrigin-RevId: 969773896 Change-Id: I7e9fbd29625c4c8c3ec3e056c13745cdfefe929c
git_repository checks add_prefix containment with a raw string prefix. A normalized path can therefore select a sibling whose name shares the checkout-root prefix, and the worker deletes that sibling before fetching. Require a path-component boundary (or the root itself) and cover the destructive sibling case with a sentinel regression. Follow-up to bazelbuild#29453. Closes bazelbuild#30309. PiperOrigin-RevId: 969773896 Change-Id: I7e9fbd29625c4c8c3ec3e056c13745cdfefe929c
Description
Similar to
http_archive'sadd_prefixattribute, this allows git repos to be cloned into a given subdirectory.Fixes #25453
Motivation
See #25453 . This also further aligns the attributes of
http_archivewithgit_repositoryBuild API Changes
See #25453
Yes - an attribute is added. It is optional to use.
N/A
Checklist
Release Notes
RELNOTES[NEW]: Add
add_prefixattribute togit_repositoryto allow cloning repositories into a given subdirectory prefix