on iko/ghcjs-patch-master help sign in
Fix cleaning git submodules not in a worktree (#311)

When we are not in a worktree the submodule info is in the `.git` dirs scattered throughout the repo itself. In that situation we need to copy the `.git` dirs into the temp location we are using to run `git ls-files`.

commit3c78559621cae69b45d8d633c56bb20705866c00 authorHamish Mackenzie <Hamish.Mackenzie@iohk.io> dateSun 10 Nov 2019, 14:25 (7 years ago) parentbaff198
1 file changed+22−2
lib/clean-git.nix+22−2
lib/clean-git.nix+22−2
@@ -54,6 +54,8 @@ in
5454if builtins.pathExists (toString src + "/.git")
5555then
5656 let
57+ isWorktree = !builtins.pathExists (toString src + "/.git/index");
58+
5759 # Identify the .git directory and filter just the files that we need.
5860 gitDir = cleanSourceWith ({
5961 filter = path: type:
@@ -64,7 +66,7 @@ then
6466 lib.any (i: (lib.hasSuffix i path)) [
6567 "/config" "/index" "/HEAD" "/objects" "/refs" ]);
6668 } // (
67− if builtins.pathExists (toString src + "/.git/index")
69+ if !isWorktree
6870 then { inherit src; subDir = ".git"; }
6971 else {
7072 # likely a git worktree, so follow the indirection
@@ -96,6 +98,19 @@ then
9698 gitModulesStr = toString src + "/.gitmodules";
9799 gitModules = builtins.path { name = "gitmodules"; path = gitModulesStr; };
98100
101+ gitSubmoduleFiles = cleanSourceWith {
102+ inherit src;
103+ filter = path: type:
104+ type == "directory" # TODO get sudmodule directories from `.gitmodules`
105+ # and use that to filter directory tree here
106+ ||
107+ lib.any (i: (lib.hasSuffix i path)) [
108+ ".gitmodules" ".git/config" ".git/index" ".git/HEAD" ".git/objects" ".git/refs" ] ||
109+ (lib.strings.hasInfix ".git/modules/" path &&
110+ lib.any (i: (lib.hasSuffix i path)) [
111+ "config" "index" "HEAD" "objects" "refs" ]);
112+ };
113+
99114 # Make a temporary dir that looks enough like the real thing for
100115 # `git ls-files --recurse-submodules` to give us an accurate list
101116 # of all the files in the index.
@@ -103,13 +118,18 @@ then
103118 runCommand "git-ls-files" {envVariable = true;} ''
104119 tmp=$(mktemp -d)
105120 cd $tmp
121+ ${ lib.optionalString (!isWorktree && builtins.pathExists gitModulesStr) ''
122+ cp -ra ${gitSubmoduleFiles}/. $tmp
123+ chmod +w -R $tmp
124+ rm -rf .git
125+ ''}
106126 cp -r ${gitDir} .git
107127 chmod +w -R .git
108128 mkdir -p .git/objects .git/refs
109129 mkdir -p .git-common/objects .git-common/refs
110130 cp ${commonConfig} .git-common/config
111131 echo ../.git-common > .git/commondir
112− ${ lib.optionalString (builtins.pathExists gitModulesStr) ''
132+ ${ lib.optionalString (isWorktree && builtins.pathExists gitModulesStr) ''
113133 cp ${gitModules} ./.gitmodules
114134 ''}
115135 ${git}/bin/git ls-files --recurse-submodules > $out