@@ -96,10 +96,11 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
9696 printf ("\n" );
9797 }
9898
99- return 0 ;
99+ string_list_clear (& sl , 0 );
100+ } else {
101+ write_patterns_to_file (stdout , & pl );
100102 }
101103
102- write_patterns_to_file (stdout , & pl );
103104 clear_pattern_list (& pl );
104105
105106 return 0 ;
@@ -205,11 +206,13 @@ static int update_working_directory(struct pattern_list *pl)
205206 struct unpack_trees_options o ;
206207 struct lock_file lock_file = LOCK_INIT ;
207208 struct repository * r = the_repository ;
209+ struct pattern_list * old_pl ;
208210
209211 /* If no branch has been checked out, there are no updates to make. */
210212 if (is_index_unborn (r -> index ))
211213 return UPDATE_SPARSITY_SUCCESS ;
212214
215+ old_pl = r -> index -> sparse_checkout_patterns ;
213216 r -> index -> sparse_checkout_patterns = pl ;
214217
215218 memset (& o , 0 , sizeof (o ));
@@ -241,7 +244,12 @@ static int update_working_directory(struct pattern_list *pl)
241244
242245 clean_tracked_sparse_directories (r );
243246
244- r -> index -> sparse_checkout_patterns = NULL ;
247+ if (r -> index -> sparse_checkout_patterns != pl ) {
248+ clear_pattern_list (r -> index -> sparse_checkout_patterns );
249+ FREE_AND_NULL (r -> index -> sparse_checkout_patterns );
250+ }
251+ r -> index -> sparse_checkout_patterns = old_pl ;
252+
245253 return result ;
246254}
247255
@@ -311,6 +319,8 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
311319 fprintf (fp , "%s/\n" , pattern );
312320 free (pattern );
313321 }
322+
323+ string_list_clear (& sl , 0 );
314324}
315325
316326static int write_patterns_and_update (struct pattern_list * pl )
@@ -440,7 +450,6 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
440450 char * sparse_filename ;
441451 int res ;
442452 struct object_id oid ;
443- struct strbuf pattern = STRBUF_INIT ;
444453
445454 static struct option builtin_sparse_checkout_init_options [] = {
446455 OPT_BOOL (0 , "cone" , & init_opts .cone_mode ,
@@ -471,6 +480,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
471480 /* If we already have a sparse-checkout file, use it. */
472481 if (res >= 0 ) {
473482 free (sparse_filename );
483+ clear_pattern_list (& pl );
474484 return update_working_directory (NULL );
475485 }
476486
@@ -491,10 +501,10 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
491501 return 0 ;
492502 }
493503
494- strbuf_addstr ( & pattern , "/*" );
495- add_pattern ( strbuf_detach ( & pattern , NULL ), empty_base , 0 , & pl , 0 );
496- strbuf_addstr ( & pattern , "!/*/" );
497- add_pattern (strbuf_detach ( & pattern , NULL ) , empty_base , 0 , & pl , 0 );
504+ free ( sparse_filename );
505+
506+ add_pattern ( "/*" , empty_base , 0 , & pl , 0 );
507+ add_pattern ("!/*/" , empty_base , 0 , & pl , 0 );
498508 pl .use_cone_patterns = init_opts .cone_mode ;
499509
500510 return write_patterns_and_update (& pl );
@@ -513,6 +523,7 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat
513523 char * slash = strrchr (e -> pattern , '/' );
514524 char * oldpattern = e -> pattern ;
515525 size_t newlen ;
526+ struct pattern_entry * dup ;
516527
517528 if (!slash || slash == e -> pattern )
518529 break ;
@@ -523,8 +534,14 @@ static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *pat
523534 e -> pattern = xstrndup (oldpattern , newlen );
524535 hashmap_entry_init (& e -> ent , fspathhash (e -> pattern ));
525536
526- if (!hashmap_get_entry (& pl -> parent_hashmap , e , ent , NULL ))
537+ dup = hashmap_get_entry (& pl -> parent_hashmap , e , ent , NULL );
538+ if (!dup ) {
527539 hashmap_add (& pl -> parent_hashmap , & e -> ent );
540+ } else {
541+ free (e -> pattern );
542+ free (e );
543+ e = dup ;
544+ }
528545 }
529546}
530547
@@ -581,15 +598,15 @@ static void add_patterns_from_input(struct pattern_list *pl,
581598 strbuf_to_cone_pattern (& line , pl );
582599 }
583600 }
601+ strbuf_release (& line );
584602 } else {
585603 if (file ) {
586604 struct strbuf line = STRBUF_INIT ;
587605
588- while (!strbuf_getline (& line , file )) {
589- size_t len ;
590- char * buf = strbuf_detach (& line , & len );
591- add_pattern (buf , empty_base , 0 , pl , 0 );
592- }
606+ while (!strbuf_getline (& line , file ))
607+ add_pattern (line .buf , empty_base , 0 , pl , 0 );
608+
609+ strbuf_release (& line );
593610 } else {
594611 for (i = 0 ; i < argc ; i ++ )
595612 add_pattern (argv [i ], empty_base , 0 , pl , 0 );
@@ -891,7 +908,6 @@ static int sparse_checkout_disable(int argc, const char **argv,
891908 OPT_END (),
892909 };
893910 struct pattern_list pl ;
894- struct strbuf match_all = STRBUF_INIT ;
895911
896912 /*
897913 * We do not exit early if !core_apply_sparse_checkout; due to the
@@ -917,8 +933,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
917933 pl .use_cone_patterns = 0 ;
918934 core_apply_sparse_checkout = 1 ;
919935
920- strbuf_addstr (& match_all , "/*" );
921- add_pattern (strbuf_detach (& match_all , NULL ), empty_base , 0 , & pl , 0 );
936+ add_pattern ("/*" , empty_base , 0 , & pl , 0 );
922937
923938 prepare_repo_settings (the_repository );
924939 the_repository -> settings .sparse_index = 0 ;
0 commit comments