1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Linq ;
3
4
using System . Text . RegularExpressions ;
4
5
using System . Threading . Tasks ;
5
6
using System . Windows . Forms ;
6
7
using GitCommands ;
7
8
using GitCommands . Git . Commands ;
9
+ using GitExtUtils . GitUI ;
8
10
using GitUI ;
9
11
using GitUIPluginInterfaces ;
10
12
using Newtonsoft . Json . Linq ;
@@ -17,21 +19,27 @@ public partial class FormGerritDownload : FormGerritBase
17
19
private string _currentBranchRemote ;
18
20
19
21
#region Translation
20
- private readonly TranslationString _downloadGerritChangeCaption = new TranslationString ( "Download Gerrit Change" ) ;
22
+ private readonly TranslationString _downloadGerritChangeCaption = new ( "Download Gerrit Change" ) ;
21
23
22
- private readonly TranslationString _downloadCaption = new TranslationString ( "Download change {0}" ) ;
24
+ private readonly TranslationString _downloadCaption = new ( "Download change {0}" ) ;
23
25
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)" ) ;
27
33
#endregion
28
34
29
35
public FormGerritDownload ( IGitUICommands uiCommand )
30
36
: base ( uiCommand )
31
37
{
32
38
InitializeComponent ( ) ;
33
39
InitializeComplete ( ) ;
34
- }
40
+ }
41
+
42
+ protected ToolTip ToolTip => toolTip ;
35
43
36
44
private void DownloadClick ( object sender , EventArgs e )
37
45
{
@@ -49,49 +57,64 @@ private async Task<bool> DownloadChangeAsync(IWin32Window owner)
49
57
50
58
if ( string . IsNullOrEmpty ( _NO_TRANSLATE_Remotes . Text ) )
51
59
{
52
- MessageBox . Show ( owner , _selectRemote . Text , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
60
+ MessageBox . Show ( owner , _selectRemote . Text , _error . Text , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
53
61
return false ;
54
62
}
55
63
56
64
if ( string . IsNullOrEmpty ( change ) )
57
65
{
58
- MessageBox . Show ( owner , _selectChange . Text , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
66
+ MessageBox . Show ( owner , _selectChange . Text , _error . Text , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
59
67
return false ;
60
68
}
61
69
62
70
GerritUtil . StartAgent ( owner , Module , _NO_TRANSLATE_Remotes . Text ) ;
63
71
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 ) ;
65
79
await this . SwitchToMainThreadAsync ( ) ;
66
80
67
81
if ( reviewInfo ? [ "id" ] == null )
68
82
{
69
- MessageBox . Show ( owner , _cannotGetChangeDetails . Text , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
83
+ MessageBox . Show ( owner , _cannotGetChangeDetails . Text , _error . Text , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
70
84
return false ;
71
85
}
72
86
73
87
// The user can enter both the Change-Id or the number. Here we
74
88
// force the number to get prettier branches.
75
89
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
+ }
77
98
99
+ change = ( string ) reviewInfo [ "number" ] ;
78
100
string topic = _NO_TRANSLATE_TopicBranch . Text . Trim ( ) ;
79
101
80
102
if ( string . IsNullOrEmpty ( topic ) )
81
103
{
82
104
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 ;
85
108
}
86
109
87
110
var authorValue = ( string ) ( ( JValue ) reviewInfo [ "owner" ] [ "name" ] ) . Value ;
88
111
string author = Regex . Replace ( authorValue . ToLowerInvariant ( ) , "\\ W+" , "_" ) ;
89
112
string branchName = "review/" + author + "/" + topic ;
90
- var refspec = ( string ) ( ( JValue ) reviewInfo [ "currentPatchSet" ] [ "ref" ] ) . Value ;
113
+ var refSpec = ( string ) ( ( JValue ) patchSetInfo [ "ref" ] ) . Value ;
91
114
92
115
var fetchCommand = UICommands . CreateRemoteCommand ( ) ;
93
116
94
- fetchCommand . CommandText = FetchCommand ( _NO_TRANSLATE_Remotes . Text , refspec ) ;
117
+ fetchCommand . CommandText = FetchCommand ( _NO_TRANSLATE_Remotes . Text , refSpec ) ;
95
118
96
119
if ( ! RunCommand ( fetchCommand , change ) )
97
120
{
@@ -148,7 +171,7 @@ private bool RunCommand(IGitRemoteCommand command, string change)
148
171
149
172
private static string FetchCommand ( string remote , string remoteBranch )
150
173
{
151
- var progressOption = "" ;
174
+ var progressOption = string . Empty ;
152
175
if ( GitVersion . Current . FetchCanAskForProgress )
153
176
{
154
177
progressOption = "--progress " ;
@@ -157,7 +180,7 @@ private static string FetchCommand(string remote, string remoteBranch)
157
180
remote = FixPath ( remote ) ;
158
181
159
182
// Remove spaces...
160
- remoteBranch = remoteBranch ? . Replace ( " " , "" ) ;
183
+ remoteBranch = remoteBranch ? . Replace ( " " , string . Empty ) ;
161
184
162
185
return "fetch " + progressOption + "\" " + remote . Trim ( ) + "\" " + remoteBranch ;
163
186
}
@@ -168,7 +191,7 @@ private static string FixPath(string path)
168
191
return path . ToPosixPath ( ) ;
169
192
}
170
193
171
- private async Task < JObject > LoadReviewInfoAsync ( )
194
+ private async Task < JObject > LoadReviewInfoAsync ( int ? patchSet = null )
172
195
{
173
196
var fetchUrl = GerritUtil . GetFetchUrl ( Module , _currentBranchRemote ) ;
174
197
@@ -184,8 +207,9 @@ private async Task<JObject> LoadReviewInfoAsync()
184
207
this ,
185
208
Module ,
186
209
string . Format (
187
- "gerrit query --format=JSON project:{0} --current-patch-set change:{1 }" ,
210
+ "gerrit query --format=JSON project:{0} {1} change:{2 }" ,
188
211
projectName ,
212
+ patchSet == null ? "--current-patch-set" : "--patch-sets" ,
189
213
_NO_TRANSLATE_Change . Text ) ,
190
214
fetchUrl ,
191
215
_currentBranchRemote ,
@@ -220,6 +244,11 @@ private void FormGerritDownloadLoad(object sender, EventArgs e)
220
244
_NO_TRANSLATE_Change . Select ( ) ;
221
245
222
246
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 ) ;
223
252
}
224
253
225
254
private void AddRemoteClick ( object sender , EventArgs e )
0 commit comments