From 6af6c2df574e2ad36cf77f8a82c03cf7bb8ed087 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 3 Jul 2018 06:07:53 -0700 Subject: 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 --- tools/build_defs/repo/git.bzl | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'tools') 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 -- cgit v1.2.3