1
1
use documented:: { Documented , DocumentedFields } ;
2
2
3
3
use super :: flags:: CliFlag ;
4
- use super :: { CliParseError , HelpOrVersion , LocalFlag , SubCommand } ;
4
+ use super :: { CliParseError , Flag , HelpOrVersion , LocalFlag , SubCommand } ;
5
5
6
6
/// A branch
7
7
#[ derive( Default , Debug , PartialEq , Eq , PartialOrd , Ord , Documented , DocumentedFields ) ]
@@ -17,10 +17,11 @@ pub struct Branch {
17
17
}
18
18
19
19
/// Fetch branches for a GitHub repository as a local branch
20
- #[ derive( Debug , PartialEq , Eq , PartialOrd , Ord , Documented , DocumentedFields ) ]
20
+ #[ derive( Debug , PartialEq , Eq , PartialOrd , Ord , Documented ) ]
21
21
pub struct BranchFetch {
22
22
/// A list of branches to fetch
23
23
pub branches : Vec < Branch > ,
24
+ pub checkout : bool ,
24
25
}
25
26
26
27
impl BranchFetch {
@@ -45,18 +46,24 @@ impl SubCommand for BranchFetch {
45
46
global_flag : & mut HelpOrVersion ,
46
47
) -> Result < Self , CliParseError > {
47
48
let mut branches: Vec < Branch > = vec ! [ ] ;
49
+ let mut checkout = false ;
48
50
49
51
for arg in args. by_ref ( ) {
50
52
if let Ok ( flag) = arg. parse :: < HelpOrVersion > ( ) {
51
53
global_flag. validate ( flag) ?;
52
54
continue ;
53
55
}
54
56
55
- // Non-flag arguments for branch-fetch are always branch names with optional
56
- // commits
57
- if let Some ( local_flag) = LocalFlag :: parse ( & arg) ? {
58
- // Only global flags should be parsed for branch-fetch
59
- return Err ( CliParseError :: UnexpectedFlag ( local_flag) ) ;
57
+ match dbg ! ( LocalFlag :: parse( & arg) ?) {
58
+ Some ( flag @ LocalFlag :: Checkout ) => {
59
+ if checkout {
60
+ return Err ( CliParseError :: DuplicateFlag ( Flag :: LocalFlag ( flag) ) ) ;
61
+ }
62
+ checkout = true ;
63
+ continue ;
64
+ } ,
65
+ Some ( flag) => return Err ( CliParseError :: UnexpectedFlag ( flag) ) ,
66
+ None => ( ) ,
60
67
}
61
68
62
69
let ( branch_name, commit) = match arg. split_once ( '@' ) {
@@ -87,7 +94,11 @@ impl SubCommand for BranchFetch {
87
94
} ) ;
88
95
}
89
96
90
- Ok ( BranchFetch { branches } )
97
+ if checkout && branches. is_empty ( ) {
98
+ return Err ( CliParseError :: CheckoutNoSource ) ;
99
+ }
100
+
101
+ Ok ( BranchFetch { branches, checkout } )
91
102
}
92
103
}
93
104
@@ -111,6 +122,7 @@ mod tests {
111
122
name: "master" . to_owned( ) ,
112
123
commit: None ,
113
124
} ] ,
125
+ checkout: false ,
114
126
} ) ) ,
115
127
help_or_version: HelpOrVersion :: None ,
116
128
} )
@@ -141,6 +153,36 @@ mod tests {
141
153
commit: None ,
142
154
}
143
155
] ,
156
+ checkout: false ,
157
+ } ) ) ,
158
+ help_or_version: HelpOrVersion :: None ,
159
+ } )
160
+ ) ;
161
+ // with checkout flag
162
+ assert_eq ! (
163
+ patchy( & [
164
+ "branch-fetch" ,
165
+ "-c" ,
166
+ "helix-editor/helix/master" ,
167
+ "helix-editor/helix/develop"
168
+ ] ) ,
169
+ Ok ( Cli {
170
+ subcommand: Some ( Subcommand :: BranchFetch ( BranchFetch {
171
+ branches: vec![
172
+ Branch {
173
+ repo_owner: "helix-editor" . to_owned( ) ,
174
+ repo_name: "helix" . to_owned( ) ,
175
+ name: "master" . to_owned( ) ,
176
+ commit: None ,
177
+ } ,
178
+ Branch {
179
+ repo_owner: "helix-editor" . to_owned( ) ,
180
+ repo_name: "helix" . to_owned( ) ,
181
+ name: "develop" . to_owned( ) ,
182
+ commit: None ,
183
+ }
184
+ ] ,
185
+ checkout: true ,
144
186
} ) ) ,
145
187
help_or_version: HelpOrVersion :: None ,
146
188
} )
@@ -159,6 +201,7 @@ mod tests {
159
201
name: "master" . to_owned( ) ,
160
202
commit: Some ( "6049f20" . to_owned( ) ) ,
161
203
} ] ,
204
+ checkout: false ,
162
205
} ) ) ,
163
206
help_or_version: HelpOrVersion :: None ,
164
207
} )
@@ -196,6 +239,7 @@ mod tests {
196
239
commit: Some ( "abc123" . to_owned( ) ) ,
197
240
}
198
241
] ,
242
+ checkout: false ,
199
243
} ) ) ,
200
244
help_or_version: HelpOrVersion :: None ,
201
245
} )
@@ -205,7 +249,11 @@ mod tests {
205
249
#[ test]
206
250
fn multiple_at_in_branch_name ( ) {
207
251
assert_eq ! (
208
- patchy( & [ "branch-fetch" , "owner/repo/branch@commit@extra" ] ) ,
252
+ patchy( & [
253
+ "branch-fetch" ,
254
+ "owner/repo/branch@commit@extra" ,
255
+ "--checkout"
256
+ ] ) ,
209
257
Ok ( Cli {
210
258
subcommand: Some ( Subcommand :: BranchFetch ( BranchFetch {
211
259
branches: vec![ Branch {
@@ -214,6 +262,7 @@ mod tests {
214
262
name: "branch" . to_owned( ) ,
215
263
commit: Some ( "commit@extra" . to_owned( ) ) ,
216
264
} , ] ,
265
+ checkout: true ,
217
266
} ) ) ,
218
267
help_or_version: HelpOrVersion :: None ,
219
268
} )
@@ -225,20 +274,48 @@ mod tests {
225
274
assert_eq ! (
226
275
patchy( & [ "branch-fetch" , "--help" ] ) ,
227
276
Ok ( Cli {
228
- subcommand: Some ( Subcommand :: BranchFetch ( BranchFetch { branches: vec![ ] } ) ) ,
277
+ subcommand: Some ( Subcommand :: BranchFetch ( BranchFetch {
278
+ branches: vec![ ] ,
279
+ checkout: false
280
+ } ) ) ,
229
281
help_or_version: HelpOrVersion :: Help ,
230
282
} )
231
283
) ;
232
284
233
285
assert_eq ! (
234
286
patchy( & [ "branch-fetch" , "--version" ] ) ,
235
287
Ok ( Cli {
236
- subcommand: Some ( Subcommand :: BranchFetch ( BranchFetch { branches: vec![ ] } ) ) ,
288
+ subcommand: Some ( Subcommand :: BranchFetch ( BranchFetch {
289
+ branches: vec![ ] ,
290
+ checkout: false
291
+ } ) ) ,
237
292
help_or_version: HelpOrVersion :: Version ,
238
293
} )
239
294
) ;
240
295
}
241
296
297
+ #[ test]
298
+ fn duplicate_checkout ( ) {
299
+ assert_eq ! (
300
+ patchy( & [ "branch-fetch" , "some/branch/somewhere" , "-c" , "--checkout" ] ) ,
301
+ Err ( CliParseError :: DuplicateFlag ( Flag :: LocalFlag (
302
+ LocalFlag :: Checkout
303
+ ) ) )
304
+ ) ;
305
+ }
306
+
307
+ #[ test]
308
+ fn checkout_with_no_source ( ) {
309
+ assert_eq ! (
310
+ patchy( & [ "branch-fetch" , "-c" ] ) ,
311
+ Err ( CliParseError :: CheckoutNoSource )
312
+ ) ;
313
+ assert_eq ! (
314
+ patchy( & [ "branch-fetch" , "--checkout" ] ) ,
315
+ Err ( CliParseError :: CheckoutNoSource )
316
+ ) ;
317
+ }
318
+
242
319
#[ test]
243
320
fn invalid_flags ( ) {
244
321
assert_eq ! (
@@ -247,7 +324,7 @@ mod tests {
247
324
) ;
248
325
assert_eq ! (
249
326
patchy( & [ "branch-fetch" , "--checkout" ] ) ,
250
- Err ( CliParseError :: UnexpectedFlag ( LocalFlag :: Checkout ) )
327
+ Err ( CliParseError :: CheckoutNoSource )
251
328
) ;
252
329
assert_eq ! (
253
330
patchy( & [ "branch-fetch" , "--branch-name=test" ] ) ,
0 commit comments