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