Skip to content

Commit

Permalink
Fix using load statement on git_repository with strip_prefix
Browse files Browse the repository at this point in the history
Prevent git_repository from creating directory outside of its repository root while implementing strip_prefix.

Fixes #10062

Closes #12539.

PiperOrigin-RevId: 344216891
  • Loading branch information
meteorcloudy authored and copybara-github committed Nov 25, 2020
1 parent 873b256 commit 11fe399
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/test/shell/bazel/git_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ function set_up() {
cp "$(rlocation io_bazel/src/test/shell/bazel/testdata/pluto-repo.tar.gz)" $repos_dir
cp "$(rlocation io_bazel/src/test/shell/bazel/testdata/outer-planets-repo.tar.gz)" $repos_dir
cp "$(rlocation io_bazel/src/test/shell/bazel/testdata/refetch-repo.tar.gz)" $repos_dir
cp "$(rlocation io_bazel/src/test/shell/bazel/testdata/strip-prefix-repo.tar.gz)" $repos_dir
cd $repos_dir
tar zxf pluto-repo.tar.gz
tar zxf outer-planets-repo.tar.gz
tar zxf refetch-repo.tar.gz
tar zxf strip-prefix-repo.tar.gz
}

# Test cloning a Git repository using the git_repository rule.
Expand Down Expand Up @@ -481,4 +483,33 @@ EOF
expect_log "Exactly one of commit"
}

# Verifies that load statement works while using strip_prefix.
#
# This test uses the strip-prefix Git repository, which contains the
# following files:
#
# strip-prefix
# └── prefix-foo
# ├── BUILD
# ├── WORKSPACE
# └── defs.bzl
function test_git_repository_with_strip_prefix_for_load_statement() {
setup_error_test
local strip_prefix_repo_dir=$TEST_TMPDIR/repos/strip-prefix

cd $WORKSPACE_DIR
cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "foo",
remote = "$strip_prefix_repo_dir",
commit = "f8167a60de4460e89601724fb13b4fc505da3f3d",
strip_prefix = "prefix-foo",
)
load("@foo//:defs.bzl", "FOO")
EOF

bazel info >& $TEST_log || fail "Expect bazel info to succeed."
}

run_suite "git_repository tests"
1 change: 1 addition & 0 deletions src/test/shell/bazel/testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ filegroup(
"outer-planets-repo.tar.gz",
"pluto-repo.tar.gz",
"refetch-repo.tar.gz",
"strip-prefix-repo.tar.gz",
],
visibility = ["//src/test/shell/bazel:__pkg__"],
)
Expand Down
4 changes: 3 additions & 1 deletion src/test/shell/bazel/testdata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ workspace rules:
* outer-planets-repo.tar.gz
* pluto-repo.tar.gz
* refetch-repo.tar.gz
* strip-prefix.tar.gz

For reference, the following files contain the output of `git log -p --decorate`
for the two repositories:
for the four repositories:

* outer-planets.git_log
* pluto.git_log
* refetch.git_log
* strip-prefix.git_log

These files were created by manually creating a git repository and tarring up
the result using `tar -zcvf`.
Binary file not shown.
19 changes: 19 additions & 0 deletions src/test/shell/bazel/testdata/strip-prefix.git_log
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
commit f8167a60de4460e89601724fb13b4fc505da3f3d (HEAD -> main)
Author: Yun Peng <[email protected]>
Date: Mon Nov 23 17:31:04 2020 +0100

First commit

diff --git a/prefix-foo/BUILD b/prefix-foo/BUILD
new file mode 100644
index 0000000..e69de29
diff --git a/prefix-foo/WORKSPACE b/prefix-foo/WORKSPACE
new file mode 100644
index 0000000..e69de29
diff --git a/prefix-foo/defs.bzl b/prefix-foo/defs.bzl
new file mode 100644
index 0000000..3563ad1
--- /dev/null
+++ b/prefix-foo/defs.bzl
@@ -0,0 +1 @@
+FOO=123
6 changes: 3 additions & 3 deletions tools/build_defs/repo/git.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ def _clone_or_update(ctx):
root = ctx.path(".")
directory = str(root)
if ctx.attr.strip_prefix:
directory = directory + "-tmp"
directory = root.get_child(".tmp_git_root")

git_ = git_repo(ctx, directory)

if ctx.attr.strip_prefix:
dest_link = "{}/{}".format(directory, ctx.attr.strip_prefix)
if not ctx.path(dest_link).exists:
fail("strip_prefix at {} does not exist in repo".format(ctx.attr.strip_prefix))
ctx.delete(root)
ctx.symlink(dest_link, root)
for item in ctx.path(dest_link).readdir():
ctx.symlink(item, root.get_child(item.basename))

return {"commit": git_.commit, "shallow_since": git_.shallow_since}

Expand Down

0 comments on commit 11fe399

Please sign in to comment.