Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/main/git/source-control/submodule-gitlink-oid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GitRuntimeOptions } from '../git-runtime-options'
import { gitOptionsForWorktree } from '../git-runtime-options'
import { gitExecFileAsync, gitOptionalLocksDisabledEnv } from '../runner'
import { literalPathspec } from './git-pathspec'

export async function readGitlinkOidFromTree(
worktreePath: string,
Expand All @@ -9,10 +10,13 @@ export async function readGitlinkOidFromTree(
options: GitRuntimeOptions
): Promise<string> {
try {
const { stdout } = await gitExecFileAsync(['ls-tree', ref, '--', submodulePath], {
...gitOptionsForWorktree(worktreePath, options),
env: gitOptionalLocksDisabledEnv()
})
const { stdout } = await gitExecFileAsync(
['ls-tree', ref, '--', literalPathspec(submodulePath, worktreePath, options)],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Functional Correctness · Quick win

Preserve gitlink reads when GIT_LITERAL_PATHSPECS=1.

If the process inherits GIT_LITERAL_PATHSPECS=1, Git treats :(literal) as filename text, not pathspec magic. Both changed lookups then return an empty OID, even for an ordinary submodule path. Disable that inherited setting for these commands, or select a literal-path argument that remains valid under it. (git-scm.com)

{
...gitOptionsForWorktree(worktreePath, options),
env: gitOptionalLocksDisabledEnv()
}
)
return stdout.match(/^160000 commit ([0-9a-f]+)\t/m)?.[1] ?? ''
} catch {
return ''
Expand All @@ -25,10 +29,13 @@ export async function readGitlinkOidFromIndex(
options: GitRuntimeOptions
): Promise<string> {
try {
const { stdout } = await gitExecFileAsync(['ls-files', '-s', '--', submodulePath], {
...gitOptionsForWorktree(worktreePath, options),
env: gitOptionalLocksDisabledEnv()
})
const { stdout } = await gitExecFileAsync(
['ls-files', '-s', '--', literalPathspec(submodulePath, worktreePath, options)],
{
...gitOptionsForWorktree(worktreePath, options),
env: gitOptionalLocksDisabledEnv()
}
)
return stdout.match(/^160000 ([0-9a-f]+) /m)?.[1] ?? ''
} catch {
return ''
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { runProcess } from '@orca/process-host'
import { afterEach, describe, expect, it } from 'vitest'
import { getDiff } from './file-diff'
import { getSubmoduleStatus } from './submodule-status'

const directories: string[] = []

afterEach(async () => {
await Promise.all(directories.splice(0).map((directory) => rm(directory, { recursive: true })))
})

describe('literal submodule gitlinks with real Git', () => {
it('reads a glob-like submodule path without selecting a matching neighbor', async () => {
const submodulePath = 'module[12]'
const root = await mkdtemp(join(tmpdir(), 'orca-gitlink-pathspec-'))
directories.push(root)
const source = join(root, 'source')
const neighborSource = join(root, 'neighbor-source')
const parent = join(root, 'parent')
const child = join(parent, submodulePath)
const git = async (cwd: string, ...args: string[]) => {
const result = await runProcess({
program: 'git',
args,
cwd,
timeoutMs: 10_000,
env: {
...process.env,
GIT_CONFIG_NOSYSTEM: '1',
GIT_CONFIG_GLOBAL: join(root, 'missing-config')
Comment on lines +30 to +33

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Stability & Availability · Quick win

Remove inherited repository locations from the test Git environment.

If a Git hook launches this test with GIT_DIR or GIT_WORK_TREE set, the copied environment can direct commands away from the temporary repository named by cwd. The setup can fail or operate on the hook’s repository. Clear repository-location variables before spawning Git for these fixtures. Git documents this requirement for commands that a hook runs in another repository. (git-scm.com) Based on learnings, clear inherited Git repository-location variables when running Git against temporary repositories.

Source: Learnings

}
})
expect(result.code, result.stderr).toBe(0)
return result.stdout
}
const init = async (repo: string) => {
await mkdir(repo)
await git(repo, 'init', '-q')
await git(repo, 'config', 'user.name', 'Test')
await git(repo, 'config', 'user.email', 'test@example.test')
await git(repo, 'config', 'commit.gpgsign', 'false')
}
await init(source)
await writeFile(join(source, 'file.txt'), 'target original\n')
await git(source, 'add', '.')
await git(source, 'commit', '-qm', 'base')
const base = (await git(source, 'rev-parse', 'HEAD')).trim()
await writeFile(join(source, 'file.txt'), 'target committed\n')
await git(source, 'commit', '-qam', 'update')
const head = (await git(source, 'rev-parse', 'HEAD')).trim()
await init(neighborSource)
await writeFile(join(neighborSource, 'neighbor.txt'), 'neighbor\n')
await git(neighborSource, 'add', '.')
await git(neighborSource, 'commit', '-qm', 'unrelated history')
await init(parent)
await git(parent, 'clone', '-q', source, submodulePath)
await git(parent, 'clone', '-q', neighborSource, 'module1')
await git(child, 'checkout', '-q', base)
await git(parent, 'config', '--file', '.gitmodules', 'submodule.target.path', submodulePath)
await git(parent, 'config', '--file', '.gitmodules', 'submodule.target.url', source)
await git(parent, 'config', '--file', '.gitmodules', 'submodule.neighbor.path', 'module1')
await git(parent, 'config', '--file', '.gitmodules', 'submodule.neighbor.url', neighborSource)
await git(parent, 'add', '.')
await git(parent, 'commit', '-qm', 'parent')
await git(child, 'checkout', '-q', head)

expect((await getSubmoduleStatus(parent, submodulePath)).entries).toContainEqual(
expect.objectContaining({ path: 'file.txt', status: 'modified' })
)
expect(await getDiff(parent, submodulePath, false)).toMatchObject({
originalContent: `Subproject commit ${base}\n`,
modifiedContent: `Subproject commit ${head}\n`
})
expect(await getDiff(parent, `${submodulePath}/file.txt`, false)).toMatchObject({
originalContent: 'target original\n',
modifiedContent: 'target committed\n'
})
})
})
Loading