Skip to content

Commit d02343b

Browse files
committed
Merge branch 'ws/sparse-check-rules'
"git sparse-checkout" command learns a debugging aid for the sparse rule definitions. * ws/sparse-check-rules: builtin/sparse-checkout: add check-rules command builtin/sparse-checkout: remove NEED_WORK_TREE flag
2 parents f285f68 + 00408ad commit d02343b

File tree

4 files changed

+307
-24
lines changed

4 files changed

+307
-24
lines changed

Documentation/git-sparse-checkout.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-sparse-checkout - Reduce your working tree to a subset of tracked files
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git sparse-checkout' (init | list | set | add | reapply | disable) [<options>]
12+
'git sparse-checkout' (init | list | set | add | reapply | disable | check-rules) [<options>]
1313

1414

1515
DESCRIPTION
@@ -135,6 +135,29 @@ paths to pass to a subsequent 'set' or 'add' command. However,
135135
the disable command, so the easy restore of calling a plain `init`
136136
decreased in utility.
137137

138+
'check-rules'::
139+
Check whether sparsity rules match one or more paths.
140+
+
141+
By default `check-rules` reads a list of paths from stdin and outputs only
142+
the ones that match the current sparsity rules. The input is expected to consist
143+
of one path per line, matching the output of `git ls-tree --name-only` including
144+
that pathnames that begin with a double quote (") are interpreted as C-style
145+
quoted strings.
146+
+
147+
When called with the `--rules-file <file>` flag the input files are matched
148+
against the sparse checkout rules found in `<file>` instead of the current ones.
149+
The rules in the files are expected to be in the same form as accepted by `git
150+
sparse-checkout set --stdin` (in particular, they must be newline-delimited).
151+
+
152+
By default, the rules passed to the `--rules-file` option are interpreted as
153+
cone mode directories. To pass non-cone mode patterns with `--rules-file`,
154+
combine the option with the `--no-cone` option.
155+
+
156+
When called with the `-z` flag, the format of the paths input on stdin as well
157+
as the output paths are \0 terminated and not quoted. Note that this does not
158+
apply to the format of the rules passed with the `--rules-file` option.
159+
160+
138161
EXAMPLES
139162
--------
140163
`git sparse-checkout set MY/DIR1 SUB/DIR2`::

builtin/sparse-checkout.c

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
static const char *empty_base = "";
2424

2525
static char const * const builtin_sparse_checkout_usage[] = {
26-
N_("git sparse-checkout (init | list | set | add | reapply | disable) [<options>]"),
26+
N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
2727
NULL
2828
};
2929

@@ -60,6 +60,7 @@ static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
6060
char *sparse_filename;
6161
int res;
6262

63+
setup_work_tree();
6364
if (!core_apply_sparse_checkout)
6465
die(_("this worktree is not sparse"));
6566

@@ -384,26 +385,29 @@ static int set_config(enum sparse_checkout_mode mode)
384385
return 0;
385386
}
386387

387-
static int update_modes(int *cone_mode, int *sparse_index)
388-
{
389-
int mode, record_mode;
390-
391-
/* Determine if we need to record the mode; ensure sparse checkout on */
392-
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
393-
388+
static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
394389
/* If not specified, use previous definition of cone mode */
395390
if (*cone_mode == -1 && core_apply_sparse_checkout)
396391
*cone_mode = core_sparse_checkout_cone;
397392

398393
/* Set cone/non-cone mode appropriately */
399394
core_apply_sparse_checkout = 1;
400395
if (*cone_mode == 1 || *cone_mode == -1) {
401-
mode = MODE_CONE_PATTERNS;
402396
core_sparse_checkout_cone = 1;
403-
} else {
404-
mode = MODE_ALL_PATTERNS;
405-
core_sparse_checkout_cone = 0;
397+
return MODE_CONE_PATTERNS;
406398
}
399+
core_sparse_checkout_cone = 0;
400+
return MODE_ALL_PATTERNS;
401+
}
402+
403+
static int update_modes(int *cone_mode, int *sparse_index)
404+
{
405+
int mode, record_mode;
406+
407+
/* Determine if we need to record the mode; ensure sparse checkout on */
408+
record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
409+
410+
mode = update_cone_mode(cone_mode);
407411
if (record_mode && set_config(mode))
408412
return 1;
409413

@@ -449,6 +453,7 @@ static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
449453
OPT_END(),
450454
};
451455

456+
setup_work_tree();
452457
repo_read_index(the_repository);
453458

454459
init_opts.cone_mode = -1;
@@ -546,7 +551,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
546551

547552
static void add_patterns_from_input(struct pattern_list *pl,
548553
int argc, const char **argv,
549-
int use_stdin)
554+
FILE *file)
550555
{
551556
int i;
552557
if (core_sparse_checkout_cone) {
@@ -556,9 +561,9 @@ static void add_patterns_from_input(struct pattern_list *pl,
556561
hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
557562
pl->use_cone_patterns = 1;
558563

559-
if (use_stdin) {
564+
if (file) {
560565
struct strbuf unquoted = STRBUF_INIT;
561-
while (!strbuf_getline(&line, stdin)) {
566+
while (!strbuf_getline(&line, file)) {
562567
if (line.buf[0] == '"') {
563568
strbuf_reset(&unquoted);
564569
if (unquote_c_style(&unquoted, line.buf, NULL))
@@ -580,10 +585,10 @@ static void add_patterns_from_input(struct pattern_list *pl,
580585
}
581586
}
582587
} else {
583-
if (use_stdin) {
588+
if (file) {
584589
struct strbuf line = STRBUF_INIT;
585590

586-
while (!strbuf_getline(&line, stdin)) {
591+
while (!strbuf_getline(&line, file)) {
587592
size_t len;
588593
char *buf = strbuf_detach(&line, &len);
589594
add_pattern(buf, empty_base, 0, pl, 0);
@@ -610,7 +615,8 @@ static void add_patterns_cone_mode(int argc, const char **argv,
610615
struct pattern_list existing;
611616
char *sparse_filename = get_sparse_checkout_filename();
612617

613-
add_patterns_from_input(pl, argc, argv, use_stdin);
618+
add_patterns_from_input(pl, argc, argv,
619+
use_stdin ? stdin : NULL);
614620

615621
memset(&existing, 0, sizeof(existing));
616622
existing.use_cone_patterns = core_sparse_checkout_cone;
@@ -647,7 +653,7 @@ static void add_patterns_literal(int argc, const char **argv,
647653
pl, NULL, 0))
648654
die(_("unable to load existing sparse-checkout patterns"));
649655
free(sparse_filename);
650-
add_patterns_from_input(pl, argc, argv, use_stdin);
656+
add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
651657
}
652658

653659
static int modify_pattern_list(int argc, const char **argv, int use_stdin,
@@ -666,7 +672,8 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin,
666672
break;
667673

668674
case REPLACE:
669-
add_patterns_from_input(pl, argc, argv, use_stdin);
675+
add_patterns_from_input(pl, argc, argv,
676+
use_stdin ? stdin : NULL);
670677
break;
671678
}
672679

@@ -761,6 +768,7 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
761768
OPT_END(),
762769
};
763770

771+
setup_work_tree();
764772
if (!core_apply_sparse_checkout)
765773
die(_("no sparse-checkout to add to"));
766774

@@ -807,6 +815,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
807815
OPT_END(),
808816
};
809817

818+
setup_work_tree();
810819
repo_read_index(the_repository);
811820

812821
set_opts.cone_mode = -1;
@@ -856,6 +865,7 @@ static int sparse_checkout_reapply(int argc, const char **argv,
856865
OPT_END(),
857866
};
858867

868+
setup_work_tree();
859869
if (!core_apply_sparse_checkout)
860870
die(_("must be in a sparse-checkout to reapply sparsity patterns"));
861871

@@ -899,6 +909,7 @@ static int sparse_checkout_disable(int argc, const char **argv,
899909
* forcibly return to a dense checkout regardless of initial state.
900910
*/
901911

912+
setup_work_tree();
902913
argc = parse_options(argc, argv, prefix,
903914
builtin_sparse_checkout_disable_options,
904915
builtin_sparse_checkout_disable_usage, 0);
@@ -924,6 +935,91 @@ static int sparse_checkout_disable(int argc, const char **argv,
924935
return set_config(MODE_NO_PATTERNS);
925936
}
926937

938+
static char const * const builtin_sparse_checkout_check_rules_usage[] = {
939+
N_("git sparse-checkout check-rules [-z] [--skip-checks]"
940+
"[--[no-]cone] [--rules-file <file>]"),
941+
NULL
942+
};
943+
944+
static struct sparse_checkout_check_rules_opts {
945+
int cone_mode;
946+
int null_termination;
947+
char *rules_file;
948+
} check_rules_opts;
949+
950+
static int check_rules(struct pattern_list *pl, int null_terminated) {
951+
struct strbuf line = STRBUF_INIT;
952+
struct strbuf unquoted = STRBUF_INIT;
953+
char *path;
954+
int line_terminator = null_terminated ? 0 : '\n';
955+
strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
956+
: strbuf_getline;
957+
the_repository->index->sparse_checkout_patterns = pl;
958+
while (!getline_fn(&line, stdin)) {
959+
path = line.buf;
960+
if (!null_terminated && line.buf[0] == '"') {
961+
strbuf_reset(&unquoted);
962+
if (unquote_c_style(&unquoted, line.buf, NULL))
963+
die(_("unable to unquote C-style string '%s'"),
964+
line.buf);
965+
966+
path = unquoted.buf;
967+
}
968+
969+
if (path_in_sparse_checkout(path, the_repository->index))
970+
write_name_quoted(path, stdout, line_terminator);
971+
}
972+
strbuf_release(&line);
973+
strbuf_release(&unquoted);
974+
975+
return 0;
976+
}
977+
978+
static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
979+
{
980+
static struct option builtin_sparse_checkout_check_rules_options[] = {
981+
OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
982+
N_("terminate input and output files by a NUL character")),
983+
OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
984+
N_("when used with --rules-file interpret patterns as cone mode patterns")),
985+
OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
986+
N_("use patterns in <file> instead of the current ones.")),
987+
OPT_END(),
988+
};
989+
990+
FILE *fp;
991+
int ret;
992+
struct pattern_list pl = {0};
993+
char *sparse_filename;
994+
check_rules_opts.cone_mode = -1;
995+
996+
argc = parse_options(argc, argv, prefix,
997+
builtin_sparse_checkout_check_rules_options,
998+
builtin_sparse_checkout_check_rules_usage,
999+
PARSE_OPT_KEEP_UNKNOWN_OPT);
1000+
1001+
if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
1002+
check_rules_opts.cone_mode = 1;
1003+
1004+
update_cone_mode(&check_rules_opts.cone_mode);
1005+
pl.use_cone_patterns = core_sparse_checkout_cone;
1006+
if (check_rules_opts.rules_file) {
1007+
fp = xfopen(check_rules_opts.rules_file, "r");
1008+
add_patterns_from_input(&pl, argc, argv, fp);
1009+
fclose(fp);
1010+
} else {
1011+
sparse_filename = get_sparse_checkout_filename();
1012+
if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1013+
NULL, 0))
1014+
die(_("unable to load existing sparse-checkout patterns"));
1015+
free(sparse_filename);
1016+
}
1017+
1018+
ret = check_rules(&pl, check_rules_opts.null_termination);
1019+
clear_pattern_list(&pl);
1020+
return ret;
1021+
}
1022+
9271023
int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
9281024
{
9291025
parse_opt_subcommand_fn *fn = NULL;
@@ -934,6 +1030,7 @@ int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
9341030
OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
9351031
OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
9361032
OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1033+
OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
9371034
OPT_END(),
9381035
};
9391036

git.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ static struct cmd_struct commands[] = {
587587
{ "show-branch", cmd_show_branch, RUN_SETUP },
588588
{ "show-index", cmd_show_index, RUN_SETUP_GENTLY },
589589
{ "show-ref", cmd_show_ref, RUN_SETUP },
590-
{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP | NEED_WORK_TREE },
590+
{ "sparse-checkout", cmd_sparse_checkout, RUN_SETUP },
591591
{ "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
592592
{ "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE },
593593
{ "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },

0 commit comments

Comments
 (0)