aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
authorGravatar Ed Baunton <edbaunton@gmail.com>2018-01-10 02:23:11 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-01-10 02:25:27 -0800
commit512b9b9b353bcabc436a87329fadd449c684c44b (patch)
tree505864869094971e3ba16168664c09c75c6622fc /tools
parent950c2a2d5bb4886dcec94caf601c5fb10f7c3f3d (diff)
Add support for strip_prefix to {new_}git_repository skylark rules
Continuation of https://github.com/bazelbuild/bazel/pull/4356 An approach at supporting strip_prefix with the git skylark rules. This approach unfortunately uses symlinks since you cannot clone a subset of a git repository. It creates a tmp directory which is the 'real' clone and then provides a link in place of the expected location of the repository to the path of the required prefix. Behaviour is only changed if a strip_prefix is provided. Closes #4368. PiperOrigin-RevId: 181438640
Diffstat (limited to 'tools')
-rw-r--r--tools/build_defs/repo/git.bzl34
1 files changed, 26 insertions, 8 deletions
diff --git a/tools/build_defs/repo/git.bzl b/tools/build_defs/repo/git.bzl
index 36eb5fbe16..87bae53f1b 100644
--- a/tools/build_defs/repo/git.bzl
+++ b/tools/build_defs/repo/git.bzl
@@ -15,7 +15,8 @@
def _clone_or_update(ctx):
if (ctx.attr.verbose):
- print('git.bzl: Cloning or updating repository %s' % ctx.name)
+ print('git.bzl: Cloning or updating repository %s using strip_prefix of [%s]' %
+ (ctx.name, ctx.attr.strip_prefix if ctx.attr.strip_prefix else 'None'))
if ((not ctx.attr.tag and not ctx.attr.commit) or
(ctx.attr.tag and ctx.attr.commit)):
fail('Exactly one of commit and tag must be provided')
@@ -23,32 +24,44 @@ def _clone_or_update(ctx):
ref = ctx.attr.commit
else:
ref = 'tags/' + ctx.attr.tag
+ directory=str(ctx.path('.'))
+ if ctx.attr.strip_prefix:
+ directory = directory + "-tmp"
st = ctx.execute(['bash', '-c', """
set -ex
( cd {working_dir} &&
- if ! ( cd '{dir}' && [[ "$(git rev-parse --git-dir)" == '.git' ]] ) >/dev/null 2>&1; then
- rm -rf '{dir}'
- git clone --depth=1 '{remote}' '{dir}'
+ if ! ( cd '{dir_link}' && [[ "$(git rev-parse --git-dir)" == '.git' ]] ) >/dev/null 2>&1; then
+ rm -rf '{directory}' '{dir_link}'
+ git clone --depth=1 '{remote}' '{directory}'
fi
- cd '{dir}'
+ cd '{directory}'
git reset --hard {ref} || (git fetch --depth=1 origin {ref}:{ref} && git reset --hard {ref})
git clean -xdf )
""".format(
working_dir=ctx.path('.').dirname,
- dir=ctx.path('.'),
+ dir_link=ctx.path('.'),
+ directory=directory,
remote=ctx.attr.remote,
ref=ref,
)])
+
if st.return_code:
fail('error cloning %s:\n%s' % (ctx.name, st.stderr))
+
+ 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.symlink(dest_link, ctx.path('.'))
if ctx.attr.init_submodules:
st = ctx.execute(['bash', '-c', """
set -ex
-( cd '{dir}'
+( cd '{directory}'
git submodule update --init --checkout --force )
""".format(
- dir=ctx.path('.'),
+ directory=ctx.path('.'),
)])
if st.return_code:
fail('error updating submodules %s:\n%s' % (ctx.name, st.stderr))
@@ -75,6 +88,7 @@ _common_attrs = {
'tag': attr.string(default=''),
'init_submodules': attr.bool(default=False),
'verbose': attr.bool(default=False),
+ 'strip_prefix': attr.string(default='')
}
@@ -107,6 +121,8 @@ Args:
init_submodules: Whether to clone submodules in the repository.
remote: The URI of the remote Git repository.
+
+ strip_prefix: A directory prefix to strip from the extracted files.
"""
git_repository = repository_rule(
@@ -124,4 +140,6 @@ Args:
init_submodules: Whether to clone submodules in the repository.
remote: The URI of the remote Git repository.
+
+ strip_prefix: A directory prefix to strip from the extracted files.
"""