Exemple #1
0
 public void Exec(CommandContext context,
                  IConsole console, CancellationToken cancellationToken,
                  ProjOrRepoKeys projOrRepoKeys,
                  [Option('c',
                          Description = "Use the current directory as the working directly, else the repository directory is used")]
                  bool useCurrentDirectory)
 {
     using var repositories = _gitService.GetLocalRepos(projOrRepoKeys);
     new RepositoryExecutor(context, console, cancellationToken, useCurrentDirectory).ExecuteFor(repositories);
 }
Exemple #2
0
        public void Pull(
            IAnsiConsole console, CancellationToken cancellationToken,
            ProjOrRepoKeys projOrRepoKeys,
            DryRunArgs dryRunArgs,
            [Option] string?branch,
            [Option] bool prune = false)
        {
            using var repositories = _gitService.GetLocalRepos(projOrRepoKeys);

            if (dryRunArgs.IsDryRun)
            {
                console.WriteLine();
                console.WriteLine("dryrun: the following repositories would be pulled");
                var records = repositories.OrderBy(r => r.Name);
                PrintReposTable(AnsiConsole.Console, records);
            }
            else
            {
                repositories.SafelyForEach(
                    r => _gitService.PullLatest(r, prune, branch),
                    cancellationToken,
                    summarizeErrors: true);
            }
        }
Exemple #3
0
        public void Repo(
            IAnsiConsole console, CancellationToken cancellationToken,
            TableFormatModel tableFormatModel,
            ProjOrRepoKeys projOrRepoKeys,
            [Option('n', Description = "regex to match name. Prefix with $! for NOT matching.")]
            Regex?namePattern,
            [Option("branch", Description = "return only with the branch name. Prefix with $! for NOT matching.")]
            Regex?branchPattern,
            [Option('o', null, Description = "output only repo names")]
            bool outputNames = false,
            [Option("opk", Description = "output only project keys")]
            bool outputProjectKeys = false,
            [Option('l', Description = "where local branch is checked out")]
            bool isInLocalBranch = false,
            [Option('b', Description = "with local branches")]
            bool withLocalBranches = false,
            [Option('c', Description = "with changes: staged or unstaged")]
            bool withLocalChanges = false,
            [Option('s', Description = "with stashes")]
            bool withLocalStashes = false,
            [Option("or", Description = "treat options:b,w,s as `OR` instead of `AND`")]
            bool orLocalChecks = false,
            [Option('d', Description = "has no remote repo")]
            bool noRemote = false,
            [Option(Description = "inverts the filter")]
            bool not = false)
        {
            using var localRepos = _gitService.GetLocalRepos(projOrRepoKeys);

            var repos = localRepos.AsEnumerable();

            repos = repos.WhereMatches(r => r.Name, namePattern)
                    .WhereMatches(r => r.CurrentBranchName, branchPattern)
                    .WhereIf(noRemote, r => r.RemoteRepo is null)
                    .WhereIf(isInLocalBranch, r => r.IsInLocalBranch);

            if (withLocalBranches || withLocalChanges || withLocalStashes)
            {
                repos = orLocalChecks
                    ? repos.Where(r =>
                                  (!withLocalBranches || r.LocalBranchCount > 0) ||
                                  (!withLocalChanges || r.LocalChangesCount > 0) ||
                                  (!withLocalStashes || r.StashCount > 0)
                                  )
                    : repos.Where(r =>
                                  (!withLocalBranches || r.LocalBranchCount > 0) &&
                                  (!withLocalChanges || r.LocalChangesCount > 0) &&
                                  (!withLocalStashes || r.StashCount > 0)
                                  );
            }

            if (not)
            {
                var reposToHide = repos.Select(r => r.Name).ToHashSet();
                repos = localRepos.Where(r => !reposToHide.Contains(r.Name));
            }

            if (outputNames || Console.IsOutputRedirected)
            {
                repos
                .Select(r => r.Name)
                .ForEach(console.WriteLine);
            }
            else if (outputProjectKeys)
            {
                repos
                .Select(r => r.RemoteRepo?.ProjectKey)
                .Where(k => k is not null)
                .Distinct()
                .OrderBy(k => k)
                .ForEach(console.WriteLine !);
            }
            else
            {
                PrintReposTable(console, repos, tableFormatModel.GetTheme());
            }
        }
Exemple #4
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);
            }
        }