@@ -535,6 +535,34 @@ N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
535
535
"\n"
536
536
"Run \"git rev-parse --parseopt -h\" for more information on the first usage." );
537
537
538
+ /*
539
+ * Parse "opt" or "opt=<value>", setting value respectively to either
540
+ * NULL or the string after "=".
541
+ */
542
+ static int opt_with_value (const char * arg , const char * opt , const char * * value )
543
+ {
544
+ if (skip_prefix (arg , opt , & arg )) {
545
+ if (!* arg ) {
546
+ * value = NULL ;
547
+ return 1 ;
548
+ }
549
+ if (* arg ++ == '=' ) {
550
+ * value = arg ;
551
+ return 1 ;
552
+ }
553
+ }
554
+ return 0 ;
555
+ }
556
+
557
+ static void handle_ref_opt (const char * pattern , const char * prefix )
558
+ {
559
+ if (pattern )
560
+ for_each_glob_ref_in (show_reference , pattern , prefix , NULL );
561
+ else
562
+ for_each_ref_in (prefix , show_reference , NULL );
563
+ clear_ref_exclusion (& ref_excludes );
564
+ }
565
+
538
566
int cmd_rev_parse (int argc , const char * * argv , const char * prefix )
539
567
{
540
568
int i , as_is = 0 , verify = 0 , quiet = 0 , revs_count = 0 , type = 0 ;
@@ -671,14 +699,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
671
699
flags |= GET_SHA1_QUIETLY ;
672
700
continue ;
673
701
}
674
- if (!strcmp (arg , "--short" ) ||
675
- starts_with (arg , "--short=" )) {
702
+ if (opt_with_value (arg , "--short" , & arg )) {
676
703
filter &= ~(DO_FLAGS |DO_NOREV );
677
704
verify = 1 ;
678
705
abbrev = DEFAULT_ABBREV ;
679
- if (!arg [ 7 ] )
706
+ if (!arg )
680
707
continue ;
681
- abbrev = strtoul (arg + 8 , NULL , 10 );
708
+ abbrev = strtoul (arg , NULL , 10 );
682
709
if (abbrev < MINIMUM_ABBREV )
683
710
abbrev = MINIMUM_ABBREV ;
684
711
else if (40 <= abbrev )
@@ -701,73 +728,51 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
701
728
symbolic = SHOW_SYMBOLIC_FULL ;
702
729
continue ;
703
730
}
704
- if (starts_with (arg , "--abbrev-ref" ) &&
705
- (!arg [12 ] || arg [12 ] == '=' )) {
731
+ if (opt_with_value (arg , "--abbrev-ref" , & arg )) {
706
732
abbrev_ref = 1 ;
707
733
abbrev_ref_strict = warn_ambiguous_refs ;
708
- if (arg [ 12 ] == '=' ) {
709
- if (!strcmp (arg + 13 , "strict" ))
734
+ if (arg ) {
735
+ if (!strcmp (arg , "strict" ))
710
736
abbrev_ref_strict = 1 ;
711
- else if (!strcmp (arg + 13 , "loose" ))
737
+ else if (!strcmp (arg , "loose" ))
712
738
abbrev_ref_strict = 0 ;
713
739
else
714
- die ("unknown mode for %s" , arg );
740
+ die ("unknown mode for --abbrev-ref: %s" ,
741
+ arg );
715
742
}
716
743
continue ;
717
744
}
718
745
if (!strcmp (arg , "--all" )) {
719
746
for_each_ref (show_reference , NULL );
720
747
continue ;
721
748
}
722
- if (starts_with (arg , "--disambiguate=" )) {
723
- for_each_abbrev (arg + 15 , show_abbrev , NULL );
749
+ if (skip_prefix (arg , "--disambiguate=" , & arg )) {
750
+ for_each_abbrev (arg , show_abbrev , NULL );
724
751
continue ;
725
752
}
726
753
if (!strcmp (arg , "--bisect" )) {
727
754
for_each_ref_in ("refs/bisect/bad" , show_reference , NULL );
728
755
for_each_ref_in ("refs/bisect/good" , anti_reference , NULL );
729
756
continue ;
730
757
}
731
- if (starts_with (arg , "--branches=" )) {
732
- for_each_glob_ref_in (show_reference , arg + 11 ,
733
- "refs/heads/" , NULL );
734
- clear_ref_exclusion (& ref_excludes );
735
- continue ;
736
- }
737
- if (!strcmp (arg , "--branches" )) {
738
- for_each_branch_ref (show_reference , NULL );
739
- clear_ref_exclusion (& ref_excludes );
740
- continue ;
741
- }
742
- if (starts_with (arg , "--tags=" )) {
743
- for_each_glob_ref_in (show_reference , arg + 7 ,
744
- "refs/tags/" , NULL );
745
- clear_ref_exclusion (& ref_excludes );
746
- continue ;
747
- }
748
- if (!strcmp (arg , "--tags" )) {
749
- for_each_tag_ref (show_reference , NULL );
750
- clear_ref_exclusion (& ref_excludes );
758
+ if (opt_with_value (arg , "--branches" , & arg )) {
759
+ handle_ref_opt (arg , "refs/heads/" );
751
760
continue ;
752
761
}
753
- if (starts_with (arg , "--glob=" )) {
754
- for_each_glob_ref (show_reference , arg + 7 , NULL );
755
- clear_ref_exclusion (& ref_excludes );
762
+ if (opt_with_value (arg , "--tags" , & arg )) {
763
+ handle_ref_opt (arg , "refs/tags/" );
756
764
continue ;
757
765
}
758
- if (starts_with (arg , "--remotes=" )) {
759
- for_each_glob_ref_in (show_reference , arg + 10 ,
760
- "refs/remotes/" , NULL );
761
- clear_ref_exclusion (& ref_excludes );
766
+ if (skip_prefix (arg , "--glob=" , & arg )) {
767
+ handle_ref_opt (arg , NULL );
762
768
continue ;
763
769
}
764
- if (!strcmp (arg , "--remotes" )) {
765
- for_each_remote_ref (show_reference , NULL );
766
- clear_ref_exclusion (& ref_excludes );
770
+ if (opt_with_value (arg , "--remotes" , & arg )) {
771
+ handle_ref_opt (arg , "refs/remotes/" );
767
772
continue ;
768
773
}
769
- if (starts_with (arg , "--exclude=" )) {
770
- add_ref_exclusion (& ref_excludes , arg + 10 );
774
+ if (skip_prefix (arg , "--exclude=" , & arg )) {
775
+ add_ref_exclusion (& ref_excludes , arg );
771
776
continue ;
772
777
}
773
778
if (!strcmp (arg , "--show-toplevel" )) {
@@ -849,20 +854,20 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
849
854
}
850
855
continue ;
851
856
}
852
- if (starts_with (arg , "--since=" )) {
853
- show_datestring ("--max-age=" , arg + 8 );
857
+ if (skip_prefix (arg , "--since=" , & arg )) {
858
+ show_datestring ("--max-age=" , arg );
854
859
continue ;
855
860
}
856
- if (starts_with (arg , "--after=" )) {
857
- show_datestring ("--max-age=" , arg + 8 );
861
+ if (skip_prefix (arg , "--after=" , & arg )) {
862
+ show_datestring ("--max-age=" , arg );
858
863
continue ;
859
864
}
860
- if (starts_with (arg , "--before=" )) {
861
- show_datestring ("--min-age=" , arg + 9 );
865
+ if (skip_prefix (arg , "--before=" , & arg )) {
866
+ show_datestring ("--min-age=" , arg );
862
867
continue ;
863
868
}
864
- if (starts_with (arg , "--until=" )) {
865
- show_datestring ("--min-age=" , arg + 8 );
869
+ if (skip_prefix (arg , "--until=" , & arg )) {
870
+ show_datestring ("--min-age=" , arg );
866
871
continue ;
867
872
}
868
873
if (show_flag (arg ) && verify )
0 commit comments