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