Skip to content

Commit

Permalink
windows: Compare drive letters case-insensitively when not UNC
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed May 20, 2023
1 parent b95cb1e commit 383ba5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,40 +757,39 @@ export class Minimatch {
matchOne(file: string[], pattern: ParseReturn[], partial: boolean = false) {
const options = this.options

// a UNC pattern like //?/c:/* can match a path like c:/x
// and vice versa
// UNC paths like //?/X:/... can match X:/... and vice versa
// Drive letters in absolute drive or unc paths are always compared
// case-insensitively.
if (this.isWindows) {
const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0])
const fileUNC =
!fileDrive &&
file[0] === '' &&
file[1] === '' &&
file[2] === '?' &&
typeof file[3] === 'string' &&
/^[a-z]:$/i.test(file[3])

const patternDrive =
typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0])
const patternUNC =
!patternDrive &&
pattern[0] === '' &&
pattern[1] === '' &&
pattern[2] === '?' &&
typeof pattern[3] === 'string' &&
/^[a-z]:$/i.test(pattern[3])

if (fileUNC && patternUNC) {
const fd = file[3] as string
const pd = pattern[3] as string
const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined
const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined
if (typeof fdi === 'number' && typeof pdi === 'number') {
const [fd, pd]: [string, string] = [file[fdi], pattern[pdi] as string]
if (fd.toLowerCase() === pd.toLowerCase()) {
file[3] = pd
}
} else if (patternUNC && typeof file[0] === 'string') {
const pd = pattern[3] as string
const fd = file[0]
if (pd.toLowerCase() === fd.toLowerCase()) {
pattern[3] = fd
pattern = pattern.slice(3)
}
} else if (fileUNC && typeof pattern[0] === 'string') {
const fd = file[3]
if (fd.toLowerCase() === pattern[0].toLowerCase()) {
pattern[0] = fd
file = file.slice(3)
pattern[pdi] = fd
if (pdi > fdi) {
pattern = pattern.slice( pdi)
} else if (fdi > pdi) {
file = file.slice(fdi)
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/unc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const cases: Case[] = [
['c:/x', '//?/C:/*', true],
['//?/c:/x', 'C:/*', true],
['//?/c:/x', '//?/C:/*', true],
['c:/x', '//?/C:/*', true],
['c:/x', 'C:/*', true],
['C:/x', '//?/c:/*', true],
['C:/x', 'c:/*', true],

['d:/x', '//?/c:/*', false],
['//?/d:/x', 'c:/*', false],
Expand Down

0 comments on commit 383ba5a

Please sign in to comment.