Skip to content

Instantly share code, notes, and snippets.

@be9
Created June 10, 2024 08:38
Show Gist options
  • Save be9/c661f242738a9ad5e8144dda89eb2042 to your computer and use it in GitHub Desktop.
Save be9/c661f242738a9ad5e8144dda89eb2042 to your computer and use it in GitHub Desktop.
Bazel rule to create a git repo dynamically
load("@aspect_bazel_lib//lib:copy_file.bzl", "COPY_FILE_TOOLCHAINS", "copy_file_action")
def _adhoc_git_repository_impl(ctx):
copied_files = []
for s in ctx.files.srcs:
if not s.is_source:
continue # fix if needed
f = ctx.actions.declare_file(ctx.label.name + "/" + s.path)
copy_file_action(ctx, s, f)
copied_files.append(f)
dotgit = ctx.actions.declare_directory(ctx.label.name + "/.git")
ctx.actions.run_shell(
inputs = copied_files,
outputs = [dotgit],
command = """cd {dir} &&
git init &&
git add . &&
git config user.email [email protected] &&
git config user.name testuser &&
git commit -m testcommit""".format(dir = dotgit.dirname),
)
# return DefaultInfo(files = depset(direct = copied_files + [dotgit]))
return DefaultInfo(files = depset(direct = [dotgit]))
adhoc_git_repository = rule(
implementation = _adhoc_git_repository_impl,
attrs = {
"srcs": attr.label_list(
allow_files = True,
),
},
toolchains = COPY_FILE_TOOLCHAINS,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment