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