4 #include "parse-options.h"
6 #include "repository.h"
7 #include "run-command.h"
10 static char const * const builtin_sparse_checkout_usage[] = {
11 N_("git sparse-checkout list"),
15 static char *get_sparse_checkout_filename(void)
17 return git_pathdup("info/sparse-checkout");
20 static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
24 for (i = 0; i < pl->nr; i++) {
25 struct path_pattern *p = pl->patterns[i];
27 if (p->flags & PATTERN_FLAG_NEGATIVE)
30 fprintf(fp, "%s", p->pattern);
32 if (p->flags & PATTERN_FLAG_MUSTBEDIR)
39 static int sparse_checkout_list(int argc, const char **argv)
41 struct pattern_list pl;
42 char *sparse_filename;
45 memset(&pl, 0, sizeof(pl));
47 sparse_filename = get_sparse_checkout_filename();
48 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
49 free(sparse_filename);
52 warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
56 write_patterns_to_file(stdout, &pl);
57 clear_pattern_list(&pl);
62 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
64 static struct option builtin_sparse_checkout_options[] = {
68 if (argc == 2 && !strcmp(argv[1], "-h"))
69 usage_with_options(builtin_sparse_checkout_usage,
70 builtin_sparse_checkout_options);
72 argc = parse_options(argc, argv, prefix,
73 builtin_sparse_checkout_options,
74 builtin_sparse_checkout_usage,
75 PARSE_OPT_STOP_AT_NON_OPTION);
77 git_config(git_default_config, NULL);
80 if (!strcmp(argv[0], "list"))
81 return sparse_checkout_list(argc, argv);
84 usage_with_options(builtin_sparse_checkout_usage,
85 builtin_sparse_checkout_options);