cabal-buck2-0.6.0.0: buck2/prelude/git/git_fetch.bzl
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is dual-licensed under either the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree or the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree. You may select, at your option, one of the
# above-listed licenses.
_HEX_DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
def _is_hex(rev: str, *, length: int) -> bool:
if len(rev) != length:
return False
for digit in rev.elems():
if digit not in _HEX_DIGITS:
return False
return True
def git_fetch_impl(ctx: AnalysisContext) -> list[Provider]:
object_format = ctx.attrs.object_format
rev = ctx.attrs.rev
if object_format == None:
if not _is_hex(rev, length = 40):
fail("git_fetch's `rev` must be a 40-hex-digit commit hash: {}".format(rev))
elif object_format == "sha1":
if not _is_hex(rev, length = 40):
fail("git_fetch's `rev` must be a 40-hex-digit commit hash when the chosen object format is sha1: {}".format(rev))
elif object_format == "sha256":
if not _is_hex(rev, length = 64):
fail("git_fetch's `rev` must be a 64-hex-digit commit hash when the chosen object format is sha256: {}".format(rev))
else:
fail("Invalid git_fetch `object_format`: Must be one of sha1 or sha256: {}".format(object_format))
git_dir = ctx.actions.declare_output(".git", dir = True, has_content_based_path = False)
short_path = ctx.attrs.name.removesuffix(".git")
if not short_path:
short_path = "work-tree"
work_tree = ctx.actions.declare_output(short_path, dir = True, has_content_based_path = False)
if ctx.attrs.update_submodules:
update_submodules = cmd_args("--update-submodules")
else:
update_submodules = cmd_args()
cmd = [
ctx.attrs._git_fetch_tool[RunInfo],
cmd_args("--git-dir=", git_dir.as_output(), delimiter = ""),
cmd_args("--work-tree=", work_tree.as_output(), delimiter = ""),
cmd_args("--repo=", ctx.attrs.repo, delimiter = ""),
cmd_args("--rev=", rev, delimiter = ""),
update_submodules,
]
if ctx.attrs.git != None:
cmd.append(cmd_args("--git=", ctx.attrs.git, delimiter = ""))
if object_format != None:
cmd.append(cmd_args("--object-format=", object_format, delimiter = ""))
ctx.actions.run(
cmd,
category = "git_fetch",
local_only = True,
allow_cache_upload = ctx.attrs.allow_cache_upload,
)
sub_targets = {path: [DefaultInfo(default_output = work_tree.project(path))] for path in ctx.attrs.sub_targets}
# The repository itself, for consumers that need to serve the fetch as a local git remote
# rather than read the checked-out files. git refuses to track a path named ".git", so no
# requested work-tree projection can ever claim this key.
sub_targets[".git"] = [DefaultInfo(default_output = git_dir)]
return [
DefaultInfo(
default_output = work_tree,
sub_targets = sub_targets,
)
]