Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(git): read diff blobs with cat-file so glob paths cannot fall back to HEAD commit (#34778)

OrcaWincb783ef09a94
diff --git a/src/main/git/source-control/blob-absence-real-git.test.ts b/src/main/git/source-control/blob-absence-real-git.test.ts
index 0a03f574746..c5638d23bce 100644
--- a/src/main/git/source-control/blob-absence-real-git.test.ts
+++ b/src/main/git/source-control/blob-absence-real-git.test.ts
@@ -5,8 +5,8 @@ import { tmpdir } from 'node:os'
import * as path from 'node:path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { runProcess } from '@orca/process-host'
-import { isMissingGitBlobPath } from '../../../shared/git-blob-absence'
-import { readGitBlobAtIndexPath } from './git-blob-read'
+import { readGitBlobAtIndexPath, readGitBlobAtOidPath } from './git-blob-read'
+import { getCommitDiff } from './commit-diff'
import { getDiff } from './file-diff'
import { invalidateGitReadCaches, settledDiffCache } from './git-read-cache-invalidation'
import { readWorktreeDiffStamp } from './worktree-diff-stamp'
@@ -97,24 +97,61 @@ describe('real Git missing paths and failed blob reads', () => {
})
})
+ it('shows empty baselines for untracked and newly committed paths containing brackets', async () => {
+ await writeFile(path.join(repo, 'seed.txt'), 'seed\n')
+ gitCommit(repo, 'COMMIT_METADATA_MUST_NOT_BE_DIFF_CONTENT')
+ const parentOid = (await git(['rev-parse', 'HEAD'])).trim()
+ const filePath = 'new [draft].txt'
+ await writeFile(path.join(repo, filePath), 'new content\n')
+
+ expect(await readGitBlobAtIndexPath(repo, filePath)).toEqual({
+ content: '',
+ isBinary: false,
+ exists: false,
+ failed: false
+ })
+ expect(await readGitBlobAtOidPath(repo, 'HEAD', filePath)).toEqual({
+ content: '',
+ isBinary: false,
+ exists: false,
+ failed: false
+ })
+ expect(await getDiff(repo, filePath, false)).toMatchObject({
+ originalContent: '',
+ modifiedContent: 'new content\n'
+ })
+
+ await git(['add', '--', filePath])
+ expect(await getDiff(repo, filePath, true)).toMatchObject({
+ originalContent: '',
+ modifiedContent: 'new content\n'
+ })
+ gitCommit(repo, 'add bracket path')
+ const commitOid = (await git(['rev-parse', 'HEAD'])).trim()
+ expect(await getCommitDiff(repo, { commitOid, parentOid, filePath })).toMatchObject({
+ originalContent: '',
+ modifiedContent: 'new content\n'
+ })
+ })
+
it('recognizes actual index and tree absence without accepting corrupt objects', async () => {
await writeFile(path.join(repo, 'seed.txt'), 'seed\n')
gitCommit(repo, 'initial')
- const names = ['file.txt', ...(process.platform === 'win32' ? [] : ["quote' and\nnewline.txt"])]
+ const names = [
+ 'file.txt',
+ 'new [draft].txt',
+ ...(process.platform === 'win32' ? [] : ["quote' and\nnewline.txt"])
+ ]
for (const filePath of names) {
for (const presentOnDisk of [false, true]) {
if (presentOnDisk) {
await writeFile(path.join(repo, filePath), 'untracked\n')
}
- for (const oid of [undefined, 'HEAD']) {
- const result = await runProcess({
- program: 'git',
- args: ['show', '--end-of-options', `${oid ?? ''}:${filePath}`],
- cwd: repo
- })
- expect(
- isMissingGitBlobPath({ code: result.code, stderr: result.stderr }, filePath, oid)
- ).toBe(true)
+ for (const result of [
+ await readGitBlobAtIndexPath(repo, filePath),
+ await readGitBlobAtOidPath(repo, 'HEAD', filePath)
+ ]) {
+ expect(result).toEqual({ content: '', isBinary: false, exists: false, failed: false })
}
}
}
diff --git a/src/main/git/source-control/git-blob-read.ts b/src/main/git/source-control/git-blob-read.ts
index d7721fa8bfd..6f173c8b514 100644
--- a/src/main/git/source-control/git-blob-read.ts
+++ b/src/main/git/source-control/git-blob-read.ts
@@ -1,6 +1,7 @@
import { readFile, stat } from 'node:fs/promises'
import { isBinaryBuffer } from '../../../shared/binary-buffer'
import { isMissingGitBlobPath } from '../../../shared/git-blob-absence'
+import { readGitCommandFailureStderr } from '../../../shared/git-command-failure-text'
import type { GitRuntimeOptions } from '../git-runtime-options'
import { gitReadOptionsForWorktree } from '../git-runtime-options'
import { gitExecFileAsyncBuffer } from '../runner'
@@ -10,6 +11,7 @@ import {
IMAGE_AND_PDF_PREVIEW_MIME_TYPES,
filePreviewExtension
} from '../../../shared/file-preview-kind'
+import { literalPathspec } from './git-pathspec'
export type GitBlobReadResult = {
content: string
@@ -49,7 +51,7 @@ export async function readGitBlobAtIndexPath(
// Git blob syntax requires forward slashes, including on Windows.
const gitPath = filePath.replace(/\\/g, '/')
try {
- const { stdout } = await gitExecFileAsyncBuffer(['show', `:${gitPath}`], {
+ const { stdout } = await gitExecFileAsyncBuffer(['cat-file', 'blob', '--', `:${gitPath}`], {
...gitReadOptionsForWorktree(worktreePath, options),
maxBuffer: MAX_GIT_SHOW_BYTES
})
@@ -63,7 +65,7 @@ export async function readGitBlobAtIndexPath(
content: '',
isBinary: false,
exists: false,
- failed: !isMissingGitBlobPath(error, gitPath)
+ failed: !(await verifyMissingBlobPath(error, worktreePath, gitPath, options))
}
}
}
@@ -78,7 +80,7 @@ export async function readGitBlobAtOidPath(
const gitPath = filePath.replace(/\\/g, '/')
try {
const { stdout } = await gitExecFileAsyncBuffer(
- ['show', '--end-of-options', `${oid}:${gitPath}`],
+ ['cat-file', 'blob', '--', `${oid}:${gitPath}`],
{
...gitReadOptionsForWorktree(worktreePath, options),
maxBuffer: MAX_GIT_SHOW_BYTES
@@ -94,11 +96,47 @@ export async function readGitBlobAtOidPath(
content: '',
isBinary: false,
exists: false,
- failed: !isMissingGitBlobPath(error, gitPath, oid)
+ failed: !(await verifyMissingBlobPath(error, worktreePath, gitPath, options, oid))
}
}
}
+async function verifyMissingBlobPath(
+ error: unknown,
+ worktreePath: string,
+ gitPath: string,
+ options: GitRuntimeOptions,
+ oid?: string
+): Promise<boolean> {
+ if (isMissingGitBlobPath(error, gitPath, oid)) {
+ return true
+ }
+ if (
+ typeof error !== 'object' ||
+ error === null ||
+ !('code' in error) ||
+ error.code !== 128 ||
+ readGitCommandFailureStderr(error)?.replace(/\r?\n$/, '') !==
+ `fatal: Not a valid object name ${oid ?? ''}:${gitPath}` ||
+ oid?.startsWith('-') ||
+ options.signal?.aborted
+ ) {
+ return false
+ }
+ // Older cat-file omits path diagnostics; only an empty successful listing proves absence.
+ try {
+ const { stdout } = await gitExecFileAsyncBuffer(
+ oid === undefined
+ ? ['ls-files', '--stage', '-z', '--', literalPathspec(gitPath, worktreePath, options)]
+ : ['ls-tree', '-z', oid, '--', literalPathspec(gitPath, worktreePath, options)],
+ { ...gitReadOptionsForWorktree(worktreePath, options), maxBuffer: MAX_GIT_SHOW_BYTES }
+ )
+ return stdout.length === 0
+ } catch {
+ return false
+ }
+}
+
export async function readWorkingTreeFile(filePath: string): Promise<GitBlobReadResult> {
let fileStat
try {
diff --git a/src/main/git/status-diff.test.ts b/src/main/git/status-diff.test.ts
index e9597d0f0f8..37b778ecdae 100644
--- a/src/main/git/status-diff.test.ts
+++ b/src/main/git/status-diff.test.ts
@@ -100,11 +100,14 @@ describe('getDiff', () => {
const result = await getDiff('/repo', 'src/file.ts', false)
- expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(['show', ':src/file.ts'], {
- cwd: '/repo',
- maxBuffer: 10 * 1024 * 1024,
- preferWslDirectGit: true
- })
+ expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
+ ['cat-file', 'blob', '--', ':src/file.ts'],
+ {
+ cwd: '/repo',
+ maxBuffer: 10 * 1024 * 1024,
+ preferWslDirectGit: true
+ }
+ )
expect(readFileMock).toHaveBeenCalledWith(path.join('/repo', 'src/file.ts'))
expect(result).toEqual({
kind: 'text',
@@ -121,11 +124,14 @@ describe('getDiff', () => {
await getDiff('/repo', 'src\\file.ts', false)
- expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(['show', ':src/file.ts'], {
- cwd: '/repo',
- maxBuffer: 10 * 1024 * 1024,
- preferWslDirectGit: true
- })
+ expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
+ ['cat-file', 'blob', '--', ':src/file.ts'],
+ {
+ cwd: '/repo',
+ maxBuffer: 10 * 1024 * 1024,
+ preferWslDirectGit: true
+ }
+ )
})
it('falls back to HEAD for unstaged diffs when the file is not in the index', async () => {
@@ -143,7 +149,7 @@ describe('getDiff', () => {
expect(gitExecFileAsyncBufferMock).toHaveBeenNthCalledWith(
2,
- ['show', '--end-of-options', 'HEAD:src/file.ts'],
+ ['cat-file', 'blob', '--', 'HEAD:src/file.ts'],
{
cwd: '/repo',
maxBuffer: 10 * 1024 * 1024,
diff --git a/src/main/git/status-submodule.test.ts b/src/main/git/status-submodule.test.ts
index 34576f211d8..a4c3d24d1a5 100644
--- a/src/main/git/status-submodule.test.ts
+++ b/src/main/git/status-submodule.test.ts
@@ -119,11 +119,11 @@ describe('submodule diff routing', () => {
const result = await getDiff(PARENT, 'flutter_mine/lib/main.dart', false)
expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
- ['show', '--end-of-options', `${OLD_OID}:lib/main.dart`],
+ ['cat-file', 'blob', '--', `${OLD_OID}:lib/main.dart`],
{ cwd: SUBMODULE, maxBuffer: 10 * 1024 * 1024, preferWslDirectGit: true }
)
expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
- ['show', '--end-of-options', `${NEW_OID}:lib/main.dart`],
+ ['cat-file', 'blob', '--', `${NEW_OID}:lib/main.dart`],
{ cwd: SUBMODULE, maxBuffer: 10 * 1024 * 1024, preferWslDirectGit: true }
)
expect(result.kind).toBe('text')
@@ -163,11 +163,11 @@ describe('submodule diff routing', () => {
const result = await getDiff(PARENT, 'flutter_mine/lib/main.dart', true)
expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
- ['show', '--end-of-options', `${OLD_OID}:lib/main.dart`],
+ ['cat-file', 'blob', '--', `${OLD_OID}:lib/main.dart`],
{ cwd: SUBMODULE, maxBuffer: 10 * 1024 * 1024, preferWslDirectGit: true }
)
expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
- ['show', '--end-of-options', `${NEW_OID}:lib/main.dart`],
+ ['cat-file', 'blob', '--', `${NEW_OID}:lib/main.dart`],
{ cwd: SUBMODULE, maxBuffer: 10 * 1024 * 1024, preferWslDirectGit: true }
)
expect(result.kind).toBe('text')
@@ -195,11 +195,14 @@ describe('submodule diff routing', () => {
const result = await getDiff(PARENT, 'flutter_mine/lib/main.dart', false)
- expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(['show', ':lib/main.dart'], {
- cwd: SUBMODULE,
- maxBuffer: 10 * 1024 * 1024,
- preferWslDirectGit: true
- })
+ expect(gitExecFileAsyncBufferMock).toHaveBeenCalledWith(
+ ['cat-file', 'blob', '--', ':lib/main.dart'],
+ {
+ cwd: SUBMODULE,
+ maxBuffer: 10 * 1024 * 1024,
+ preferWslDirectGit: true
+ }
+ )
expect(readFileMock).toHaveBeenCalledWith(path.join(SUBMODULE, 'lib/main.dart'))
expect(result.kind).toBe('text')
expect(result.originalContent).toBe('old\n')