git

Форк
0
/
sparse-checkout.c 
1057 строк · 27.6 Кб
1
#include "builtin.h"
2
#include "config.h"
3
#include "dir.h"
4
#include "environment.h"
5
#include "gettext.h"
6
#include "object-file.h"
7
#include "object-name.h"
8
#include "parse-options.h"
9
#include "pathspec.h"
10
#include "repository.h"
11
#include "strbuf.h"
12
#include "string-list.h"
13
#include "lockfile.h"
14
#include "unpack-trees.h"
15
#include "quote.h"
16
#include "setup.h"
17
#include "sparse-index.h"
18
#include "worktree.h"
19

20
static const char *empty_base = "";
21

22
static char const * const builtin_sparse_checkout_usage[] = {
23
	N_("git sparse-checkout (init | list | set | add | reapply | disable | check-rules) [<options>]"),
24
	NULL
25
};
26

27
static void write_patterns_to_file(FILE *fp, struct pattern_list *pl)
28
{
29
	int i;
30

31
	for (i = 0; i < pl->nr; i++) {
32
		struct path_pattern *p = pl->patterns[i];
33

34
		if (p->flags & PATTERN_FLAG_NEGATIVE)
35
			fprintf(fp, "!");
36

37
		fprintf(fp, "%s", p->pattern);
38

39
		if (p->flags & PATTERN_FLAG_MUSTBEDIR)
40
			fprintf(fp, "/");
41

42
		fprintf(fp, "\n");
43
	}
44
}
45

46
static char const * const builtin_sparse_checkout_list_usage[] = {
47
	"git sparse-checkout list",
48
	NULL
49
};
50

51
static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
52
{
53
	static struct option builtin_sparse_checkout_list_options[] = {
54
		OPT_END(),
55
	};
56
	struct pattern_list pl;
57
	char *sparse_filename;
58
	int res;
59

60
	setup_work_tree();
61
	if (!core_apply_sparse_checkout)
62
		die(_("this worktree is not sparse"));
63

64
	argc = parse_options(argc, argv, prefix,
65
			     builtin_sparse_checkout_list_options,
66
			     builtin_sparse_checkout_list_usage, 0);
67

68
	memset(&pl, 0, sizeof(pl));
69

70
	pl.use_cone_patterns = core_sparse_checkout_cone;
71

72
	sparse_filename = get_sparse_checkout_filename();
73
	res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
74
	free(sparse_filename);
75

76
	if (res < 0) {
77
		warning(_("this worktree is not sparse (sparse-checkout file may not exist)"));
78
		return 0;
79
	}
80

81
	if (pl.use_cone_patterns) {
82
		int i;
83
		struct pattern_entry *pe;
84
		struct hashmap_iter iter;
85
		struct string_list sl = STRING_LIST_INIT_DUP;
86

87
		hashmap_for_each_entry(&pl.recursive_hashmap, &iter, pe, ent) {
88
			/* pe->pattern starts with "/", skip it */
89
			string_list_insert(&sl, pe->pattern + 1);
90
		}
91

92
		string_list_sort(&sl);
93

94
		for (i = 0; i < sl.nr; i++) {
95
			quote_c_style(sl.items[i].string, NULL, stdout, 0);
96
			printf("\n");
97
		}
98

99
		string_list_clear(&sl, 0);
100
	} else {
101
		write_patterns_to_file(stdout, &pl);
102
	}
103

104
	clear_pattern_list(&pl);
105

106
	return 0;
107
}
108

109
static void clean_tracked_sparse_directories(struct repository *r)
110
{
111
	int i, was_full = 0;
112
	struct strbuf path = STRBUF_INIT;
113
	size_t pathlen;
114
	struct string_list_item *item;
115
	struct string_list sparse_dirs = STRING_LIST_INIT_DUP;
116

117
	/*
118
	 * If we are not using cone mode patterns, then we cannot
119
	 * delete directories outside of the sparse cone.
120
	 */
121
	if (!r || !r->index || !r->worktree)
122
		return;
123
	if (init_sparse_checkout_patterns(r->index) ||
124
	    !r->index->sparse_checkout_patterns->use_cone_patterns)
125
		return;
126

127
	/*
128
	 * Use the sparse index as a data structure to assist finding
129
	 * directories that are safe to delete. This conversion to a
130
	 * sparse index will not delete directories that contain
131
	 * conflicted entries or submodules.
132
	 */
133
	if (r->index->sparse_index == INDEX_EXPANDED) {
134
		/*
135
		 * If something, such as a merge conflict or other concern,
136
		 * prevents us from converting to a sparse index, then do
137
		 * not try deleting files.
138
		 */
139
		if (convert_to_sparse(r->index, SPARSE_INDEX_MEMORY_ONLY))
140
			return;
141
		was_full = 1;
142
	}
143

144
	strbuf_addstr(&path, r->worktree);
145
	strbuf_complete(&path, '/');
146
	pathlen = path.len;
147

148
	/*
149
	 * Collect directories that have gone out of scope but also
150
	 * exist on disk, so there is some work to be done. We need to
151
	 * store the entries in a list before exploring, since that might
152
	 * expand the sparse-index again.
153
	 */
154
	for (i = 0; i < r->index->cache_nr; i++) {
155
		struct cache_entry *ce = r->index->cache[i];
156

157
		if (S_ISSPARSEDIR(ce->ce_mode) &&
158
		    repo_file_exists(r, ce->name))
159
			string_list_append(&sparse_dirs, ce->name);
160
	}
161

162
	for_each_string_list_item(item, &sparse_dirs) {
163
		struct dir_struct dir = DIR_INIT;
164
		struct pathspec p = { 0 };
165
		struct strvec s = STRVEC_INIT;
166

167
		strbuf_setlen(&path, pathlen);
168
		strbuf_addstr(&path, item->string);
169

170
		dir.flags |= DIR_SHOW_IGNORED_TOO;
171

172
		setup_standard_excludes(&dir);
173
		strvec_push(&s, path.buf);
174

175
		parse_pathspec(&p, PATHSPEC_GLOB, 0, NULL, s.v);
176
		fill_directory(&dir, r->index, &p);
177

178
		if (dir.nr) {
179
			warning(_("directory '%s' contains untracked files,"
180
				  " but is not in the sparse-checkout cone"),
181
				item->string);
182
		} else if (remove_dir_recursively(&path, 0)) {
183
			/*
184
			 * Removal is "best effort". If something blocks
185
			 * the deletion, then continue with a warning.
186
			 */
187
			warning(_("failed to remove directory '%s'"),
188
				item->string);
189
		}
190

191
		strvec_clear(&s);
192
		clear_pathspec(&p);
193
		dir_clear(&dir);
194
	}
195

196
	string_list_clear(&sparse_dirs, 0);
197
	strbuf_release(&path);
198

199
	if (was_full)
200
		ensure_full_index(r->index);
201
}
202

203
static int update_working_directory(struct pattern_list *pl)
204
{
205
	enum update_sparsity_result result;
206
	struct unpack_trees_options o;
207
	struct lock_file lock_file = LOCK_INIT;
208
	struct repository *r = the_repository;
209
	struct pattern_list *old_pl;
210

211
	/* If no branch has been checked out, there are no updates to make. */
212
	if (is_index_unborn(r->index))
213
		return UPDATE_SPARSITY_SUCCESS;
214

215
	old_pl = r->index->sparse_checkout_patterns;
216
	r->index->sparse_checkout_patterns = pl;
217

218
	memset(&o, 0, sizeof(o));
219
	o.verbose_update = isatty(2);
220
	o.update = 1;
221
	o.head_idx = -1;
222
	o.src_index = r->index;
223
	o.dst_index = r->index;
224
	o.skip_sparse_checkout = 0;
225

226
	setup_work_tree();
227

228
	repo_hold_locked_index(r, &lock_file, LOCK_DIE_ON_ERROR);
229

230
	setup_unpack_trees_porcelain(&o, "sparse-checkout");
231
	result = update_sparsity(&o, pl);
232
	clear_unpack_trees_porcelain(&o);
233

234
	if (result == UPDATE_SPARSITY_WARNINGS)
235
		/*
236
		 * We don't do any special handling of warnings from untracked
237
		 * files in the way or dirty entries that can't be removed.
238
		 */
239
		result = UPDATE_SPARSITY_SUCCESS;
240
	if (result == UPDATE_SPARSITY_SUCCESS)
241
		write_locked_index(r->index, &lock_file, COMMIT_LOCK);
242
	else
243
		rollback_lock_file(&lock_file);
244

245
	clean_tracked_sparse_directories(r);
246

247
	if (r->index->sparse_checkout_patterns != pl) {
248
		clear_pattern_list(r->index->sparse_checkout_patterns);
249
		FREE_AND_NULL(r->index->sparse_checkout_patterns);
250
	}
251
	r->index->sparse_checkout_patterns = old_pl;
252

253
	return result;
254
}
255

256
static char *escaped_pattern(char *pattern)
257
{
258
	char *p = pattern;
259
	struct strbuf final = STRBUF_INIT;
260

261
	while (*p) {
262
		if (is_glob_special(*p))
263
			strbuf_addch(&final, '\\');
264

265
		strbuf_addch(&final, *p);
266
		p++;
267
	}
268

269
	return strbuf_detach(&final, NULL);
270
}
271

272
static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
273
{
274
	int i;
275
	struct pattern_entry *pe;
276
	struct hashmap_iter iter;
277
	struct string_list sl = STRING_LIST_INIT_DUP;
278
	struct strbuf parent_pattern = STRBUF_INIT;
279

280
	hashmap_for_each_entry(&pl->parent_hashmap, &iter, pe, ent) {
281
		if (hashmap_get_entry(&pl->recursive_hashmap, pe, ent, NULL))
282
			continue;
283

284
		if (!hashmap_contains_parent(&pl->recursive_hashmap,
285
					     pe->pattern,
286
					     &parent_pattern))
287
			string_list_insert(&sl, pe->pattern);
288
	}
289

290
	string_list_sort(&sl);
291
	string_list_remove_duplicates(&sl, 0);
292

293
	fprintf(fp, "/*\n!/*/\n");
294

295
	for (i = 0; i < sl.nr; i++) {
296
		char *pattern = escaped_pattern(sl.items[i].string);
297

298
		if (strlen(pattern))
299
			fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
300
		free(pattern);
301
	}
302

303
	string_list_clear(&sl, 0);
304

305
	hashmap_for_each_entry(&pl->recursive_hashmap, &iter, pe, ent) {
306
		if (!hashmap_contains_parent(&pl->recursive_hashmap,
307
					     pe->pattern,
308
					     &parent_pattern))
309
			string_list_insert(&sl, pe->pattern);
310
	}
311

312
	strbuf_release(&parent_pattern);
313

314
	string_list_sort(&sl);
315
	string_list_remove_duplicates(&sl, 0);
316

317
	for (i = 0; i < sl.nr; i++) {
318
		char *pattern = escaped_pattern(sl.items[i].string);
319
		fprintf(fp, "%s/\n", pattern);
320
		free(pattern);
321
	}
322

323
	string_list_clear(&sl, 0);
324
}
325

326
static int write_patterns_and_update(struct pattern_list *pl)
327
{
328
	char *sparse_filename;
329
	FILE *fp;
330
	int fd;
331
	struct lock_file lk = LOCK_INIT;
332
	int result;
333

334
	sparse_filename = get_sparse_checkout_filename();
335

336
	if (safe_create_leading_directories(sparse_filename))
337
		die(_("failed to create directory for sparse-checkout file"));
338

339
	fd = hold_lock_file_for_update(&lk, sparse_filename,
340
				      LOCK_DIE_ON_ERROR);
341
	free(sparse_filename);
342

343
	result = update_working_directory(pl);
344
	if (result) {
345
		rollback_lock_file(&lk);
346
		clear_pattern_list(pl);
347
		update_working_directory(NULL);
348
		return result;
349
	}
350

351
	fp = xfdopen(fd, "w");
352

353
	if (core_sparse_checkout_cone)
354
		write_cone_to_file(fp, pl);
355
	else
356
		write_patterns_to_file(fp, pl);
357

358
	fflush(fp);
359
	commit_lock_file(&lk);
360

361
	clear_pattern_list(pl);
362

363
	return 0;
364
}
365

366
enum sparse_checkout_mode {
367
	MODE_NO_PATTERNS = 0,
368
	MODE_ALL_PATTERNS = 1,
369
	MODE_CONE_PATTERNS = 2,
370
};
371

372
static int set_config(enum sparse_checkout_mode mode)
373
{
374
	/* Update to use worktree config, if not already. */
375
	if (init_worktree_config(the_repository)) {
376
		error(_("failed to initialize worktree config"));
377
		return 1;
378
	}
379

380
	if (repo_config_set_worktree_gently(the_repository,
381
					    "core.sparseCheckout",
382
					    mode ? "true" : "false") ||
383
	    repo_config_set_worktree_gently(the_repository,
384
					    "core.sparseCheckoutCone",
385
					    mode == MODE_CONE_PATTERNS ?
386
						"true" : "false"))
387
		return 1;
388

389
	if (mode == MODE_NO_PATTERNS)
390
		return set_sparse_index_config(the_repository, 0);
391

392
	return 0;
393
}
394

395
static enum sparse_checkout_mode update_cone_mode(int *cone_mode) {
396
	/* If not specified, use previous definition of cone mode */
397
	if (*cone_mode == -1 && core_apply_sparse_checkout)
398
		*cone_mode = core_sparse_checkout_cone;
399

400
	/* Set cone/non-cone mode appropriately */
401
	core_apply_sparse_checkout = 1;
402
	if (*cone_mode == 1 || *cone_mode == -1) {
403
		core_sparse_checkout_cone = 1;
404
		return MODE_CONE_PATTERNS;
405
	}
406
	core_sparse_checkout_cone = 0;
407
	return MODE_ALL_PATTERNS;
408
}
409

410
static int update_modes(int *cone_mode, int *sparse_index)
411
{
412
	int mode, record_mode;
413

414
	/* Determine if we need to record the mode; ensure sparse checkout on */
415
	record_mode = (*cone_mode != -1) || !core_apply_sparse_checkout;
416

417
	mode = update_cone_mode(cone_mode);
418
	if (record_mode && set_config(mode))
419
		return 1;
420

421
	/* Set sparse-index/non-sparse-index mode if specified */
422
	if (*sparse_index >= 0) {
423
		if (set_sparse_index_config(the_repository, *sparse_index) < 0)
424
			die(_("failed to modify sparse-index config"));
425

426
		/* force an index rewrite */
427
		repo_read_index(the_repository);
428
		the_repository->index->updated_workdir = 1;
429

430
		if (!*sparse_index)
431
			ensure_full_index(the_repository->index);
432
	}
433

434
	return 0;
435
}
436

437
static char const * const builtin_sparse_checkout_init_usage[] = {
438
	"git sparse-checkout init [--cone] [--[no-]sparse-index]",
439
	NULL
440
};
441

442
static struct sparse_checkout_init_opts {
443
	int cone_mode;
444
	int sparse_index;
445
} init_opts;
446

447
static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
448
{
449
	struct pattern_list pl;
450
	char *sparse_filename;
451
	int res;
452
	struct object_id oid;
453

454
	static struct option builtin_sparse_checkout_init_options[] = {
455
		OPT_BOOL(0, "cone", &init_opts.cone_mode,
456
			 N_("initialize the sparse-checkout in cone mode")),
457
		OPT_BOOL(0, "sparse-index", &init_opts.sparse_index,
458
			 N_("toggle the use of a sparse index")),
459
		OPT_END(),
460
	};
461

462
	setup_work_tree();
463
	repo_read_index(the_repository);
464

465
	init_opts.cone_mode = -1;
466
	init_opts.sparse_index = -1;
467

468
	argc = parse_options(argc, argv, prefix,
469
			     builtin_sparse_checkout_init_options,
470
			     builtin_sparse_checkout_init_usage, 0);
471

472
	if (update_modes(&init_opts.cone_mode, &init_opts.sparse_index))
473
		return 1;
474

475
	memset(&pl, 0, sizeof(pl));
476

477
	sparse_filename = get_sparse_checkout_filename();
478
	res = add_patterns_from_file_to_list(sparse_filename, "", 0, &pl, NULL, 0);
479

480
	/* If we already have a sparse-checkout file, use it. */
481
	if (res >= 0) {
482
		free(sparse_filename);
483
		clear_pattern_list(&pl);
484
		return update_working_directory(NULL);
485
	}
486

487
	if (repo_get_oid(the_repository, "HEAD", &oid)) {
488
		FILE *fp;
489

490
		/* assume we are in a fresh repo, but update the sparse-checkout file */
491
		if (safe_create_leading_directories(sparse_filename))
492
			die(_("unable to create leading directories of %s"),
493
			    sparse_filename);
494
		fp = xfopen(sparse_filename, "w");
495
		if (!fp)
496
			die(_("failed to open '%s'"), sparse_filename);
497

498
		free(sparse_filename);
499
		fprintf(fp, "/*\n!/*/\n");
500
		fclose(fp);
501
		return 0;
502
	}
503

504
	free(sparse_filename);
505

506
	add_pattern("/*", empty_base, 0, &pl, 0);
507
	add_pattern("!/*/", empty_base, 0, &pl, 0);
508
	pl.use_cone_patterns = init_opts.cone_mode;
509

510
	return write_patterns_and_update(&pl);
511
}
512

513
static void insert_recursive_pattern(struct pattern_list *pl, struct strbuf *path)
514
{
515
	struct pattern_entry *e = xmalloc(sizeof(*e));
516
	e->patternlen = path->len;
517
	e->pattern = strbuf_detach(path, NULL);
518
	hashmap_entry_init(&e->ent, fspathhash(e->pattern));
519

520
	hashmap_add(&pl->recursive_hashmap, &e->ent);
521

522
	while (e->patternlen) {
523
		char *slash = strrchr(e->pattern, '/');
524
		char *oldpattern = e->pattern;
525
		size_t newlen;
526
		struct pattern_entry *dup;
527

528
		if (!slash || slash == e->pattern)
529
			break;
530

531
		newlen = slash - e->pattern;
532
		e = xmalloc(sizeof(struct pattern_entry));
533
		e->patternlen = newlen;
534
		e->pattern = xstrndup(oldpattern, newlen);
535
		hashmap_entry_init(&e->ent, fspathhash(e->pattern));
536

537
		dup = hashmap_get_entry(&pl->parent_hashmap, e, ent, NULL);
538
		if (!dup) {
539
			hashmap_add(&pl->parent_hashmap, &e->ent);
540
		} else {
541
			free(e->pattern);
542
			free(e);
543
			e = dup;
544
		}
545
	}
546
}
547

548
static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
549
{
550
	strbuf_trim(line);
551

552
	strbuf_trim_trailing_dir_sep(line);
553

554
	if (strbuf_normalize_path(line))
555
		die(_("could not normalize path %s"), line->buf);
556

557
	if (!line->len)
558
		return;
559

560
	if (line->buf[0] != '/')
561
		strbuf_insertstr(line, 0, "/");
562

563
	insert_recursive_pattern(pl, line);
564
}
565

566
static void add_patterns_from_input(struct pattern_list *pl,
567
				    int argc, const char **argv,
568
				    FILE *file)
569
{
570
	int i;
571
	if (core_sparse_checkout_cone) {
572
		struct strbuf line = STRBUF_INIT;
573

574
		hashmap_init(&pl->recursive_hashmap, pl_hashmap_cmp, NULL, 0);
575
		hashmap_init(&pl->parent_hashmap, pl_hashmap_cmp, NULL, 0);
576
		pl->use_cone_patterns = 1;
577

578
		if (file) {
579
			struct strbuf unquoted = STRBUF_INIT;
580
			while (!strbuf_getline(&line, file)) {
581
				if (line.buf[0] == '"') {
582
					strbuf_reset(&unquoted);
583
					if (unquote_c_style(&unquoted, line.buf, NULL))
584
						die(_("unable to unquote C-style string '%s'"),
585
						line.buf);
586

587
					strbuf_swap(&unquoted, &line);
588
				}
589

590
				strbuf_to_cone_pattern(&line, pl);
591
			}
592

593
			strbuf_release(&unquoted);
594
		} else {
595
			for (i = 0; i < argc; i++) {
596
				strbuf_setlen(&line, 0);
597
				strbuf_addstr(&line, argv[i]);
598
				strbuf_to_cone_pattern(&line, pl);
599
			}
600
		}
601
		strbuf_release(&line);
602
	} else {
603
		if (file) {
604
			struct strbuf line = STRBUF_INIT;
605

606
			while (!strbuf_getline(&line, file))
607
				add_pattern(line.buf, empty_base, 0, pl, 0);
608

609
			strbuf_release(&line);
610
		} else {
611
			for (i = 0; i < argc; i++)
612
				add_pattern(argv[i], empty_base, 0, pl, 0);
613
		}
614
	}
615
}
616

617
enum modify_type {
618
	REPLACE,
619
	ADD,
620
};
621

622
static void add_patterns_cone_mode(int argc, const char **argv,
623
				   struct pattern_list *pl,
624
				   int use_stdin)
625
{
626
	struct strbuf buffer = STRBUF_INIT;
627
	struct pattern_entry *pe;
628
	struct hashmap_iter iter;
629
	struct pattern_list existing;
630
	char *sparse_filename = get_sparse_checkout_filename();
631

632
	add_patterns_from_input(pl, argc, argv,
633
				use_stdin ? stdin : NULL);
634

635
	memset(&existing, 0, sizeof(existing));
636
	existing.use_cone_patterns = core_sparse_checkout_cone;
637

638
	if (add_patterns_from_file_to_list(sparse_filename, "", 0,
639
					   &existing, NULL, 0))
640
		die(_("unable to load existing sparse-checkout patterns"));
641
	free(sparse_filename);
642

643
	if (!existing.use_cone_patterns)
644
		die(_("existing sparse-checkout patterns do not use cone mode"));
645

646
	hashmap_for_each_entry(&existing.recursive_hashmap, &iter, pe, ent) {
647
		if (!hashmap_contains_parent(&pl->recursive_hashmap,
648
					pe->pattern, &buffer) ||
649
		    !hashmap_contains_parent(&pl->parent_hashmap,
650
					pe->pattern, &buffer)) {
651
			strbuf_reset(&buffer);
652
			strbuf_addstr(&buffer, pe->pattern);
653
			insert_recursive_pattern(pl, &buffer);
654
		}
655
	}
656

657
	clear_pattern_list(&existing);
658
	strbuf_release(&buffer);
659
}
660

661
static void add_patterns_literal(int argc, const char **argv,
662
				 struct pattern_list *pl,
663
				 int use_stdin)
664
{
665
	char *sparse_filename = get_sparse_checkout_filename();
666
	if (add_patterns_from_file_to_list(sparse_filename, "", 0,
667
					   pl, NULL, 0))
668
		die(_("unable to load existing sparse-checkout patterns"));
669
	free(sparse_filename);
670
	add_patterns_from_input(pl, argc, argv, use_stdin ? stdin : NULL);
671
}
672

673
static int modify_pattern_list(int argc, const char **argv, int use_stdin,
674
			       enum modify_type m)
675
{
676
	int result;
677
	int changed_config = 0;
678
	struct pattern_list *pl = xcalloc(1, sizeof(*pl));
679

680
	switch (m) {
681
	case ADD:
682
		if (core_sparse_checkout_cone)
683
			add_patterns_cone_mode(argc, argv, pl, use_stdin);
684
		else
685
			add_patterns_literal(argc, argv, pl, use_stdin);
686
		break;
687

688
	case REPLACE:
689
		add_patterns_from_input(pl, argc, argv,
690
					use_stdin ? stdin : NULL);
691
		break;
692
	}
693

694
	if (!core_apply_sparse_checkout) {
695
		set_config(MODE_ALL_PATTERNS);
696
		core_apply_sparse_checkout = 1;
697
		changed_config = 1;
698
	}
699

700
	result = write_patterns_and_update(pl);
701

702
	if (result && changed_config)
703
		set_config(MODE_NO_PATTERNS);
704

705
	clear_pattern_list(pl);
706
	free(pl);
707
	return result;
708
}
709

710
static void sanitize_paths(int argc, const char **argv,
711
			   const char *prefix, int skip_checks)
712
{
713
	int i;
714

715
	if (!argc)
716
		return;
717

718
	if (prefix && *prefix && core_sparse_checkout_cone) {
719
		/*
720
		 * The args are not pathspecs, so unfortunately we
721
		 * cannot imitate how cmd_add() uses parse_pathspec().
722
		 */
723
		int prefix_len = strlen(prefix);
724

725
		for (i = 0; i < argc; i++)
726
			argv[i] = prefix_path(prefix, prefix_len, argv[i]);
727
	}
728

729
	if (skip_checks)
730
		return;
731

732
	if (prefix && *prefix && !core_sparse_checkout_cone)
733
		die(_("please run from the toplevel directory in non-cone mode"));
734

735
	if (core_sparse_checkout_cone) {
736
		for (i = 0; i < argc; i++) {
737
			if (argv[i][0] == '/')
738
				die(_("specify directories rather than patterns (no leading slash)"));
739
			if (argv[i][0] == '!')
740
				die(_("specify directories rather than patterns.  If your directory starts with a '!', pass --skip-checks"));
741
			if (strpbrk(argv[i], "*?[]"))
742
				die(_("specify directories rather than patterns.  If your directory really has any of '*?[]\\' in it, pass --skip-checks"));
743
		}
744
	}
745

746
	for (i = 0; i < argc; i++) {
747
		struct cache_entry *ce;
748
		struct index_state *index = the_repository->index;
749
		int pos = index_name_pos(index, argv[i], strlen(argv[i]));
750

751
		if (pos < 0)
752
			continue;
753
		ce = index->cache[pos];
754
		if (S_ISSPARSEDIR(ce->ce_mode))
755
			continue;
756

757
		if (core_sparse_checkout_cone)
758
			die(_("'%s' is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]);
759
		else
760
			warning(_("pass a leading slash before paths such as '%s' if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)."), argv[i]);
761
	}
762
}
763

764
static char const * const builtin_sparse_checkout_add_usage[] = {
765
	N_("git sparse-checkout add [--skip-checks] (--stdin | <patterns>)"),
766
	NULL
767
};
768

769
static struct sparse_checkout_add_opts {
770
	int skip_checks;
771
	int use_stdin;
772
} add_opts;
773

774
static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
775
{
776
	static struct option builtin_sparse_checkout_add_options[] = {
777
		OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
778
			   N_("skip some sanity checks on the given paths that might give false positives"),
779
			   PARSE_OPT_NONEG),
780
		OPT_BOOL(0, "stdin", &add_opts.use_stdin,
781
			 N_("read patterns from standard in")),
782
		OPT_END(),
783
	};
784

785
	setup_work_tree();
786
	if (!core_apply_sparse_checkout)
787
		die(_("no sparse-checkout to add to"));
788

789
	repo_read_index(the_repository);
790

791
	argc = parse_options(argc, argv, prefix,
792
			     builtin_sparse_checkout_add_options,
793
			     builtin_sparse_checkout_add_usage, 0);
794

795
	sanitize_paths(argc, argv, prefix, add_opts.skip_checks);
796

797
	return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD);
798
}
799

800
static char const * const builtin_sparse_checkout_set_usage[] = {
801
	N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | <patterns>)"),
802
	NULL
803
};
804

805
static struct sparse_checkout_set_opts {
806
	int cone_mode;
807
	int sparse_index;
808
	int skip_checks;
809
	int use_stdin;
810
} set_opts;
811

812
static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
813
{
814
	int default_patterns_nr = 2;
815
	const char *default_patterns[] = {"/*", "!/*/", NULL};
816

817
	static struct option builtin_sparse_checkout_set_options[] = {
818
		OPT_BOOL(0, "cone", &set_opts.cone_mode,
819
			 N_("initialize the sparse-checkout in cone mode")),
820
		OPT_BOOL(0, "sparse-index", &set_opts.sparse_index,
821
			 N_("toggle the use of a sparse index")),
822
		OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks,
823
			   N_("skip some sanity checks on the given paths that might give false positives"),
824
			   PARSE_OPT_NONEG),
825
		OPT_BOOL_F(0, "stdin", &set_opts.use_stdin,
826
			   N_("read patterns from standard in"),
827
			   PARSE_OPT_NONEG),
828
		OPT_END(),
829
	};
830

831
	setup_work_tree();
832
	repo_read_index(the_repository);
833

834
	set_opts.cone_mode = -1;
835
	set_opts.sparse_index = -1;
836

837
	argc = parse_options(argc, argv, prefix,
838
			     builtin_sparse_checkout_set_options,
839
			     builtin_sparse_checkout_set_usage, 0);
840

841
	if (update_modes(&set_opts.cone_mode, &set_opts.sparse_index))
842
		return 1;
843

844
	/*
845
	 * Cone mode automatically specifies the toplevel directory.  For
846
	 * non-cone mode, if nothing is specified, manually select just the
847
	 * top-level directory (much as 'init' would do).
848
	 */
849
	if (!core_sparse_checkout_cone && !set_opts.use_stdin && argc == 0) {
850
		argv = default_patterns;
851
		argc = default_patterns_nr;
852
	} else {
853
		sanitize_paths(argc, argv, prefix, set_opts.skip_checks);
854
	}
855

856
	return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE);
857
}
858

859
static char const * const builtin_sparse_checkout_reapply_usage[] = {
860
	"git sparse-checkout reapply [--[no-]cone] [--[no-]sparse-index]",
861
	NULL
862
};
863

864
static struct sparse_checkout_reapply_opts {
865
	int cone_mode;
866
	int sparse_index;
867
} reapply_opts;
868

869
static int sparse_checkout_reapply(int argc, const char **argv,
870
				   const char *prefix)
871
{
872
	static struct option builtin_sparse_checkout_reapply_options[] = {
873
		OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
874
			 N_("initialize the sparse-checkout in cone mode")),
875
		OPT_BOOL(0, "sparse-index", &reapply_opts.sparse_index,
876
			 N_("toggle the use of a sparse index")),
877
		OPT_END(),
878
	};
879

880
	setup_work_tree();
881
	if (!core_apply_sparse_checkout)
882
		die(_("must be in a sparse-checkout to reapply sparsity patterns"));
883

884
	reapply_opts.cone_mode = -1;
885
	reapply_opts.sparse_index = -1;
886

887
	argc = parse_options(argc, argv, prefix,
888
			     builtin_sparse_checkout_reapply_options,
889
			     builtin_sparse_checkout_reapply_usage, 0);
890

891
	repo_read_index(the_repository);
892

893
	if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index))
894
		return 1;
895

896
	return update_working_directory(NULL);
897
}
898

899
static char const * const builtin_sparse_checkout_disable_usage[] = {
900
	"git sparse-checkout disable",
901
	NULL
902
};
903

904
static int sparse_checkout_disable(int argc, const char **argv,
905
				   const char *prefix)
906
{
907
	static struct option builtin_sparse_checkout_disable_options[] = {
908
		OPT_END(),
909
	};
910
	struct pattern_list pl;
911

912
	/*
913
	 * We do not exit early if !core_apply_sparse_checkout; due to the
914
	 * ability for users to manually muck things up between
915
	 *   direct editing of .git/info/sparse-checkout
916
	 *   running read-tree -m u HEAD or update-index --skip-worktree
917
	 *   direct toggling of config options
918
	 * users might end up with an index with SKIP_WORKTREE bit set on
919
	 * some files and not know how to undo it.  So, here we just
920
	 * forcibly return to a dense checkout regardless of initial state.
921
	 */
922

923
	setup_work_tree();
924
	argc = parse_options(argc, argv, prefix,
925
			     builtin_sparse_checkout_disable_options,
926
			     builtin_sparse_checkout_disable_usage, 0);
927

928
	repo_read_index(the_repository);
929

930
	memset(&pl, 0, sizeof(pl));
931
	hashmap_init(&pl.recursive_hashmap, pl_hashmap_cmp, NULL, 0);
932
	hashmap_init(&pl.parent_hashmap, pl_hashmap_cmp, NULL, 0);
933
	pl.use_cone_patterns = 0;
934
	core_apply_sparse_checkout = 1;
935

936
	add_pattern("/*", empty_base, 0, &pl, 0);
937

938
	prepare_repo_settings(the_repository);
939
	the_repository->settings.sparse_index = 0;
940

941
	if (update_working_directory(&pl))
942
		die(_("error while refreshing working directory"));
943

944
	clear_pattern_list(&pl);
945
	return set_config(MODE_NO_PATTERNS);
946
}
947

948
static char const * const builtin_sparse_checkout_check_rules_usage[] = {
949
	N_("git sparse-checkout check-rules [-z] [--skip-checks]"
950
	   "[--[no-]cone] [--rules-file <file>]"),
951
	NULL
952
};
953

954
static struct sparse_checkout_check_rules_opts {
955
	int cone_mode;
956
	int null_termination;
957
	char *rules_file;
958
} check_rules_opts;
959

960
static int check_rules(struct pattern_list *pl, int null_terminated) {
961
	struct strbuf line = STRBUF_INIT;
962
	struct strbuf unquoted = STRBUF_INIT;
963
	char *path;
964
	int line_terminator = null_terminated ? 0 : '\n';
965
	strbuf_getline_fn getline_fn = null_terminated ? strbuf_getline_nul
966
		: strbuf_getline;
967
	the_repository->index->sparse_checkout_patterns = pl;
968
	while (!getline_fn(&line, stdin)) {
969
		path = line.buf;
970
		if (!null_terminated && line.buf[0] == '"') {
971
			strbuf_reset(&unquoted);
972
			if (unquote_c_style(&unquoted, line.buf, NULL))
973
				die(_("unable to unquote C-style string '%s'"),
974
					line.buf);
975

976
			path = unquoted.buf;
977
		}
978

979
		if (path_in_sparse_checkout(path, the_repository->index))
980
			write_name_quoted(path, stdout, line_terminator);
981
	}
982
	strbuf_release(&line);
983
	strbuf_release(&unquoted);
984

985
	return 0;
986
}
987

988
static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
989
{
990
	static struct option builtin_sparse_checkout_check_rules_options[] = {
991
		OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
992
			 N_("terminate input and output files by a NUL character")),
993
		OPT_BOOL(0, "cone", &check_rules_opts.cone_mode,
994
			 N_("when used with --rules-file interpret patterns as cone mode patterns")),
995
		OPT_FILENAME(0, "rules-file", &check_rules_opts.rules_file,
996
			 N_("use patterns in <file> instead of the current ones.")),
997
		OPT_END(),
998
	};
999

1000
	FILE *fp;
1001
	int ret;
1002
	struct pattern_list pl = {0};
1003
	char *sparse_filename;
1004
	check_rules_opts.cone_mode = -1;
1005

1006
	argc = parse_options(argc, argv, prefix,
1007
			     builtin_sparse_checkout_check_rules_options,
1008
			     builtin_sparse_checkout_check_rules_usage, 0);
1009

1010
	if (check_rules_opts.rules_file && check_rules_opts.cone_mode < 0)
1011
		check_rules_opts.cone_mode = 1;
1012

1013
	update_cone_mode(&check_rules_opts.cone_mode);
1014
	pl.use_cone_patterns = core_sparse_checkout_cone;
1015
	if (check_rules_opts.rules_file) {
1016
		fp = xfopen(check_rules_opts.rules_file, "r");
1017
		add_patterns_from_input(&pl, argc, argv, fp);
1018
		fclose(fp);
1019
	} else {
1020
		sparse_filename = get_sparse_checkout_filename();
1021
		if (add_patterns_from_file_to_list(sparse_filename, "", 0, &pl,
1022
						   NULL, 0))
1023
			die(_("unable to load existing sparse-checkout patterns"));
1024
		free(sparse_filename);
1025
	}
1026

1027
	ret = check_rules(&pl, check_rules_opts.null_termination);
1028
	clear_pattern_list(&pl);
1029
	free(check_rules_opts.rules_file);
1030
	return ret;
1031
}
1032

1033
int cmd_sparse_checkout(int argc, const char **argv, const char *prefix)
1034
{
1035
	parse_opt_subcommand_fn *fn = NULL;
1036
	struct option builtin_sparse_checkout_options[] = {
1037
		OPT_SUBCOMMAND("list", &fn, sparse_checkout_list),
1038
		OPT_SUBCOMMAND("init", &fn, sparse_checkout_init),
1039
		OPT_SUBCOMMAND("set", &fn, sparse_checkout_set),
1040
		OPT_SUBCOMMAND("add", &fn, sparse_checkout_add),
1041
		OPT_SUBCOMMAND("reapply", &fn, sparse_checkout_reapply),
1042
		OPT_SUBCOMMAND("disable", &fn, sparse_checkout_disable),
1043
		OPT_SUBCOMMAND("check-rules", &fn, sparse_checkout_check_rules),
1044
		OPT_END(),
1045
	};
1046

1047
	argc = parse_options(argc, argv, prefix,
1048
			     builtin_sparse_checkout_options,
1049
			     builtin_sparse_checkout_usage, 0);
1050

1051
	git_config(git_default_config, NULL);
1052

1053
	prepare_repo_settings(the_repository);
1054
	the_repository->settings.command_requires_full_index = 0;
1055

1056
	return fn(argc, argv, prefix);
1057
}
1058

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.