Ejemplo n.º 1
0
        public DialogResult PullChanges(IWin32Window owner)
        {
            if (!ShouldPullChanges())
            {
                return(DialogResult.No);
            }

            UpdateSettingsDuringPull();

            DialogResult dr = ShouldRebaseMergeCommit();

            if (dr != DialogResult.Yes)
            {
                return(dr);
            }

            if (!Fetch.Checked && Branches.Text.IsNullOrWhiteSpace() && Module.IsDetachedHead())
            {
                int idx = PSTaskDialog.cTaskDialog.ShowCommandBox(owner,
                                                                  _notOnBranchCaption.Text,
                                                                  _notOnBranchMainInstruction.Text,
                                                                  _notOnBranch.Text,
                                                                  _notOnBranchButtons.Text,
                                                                  true);
                switch (idx)
                {
                case 0:
                    if (!UICommands.StartCheckoutBranch(owner, ""))
                    {
                        return(DialogResult.Cancel);
                    }
                    break;

                case -1:
                    return(DialogResult.Cancel);
                }
            }

            if (PullFromUrl.Checked)
            {
                Repositories.RepositoryHistory.AddMostRecentRepository(comboBoxPullSource.Text);
            }

            var source = CalculateSource();

            ScriptManager.RunEventScripts(Module, ScriptEvent.BeforePull);

            var stashed = CalculateStashedValue(owner);

            using (FormProcess process = CreateFormProcess(source))
            {
                ShowProcessDialogBox(owner, source, process);

                return(EvaluateProcessDialogResults(owner, process, stashed));
            }
        }
Ejemplo n.º 2
0
        public DialogResult PullChanges(IWin32Window owner)
        {
            if (!ShouldPullChanges())
            {
                return(DialogResult.No);
            }

            DialogResult dr = ShouldRebaseMergeCommit();

            if (dr != DialogResult.Yes)
            {
                return(dr);
            }

            if (!Fetch.Checked && Branches.Text.IsNullOrWhiteSpace() && Module.IsDetachedHead())
            {
                int idx = PSTaskDialog.cTaskDialog.ShowCommandBox(
                    owner,
                    _notOnBranchCaption.Text,
                    _notOnBranchMainInstruction.Text,
                    _notOnBranch.Text,
                    _notOnBranchButtons.Text,
                    ShowCancelButton: true);

                switch (idx)
                {
                case 0:
                    if (!UICommands.StartCheckoutBranch(owner))
                    {
                        return(DialogResult.Cancel);
                    }

                    break;

                case -1:
                    return(DialogResult.Cancel);
                }
            }

            if (PullFromUrl.Checked && Directory.Exists(comboBoxPullSource.Text))
            {
                var path = comboBoxPullSource.Text;
                ThreadHelper.JoinableTaskFactory.Run(() => RepositoryHistoryManager.Remotes.AddAsMostRecentAsync(path));
            }

            var source = CalculateSource();

            if (!CalculateLocalBranch(source, out var curLocalBranch, out var curRemoteBranch))
            {
                return(DialogResult.No);
            }

            ScriptManager.RunEventScripts(this, ScriptEvent.BeforePull);

            var stashed = CalculateStashedValue(owner);

            using (var form = CreateFormProcess(source, curLocalBranch, curRemoteBranch))
            {
                if (!IsPullAll())
                {
                    form.Remote = source;
                }

                form.ShowDialog(owner);
                ErrorOccurred = form.ErrorOccurred();

                try
                {
                    bool aborted = form.DialogResult == DialogResult.Abort;
                    if (!aborted && !Fetch.Checked)
                    {
                        if (!ErrorOccurred)
                        {
                            if (!InitModules())
                            {
                                UICommands.UpdateSubmodules(owner);
                            }
                        }
                        else
                        {
                            CheckMergeConflictsOnError();
                        }
                    }
                }
                finally
                {
                    if (stashed)
                    {
                        PopStash();
                    }

                    ScriptManager.RunEventScripts(this, ScriptEvent.AfterPull);
                }
            }

            return(DialogResult.OK);

            bool ShouldPullChanges()
            {
                if (PullFromUrl.Checked && string.IsNullOrEmpty(comboBoxPullSource.Text))
                {
                    MessageBox.Show(this, _selectSourceDirectory.Text);
                    return(false);
                }

                if (PullFromRemote.Checked && string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text) && !IsPullAll())
                {
                    MessageBox.Show(this, _selectRemoteRepository.Text);
                    return(false);
                }

                if (!Fetch.Checked && Branches.Text == "*")
                {
                    MessageBox.Show(this, _fetchAllBranchesCanOnlyWithFetch.Text);
                    return(false);
                }

                return(true);
            }

            string CalculateSource()
            {
                if (PullFromUrl.Checked)
                {
                    return(comboBoxPullSource.Text);
                }

                LoadPuttyKey();
                return(IsPullAll() ? "--all" : _NO_TRANSLATE_Remotes.Text);
            }

            bool InitModules()
            {
                if (!File.Exists(_fullPathResolver.Resolve(".gitmodules")))
                {
                    return(false);
                }

                if (!IsSubmodulesInitialized())
                {
                    if (AskIfSubmodulesShouldBeInitialized())
                    {
                        UICommands.StartUpdateSubmodulesDialog(this);
                    }

                    return(true);
                }

                return(false);

                bool IsSubmodulesInitialized()
                {
                    // Fast submodules check
                    return(Module.GetSubmodulesLocalPaths()
                           .Select(submoduleName => Module.GetSubmodule(submoduleName))
                           .All(submodule => submodule.IsValidGitWorkingDir()));
                }

                bool AskIfSubmodulesShouldBeInitialized()
                {
                    return(MessageBox.Show(this, _questionInitSubmodules.Text, _questionInitSubmodulesCaption.Text,
                                           MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
                }
            }

            void CheckMergeConflictsOnError()
            {
                // Rebase failed -> special 'rebase' merge conflict
                if (Rebase.Checked && Module.InTheMiddleOfRebase())
                {
                    UICommands.StartTheContinueRebaseDialog(owner);
                }
                else if (Module.InTheMiddleOfAction())
                {
                    MergeConflictHandler.HandleMergeConflicts(UICommands, owner);
                }
            }

            void PopStash()
            {
                if (ErrorOccurred || Module.InTheMiddleOfAction())
                {
                    return;
                }

                bool?messageBoxResult = AppSettings.AutoPopStashAfterPull;

                if (messageBoxResult == null)
                {
                    DialogResult res = PSTaskDialog.cTaskDialog.MessageBox(
                        owner,
                        _applyStashedItemsAgainCaption.Text,
                        "",
                        _applyStashedItemsAgain.Text,
                        "",
                        "",
                        _dontShowAgain.Text,
                        PSTaskDialog.eTaskDialogButtons.YesNo,
                        PSTaskDialog.eSysIcons.Question,
                        PSTaskDialog.eSysIcons.Question);
                    messageBoxResult = res == DialogResult.Yes;
                    if (PSTaskDialog.cTaskDialog.VerificationChecked)
                    {
                        AppSettings.AutoPopStashAfterPull = messageBoxResult;
                    }
                }

                if ((bool)messageBoxResult)
                {
                    UICommands.StashPop(owner);
                }
            }
        }
Ejemplo n.º 3
0
 public bool Checkout()
 {
     return(UICommands.StartCheckoutBranch(FullPath, false));
 }
Ejemplo n.º 4
0
 public void Checkout()
 {
     UICommands.StartCheckoutBranch(FullPath, false);
 }
Ejemplo n.º 5
0
        public DialogResult PullChanges(IWin32Window owner)
        {
            if (!ShouldPullChanges())
            {
                return(DialogResult.No);
            }

            UpdateSettingsDuringPull();

            DialogResult dr = ShouldRebaseMergeCommit();

            if (dr != DialogResult.Yes)
            {
                return(dr);
            }

            if (!Fetch.Checked && Branches.Text.IsNullOrWhiteSpace() && Module.IsDetachedHead())
            {
                int idx = PSTaskDialog.cTaskDialog.ShowCommandBox(owner,
                                                                  _notOnBranchCaption.Text,
                                                                  _notOnBranchMainInstruction.Text,
                                                                  _notOnBranch.Text,
                                                                  _notOnBranchButtons.Text,
                                                                  true);
                switch (idx)
                {
                case 0:
                    if (!UICommands.StartCheckoutBranch(owner, new[] { "" }))
                    {
                        return(DialogResult.Cancel);
                    }
                    break;

                case -1:
                    return(DialogResult.Cancel);
                }
            }

            if (PullFromUrl.Checked && Directory.Exists(comboBoxPullSource.Text))
            {
                Repositories.RepositoryHistory.AddMostRecentRepository(comboBoxPullSource.Text);
            }

            var source = CalculateSource();

            string curLocalBranch;
            string curRemoteBranch;

            if (!CalculateLocalBranch(source, out curLocalBranch, out curRemoteBranch))
            {
                return(DialogResult.No);
            }

            ScriptManager.RunEventScripts(this, ScriptEvent.BeforePull);

            var stashed = CalculateStashedValue(owner);

            using (FormProcess process = CreateFormProcess(source, curLocalBranch, curRemoteBranch))
            {
                ShowProcessDialogBox(owner, source, process);

                try
                {
                    bool aborted = process != null && process.DialogResult == DialogResult.Abort;
                    if (!aborted && !Fetch.Checked)
                    {
                        if (!ErrorOccurred)
                        {
                            if (!InitModules())
                            {
                                UICommands.UpdateSubmodules(owner);
                            }
                        }
                        else
                        {
                            CheckMergeConflictsOnError(owner);
                        }
                    }
                }
                finally
                {
                    if (stashed)
                    {
                        PopStash(owner);
                    }

                    ScriptManager.RunEventScripts(this, ScriptEvent.AfterPull);
                }

                return(DialogResult.OK);
            }
        }
Ejemplo n.º 6
0
        public DialogResult PullChanges(IWin32Window owner)
        {
            if (!ShouldPullChanges())
            {
                return(DialogResult.No);
            }

            UpdateSettingsDuringPull();

            DialogResult dr = ShouldRebaseMergeCommit();

            if (dr != DialogResult.Yes)
            {
                return(dr);
            }

            if (!Fetch.Checked && string.IsNullOrWhiteSpace(Branches.Text) && Module.IsDetachedHead())
            {
                int dialogResult = -1;

                using var dialog = new TaskDialog
                      {
                          OwnerWindowHandle = owner.Handle,
                          Text            = _notOnBranch.Text,
                          InstructionText = Strings.ErrorInstructionNotOnBranch,
                          Caption         = Strings.ErrorCaptionNotOnBranch,
                          StandardButtons = TaskDialogStandardButtons.Cancel,
                          Icon            = TaskDialogStandardIcon.Error,
                          Cancelable      = true,
                      };
                var btnCheckout = new TaskDialogCommandLink("Checkout", null, Strings.ButtonCheckoutBranch);
                btnCheckout.Click += (s, e) =>
                {
                    dialogResult = 0;
                    dialog.Close();
                };
                var btnContinue = new TaskDialogCommandLink("Continue", null, Strings.ButtonContinue);
                btnContinue.Click += (s, e) =>
                {
                    dialogResult = 1;
                    dialog.Close();
                };
                dialog.Controls.Add(btnCheckout);
                dialog.Controls.Add(btnContinue);

                dialog.Show();

                switch (dialogResult)
                {
                case 0:
                    if (!UICommands.StartCheckoutBranch(owner))
                    {
                        return(DialogResult.Cancel);
                    }

                    break;

                case -1:
                    return(DialogResult.Cancel);
                }
            }

            if (PullFromUrl.Checked && Directory.Exists(comboBoxPullSource.Text))
            {
                var path = comboBoxPullSource.Text;
                ThreadHelper.JoinableTaskFactory.Run(() => RepositoryHistoryManager.Remotes.AddAsMostRecentAsync(path));
            }

            var source = CalculateSource();

            if (!CalculateLocalBranch(source, out var curLocalBranch, out var curRemoteBranch))
            {
                return(DialogResult.No);
            }

            executeBeforeScripts();

            var stashed = CalculateStashedValue(owner);

            using (var form = CreateFormProcess(source, curLocalBranch, curRemoteBranch))
            {
                if (!IsPullAll())
                {
                    form.Remote = source;
                }

                form.ShowDialog(owner);
                ErrorOccurred = form.ErrorOccurred();

                bool executeScripts = false;
                try
                {
                    bool aborted = form.DialogResult == DialogResult.Abort;
                    executeScripts = !aborted && !ErrorOccurred;

                    if (!aborted && !Fetch.Checked)
                    {
                        if (!ErrorOccurred)
                        {
                            if (!InitModules())
                            {
                                UICommands.UpdateSubmodules(owner);
                            }
                        }
                        else
                        {
                            executeScripts |= CheckMergeConflictsOnError();
                        }
                    }
                }
                finally
                {
                    if (stashed)
                    {
                        PopStash();
                    }

                    if (executeScripts)
                    {
                        executeAfterScripts();
                    }
                }
            }

            return(DialogResult.OK);

            bool ShouldPullChanges()
            {
                if (PullFromUrl.Checked && string.IsNullOrEmpty(comboBoxPullSource.Text))
                {
                    MessageBox.Show(this, _selectSourceDirectory.Text, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                if (PullFromRemote.Checked && string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text) && !IsPullAll())
                {
                    MessageBox.Show(this, _selectRemoteRepository.Text, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                if (!Fetch.Checked && Branches.Text == "*")
                {
                    MessageBox.Show(this, _fetchAllBranchesCanOnlyWithFetch.Text, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                return(true);
            }

            string CalculateSource()
            {
                if (PullFromUrl.Checked)
                {
                    return(comboBoxPullSource.Text);
                }

                LoadPuttyKey();
                return(IsPullAll() ? "--all" : _NO_TRANSLATE_Remotes.Text);
            }

            bool InitModules()
            {
                if (!File.Exists(_fullPathResolver.Resolve(".gitmodules")))
                {
                    return(false);
                }

                if (!IsSubmodulesInitialized())
                {
                    if (AskIfSubmodulesShouldBeInitialized())
                    {
                        UICommands.StartUpdateSubmodulesDialog(this);
                    }

                    return(true);
                }

                return(false);

                bool IsSubmodulesInitialized()
                {
                    // Fast submodules check
                    return(Module.GetSubmodulesLocalPaths()
                           .Select(submoduleName => Module.GetSubmodule(submoduleName))
                           .All(submodule => submodule.IsValidGitWorkingDir()));
                }

                bool AskIfSubmodulesShouldBeInitialized()
                {
                    return(MessageBox.Show(this, _questionInitSubmodules.Text, _questionInitSubmodulesCaption.Text,
                                           MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes);
                }
            }

            bool CheckMergeConflictsOnError()
            {
                // Rebase failed -> special 'rebase' merge conflict
                if (Rebase.Checked && Module.InTheMiddleOfRebase())
                {
                    return(UICommands.StartTheContinueRebaseDialog(owner));
                }
                else if (Module.InTheMiddleOfAction())
                {
                    return(MergeConflictHandler.HandleMergeConflicts(UICommands, owner));
                }

                return(false);
            }

            void PopStash()
            {
                if (ErrorOccurred || Module.InTheMiddleOfAction())
                {
                    return;
                }

                bool?messageBoxResult = AppSettings.AutoPopStashAfterPull;

                if (messageBoxResult == null)
                {
                    using var dialog = new TaskDialog
                          {
                              OwnerWindowHandle = owner.Handle,
                              Text               = _applyStashedItemsAgain.Text,
                              Caption            = _applyStashedItemsAgainCaption.Text,
                              StandardButtons    = TaskDialogStandardButtons.Yes | TaskDialogStandardButtons.No,
                              Icon               = TaskDialogStandardIcon.Information,
                              FooterCheckBoxText = _dontShowAgain.Text,
                              FooterIcon         = TaskDialogStandardIcon.Information,
                              StartupLocation    = TaskDialogStartupLocation.CenterOwner,
                          };

                    messageBoxResult = dialog.Show() == TaskDialogResult.Yes;

                    if (dialog.FooterCheckBoxChecked == true)
                    {
                        AppSettings.AutoPopStashAfterPull = messageBoxResult;
                    }
                }

                if ((bool)messageBoxResult)
                {
                    UICommands.StashPop(owner);
                }
            }

            void executeBeforeScripts()
            {
                // Request to pull/merge in addition to the fetch
                if (!Fetch.Checked)
                {
                    ScriptManager.RunEventScripts(this, ScriptEvent.BeforePull);
                }

                ScriptManager.RunEventScripts(this, ScriptEvent.BeforeFetch);
            }

            void executeAfterScripts()
            {
                ScriptManager.RunEventScripts(this, ScriptEvent.AfterFetch);

                // Request to pull/merge in addition to the fetch
                if (!Fetch.Checked)
                {
                    ScriptManager.RunEventScripts(this, ScriptEvent.AfterPull);
                }
            }
        }