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.

Implementation

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

  for (final dir in RepoFolderResolver.repoDirs(workspacePath)) {
    if (path.equals(dir.path, clone.path)) {
      continue;
    }
    if (RepoIdentity.of(dir).declaredIdentity == identity) {
      return dir;
    }
  }

  return null;
}