|
13 | 13 | #include "resolve-undo.h"
|
14 | 14 | #include "unpack-trees.h"
|
15 | 15 |
|
| 16 | +static const char *empty_base = ""; |
| 17 | + |
16 | 18 | static char const * const builtin_sparse_checkout_usage[] = {
|
17 | 19 | N_("git sparse-checkout (init|list|set|disable) <options>"),
|
18 | 20 | NULL
|
@@ -243,10 +245,10 @@ static int sparse_checkout_init(int argc, const char **argv)
|
243 | 245 | {
|
244 | 246 | struct pattern_list pl;
|
245 | 247 | char *sparse_filename;
|
246 |
| - FILE *fp; |
247 | 248 | int res;
|
248 | 249 | struct object_id oid;
|
249 | 250 | int mode;
|
| 251 | + struct strbuf pattern = STRBUF_INIT; |
250 | 252 |
|
251 | 253 | static struct option builtin_sparse_checkout_init_options[] = {
|
252 | 254 | OPT_BOOL(0, "cone", &init_opts.cone_mode,
|
@@ -275,26 +277,30 @@ static int sparse_checkout_init(int argc, const char **argv)
|
275 | 277 | /* If we already have a sparse-checkout file, use it. */
|
276 | 278 | if (res >= 0) {
|
277 | 279 | free(sparse_filename);
|
278 |
| - goto reset_dir; |
| 280 | + core_apply_sparse_checkout = 1; |
| 281 | + return update_working_directory(NULL); |
279 | 282 | }
|
280 | 283 |
|
281 |
| - /* initial mode: all blobs at root */ |
282 |
| - fp = xfopen(sparse_filename, "w"); |
283 |
| - if (!fp) |
284 |
| - die(_("failed to open '%s'"), sparse_filename); |
| 284 | + if (get_oid("HEAD", &oid)) { |
| 285 | + FILE *fp; |
285 | 286 |
|
286 |
| - free(sparse_filename); |
287 |
| - fprintf(fp, "/*\n!/*/\n"); |
288 |
| - fclose(fp); |
| 287 | + /* assume we are in a fresh repo, but update the sparse-checkout file */ |
| 288 | + fp = xfopen(sparse_filename, "w"); |
| 289 | + if (!fp) |
| 290 | + die(_("failed to open '%s'"), sparse_filename); |
289 | 291 |
|
290 |
| - if (get_oid("HEAD", &oid)) { |
291 |
| - /* assume we are in a fresh repo */ |
| 292 | + free(sparse_filename); |
| 293 | + fprintf(fp, "/*\n!/*/\n"); |
| 294 | + fclose(fp); |
292 | 295 | return 0;
|
293 | 296 | }
|
294 | 297 |
|
295 |
| -reset_dir: |
296 |
| - core_apply_sparse_checkout = 1; |
297 |
| - return update_working_directory(NULL); |
| 298 | + strbuf_addstr(&pattern, "/*"); |
| 299 | + add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); |
| 300 | + strbuf_addstr(&pattern, "!/*/"); |
| 301 | + add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0); |
| 302 | + |
| 303 | + return write_patterns_and_update(&pl); |
298 | 304 | }
|
299 | 305 |
|
300 | 306 | static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
|
@@ -351,7 +357,6 @@ static struct sparse_checkout_set_opts {
|
351 | 357 |
|
352 | 358 | static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
|
353 | 359 | {
|
354 |
| - static const char *empty_base = ""; |
355 | 360 | int i;
|
356 | 361 | struct pattern_list pl;
|
357 | 362 | int result;
|
@@ -419,7 +424,6 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
|
419 | 424 |
|
420 | 425 | static int sparse_checkout_disable(int argc, const char **argv)
|
421 | 426 | {
|
422 |
| - static const char *empty_base = ""; |
423 | 427 | struct pattern_list pl;
|
424 | 428 | struct strbuf match_all = STRBUF_INIT;
|
425 | 429 |
|
|
0 commit comments