From 5d6a7e3ad7876b000cc46de65322f6d17eee4234 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Thu, 1 Aug 2024 17:19:32 +0100 Subject: [PATCH 1/2] feat: upgrade to `bazel-git@1.0.0-beta.9` Brings `archive` command. --- MODULE.bazel | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/MODULE.bazel b/MODULE.bazel index 2decb76..351e428 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -24,15 +24,15 @@ download_file = use_repo_rule("@download_utils//download/file:defs.bzl", "downlo "entrypoint": "git", }, output = binary, - urls = ["https://gitlab.arm.com/api/v4/projects/bazel%2Fgit/packages/generic/release/1.0.0-alpha.8/{}".format(binary)], + urls = ["https://gitlab.arm.com/api/v4/projects/bazel%2Fgit/packages/generic/release/1.0.0-alpha.9/{}".format(binary)], ) for binary, integrity in ( - ("amd64-linux-bazel-git", "sha256-krzgELhKTt+I/Wg6Vi9T+gfF1vQnRHtx61tlNmXgUAw="), - ("arm64-linux-bazel-git", "sha256-JgRhWkcfh72AaR0aBV/M+Bf8CL/cYrtzwcObkqx4hEs="), - ("amd64-darwin-bazel-git", "sha256-6audwdUyig0itmhkawBROxLdFYMog6nCkjFhPZSOTFg="), - ("arm64-darwin-bazel-git", "sha256-nb3pJ+ZD8xMBjUCgJ5WP52ZqtKhcuUh1kDXvHnrqBL0="), - ("amd64-windows-bazel-git.exe", "sha256-MI9lEjEg0kKbJhMidX9nbQY/xEmVdQ2KVnsyrR7ifbY="), - ("arm64-windows-bazel-git.exe", "sha256-rPFzGUiXlO2SHnw5k4/CR+sFJQt3xptUOAKIlVuh24Q="), + ("amd64-linux-bazel-git", "sha256-fWAiKdC+wreO/Rm5+qlDDJXBDd16QprJsJo1/RNv2SU="), + ("arm64-linux-bazel-git", "sha256-kXscEZO2t0kb4628WgFtyJEG9ldhfmmxESeftooHtdA="), + ("amd64-darwin-bazel-git", "sha256-aI/sGwYgbEc3oOvGCk/ZsQ0orwfxzqC6PyIPBdOQsgg="), + ("arm64-darwin-bazel-git", "sha256-8Y3IRacAg1nmL1/FIbLVPcoM8/J3zrcPzS8NKR8eai8="), + ("amd64-windows-bazel-git.exe", "sha256-ZrpJMlSyG9ZES6OfI4Gd+1SyvL3P5meCX3vvve4isf4="), + ("arm64-windows-bazel-git.exe", "sha256-ks6HpcSGN4QTjLLiXWUnWxIDO4hrmEPB1a1jvmllagM="), ) ] -- GitLab From 0b386ee9f38cac8c9d83e83ca38ab8f88ee27b86 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Thu, 1 Aug 2024 18:42:00 +0100 Subject: [PATCH 2/2] feat: add `git_archive` rule --- MODULE.bazel | 3 +- MODULE.bazel.lock | 12 ++-- e2e/MODULE.bazel | 6 +- e2e/MODULE.bazel.lock | 11 +-- e2e/github-git-git/BUILD.bazel | 36 ++++++++++ git/archive/BUILD.bazel | 8 +++ git/archive/compress/BUILD.bazel | 29 ++++++++ git/archive/defs.bzl | 5 ++ git/archive/posix.tmpl.sh | 84 ++++++++++++++++++++++ git/archive/rule.bzl | 113 ++++++++++++++++++++++++++++++ git/fetch/module/BUILD.tmpl.bazel | 40 +++++++++++ lib/fetch.bzl | 2 +- 12 files changed, 336 insertions(+), 13 deletions(-) create mode 100644 git/archive/BUILD.bazel create mode 100644 git/archive/compress/BUILD.bazel create mode 100644 git/archive/defs.bzl create mode 100644 git/archive/posix.tmpl.sh create mode 100644 git/archive/rule.bzl diff --git a/MODULE.bazel b/MODULE.bazel index 351e428..408767a 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -7,9 +7,10 @@ module( ) bazel_dep(name = "bazel_skylib", version = "1.5.0") -bazel_dep(name = "toolchain_utils", version = "1.0.0-beta.11") +bazel_dep(name = "toolchain_utils", version = "1.0.0-beta.13") bazel_dep(name = "download_utils", version = "1.0.0-beta.2") bazel_dep(name = "rules_coreutils", version = "1.0.0-beta.4") +bazel_dep(name = "ape", version = "1.0.0-beta.12") download_file = use_repo_rule("@download_utils//download/file:defs.bzl", "download_file") diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 236363b..0ff9ccb 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -5,8 +5,9 @@ "https://bcr.bazel.build/modules/abseil-cpp/20210324.2/MODULE.bazel": "7cd0312e064fde87c8d1cd79ba06c876bd23630c83466e9500321be55c96ace2", "https://bcr.bazel.build/modules/abseil-cpp/20211102.0/MODULE.bazel": "70390338f7a5106231d20620712f7cccb659cd0e9d073d1991c038eb9fc57589", "https://bcr.bazel.build/modules/abseil-cpp/20211102.0/source.json": "7e3a9adf473e9af076ae485ed649d5641ad50ec5c11718103f34de03170d94ad", + "https://bcr.bazel.build/modules/ape/1.0.0-beta.12/MODULE.bazel": "e75be27b37a2f7cf83dbeb497aeac93f8da9aa55fc2b35a9a1cb46db9b7f8885", + "https://bcr.bazel.build/modules/ape/1.0.0-beta.12/source.json": "4b9e00b30d41ff209d7deddba1fbb4538cdfc7a54b4e07e607134ccc147e6a29", "https://bcr.bazel.build/modules/ape/1.0.0-beta.6/MODULE.bazel": "247bcdbc358206c37ce8bf1c850115002c6560570d96f9ac8a79dd810b18ea85", - "https://bcr.bazel.build/modules/ape/1.0.0-beta.6/source.json": "a2dda258a2985f703b359d4e287863489314078675e77c4afbf57122324d7c8a", "https://bcr.bazel.build/modules/apple_support/1.5.0/MODULE.bazel": "50341a62efbc483e8a2a6aec30994a58749bd7b885e18dd96aa8c33031e558ef", "https://bcr.bazel.build/modules/apple_support/1.5.0/source.json": "eb98a7627c0bc486b57f598ad8da50f6625d974c8f723e9ea71bd39f709c9862", "https://bcr.bazel.build/modules/bazel_features/1.11.0/MODULE.bazel": "f9382337dd5a474c3b7d334c2f83e50b6eaedc284253334cf823044a26de03e8", @@ -25,12 +26,13 @@ "https://bcr.bazel.build/modules/download_utils/1.0.0-beta.2/source.json": "0ab7ebbc57f39a7fe96190e01fe9773482bc4e3d465e9cd9b239bb44ad57791d", "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", "https://bcr.bazel.build/modules/googletest/1.11.0/source.json": "c73d9ef4268c91bd0c1cd88f1f9dfa08e814b1dbe89b5f594a9f08ba0244d206", + "https://bcr.bazel.build/modules/platforms/0.0.10/MODULE.bazel": "8cb8efaf200bdeb2150d93e162c40f388529a25852b332cec879373771e48ed5", + "https://bcr.bazel.build/modules/platforms/0.0.10/source.json": "f22828ff4cf021a6b577f1bf6341cb9dcd7965092a439f64fc1bb3b7a5ae4bd5", "https://bcr.bazel.build/modules/platforms/0.0.4/MODULE.bazel": "9b328e31ee156f53f3c416a64f8491f7eb731742655a47c9eec4703a71644aee", "https://bcr.bazel.build/modules/platforms/0.0.5/MODULE.bazel": "5733b54ea419d5eaf7997054bb55f6a1d0b5ff8aedf0176fef9eea44f3acda37", "https://bcr.bazel.build/modules/platforms/0.0.6/MODULE.bazel": "ad6eeef431dc52aefd2d77ed20a4b353f8ebf0f4ecdd26a807d2da5aa8cd0615", "https://bcr.bazel.build/modules/platforms/0.0.7/MODULE.bazel": "72fd4a0ede9ee5c021f6a8dd92b503e089f46c227ba2813ff183b71616034814", "https://bcr.bazel.build/modules/platforms/0.0.9/MODULE.bazel": "4a87a60c927b56ddd67db50c89acaa62f4ce2a1d2149ccb63ffd871d5ce29ebc", - "https://bcr.bazel.build/modules/platforms/0.0.9/source.json": "cd74d854bf16a9e002fb2ca7b1a421f4403cda29f824a765acd3a8c56f8d43e6", "https://bcr.bazel.build/modules/protobuf/21.7/MODULE.bazel": "a5a29bb89544f9b97edce05642fac225a808b5b7be74038ea3640fae2f8e66a7", "https://bcr.bazel.build/modules/protobuf/21.7/source.json": "bbe500720421e582ff2d18b0802464205138c06056f443184de39fbb8187b09b", "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", @@ -61,8 +63,8 @@ "https://bcr.bazel.build/modules/rules_python/0.4.0/MODULE.bazel": "9208ee05fd48bf09ac60ed269791cf17fb343db56c8226a720fbb1cdf467166c", "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", "https://bcr.bazel.build/modules/stardoc/0.5.1/source.json": "a96f95e02123320aa015b956f29c00cb818fa891ef823d55148e1a362caacf29", - "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.11/MODULE.bazel": "c6e3b5829ac9c0d096d65e1bd06616e42e859ba4ea10c8762b5de4f316bc983b", - "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.11/source.json": "421c4e38637937513784e1cc3776caffac21f1998faac48467ab15a5390e9f59", + "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.13/MODULE.bazel": "8fa38d6dfa905bea519caca92bfb5571dd2d70b45e307674e33692c7a7c16ace", + "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.13/source.json": "ca58149525f45ec454b0f12c09efaa6edef163784631f415b65322650307404d", "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.9/MODULE.bazel": "9a8edfa6905229b899225489cbd6db36f073d9455c77238bd6ae6c52da5256d8", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/source.json": "f1ef7d3f9e0e26d4b23d1c39b5f5de71f584dd7d1b4ef83d9bbba6ec7a6a6459", @@ -104,7 +106,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", + "usagesDigest": "V1R2Y2oMxKNfx2WCWpSCaUV1WefW1o8HZGm3v1vHgY4=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/e2e/MODULE.bazel b/e2e/MODULE.bazel index acf6560..f9dafb8 100644 --- a/e2e/MODULE.bazel +++ b/e2e/MODULE.bazel @@ -5,9 +5,11 @@ module( ], ) -bazel_dep(name = "rules_git", version = "0.0.0") +bazel_dep(name = "rules_git") +bazel_dep(name = "bazel_skylib", version = "1.5.0") bazel_dep(name = "rules_diff", version = "1.0.0-beta.3") -bazel_dep(name = "toolchain_utils", version = "1.0.0-beta.11", dev_dependency=True) + +bazel_dep(name = "toolchain_utils", version = "1.0.0-beta.11", dev_dependency = True) local_path_override( module_name = "rules_git", diff --git a/e2e/MODULE.bazel.lock b/e2e/MODULE.bazel.lock index 4a693ae..a1fd841 100644 --- a/e2e/MODULE.bazel.lock +++ b/e2e/MODULE.bazel.lock @@ -5,8 +5,9 @@ "https://bcr.bazel.build/modules/abseil-cpp/20210324.2/MODULE.bazel": "7cd0312e064fde87c8d1cd79ba06c876bd23630c83466e9500321be55c96ace2", "https://bcr.bazel.build/modules/abseil-cpp/20211102.0/MODULE.bazel": "70390338f7a5106231d20620712f7cccb659cd0e9d073d1991c038eb9fc57589", "https://bcr.bazel.build/modules/abseil-cpp/20211102.0/source.json": "7e3a9adf473e9af076ae485ed649d5641ad50ec5c11718103f34de03170d94ad", + "https://bcr.bazel.build/modules/ape/1.0.0-beta.12/MODULE.bazel": "e75be27b37a2f7cf83dbeb497aeac93f8da9aa55fc2b35a9a1cb46db9b7f8885", + "https://bcr.bazel.build/modules/ape/1.0.0-beta.12/source.json": "4b9e00b30d41ff209d7deddba1fbb4538cdfc7a54b4e07e607134ccc147e6a29", "https://bcr.bazel.build/modules/ape/1.0.0-beta.6/MODULE.bazel": "247bcdbc358206c37ce8bf1c850115002c6560570d96f9ac8a79dd810b18ea85", - "https://bcr.bazel.build/modules/ape/1.0.0-beta.6/source.json": "a2dda258a2985f703b359d4e287863489314078675e77c4afbf57122324d7c8a", "https://bcr.bazel.build/modules/apple_support/1.5.0/MODULE.bazel": "50341a62efbc483e8a2a6aec30994a58749bd7b885e18dd96aa8c33031e558ef", "https://bcr.bazel.build/modules/apple_support/1.5.0/source.json": "eb98a7627c0bc486b57f598ad8da50f6625d974c8f723e9ea71bd39f709c9862", "https://bcr.bazel.build/modules/bazel_features/1.11.0/MODULE.bazel": "f9382337dd5a474c3b7d334c2f83e50b6eaedc284253334cf823044a26de03e8", @@ -25,12 +26,13 @@ "https://bcr.bazel.build/modules/download_utils/1.0.0-beta.2/source.json": "0ab7ebbc57f39a7fe96190e01fe9773482bc4e3d465e9cd9b239bb44ad57791d", "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", "https://bcr.bazel.build/modules/googletest/1.11.0/source.json": "c73d9ef4268c91bd0c1cd88f1f9dfa08e814b1dbe89b5f594a9f08ba0244d206", + "https://bcr.bazel.build/modules/platforms/0.0.10/MODULE.bazel": "8cb8efaf200bdeb2150d93e162c40f388529a25852b332cec879373771e48ed5", + "https://bcr.bazel.build/modules/platforms/0.0.10/source.json": "f22828ff4cf021a6b577f1bf6341cb9dcd7965092a439f64fc1bb3b7a5ae4bd5", "https://bcr.bazel.build/modules/platforms/0.0.4/MODULE.bazel": "9b328e31ee156f53f3c416a64f8491f7eb731742655a47c9eec4703a71644aee", "https://bcr.bazel.build/modules/platforms/0.0.5/MODULE.bazel": "5733b54ea419d5eaf7997054bb55f6a1d0b5ff8aedf0176fef9eea44f3acda37", "https://bcr.bazel.build/modules/platforms/0.0.6/MODULE.bazel": "ad6eeef431dc52aefd2d77ed20a4b353f8ebf0f4ecdd26a807d2da5aa8cd0615", "https://bcr.bazel.build/modules/platforms/0.0.7/MODULE.bazel": "72fd4a0ede9ee5c021f6a8dd92b503e089f46c227ba2813ff183b71616034814", "https://bcr.bazel.build/modules/platforms/0.0.9/MODULE.bazel": "4a87a60c927b56ddd67db50c89acaa62f4ce2a1d2149ccb63ffd871d5ce29ebc", - "https://bcr.bazel.build/modules/platforms/0.0.9/source.json": "cd74d854bf16a9e002fb2ca7b1a421f4403cda29f824a765acd3a8c56f8d43e6", "https://bcr.bazel.build/modules/protobuf/21.7/MODULE.bazel": "a5a29bb89544f9b97edce05642fac225a808b5b7be74038ea3640fae2f8e66a7", "https://bcr.bazel.build/modules/protobuf/21.7/source.json": "bbe500720421e582ff2d18b0802464205138c06056f443184de39fbb8187b09b", "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", @@ -64,7 +66,8 @@ "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", "https://bcr.bazel.build/modules/stardoc/0.5.1/source.json": "a96f95e02123320aa015b956f29c00cb818fa891ef823d55148e1a362caacf29", "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.11/MODULE.bazel": "c6e3b5829ac9c0d096d65e1bd06616e42e859ba4ea10c8762b5de4f316bc983b", - "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.11/source.json": "421c4e38637937513784e1cc3776caffac21f1998faac48467ab15a5390e9f59", + "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.13/MODULE.bazel": "8fa38d6dfa905bea519caca92bfb5571dd2d70b45e307674e33692c7a7c16ace", + "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.13/source.json": "ca58149525f45ec454b0f12c09efaa6edef163784631f415b65322650307404d", "https://bcr.bazel.build/modules/toolchain_utils/1.0.0-beta.9/MODULE.bazel": "9a8edfa6905229b899225489cbd6db36f073d9455c77238bd6ae6c52da5256d8", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/source.json": "f1ef7d3f9e0e26d4b23d1c39b5f5de71f584dd7d1b4ef83d9bbba6ec7a6a6459", @@ -106,7 +109,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", + "usagesDigest": "V1R2Y2oMxKNfx2WCWpSCaUV1WefW1o8HZGm3v1vHgY4=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/e2e/github-git-git/BUILD.bazel b/e2e/github-git-git/BUILD.bazel index ff2b13a..897aacf 100644 --- a/e2e/github-git-git/BUILD.bazel +++ b/e2e/github-git-git/BUILD.bazel @@ -1,3 +1,4 @@ +load("@bazel_skylib//rules:build_test.bzl", "build_test") load("@rules_diff//diff/directory/test:defs.bzl", "diff_directory_test") diff_directory_test( @@ -6,3 +7,38 @@ diff_directory_test( a = "@github-git-git-2.43.0//:checkout", b = "@github-git-git-c089584ac8dedc3aa7c2c404839bc098050298a2//:checkout", ) + +build_test( + name = "archive.tar", + targets = [ + "@github-git-git-c089584ac8dedc3aa7c2c404839bc098050298a2//:archive.tar", + ], +) + +build_test( + name = "archive.tar.gz", + targets = [ + "@github-git-git-c089584ac8dedc3aa7c2c404839bc098050298a2//:archive.tar.gz", + ], +) + +build_test( + name = "archive.tar.bz2", + targets = [ + "@github-git-git-c089584ac8dedc3aa7c2c404839bc098050298a2//:archive.tar.bz2", + ], +) + +build_test( + name = "archive.tar.xz", + targets = [ + "@github-git-git-c089584ac8dedc3aa7c2c404839bc098050298a2//:archive.tar.xz", + ], +) + +build_test( + name = "archive.tar.zst", + targets = [ + "@github-git-git-c089584ac8dedc3aa7c2c404839bc098050298a2//:archive.tar.zst", + ], +) diff --git a/git/archive/BUILD.bazel b/git/archive/BUILD.bazel new file mode 100644 index 0000000..e330b50 --- /dev/null +++ b/git/archive/BUILD.bazel @@ -0,0 +1,8 @@ +alias( + name = "template", + actual = select({ + "@toolchain_utils//toolchain/constraint/os:windows": ":nt.tmpl.bat", + "//conditions:default": "posix.tmpl.sh", + }), + visibility = ["//visibility:public"], +) diff --git a/git/archive/compress/BUILD.bazel b/git/archive/compress/BUILD.bazel new file mode 100644 index 0000000..3685bf0 --- /dev/null +++ b/git/archive/compress/BUILD.bazel @@ -0,0 +1,29 @@ +alias( + name = "cat", + actual = "@ape//ape:cat", + visibility = ["//visibility:public"], +) + +alias( + name = "gzip", + actual = "@ape//ape:gzip", + visibility = ["//visibility:public"], +) + +alias( + name = "bzip2", + actual = "@ape//ape:bzip2", + visibility = ["//visibility:public"], +) + +alias( + name = "xz", + actual = "@ape//ape:xz", + visibility = ["//visibility:public"], +) + +alias( + name = "zstd", + actual = "@ape//ape:zstd", + visibility = ["//visibility:public"], +) diff --git a/git/archive/defs.bzl b/git/archive/defs.bzl new file mode 100644 index 0000000..a2de1ef --- /dev/null +++ b/git/archive/defs.bzl @@ -0,0 +1,5 @@ +load(":rule.bzl", _archive = "archive") + +visibility("public") + +git_archive = _archive diff --git a/git/archive/posix.tmpl.sh b/git/archive/posix.tmpl.sh new file mode 100644 index 0000000..1126e49 --- /dev/null +++ b/git/archive/posix.tmpl.sh @@ -0,0 +1,84 @@ +#! /usr/bin/env sh + +# Strict Shell +set -o errexit +set -o nounset + +# Bazel replacements +SORT="{{sort}}" +CP="{{cp}}" +CHMOD="{{chmod}}" +LN="{{ln}}" +MKDIR="{{mkdir}}" +COMPRESS="{{compress}}" +GIT="{{git}}" +readonly SORT CP CHMOD LN MKDIR COMPRESS GIT + +# Parse arguments +while test 0 -ne "${#}"; do + case "${1}" in + "--git-dir") + shift + GITDIR="${1?Must provide an argument for --git-dir}" + ;; + "archive") ;; + "--format") + shift + FORMAT="${1?Must provide an argument for --format}" + ;; + "--output") + shift + OUTPUT="${1?Must provide an argument for --output}" + shift + break + ;; + *) + printf >&2 'Error: unknown argument: %s\n' "${1}" + exit 2 + ;; + esac + shift +done +readonly GITDIR FORMAT OUTPUT +if test "${#}" -eq 0; then + echo >&2 "Must provide modules to archive" + exit 2 +fi + +# The `git` CLI has strict requirements for the `.git` directory[1] +# Specifically: +# * `HEAD` if a symlink _must_ point to a file in the `refs` directory +# * `HEAD` must be writable +# * `objects`/`refs _must_ exist +# [1]: https://github.com/git/git/blob/21306a098c3f174ad4c2a5cddb9069ee27a548b0/setup.c#L355 +DST="${OUTPUT}.working" +readonly DST +"${MKDIR}" -p "${DST}" +for SRC in "${GITDIR}/"*; do + case "${SRC}" in + *"/HEAD") + "${CP}" -p "${SRC}" "${DST}/${SRC#"${GITDIR}"/}" + "${CHMOD}" u+w "${DST}/${SRC#"${GITDIR}"/}" + ;; + *"/objects" | *"/refs") # TODO: can we symlink here? + "${CP}" -RLp "${SRC}" "${DST}/${SRC#"${GITDIR}"/}" + ;; + *) + "${LN}" -s "${SRC}" "${DST}/${SRC#"${GITDIR}"/}" + ;; + esac +done + +# Do the archive of the module +{ + (for MODULE in "${@}"; do echo "${MODULE}"; done) | + "${SORT}" -rt ":" | + while IFS=: read -r RELATIVE COMMIT; do + "${GIT}" \ + --git-dir "${DST}" \ + archive \ + --format="${FORMAT}" \ + --prefix="${RELATIVE}/" \ + "${COMMIT}" + done +} | "${COMPRESS}" >"${OUTPUT}" diff --git a/git/archive/rule.bzl b/git/archive/rule.bzl new file mode 100644 index 0000000..03cbc1f --- /dev/null +++ b/git/archive/rule.bzl @@ -0,0 +1,113 @@ +load("//git/modules:defs.bzl", "GitModulesInfo") + +visibility("//...") + +DOC = """Checks out a working tree from a fetched repository.""" + +ATTRS = { + "dir": attr.label( + doc = "`git` directory to checkout `commit`.", + mandatory = True, + ), + "module": attr.label( + doc = "The commit SHA to archive the `objects`.", + providers = [GitModulesInfo], + mandatory = True, + ), + "template": attr.label( + doc = "The script to template and run.", + allow_single_file = True, + default = ":template", + ), + "format": attr.string( + doc = "The format of the archive", + values = ["tar"], + default = "tar", + ), + "compress": attr.label( + doc = "A compression binary.", + default = "//git/archive/compress:cat", + executable = True, + cfg = "exec", + ), +} + +def _find_head(file): + if file.basename == "HEAD": + return file.dirname + return None + +def _module(module): + return "{}:{}".format(module.path, module.commit) + +def implementation(ctx): + sort = ctx.toolchains["@rules_coreutils//coreutils/toolchain/sort:type"] + chmod = ctx.toolchains["@rules_coreutils//coreutils/toolchain/chmod:type"] + cp = ctx.toolchains["@rules_coreutils//coreutils/toolchain/cp:type"] + ln = ctx.toolchains["@rules_coreutils//coreutils/toolchain/ln:type"] + mkdir = ctx.toolchains["@rules_coreutils//coreutils/toolchain/mkdir:type"] + git = ctx.toolchains["//git/toolchain/git:type"] + + rendered = ctx.actions.declare_file("{}.rendered.{}".format(ctx.label.name, ctx.file.template.extension)) + substitutions = ctx.actions.template_dict() + + # TODO: remove `.path` when `TemplateDict#add` supports `File` for Path Mapping + substitutions.add("{{sort}}", sort.executable.path) + substitutions.add("{{chmod}}", chmod.executable.path) + substitutions.add("{{cp}}", cp.executable.path) + substitutions.add("{{ln}}", ln.executable.path) + substitutions.add("{{mkdir}}", mkdir.executable.path) + substitutions.add("{{compress}}", ctx.executable.compress.path) + substitutions.add("{{git}}", git.executable.path) + ctx.actions.expand_template( + output = rendered, + template = ctx.file.template, + computed_substitutions = substitutions, + is_executable = True, + ) + + modules = ctx.attr.module[GitModulesInfo].modules + + archive = ctx.actions.declare_file("{}".format(ctx.label.name)) + + args = ctx.actions.args() + args.add_joined("--git-dir", ctx.files.dir, map_each = _find_head, join_with = ",", omit_if_empty = False) + args.add("archive") + args.add("--format").add(ctx.attr.format) + args.add("--output").add(archive) + args.add_all(modules, map_each = _module) + + ctx.actions.run( + outputs = [archive], + inputs = ctx.files.dir, + executable = rendered, + tools = [ + sort.run, + chmod.run, + cp.run, + ln.run, + mkdir.run, + git.run, + ctx.attr.compress.files_to_run, + ], + arguments = [args], + mnemonic = "GitArchive", + ) + + return DefaultInfo(files = depset([archive])) + +git_archive = rule( + doc = DOC, + attrs = ATTRS, + implementation = implementation, + toolchains = [ + "//git/toolchain/git:type", + "@rules_coreutils//coreutils/toolchain/sort:type", + "@rules_coreutils//coreutils/toolchain/chmod:type", + "@rules_coreutils//coreutils/toolchain/cp:type", + "@rules_coreutils//coreutils/toolchain/ln:type", + "@rules_coreutils//coreutils/toolchain/mkdir:type", + ], +) + +archive = git_archive diff --git a/git/fetch/module/BUILD.tmpl.bazel b/git/fetch/module/BUILD.tmpl.bazel index 1968cfc..560c484 100644 --- a/git/fetch/module/BUILD.tmpl.bazel +++ b/git/fetch/module/BUILD.tmpl.bazel @@ -1,5 +1,6 @@ load("@rules_git//git/module:defs.bzl", "git_module") load("@rules_git//git/checkout:defs.bzl", "git_checkout") +load("@rules_git//git/archive:defs.bzl", "git_archive") git_module( name = "{{name}}", @@ -23,3 +24,42 @@ git_checkout( module = ":{{name}}", visibility = ["//visibility:public"], ) + +git_archive( + name = "archive.tar", + dir = "//.git:dir", + module = ":{{name}}", + visibility = ["//visibility:public"], +) + +git_archive( + name = "archive.tar.gz", + compress = "@rules_git//git/archive/compress:gzip", + dir = "//.git:dir", + module = ":{{name}}", + visibility = ["//visibility:public"], +) + +git_archive( + name = "archive.tar.bz2", + compress = "@rules_git//git/archive/compress:bzip2", + dir = "//.git:dir", + module = ":{{name}}", + visibility = ["//visibility:public"], +) + +git_archive( + name = "archive.tar.xz", + compress = "@rules_git//git/archive/compress:xz", + dir = "//.git:dir", + module = ":{{name}}", + visibility = ["//visibility:public"], +) + +git_archive( + name = "archive.tar.zst", + compress = "@rules_git//git/archive/compress:zstd", + dir = "//.git:dir", + module = ":{{name}}", + visibility = ["//visibility:public"], +) diff --git a/lib/fetch.bzl b/lib/fetch.bzl index 65b7d7a..a6f31e3 100644 --- a/lib/fetch.bzl +++ b/lib/fetch.bzl @@ -2,7 +2,7 @@ load("@bazel_skylib//lib:structs.bzl", "structs") load(":execute.bzl", "execute") load(":submodules.bzl", "submodules") load(":init.bzl", "init", _ATTRS = "ATTRS") -load(":log.bzl", "log", "LEVEL") +load(":log.bzl", "LEVEL", "log") visibility("//git/...") -- GitLab