]> git.ipfire.org Git - thirdparty/git.git/blob - submodule.c
pathspec: remove PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE flag
[thirdparty/git.git] / submodule.c
1 #include "cache.h"
2 #include "submodule-config.h"
3 #include "submodule.h"
4 #include "dir.h"
5 #include "diff.h"
6 #include "commit.h"
7 #include "revision.h"
8 #include "run-command.h"
9 #include "diffcore.h"
10 #include "refs.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "argv-array.h"
14 #include "blob.h"
15 #include "thread-utils.h"
16 #include "quote.h"
17 #include "remote.h"
18 #include "worktree.h"
19
20 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
21 static int config_update_recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
22 static int parallel_jobs = 1;
23 static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP;
24 static int initialized_fetch_ref_tips;
25 static struct oid_array ref_tips_before_fetch;
26 static struct oid_array ref_tips_after_fetch;
27
28 /*
29 * The following flag is set if the .gitmodules file is unmerged. We then
30 * disable recursion for all submodules where .git/config doesn't have a
31 * matching config entry because we can't guess what might be configured in
32 * .gitmodules unless the user resolves the conflict. When a command line
33 * option is given (which always overrides configuration) this flag will be
34 * ignored.
35 */
36 static int gitmodules_is_unmerged;
37
38 /*
39 * This flag is set if the .gitmodules file had unstaged modifications on
40 * startup. This must be checked before allowing modifications to the
41 * .gitmodules file with the intention to stage them later, because when
42 * continuing we would stage the modifications the user didn't stage herself
43 * too. That might change in a future version when we learn to stage the
44 * changes we do ourselves without staging any previous modifications.
45 */
46 static int gitmodules_is_modified;
47
48 int is_staging_gitmodules_ok(void)
49 {
50 return !gitmodules_is_modified;
51 }
52
53 /*
54 * Try to update the "path" entry in the "submodule.<name>" section of the
55 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
56 * with the correct path=<oldpath> setting was found and we could update it.
57 */
58 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
59 {
60 struct strbuf entry = STRBUF_INIT;
61 const struct submodule *submodule;
62
63 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
64 return -1;
65
66 if (gitmodules_is_unmerged)
67 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
68
69 submodule = submodule_from_path(null_sha1, oldpath);
70 if (!submodule || !submodule->name) {
71 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
72 return -1;
73 }
74 strbuf_addstr(&entry, "submodule.");
75 strbuf_addstr(&entry, submodule->name);
76 strbuf_addstr(&entry, ".path");
77 if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
78 /* Maybe the user already did that, don't error out here */
79 warning(_("Could not update .gitmodules entry %s"), entry.buf);
80 strbuf_release(&entry);
81 return -1;
82 }
83 strbuf_release(&entry);
84 return 0;
85 }
86
87 /*
88 * Try to remove the "submodule.<name>" section from .gitmodules where the given
89 * path is configured. Return 0 only if a .gitmodules file was found, a section
90 * with the correct path=<path> setting was found and we could remove it.
91 */
92 int remove_path_from_gitmodules(const char *path)
93 {
94 struct strbuf sect = STRBUF_INIT;
95 const struct submodule *submodule;
96
97 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
98 return -1;
99
100 if (gitmodules_is_unmerged)
101 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
102
103 submodule = submodule_from_path(null_sha1, path);
104 if (!submodule || !submodule->name) {
105 warning(_("Could not find section in .gitmodules where path=%s"), path);
106 return -1;
107 }
108 strbuf_addstr(&sect, "submodule.");
109 strbuf_addstr(&sect, submodule->name);
110 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
111 /* Maybe the user already did that, don't error out here */
112 warning(_("Could not remove .gitmodules entry for %s"), path);
113 strbuf_release(&sect);
114 return -1;
115 }
116 strbuf_release(&sect);
117 return 0;
118 }
119
120 void stage_updated_gitmodules(void)
121 {
122 if (add_file_to_cache(".gitmodules", 0))
123 die(_("staging updated .gitmodules failed"));
124 }
125
126 static int add_submodule_odb(const char *path)
127 {
128 struct strbuf objects_directory = STRBUF_INIT;
129 int ret = 0;
130
131 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
132 if (ret)
133 goto done;
134 if (!is_directory(objects_directory.buf)) {
135 ret = -1;
136 goto done;
137 }
138 add_to_alternates_memory(objects_directory.buf);
139 done:
140 strbuf_release(&objects_directory);
141 return ret;
142 }
143
144 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
145 const char *path)
146 {
147 const struct submodule *submodule = submodule_from_path(null_sha1, path);
148 if (submodule) {
149 if (submodule->ignore)
150 handle_ignore_submodules_arg(diffopt, submodule->ignore);
151 else if (gitmodules_is_unmerged)
152 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
153 }
154 }
155
156 int submodule_config(const char *var, const char *value, void *cb)
157 {
158 if (!strcmp(var, "submodule.fetchjobs")) {
159 parallel_jobs = git_config_int(var, value);
160 if (parallel_jobs < 0)
161 die(_("negative values not allowed for submodule.fetchJobs"));
162 return 0;
163 } else if (starts_with(var, "submodule."))
164 return parse_submodule_config_option(var, value);
165 else if (!strcmp(var, "fetch.recursesubmodules")) {
166 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
167 return 0;
168 }
169 return 0;
170 }
171
172 void gitmodules_config(void)
173 {
174 const char *work_tree = get_git_work_tree();
175 if (work_tree) {
176 struct strbuf gitmodules_path = STRBUF_INIT;
177 int pos;
178 strbuf_addstr(&gitmodules_path, work_tree);
179 strbuf_addstr(&gitmodules_path, "/.gitmodules");
180 if (read_cache() < 0)
181 die("index file corrupt");
182 pos = cache_name_pos(".gitmodules", 11);
183 if (pos < 0) { /* .gitmodules not found or isn't merged */
184 pos = -1 - pos;
185 if (active_nr > pos) { /* there is a .gitmodules */
186 const struct cache_entry *ce = active_cache[pos];
187 if (ce_namelen(ce) == 11 &&
188 !memcmp(ce->name, ".gitmodules", 11))
189 gitmodules_is_unmerged = 1;
190 }
191 } else if (pos < active_nr) {
192 struct stat st;
193 if (lstat(".gitmodules", &st) == 0 &&
194 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
195 gitmodules_is_modified = 1;
196 }
197
198 if (!gitmodules_is_unmerged)
199 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
200 strbuf_release(&gitmodules_path);
201 }
202 }
203
204 void gitmodules_config_sha1(const unsigned char *commit_sha1)
205 {
206 struct strbuf rev = STRBUF_INIT;
207 unsigned char sha1[20];
208
209 if (gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) {
210 git_config_from_blob_sha1(submodule_config, rev.buf,
211 sha1, NULL);
212 }
213 strbuf_release(&rev);
214 }
215
216 /*
217 * NEEDSWORK: With the addition of different configuration options to determine
218 * if a submodule is of interests, the validity of this function's name comes
219 * into question. Once the dust has settled and more concrete terminology is
220 * decided upon, come up with a more proper name for this function. One
221 * potential candidate could be 'is_submodule_active()'.
222 *
223 * Determine if a submodule has been initialized at a given 'path'
224 */
225 int is_submodule_initialized(const char *path)
226 {
227 int ret = 0;
228 char *key = NULL;
229 char *value = NULL;
230 const struct string_list *sl;
231 const struct submodule *module = submodule_from_path(null_sha1, path);
232
233 /* early return if there isn't a path->module mapping */
234 if (!module)
235 return 0;
236
237 /* submodule.<name>.active is set */
238 key = xstrfmt("submodule.%s.active", module->name);
239 if (!git_config_get_bool(key, &ret)) {
240 free(key);
241 return ret;
242 }
243 free(key);
244
245 /* submodule.active is set */
246 sl = git_config_get_value_multi("submodule.active");
247 if (sl) {
248 struct pathspec ps;
249 struct argv_array args = ARGV_ARRAY_INIT;
250 const struct string_list_item *item;
251
252 for_each_string_list_item(item, sl) {
253 argv_array_push(&args, item->string);
254 }
255
256 parse_pathspec(&ps, 0, 0, NULL, args.argv);
257 ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
258
259 argv_array_clear(&args);
260 clear_pathspec(&ps);
261 return ret;
262 }
263
264 /* fallback to checking if the URL is set */
265 key = xstrfmt("submodule.%s.url", module->name);
266 ret = !git_config_get_string(key, &value);
267
268 free(value);
269 free(key);
270 return ret;
271 }
272
273 int is_submodule_populated_gently(const char *path, int *return_error_code)
274 {
275 int ret = 0;
276 char *gitdir = xstrfmt("%s/.git", path);
277
278 if (resolve_gitdir_gently(gitdir, return_error_code))
279 ret = 1;
280
281 free(gitdir);
282 return ret;
283 }
284
285 /*
286 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
287 */
288 void die_in_unpopulated_submodule(const struct index_state *istate,
289 const char *prefix)
290 {
291 int i, prefixlen;
292
293 if (!prefix)
294 return;
295
296 prefixlen = strlen(prefix);
297
298 for (i = 0; i < istate->cache_nr; i++) {
299 struct cache_entry *ce = istate->cache[i];
300 int ce_len = ce_namelen(ce);
301
302 if (!S_ISGITLINK(ce->ce_mode))
303 continue;
304 if (prefixlen <= ce_len)
305 continue;
306 if (strncmp(ce->name, prefix, ce_len))
307 continue;
308 if (prefix[ce_len] != '/')
309 continue;
310
311 die(_("in unpopulated submodule '%s'"), ce->name);
312 }
313 }
314
315 /*
316 * Dies if any paths in the provided pathspec descends into a submodule
317 */
318 void die_path_inside_submodule(const struct index_state *istate,
319 const struct pathspec *ps)
320 {
321 int i, j;
322
323 for (i = 0; i < istate->cache_nr; i++) {
324 struct cache_entry *ce = istate->cache[i];
325 int ce_len = ce_namelen(ce);
326
327 if (!S_ISGITLINK(ce->ce_mode))
328 continue;
329
330 for (j = 0; j < ps->nr ; j++) {
331 const struct pathspec_item *item = &ps->items[j];
332
333 if (item->len <= ce_len)
334 continue;
335 if (item->match[ce_len] != '/')
336 continue;
337 if (strncmp(ce->name, item->match, ce_len))
338 continue;
339 if (item->len == ce_len + 1)
340 continue;
341
342 die(_("Pathspec '%s' is in submodule '%.*s'"),
343 item->original, ce_len, ce->name);
344 }
345 }
346 }
347
348 int parse_submodule_update_strategy(const char *value,
349 struct submodule_update_strategy *dst)
350 {
351 free((void*)dst->command);
352 dst->command = NULL;
353 if (!strcmp(value, "none"))
354 dst->type = SM_UPDATE_NONE;
355 else if (!strcmp(value, "checkout"))
356 dst->type = SM_UPDATE_CHECKOUT;
357 else if (!strcmp(value, "rebase"))
358 dst->type = SM_UPDATE_REBASE;
359 else if (!strcmp(value, "merge"))
360 dst->type = SM_UPDATE_MERGE;
361 else if (skip_prefix(value, "!", &value)) {
362 dst->type = SM_UPDATE_COMMAND;
363 dst->command = xstrdup(value);
364 } else
365 return -1;
366 return 0;
367 }
368
369 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
370 {
371 struct strbuf sb = STRBUF_INIT;
372 switch (s->type) {
373 case SM_UPDATE_CHECKOUT:
374 return "checkout";
375 case SM_UPDATE_MERGE:
376 return "merge";
377 case SM_UPDATE_REBASE:
378 return "rebase";
379 case SM_UPDATE_NONE:
380 return "none";
381 case SM_UPDATE_UNSPECIFIED:
382 return NULL;
383 case SM_UPDATE_COMMAND:
384 strbuf_addf(&sb, "!%s", s->command);
385 return strbuf_detach(&sb, NULL);
386 }
387 return NULL;
388 }
389
390 void handle_ignore_submodules_arg(struct diff_options *diffopt,
391 const char *arg)
392 {
393 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
394 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
395 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
396
397 if (!strcmp(arg, "all"))
398 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
399 else if (!strcmp(arg, "untracked"))
400 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
401 else if (!strcmp(arg, "dirty"))
402 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
403 else if (strcmp(arg, "none"))
404 die("bad --ignore-submodules argument: %s", arg);
405 }
406
407 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
408 struct commit *left, struct commit *right,
409 struct commit_list *merge_bases)
410 {
411 struct commit_list *list;
412
413 init_revisions(rev, NULL);
414 setup_revisions(0, NULL, rev, NULL);
415 rev->left_right = 1;
416 rev->first_parent_only = 1;
417 left->object.flags |= SYMMETRIC_LEFT;
418 add_pending_object(rev, &left->object, path);
419 add_pending_object(rev, &right->object, path);
420 for (list = merge_bases; list; list = list->next) {
421 list->item->object.flags |= UNINTERESTING;
422 add_pending_object(rev, &list->item->object,
423 oid_to_hex(&list->item->object.oid));
424 }
425 return prepare_revision_walk(rev);
426 }
427
428 static void print_submodule_summary(struct rev_info *rev, FILE *f,
429 const char *line_prefix,
430 const char *del, const char *add, const char *reset)
431 {
432 static const char format[] = " %m %s";
433 struct strbuf sb = STRBUF_INIT;
434 struct commit *commit;
435
436 while ((commit = get_revision(rev))) {
437 struct pretty_print_context ctx = {0};
438 ctx.date_mode = rev->date_mode;
439 ctx.output_encoding = get_log_output_encoding();
440 strbuf_setlen(&sb, 0);
441 strbuf_addstr(&sb, line_prefix);
442 if (commit->object.flags & SYMMETRIC_LEFT) {
443 if (del)
444 strbuf_addstr(&sb, del);
445 }
446 else if (add)
447 strbuf_addstr(&sb, add);
448 format_commit_message(commit, format, &sb, &ctx);
449 if (reset)
450 strbuf_addstr(&sb, reset);
451 strbuf_addch(&sb, '\n');
452 fprintf(f, "%s", sb.buf);
453 }
454 strbuf_release(&sb);
455 }
456
457 static void prepare_submodule_repo_env_no_git_dir(struct argv_array *out)
458 {
459 const char * const *var;
460
461 for (var = local_repo_env; *var; var++) {
462 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
463 argv_array_push(out, *var);
464 }
465 }
466
467 void prepare_submodule_repo_env(struct argv_array *out)
468 {
469 prepare_submodule_repo_env_no_git_dir(out);
470 argv_array_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT,
471 DEFAULT_GIT_DIR_ENVIRONMENT);
472 }
473
474 /* Helper function to display the submodule header line prior to the full
475 * summary output. If it can locate the submodule objects directory it will
476 * attempt to lookup both the left and right commits and put them into the
477 * left and right pointers.
478 */
479 static void show_submodule_header(FILE *f, const char *path,
480 const char *line_prefix,
481 struct object_id *one, struct object_id *two,
482 unsigned dirty_submodule, const char *meta,
483 const char *reset,
484 struct commit **left, struct commit **right,
485 struct commit_list **merge_bases)
486 {
487 const char *message = NULL;
488 struct strbuf sb = STRBUF_INIT;
489 int fast_forward = 0, fast_backward = 0;
490
491 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
492 fprintf(f, "%sSubmodule %s contains untracked content\n",
493 line_prefix, path);
494 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
495 fprintf(f, "%sSubmodule %s contains modified content\n",
496 line_prefix, path);
497
498 if (is_null_oid(one))
499 message = "(new submodule)";
500 else if (is_null_oid(two))
501 message = "(submodule deleted)";
502
503 if (add_submodule_odb(path)) {
504 if (!message)
505 message = "(not initialized)";
506 goto output_header;
507 }
508
509 /*
510 * Attempt to lookup the commit references, and determine if this is
511 * a fast forward or fast backwards update.
512 */
513 *left = lookup_commit_reference(one->hash);
514 *right = lookup_commit_reference(two->hash);
515
516 /*
517 * Warn about missing commits in the submodule project, but only if
518 * they aren't null.
519 */
520 if ((!is_null_oid(one) && !*left) ||
521 (!is_null_oid(two) && !*right))
522 message = "(commits not present)";
523
524 *merge_bases = get_merge_bases(*left, *right);
525 if (*merge_bases) {
526 if ((*merge_bases)->item == *left)
527 fast_forward = 1;
528 else if ((*merge_bases)->item == *right)
529 fast_backward = 1;
530 }
531
532 if (!oidcmp(one, two)) {
533 strbuf_release(&sb);
534 return;
535 }
536
537 output_header:
538 strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
539 strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
540 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
541 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
542 if (message)
543 strbuf_addf(&sb, " %s%s\n", message, reset);
544 else
545 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
546 fwrite(sb.buf, sb.len, 1, f);
547
548 strbuf_release(&sb);
549 }
550
551 void show_submodule_summary(FILE *f, const char *path,
552 const char *line_prefix,
553 struct object_id *one, struct object_id *two,
554 unsigned dirty_submodule, const char *meta,
555 const char *del, const char *add, const char *reset)
556 {
557 struct rev_info rev;
558 struct commit *left = NULL, *right = NULL;
559 struct commit_list *merge_bases = NULL;
560
561 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
562 meta, reset, &left, &right, &merge_bases);
563
564 /*
565 * If we don't have both a left and a right pointer, there is no
566 * reason to try and display a summary. The header line should contain
567 * all the information the user needs.
568 */
569 if (!left || !right)
570 goto out;
571
572 /* Treat revision walker failure the same as missing commits */
573 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
574 fprintf(f, "%s(revision walker failed)\n", line_prefix);
575 goto out;
576 }
577
578 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
579
580 out:
581 if (merge_bases)
582 free_commit_list(merge_bases);
583 clear_commit_marks(left, ~0);
584 clear_commit_marks(right, ~0);
585 }
586
587 void show_submodule_inline_diff(FILE *f, const char *path,
588 const char *line_prefix,
589 struct object_id *one, struct object_id *two,
590 unsigned dirty_submodule, const char *meta,
591 const char *del, const char *add, const char *reset,
592 const struct diff_options *o)
593 {
594 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
595 struct commit *left = NULL, *right = NULL;
596 struct commit_list *merge_bases = NULL;
597 struct strbuf submodule_dir = STRBUF_INIT;
598 struct child_process cp = CHILD_PROCESS_INIT;
599
600 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
601 meta, reset, &left, &right, &merge_bases);
602
603 /* We need a valid left and right commit to display a difference */
604 if (!(left || is_null_oid(one)) ||
605 !(right || is_null_oid(two)))
606 goto done;
607
608 if (left)
609 old = one;
610 if (right)
611 new = two;
612
613 fflush(f);
614 cp.git_cmd = 1;
615 cp.dir = path;
616 cp.out = dup(fileno(f));
617 cp.no_stdin = 1;
618
619 /* TODO: other options may need to be passed here. */
620 argv_array_push(&cp.args, "diff");
621 argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
622 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
623 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
624 o->b_prefix, path);
625 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
626 o->a_prefix, path);
627 } else {
628 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
629 o->a_prefix, path);
630 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
631 o->b_prefix, path);
632 }
633 argv_array_push(&cp.args, oid_to_hex(old));
634 /*
635 * If the submodule has modified content, we will diff against the
636 * work tree, under the assumption that the user has asked for the
637 * diff format and wishes to actually see all differences even if they
638 * haven't yet been committed to the submodule yet.
639 */
640 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
641 argv_array_push(&cp.args, oid_to_hex(new));
642
643 prepare_submodule_repo_env(&cp.env_array);
644 if (run_command(&cp))
645 fprintf(f, "(diff failed)\n");
646
647 done:
648 strbuf_release(&submodule_dir);
649 if (merge_bases)
650 free_commit_list(merge_bases);
651 if (left)
652 clear_commit_marks(left, ~0);
653 if (right)
654 clear_commit_marks(right, ~0);
655 }
656
657 void set_config_fetch_recurse_submodules(int value)
658 {
659 config_fetch_recurse_submodules = value;
660 }
661
662 void set_config_update_recurse_submodules(int value)
663 {
664 config_update_recurse_submodules = value;
665 }
666
667 int should_update_submodules(void)
668 {
669 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
670 }
671
672 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
673 {
674 if (!S_ISGITLINK(ce->ce_mode))
675 return NULL;
676
677 if (!should_update_submodules())
678 return NULL;
679
680 return submodule_from_path(null_sha1, ce->name);
681 }
682
683 static int has_remote(const char *refname, const struct object_id *oid,
684 int flags, void *cb_data)
685 {
686 return 1;
687 }
688
689 static int append_oid_to_argv(const struct object_id *oid, void *data)
690 {
691 struct argv_array *argv = data;
692 argv_array_push(argv, oid_to_hex(oid));
693 return 0;
694 }
695
696 static int check_has_commit(const struct object_id *oid, void *data)
697 {
698 int *has_commit = data;
699
700 if (!lookup_commit_reference(oid->hash))
701 *has_commit = 0;
702
703 return 0;
704 }
705
706 static int submodule_has_commits(const char *path, struct oid_array *commits)
707 {
708 int has_commit = 1;
709
710 if (add_submodule_odb(path))
711 return 0;
712
713 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
714 return has_commit;
715 }
716
717 static int submodule_needs_pushing(const char *path, struct oid_array *commits)
718 {
719 if (!submodule_has_commits(path, commits))
720 /*
721 * NOTE: We do consider it safe to return "no" here. The
722 * correct answer would be "We do not know" instead of
723 * "No push needed", but it is quite hard to change
724 * the submodule pointer without having the submodule
725 * around. If a user did however change the submodules
726 * without having the submodule around, this indicates
727 * an expert who knows what they are doing or a
728 * maintainer integrating work from other people. In
729 * both cases it should be safe to skip this check.
730 */
731 return 0;
732
733 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
734 struct child_process cp = CHILD_PROCESS_INIT;
735 struct strbuf buf = STRBUF_INIT;
736 int needs_pushing = 0;
737
738 argv_array_push(&cp.args, "rev-list");
739 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
740 argv_array_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
741
742 prepare_submodule_repo_env(&cp.env_array);
743 cp.git_cmd = 1;
744 cp.no_stdin = 1;
745 cp.out = -1;
746 cp.dir = path;
747 if (start_command(&cp))
748 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
749 path);
750 if (strbuf_read(&buf, cp.out, 41))
751 needs_pushing = 1;
752 finish_command(&cp);
753 close(cp.out);
754 strbuf_release(&buf);
755 return needs_pushing;
756 }
757
758 return 0;
759 }
760
761 static struct oid_array *submodule_commits(struct string_list *submodules,
762 const char *path)
763 {
764 struct string_list_item *item;
765
766 item = string_list_insert(submodules, path);
767 if (item->util)
768 return (struct oid_array *) item->util;
769
770 /* NEEDSWORK: should we have oid_array_init()? */
771 item->util = xcalloc(1, sizeof(struct oid_array));
772 return (struct oid_array *) item->util;
773 }
774
775 static void collect_submodules_from_diff(struct diff_queue_struct *q,
776 struct diff_options *options,
777 void *data)
778 {
779 int i;
780 struct string_list *submodules = data;
781
782 for (i = 0; i < q->nr; i++) {
783 struct diff_filepair *p = q->queue[i];
784 struct oid_array *commits;
785 if (!S_ISGITLINK(p->two->mode))
786 continue;
787 commits = submodule_commits(submodules, p->two->path);
788 oid_array_append(commits, &p->two->oid);
789 }
790 }
791
792 static void find_unpushed_submodule_commits(struct commit *commit,
793 struct string_list *needs_pushing)
794 {
795 struct rev_info rev;
796
797 init_revisions(&rev, NULL);
798 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
799 rev.diffopt.format_callback = collect_submodules_from_diff;
800 rev.diffopt.format_callback_data = needs_pushing;
801 diff_tree_combined_merge(commit, 1, &rev);
802 }
803
804 static void free_submodules_sha1s(struct string_list *submodules)
805 {
806 struct string_list_item *item;
807 for_each_string_list_item(item, submodules)
808 oid_array_clear((struct oid_array *) item->util);
809 string_list_clear(submodules, 1);
810 }
811
812 int find_unpushed_submodules(struct oid_array *commits,
813 const char *remotes_name, struct string_list *needs_pushing)
814 {
815 struct rev_info rev;
816 struct commit *commit;
817 struct string_list submodules = STRING_LIST_INIT_DUP;
818 struct string_list_item *submodule;
819 struct argv_array argv = ARGV_ARRAY_INIT;
820
821 init_revisions(&rev, NULL);
822
823 /* argv.argv[0] will be ignored by setup_revisions */
824 argv_array_push(&argv, "find_unpushed_submodules");
825 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
826 argv_array_push(&argv, "--not");
827 argv_array_pushf(&argv, "--remotes=%s", remotes_name);
828
829 setup_revisions(argv.argc, argv.argv, &rev, NULL);
830 if (prepare_revision_walk(&rev))
831 die("revision walk setup failed");
832
833 while ((commit = get_revision(&rev)) != NULL)
834 find_unpushed_submodule_commits(commit, &submodules);
835
836 reset_revision_walk();
837 argv_array_clear(&argv);
838
839 for_each_string_list_item(submodule, &submodules) {
840 struct oid_array *commits = (struct oid_array *) submodule->util;
841
842 if (submodule_needs_pushing(submodule->string, commits))
843 string_list_insert(needs_pushing, submodule->string);
844 }
845 free_submodules_sha1s(&submodules);
846
847 return needs_pushing->nr;
848 }
849
850 static int push_submodule(const char *path,
851 const struct remote *remote,
852 const char **refspec, int refspec_nr,
853 const struct string_list *push_options,
854 int dry_run)
855 {
856 if (add_submodule_odb(path))
857 return 1;
858
859 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
860 struct child_process cp = CHILD_PROCESS_INIT;
861 argv_array_push(&cp.args, "push");
862 if (dry_run)
863 argv_array_push(&cp.args, "--dry-run");
864
865 if (push_options && push_options->nr) {
866 const struct string_list_item *item;
867 for_each_string_list_item(item, push_options)
868 argv_array_pushf(&cp.args, "--push-option=%s",
869 item->string);
870 }
871
872 if (remote->origin != REMOTE_UNCONFIGURED) {
873 int i;
874 argv_array_push(&cp.args, remote->name);
875 for (i = 0; i < refspec_nr; i++)
876 argv_array_push(&cp.args, refspec[i]);
877 }
878
879 prepare_submodule_repo_env(&cp.env_array);
880 cp.git_cmd = 1;
881 cp.no_stdin = 1;
882 cp.dir = path;
883 if (run_command(&cp))
884 return 0;
885 close(cp.out);
886 }
887
888 return 1;
889 }
890
891 /*
892 * Perform a check in the submodule to see if the remote and refspec work.
893 * Die if the submodule can't be pushed.
894 */
895 static void submodule_push_check(const char *path, const struct remote *remote,
896 const char **refspec, int refspec_nr)
897 {
898 struct child_process cp = CHILD_PROCESS_INIT;
899 int i;
900
901 argv_array_push(&cp.args, "submodule--helper");
902 argv_array_push(&cp.args, "push-check");
903 argv_array_push(&cp.args, remote->name);
904
905 for (i = 0; i < refspec_nr; i++)
906 argv_array_push(&cp.args, refspec[i]);
907
908 prepare_submodule_repo_env(&cp.env_array);
909 cp.git_cmd = 1;
910 cp.no_stdin = 1;
911 cp.no_stdout = 1;
912 cp.dir = path;
913
914 /*
915 * Simply indicate if 'submodule--helper push-check' failed.
916 * More detailed error information will be provided by the
917 * child process.
918 */
919 if (run_command(&cp))
920 die("process for submodule '%s' failed", path);
921 }
922
923 int push_unpushed_submodules(struct oid_array *commits,
924 const struct remote *remote,
925 const char **refspec, int refspec_nr,
926 const struct string_list *push_options,
927 int dry_run)
928 {
929 int i, ret = 1;
930 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
931
932 if (!find_unpushed_submodules(commits, remote->name, &needs_pushing))
933 return 1;
934
935 /*
936 * Verify that the remote and refspec can be propagated to all
937 * submodules. This check can be skipped if the remote and refspec
938 * won't be propagated due to the remote being unconfigured (e.g. a URL
939 * instead of a remote name).
940 */
941 if (remote->origin != REMOTE_UNCONFIGURED)
942 for (i = 0; i < needs_pushing.nr; i++)
943 submodule_push_check(needs_pushing.items[i].string,
944 remote, refspec, refspec_nr);
945
946 /* Actually push the submodules */
947 for (i = 0; i < needs_pushing.nr; i++) {
948 const char *path = needs_pushing.items[i].string;
949 fprintf(stderr, "Pushing submodule '%s'\n", path);
950 if (!push_submodule(path, remote, refspec, refspec_nr,
951 push_options, dry_run)) {
952 fprintf(stderr, "Unable to push submodule '%s'\n", path);
953 ret = 0;
954 }
955 }
956
957 string_list_clear(&needs_pushing, 0);
958
959 return ret;
960 }
961
962 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
963 {
964 int is_present = 0;
965 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
966 /* Even if the submodule is checked out and the commit is
967 * present, make sure it is reachable from a ref. */
968 struct child_process cp = CHILD_PROCESS_INIT;
969 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
970 struct strbuf buf = STRBUF_INIT;
971
972 argv[3] = sha1_to_hex(sha1);
973 cp.argv = argv;
974 prepare_submodule_repo_env(&cp.env_array);
975 cp.git_cmd = 1;
976 cp.no_stdin = 1;
977 cp.dir = path;
978 if (!capture_command(&cp, &buf, 1024) && !buf.len)
979 is_present = 1;
980
981 strbuf_release(&buf);
982 }
983 return is_present;
984 }
985
986 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
987 struct diff_options *options,
988 void *data)
989 {
990 int i;
991 for (i = 0; i < q->nr; i++) {
992 struct diff_filepair *p = q->queue[i];
993 if (!S_ISGITLINK(p->two->mode))
994 continue;
995
996 if (S_ISGITLINK(p->one->mode)) {
997 /* NEEDSWORK: We should honor the name configured in
998 * the .gitmodules file of the commit we are examining
999 * here to be able to correctly follow submodules
1000 * being moved around. */
1001 struct string_list_item *path;
1002 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
1003 if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash))
1004 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
1005 } else {
1006 /* Submodule is new or was moved here */
1007 /* NEEDSWORK: When the .git directories of submodules
1008 * live inside the superprojects .git directory some
1009 * day we should fetch new submodules directly into
1010 * that location too when config or options request
1011 * that so they can be checked out from there. */
1012 continue;
1013 }
1014 }
1015 }
1016
1017 static int add_sha1_to_array(const char *ref, const struct object_id *oid,
1018 int flags, void *data)
1019 {
1020 oid_array_append(data, oid);
1021 return 0;
1022 }
1023
1024 void check_for_new_submodule_commits(struct object_id *oid)
1025 {
1026 if (!initialized_fetch_ref_tips) {
1027 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
1028 initialized_fetch_ref_tips = 1;
1029 }
1030
1031 oid_array_append(&ref_tips_after_fetch, oid);
1032 }
1033
1034 static int add_oid_to_argv(const struct object_id *oid, void *data)
1035 {
1036 argv_array_push(data, oid_to_hex(oid));
1037 return 0;
1038 }
1039
1040 static void calculate_changed_submodule_paths(void)
1041 {
1042 struct rev_info rev;
1043 struct commit *commit;
1044 struct argv_array argv = ARGV_ARRAY_INIT;
1045
1046 /* No need to check if there are no submodules configured */
1047 if (!submodule_from_path(NULL, NULL))
1048 return;
1049
1050 init_revisions(&rev, NULL);
1051 argv_array_push(&argv, "--"); /* argv[0] program name */
1052 oid_array_for_each_unique(&ref_tips_after_fetch,
1053 add_oid_to_argv, &argv);
1054 argv_array_push(&argv, "--not");
1055 oid_array_for_each_unique(&ref_tips_before_fetch,
1056 add_oid_to_argv, &argv);
1057 setup_revisions(argv.argc, argv.argv, &rev, NULL);
1058 if (prepare_revision_walk(&rev))
1059 die("revision walk setup failed");
1060
1061 /*
1062 * Collect all submodules (whether checked out or not) for which new
1063 * commits have been recorded upstream in "changed_submodule_paths".
1064 */
1065 while ((commit = get_revision(&rev))) {
1066 struct commit_list *parent = commit->parents;
1067 while (parent) {
1068 struct diff_options diff_opts;
1069 diff_setup(&diff_opts);
1070 DIFF_OPT_SET(&diff_opts, RECURSIVE);
1071 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
1072 diff_opts.format_callback = submodule_collect_changed_cb;
1073 diff_setup_done(&diff_opts);
1074 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
1075 diffcore_std(&diff_opts);
1076 diff_flush(&diff_opts);
1077 parent = parent->next;
1078 }
1079 }
1080
1081 argv_array_clear(&argv);
1082 oid_array_clear(&ref_tips_before_fetch);
1083 oid_array_clear(&ref_tips_after_fetch);
1084 initialized_fetch_ref_tips = 0;
1085 }
1086
1087 struct submodule_parallel_fetch {
1088 int count;
1089 struct argv_array args;
1090 const char *work_tree;
1091 const char *prefix;
1092 int command_line_option;
1093 int quiet;
1094 int result;
1095 };
1096 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
1097
1098 static int get_next_submodule(struct child_process *cp,
1099 struct strbuf *err, void *data, void **task_cb)
1100 {
1101 int ret = 0;
1102 struct submodule_parallel_fetch *spf = data;
1103
1104 for (; spf->count < active_nr; spf->count++) {
1105 struct strbuf submodule_path = STRBUF_INIT;
1106 struct strbuf submodule_git_dir = STRBUF_INIT;
1107 struct strbuf submodule_prefix = STRBUF_INIT;
1108 const struct cache_entry *ce = active_cache[spf->count];
1109 const char *git_dir, *default_argv;
1110 const struct submodule *submodule;
1111
1112 if (!S_ISGITLINK(ce->ce_mode))
1113 continue;
1114
1115 submodule = submodule_from_path(null_sha1, ce->name);
1116 if (!submodule)
1117 submodule = submodule_from_name(null_sha1, ce->name);
1118
1119 default_argv = "yes";
1120 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
1121 if (submodule &&
1122 submodule->fetch_recurse !=
1123 RECURSE_SUBMODULES_NONE) {
1124 if (submodule->fetch_recurse ==
1125 RECURSE_SUBMODULES_OFF)
1126 continue;
1127 if (submodule->fetch_recurse ==
1128 RECURSE_SUBMODULES_ON_DEMAND) {
1129 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
1130 continue;
1131 default_argv = "on-demand";
1132 }
1133 } else {
1134 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
1135 gitmodules_is_unmerged)
1136 continue;
1137 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
1138 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
1139 continue;
1140 default_argv = "on-demand";
1141 }
1142 }
1143 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
1144 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
1145 continue;
1146 default_argv = "on-demand";
1147 }
1148
1149 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
1150 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
1151 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
1152 git_dir = read_gitfile(submodule_git_dir.buf);
1153 if (!git_dir)
1154 git_dir = submodule_git_dir.buf;
1155 if (is_directory(git_dir)) {
1156 child_process_init(cp);
1157 cp->dir = strbuf_detach(&submodule_path, NULL);
1158 prepare_submodule_repo_env(&cp->env_array);
1159 cp->git_cmd = 1;
1160 if (!spf->quiet)
1161 strbuf_addf(err, "Fetching submodule %s%s\n",
1162 spf->prefix, ce->name);
1163 argv_array_init(&cp->args);
1164 argv_array_pushv(&cp->args, spf->args.argv);
1165 argv_array_push(&cp->args, default_argv);
1166 argv_array_push(&cp->args, "--submodule-prefix");
1167 argv_array_push(&cp->args, submodule_prefix.buf);
1168 ret = 1;
1169 }
1170 strbuf_release(&submodule_path);
1171 strbuf_release(&submodule_git_dir);
1172 strbuf_release(&submodule_prefix);
1173 if (ret) {
1174 spf->count++;
1175 return 1;
1176 }
1177 }
1178 return 0;
1179 }
1180
1181 static int fetch_start_failure(struct strbuf *err,
1182 void *cb, void *task_cb)
1183 {
1184 struct submodule_parallel_fetch *spf = cb;
1185
1186 spf->result = 1;
1187
1188 return 0;
1189 }
1190
1191 static int fetch_finish(int retvalue, struct strbuf *err,
1192 void *cb, void *task_cb)
1193 {
1194 struct submodule_parallel_fetch *spf = cb;
1195
1196 if (retvalue)
1197 spf->result = 1;
1198
1199 return 0;
1200 }
1201
1202 int fetch_populated_submodules(const struct argv_array *options,
1203 const char *prefix, int command_line_option,
1204 int quiet, int max_parallel_jobs)
1205 {
1206 int i;
1207 struct submodule_parallel_fetch spf = SPF_INIT;
1208
1209 spf.work_tree = get_git_work_tree();
1210 spf.command_line_option = command_line_option;
1211 spf.quiet = quiet;
1212 spf.prefix = prefix;
1213
1214 if (!spf.work_tree)
1215 goto out;
1216
1217 if (read_cache() < 0)
1218 die("index file corrupt");
1219
1220 argv_array_push(&spf.args, "fetch");
1221 for (i = 0; i < options->argc; i++)
1222 argv_array_push(&spf.args, options->argv[i]);
1223 argv_array_push(&spf.args, "--recurse-submodules-default");
1224 /* default value, "--submodule-prefix" and its value are added later */
1225
1226 if (max_parallel_jobs < 0)
1227 max_parallel_jobs = parallel_jobs;
1228
1229 calculate_changed_submodule_paths();
1230 run_processes_parallel(max_parallel_jobs,
1231 get_next_submodule,
1232 fetch_start_failure,
1233 fetch_finish,
1234 &spf);
1235
1236 argv_array_clear(&spf.args);
1237 out:
1238 string_list_clear(&changed_submodule_paths, 1);
1239 return spf.result;
1240 }
1241
1242 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1243 {
1244 struct child_process cp = CHILD_PROCESS_INIT;
1245 struct strbuf buf = STRBUF_INIT;
1246 FILE *fp;
1247 unsigned dirty_submodule = 0;
1248 const char *git_dir;
1249 int ignore_cp_exit_code = 0;
1250
1251 strbuf_addf(&buf, "%s/.git", path);
1252 git_dir = read_gitfile(buf.buf);
1253 if (!git_dir)
1254 git_dir = buf.buf;
1255 if (!is_git_directory(git_dir)) {
1256 if (is_directory(git_dir))
1257 die(_("'%s' not recognized as a git repository"), git_dir);
1258 strbuf_release(&buf);
1259 /* The submodule is not checked out, so it is not modified */
1260 return 0;
1261 }
1262 strbuf_reset(&buf);
1263
1264 argv_array_pushl(&cp.args, "status", "--porcelain=2", NULL);
1265 if (ignore_untracked)
1266 argv_array_push(&cp.args, "-uno");
1267
1268 prepare_submodule_repo_env(&cp.env_array);
1269 cp.git_cmd = 1;
1270 cp.no_stdin = 1;
1271 cp.out = -1;
1272 cp.dir = path;
1273 if (start_command(&cp))
1274 die("Could not run 'git status --porcelain=2' in submodule %s", path);
1275
1276 fp = xfdopen(cp.out, "r");
1277 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1278 /* regular untracked files */
1279 if (buf.buf[0] == '?')
1280 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1281
1282 if (buf.buf[0] == 'u' ||
1283 buf.buf[0] == '1' ||
1284 buf.buf[0] == '2') {
1285 /* T = line type, XY = status, SSSS = submodule state */
1286 if (buf.len < strlen("T XY SSSS"))
1287 die("BUG: invalid status --porcelain=2 line %s",
1288 buf.buf);
1289
1290 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1291 /* nested untracked file */
1292 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1293
1294 if (buf.buf[0] == 'u' ||
1295 buf.buf[0] == '2' ||
1296 memcmp(buf.buf + 5, "S..U", 4))
1297 /* other change */
1298 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1299 }
1300
1301 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1302 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1303 ignore_untracked)) {
1304 /*
1305 * We're not interested in any further information from
1306 * the child any more, neither output nor its exit code.
1307 */
1308 ignore_cp_exit_code = 1;
1309 break;
1310 }
1311 }
1312 fclose(fp);
1313
1314 if (finish_command(&cp) && !ignore_cp_exit_code)
1315 die("'git status --porcelain=2' failed in submodule %s", path);
1316
1317 strbuf_release(&buf);
1318 return dirty_submodule;
1319 }
1320
1321 int submodule_uses_gitfile(const char *path)
1322 {
1323 struct child_process cp = CHILD_PROCESS_INIT;
1324 const char *argv[] = {
1325 "submodule",
1326 "foreach",
1327 "--quiet",
1328 "--recursive",
1329 "test -f .git",
1330 NULL,
1331 };
1332 struct strbuf buf = STRBUF_INIT;
1333 const char *git_dir;
1334
1335 strbuf_addf(&buf, "%s/.git", path);
1336 git_dir = read_gitfile(buf.buf);
1337 if (!git_dir) {
1338 strbuf_release(&buf);
1339 return 0;
1340 }
1341 strbuf_release(&buf);
1342
1343 /* Now test that all nested submodules use a gitfile too */
1344 cp.argv = argv;
1345 prepare_submodule_repo_env(&cp.env_array);
1346 cp.git_cmd = 1;
1347 cp.no_stdin = 1;
1348 cp.no_stderr = 1;
1349 cp.no_stdout = 1;
1350 cp.dir = path;
1351 if (run_command(&cp))
1352 return 0;
1353
1354 return 1;
1355 }
1356
1357 /*
1358 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1359 * when doing so.
1360 *
1361 * Return 1 if we'd lose data, return 0 if the removal is fine,
1362 * and negative values for errors.
1363 */
1364 int bad_to_remove_submodule(const char *path, unsigned flags)
1365 {
1366 ssize_t len;
1367 struct child_process cp = CHILD_PROCESS_INIT;
1368 struct strbuf buf = STRBUF_INIT;
1369 int ret = 0;
1370
1371 if (!file_exists(path) || is_empty_dir(path))
1372 return 0;
1373
1374 if (!submodule_uses_gitfile(path))
1375 return 1;
1376
1377 argv_array_pushl(&cp.args, "status", "--porcelain",
1378 "--ignore-submodules=none", NULL);
1379
1380 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1381 argv_array_push(&cp.args, "-uno");
1382 else
1383 argv_array_push(&cp.args, "-uall");
1384
1385 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1386 argv_array_push(&cp.args, "--ignored");
1387
1388 prepare_submodule_repo_env(&cp.env_array);
1389 cp.git_cmd = 1;
1390 cp.no_stdin = 1;
1391 cp.out = -1;
1392 cp.dir = path;
1393 if (start_command(&cp)) {
1394 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1395 die(_("could not start 'git status' in submodule '%s'"),
1396 path);
1397 ret = -1;
1398 goto out;
1399 }
1400
1401 len = strbuf_read(&buf, cp.out, 1024);
1402 if (len > 2)
1403 ret = 1;
1404 close(cp.out);
1405
1406 if (finish_command(&cp)) {
1407 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1408 die(_("could not run 'git status' in submodule '%s'"),
1409 path);
1410 ret = -1;
1411 }
1412 out:
1413 strbuf_release(&buf);
1414 return ret;
1415 }
1416
1417 static const char *get_super_prefix_or_empty(void)
1418 {
1419 const char *s = get_super_prefix();
1420 if (!s)
1421 s = "";
1422 return s;
1423 }
1424
1425 static int submodule_has_dirty_index(const struct submodule *sub)
1426 {
1427 struct child_process cp = CHILD_PROCESS_INIT;
1428
1429 prepare_submodule_repo_env_no_git_dir(&cp.env_array);
1430
1431 cp.git_cmd = 1;
1432 argv_array_pushl(&cp.args, "diff-index", "--quiet",
1433 "--cached", "HEAD", NULL);
1434 cp.no_stdin = 1;
1435 cp.no_stdout = 1;
1436 cp.dir = sub->path;
1437 if (start_command(&cp))
1438 die("could not recurse into submodule '%s'", sub->path);
1439
1440 return finish_command(&cp);
1441 }
1442
1443 static void submodule_reset_index(const char *path)
1444 {
1445 struct child_process cp = CHILD_PROCESS_INIT;
1446 prepare_submodule_repo_env_no_git_dir(&cp.env_array);
1447
1448 cp.git_cmd = 1;
1449 cp.no_stdin = 1;
1450 cp.dir = path;
1451
1452 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1453 get_super_prefix_or_empty(), path);
1454 argv_array_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
1455
1456 argv_array_push(&cp.args, EMPTY_TREE_SHA1_HEX);
1457
1458 if (run_command(&cp))
1459 die("could not reset submodule index");
1460 }
1461
1462 /**
1463 * Moves a submodule at a given path from a given head to another new head.
1464 * For edge cases (a submodule coming into existence or removing a submodule)
1465 * pass NULL for old or new respectively.
1466 */
1467 int submodule_move_head(const char *path,
1468 const char *old,
1469 const char *new,
1470 unsigned flags)
1471 {
1472 int ret = 0;
1473 struct child_process cp = CHILD_PROCESS_INIT;
1474 const struct submodule *sub;
1475
1476 sub = submodule_from_path(null_sha1, path);
1477
1478 if (!sub)
1479 die("BUG: could not get submodule information for '%s'", path);
1480
1481 if (old && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1482 /* Check if the submodule has a dirty index. */
1483 if (submodule_has_dirty_index(sub))
1484 return error(_("submodule '%s' has dirty index"), path);
1485 }
1486
1487 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1488 if (old) {
1489 if (!submodule_uses_gitfile(path))
1490 absorb_git_dir_into_superproject("", path,
1491 ABSORB_GITDIR_RECURSE_SUBMODULES);
1492 } else {
1493 struct strbuf sb = STRBUF_INIT;
1494 strbuf_addf(&sb, "%s/modules/%s",
1495 get_git_common_dir(), sub->name);
1496 connect_work_tree_and_git_dir(path, sb.buf);
1497 strbuf_release(&sb);
1498
1499 /* make sure the index is clean as well */
1500 submodule_reset_index(path);
1501 }
1502 }
1503
1504 prepare_submodule_repo_env_no_git_dir(&cp.env_array);
1505
1506 cp.git_cmd = 1;
1507 cp.no_stdin = 1;
1508 cp.dir = path;
1509
1510 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1511 get_super_prefix_or_empty(), path);
1512 argv_array_pushl(&cp.args, "read-tree", NULL);
1513
1514 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
1515 argv_array_push(&cp.args, "-n");
1516 else
1517 argv_array_push(&cp.args, "-u");
1518
1519 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1520 argv_array_push(&cp.args, "--reset");
1521 else
1522 argv_array_push(&cp.args, "-m");
1523
1524 argv_array_push(&cp.args, old ? old : EMPTY_TREE_SHA1_HEX);
1525 argv_array_push(&cp.args, new ? new : EMPTY_TREE_SHA1_HEX);
1526
1527 if (run_command(&cp)) {
1528 ret = -1;
1529 goto out;
1530 }
1531
1532 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1533 if (new) {
1534 struct child_process cp1 = CHILD_PROCESS_INIT;
1535 /* also set the HEAD accordingly */
1536 cp1.git_cmd = 1;
1537 cp1.no_stdin = 1;
1538 cp1.dir = path;
1539
1540 argv_array_pushl(&cp1.args, "update-ref", "HEAD", new, NULL);
1541
1542 if (run_command(&cp1)) {
1543 ret = -1;
1544 goto out;
1545 }
1546 } else {
1547 struct strbuf sb = STRBUF_INIT;
1548
1549 strbuf_addf(&sb, "%s/.git", path);
1550 unlink_or_warn(sb.buf);
1551 strbuf_release(&sb);
1552
1553 if (is_empty_dir(path))
1554 rmdir_or_warn(path);
1555 }
1556 }
1557 out:
1558 return ret;
1559 }
1560
1561 static int find_first_merges(struct object_array *result, const char *path,
1562 struct commit *a, struct commit *b)
1563 {
1564 int i, j;
1565 struct object_array merges = OBJECT_ARRAY_INIT;
1566 struct commit *commit;
1567 int contains_another;
1568
1569 char merged_revision[42];
1570 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1571 "--all", merged_revision, NULL };
1572 struct rev_info revs;
1573 struct setup_revision_opt rev_opts;
1574
1575 memset(result, 0, sizeof(struct object_array));
1576 memset(&rev_opts, 0, sizeof(rev_opts));
1577
1578 /* get all revisions that merge commit a */
1579 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1580 oid_to_hex(&a->object.oid));
1581 init_revisions(&revs, NULL);
1582 rev_opts.submodule = path;
1583 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1584
1585 /* save all revisions from the above list that contain b */
1586 if (prepare_revision_walk(&revs))
1587 die("revision walk setup failed");
1588 while ((commit = get_revision(&revs)) != NULL) {
1589 struct object *o = &(commit->object);
1590 if (in_merge_bases(b, commit))
1591 add_object_array(o, NULL, &merges);
1592 }
1593 reset_revision_walk();
1594
1595 /* Now we've got all merges that contain a and b. Prune all
1596 * merges that contain another found merge and save them in
1597 * result.
1598 */
1599 for (i = 0; i < merges.nr; i++) {
1600 struct commit *m1 = (struct commit *) merges.objects[i].item;
1601
1602 contains_another = 0;
1603 for (j = 0; j < merges.nr; j++) {
1604 struct commit *m2 = (struct commit *) merges.objects[j].item;
1605 if (i != j && in_merge_bases(m2, m1)) {
1606 contains_another = 1;
1607 break;
1608 }
1609 }
1610
1611 if (!contains_another)
1612 add_object_array(merges.objects[i].item, NULL, result);
1613 }
1614
1615 free(merges.objects);
1616 return result->nr;
1617 }
1618
1619 static void print_commit(struct commit *commit)
1620 {
1621 struct strbuf sb = STRBUF_INIT;
1622 struct pretty_print_context ctx = {0};
1623 ctx.date_mode.type = DATE_NORMAL;
1624 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1625 fprintf(stderr, "%s\n", sb.buf);
1626 strbuf_release(&sb);
1627 }
1628
1629 #define MERGE_WARNING(path, msg) \
1630 warning("Failed to merge submodule %s (%s)", path, msg);
1631
1632 int merge_submodule(unsigned char result[20], const char *path,
1633 const unsigned char base[20], const unsigned char a[20],
1634 const unsigned char b[20], int search)
1635 {
1636 struct commit *commit_base, *commit_a, *commit_b;
1637 int parent_count;
1638 struct object_array merges;
1639
1640 int i;
1641
1642 /* store a in result in case we fail */
1643 hashcpy(result, a);
1644
1645 /* we can not handle deletion conflicts */
1646 if (is_null_sha1(base))
1647 return 0;
1648 if (is_null_sha1(a))
1649 return 0;
1650 if (is_null_sha1(b))
1651 return 0;
1652
1653 if (add_submodule_odb(path)) {
1654 MERGE_WARNING(path, "not checked out");
1655 return 0;
1656 }
1657
1658 if (!(commit_base = lookup_commit_reference(base)) ||
1659 !(commit_a = lookup_commit_reference(a)) ||
1660 !(commit_b = lookup_commit_reference(b))) {
1661 MERGE_WARNING(path, "commits not present");
1662 return 0;
1663 }
1664
1665 /* check whether both changes are forward */
1666 if (!in_merge_bases(commit_base, commit_a) ||
1667 !in_merge_bases(commit_base, commit_b)) {
1668 MERGE_WARNING(path, "commits don't follow merge-base");
1669 return 0;
1670 }
1671
1672 /* Case #1: a is contained in b or vice versa */
1673 if (in_merge_bases(commit_a, commit_b)) {
1674 hashcpy(result, b);
1675 return 1;
1676 }
1677 if (in_merge_bases(commit_b, commit_a)) {
1678 hashcpy(result, a);
1679 return 1;
1680 }
1681
1682 /*
1683 * Case #2: There are one or more merges that contain a and b in
1684 * the submodule. If there is only one, then present it as a
1685 * suggestion to the user, but leave it marked unmerged so the
1686 * user needs to confirm the resolution.
1687 */
1688
1689 /* Skip the search if makes no sense to the calling context. */
1690 if (!search)
1691 return 0;
1692
1693 /* find commit which merges them */
1694 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1695 switch (parent_count) {
1696 case 0:
1697 MERGE_WARNING(path, "merge following commits not found");
1698 break;
1699
1700 case 1:
1701 MERGE_WARNING(path, "not fast-forward");
1702 fprintf(stderr, "Found a possible merge resolution "
1703 "for the submodule:\n");
1704 print_commit((struct commit *) merges.objects[0].item);
1705 fprintf(stderr,
1706 "If this is correct simply add it to the index "
1707 "for example\n"
1708 "by using:\n\n"
1709 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1710 "which will accept this suggestion.\n",
1711 oid_to_hex(&merges.objects[0].item->oid), path);
1712 break;
1713
1714 default:
1715 MERGE_WARNING(path, "multiple merges found");
1716 for (i = 0; i < merges.nr; i++)
1717 print_commit((struct commit *) merges.objects[i].item);
1718 }
1719
1720 free(merges.objects);
1721 return 0;
1722 }
1723
1724 int parallel_submodules(void)
1725 {
1726 return parallel_jobs;
1727 }
1728
1729 /*
1730 * Embeds a single submodules git directory into the superprojects git dir,
1731 * non recursively.
1732 */
1733 static void relocate_single_git_dir_into_superproject(const char *prefix,
1734 const char *path)
1735 {
1736 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1737 const char *new_git_dir;
1738 const struct submodule *sub;
1739
1740 if (submodule_uses_worktrees(path))
1741 die(_("relocate_gitdir for submodule '%s' with "
1742 "more than one worktree not supported"), path);
1743
1744 old_git_dir = xstrfmt("%s/.git", path);
1745 if (read_gitfile(old_git_dir))
1746 /* If it is an actual gitfile, it doesn't need migration. */
1747 return;
1748
1749 real_old_git_dir = real_pathdup(old_git_dir, 1);
1750
1751 sub = submodule_from_path(null_sha1, path);
1752 if (!sub)
1753 die(_("could not lookup name for submodule '%s'"), path);
1754
1755 new_git_dir = git_path("modules/%s", sub->name);
1756 if (safe_create_leading_directories_const(new_git_dir) < 0)
1757 die(_("could not create directory '%s'"), new_git_dir);
1758 real_new_git_dir = real_pathdup(new_git_dir, 1);
1759
1760 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1761 get_super_prefix_or_empty(), path,
1762 real_old_git_dir, real_new_git_dir);
1763
1764 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
1765
1766 free(old_git_dir);
1767 free(real_old_git_dir);
1768 free(real_new_git_dir);
1769 }
1770
1771 /*
1772 * Migrate the git directory of the submodule given by path from
1773 * having its git directory within the working tree to the git dir nested
1774 * in its superprojects git dir under modules/.
1775 */
1776 void absorb_git_dir_into_superproject(const char *prefix,
1777 const char *path,
1778 unsigned flags)
1779 {
1780 int err_code;
1781 const char *sub_git_dir;
1782 struct strbuf gitdir = STRBUF_INIT;
1783 strbuf_addf(&gitdir, "%s/.git", path);
1784 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
1785
1786 /* Not populated? */
1787 if (!sub_git_dir) {
1788 const struct submodule *sub;
1789
1790 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
1791 /* unpopulated as expected */
1792 strbuf_release(&gitdir);
1793 return;
1794 }
1795
1796 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
1797 /* We don't know what broke here. */
1798 read_gitfile_error_die(err_code, path, NULL);
1799
1800 /*
1801 * Maybe populated, but no git directory was found?
1802 * This can happen if the superproject is a submodule
1803 * itself and was just absorbed. The absorption of the
1804 * superproject did not rewrite the git file links yet,
1805 * fix it now.
1806 */
1807 sub = submodule_from_path(null_sha1, path);
1808 if (!sub)
1809 die(_("could not lookup name for submodule '%s'"), path);
1810 connect_work_tree_and_git_dir(path,
1811 git_path("modules/%s", sub->name));
1812 } else {
1813 /* Is it already absorbed into the superprojects git dir? */
1814 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
1815 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
1816
1817 if (!starts_with(real_sub_git_dir, real_common_git_dir))
1818 relocate_single_git_dir_into_superproject(prefix, path);
1819
1820 free(real_sub_git_dir);
1821 free(real_common_git_dir);
1822 }
1823 strbuf_release(&gitdir);
1824
1825 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1826 struct child_process cp = CHILD_PROCESS_INIT;
1827 struct strbuf sb = STRBUF_INIT;
1828
1829 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
1830 die("BUG: we don't know how to pass the flags down?");
1831
1832 strbuf_addstr(&sb, get_super_prefix_or_empty());
1833 strbuf_addstr(&sb, path);
1834 strbuf_addch(&sb, '/');
1835
1836 cp.dir = path;
1837 cp.git_cmd = 1;
1838 cp.no_stdin = 1;
1839 argv_array_pushl(&cp.args, "--super-prefix", sb.buf,
1840 "submodule--helper",
1841 "absorb-git-dirs", NULL);
1842 prepare_submodule_repo_env(&cp.env_array);
1843 if (run_command(&cp))
1844 die(_("could not recurse into submodule '%s'"), path);
1845
1846 strbuf_release(&sb);
1847 }
1848 }
1849
1850 const char *get_superproject_working_tree(void)
1851 {
1852 struct child_process cp = CHILD_PROCESS_INIT;
1853 struct strbuf sb = STRBUF_INIT;
1854 const char *one_up = real_path_if_valid("../");
1855 const char *cwd = xgetcwd();
1856 const char *ret = NULL;
1857 const char *subpath;
1858 int code;
1859 ssize_t len;
1860
1861 if (!is_inside_work_tree())
1862 /*
1863 * FIXME:
1864 * We might have a superproject, but it is harder
1865 * to determine.
1866 */
1867 return NULL;
1868
1869 if (!one_up)
1870 return NULL;
1871
1872 subpath = relative_path(cwd, one_up, &sb);
1873
1874 prepare_submodule_repo_env(&cp.env_array);
1875 argv_array_pop(&cp.env_array);
1876
1877 argv_array_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
1878 "ls-files", "-z", "--stage", "--full-name", "--",
1879 subpath, NULL);
1880 strbuf_reset(&sb);
1881
1882 cp.no_stdin = 1;
1883 cp.no_stderr = 1;
1884 cp.out = -1;
1885 cp.git_cmd = 1;
1886
1887 if (start_command(&cp))
1888 die(_("could not start ls-files in .."));
1889
1890 len = strbuf_read(&sb, cp.out, PATH_MAX);
1891 close(cp.out);
1892
1893 if (starts_with(sb.buf, "160000")) {
1894 int super_sub_len;
1895 int cwd_len = strlen(cwd);
1896 char *super_sub, *super_wt;
1897
1898 /*
1899 * There is a superproject having this repo as a submodule.
1900 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
1901 * We're only interested in the name after the tab.
1902 */
1903 super_sub = strchr(sb.buf, '\t') + 1;
1904 super_sub_len = sb.buf + sb.len - super_sub - 1;
1905
1906 if (super_sub_len > cwd_len ||
1907 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
1908 die (_("BUG: returned path string doesn't match cwd?"));
1909
1910 super_wt = xstrdup(cwd);
1911 super_wt[cwd_len - super_sub_len] = '\0';
1912
1913 ret = real_path(super_wt);
1914 free(super_wt);
1915 }
1916 strbuf_release(&sb);
1917
1918 code = finish_command(&cp);
1919
1920 if (code == 128)
1921 /* '../' is not a git repository */
1922 return NULL;
1923 if (code == 0 && len == 0)
1924 /* There is an unrelated git repository at '../' */
1925 return NULL;
1926 if (code)
1927 die(_("ls-tree returned unexpected return code %d"), code);
1928
1929 return ret;
1930 }
1931
1932 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
1933 {
1934 const struct submodule *sub;
1935 const char *git_dir;
1936 int ret = 0;
1937
1938 strbuf_reset(buf);
1939 strbuf_addstr(buf, submodule);
1940 strbuf_complete(buf, '/');
1941 strbuf_addstr(buf, ".git");
1942
1943 git_dir = read_gitfile(buf->buf);
1944 if (git_dir) {
1945 strbuf_reset(buf);
1946 strbuf_addstr(buf, git_dir);
1947 }
1948 if (!is_git_directory(buf->buf)) {
1949 gitmodules_config();
1950 sub = submodule_from_path(null_sha1, submodule);
1951 if (!sub) {
1952 ret = -1;
1953 goto cleanup;
1954 }
1955 strbuf_reset(buf);
1956 strbuf_git_path(buf, "%s/%s", "modules", sub->name);
1957 }
1958
1959 cleanup:
1960 return ret;
1961 }