Skip to content

Add add_prefix attribute to git_repository - #29453

Closed
willstranton wants to merge 3 commits into
bazelbuild:masterfrom
willstranton:git-add-prefix
Closed

willstranton wants to merge 3 commits into
bazelbuild:masterfrom
willstranton:git-add-prefix

Conversation

@willstranton

Copy link
Copy Markdown
Contributor

Description

Similar to http_archive's add_prefix attribute, this allows git repos to be cloned into a given subdirectory.

Fixes #25453

Motivation

See #25453 . This also further aligns the attributes of http_archive with git_repository

Build API Changes

  1. Has this been discussed in a design doc or issue? (Please link it)

See #25453

  1. Is the change backward compatible?

Yes - an attribute is added. It is optional to use.

  1. If it's a breaking change, what is the migration plan?

N/A

Checklist

  • I have added tests for the new use cases (if any).
  • I have updated the documentation (if applicable).

Release Notes

RELNOTES[NEW]: Add add_prefix attribute to git_repository to allow cloning repositories into a given subdirectory prefix

Similar to `http_archive`'s `add_prefix` attribute, this allows git
repos to be cloned into a given subdirectory.

Fixes bazelbuild#25453
@willstranton
willstranton marked this pull request as ready for review May 2, 2026 19:47
@github-actions github-actions Bot added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. area-Bzlmod Bzlmod-specific PRs, issues, and feature requests awaiting-review PR is awaiting review from an assigned reviewer labels May 2, 2026
@meteorcloudy
meteorcloudy requested a review from Copilot May 4, 2026 13:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_prefix attribute support to tools/build_defs/repo/git.bzl, including handling combined strip_prefix + add_prefix.
  • Extend Bzlmod index registry parsing and GitRepoSpecBuilder to propagate add_prefix from source.json into the generated repo spec.
  • Update shell integration tests to exercise add_prefix and add_prefix + strip_prefix scenarios.

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.

Comment thread tools/build_defs/repo/git.bzl Outdated
Comment on lines 43 to 46
root = ctx.path(".")
if ctx.attr.add_prefix:
root = root.get_child(ctx.attr.add_prefix)
directory = str(root)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a good one, given we are receiving similar security reports everywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tools/build_defs/repo/git.bzl Outdated
Comment on lines +246 to +251
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))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +119 to +121
[ $# -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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment on lines +245 to +246
[ $# -ge 3 ] && [ -n $3 ] && strip_prefix="strip_prefix=\"$3\","
[ $# -ge 4 ] && [ -n $4 ] && add_prefix="add_prefix=\"$4\"," && add_prefix_path="$4/"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@meteorcloudy meteorcloudy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you!

@meteorcloudy meteorcloudy added awaiting-PR-merge and removed awaiting-review PR is awaiting review from an assigned reviewer labels May 5, 2026
@copybara-service copybara-service Bot closed this in accb2de May 5, 2026
tamird added a commit to tamird/bazel that referenced this pull request Jul 29, 2026
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.
tamird added a commit to tamird/bazel that referenced this pull request Jul 29, 2026
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.
copybara-service Bot pushed a commit that referenced this pull request Aug 24, 2026
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
deepalak56 pushed a commit to deepalak56/bazel_deepa that referenced this pull request Dec 1, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Bzlmod Bzlmod-specific PRs, issues, and feature requests team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can git.bzl support checking out repo to another sub directory

3 participants