Exemplo n.º 1
0
        /// <inheritdoc />
        protected override void CleanChanges(IPackage package, string cwd, bool isUpdate)
        {
            Git.CleanEnvironment();
            cwd = NormalizePath(cwd);

            var unpushed       = GetUnpushedChanges(package, cwd);
            var discardChanges = Config.Get(Settings.DiscardChanges);

            // For unpushed changes, we throw an exception unless
            // it is not interactive and is configured to discard
            // the changes.
            if (!string.IsNullOrEmpty(unpushed) && (IO.IsInteractive || discardChanges != "true"))
            {
                throw new RuntimeException(
                          $"Source directory \"{cwd}\" has unpushed changes on the current branch: {Environment.NewLine}{unpushed}");
            }

            var changes = GetLocalChanges(package, cwd);

            if (string.IsNullOrEmpty(changes))
            {
                return;
            }

            if (!IO.IsInteractive)
            {
                if (discardChanges == "true")
                {
                    DiscardChanges(cwd);
                    return;
                }

                if (discardChanges == "stash")
                {
                    if (!isUpdate)
                    {
                        base.CleanChanges(package, cwd, isUpdate);
                        return;
                    }

                    StashChanges(cwd);
                    return;
                }

                base.CleanChanges(package, cwd, isUpdate);
                return;
            }

            var changeLines = Arr.Map(Regex.Split(changes, @"\s*\r?\n\s*"), (line) => $"    {line}");

            IO.WriteError("    <error>The package has modified files:</error>");
            IO.WriteError(Arr.Slice(changeLines, 0, 10));

            if (changeLines.Length > 10)
            {
                IO.WriteError($"    <info>{changeLines.Length - 10} more files modified, choose \"v\" to view the full list.</info>");
            }

            while (true)
            {
                string answer = IO.Ask($"    <info>Discard changes [y,n,v,d,{(isUpdate ? "s," : string.Empty)}?,h]?", "?");
                switch (answer.Trim())
                {
                case "y":
                    DiscardChanges(cwd);
                    return;

                case "s":
                    if (!isUpdate)
                    {
                        goto help;
                    }

                    StashChanges(cwd);
                    return;

                case "n":
                    throw new RuntimeException("Update aborted. because the user refused to operate.");

                case "v":
                    IO.WriteError(changeLines);
                    break;

                case "d":
                    ViewDiff(cwd);
                    break;

                default:
help:
                    var action = isUpdate ? "update" : "uninstall";
                    IO.WriteError(new[]
                    {
                        $"    y - discard changes and apply the {action}.",
                        $"    n - abort the {action} and let you manually clean things up.",
                        "    v - view modified files.",
                        "    d - view local modifications (diff).",
                    });

                    if (isUpdate)
                    {
                        IO.WriteError("    s - stash changes and try to reapply them after the update.");
                    }

                    IO.WriteError("    ? - print help(h).");
                    break;
                }
            }
        }