private async Task <bool> DownloadChangeAsync(IWin32Window owner)
        {
            await this.SwitchToMainThreadAsync();

            string change = _NO_TRANSLATE_Change.Text.Trim();

            if (string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text))
            {
                MessageBox.Show(owner, _selectRemote.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            if (string.IsNullOrEmpty(change))
            {
                MessageBox.Show(owner, _selectChange.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            GerritUtil.StartAgent(owner, Module, _NO_TRANSLATE_Remotes.Text);

            var reviewInfo = await LoadReviewInfoAsync();

            await this.SwitchToMainThreadAsync();

            if (reviewInfo?["id"] == null)
            {
                MessageBox.Show(owner, _cannotGetChangeDetails.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            // The user can enter both the Change-Id or the number. Here we
            // force the number to get prettier branches.

            change = (string)reviewInfo["number"];

            string topic = _NO_TRANSLATE_TopicBranch.Text.Trim();

            if (string.IsNullOrEmpty(topic))
            {
                var topicNode = (JValue)reviewInfo["topic"];

                topic = topicNode == null ? change : (string)topicNode.Value;
            }

            var    authorValue = (string)((JValue)reviewInfo["owner"]["name"]).Value;
            string author      = Regex.Replace(authorValue.ToLowerInvariant(), "\\W+", "_");
            string branchName  = "review/" + author + "/" + topic;
            var    refspec     = (string)((JValue)reviewInfo["currentPatchSet"]["ref"]).Value;

            var fetchCommand = UICommands.CreateRemoteCommand();

            fetchCommand.CommandText = FetchCommand(_NO_TRANSLATE_Remotes.Text, refspec);

            if (!RunCommand(fetchCommand, change))
            {
                return(false);
            }

            var checkoutCommand = UICommands.CreateRemoteCommand();

            checkoutCommand.CommandText = GitCommandHelpers.BranchCmd(branchName, "FETCH_HEAD", true);
            checkoutCommand.Completed  += (s, e) =>
            {
                if (e.IsError)
                {
                    if (e.Command.CommandText.Contains("already exists"))
                    {
                        // Recycle the current review branch.

                        var recycleCommand = UICommands.CreateRemoteCommand();

                        recycleCommand.CommandText = "checkout " + branchName;

                        if (!RunCommand(recycleCommand, change))
                        {
                            return;
                        }

                        var resetCommand = UICommands.CreateRemoteCommand();

                        resetCommand.CommandText = GitCommandHelpers.ResetCmd(ResetMode.Hard, "FETCH_HEAD");

                        if (!RunCommand(resetCommand, change))
                        {
                            return;
                        }

                        e.IsError = false;
                    }
                }
            };

            return(RunCommand(checkoutCommand, change));
        }
Ejemplo n.º 2
0
        private bool PublishChange(IWin32Window owner)
        {
            string branch = _NO_TRANSLATE_Branch.Text.Trim();

            if (string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text))
            {
                MessageBox.Show(owner, _selectRemote.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            if (string.IsNullOrEmpty(branch))
            {
                MessageBox.Show(owner, _selectBranch.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            GerritUtil.StartAgent(owner, Module, _NO_TRANSLATE_Remotes.Text);

            var builder = _capabilities.NewBuilder()
                          .WithReviewers(_NO_TRANSLATE_Reviewers.Text)
                          .WithCC(_NO_TRANSLATE_Cc.Text)
                          .WithTopic(_NO_TRANSLATE_Topic.Text)
                          .WithHashTag(_NO_TRANSLATE_Hashtag.Text)
                          .WithPublishType(((KeyValuePair <string, string>)PublishType.SelectedItem).Value);

            var pushCommand = UICommands.CreateRemoteCommand();

            pushCommand.CommandText = PushCmd(
                _NO_TRANSLATE_Remotes.Text,
                builder.Build(branch));

            pushCommand.Remote = _NO_TRANSLATE_Remotes.Text;
            pushCommand.Title  = _publishCaption.Text;

            pushCommand.Execute();

            if (!pushCommand.ErrorOccurred)
            {
                bool   hadNewChanges = false;
                string change        = null;

                foreach (string line in pushCommand.CommandOutput.Split('\n'))
                {
                    if (hadNewChanges)
                    {
                        const char esc = (char)27;
                        change = line
                                 .RemovePrefix("remote:")
                                 .SubstringUntilLast(esc)
                                 .Trim()
                                 .SubstringUntil(' ');
                        break;
                    }
                    else if (line.Contains("New Changes"))
                    {
                        hadNewChanges = true;
                    }
                }

                if (change != null)
                {
                    FormGerritChangeSubmitted.ShowSubmitted(owner, change);
                }
            }

            return(!pushCommand.ErrorOccurred);
        }