@@ -19,35 +19,69 @@ function isRootPath(path: string): boolean {
1919 return ! ! path && ! CWD_RELATIVE . test ( path ) && ! isAbsolute ( path )
2020}
2121
22- /** Keep Git in the service cwd so relative Git settings still identify the same worktree and index. */
23- export function fileArgs ( cwd : string , root : string , command : string [ ] , paths : string [ ] ) : string [ ] {
24- if ( paths . length === 0 )
25- return command
26- if ( cwd === root || ! paths . some ( isRootPath ) ) {
27- return [
28- '--literal-pathspecs' ,
29- ...command ,
30- '--' ,
31- ...paths ,
32- ]
22+ // Git resolves these paths against the directory where its process starts.
23+ const GIT_PATH_SETTINGS = [
24+ 'GIT_DIR' ,
25+ 'GIT_WORK_TREE' ,
26+ 'GIT_INDEX_FILE' ,
27+ 'GIT_COMMON_DIR' ,
28+ 'GIT_OBJECT_DIRECTORY' ,
29+ 'GIT_GRAFT_FILE' ,
30+ 'GIT_SHALLOW_FILE' ,
31+ 'GIT_CONFIG' ,
32+ 'GIT_CONFIG_GLOBAL' ,
33+ 'GIT_CONFIG_SYSTEM' ,
34+ ]
35+
36+ function needsOriginalCwd ( ) : boolean {
37+ return GIT_PATH_SETTINGS . some ( ( key ) => {
38+ const value = process . env [ key ]
39+ return value && ! isAbsolute ( value )
40+ } ) || ! ! process . env . GIT_ALTERNATE_OBJECT_DIRECTORIES || ! ! process . env . GIT_CONFIG_PARAMETERS
41+ }
42+
43+ /** Run each list from its natural base. Git resolves dot segments in the literal filenames. */
44+ export async function fileSelection ( cwd : string , root : string , paths : string [ ] ) {
45+ if ( ! paths . length || cwd === root )
46+ return { cwd, paths, rootDiff : false }
47+ const rootCount = paths . filter ( isRootPath ) . length
48+ if ( ! rootCount )
49+ return { cwd, paths, rootDiff : false }
50+ const cwdCount = paths . filter ( path => CWD_RELATIVE . test ( path ) ) . length
51+ const fromRoot = rootCount > cwdCount && ! needsOriginalCwd ( )
52+ if ( fromRoot && ! cwdCount )
53+ return { cwd : root , paths, rootDiff : true }
54+
55+ // Translate only paths from the other base. Git supplies its own worktree prefix even for a symlinked cwd.
56+ const { stdout } = await runGit ( cwd , [
57+ 'rev-parse' ,
58+ fromRoot ? '--show-prefix' : '--show-cdup' ,
59+ ] )
60+ const prefix = stdout . replace ( / \n $ / , '' )
61+ const selected = paths . map ( ( path ) => {
62+ const rebase = fromRoot
63+ ? CWD_RELATIVE . test ( path )
64+ : isRootPath ( path )
65+ return rebase
66+ ? prefix + path
67+ : path
68+ } )
69+ return {
70+ cwd : fromRoot ? root : cwd ,
71+ paths : selected ,
72+ rootDiff : true ,
3373 }
74+ }
3475
35- // The top flag anchors only repo-relative names. Git still receives a single list of literal paths.
36- const pathspecs = paths . map ( path => isRootPath ( path )
37- ? `:(top)${ path } `
38- : path )
39- // diff.relative can hide a selected root file outside the service directory.
40- const options = command [ 0 ] === 'diff'
41- ? [ '--no-relative' ]
42- : [ ]
43- return [
44- '--no-literal-pathspecs' ,
45- '--noglob-pathspecs' ,
46- ...command ,
47- ...options ,
48- '--' ,
49- ...pathspecs ,
50- ]
76+ export function fileArgs ( command : string [ ] , paths : string [ ] ) : string [ ] {
77+ return paths . length
78+ ? [
79+ '--literal-pathspecs' ,
80+ ...command ,
81+ '--' ,
82+ ...paths ,
83+ ]
84+ : command
5185}
5286
5387export interface GitRunResult {
@@ -61,7 +95,7 @@ export interface GitRunResult {
6195 */
6296export async function runGit ( cwd : string , args : string [ ] ) : Promise < GitRunResult > {
6397 // Inherited glob and case-folding modes conflict with the service's exact file selections.
64- const selectingFiles = args [ 0 ] === '--literal-pathspecs' || args [ 0 ] === '--no-literal-pathspecs'
98+ const selectingFiles = args [ 0 ] === '--literal-pathspecs'
6599 const { stdout, stderr } = await execFileAsync ( 'git' , args , {
66100 cwd,
67101 maxBuffer : MAX_BUFFER ,
0 commit comments