diff --git a/pkgs/build-support/fetchgitlocal/default.nix b/pkgs/build-support/fetchgitlocal/default.nix index 7a25966e9fb520..cc58bdecad6bfd 100644 --- a/pkgs/build-support/fetchgitlocal/default.nix +++ b/pkgs/build-support/fetchgitlocal/default.nix @@ -1,27 +1,26 @@ { runCommand, git, nix }: src: let + tmpFolder = "/tmp/fetchgitlocal-${builtins.toString currentTime}"; + currentTime = builtins.currentTime; # impure, do every time + srcStr = toString src; - # Adds the current directory (respecting ignored files) to the git store, and returns the hash + # Adds the current directory in the index (respecting ignored files) to the git store, + # and returns the hash gitHashFile = runCommand "put-in-git" { nativeBuildInputs = [ git ]; - dummy = builtins.currentTime; # impure, do every time + dummy = currentTime; preferLocalBuild = true; } '' - cd ${srcStr} - DOT_GIT=$(git rev-parse --resolve-git-dir .git) # path to repo - - cp $DOT_GIT/index $DOT_GIT/index-user # backup index - git reset # reset index - git add . # add current directory - - # hash of current directory - # remove trailing newline - git rev-parse $(git write-tree) \ - | tr -d '\n' > $out - - mv $DOT_GIT/index-user $DOT_GIT/index # restore index + # we need write access to update index + cp -r ${srcStr}/.git ${tmpFolder} + chmod a+w ${tmpFolder} + export GIT_DIR=${tmpFolder} + + # `tr` to remove trailing newline + res="$(git rev-parse --show-prefix)" + git write-tree --prefix="$res" | tr -d '\n' > $out ''; gitHash = builtins.readFile gitHashFile; # cache against git hash @@ -32,9 +31,13 @@ let } '' mkdir $out + # git annoyingly breaks without doing this since the hash does + # not correspond to repo root. + cd $(git -C ${srcStr} rev-parse --show-toplevel) + # dump tar of *current directory* at given revision - git -C ${srcStr} archive --format=tar ${gitHash} \ - | tar xf - -C $out + hash="${gitHash}" + git archive --format=tar $hash | tar xv -C $out ''; in nixPath