@@ -18,6 +18,7 @@ import {
1818 VcsProcessStdinWriteError ,
1919 VcsProcessTimeoutError ,
2020} from "@t3tools/contracts" ;
21+ import { HostProcessPlatform } from "@t3tools/shared/hostProcess" ;
2122import * as ProcessRunner from "../processRunner.ts" ;
2223
2324export interface VcsProcessInput {
@@ -113,6 +114,75 @@ const isTransientGitExit = (stderr: string) =>
113114 / u n a b l e t o c r e a t e [ ^ \n ] * \. l o c k [ ' " ] ? : f i l e e x i s t s / i. test ( stderr ) ||
114115 / (?: u n a b l e t o s t a t | l s t a t \( | e r r o r : o p e n \( ) [ ^ \n ] + : n o s u c h f i l e o r d i r e c t o r y / i. test ( stderr ) ;
115116
117+ /**
118+ * Git on Windows refuses to create or delete a path longer than MAX_PATH (260)
119+ * unless `core.longpaths` is set. The OS-level LongPathsEnabled setting does not
120+ * cover it: git opts in per repository, and it is off by default.
121+ *
122+ * Worktrees are where this bites. A worktree base path is longer than the
123+ * repository root, so a repository that clones fine can still fail to check out
124+ * into a worktree, and fail to be removed afterwards. A failed removal can drop
125+ * the administrative record and leave files that `git worktree list` cannot see.
126+ *
127+ * Passed per invocation through `GIT_CONFIG_*` rather than argv so the config
128+ * reaches every git subcommand without changing the command line, and nothing is
129+ * written to the user's config. Appended after any entries the caller already
130+ * set so an inherited `GIT_CONFIG_COUNT` keeps working.
131+ *
132+ * A `GIT_CONFIG_COUNT` that is not a count is left alone. Git rejects a bogus
133+ * value itself, and appending to it would overwrite the caller's first entry and
134+ * turn that loud failure into a silently different config.
135+ */
136+ export const windowsLongPathConfigEnv = (
137+ platform : NodeJS . Platform ,
138+ env : NodeJS . ProcessEnv ,
139+ ) : NodeJS . ProcessEnv => {
140+ if ( platform !== "win32" ) {
141+ return { } ;
142+ }
143+ const countKey =
144+ Object . keys ( env ) . find ( ( key ) => key . toUpperCase ( ) === "GIT_CONFIG_COUNT" ) ?? "GIT_CONFIG_COUNT" ;
145+ const inherited = env [ countKey ] ;
146+ if (
147+ inherited !== undefined &&
148+ inherited !== "" &&
149+ / ^ [ \t \r \n \v \f ] * \+ ? \d + / . exec ( inherited ) ?. [ 0 ] !== inherited
150+ ) {
151+ return { } ;
152+ }
153+ const count = inherited === undefined || inherited === "" ? 0 : Number . parseInt ( inherited , 10 ) ;
154+ return {
155+ [ countKey ] : String ( count + 1 ) ,
156+ [ `GIT_CONFIG_KEY_${ count } ` ] : "core.longpaths" ,
157+ [ `GIT_CONFIG_VALUE_${ count } ` ] : "true" ,
158+ } ;
159+ } ;
160+
161+ const GIT_CONFIG_ENV_KEY = / ^ G I T _ C O N F I G _ (?: C O U N T | K E Y _ \d + | V A L U E _ \d + ) $ / i;
162+
163+ /**
164+ * Merge env sources in precedence order for a git spawn. On Windows, env names
165+ * are case-insensitive and spawn keeps only one spelling of each, so `GIT_CONFIG_*`
166+ * names are uppercased first and a later source's entry replaces an earlier one
167+ * regardless of casing.
168+ */
169+ export const gitCommandEnv = (
170+ platform : NodeJS . Platform ,
171+ ...sources : ReadonlyArray < NodeJS . ProcessEnv | undefined >
172+ ) : NodeJS . ProcessEnv => {
173+ const env : NodeJS . ProcessEnv = { } ;
174+ for ( const source of sources ) {
175+ if ( platform !== "win32" ) {
176+ Object . assign ( env , source ) ;
177+ continue ;
178+ }
179+ for ( const [ key , value ] of Object . entries ( source ?? { } ) ) {
180+ env [ GIT_CONFIG_ENV_KEY . test ( key ) ? key . toUpperCase ( ) : key ] = value ;
181+ }
182+ }
183+ return { ...env , ...windowsLongPathConfigEnv ( platform , env ) } ;
184+ } ;
185+
116186export const make = Effect . gen ( function * ( ) {
117187 const processRunner = yield * ProcessRunner . ProcessRunner ;
118188 const vcsProcesses = yield * Semaphore . make ( VCS_PROCESS_CONCURRENCY ) ;
@@ -125,6 +195,11 @@ export const make = Effect.gen(function* () {
125195 cwd : input . cwd ,
126196 argumentCount : input . args . length ,
127197 } ;
198+ const platform = yield * HostProcessPlatform ;
199+ const env =
200+ input . command === "git" && platform === "win32"
201+ ? gitCommandEnv ( platform , process . env , input . env )
202+ : input . env ;
128203
129204 const result = yield * processRunner
130205 . run ( {
@@ -134,7 +209,7 @@ export const make = Effect.gen(function* () {
134209 ...( input . spawnCwd !== undefined ? { spawnCwd : input . spawnCwd } : { } ) ,
135210 ...( input . stdin !== undefined ? { stdin : input . stdin } : { } ) ,
136211 ...( input . onStdoutChunk !== undefined ? { onStdoutChunk : input . onStdoutChunk } : { } ) ,
137- ...( input . env !== undefined ? { env : input . env } : { } ) ,
212+ ...( env !== undefined ? { env } : { } ) ,
138213 timeout : input . timeoutMs ?? DEFAULT_TIMEOUT_MS ,
139214 maxOutputBytes : input . maxOutputBytes ?? DEFAULT_MAX_OUTPUT_BYTES ,
140215 outputMode : input . outputMode ?? "truncate" ,
0 commit comments