From a6c016ce89028d5195522a52f6dc0587c196fcb8 Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Sat, 19 May 2018 07:47:37 +0300 Subject: Clone package repos without tags to user cache --- src/common.nim | 59 ++++++++++++++++++++++++---------------------- src/feature/syncsource.nim | 14 +++++------ 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/common.nim b/src/common.nim index bd9573a..eb973c5 100644 --- a/src/common.nim +++ b/src/common.nim @@ -7,6 +7,9 @@ type CacheKind* {.pure.} = enum repositories + BareKind* {.pure.} = enum + repo + SyncFoundPackageInfo* = tuple[ base: string, version: string, @@ -442,51 +445,53 @@ proc reloadPkgInfos*(config: Config, path: string, pkgInfos: seq[PackageInfo]): else: pkgInfos -proc cloneBareRepo(config: Config, bareName: string, url: string, +template bareFullName*(bareKind: BareKind, bareName: string): string = + $bareKind & "-" & bareName & ".git" + +proc cloneBareRepo(config: Config, bareKind: BareKind, bareName: string, url: string, dropPrivileges: bool): Option[string] = - let fullName = bareName & ".git" - let repoPath = repoPath(config.tmpRoot(dropPrivileges), fullName) - removeDirQuiet(repoPath) + let fullName = bareFullName(bareKind, bareName) + let cachePath = config.userCache(dropPrivileges).cache(CacheKind.repositories) + let repoPath = repoPath(cachePath, fullName) if forkWait(() => (block: if not dropPrivileges or dropPrivileges(): - execResult(gitCmd, "-C", config.tmpRoot(dropPrivileges), - "clone", "-q", "--bare", url, fullName) + if existsDir(repoPath): + execResult(gitCmd, "-C", repoPath, "fetch", "-q", "--no-tags") + else: + execResult(gitCmd, "-C", cachePath, "clone", "-q", "--bare", "--no-tags", 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) +proc cloneBareRepos*(config: Config, bareKind: BareKind, gitRepos: seq[GitRepo], + progressCallback: (int, int) -> void, dropPrivileges: bool): (int, seq[string]) = + let message = ensureUserCacheOrError(config, CacheKind.repositories, dropPrivileges) if message.isSome: - (@[], @[message.unsafeGet]) + (0, @[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]) = + proc cloneNext(index: int, messages: List[string]): seq[string] = progressCallback(index, bare.len) if index >= bare.len: - (paths.reversed, messages.reversed) + toSeq(messages.reversed.items) else: let (bareName, url) = bare[index] - let repoPath = cloneBareRepo(config, bareName, url, dropPrivileges) + let repoPath = cloneBareRepo(config, bareKind, bareName, url, dropPrivileges) if repoPath.isSome: - cloneNext(index + 1, repoPath.unsafeGet ^& paths, messages) + cloneNext(index + 1, messages) else: let message = tr"$#: failed to clone git repository" % [bareName] - cloneNext(index + 1, paths, message ^& messages) + cloneNext(index + 1, message ^& messages) - let (paths, messages) = cloneNext(0, nil, nil) - (toSeq(paths.items), toSeq(messages.items)) + (bare.len, cloneNext(0, nil)) proc clonePackageRepoInternal(config: Config, base: string, version: string, git: GitRepo, dropPrivileges: bool): Option[string] = @@ -494,7 +499,8 @@ proc clonePackageRepoInternal(config: Config, base: string, version: string, removeDirQuiet(repoPath) let url = if git.bareName.isSome: - repoPath(config.tmpRoot(dropPrivileges), git.bareName.unsafeGet & ".git") + repoPath(config.userCache(dropPrivileges).cache(CacheKind.repositories), + bareFullName(BareKind.repo, git.bareName.unsafeGet)) else: git.url @@ -554,15 +560,14 @@ proc obtainBuildPkgInfosInternal(config: Config, bases: seq[LookupBaseGroup], if message.isSome: (@[], @[], @[message.unsafeGet]) else: - let (barePaths, berrors) = cloneBareRepos(config, lookupResults.map(r => r.git.unsafeGet), + let (bcount, berrors) = cloneBareRepos(config, BareKind.repo, + 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) + (newSeq[PackageInfo](), newSeq[string](), berrors) else: proc findCommitAndGetSrcInfo(base: string, version: string, repo: string, git: GitRepo): tuple[pkgInfos: seq[PackageInfo], path: Option[string]] = @@ -581,7 +586,7 @@ proc obtainBuildPkgInfosInternal(config: Config, bases: seq[LookupBaseGroup], 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) + progressCallback(bcount + index + 1, bcount + lookupResults.len) (res, index + 1), (list[tuple[pkgInfos: seq[PackageInfo], path: Option[string]]](), 0)) @@ -598,12 +603,10 @@ proc obtainBuildPkgInfosInternal(config: Config, bases: seq[LookupBaseGroup], .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) + (foundPkgInfos, paths, errorMessages) proc obtainBuildPkgInfos*[T: RpcPackageInfo](config: Config, pacmanTargets: seq[FullPackageTarget[T]], progressCallback: (int, int) -> void, diff --git a/src/feature/syncsource.nim b/src/feature/syncsource.nim index f7e2524..2bc88ee 100644 --- a/src/feature/syncsource.nim +++ b/src/feature/syncsource.nim @@ -33,14 +33,14 @@ proc getFilesOrClear(base: string, repoPath: string, gitSubdir: Option[string]): (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], seq[string]) = - let (barePaths, berrors) = cloneBareRepos(config, + 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(barePaths.len + index, barePaths.len + targets.len) + update(bcount + index, bcount + targets.len) if index >= targets.len: (results.reversed, messages.reversed) @@ -81,10 +81,10 @@ proc cloneRepositories(config: Config, targets: seq[BaseTarget], cloneNext(index + 1, results, message ^& messages) if berrors.len > 0: - (nil, barePaths, berrors) + (nil, berrors) else: let (results, cerrors) = cloneNext(0, nil, nil) - (results, barePaths, toSeq(cerrors.items)) + (results, toSeq(cerrors.items)) proc copyFiles(config: Config, quiet: bool, results: seq[CloneResult]): List[string] = proc copyNext(index: int, messages: List[string]): List[string] = @@ -136,7 +136,7 @@ proc cloneAndCopy(config: Config, quiet: bool, else: printProgressShare(config.progressBar, tr"cloning repositories") - let (results, barePaths, rerrors) = cloneRepositories(config, baseTargets, update) + let (results, rerrors) = cloneRepositories(config, baseTargets, update) terminate() for e in rerrors: printError(config.color, e) @@ -145,8 +145,6 @@ proc cloneAndCopy(config: Config, quiet: bool, for result in results: removeDirQuiet(result.path) - for path in barePaths: - removeDirQuiet(path) discard rmdir(config.tmpRootCurrent) if rerrors != nil and cerrors != nil: -- cgit v1.2.3-70-g09d2