Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

minimatch.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. module.exports = minimatch
  2. minimatch.Minimatch = Minimatch
  3. var path = { sep: '/' }
  4. try {
  5. path = require('path')
  6. } catch (er) {}
  7. var GLOBSTAR = minimatch.GLOBSTAR = Minimatch.GLOBSTAR = {}
  8. var expand = require('brace-expansion')
  9. var plTypes = {
  10. '!': { open: '(?:(?!(?:', close: '))[^/]*?)'},
  11. '?': { open: '(?:', close: ')?' },
  12. '+': { open: '(?:', close: ')+' },
  13. '*': { open: '(?:', close: ')*' },
  14. '@': { open: '(?:', close: ')' }
  15. }
  16. // any single thing other than /
  17. // don't need to escape / when using new RegExp()
  18. var qmark = '[^/]'
  19. // * => any number of characters
  20. var star = qmark + '*?'
  21. // ** when dots are allowed. Anything goes, except .. and .
  22. // not (^ or / followed by one or two dots followed by $ or /),
  23. // followed by anything, any number of times.
  24. var twoStarDot = '(?:(?!(?:\\\/|^)(?:\\.{1,2})($|\\\/)).)*?'
  25. // not a ^ or / followed by a dot,
  26. // followed by anything, any number of times.
  27. var twoStarNoDot = '(?:(?!(?:\\\/|^)\\.).)*?'
  28. // characters that need to be escaped in RegExp.
  29. var reSpecials = charSet('().*{}+?[]^$\\!')
  30. // "abc" -> { a:true, b:true, c:true }
  31. function charSet (s) {
  32. return s.split('').reduce(function (set, c) {
  33. set[c] = true
  34. return set
  35. }, {})
  36. }
  37. // normalizes slashes.
  38. var slashSplit = /\/+/
  39. minimatch.filter = filter
  40. function filter (pattern, options) {
  41. options = options || {}
  42. return function (p, i, list) {
  43. return minimatch(p, pattern, options)
  44. }
  45. }
  46. function ext (a, b) {
  47. a = a || {}
  48. b = b || {}
  49. var t = {}
  50. Object.keys(b).forEach(function (k) {
  51. t[k] = b[k]
  52. })
  53. Object.keys(a).forEach(function (k) {
  54. t[k] = a[k]
  55. })
  56. return t
  57. }
  58. minimatch.defaults = function (def) {
  59. if (!def || !Object.keys(def).length) return minimatch
  60. var orig = minimatch
  61. var m = function minimatch (p, pattern, options) {
  62. return orig.minimatch(p, pattern, ext(def, options))
  63. }
  64. m.Minimatch = function Minimatch (pattern, options) {
  65. return new orig.Minimatch(pattern, ext(def, options))
  66. }
  67. return m
  68. }
  69. Minimatch.defaults = function (def) {
  70. if (!def || !Object.keys(def).length) return Minimatch
  71. return minimatch.defaults(def).Minimatch
  72. }
  73. function minimatch (p, pattern, options) {
  74. if (typeof pattern !== 'string') {
  75. throw new TypeError('glob pattern string required')
  76. }
  77. if (!options) options = {}
  78. // shortcut: comments match nothing.
  79. if (!options.nocomment && pattern.charAt(0) === '#') {
  80. return false
  81. }
  82. // "" only matches ""
  83. if (pattern.trim() === '') return p === ''
  84. return new Minimatch(pattern, options).match(p)
  85. }
  86. function Minimatch (pattern, options) {
  87. if (!(this instanceof Minimatch)) {
  88. return new Minimatch(pattern, options)
  89. }
  90. if (typeof pattern !== 'string') {
  91. throw new TypeError('glob pattern string required')
  92. }
  93. if (!options) options = {}
  94. pattern = pattern.trim()
  95. // windows support: need to use /, not \
  96. if (path.sep !== '/') {
  97. pattern = pattern.split(path.sep).join('/')
  98. }
  99. this.options = options
  100. this.set = []
  101. this.pattern = pattern
  102. this.regexp = null
  103. this.negate = false
  104. this.comment = false
  105. this.empty = false
  106. // make the set of regexps etc.
  107. this.make()
  108. }
  109. Minimatch.prototype.debug = function () {}
  110. Minimatch.prototype.make = make
  111. function make () {
  112. // don't do it more than once.
  113. if (this._made) return
  114. var pattern = this.pattern
  115. var options = this.options
  116. // empty patterns and comments match nothing.
  117. if (!options.nocomment && pattern.charAt(0) === '#') {
  118. this.comment = true
  119. return
  120. }
  121. if (!pattern) {
  122. this.empty = true
  123. return
  124. }
  125. // step 1: figure out negation, etc.
  126. this.parseNegate()
  127. // step 2: expand braces
  128. var set = this.globSet = this.braceExpand()
  129. if (options.debug) this.debug = console.error
  130. this.debug(this.pattern, set)
  131. // step 3: now we have a set, so turn each one into a series of path-portion
  132. // matching patterns.
  133. // These will be regexps, except in the case of "**", which is
  134. // set to the GLOBSTAR object for globstar behavior,
  135. // and will not contain any / characters
  136. set = this.globParts = set.map(function (s) {
  137. return s.split(slashSplit)
  138. })
  139. this.debug(this.pattern, set)
  140. // glob --> regexps
  141. set = set.map(function (s, si, set) {
  142. return s.map(this.parse, this)
  143. }, this)
  144. this.debug(this.pattern, set)
  145. // filter out everything that didn't compile properly.
  146. set = set.filter(function (s) {
  147. return s.indexOf(false) === -1
  148. })
  149. this.debug(this.pattern, set)
  150. this.set = set
  151. }
  152. Minimatch.prototype.parseNegate = parseNegate
  153. function parseNegate () {
  154. var pattern = this.pattern
  155. var negate = false
  156. var options = this.options
  157. var negateOffset = 0
  158. if (options.nonegate) return
  159. for (var i = 0, l = pattern.length
  160. ; i < l && pattern.charAt(i) === '!'
  161. ; i++) {
  162. negate = !negate
  163. negateOffset++
  164. }
  165. if (negateOffset) this.pattern = pattern.substr(negateOffset)
  166. this.negate = negate
  167. }
  168. // Brace expansion:
  169. // a{b,c}d -> abd acd
  170. // a{b,}c -> abc ac
  171. // a{0..3}d -> a0d a1d a2d a3d
  172. // a{b,c{d,e}f}g -> abg acdfg acefg
  173. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  174. //
  175. // Invalid sets are not expanded.
  176. // a{2..}b -> a{2..}b
  177. // a{b}c -> a{b}c
  178. minimatch.braceExpand = function (pattern, options) {
  179. return braceExpand(pattern, options)
  180. }
  181. Minimatch.prototype.braceExpand = braceExpand
  182. function braceExpand (pattern, options) {
  183. if (!options) {
  184. if (this instanceof Minimatch) {
  185. options = this.options
  186. } else {
  187. options = {}
  188. }
  189. }
  190. pattern = typeof pattern === 'undefined'
  191. ? this.pattern : pattern
  192. if (typeof pattern === 'undefined') {
  193. throw new TypeError('undefined pattern')
  194. }
  195. if (options.nobrace ||
  196. !pattern.match(/\{.*\}/)) {
  197. // shortcut. no need to expand.
  198. return [pattern]
  199. }
  200. return expand(pattern)
  201. }
  202. // parse a component of the expanded set.
  203. // At this point, no pattern may contain "/" in it
  204. // so we're going to return a 2d array, where each entry is the full
  205. // pattern, split on '/', and then turned into a regular expression.
  206. // A regexp is made at the end which joins each array with an
  207. // escaped /, and another full one which joins each regexp with |.
  208. //
  209. // Following the lead of Bash 4.1, note that "**" only has special meaning
  210. // when it is the *only* thing in a path portion. Otherwise, any series
  211. // of * is equivalent to a single *. Globstar behavior is enabled by
  212. // default, and can be disabled by setting options.noglobstar.
  213. Minimatch.prototype.parse = parse
  214. var SUBPARSE = {}
  215. function parse (pattern, isSub) {
  216. if (pattern.length > 1024 * 64) {
  217. throw new TypeError('pattern is too long')
  218. }
  219. var options = this.options
  220. // shortcuts
  221. if (!options.noglobstar && pattern === '**') return GLOBSTAR
  222. if (pattern === '') return ''
  223. var re = ''
  224. var hasMagic = !!options.nocase
  225. var escaping = false
  226. // ? => one single character
  227. var patternListStack = []
  228. var negativeLists = []
  229. var stateChar
  230. var inClass = false
  231. var reClassStart = -1
  232. var classStart = -1
  233. // . and .. never match anything that doesn't start with .,
  234. // even when options.dot is set.
  235. var patternStart = pattern.charAt(0) === '.' ? '' // anything
  236. // not (start or / followed by . or .. followed by / or end)
  237. : options.dot ? '(?!(?:^|\\\/)\\.{1,2}(?:$|\\\/))'
  238. : '(?!\\.)'
  239. var self = this
  240. function clearStateChar () {
  241. if (stateChar) {
  242. // we had some state-tracking character
  243. // that wasn't consumed by this pass.
  244. switch (stateChar) {
  245. case '*':
  246. re += star
  247. hasMagic = true
  248. break
  249. case '?':
  250. re += qmark
  251. hasMagic = true
  252. break
  253. default:
  254. re += '\\' + stateChar
  255. break
  256. }
  257. self.debug('clearStateChar %j %j', stateChar, re)
  258. stateChar = false
  259. }
  260. }
  261. for (var i = 0, len = pattern.length, c
  262. ; (i < len) && (c = pattern.charAt(i))
  263. ; i++) {
  264. this.debug('%s\t%s %s %j', pattern, i, re, c)
  265. // skip over any that are escaped.
  266. if (escaping && reSpecials[c]) {
  267. re += '\\' + c
  268. escaping = false
  269. continue
  270. }
  271. switch (c) {
  272. case '/':
  273. // completely not allowed, even escaped.
  274. // Should already be path-split by now.
  275. return false
  276. case '\\':
  277. clearStateChar()
  278. escaping = true
  279. continue
  280. // the various stateChar values
  281. // for the "extglob" stuff.
  282. case '?':
  283. case '*':
  284. case '+':
  285. case '@':
  286. case '!':
  287. this.debug('%s\t%s %s %j <-- stateChar', pattern, i, re, c)
  288. // all of those are literals inside a class, except that
  289. // the glob [!a] means [^a] in regexp
  290. if (inClass) {
  291. this.debug(' in class')
  292. if (c === '!' && i === classStart + 1) c = '^'
  293. re += c
  294. continue
  295. }
  296. // if we already have a stateChar, then it means
  297. // that there was something like ** or +? in there.
  298. // Handle the stateChar, then proceed with this one.
  299. self.debug('call clearStateChar %j', stateChar)
  300. clearStateChar()
  301. stateChar = c
  302. // if extglob is disabled, then +(asdf|foo) isn't a thing.
  303. // just clear the statechar *now*, rather than even diving into
  304. // the patternList stuff.
  305. if (options.noext) clearStateChar()
  306. continue
  307. case '(':
  308. if (inClass) {
  309. re += '('
  310. continue
  311. }
  312. if (!stateChar) {
  313. re += '\\('
  314. continue
  315. }
  316. patternListStack.push({
  317. type: stateChar,
  318. start: i - 1,
  319. reStart: re.length,
  320. open: plTypes[stateChar].open,
  321. close: plTypes[stateChar].close
  322. })
  323. // negation is (?:(?!js)[^/]*)
  324. re += stateChar === '!' ? '(?:(?!(?:' : '(?:'
  325. this.debug('plType %j %j', stateChar, re)
  326. stateChar = false
  327. continue
  328. case ')':
  329. if (inClass || !patternListStack.length) {
  330. re += '\\)'
  331. continue
  332. }
  333. clearStateChar()
  334. hasMagic = true
  335. var pl = patternListStack.pop()
  336. // negation is (?:(?!js)[^/]*)
  337. // The others are (?:<pattern>)<type>
  338. re += pl.close
  339. if (pl.type === '!') {
  340. negativeLists.push(pl)
  341. }
  342. pl.reEnd = re.length
  343. continue
  344. case '|':
  345. if (inClass || !patternListStack.length || escaping) {
  346. re += '\\|'
  347. escaping = false
  348. continue
  349. }
  350. clearStateChar()
  351. re += '|'
  352. continue
  353. // these are mostly the same in regexp and glob
  354. case '[':
  355. // swallow any state-tracking char before the [
  356. clearStateChar()
  357. if (inClass) {
  358. re += '\\' + c
  359. continue
  360. }
  361. inClass = true
  362. classStart = i
  363. reClassStart = re.length
  364. re += c
  365. continue
  366. case ']':
  367. // a right bracket shall lose its special
  368. // meaning and represent itself in
  369. // a bracket expression if it occurs
  370. // first in the list. -- POSIX.2 2.8.3.2
  371. if (i === classStart + 1 || !inClass) {
  372. re += '\\' + c
  373. escaping = false
  374. continue
  375. }
  376. // handle the case where we left a class open.
  377. // "[z-a]" is valid, equivalent to "\[z-a\]"
  378. if (inClass) {
  379. // split where the last [ was, make sure we don't have
  380. // an invalid re. if so, re-walk the contents of the
  381. // would-be class to re-translate any characters that
  382. // were passed through as-is
  383. // TODO: It would probably be faster to determine this
  384. // without a try/catch and a new RegExp, but it's tricky
  385. // to do safely. For now, this is safe and works.
  386. var cs = pattern.substring(classStart + 1, i)
  387. try {
  388. RegExp('[' + cs + ']')
  389. } catch (er) {
  390. // not a valid class!
  391. var sp = this.parse(cs, SUBPARSE)
  392. re = re.substr(0, reClassStart) + '\\[' + sp[0] + '\\]'
  393. hasMagic = hasMagic || sp[1]
  394. inClass = false
  395. continue
  396. }
  397. }
  398. // finish up the class.
  399. hasMagic = true
  400. inClass = false
  401. re += c
  402. continue
  403. default:
  404. // swallow any state char that wasn't consumed
  405. clearStateChar()
  406. if (escaping) {
  407. // no need
  408. escaping = false
  409. } else if (reSpecials[c]
  410. && !(c === '^' && inClass)) {
  411. re += '\\'
  412. }
  413. re += c
  414. } // switch
  415. } // for
  416. // handle the case where we left a class open.
  417. // "[abc" is valid, equivalent to "\[abc"
  418. if (inClass) {
  419. // split where the last [ was, and escape it
  420. // this is a huge pita. We now have to re-walk
  421. // the contents of the would-be class to re-translate
  422. // any characters that were passed through as-is
  423. cs = pattern.substr(classStart + 1)
  424. sp = this.parse(cs, SUBPARSE)
  425. re = re.substr(0, reClassStart) + '\\[' + sp[0]
  426. hasMagic = hasMagic || sp[1]
  427. }
  428. // handle the case where we had a +( thing at the *end*
  429. // of the pattern.
  430. // each pattern list stack adds 3 chars, and we need to go through
  431. // and escape any | chars that were passed through as-is for the regexp.
  432. // Go through and escape them, taking care not to double-escape any
  433. // | chars that were already escaped.
  434. for (pl = patternListStack.pop(); pl; pl = patternListStack.pop()) {
  435. var tail = re.slice(pl.reStart + pl.open.length)
  436. this.debug('setting tail', re, pl)
  437. // maybe some even number of \, then maybe 1 \, followed by a |
  438. tail = tail.replace(/((?:\\{2}){0,64})(\\?)\|/g, function (_, $1, $2) {
  439. if (!$2) {
  440. // the | isn't already escaped, so escape it.
  441. $2 = '\\'
  442. }
  443. // need to escape all those slashes *again*, without escaping the
  444. // one that we need for escaping the | character. As it works out,
  445. // escaping an even number of slashes can be done by simply repeating
  446. // it exactly after itself. That's why this trick works.
  447. //
  448. // I am sorry that you have to see this.
  449. return $1 + $1 + $2 + '|'
  450. })
  451. this.debug('tail=%j\n %s', tail, tail, pl, re)
  452. var t = pl.type === '*' ? star
  453. : pl.type === '?' ? qmark
  454. : '\\' + pl.type
  455. hasMagic = true
  456. re = re.slice(0, pl.reStart) + t + '\\(' + tail
  457. }
  458. // handle trailing things that only matter at the very end.
  459. clearStateChar()
  460. if (escaping) {
  461. // trailing \\
  462. re += '\\\\'
  463. }
  464. // only need to apply the nodot start if the re starts with
  465. // something that could conceivably capture a dot
  466. var addPatternStart = false
  467. switch (re.charAt(0)) {
  468. case '.':
  469. case '[':
  470. case '(': addPatternStart = true
  471. }
  472. // Hack to work around lack of negative lookbehind in JS
  473. // A pattern like: *.!(x).!(y|z) needs to ensure that a name
  474. // like 'a.xyz.yz' doesn't match. So, the first negative
  475. // lookahead, has to look ALL the way ahead, to the end of
  476. // the pattern.
  477. for (var n = negativeLists.length - 1; n > -1; n--) {
  478. var nl = negativeLists[n]
  479. var nlBefore = re.slice(0, nl.reStart)
  480. var nlFirst = re.slice(nl.reStart, nl.reEnd - 8)
  481. var nlLast = re.slice(nl.reEnd - 8, nl.reEnd)
  482. var nlAfter = re.slice(nl.reEnd)
  483. nlLast += nlAfter
  484. // Handle nested stuff like *(*.js|!(*.json)), where open parens
  485. // mean that we should *not* include the ) in the bit that is considered
  486. // "after" the negated section.
  487. var openParensBefore = nlBefore.split('(').length - 1
  488. var cleanAfter = nlAfter
  489. for (i = 0; i < openParensBefore; i++) {
  490. cleanAfter = cleanAfter.replace(/\)[+*?]?/, '')
  491. }
  492. nlAfter = cleanAfter
  493. var dollar = ''
  494. if (nlAfter === '' && isSub !== SUBPARSE) {
  495. dollar = '$'
  496. }
  497. var newRe = nlBefore + nlFirst + nlAfter + dollar + nlLast
  498. re = newRe
  499. }
  500. // if the re is not "" at this point, then we need to make sure
  501. // it doesn't match against an empty path part.
  502. // Otherwise a/* will match a/, which it should not.
  503. if (re !== '' && hasMagic) {
  504. re = '(?=.)' + re
  505. }
  506. if (addPatternStart) {
  507. re = patternStart + re
  508. }
  509. // parsing just a piece of a larger pattern.
  510. if (isSub === SUBPARSE) {
  511. return [re, hasMagic]
  512. }
  513. // skip the regexp for non-magical patterns
  514. // unescape anything in it, though, so that it'll be
  515. // an exact match against a file etc.
  516. if (!hasMagic) {
  517. return globUnescape(pattern)
  518. }
  519. var flags = options.nocase ? 'i' : ''
  520. try {
  521. var regExp = new RegExp('^' + re + '$', flags)
  522. } catch (er) {
  523. // If it was an invalid regular expression, then it can't match
  524. // anything. This trick looks for a character after the end of
  525. // the string, which is of course impossible, except in multi-line
  526. // mode, but it's not a /m regex.
  527. return new RegExp('$.')
  528. }
  529. regExp._glob = pattern
  530. regExp._src = re
  531. return regExp
  532. }
  533. minimatch.makeRe = function (pattern, options) {
  534. return new Minimatch(pattern, options || {}).makeRe()
  535. }
  536. Minimatch.prototype.makeRe = makeRe
  537. function makeRe () {
  538. if (this.regexp || this.regexp === false) return this.regexp
  539. // at this point, this.set is a 2d array of partial
  540. // pattern strings, or "**".
  541. //
  542. // It's better to use .match(). This function shouldn't
  543. // be used, really, but it's pretty convenient sometimes,
  544. // when you just want to work with a regex.
  545. var set = this.set
  546. if (!set.length) {
  547. this.regexp = false
  548. return this.regexp
  549. }
  550. var options = this.options
  551. var twoStar = options.noglobstar ? star
  552. : options.dot ? twoStarDot
  553. : twoStarNoDot
  554. var flags = options.nocase ? 'i' : ''
  555. var re = set.map(function (pattern) {
  556. return pattern.map(function (p) {
  557. return (p === GLOBSTAR) ? twoStar
  558. : (typeof p === 'string') ? regExpEscape(p)
  559. : p._src
  560. }).join('\\\/')
  561. }).join('|')
  562. // must match entire pattern
  563. // ending in a * or ** will make it less strict.
  564. re = '^(?:' + re + ')$'
  565. // can match anything, as long as it's not this.
  566. if (this.negate) re = '^(?!' + re + ').*$'
  567. try {
  568. this.regexp = new RegExp(re, flags)
  569. } catch (ex) {
  570. this.regexp = false
  571. }
  572. return this.regexp
  573. }
  574. minimatch.match = function (list, pattern, options) {
  575. options = options || {}
  576. var mm = new Minimatch(pattern, options)
  577. list = list.filter(function (f) {
  578. return mm.match(f)
  579. })
  580. if (mm.options.nonull && !list.length) {
  581. list.push(pattern)
  582. }
  583. return list
  584. }
  585. Minimatch.prototype.match = match
  586. function match (f, partial) {
  587. this.debug('match', f, this.pattern)
  588. // short-circuit in the case of busted things.
  589. // comments, etc.
  590. if (this.comment) return false
  591. if (this.empty) return f === ''
  592. if (f === '/' && partial) return true
  593. var options = this.options
  594. // windows: need to use /, not \
  595. if (path.sep !== '/') {
  596. f = f.split(path.sep).join('/')
  597. }
  598. // treat the test path as a set of pathparts.
  599. f = f.split(slashSplit)
  600. this.debug(this.pattern, 'split', f)
  601. // just ONE of the pattern sets in this.set needs to match
  602. // in order for it to be valid. If negating, then just one
  603. // match means that we have failed.
  604. // Either way, return on the first hit.
  605. var set = this.set
  606. this.debug(this.pattern, 'set', set)
  607. // Find the basename of the path by looking for the last non-empty segment
  608. var filename
  609. var i
  610. for (i = f.length - 1; i >= 0; i--) {
  611. filename = f[i]
  612. if (filename) break
  613. }
  614. for (i = 0; i < set.length; i++) {
  615. var pattern = set[i]
  616. var file = f
  617. if (options.matchBase && pattern.length === 1) {
  618. file = [filename]
  619. }
  620. var hit = this.matchOne(file, pattern, partial)
  621. if (hit) {
  622. if (options.flipNegate) return true
  623. return !this.negate
  624. }
  625. }
  626. // didn't get any hits. this is success if it's a negative
  627. // pattern, failure otherwise.
  628. if (options.flipNegate) return false
  629. return this.negate
  630. }
  631. // set partial to true to test if, for example,
  632. // "/a/b" matches the start of "/*/b/*/d"
  633. // Partial means, if you run out of file before you run
  634. // out of pattern, then that's fine, as long as all
  635. // the parts match.
  636. Minimatch.prototype.matchOne = function (file, pattern, partial) {
  637. var options = this.options
  638. this.debug('matchOne',
  639. { 'this': this, file: file, pattern: pattern })
  640. this.debug('matchOne', file.length, pattern.length)
  641. for (var fi = 0,
  642. pi = 0,
  643. fl = file.length,
  644. pl = pattern.length
  645. ; (fi < fl) && (pi < pl)
  646. ; fi++, pi++) {
  647. this.debug('matchOne loop')
  648. var p = pattern[pi]
  649. var f = file[fi]
  650. this.debug(pattern, p, f)
  651. // should be impossible.
  652. // some invalid regexp stuff in the set.
  653. if (p === false) return false
  654. if (p === GLOBSTAR) {
  655. this.debug('GLOBSTAR', [pattern, p, f])
  656. // "**"
  657. // a/**/b/**/c would match the following:
  658. // a/b/x/y/z/c
  659. // a/x/y/z/b/c
  660. // a/b/x/b/x/c
  661. // a/b/c
  662. // To do this, take the rest of the pattern after
  663. // the **, and see if it would match the file remainder.
  664. // If so, return success.
  665. // If not, the ** "swallows" a segment, and try again.
  666. // This is recursively awful.
  667. //
  668. // a/**/b/**/c matching a/b/x/y/z/c
  669. // - a matches a
  670. // - doublestar
  671. // - matchOne(b/x/y/z/c, b/**/c)
  672. // - b matches b
  673. // - doublestar
  674. // - matchOne(x/y/z/c, c) -> no
  675. // - matchOne(y/z/c, c) -> no
  676. // - matchOne(z/c, c) -> no
  677. // - matchOne(c, c) yes, hit
  678. var fr = fi
  679. var pr = pi + 1
  680. if (pr === pl) {
  681. this.debug('** at the end')
  682. // a ** at the end will just swallow the rest.
  683. // We have found a match.
  684. // however, it will not swallow /.x, unless
  685. // options.dot is set.
  686. // . and .. are *never* matched by **, for explosively
  687. // exponential reasons.
  688. for (; fi < fl; fi++) {
  689. if (file[fi] === '.' || file[fi] === '..' ||
  690. (!options.dot && file[fi].charAt(0) === '.')) return false
  691. }
  692. return true
  693. }
  694. // ok, let's see if we can swallow whatever we can.
  695. while (fr < fl) {
  696. var swallowee = file[fr]
  697. this.debug('\nglobstar while', file, fr, pattern, pr, swallowee)
  698. // XXX remove this slice. Just pass the start index.
  699. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  700. this.debug('globstar found match!', fr, fl, swallowee)
  701. // found a match.
  702. return true
  703. } else {
  704. // can't swallow "." or ".." ever.
  705. // can only swallow ".foo" when explicitly asked.
  706. if (swallowee === '.' || swallowee === '..' ||
  707. (!options.dot && swallowee.charAt(0) === '.')) {
  708. this.debug('dot detected!', file, fr, pattern, pr)
  709. break
  710. }
  711. // ** swallows a segment, and continue.
  712. this.debug('globstar swallow a segment, and continue')
  713. fr++
  714. }
  715. }
  716. // no match was found.
  717. // However, in partial mode, we can't say this is necessarily over.
  718. // If there's more *pattern* left, then
  719. if (partial) {
  720. // ran out of file
  721. this.debug('\n>>> no match, partial?', file, fr, pattern, pr)
  722. if (fr === fl) return true
  723. }
  724. return false
  725. }
  726. // something other than **
  727. // non-magic patterns just have to match exactly
  728. // patterns with magic have been turned into regexps.
  729. var hit
  730. if (typeof p === 'string') {
  731. if (options.nocase) {
  732. hit = f.toLowerCase() === p.toLowerCase()
  733. } else {
  734. hit = f === p
  735. }
  736. this.debug('string match', p, f, hit)
  737. } else {
  738. hit = f.match(p)
  739. this.debug('pattern match', p, f, hit)
  740. }
  741. if (!hit) return false
  742. }
  743. // Note: ending in / means that we'll get a final ""
  744. // at the end of the pattern. This can only match a
  745. // corresponding "" at the end of the file.
  746. // If the file ends in /, then it can only match a
  747. // a pattern that ends in /, unless the pattern just
  748. // doesn't have any more for it. But, a/b/ should *not*
  749. // match "a/b/*", even though "" matches against the
  750. // [^/]*? pattern, except in partial mode, where it might
  751. // simply not be reached yet.
  752. // However, a/b/ should still satisfy a/*
  753. // now either we fell off the end of the pattern, or we're done.
  754. if (fi === fl && pi === pl) {
  755. // ran out of pattern and filename at the same time.
  756. // an exact hit!
  757. return true
  758. } else if (fi === fl) {
  759. // ran out of file, but still had pattern left.
  760. // this is ok if we're doing the match as part of
  761. // a glob fs traversal.
  762. return partial
  763. } else if (pi === pl) {
  764. // ran out of pattern, still have file left.
  765. // this is only acceptable if we're on the very last
  766. // empty segment of a file with a trailing slash.
  767. // a/* should match a/b/
  768. var emptyFileEnd = (fi === fl - 1) && (file[fi] === '')
  769. return emptyFileEnd
  770. }
  771. // should be unreachable.
  772. throw new Error('wtf?')
  773. }
  774. // replace stuff like \* with *
  775. function globUnescape (s) {
  776. return s.replace(/\\(.)/g, '$1')
  777. }
  778. function regExpEscape (s) {
  779. return s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
  780. }