diff --git a/prelude/decls/git_rules.bzl b/prelude/decls/git_rules.bzl index 78b3aad5436ac..4a10c9d0c9b61 100644 --- a/prelude/decls/git_rules.bzl +++ b/prelude/decls/git_rules.bzl @@ -43,6 +43,12 @@ git_fetch = prelude_rule( "rev": attrs.string(doc = """ Commit hash. 40 hex digits for sha1, 64 hex digits for sha256. """), + "update_submodules": attrs.bool( + default = False, + doc = """ + Whether to initialize and update Git submodules after there worktree checkout. + """ + ), "sub_targets": attrs.list( attrs.string(), default = [], diff --git a/prelude/git/git_fetch.bzl b/prelude/git/git_fetch.bzl index 251cad7ab1254..c78b1a5a612a8 100644 --- a/prelude/git/git_fetch.bzl +++ b/prelude/git/git_fetch.bzl @@ -38,12 +38,18 @@ def git_fetch_impl(ctx: AnalysisContext) -> list[Provider]: short_path = "work-tree" work_tree = ctx.actions.declare_output(short_path, dir = True) + 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 object_format != None: cmd.append(cmd_args("--object-format=", object_format, delimiter = "")) diff --git a/prelude/git/tools/git_fetch.py b/prelude/git/tools/git_fetch.py index 4b566215707e4..0e44c12c7bfeb 100755 --- a/prelude/git/tools/git_fetch.py +++ b/prelude/git/tools/git_fetch.py @@ -7,6 +7,7 @@ # above-listed licenses. import argparse +import os import subprocess import sys import time @@ -51,6 +52,7 @@ class Args(NamedTuple): object_format: ObjectFormat repo: str rev: str + update_submodules: bool def arg_parse() -> Args: @@ -58,6 +60,7 @@ def arg_parse() -> Args: parser.add_argument("--git-dir", type=Path, required=True) parser.add_argument("--work-tree", type=Path, required=True) parser.add_argument("--object-format", type=ObjectFormat, required=False) + parser.add_argument("--update-submodules", action='store_true', required=False) parser.add_argument("--repo", type=str, required=True) parser.add_argument("--rev", type=str, required=True) return Args(**vars(parser.parse_args())) @@ -76,7 +79,7 @@ def main() -> None: args.work_tree.mkdir(exist_ok=True) - git = ["git", f"--git-dir={args.git_dir}", f"--work-tree={args.work_tree}"] + git = ["git", "--git-dir", args.git_dir, "--work-tree", args.work_tree] if args.object_format is None: object_format_args = [] else: @@ -97,6 +100,10 @@ def main() -> None: run([*git, "checkout", "FETCH_HEAD"], check=True) + if args.update_submodules: + git_in_worktree = ["git", "--git-dir", os.path.abspath(args.git_dir), "-C", args.work_tree] + run([*git_in_worktree, "submodule", "update", "--init"], check=True) + if __name__ == "__main__": main()