1
1
#include "builtin.h"
2
+ #include "cache.h"
2
3
#include "config.h"
3
4
#include "dir.h"
4
5
#include "parse-options.h"
@@ -401,6 +402,7 @@ static int update_modes(int *cone_mode, int *sparse_index)
401
402
core_sparse_checkout_cone = 1 ;
402
403
} else {
403
404
mode = MODE_ALL_PATTERNS ;
405
+ core_sparse_checkout_cone = 0 ;
404
406
}
405
407
if (record_mode && set_config (mode ))
406
408
return 1 ;
@@ -681,18 +683,76 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
681
683
return result ;
682
684
}
683
685
686
+ static void sanitize_paths (int argc , const char * * argv ,
687
+ const char * prefix , int skip_checks )
688
+ {
689
+ int i ;
690
+
691
+ if (!argc )
692
+ return ;
693
+
694
+ if (prefix && * prefix && core_sparse_checkout_cone ) {
695
+ /*
696
+ * The args are not pathspecs, so unfortunately we
697
+ * cannot imitate how cmd_add() uses parse_pathspec().
698
+ */
699
+ int prefix_len = strlen (prefix );
700
+
701
+ for (i = 0 ; i < argc ; i ++ )
702
+ argv [i ] = prefix_path (prefix , prefix_len , argv [i ]);
703
+ }
704
+
705
+ if (skip_checks )
706
+ return ;
707
+
708
+ if (prefix && * prefix && !core_sparse_checkout_cone )
709
+ die (_ ("please run from the toplevel directory in non-cone mode" ));
710
+
711
+ if (core_sparse_checkout_cone ) {
712
+ for (i = 0 ; i < argc ; i ++ ) {
713
+ if (argv [i ][0 ] == '/' )
714
+ die (_ ("specify directories rather than patterns (no leading slash)" ));
715
+ if (argv [i ][0 ] == '!' )
716
+ die (_ ("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks" ));
717
+ if (strpbrk (argv [i ], "*?[]" ))
718
+ die (_ ("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks" ));
719
+ }
720
+ }
721
+
722
+ for (i = 0 ; i < argc ; i ++ ) {
723
+ struct cache_entry * ce ;
724
+ struct index_state * index = the_repository -> index ;
725
+ int pos = index_name_pos (index , argv [i ], strlen (argv [i ]));
726
+
727
+ if (pos < 0 )
728
+ continue ;
729
+ ce = index -> cache [pos ];
730
+ if (S_ISSPARSEDIR (ce -> ce_mode ))
731
+ continue ;
732
+
733
+ if (core_sparse_checkout_cone )
734
+ die (_ ("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks" ), argv [i ]);
735
+ else
736
+ warning (_ ("pass a leading slash before paths such as '%s' if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)." ), argv [i ]);
737
+ }
738
+ }
739
+
684
740
static char const * const builtin_sparse_checkout_add_usage [] = {
685
- N_ ("git sparse-checkout add (--stdin | <patterns>)" ),
741
+ N_ ("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)" ),
686
742
NULL
687
743
};
688
744
689
745
static struct sparse_checkout_add_opts {
746
+ int skip_checks ;
690
747
int use_stdin ;
691
748
} add_opts ;
692
749
693
750
static int sparse_checkout_add (int argc , const char * * argv , const char * prefix )
694
751
{
695
752
static struct option builtin_sparse_checkout_add_options [] = {
753
+ OPT_BOOL_F (0 , "skip-checks" , & add_opts .skip_checks ,
754
+ N_ ("skip some sanity checks on the given paths that might give false positives" ),
755
+ PARSE_OPT_NONEG ),
696
756
OPT_BOOL (0 , "stdin" , & add_opts .use_stdin ,
697
757
N_ ("read patterns from standard in" )),
698
758
OPT_END (),
@@ -708,17 +768,20 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
708
768
builtin_sparse_checkout_add_usage ,
709
769
PARSE_OPT_KEEP_UNKNOWN );
710
770
771
+ sanitize_paths (argc , argv , prefix , add_opts .skip_checks );
772
+
711
773
return modify_pattern_list (argc , argv , add_opts .use_stdin , ADD );
712
774
}
713
775
714
776
static char const * const builtin_sparse_checkout_set_usage [] = {
715
- N_ ("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | <patterns>)" ),
777
+ N_ ("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)" ),
716
778
NULL
717
779
};
718
780
719
781
static struct sparse_checkout_set_opts {
720
782
int cone_mode ;
721
783
int sparse_index ;
784
+ int skip_checks ;
722
785
int use_stdin ;
723
786
} set_opts ;
724
787
@@ -732,6 +795,9 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
732
795
N_ ("initialize the sparse-checkout in cone mode" )),
733
796
OPT_BOOL (0 , "sparse-index" , & set_opts .sparse_index ,
734
797
N_ ("toggle the use of a sparse index" )),
798
+ OPT_BOOL_F (0 , "skip-checks" , & set_opts .skip_checks ,
799
+ N_ ("skip some sanity checks on the given paths that might give false positives" ),
800
+ PARSE_OPT_NONEG ),
735
801
OPT_BOOL_F (0 , "stdin" , & set_opts .use_stdin ,
736
802
N_ ("read patterns from standard in" ),
737
803
PARSE_OPT_NONEG ),
@@ -759,6 +825,8 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
759
825
if (!core_sparse_checkout_cone && argc == 0 ) {
760
826
argv = default_patterns ;
761
827
argc = default_patterns_nr ;
828
+ } else {
829
+ sanitize_paths (argc , argv , prefix , set_opts .skip_checks );
762
830
}
763
831
764
832
return modify_pattern_list (argc , argv , set_opts .use_stdin , REPLACE );
@@ -787,15 +855,15 @@ static int sparse_checkout_reapply(int argc, const char **argv)
787
855
if (!core_apply_sparse_checkout )
788
856
die (_ ("must be in a sparse-checkout to reapply sparsity patterns" ));
789
857
858
+ reapply_opts .cone_mode = -1 ;
859
+ reapply_opts .sparse_index = -1 ;
860
+
790
861
argc = parse_options (argc , argv , NULL ,
791
862
builtin_sparse_checkout_reapply_options ,
792
863
builtin_sparse_checkout_reapply_usage , 0 );
793
864
794
865
repo_read_index (the_repository );
795
866
796
- reapply_opts .cone_mode = -1 ;
797
- reapply_opts .sparse_index = -1 ;
798
-
799
867
if (update_modes (& reapply_opts .cone_mode , & reapply_opts .sparse_index ))
800
868
return 1 ;
801
869
0 commit comments