@@ -54,6 +54,8 @@ in
54if builtins.pathExists (toString src + "/.git")
54if builtins.pathExists (toString src + "/.git")
57+ isWorktree = !builtins.pathExists (toString src + "/.git/index");
57 # Identify the .git directory and filter just the files that we need.
59 # Identify the .git directory and filter just the files that we need.
58 gitDir = cleanSourceWith ({
60 gitDir = cleanSourceWith ({
59 filter = path: type:
61 filter = path: type:
@@ -64,7 +66,7 @@ then
64 lib.any (i: (lib.hasSuffix i path)) [
66 lib.any (i: (lib.hasSuffix i path)) [
65 "/config" "/index" "/HEAD" "/objects" "/refs" ]);
67 "/config" "/index" "/HEAD" "/objects" "/refs" ]);
67− if builtins.pathExists (toString src + "/.git/index")
69+ if !isWorktree
68 then { inherit src; subDir = ".git"; }
70 then { inherit src; subDir = ".git"; }
70 # likely a git worktree, so follow the indirection
72 # likely a git worktree, so follow the indirection
@@ -96,6 +98,19 @@ then
96 gitModulesStr = toString src + "/.gitmodules";
98 gitModulesStr = toString src + "/.gitmodules";
97 gitModules = builtins.path { name = "gitmodules"; path = gitModulesStr; };
99 gitModules = builtins.path { name = "gitmodules"; path = gitModulesStr; };
101+ gitSubmoduleFiles = cleanSourceWith {
103+ filter = path: type:
104+ type == "directory" # TODO get sudmodule directories from `.gitmodules`
105+ # and use that to filter directory tree here
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" ]);
99 # Make a temporary dir that looks enough like the real thing for
114 # Make a temporary dir that looks enough like the real thing for
100 # `git ls-files --recurse-submodules` to give us an accurate list
115 # `git ls-files --recurse-submodules` to give us an accurate list
101 # of all the files in the index.
116 # of all the files in the index.
@@ -103,13 +118,18 @@ then
103 runCommand "git-ls-files" {envVariable = true;} ''
118 runCommand "git-ls-files" {envVariable = true;} ''
104 tmp=$(mktemp -d)
119 tmp=$(mktemp -d)
121+ ${ lib.optionalString (!isWorktree && builtins.pathExists gitModulesStr) ''
122+ cp -ra ${gitSubmoduleFiles}/. $tmp
106 cp -r ${gitDir} .git
126 cp -r ${gitDir} .git
107 chmod +w -R .git
127 chmod +w -R .git
108 mkdir -p .git/objects .git/refs
128 mkdir -p .git/objects .git/refs
109 mkdir -p .git-common/objects .git-common/refs
129 mkdir -p .git-common/objects .git-common/refs
110 cp ${commonConfig} .git-common/config
130 cp ${commonConfig} .git-common/config
111 echo ../.git-common > .git/commondir
131 echo ../.git-common > .git/commondir
112− ${ lib.optionalString (builtins.pathExists gitModulesStr) ''
132+ ${ lib.optionalString (isWorktree && builtins.pathExists gitModulesStr) ''
113 cp ${gitModules} ./.gitmodules
133 cp ${gitModules} ./.gitmodules
115 ${git}/bin/git ls-files --recurse-submodules > $out
135 ${git}/bin/git ls-files --recurse-submodules > $out