git

Форк
0
/
submodule--helper.c 
3558 строк · 98.2 Кб
1
#include "builtin.h"
2
#include "abspath.h"
3
#include "environment.h"
4
#include "gettext.h"
5
#include "hex.h"
6
#include "repository.h"
7
#include "config.h"
8
#include "parse-options.h"
9
#include "quote.h"
10
#include "path.h"
11
#include "pathspec.h"
12
#include "preload-index.h"
13
#include "dir.h"
14
#include "read-cache.h"
15
#include "setup.h"
16
#include "sparse-index.h"
17
#include "submodule.h"
18
#include "submodule-config.h"
19
#include "string-list.h"
20
#include "run-command.h"
21
#include "remote.h"
22
#include "refs.h"
23
#include "refspec.h"
24
#include "revision.h"
25
#include "diffcore.h"
26
#include "diff.h"
27
#include "object-file.h"
28
#include "object-name.h"
29
#include "object-store-ll.h"
30
#include "advice.h"
31
#include "branch.h"
32
#include "list-objects-filter-options.h"
33

34
#define OPT_QUIET (1 << 0)
35
#define OPT_CACHED (1 << 1)
36
#define OPT_RECURSIVE (1 << 2)
37
#define OPT_FORCE (1 << 3)
38

39
typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
40
				  void *cb_data);
41

42
static int repo_get_default_remote(struct repository *repo, char **default_remote)
43
{
44
	char *dest = NULL;
45
	struct strbuf sb = STRBUF_INIT;
46
	struct ref_store *store = get_main_ref_store(repo);
47
	const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
48
						      NULL);
49

50
	if (!refname)
51
		return die_message(_("No such ref: %s"), "HEAD");
52

53
	/* detached HEAD */
54
	if (!strcmp(refname, "HEAD")) {
55
		*default_remote = xstrdup("origin");
56
		return 0;
57
	}
58

59
	if (!skip_prefix(refname, "refs/heads/", &refname))
60
		return die_message(_("Expecting a full ref name, got %s"),
61
				   refname);
62

63
	strbuf_addf(&sb, "branch.%s.remote", refname);
64
	if (repo_config_get_string(repo, sb.buf, &dest))
65
		*default_remote = xstrdup("origin");
66
	else
67
		*default_remote = dest;
68

69
	strbuf_release(&sb);
70
	return 0;
71
}
72

73
static int get_default_remote_submodule(const char *module_path, char **default_remote)
74
{
75
	struct repository subrepo;
76
	int ret;
77

78
	if (repo_submodule_init(&subrepo, the_repository, module_path,
79
				null_oid()) < 0)
80
		return die_message(_("could not get a repository handle for submodule '%s'"),
81
				   module_path);
82
	ret = repo_get_default_remote(&subrepo, default_remote);
83
	repo_clear(&subrepo);
84

85
	return ret;
86
}
87

88
static char *get_default_remote(void)
89
{
90
	char *default_remote;
91
	int code = repo_get_default_remote(the_repository, &default_remote);
92

93
	if (code)
94
		exit(code);
95

96
	return default_remote;
97
}
98

99
static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
100
{
101
	char *remoteurl, *resolved_url;
102
	char *remote = get_default_remote();
103
	struct strbuf remotesb = STRBUF_INIT;
104

105
	strbuf_addf(&remotesb, "remote.%s.url", remote);
106
	if (git_config_get_string(remotesb.buf, &remoteurl)) {
107
		if (!quiet)
108
			warning(_("could not look up configuration '%s'. "
109
				  "Assuming this repository is its own "
110
				  "authoritative upstream."),
111
				remotesb.buf);
112
		remoteurl = xgetcwd();
113
	}
114
	resolved_url = relative_url(remoteurl, rel_url, up_path);
115

116
	free(remote);
117
	free(remoteurl);
118
	strbuf_release(&remotesb);
119

120
	return resolved_url;
121
}
122

123
/* the result should be freed by the caller. */
124
static char *get_submodule_displaypath(const char *path, const char *prefix,
125
				       const char *super_prefix)
126
{
127
	if (prefix && super_prefix) {
128
		BUG("cannot have prefix '%s' and superprefix '%s'",
129
		    prefix, super_prefix);
130
	} else if (prefix) {
131
		struct strbuf sb = STRBUF_INIT;
132
		char *displaypath = xstrdup(relative_path(path, prefix, &sb));
133
		strbuf_release(&sb);
134
		return displaypath;
135
	} else if (super_prefix) {
136
		return xstrfmt("%s%s", super_prefix, path);
137
	} else {
138
		return xstrdup(path);
139
	}
140
}
141

142
static char *compute_rev_name(const char *sub_path, const char* object_id)
143
{
144
	struct strbuf sb = STRBUF_INIT;
145
	const char ***d;
146

147
	static const char *describe_bare[] = { NULL };
148

149
	static const char *describe_tags[] = { "--tags", NULL };
150

151
	static const char *describe_contains[] = { "--contains", NULL };
152

153
	static const char *describe_all_always[] = { "--all", "--always", NULL };
154

155
	static const char **describe_argv[] = { describe_bare, describe_tags,
156
						describe_contains,
157
						describe_all_always, NULL };
158

159
	for (d = describe_argv; *d; d++) {
160
		struct child_process cp = CHILD_PROCESS_INIT;
161
		prepare_submodule_repo_env(&cp.env);
162
		cp.dir = sub_path;
163
		cp.git_cmd = 1;
164
		cp.no_stderr = 1;
165

166
		strvec_push(&cp.args, "describe");
167
		strvec_pushv(&cp.args, *d);
168
		strvec_push(&cp.args, object_id);
169

170
		if (!capture_command(&cp, &sb, 0)) {
171
			strbuf_strip_suffix(&sb, "\n");
172
			return strbuf_detach(&sb, NULL);
173
		}
174
	}
175

176
	strbuf_release(&sb);
177
	return NULL;
178
}
179

180
struct module_list {
181
	const struct cache_entry **entries;
182
	int alloc, nr;
183
};
184
#define MODULE_LIST_INIT { 0 }
185

186
static void module_list_release(struct module_list *ml)
187
{
188
	free(ml->entries);
189
}
190

191
static int module_list_compute(const char **argv,
192
			       const char *prefix,
193
			       struct pathspec *pathspec,
194
			       struct module_list *list)
195
{
196
	int i, result = 0;
197
	char *ps_matched = NULL;
198

199
	parse_pathspec(pathspec, 0,
200
		       PATHSPEC_PREFER_FULL,
201
		       prefix, argv);
202

203
	if (pathspec->nr)
204
		ps_matched = xcalloc(pathspec->nr, 1);
205

206
	if (repo_read_index(the_repository) < 0)
207
		die(_("index file corrupt"));
208

209
	for (i = 0; i < the_repository->index->cache_nr; i++) {
210
		const struct cache_entry *ce = the_repository->index->cache[i];
211

212
		if (!match_pathspec(the_repository->index, pathspec, ce->name, ce_namelen(ce),
213
				    0, ps_matched, 1) ||
214
		    !S_ISGITLINK(ce->ce_mode))
215
			continue;
216

217
		ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
218
		list->entries[list->nr++] = ce;
219
		while (i + 1 < the_repository->index->cache_nr &&
220
		       !strcmp(ce->name, the_repository->index->cache[i + 1]->name))
221
			/*
222
			 * Skip entries with the same name in different stages
223
			 * to make sure an entry is returned only once.
224
			 */
225
			i++;
226
	}
227

228
	if (ps_matched && report_path_error(ps_matched, pathspec))
229
		result = -1;
230

231
	free(ps_matched);
232

233
	return result;
234
}
235

236
static void module_list_active(struct module_list *list)
237
{
238
	int i;
239
	struct module_list active_modules = MODULE_LIST_INIT;
240

241
	for (i = 0; i < list->nr; i++) {
242
		const struct cache_entry *ce = list->entries[i];
243

244
		if (!is_submodule_active(the_repository, ce->name))
245
			continue;
246

247
		ALLOC_GROW(active_modules.entries,
248
			   active_modules.nr + 1,
249
			   active_modules.alloc);
250
		active_modules.entries[active_modules.nr++] = ce;
251
	}
252

253
	module_list_release(list);
254
	*list = active_modules;
255
}
256

257
static char *get_up_path(const char *path)
258
{
259
	struct strbuf sb = STRBUF_INIT;
260

261
	strbuf_addstrings(&sb, "../", count_slashes(path));
262

263
	/*
264
	 * Check if 'path' ends with slash or not
265
	 * for having the same output for dir/sub_dir
266
	 * and dir/sub_dir/
267
	 */
268
	if (!is_dir_sep(path[strlen(path) - 1]))
269
		strbuf_addstr(&sb, "../");
270

271
	return strbuf_detach(&sb, NULL);
272
}
273

274
static void for_each_listed_submodule(const struct module_list *list,
275
				      each_submodule_fn fn, void *cb_data)
276
{
277
	int i;
278

279
	for (i = 0; i < list->nr; i++)
280
		fn(list->entries[i], cb_data);
281
}
282

283
struct foreach_cb {
284
	int argc;
285
	const char **argv;
286
	const char *prefix;
287
	const char *super_prefix;
288
	int quiet;
289
	int recursive;
290
};
291
#define FOREACH_CB_INIT { 0 }
292

293
static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
294
				       void *cb_data)
295
{
296
	struct foreach_cb *info = cb_data;
297
	const char *path = list_item->name;
298
	const struct object_id *ce_oid = &list_item->oid;
299
	const struct submodule *sub;
300
	struct child_process cp = CHILD_PROCESS_INIT;
301
	char *displaypath;
302

303
	if (validate_submodule_path(path) < 0)
304
		exit(128);
305

306
	displaypath = get_submodule_displaypath(path, info->prefix,
307
						info->super_prefix);
308

309
	sub = submodule_from_path(the_repository, null_oid(), path);
310

311
	if (!sub)
312
		die(_("No url found for submodule path '%s' in .gitmodules"),
313
			displaypath);
314

315
	if (!is_submodule_populated_gently(path, NULL))
316
		goto cleanup;
317

318
	prepare_submodule_repo_env(&cp.env);
319

320
	/*
321
	 * For the purpose of executing <command> in the submodule,
322
	 * separate shell is used for the purpose of running the
323
	 * child process.
324
	 */
325
	cp.use_shell = 1;
326
	cp.dir = path;
327

328
	/*
329
	 * NEEDSWORK: the command currently has access to the variables $name,
330
	 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
331
	 * contains a single argument. This is done for maintaining a faithful
332
	 * translation from shell script.
333
	 */
334
	if (info->argc == 1) {
335
		char *toplevel = xgetcwd();
336
		struct strbuf sb = STRBUF_INIT;
337

338
		strvec_pushf(&cp.env, "name=%s", sub->name);
339
		strvec_pushf(&cp.env, "sm_path=%s", path);
340
		strvec_pushf(&cp.env, "displaypath=%s", displaypath);
341
		strvec_pushf(&cp.env, "sha1=%s",
342
			     oid_to_hex(ce_oid));
343
		strvec_pushf(&cp.env, "toplevel=%s", toplevel);
344

345
		/*
346
		 * Since the path variable was accessible from the script
347
		 * before porting, it is also made available after porting.
348
		 * The environment variable "PATH" has a very special purpose
349
		 * on windows. And since environment variables are
350
		 * case-insensitive in windows, it interferes with the
351
		 * existing PATH variable. Hence, to avoid that, we expose
352
		 * path via the args strvec and not via env.
353
		 */
354
		sq_quote_buf(&sb, path);
355
		strvec_pushf(&cp.args, "path=%s; %s",
356
			     sb.buf, info->argv[0]);
357
		strbuf_release(&sb);
358
		free(toplevel);
359
	} else {
360
		strvec_pushv(&cp.args, info->argv);
361
	}
362

363
	if (!info->quiet)
364
		printf(_("Entering '%s'\n"), displaypath);
365

366
	if (info->argv[0] && run_command(&cp))
367
		die(_("run_command returned non-zero status for %s\n."),
368
			displaypath);
369

370
	if (info->recursive) {
371
		struct child_process cpr = CHILD_PROCESS_INIT;
372

373
		cpr.git_cmd = 1;
374
		cpr.dir = path;
375
		prepare_submodule_repo_env(&cpr.env);
376

377
		strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
378
			     NULL);
379
		strvec_pushf(&cpr.args, "--super-prefix=%s/", displaypath);
380

381
		if (info->quiet)
382
			strvec_push(&cpr.args, "--quiet");
383

384
		strvec_push(&cpr.args, "--");
385
		strvec_pushv(&cpr.args, info->argv);
386

387
		if (run_command(&cpr))
388
			die(_("run_command returned non-zero status while "
389
				"recursing in the nested submodules of %s\n."),
390
				displaypath);
391
	}
392

393
cleanup:
394
	free(displaypath);
395
}
396

397
static int module_foreach(int argc, const char **argv, const char *prefix)
398
{
399
	struct foreach_cb info = FOREACH_CB_INIT;
400
	struct pathspec pathspec = { 0 };
401
	struct module_list list = MODULE_LIST_INIT;
402
	struct option module_foreach_options[] = {
403
		OPT__SUPER_PREFIX(&info.super_prefix),
404
		OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
405
		OPT_BOOL(0, "recursive", &info.recursive,
406
			 N_("recurse into nested submodules")),
407
		OPT_END()
408
	};
409
	const char *const git_submodule_helper_usage[] = {
410
		N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
411
		NULL
412
	};
413
	int ret = 1;
414

415
	argc = parse_options(argc, argv, prefix, module_foreach_options,
416
			     git_submodule_helper_usage, 0);
417

418
	if (module_list_compute(NULL, prefix, &pathspec, &list) < 0)
419
		goto cleanup;
420

421
	info.argc = argc;
422
	info.argv = argv;
423
	info.prefix = prefix;
424

425
	for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
426

427
	ret = 0;
428
cleanup:
429
	module_list_release(&list);
430
	clear_pathspec(&pathspec);
431
	return ret;
432
}
433

434
static int starts_with_dot_slash(const char *const path)
435
{
436
	return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
437
				PATH_MATCH_XPLATFORM);
438
}
439

440
static int starts_with_dot_dot_slash(const char *const path)
441
{
442
	return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
443
				PATH_MATCH_XPLATFORM);
444
}
445

446
struct init_cb {
447
	const char *prefix;
448
	const char *super_prefix;
449
	unsigned int flags;
450
};
451
#define INIT_CB_INIT { 0 }
452

453
static void init_submodule(const char *path, const char *prefix,
454
			   const char *super_prefix,
455
			   unsigned int flags)
456
{
457
	const struct submodule *sub;
458
	struct strbuf sb = STRBUF_INIT;
459
	const char *upd;
460
	char *url = NULL, *displaypath;
461

462
	displaypath = get_submodule_displaypath(path, prefix, super_prefix);
463

464
	sub = submodule_from_path(the_repository, null_oid(), path);
465

466
	if (!sub)
467
		die(_("No url found for submodule path '%s' in .gitmodules"),
468
			displaypath);
469

470
	/*
471
	 * NEEDSWORK: In a multi-working-tree world, this needs to be
472
	 * set in the per-worktree config.
473
	 *
474
	 * Set active flag for the submodule being initialized
475
	 */
476
	if (!is_submodule_active(the_repository, path)) {
477
		strbuf_addf(&sb, "submodule.%s.active", sub->name);
478
		git_config_set_gently(sb.buf, "true");
479
		strbuf_reset(&sb);
480
	}
481

482
	/*
483
	 * Copy url setting when it is not set yet.
484
	 * To look up the url in .git/config, we must not fall back to
485
	 * .gitmodules, so look it up directly.
486
	 */
487
	strbuf_addf(&sb, "submodule.%s.url", sub->name);
488
	if (git_config_get_string(sb.buf, &url)) {
489
		if (!sub->url)
490
			die(_("No url found for submodule path '%s' in .gitmodules"),
491
				displaypath);
492

493
		url = xstrdup(sub->url);
494

495
		/* Possibly a url relative to parent */
496
		if (starts_with_dot_dot_slash(url) ||
497
		    starts_with_dot_slash(url)) {
498
			char *oldurl = url;
499

500
			url = resolve_relative_url(oldurl, NULL, 0);
501
			free(oldurl);
502
		}
503

504
		if (git_config_set_gently(sb.buf, url))
505
			die(_("Failed to register url for submodule path '%s'"),
506
			    displaypath);
507
		if (!(flags & OPT_QUIET))
508
			fprintf(stderr,
509
				_("Submodule '%s' (%s) registered for path '%s'\n"),
510
				sub->name, url, displaypath);
511
	}
512
	strbuf_reset(&sb);
513

514
	/* Copy "update" setting when it is not set yet */
515
	strbuf_addf(&sb, "submodule.%s.update", sub->name);
516
	if (git_config_get_string_tmp(sb.buf, &upd) &&
517
	    sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
518
		if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
519
			fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
520
				sub->name);
521
			upd = "none";
522
		} else {
523
			upd = submodule_update_type_to_string(sub->update_strategy.type);
524
		}
525

526
		if (git_config_set_gently(sb.buf, upd))
527
			die(_("Failed to register update mode for submodule path '%s'"), displaypath);
528
	}
529
	strbuf_release(&sb);
530
	free(displaypath);
531
	free(url);
532
}
533

534
static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
535
{
536
	struct init_cb *info = cb_data;
537

538
	init_submodule(list_item->name, info->prefix, info->super_prefix,
539
		       info->flags);
540
}
541

542
static int module_init(int argc, const char **argv, const char *prefix)
543
{
544
	struct init_cb info = INIT_CB_INIT;
545
	struct pathspec pathspec = { 0 };
546
	struct module_list list = MODULE_LIST_INIT;
547
	int quiet = 0;
548
	struct option module_init_options[] = {
549
		OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
550
		OPT_END()
551
	};
552
	const char *const git_submodule_helper_usage[] = {
553
		N_("git submodule init [<options>] [<path>]"),
554
		NULL
555
	};
556
	int ret = 1;
557

558
	argc = parse_options(argc, argv, prefix, module_init_options,
559
			     git_submodule_helper_usage, 0);
560

561
	if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
562
		goto cleanup;
563

564
	/*
565
	 * If there are no path args and submodule.active is set then,
566
	 * by default, only initialize 'active' modules.
567
	 */
568
	if (!argc && !git_config_get("submodule.active"))
569
		module_list_active(&list);
570

571
	info.prefix = prefix;
572
	if (quiet)
573
		info.flags |= OPT_QUIET;
574

575
	for_each_listed_submodule(&list, init_submodule_cb, &info);
576

577
	ret = 0;
578
cleanup:
579
	module_list_release(&list);
580
	clear_pathspec(&pathspec);
581
	return ret;
582
}
583

584
struct status_cb {
585
	const char *prefix;
586
	const char *super_prefix;
587
	unsigned int flags;
588
};
589
#define STATUS_CB_INIT { 0 }
590

591
static void print_status(unsigned int flags, char state, const char *path,
592
			 const struct object_id *oid, const char *displaypath)
593
{
594
	if (flags & OPT_QUIET)
595
		return;
596

597
	printf("%c%s %s", state, oid_to_hex(oid), displaypath);
598

599
	if (state == ' ' || state == '+') {
600
		char *name = compute_rev_name(path, oid_to_hex(oid));
601

602
		if (name)
603
			printf(" (%s)", name);
604
		free(name);
605
	}
606

607
	printf("\n");
608
}
609

610
static int handle_submodule_head_ref(const char *refname UNUSED,
611
				     const char *referent UNUSED,
612
				     const struct object_id *oid,
613
				     int flags UNUSED,
614
				     void *cb_data)
615
{
616
	struct object_id *output = cb_data;
617

618
	if (oid)
619
		oidcpy(output, oid);
620

621
	return 0;
622
}
623

624
static void status_submodule(const char *path, const struct object_id *ce_oid,
625
			     unsigned int ce_flags, const char *prefix,
626
			     const char *super_prefix, unsigned int flags)
627
{
628
	char *displaypath;
629
	struct strvec diff_files_args = STRVEC_INIT;
630
	struct rev_info rev = REV_INFO_INIT;
631
	struct strbuf buf = STRBUF_INIT;
632
	const char *git_dir;
633
	struct setup_revision_opt opt = {
634
		.free_removed_argv_elements = 1,
635
	};
636

637
	if (validate_submodule_path(path) < 0)
638
		exit(128);
639

640
	if (!submodule_from_path(the_repository, null_oid(), path))
641
		die(_("no submodule mapping found in .gitmodules for path '%s'"),
642
		      path);
643

644
	displaypath = get_submodule_displaypath(path, prefix, super_prefix);
645

646
	if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
647
		print_status(flags, 'U', path, null_oid(), displaypath);
648
		goto cleanup;
649
	}
650

651
	strbuf_addf(&buf, "%s/.git", path);
652
	git_dir = read_gitfile(buf.buf);
653
	if (!git_dir)
654
		git_dir = buf.buf;
655

656
	if (!is_submodule_active(the_repository, path) ||
657
	    !is_git_directory(git_dir)) {
658
		print_status(flags, '-', path, ce_oid, displaypath);
659
		strbuf_release(&buf);
660
		goto cleanup;
661
	}
662
	strbuf_release(&buf);
663

664
	strvec_pushl(&diff_files_args, "diff-files",
665
		     "--ignore-submodules=dirty", "--quiet", "--",
666
		     path, NULL);
667

668
	git_config(git_diff_basic_config, NULL);
669

670
	repo_init_revisions(the_repository, &rev, NULL);
671
	rev.abbrev = 0;
672
	setup_revisions(diff_files_args.nr, diff_files_args.v, &rev, &opt);
673
	run_diff_files(&rev, 0);
674

675
	if (!diff_result_code(&rev.diffopt)) {
676
		print_status(flags, ' ', path, ce_oid,
677
			     displaypath);
678
	} else if (!(flags & OPT_CACHED)) {
679
		struct object_id oid;
680
		struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
681
								      path);
682

683
		if (!refs) {
684
			print_status(flags, '-', path, ce_oid, displaypath);
685
			goto cleanup;
686
		}
687
		if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
688
			die(_("could not resolve HEAD ref inside the "
689
			      "submodule '%s'"), path);
690

691
		print_status(flags, '+', path, &oid, displaypath);
692
	} else {
693
		print_status(flags, '+', path, ce_oid, displaypath);
694
	}
695

696
	if (flags & OPT_RECURSIVE) {
697
		struct child_process cpr = CHILD_PROCESS_INIT;
698

699
		cpr.git_cmd = 1;
700
		cpr.dir = path;
701
		prepare_submodule_repo_env(&cpr.env);
702

703
		strvec_pushl(&cpr.args, "submodule--helper", "status",
704
			     "--recursive", NULL);
705
		strvec_pushf(&cpr.args, "--super-prefix=%s/", displaypath);
706

707
		if (flags & OPT_CACHED)
708
			strvec_push(&cpr.args, "--cached");
709

710
		if (flags & OPT_QUIET)
711
			strvec_push(&cpr.args, "--quiet");
712

713
		if (run_command(&cpr))
714
			die(_("failed to recurse into submodule '%s'"), path);
715
	}
716

717
cleanup:
718
	strvec_clear(&diff_files_args);
719
	free(displaypath);
720
	release_revisions(&rev);
721
}
722

723
static void status_submodule_cb(const struct cache_entry *list_item,
724
				void *cb_data)
725
{
726
	struct status_cb *info = cb_data;
727

728
	status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
729
			 info->prefix, info->super_prefix, info->flags);
730
}
731

732
static int module_status(int argc, const char **argv, const char *prefix)
733
{
734
	struct status_cb info = STATUS_CB_INIT;
735
	struct pathspec pathspec = { 0 };
736
	struct module_list list = MODULE_LIST_INIT;
737
	int quiet = 0;
738
	struct option module_status_options[] = {
739
		OPT__SUPER_PREFIX(&info.super_prefix),
740
		OPT__QUIET(&quiet, N_("suppress submodule status output")),
741
		OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
742
		OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
743
		OPT_END()
744
	};
745
	const char *const git_submodule_helper_usage[] = {
746
		N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
747
		NULL
748
	};
749
	int ret = 1;
750

751
	argc = parse_options(argc, argv, prefix, module_status_options,
752
			     git_submodule_helper_usage, 0);
753

754
	if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
755
		goto cleanup;
756

757
	info.prefix = prefix;
758
	if (quiet)
759
		info.flags |= OPT_QUIET;
760

761
	for_each_listed_submodule(&list, status_submodule_cb, &info);
762

763
	ret = 0;
764
cleanup:
765
	module_list_release(&list);
766
	clear_pathspec(&pathspec);
767
	return ret;
768
}
769

770
struct module_cb {
771
	unsigned int mod_src;
772
	unsigned int mod_dst;
773
	struct object_id oid_src;
774
	struct object_id oid_dst;
775
	char status;
776
	char *sm_path;
777
};
778
#define MODULE_CB_INIT { 0 }
779

780
static void module_cb_release(struct module_cb *mcb)
781
{
782
	free(mcb->sm_path);
783
}
784

785
struct module_cb_list {
786
	struct module_cb **entries;
787
	int alloc, nr;
788
};
789
#define MODULE_CB_LIST_INIT { 0 }
790

791
static void module_cb_list_release(struct module_cb_list *mcbl)
792
{
793
	int i;
794

795
	for (i = 0; i < mcbl->nr; i++) {
796
		struct module_cb *mcb = mcbl->entries[i];
797

798
		module_cb_release(mcb);
799
		free(mcb);
800
	}
801
	free(mcbl->entries);
802
}
803

804
struct summary_cb {
805
	int argc;
806
	const char **argv;
807
	const char *prefix;
808
	const char *super_prefix;
809
	unsigned int cached: 1;
810
	unsigned int for_status: 1;
811
	unsigned int files: 1;
812
	int summary_limit;
813
};
814
#define SUMMARY_CB_INIT { 0 }
815

816
enum diff_cmd {
817
	DIFF_INDEX,
818
	DIFF_FILES
819
};
820

821
static char *verify_submodule_committish(const char *sm_path,
822
					 const char *committish)
823
{
824
	struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
825
	struct strbuf result = STRBUF_INIT;
826

827
	cp_rev_parse.git_cmd = 1;
828
	cp_rev_parse.dir = sm_path;
829
	prepare_submodule_repo_env(&cp_rev_parse.env);
830
	strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
831
	strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
832
	strvec_push(&cp_rev_parse.args, "--");
833

834
	if (capture_command(&cp_rev_parse, &result, 0))
835
		return NULL;
836

837
	strbuf_trim_trailing_newline(&result);
838
	return strbuf_detach(&result, NULL);
839
}
840

841
static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
842
				    int total_commits, const char *displaypath,
843
				    const char *src_abbrev, const char *dst_abbrev,
844
				    struct module_cb *p)
845
{
846
	if (p->status == 'T') {
847
		if (S_ISGITLINK(p->mod_dst))
848
			printf(_("* %s %s(blob)->%s(submodule)"),
849
				 displaypath, src_abbrev, dst_abbrev);
850
		else
851
			printf(_("* %s %s(submodule)->%s(blob)"),
852
				 displaypath, src_abbrev, dst_abbrev);
853
	} else {
854
		printf("* %s %s...%s",
855
			displaypath, src_abbrev, dst_abbrev);
856
	}
857

858
	if (total_commits < 0)
859
		printf(":\n");
860
	else
861
		printf(" (%d):\n", total_commits);
862

863
	if (errmsg) {
864
		printf(_("%s"), errmsg);
865
	} else if (total_commits > 0) {
866
		struct child_process cp_log = CHILD_PROCESS_INIT;
867

868
		cp_log.git_cmd = 1;
869
		cp_log.dir = p->sm_path;
870
		prepare_submodule_repo_env(&cp_log.env);
871
		strvec_pushl(&cp_log.args, "log", NULL);
872

873
		if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
874
			if (info->summary_limit > 0)
875
				strvec_pushf(&cp_log.args, "-%d",
876
					     info->summary_limit);
877

878
			strvec_pushl(&cp_log.args, "--pretty=  %m %s",
879
				     "--first-parent", NULL);
880
			strvec_pushf(&cp_log.args, "%s...%s",
881
				     src_abbrev, dst_abbrev);
882
		} else if (S_ISGITLINK(p->mod_dst)) {
883
			strvec_pushl(&cp_log.args, "--pretty=  > %s",
884
				     "-1", dst_abbrev, NULL);
885
		} else {
886
			strvec_pushl(&cp_log.args, "--pretty=  < %s",
887
				     "-1", src_abbrev, NULL);
888
		}
889
		run_command(&cp_log);
890
	}
891
	printf("\n");
892
}
893

894
static void generate_submodule_summary(struct summary_cb *info,
895
				       struct module_cb *p)
896
{
897
	char *displaypath, *src_abbrev = NULL, *dst_abbrev;
898
	int missing_src = 0, missing_dst = 0;
899
	struct strbuf errmsg = STRBUF_INIT;
900
	int total_commits = -1;
901

902
	if (!info->cached && oideq(&p->oid_dst, null_oid())) {
903
		if (S_ISGITLINK(p->mod_dst)) {
904
			struct ref_store *refs = repo_get_submodule_ref_store(the_repository,
905
									      p->sm_path);
906

907
			if (refs)
908
				refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
909
		} else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
910
			struct stat st;
911
			int fd = open(p->sm_path, O_RDONLY);
912

913
			if (fd < 0 || fstat(fd, &st) < 0 ||
914
			    index_fd(the_repository->index, &p->oid_dst, fd, &st, OBJ_BLOB,
915
				     p->sm_path, 0))
916
				error(_("couldn't hash object from '%s'"), p->sm_path);
917
		} else {
918
			/* for a submodule removal (mode:0000000), don't warn */
919
			if (p->mod_dst)
920
				warning(_("unexpected mode %o\n"), p->mod_dst);
921
		}
922
	}
923

924
	if (S_ISGITLINK(p->mod_src)) {
925
		if (p->status != 'D')
926
			src_abbrev = verify_submodule_committish(p->sm_path,
927
								 oid_to_hex(&p->oid_src));
928
		if (!src_abbrev) {
929
			missing_src = 1;
930
			/*
931
			 * As `rev-parse` failed, we fallback to getting
932
			 * the abbreviated hash using oid_src. We do
933
			 * this as we might still need the abbreviated
934
			 * hash in cases like a submodule type change, etc.
935
			 */
936
			src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
937
		}
938
	} else {
939
		/*
940
		 * The source does not point to a submodule.
941
		 * So, we fallback to getting the abbreviation using
942
		 * oid_src as we might still need the abbreviated
943
		 * hash in cases like submodule add, etc.
944
		 */
945
		src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
946
	}
947

948
	if (S_ISGITLINK(p->mod_dst)) {
949
		dst_abbrev = verify_submodule_committish(p->sm_path,
950
							 oid_to_hex(&p->oid_dst));
951
		if (!dst_abbrev) {
952
			missing_dst = 1;
953
			/*
954
			 * As `rev-parse` failed, we fallback to getting
955
			 * the abbreviated hash using oid_dst. We do
956
			 * this as we might still need the abbreviated
957
			 * hash in cases like a submodule type change, etc.
958
			 */
959
			dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
960
		}
961
	} else {
962
		/*
963
		 * The destination does not point to a submodule.
964
		 * So, we fallback to getting the abbreviation using
965
		 * oid_dst as we might still need the abbreviated
966
		 * hash in cases like a submodule removal, etc.
967
		 */
968
		dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
969
	}
970

971
	displaypath = get_submodule_displaypath(p->sm_path, info->prefix,
972
						info->super_prefix);
973

974
	if (!missing_src && !missing_dst) {
975
		struct child_process cp_rev_list = CHILD_PROCESS_INIT;
976
		struct strbuf sb_rev_list = STRBUF_INIT;
977

978
		strvec_pushl(&cp_rev_list.args, "rev-list",
979
			     "--first-parent", "--count", NULL);
980
		if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
981
			strvec_pushf(&cp_rev_list.args, "%s...%s",
982
				     src_abbrev, dst_abbrev);
983
		else
984
			strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
985
				    src_abbrev : dst_abbrev);
986
		strvec_push(&cp_rev_list.args, "--");
987

988
		cp_rev_list.git_cmd = 1;
989
		cp_rev_list.dir = p->sm_path;
990
		prepare_submodule_repo_env(&cp_rev_list.env);
991

992
		if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
993
			total_commits = atoi(sb_rev_list.buf);
994

995
		strbuf_release(&sb_rev_list);
996
	} else {
997
		/*
998
		 * Don't give error msg for modification whose dst is not
999
		 * submodule, i.e., deleted or changed to blob
1000
		 */
1001
		if (S_ISGITLINK(p->mod_dst)) {
1002
			if (missing_src && missing_dst) {
1003
				strbuf_addf(&errmsg, "  Warn: %s doesn't contain commits %s and %s\n",
1004
					    displaypath, oid_to_hex(&p->oid_src),
1005
					    oid_to_hex(&p->oid_dst));
1006
			} else {
1007
				strbuf_addf(&errmsg, "  Warn: %s doesn't contain commit %s\n",
1008
					    displaypath, missing_src ?
1009
					    oid_to_hex(&p->oid_src) :
1010
					    oid_to_hex(&p->oid_dst));
1011
			}
1012
		}
1013
	}
1014

1015
	print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
1016
				total_commits, displaypath, src_abbrev,
1017
				dst_abbrev, p);
1018

1019
	free(displaypath);
1020
	free(src_abbrev);
1021
	free(dst_abbrev);
1022
	strbuf_release(&errmsg);
1023
}
1024

1025
static void prepare_submodule_summary(struct summary_cb *info,
1026
				      struct module_cb_list *list)
1027
{
1028
	int i;
1029
	for (i = 0; i < list->nr; i++) {
1030
		const struct submodule *sub;
1031
		struct module_cb *p = list->entries[i];
1032
		struct strbuf sm_gitdir = STRBUF_INIT;
1033

1034
		if (p->status == 'D' || p->status == 'T') {
1035
			generate_submodule_summary(info, p);
1036
			continue;
1037
		}
1038

1039
		if (info->for_status && p->status != 'A' &&
1040
		    (sub = submodule_from_path(the_repository,
1041
					       null_oid(), p->sm_path))) {
1042
			char *config_key = NULL;
1043
			const char *value;
1044
			int ignore_all = 0;
1045

1046
			config_key = xstrfmt("submodule.%s.ignore",
1047
					     sub->name);
1048
			if (!git_config_get_string_tmp(config_key, &value))
1049
				ignore_all = !strcmp(value, "all");
1050
			else if (sub->ignore)
1051
				ignore_all = !strcmp(sub->ignore, "all");
1052

1053
			free(config_key);
1054
			if (ignore_all)
1055
				continue;
1056
		}
1057

1058
		/* Also show added or modified modules which are checked out */
1059
		strbuf_addstr(&sm_gitdir, p->sm_path);
1060
		if (is_nonbare_repository_dir(&sm_gitdir))
1061
			generate_submodule_summary(info, p);
1062
		strbuf_release(&sm_gitdir);
1063
	}
1064
}
1065

1066
static void submodule_summary_callback(struct diff_queue_struct *q,
1067
				       struct diff_options *options UNUSED,
1068
				       void *data)
1069
{
1070
	int i;
1071
	struct module_cb_list *list = data;
1072
	for (i = 0; i < q->nr; i++) {
1073
		struct diff_filepair *p = q->queue[i];
1074
		struct module_cb *temp;
1075

1076
		if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1077
			continue;
1078
		temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1079
		temp->mod_src = p->one->mode;
1080
		temp->mod_dst = p->two->mode;
1081
		temp->oid_src = p->one->oid;
1082
		temp->oid_dst = p->two->oid;
1083
		temp->status = p->status;
1084
		temp->sm_path = xstrdup(p->one->path);
1085

1086
		ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1087
		list->entries[list->nr++] = temp;
1088
	}
1089
}
1090

1091
static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1092
{
1093
	switch (diff_cmd) {
1094
	case DIFF_INDEX: return "diff-index";
1095
	case DIFF_FILES: return "diff-files";
1096
	default: BUG("bad diff_cmd value %d", diff_cmd);
1097
	}
1098
}
1099

1100
static int compute_summary_module_list(struct object_id *head_oid,
1101
				       struct summary_cb *info,
1102
				       enum diff_cmd diff_cmd)
1103
{
1104
	struct strvec diff_args = STRVEC_INIT;
1105
	struct rev_info rev;
1106
	struct setup_revision_opt opt = {
1107
		.free_removed_argv_elements = 1,
1108
	};
1109
	struct module_cb_list list = MODULE_CB_LIST_INIT;
1110
	int ret = 0;
1111

1112
	strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1113
	if (info->cached)
1114
		strvec_push(&diff_args, "--cached");
1115
	strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1116
	if (head_oid)
1117
		strvec_push(&diff_args, oid_to_hex(head_oid));
1118
	strvec_push(&diff_args, "--");
1119
	if (info->argc)
1120
		strvec_pushv(&diff_args, info->argv);
1121

1122
	git_config(git_diff_basic_config, NULL);
1123
	repo_init_revisions(the_repository, &rev, info->prefix);
1124
	rev.abbrev = 0;
1125
	precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1126
	setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1127
	rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1128
	rev.diffopt.format_callback = submodule_summary_callback;
1129
	rev.diffopt.format_callback_data = &list;
1130

1131
	if (!info->cached) {
1132
		if (diff_cmd == DIFF_INDEX)
1133
			setup_work_tree();
1134
		if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
1135
			perror("repo_read_index_preload");
1136
			ret = -1;
1137
			goto cleanup;
1138
		}
1139
	} else if (repo_read_index(the_repository) < 0) {
1140
		perror("repo_read_cache");
1141
		ret = -1;
1142
		goto cleanup;
1143
	}
1144

1145
	if (diff_cmd == DIFF_INDEX)
1146
		run_diff_index(&rev, info->cached ? DIFF_INDEX_CACHED : 0);
1147
	else
1148
		run_diff_files(&rev, 0);
1149
	prepare_submodule_summary(info, &list);
1150
cleanup:
1151
	strvec_clear(&diff_args);
1152
	release_revisions(&rev);
1153
	module_cb_list_release(&list);
1154
	return ret;
1155
}
1156

1157
static int module_summary(int argc, const char **argv, const char *prefix)
1158
{
1159
	struct summary_cb info = SUMMARY_CB_INIT;
1160
	int cached = 0;
1161
	int for_status = 0;
1162
	int files = 0;
1163
	int summary_limit = -1;
1164
	enum diff_cmd diff_cmd = DIFF_INDEX;
1165
	struct object_id head_oid;
1166
	int ret;
1167
	struct option module_summary_options[] = {
1168
		OPT_BOOL(0, "cached", &cached,
1169
			 N_("use the commit stored in the index instead of the submodule HEAD")),
1170
		OPT_BOOL(0, "files", &files,
1171
			 N_("compare the commit in the index with that in the submodule HEAD")),
1172
		OPT_BOOL(0, "for-status", &for_status,
1173
			 N_("skip submodules with 'ignore_config' value set to 'all'")),
1174
		OPT_INTEGER('n', "summary-limit", &summary_limit,
1175
			     N_("limit the summary size")),
1176
		OPT_END()
1177
	};
1178
	const char *const git_submodule_helper_usage[] = {
1179
		N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1180
		NULL
1181
	};
1182

1183
	argc = parse_options(argc, argv, prefix, module_summary_options,
1184
			     git_submodule_helper_usage, 0);
1185

1186
	if (!summary_limit)
1187
		return 0;
1188

1189
	if (!repo_get_oid(the_repository, argc ? argv[0] : "HEAD", &head_oid)) {
1190
		if (argc) {
1191
			argv++;
1192
			argc--;
1193
		}
1194
	} else if (!argc || !strcmp(argv[0], "HEAD")) {
1195
		/* before the first commit: compare with an empty tree */
1196
		oidcpy(&head_oid, the_hash_algo->empty_tree);
1197
		if (argc) {
1198
			argv++;
1199
			argc--;
1200
		}
1201
	} else {
1202
		if (repo_get_oid(the_repository, "HEAD", &head_oid))
1203
			die(_("could not fetch a revision for HEAD"));
1204
	}
1205

1206
	if (files) {
1207
		if (cached)
1208
			die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1209
		diff_cmd = DIFF_FILES;
1210
	}
1211

1212
	info.argc = argc;
1213
	info.argv = argv;
1214
	info.prefix = prefix;
1215
	info.cached = !!cached;
1216
	info.files = !!files;
1217
	info.for_status = !!for_status;
1218
	info.summary_limit = summary_limit;
1219

1220
	ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1221
					  &info, diff_cmd);
1222
	return ret;
1223
}
1224

1225
struct sync_cb {
1226
	const char *prefix;
1227
	const char *super_prefix;
1228
	unsigned int flags;
1229
};
1230
#define SYNC_CB_INIT { 0 }
1231

1232
static void sync_submodule(const char *path, const char *prefix,
1233
			   const char *super_prefix, unsigned int flags)
1234
{
1235
	const struct submodule *sub;
1236
	char *remote_key = NULL;
1237
	char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1238
	struct strbuf sb = STRBUF_INIT;
1239
	char *sub_config_path = NULL;
1240
	int code;
1241

1242
	if (!is_submodule_active(the_repository, path))
1243
		return;
1244

1245
	if (validate_submodule_path(path) < 0)
1246
		exit(128);
1247

1248
	sub = submodule_from_path(the_repository, null_oid(), path);
1249

1250
	if (sub && sub->url) {
1251
		if (starts_with_dot_dot_slash(sub->url) ||
1252
		    starts_with_dot_slash(sub->url)) {
1253
			char *up_path = get_up_path(path);
1254

1255
			sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1256
			super_config_url = resolve_relative_url(sub->url, NULL, 1);
1257
			free(up_path);
1258
		} else {
1259
			sub_origin_url = xstrdup(sub->url);
1260
			super_config_url = xstrdup(sub->url);
1261
		}
1262
	} else {
1263
		sub_origin_url = xstrdup("");
1264
		super_config_url = xstrdup("");
1265
	}
1266

1267
	displaypath = get_submodule_displaypath(path, prefix, super_prefix);
1268

1269
	if (!(flags & OPT_QUIET))
1270
		printf(_("Synchronizing submodule url for '%s'\n"),
1271
			 displaypath);
1272

1273
	strbuf_reset(&sb);
1274
	strbuf_addf(&sb, "submodule.%s.url", sub->name);
1275
	if (git_config_set_gently(sb.buf, super_config_url))
1276
		die(_("failed to register url for submodule path '%s'"),
1277
		      displaypath);
1278

1279
	if (!is_submodule_populated_gently(path, NULL))
1280
		goto cleanup;
1281

1282
	strbuf_reset(&sb);
1283
	code = get_default_remote_submodule(path, &default_remote);
1284
	if (code)
1285
		exit(code);
1286

1287
	remote_key = xstrfmt("remote.%s.url", default_remote);
1288
	free(default_remote);
1289

1290
	submodule_to_gitdir(&sb, path);
1291
	strbuf_addstr(&sb, "/config");
1292

1293
	if (git_config_set_in_file_gently(sb.buf, remote_key, NULL, sub_origin_url))
1294
		die(_("failed to update remote for submodule '%s'"),
1295
		      path);
1296

1297
	if (flags & OPT_RECURSIVE) {
1298
		struct child_process cpr = CHILD_PROCESS_INIT;
1299

1300
		cpr.git_cmd = 1;
1301
		cpr.dir = path;
1302
		prepare_submodule_repo_env(&cpr.env);
1303

1304
		strvec_pushl(&cpr.args, "submodule--helper", "sync",
1305
			     "--recursive", NULL);
1306
		strvec_pushf(&cpr.args, "--super-prefix=%s/", displaypath);
1307

1308
		if (flags & OPT_QUIET)
1309
			strvec_push(&cpr.args, "--quiet");
1310

1311
		if (run_command(&cpr))
1312
			die(_("failed to recurse into submodule '%s'"),
1313
			      path);
1314
	}
1315

1316
cleanup:
1317
	free(super_config_url);
1318
	free(sub_origin_url);
1319
	strbuf_release(&sb);
1320
	free(remote_key);
1321
	free(displaypath);
1322
	free(sub_config_path);
1323
}
1324

1325
static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1326
{
1327
	struct sync_cb *info = cb_data;
1328

1329
	sync_submodule(list_item->name, info->prefix, info->super_prefix,
1330
		       info->flags);
1331
}
1332

1333
static int module_sync(int argc, const char **argv, const char *prefix)
1334
{
1335
	struct sync_cb info = SYNC_CB_INIT;
1336
	struct pathspec pathspec = { 0 };
1337
	struct module_list list = MODULE_LIST_INIT;
1338
	int quiet = 0;
1339
	int recursive = 0;
1340
	struct option module_sync_options[] = {
1341
		OPT__SUPER_PREFIX(&info.super_prefix),
1342
		OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1343
		OPT_BOOL(0, "recursive", &recursive,
1344
			N_("recurse into nested submodules")),
1345
		OPT_END()
1346
	};
1347
	const char *const git_submodule_helper_usage[] = {
1348
		N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1349
		NULL
1350
	};
1351
	int ret = 1;
1352

1353
	argc = parse_options(argc, argv, prefix, module_sync_options,
1354
			     git_submodule_helper_usage, 0);
1355

1356
	if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1357
		goto cleanup;
1358

1359
	info.prefix = prefix;
1360
	if (quiet)
1361
		info.flags |= OPT_QUIET;
1362
	if (recursive)
1363
		info.flags |= OPT_RECURSIVE;
1364

1365
	for_each_listed_submodule(&list, sync_submodule_cb, &info);
1366

1367
	ret = 0;
1368
cleanup:
1369
	module_list_release(&list);
1370
	clear_pathspec(&pathspec);
1371
	return ret;
1372
}
1373

1374
struct deinit_cb {
1375
	const char *prefix;
1376
	unsigned int flags;
1377
};
1378
#define DEINIT_CB_INIT { 0 }
1379

1380
static void deinit_submodule(const char *path, const char *prefix,
1381
			     unsigned int flags)
1382
{
1383
	const struct submodule *sub;
1384
	char *displaypath = NULL;
1385
	struct child_process cp_config = CHILD_PROCESS_INIT;
1386
	struct strbuf sb_config = STRBUF_INIT;
1387
	char *sub_git_dir = xstrfmt("%s/.git", path);
1388

1389
	if (validate_submodule_path(path) < 0)
1390
		exit(128);
1391

1392
	sub = submodule_from_path(the_repository, null_oid(), path);
1393

1394
	if (!sub || !sub->name)
1395
		goto cleanup;
1396

1397
	displaypath = get_submodule_displaypath(path, prefix, NULL);
1398

1399
	/* remove the submodule work tree (unless the user already did it) */
1400
	if (is_directory(path)) {
1401
		struct strbuf sb_rm = STRBUF_INIT;
1402
		const char *format;
1403

1404
		if (is_directory(sub_git_dir)) {
1405
			if (!(flags & OPT_QUIET))
1406
				warning(_("Submodule work tree '%s' contains a .git "
1407
					  "directory. This will be replaced with a "
1408
					  ".git file by using absorbgitdirs."),
1409
					displaypath);
1410

1411
			absorb_git_dir_into_superproject(path, NULL);
1412

1413
		}
1414

1415
		if (!(flags & OPT_FORCE)) {
1416
			struct child_process cp_rm = CHILD_PROCESS_INIT;
1417

1418
			cp_rm.git_cmd = 1;
1419
			strvec_pushl(&cp_rm.args, "rm", "-qn",
1420
				     path, NULL);
1421

1422
			if (run_command(&cp_rm))
1423
				die(_("Submodule work tree '%s' contains local "
1424
				      "modifications; use '-f' to discard them"),
1425
				      displaypath);
1426
		}
1427

1428
		strbuf_addstr(&sb_rm, path);
1429

1430
		if (!remove_dir_recursively(&sb_rm, 0))
1431
			format = _("Cleared directory '%s'\n");
1432
		else
1433
			format = _("Could not remove submodule work tree '%s'\n");
1434

1435
		if (!(flags & OPT_QUIET))
1436
			printf(format, displaypath);
1437

1438
		submodule_unset_core_worktree(sub);
1439

1440
		strbuf_release(&sb_rm);
1441
	}
1442

1443
	if (mkdir(path, 0777))
1444
		printf(_("could not create empty submodule directory %s"),
1445
		      displaypath);
1446

1447
	cp_config.git_cmd = 1;
1448
	strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1449
	strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1450

1451
	/* remove the .git/config entries (unless the user already did it) */
1452
	if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1453
		char *sub_key = xstrfmt("submodule.%s", sub->name);
1454

1455
		/*
1456
		 * remove the whole section so we have a clean state when
1457
		 * the user later decides to init this submodule again
1458
		 */
1459
		repo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);
1460
		if (!(flags & OPT_QUIET))
1461
			printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1462
				 sub->name, sub->url, displaypath);
1463
		free(sub_key);
1464
	}
1465

1466
cleanup:
1467
	free(displaypath);
1468
	free(sub_git_dir);
1469
	strbuf_release(&sb_config);
1470
}
1471

1472
static void deinit_submodule_cb(const struct cache_entry *list_item,
1473
				void *cb_data)
1474
{
1475
	struct deinit_cb *info = cb_data;
1476
	deinit_submodule(list_item->name, info->prefix, info->flags);
1477
}
1478

1479
static int module_deinit(int argc, const char **argv, const char *prefix)
1480
{
1481
	struct deinit_cb info = DEINIT_CB_INIT;
1482
	struct pathspec pathspec = { 0 };
1483
	struct module_list list = MODULE_LIST_INIT;
1484
	int quiet = 0;
1485
	int force = 0;
1486
	int all = 0;
1487
	struct option module_deinit_options[] = {
1488
		OPT__QUIET(&quiet, N_("suppress submodule status output")),
1489
		OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1490
		OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1491
		OPT_END()
1492
	};
1493
	const char *const git_submodule_helper_usage[] = {
1494
		N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1495
		NULL
1496
	};
1497
	int ret = 1;
1498

1499
	argc = parse_options(argc, argv, prefix, module_deinit_options,
1500
			     git_submodule_helper_usage, 0);
1501

1502
	if (all && argc) {
1503
		error("pathspec and --all are incompatible");
1504
		usage_with_options(git_submodule_helper_usage,
1505
				   module_deinit_options);
1506
	}
1507

1508
	if (!argc && !all)
1509
		die(_("Use '--all' if you really want to deinitialize all submodules"));
1510

1511
	if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
1512
		goto cleanup;
1513

1514
	info.prefix = prefix;
1515
	if (quiet)
1516
		info.flags |= OPT_QUIET;
1517
	if (force)
1518
		info.flags |= OPT_FORCE;
1519

1520
	for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1521

1522
	ret = 0;
1523
cleanup:
1524
	module_list_release(&list);
1525
	clear_pathspec(&pathspec);
1526
	return ret;
1527
}
1528

1529
struct module_clone_data {
1530
	const char *prefix;
1531
	const char *path;
1532
	const char *name;
1533
	const char *url;
1534
	int depth;
1535
	struct list_objects_filter_options *filter_options;
1536
	enum ref_storage_format ref_storage_format;
1537
	unsigned int quiet: 1;
1538
	unsigned int progress: 1;
1539
	unsigned int dissociate: 1;
1540
	unsigned int require_init: 1;
1541
	int single_branch;
1542
};
1543
#define MODULE_CLONE_DATA_INIT { \
1544
	.single_branch = -1, \
1545
	.ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
1546
}
1547

1548
struct submodule_alternate_setup {
1549
	const char *submodule_name;
1550
	enum SUBMODULE_ALTERNATE_ERROR_MODE {
1551
		SUBMODULE_ALTERNATE_ERROR_DIE,
1552
		SUBMODULE_ALTERNATE_ERROR_INFO,
1553
		SUBMODULE_ALTERNATE_ERROR_IGNORE
1554
	} error_mode;
1555
	struct string_list *reference;
1556
};
1557
#define SUBMODULE_ALTERNATE_SETUP_INIT { \
1558
	.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1559
}
1560

1561
static const char alternate_error_advice[] = N_(
1562
"An alternate computed from a superproject's alternate is invalid.\n"
1563
"To allow Git to clone without an alternate in such a case, set\n"
1564
"submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1565
"'--reference-if-able' instead of '--reference'."
1566
);
1567

1568
static int add_possible_reference_from_superproject(
1569
		struct object_directory *odb, void *sas_cb)
1570
{
1571
	struct submodule_alternate_setup *sas = sas_cb;
1572
	size_t len;
1573

1574
	/*
1575
	 * If the alternate object store is another repository, try the
1576
	 * standard layout with .git/(modules/<name>)+/objects
1577
	 */
1578
	if (strip_suffix(odb->path, "/objects", &len)) {
1579
		struct repository alternate;
1580
		char *sm_alternate;
1581
		struct strbuf sb = STRBUF_INIT;
1582
		struct strbuf err = STRBUF_INIT;
1583
		strbuf_add(&sb, odb->path, len);
1584

1585
		if (repo_init(&alternate, sb.buf, NULL) < 0)
1586
			die(_("could not get a repository handle for gitdir '%s'"),
1587
			    sb.buf);
1588

1589
		/*
1590
		 * We need to end the new path with '/' to mark it as a dir,
1591
		 * otherwise a submodule name containing '/' will be broken
1592
		 * as the last part of a missing submodule reference would
1593
		 * be taken as a file name.
1594
		 */
1595
		strbuf_reset(&sb);
1596
		submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1597
		strbuf_addch(&sb, '/');
1598
		repo_clear(&alternate);
1599

1600
		sm_alternate = compute_alternate_path(sb.buf, &err);
1601
		if (sm_alternate) {
1602
			char *p = strbuf_detach(&sb, NULL);
1603

1604
			string_list_append(sas->reference, p)->util = p;
1605
			free(sm_alternate);
1606
		} else {
1607
			switch (sas->error_mode) {
1608
			case SUBMODULE_ALTERNATE_ERROR_DIE:
1609
				if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1610
					advise(_(alternate_error_advice));
1611
				die(_("submodule '%s' cannot add alternate: %s"),
1612
				    sas->submodule_name, err.buf);
1613
			case SUBMODULE_ALTERNATE_ERROR_INFO:
1614
				fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1615
					sas->submodule_name, err.buf);
1616
			case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1617
				; /* nothing */
1618
			}
1619
		}
1620
		strbuf_release(&sb);
1621
	}
1622

1623
	return 0;
1624
}
1625

1626
static void prepare_possible_alternates(const char *sm_name,
1627
		struct string_list *reference)
1628
{
1629
	char *sm_alternate = NULL, *error_strategy = NULL;
1630
	struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1631

1632
	git_config_get_string("submodule.alternateLocation", &sm_alternate);
1633
	if (!sm_alternate)
1634
		return;
1635

1636
	git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1637

1638
	if (!error_strategy)
1639
		error_strategy = xstrdup("die");
1640

1641
	sas.submodule_name = sm_name;
1642
	sas.reference = reference;
1643
	if (!strcmp(error_strategy, "die"))
1644
		sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1645
	else if (!strcmp(error_strategy, "info"))
1646
		sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1647
	else if (!strcmp(error_strategy, "ignore"))
1648
		sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1649
	else
1650
		die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1651

1652
	if (!strcmp(sm_alternate, "superproject"))
1653
		foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1654
	else if (!strcmp(sm_alternate, "no"))
1655
		; /* do nothing */
1656
	else
1657
		die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1658

1659
	free(sm_alternate);
1660
	free(error_strategy);
1661
}
1662

1663
static char *clone_submodule_sm_gitdir(const char *name)
1664
{
1665
	struct strbuf sb = STRBUF_INIT;
1666
	char *sm_gitdir;
1667

1668
	submodule_name_to_gitdir(&sb, the_repository, name);
1669
	sm_gitdir = absolute_pathdup(sb.buf);
1670
	strbuf_release(&sb);
1671

1672
	return sm_gitdir;
1673
}
1674

1675
static int dir_contains_only_dotgit(const char *path)
1676
{
1677
	DIR *dir = opendir(path);
1678
	struct dirent *e;
1679
	int ret = 1;
1680

1681
	if (!dir)
1682
		return 0;
1683

1684
	e = readdir_skip_dot_and_dotdot(dir);
1685
	if (!e)
1686
		ret = 0;
1687
	else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT, e->d_name) ||
1688
		 (e = readdir_skip_dot_and_dotdot(dir))) {
1689
		error("unexpected item '%s' in '%s'", e->d_name, path);
1690
		ret = 0;
1691
	}
1692

1693
	closedir(dir);
1694
	return ret;
1695
}
1696

1697
static int clone_submodule(const struct module_clone_data *clone_data,
1698
			   struct string_list *reference)
1699
{
1700
	char *p;
1701
	char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1702
	char *sm_alternate = NULL, *error_strategy = NULL;
1703
	struct stat st;
1704
	struct child_process cp = CHILD_PROCESS_INIT;
1705
	const char *clone_data_path = clone_data->path;
1706
	char *to_free = NULL;
1707

1708
	if (validate_submodule_path(clone_data_path) < 0)
1709
		exit(128);
1710

1711
	if (!is_absolute_path(clone_data->path))
1712
		clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1713
						    clone_data->path);
1714

1715
	if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1716
		die(_("refusing to create/use '%s' in another submodule's "
1717
		      "git dir"), sm_gitdir);
1718

1719
	if (!file_exists(sm_gitdir)) {
1720
		if (clone_data->require_init && !stat(clone_data_path, &st) &&
1721
		    !is_empty_dir(clone_data_path))
1722
			die(_("directory not empty: '%s'"), clone_data_path);
1723

1724
		if (safe_create_leading_directories_const(sm_gitdir) < 0)
1725
			die(_("could not create directory '%s'"), sm_gitdir);
1726

1727
		prepare_possible_alternates(clone_data->name, reference);
1728

1729
		strvec_push(&cp.args, "clone");
1730
		strvec_push(&cp.args, "--no-checkout");
1731
		if (clone_data->quiet)
1732
			strvec_push(&cp.args, "--quiet");
1733
		if (clone_data->progress)
1734
			strvec_push(&cp.args, "--progress");
1735
		if (clone_data->depth > 0)
1736
			strvec_pushf(&cp.args, "--depth=%d", clone_data->depth);
1737
		if (reference->nr) {
1738
			struct string_list_item *item;
1739

1740
			for_each_string_list_item(item, reference)
1741
				strvec_pushl(&cp.args, "--reference",
1742
					     item->string, NULL);
1743
		}
1744
		if (clone_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
1745
			strvec_pushf(&cp.args, "--ref-format=%s",
1746
				     ref_storage_format_to_name(clone_data->ref_storage_format));
1747
		if (clone_data->dissociate)
1748
			strvec_push(&cp.args, "--dissociate");
1749
		if (sm_gitdir && *sm_gitdir)
1750
			strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1751
		if (clone_data->filter_options && clone_data->filter_options->choice)
1752
			strvec_pushf(&cp.args, "--filter=%s",
1753
				     expand_list_objects_filter_spec(
1754
					     clone_data->filter_options));
1755
		if (clone_data->single_branch >= 0)
1756
			strvec_push(&cp.args, clone_data->single_branch ?
1757
				    "--single-branch" :
1758
				    "--no-single-branch");
1759

1760
		strvec_push(&cp.args, "--");
1761
		strvec_push(&cp.args, clone_data->url);
1762
		strvec_push(&cp.args, clone_data_path);
1763

1764
		cp.git_cmd = 1;
1765
		prepare_submodule_repo_env(&cp.env);
1766
		cp.no_stdin = 1;
1767

1768
		if(run_command(&cp))
1769
			die(_("clone of '%s' into submodule path '%s' failed"),
1770
			    clone_data->url, clone_data_path);
1771

1772
		if (clone_data->require_init && !stat(clone_data_path, &st) &&
1773
		    !dir_contains_only_dotgit(clone_data_path)) {
1774
			char *dot_git = xstrfmt("%s/.git", clone_data_path);
1775
			unlink(dot_git);
1776
			free(dot_git);
1777
			die(_("directory not empty: '%s'"), clone_data_path);
1778
		}
1779
	} else {
1780
		char *path;
1781

1782
		if (clone_data->require_init && !stat(clone_data_path, &st) &&
1783
		    !is_empty_dir(clone_data_path))
1784
			die(_("directory not empty: '%s'"), clone_data_path);
1785
		if (safe_create_leading_directories_const(clone_data_path) < 0)
1786
			die(_("could not create directory '%s'"), clone_data_path);
1787
		path = xstrfmt("%s/index", sm_gitdir);
1788
		unlink_or_warn(path);
1789
		free(path);
1790
	}
1791

1792
	/*
1793
	 * We already performed this check at the beginning of this function,
1794
	 * before cloning the objects. This tries to detect racy behavior e.g.
1795
	 * in parallel clones, where another process could easily have made the
1796
	 * gitdir nested _after_ it was created.
1797
	 *
1798
	 * To prevent further harm coming from this unintentionally-nested
1799
	 * gitdir, let's disable it by deleting the `HEAD` file.
1800
	 */
1801
	if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) {
1802
		char *head = xstrfmt("%s/HEAD", sm_gitdir);
1803
		unlink(head);
1804
		free(head);
1805
		die(_("refusing to create/use '%s' in another submodule's "
1806
		      "git dir"), sm_gitdir);
1807
	}
1808

1809
	connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1810

1811
	p = git_pathdup_submodule(clone_data_path, "config");
1812
	if (!p)
1813
		die(_("could not get submodule directory for '%s'"), clone_data_path);
1814

1815
	/* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1816
	git_config_get_string("submodule.alternateLocation", &sm_alternate);
1817
	if (sm_alternate)
1818
		git_config_set_in_file(p, "submodule.alternateLocation",
1819
				       sm_alternate);
1820
	git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1821
	if (error_strategy)
1822
		git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1823
				       error_strategy);
1824

1825
	free(sm_alternate);
1826
	free(error_strategy);
1827

1828
	free(sm_gitdir);
1829
	free(p);
1830
	free(to_free);
1831
	return 0;
1832
}
1833

1834
static int module_clone(int argc, const char **argv, const char *prefix)
1835
{
1836
	int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1837
	struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1838
	struct string_list reference = STRING_LIST_INIT_NODUP;
1839
	struct list_objects_filter_options filter_options =
1840
		LIST_OBJECTS_FILTER_INIT;
1841
	const char *ref_storage_format = NULL;
1842

1843
	struct option module_clone_options[] = {
1844
		OPT_STRING(0, "prefix", &clone_data.prefix,
1845
			   N_("path"),
1846
			   N_("alternative anchor for relative paths")),
1847
		OPT_STRING(0, "path", &clone_data.path,
1848
			   N_("path"),
1849
			   N_("where the new submodule will be cloned to")),
1850
		OPT_STRING(0, "name", &clone_data.name,
1851
			   N_("string"),
1852
			   N_("name of the new submodule")),
1853
		OPT_STRING(0, "url", &clone_data.url,
1854
			   N_("string"),
1855
			   N_("url where to clone the submodule from")),
1856
		OPT_STRING_LIST(0, "reference", &reference,
1857
			   N_("repo"),
1858
			   N_("reference repository")),
1859
		OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
1860
			   N_("specify the reference format to use")),
1861
		OPT_BOOL(0, "dissociate", &dissociate,
1862
			   N_("use --reference only while cloning")),
1863
		OPT_INTEGER(0, "depth", &clone_data.depth,
1864
			   N_("depth for shallow clones")),
1865
		OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1866
		OPT_BOOL(0, "progress", &progress,
1867
			   N_("force cloning progress")),
1868
		OPT_BOOL(0, "require-init", &require_init,
1869
			   N_("disallow cloning into non-empty directory")),
1870
		OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1871
			 N_("clone only one branch, HEAD or --branch")),
1872
		OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1873
		OPT_END()
1874
	};
1875
	const char *const git_submodule_helper_usage[] = {
1876
		N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1877
		   "[--reference <repository>] [--name <name>] [--depth <depth>] "
1878
		   "[--single-branch] [--filter <filter-spec>] "
1879
		   "--url <url> --path <path>"),
1880
		NULL
1881
	};
1882

1883
	argc = parse_options(argc, argv, prefix, module_clone_options,
1884
			     git_submodule_helper_usage, 0);
1885

1886
	if (ref_storage_format) {
1887
		clone_data.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
1888
		if (clone_data.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
1889
			die(_("unknown ref storage format '%s'"), ref_storage_format);
1890
	}
1891
	clone_data.dissociate = !!dissociate;
1892
	clone_data.quiet = !!quiet;
1893
	clone_data.progress = !!progress;
1894
	clone_data.require_init = !!require_init;
1895
	clone_data.filter_options = &filter_options;
1896

1897
	if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1898
		usage_with_options(git_submodule_helper_usage,
1899
				   module_clone_options);
1900

1901
	clone_submodule(&clone_data, &reference);
1902
	list_objects_filter_release(&filter_options);
1903
	string_list_clear(&reference, 1);
1904
	return 0;
1905
}
1906

1907
static int determine_submodule_update_strategy(struct repository *r,
1908
					       int just_cloned,
1909
					       const char *path,
1910
					       enum submodule_update_type update,
1911
					       struct submodule_update_strategy *out)
1912
{
1913
	const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1914
	char *key;
1915
	const char *val;
1916
	int ret;
1917

1918
	key = xstrfmt("submodule.%s.update", sub->name);
1919

1920
	if (update) {
1921
		out->type = update;
1922
	} else if (!repo_config_get_string_tmp(r, key, &val)) {
1923
		if (parse_submodule_update_strategy(val, out) < 0) {
1924
			ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1925
					  val, path);
1926
			goto cleanup;
1927
		}
1928
	} else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1929
		if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1930
			BUG("how did we read update = !command from .gitmodules?");
1931
		out->type = sub->update_strategy.type;
1932
		out->command = sub->update_strategy.command;
1933
	} else
1934
		out->type = SM_UPDATE_CHECKOUT;
1935

1936
	if (just_cloned &&
1937
	    (out->type == SM_UPDATE_MERGE ||
1938
	     out->type == SM_UPDATE_REBASE ||
1939
	     out->type == SM_UPDATE_NONE))
1940
		out->type = SM_UPDATE_CHECKOUT;
1941

1942
	ret = 0;
1943
cleanup:
1944
	free(key);
1945
	return ret;
1946
}
1947

1948
struct update_clone_data {
1949
	const struct submodule *sub;
1950
	struct object_id oid;
1951
	unsigned just_cloned;
1952
};
1953

1954
struct submodule_update_clone {
1955
	/* index into 'update_data.list', the list of submodules to look into for cloning */
1956
	int current;
1957

1958
	/* configuration parameters which are passed on to the children */
1959
	const struct update_data *update_data;
1960

1961
	/* to be consumed by update_submodule() */
1962
	struct update_clone_data *update_clone;
1963
	int update_clone_nr; int update_clone_alloc;
1964

1965
	/* If we want to stop as fast as possible and return an error */
1966
	unsigned quickstop : 1;
1967

1968
	/* failed clones to be retried again */
1969
	const struct cache_entry **failed_clones;
1970
	int failed_clones_nr, failed_clones_alloc;
1971
};
1972
#define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1973

1974
static void submodule_update_clone_release(struct submodule_update_clone *suc)
1975
{
1976
	free(suc->update_clone);
1977
	free(suc->failed_clones);
1978
}
1979

1980
struct update_data {
1981
	const char *prefix;
1982
	const char *super_prefix;
1983
	char *displaypath;
1984
	enum submodule_update_type update_default;
1985
	struct object_id suboid;
1986
	struct string_list references;
1987
	struct submodule_update_strategy update_strategy;
1988
	struct list_objects_filter_options *filter_options;
1989
	struct module_list list;
1990
	enum ref_storage_format ref_storage_format;
1991
	int depth;
1992
	int max_jobs;
1993
	int single_branch;
1994
	int recommend_shallow;
1995
	unsigned int require_init;
1996
	unsigned int force;
1997
	unsigned int quiet;
1998
	unsigned int nofetch;
1999
	unsigned int remote;
2000
	unsigned int progress;
2001
	unsigned int dissociate;
2002
	unsigned int init;
2003
	unsigned int warn_if_uninitialized;
2004
	unsigned int recursive;
2005

2006
	/* copied over from update_clone_data */
2007
	struct object_id oid;
2008
	unsigned int just_cloned;
2009
	const char *sm_path;
2010
};
2011
#define UPDATE_DATA_INIT { \
2012
	.update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
2013
	.list = MODULE_LIST_INIT, \
2014
	.ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
2015
	.recommend_shallow = -1, \
2016
	.references = STRING_LIST_INIT_DUP, \
2017
	.single_branch = -1, \
2018
	.max_jobs = 1, \
2019
}
2020

2021
static void update_data_release(struct update_data *ud)
2022
{
2023
	free(ud->displaypath);
2024
	module_list_release(&ud->list);
2025
}
2026

2027
static void next_submodule_warn_missing(struct submodule_update_clone *suc,
2028
		struct strbuf *out, const char *displaypath)
2029
{
2030
	/*
2031
	 * Only mention uninitialized submodules when their
2032
	 * paths have been specified.
2033
	 */
2034
	if (suc->update_data->warn_if_uninitialized) {
2035
		strbuf_addf(out,
2036
			_("Submodule path '%s' not initialized"),
2037
			displaypath);
2038
		strbuf_addch(out, '\n');
2039
		strbuf_addstr(out,
2040
			_("Maybe you want to use 'update --init'?"));
2041
		strbuf_addch(out, '\n');
2042
	}
2043
}
2044

2045
/**
2046
 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
2047
 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
2048
 */
2049
static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
2050
					   struct child_process *child,
2051
					   struct submodule_update_clone *suc,
2052
					   struct strbuf *out)
2053
{
2054
	const struct submodule *sub = NULL;
2055
	const char *url = NULL;
2056
	const char *update_string;
2057
	enum submodule_update_type update_type;
2058
	char *key;
2059
	const struct update_data *ud = suc->update_data;
2060
	char *displaypath = get_submodule_displaypath(ce->name, ud->prefix,
2061
						      ud->super_prefix);
2062
	struct strbuf sb = STRBUF_INIT;
2063
	int needs_cloning = 0;
2064
	int need_free_url = 0;
2065

2066
	if (ce_stage(ce)) {
2067
		strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
2068
		strbuf_addch(out, '\n');
2069
		goto cleanup;
2070
	}
2071

2072
	sub = submodule_from_path(the_repository, null_oid(), ce->name);
2073

2074
	if (!sub) {
2075
		next_submodule_warn_missing(suc, out, displaypath);
2076
		goto cleanup;
2077
	}
2078

2079
	key = xstrfmt("submodule.%s.update", sub->name);
2080
	if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
2081
		update_type = parse_submodule_update_type(update_string);
2082
	} else {
2083
		update_type = sub->update_strategy.type;
2084
	}
2085
	free(key);
2086

2087
	if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
2088
	    || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
2089
		&& update_type == SM_UPDATE_NONE)) {
2090
		strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
2091
		strbuf_addch(out, '\n');
2092
		goto cleanup;
2093
	}
2094

2095
	/* Check if the submodule has been initialized. */
2096
	if (!is_submodule_active(the_repository, ce->name)) {
2097
		next_submodule_warn_missing(suc, out, displaypath);
2098
		goto cleanup;
2099
	}
2100

2101
	strbuf_reset(&sb);
2102
	strbuf_addf(&sb, "submodule.%s.url", sub->name);
2103
	if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2104
		if (sub->url && (starts_with_dot_slash(sub->url) ||
2105
				 starts_with_dot_dot_slash(sub->url))) {
2106
			url = resolve_relative_url(sub->url, NULL, 0);
2107
			need_free_url = 1;
2108
		} else
2109
			url = sub->url;
2110
	}
2111

2112
	if (!url)
2113
		die(_("cannot clone submodule '%s' without a URL"), sub->name);
2114

2115
	strbuf_reset(&sb);
2116
	strbuf_addf(&sb, "%s/.git", ce->name);
2117
	needs_cloning = !file_exists(sb.buf);
2118

2119
	ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2120
		   suc->update_clone_alloc);
2121
	oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2122
	suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2123
	suc->update_clone[suc->update_clone_nr].sub = sub;
2124
	suc->update_clone_nr++;
2125

2126
	if (!needs_cloning)
2127
		goto cleanup;
2128

2129
	child->git_cmd = 1;
2130
	child->no_stdin = 1;
2131
	child->stdout_to_stderr = 1;
2132
	child->err = -1;
2133
	strvec_push(&child->args, "submodule--helper");
2134
	strvec_push(&child->args, "clone");
2135
	if (suc->update_data->progress)
2136
		strvec_push(&child->args, "--progress");
2137
	if (suc->update_data->quiet)
2138
		strvec_push(&child->args, "--quiet");
2139
	if (suc->update_data->prefix)
2140
		strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2141
	if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2142
		strvec_push(&child->args, "--depth=1");
2143
	else if (suc->update_data->depth)
2144
		strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2145
	if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2146
		strvec_pushf(&child->args, "--filter=%s",
2147
			     expand_list_objects_filter_spec(suc->update_data->filter_options));
2148
	if (suc->update_data->require_init)
2149
		strvec_push(&child->args, "--require-init");
2150
	if (suc->update_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
2151
		strvec_pushf(&child->args, "--ref-format=%s",
2152
			     ref_storage_format_to_name(suc->update_data->ref_storage_format));
2153
	strvec_pushl(&child->args, "--path", sub->path, NULL);
2154
	strvec_pushl(&child->args, "--name", sub->name, NULL);
2155
	strvec_pushl(&child->args, "--url", url, NULL);
2156
	if (suc->update_data->references.nr) {
2157
		struct string_list_item *item;
2158

2159
		for_each_string_list_item(item, &suc->update_data->references)
2160
			strvec_pushl(&child->args, "--reference", item->string, NULL);
2161
	}
2162
	if (suc->update_data->dissociate)
2163
		strvec_push(&child->args, "--dissociate");
2164
	if (suc->update_data->single_branch >= 0)
2165
		strvec_push(&child->args, suc->update_data->single_branch ?
2166
					      "--single-branch" :
2167
					      "--no-single-branch");
2168

2169
cleanup:
2170
	free(displaypath);
2171
	strbuf_release(&sb);
2172
	if (need_free_url)
2173
		free((void*)url);
2174

2175
	return needs_cloning;
2176
}
2177

2178
static int update_clone_get_next_task(struct child_process *child,
2179
				      struct strbuf *err,
2180
				      void *suc_cb,
2181
				      void **idx_task_cb)
2182
{
2183
	struct submodule_update_clone *suc = suc_cb;
2184
	const struct cache_entry *ce;
2185
	int index;
2186

2187
	for (; suc->current < suc->update_data->list.nr; suc->current++) {
2188
		ce = suc->update_data->list.entries[suc->current];
2189
		if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2190
			int *p = xmalloc(sizeof(*p));
2191

2192
			*p = suc->current;
2193
			*idx_task_cb = p;
2194
			suc->current++;
2195
			return 1;
2196
		}
2197
	}
2198

2199
	/*
2200
	 * The loop above tried cloning each submodule once, now try the
2201
	 * stragglers again, which we can imagine as an extension of the
2202
	 * entry list.
2203
	 */
2204
	index = suc->current - suc->update_data->list.nr;
2205
	if (index < suc->failed_clones_nr) {
2206
		int *p;
2207

2208
		ce = suc->failed_clones[index];
2209
		if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2210
			suc->current ++;
2211
			strbuf_addstr(err, "BUG: submodule considered for "
2212
					   "cloning, doesn't need cloning "
2213
					   "any more?\n");
2214
			return 0;
2215
		}
2216
		p = xmalloc(sizeof(*p));
2217
		*p = suc->current;
2218
		*idx_task_cb = p;
2219
		suc->current ++;
2220
		return 1;
2221
	}
2222

2223
	return 0;
2224
}
2225

2226
static int update_clone_start_failure(struct strbuf *err UNUSED,
2227
				      void *suc_cb,
2228
				      void *idx_task_cb UNUSED)
2229
{
2230
	struct submodule_update_clone *suc = suc_cb;
2231

2232
	suc->quickstop = 1;
2233
	return 1;
2234
}
2235

2236
static int update_clone_task_finished(int result,
2237
				      struct strbuf *err,
2238
				      void *suc_cb,
2239
				      void *idx_task_cb)
2240
{
2241
	const struct cache_entry *ce;
2242
	struct submodule_update_clone *suc = suc_cb;
2243
	int *idxP = idx_task_cb;
2244
	int idx = *idxP;
2245

2246
	free(idxP);
2247

2248
	if (!result)
2249
		return 0;
2250

2251
	if (idx < suc->update_data->list.nr) {
2252
		ce  = suc->update_data->list.entries[idx];
2253
		strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2254
			    ce->name);
2255
		strbuf_addch(err, '\n');
2256
		ALLOC_GROW(suc->failed_clones,
2257
			   suc->failed_clones_nr + 1,
2258
			   suc->failed_clones_alloc);
2259
		suc->failed_clones[suc->failed_clones_nr++] = ce;
2260
		return 0;
2261
	} else {
2262
		idx -= suc->update_data->list.nr;
2263
		ce  = suc->failed_clones[idx];
2264
		strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2265
			    ce->name);
2266
		strbuf_addch(err, '\n');
2267
		suc->quickstop = 1;
2268
		return 1;
2269
	}
2270

2271
	return 0;
2272
}
2273

2274
static int git_update_clone_config(const char *var, const char *value,
2275
				   const struct config_context *ctx,
2276
				   void *cb)
2277
{
2278
	int *max_jobs = cb;
2279

2280
	if (!strcmp(var, "submodule.fetchjobs"))
2281
		*max_jobs = parse_submodule_fetchjobs(var, value, ctx->kvi);
2282
	return 0;
2283
}
2284

2285
static int is_tip_reachable(const char *path, const struct object_id *oid)
2286
{
2287
	struct child_process cp = CHILD_PROCESS_INIT;
2288
	struct strbuf rev = STRBUF_INIT;
2289
	char *hex = oid_to_hex(oid);
2290
	int reachable;
2291

2292
	cp.git_cmd = 1;
2293
	cp.dir = path;
2294
	cp.no_stderr = 1;
2295
	strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2296

2297
	prepare_submodule_repo_env(&cp.env);
2298

2299
	if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2300
		reachable = 0;
2301
	else
2302
		reachable = 1;
2303

2304
	strbuf_release(&rev);
2305
	return reachable;
2306
}
2307

2308
static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2309
			      const struct object_id *oid)
2310
{
2311
	struct child_process cp = CHILD_PROCESS_INIT;
2312

2313
	prepare_submodule_repo_env(&cp.env);
2314
	cp.git_cmd = 1;
2315
	cp.dir = module_path;
2316

2317
	strvec_push(&cp.args, "fetch");
2318
	if (quiet)
2319
		strvec_push(&cp.args, "--quiet");
2320
	if (depth)
2321
		strvec_pushf(&cp.args, "--depth=%d", depth);
2322
	if (oid) {
2323
		char *hex = oid_to_hex(oid);
2324
		char *remote = get_default_remote();
2325

2326
		strvec_pushl(&cp.args, remote, hex, NULL);
2327
		free(remote);
2328
	}
2329

2330
	return run_command(&cp);
2331
}
2332

2333
static int run_update_command(const struct update_data *ud, int subforce)
2334
{
2335
	struct child_process cp = CHILD_PROCESS_INIT;
2336
	char *oid = oid_to_hex(&ud->oid);
2337
	int ret;
2338

2339
	switch (ud->update_strategy.type) {
2340
	case SM_UPDATE_CHECKOUT:
2341
		cp.git_cmd = 1;
2342
		strvec_pushl(&cp.args, "checkout", "-q", NULL);
2343
		if (subforce)
2344
			strvec_push(&cp.args, "-f");
2345
		break;
2346
	case SM_UPDATE_REBASE:
2347
		cp.git_cmd = 1;
2348
		strvec_push(&cp.args, "rebase");
2349
		if (ud->quiet)
2350
			strvec_push(&cp.args, "--quiet");
2351
		break;
2352
	case SM_UPDATE_MERGE:
2353
		cp.git_cmd = 1;
2354
		strvec_push(&cp.args, "merge");
2355
		if (ud->quiet)
2356
			strvec_push(&cp.args, "--quiet");
2357
		break;
2358
	case SM_UPDATE_COMMAND:
2359
		cp.use_shell = 1;
2360
		strvec_push(&cp.args, ud->update_strategy.command);
2361
		break;
2362
	default:
2363
		BUG("unexpected update strategy type: %d",
2364
		    ud->update_strategy.type);
2365
	}
2366
	strvec_push(&cp.args, oid);
2367

2368
	cp.dir = ud->sm_path;
2369
	prepare_submodule_repo_env(&cp.env);
2370
	if ((ret = run_command(&cp))) {
2371
		switch (ud->update_strategy.type) {
2372
		case SM_UPDATE_CHECKOUT:
2373
			die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2374
				    oid, ud->displaypath);
2375
			/* No "ret" assignment, use "git checkout"'s */
2376
			break;
2377
		case SM_UPDATE_REBASE:
2378
			ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2379
					  oid, ud->displaypath);
2380
			break;
2381
		case SM_UPDATE_MERGE:
2382
			ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2383
					  oid, ud->displaypath);
2384
			break;
2385
		case SM_UPDATE_COMMAND:
2386
			ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2387
					  ud->update_strategy.command, oid, ud->displaypath);
2388
			break;
2389
		default:
2390
			BUG("unexpected update strategy type: %d",
2391
			    ud->update_strategy.type);
2392
		}
2393

2394
		return ret;
2395
	}
2396

2397
	if (ud->quiet)
2398
		return 0;
2399

2400
	switch (ud->update_strategy.type) {
2401
	case SM_UPDATE_CHECKOUT:
2402
		printf(_("Submodule path '%s': checked out '%s'\n"),
2403
		       ud->displaypath, oid);
2404
		break;
2405
	case SM_UPDATE_REBASE:
2406
		printf(_("Submodule path '%s': rebased into '%s'\n"),
2407
		       ud->displaypath, oid);
2408
		break;
2409
	case SM_UPDATE_MERGE:
2410
		printf(_("Submodule path '%s': merged in '%s'\n"),
2411
		       ud->displaypath, oid);
2412
		break;
2413
	case SM_UPDATE_COMMAND:
2414
		printf(_("Submodule path '%s': '%s %s'\n"),
2415
		       ud->displaypath, ud->update_strategy.command, oid);
2416
		break;
2417
	default:
2418
		BUG("unexpected update strategy type: %d",
2419
		    ud->update_strategy.type);
2420
	}
2421

2422
	return 0;
2423
}
2424

2425
static int run_update_procedure(const struct update_data *ud)
2426
{
2427
	int subforce = is_null_oid(&ud->suboid) || ud->force;
2428

2429
	if (!ud->nofetch) {
2430
		/*
2431
		 * Run fetch only if `oid` isn't present or it
2432
		 * is not reachable from a ref.
2433
		 */
2434
		if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2435
		    fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2436
		    !ud->quiet)
2437
			fprintf_ln(stderr,
2438
				   _("Unable to fetch in submodule path '%s'; "
2439
				     "trying to directly fetch %s:"),
2440
				   ud->displaypath, oid_to_hex(&ud->oid));
2441
		/*
2442
		 * Now we tried the usual fetch, but `oid` may
2443
		 * not be reachable from any of the refs.
2444
		 */
2445
		if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2446
		    fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2447
			return die_message(_("Fetched in submodule path '%s', but it did not "
2448
					     "contain %s. Direct fetching of that commit failed."),
2449
					   ud->displaypath, oid_to_hex(&ud->oid));
2450
	}
2451

2452
	return run_update_command(ud, subforce);
2453
}
2454

2455
static int remote_submodule_branch(const char *path, const char **branch)
2456
{
2457
	const struct submodule *sub;
2458
	char *key;
2459
	*branch = NULL;
2460

2461
	sub = submodule_from_path(the_repository, null_oid(), path);
2462
	if (!sub)
2463
		return die_message(_("could not initialize submodule at path '%s'"),
2464
				   path);
2465

2466
	key = xstrfmt("submodule.%s.branch", sub->name);
2467
	if (repo_config_get_string_tmp(the_repository, key, branch))
2468
		*branch = sub->branch;
2469
	free(key);
2470

2471
	if (!*branch) {
2472
		*branch = "HEAD";
2473
		return 0;
2474
	}
2475

2476
	if (!strcmp(*branch, ".")) {
2477
		const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
2478
							      "HEAD", 0, NULL,
2479
							      NULL);
2480

2481
		if (!refname)
2482
			return die_message(_("No such ref: %s"), "HEAD");
2483

2484
		/* detached HEAD */
2485
		if (!strcmp(refname, "HEAD"))
2486
			return die_message(_("Submodule (%s) branch configured to inherit "
2487
					     "branch from superproject, but the superproject "
2488
					     "is not on any branch"), sub->name);
2489

2490
		if (!skip_prefix(refname, "refs/heads/", &refname))
2491
			return die_message(_("Expecting a full ref name, got %s"),
2492
					   refname);
2493

2494
		*branch = refname;
2495
		return 0;
2496
	}
2497

2498
	/* Our "branch" is coming from repo_config_get_string_tmp() */
2499
	return 0;
2500
}
2501

2502
static int ensure_core_worktree(const char *path)
2503
{
2504
	const char *cw;
2505
	struct repository subrepo;
2506

2507
	if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2508
		return die_message(_("could not get a repository handle for submodule '%s'"),
2509
				   path);
2510

2511
	if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2512
		char *cfg_file, *abs_path;
2513
		const char *rel_path;
2514
		struct strbuf sb = STRBUF_INIT;
2515

2516
		cfg_file = repo_git_path(&subrepo, "config");
2517

2518
		abs_path = absolute_pathdup(path);
2519
		rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2520

2521
		git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2522

2523
		free(cfg_file);
2524
		free(abs_path);
2525
		strbuf_release(&sb);
2526
	}
2527

2528
	repo_clear(&subrepo);
2529
	return 0;
2530
}
2531

2532
static const char *submodule_update_type_to_label(enum submodule_update_type type)
2533
{
2534
	switch (type) {
2535
	case SM_UPDATE_CHECKOUT:
2536
		return "checkout";
2537
	case SM_UPDATE_MERGE:
2538
		return "merge";
2539
	case SM_UPDATE_REBASE:
2540
		return "rebase";
2541
	case SM_UPDATE_UNSPECIFIED:
2542
	case SM_UPDATE_NONE:
2543
	case SM_UPDATE_COMMAND:
2544
		break;
2545
	}
2546
	BUG("unreachable with type %d", type);
2547
}
2548

2549
static void update_data_to_args(const struct update_data *update_data,
2550
				struct strvec *args)
2551
{
2552
	enum submodule_update_type update_type = update_data->update_default;
2553

2554
	strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2555
	if (update_data->displaypath)
2556
		strvec_pushf(args, "--super-prefix=%s/",
2557
			     update_data->displaypath);
2558
	strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2559
	if (update_data->quiet)
2560
		strvec_push(args, "--quiet");
2561
	if (update_data->force)
2562
		strvec_push(args, "--force");
2563
	if (update_data->init)
2564
		strvec_push(args, "--init");
2565
	if (update_data->remote)
2566
		strvec_push(args, "--remote");
2567
	if (update_data->nofetch)
2568
		strvec_push(args, "--no-fetch");
2569
	if (update_data->dissociate)
2570
		strvec_push(args, "--dissociate");
2571
	if (update_data->progress)
2572
		strvec_push(args, "--progress");
2573
	if (update_data->require_init)
2574
		strvec_push(args, "--require-init");
2575
	if (update_data->depth)
2576
		strvec_pushf(args, "--depth=%d", update_data->depth);
2577
	if (update_type != SM_UPDATE_UNSPECIFIED)
2578
		strvec_pushf(args, "--%s",
2579
			     submodule_update_type_to_label(update_type));
2580

2581
	if (update_data->references.nr) {
2582
		struct string_list_item *item;
2583

2584
		for_each_string_list_item(item, &update_data->references)
2585
			strvec_pushl(args, "--reference", item->string, NULL);
2586
	}
2587
	if (update_data->ref_storage_format != REF_STORAGE_FORMAT_UNKNOWN)
2588
		strvec_pushf(args, "--ref-format=%s",
2589
			     ref_storage_format_to_name(update_data->ref_storage_format));
2590
	if (update_data->filter_options && update_data->filter_options->choice)
2591
		strvec_pushf(args, "--filter=%s",
2592
				expand_list_objects_filter_spec(
2593
					update_data->filter_options));
2594
	if (update_data->recommend_shallow == 0)
2595
		strvec_push(args, "--no-recommend-shallow");
2596
	else if (update_data->recommend_shallow == 1)
2597
		strvec_push(args, "--recommend-shallow");
2598
	if (update_data->single_branch >= 0)
2599
		strvec_push(args, update_data->single_branch ?
2600
				    "--single-branch" :
2601
				    "--no-single-branch");
2602
}
2603

2604
static int update_submodule(struct update_data *update_data)
2605
{
2606
	int ret;
2607

2608
	if (validate_submodule_path(update_data->sm_path) < 0)
2609
		return -1;
2610

2611
	ret = determine_submodule_update_strategy(the_repository,
2612
						  update_data->just_cloned,
2613
						  update_data->sm_path,
2614
						  update_data->update_default,
2615
						  &update_data->update_strategy);
2616
	if (ret)
2617
		return ret;
2618

2619
	if (update_data->just_cloned)
2620
		oidcpy(&update_data->suboid, null_oid());
2621
	else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2622
					  "HEAD", &update_data->suboid))
2623
		return die_message(_("Unable to find current revision in submodule path '%s'"),
2624
				   update_data->displaypath);
2625

2626
	if (update_data->remote) {
2627
		char *remote_name;
2628
		const char *branch;
2629
		char *remote_ref;
2630
		int code;
2631

2632
		code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2633
		if (code)
2634
			return code;
2635
		code = remote_submodule_branch(update_data->sm_path, &branch);
2636
		if (code)
2637
			return code;
2638
		remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2639

2640
		free(remote_name);
2641

2642
		if (!update_data->nofetch) {
2643
			if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2644
					      0, NULL))
2645
				return die_message(_("Unable to fetch in submodule path '%s'"),
2646
						   update_data->sm_path);
2647
		}
2648

2649
		if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path,
2650
					     remote_ref, &update_data->oid))
2651
			return die_message(_("Unable to find %s revision in submodule path '%s'"),
2652
					   remote_ref, update_data->sm_path);
2653

2654
		free(remote_ref);
2655
	}
2656

2657
	if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2658
		ret = run_update_procedure(update_data);
2659
		if (ret)
2660
			return ret;
2661
	}
2662

2663
	if (update_data->recursive) {
2664
		struct child_process cp = CHILD_PROCESS_INIT;
2665
		struct update_data next = *update_data;
2666

2667
		next.prefix = NULL;
2668
		oidcpy(&next.oid, null_oid());
2669
		oidcpy(&next.suboid, null_oid());
2670

2671
		cp.dir = update_data->sm_path;
2672
		cp.git_cmd = 1;
2673
		prepare_submodule_repo_env(&cp.env);
2674
		update_data_to_args(&next, &cp.args);
2675

2676
		ret = run_command(&cp);
2677
		if (ret)
2678
			die_message(_("Failed to recurse into submodule path '%s'"),
2679
				    update_data->displaypath);
2680
		return ret;
2681
	}
2682

2683
	return 0;
2684
}
2685

2686
static int update_submodules(struct update_data *update_data)
2687
{
2688
	int i, ret = 0;
2689
	struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2690
	const struct run_process_parallel_opts opts = {
2691
		.tr2_category = "submodule",
2692
		.tr2_label = "parallel/update",
2693

2694
		.processes = update_data->max_jobs,
2695

2696
		.get_next_task = update_clone_get_next_task,
2697
		.start_failure = update_clone_start_failure,
2698
		.task_finished = update_clone_task_finished,
2699
		.data = &suc,
2700
	};
2701

2702
	suc.update_data = update_data;
2703
	run_processes_parallel(&opts);
2704

2705
	/*
2706
	 * We saved the output and put it out all at once now.
2707
	 * That means:
2708
	 * - the listener does not have to interleave their (checkout)
2709
	 *   work with our fetching.  The writes involved in a
2710
	 *   checkout involve more straightforward sequential I/O.
2711
	 * - the listener can avoid doing any work if fetching failed.
2712
	 */
2713
	if (suc.quickstop) {
2714
		ret = 1;
2715
		goto cleanup;
2716
	}
2717

2718
	for (i = 0; i < suc.update_clone_nr; i++) {
2719
		struct update_clone_data ucd = suc.update_clone[i];
2720
		int code = 128;
2721

2722
		oidcpy(&update_data->oid, &ucd.oid);
2723
		update_data->just_cloned = ucd.just_cloned;
2724
		update_data->sm_path = ucd.sub->path;
2725

2726
		/*
2727
		 * Verify that the submodule path does not contain any
2728
		 * symlinks; if it does, it might have been tampered with.
2729
		 * TODO: allow exempting it via
2730
		 * `safe.submodule.path` or something
2731
		 */
2732
		if (validate_submodule_path(update_data->sm_path) < 0)
2733
			goto fail;
2734

2735
		code = ensure_core_worktree(update_data->sm_path);
2736
		if (code)
2737
			goto fail;
2738

2739
		update_data->displaypath = get_submodule_displaypath(
2740
			update_data->sm_path, update_data->prefix,
2741
			update_data->super_prefix);
2742
		code = update_submodule(update_data);
2743
		FREE_AND_NULL(update_data->displaypath);
2744
fail:
2745
		if (!code)
2746
			continue;
2747
		ret = code;
2748
		if (ret == 128)
2749
			goto cleanup;
2750
	}
2751

2752
cleanup:
2753
	submodule_update_clone_release(&suc);
2754
	string_list_clear(&update_data->references, 0);
2755
	return ret;
2756
}
2757

2758
static int module_update(int argc, const char **argv, const char *prefix)
2759
{
2760
	struct pathspec pathspec = { 0 };
2761
	struct pathspec pathspec2 = { 0 };
2762
	struct update_data opt = UPDATE_DATA_INIT;
2763
	struct list_objects_filter_options filter_options =
2764
		LIST_OBJECTS_FILTER_INIT;
2765
	const char *ref_storage_format = NULL;
2766
	int ret;
2767
	struct option module_update_options[] = {
2768
		OPT__SUPER_PREFIX(&opt.super_prefix),
2769
		OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2770
		OPT_BOOL(0, "init", &opt.init,
2771
			 N_("initialize uninitialized submodules before update")),
2772
		OPT_BOOL(0, "remote", &opt.remote,
2773
			 N_("use SHA-1 of submodule's remote tracking branch")),
2774
		OPT_BOOL(0, "recursive", &opt.recursive,
2775
			 N_("traverse submodules recursively")),
2776
		OPT_BOOL('N', "no-fetch", &opt.nofetch,
2777
			 N_("don't fetch new objects from the remote site")),
2778
		OPT_SET_INT(0, "checkout", &opt.update_default,
2779
			N_("use the 'checkout' update strategy (default)"),
2780
			SM_UPDATE_CHECKOUT),
2781
		OPT_SET_INT('m', "merge", &opt.update_default,
2782
			N_("use the 'merge' update strategy"),
2783
			SM_UPDATE_MERGE),
2784
		OPT_SET_INT('r', "rebase", &opt.update_default,
2785
			N_("use the 'rebase' update strategy"),
2786
			SM_UPDATE_REBASE),
2787
		OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2788
			   N_("reference repository")),
2789
		OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
2790
			   N_("specify the reference format to use")),
2791
		OPT_BOOL(0, "dissociate", &opt.dissociate,
2792
			   N_("use --reference only while cloning")),
2793
		OPT_INTEGER(0, "depth", &opt.depth,
2794
			   N_("create a shallow clone truncated to the "
2795
			      "specified number of revisions")),
2796
		OPT_INTEGER('j', "jobs", &opt.max_jobs,
2797
			    N_("parallel jobs")),
2798
		OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2799
			    N_("whether the initial clone should follow the shallow recommendation")),
2800
		OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2801
		OPT_BOOL(0, "progress", &opt.progress,
2802
			    N_("force cloning progress")),
2803
		OPT_BOOL(0, "require-init", &opt.require_init,
2804
			   N_("disallow cloning into non-empty directory, implies --init")),
2805
		OPT_BOOL(0, "single-branch", &opt.single_branch,
2806
			 N_("clone only one branch, HEAD or --branch")),
2807
		OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2808
		OPT_END()
2809
	};
2810
	const char *const git_submodule_helper_usage[] = {
2811
		N_("git submodule [--quiet] update"
2812
		" [--init [--filter=<filter-spec>]] [--remote]"
2813
		" [-N|--no-fetch] [-f|--force]"
2814
		" [--checkout|--merge|--rebase]"
2815
		" [--[no-]recommend-shallow] [--reference <repository>]"
2816
		" [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2817
		NULL
2818
	};
2819

2820
	update_clone_config_from_gitmodules(&opt.max_jobs);
2821
	git_config(git_update_clone_config, &opt.max_jobs);
2822

2823
	argc = parse_options(argc, argv, prefix, module_update_options,
2824
			     git_submodule_helper_usage, 0);
2825

2826
	if (opt.require_init)
2827
		opt.init = 1;
2828

2829
	if (filter_options.choice && !opt.init) {
2830
		usage_with_options(git_submodule_helper_usage,
2831
				   module_update_options);
2832
	}
2833

2834
	if (ref_storage_format) {
2835
		opt.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
2836
		if (opt.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
2837
			die(_("unknown ref storage format '%s'"), ref_storage_format);
2838
	}
2839

2840
	opt.filter_options = &filter_options;
2841
	opt.prefix = prefix;
2842

2843
	if (opt.update_default)
2844
		opt.update_strategy.type = opt.update_default;
2845

2846
	if (module_list_compute(argv, prefix, &pathspec, &opt.list) < 0) {
2847
		ret = 1;
2848
		goto cleanup;
2849
	}
2850

2851
	if (pathspec.nr)
2852
		opt.warn_if_uninitialized = 1;
2853

2854
	if (opt.init) {
2855
		struct module_list list = MODULE_LIST_INIT;
2856
		struct init_cb info = INIT_CB_INIT;
2857

2858
		if (module_list_compute(argv, opt.prefix,
2859
					&pathspec2, &list) < 0) {
2860
			module_list_release(&list);
2861
			ret = 1;
2862
			goto cleanup;
2863
		}
2864

2865
		/*
2866
		 * If there are no path args and submodule.active is set then,
2867
		 * by default, only initialize 'active' modules.
2868
		 */
2869
		if (!argc && !git_config_get("submodule.active"))
2870
			module_list_active(&list);
2871

2872
		info.prefix = opt.prefix;
2873
		info.super_prefix = opt.super_prefix;
2874
		if (opt.quiet)
2875
			info.flags |= OPT_QUIET;
2876

2877
		for_each_listed_submodule(&list, init_submodule_cb, &info);
2878
		module_list_release(&list);
2879
	}
2880

2881
	ret = update_submodules(&opt);
2882
cleanup:
2883
	update_data_release(&opt);
2884
	list_objects_filter_release(&filter_options);
2885
	clear_pathspec(&pathspec);
2886
	clear_pathspec(&pathspec2);
2887
	return ret;
2888
}
2889

2890
static int push_check(int argc, const char **argv, const char *prefix UNUSED)
2891
{
2892
	struct remote *remote;
2893
	const char *superproject_head;
2894
	char *head;
2895
	int detached_head = 0;
2896
	struct object_id head_oid;
2897

2898
	if (argc < 3)
2899
		die("submodule--helper push-check requires at least 2 arguments");
2900

2901
	/*
2902
	 * superproject's resolved head ref.
2903
	 * if HEAD then the superproject is in a detached head state, otherwise
2904
	 * it will be the resolved head ref.
2905
	 */
2906
	superproject_head = argv[1];
2907
	argv++;
2908
	argc--;
2909
	/* Get the submodule's head ref and determine if it is detached */
2910
	head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD",
2911
				   0, &head_oid, NULL);
2912
	if (!head)
2913
		die(_("Failed to resolve HEAD as a valid ref."));
2914
	if (!strcmp(head, "HEAD"))
2915
		detached_head = 1;
2916

2917
	/*
2918
	 * The remote must be configured.
2919
	 * This is to avoid pushing to the exact same URL as the parent.
2920
	 */
2921
	remote = pushremote_get(argv[1]);
2922
	if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2923
		die("remote '%s' not configured", argv[1]);
2924

2925
	/* Check the refspec */
2926
	if (argc > 2) {
2927
		int i;
2928
		struct ref *local_refs = get_local_heads();
2929
		struct refspec refspec = REFSPEC_INIT_PUSH;
2930

2931
		refspec_appendn(&refspec, argv + 2, argc - 2);
2932

2933
		for (i = 0; i < refspec.nr; i++) {
2934
			const struct refspec_item *rs = &refspec.items[i];
2935

2936
			if (rs->pattern || rs->matching)
2937
				continue;
2938

2939
			/* LHS must match a single ref */
2940
			switch (count_refspec_match(rs->src, local_refs, NULL)) {
2941
			case 1:
2942
				break;
2943
			case 0:
2944
				/*
2945
				 * If LHS matches 'HEAD' then we need to ensure
2946
				 * that it matches the same named branch
2947
				 * checked out in the superproject.
2948
				 */
2949
				if (!strcmp(rs->src, "HEAD")) {
2950
					if (!detached_head &&
2951
					    !strcmp(head, superproject_head))
2952
						break;
2953
					die("HEAD does not match the named branch in the superproject");
2954
				}
2955
				/* fallthrough */
2956
			default:
2957
				die("src refspec '%s' must name a ref",
2958
				    rs->src);
2959
			}
2960
		}
2961
		refspec_clear(&refspec);
2962
	}
2963
	free(head);
2964

2965
	return 0;
2966
}
2967

2968
static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2969
{
2970
	int i;
2971
	struct pathspec pathspec = { 0 };
2972
	struct module_list list = MODULE_LIST_INIT;
2973
	const char *super_prefix = NULL;
2974
	struct option embed_gitdir_options[] = {
2975
		OPT__SUPER_PREFIX(&super_prefix),
2976
		OPT_END()
2977
	};
2978
	const char *const git_submodule_helper_usage[] = {
2979
		N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2980
		NULL
2981
	};
2982
	int ret = 1;
2983

2984
	argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2985
			     git_submodule_helper_usage, 0);
2986

2987
	if (module_list_compute(argv, prefix, &pathspec, &list) < 0)
2988
		goto cleanup;
2989

2990
	for (i = 0; i < list.nr; i++)
2991
		absorb_git_dir_into_superproject(list.entries[i]->name,
2992
						 super_prefix);
2993

2994
	ret = 0;
2995
cleanup:
2996
	clear_pathspec(&pathspec);
2997
	module_list_release(&list);
2998
	return ret;
2999
}
3000

3001
static int module_set_url(int argc, const char **argv, const char *prefix)
3002
{
3003
	int quiet = 0, ret;
3004
	const char *newurl;
3005
	const char *path;
3006
	char *config_name;
3007
	struct option options[] = {
3008
		OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
3009
		OPT_END()
3010
	};
3011
	const char *const usage[] = {
3012
		N_("git submodule set-url [--quiet] <path> <newurl>"),
3013
		NULL
3014
	};
3015
	const struct submodule *sub;
3016

3017
	argc = parse_options(argc, argv, prefix, options, usage, 0);
3018

3019
	if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
3020
		usage_with_options(usage, options);
3021

3022
	sub = submodule_from_path(the_repository, null_oid(), path);
3023

3024
	if (!sub)
3025
		die(_("no submodule mapping found in .gitmodules for path '%s'"),
3026
		    path);
3027

3028
	config_name = xstrfmt("submodule.%s.url", sub->name);
3029
	ret = config_set_in_gitmodules_file_gently(config_name, newurl);
3030

3031
	if (!ret) {
3032
		repo_read_gitmodules(the_repository, 0);
3033
		sync_submodule(sub->path, prefix, NULL, quiet ? OPT_QUIET : 0);
3034
	}
3035

3036
	free(config_name);
3037
	return !!ret;
3038
}
3039

3040
static int module_set_branch(int argc, const char **argv, const char *prefix)
3041
{
3042
	int opt_default = 0, ret;
3043
	const char *opt_branch = NULL;
3044
	const char *path;
3045
	char *config_name;
3046
	struct option options[] = {
3047
		/*
3048
		 * We accept the `quiet` option for uniformity across subcommands,
3049
		 * though there is nothing to make less verbose in this subcommand.
3050
		 */
3051
		OPT_NOOP_NOARG('q', "quiet"),
3052

3053
		OPT_BOOL('d', "default", &opt_default,
3054
			N_("set the default tracking branch to master")),
3055
		OPT_STRING('b', "branch", &opt_branch, N_("branch"),
3056
			N_("set the default tracking branch")),
3057
		OPT_END()
3058
	};
3059
	const char *const usage[] = {
3060
		N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
3061
		N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
3062
		NULL
3063
	};
3064
	const struct submodule *sub;
3065

3066
	argc = parse_options(argc, argv, prefix, options, usage, 0);
3067

3068
	if (!opt_branch && !opt_default)
3069
		die(_("--branch or --default required"));
3070

3071
	if (opt_branch && opt_default)
3072
		die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
3073

3074
	if (argc != 1 || !(path = argv[0]))
3075
		usage_with_options(usage, options);
3076

3077
	sub = submodule_from_path(the_repository, null_oid(), path);
3078

3079
	if (!sub)
3080
		die(_("no submodule mapping found in .gitmodules for path '%s'"),
3081
		    path);
3082

3083
	config_name = xstrfmt("submodule.%s.branch", sub->name);
3084
	ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
3085

3086
	free(config_name);
3087
	return !!ret;
3088
}
3089

3090
static int module_create_branch(int argc, const char **argv, const char *prefix)
3091
{
3092
	enum branch_track track;
3093
	int quiet = 0, force = 0, reflog = 0, dry_run = 0;
3094
	struct option options[] = {
3095
		OPT__QUIET(&quiet, N_("print only error messages")),
3096
		OPT__FORCE(&force, N_("force creation"), 0),
3097
		OPT_BOOL(0, "create-reflog", &reflog,
3098
			 N_("create the branch's reflog")),
3099
		OPT_CALLBACK_F('t', "track",  &track, "(direct|inherit)",
3100
			N_("set branch tracking configuration"),
3101
			PARSE_OPT_OPTARG,
3102
			parse_opt_tracking_mode),
3103
		OPT__DRY_RUN(&dry_run,
3104
			     N_("show whether the branch would be created")),
3105
		OPT_END()
3106
	};
3107
	const char *const usage[] = {
3108
		N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
3109
		NULL
3110
	};
3111

3112
	git_config(git_default_config, NULL);
3113
	track = git_branch_track;
3114
	argc = parse_options(argc, argv, prefix, options, usage, 0);
3115

3116
	if (argc != 3)
3117
		usage_with_options(usage, options);
3118

3119
	if (!quiet && !dry_run)
3120
		printf_ln(_("creating branch '%s'"), argv[0]);
3121

3122
	create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
3123
				    force, reflog, quiet, track, dry_run);
3124
	return 0;
3125
}
3126

3127
struct add_data {
3128
	const char *prefix;
3129
	const char *branch;
3130
	const char *reference_path;
3131
	char *sm_path;
3132
	const char *sm_name;
3133
	const char *repo;
3134
	const char *realrepo;
3135
	enum ref_storage_format ref_storage_format;
3136
	int depth;
3137
	unsigned int force: 1;
3138
	unsigned int quiet: 1;
3139
	unsigned int progress: 1;
3140
	unsigned int dissociate: 1;
3141
};
3142
#define ADD_DATA_INIT { \
3143
	.depth = -1, \
3144
	.ref_storage_format = REF_STORAGE_FORMAT_UNKNOWN, \
3145
}
3146

3147
static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3148
{
3149
	struct child_process cp_remote = CHILD_PROCESS_INIT;
3150
	struct strbuf sb_remote_out = STRBUF_INIT;
3151

3152
	cp_remote.git_cmd = 1;
3153
	strvec_pushf(&cp_remote.env,
3154
		     "GIT_DIR=%s", git_dir_path);
3155
	strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3156
	strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3157
	if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3158
		char *next_line;
3159
		char *line = sb_remote_out.buf;
3160

3161
		while ((next_line = strchr(line, '\n')) != NULL) {
3162
			size_t len = next_line - line;
3163

3164
			if (strip_suffix_mem(line, &len, " (fetch)"))
3165
				strbuf_addf(msg, "  %.*s\n", (int)len, line);
3166
			line = next_line + 1;
3167
		}
3168
	}
3169

3170
	strbuf_release(&sb_remote_out);
3171
}
3172

3173
static int add_submodule(const struct add_data *add_data)
3174
{
3175
	char *submod_gitdir_path;
3176
	struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3177
	struct string_list reference = STRING_LIST_INIT_NODUP;
3178
	int ret = -1;
3179

3180
	/* perhaps the path already exists and is already a git repo, else clone it */
3181
	if (is_directory(add_data->sm_path)) {
3182
		struct strbuf sm_path = STRBUF_INIT;
3183
		strbuf_addstr(&sm_path, add_data->sm_path);
3184
		submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3185
		if (is_nonbare_repository_dir(&sm_path))
3186
			printf(_("Adding existing repo at '%s' to the index\n"),
3187
			       add_data->sm_path);
3188
		else
3189
			die(_("'%s' already exists and is not a valid git repo"),
3190
			    add_data->sm_path);
3191
		strbuf_release(&sm_path);
3192
		free(submod_gitdir_path);
3193
	} else {
3194
		struct child_process cp = CHILD_PROCESS_INIT;
3195

3196
		submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3197

3198
		if (is_directory(submod_gitdir_path)) {
3199
			if (!add_data->force) {
3200
				struct strbuf msg = STRBUF_INIT;
3201
				char *die_msg;
3202

3203
				strbuf_addf(&msg, _("A git directory for '%s' is found "
3204
						    "locally with remote(s):\n"),
3205
					    add_data->sm_name);
3206

3207
				append_fetch_remotes(&msg, submod_gitdir_path);
3208
				free(submod_gitdir_path);
3209

3210
				strbuf_addf(&msg, _("If you want to reuse this local git "
3211
						    "directory instead of cloning again from\n"
3212
						    "  %s\n"
3213
						    "use the '--force' option. If the local git "
3214
						    "directory is not the correct repo\n"
3215
						    "or you are unsure what this means choose "
3216
						    "another name with the '--name' option."),
3217
					    add_data->realrepo);
3218

3219
				die_msg = strbuf_detach(&msg, NULL);
3220
				die("%s", die_msg);
3221
			} else {
3222
				printf(_("Reactivating local git directory for "
3223
					 "submodule '%s'\n"), add_data->sm_name);
3224
			}
3225
		}
3226
		free(submod_gitdir_path);
3227

3228
		clone_data.prefix = add_data->prefix;
3229
		clone_data.path = add_data->sm_path;
3230
		clone_data.name = add_data->sm_name;
3231
		clone_data.url = add_data->realrepo;
3232
		clone_data.quiet = add_data->quiet;
3233
		clone_data.progress = add_data->progress;
3234
		if (add_data->reference_path) {
3235
			char *p = xstrdup(add_data->reference_path);
3236

3237
			string_list_append(&reference, p)->util = p;
3238
		}
3239
		clone_data.ref_storage_format = add_data->ref_storage_format;
3240
		clone_data.dissociate = add_data->dissociate;
3241
		if (add_data->depth >= 0)
3242
			clone_data.depth = add_data->depth;
3243

3244
		if (clone_submodule(&clone_data, &reference))
3245
			goto cleanup;
3246

3247
		prepare_submodule_repo_env(&cp.env);
3248
		cp.git_cmd = 1;
3249
		cp.dir = add_data->sm_path;
3250
		/*
3251
		 * NOTE: we only get here if add_data->force is true, so
3252
		 * passing --force to checkout is reasonable.
3253
		 */
3254
		strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3255

3256
		if (add_data->branch) {
3257
			strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3258
			strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3259
		}
3260

3261
		if (run_command(&cp))
3262
			die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3263
	}
3264
	ret = 0;
3265

3266
cleanup:
3267
	string_list_clear(&reference, 1);
3268
	return ret;
3269
}
3270

3271
static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3272
{
3273
	char *key;
3274
	int ret;
3275

3276
	if (!is_writing_gitmodules_ok())
3277
		die(_("please make sure that the .gitmodules file is in the working tree"));
3278

3279
	key = xstrfmt("submodule.%s.%s", name, var);
3280
	ret = config_set_in_gitmodules_file_gently(key, value);
3281
	free(key);
3282

3283
	return ret;
3284
}
3285

3286
static void configure_added_submodule(struct add_data *add_data)
3287
{
3288
	char *key;
3289
	struct child_process add_submod = CHILD_PROCESS_INIT;
3290
	struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3291

3292
	key = xstrfmt("submodule.%s.url", add_data->sm_name);
3293
	git_config_set_gently(key, add_data->realrepo);
3294
	free(key);
3295

3296
	add_submod.git_cmd = 1;
3297
	strvec_pushl(&add_submod.args, "add",
3298
		     "--no-warn-embedded-repo", NULL);
3299
	if (add_data->force)
3300
		strvec_push(&add_submod.args, "--force");
3301
	strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3302

3303
	if (run_command(&add_submod))
3304
		die(_("Failed to add submodule '%s'"), add_data->sm_path);
3305

3306
	if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3307
	    config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3308
		die(_("Failed to register submodule '%s'"), add_data->sm_path);
3309

3310
	if (add_data->branch) {
3311
		if (config_submodule_in_gitmodules(add_data->sm_name,
3312
						   "branch", add_data->branch))
3313
			die(_("Failed to register submodule '%s'"), add_data->sm_path);
3314
	}
3315

3316
	add_gitmodules.git_cmd = 1;
3317
	strvec_pushl(&add_gitmodules.args,
3318
		     "add", "--force", "--", ".gitmodules", NULL);
3319

3320
	if (run_command(&add_gitmodules))
3321
		die(_("Failed to register submodule '%s'"), add_data->sm_path);
3322

3323
	/*
3324
	 * NEEDSWORK: In a multi-working-tree world this needs to be
3325
	 * set in the per-worktree config.
3326
	 */
3327
	/*
3328
	 * NEEDSWORK: In the longer run, we need to get rid of this
3329
	 * pattern of querying "submodule.active" before calling
3330
	 * is_submodule_active(), since that function needs to find
3331
	 * out the value of "submodule.active" again anyway.
3332
	 */
3333
	if (!git_config_get("submodule.active")) {
3334
		/*
3335
		 * If the submodule being added isn't already covered by the
3336
		 * current configured pathspec, set the submodule's active flag
3337
		 */
3338
		if (!is_submodule_active(the_repository, add_data->sm_path)) {
3339
			key = xstrfmt("submodule.%s.active", add_data->sm_name);
3340
			git_config_set_gently(key, "true");
3341
			free(key);
3342
		}
3343
	} else {
3344
		key = xstrfmt("submodule.%s.active", add_data->sm_name);
3345
		git_config_set_gently(key, "true");
3346
		free(key);
3347
	}
3348
}
3349

3350
static void die_on_index_match(const char *path, int force)
3351
{
3352
	struct pathspec ps;
3353
	const char *args[] = { path, NULL };
3354
	parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3355

3356
	if (repo_read_index_preload(the_repository, NULL, 0) < 0)
3357
		die(_("index file corrupt"));
3358

3359
	if (ps.nr) {
3360
		int i;
3361
		char *ps_matched = xcalloc(ps.nr, 1);
3362

3363
		/* TODO: audit for interaction with sparse-index. */
3364
		ensure_full_index(the_repository->index);
3365

3366
		/*
3367
		 * Since there is only one pathspec, we just need to
3368
		 * check ps_matched[0] to know if a cache entry matched.
3369
		 */
3370
		for (i = 0; i < the_repository->index->cache_nr; i++) {
3371
			ce_path_match(the_repository->index, the_repository->index->cache[i], &ps,
3372
				      ps_matched);
3373

3374
			if (ps_matched[0]) {
3375
				if (!force)
3376
					die(_("'%s' already exists in the index"),
3377
					    path);
3378
				if (!S_ISGITLINK(the_repository->index->cache[i]->ce_mode))
3379
					die(_("'%s' already exists in the index "
3380
					      "and is not a submodule"), path);
3381
				break;
3382
			}
3383
		}
3384
		free(ps_matched);
3385
	}
3386
	clear_pathspec(&ps);
3387
}
3388

3389
static void die_on_repo_without_commits(const char *path)
3390
{
3391
	struct strbuf sb = STRBUF_INIT;
3392
	strbuf_addstr(&sb, path);
3393
	if (is_nonbare_repository_dir(&sb)) {
3394
		struct object_id oid;
3395
		if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0)
3396
			die(_("'%s' does not have a commit checked out"), path);
3397
	}
3398
	strbuf_release(&sb);
3399
}
3400

3401
static int module_add(int argc, const char **argv, const char *prefix)
3402
{
3403
	int force = 0, quiet = 0, progress = 0, dissociate = 0;
3404
	struct add_data add_data = ADD_DATA_INIT;
3405
	const char *ref_storage_format = NULL;
3406
	char *to_free = NULL;
3407
	struct option options[] = {
3408
		OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3409
			   N_("branch of repository to add as submodule")),
3410
		OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3411
			   PARSE_OPT_NOCOMPLETE),
3412
		OPT__QUIET(&quiet, N_("print only error messages")),
3413
		OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3414
		OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3415
			   N_("reference repository")),
3416
		OPT_STRING(0, "ref-format", &ref_storage_format, N_("format"),
3417
			   N_("specify the reference format to use")),
3418
		OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3419
		OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3420
			   N_("sets the submodule's name to the given string "
3421
			      "instead of defaulting to its path")),
3422
		OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3423
		OPT_END()
3424
	};
3425
	const char *const usage[] = {
3426
		N_("git submodule add [<options>] [--] <repository> [<path>]"),
3427
		NULL
3428
	};
3429
	struct strbuf sb = STRBUF_INIT;
3430
	int ret = 1;
3431

3432
	argc = parse_options(argc, argv, prefix, options, usage, 0);
3433

3434
	if (!is_writing_gitmodules_ok())
3435
		die(_("please make sure that the .gitmodules file is in the working tree"));
3436

3437
	if (prefix && *prefix &&
3438
	    add_data.reference_path && !is_absolute_path(add_data.reference_path))
3439
		add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3440

3441
	if (argc == 0 || argc > 2)
3442
		usage_with_options(usage, options);
3443

3444
	if (ref_storage_format) {
3445
		add_data.ref_storage_format = ref_storage_format_by_name(ref_storage_format);
3446
		if (add_data.ref_storage_format == REF_STORAGE_FORMAT_UNKNOWN)
3447
			die(_("unknown ref storage format '%s'"), ref_storage_format);
3448
	}
3449

3450
	add_data.repo = argv[0];
3451
	if (argc == 1)
3452
		add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3453
	else
3454
		add_data.sm_path = xstrdup(argv[1]);
3455

3456
	if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3457
		char *sm_path = add_data.sm_path;
3458

3459
		add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3460
		free(sm_path);
3461
	}
3462

3463
	if (starts_with_dot_dot_slash(add_data.repo) ||
3464
	    starts_with_dot_slash(add_data.repo)) {
3465
		if (prefix)
3466
			die(_("Relative path can only be used from the toplevel "
3467
			      "of the working tree"));
3468

3469
		/* dereference source url relative to parent's url */
3470
		to_free = resolve_relative_url(add_data.repo, NULL, 1);
3471
		add_data.realrepo = to_free;
3472
	} else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3473
		add_data.realrepo = add_data.repo;
3474
	} else {
3475
		die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3476
		    add_data.repo);
3477
	}
3478

3479
	/*
3480
	 * normalize path:
3481
	 * multiple //; leading ./; /./; /../;
3482
	 */
3483
	normalize_path_copy(add_data.sm_path, add_data.sm_path);
3484
	strip_dir_trailing_slashes(add_data.sm_path);
3485

3486
	if (validate_submodule_path(add_data.sm_path) < 0)
3487
		exit(128);
3488

3489
	die_on_index_match(add_data.sm_path, force);
3490
	die_on_repo_without_commits(add_data.sm_path);
3491

3492
	if (!force) {
3493
		struct child_process cp = CHILD_PROCESS_INIT;
3494

3495
		cp.git_cmd = 1;
3496
		cp.no_stdout = 1;
3497
		strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3498
			     "--no-warn-embedded-repo", add_data.sm_path, NULL);
3499
		if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3500
			strbuf_complete_line(&sb);
3501
			fputs(sb.buf, stderr);
3502
			goto cleanup;
3503
		}
3504
	}
3505

3506
	if(!add_data.sm_name)
3507
		add_data.sm_name = add_data.sm_path;
3508

3509
	if (check_submodule_name(add_data.sm_name))
3510
		die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3511

3512
	add_data.prefix = prefix;
3513
	add_data.force = !!force;
3514
	add_data.quiet = !!quiet;
3515
	add_data.progress = !!progress;
3516
	add_data.dissociate = !!dissociate;
3517

3518
	if (add_submodule(&add_data))
3519
		goto cleanup;
3520
	configure_added_submodule(&add_data);
3521

3522
	ret = 0;
3523
cleanup:
3524
	free(add_data.sm_path);
3525
	free(to_free);
3526
	strbuf_release(&sb);
3527

3528
	return ret;
3529
}
3530

3531
int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3532
{
3533
	parse_opt_subcommand_fn *fn = NULL;
3534
	const char *const usage[] = {
3535
		N_("git submodule--helper <command>"),
3536
		NULL
3537
	};
3538
	struct option options[] = {
3539
		OPT_SUBCOMMAND("clone", &fn, module_clone),
3540
		OPT_SUBCOMMAND("add", &fn, module_add),
3541
		OPT_SUBCOMMAND("update", &fn, module_update),
3542
		OPT_SUBCOMMAND("foreach", &fn, module_foreach),
3543
		OPT_SUBCOMMAND("init", &fn, module_init),
3544
		OPT_SUBCOMMAND("status", &fn, module_status),
3545
		OPT_SUBCOMMAND("sync", &fn, module_sync),
3546
		OPT_SUBCOMMAND("deinit", &fn, module_deinit),
3547
		OPT_SUBCOMMAND("summary", &fn, module_summary),
3548
		OPT_SUBCOMMAND("push-check", &fn, push_check),
3549
		OPT_SUBCOMMAND("absorbgitdirs", &fn, absorb_git_dirs),
3550
		OPT_SUBCOMMAND("set-url", &fn, module_set_url),
3551
		OPT_SUBCOMMAND("set-branch", &fn, module_set_branch),
3552
		OPT_SUBCOMMAND("create-branch", &fn, module_create_branch),
3553
		OPT_END()
3554
	};
3555
	argc = parse_options(argc, argv, prefix, options, usage, 0);
3556

3557
	return fn(argc, argv, prefix);
3558
}
3559

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

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

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

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