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
86 changes: 86 additions & 0 deletions src/main/git/worktree-sparse-checkout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ describe('sparse-checkout detection', () => {
expect(mainWorktree(await listWorktrees(repoPath)).isSparse).toBeFalsy()
}
)

it.skipIf(process.platform === 'win32')(
'ignores config.worktree while extensions.worktreeConfig is off',
async () => {
const repoPath = await createRepoWithTwoDirs()

git(repoPath, ['sparse-checkout', 'set', 'keep'])
git(repoPath, ['config', 'extensions.worktreeConfig', 'false'])
git(repoPath, ['config', 'core.sparseCheckout', 'false'])
await writeFile(
path.join(repoPath, '.git', 'config.worktree'),
'[core]\n\tsparseCheckout = true\n'
)

expect(git(repoPath, ['config', '--get', 'core.sparseCheckout']).trim()).toBe('false')
expect(mainWorktree(await listWorktrees(repoPath)).isSparse).toBeFalsy()
}
)

it.skipIf(process.platform === 'win32')(
'honors config.worktree while extensions.worktreeConfig is on',
async () => {
const repoPath = await createRepoWithTwoDirs()

git(repoPath, ['sparse-checkout', 'set', 'keep'])
git(repoPath, ['config', 'extensions.worktreeConfig', 'true'])
git(repoPath, ['config', 'core.sparseCheckout', 'false'])
await writeFile(
path.join(repoPath, '.git', 'config.worktree'),
'[core]\n\tsparseCheckout = true\n'
)

expect(git(repoPath, ['config', '--get', 'core.sparseCheckout']).trim()).toBe('true')
expect(mainWorktree(await listWorktrees(repoPath)).isSparse).toBe(true)
}
)
})

describe('parseCoreSparseCheckoutFlag', () => {
Expand Down Expand Up @@ -113,4 +149,54 @@ describe('parseCoreSparseCheckoutFlag', () => {
it('ignores an inline comment after the value', () => {
expect(parseCoreSparseCheckoutFlag('[core]\n\tsparseCheckout = true # on\n')).toBe(true)
})

// Git's config parser is character- not line-based, so a header may be followed on the same line
// by the assignment. Every expectation below was confirmed against `git config --file --get`.
it('reads an assignment on the same line as the section header', () => {
expect(parseCoreSparseCheckoutFlag('[core] sparseCheckout = true\n')).toBe(true)
expect(parseCoreSparseCheckoutFlag('[core] sparseCheckout = false\n')).toBe(false)
expect(parseCoreSparseCheckoutFlag('[core]sparseCheckout=true\n')).toBe(true)
expect(parseCoreSparseCheckoutFlag('[core] sparseCheckout\n')).toBe(true)
})

it('keeps the section open for later lines after a same-line assignment', () => {
expect(
parseCoreSparseCheckoutFlag('[core] sparseCheckout = false\n\tsparseCheckout = true\n')
).toBe(true)
})

it('lets the last header on a line decide the section', () => {
expect(parseCoreSparseCheckoutFlag('[core "sub"] [core] sparseCheckout = true\n')).toBe(true)
expect(parseCoreSparseCheckoutFlag('[core] [other] sparseCheckout = true\n')).toBeUndefined()
})

it('ignores a same-line assignment under a [core "subsection"] header', () => {
expect(parseCoreSparseCheckoutFlag('[core "sub"] sparseCheckout = true\n')).toBeUndefined()
})

it('honors the last assignment across mixed same-line and indented forms', () => {
expect(
parseCoreSparseCheckoutFlag(
'[core] sparseCheckout = true\n[core]\n\tsparseCheckout = false\n'
)
).toBe(false)
expect(
parseCoreSparseCheckoutFlag(
'[core]\n\tsparseCheckout = false\n[core] sparseCheckout = true\n'
)
).toBe(true)
})

it('handles comments and whitespace around a same-line header', () => {
expect(parseCoreSparseCheckoutFlag('[core]# c\n\tsparseCheckout = true\n')).toBe(true)
expect(parseCoreSparseCheckoutFlag('\t[core] sparseCheckout = true ; c\n')).toBe(true)
})

it('does not treat trailing junk as a second assignment', () => {
// Git parses this line fine and takes the whole tail as one value (`git config --list` reports
// `core.sparsecheckout=true bogus = false`) — a value runs to end of line, so only one
// assignment can share a line. Git then fails the boolean coercion outright, so reading the
// whole tail as the value keeps us on the conservative "not sparse" side.
expect(parseCoreSparseCheckoutFlag('[core] sparseCheckout = true bogus = false\n')).toBe(false)
})
})
57 changes: 39 additions & 18 deletions src/main/git/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1436,16 +1436,22 @@ async function resolveGitCommonDir(gitDir: string): Promise<string> {
// `config.worktree`; later files override earlier ones, matching git's config precedence.
async function isSparseCheckoutEnabled(gitDir: string): Promise<boolean> {
const commonDir = await resolveGitCommonDir(gitDir)
const sharedFlag = await readCoreSparseCheckoutFlag(join(commonDir, 'config'))
const worktreeFlag = await readCoreSparseCheckoutFlag(join(gitDir, 'config.worktree'))
return worktreeFlag ?? sharedFlag ?? false
const sharedConfig = await readGitConfigText(join(commonDir, 'config'))
const sharedFlag = parseCoreSparseCheckoutFlag(sharedConfig)
// Git reads `config.worktree` only while extensions.worktreeConfig is on; without that gate a
// stale worktree config left behind by an earlier sparse checkout overrides the real repo value.
if (parseGitConfigFlag(sharedConfig, 'extensions', 'worktreeconfig') !== true) {
return sharedFlag ?? false
}
const worktreeConfig = await readGitConfigText(join(gitDir, 'config.worktree'))
return parseCoreSparseCheckoutFlag(worktreeConfig) ?? sharedFlag ?? false
}

async function readCoreSparseCheckoutFlag(configPath: string): Promise<boolean | undefined> {
async function readGitConfigText(configPath: string): Promise<string> {
try {
return parseCoreSparseCheckoutFlag(await readFile(configPath, 'utf-8'))
return await readFile(configPath, 'utf-8')
} catch {
return undefined
return ''
}
}

Expand All @@ -1454,23 +1460,38 @@ async function readCoreSparseCheckoutFlag(configPath: string): Promise<boolean |
// git-config parsing edge cases can be unit tested without touching the filesystem. Only the last
// assignment wins, and a `[core "subsection"]` header is intentionally not treated as `[core]`.
export function parseCoreSparseCheckoutFlag(configContent: string): boolean | undefined {
let inCoreSection = false
return parseGitConfigFlag(configContent, 'core', 'sparsecheckout')
}

// A section header may be followed on the same line by further headers and then one assignment
// (`[core] sparseCheckout = true` is legal git config); the value runs to end of line, so at most
// one assignment can share a line and the last header before it decides the section.
const GIT_CONFIG_SECTION_HEADER = /^\[\s*([A-Za-z0-9.-]+)(\s+"(?:[^"\\]|\\.)*")?\s*\]/
const GIT_CONFIG_ASSIGNMENT = /^([A-Za-z][A-Za-z0-9-]*)\s*(?:=\s*(.*))?$/

// `section` and `key` must be lowercase: git config names are case-insensitive.
function parseGitConfigFlag(
configContent: string,
section: string,
key: string
): boolean | undefined {
let inSection = false
let value: boolean | undefined
for (const rawLine of configContent.split(/\r?\n/)) {
const line = stripGitConfigComment(rawLine).trim()
if (line.length === 0) {
continue
}
const sectionHeader = line.match(/^\[\s*([A-Za-z0-9.-]+)(\s+"(?:[^"\\]|\\.)*")?\s*\]$/)
if (sectionHeader) {
inCoreSection = sectionHeader[1].toLowerCase() === 'core' && sectionHeader[2] === undefined
continue
let rest = stripGitConfigComment(rawLine).trim()
for (
let header = rest.match(GIT_CONFIG_SECTION_HEADER);
header;
header = rest.match(GIT_CONFIG_SECTION_HEADER)
) {
inSection = header[1].toLowerCase() === section && header[2] === undefined
rest = rest.slice(header[0].length).trim()
}
if (!inCoreSection) {
if (!inSection || rest.length === 0) {
continue
}
const assignment = line.match(/^([A-Za-z][A-Za-z0-9-]*)\s*(?:=\s*(.*))?$/)
if (!assignment || assignment[1].toLowerCase() !== 'sparsecheckout') {
const assignment = rest.match(GIT_CONFIG_ASSIGNMENT)
if (!assignment || assignment[1].toLowerCase() !== key) {
continue
}
value = parseGitConfigBoolean(assignment[2])
Expand Down
Loading