about summary refs log tree commit diff
diff options
context:
space:
mode:
authoraszlig <aszlig@redmoonstudios.org>2016-05-29 20:19:50 +0200
committeraszlig <aszlig@redmoonstudios.org>2016-05-29 20:19:50 +0200
commit576e94798ded05bb8eaf6e88c3fd1919eb14088d (patch)
tree0405296dca3c8ea935d7d32fc35ce72760d9ea4c
parent53e5da6c11b9fb00b460363e0ff670f5454c00e5 (diff)
pkgs: Add new script git-detach
It's a small helper tool which I specifically use for running NixOS
tests (especially the installer ones) that require <nixpkgs> to be
copied to the store.

What git-detach does is creating a temporary working directory which
only contains a trimmed-down (without untracked files and .git
directory) version of the current Git repository.

So in case of <nixpkgs> this is especially useful to keep down the
closure size whenever the working dir is going to be exported to the
store.

Signed-off-by: aszlig <aszlig@redmoonstudios.org>
-rw-r--r--pkgs/default.nix1
-rw-r--r--pkgs/git-detach/default.nix33
2 files changed, 34 insertions, 0 deletions
diff --git a/pkgs/default.nix b/pkgs/default.nix
index 674ae29d..074f1130 100644
--- a/pkgs/default.nix
+++ b/pkgs/default.nix
@@ -10,6 +10,7 @@ let
     axbo = callPackage ./axbo { };
     beehive = callPackage ./beehive { };
     blop = callPackage ./blop { };
+    git-detach = callPackage ./git-detach { };
     grandpa = callPackage ./grandpa { };
     greybird-xfce-theme = callPackage ./greybird-xfce-theme { };
     nixops = callPackage ./nixops { };
diff --git a/pkgs/git-detach/default.nix b/pkgs/git-detach/default.nix
new file mode 100644
index 00000000..fb20843e
--- /dev/null
+++ b/pkgs/git-detach/default.nix
@@ -0,0 +1,33 @@
+{ writeScriptBin, stdenv, git, coreutils, patch }:
+
+writeScriptBin "git-detach" ''
+  #!${stdenv.shell}
+
+  if [ $# -le 0 -o "$1" = "--help" -o "$1" = "-h" ]; then
+      echo "Usage: $0 COMMAND [ARGS...]" >&2
+      echo >&2
+      echo "Run COMMAND in a clean Git working directory" >&2
+      echo "without untracked files and .git directory." >&2
+      exit 1
+  fi
+
+  diffToHead="$("${git}/bin/git" diff HEAD)"
+
+  if tmpdir="$("${coreutils}/bin/mktemp" -d git-detach.XXXXXXXXXX)"; then
+    trap "rm -rf '${"\${tmpdir//\\'/\\'\\\\\\'\\'}"}'" EXIT
+    "${git}/bin/git" archive --format=tar HEAD | (
+      set -e
+      basedir="$tmpdir/$("${coreutils}/bin/basename" "$(pwd)")"
+      mkdir "$basedir"
+      cd "$basedir"
+      tar x
+      if [ -n "$diffToHead" ]; then
+        echo "$diffToHead" | "${patch}/bin/patch" -s -p1
+      fi
+      exec "$@"
+    )
+    exit $?
+  else
+    echo "Unable to create temporary directory!" >&2
+  fi
+''