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 (init|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 static int update_working_directory(void)
64 struct argv_array argv = ARGV_ARRAY_INIT;
66 argv_array_pushl(&argv, "read-tree", "-m", "-u", "HEAD", NULL);
68 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
69 error(_("failed to update index with new sparse-checkout paths"));
73 argv_array_clear(&argv);
77 enum sparse_checkout_mode {
79 MODE_ALL_PATTERNS = 1,
82 static int set_config(enum sparse_checkout_mode mode)
84 const char *config_path;
86 if (git_config_set_gently("extensions.worktreeConfig", "true")) {
87 error(_("failed to set extensions.worktreeConfig setting"));
91 config_path = git_path("config.worktree");
92 git_config_set_in_file_gently(config_path,
93 "core.sparseCheckout",
94 mode ? "true" : NULL);
99 static int sparse_checkout_init(int argc, const char **argv)
101 struct pattern_list pl;
102 char *sparse_filename;
106 if (set_config(MODE_ALL_PATTERNS))
109 memset(&pl, 0, sizeof(pl));
111 sparse_filename = get_sparse_checkout_filename();
112 res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL);
114 /* If we already have a sparse-checkout file, use it. */
116 free(sparse_filename);
120 /* initial mode: all blobs at root */
121 fp = xfopen(sparse_filename, "w");
123 die(_("failed to open '%s'"), sparse_filename);
125 free(sparse_filename);
126 fprintf(fp, "/*\n!/*/\n");
130 return update_working_directory();
133 int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
135 static struct option builtin_sparse_checkout_options[] = {
139 if (argc == 2 && !strcmp(argv[1], "-h"))
140 usage_with_options(builtin_sparse_checkout_usage,
141 builtin_sparse_checkout_options);
143 argc = parse_options(argc, argv, prefix,
144 builtin_sparse_checkout_options,
145 builtin_sparse_checkout_usage,
146 PARSE_OPT_STOP_AT_NON_OPTION);
148 git_config(git_default_config, NULL);
151 if (!strcmp(argv[0], "list"))
152 return sparse_checkout_list(argc, argv);
153 if (!strcmp(argv[0], "init"))
154 return sparse_checkout_init(argc, argv);
157 usage_with_options(builtin_sparse_checkout_usage,
158 builtin_sparse_checkout_options);