Esempio n. 1
0
        public void Delete(
            IAnsiConsole console, CancellationToken cancellationToken,
            ProjOrRepoKeys projOrRepoKeys,
            DryRunArgs dryRunArgs)
        {
            using var repositories = _gitService.GetLocalRepos(projOrRepoKeys);

            IEnumerable <string> BuildWarning(LocalRepo r)
            {
                if (r.LocalChangesCount > 0)
                {
                    yield return($"{r.LocalChangesCount} local changes");
                }
                if (r.LocalBranchCount > 0)
                {
                    yield return($"{r.LocalBranchCount} local branches");
                }
                if (r.StashCount > 0)
                {
                    yield return($"{r.StashCount} local stashes");
                }
            }

            if (dryRunArgs.IsDryRun)
            {
                console.WriteLine();
                console.WriteLine("dryrun: the following repositories would be deleted");
                var records = repositories.OrderBy(r => r.Name);
                PrintReposTable(console, records);
            }
            else
            {
                repositories.SafelyForEach(
                    r =>
                {
                    var warning = BuildWarning(r).ToCsv();
                    if (!warning.IsNullOrEmpty())
                    {
                        if (!console.Confirm($"{r.Name} has {warning}. Would you still like to delete?"))
                        {
                            console.WriteLine($"skipping {r.Name}");
                            return;
                        }
                    }

                    try
                    {
                        r.Dispose();
                        console.WriteLine($"Deleting {r.FullPath}");
                        Directory.Delete(r.FullPath, true);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        r.FullPath.SetFileAttributes(FileAttributes.Normal);
                        Directory.Delete(r.FullPath, true);
                    }
                },
                    cancellationToken,
                    summarizeErrors: true);
            }
        }