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
;
105 struct object_id oid
;
107 if (set_config(MODE_ALL_PATTERNS
))
110 memset(&pl
, 0, sizeof(pl
));
112 sparse_filename
= get_sparse_checkout_filename();
113 res
= add_patterns_from_file_to_list(sparse_filename
, "", 0, &pl
, NULL
);
115 /* If we already have a sparse-checkout file, use it. */
117 free(sparse_filename
);
121 /* initial mode: all blobs at root */
122 fp
= xfopen(sparse_filename
, "w");
124 die(_("failed to open '%s'"), sparse_filename
);
126 free(sparse_filename
);
127 fprintf(fp
, "/*\n!/*/\n");
130 if (get_oid("HEAD", &oid
)) {
131 /* assume we are in a fresh repo */
136 return update_working_directory();
139 int cmd_sparse_checkout(int argc
, const char **argv
, const char *prefix
)
141 static struct option builtin_sparse_checkout_options
[] = {
145 if (argc
== 2 && !strcmp(argv
[1], "-h"))
146 usage_with_options(builtin_sparse_checkout_usage
,
147 builtin_sparse_checkout_options
);
149 argc
= parse_options(argc
, argv
, prefix
,
150 builtin_sparse_checkout_options
,
151 builtin_sparse_checkout_usage
,
152 PARSE_OPT_STOP_AT_NON_OPTION
);
154 git_config(git_default_config
, NULL
);
157 if (!strcmp(argv
[0], "list"))
158 return sparse_checkout_list(argc
, argv
);
159 if (!strcmp(argv
[0], "init"))
160 return sparse_checkout_init(argc
, argv
);
163 usage_with_options(builtin_sparse_checkout_usage
,
164 builtin_sparse_checkout_options
);