Commit 84d2cae2 authored by Dan Allen's avatar Dan Allen
Browse files

refactor findWorktrees function to be simpler and more efficient

parent 846955c7
Loading
Loading
Loading
Loading
Loading
+18 −20
Original line number Diff line number Diff line
@@ -1080,11 +1080,10 @@ function findWorktrees (repo, patterns) {
    patterns[0] === '.' && (patterns = patterns.slice(1))
      ? getCurrentBranchName(repo).then((branch) => branch && [branch, { head: repo.dir, name: '.' }])
      : Promise.resolve()
  const worktreesDir = patterns.length ? ospath.join(repo.dir, '.git', 'worktrees') : undefined
  if (!patterns.length) return mainWorktree.then((entry) => new Map(entry && [entry]))
  const worktreesDir = ospath.join(repo.dir, '.git', 'worktrees')
  const patternCache = repo.cache[REF_PATTERN_CACHE_KEY]
  return (
    worktreesDir
      ? fsp
  return fsp
    .readdir(worktreesDir)
    .then((worktreeNames) => filterRefs(worktreeNames, patterns, patternCache), invariably.emptyArray)
    .then((worktreeNames) =>
@@ -1100,8 +1099,7 @@ function findWorktrees (repo, patterns) {
        })
      )
    )
      : Promise.resolve()
  ).then((entries = []) => mainWorktree.then((entry) => new Map(entry ? entries.push(entry) && entries : entries)))
    .then((entries) => mainWorktree.then((main) => (main ? new Map(entries).set(main[0], main[1]) : new Map(entries))))
}

async function gracefulPromiseAllWithLimit (tasks, limit = Infinity) {