sparse-checkout: update working directory in-process for 'init'

The 'git sparse-checkout init' subcommand previously wrote directly
to the sparse-checkout file and then updated the working directory.
This may fail if there are modified files not included in the initial
pattern set. However, that left a populated sparse-checkout file.

Use the in-process working directory update to guarantee that the
init subcommand only changes the sparse-checkout file if the working
directory update succeeds.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Derrick Stolee 2019-11-21 22:04:50 +00:00 committed by Junio C Hamano
parent f75a69f880
commit 416adc8711

View file

@ -13,6 +13,8 @@
#include "resolve-undo.h" #include "resolve-undo.h"
#include "unpack-trees.h" #include "unpack-trees.h"
static const char *empty_base = "";
static char const * const builtin_sparse_checkout_usage[] = { static char const * const builtin_sparse_checkout_usage[] = {
N_("git sparse-checkout (init|list|set|disable) <options>"), N_("git sparse-checkout (init|list|set|disable) <options>"),
NULL NULL
@ -243,10 +245,10 @@ static int sparse_checkout_init(int argc, const char **argv)
{ {
struct pattern_list pl; struct pattern_list pl;
char *sparse_filename; char *sparse_filename;
FILE *fp;
int res; int res;
struct object_id oid; struct object_id oid;
int mode; int mode;
struct strbuf pattern = STRBUF_INIT;
static struct option builtin_sparse_checkout_init_options[] = { static struct option builtin_sparse_checkout_init_options[] = {
OPT_BOOL(0, "cone", &init_opts.cone_mode, OPT_BOOL(0, "cone", &init_opts.cone_mode,
@ -275,10 +277,14 @@ static int sparse_checkout_init(int argc, const char **argv)
/* If we already have a sparse-checkout file, use it. */ /* If we already have a sparse-checkout file, use it. */
if (res >= 0) { if (res >= 0) {
free(sparse_filename); free(sparse_filename);
goto reset_dir; core_apply_sparse_checkout = 1;
return update_working_directory(NULL);
} }
/* initial mode: all blobs at root */ if (get_oid("HEAD", &oid)) {
FILE *fp;
/* assume we are in a fresh repo, but update the sparse-checkout file */
fp = xfopen(sparse_filename, "w"); fp = xfopen(sparse_filename, "w");
if (!fp) if (!fp)
die(_("failed to open '%s'"), sparse_filename); die(_("failed to open '%s'"), sparse_filename);
@ -286,15 +292,15 @@ static int sparse_checkout_init(int argc, const char **argv)
free(sparse_filename); free(sparse_filename);
fprintf(fp, "/*\n!/*/\n"); fprintf(fp, "/*\n!/*/\n");
fclose(fp); fclose(fp);
if (get_oid("HEAD", &oid)) {
/* assume we are in a fresh repo */
return 0; return 0;
} }
reset_dir: strbuf_addstr(&pattern, "/*");
core_apply_sparse_checkout = 1; add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
return update_working_directory(NULL); strbuf_addstr(&pattern, "!/*/");
add_pattern(strbuf_detach(&pattern, NULL), empty_base, 0, &pl, 0);
return write_patterns_and_update(&pl);
} }
static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path) static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
@ -351,7 +357,6 @@ static struct sparse_checkout_set_opts {
static int sparse_checkout_set(int argc, const char **argv, const char *prefix) static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
{ {
static const char *empty_base = "";
int i; int i;
struct pattern_list pl; struct pattern_list pl;
int result; int result;
@ -419,7 +424,6 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
static int sparse_checkout_disable(int argc, const char **argv) static int sparse_checkout_disable(int argc, const char **argv)
{ {
static const char *empty_base = "";
struct pattern_list pl; struct pattern_list pl;
struct strbuf match_all = STRBUF_INIT; struct strbuf match_all = STRBUF_INIT;