aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Klaus Aehlig <aehlig@google.com>2018-07-03 06:07:53 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-07-03 06:09:41 -0700
commit6af6c2df574e2ad36cf77f8a82c03cf7bb8ed087 (patch)
tree58c91bc700c2cd65fe337ffa3e0b03ba5c4602d0
parentf37b2bfa61b65d0b911a47cd1d9d1f7e1510c096 (diff)
git_repository: support branch as well
Now that git_repository returns the commit actually checked out (and we have means of recording it), support following an active branch of a git repository. Change-Id: I6e152c59b694bbf562b345ee88282a8c9bd58e4c PiperOrigin-RevId: 203110735
-rwxr-xr-xsrc/test/shell/bazel/skylark_git_repository_test.sh4
-rwxr-xr-xsrc/test/shell/bazel/workspace_resolved_test.sh72
-rw-r--r--tools/build_defs/repo/git.bzl25
3 files changed, 92 insertions, 9 deletions
diff --git a/src/test/shell/bazel/skylark_git_repository_test.sh b/src/test/shell/bazel/skylark_git_repository_test.sh
index 3509a9bc43..4fb422bc17 100755
--- a/src/test/shell/bazel/skylark_git_repository_test.sh
+++ b/src/test/shell/bazel/skylark_git_repository_test.sh
@@ -501,7 +501,7 @@ EOF
bazel fetch //planets:planet-info >& $TEST_log \
|| echo "Expect run to fail."
- expect_log "Exactly one of commit and tag must be provided"
+ expect_log "Exactly one of commit"
}
# Verifies that rule fails if neither tag or commit are set.
@@ -528,7 +528,7 @@ EOF
bazel fetch //planets:planet-info >& $TEST_log \
|| echo "Expect run to fail."
- expect_log "Exactly one of commit and tag must be provided"
+ expect_log "Exactly one of commit"
}
# Verifies that if a non-existent subdirectory is supplied, then strip_prefix
diff --git a/src/test/shell/bazel/workspace_resolved_test.sh b/src/test/shell/bazel/workspace_resolved_test.sh
index 624a90f7fc..e3a18f21c2 100755
--- a/src/test/shell/bazel/workspace_resolved_test.sh
+++ b/src/test/shell/bazel/workspace_resolved_test.sh
@@ -166,6 +166,78 @@ EOF
&& fail "not taking the frozen commit" || :
}
+test_git_follow_branch() {
+ EXTREPODIR=`pwd`
+ export GIT_CONFIG_NOSYSTEM=YES
+
+ mkdir extgit
+ (cd extgit && git init \
+ && git config user.email 'me@example.com' \
+ && git config user.name 'E X Ample' )
+ echo Hello World > extgit/hello.txt
+ (cd extgit
+ git add .
+ git commit --author="A U Thor <author@example.com>" -m 'initial commit')
+ # Check out the external git repository at the given branch, and record
+ # the return value of the git rule.
+ mkdir branchcheckout
+ cd branchcheckout
+ cat > WORKSPACE <<EOF
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
+new_git_repository(
+ name="ext",
+ remote="file://${EXTREPODIR}/extgit/.git",
+ branch="master",
+ build_file_content="exports_files([\"hello.txt\"])",
+)
+EOF
+ bazel sync --experimental_repository_resolved_file=../repo.bzl
+ # some of the file systems on our test machines are really slow to
+ # notice the creation of a file---even after the call to sync(1).
+ bazel shutdown; sync; sleep 10
+
+ cd ..
+ echo; cat repo.bzl; echo
+
+ # Now add an additional commit to the upstream repository
+ echo CHANGED > extgit/hello.txt
+ (cd extgit
+ git add .
+ git commit --author="A U Thor <author@example.com>" -m 'change hello.txt')
+
+ # Verify that the recorded resolved information is what we expect. In
+ # particular, verify that we don't get the new upstream commit.
+ mkdir analysisrepo
+ cd analysisrepo
+ cp ../repo.bzl .
+ cat > workspace.bzl <<'EOF'
+load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository")
+load("//:repo.bzl", "resolved")
+
+def repo():
+ for entry in resolved:
+ if entry["original_attributes"]["name"] == "ext":
+ new_git_repository(**(entry["repositories"][0]["attributes"]))
+EOF
+ cat > WORKSPACE <<'EOF'
+load("//:workspace.bzl", "repo")
+repo()
+EOF
+ cat > BUILD <<'EOF'
+genrule(
+ name = "out",
+ outs = ["out.txt"],
+ srcs = ["@ext//:hello.txt"],
+ cmd = "cp $< $@",
+)
+EOF
+ bazel build //:out
+ grep "Hello World" `bazel info bazel-genfiles`/out.txt \
+ || fail "ext not taken at the right commit"
+ grep "CHANGED" `bazel info bazel-genfiles`/out.txt \
+ && fail "not taking the frozen commit" || :
+}
+
test_sync_calls_all() {
mkdir sync_calls_all && cd sync_calls_all
diff --git a/tools/build_defs/repo/git.bzl b/tools/build_defs/repo/git.bzl
index 8b283cf9b0..2616ff6814 100644
--- a/tools/build_defs/repo/git.bzl
+++ b/tools/build_defs/repo/git.bzl
@@ -16,15 +16,19 @@
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch", "workspace_and_buildfile")
def _clone_or_update(ctx):
- 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")
+ if ((not ctx.attr.tag and not ctx.attr.commit and not ctx.attr.branch) or
+ (ctx.attr.tag and ctx.attr.commit) or
+ (ctx.attr.tag and ctx.attr.branch) or
+ (ctx.attr.commit and ctx.attr.branch)):
+ fail("Exactly one of commit, tag, or branch must be provided")
shallow = ""
if ctx.attr.commit:
ref = ctx.attr.commit
- else:
+ elif ctx.attr.tag:
ref = "tags/" + ctx.attr.tag
shallow = "--depth=1"
+ else:
+ ref = ctx.attr.branch
directory = str(ctx.path("."))
if ctx.attr.strip_prefix:
directory = directory + "-tmp"
@@ -107,9 +111,11 @@ def _update_commit(orig, keys, override):
result["name"] = orig.name
result.update(override)
- # remove tag if we found the actual commit
+ # if we found the actual commit, remove all other means of specifying it,
+ # like tag or branch.
if "commit" in result:
result.pop("tag", None)
+ result.pop("branch", None)
return result
_common_attrs = {
@@ -117,6 +123,7 @@ _common_attrs = {
"commit": attr.string(default = ""),
"shallow_since": attr.string(default = ""),
"tag": attr.string(default = ""),
+ "branch": attr.string(default = ""),
"init_submodules": attr.bool(default = False),
"verbose": attr.bool(default = False),
"strip_prefix": attr.string(default = ""),
@@ -181,10 +188,12 @@ Args:
Either `workspace_file` or `workspace_file_content` can be specified, or
neither, but not both.
+ branch: branch in the remote repository to checked out
+
tag: tag in the remote repository to checked out
commit: specific commit to be checked out
- Either tag or commit must be specified.
+ Precisely one of branch, tag, or commit must be specified.
shallow_since: an optional date, not after the specified commit; the
argument is not allowed if a tag is specified (which allows cloning
@@ -225,10 +234,12 @@ Args:
remote: The URI of the remote Git repository.
+ branch: branch in the remote repository to checked out
+
tag: tag in the remote repository to checked out
commit: specific commit to be checked out
- Either tag or commit must be specified.
+ Precisely one of branch, tag, or commit must be specified.
shallow_since: an optional date in the form YYYY-MM-DD, not after
the specified commit; the argument is not allowed if a tag is specified