Skip to content

Commit d83491a

Browse files
committed
Merge branch 'ac/sparse-checkout-string-list-cleanup'
Code clean-up. * ac/sparse-checkout-string-list-cleanup: sparse-checkout: optimize string_list construction and add tests to verify deduplication.
2 parents b77c91a + 4922359 commit d83491a

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

builtin/sparse-checkout.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,11 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
9191

9292
hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
9393
/* pe->pattern starts with "/", skip it */
94-
string_list_insert(&sl, pe->pattern + 1);
94+
string_list_append(&sl, pe->pattern + 1);
9595
}
9696

9797
string_list_sort(&sl);
98+
string_list_remove_duplicates(&sl, 0);
9899

99100
for (i = 0; i < sl.nr; i++) {
100101
quote_c_style(sl.items[i].string, NULL, stdout, 0);
@@ -289,7 +290,7 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
289290
if (!hashmap_contains_parent(&pl->recursive_hashmap,
290291
pe->pattern,
291292
&parent_pattern))
292-
string_list_insert(&sl, pe->pattern);
293+
string_list_append(&sl, pe->pattern);
293294
}
294295

295296
string_list_sort(&sl);
@@ -311,7 +312,7 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
311312
if (!hashmap_contains_parent(&pl->recursive_hashmap,
312313
pe->pattern,
313314
&parent_pattern))
314-
string_list_insert(&sl, pe->pattern);
315+
string_list_append(&sl, pe->pattern);
315316
}
316317

317318
strbuf_release(&parent_pattern);

t/t1091-sparse-checkout-builtin.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,54 @@ test_expect_success 'cone mode clears ignored subdirectories' '
817817
test_cmp expect out
818818
'
819819

820+
test_expect_success 'sparse-checkout deduplicates repeated cone patterns' '
821+
rm -f repo/.git/info/sparse-checkout &&
822+
git -C repo sparse-checkout init --cone &&
823+
git -C repo sparse-checkout add --stdin <<-\EOF &&
824+
foo/bar/baz
825+
a/b/c
826+
foo/bar/baz
827+
a/b
828+
EOF
829+
cat >expect <<-\EOF &&
830+
/*
831+
!/*/
832+
/a/
833+
!/a/*/
834+
/foo/
835+
!/foo/*/
836+
/foo/bar/
837+
!/foo/bar/*/
838+
/a/b/
839+
/foo/bar/baz/
840+
EOF
841+
test_cmp expect repo/.git/info/sparse-checkout
842+
'
843+
844+
test_expect_success 'sparse-checkout list deduplicates repeated cone patterns' '
845+
rm -f repo/.git/info/sparse-checkout &&
846+
git -C repo sparse-checkout init --cone &&
847+
cat <<-\EOF >repo/.git/info/sparse-checkout &&
848+
/*
849+
!/*/
850+
/a/
851+
!/a/*/
852+
/foo/
853+
!/foo/*/
854+
/foo/bar/
855+
!/foo/bar/*/
856+
/a/b/
857+
/foo/bar/baz/
858+
/foo/bar/baz/
859+
EOF
860+
git -C repo sparse-checkout list >actual &&
861+
cat <<-\EOF >expect &&
862+
a/b
863+
foo/bar/baz
864+
EOF
865+
test_cmp expect actual
866+
'
867+
820868
test_expect_success 'malformed cone-mode patterns' '
821869
git -C repo sparse-checkout init --cone &&
822870
mkdir -p repo/foo/bar &&

0 commit comments

Comments
 (0)