Skip to content

Commit 39b3949

Browse files
authored
Merge pull request #40 from lekhmanrus/download-patchset
Features: * Add patchset number in branch name (#39) * Ability to download specific patchset - not the latest/current only, as before * Enable/Disable Plugin via checkbox (#28) * Hide push button settings (#27)
2 parents c974c43 + 0d8d0c4 commit 39b3949

File tree

11 files changed

+681
-558
lines changed

11 files changed

+681
-558
lines changed

UnitTests/GitExtensions.GerritPlugin.Tests/Server/GerritCapabilityTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ public static class GerritCapabilityTests
99
[Test]
1010
public static void PublishTypes_for_Version2_15_has_expected_list_of_values()
1111
{
12-
RunTest(GerritCapabilities.Version2_15, new[] { "", "wip", "private" });
12+
RunTest(GerritCapabilities.Version2_15, new[] { string.Empty, "wip", "private" });
1313
}
1414

1515
[Test]
1616
public static void PublishTypes_for_OlderVersion_has_expected_list_of_values()
1717
{
18-
RunTest(GerritCapabilities.OldestVersion, new[] { "", "drafts" });
18+
RunTest(GerritCapabilities.OldestVersion, new[] { string.Empty, "drafts" });
1919
}
2020

2121
private static void RunTest(GerritCapabilities capability, string[] expectedValues)

src/GitExtensions.GerritPlugin/FormGerritDownload.Designer.cs

Lines changed: 182 additions & 135 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/GitExtensions.GerritPlugin/FormGerritDownload.cs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text.RegularExpressions;
45
using System.Threading.Tasks;
56
using System.Windows.Forms;
67
using GitCommands;
78
using GitCommands.Git.Commands;
9+
using GitExtUtils.GitUI;
810
using GitUI;
911
using GitUIPluginInterfaces;
1012
using Newtonsoft.Json.Linq;
@@ -17,21 +19,27 @@ public partial class FormGerritDownload : FormGerritBase
1719
private string _currentBranchRemote;
1820

1921
#region Translation
20-
private readonly TranslationString _downloadGerritChangeCaption = new TranslationString("Download Gerrit Change");
22+
private readonly TranslationString _downloadGerritChangeCaption = new("Download Gerrit Change");
2123

22-
private readonly TranslationString _downloadCaption = new TranslationString("Download change {0}");
24+
private readonly TranslationString _downloadCaption = new("Download change {0}");
2325

24-
private readonly TranslationString _selectRemote = new TranslationString("Please select a remote repository");
25-
private readonly TranslationString _selectChange = new TranslationString("Please enter a change");
26-
private readonly TranslationString _cannotGetChangeDetails = new TranslationString("Could not retrieve the change details");
26+
private readonly TranslationString _error = new("Error");
27+
private readonly TranslationString _selectRemote = new("Please select a remote repository");
28+
private readonly TranslationString _selectChange = new("Please enter a change");
29+
private readonly TranslationString _cannotGetChangeDetails = new("Could not retrieve the change details");
30+
private readonly TranslationString _cannotGetPatchSetDetails = new("Could not retrieve the patchset details");
31+
private readonly TranslationString _changeHelp = new("Enter the Change-Id or the number from the Gerrit URL");
32+
private readonly TranslationString _patchSetHelp = new("Optionally enter the Patchset # to download (the latest by default)");
2733
#endregion
2834

2935
public FormGerritDownload(IGitUICommands uiCommand)
3036
: base(uiCommand)
3137
{
3238
InitializeComponent();
3339
InitializeComplete();
34-
}
40+
}
41+
42+
protected ToolTip ToolTip => toolTip;
3543

3644
private void DownloadClick(object sender, EventArgs e)
3745
{
@@ -49,49 +57,64 @@ private async Task<bool> DownloadChangeAsync(IWin32Window owner)
4957

5058
if (string.IsNullOrEmpty(_NO_TRANSLATE_Remotes.Text))
5159
{
52-
MessageBox.Show(owner, _selectRemote.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
60+
MessageBox.Show(owner, _selectRemote.Text, _error.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
5361
return false;
5462
}
5563

5664
if (string.IsNullOrEmpty(change))
5765
{
58-
MessageBox.Show(owner, _selectChange.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
66+
MessageBox.Show(owner, _selectChange.Text, _error.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
5967
return false;
6068
}
6169

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

64-
var reviewInfo = await LoadReviewInfoAsync();
72+
int? patchSet = null;
73+
if (int.TryParse(_NO_TRANSLATE_Patchset.Text.Trim(), out int inputtedPatchSet))
74+
{
75+
patchSet = inputtedPatchSet;
76+
}
77+
78+
var reviewInfo = await LoadReviewInfoAsync(patchSet);
6579
await this.SwitchToMainThreadAsync();
6680

6781
if (reviewInfo?["id"] == null)
6882
{
69-
MessageBox.Show(owner, _cannotGetChangeDetails.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
83+
MessageBox.Show(owner, _cannotGetChangeDetails.Text, _error.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
7084
return false;
7185
}
7286

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

76-
change = (string)reviewInfo["number"];
90+
JObject patchSetInfo = patchSet == null
91+
? (JObject)reviewInfo["currentPatchSet"]
92+
: (JObject)((JArray)reviewInfo["patchSets"]).FirstOrDefault(q => (int)q["number"] == patchSet);
93+
if (patchSetInfo == null)
94+
{
95+
MessageBox.Show(owner, _cannotGetPatchSetDetails.Text, _error.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
96+
return false;
97+
}
7798

99+
change = (string)reviewInfo["number"];
78100
string topic = _NO_TRANSLATE_TopicBranch.Text.Trim();
79101

80102
if (string.IsNullOrEmpty(topic))
81103
{
82104
var topicNode = (JValue)reviewInfo["topic"];
83-
84-
topic = topicNode == null ? change : (string)topicNode.Value;
105+
topic = topicNode == null
106+
? change + "/" + (string)patchSetInfo["number"]
107+
: (string)topicNode.Value;
85108
}
86109

87110
var authorValue = (string)((JValue)reviewInfo["owner"]["name"]).Value;
88111
string author = Regex.Replace(authorValue.ToLowerInvariant(), "\\W+", "_");
89112
string branchName = "review/" + author + "/" + topic;
90-
var refspec = (string)((JValue)reviewInfo["currentPatchSet"]["ref"]).Value;
113+
var refSpec = (string)((JValue)patchSetInfo["ref"]).Value;
91114

92115
var fetchCommand = UICommands.CreateRemoteCommand();
93116

94-
fetchCommand.CommandText = FetchCommand(_NO_TRANSLATE_Remotes.Text, refspec);
117+
fetchCommand.CommandText = FetchCommand(_NO_TRANSLATE_Remotes.Text, refSpec);
95118

96119
if (!RunCommand(fetchCommand, change))
97120
{
@@ -148,7 +171,7 @@ private bool RunCommand(IGitRemoteCommand command, string change)
148171

149172
private static string FetchCommand(string remote, string remoteBranch)
150173
{
151-
var progressOption = "";
174+
var progressOption = string.Empty;
152175
if (GitVersion.Current.FetchCanAskForProgress)
153176
{
154177
progressOption = "--progress ";
@@ -157,7 +180,7 @@ private static string FetchCommand(string remote, string remoteBranch)
157180
remote = FixPath(remote);
158181

159182
// Remove spaces...
160-
remoteBranch = remoteBranch?.Replace(" ", "");
183+
remoteBranch = remoteBranch?.Replace(" ", string.Empty);
161184

162185
return "fetch " + progressOption + "\"" + remote.Trim() + "\" " + remoteBranch;
163186
}
@@ -168,7 +191,7 @@ private static string FixPath(string path)
168191
return path.ToPosixPath();
169192
}
170193

171-
private async Task<JObject> LoadReviewInfoAsync()
194+
private async Task<JObject> LoadReviewInfoAsync(int? patchSet = null)
172195
{
173196
var fetchUrl = GerritUtil.GetFetchUrl(Module, _currentBranchRemote);
174197

@@ -184,8 +207,9 @@ private async Task<JObject> LoadReviewInfoAsync()
184207
this,
185208
Module,
186209
string.Format(
187-
"gerrit query --format=JSON project:{0} --current-patch-set change:{1}",
210+
"gerrit query --format=JSON project:{0} {1} change:{2}",
188211
projectName,
212+
patchSet == null ? "--current-patch-set" : "--patch-sets",
189213
_NO_TRANSLATE_Change.Text),
190214
fetchUrl,
191215
_currentBranchRemote,
@@ -220,6 +244,11 @@ private void FormGerritDownloadLoad(object sender, EventArgs e)
220244
_NO_TRANSLATE_Change.Select();
221245

222246
Text = string.Concat(_downloadGerritChangeCaption.Text, " (", Module.WorkingDir, ")");
247+
248+
ToolTip.SetToolTip(_NO_TRANSLATE_ChangeHelp, _changeHelp.Text);
249+
ToolTip.SetToolTip(_NO_TRANSLATE_PatchsetHelp, _patchSetHelp.Text);
250+
_NO_TRANSLATE_ChangeHelp.Size = DpiUtil.Scale(_NO_TRANSLATE_ChangeHelp.Size);
251+
_NO_TRANSLATE_PatchsetHelp.Size = DpiUtil.Scale(_NO_TRANSLATE_PatchsetHelp.Size);
223252
}
224253

225254
private void AddRemoteClick(object sender, EventArgs e)

0 commit comments

Comments
 (0)