@@ -10,6 +10,7 @@ use clap::{Arg, ArgMatches};
10
10
use termcolor:: WriteColor ;
11
11
12
12
use crate :: {
13
+ argset:: { self , get_one_str} ,
13
14
print_info_message, print_warning_message,
14
15
repo:: RepositoryExtended ,
15
16
stack:: { get_branch_name, state_refname_from_branch_name, Stack , StackStateAccess } ,
@@ -82,12 +83,12 @@ fn make() -> clap::Command<'static> {
82
83
Arg :: new ( "new-branch" )
83
84
. help ( "New branch name" )
84
85
. required ( true )
85
- . forbid_empty_values ( true ) ,
86
+ . value_parser ( argset :: parse_branch_name ) ,
86
87
)
87
88
. arg (
88
89
Arg :: new ( "committish" )
89
90
. help ( "Base commit for new branch" )
90
- . forbid_empty_values ( true ) ,
91
+ . value_parser ( clap :: builder :: NonEmptyStringValueParser :: new ( ) ) ,
91
92
) ,
92
93
)
93
94
. subcommand (
@@ -105,7 +106,7 @@ fn make() -> clap::Command<'static> {
105
106
. arg (
106
107
Arg :: new ( "new-branch" )
107
108
. help ( "New branch name" )
108
- . forbid_empty_values ( true ) ,
109
+ . value_parser ( argset :: parse_branch_name ) ,
109
110
) ,
110
111
)
111
112
. subcommand (
@@ -121,7 +122,7 @@ fn make() -> clap::Command<'static> {
121
122
. multiple_values ( true )
122
123
. min_values ( 1 )
123
124
. max_values ( 2 )
124
- . forbid_empty_values ( true ) ,
125
+ . value_parser ( argset :: parse_branch_name ) ,
125
126
) ,
126
127
)
127
128
. subcommand (
@@ -133,7 +134,7 @@ fn make() -> clap::Command<'static> {
133
134
Arg :: new ( "branch" )
134
135
. help ( "Branch to protect" )
135
136
. value_name ( "branch" )
136
- . forbid_empty_values ( true ) ,
137
+ . value_parser ( argset :: parse_branch_name ) ,
137
138
) ,
138
139
)
139
140
. subcommand (
@@ -145,7 +146,7 @@ fn make() -> clap::Command<'static> {
145
146
Arg :: new ( "branch" )
146
147
. help ( "Branch to unprotect" )
147
148
. value_name ( "branch" )
148
- . forbid_empty_values ( true ) ,
149
+ . value_parser ( argset :: parse_branch_name ) ,
149
150
) ,
150
151
)
151
152
. subcommand (
@@ -165,7 +166,7 @@ fn make() -> clap::Command<'static> {
165
166
. help ( "Branch to delete" )
166
167
. value_name ( "branch" )
167
168
. required ( true )
168
- . forbid_empty_values ( true ) ,
169
+ . value_parser ( argset :: parse_branch_name ) ,
169
170
)
170
171
. arg (
171
172
Arg :: new ( "force" )
@@ -189,7 +190,7 @@ fn make() -> clap::Command<'static> {
189
190
Arg :: new ( "branch" )
190
191
. help ( "Branch to clean up" )
191
192
. value_name ( "branch" )
192
- . forbid_empty_values ( true ) ,
193
+ . value_parser ( argset :: parse_branch_name ) ,
193
194
)
194
195
. arg (
195
196
Arg :: new ( "force" )
@@ -208,7 +209,7 @@ fn make() -> clap::Command<'static> {
208
209
Arg :: new ( "branch-any" )
209
210
. help ( "Branch to describe" )
210
211
. value_name ( "branch" )
211
- . forbid_empty_values ( true ) ,
212
+ . value_parser ( argset :: parse_branch_name ) ,
212
213
) ,
213
214
)
214
215
. arg (
@@ -221,7 +222,7 @@ fn make() -> clap::Command<'static> {
221
222
Arg :: new ( "branch-any" )
222
223
. help ( "Branch to switch to" )
223
224
. value_name ( "branch" )
224
- . forbid_empty_values ( true ) ,
225
+ . value_parser ( argset :: parse_branch_name ) ,
225
226
)
226
227
}
227
228
@@ -243,11 +244,11 @@ fn run(matches: &ArgMatches) -> Result<()> {
243
244
} else {
244
245
let current_branch = repo. get_branch ( None ) ?;
245
246
let current_branchname = get_branch_name ( & current_branch) ?;
246
- if let Some ( target_branchname) = matches . value_of ( "branch-any" ) {
247
+ if let Some ( target_branchname) = get_one_str ( matches , "branch-any" ) {
247
248
if target_branchname == current_branchname {
248
249
Err ( anyhow ! ( "{target_branchname} is already the current branch" ) )
249
250
} else {
250
- if !matches. is_present ( "merge" ) {
251
+ if !matches. contains_id ( "merge" ) {
251
252
repo. check_worktree_clean ( ) ?;
252
253
}
253
254
let target_branch = repo. get_branch ( Some ( target_branchname) ) ?;
@@ -422,12 +423,12 @@ fn list(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
422
423
}
423
424
424
425
fn create ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
425
- let new_branchname = matches . value_of ( "new-branch" ) . expect ( "required argument" ) ;
426
+ let new_branchname = get_one_str ( matches , "new-branch" ) . expect ( "required argument" ) ;
426
427
427
428
repo. check_repository_state ( ) ?;
428
429
repo. check_conflicts ( ) ?;
429
430
430
- let parent_branch = if let Some ( committish) = matches . value_of ( "committish" ) {
431
+ let parent_branch = if let Some ( committish) = get_one_str ( matches , "committish" ) {
431
432
repo. check_worktree_clean ( ) ?;
432
433
let mut parent_branch = None ;
433
434
if let Ok ( local_parent_branch) = repo. find_branch ( committish, git2:: BranchType :: Local ) {
@@ -458,7 +459,7 @@ fn create(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
458
459
459
460
let target_commit = if let Some ( parent_branch) = parent_branch. as_ref ( ) {
460
461
parent_branch. get ( ) . peel_to_commit ( ) ?
461
- } else if let Some ( committish) = matches . value_of ( "committish" ) {
462
+ } else if let Some ( committish) = get_one_str ( matches , "committish" ) {
462
463
crate :: revspec:: parse_stgit_revision ( & repo, Some ( committish) , None ) ?. peel_to_commit ( ) ?
463
464
} else {
464
465
repo. head ( ) ?. peel_to_commit ( ) ?
@@ -515,7 +516,7 @@ fn clone(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
515
516
let current_branch = repo. get_branch ( None ) ?;
516
517
let current_branchname = get_branch_name ( & current_branch) ?;
517
518
518
- let new_branchname = if let Some ( new_branchname) = matches . value_of ( "new-branch" ) {
519
+ let new_branchname = if let Some ( new_branchname) = get_one_str ( matches , "new-branch" ) {
519
520
new_branchname. to_string ( )
520
521
} else {
521
522
let suffix = chrono:: Local :: now ( ) . format ( "%Y%m%d-%H%M%S" ) ;
@@ -560,7 +561,11 @@ fn clone(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
560
561
}
561
562
562
563
fn rename ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
563
- let names: Vec < _ > = matches. values_of ( "branch-any" ) . unwrap ( ) . collect ( ) ;
564
+ let names: Vec < _ > = matches
565
+ . get_many :: < String > ( "branch-any" )
566
+ . unwrap ( )
567
+ . map ( |s| s. as_str ( ) )
568
+ . collect ( ) ;
564
569
let current_branchname;
565
570
let ( old_branchname, new_branchname) = if names. len ( ) == 2 {
566
571
repo. get_branch ( Some ( names[ 0 ] ) ) ?;
@@ -596,19 +601,19 @@ fn rename(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
596
601
}
597
602
598
603
fn protect ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
599
- let stack = Stack :: from_branch ( & repo, matches . value_of ( "branch" ) ) ?;
604
+ let stack = Stack :: from_branch ( & repo, get_one_str ( matches , "branch" ) ) ?;
600
605
let mut config = repo. config ( ) ?;
601
606
stack. set_protected ( & mut config, true )
602
607
}
603
608
604
609
fn unprotect ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
605
- let stack = Stack :: from_branch ( & repo, matches . value_of ( "branch" ) ) ?;
610
+ let stack = Stack :: from_branch ( & repo, get_one_str ( matches , "branch" ) ) ?;
606
611
let mut config = repo. config ( ) ?;
607
612
stack. set_protected ( & mut config, false )
608
613
}
609
614
610
615
fn delete ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
611
- let target_branchname = matches . value_of ( "branch-any" ) . expect ( "required argument" ) ;
616
+ let target_branchname = get_one_str ( matches , "branch-any" ) . expect ( "required argument" ) ;
612
617
let mut target_branch = repo. get_branch ( Some ( target_branchname) ) ?;
613
618
let current_branch = repo. get_branch ( None ) . ok ( ) ;
614
619
let current_branchname = current_branch. and_then ( |branch| get_branch_name ( & branch) . ok ( ) ) ;
@@ -621,7 +626,7 @@ fn delete(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
621
626
if let Ok ( stack) = Stack :: from_branch ( & repo, Some ( target_branchname) ) {
622
627
if stack. is_protected ( & config) {
623
628
return Err ( anyhow ! ( "Delete not permitted: this branch is protected" ) ) ;
624
- } else if !matches. is_present ( "force" ) && stack. all_patches ( ) . count ( ) > 0 {
629
+ } else if !matches. contains_id ( "force" ) && stack. all_patches ( ) . count ( ) > 0 {
625
630
return Err ( anyhow ! (
626
631
"Delete not permitted: the series still contains patches (override with --force)"
627
632
) ) ;
@@ -634,11 +639,11 @@ fn delete(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
634
639
}
635
640
636
641
fn cleanup ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
637
- let stack = Stack :: from_branch ( & repo, matches . value_of ( "branch" ) ) ?;
642
+ let stack = Stack :: from_branch ( & repo, get_one_str ( matches , "branch" ) ) ?;
638
643
let config = repo. config ( ) ?;
639
644
if stack. is_protected ( & config) {
640
645
return Err ( anyhow ! ( "Clean up not permitted: this branch is protected" ) ) ;
641
- } else if !matches. is_present ( "force" ) && stack. all_patches ( ) . count ( ) > 0 {
646
+ } else if !matches. contains_id ( "force" ) && stack. all_patches ( ) . count ( ) > 0 {
642
647
return Err ( anyhow ! (
643
648
"Clean up not permitted: the series still contains patches (override with --force)"
644
649
) ) ;
@@ -648,8 +653,8 @@ fn cleanup(repo: git2::Repository, matches: &ArgMatches) -> Result<()> {
648
653
}
649
654
650
655
fn describe ( repo : git2:: Repository , matches : & ArgMatches ) -> Result < ( ) > {
651
- let branch = repo. get_branch ( matches . value_of ( "branch-any" ) ) ?;
652
- let description = matches . value_of ( "description" ) . expect ( "required argument" ) ;
656
+ let branch = repo. get_branch ( get_one_str ( matches , "branch-any" ) ) ?;
657
+ let description = get_one_str ( matches , "description" ) . expect ( "required argument" ) ;
653
658
let branchname = get_branch_name ( & branch) ?;
654
659
let mut config = repo. config ( ) ?;
655
660
set_description ( & mut config, & branchname, description)
0 commit comments