Skip to content

Commit fbae4a2

Browse files
committed
fix(service-git): keep bulk file arguments within native limits
Pass groups from one directory to Git in their original spelling. For mixed groups, use Git’s own relative directory prefix and keep relative Git worktree and index settings bound to the service directory. Literal file arguments also retain Git’s normal handling of redundant path separators and dot components.
1 parent 040d933 commit fbae4a2

3 files changed

Lines changed: 82 additions & 36 deletions

File tree

‎services/git/src/git.ts‎

Lines changed: 62 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5387
export interface GitRunResult {
@@ -61,7 +95,7 @@ export interface GitRunResult {
6195
*/
6296
export 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,

‎services/git/src/operations.ts‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type {
1515
} from './types'
1616
import {
1717
fileArgs,
18+
fileSelection,
1819
gitErrorMessage,
1920
isSafeRevision,
2021
RECORD,
@@ -390,8 +391,10 @@ export function createGitOps(cwd: string): GitServiceApi {
390391

391392
async function updateIndex(command: string[], paths: string[]): Promise<GitStatus> {
392393
const root = await resolveRoot()
393-
if (root && paths.length > 0)
394-
await runGit(cwd, fileArgs(cwd, root, command, paths))
394+
if (root && paths.length > 0) {
395+
const selection = await fileSelection(cwd, root, paths)
396+
await runGit(selection.cwd, fileArgs(command, selection.paths))
397+
}
395398
return status()
396399
}
397400

@@ -517,7 +520,8 @@ export function createGitOps(cwd: string): GitServiceApi {
517520
command.push('--end-of-options', ref)
518521
}
519522
const paths = (args.paths ?? []).filter(Boolean)
520-
const raw = await tryGit(cwd, fileArgs(cwd, root, command, paths))
523+
const selection = await fileSelection(cwd, root, paths)
524+
const raw = await tryGit(selection.cwd, fileArgs(command, selection.paths))
521525
const commits = raw
522526
? parseLog(raw)
523527
: []
@@ -599,19 +603,21 @@ export function createGitOps(cwd: string): GitServiceApi {
599603
const base = staged
600604
? ['diff', '--cached']
601605
: ['diff']
602-
const paths = path
606+
const selection = await fileSelection(cwd, root, path
603607
? [path]
604-
: []
605-
const summary = fileArgs(cwd, root, [...base, '--numstat'], paths)
606-
const numstatRaw = await tryGit(cwd, summary)
608+
: [])
609+
if (selection.rootDiff)
610+
base.push('--no-relative')
611+
const summary = fileArgs([...base, '--numstat'], selection.paths)
612+
const numstatRaw = await tryGit(selection.cwd, summary)
607613
const files = numstatRaw
608614
? parseNumstat(numstatRaw)
609615
: []
610616

611617
let patch: string | null = null
612618
let truncated = false
613619
if (path) {
614-
const { stdout } = await runGit(cwd, fileArgs(cwd, root, base, paths))
620+
const { stdout } = await runGit(selection.cwd, fileArgs(base, selection.paths))
615621
;({ patch, truncated } = clipPatch(stdout))
616622
}
617623

‎services/git/test/git.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ describe('@devframes/service-git', () => {
270270
expect(diff.files.map(f => f.path)).toEqual([file])
271271
expect(diff.patch).toContain(`+${location} edited`)
272272
}
273+
const packagePath = 'packages//app/./README.md'
274+
expect((await git.diff({ path: packagePath, staged: true })).patch).toContain('+package edited')
275+
const remaining = await git.unstage({
276+
paths: [packagePath],
277+
})
278+
expect(remaining.staged.map(f => f.path)).toEqual([root.file])
273279
})
274280

275281
it('stages and unstages root and explicit package paths in the same request', async () => {

0 commit comments

Comments
 (0)