aboutsummaryrefslogtreecommitdiff
path: root/src/feature/syncsource.nim
blob: e0531bce2a035ca8bee3ead573afe2a6eb037169 (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
import
  future, options, os, posix, sequtils, strutils, tables,
  "../args", "../aur", "../common", "../config", "../format", "../lists",
    "../package", "../pacman", "../utils",
  "../wrapper/alpm"

type
  BaseTarget = tuple[
    base: string,
    version: string,
    destination: string,
    aurGitUrl: Option[string],
    gitRepo: Option[GitRepo]
  ]

  CloneResult = tuple[
    base: string,
    path: string,
    files: seq[string],
    destination: string
  ]

proc getFilesOrClear(base: string, repoPath: string, gitSubdir: Option[string]):
  (seq[string], Option[string]) =
  let rawFiles = getGitFiles(repoPath, gitSubdir, false)
    .filter(f => f != ".gitignore" and f != ".SRCINFO" and f.find('/') < 0)
    .map(f => gitSubdir.map(s => s & "/" & f).get(f))

  if rawFiles.len > 0:
    (rawFiles, none(string))
  else:
    removeDirQuiet(repoPath)
    (newSeq[string](), some(tr"$#: failed to clone git repository" % [base]))

proc cloneRepositories(config: Config, targets: seq[BaseTarget],
  update: (int, int) -> void): (List[CloneResult], seq[string]) =
  let (bcount, berrors) = cloneBareRepos(config, BareKind.repo,
    targets.filter(t => t.gitRepo.isSome).map(t => t.gitRepo.unsafeGet),
    proc (progress: int, count: int) = update(progress, count + targets.len), false)

  proc cloneNext(index: int, results: List[CloneResult], messages: List[string]):
    (List[CloneResult], List[string]) =
    update(bcount + index, bcount + targets.len)

    if index >= targets.len:
      (results.reversed, messages.reversed)
    else:
      let target = targets[index]
      let repoPath = repoPath(config.tmpRootCurrent, target.base)
      removeDirQuiet(repoPath)

      if target.aurGitUrl.isSome:
        let (cloneCode, cerror) = cloneAurRepo(config,
          target.base, target.aurGitUrl.unsafeGet, false)

        if cloneCode != 0:
          cloneNext(index + 1, results, toSeq(cerror.items) ^& messages)
        else:
          let (files, ferror) = getFilesOrClear(target.base, repoPath, none(string))
          if ferror.isSome:
            cloneNext(index + 1, results, ferror.unsafeGet ^& messages)
          else:
            cloneNext(index + 1, (target.base, repoPath, files,
              target.destination) ^& results, messages)
      elif target.gitRepo.isSome:
        let gitRepo = target.gitRepo.unsafeGet
        let cerror = clonePackageRepo(config, target.base,
          target.version, gitRepo, false)

        if cerror.isSome:
          cloneNext(index + 1, results, cerror.unsafeGet ^& messages)
        else:
          let (files, ferror) = getFilesOrClear(target.base, repoPath, some(gitRepo.path))
          if ferror.isSome:
            cloneNext(index + 1, results, ferror.unsafeGet ^& messages)
          else:
            cloneNext(index + 1, (target.base, repoPath, files,
              target.destination) ^& results, messages)
      else:
        let message = tr"$#: repository not found" % [target.base]
        cloneNext(index + 1, results, message ^& messages)

  if berrors.len > 0:
    (nil, berrors)
  else:
    let (results, cerrors) = cloneNext(0, nil, nil)
    (results, toSeq(cerrors.items))

proc copyFiles(config: Config, quiet: bool, results: seq[CloneResult]): List[string] =
  proc copyNext(index: int, messages: List[string]): List[string] =
    if index >= results.len:
      messages.reversed
    else:
      let res = results[index]
      discard mkdir(res.destination, 0o755)

      let error = try:
        for f in res.files:
          let index = f.rfind('/')
          let name = if index >= 0: f[index + 1 .. ^1] else: f
          let dest = if res.destination == ".": name else: res.destination & "/" & name
          moveFile(res.path & "/" & f, dest)
          printFile(config.color, quiet, res.base, dest)
        none(string)
      except OSError:
        some(tr"$#: failed to move files" % [res.base])

      copyNext(index + 1, toSeq(error.items) ^& messages)

  copyNext(0, nil)

proc cloneAndCopy(config: Config, quiet: bool,
  fullTargets: seq[FullPackageTarget[RpcPackageInfo]]): int =
  let baseTargets = fullTargets.foldl(block:
    let bases = a.map(x => x.base)
    if b.isAurTargetFull(config.aurRepo):
      let rpcInfo = b.pkgInfo.get
      if rpcInfo.base in bases:
        a
      else:
        a & (rpcInfo.base, rpcInfo.version, b.destination.get(rpcInfo.base),
          some(rpcInfo.gitUrl), none(GitRepo))
    else:
      let foundInfo = b.foundInfos[0]
      let pkg = foundInfo.pkg.get
      if pkg.base in bases:
        a
      else:
        let git = lookupGitRepo(foundInfo.repo, pkg.base, pkg.arch.get)
        a & (pkg.base, pkg.version, b.destination.get(pkg.base),
          none(string), git),
    newSeq[BaseTarget]())

  let (update, terminate) = if quiet:
      (proc (a: int, b: int) {.closure.} = discard, proc () {.closure.} = discard)
    else:
      printProgressShare(config.progressBar, tr"cloning repositories")

  let (results, rerrors) = cloneRepositories(config, baseTargets, update)
  terminate()
  for e in rerrors: printError(config.color, e)

  let cerrors = copyFiles(config, quiet, toSeq(results.items))
  for e in cerrors: printError(config.color, e)

  for result in results:
    removeDirQuiet(result.path)
  discard rmdir(config.tmpRootCurrent)

  if rerrors != nil and cerrors != nil:
    1
  else:
    0

proc handleSyncSource*(args: seq[Argument], config: Config): int =
  let (refreshCode, _) = checkAndRefresh(config.color, args)
  if refreshCode != 0:
    refreshCode
  else:
    let quiet = args.check(%%%"quiet")
    let targets = args.packageTargets(true)

    if targets.len == 0:
      printError(config.color, trp("no targets specified (use -h for help)\n"))
      1
    else:
      let (syncTargets, checkAurNames) = withAlpmConfig(config, true, handle, dbs, errors):
        for e in errors: printError(config.color, e)
        findSyncTargets(handle, dbs, targets, config.aurRepo, false, false)

      let (rpcInfos, aerrors) = getRpcPackageInfos(checkAurNames,
        config.aurRepo, config.downloadTimeout)
      for e in aerrors: printError(config.color, e)

      let notFoundTargets = filterNotFoundSyncTargets(syncTargets,
        rpcInfos, initTable[string, PackageReference](), config.aurRepo)

      if notFoundTargets.len > 0:
        printSyncNotFound(config, notFoundTargets)
        1
      else:
        let fullTargets = mapAurTargets[RpcPackageInfo](syncTargets, rpcInfos, config.aurRepo)
        cloneAndCopy(config, quiet, fullTargets)