From 54eb1c89cf66ab882427c0d714f5c9a498687326 Mon Sep 17 00:00:00 2001 From: Enno Lohmeier Date: Thu, 2 Jul 2020 09:27:55 +0200 Subject: add `shallow` option to git source type --- README.md | 4 ++++ lib/types/populate.nix | 4 ++++ pkgs/populate/default.nix | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d6f921f..70ba3dc 100644 --- a/README.md +++ b/README.md @@ -242,6 +242,10 @@ Supported attributes: * `clean.exclude` - List of patterns that should be excluded from Git cleaning. +* `shallow` (optional) + boolean that controls whether only the requested commit ref. should be fetched + instead of the whole history, to save disk space and bandwith. Defaults to `false`. + ### `pass` diff --git a/lib/types/populate.nix b/lib/types/populate.nix index 4ac9b1f..894b8cc 100644 --- a/lib/types/populate.nix +++ b/lib/types/populate.nix @@ -140,6 +140,10 @@ url = lib.mkOption { type = lib.types.str; # TODO lib.types.git.url }; + shallow = lib.mkOption { + default = false; + type = lib.types.bool; + }; }; }; pass = lib.types.submodule { diff --git a/pkgs/populate/default.nix b/pkgs/populate/default.nix index 40c37e3..bac1432 100644 --- a/pkgs/populate/default.nix +++ b/pkgs/populate/default.nix @@ -53,7 +53,11 @@ let pop.git = target: source: runShell target /* sh */ '' set -efu if ! test -e ${quote target.path}; then - git clone --recurse-submodules ${quote source.url} ${quote target.path} + ${if source.shallow then /* sh */ '' + git init ${quote target.path} + '' else /* sh */ '' + git clone --recurse-submodules ${quote source.url} ${quote target.path} + ''} fi cd ${quote target.path} if ! url=$(git config remote.origin.url); then @@ -67,10 +71,18 @@ let if ! test "$(git log --format=%H -1)" = "$hash"; then ${if source.fetchAlways then /* sh */ '' - git fetch origin + ${if source.shallow then /* sh */ '' + git fetch --depth=1 origin "$hash" + '' else /* sh */ '' + git fetch origin + ''} '' else /* sh */ '' if ! git log -1 "$hash" >/dev/null 2>&1; then - git fetch origin + ${if source.shallow then /* sh */ '' + git fetch --depth=1 origin "$hash" + '' else /* sh */ '' + git fetch origin + ''} fi ''} git reset --hard "$hash" >&2 -- cgit v1.2.3