Exemple #1
0
        private string DownloadFromScp(GerritSettings settings)
        {
            // This is a very quick and dirty "implementation" of the scp
            // protocol. By sending the 0's as input, we trigger scp to
            // send the file.

            string content = GerritUtil.RunGerritCommand(
                _mainForm,
                _gitUiCommands.GitModule,
                "scp -f hooks/commit-msg",
                settings.DefaultRemote,
                new byte[] { 0, 0, 0, 0, 0, 0, 0 }
                );

            // The first line of the output contains the file we're receiving
            // in a format like "C0755 4248 commit-msg".

            if (String.IsNullOrEmpty(content))
            {
                return(null);
            }

            int index = content.IndexOf('\n');

            if (index == -1)
            {
                return(null);
            }

            string header = content.Substring(0, index);

            if (!header.EndsWith(" commit-msg"))
            {
                return(null);
            }

            // This looks like a valid scp response; return the rest of the
            // response.

            content = content.Substring(index + 1);

            // The file should be terminated by a nul.

            index = content.LastIndexOf((char)0);

            Debug.Assert(index == content.Length - 1);

            if (index != -1)
            {
                content = content.Substring(0, index);
            }

            return(content);
        }
Exemple #2
0
        private JObject LoadReviewInfo()
        {
            var fetchUrl = GerritUtil.GetFetchUrl(Module, _currentBranchRemote);

            string projectName = fetchUrl.AbsolutePath.TrimStart('/');

            if (projectName.EndsWith(".git"))
            {
                projectName = projectName.Substring(0, projectName.Length - 4);
            }

            string change = GerritUtil.RunGerritCommand(
                this,
                Module,
                String.Format(
                    "gerrit query --format=JSON project:{0} --current-patch-set change:{1}",
                    projectName,
                    _NO_TRANSLATE_Change.Text
                    ),
                fetchUrl,
                _currentBranchRemote,
                null
                );

            foreach (string line in change.Split('\n'))
            {
                try
                {
                    return(JObject.Parse(line));
                }
                catch
                {
                    // Ignore exceptions.
                }
            }

            return(null);
        }
Exemple #3
0
        private bool DownloadChange(IWin32Window owner)
        {
            string change = _NO_TRANSLATE_Change.Text.Trim();

            if (string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text))
            {
                MessageBox.Show(owner, _selectRemote.Text);
                return(false);
            }
            if (string.IsNullOrEmpty(change))
            {
                MessageBox.Show(owner, _selectChange.Text);
                return(false);
            }

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

            var reviewInfo = LoadReviewInfo();

            if (reviewInfo == null || reviewInfo["id"] == null)
            {
                MessageBox.Show(owner, _cannotGetChangeDetails.Text);
                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;
            }

            string authorValue = (string)((JValue)reviewInfo["owner"]["name"]).Value;
            string author      = Regex.Replace(authorValue.ToLowerInvariant(), "\\W+", "_");
            string branchName  = "review/" + author + "/" + topic;
            string 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.ResetHardCmd("FETCH_HEAD");

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

                        e.IsError = false;
                    }
                }
            };

            return(RunCommand(checkoutCommand, change));
        }
Exemple #4
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);
                return(false);
            }

            if (string.IsNullOrEmpty(branch))
            {
                MessageBox.Show(owner, _selectBranch.Text);
                return(false);
            }

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

            string targetRef = PublishDraft.Checked ? "drafts" : "publish";

            var pushCommand = UICommands.CreateRemoteCommand();

            string targetBranch = "refs/" + targetRef + "/" + branch;
            string topic        = _NO_TRANSLATE_Topic.Text.Trim();

            if (!string.IsNullOrEmpty(topic))
            {
                targetBranch += "/" + topic;
            }

            string reviewers = _NO_TRANSLATE_Reviewers.Text.Trim();

            if (!string.IsNullOrEmpty(reviewers))
            {
                string formattedReviewers = string.Join(",", reviewers.Split(' ')
                                                        .Where(r => !string.IsNullOrEmpty(r))
                                                        .Select(r => "r=" + r));
                if (!formattedReviewers.IsNullOrEmpty())
                {
                    targetBranch += "%" + formattedReviewers;
                }
            }

            pushCommand.CommandText = PushCmd(
                _NO_TRANSLATE_Remotes.Text,
                targetBranch);
            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(true);
        }
        private bool PublishChange(IWin32Window owner)
        {
            string branch = _NO_TRANSLATE_Branch.Text.Trim();

            if (string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text))
            {
                MessageBox.Show(owner, _selectRemote.Text);
                return(false);
            }

            if (string.IsNullOrEmpty(branch))
            {
                MessageBox.Show(owner, _selectBranch.Text);
                return(false);
            }

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

            List <string> additionalOptions = new List <string>();

            string reviewers = _NO_TRANSLATE_Reviewers.Text.Trim();

            if (!string.IsNullOrEmpty(reviewers))
            {
                additionalOptions.AddRange(reviewers.Split(new[] { ' ', ',', ';', '|' })
                                           .Where(r => !string.IsNullOrEmpty(r))
                                           .Select(r => "r=" + r));
            }

            string cc = _NO_TRANSLATE_Cc.Text.Trim();

            if (!string.IsNullOrEmpty(cc))
            {
                additionalOptions.AddRange(cc.Split(new[] { ' ', ',', ';', '|' })
                                           .Where(r => !string.IsNullOrEmpty(r))
                                           .Select(r => "cc=" + r));
            }

            string topic = _NO_TRANSLATE_Topic.Text.Trim();

            if (!string.IsNullOrEmpty(topic))
            {
                additionalOptions.Add("topic=" + topic);
            }

            string hashtag = _NO_TRANSLATE_Hashtag.Text.Trim();

            if (!string.IsNullOrEmpty(hashtag))
            {
                additionalOptions.Add("hashtag=" + hashtag);
            }

            additionalOptions = additionalOptions.Where(r => !string.IsNullOrEmpty(r)).ToList();

            string publishType = ((KeyValuePair <string, string>)PublishType.SelectedItem).Value;
            string targetRef   = "for";

            if (Version >= Version.Parse("2.15"))
            {
                additionalOptions.Add(publishType);
            }
            else if (publishType == "wip")
            {
                targetRef = "drafts";
            }

            string targetBranch = $"refs/{targetRef}/{branch}";

            if (additionalOptions.Count > 0)
            {
                targetBranch += "%" + string.Join(",", additionalOptions);
            }

            var pushCommand = UICommands.CreateRemoteCommand();

            pushCommand.CommandText = PushCmd(
                _NO_TRANSLATE_Remotes.Text,
                targetBranch);

            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(true);
        }
Exemple #6
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);
                return(false);
            }

            if (string.IsNullOrEmpty(branch))
            {
                MessageBox.Show(owner, _selectBranch.Text);
                return(false);
            }

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

            string targetRef = PublishDraft.Checked ? "drafts" : "publish";

            var pushCommand = UICommands.CreateRemoteCommand();

            string targetBranch = "refs/" + targetRef + "/" + branch;
            string topic        = _NO_TRANSLATE_Topic.Text.Trim();

            if (!string.IsNullOrEmpty(topic))
            {
                targetBranch += "/" + topic;
            }

            pushCommand.CommandText = PushCmd(
                _NO_TRANSLATE_Remotes.Text,
                targetBranch);
            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)
                    {
                        change = line;
                        const string remotePrefix = "remote:";

                        if (change.StartsWith(remotePrefix))
                        {
                            change = change.Substring(remotePrefix.Length);
                        }

                        int escapePos = change.LastIndexOf((char)27);
                        if (escapePos != -1)
                        {
                            change = change.Substring(0, escapePos);
                        }

                        change = change.Trim();

                        int spacePos = change.IndexOf(' ');
                        if (spacePos != -1)
                        {
                            change = change.Substring(0, spacePos);
                        }

                        break;
                    }
                    else if (line.Contains("New Changes"))
                    {
                        hadNewChanges = true;
                    }
                }

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

            return(true);
        }
        private bool PublishChange(IWin32Window owner)
        {
            string branch = _NO_TRANSLATE_Branch.Text.Trim();

            if (string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text))
            {
                MessageBox.Show(owner, _selectRemote.Text);
                return(false);
            }

            if (string.IsNullOrEmpty(branch))
            {
                MessageBox.Show(owner, _selectBranch.Text);
                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);
        }