existingCheckoutOf function

Directory? existingCheckoutOf({
  1. required String workspacePath,
  2. required Directory clone,
})

The checkout workspacePath already holds of the repository just cloned into clone, or null when clone is the only one.

Renaming a repository does not invalidate its former name — the platform redirects it — so cloning a manifest's outdated name succeeds and yields a second checkout of a repository that is already there, in a folder named after a repository that does not exist any more.

Only the repository the manifests declare counts as proof here. Two packages that merely share a name are a different problem, and the price of getting this wrong is a repository that was legitimately added being thrown away again.

A checkout outside the folder its manifests name does not count when the clone sits in its own — the rule of DuplicateRepoCleanup: the clone is current and the checkout a leftover of a former name, see isFormerNameCheckoutOf.

Implementation

Directory? existingCheckoutOf({
  required String workspacePath,
  required Directory clone,
}) {
  final cloned = RepoIdentity.of(clone);
  if (cloned.declaredIdentity == null) {
    return null;
  }

  for (final dir in RepoFolderResolver.repoDirs(workspacePath)) {
    if (path.equals(dir.path, clone.path)) {
      continue;
    }
    final other = RepoIdentity.of(dir);
    if (!cloned.isSameRepoAs(other) ||
        (cloned.sitsInDeclaredRepoFolder && !other.sitsInDeclaredRepoFolder)) {
      continue;
    }
    return dir;
  }

  return null;
}