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); } StartAgent(owner, _NO_TRANSLATE_Remotes.Text); var reviewInfo = LoadReviewInfo(); if (reviewInfo == null || reviewInfo["id"] == null) { MessageBox.Show(owner, _cannotGetChangeDetails.Text); return(false); } 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 = _uiCommand.CreateRemoteCommand(); fetchCommand.CommandText = FetchCommand(_NO_TRANSLATE_Remotes.Text, refspec); if (!RunCommand(fetchCommand, change)) { return(false); } var checkoutCommand = _uiCommand.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 = _uiCommand.CreateRemoteCommand(); recycleCommand.CommandText = "checkout " + branchName; if (!RunCommand(recycleCommand, change)) { return; } var resetCommand = _uiCommand.CreateRemoteCommand(); resetCommand.CommandText = GitCommandHelpers.ResetHardCmd("FETCH_HEAD"); if (!RunCommand(resetCommand, change)) { return; } e.IsError = false; } } }; return(RunCommand(checkoutCommand, change)); }
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); } StartAgent(owner, _NO_TRANSLATE_Remotes.Text); string targetRef = PublishDraft.Checked ? "drafts" : "publish"; var pushCommand = _uiCommand.CreateRemoteCommand(); string targetBranch = "refs/" + targetRef + "/" + branch; string topic = _NO_TRANSLATE_Topic.Text.Trim(); if (!string.IsNullOrEmpty(topic)) { targetBranch += "/" + topic; } pushCommand.CommandText = GitCommandHelpers.PushCmd( _NO_TRANSLATE_Remotes.Text, targetBranch, false ); 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.CommandText.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); }