submodule.c: get_super_prefix_or_empty
[git/git.git] / submodule.c
CommitLineData
752c0c24 1#include "cache.h"
851e18c3 2#include "submodule-config.h"
752c0c24
JS
3#include "submodule.h"
4#include "dir.h"
5#include "diff.h"
6#include "commit.h"
7#include "revision.h"
ee6fc514 8#include "run-command.h"
c7e1a736 9#include "diffcore.h"
68d03e4a 10#include "refs.h"
aee9c7d6 11#include "string-list.h"
6859de45 12#include "sha1-array.h"
c1189cae 13#include "argv-array.h"
5fee9952 14#include "blob.h"
fe85ee6e 15#include "thread-utils.h"
4638728c 16#include "quote.h"
f6f85861 17#include "worktree.h"
aee9c7d6 18
88a21979 19static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
cf055e46 20static int config_update_recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
a028a193 21static int parallel_jobs = 1;
2721ce21 22static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP;
6859de45
JK
23static int initialized_fetch_ref_tips;
24static struct sha1_array ref_tips_before_fetch;
25static struct sha1_array ref_tips_after_fetch;
26
d4e98b58
JL
27/*
28 * The following flag is set if the .gitmodules file is unmerged. We then
29 * disable recursion for all submodules where .git/config doesn't have a
30 * matching config entry because we can't guess what might be configured in
31 * .gitmodules unless the user resolves the conflict. When a command line
32 * option is given (which always overrides configuration) this flag will be
33 * ignored.
34 */
35static int gitmodules_is_unmerged;
752c0c24 36
5fee9952
JL
37/*
38 * This flag is set if the .gitmodules file had unstaged modifications on
39 * startup. This must be checked before allowing modifications to the
40 * .gitmodules file with the intention to stage them later, because when
41 * continuing we would stage the modifications the user didn't stage herself
42 * too. That might change in a future version when we learn to stage the
43 * changes we do ourselves without staging any previous modifications.
44 */
45static int gitmodules_is_modified;
46
5fee9952
JL
47int is_staging_gitmodules_ok(void)
48{
49 return !gitmodules_is_modified;
50}
51
0656781f
JL
52/*
53 * Try to update the "path" entry in the "submodule.<name>" section of the
54 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
55 * with the correct path=<oldpath> setting was found and we could update it.
56 */
57int update_path_in_gitmodules(const char *oldpath, const char *newpath)
58{
59 struct strbuf entry = STRBUF_INIT;
851e18c3 60 const struct submodule *submodule;
0656781f
JL
61
62 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
63 return -1;
64
65 if (gitmodules_is_unmerged)
66 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
67
851e18c3
HV
68 submodule = submodule_from_path(null_sha1, oldpath);
69 if (!submodule || !submodule->name) {
0656781f
JL
70 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
71 return -1;
72 }
73 strbuf_addstr(&entry, "submodule.");
851e18c3 74 strbuf_addstr(&entry, submodule->name);
0656781f 75 strbuf_addstr(&entry, ".path");
30598ad0 76 if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
0656781f
JL
77 /* Maybe the user already did that, don't error out here */
78 warning(_("Could not update .gitmodules entry %s"), entry.buf);
79 strbuf_release(&entry);
80 return -1;
81 }
82 strbuf_release(&entry);
83 return 0;
84}
85
95c16418
JL
86/*
87 * Try to remove the "submodule.<name>" section from .gitmodules where the given
88 * path is configured. Return 0 only if a .gitmodules file was found, a section
89 * with the correct path=<path> setting was found and we could remove it.
90 */
91int remove_path_from_gitmodules(const char *path)
92{
93 struct strbuf sect = STRBUF_INIT;
851e18c3 94 const struct submodule *submodule;
95c16418
JL
95
96 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
97 return -1;
98
99 if (gitmodules_is_unmerged)
100 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
101
851e18c3
HV
102 submodule = submodule_from_path(null_sha1, path);
103 if (!submodule || !submodule->name) {
95c16418
JL
104 warning(_("Could not find section in .gitmodules where path=%s"), path);
105 return -1;
106 }
107 strbuf_addstr(&sect, "submodule.");
851e18c3 108 strbuf_addstr(&sect, submodule->name);
95c16418
JL
109 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
110 /* Maybe the user already did that, don't error out here */
111 warning(_("Could not remove .gitmodules entry for %s"), path);
112 strbuf_release(&sect);
113 return -1;
114 }
115 strbuf_release(&sect);
116 return 0;
117}
118
5fee9952
JL
119void stage_updated_gitmodules(void)
120{
bc8d6b9b 121 if (add_file_to_cache(".gitmodules", 0))
5fee9952
JL
122 die(_("staging updated .gitmodules failed"));
123}
124
cb58c932 125static int add_submodule_odb(const char *path)
752c0c24
JS
126{
127 struct strbuf objects_directory = STRBUF_INIT;
de7a7960 128 int ret = 0;
752c0c24 129
99b43a61
JK
130 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
131 if (ret)
132 goto done;
de7a7960
JL
133 if (!is_directory(objects_directory.buf)) {
134 ret = -1;
135 goto done;
136 }
a5b34d21 137 add_to_alternates_memory(objects_directory.buf);
de7a7960
JL
138done:
139 strbuf_release(&objects_directory);
140 return ret;
752c0c24
JS
141}
142
aee9c7d6
JL
143void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
144 const char *path)
145{
851e18c3
HV
146 const struct submodule *submodule = submodule_from_path(null_sha1, path);
147 if (submodule) {
148 if (submodule->ignore)
149 handle_ignore_submodules_arg(diffopt, submodule->ignore);
d4e98b58
JL
150 else if (gitmodules_is_unmerged)
151 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
aee9c7d6
JL
152 }
153}
154
7dce19d3 155int submodule_config(const char *var, const char *value, void *cb)
302ad7a9 156{
a028a193
SB
157 if (!strcmp(var, "submodule.fetchjobs")) {
158 parallel_jobs = git_config_int(var, value);
159 if (parallel_jobs < 0)
160 die(_("negative values not allowed for submodule.fetchJobs"));
161 return 0;
162 } else if (starts_with(var, "submodule."))
302ad7a9 163 return parse_submodule_config_option(var, value);
be254a0e 164 else if (!strcmp(var, "fetch.recursesubmodules")) {
1fb25502 165 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
be254a0e
JL
166 return 0;
167 }
302ad7a9
JL
168 return 0;
169}
170
171void gitmodules_config(void)
172{
173 const char *work_tree = get_git_work_tree();
174 if (work_tree) {
175 struct strbuf gitmodules_path = STRBUF_INIT;
d4e98b58 176 int pos;
302ad7a9
JL
177 strbuf_addstr(&gitmodules_path, work_tree);
178 strbuf_addstr(&gitmodules_path, "/.gitmodules");
d4e98b58
JL
179 if (read_cache() < 0)
180 die("index file corrupt");
181 pos = cache_name_pos(".gitmodules", 11);
182 if (pos < 0) { /* .gitmodules not found or isn't merged */
183 pos = -1 - pos;
184 if (active_nr > pos) { /* there is a .gitmodules */
185 const struct cache_entry *ce = active_cache[pos];
186 if (ce_namelen(ce) == 11 &&
187 !memcmp(ce->name, ".gitmodules", 11))
188 gitmodules_is_unmerged = 1;
189 }
5fee9952
JL
190 } else if (pos < active_nr) {
191 struct stat st;
192 if (lstat(".gitmodules", &st) == 0 &&
193 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
194 gitmodules_is_modified = 1;
d4e98b58
JL
195 }
196
197 if (!gitmodules_is_unmerged)
198 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
302ad7a9
JL
199 strbuf_release(&gitmodules_path);
200 }
201}
202
9ebf689a
BW
203void gitmodules_config_sha1(const unsigned char *commit_sha1)
204{
205 struct strbuf rev = STRBUF_INIT;
206 unsigned char sha1[20];
207
208 if (gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) {
209 git_config_from_blob_sha1(submodule_config, rev.buf,
210 sha1, NULL);
211 }
212 strbuf_release(&rev);
213}
214
f9f42560
BW
215/*
216 * Determine if a submodule has been initialized at a given 'path'
217 */
218int is_submodule_initialized(const char *path)
219{
220 int ret = 0;
221 const struct submodule *module = NULL;
222
223 module = submodule_from_path(null_sha1, path);
224
225 if (module) {
226 char *key = xstrfmt("submodule.%s.url", module->name);
227 char *value = NULL;
228
229 ret = !git_config_get_string(key, &value);
230
231 free(value);
232 free(key);
233 }
234
235 return ret;
236}
237
15cdc647 238int is_submodule_populated_gently(const char *path, int *return_error_code)
5688c28d
BW
239{
240 int ret = 0;
241 char *gitdir = xstrfmt("%s/.git", path);
242
15cdc647 243 if (resolve_gitdir_gently(gitdir, return_error_code))
5688c28d
BW
244 ret = 1;
245
246 free(gitdir);
247 return ret;
248}
249
ea2fa5a3
SB
250int parse_submodule_update_strategy(const char *value,
251 struct submodule_update_strategy *dst)
252{
253 free((void*)dst->command);
254 dst->command = NULL;
255 if (!strcmp(value, "none"))
256 dst->type = SM_UPDATE_NONE;
257 else if (!strcmp(value, "checkout"))
258 dst->type = SM_UPDATE_CHECKOUT;
259 else if (!strcmp(value, "rebase"))
260 dst->type = SM_UPDATE_REBASE;
261 else if (!strcmp(value, "merge"))
262 dst->type = SM_UPDATE_MERGE;
263 else if (skip_prefix(value, "!", &value)) {
264 dst->type = SM_UPDATE_COMMAND;
265 dst->command = xstrdup(value);
266 } else
267 return -1;
268 return 0;
269}
270
3604242f
SB
271const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
272{
273 struct strbuf sb = STRBUF_INIT;
274 switch (s->type) {
275 case SM_UPDATE_CHECKOUT:
276 return "checkout";
277 case SM_UPDATE_MERGE:
278 return "merge";
279 case SM_UPDATE_REBASE:
280 return "rebase";
281 case SM_UPDATE_NONE:
282 return "none";
283 case SM_UPDATE_UNSPECIFIED:
284 return NULL;
285 case SM_UPDATE_COMMAND:
286 strbuf_addf(&sb, "!%s", s->command);
287 return strbuf_detach(&sb, NULL);
288 }
289 return NULL;
290}
291
46a958b3
JL
292void handle_ignore_submodules_arg(struct diff_options *diffopt,
293 const char *arg)
294{
be4f2b40
JS
295 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
296 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
297 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
298
46a958b3
JL
299 if (!strcmp(arg, "all"))
300 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
301 else if (!strcmp(arg, "untracked"))
302 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
303 else if (!strcmp(arg, "dirty"))
304 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
aee9c7d6 305 else if (strcmp(arg, "none"))
46a958b3
JL
306 die("bad --ignore-submodules argument: %s", arg);
307}
308
808a95dc
JN
309static int prepare_submodule_summary(struct rev_info *rev, const char *path,
310 struct commit *left, struct commit *right,
8e6df650 311 struct commit_list *merge_bases)
808a95dc 312{
8e6df650 313 struct commit_list *list;
808a95dc
JN
314
315 init_revisions(rev, NULL);
316 setup_revisions(0, NULL, rev, NULL);
317 rev->left_right = 1;
318 rev->first_parent_only = 1;
319 left->object.flags |= SYMMETRIC_LEFT;
320 add_pending_object(rev, &left->object, path);
321 add_pending_object(rev, &right->object, path);
808a95dc
JN
322 for (list = merge_bases; list; list = list->next) {
323 list->item->object.flags |= UNINTERESTING;
324 add_pending_object(rev, &list->item->object,
f2fd0760 325 oid_to_hex(&list->item->object.oid));
808a95dc
JN
326 }
327 return prepare_revision_walk(rev);
328}
329
330static void print_submodule_summary(struct rev_info *rev, FILE *f,
0f33a067 331 const char *line_prefix,
808a95dc
JN
332 const char *del, const char *add, const char *reset)
333{
334 static const char format[] = " %m %s";
335 struct strbuf sb = STRBUF_INIT;
336 struct commit *commit;
337
338 while ((commit = get_revision(rev))) {
339 struct pretty_print_context ctx = {0};
340 ctx.date_mode = rev->date_mode;
ecaee805 341 ctx.output_encoding = get_log_output_encoding();
808a95dc 342 strbuf_setlen(&sb, 0);
0f33a067 343 strbuf_addstr(&sb, line_prefix);
808a95dc
JN
344 if (commit->object.flags & SYMMETRIC_LEFT) {
345 if (del)
346 strbuf_addstr(&sb, del);
347 }
348 else if (add)
349 strbuf_addstr(&sb, add);
350 format_commit_message(commit, format, &sb, &ctx);
351 if (reset)
352 strbuf_addstr(&sb, reset);
353 strbuf_addch(&sb, '\n');
354 fprintf(f, "%s", sb.buf);
355 }
356 strbuf_release(&sb);
357}
358
6cd5757c
SB
359static void prepare_submodule_repo_env_no_git_dir(struct argv_array *out)
360{
361 const char * const *var;
362
363 for (var = local_repo_env; *var; var++) {
364 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
365 argv_array_push(out, *var);
366 }
367}
368
369void prepare_submodule_repo_env(struct argv_array *out)
370{
371 prepare_submodule_repo_env_no_git_dir(out);
372 argv_array_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT,
373 DEFAULT_GIT_DIR_ENVIRONMENT);
374}
375
8e6df650
JK
376/* Helper function to display the submodule header line prior to the full
377 * summary output. If it can locate the submodule objects directory it will
378 * attempt to lookup both the left and right commits and put them into the
379 * left and right pointers.
380 */
381static void show_submodule_header(FILE *f, const char *path,
0f33a067 382 const char *line_prefix,
602a283a 383 struct object_id *one, struct object_id *two,
4e215131 384 unsigned dirty_submodule, const char *meta,
8e6df650
JK
385 const char *reset,
386 struct commit **left, struct commit **right,
387 struct commit_list **merge_bases)
752c0c24 388{
752c0c24
JS
389 const char *message = NULL;
390 struct strbuf sb = STRBUF_INIT;
752c0c24
JS
391 int fast_forward = 0, fast_backward = 0;
392
c7e1a736 393 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
0f33a067
JK
394 fprintf(f, "%sSubmodule %s contains untracked content\n",
395 line_prefix, path);
c7e1a736 396 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
0f33a067
JK
397 fprintf(f, "%sSubmodule %s contains modified content\n",
398 line_prefix, path);
c7e1a736 399
8e6df650
JK
400 if (is_null_oid(one))
401 message = "(new submodule)";
402 else if (is_null_oid(two))
403 message = "(submodule deleted)";
404
405 if (add_submodule_odb(path)) {
406 if (!message)
407 message = "(not initialized)";
408 goto output_header;
409 }
410
411 /*
412 * Attempt to lookup the commit references, and determine if this is
413 * a fast forward or fast backwards update.
414 */
415 *left = lookup_commit_reference(one->hash);
416 *right = lookup_commit_reference(two->hash);
417
418 /*
419 * Warn about missing commits in the submodule project, but only if
420 * they aren't null.
421 */
422 if ((!is_null_oid(one) && !*left) ||
423 (!is_null_oid(two) && !*right))
424 message = "(commits not present)";
425
426 *merge_bases = get_merge_bases(*left, *right);
427 if (*merge_bases) {
428 if ((*merge_bases)->item == *left)
429 fast_forward = 1;
430 else if ((*merge_bases)->item == *right)
431 fast_backward = 1;
432 }
433
602a283a 434 if (!oidcmp(one, two)) {
c7e1a736
JL
435 strbuf_release(&sb);
436 return;
437 }
438
8e6df650 439output_header:
a94bb683 440 strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
69e65449 441 strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
a94bb683 442 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
f0798e6c 443 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
752c0c24 444 if (message)
4e215131 445 strbuf_addf(&sb, " %s%s\n", message, reset);
752c0c24 446 else
4e215131 447 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
752c0c24
JS
448 fwrite(sb.buf, sb.len, 1, f);
449
752c0c24
JS
450 strbuf_release(&sb);
451}
ee6fc514 452
8e6df650
JK
453void show_submodule_summary(FILE *f, const char *path,
454 const char *line_prefix,
455 struct object_id *one, struct object_id *two,
456 unsigned dirty_submodule, const char *meta,
457 const char *del, const char *add, const char *reset)
458{
459 struct rev_info rev;
460 struct commit *left = NULL, *right = NULL;
461 struct commit_list *merge_bases = NULL;
462
463 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
464 meta, reset, &left, &right, &merge_bases);
465
466 /*
467 * If we don't have both a left and a right pointer, there is no
468 * reason to try and display a summary. The header line should contain
469 * all the information the user needs.
470 */
471 if (!left || !right)
472 goto out;
473
474 /* Treat revision walker failure the same as missing commits */
475 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
476 fprintf(f, "%s(revision walker failed)\n", line_prefix);
477 goto out;
478 }
479
480 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
481
482out:
483 if (merge_bases)
484 free_commit_list(merge_bases);
485 clear_commit_marks(left, ~0);
486 clear_commit_marks(right, ~0);
487}
488
fd47ae6a
JK
489void show_submodule_inline_diff(FILE *f, const char *path,
490 const char *line_prefix,
491 struct object_id *one, struct object_id *two,
492 unsigned dirty_submodule, const char *meta,
493 const char *del, const char *add, const char *reset,
494 const struct diff_options *o)
495{
496 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
497 struct commit *left = NULL, *right = NULL;
498 struct commit_list *merge_bases = NULL;
499 struct strbuf submodule_dir = STRBUF_INIT;
500 struct child_process cp = CHILD_PROCESS_INIT;
501
502 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
503 meta, reset, &left, &right, &merge_bases);
504
505 /* We need a valid left and right commit to display a difference */
506 if (!(left || is_null_oid(one)) ||
507 !(right || is_null_oid(two)))
508 goto done;
509
510 if (left)
511 old = one;
512 if (right)
513 new = two;
514
515 fflush(f);
516 cp.git_cmd = 1;
517 cp.dir = path;
518 cp.out = dup(fileno(f));
519 cp.no_stdin = 1;
520
521 /* TODO: other options may need to be passed here. */
522 argv_array_push(&cp.args, "diff");
523 argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
524 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
525 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
526 o->b_prefix, path);
527 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
528 o->a_prefix, path);
529 } else {
530 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
531 o->a_prefix, path);
532 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
533 o->b_prefix, path);
534 }
535 argv_array_push(&cp.args, oid_to_hex(old));
536 /*
537 * If the submodule has modified content, we will diff against the
538 * work tree, under the assumption that the user has asked for the
539 * diff format and wishes to actually see all differences even if they
540 * haven't yet been committed to the submodule yet.
541 */
542 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
543 argv_array_push(&cp.args, oid_to_hex(new));
544
545 if (run_command(&cp))
546 fprintf(f, "(diff failed)\n");
547
548done:
549 strbuf_release(&submodule_dir);
550 if (merge_bases)
551 free_commit_list(merge_bases);
552 if (left)
553 clear_commit_marks(left, ~0);
554 if (right)
555 clear_commit_marks(right, ~0);
556}
557
be254a0e
JL
558void set_config_fetch_recurse_submodules(int value)
559{
560 config_fetch_recurse_submodules = value;
561}
562
cf055e46
SB
563void set_config_update_recurse_submodules(int value)
564{
565 config_update_recurse_submodules = value;
566}
567
84f8925e
SB
568int should_update_submodules(void)
569{
570 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
571}
572
573const struct submodule *submodule_from_ce(const struct cache_entry *ce)
574{
575 if (!S_ISGITLINK(ce->ce_mode))
576 return NULL;
577
578 if (!should_update_submodules())
579 return NULL;
580
581 return submodule_from_path(null_sha1, ce->name);
582}
583
7290ef58
MH
584static int has_remote(const char *refname, const struct object_id *oid,
585 int flags, void *cb_data)
d2b17b32
FG
586{
587 return 1;
588}
589
9cfa1c26 590static int append_sha1_to_argv(const unsigned char sha1[20], void *data)
d2b17b32 591{
9cfa1c26
HV
592 struct argv_array *argv = data;
593 argv_array_push(argv, sha1_to_hex(sha1));
594 return 0;
595}
596
5b6607d2 597static int check_has_commit(const unsigned char sha1[20], void *data)
d2b17b32 598{
5b6607d2
HV
599 int *has_commit = data;
600
601 if (!lookup_commit_reference(sha1))
602 *has_commit = 0;
603
604 return 0;
605}
606
607static int submodule_has_commits(const char *path, struct sha1_array *commits)
608{
609 int has_commit = 1;
610
611 if (add_submodule_odb(path))
612 return 0;
613
614 sha1_array_for_each_unique(commits, check_has_commit, &has_commit);
615 return has_commit;
616}
617
618static int submodule_needs_pushing(const char *path, struct sha1_array *commits)
619{
620 if (!submodule_has_commits(path, commits))
250ab24a
HV
621 /*
622 * NOTE: We do consider it safe to return "no" here. The
623 * correct answer would be "We do not know" instead of
624 * "No push needed", but it is quite hard to change
625 * the submodule pointer without having the submodule
626 * around. If a user did however change the submodules
627 * without having the submodule around, this indicates
628 * an expert who knows what they are doing or a
629 * maintainer integrating work from other people. In
630 * both cases it should be safe to skip this check.
631 */
d2b17b32
FG
632 return 0;
633
634 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 635 struct child_process cp = CHILD_PROCESS_INIT;
d2b17b32
FG
636 struct strbuf buf = STRBUF_INIT;
637 int needs_pushing = 0;
638
5b6607d2
HV
639 argv_array_push(&cp.args, "rev-list");
640 sha1_array_for_each_unique(commits, append_sha1_to_argv, &cp.args);
641 argv_array_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
642
c12e8656 643 prepare_submodule_repo_env(&cp.env_array);
d2b17b32
FG
644 cp.git_cmd = 1;
645 cp.no_stdin = 1;
646 cp.out = -1;
647 cp.dir = path;
648 if (start_command(&cp))
5b6607d2
HV
649 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
650 path);
d2b17b32
FG
651 if (strbuf_read(&buf, cp.out, 41))
652 needs_pushing = 1;
653 finish_command(&cp);
654 close(cp.out);
655 strbuf_release(&buf);
656 return needs_pushing;
657 }
658
659 return 0;
660}
661
14739447
HV
662static struct sha1_array *submodule_commits(struct string_list *submodules,
663 const char *path)
664{
665 struct string_list_item *item;
666
667 item = string_list_insert(submodules, path);
668 if (item->util)
669 return (struct sha1_array *) item->util;
670
671 /* NEEDSWORK: should we have sha1_array_init()? */
672 item->util = xcalloc(1, sizeof(struct sha1_array));
673 return (struct sha1_array *) item->util;
674}
675
d2b17b32
FG
676static void collect_submodules_from_diff(struct diff_queue_struct *q,
677 struct diff_options *options,
678 void *data)
679{
680 int i;
14739447 681 struct string_list *submodules = data;
d2b17b32
FG
682
683 for (i = 0; i < q->nr; i++) {
684 struct diff_filepair *p = q->queue[i];
14739447 685 struct sha1_array *commits;
d2b17b32
FG
686 if (!S_ISGITLINK(p->two->mode))
687 continue;
14739447
HV
688 commits = submodule_commits(submodules, p->two->path);
689 sha1_array_append(commits, p->two->oid.hash);
d2b17b32
FG
690 }
691}
692
a762e51e
HV
693static void find_unpushed_submodule_commits(struct commit *commit,
694 struct string_list *needs_pushing)
d2b17b32 695{
d2b17b32
FG
696 struct rev_info rev;
697
d2b17b32
FG
698 init_revisions(&rev, NULL);
699 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
700 rev.diffopt.format_callback = collect_submodules_from_diff;
701 rev.diffopt.format_callback_data = needs_pushing;
78e98eaf 702 diff_tree_combined_merge(commit, 1, &rev);
d2b17b32
FG
703}
704
14739447
HV
705static void free_submodules_sha1s(struct string_list *submodules)
706{
707 struct string_list_item *item;
708 for_each_string_list_item(item, submodules)
709 sha1_array_clear((struct sha1_array *) item->util);
710 string_list_clear(submodules, 1);
711}
712
9cfa1c26 713int find_unpushed_submodules(struct sha1_array *commits,
a762e51e 714 const char *remotes_name, struct string_list *needs_pushing)
d2b17b32
FG
715{
716 struct rev_info rev;
717 struct commit *commit;
14739447
HV
718 struct string_list submodules = STRING_LIST_INIT_DUP;
719 struct string_list_item *submodule;
9cfa1c26 720 struct argv_array argv = ARGV_ARRAY_INIT;
a762e51e 721
d2b17b32 722 init_revisions(&rev, NULL);
9cfa1c26
HV
723
724 /* argv.argv[0] will be ignored by setup_revisions */
725 argv_array_push(&argv, "find_unpushed_submodules");
726 sha1_array_for_each_unique(commits, append_sha1_to_argv, &argv);
727 argv_array_push(&argv, "--not");
728 argv_array_pushf(&argv, "--remotes=%s", remotes_name);
729
730 setup_revisions(argv.argc, argv.argv, &rev, NULL);
d2b17b32
FG
731 if (prepare_revision_walk(&rev))
732 die("revision walk setup failed");
733
a762e51e 734 while ((commit = get_revision(&rev)) != NULL)
14739447 735 find_unpushed_submodule_commits(commit, &submodules);
d2b17b32 736
bcc0a3ea 737 reset_revision_walk();
9cfa1c26 738 argv_array_clear(&argv);
d2b17b32 739
14739447 740 for_each_string_list_item(submodule, &submodules) {
5b6607d2
HV
741 struct sha1_array *commits = (struct sha1_array *) submodule->util;
742
743 if (submodule_needs_pushing(submodule->string, commits))
744 string_list_insert(needs_pushing, submodule->string);
14739447
HV
745 }
746 free_submodules_sha1s(&submodules);
d2b17b32 747
a762e51e 748 return needs_pushing->nr;
d2b17b32
FG
749}
750
0301c821 751static int push_submodule(const char *path, int dry_run)
eb21c732
HV
752{
753 if (add_submodule_odb(path))
754 return 1;
755
756 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 757 struct child_process cp = CHILD_PROCESS_INIT;
0301c821
BW
758 argv_array_push(&cp.args, "push");
759 if (dry_run)
760 argv_array_push(&cp.args, "--dry-run");
eb21c732 761
c12e8656 762 prepare_submodule_repo_env(&cp.env_array);
eb21c732
HV
763 cp.git_cmd = 1;
764 cp.no_stdin = 1;
765 cp.dir = path;
766 if (run_command(&cp))
767 return 0;
768 close(cp.out);
769 }
770
771 return 1;
772}
773
0301c821
BW
774int push_unpushed_submodules(struct sha1_array *commits,
775 const char *remotes_name,
776 int dry_run)
eb21c732
HV
777{
778 int i, ret = 1;
f93d7c6f 779 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
eb21c732 780
9cfa1c26 781 if (!find_unpushed_submodules(commits, remotes_name, &needs_pushing))
eb21c732
HV
782 return 1;
783
784 for (i = 0; i < needs_pushing.nr; i++) {
785 const char *path = needs_pushing.items[i].string;
786 fprintf(stderr, "Pushing submodule '%s'\n", path);
0301c821 787 if (!push_submodule(path, dry_run)) {
eb21c732
HV
788 fprintf(stderr, "Unable to push submodule '%s'\n", path);
789 ret = 0;
790 }
791 }
792
793 string_list_clear(&needs_pushing, 0);
794
795 return ret;
796}
797
c16c3e40
JL
798static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
799{
800 int is_present = 0;
801 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
802 /* Even if the submodule is checked out and the commit is
803 * present, make sure it is reachable from a ref. */
d3180279 804 struct child_process cp = CHILD_PROCESS_INIT;
c16c3e40
JL
805 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
806 struct strbuf buf = STRBUF_INIT;
807
808 argv[3] = sha1_to_hex(sha1);
c16c3e40 809 cp.argv = argv;
c12e8656 810 prepare_submodule_repo_env(&cp.env_array);
c16c3e40
JL
811 cp.git_cmd = 1;
812 cp.no_stdin = 1;
c16c3e40 813 cp.dir = path;
1d4974c9 814 if (!capture_command(&cp, &buf, 1024) && !buf.len)
c16c3e40
JL
815 is_present = 1;
816
c16c3e40
JL
817 strbuf_release(&buf);
818 }
819 return is_present;
820}
821
88a21979
JL
822static void submodule_collect_changed_cb(struct diff_queue_struct *q,
823 struct diff_options *options,
824 void *data)
825{
826 int i;
827 for (i = 0; i < q->nr; i++) {
828 struct diff_filepair *p = q->queue[i];
829 if (!S_ISGITLINK(p->two->mode))
830 continue;
831
832 if (S_ISGITLINK(p->one->mode)) {
833 /* NEEDSWORK: We should honor the name configured in
834 * the .gitmodules file of the commit we are examining
835 * here to be able to correctly follow submodules
836 * being moved around. */
837 struct string_list_item *path;
838 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
a0d12c44 839 if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash))
88a21979
JL
840 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
841 } else {
842 /* Submodule is new or was moved here */
843 /* NEEDSWORK: When the .git directories of submodules
844 * live inside the superprojects .git directory some
845 * day we should fetch new submodules directly into
846 * that location too when config or options request
847 * that so they can be checked out from there. */
848 continue;
849 }
850 }
851}
852
7290ef58 853static int add_sha1_to_array(const char *ref, const struct object_id *oid,
6859de45
JK
854 int flags, void *data)
855{
7290ef58 856 sha1_array_append(data, oid->hash);
6859de45
JK
857 return 0;
858}
859
88a21979 860void check_for_new_submodule_commits(unsigned char new_sha1[20])
6859de45
JK
861{
862 if (!initialized_fetch_ref_tips) {
863 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
864 initialized_fetch_ref_tips = 1;
865 }
866
867 sha1_array_append(&ref_tips_after_fetch, new_sha1);
868}
869
16ddcd40 870static int add_sha1_to_argv(const unsigned char sha1[20], void *data)
6859de45 871{
c1189cae 872 argv_array_push(data, sha1_to_hex(sha1));
16ddcd40 873 return 0;
6859de45
JK
874}
875
876static void calculate_changed_submodule_paths(void)
88a21979
JL
877{
878 struct rev_info rev;
879 struct commit *commit;
c1189cae 880 struct argv_array argv = ARGV_ARRAY_INIT;
88a21979 881
18322bad 882 /* No need to check if there are no submodules configured */
851e18c3 883 if (!submodule_from_path(NULL, NULL))
18322bad
JL
884 return;
885
88a21979 886 init_revisions(&rev, NULL);
c1189cae 887 argv_array_push(&argv, "--"); /* argv[0] program name */
6859de45
JK
888 sha1_array_for_each_unique(&ref_tips_after_fetch,
889 add_sha1_to_argv, &argv);
c1189cae 890 argv_array_push(&argv, "--not");
6859de45
JK
891 sha1_array_for_each_unique(&ref_tips_before_fetch,
892 add_sha1_to_argv, &argv);
893 setup_revisions(argv.argc, argv.argv, &rev, NULL);
88a21979
JL
894 if (prepare_revision_walk(&rev))
895 die("revision walk setup failed");
896
897 /*
898 * Collect all submodules (whether checked out or not) for which new
899 * commits have been recorded upstream in "changed_submodule_paths".
900 */
901 while ((commit = get_revision(&rev))) {
902 struct commit_list *parent = commit->parents;
903 while (parent) {
904 struct diff_options diff_opts;
905 diff_setup(&diff_opts);
ea2d325b 906 DIFF_OPT_SET(&diff_opts, RECURSIVE);
88a21979
JL
907 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
908 diff_opts.format_callback = submodule_collect_changed_cb;
28452655 909 diff_setup_done(&diff_opts);
ed1c9977 910 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
88a21979
JL
911 diffcore_std(&diff_opts);
912 diff_flush(&diff_opts);
913 parent = parent->next;
914 }
915 }
6859de45 916
c1189cae 917 argv_array_clear(&argv);
6859de45
JK
918 sha1_array_clear(&ref_tips_before_fetch);
919 sha1_array_clear(&ref_tips_after_fetch);
920 initialized_fetch_ref_tips = 0;
88a21979
JL
921}
922
fe85ee6e
SB
923struct submodule_parallel_fetch {
924 int count;
925 struct argv_array args;
926 const char *work_tree;
927 const char *prefix;
928 int command_line_option;
929 int quiet;
930 int result;
931};
932#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
933
934static int get_next_submodule(struct child_process *cp,
935 struct strbuf *err, void *data, void **task_cb)
7dce19d3 936{
fe85ee6e
SB
937 int ret = 0;
938 struct submodule_parallel_fetch *spf = data;
6859de45 939
fe85ee6e 940 for (; spf->count < active_nr; spf->count++) {
7dce19d3
JL
941 struct strbuf submodule_path = STRBUF_INIT;
942 struct strbuf submodule_git_dir = STRBUF_INIT;
943 struct strbuf submodule_prefix = STRBUF_INIT;
fe85ee6e 944 const struct cache_entry *ce = active_cache[spf->count];
851e18c3
HV
945 const char *git_dir, *default_argv;
946 const struct submodule *submodule;
7dce19d3
JL
947
948 if (!S_ISGITLINK(ce->ce_mode))
949 continue;
950
851e18c3
HV
951 submodule = submodule_from_path(null_sha1, ce->name);
952 if (!submodule)
953 submodule = submodule_from_name(null_sha1, ce->name);
7dce19d3 954
88a21979 955 default_argv = "yes";
fe85ee6e 956 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
851e18c3
HV
957 if (submodule &&
958 submodule->fetch_recurse !=
959 RECURSE_SUBMODULES_NONE) {
960 if (submodule->fetch_recurse ==
961 RECURSE_SUBMODULES_OFF)
c1a3c364 962 continue;
851e18c3
HV
963 if (submodule->fetch_recurse ==
964 RECURSE_SUBMODULES_ON_DEMAND) {
bf42b384
JL
965 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
966 continue;
967 default_argv = "on-demand";
968 }
c1a3c364 969 } else {
d4e98b58
JL
970 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
971 gitmodules_is_unmerged)
c1a3c364 972 continue;
88a21979
JL
973 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
974 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
975 continue;
976 default_argv = "on-demand";
977 }
c1a3c364 978 }
fe85ee6e 979 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
8f0700dd
JL
980 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
981 continue;
982 default_argv = "on-demand";
be254a0e
JL
983 }
984
fe85ee6e 985 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
7dce19d3 986 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
fe85ee6e 987 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
13d6ec91 988 git_dir = read_gitfile(submodule_git_dir.buf);
7dce19d3
JL
989 if (!git_dir)
990 git_dir = submodule_git_dir.buf;
991 if (is_directory(git_dir)) {
fe85ee6e
SB
992 child_process_init(cp);
993 cp->dir = strbuf_detach(&submodule_path, NULL);
c12e8656 994 prepare_submodule_repo_env(&cp->env_array);
fe85ee6e
SB
995 cp->git_cmd = 1;
996 if (!spf->quiet)
997 strbuf_addf(err, "Fetching submodule %s%s\n",
998 spf->prefix, ce->name);
999 argv_array_init(&cp->args);
1000 argv_array_pushv(&cp->args, spf->args.argv);
1001 argv_array_push(&cp->args, default_argv);
1002 argv_array_push(&cp->args, "--submodule-prefix");
1003 argv_array_push(&cp->args, submodule_prefix.buf);
1004 ret = 1;
7dce19d3
JL
1005 }
1006 strbuf_release(&submodule_path);
1007 strbuf_release(&submodule_git_dir);
1008 strbuf_release(&submodule_prefix);
fe85ee6e
SB
1009 if (ret) {
1010 spf->count++;
1011 return 1;
1012 }
7dce19d3 1013 }
fe85ee6e
SB
1014 return 0;
1015}
1016
2a73b3da 1017static int fetch_start_failure(struct strbuf *err,
fe85ee6e
SB
1018 void *cb, void *task_cb)
1019{
1020 struct submodule_parallel_fetch *spf = cb;
1021
1022 spf->result = 1;
1023
1024 return 0;
1025}
1026
2a73b3da
SB
1027static int fetch_finish(int retvalue, struct strbuf *err,
1028 void *cb, void *task_cb)
fe85ee6e
SB
1029{
1030 struct submodule_parallel_fetch *spf = cb;
1031
1032 if (retvalue)
1033 spf->result = 1;
1034
1035 return 0;
1036}
1037
1038int fetch_populated_submodules(const struct argv_array *options,
1039 const char *prefix, int command_line_option,
62104ba1 1040 int quiet, int max_parallel_jobs)
fe85ee6e
SB
1041{
1042 int i;
fe85ee6e
SB
1043 struct submodule_parallel_fetch spf = SPF_INIT;
1044
1045 spf.work_tree = get_git_work_tree();
1046 spf.command_line_option = command_line_option;
1047 spf.quiet = quiet;
1048 spf.prefix = prefix;
1049
1050 if (!spf.work_tree)
1051 goto out;
1052
1053 if (read_cache() < 0)
1054 die("index file corrupt");
1055
1056 argv_array_push(&spf.args, "fetch");
1057 for (i = 0; i < options->argc; i++)
1058 argv_array_push(&spf.args, options->argv[i]);
1059 argv_array_push(&spf.args, "--recurse-submodules-default");
1060 /* default value, "--submodule-prefix" and its value are added later */
1061
a028a193
SB
1062 if (max_parallel_jobs < 0)
1063 max_parallel_jobs = parallel_jobs;
1064
fe85ee6e
SB
1065 calculate_changed_submodule_paths();
1066 run_processes_parallel(max_parallel_jobs,
1067 get_next_submodule,
1068 fetch_start_failure,
1069 fetch_finish,
1070 &spf);
1071
1072 argv_array_clear(&spf.args);
88a21979
JL
1073out:
1074 string_list_clear(&changed_submodule_paths, 1);
fe85ee6e 1075 return spf.result;
7dce19d3
JL
1076}
1077
3bfc4504 1078unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 1079{
c7e1a736 1080 ssize_t len;
d3180279 1081 struct child_process cp = CHILD_PROCESS_INIT;
ee6fc514
JL
1082 const char *argv[] = {
1083 "status",
1084 "--porcelain",
1085 NULL,
3bfc4504 1086 NULL,
ee6fc514 1087 };
ee6fc514 1088 struct strbuf buf = STRBUF_INIT;
c7e1a736
JL
1089 unsigned dirty_submodule = 0;
1090 const char *line, *next_line;
eee49b6c 1091 const char *git_dir;
ee6fc514 1092
eee49b6c 1093 strbuf_addf(&buf, "%s/.git", path);
13d6ec91 1094 git_dir = read_gitfile(buf.buf);
eee49b6c
JL
1095 if (!git_dir)
1096 git_dir = buf.buf;
1097 if (!is_directory(git_dir)) {
ee6fc514
JL
1098 strbuf_release(&buf);
1099 /* The submodule is not checked out, so it is not modified */
1100 return 0;
1101
1102 }
1103 strbuf_reset(&buf);
1104
3bfc4504
JL
1105 if (ignore_untracked)
1106 argv[2] = "-uno";
1107
ee6fc514 1108 cp.argv = argv;
c12e8656 1109 prepare_submodule_repo_env(&cp.env_array);
ee6fc514
JL
1110 cp.git_cmd = 1;
1111 cp.no_stdin = 1;
1112 cp.out = -1;
eee49b6c 1113 cp.dir = path;
ee6fc514 1114 if (start_command(&cp))
6a5cedac 1115 die("Could not run 'git status --porcelain' in submodule %s", path);
ee6fc514
JL
1116
1117 len = strbuf_read(&buf, cp.out, 1024);
c7e1a736
JL
1118 line = buf.buf;
1119 while (len > 2) {
1120 if ((line[0] == '?') && (line[1] == '?')) {
1121 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1122 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
1123 break;
1124 } else {
1125 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
3bfc4504
JL
1126 if (ignore_untracked ||
1127 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
c7e1a736
JL
1128 break;
1129 }
1130 next_line = strchr(line, '\n');
1131 if (!next_line)
1132 break;
1133 next_line++;
1134 len -= (next_line - line);
1135 line = next_line;
1136 }
ee6fc514
JL
1137 close(cp.out);
1138
1139 if (finish_command(&cp))
6a5cedac 1140 die("'git status --porcelain' failed in submodule %s", path);
ee6fc514 1141
ee6fc514 1142 strbuf_release(&buf);
c7e1a736 1143 return dirty_submodule;
ee6fc514 1144}
68d03e4a 1145
293ab15e
JL
1146int submodule_uses_gitfile(const char *path)
1147{
d3180279 1148 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e
JL
1149 const char *argv[] = {
1150 "submodule",
1151 "foreach",
1152 "--quiet",
1153 "--recursive",
1154 "test -f .git",
1155 NULL,
1156 };
1157 struct strbuf buf = STRBUF_INIT;
1158 const char *git_dir;
1159
1160 strbuf_addf(&buf, "%s/.git", path);
1161 git_dir = read_gitfile(buf.buf);
1162 if (!git_dir) {
1163 strbuf_release(&buf);
1164 return 0;
1165 }
1166 strbuf_release(&buf);
1167
1168 /* Now test that all nested submodules use a gitfile too */
293ab15e 1169 cp.argv = argv;
c12e8656 1170 prepare_submodule_repo_env(&cp.env_array);
293ab15e
JL
1171 cp.git_cmd = 1;
1172 cp.no_stdin = 1;
1173 cp.no_stderr = 1;
1174 cp.no_stdout = 1;
1175 cp.dir = path;
1176 if (run_command(&cp))
1177 return 0;
1178
1179 return 1;
1180}
1181
83b76966
SB
1182/*
1183 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1184 * when doing so.
1185 *
1186 * Return 1 if we'd lose data, return 0 if the removal is fine,
1187 * and negative values for errors.
1188 */
1189int bad_to_remove_submodule(const char *path, unsigned flags)
293ab15e 1190{
293ab15e 1191 ssize_t len;
d3180279 1192 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e 1193 struct strbuf buf = STRBUF_INIT;
83b76966 1194 int ret = 0;
293ab15e 1195
dbe44faa 1196 if (!file_exists(path) || is_empty_dir(path))
83b76966 1197 return 0;
293ab15e
JL
1198
1199 if (!submodule_uses_gitfile(path))
83b76966 1200 return 1;
293ab15e 1201
83b76966 1202 argv_array_pushl(&cp.args, "status", "--porcelain",
5a1c824f 1203 "--ignore-submodules=none", NULL);
83b76966
SB
1204
1205 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1206 argv_array_push(&cp.args, "-uno");
1207 else
1208 argv_array_push(&cp.args, "-uall");
1209
1210 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1211 argv_array_push(&cp.args, "--ignored");
293ab15e 1212
c12e8656 1213 prepare_submodule_repo_env(&cp.env_array);
293ab15e
JL
1214 cp.git_cmd = 1;
1215 cp.no_stdin = 1;
1216 cp.out = -1;
1217 cp.dir = path;
83b76966
SB
1218 if (start_command(&cp)) {
1219 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1220 die(_("could not start 'git status in submodule '%s'"),
1221 path);
1222 ret = -1;
1223 goto out;
1224 }
293ab15e
JL
1225
1226 len = strbuf_read(&buf, cp.out, 1024);
1227 if (len > 2)
83b76966 1228 ret = 1;
293ab15e
JL
1229 close(cp.out);
1230
83b76966
SB
1231 if (finish_command(&cp)) {
1232 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1233 die(_("could not run 'git status in submodule '%s'"),
1234 path);
1235 ret = -1;
1236 }
1237out:
293ab15e 1238 strbuf_release(&buf);
83b76966 1239 return ret;
293ab15e
JL
1240}
1241
202275b9
SB
1242static const char *get_super_prefix_or_empty(void)
1243{
1244 const char *s = get_super_prefix();
1245 if (!s)
1246 s = "";
1247 return s;
1248}
1249
68d03e4a
HV
1250static int find_first_merges(struct object_array *result, const char *path,
1251 struct commit *a, struct commit *b)
1252{
1253 int i, j;
3826902d 1254 struct object_array merges = OBJECT_ARRAY_INIT;
68d03e4a
HV
1255 struct commit *commit;
1256 int contains_another;
1257
1258 char merged_revision[42];
1259 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1260 "--all", merged_revision, NULL };
1261 struct rev_info revs;
1262 struct setup_revision_opt rev_opts;
1263
68d03e4a
HV
1264 memset(result, 0, sizeof(struct object_array));
1265 memset(&rev_opts, 0, sizeof(rev_opts));
1266
1267 /* get all revisions that merge commit a */
1268 snprintf(merged_revision, sizeof(merged_revision), "^%s",
f2fd0760 1269 oid_to_hex(&a->object.oid));
68d03e4a
HV
1270 init_revisions(&revs, NULL);
1271 rev_opts.submodule = path;
94c0cc8f 1272 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
68d03e4a
HV
1273
1274 /* save all revisions from the above list that contain b */
1275 if (prepare_revision_walk(&revs))
1276 die("revision walk setup failed");
1277 while ((commit = get_revision(&revs)) != NULL) {
1278 struct object *o = &(commit->object);
a20efee9 1279 if (in_merge_bases(b, commit))
68d03e4a
HV
1280 add_object_array(o, NULL, &merges);
1281 }
bcc0a3ea 1282 reset_revision_walk();
68d03e4a
HV
1283
1284 /* Now we've got all merges that contain a and b. Prune all
1285 * merges that contain another found merge and save them in
1286 * result.
1287 */
1288 for (i = 0; i < merges.nr; i++) {
1289 struct commit *m1 = (struct commit *) merges.objects[i].item;
1290
1291 contains_another = 0;
1292 for (j = 0; j < merges.nr; j++) {
1293 struct commit *m2 = (struct commit *) merges.objects[j].item;
a20efee9 1294 if (i != j && in_merge_bases(m2, m1)) {
68d03e4a
HV
1295 contains_another = 1;
1296 break;
1297 }
1298 }
1299
1300 if (!contains_another)
5de0c015 1301 add_object_array(merges.objects[i].item, NULL, result);
68d03e4a
HV
1302 }
1303
1304 free(merges.objects);
1305 return result->nr;
1306}
1307
1308static void print_commit(struct commit *commit)
1309{
1310 struct strbuf sb = STRBUF_INIT;
1311 struct pretty_print_context ctx = {0};
a5481a6c 1312 ctx.date_mode.type = DATE_NORMAL;
68d03e4a
HV
1313 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1314 fprintf(stderr, "%s\n", sb.buf);
1315 strbuf_release(&sb);
1316}
1317
1318#define MERGE_WARNING(path, msg) \
1319 warning("Failed to merge submodule %s (%s)", path, msg);
1320
1321int merge_submodule(unsigned char result[20], const char *path,
1322 const unsigned char base[20], const unsigned char a[20],
80988783 1323 const unsigned char b[20], int search)
68d03e4a
HV
1324{
1325 struct commit *commit_base, *commit_a, *commit_b;
1326 int parent_count;
1327 struct object_array merges;
1328
1329 int i;
1330
1331 /* store a in result in case we fail */
1332 hashcpy(result, a);
1333
1334 /* we can not handle deletion conflicts */
1335 if (is_null_sha1(base))
1336 return 0;
1337 if (is_null_sha1(a))
1338 return 0;
1339 if (is_null_sha1(b))
1340 return 0;
1341
1342 if (add_submodule_odb(path)) {
1343 MERGE_WARNING(path, "not checked out");
1344 return 0;
1345 }
1346
1347 if (!(commit_base = lookup_commit_reference(base)) ||
1348 !(commit_a = lookup_commit_reference(a)) ||
1349 !(commit_b = lookup_commit_reference(b))) {
1350 MERGE_WARNING(path, "commits not present");
1351 return 0;
1352 }
1353
1354 /* check whether both changes are forward */
a20efee9
JH
1355 if (!in_merge_bases(commit_base, commit_a) ||
1356 !in_merge_bases(commit_base, commit_b)) {
68d03e4a
HV
1357 MERGE_WARNING(path, "commits don't follow merge-base");
1358 return 0;
1359 }
1360
1361 /* Case #1: a is contained in b or vice versa */
a20efee9 1362 if (in_merge_bases(commit_a, commit_b)) {
68d03e4a
HV
1363 hashcpy(result, b);
1364 return 1;
1365 }
a20efee9 1366 if (in_merge_bases(commit_b, commit_a)) {
68d03e4a
HV
1367 hashcpy(result, a);
1368 return 1;
1369 }
1370
1371 /*
1372 * Case #2: There are one or more merges that contain a and b in
1373 * the submodule. If there is only one, then present it as a
1374 * suggestion to the user, but leave it marked unmerged so the
1375 * user needs to confirm the resolution.
1376 */
1377
80988783
BK
1378 /* Skip the search if makes no sense to the calling context. */
1379 if (!search)
1380 return 0;
1381
68d03e4a
HV
1382 /* find commit which merges them */
1383 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1384 switch (parent_count) {
1385 case 0:
1386 MERGE_WARNING(path, "merge following commits not found");
1387 break;
1388
1389 case 1:
1390 MERGE_WARNING(path, "not fast-forward");
1391 fprintf(stderr, "Found a possible merge resolution "
1392 "for the submodule:\n");
1393 print_commit((struct commit *) merges.objects[0].item);
1394 fprintf(stderr,
1395 "If this is correct simply add it to the index "
1396 "for example\n"
1397 "by using:\n\n"
1398 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1399 "which will accept this suggestion.\n",
f2fd0760 1400 oid_to_hex(&merges.objects[0].item->oid), path);
68d03e4a
HV
1401 break;
1402
1403 default:
1404 MERGE_WARNING(path, "multiple merges found");
1405 for (i = 0; i < merges.nr; i++)
1406 print_commit((struct commit *) merges.objects[i].item);
1407 }
1408
1409 free(merges.objects);
1410 return 0;
1411}
a88c915d 1412
a028a193
SB
1413int parallel_submodules(void)
1414{
1415 return parallel_jobs;
1416}
e059388f 1417
f6f85861
SB
1418/*
1419 * Embeds a single submodules git directory into the superprojects git dir,
1420 * non recursively.
1421 */
1422static void relocate_single_git_dir_into_superproject(const char *prefix,
1423 const char *path)
1424{
1425 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1426 const char *new_git_dir;
1427 const struct submodule *sub;
1428
1429 if (submodule_uses_worktrees(path))
1430 die(_("relocate_gitdir for submodule '%s' with "
1431 "more than one worktree not supported"), path);
1432
1433 old_git_dir = xstrfmt("%s/.git", path);
1434 if (read_gitfile(old_git_dir))
1435 /* If it is an actual gitfile, it doesn't need migration. */
1436 return;
1437
55d128ae 1438 real_old_git_dir = real_pathdup(old_git_dir);
f6f85861
SB
1439
1440 sub = submodule_from_path(null_sha1, path);
1441 if (!sub)
1442 die(_("could not lookup name for submodule '%s'"), path);
1443
1444 new_git_dir = git_path("modules/%s", sub->name);
1445 if (safe_create_leading_directories_const(new_git_dir) < 0)
1446 die(_("could not create directory '%s'"), new_git_dir);
55d128ae 1447 real_new_git_dir = real_pathdup(new_git_dir);
f6f85861 1448
f6f85861 1449 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
202275b9 1450 get_super_prefix_or_empty(), path,
f6f85861
SB
1451 real_old_git_dir, real_new_git_dir);
1452
1453 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
1454
1455 free(old_git_dir);
1456 free(real_old_git_dir);
1457 free(real_new_git_dir);
1458}
1459
1460/*
1461 * Migrate the git directory of the submodule given by path from
1462 * having its git directory within the working tree to the git dir nested
1463 * in its superprojects git dir under modules/.
1464 */
1465void absorb_git_dir_into_superproject(const char *prefix,
1466 const char *path,
1467 unsigned flags)
1468{
ec9629b3
SB
1469 int err_code;
1470 const char *sub_git_dir;
f6f85861 1471 struct strbuf gitdir = STRBUF_INIT;
f6f85861 1472 strbuf_addf(&gitdir, "%s/.git", path);
ec9629b3 1473 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
f6f85861
SB
1474
1475 /* Not populated? */
ec9629b3 1476 if (!sub_git_dir) {
ec9629b3
SB
1477 const struct submodule *sub;
1478
1479 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
1480 /* unpopulated as expected */
1481 strbuf_release(&gitdir);
1482 return;
1483 }
1484
1485 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
1486 /* We don't know what broke here. */
1487 read_gitfile_error_die(err_code, path, NULL);
1488
1489 /*
1490 * Maybe populated, but no git directory was found?
1491 * This can happen if the superproject is a submodule
1492 * itself and was just absorbed. The absorption of the
1493 * superproject did not rewrite the git file links yet,
1494 * fix it now.
1495 */
1496 sub = submodule_from_path(null_sha1, path);
1497 if (!sub)
1498 die(_("could not lookup name for submodule '%s'"), path);
365444a6
SB
1499 connect_work_tree_and_git_dir(path,
1500 git_path("modules/%s", sub->name));
ec9629b3
SB
1501 } else {
1502 /* Is it already absorbed into the superprojects git dir? */
1503 char *real_sub_git_dir = real_pathdup(sub_git_dir);
1504 char *real_common_git_dir = real_pathdup(get_git_common_dir());
f6f85861 1505
ec9629b3
SB
1506 if (!starts_with(real_sub_git_dir, real_common_git_dir))
1507 relocate_single_git_dir_into_superproject(prefix, path);
1508
1509 free(real_sub_git_dir);
1510 free(real_common_git_dir);
1511 }
1512 strbuf_release(&gitdir);
f6f85861
SB
1513
1514 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1515 struct child_process cp = CHILD_PROCESS_INIT;
1516 struct strbuf sb = STRBUF_INIT;
1517
1518 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
1519 die("BUG: we don't know how to pass the flags down?");
1520
202275b9 1521 strbuf_addstr(&sb, get_super_prefix_or_empty());
f6f85861
SB
1522 strbuf_addstr(&sb, path);
1523 strbuf_addch(&sb, '/');
1524
1525 cp.dir = path;
1526 cp.git_cmd = 1;
1527 cp.no_stdin = 1;
1528 argv_array_pushl(&cp.args, "--super-prefix", sb.buf,
1529 "submodule--helper",
1530 "absorb-git-dirs", NULL);
1531 prepare_submodule_repo_env(&cp.env_array);
1532 if (run_command(&cp))
1533 die(_("could not recurse into submodule '%s'"), path);
1534
1535 strbuf_release(&sb);
1536 }
f6f85861 1537}