]> git.ipfire.org Git - thirdparty/git.git/blame - submodule.c
Git 2.45-rc0
[thirdparty/git.git] / submodule.c
CommitLineData
bc5c5ec0 1#include "git-compat-util.h"
0b027f6c 2#include "abspath.h"
69aba532 3#include "repository.h"
b2141fc1 4#include "config.h"
851e18c3 5#include "submodule-config.h"
752c0c24
JS
6#include "submodule.h"
7#include "dir.h"
8#include "diff.h"
9#include "commit.h"
32a8f510 10#include "environment.h"
f394e093 11#include "gettext.h"
41771fa4 12#include "hex.h"
752c0c24 13#include "revision.h"
ee6fc514 14#include "run-command.h"
c7e1a736 15#include "diffcore.h"
68d03e4a 16#include "refs.h"
aee9c7d6 17#include "string-list.h"
fe299ec5 18#include "oid-array.h"
dbbcd44f 19#include "strvec.h"
fe85ee6e 20#include "thread-utils.h"
c339932b 21#include "path.h"
06bf4ad1 22#include "remote.h"
f6f85861 23#include "worktree.h"
046b4823 24#include "parse-options.h"
87bed179 25#include "object-file.h"
dabab1d6 26#include "object-name.h"
a034e910 27#include "object-store-ll.h"
64043556 28#include "commit-reach.h"
08c46a49 29#include "read-cache-ll.h"
e38da487 30#include "setup.h"
74ea5c95 31#include "trace2.h"
aee9c7d6 32
d7a3803f 33static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
6859de45 34static int initialized_fetch_ref_tips;
910650d2 35static struct oid_array ref_tips_before_fetch;
36static struct oid_array ref_tips_after_fetch;
6859de45 37
d4e98b58 38/*
34e2ba04
BW
39 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
40 * will be disabled because we can't guess what might be configured in
41 * .gitmodules unless the user resolves the conflict.
d4e98b58 42 */
847a9e5d 43int is_gitmodules_unmerged(struct index_state *istate)
34e2ba04
BW
44{
45 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
46 if (pos < 0) { /* .gitmodules not found or isn't merged */
47 pos = -1 - pos;
48 if (istate->cache_nr > pos) { /* there is a .gitmodules */
49 const struct cache_entry *ce = istate->cache[pos];
50 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
51 !strcmp(ce->name, GITMODULES_FILE))
52 return 1;
53 }
54 }
55
56 return 0;
57}
752c0c24 58
b5c259f2
AO
59/*
60 * Check if the .gitmodules file is safe to write.
61 *
62 * Writing to the .gitmodules file requires that the file exists in the
63 * working tree or, if it doesn't, that a brand new .gitmodules file is going
64 * to be created (i.e. it's neither in the index nor in the current branch).
65 *
66 * It is not safe to write to .gitmodules if it's not in the working tree but
67 * it is in the index or in the current branch, because writing new values
68 * (and staging them) would blindly overwrite ALL the old content.
69 */
70int is_writing_gitmodules_ok(void)
71{
72 struct object_id oid;
73 return file_exists(GITMODULES_FILE) ||
d850b7a5 74 (repo_get_oid(the_repository, GITMODULES_INDEX, &oid) < 0 && repo_get_oid(the_repository, GITMODULES_HEAD, &oid) < 0);
b5c259f2
AO
75}
76
5fee9952 77/*
91b83480
BW
78 * Check if the .gitmodules file has unstaged modifications. This must be
79 * checked before allowing modifications to the .gitmodules file with the
80 * intention to stage them later, because when continuing we would stage the
81 * modifications the user didn't stage herself too. That might change in a
82 * future version when we learn to stage the changes we do ourselves without
83 * staging any previous modifications.
5fee9952 84 */
7da9aba4 85int is_staging_gitmodules_ok(struct index_state *istate)
5fee9952 86{
91b83480
BW
87 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
88
89 if ((pos >= 0) && (pos < istate->cache_nr)) {
90 struct stat st;
91 if (lstat(GITMODULES_FILE, &st) == 0 &&
7edee329 92 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
91b83480
BW
93 return 0;
94 }
95
96 return 1;
5fee9952
JL
97}
98
2e2d4040
NTND
99static int for_each_remote_ref_submodule(const char *submodule,
100 each_ref_fn fn, void *cb_data)
101{
102 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
103 fn, cb_data);
104}
105
0656781f
JL
106/*
107 * Try to update the "path" entry in the "submodule.<name>" section of the
108 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
109 * with the correct path=<oldpath> setting was found and we could update it.
110 */
111int update_path_in_gitmodules(const char *oldpath, const char *newpath)
112{
113 struct strbuf entry = STRBUF_INIT;
851e18c3 114 const struct submodule *submodule;
45f5ef3d 115 int ret;
0656781f 116
4c0eeafe 117 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
0656781f
JL
118 return -1;
119
68f08b4b 120 if (is_gitmodules_unmerged(the_repository->index))
0656781f
JL
121 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
122
14228447 123 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
851e18c3 124 if (!submodule || !submodule->name) {
0656781f
JL
125 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
126 return -1;
127 }
128 strbuf_addstr(&entry, "submodule.");
851e18c3 129 strbuf_addstr(&entry, submodule->name);
0656781f 130 strbuf_addstr(&entry, ".path");
45f5ef3d 131 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
0656781f 132 strbuf_release(&entry);
45f5ef3d 133 return ret;
0656781f
JL
134}
135
95c16418
JL
136/*
137 * Try to remove the "submodule.<name>" section from .gitmodules where the given
138 * path is configured. Return 0 only if a .gitmodules file was found, a section
139 * with the correct path=<path> setting was found and we could remove it.
140 */
141int remove_path_from_gitmodules(const char *path)
142{
143 struct strbuf sect = STRBUF_INIT;
851e18c3 144 const struct submodule *submodule;
95c16418 145
4c0eeafe 146 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
95c16418
JL
147 return -1;
148
68f08b4b 149 if (is_gitmodules_unmerged(the_repository->index))
95c16418
JL
150 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
151
14228447 152 submodule = submodule_from_path(the_repository, null_oid(), path);
851e18c3 153 if (!submodule || !submodule->name) {
95c16418
JL
154 warning(_("Could not find section in .gitmodules where path=%s"), path);
155 return -1;
156 }
157 strbuf_addstr(&sect, "submodule.");
851e18c3 158 strbuf_addstr(&sect, submodule->name);
4c0eeafe 159 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
95c16418
JL
160 /* Maybe the user already did that, don't error out here */
161 warning(_("Could not remove .gitmodules entry for %s"), path);
162 strbuf_release(&sect);
163 return -1;
164 }
165 strbuf_release(&sect);
166 return 0;
167}
168
3b8317a9 169void stage_updated_gitmodules(struct index_state *istate)
5fee9952 170{
3b8317a9 171 if (add_file_to_index(istate, GITMODULES_FILE, 0))
5fee9952
JL
172 die(_("staging updated .gitmodules failed"));
173}
174
a35e03de
JT
175static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
176
8d33c3af
JT
177void add_submodule_odb_by_path(const char *path)
178{
179 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
180}
181
a35e03de
JT
182int register_all_submodule_odb_as_alternates(void)
183{
184 int i;
185 int ret = added_submodule_odb_paths.nr;
186
187 for (i = 0; i < added_submodule_odb_paths.nr; i++)
188 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
189 if (ret) {
190 string_list_clear(&added_submodule_odb_paths, 0);
71ef66d7
JT
191 trace2_data_intmax("submodule", the_repository,
192 "register_all_submodule_odb_as_alternates/registered", ret);
a35e03de
JT
193 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
194 BUG("register_all_submodule_odb_as_alternates() called");
195 }
196 return ret;
197}
198
aee9c7d6
JL
199void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
200 const char *path)
201{
3b8fb393 202 const struct submodule *submodule = submodule_from_path(the_repository,
14228447 203 null_oid(),
204 path);
851e18c3 205 if (submodule) {
fdfa9e97
BW
206 const char *ignore;
207 char *key;
aee9c7d6 208
fdfa9e97 209 key = xstrfmt("submodule.%s.ignore", submodule->name);
f1de981e 210 if (repo_config_get_string_tmp(the_repository, key, &ignore))
fdfa9e97
BW
211 ignore = submodule->ignore;
212 free(key);
302ad7a9 213
fdfa9e97
BW
214 if (ignore)
215 handle_ignore_submodules_arg(diffopt, ignore);
68f08b4b 216 else if (is_gitmodules_unmerged(the_repository->index))
0d1e0e78 217 diffopt->flags.ignore_submodules = 1;
046b4823
SB
218 }
219}
220
221/* Cheap function that only determines if we're interested in submodules at all */
783a86c1 222int git_default_submodule_config(const char *var, const char *value,
5cf88fd8 223 void *cb UNUSED)
046b4823
SB
224{
225 if (!strcmp(var, "submodule.recurse")) {
226 int v = git_config_bool(var, value) ?
227 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
228 config_update_recurse_submodules = v;
229 }
230 return 0;
1d789d08
SB
231}
232
d7a3803f
SB
233int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
234 const char *arg, int unset)
235{
236 if (unset) {
237 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
238 return 0;
239 }
240 if (arg)
241 config_update_recurse_submodules =
242 parse_update_recurse_submodules_arg(opt->long_name,
243 arg);
244 else
245 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
246
247 return 0;
248}
249
f9f42560
BW
250/*
251 * Determine if a submodule has been initialized at a given 'path'
252 */
a452128a
AR
253/*
254 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
255 * ie, the config looks like: "[submodule] active\n".
256 * Since that is an invalid pathspec, we should inform the user.
257 */
961b130d
GC
258int is_tree_submodule_active(struct repository *repo,
259 const struct object_id *treeish_name,
260 const char *path)
f9f42560
BW
261{
262 int ret = 0;
a086f921
BW
263 char *key = NULL;
264 char *value = NULL;
265 const struct string_list *sl;
627d9342
BW
266 const struct submodule *module;
267
961b130d 268 module = submodule_from_path(repo, treeish_name, path);
f9f42560 269
a086f921
BW
270 /* early return if there isn't a path->module mapping */
271 if (!module)
272 return 0;
f9f42560 273
a086f921
BW
274 /* submodule.<name>.active is set */
275 key = xstrfmt("submodule.%s.active", module->name);
627d9342 276 if (!repo_config_get_bool(repo, key, &ret)) {
a086f921
BW
277 free(key);
278 return ret;
279 }
280 free(key);
f9f42560 281
a086f921 282 /* submodule.active is set */
9e2d884d 283 if (!repo_config_get_string_multi(repo, "submodule.active", &sl)) {
a086f921 284 struct pathspec ps;
c972bf4c 285 struct strvec args = STRVEC_INIT;
a086f921 286 const struct string_list_item *item;
f9f42560 287
a086f921 288 for_each_string_list_item(item, sl) {
c972bf4c 289 strvec_push(&args, item->string);
a086f921
BW
290 }
291
d70a9eb6 292 parse_pathspec(&ps, 0, 0, NULL, args.v);
68f08b4b 293 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
a086f921 294
c972bf4c 295 strvec_clear(&args);
a086f921
BW
296 clear_pathspec(&ps);
297 return ret;
f9f42560
BW
298 }
299
a086f921
BW
300 /* fallback to checking if the URL is set */
301 key = xstrfmt("submodule.%s.url", module->name);
627d9342 302 ret = !repo_config_get_string(repo, key, &value);
a086f921
BW
303
304 free(value);
305 free(key);
f9f42560
BW
306 return ret;
307}
308
961b130d
GC
309int is_submodule_active(struct repository *repo, const char *path)
310{
311 return is_tree_submodule_active(repo, null_oid(), path);
312}
313
15cdc647 314int is_submodule_populated_gently(const char *path, int *return_error_code)
5688c28d
BW
315{
316 int ret = 0;
317 char *gitdir = xstrfmt("%s/.git", path);
318
15cdc647 319 if (resolve_gitdir_gently(gitdir, return_error_code))
5688c28d
BW
320 ret = 1;
321
322 free(gitdir);
323 return ret;
324}
325
bdab9721
BW
326/*
327 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
328 */
847a9e5d 329void die_in_unpopulated_submodule(struct index_state *istate,
bdab9721
BW
330 const char *prefix)
331{
332 int i, prefixlen;
333
334 if (!prefix)
335 return;
336
337 prefixlen = strlen(prefix);
338
339 for (i = 0; i < istate->cache_nr; i++) {
340 struct cache_entry *ce = istate->cache[i];
341 int ce_len = ce_namelen(ce);
342
343 if (!S_ISGITLINK(ce->ce_mode))
344 continue;
345 if (prefixlen <= ce_len)
346 continue;
347 if (strncmp(ce->name, prefix, ce_len))
348 continue;
349 if (prefix[ce_len] != '/')
350 continue;
351
352 die(_("in unpopulated submodule '%s'"), ce->name);
353 }
354}
355
c08397e3
BW
356/*
357 * Dies if any paths in the provided pathspec descends into a submodule
358 */
847a9e5d 359void die_path_inside_submodule(struct index_state *istate,
c08397e3
BW
360 const struct pathspec *ps)
361{
362 int i, j;
363
364 for (i = 0; i < istate->cache_nr; i++) {
365 struct cache_entry *ce = istate->cache[i];
366 int ce_len = ce_namelen(ce);
367
368 if (!S_ISGITLINK(ce->ce_mode))
369 continue;
370
371 for (j = 0; j < ps->nr ; j++) {
372 const struct pathspec_item *item = &ps->items[j];
373
374 if (item->len <= ce_len)
375 continue;
376 if (item->match[ce_len] != '/')
377 continue;
378 if (strncmp(ce->name, item->match, ce_len))
379 continue;
380 if (item->len == ce_len + 1)
381 continue;
382
383 die(_("Pathspec '%s' is in submodule '%.*s'"),
384 item->original, ce_len, ce->name);
385 }
386 }
387}
388
ec6141a0 389enum submodule_update_type parse_submodule_update_type(const char *value)
ea2fa5a3 390{
ea2fa5a3 391 if (!strcmp(value, "none"))
ec6141a0 392 return SM_UPDATE_NONE;
ea2fa5a3 393 else if (!strcmp(value, "checkout"))
ec6141a0 394 return SM_UPDATE_CHECKOUT;
ea2fa5a3 395 else if (!strcmp(value, "rebase"))
ec6141a0 396 return SM_UPDATE_REBASE;
ea2fa5a3 397 else if (!strcmp(value, "merge"))
ec6141a0
BW
398 return SM_UPDATE_MERGE;
399 else if (*value == '!')
400 return SM_UPDATE_COMMAND;
401 else
402 return SM_UPDATE_UNSPECIFIED;
403}
404
405int parse_submodule_update_strategy(const char *value,
406 struct submodule_update_strategy *dst)
407{
408 enum submodule_update_type type;
409
410 free((void*)dst->command);
411 dst->command = NULL;
412
413 type = parse_submodule_update_type(value);
414 if (type == SM_UPDATE_UNSPECIFIED)
ea2fa5a3 415 return -1;
ec6141a0
BW
416
417 dst->type = type;
418 if (type == SM_UPDATE_COMMAND)
419 dst->command = xstrdup(value + 1);
420
ea2fa5a3
SB
421 return 0;
422}
423
b9dd63ff 424const char *submodule_update_type_to_string(enum submodule_update_type type)
3604242f 425{
b9dd63ff 426 switch (type) {
3604242f
SB
427 case SM_UPDATE_CHECKOUT:
428 return "checkout";
429 case SM_UPDATE_MERGE:
430 return "merge";
431 case SM_UPDATE_REBASE:
432 return "rebase";
433 case SM_UPDATE_NONE:
434 return "none";
435 case SM_UPDATE_UNSPECIFIED:
3604242f 436 case SM_UPDATE_COMMAND:
b9dd63ff
ÆAB
437 BUG("init_submodule() should handle type %d", type);
438 default:
439 BUG("unexpected update strategy type: %d", type);
3604242f 440 }
3604242f
SB
441}
442
46a958b3
JL
443void handle_ignore_submodules_arg(struct diff_options *diffopt,
444 const char *arg)
445{
8ef93124 446 diffopt->flags.ignore_submodule_set = 1;
0d1e0e78
BW
447 diffopt->flags.ignore_submodules = 0;
448 diffopt->flags.ignore_untracked_in_submodules = 0;
449 diffopt->flags.ignore_dirty_submodules = 0;
be4f2b40 450
46a958b3 451 if (!strcmp(arg, "all"))
0d1e0e78 452 diffopt->flags.ignore_submodules = 1;
46a958b3 453 else if (!strcmp(arg, "untracked"))
0d1e0e78 454 diffopt->flags.ignore_untracked_in_submodules = 1;
46a958b3 455 else if (!strcmp(arg, "dirty"))
0d1e0e78 456 diffopt->flags.ignore_dirty_submodules = 1;
aee9c7d6 457 else if (strcmp(arg, "none"))
a4ffbbbb 458 die(_("bad --ignore-submodules argument: %s"), arg);
5a59a230
NTND
459 /*
460 * Please update _git_status() in git-completion.bash when you
461 * add new options
462 */
46a958b3
JL
463}
464
4831c23f
JH
465static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
466 const char *path,
467 struct commit *left, struct commit *right,
468 struct commit_list *merge_bases)
808a95dc 469{
8e6df650 470 struct commit_list *list;
808a95dc 471
85a1ec2c 472 repo_init_revisions(r, rev, NULL);
808a95dc
JN
473 setup_revisions(0, NULL, rev, NULL);
474 rev->left_right = 1;
475 rev->first_parent_only = 1;
476 left->object.flags |= SYMMETRIC_LEFT;
477 add_pending_object(rev, &left->object, path);
478 add_pending_object(rev, &right->object, path);
808a95dc
JN
479 for (list = merge_bases; list; list = list->next) {
480 list->item->object.flags |= UNINTERESTING;
481 add_pending_object(rev, &list->item->object,
f2fd0760 482 oid_to_hex(&list->item->object.oid));
808a95dc
JN
483 }
484 return prepare_revision_walk(rev);
485}
486
180b154b 487static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
808a95dc
JN
488{
489 static const char format[] = " %m %s";
490 struct strbuf sb = STRBUF_INIT;
491 struct commit *commit;
492
493 while ((commit = get_revision(rev))) {
494 struct pretty_print_context ctx = {0};
495 ctx.date_mode = rev->date_mode;
ecaee805 496 ctx.output_encoding = get_log_output_encoding();
808a95dc 497 strbuf_setlen(&sb, 0);
605f0ec1
SB
498 repo_format_commit_message(r, commit, format, &sb,
499 &ctx);
808a95dc 500 strbuf_addch(&sb, '\n');
f3597138
SB
501 if (commit->object.flags & SYMMETRIC_LEFT)
502 diff_emit_submodule_del(o, sb.buf);
503 else
504 diff_emit_submodule_add(o, sb.buf);
808a95dc
JN
505 }
506 strbuf_release(&sb);
507}
508
c972bf4c 509void prepare_submodule_repo_env(struct strvec *out)
6cd5757c 510{
d1fa9435 511 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
6cd5757c
SB
512}
513
c972bf4c 514static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
a62387b3 515{
d1fa9435 516 prepare_other_repo_env(out, ".");
a62387b3
SB
517}
518
605f0ec1
SB
519/*
520 * Initialize a repository struct for a submodule based on the provided 'path'.
521 *
605f0ec1
SB
522 * Returns the repository struct on success,
523 * NULL when the submodule is not present.
8e6df650 524 */
605f0ec1
SB
525static struct repository *open_submodule(const char *path)
526{
527 struct strbuf sb = STRBUF_INIT;
528 struct repository *out = xmalloc(sizeof(*out));
529
530 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
531 strbuf_release(&sb);
532 free(out);
533 return NULL;
534 }
535
536 /* Mark it as a submodule */
537 out->submodule_prefix = xstrdup(path);
538
539 strbuf_release(&sb);
540 return out;
541}
542
543/*
544 * Helper function to display the submodule header line prior to the full
545 * summary output.
546 *
547 * If it can locate the submodule git directory it will create a repository
548 * handle for the submodule and lookup both the left and right commits and
549 * put them into the left and right pointers.
8e6df650 550 */
605f0ec1
SB
551static void show_submodule_header(struct diff_options *o,
552 const char *path,
602a283a 553 struct object_id *one, struct object_id *two,
f3597138 554 unsigned dirty_submodule,
605f0ec1 555 struct repository *sub,
8e6df650
JK
556 struct commit **left, struct commit **right,
557 struct commit_list **merge_bases)
752c0c24 558{
752c0c24
JS
559 const char *message = NULL;
560 struct strbuf sb = STRBUF_INIT;
752c0c24
JS
561 int fast_forward = 0, fast_backward = 0;
562
c7e1a736 563 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
f3597138
SB
564 diff_emit_submodule_untracked(o, path);
565
c7e1a736 566 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
f3597138 567 diff_emit_submodule_modified(o, path);
c7e1a736 568
8e6df650
JK
569 if (is_null_oid(one))
570 message = "(new submodule)";
571 else if (is_null_oid(two))
572 message = "(submodule deleted)";
573
605f0ec1 574 if (!sub) {
8e6df650 575 if (!message)
2d94dd2f 576 message = "(commits not present)";
8e6df650
JK
577 goto output_header;
578 }
579
580 /*
581 * Attempt to lookup the commit references, and determine if this is
582 * a fast forward or fast backwards update.
583 */
605f0ec1
SB
584 *left = lookup_commit_reference(sub, one);
585 *right = lookup_commit_reference(sub, two);
8e6df650
JK
586
587 /*
588 * Warn about missing commits in the submodule project, but only if
589 * they aren't null.
590 */
591 if ((!is_null_oid(one) && !*left) ||
592 (!is_null_oid(two) && !*right))
593 message = "(commits not present)";
594
76e2a099
JS
595 *merge_bases = NULL;
596 if (repo_get_merge_bases(sub, *left, *right, merge_bases) < 0) {
597 message = "(corrupt repository)";
598 goto output_header;
599 }
600
8e6df650
JK
601 if (*merge_bases) {
602 if ((*merge_bases)->item == *left)
603 fast_forward = 1;
604 else if ((*merge_bases)->item == *right)
605 fast_backward = 1;
606 }
607
4a7e27e9 608 if (oideq(one, two)) {
c7e1a736
JL
609 strbuf_release(&sb);
610 return;
611 }
612
8e6df650 613output_header:
f3597138 614 strbuf_addf(&sb, "Submodule %s ", path);
30e677e0 615 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
a94bb683 616 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
30e677e0 617 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
752c0c24 618 if (message)
f3597138 619 strbuf_addf(&sb, " %s\n", message);
752c0c24 620 else
f3597138
SB
621 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
622 diff_emit_submodule_header(o, sb.buf);
752c0c24 623
752c0c24
JS
624 strbuf_release(&sb);
625}
ee6fc514 626
180b154b 627void show_submodule_diff_summary(struct diff_options *o, const char *path,
8e6df650 628 struct object_id *one, struct object_id *two,
f3597138 629 unsigned dirty_submodule)
8e6df650 630{
f196c1e9 631 struct rev_info rev = REV_INFO_INIT;
8e6df650
JK
632 struct commit *left = NULL, *right = NULL;
633 struct commit_list *merge_bases = NULL;
605f0ec1 634 struct repository *sub;
8e6df650 635
605f0ec1 636 sub = open_submodule(path);
f3597138 637 show_submodule_header(o, path, one, two, dirty_submodule,
605f0ec1 638 sub, &left, &right, &merge_bases);
8e6df650
JK
639
640 /*
641 * If we don't have both a left and a right pointer, there is no
642 * reason to try and display a summary. The header line should contain
643 * all the information the user needs.
644 */
605f0ec1 645 if (!left || !right || !sub)
8e6df650
JK
646 goto out;
647
648 /* Treat revision walker failure the same as missing commits */
4831c23f 649 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
f3597138 650 diff_emit_submodule_error(o, "(revision walker failed)\n");
8e6df650
JK
651 goto out;
652 }
653
180b154b 654 print_submodule_diff_summary(sub, &rev, o);
8e6df650
JK
655
656out:
bf20fe4c 657 free_commit_list(merge_bases);
f196c1e9 658 release_revisions(&rev);
8e6df650
JK
659 clear_commit_marks(left, ~0);
660 clear_commit_marks(right, ~0);
605f0ec1
SB
661 if (sub) {
662 repo_clear(sub);
663 free(sub);
664 }
8e6df650
JK
665}
666
f3597138 667void show_submodule_inline_diff(struct diff_options *o, const char *path,
fd47ae6a 668 struct object_id *one, struct object_id *two,
f3597138 669 unsigned dirty_submodule)
fd47ae6a 670{
bc099914 671 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
fd47ae6a
JK
672 struct commit *left = NULL, *right = NULL;
673 struct commit_list *merge_bases = NULL;
fd47ae6a 674 struct child_process cp = CHILD_PROCESS_INIT;
f3597138 675 struct strbuf sb = STRBUF_INIT;
605f0ec1 676 struct repository *sub;
fd47ae6a 677
605f0ec1 678 sub = open_submodule(path);
f3597138 679 show_submodule_header(o, path, one, two, dirty_submodule,
605f0ec1 680 sub, &left, &right, &merge_bases);
fd47ae6a
JK
681
682 /* We need a valid left and right commit to display a difference */
683 if (!(left || is_null_oid(one)) ||
684 !(right || is_null_oid(two)))
685 goto done;
686
687 if (left)
bc099914 688 old_oid = one;
fd47ae6a 689 if (right)
bc099914 690 new_oid = two;
fd47ae6a 691
fd47ae6a
JK
692 cp.git_cmd = 1;
693 cp.dir = path;
f3597138 694 cp.out = -1;
fd47ae6a
JK
695 cp.no_stdin = 1;
696
697 /* TODO: other options may need to be passed here. */
c972bf4c
JK
698 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
699 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
f3597138 700 "always" : "never");
5a522142 701
0d1e0e78 702 if (o->flags.reverse_diff) {
c972bf4c 703 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
f6d8942b 704 o->b_prefix, path);
c972bf4c 705 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
f6d8942b 706 o->a_prefix, path);
fd47ae6a 707 } else {
c972bf4c 708 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
f6d8942b 709 o->a_prefix, path);
c972bf4c 710 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
f6d8942b 711 o->b_prefix, path);
fd47ae6a 712 }
c972bf4c 713 strvec_push(&cp.args, oid_to_hex(old_oid));
fd47ae6a
JK
714 /*
715 * If the submodule has modified content, we will diff against the
716 * work tree, under the assumption that the user has asked for the
717 * diff format and wishes to actually see all differences even if they
718 * haven't yet been committed to the submodule yet.
719 */
720 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
c972bf4c 721 strvec_push(&cp.args, oid_to_hex(new_oid));
fd47ae6a 722
29fda24d 723 prepare_submodule_repo_env(&cp.env);
f1c0368d
DT
724
725 if (!is_directory(path)) {
726 /* fall back to absorbed git dir, if any */
727 if (!sub)
728 goto done;
729 cp.dir = sub->gitdir;
29fda24d
ÆAB
730 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=.");
731 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=.");
f1c0368d
DT
732 }
733
67f61efb 734 if (start_command(&cp)) {
f3597138 735 diff_emit_submodule_error(o, "(diff failed)\n");
67f61efb
DT
736 goto done;
737 }
f3597138
SB
738
739 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
740 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
741
742 if (finish_command(&cp))
743 diff_emit_submodule_error(o, "(diff failed)\n");
fd47ae6a
JK
744
745done:
f3597138 746 strbuf_release(&sb);
bf20fe4c 747 free_commit_list(merge_bases);
fd47ae6a
JK
748 if (left)
749 clear_commit_marks(left, ~0);
750 if (right)
751 clear_commit_marks(right, ~0);
605f0ec1
SB
752 if (sub) {
753 repo_clear(sub);
754 free(sub);
755 }
fd47ae6a
JK
756}
757
84f8925e
SB
758int should_update_submodules(void)
759{
760 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
761}
762
763const struct submodule *submodule_from_ce(const struct cache_entry *ce)
764{
765 if (!S_ISGITLINK(ce->ce_mode))
766 return NULL;
767
768 if (!should_update_submodules())
769 return NULL;
770
14228447 771 return submodule_from_path(the_repository, null_oid(), ce->name);
84f8925e
SB
772}
773
aacc5c1a 774
c68f8375 775struct collect_changed_submodules_cb_data {
6245b98b 776 struct repository *repo;
c68f8375
HV
777 struct string_list *changed;
778 const struct object_id *commit_oid;
779};
780
781/*
782 * this would normally be two functions: default_name_from_path() and
783 * path_from_default_name(). Since the default name is the same as
784 * the submodule path we can get away with just one function which only
785 * checks whether there is a submodule in the working directory at that
786 * location.
787 */
788static const char *default_name_or_path(const char *path_or_name)
789{
790 int error_code;
791
792 if (!is_submodule_populated_gently(path_or_name, &error_code))
793 return NULL;
794
795 return path_or_name;
796}
797
6e1e0c99
GC
798/*
799 * Holds relevant information for a changed submodule. Used as the .util
b90d9f76
GC
800 * member of the changed submodule name string_list_item.
801 *
802 * (super_oid, path) allows the submodule config to be read from _some_
803 * .gitmodules file. We store this information the first time we find a
804 * superproject commit that points to the submodule, but this is
805 * arbitrary - we can choose any (super_oid, path) that matches the
806 * submodule's name.
807 *
808 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
809 * guarantee that we're reading the commit that the user would expect. A better
810 * scheme would be to just fetch a submodule by its name. This requires two
811 * steps:
812 * - Create a function that behaves like repo_submodule_init(), but accepts a
813 * submodule name instead of treeish_name and path. This should be easy
814 * because repo_submodule_init() internally uses the submodule's name.
815 *
816 * - Replace most instances of 'struct submodule' (which is the .gitmodules
817 * config) with just the submodule name. This is OK because we expect
818 * submodule settings to be stored in .git/config (via "git submodule init"),
819 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
820 * which constructs a bogus 'struct submodule' for the sake of giving a
821 * placeholder name to a gitlink.
6e1e0c99
GC
822 */
823struct changed_submodule_data {
b90d9f76
GC
824 /*
825 * The first superproject commit in the rev walk that points to
826 * the submodule.
827 */
828 const struct object_id *super_oid;
829 /*
830 * Path to the submodule in the superproject commit referenced
831 * by 'super_oid'.
832 */
833 char *path;
6e1e0c99
GC
834 /* The submodule commits that have changed in the rev walk. */
835 struct oid_array new_commits;
836};
837
838static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
839{
840 oid_array_clear(&cs_data->new_commits);
b90d9f76 841 free(cs_data->path);
6e1e0c99
GC
842}
843
aacc5c1a 844static void collect_changed_submodules_cb(struct diff_queue_struct *q,
61bdc7c5 845 struct diff_options *options UNUSED,
aacc5c1a
BW
846 void *data)
847{
c68f8375
HV
848 struct collect_changed_submodules_cb_data *me = data;
849 struct string_list *changed = me->changed;
850 const struct object_id *commit_oid = me->commit_oid;
aacc5c1a 851 int i;
aacc5c1a
BW
852
853 for (i = 0; i < q->nr; i++) {
854 struct diff_filepair *p = q->queue[i];
c68f8375
HV
855 const struct submodule *submodule;
856 const char *name;
1e5dd3a1 857 struct string_list_item *item;
6e1e0c99 858 struct changed_submodule_data *cs_data;
c68f8375 859
aacc5c1a
BW
860 if (!S_ISGITLINK(p->two->mode))
861 continue;
862
6245b98b 863 submodule = submodule_from_path(me->repo,
3b8fb393 864 commit_oid, p->two->path);
c68f8375
HV
865 if (submodule)
866 name = submodule->name;
867 else {
868 name = default_name_or_path(p->two->path);
869 /* make sure name does not collide with existing one */
5fc84755 870 if (name)
6245b98b 871 submodule = submodule_from_name(me->repo,
5fc84755 872 commit_oid, name);
c68f8375 873 if (submodule) {
a4ffbbbb 874 warning(_("Submodule in commit %s at path: "
c68f8375 875 "'%s' collides with a submodule named "
a4ffbbbb 876 "the same. Skipping it."),
5fc84755 877 oid_to_hex(commit_oid), p->two->path);
c68f8375
HV
878 name = NULL;
879 }
aacc5c1a 880 }
c68f8375
HV
881
882 if (!name)
883 continue;
884
1e5dd3a1 885 item = string_list_insert(changed, name);
b90d9f76
GC
886 if (item->util)
887 cs_data = item->util;
888 else {
6e1e0c99 889 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
b90d9f76
GC
890 cs_data = item->util;
891 cs_data->super_oid = commit_oid;
892 cs_data->path = xstrdup(p->two->path);
893 }
6e1e0c99 894 oid_array_append(&cs_data->new_commits, &p->two->oid);
aacc5c1a
BW
895 }
896}
897
898/*
899 * Collect the paths of submodules in 'changed' which have changed based on
900 * the revisions as specified in 'argv'. Each entry in 'changed' will also
901 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
902 * what the submodule pointers were updated to during the change.
903 */
6245b98b 904static void collect_changed_submodules(struct repository *r,
174d131f 905 struct string_list *changed,
c972bf4c 906 struct strvec *argv)
aacc5c1a
BW
907{
908 struct rev_info rev;
909 const struct commit *commit;
a462bee5
OS
910 int save_warning;
911 struct setup_revision_opt s_r_opt = {
912 .assume_dashdash = 1,
913 };
aacc5c1a 914
a462bee5
OS
915 save_warning = warn_on_object_refname_ambiguity;
916 warn_on_object_refname_ambiguity = 0;
6245b98b 917 repo_init_revisions(r, &rev, NULL);
a462bee5
OS
918 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
919 warn_on_object_refname_ambiguity = save_warning;
aacc5c1a 920 if (prepare_revision_walk(&rev))
a4ffbbbb 921 die(_("revision walk setup failed"));
aacc5c1a
BW
922
923 while ((commit = get_revision(&rev))) {
924 struct rev_info diff_rev;
c68f8375 925 struct collect_changed_submodules_cb_data data;
6245b98b 926 data.repo = r;
c68f8375
HV
927 data.changed = changed;
928 data.commit_oid = &commit->object.oid;
aacc5c1a 929
6245b98b 930 repo_init_revisions(r, &diff_rev, NULL);
aacc5c1a
BW
931 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
932 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
c68f8375 933 diff_rev.diffopt.format_callback_data = &data;
d01141de
SO
934 diff_rev.dense_combined_merges = 1;
935 diff_tree_combined_merge(commit, &diff_rev);
2108fe4a 936 release_revisions(&diff_rev);
aacc5c1a
BW
937 }
938
939 reset_revision_walk();
2108fe4a 940 release_revisions(&rev);
aacc5c1a
BW
941}
942
6e1e0c99 943static void free_submodules_data(struct string_list *submodules)
aacc5c1a
BW
944{
945 struct string_list_item *item;
946 for_each_string_list_item(item, submodules)
6e1e0c99
GC
947 changed_submodule_data_clear(item->util);
948
aacc5c1a
BW
949 string_list_clear(submodules, 1);
950}
951
5cf88fd8
ÆAB
952static int has_remote(const char *refname UNUSED,
953 const struct object_id *oid UNUSED,
954 int flags UNUSED, void *cb_data UNUSED)
d2b17b32
FG
955{
956 return 1;
957}
958
1b7ba794 959static int append_oid_to_argv(const struct object_id *oid, void *data)
d2b17b32 960{
c972bf4c
JK
961 struct strvec *argv = data;
962 strvec_push(argv, oid_to_hex(oid));
9cfa1c26
HV
963 return 0;
964}
965
3c96aa97 966struct has_commit_data {
6245b98b 967 struct repository *repo;
3c96aa97
SB
968 int result;
969 const char *path;
7c2f8cc5 970 const struct object_id *super_oid;
3c96aa97
SB
971};
972
1b7ba794 973static int check_has_commit(const struct object_id *oid, void *data)
d2b17b32 974{
3c96aa97 975 struct has_commit_data *cb = data;
13a2f620
JT
976 struct repository subrepo;
977 enum object_type type;
5b6607d2 978
7c2f8cc5 979 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
13a2f620 980 cb->result = 0;
5fff35d8
GC
981 /* subrepo failed to init, so don't clean it up. */
982 return 0;
13a2f620
JT
983 }
984
985 type = oid_object_info(&subrepo, oid, NULL);
5b6607d2 986
3c96aa97
SB
987 switch (type) {
988 case OBJ_COMMIT:
13a2f620 989 goto cleanup;
3c96aa97
SB
990 case OBJ_BAD:
991 /*
992 * Object is missing or invalid. If invalid, an error message
993 * has already been printed.
994 */
995 cb->result = 0;
13a2f620 996 goto cleanup;
3c96aa97
SB
997 default:
998 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
debca9d2 999 cb->path, oid_to_hex(oid), type_name(type));
3c96aa97 1000 }
13a2f620
JT
1001cleanup:
1002 repo_clear(&subrepo);
1003 return 0;
5b6607d2
HV
1004}
1005
6245b98b
NTND
1006static int submodule_has_commits(struct repository *r,
1007 const char *path,
7c2f8cc5 1008 const struct object_id *super_oid,
6245b98b 1009 struct oid_array *commits)
5b6607d2 1010{
7c2f8cc5
GC
1011 struct has_commit_data has_commit = {
1012 .repo = r,
1013 .result = 1,
1014 .path = path,
1015 .super_oid = super_oid
1016 };
5b6607d2 1017
910650d2 1018 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
7c8d2b00 1019
3c96aa97 1020 if (has_commit.result) {
7c8d2b00
BW
1021 /*
1022 * Even if the submodule is checked out and the commit is
1023 * present, make sure it exists in the submodule's object store
1024 * and that it is reachable from a ref.
1025 */
1026 struct child_process cp = CHILD_PROCESS_INIT;
1027 struct strbuf out = STRBUF_INIT;
1028
c972bf4c 1029 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
7c8d2b00 1030 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
c972bf4c 1031 strvec_pushl(&cp.args, "--not", "--all", NULL);
7c8d2b00 1032
29fda24d 1033 prepare_submodule_repo_env(&cp.env);
7c8d2b00
BW
1034 cp.git_cmd = 1;
1035 cp.no_stdin = 1;
1036 cp.dir = path;
1037
1038 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
3c96aa97 1039 has_commit.result = 0;
7c8d2b00
BW
1040
1041 strbuf_release(&out);
1042 }
1043
3c96aa97 1044 return has_commit.result;
5b6607d2
HV
1045}
1046
6245b98b
NTND
1047static int submodule_needs_pushing(struct repository *r,
1048 const char *path,
1049 struct oid_array *commits)
5b6607d2 1050{
7c2f8cc5 1051 if (!submodule_has_commits(r, path, null_oid(), commits))
250ab24a
HV
1052 /*
1053 * NOTE: We do consider it safe to return "no" here. The
1054 * correct answer would be "We do not know" instead of
1055 * "No push needed", but it is quite hard to change
1056 * the submodule pointer without having the submodule
1057 * around. If a user did however change the submodules
1058 * without having the submodule around, this indicates
1059 * an expert who knows what they are doing or a
1060 * maintainer integrating work from other people. In
1061 * both cases it should be safe to skip this check.
1062 */
d2b17b32
FG
1063 return 0;
1064
1065 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 1066 struct child_process cp = CHILD_PROCESS_INIT;
d2b17b32
FG
1067 struct strbuf buf = STRBUF_INIT;
1068 int needs_pushing = 0;
1069
c972bf4c 1070 strvec_push(&cp.args, "rev-list");
910650d2 1071 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
c972bf4c 1072 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
5b6607d2 1073
29fda24d 1074 prepare_submodule_repo_env(&cp.env);
d2b17b32
FG
1075 cp.git_cmd = 1;
1076 cp.no_stdin = 1;
1077 cp.out = -1;
1078 cp.dir = path;
1079 if (start_command(&cp))
a4ffbbbb 1080 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
5b6607d2 1081 path);
db1ba2a2 1082 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
d2b17b32
FG
1083 needs_pushing = 1;
1084 finish_command(&cp);
1085 close(cp.out);
1086 strbuf_release(&buf);
1087 return needs_pushing;
1088 }
1089
1090 return 0;
1091}
1092
6245b98b 1093int find_unpushed_submodules(struct repository *r,
174d131f
NTND
1094 struct oid_array *commits,
1095 const char *remotes_name,
1096 struct string_list *needs_pushing)
d2b17b32 1097{
14739447 1098 struct string_list submodules = STRING_LIST_INIT_DUP;
c68f8375 1099 struct string_list_item *name;
c972bf4c 1100 struct strvec argv = STRVEC_INIT;
a762e51e 1101
d70a9eb6 1102 /* argv.v[0] will be ignored by setup_revisions */
c972bf4c 1103 strvec_push(&argv, "find_unpushed_submodules");
910650d2 1104 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
c972bf4c
JK
1105 strvec_push(&argv, "--not");
1106 strvec_pushf(&argv, "--remotes=%s", remotes_name);
9cfa1c26 1107
6245b98b 1108 collect_changed_submodules(r, &submodules, &argv);
d2b17b32 1109
c68f8375 1110 for_each_string_list_item(name, &submodules) {
6e1e0c99 1111 struct changed_submodule_data *cs_data = name->util;
c68f8375
HV
1112 const struct submodule *submodule;
1113 const char *path = NULL;
1114
14228447 1115 submodule = submodule_from_name(r, null_oid(), name->string);
c68f8375
HV
1116 if (submodule)
1117 path = submodule->path;
1118 else
1119 path = default_name_or_path(name->string);
1120
1121 if (!path)
1122 continue;
5b6607d2 1123
6e1e0c99 1124 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
aacc5c1a 1125 string_list_insert(needs_pushing, path);
14739447 1126 }
610b2337 1127
6e1e0c99 1128 free_submodules_data(&submodules);
c972bf4c 1129 strvec_clear(&argv);
d2b17b32 1130
a762e51e 1131 return needs_pushing->nr;
d2b17b32
FG
1132}
1133
2a90556d 1134static int push_submodule(const char *path,
06bf4ad1 1135 const struct remote *remote,
60fba4bf 1136 const struct refspec *rs,
2a90556d
BW
1137 const struct string_list *push_options,
1138 int dry_run)
eb21c732 1139{
eb21c732 1140 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 1141 struct child_process cp = CHILD_PROCESS_INIT;
c972bf4c 1142 strvec_push(&cp.args, "push");
e62f779a
JT
1143 /*
1144 * When recursing into a submodule, treat any "only" configurations as "on-
1145 * demand", since "only" would not work (we need all submodules to be pushed
1146 * in order to be able to push the superproject).
1147 */
1148 strvec_push(&cp.args, "--recurse-submodules=only-is-on-demand");
0301c821 1149 if (dry_run)
c972bf4c 1150 strvec_push(&cp.args, "--dry-run");
eb21c732 1151
2a90556d
BW
1152 if (push_options && push_options->nr) {
1153 const struct string_list_item *item;
1154 for_each_string_list_item(item, push_options)
c972bf4c 1155 strvec_pushf(&cp.args, "--push-option=%s",
f6d8942b 1156 item->string);
2a90556d 1157 }
06bf4ad1
BW
1158
1159 if (remote->origin != REMOTE_UNCONFIGURED) {
1160 int i;
c972bf4c 1161 strvec_push(&cp.args, remote->name);
60fba4bf 1162 for (i = 0; i < rs->raw_nr; i++)
c972bf4c 1163 strvec_push(&cp.args, rs->raw[i]);
06bf4ad1
BW
1164 }
1165
29fda24d 1166 prepare_submodule_repo_env(&cp.env);
eb21c732
HV
1167 cp.git_cmd = 1;
1168 cp.no_stdin = 1;
1169 cp.dir = path;
1170 if (run_command(&cp))
1171 return 0;
1172 close(cp.out);
1173 }
1174
1175 return 1;
1176}
1177
06bf4ad1
BW
1178/*
1179 * Perform a check in the submodule to see if the remote and refspec work.
1180 * Die if the submodule can't be pushed.
1181 */
c7be7201
BW
1182static void submodule_push_check(const char *path, const char *head,
1183 const struct remote *remote,
60fba4bf 1184 const struct refspec *rs)
06bf4ad1
BW
1185{
1186 struct child_process cp = CHILD_PROCESS_INIT;
1187 int i;
1188
c972bf4c
JK
1189 strvec_push(&cp.args, "submodule--helper");
1190 strvec_push(&cp.args, "push-check");
1191 strvec_push(&cp.args, head);
1192 strvec_push(&cp.args, remote->name);
06bf4ad1 1193
60fba4bf 1194 for (i = 0; i < rs->raw_nr; i++)
c972bf4c 1195 strvec_push(&cp.args, rs->raw[i]);
06bf4ad1 1196
29fda24d 1197 prepare_submodule_repo_env(&cp.env);
06bf4ad1
BW
1198 cp.git_cmd = 1;
1199 cp.no_stdin = 1;
1200 cp.no_stdout = 1;
1201 cp.dir = path;
1202
1203 /*
1204 * Simply indicate if 'submodule--helper push-check' failed.
1205 * More detailed error information will be provided by the
1206 * child process.
1207 */
1208 if (run_command(&cp))
a4ffbbbb 1209 die(_("process for submodule '%s' failed"), path);
06bf4ad1
BW
1210}
1211
6245b98b 1212int push_unpushed_submodules(struct repository *r,
174d131f 1213 struct oid_array *commits,
06bf4ad1 1214 const struct remote *remote,
60fba4bf 1215 const struct refspec *rs,
2a90556d 1216 const struct string_list *push_options,
0301c821 1217 int dry_run)
eb21c732
HV
1218{
1219 int i, ret = 1;
f93d7c6f 1220 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
eb21c732 1221
6245b98b 1222 if (!find_unpushed_submodules(r, commits,
174d131f 1223 remote->name, &needs_pushing))
eb21c732
HV
1224 return 1;
1225
06bf4ad1
BW
1226 /*
1227 * Verify that the remote and refspec can be propagated to all
1228 * submodules. This check can be skipped if the remote and refspec
1229 * won't be propagated due to the remote being unconfigured (e.g. a URL
1230 * instead of a remote name).
1231 */
c7be7201
BW
1232 if (remote->origin != REMOTE_UNCONFIGURED) {
1233 char *head;
1234 struct object_id head_oid;
1235
0f2dc722 1236 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
c7be7201
BW
1237 if (!head)
1238 die(_("Failed to resolve HEAD as a valid ref."));
1239
06bf4ad1
BW
1240 for (i = 0; i < needs_pushing.nr; i++)
1241 submodule_push_check(needs_pushing.items[i].string,
60fba4bf 1242 head, remote, rs);
c7be7201
BW
1243 free(head);
1244 }
06bf4ad1
BW
1245
1246 /* Actually push the submodules */
eb21c732
HV
1247 for (i = 0; i < needs_pushing.nr; i++) {
1248 const char *path = needs_pushing.items[i].string;
a4ffbbbb 1249 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
60fba4bf 1250 if (!push_submodule(path, remote, rs,
06bf4ad1 1251 push_options, dry_run)) {
a4ffbbbb 1252 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
eb21c732
HV
1253 ret = 0;
1254 }
1255 }
1256
1257 string_list_clear(&needs_pushing, 0);
1258
1259 return ret;
1260}
1261
5cf88fd8 1262static int append_oid_to_array(const char *ref UNUSED,
63e14ee2 1263 const struct object_id *oid,
5cf88fd8 1264 int flags UNUSED, void *data)
6859de45 1265{
419fd786
BW
1266 struct oid_array *array = data;
1267 oid_array_append(array, oid);
6859de45
JK
1268 return 0;
1269}
1270
2eb80bcd 1271void check_for_new_submodule_commits(struct object_id *oid)
6859de45
JK
1272{
1273 if (!initialized_fetch_ref_tips) {
419fd786 1274 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
6859de45
JK
1275 initialized_fetch_ref_tips = 1;
1276 }
1277
910650d2 1278 oid_array_append(&ref_tips_after_fetch, oid);
6859de45
JK
1279}
1280
b90d9f76
GC
1281/*
1282 * Returns 1 if there is at least one submodule gitdir in
1283 * $GIT_DIR/modules and 0 otherwise. This follows
1284 * submodule_name_to_gitdir(), which looks for submodules in
1285 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1286 *
1287 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1288 * submodule absorbgitdirs", or it may be initialized there by "git
1289 * submodule update".
1290 */
1291static int repo_has_absorbed_submodules(struct repository *r)
1292{
1293 int ret;
1294 struct strbuf buf = STRBUF_INIT;
1295
1296 strbuf_repo_git_path(&buf, r, "modules/");
1297 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1298 strbuf_release(&buf);
1299 return ret;
1300}
1301
16dd6fe1
SB
1302static void calculate_changed_submodule_paths(struct repository *r,
1303 struct string_list *changed_submodule_names)
88a21979 1304{
c972bf4c 1305 struct strvec argv = STRVEC_INIT;
bcd73372 1306 struct string_list_item *name;
88a21979 1307
b90d9f76
GC
1308 /* No need to check if no submodules would be fetched */
1309 if (!submodule_from_path(r, NULL, NULL) &&
1310 !repo_has_absorbed_submodules(r))
18322bad
JL
1311 return;
1312
c972bf4c 1313 strvec_push(&argv, "--"); /* argv[0] program name */
910650d2 1314 oid_array_for_each_unique(&ref_tips_after_fetch,
d1a8460c 1315 append_oid_to_argv, &argv);
c972bf4c 1316 strvec_push(&argv, "--not");
910650d2 1317 oid_array_for_each_unique(&ref_tips_before_fetch,
d1a8460c 1318 append_oid_to_argv, &argv);
88a21979
JL
1319
1320 /*
1321 * Collect all submodules (whether checked out or not) for which new
c68f8375 1322 * commits have been recorded upstream in "changed_submodule_names".
88a21979 1323 */
bcd73372 1324 collect_changed_submodules(r, changed_submodule_names, &argv);
aacc5c1a 1325
bcd73372 1326 for_each_string_list_item(name, changed_submodule_names) {
6e1e0c99 1327 struct changed_submodule_data *cs_data = name->util;
c68f8375
HV
1328 const struct submodule *submodule;
1329 const char *path = NULL;
1330
14228447 1331 submodule = submodule_from_name(r, null_oid(), name->string);
c68f8375
HV
1332 if (submodule)
1333 path = submodule->path;
1334 else
1335 path = default_name_or_path(name->string);
1336
1337 if (!path)
1338 continue;
aacc5c1a 1339
6e1e0c99
GC
1340 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1341 changed_submodule_data_clear(cs_data);
bcd73372
SB
1342 *name->string = '\0';
1343 }
88a21979 1344 }
6859de45 1345
bcd73372
SB
1346 string_list_remove_empty_items(changed_submodule_names, 1);
1347
c972bf4c 1348 strvec_clear(&argv);
910650d2 1349 oid_array_clear(&ref_tips_before_fetch);
1350 oid_array_clear(&ref_tips_after_fetch);
6859de45 1351 initialized_fetch_ref_tips = 0;
88a21979
JL
1352}
1353
6245b98b 1354int submodule_touches_in_range(struct repository *r,
174d131f 1355 struct object_id *excl_oid,
a6d7eb2c
SB
1356 struct object_id *incl_oid)
1357{
1358 struct string_list subs = STRING_LIST_INIT_DUP;
c972bf4c 1359 struct strvec args = STRVEC_INIT;
a6d7eb2c
SB
1360 int ret;
1361
a6d7eb2c 1362 /* No need to check if there are no submodules configured */
6245b98b 1363 if (!submodule_from_path(r, NULL, NULL))
a6d7eb2c
SB
1364 return 0;
1365
c972bf4c
JK
1366 strvec_push(&args, "--"); /* args[0] program name */
1367 strvec_push(&args, oid_to_hex(incl_oid));
4d36f88b 1368 if (!is_null_oid(excl_oid)) {
c972bf4c
JK
1369 strvec_push(&args, "--not");
1370 strvec_push(&args, oid_to_hex(excl_oid));
4d36f88b 1371 }
a6d7eb2c 1372
6245b98b 1373 collect_changed_submodules(r, &subs, &args);
a6d7eb2c
SB
1374 ret = subs.nr;
1375
c972bf4c 1376 strvec_clear(&args);
a6d7eb2c 1377
6e1e0c99 1378 free_submodules_data(&subs);
a6d7eb2c
SB
1379 return ret;
1380}
1381
fe85ee6e 1382struct submodule_parallel_fetch {
b90d9f76
GC
1383 /*
1384 * The index of the last index entry processed by
1385 * get_fetch_task_from_index().
1386 */
1387 int index_count;
1388 /*
1389 * The index of the last string_list entry processed by
1390 * get_fetch_task_from_changed().
1391 */
1392 int changed_count;
c972bf4c 1393 struct strvec args;
e724197f 1394 struct repository *r;
fe85ee6e
SB
1395 const char *prefix;
1396 int command_line_option;
8fa29159 1397 int default_option;
fe85ee6e
SB
1398 int quiet;
1399 int result;
16dd6fe1 1400
b90d9f76
GC
1401 /*
1402 * Names of submodules that have new commits. Generated by
1403 * walking the newly fetched superproject commits.
1404 */
16dd6fe1 1405 struct string_list changed_submodule_names;
b90d9f76
GC
1406 /*
1407 * Names of submodules that have already been processed. Lets us
1408 * avoid fetching the same submodule more than once.
1409 */
1410 struct string_list seen_submodule_names;
be76c212
SB
1411
1412 /* Pending fetches by OIDs */
1413 struct fetch_task **oid_fetch_tasks;
1414 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
02225408
ES
1415
1416 struct strbuf submodules_with_errors;
fe85ee6e 1417};
f69a6e4f
ÆAB
1418#define SPF_INIT { \
1419 .args = STRVEC_INIT, \
1420 .changed_submodule_names = STRING_LIST_INIT_DUP, \
b90d9f76 1421 .seen_submodule_names = STRING_LIST_INIT_DUP, \
f69a6e4f
ÆAB
1422 .submodules_with_errors = STRBUF_INIT, \
1423}
fe85ee6e 1424
4b4acedd
HV
1425static int get_fetch_recurse_config(const struct submodule *submodule,
1426 struct submodule_parallel_fetch *spf)
1427{
1428 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1429 return spf->command_line_option;
1430
1431 if (submodule) {
1432 char *key;
1433 const char *value;
1434
1435 int fetch_recurse = submodule->fetch_recurse;
1436 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
f1de981e 1437 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
4b4acedd
HV
1438 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1439 }
1440 free(key);
1441
1442 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1443 /* local config overrules everything except commandline */
1444 return fetch_recurse;
1445 }
1446
1447 return spf->default_option;
1448}
1449
be76c212
SB
1450/*
1451 * Fetch in progress (if callback data) or
1452 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1453 */
1454struct fetch_task {
1455 struct repository *repo;
1456 const struct submodule *sub;
1457 unsigned free_sub : 1; /* Do we need to free the submodule? */
73bc90d7 1458 const char *default_argv; /* The default fetch mode. */
b90d9f76 1459 struct strvec git_args; /* Args for the child git process. */
be76c212
SB
1460
1461 struct oid_array *commits; /* Ensure these commits are fetched */
1462};
1463
1464/**
1465 * When a submodule is not defined in .gitmodules, we cannot access it
1466 * via the regular submodule-config. Create a fake submodule, which we can
1467 * work on.
1468 */
1469static const struct submodule *get_non_gitmodules_submodule(const char *path)
1470{
1471 struct submodule *ret = NULL;
1472 const char *name = default_name_or_path(path);
1473
1474 if (!name)
1475 return NULL;
1476
1477 ret = xmalloc(sizeof(*ret));
1478 memset(ret, 0, sizeof(*ret));
1479 ret->path = name;
1480 ret->name = name;
1481
1482 return (const struct submodule *) ret;
1483}
1484
be76c212
SB
1485static void fetch_task_release(struct fetch_task *p)
1486{
1487 if (p->free_sub)
1488 free((void*)p->sub);
1489 p->free_sub = 0;
1490 p->sub = NULL;
1491
1492 if (p->repo)
1493 repo_clear(p->repo);
1494 FREE_AND_NULL(p->repo);
b90d9f76
GC
1495
1496 strvec_clear(&p->git_args);
be76c212
SB
1497}
1498
26f80ccf 1499static struct repository *get_submodule_repo_for(struct repository *r,
7c2f8cc5
GC
1500 const char *path,
1501 const struct object_id *treeish_name)
26f80ccf
SB
1502{
1503 struct repository *ret = xmalloc(sizeof(*ret));
1504
7c2f8cc5 1505 if (repo_submodule_init(ret, r, path, treeish_name)) {
5df5106e
JT
1506 free(ret);
1507 return NULL;
26f80ccf
SB
1508 }
1509
1510 return ret;
1511}
1512
5370b91f
GC
1513static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1514 const char *path,
1515 const struct object_id *treeish_name)
1516{
1517 struct fetch_task *task = xmalloc(sizeof(*task));
1518 memset(task, 0, sizeof(*task));
1519
1520 task->sub = submodule_from_path(spf->r, treeish_name, path);
1521
1522 if (!task->sub) {
1523 /*
1524 * No entry in .gitmodules? Technically not a submodule,
1525 * but historically we supported repositories that happen to be
1526 * in-place where a gitlink is. Keep supporting them.
1527 */
1528 task->sub = get_non_gitmodules_submodule(path);
1529 if (!task->sub)
1530 goto cleanup;
1531
1532 task->free_sub = 1;
1533 }
1534
b90d9f76
GC
1535 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1536 goto cleanup;
1537
5370b91f
GC
1538 switch (get_fetch_recurse_config(task->sub, spf))
1539 {
1540 default:
1541 case RECURSE_SUBMODULES_DEFAULT:
1542 case RECURSE_SUBMODULES_ON_DEMAND:
1543 if (!task->sub ||
1544 !string_list_lookup(
1545 &spf->changed_submodule_names,
1546 task->sub->name))
1547 goto cleanup;
1548 task->default_argv = "on-demand";
1549 break;
1550 case RECURSE_SUBMODULES_ON:
1551 task->default_argv = "yes";
1552 break;
1553 case RECURSE_SUBMODULES_OFF:
1554 goto cleanup;
1555 }
1556
1557 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1558
1559 return task;
1560
1561 cleanup:
1562 fetch_task_release(task);
1563 free(task);
1564 return NULL;
1565}
1566
73bc90d7 1567static struct fetch_task *
b90d9f76
GC
1568get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1569 struct strbuf *err)
7dce19d3 1570{
b90d9f76
GC
1571 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1572 const struct cache_entry *ce =
1573 spf->r->index->cache[spf->index_count];
be76c212 1574 struct fetch_task *task;
7dce19d3
JL
1575
1576 if (!S_ISGITLINK(ce->ce_mode))
1577 continue;
1578
5370b91f 1579 task = fetch_task_create(spf, ce->name, null_oid());
be76c212
SB
1580 if (!task)
1581 continue;
492c6c46 1582
be76c212 1583 if (task->repo) {
fe85ee6e 1584 if (!spf->quiet)
a4ffbbbb 1585 strbuf_addf(err, _("Fetching submodule %s%s\n"),
fe85ee6e 1586 spf->prefix, ce->name);
26f80ccf 1587
b90d9f76 1588 spf->index_count++;
73bc90d7 1589 return task;
26f80ccf 1590 } else {
505a2765 1591 struct strbuf empty_submodule_path = STRBUF_INIT;
be76c212
SB
1592
1593 fetch_task_release(task);
1594 free(task);
1595
26f80ccf
SB
1596 /*
1597 * An empty directory is normal,
1598 * the submodule is not initialized
1599 */
505a2765
PK
1600 strbuf_addf(&empty_submodule_path, "%s/%s/",
1601 spf->r->worktree,
1602 ce->name);
26f80ccf 1603 if (S_ISGITLINK(ce->ce_mode) &&
505a2765 1604 !is_empty_dir(empty_submodule_path.buf)) {
26f80ccf
SB
1605 spf->result = 1;
1606 strbuf_addf(err,
303b3c1c 1607 _("Could not access submodule '%s'\n"),
26f80ccf
SB
1608 ce->name);
1609 }
505a2765 1610 strbuf_release(&empty_submodule_path);
fe85ee6e 1611 }
7dce19d3 1612 }
73bc90d7
GC
1613 return NULL;
1614}
1615
b90d9f76
GC
1616static struct fetch_task *
1617get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1618 struct strbuf *err)
1619{
1620 for (; spf->changed_count < spf->changed_submodule_names.nr;
1621 spf->changed_count++) {
1622 struct string_list_item item =
1623 spf->changed_submodule_names.items[spf->changed_count];
1624 struct changed_submodule_data *cs_data = item.util;
1625 struct fetch_task *task;
1626
1627 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1628 continue;
1629
1630 task = fetch_task_create(spf, cs_data->path,
1631 cs_data->super_oid);
1632 if (!task)
1633 continue;
1634
1635 if (!task->repo) {
1636 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1637 cs_data->path,
d850b7a5 1638 repo_find_unique_abbrev(the_repository, cs_data->super_oid, DEFAULT_ABBREV));
b90d9f76
GC
1639
1640 fetch_task_release(task);
1641 free(task);
1642 continue;
1643 }
1644
1645 if (!spf->quiet)
1646 strbuf_addf(err,
1647 _("Fetching submodule %s%s at commit %s\n"),
1648 spf->prefix, task->sub->path,
d850b7a5
ÆAB
1649 repo_find_unique_abbrev(the_repository, cs_data->super_oid,
1650 DEFAULT_ABBREV));
b90d9f76
GC
1651
1652 spf->changed_count++;
1653 /*
1654 * NEEDSWORK: Submodules set/unset a value for
1655 * core.worktree when they are populated/unpopulated by
1656 * "git checkout" (and similar commands, see
1657 * submodule_move_head() and
1658 * connect_work_tree_and_git_dir()), but if the
1659 * submodule is unpopulated in another way (e.g. "git
1660 * rm", "rm -r"), core.worktree will still be set even
1661 * though the directory doesn't exist, and the child
1662 * process will crash while trying to chdir into the
1663 * nonexistent directory.
1664 *
1665 * In this case, we know that the submodule has no
1666 * working tree, so we can work around this by
1667 * setting "--work-tree=." (--bare does not work because
1668 * worktree settings take precedence over bare-ness).
1669 * However, this is not necessarily true in other cases,
1670 * so a generalized solution is still necessary.
1671 *
1672 * Possible solutions:
1673 * - teach "git [add|rm]" to unset core.worktree and
1674 * discourage users from removing submodules without
1675 * using a Git command.
1676 * - teach submodule child processes to ignore stale
1677 * core.worktree values.
1678 */
1679 strvec_push(&task->git_args, "--work-tree=.");
1680 return task;
1681 }
1682 return NULL;
1683}
1684
73bc90d7
GC
1685static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1686 void *data, void **task_cb)
1687{
1688 struct submodule_parallel_fetch *spf = data;
b90d9f76
GC
1689 struct fetch_task *task =
1690 get_fetch_task_from_index(spf, err);
1691 if (!task)
1692 task = get_fetch_task_from_changed(spf, err);
73bc90d7
GC
1693
1694 if (task) {
73bc90d7
GC
1695 child_process_init(cp);
1696 cp->dir = task->repo->gitdir;
29fda24d 1697 prepare_submodule_repo_env_in_gitdir(&cp->env);
73bc90d7
GC
1698 cp->git_cmd = 1;
1699 strvec_init(&cp->args);
b90d9f76
GC
1700 if (task->git_args.nr)
1701 strvec_pushv(&cp->args, task->git_args.v);
73bc90d7
GC
1702 strvec_pushv(&cp->args, spf->args.v);
1703 strvec_push(&cp->args, task->default_argv);
9a97b43e
RS
1704 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1705 spf->prefix, task->sub->path);
73bc90d7 1706
73bc90d7
GC
1707 *task_cb = task;
1708
b90d9f76 1709 string_list_insert(&spf->seen_submodule_names, task->sub->name);
73bc90d7
GC
1710 return 1;
1711 }
be76c212
SB
1712
1713 if (spf->oid_fetch_tasks_nr) {
1714 struct fetch_task *task =
1715 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
be76c212
SB
1716 spf->oid_fetch_tasks_nr--;
1717
be76c212 1718 child_process_init(cp);
29fda24d 1719 prepare_submodule_repo_env_in_gitdir(&cp->env);
be76c212
SB
1720 cp->git_cmd = 1;
1721 cp->dir = task->repo->gitdir;
1722
c972bf4c 1723 strvec_init(&cp->args);
d70a9eb6 1724 strvec_pushv(&cp->args, spf->args.v);
c972bf4c 1725 strvec_push(&cp->args, "on-demand");
9a97b43e
RS
1726 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/",
1727 spf->prefix, task->sub->path);
be76c212
SB
1728
1729 /* NEEDSWORK: have get_default_remote from submodule--helper */
c972bf4c 1730 strvec_push(&cp->args, "origin");
be76c212
SB
1731 oid_array_for_each_unique(task->commits,
1732 append_oid_to_argv, &cp->args);
1733
1734 *task_cb = task;
be76c212 1735 return 1;
7dce19d3 1736 }
be76c212 1737
fe85ee6e
SB
1738 return 0;
1739}
1740
a5c76b36 1741static int fetch_start_failure(struct strbuf *err UNUSED,
fe85ee6e
SB
1742 void *cb, void *task_cb)
1743{
1744 struct submodule_parallel_fetch *spf = cb;
be76c212 1745 struct fetch_task *task = task_cb;
fe85ee6e
SB
1746
1747 spf->result = 1;
1748
be76c212 1749 fetch_task_release(task);
fe85ee6e
SB
1750 return 0;
1751}
1752
be76c212
SB
1753static int commit_missing_in_sub(const struct object_id *oid, void *data)
1754{
1755 struct repository *subrepo = data;
1756
1757 enum object_type type = oid_object_info(subrepo, oid, NULL);
1758
1759 return type != OBJ_COMMIT;
1760}
1761
a5c76b36 1762static int fetch_finish(int retvalue, struct strbuf *err UNUSED,
2a73b3da 1763 void *cb, void *task_cb)
fe85ee6e
SB
1764{
1765 struct submodule_parallel_fetch *spf = cb;
be76c212
SB
1766 struct fetch_task *task = task_cb;
1767
1768 struct string_list_item *it;
6e1e0c99 1769 struct changed_submodule_data *cs_data;
fe85ee6e 1770
02225408
ES
1771 if (!task || !task->sub)
1772 BUG("callback cookie bogus");
1773
1774 if (retvalue) {
bd5e567d
JT
1775 /*
1776 * NEEDSWORK: This indicates that the overall fetch
1777 * failed, even though there may be a subsequent fetch
1778 * by commit hash that might work. It may be a good
1779 * idea to not indicate failure in this case, and only
1780 * indicate failure if the subsequent fetch fails.
1781 */
fe85ee6e
SB
1782 spf->result = 1;
1783
02225408
ES
1784 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1785 task->sub->name);
1786 }
be76c212
SB
1787
1788 /* Is this the second time we process this submodule? */
1789 if (task->commits)
1790 goto out;
1791
1792 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1793 if (!it)
1794 /* Could be an unchanged submodule, not contained in the list */
1795 goto out;
1796
6e1e0c99
GC
1797 cs_data = it->util;
1798 oid_array_filter(&cs_data->new_commits,
be76c212
SB
1799 commit_missing_in_sub,
1800 task->repo);
1801
1802 /* Are there commits we want, but do not exist? */
6e1e0c99
GC
1803 if (cs_data->new_commits.nr) {
1804 task->commits = &cs_data->new_commits;
be76c212
SB
1805 ALLOC_GROW(spf->oid_fetch_tasks,
1806 spf->oid_fetch_tasks_nr + 1,
1807 spf->oid_fetch_tasks_alloc);
1808 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1809 spf->oid_fetch_tasks_nr++;
1810 return 0;
1811 }
1812
1813out:
1814 fetch_task_release(task);
1815
fe85ee6e
SB
1816 return 0;
1817}
1818
b90d9f76
GC
1819int fetch_submodules(struct repository *r,
1820 const struct strvec *options,
1821 const char *prefix, int command_line_option,
1822 int default_option,
1823 int quiet, int max_parallel_jobs)
fe85ee6e
SB
1824{
1825 int i;
fe85ee6e 1826 struct submodule_parallel_fetch spf = SPF_INIT;
36d69bf7
ÆAB
1827 const struct run_process_parallel_opts opts = {
1828 .tr2_category = "submodule",
1829 .tr2_label = "parallel/fetch",
1830
1831 .processes = max_parallel_jobs,
1832
1833 .get_next_task = get_next_submodule,
1834 .start_failure = fetch_start_failure,
1835 .task_finished = fetch_finish,
1836 .data = &spf,
1837 };
fe85ee6e 1838
e724197f 1839 spf.r = r;
fe85ee6e 1840 spf.command_line_option = command_line_option;
8fa29159 1841 spf.default_option = default_option;
fe85ee6e
SB
1842 spf.quiet = quiet;
1843 spf.prefix = prefix;
1844
e724197f 1845 if (!r->worktree)
fe85ee6e
SB
1846 goto out;
1847
e724197f 1848 if (repo_read_index(r) < 0)
a4ffbbbb 1849 die(_("index file corrupt"));
fe85ee6e 1850
c972bf4c 1851 strvec_push(&spf.args, "fetch");
d70a9eb6
JK
1852 for (i = 0; i < options->nr; i++)
1853 strvec_push(&spf.args, options->v[i]);
c972bf4c 1854 strvec_push(&spf.args, "--recurse-submodules-default");
fe85ee6e
SB
1855 /* default value, "--submodule-prefix" and its value are added later */
1856
16dd6fe1
SB
1857 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1858 string_list_sort(&spf.changed_submodule_names);
36d69bf7 1859 run_processes_parallel(&opts);
fe85ee6e 1860
02225408
ES
1861 if (spf.submodules_with_errors.len > 0)
1862 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1863 spf.submodules_with_errors.buf);
1864
1865
c972bf4c 1866 strvec_clear(&spf.args);
88a21979 1867out:
6e1e0c99 1868 free_submodules_data(&spf.changed_submodule_names);
fe85ee6e 1869 return spf.result;
7dce19d3
JL
1870}
1871
3bfc4504 1872unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 1873{
d3180279 1874 struct child_process cp = CHILD_PROCESS_INIT;
ee6fc514 1875 struct strbuf buf = STRBUF_INIT;
af6865a7 1876 FILE *fp;
c7e1a736 1877 unsigned dirty_submodule = 0;
eee49b6c 1878 const char *git_dir;
af6865a7 1879 int ignore_cp_exit_code = 0;
ee6fc514 1880
eee49b6c 1881 strbuf_addf(&buf, "%s/.git", path);
13d6ec91 1882 git_dir = read_gitfile(buf.buf);
eee49b6c
JL
1883 if (!git_dir)
1884 git_dir = buf.buf;
5c896f7c
SB
1885 if (!is_git_directory(git_dir)) {
1886 if (is_directory(git_dir))
1887 die(_("'%s' not recognized as a git repository"), git_dir);
ee6fc514
JL
1888 strbuf_release(&buf);
1889 /* The submodule is not checked out, so it is not modified */
1890 return 0;
ee6fc514
JL
1891 }
1892 strbuf_reset(&buf);
1893
c972bf4c 1894 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
3bfc4504 1895 if (ignore_untracked)
c972bf4c 1896 strvec_push(&cp.args, "-uno");
3bfc4504 1897
29fda24d 1898 prepare_submodule_repo_env(&cp.env);
ee6fc514
JL
1899 cp.git_cmd = 1;
1900 cp.no_stdin = 1;
1901 cp.out = -1;
eee49b6c 1902 cp.dir = path;
ee6fc514 1903 if (start_command(&cp))
a4ffbbbb 1904 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
ee6fc514 1905
af6865a7
SB
1906 fp = xfdopen(cp.out, "r");
1907 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
fcecf0b9
SB
1908 /* regular untracked files */
1909 if (buf.buf[0] == '?')
c7e1a736 1910 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
40069d6e
SB
1911
1912 if (buf.buf[0] == 'u' ||
1913 buf.buf[0] == '1' ||
1914 buf.buf[0] == '2') {
1915 /* T = line type, XY = status, SSSS = submodule state */
1916 if (buf.len < strlen("T XY SSSS"))
033abf97 1917 BUG("invalid status --porcelain=2 line %s",
40069d6e
SB
1918 buf.buf);
1919
1920 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1921 /* nested untracked file */
1922 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1923
1924 if (buf.buf[0] == 'u' ||
1925 buf.buf[0] == '2' ||
1926 memcmp(buf.buf + 5, "S..U", 4))
1927 /* other change */
1928 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
c7e1a736 1929 }
64f9a946
SB
1930
1931 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1932 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
af6865a7
SB
1933 ignore_untracked)) {
1934 /*
1935 * We're not interested in any further information from
1936 * the child any more, neither output nor its exit code.
1937 */
1938 ignore_cp_exit_code = 1;
c7e1a736 1939 break;
af6865a7 1940 }
c7e1a736 1941 }
af6865a7 1942 fclose(fp);
ee6fc514 1943
af6865a7 1944 if (finish_command(&cp) && !ignore_cp_exit_code)
a4ffbbbb 1945 die(_("'git status --porcelain=2' failed in submodule %s"), path);
ee6fc514 1946
ee6fc514 1947 strbuf_release(&buf);
c7e1a736 1948 return dirty_submodule;
ee6fc514 1949}
68d03e4a 1950
293ab15e
JL
1951int submodule_uses_gitfile(const char *path)
1952{
d3180279 1953 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e
JL
1954 struct strbuf buf = STRBUF_INIT;
1955 const char *git_dir;
1956
1957 strbuf_addf(&buf, "%s/.git", path);
1958 git_dir = read_gitfile(buf.buf);
1959 if (!git_dir) {
1960 strbuf_release(&buf);
1961 return 0;
1962 }
1963 strbuf_release(&buf);
1964
1965 /* Now test that all nested submodules use a gitfile too */
afbdba39
JH
1966 strvec_pushl(&cp.args,
1967 "submodule", "foreach", "--quiet", "--recursive",
1968 "test -f .git", NULL);
1969
29fda24d 1970 prepare_submodule_repo_env(&cp.env);
293ab15e
JL
1971 cp.git_cmd = 1;
1972 cp.no_stdin = 1;
1973 cp.no_stderr = 1;
1974 cp.no_stdout = 1;
1975 cp.dir = path;
1976 if (run_command(&cp))
1977 return 0;
1978
1979 return 1;
1980}
1981
83b76966
SB
1982/*
1983 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1984 * when doing so.
1985 *
1986 * Return 1 if we'd lose data, return 0 if the removal is fine,
1987 * and negative values for errors.
1988 */
1989int bad_to_remove_submodule(const char *path, unsigned flags)
293ab15e 1990{
293ab15e 1991 ssize_t len;
d3180279 1992 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e 1993 struct strbuf buf = STRBUF_INIT;
83b76966 1994 int ret = 0;
293ab15e 1995
dbe44faa 1996 if (!file_exists(path) || is_empty_dir(path))
83b76966 1997 return 0;
293ab15e
JL
1998
1999 if (!submodule_uses_gitfile(path))
83b76966 2000 return 1;
293ab15e 2001
c972bf4c 2002 strvec_pushl(&cp.args, "status", "--porcelain",
f6d8942b 2003 "--ignore-submodules=none", NULL);
83b76966
SB
2004
2005 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
c972bf4c 2006 strvec_push(&cp.args, "-uno");
83b76966 2007 else
c972bf4c 2008 strvec_push(&cp.args, "-uall");
83b76966
SB
2009
2010 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
c972bf4c 2011 strvec_push(&cp.args, "--ignored");
293ab15e 2012
29fda24d 2013 prepare_submodule_repo_env(&cp.env);
293ab15e
JL
2014 cp.git_cmd = 1;
2015 cp.no_stdin = 1;
2016 cp.out = -1;
2017 cp.dir = path;
83b76966
SB
2018 if (start_command(&cp)) {
2019 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
35ad44cb 2020 die(_("could not start 'git status' in submodule '%s'"),
83b76966
SB
2021 path);
2022 ret = -1;
2023 goto out;
2024 }
293ab15e
JL
2025
2026 len = strbuf_read(&buf, cp.out, 1024);
2027 if (len > 2)
83b76966 2028 ret = 1;
293ab15e
JL
2029 close(cp.out);
2030
83b76966
SB
2031 if (finish_command(&cp)) {
2032 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
35ad44cb 2033 die(_("could not run 'git status' in submodule '%s'"),
83b76966
SB
2034 path);
2035 ret = -1;
2036 }
2037out:
293ab15e 2038 strbuf_release(&buf);
83b76966 2039 return ret;
293ab15e
JL
2040}
2041
898c2e65
SB
2042void submodule_unset_core_worktree(const struct submodule *sub)
2043{
ce125d43 2044 struct strbuf config_path = STRBUF_INIT;
898c2e65 2045
ce125d43
JT
2046 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2047 strbuf_addstr(&config_path, "/config");
2048
42d5c033 2049 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL, NULL))
898c2e65
SB
2050 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2051 sub->path);
2052
ce125d43 2053 strbuf_release(&config_path);
898c2e65
SB
2054}
2055
6e3c1595
SB
2056static int submodule_has_dirty_index(const struct submodule *sub)
2057{
2058 struct child_process cp = CHILD_PROCESS_INIT;
2059
29fda24d 2060 prepare_submodule_repo_env(&cp.env);
6e3c1595
SB
2061
2062 cp.git_cmd = 1;
c972bf4c 2063 strvec_pushl(&cp.args, "diff-index", "--quiet",
f6d8942b 2064 "--cached", "HEAD", NULL);
6e3c1595
SB
2065 cp.no_stdin = 1;
2066 cp.no_stdout = 1;
2067 cp.dir = sub->path;
2068 if (start_command(&cp))
a4ffbbbb 2069 die(_("could not recurse into submodule '%s'"), sub->path);
6e3c1595
SB
2070
2071 return finish_command(&cp);
2072}
2073
4002ec3d 2074static void submodule_reset_index(const char *path, const char *super_prefix)
6e3c1595
SB
2075{
2076 struct child_process cp = CHILD_PROCESS_INIT;
29fda24d 2077 prepare_submodule_repo_env(&cp.env);
6e3c1595
SB
2078
2079 cp.git_cmd = 1;
2080 cp.no_stdin = 1;
2081 cp.dir = path;
2082
94b7f156 2083 /* TODO: determine if this might overwright untracked files */
c972bf4c 2084 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
4002ec3d
ÆAB
2085 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2086 (super_prefix ? super_prefix : ""), path);
6e3c1595 2087
c972bf4c 2088 strvec_push(&cp.args, empty_tree_oid_hex());
6e3c1595
SB
2089
2090 if (run_command(&cp))
a4ffbbbb 2091 die(_("could not reset submodule index"));
6e3c1595
SB
2092}
2093
2094/**
2095 * Moves a submodule at a given path from a given head to another new head.
2096 * For edge cases (a submodule coming into existence or removing a submodule)
2097 * pass NULL for old or new respectively.
2098 */
4002ec3d
ÆAB
2099int submodule_move_head(const char *path, const char *super_prefix,
2100 const char *old_head, const char *new_head,
2101 unsigned flags)
6e3c1595
SB
2102{
2103 int ret = 0;
2104 struct child_process cp = CHILD_PROCESS_INIT;
2105 const struct submodule *sub;
f2d48994 2106 int *error_code_ptr, error_code;
6e3c1595 2107
627d9342 2108 if (!is_submodule_active(the_repository, path))
823bab09
SB
2109 return 0;
2110
f2d48994
SB
2111 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2112 /*
2113 * Pass non NULL pointer to is_submodule_populated_gently
2114 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2115 * to fixup the submodule in the force case later.
2116 */
2117 error_code_ptr = &error_code;
2118 else
2119 error_code_ptr = NULL;
2120
bc099914 2121 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
f2d48994 2122 return 0;
6e3c1595 2123
14228447 2124 sub = submodule_from_path(the_repository, null_oid(), path);
6e3c1595
SB
2125
2126 if (!sub)
033abf97 2127 BUG("could not get submodule information for '%s'", path);
6e3c1595 2128
bc099914 2129 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
6e3c1595
SB
2130 /* Check if the submodule has a dirty index. */
2131 if (submodule_has_dirty_index(sub))
2132 return error(_("submodule '%s' has dirty index"), path);
2133 }
2134
2135 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
bc099914 2136 if (old_head) {
6e3c1595 2137 if (!submodule_uses_gitfile(path))
f0a5e5ad 2138 absorb_git_dir_into_superproject(path,
4002ec3d 2139 super_prefix);
6e3c1595 2140 } else {
ce125d43
JT
2141 struct strbuf gitdir = STRBUF_INIT;
2142 submodule_name_to_gitdir(&gitdir, the_repository,
2143 sub->name);
2144 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2145 strbuf_release(&gitdir);
6e3c1595
SB
2146
2147 /* make sure the index is clean as well */
4002ec3d 2148 submodule_reset_index(path, super_prefix);
6e3c1595 2149 }
f2d48994 2150
bc099914 2151 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
ce125d43
JT
2152 struct strbuf gitdir = STRBUF_INIT;
2153 submodule_name_to_gitdir(&gitdir, the_repository,
2154 sub->name);
2155 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2156 strbuf_release(&gitdir);
f2d48994 2157 }
6e3c1595
SB
2158 }
2159
29fda24d 2160 prepare_submodule_repo_env(&cp.env);
6e3c1595
SB
2161
2162 cp.git_cmd = 1;
2163 cp.no_stdin = 1;
2164 cp.dir = path;
2165
c972bf4c 2166 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
4002ec3d
ÆAB
2167 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
2168 (super_prefix ? super_prefix : ""), path);
6e3c1595
SB
2169
2170 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
c972bf4c 2171 strvec_push(&cp.args, "-n");
6e3c1595 2172 else
c972bf4c 2173 strvec_push(&cp.args, "-u");
6e3c1595
SB
2174
2175 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
c972bf4c 2176 strvec_push(&cp.args, "--reset");
6e3c1595 2177 else
c972bf4c 2178 strvec_push(&cp.args, "-m");
6e3c1595 2179
7dcc1f4d 2180 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
c972bf4c 2181 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
7dcc1f4d 2182
c972bf4c 2183 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
6e3c1595
SB
2184
2185 if (run_command(&cp)) {
ba95d4e4 2186 ret = error(_("Submodule '%s' could not be updated."), path);
6e3c1595
SB
2187 goto out;
2188 }
2189
2190 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
bc099914 2191 if (new_head) {
a17062cf 2192 child_process_init(&cp);
6e3c1595 2193 /* also set the HEAD accordingly */
a17062cf
SB
2194 cp.git_cmd = 1;
2195 cp.no_stdin = 1;
2196 cp.dir = path;
6e3c1595 2197
29fda24d 2198 prepare_submodule_repo_env(&cp.env);
c972bf4c 2199 strvec_pushl(&cp.args, "update-ref", "HEAD",
f6d8942b 2200 "--no-deref", new_head, NULL);
6e3c1595 2201
a17062cf 2202 if (run_command(&cp)) {
6e3c1595
SB
2203 ret = -1;
2204 goto out;
2205 }
2206 } else {
2207 struct strbuf sb = STRBUF_INIT;
2208
2209 strbuf_addf(&sb, "%s/.git", path);
2210 unlink_or_warn(sb.buf);
2211 strbuf_release(&sb);
2212
2213 if (is_empty_dir(path))
2214 rmdir_or_warn(path);
898c2e65
SB
2215
2216 submodule_unset_core_worktree(sub);
6e3c1595
SB
2217 }
2218 }
2219out:
2220 return ret;
2221}
2222
a8dee3ca
JS
2223int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2224{
2225 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2226 char *p;
2227 int ret = 0;
2228
2229 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2230 strcmp(p, submodule_name))
2231 BUG("submodule name '%s' not a suffix of git dir '%s'",
2232 submodule_name, git_dir);
2233
2234 /*
2235 * We prevent the contents of sibling submodules' git directories to
2236 * clash.
2237 *
2238 * Example: having a submodule named `hippo` and another one named
2239 * `hippo/hooks` would result in the git directories
2240 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2241 * but the latter directory is already designated to contain the hooks
2242 * of the former.
2243 */
2244 for (; *p; p++) {
2245 if (is_dir_sep(*p)) {
2246 char c = *p;
2247
2248 *p = '\0';
2249 if (is_git_directory(git_dir))
2250 ret = -1;
2251 *p = c;
2252
2253 if (ret < 0)
2254 return error(_("submodule git dir '%s' is "
2255 "inside git dir '%.*s'"),
2256 git_dir,
2257 (int)(p - git_dir), git_dir);
2258 }
2259 }
2260
2261 return 0;
2262}
2263
f6f85861
SB
2264/*
2265 * Embeds a single submodules git directory into the superprojects git dir,
2266 * non recursively.
2267 */
bb61a962
ÆAB
2268static void relocate_single_git_dir_into_superproject(const char *path,
2269 const char *super_prefix)
f6f85861
SB
2270{
2271 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
ce125d43 2272 struct strbuf new_gitdir = STRBUF_INIT;
f6f85861
SB
2273 const struct submodule *sub;
2274
2275 if (submodule_uses_worktrees(path))
2276 die(_("relocate_gitdir for submodule '%s' with "
2277 "more than one worktree not supported"), path);
2278
2279 old_git_dir = xstrfmt("%s/.git", path);
2280 if (read_gitfile(old_git_dir))
2281 /* If it is an actual gitfile, it doesn't need migration. */
2282 return;
2283
ce83eadd 2284 real_old_git_dir = real_pathdup(old_git_dir, 1);
f6f85861 2285
14228447 2286 sub = submodule_from_path(the_repository, null_oid(), path);
f6f85861
SB
2287 if (!sub)
2288 die(_("could not lookup name for submodule '%s'"), path);
2289
ce125d43
JT
2290 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2291 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
a8dee3ca
JS
2292 die(_("refusing to move '%s' into an existing git dir"),
2293 real_old_git_dir);
ce125d43
JT
2294 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2295 die(_("could not create directory '%s'"), new_gitdir.buf);
2296 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
f6f85861 2297
f6f85861 2298 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
bb61a962 2299 super_prefix ? super_prefix : "", path,
f6f85861
SB
2300 real_old_git_dir, real_new_git_dir);
2301
2302 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2303
2304 free(old_git_dir);
2305 free(real_old_git_dir);
2306 free(real_new_git_dir);
ce125d43 2307 strbuf_release(&new_gitdir);
f6f85861
SB
2308}
2309
f0a5e5ad
ÆAB
2310static void absorb_git_dir_into_superproject_recurse(const char *path,
2311 const char *super_prefix)
46e87b54
ÆAB
2312{
2313
2314 struct child_process cp = CHILD_PROCESS_INIT;
2315
2316 cp.dir = path;
2317 cp.git_cmd = 1;
2318 cp.no_stdin = 1;
46e87b54
ÆAB
2319 strvec_pushl(&cp.args, "submodule--helper",
2320 "absorbgitdirs", NULL);
bb61a962
ÆAB
2321 strvec_pushf(&cp.args, "--super-prefix=%s%s/", super_prefix ?
2322 super_prefix : "", path);
2323
46e87b54
ÆAB
2324 prepare_submodule_repo_env(&cp.env);
2325 if (run_command(&cp))
2326 die(_("could not recurse into submodule '%s'"), path);
2327}
2328
f6f85861
SB
2329/*
2330 * Migrate the git directory of the submodule given by path from
2331 * having its git directory within the working tree to the git dir nested
2332 * in its superprojects git dir under modules/.
2333 */
f0a5e5ad
ÆAB
2334void absorb_git_dir_into_superproject(const char *path,
2335 const char *super_prefix)
f6f85861 2336{
ec9629b3
SB
2337 int err_code;
2338 const char *sub_git_dir;
f6f85861 2339 struct strbuf gitdir = STRBUF_INIT;
f6f85861 2340 strbuf_addf(&gitdir, "%s/.git", path);
ec9629b3 2341 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
f6f85861
SB
2342
2343 /* Not populated? */
ec9629b3 2344 if (!sub_git_dir) {
ec9629b3 2345 const struct submodule *sub;
ce125d43 2346 struct strbuf sub_gitdir = STRBUF_INIT;
ec9629b3
SB
2347
2348 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2349 /* unpopulated as expected */
2350 strbuf_release(&gitdir);
2351 return;
2352 }
2353
2354 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2355 /* We don't know what broke here. */
2356 read_gitfile_error_die(err_code, path, NULL);
2357
2358 /*
2359 * Maybe populated, but no git directory was found?
2360 * This can happen if the superproject is a submodule
2361 * itself and was just absorbed. The absorption of the
2362 * superproject did not rewrite the git file links yet,
2363 * fix it now.
2364 */
14228447 2365 sub = submodule_from_path(the_repository, null_oid(), path);
ec9629b3
SB
2366 if (!sub)
2367 die(_("could not lookup name for submodule '%s'"), path);
ce125d43
JT
2368 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2369 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2370 strbuf_release(&sub_gitdir);
ec9629b3
SB
2371 } else {
2372 /* Is it already absorbed into the superprojects git dir? */
ce83eadd
JS
2373 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2374 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
f6f85861 2375
ec9629b3 2376 if (!starts_with(real_sub_git_dir, real_common_git_dir))
bb61a962 2377 relocate_single_git_dir_into_superproject(path, super_prefix);
ec9629b3
SB
2378
2379 free(real_sub_git_dir);
2380 free(real_common_git_dir);
2381 }
2382 strbuf_release(&gitdir);
f6f85861 2383
f0a5e5ad 2384 absorb_git_dir_into_superproject_recurse(path, super_prefix);
f6f85861 2385}
bf0231c6 2386
49d3c4b4 2387int get_superproject_working_tree(struct strbuf *buf)
bf0231c6
SB
2388{
2389 struct child_process cp = CHILD_PROCESS_INIT;
2390 struct strbuf sb = STRBUF_INIT;
4530a85b 2391 struct strbuf one_up = STRBUF_INIT;
bc57ba1d 2392 char *cwd = xgetcwd();
49d3c4b4 2393 int ret = 0;
bf0231c6
SB
2394 const char *subpath;
2395 int code;
2396 ssize_t len;
2397
2398 if (!is_inside_work_tree())
2399 /*
2400 * FIXME:
2401 * We might have a superproject, but it is harder
2402 * to determine.
2403 */
49d3c4b4 2404 return 0;
bf0231c6 2405
4530a85b 2406 if (!strbuf_realpath(&one_up, "../", 0))
49d3c4b4 2407 return 0;
bf0231c6 2408
4530a85b
AM
2409 subpath = relative_path(cwd, one_up.buf, &sb);
2410 strbuf_release(&one_up);
bf0231c6 2411
29fda24d
ÆAB
2412 prepare_submodule_repo_env(&cp.env);
2413 strvec_pop(&cp.env);
bf0231c6 2414
c972bf4c 2415 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
f6d8942b
JK
2416 "ls-files", "-z", "--stage", "--full-name", "--",
2417 subpath, NULL);
bf0231c6
SB
2418 strbuf_reset(&sb);
2419
2420 cp.no_stdin = 1;
2421 cp.no_stderr = 1;
2422 cp.out = -1;
2423 cp.git_cmd = 1;
2424
2425 if (start_command(&cp))
2426 die(_("could not start ls-files in .."));
2427
2428 len = strbuf_read(&sb, cp.out, PATH_MAX);
2429 close(cp.out);
2430
2431 if (starts_with(sb.buf, "160000")) {
2432 int super_sub_len;
2433 int cwd_len = strlen(cwd);
2434 char *super_sub, *super_wt;
2435
2436 /*
2437 * There is a superproject having this repo as a submodule.
2438 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2439 * We're only interested in the name after the tab.
2440 */
2441 super_sub = strchr(sb.buf, '\t') + 1;
c5cbb27c 2442 super_sub_len = strlen(super_sub);
bf0231c6
SB
2443
2444 if (super_sub_len > cwd_len ||
2445 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
c3c3486b 2446 BUG("returned path string doesn't match cwd?");
bf0231c6
SB
2447
2448 super_wt = xstrdup(cwd);
2449 super_wt[cwd_len - super_sub_len] = '\0';
2450
49d3c4b4
AM
2451 strbuf_realpath(buf, super_wt, 1);
2452 ret = 1;
bf0231c6
SB
2453 free(super_wt);
2454 }
bc57ba1d 2455 free(cwd);
bf0231c6
SB
2456 strbuf_release(&sb);
2457
2458 code = finish_command(&cp);
2459
2460 if (code == 128)
2461 /* '../' is not a git repository */
49d3c4b4 2462 return 0;
bf0231c6
SB
2463 if (code == 0 && len == 0)
2464 /* There is an unrelated git repository at '../' */
49d3c4b4 2465 return 0;
bf0231c6
SB
2466 if (code)
2467 die(_("ls-tree returned unexpected return code %d"), code);
2468
2469 return ret;
2470}
bbbb7de7 2471
3ce08548
HWN
2472/*
2473 * Put the gitdir for a submodule (given relative to the main
2474 * repository worktree) into `buf`, or return -1 on error.
2475 */
bbbb7de7
NTND
2476int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2477{
2478 const struct submodule *sub;
2479 const char *git_dir;
2480 int ret = 0;
2481
2482 strbuf_reset(buf);
2483 strbuf_addstr(buf, submodule);
2484 strbuf_complete(buf, '/');
2485 strbuf_addstr(buf, ".git");
2486
2487 git_dir = read_gitfile(buf->buf);
2488 if (git_dir) {
2489 strbuf_reset(buf);
2490 strbuf_addstr(buf, git_dir);
2491 }
2492 if (!is_git_directory(buf->buf)) {
14228447 2493 sub = submodule_from_path(the_repository, null_oid(),
2494 submodule);
bbbb7de7
NTND
2495 if (!sub) {
2496 ret = -1;
2497 goto cleanup;
2498 }
2499 strbuf_reset(buf);
ce125d43 2500 submodule_name_to_gitdir(buf, the_repository, sub->name);
bbbb7de7
NTND
2501 }
2502
2503cleanup:
2504 return ret;
2505}
ce125d43
JT
2506
2507void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2508 const char *submodule_name)
2509{
2510 /*
2511 * NEEDSWORK: The current way of mapping a submodule's name to
2512 * its location in .git/modules/ has problems with some naming
2513 * schemes. For example, if a submodule is named "foo" and
2514 * another is named "foo/bar" (whether present in the same
2515 * superproject commit or not - the problem will arise if both
2516 * superproject commits have been checked out at any point in
2517 * time), or if two submodule names only have different cases in
2518 * a case-insensitive filesystem.
2519 *
2520 * There are several solutions, including encoding the path in
2521 * some way, introducing a submodule.<name>.gitdir config in
2522 * .git/config (not .gitmodules) that allows overriding what the
2523 * gitdir of a submodule would be (and teach Git, upon noticing
2524 * a clash, to automatically determine a non-clashing name and
2525 * to write such a config), or introducing a
2526 * submodule.<name>.gitdir config in .gitmodules that repo
2527 * administrators can explicitly set. Nothing has been decided,
2528 * so for now, just append the name at the end of the path.
2529 */
2530 strbuf_repo_git_path(buf, r, "modules/");
2531 strbuf_addstr(buf, submodule_name);
2532}