aboutsummaryrefslogtreecommitdiff
path: root/src/common.nim
blob: 11f6aef19cceaa7e823bd45f42668b8dae63c542 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
import
  future, options, os, osproc, posix, sequtils, sets, strutils, tables,
  args, config, format, lists, package, pacman, utils,
  "wrapper/alpm"

type
  SyncFoundPackageInfo* = tuple[
    base: string,
    version: string,
    arch: Option[string]
  ]

  SyncFoundInfo* = tuple[
    repo: string,
    pkg: Option[SyncFoundPackageInfo]
  ]

  PackageTarget* = object of RootObj
    reference*: PackageReference
    repo*: Option[string]
    destination*: Option[string]

  SyncPackageTarget* = object of PackageTarget
    foundInfos*: seq[SyncFoundInfo]

  FullPackageTarget*[T] = object of SyncPackageTarget
    pkgInfo*: Option[T]

  LookupBaseGroup = tuple[
    base: string,
    version: string,
    arch: string,
    repo: string
  ]

proc checkAndRefresh*(color: bool, args: seq[Argument]): tuple[code: int, args: seq[Argument]] =
  let refreshCount = args.count(%%%"refresh")
  if refreshCount > 0:
    let code = pacmanRun(true, color, args
      .keepOnlyOptions(commonOptions, transactionOptions, upgradeOptions) &
      ("S", none(string), ArgumentType.short) &
      ("y", none(string), ArgumentType.short).repeat(refreshCount))

    let callArgs = args
      .filter(arg => not arg.matchOption(%%%"refresh"))
    (code, callArgs)
  else:
    (0, args)

proc packageTargets*(args: seq[Argument], parseDestination: bool): seq[PackageTarget] =
  args.targets.map(target => (block:
    let (noDestinationTarget, destination) = if parseDestination: (block:
        let split = target.split("::", 2)
        if split.len == 2:
          (split[0], some(split[1]))
        else:
          (target, none(string)))
      else:
        (target, none(string))

    let splitRepoTarget = noDestinationTarget.split('/', 2)
    let (repo, nameConstraint) = if splitRepoTarget.len == 2:
        (some(splitRepoTarget[0]), splitRepoTarget[1])
      else:
        (none(string), noDestinationTarget)

    let reference = parsePackageReference(nameConstraint, false)
    PackageTarget(reference: reference, repo: repo, destination: destination)))

proc isAurTargetSync*(target: SyncPackageTarget): bool =
  target.foundInfos.len == 0 and (target.repo.isNone or target.repo == some("aur"))

proc isAurTargetFull*[T: RpcPackageInfo](target: FullPackageTarget[T]): bool =
  target.foundInfos.len > 0 and target.foundInfos[0].repo == "aur"

proc filterNotFoundSyncTargetsInternal(syncTargets: seq[SyncPackageTarget],
  pkgInfoReferencesTable: Table[string, PackageReference],
  upToDateNeededTable: Table[string, PackageReference]): seq[SyncPackageTarget] =
  # collect packages which were found neither in sync DB nor in AUR
  syncTargets.filter(t => not (upToDateNeededTable.opt(t.reference.name)
    .map(r => t.reference.isProvidedBy(r, true)).get(false)) and t.foundInfos.len == 0 and
    not (t.isAurTargetSync and pkgInfoReferencesTable.opt(t.reference.name)
    .map(r => t.reference.isProvidedBy(r, true)).get(false)))

proc filterNotFoundSyncTargets*[T: RpcPackageInfo](syncTargets: seq[SyncPackageTarget],
  pkgInfos: seq[T], upToDateNeededTable: Table[string, PackageReference]): seq[SyncPackageTarget] =
  let pkgInfoReferencesTable = pkgInfos.map(i => (i.name, i.toPackageReference)).toTable
  filterNotFoundSyncTargetsInternal(syncTargets, pkgInfoReferencesTable, upToDateNeededTable)

proc printSyncNotFound*(config: Config, notFoundTargets: seq[SyncPackageTarget]) =
  let dbs = config.dbs.toSet

  for target in notFoundTargets:
    if target.repo.isNone or target.repo == some("aur") or target.repo.unsafeGet in dbs:
      printError(config.color, trp("target not found: %s\n") % [$target.reference])
    else:
      printError(config.color, trp("database not found: %s\n") % [target.repo.unsafeGet])

proc findSyncTargets*(handle: ptr AlpmHandle, dbs: seq[ptr AlpmDatabase],
  targets: seq[PackageTarget], allowGroups: bool, checkProvides: bool):
  (seq[SyncPackageTarget], seq[string]) =
  let dbTable = dbs.map(d => ($d.name, d)).toTable

  proc checkProvided(reference: PackageReference, db: ptr AlpmDatabase): bool =
    for pkg in db.packages:
      for provides in pkg.provides:
        if reference.isProvidedBy(provides.toPackageReference, true):
          return true
    return false

  proc findSync(target: PackageTarget): seq[SyncFoundInfo] =
    if target.repo.isSome:
      let repo = target.repo.unsafeGet

      if dbTable.hasKey(repo):
        let db = dbTable[repo]
        let pkg = db[target.reference.name]

        if pkg != nil and target.reference.isProvidedBy(pkg.toPackageReference, true):
          let base = if pkg.base == nil: target.reference.name else: $pkg.base
          return @[(repo, some((base, $pkg.version, some($pkg.arch))))]
        elif checkProvides and target.reference.checkProvided(db):
          return @[(repo, none(SyncFoundPackageInfo))]
        else:
          return @[]
      else:
        return @[]
    else:
      if allowGroups and target.reference.constraint.isNone:
        let groupRepo = lc[d | (d <- dbs, g <- d.groups,
          $g.name == target.reference.name), ptr AlpmDatabase].optFirst
        if groupRepo.isSome:
          return @[($groupRepo.unsafeGet.name, none(SyncFoundPackageInfo))]

      let directResults = dbs
        .map(db => (block:
          let pkg = db[target.reference.name]
          if pkg != nil and target.reference.isProvidedBy(pkg.toPackageReference, true):
            let base = if pkg.base == nil: target.reference.name else: $pkg.base
            some(($db.name, some((base, $pkg.version, some($pkg.arch)))))
          else:
            none(SyncFoundInfo)))
        .filter(i => i.isSome)
        .map(i => i.unsafeGet)

      if directResults.len > 0:
        return directResults
      elif checkProvides:
        for db in dbs:
          if target.reference.checkProvided(db):
            return @[($db.name, none(SyncFoundPackageInfo))]
        return @[]
      else:
        return @[]

  let syncTargets = targets.map(t => SyncPackageTarget(reference: t.reference,
    repo: t.repo, destination: t.destination, foundInfos: findSync(t)))
  let checkAurNames = syncTargets.filter(isAurTargetSync).map(t => t.reference.name)
  (syncTargets, checkAurNames)

proc mapAurTargets*[T: RpcPackageInfo](targets: seq[SyncPackageTarget],
  pkgInfos: seq[T]): seq[FullPackageTarget[T]] =
  let aurTable = pkgInfos.map(i => (i.name, i)).toTable

  targets.map(proc (target: SyncPackageTarget): FullPackageTarget[T] =
    let res = if target.foundInfos.len == 0 and aurTable.hasKey(target.reference.name): (block:
        let pkgInfo = aurTable[target.reference.name]
        if target.reference.isProvidedBy(pkgInfo.toPackageReference, true):
          some((("aur", some((pkgInfo.base, pkgInfo.version, none(string)))), pkgInfo))
        else:
          none((SyncFoundInfo, T)))
      else:
        none((SyncFoundInfo, T))

    if res.isSome:
      let (syncInfo, pkgInfo) = res.get
      FullPackageTarget[T](reference: target.reference, repo: target.repo,
        destination: target.destination, foundInfos: @[syncInfo], pkgInfo: some(pkgInfo))
    else:
      FullPackageTarget[T](reference: target.reference, repo: target.repo,
        destination: target.destination, foundInfos: target.foundInfos, pkgInfo: none(T)))

proc queryUnrequired*(handle: ptr AlpmHandle, withOptional: bool, withoutOptional: bool,
  assumeExplicit: HashSet[string]): (HashSet[string], HashSet[string], HashSet[string]) =
  let (explicit, dependsTable, alternatives) = block:
    var explicit = newSeq[string]()
    var dependsTable = initTable[string,
      HashSet[tuple[reference: PackageReference, optional: bool]]]()
    var alternatives = initTable[string, HashSet[PackageReference]]()

    for pkg in handle.local.packages:
      proc fixProvides(reference: PackageReference): PackageReference =
        if reference.constraint.isNone:
          (reference.name, reference.description,
            some((ConstraintOperation.eq, $pkg.version)))
        else:
          reference

      let depends = toSeq(pkg.depends.items)
        .map(d => d.toPackageReference).toSet
      let optional = toSeq(pkg.optional.items)
        .map(d => d.toPackageReference).toSet
      let provides = toSeq(pkg.provides.items)
        .map(d => d.toPackageReference).map(fixProvides).toSet

      if pkg.reason == AlpmReason.explicit:
        explicit &= $pkg.name
      dependsTable.add($pkg.name,
        depends.map(x => (x, false)) + optional.map(x => (x, true)))
      if provides.len > 0:
        alternatives.add($pkg.name, provides)

    (explicit.toSet + assumeExplicit, dependsTable, alternatives)

  let providedBy = lc[(y, x.key) | (x <- alternatives.namedPairs, y <- x.value),
    tuple[reference: PackageReference, name: string]]

  proc findRequired(withOptional: bool, results: HashSet[string],
    check: HashSet[string]): HashSet[string] =
    let full = results + check

    let direct = lc[x.reference | (y <- dependsTable.namedPairs, y.key in check,
      x <- y.value, withOptional or not x.optional), PackageReference]

    let indirect = lc[x.name | (y <- direct, x <- providedBy,
      y.isProvidedBy(x.reference, true)), string].toSet

    let checkNext = (direct.map(p => p.name).toSet + indirect) - full
    if checkNext.len > 0: findRequired(withOptional, full, checkNext) else: full

  let installed = toSeq(dependsTable.keys).toSet

  proc findOrphans(withOptional: bool): HashSet[string] =
    let required = findRequired(withOptional, initSet[string](), explicit)
    installed - required

  let withOptionalSet = if withOptional: findOrphans(true) else: initSet[string]()
  let withoutOptionalSet = if withoutOptional: findOrphans(false) else: initSet[string]()

  (installed, withOptionalSet, withoutOptionalSet)

proc `$`*[T: PackageTarget](target: T): string =
  target.repo.map(proc (r: string): string = r & "/" & $target.reference).get($target.reference)

template tmpRoot(config: Config, dropPrivileges: bool): string =
  if dropPrivileges: config.tmpRootInitial else: config.tmpRootCurrent

proc ensureTmpOrError*(config: Config, dropPrivileges: bool): Option[string] =
  let tmpRootExists = try:
    discard config.tmpRoot(dropPrivileges).existsOrCreateDir()
    if dropPrivileges:
      let user = initialUser.get(currentUser)
      discard chown(config.tmpRoot(dropPrivileges), (Uid) user.uid, (Gid) user.gid)
    true
  except:
    false

  if not tmpRootExists:
    some(tr"failed to create tmp directory '$#'" % [config.tmpRoot(dropPrivileges)])
  else:
    none(string)

proc getGitFiles*(repoPath: string, gitSubdir: Option[string],
  dropPrivileges: bool): seq[string] =
  if gitSubdir.isSome:
    forkWaitRedirect(() => (block:
      if not dropPrivileges or dropPrivileges():
        execResult(gitCmd, "-C", repoPath, "ls-tree", "-r", "--name-only", "@",
          gitSubdir.unsafeGet & "/")
      else:
        quit(1)))
      .output
      .map(s => s[gitSubdir.unsafeGet.len + 1 .. ^1])
  else:
    forkWaitRedirect(() => (block:
      if not dropPrivileges or dropPrivileges():
        execResult(gitCmd, "-C", repoPath, "ls-tree", "-r", "--name-only", "@")
      else:
        quit(1)))
      .output

proc bisectVersion(repoPath: string, debug: bool, firstCommit: Option[string],
  compareMethod: string, gitSubdir: string, version: string,
  dropPrivileges: bool): Option[string] =
  template forkExecWithoutOutput(args: varargs[string]): int =
    forkWait(() => (block:
      discard close(0)
      if not debug:
        discard close(1)
        discard close(2)

      if not dropPrivileges or dropPrivileges():
        execResult(args)
      else:
        quit(1)))

  let (workFirstCommit, checkFirst) = if firstCommit.isSome:
      (firstCommit, false)
    else:
      (forkWaitRedirect(() => (block:
        if not dropPrivileges or dropPrivileges():
          execResult(gitCmd, "-C", repoPath,
            "rev-list", "--max-parents=0", "@")
        else:
          quit(1)))
        .output.optLast, true)

  let (realLastThreeCommits, _) = forkWaitRedirect(() => (block:
    if not dropPrivileges or dropPrivileges():
      execResult(gitCmd, "-C", repoPath,
        "rev-list", "--max-count=3", "@")
    else:
      quit(1)))

  let index = workFirstCommit.map(c => realLastThreeCommits.find(c)).get(-1)
  let lastThreeCommits = if index >= 0:
      realLastThreeCommits[0 .. index]
    else:
      realLastThreeCommits

  proc checkCommit(commit: string): Option[string] =
    let checkout1Code = forkExecWithoutOutput(gitCmd, "-C", repoPath,
      "checkout", commit)

    if checkout1Code != 0:
      none(string)
    else:
      let foundVersion = forkWaitRedirect(() => (block:
        if not dropPrivileges or dropPrivileges():
          execResult(pkgLibDir & "/bisect",
            compareMethod, repoPath & "/" & gitSubdir, version)
        else:
          quit(1)))
        .output.optFirst

      let checkout2Code = forkExecWithoutOutput(gitCmd, "-C", repoPath,
        "checkout", lastThreeCommits[0])

      if checkout2Code != 0:
        none(string)
      elif foundVersion == some(version):
        some(commit)
      else:
        none(string)

  if lastThreeCommits.len == 0:
    none(string)
  elif lastThreeCommits.len == 1:
    if checkFirst:
      checkCommit(lastThreeCommits[0])
    else:
      none(string)
  elif lastThreeCommits.len == 2:
    let checkedCommit = checkCommit(lastThreeCommits[0])
    if checkedCommit.isSome:
      checkedCommit
    elif checkFirst:
      checkCommit(lastThreeCommits[1])
    else:
      none(string)
  else:
    # find the commit with specific package version using git bisect
    let bisectStartCode = forkExecWithoutOutput(gitCmd, "-C", repoPath,
      "bisect", "start", "@", workFirstCommit.get(""))

    if bisectStartCode != 0:
      none(string)
    else:
      discard forkExecWithoutOutput(gitCmd, "-C", repoPath,
        "bisect", "run", pkgLibDir & "/bisect", compareMethod, gitSubdir, version)

      let commit = forkWaitRedirect(() => (block:
        if not dropPrivileges or dropPrivileges():
          execResult(gitCmd, "-C", repoPath,
            "rev-list", "--max-count=1", "refs/bisect/bad")
        else:
          quit(1)))
        .output.optFirst

      discard forkExecWithoutOutput(gitCmd, "-C", repoPath,
        "bisect", "reset")

      if commit.isSome:
        let checkedCommit = commit.map(checkCommit).flatten
        if checkedCommit.isSome:
          checkedCommit
        else:
          # non-incremental git history (e.g. downgrade without epoch change), bisect again
          bisectVersion(repoPath, debug, commit, compareMethod, gitSubdir,
            version, dropPrivileges)
      elif checkFirst and workFirstCommit.isSome:
        checkCommit(workFirstCommit.unsafeGet)
      else:
        none(string)

proc obtainSrcInfo*(path: string): string =
  let (output, code) = forkWaitRedirect(() => (block:
    if dropPrivileges() and chdir(path) == 0:
      execResult(makePkgCmd, "--printsrcinfo")
    else:
      quit(1)))

  if code == 0:
    output.foldl(a & b & "\n", "")
  else:
    ""

proc reloadPkgInfos*(config: Config, path: string, pkgInfos: seq[PackageInfo]): seq[PackageInfo] =
  let srcInfo = obtainSrcInfo(path)
  let res = parseSrcInfo(pkgInfos[0].repo, srcInfo, config.arch,
    pkgInfos[0].gitUrl, pkgInfos[0].gitSubdir)
  if res.len > 0:
    res
  else:
    pkgInfos

proc cloneBareRepo(config: Config, bareName: string, url: string,
  dropPrivileges: bool): Option[string] =
  let fullName = bareName & ".git"
  let repoPath = repoPath(config.tmpRoot(dropPrivileges), fullName)
  removeDirQuiet(repoPath)

  if forkWait(() => (block:
    if not dropPrivileges or dropPrivileges():
      execResult(gitCmd, "-C", config.tmpRoot(dropPrivileges),
        "clone", "-q", "--bare", url, fullName)
    else:
      quit(1))) == 0:
    some(repoPath)
  else:
    removeDirQuiet(repoPath)
    none(string)

proc cloneBareRepos*(config: Config, gitRepos: seq[GitRepo],
  progressCallback: (int, int) -> void, dropPrivileges: bool): (seq[string], seq[string]) =
  let message = ensureTmpOrError(config, dropPrivileges)
  if message.isSome:
    (@[], @[message.unsafeGet])
  else:
    let bare = gitRepos
      .filter(t => t.bareName.isSome)
      .map(r => (r.bareName.unsafeGet, r.url))
      .deduplicate

    proc cloneNext(index: int, paths: List[string], messages: List[string]):
      (List[string], List[string]) =
      progressCallback(index, bare.len)

      if index >= bare.len:
        (paths.reversed, messages.reversed)
      else:
        let (bareName, url) = bare[index]
        let repoPath = cloneBareRepo(config, bareName, url, dropPrivileges)
        if repoPath.isSome:
          cloneNext(index + 1, repoPath.unsafeGet ^& paths, messages)
        else:
          let message = tr"$#: failed to clone git repository" % [bareName]
          cloneNext(index + 1, paths, message ^& messages)

    let (paths, messages) = cloneNext(0, nil, nil)
    (toSeq(paths.items), toSeq(messages.items))

proc clonePackageRepoInternal(config: Config, base: string, version: string,
  git: GitRepo, dropPrivileges: bool): Option[string] =
  let repoPath = repoPath(config.tmpRoot(dropPrivileges), base)
  removeDirQuiet(repoPath)

  let url = if git.bareName.isSome:
      repoPath(config.tmpRoot(dropPrivileges), git.bareName.unsafeGet & ".git")
    else:
      git.url

  if forkWait(() => (block:
    if not dropPrivileges or dropPrivileges():
      if git.branch.isSome:
        execResult(gitCmd, "-C", config.tmpRoot(dropPrivileges),
          "clone", "-q", url, "-b", git.branch.unsafeGet, "--single-branch", base)
      else:
        execResult(gitCmd, "-C", config.tmpRoot(dropPrivileges),
          "clone", "-q", url, "--single-branch", base)
    else:
      quit(1))) == 0:
    let commit = bisectVersion(repoPath, config.debug, none(string),
      "source", git.path, version, dropPrivileges)

    if commit.isNone:
      removeDirQuiet(repoPath)
      none(string)
    else:
      discard forkWait(() => (block:
        if not dropPrivileges or dropPrivileges():
          execResult(gitCmd, "-C", repoPath,
            "checkout", "-q", commit.unsafeGet)
        else:
          quit(1)))

      some(repoPath)
  else:
    removeDirQuiet(repoPath)
    none(string)

proc clonePackageRepo*(config: Config, base: string, version: string,
  git: GitRepo, dropPrivileges: bool): Option[string] =
  let message = ensureTmpOrError(config, dropPrivileges)
  if message.isSome:
    message
  else:
    let repoPath = clonePackageRepoInternal(config, base, version, git, dropPrivileges)
    if repoPath.isNone:
      some(tr"$#: failed to clone git repository" % [base])
    else:
      none(string)

proc obtainBuildPkgInfosInternal(config: Config, bases: seq[LookupBaseGroup],
  pacmanTargetNames: seq[string], progressCallback: (int, int) -> void, dropPrivileges: bool):
  (seq[PackageInfo], seq[string], seq[string]) =
  let lookupResults: seq[tuple[group: LookupBaseGroup, git: Option[GitRepo]]] = bases
    .map(b => (b, lookupGitRepo(b.repo, b.base, b.arch)))
  let notFoundRepos = lookupResults.filter(r => r.git.isNone)

  if notFoundRepos.len > 0:
    let messages = notFoundRepos.map(r => tr"$#: repository not found" % [r.group.base])
    (newSeq[PackageInfo](), newSeq[string](), messages)
  else:
    let message = ensureTmpOrError(config, dropPrivileges)
    if message.isSome:
      (@[], @[], @[message.unsafeGet])
    else:
      let (barePaths, berrors) = cloneBareRepos(config, lookupResults.map(r => r.git.unsafeGet),
        proc (progress: int, count: int) = progressCallback(progress, count + lookupResults.len),
        dropPrivileges)

      if berrors.len > 0:
        for path in barePaths:
          removeDirQuiet(path)
        discard rmdir(config.tmpRoot(dropPrivileges))
        (newSeq[PackageInfo](), barePaths, berrors)
      else:
        proc findCommitAndGetSrcInfo(base: string, version: string,
          repo: string, git: GitRepo): tuple[pkgInfos: seq[PackageInfo], path: Option[string]] =
          let repoPath = clonePackageRepoInternal(config, base, version, git, dropPrivileges)

          if repoPath.isSome:
            let srcInfo = obtainSrcInfo(repoPath.unsafeGet & "/" & git.path)
            let pkgInfos = parseSrcInfo(repo, srcInfo, config.arch,
              git.url, some(git.path))
              .filter(i => i.version == version)
            (pkgInfos, repoPath)
          else:
            (newSeq[PackageInfo](), none(string))

        let (pkgInfosWithPathsReversed, _) = lookupResults.foldl(block:
          let (list, index) = a
          let res = findCommitAndGetSrcInfo(b.group.base, b.group.version,
            b.group.repo, b.git.unsafeGet) ^& list
          progressCallback(barePaths.len + index + 1, barePaths.len + lookupResults.len)
          (res, index + 1),
          (list[tuple[pkgInfos: seq[PackageInfo], path: Option[string]]](), 0))

        let pkgInfosWithPaths = pkgInfosWithPathsReversed.reversed
        let pkgInfos = lc[x | (y <- pkgInfosWithPaths, x <- y.pkgInfos), PackageInfo]
        let paths = lc[x | (y <- pkgInfosWithPaths, x <- y.path), string]

        let pkgInfosTable = pkgInfos.map(i => (i.name, i)).toTable

        let foundPkgInfos = lc[x | (y <- pacmanTargetNames,
          x <- pkgInfosTable.opt(y)), PackageInfo]
        let errorMessages = pacmanTargetNames
          .filter(n => not pkgInfosTable.hasKey(n))
          .map(n => tr"$#: failed to get package info" % [n])

        if errorMessages.len > 0:
          for path in barePaths:
            removeDirQuiet(path)
          for path in paths:
            removeDirQuiet(path)
        discard rmdir(config.tmpRoot(dropPrivileges))
        (foundPkgInfos, barePaths & paths, errorMessages)

proc obtainBuildPkgInfos*[T: RpcPackageInfo](config: Config,
  pacmanTargets: seq[FullPackageTarget[T]], progressCallback: (int, int) -> void,
  dropPrivileges: bool): (seq[PackageInfo], seq[string], seq[string]) =
  let bases = pacmanTargets
    .map(proc (target: FullPackageTarget[T]): LookupBaseGroup =
      let info = target.foundInfos[0]
      let pkg = info.pkg.get
      (pkg.base, pkg.version, pkg.arch.get, info.repo))
    .deduplicate

  let pacmanTargetNames = pacmanTargets.map(t => t.reference.name)
  obtainBuildPkgInfosInternal(config, bases, pacmanTargetNames, progressCallback, dropPrivileges)

proc cloneAurRepo*(config: Config, base: string, gitUrl: string,
  dropPrivileges: bool): (int, Option[string]) =
  let repoPath = repoPath(config.tmpRoot(dropPrivileges), base)

  let message = ensureTmpOrError(config, dropPrivileges)
  if message.isSome:
    (1, message)
  elif repoPath.existsDir():
    (0, none(string))
  else:
    let cloneCode = forkWait(() => (block:
      if not dropPrivileges or dropPrivileges():
        execResult(gitCmd, "-C", config.tmpRoot(dropPrivileges),
          "clone", "-q", gitUrl, "--single-branch", base)
      else:
        quit(1)))

    if cloneCode != 0:
      (cloneCode, some(tr"$#: failed to clone git repository" % [base]))
    else:
      (0, none(string))

proc cloneAurReposWithPackageInfos*(config: Config, rpcInfos: seq[RpcPackageInfo],
  keepRepos: bool, progressCallback: (int, int) -> void, dropPrivileges: bool):
  (seq[PackageInfo], seq[PackageInfo], seq[string], seq[string]) =
  let bases: seq[tuple[base: string, gitUrl: string]] = rpcInfos
    .map(i => (i.base, i.gitUrl)).deduplicate

  progressCallback(0, bases.len)

  proc cloneNext(index: int, pkgInfos: List[PackageInfo], paths: List[string],
    errors: List[string]): (seq[PackageInfo], seq[string], seq[string]) =
    if index >= bases.len:
      (toSeq(pkgInfos.items), toSeq(paths.items), toSeq(errors.items))
    else:
      let repoPath = repoPath(config.tmpRoot(dropPrivileges), bases[index].base)
      removeDirQuiet(repoPath)

      let (cloneCode, cloneErrorMessage) = cloneAurRepo(config,
        bases[index].base, bases[index].gitUrl, dropPrivileges)

      progressCallback(index + 1, bases.len)

      if cloneCode != 0:
        removeDirQuiet(repoPath)
        cloneNext(index + 1, pkgInfos, paths, cloneErrorMessage.map(m => m ^& errors).get(errors))
      else:
        let srcInfos = try:
          readFile(repoPath & "/.SRCINFO")
        except:
          ""

        let addPkgInfos = parseSrcInfo("aur", srcInfos, config.arch,
          bases[index].gitUrl, none(string), rpcInfos)
        if keepRepos:
          cloneNext(index + 1, addPkgInfos ^& pkgInfos, repoPath ^& paths, errors)
        else:
          removeDirQuiet(repoPath)
          cloneNext(index + 1, addPkgInfos ^& pkgInfos, paths, errors)

  let (fullPkgInfos, paths, errors) = cloneNext(0, nil, nil, nil)
  let pkgInfosTable = fullPkgInfos.map(i => (i.name, i)).toTable
  let resultPkgInfos = lc[x | (y <- rpcInfos, x <- pkgInfosTable.opt(y.name)), PackageInfo]

  let names = rpcInfos.map(i => i.name).toSet
  let additionalPkgInfos = fullPkgInfos.filter(i => not (i.name in names))

  discard rmdir(config.tmpRoot(dropPrivileges))
  (resultPkgInfos, additionalPkgInfos, paths, errors)