2 #include "submodule-config.h"
8 #include "run-command.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "argv-array.h"
15 #include "thread-utils.h"
18 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
19 static int parallel_jobs = 1;
20 static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP;
21 static int initialized_fetch_ref_tips;
22 static struct sha1_array ref_tips_before_fetch;
23 static struct sha1_array ref_tips_after_fetch;
26 * The following flag is set if the .gitmodules file is unmerged. We then
27 * disable recursion for all submodules where .git/config doesn't have a
28 * matching config entry because we can't guess what might be configured in
29 * .gitmodules unless the user resolves the conflict. When a command line
30 * option is given (which always overrides configuration) this flag will be
33 static int gitmodules_is_unmerged;
36 * This flag is set if the .gitmodules file had unstaged modifications on
37 * startup. This must be checked before allowing modifications to the
38 * .gitmodules file with the intention to stage them later, because when
39 * continuing we would stage the modifications the user didn't stage herself
40 * too. That might change in a future version when we learn to stage the
41 * changes we do ourselves without staging any previous modifications.
43 static int gitmodules_is_modified;
45 int is_staging_gitmodules_ok(void)
47 return !gitmodules_is_modified;
51 * Try to update the "path" entry in the "submodule.<name>" section of the
52 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
53 * with the correct path=<oldpath> setting was found and we could update it.
55 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
57 struct strbuf entry = STRBUF_INIT;
58 const struct submodule *submodule;
60 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
63 if (gitmodules_is_unmerged)
64 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
66 submodule = submodule_from_path(null_sha1, oldpath);
67 if (!submodule || !submodule->name) {
68 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
71 strbuf_addstr(&entry, "submodule.");
72 strbuf_addstr(&entry, submodule->name);
73 strbuf_addstr(&entry, ".path");
74 if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
75 /* Maybe the user already did that, don't error out here */
76 warning(_("Could not update .gitmodules entry %s"), entry.buf);
77 strbuf_release(&entry);
80 strbuf_release(&entry);
85 * Try to remove the "submodule.<name>" section from .gitmodules where the given
86 * path is configured. Return 0 only if a .gitmodules file was found, a section
87 * with the correct path=<path> setting was found and we could remove it.
89 int remove_path_from_gitmodules(const char *path)
91 struct strbuf sect = STRBUF_INIT;
92 const struct submodule *submodule;
94 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
97 if (gitmodules_is_unmerged)
98 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
100 submodule = submodule_from_path(null_sha1, path);
101 if (!submodule || !submodule->name) {
102 warning(_("Could not find section in .gitmodules where path=%s"), path);
105 strbuf_addstr(§, "submodule.");
106 strbuf_addstr(§, submodule->name);
107 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
108 /* Maybe the user already did that, don't error out here */
109 warning(_("Could not remove .gitmodules entry for %s"), path);
110 strbuf_release(§);
113 strbuf_release(§);
117 void stage_updated_gitmodules(void)
119 if (add_file_to_cache(".gitmodules", 0))
120 die(_("staging updated .gitmodules failed"));
123 static int add_submodule_odb(const char *path)
125 struct strbuf objects_directory = STRBUF_INIT;
126 struct alternate_object_database *alt_odb;
130 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
133 if (!is_directory(objects_directory.buf)) {
137 /* avoid adding it twice */
139 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
140 if (alt_odb->name - alt_odb->base == objects_directory.len &&
141 !strncmp(alt_odb->base, objects_directory.buf,
142 objects_directory.len))
145 alloc = st_add(objects_directory.len, 42); /* for "12/345..." sha1 */
146 alt_odb = xmalloc(st_add(sizeof(*alt_odb), alloc));
147 alt_odb->next = alt_odb_list;
148 xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
149 alt_odb->name = alt_odb->base + objects_directory.len;
150 alt_odb->name[2] = '/';
151 alt_odb->name[40] = '\0';
152 alt_odb->name[41] = '\0';
153 alt_odb_list = alt_odb;
155 /* add possible alternates from the submodule */
156 read_info_alternates(objects_directory.buf, 0);
158 strbuf_release(&objects_directory);
162 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
165 const struct submodule *submodule = submodule_from_path(null_sha1, path);
167 if (submodule->ignore)
168 handle_ignore_submodules_arg(diffopt, submodule->ignore);
169 else if (gitmodules_is_unmerged)
170 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
174 int submodule_config(const char *var, const char *value, void *cb)
176 if (!strcmp(var, "submodule.fetchjobs")) {
177 parallel_jobs = git_config_int(var, value);
178 if (parallel_jobs < 0)
179 die(_("negative values not allowed for submodule.fetchJobs"));
181 } else if (starts_with(var, "submodule."))
182 return parse_submodule_config_option(var, value);
183 else if (!strcmp(var, "fetch.recursesubmodules")) {
184 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
190 void gitmodules_config(void)
192 const char *work_tree = get_git_work_tree();
194 struct strbuf gitmodules_path = STRBUF_INIT;
196 strbuf_addstr(&gitmodules_path, work_tree);
197 strbuf_addstr(&gitmodules_path, "/.gitmodules");
198 if (read_cache() < 0)
199 die("index file corrupt");
200 pos = cache_name_pos(".gitmodules", 11);
201 if (pos < 0) { /* .gitmodules not found or isn't merged */
203 if (active_nr > pos) { /* there is a .gitmodules */
204 const struct cache_entry *ce = active_cache[pos];
205 if (ce_namelen(ce) == 11 &&
206 !memcmp(ce->name, ".gitmodules", 11))
207 gitmodules_is_unmerged = 1;
209 } else if (pos < active_nr) {
211 if (lstat(".gitmodules", &st) == 0 &&
212 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
213 gitmodules_is_modified = 1;
216 if (!gitmodules_is_unmerged)
217 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
218 strbuf_release(&gitmodules_path);
222 int parse_submodule_update_strategy(const char *value,
223 struct submodule_update_strategy *dst)
225 free((void*)dst->command);
227 if (!strcmp(value, "none"))
228 dst->type = SM_UPDATE_NONE;
229 else if (!strcmp(value, "checkout"))
230 dst->type = SM_UPDATE_CHECKOUT;
231 else if (!strcmp(value, "rebase"))
232 dst->type = SM_UPDATE_REBASE;
233 else if (!strcmp(value, "merge"))
234 dst->type = SM_UPDATE_MERGE;
235 else if (skip_prefix(value, "!", &value)) {
236 dst->type = SM_UPDATE_COMMAND;
237 dst->command = xstrdup(value);
243 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
245 struct strbuf sb = STRBUF_INIT;
247 case SM_UPDATE_CHECKOUT:
249 case SM_UPDATE_MERGE:
251 case SM_UPDATE_REBASE:
255 case SM_UPDATE_UNSPECIFIED:
257 case SM_UPDATE_COMMAND:
258 strbuf_addf(&sb, "!%s", s->command);
259 return strbuf_detach(&sb, NULL);
264 void handle_ignore_submodules_arg(struct diff_options *diffopt,
267 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
268 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
269 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
271 if (!strcmp(arg, "all"))
272 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
273 else if (!strcmp(arg, "untracked"))
274 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
275 else if (!strcmp(arg, "dirty"))
276 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
277 else if (strcmp(arg, "none"))
278 die("bad --ignore-submodules argument: %s", arg);
281 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
282 struct commit *left, struct commit *right,
283 struct commit_list *merge_bases)
285 struct commit_list *list;
287 init_revisions(rev, NULL);
288 setup_revisions(0, NULL, rev, NULL);
290 rev->first_parent_only = 1;
291 left->object.flags |= SYMMETRIC_LEFT;
292 add_pending_object(rev, &left->object, path);
293 add_pending_object(rev, &right->object, path);
294 for (list = merge_bases; list; list = list->next) {
295 list->item->object.flags |= UNINTERESTING;
296 add_pending_object(rev, &list->item->object,
297 oid_to_hex(&list->item->object.oid));
299 return prepare_revision_walk(rev);
302 static void print_submodule_summary(struct rev_info *rev, FILE *f,
303 const char *line_prefix,
304 const char *del, const char *add, const char *reset)
306 static const char format[] = " %m %s";
307 struct strbuf sb = STRBUF_INIT;
308 struct commit *commit;
310 while ((commit = get_revision(rev))) {
311 struct pretty_print_context ctx = {0};
312 ctx.date_mode = rev->date_mode;
313 ctx.output_encoding = get_log_output_encoding();
314 strbuf_setlen(&sb, 0);
315 strbuf_addstr(&sb, line_prefix);
316 if (commit->object.flags & SYMMETRIC_LEFT) {
318 strbuf_addstr(&sb, del);
321 strbuf_addstr(&sb, add);
322 format_commit_message(commit, format, &sb, &ctx);
324 strbuf_addstr(&sb, reset);
325 strbuf_addch(&sb, '\n');
326 fprintf(f, "%s", sb.buf);
331 /* Helper function to display the submodule header line prior to the full
332 * summary output. If it can locate the submodule objects directory it will
333 * attempt to lookup both the left and right commits and put them into the
334 * left and right pointers.
336 static void show_submodule_header(FILE *f, const char *path,
337 const char *line_prefix,
338 struct object_id *one, struct object_id *two,
339 unsigned dirty_submodule, const char *meta,
341 struct commit **left, struct commit **right,
342 struct commit_list **merge_bases)
344 const char *message = NULL;
345 struct strbuf sb = STRBUF_INIT;
346 int fast_forward = 0, fast_backward = 0;
348 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
349 fprintf(f, "%sSubmodule %s contains untracked content\n",
351 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
352 fprintf(f, "%sSubmodule %s contains modified content\n",
355 if (is_null_oid(one))
356 message = "(new submodule)";
357 else if (is_null_oid(two))
358 message = "(submodule deleted)";
360 if (add_submodule_odb(path)) {
362 message = "(not initialized)";
367 * Attempt to lookup the commit references, and determine if this is
368 * a fast forward or fast backwards update.
370 *left = lookup_commit_reference(one->hash);
371 *right = lookup_commit_reference(two->hash);
374 * Warn about missing commits in the submodule project, but only if
377 if ((!is_null_oid(one) && !*left) ||
378 (!is_null_oid(two) && !*right))
379 message = "(commits not present)";
381 *merge_bases = get_merge_bases(*left, *right);
383 if ((*merge_bases)->item == *left)
385 else if ((*merge_bases)->item == *right)
389 if (!oidcmp(one, two)) {
395 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
396 find_unique_abbrev(one->hash, DEFAULT_ABBREV));
397 if (!fast_backward && !fast_forward)
398 strbuf_addch(&sb, '.');
399 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
401 strbuf_addf(&sb, " %s%s\n", message, reset);
403 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
404 fwrite(sb.buf, sb.len, 1, f);
409 void show_submodule_summary(FILE *f, const char *path,
410 const char *line_prefix,
411 struct object_id *one, struct object_id *two,
412 unsigned dirty_submodule, const char *meta,
413 const char *del, const char *add, const char *reset)
416 struct commit *left = NULL, *right = NULL;
417 struct commit_list *merge_bases = NULL;
419 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
420 meta, reset, &left, &right, &merge_bases);
423 * If we don't have both a left and a right pointer, there is no
424 * reason to try and display a summary. The header line should contain
425 * all the information the user needs.
430 /* Treat revision walker failure the same as missing commits */
431 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
432 fprintf(f, "%s(revision walker failed)\n", line_prefix);
436 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
440 free_commit_list(merge_bases);
441 clear_commit_marks(left, ~0);
442 clear_commit_marks(right, ~0);
445 void show_submodule_inline_diff(FILE *f, const char *path,
446 const char *line_prefix,
447 struct object_id *one, struct object_id *two,
448 unsigned dirty_submodule, const char *meta,
449 const char *del, const char *add, const char *reset,
450 const struct diff_options *o)
452 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
453 struct commit *left = NULL, *right = NULL;
454 struct commit_list *merge_bases = NULL;
455 struct strbuf submodule_dir = STRBUF_INIT;
456 struct child_process cp = CHILD_PROCESS_INIT;
458 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
459 meta, reset, &left, &right, &merge_bases);
461 /* We need a valid left and right commit to display a difference */
462 if (!(left || is_null_oid(one)) ||
463 !(right || is_null_oid(two)))
474 cp.out = dup(fileno(f));
477 /* TODO: other options may need to be passed here. */
478 argv_array_push(&cp.args, "diff");
479 argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
480 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
481 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
483 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
486 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
488 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
491 argv_array_push(&cp.args, oid_to_hex(old));
493 * If the submodule has modified content, we will diff against the
494 * work tree, under the assumption that the user has asked for the
495 * diff format and wishes to actually see all differences even if they
496 * haven't yet been committed to the submodule yet.
498 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
499 argv_array_push(&cp.args, oid_to_hex(new));
501 if (run_command(&cp))
502 fprintf(f, "(diff failed)\n");
505 strbuf_release(&submodule_dir);
507 free_commit_list(merge_bases);
509 clear_commit_marks(left, ~0);
511 clear_commit_marks(right, ~0);
514 void set_config_fetch_recurse_submodules(int value)
516 config_fetch_recurse_submodules = value;
519 static int has_remote(const char *refname, const struct object_id *oid,
520 int flags, void *cb_data)
525 static int append_sha1_to_argv(const unsigned char sha1[20], void *data)
527 struct argv_array *argv = data;
528 argv_array_push(argv, sha1_to_hex(sha1));
532 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
534 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
537 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
538 struct child_process cp = CHILD_PROCESS_INIT;
539 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
540 struct strbuf buf = STRBUF_INIT;
541 int needs_pushing = 0;
543 argv[1] = sha1_to_hex(sha1);
545 prepare_submodule_repo_env(&cp.env_array);
550 if (start_command(&cp))
551 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
552 sha1_to_hex(sha1), path);
553 if (strbuf_read(&buf, cp.out, 41))
557 strbuf_release(&buf);
558 return needs_pushing;
564 static struct sha1_array *submodule_commits(struct string_list *submodules,
567 struct string_list_item *item;
569 item = string_list_insert(submodules, path);
571 return (struct sha1_array *) item->util;
573 /* NEEDSWORK: should we have sha1_array_init()? */
574 item->util = xcalloc(1, sizeof(struct sha1_array));
575 return (struct sha1_array *) item->util;
578 static void collect_submodules_from_diff(struct diff_queue_struct *q,
579 struct diff_options *options,
583 struct string_list *submodules = data;
585 for (i = 0; i < q->nr; i++) {
586 struct diff_filepair *p = q->queue[i];
587 struct sha1_array *commits;
588 if (!S_ISGITLINK(p->two->mode))
590 commits = submodule_commits(submodules, p->two->path);
591 sha1_array_append(commits, p->two->oid.hash);
595 static void find_unpushed_submodule_commits(struct commit *commit,
596 struct string_list *needs_pushing)
600 init_revisions(&rev, NULL);
601 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
602 rev.diffopt.format_callback = collect_submodules_from_diff;
603 rev.diffopt.format_callback_data = needs_pushing;
604 diff_tree_combined_merge(commit, 1, &rev);
607 struct collect_submodule_from_sha1s_data {
608 char *submodule_path;
609 struct string_list *needs_pushing;
612 static int collect_submodules_from_sha1s(const unsigned char sha1[20],
615 struct collect_submodule_from_sha1s_data *me = data;
617 if (submodule_needs_pushing(me->submodule_path, sha1))
618 string_list_insert(me->needs_pushing, me->submodule_path);
623 static void free_submodules_sha1s(struct string_list *submodules)
625 struct string_list_item *item;
626 for_each_string_list_item(item, submodules)
627 sha1_array_clear((struct sha1_array *) item->util);
628 string_list_clear(submodules, 1);
631 int find_unpushed_submodules(struct sha1_array *commits,
632 const char *remotes_name, struct string_list *needs_pushing)
635 struct commit *commit;
636 struct string_list submodules = STRING_LIST_INIT_DUP;
637 struct string_list_item *submodule;
638 struct argv_array argv = ARGV_ARRAY_INIT;
640 init_revisions(&rev, NULL);
642 /* argv.argv[0] will be ignored by setup_revisions */
643 argv_array_push(&argv, "find_unpushed_submodules");
644 sha1_array_for_each_unique(commits, append_sha1_to_argv, &argv);
645 argv_array_push(&argv, "--not");
646 argv_array_pushf(&argv, "--remotes=%s", remotes_name);
648 setup_revisions(argv.argc, argv.argv, &rev, NULL);
649 if (prepare_revision_walk(&rev))
650 die("revision walk setup failed");
652 while ((commit = get_revision(&rev)) != NULL)
653 find_unpushed_submodule_commits(commit, &submodules);
655 reset_revision_walk();
656 argv_array_clear(&argv);
658 for_each_string_list_item(submodule, &submodules) {
659 struct collect_submodule_from_sha1s_data data;
660 data.submodule_path = submodule->string;
661 data.needs_pushing = needs_pushing;
662 sha1_array_for_each_unique((struct sha1_array *) submodule->util,
663 collect_submodules_from_sha1s,
666 free_submodules_sha1s(&submodules);
668 return needs_pushing->nr;
671 static int push_submodule(const char *path)
673 if (add_submodule_odb(path))
676 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
677 struct child_process cp = CHILD_PROCESS_INIT;
678 const char *argv[] = {"push", NULL};
681 prepare_submodule_repo_env(&cp.env_array);
685 if (run_command(&cp))
693 int push_unpushed_submodules(struct sha1_array *commits, const char *remotes_name)
696 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
698 if (!find_unpushed_submodules(commits, remotes_name, &needs_pushing))
701 for (i = 0; i < needs_pushing.nr; i++) {
702 const char *path = needs_pushing.items[i].string;
703 fprintf(stderr, "Pushing submodule '%s'\n", path);
704 if (!push_submodule(path)) {
705 fprintf(stderr, "Unable to push submodule '%s'\n", path);
710 string_list_clear(&needs_pushing, 0);
715 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
718 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
719 /* Even if the submodule is checked out and the commit is
720 * present, make sure it is reachable from a ref. */
721 struct child_process cp = CHILD_PROCESS_INIT;
722 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
723 struct strbuf buf = STRBUF_INIT;
725 argv[3] = sha1_to_hex(sha1);
727 prepare_submodule_repo_env(&cp.env_array);
731 if (!capture_command(&cp, &buf, 1024) && !buf.len)
734 strbuf_release(&buf);
739 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
740 struct diff_options *options,
744 for (i = 0; i < q->nr; i++) {
745 struct diff_filepair *p = q->queue[i];
746 if (!S_ISGITLINK(p->two->mode))
749 if (S_ISGITLINK(p->one->mode)) {
750 /* NEEDSWORK: We should honor the name configured in
751 * the .gitmodules file of the commit we are examining
752 * here to be able to correctly follow submodules
753 * being moved around. */
754 struct string_list_item *path;
755 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
756 if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash))
757 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
759 /* Submodule is new or was moved here */
760 /* NEEDSWORK: When the .git directories of submodules
761 * live inside the superprojects .git directory some
762 * day we should fetch new submodules directly into
763 * that location too when config or options request
764 * that so they can be checked out from there. */
770 static int add_sha1_to_array(const char *ref, const struct object_id *oid,
771 int flags, void *data)
773 sha1_array_append(data, oid->hash);
777 void check_for_new_submodule_commits(unsigned char new_sha1[20])
779 if (!initialized_fetch_ref_tips) {
780 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
781 initialized_fetch_ref_tips = 1;
784 sha1_array_append(&ref_tips_after_fetch, new_sha1);
787 static int add_sha1_to_argv(const unsigned char sha1[20], void *data)
789 argv_array_push(data, sha1_to_hex(sha1));
793 static void calculate_changed_submodule_paths(void)
796 struct commit *commit;
797 struct argv_array argv = ARGV_ARRAY_INIT;
799 /* No need to check if there are no submodules configured */
800 if (!submodule_from_path(NULL, NULL))
803 init_revisions(&rev, NULL);
804 argv_array_push(&argv, "--"); /* argv[0] program name */
805 sha1_array_for_each_unique(&ref_tips_after_fetch,
806 add_sha1_to_argv, &argv);
807 argv_array_push(&argv, "--not");
808 sha1_array_for_each_unique(&ref_tips_before_fetch,
809 add_sha1_to_argv, &argv);
810 setup_revisions(argv.argc, argv.argv, &rev, NULL);
811 if (prepare_revision_walk(&rev))
812 die("revision walk setup failed");
815 * Collect all submodules (whether checked out or not) for which new
816 * commits have been recorded upstream in "changed_submodule_paths".
818 while ((commit = get_revision(&rev))) {
819 struct commit_list *parent = commit->parents;
821 struct diff_options diff_opts;
822 diff_setup(&diff_opts);
823 DIFF_OPT_SET(&diff_opts, RECURSIVE);
824 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
825 diff_opts.format_callback = submodule_collect_changed_cb;
826 diff_setup_done(&diff_opts);
827 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
828 diffcore_std(&diff_opts);
829 diff_flush(&diff_opts);
830 parent = parent->next;
834 argv_array_clear(&argv);
835 sha1_array_clear(&ref_tips_before_fetch);
836 sha1_array_clear(&ref_tips_after_fetch);
837 initialized_fetch_ref_tips = 0;
840 struct submodule_parallel_fetch {
842 struct argv_array args;
843 const char *work_tree;
845 int command_line_option;
849 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
851 static int get_next_submodule(struct child_process *cp,
852 struct strbuf *err, void *data, void **task_cb)
855 struct submodule_parallel_fetch *spf = data;
857 for (; spf->count < active_nr; spf->count++) {
858 struct strbuf submodule_path = STRBUF_INIT;
859 struct strbuf submodule_git_dir = STRBUF_INIT;
860 struct strbuf submodule_prefix = STRBUF_INIT;
861 const struct cache_entry *ce = active_cache[spf->count];
862 const char *git_dir, *default_argv;
863 const struct submodule *submodule;
865 if (!S_ISGITLINK(ce->ce_mode))
868 submodule = submodule_from_path(null_sha1, ce->name);
870 submodule = submodule_from_name(null_sha1, ce->name);
872 default_argv = "yes";
873 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
875 submodule->fetch_recurse !=
876 RECURSE_SUBMODULES_NONE) {
877 if (submodule->fetch_recurse ==
878 RECURSE_SUBMODULES_OFF)
880 if (submodule->fetch_recurse ==
881 RECURSE_SUBMODULES_ON_DEMAND) {
882 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
884 default_argv = "on-demand";
887 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
888 gitmodules_is_unmerged)
890 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
891 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
893 default_argv = "on-demand";
896 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
897 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
899 default_argv = "on-demand";
902 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
903 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
904 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
905 git_dir = read_gitfile(submodule_git_dir.buf);
907 git_dir = submodule_git_dir.buf;
908 if (is_directory(git_dir)) {
909 child_process_init(cp);
910 cp->dir = strbuf_detach(&submodule_path, NULL);
911 prepare_submodule_repo_env(&cp->env_array);
914 strbuf_addf(err, "Fetching submodule %s%s\n",
915 spf->prefix, ce->name);
916 argv_array_init(&cp->args);
917 argv_array_pushv(&cp->args, spf->args.argv);
918 argv_array_push(&cp->args, default_argv);
919 argv_array_push(&cp->args, "--submodule-prefix");
920 argv_array_push(&cp->args, submodule_prefix.buf);
923 strbuf_release(&submodule_path);
924 strbuf_release(&submodule_git_dir);
925 strbuf_release(&submodule_prefix);
934 static int fetch_start_failure(struct strbuf *err,
935 void *cb, void *task_cb)
937 struct submodule_parallel_fetch *spf = cb;
944 static int fetch_finish(int retvalue, struct strbuf *err,
945 void *cb, void *task_cb)
947 struct submodule_parallel_fetch *spf = cb;
955 int fetch_populated_submodules(const struct argv_array *options,
956 const char *prefix, int command_line_option,
957 int quiet, int max_parallel_jobs)
960 struct submodule_parallel_fetch spf = SPF_INIT;
962 spf.work_tree = get_git_work_tree();
963 spf.command_line_option = command_line_option;
970 if (read_cache() < 0)
971 die("index file corrupt");
973 argv_array_push(&spf.args, "fetch");
974 for (i = 0; i < options->argc; i++)
975 argv_array_push(&spf.args, options->argv[i]);
976 argv_array_push(&spf.args, "--recurse-submodules-default");
977 /* default value, "--submodule-prefix" and its value are added later */
979 if (max_parallel_jobs < 0)
980 max_parallel_jobs = parallel_jobs;
982 calculate_changed_submodule_paths();
983 run_processes_parallel(max_parallel_jobs,
989 argv_array_clear(&spf.args);
991 string_list_clear(&changed_submodule_paths, 1);
995 unsigned is_submodule_modified(const char *path, int ignore_untracked)
998 struct child_process cp = CHILD_PROCESS_INIT;
999 const char *argv[] = {
1005 struct strbuf buf = STRBUF_INIT;
1006 unsigned dirty_submodule = 0;
1007 const char *line, *next_line;
1008 const char *git_dir;
1010 strbuf_addf(&buf, "%s/.git", path);
1011 git_dir = read_gitfile(buf.buf);
1014 if (!is_directory(git_dir)) {
1015 strbuf_release(&buf);
1016 /* The submodule is not checked out, so it is not modified */
1022 if (ignore_untracked)
1026 prepare_submodule_repo_env(&cp.env_array);
1031 if (start_command(&cp))
1032 die("Could not run 'git status --porcelain' in submodule %s", path);
1034 len = strbuf_read(&buf, cp.out, 1024);
1037 if ((line[0] == '?') && (line[1] == '?')) {
1038 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1039 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
1042 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1043 if (ignore_untracked ||
1044 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
1047 next_line = strchr(line, '\n');
1051 len -= (next_line - line);
1056 if (finish_command(&cp))
1057 die("'git status --porcelain' failed in submodule %s", path);
1059 strbuf_release(&buf);
1060 return dirty_submodule;
1063 int submodule_uses_gitfile(const char *path)
1065 struct child_process cp = CHILD_PROCESS_INIT;
1066 const char *argv[] = {
1074 struct strbuf buf = STRBUF_INIT;
1075 const char *git_dir;
1077 strbuf_addf(&buf, "%s/.git", path);
1078 git_dir = read_gitfile(buf.buf);
1080 strbuf_release(&buf);
1083 strbuf_release(&buf);
1085 /* Now test that all nested submodules use a gitfile too */
1087 prepare_submodule_repo_env(&cp.env_array);
1093 if (run_command(&cp))
1099 int ok_to_remove_submodule(const char *path)
1102 struct child_process cp = CHILD_PROCESS_INIT;
1103 const char *argv[] = {
1107 "--ignore-submodules=none",
1110 struct strbuf buf = STRBUF_INIT;
1111 int ok_to_remove = 1;
1113 if (!file_exists(path) || is_empty_dir(path))
1116 if (!submodule_uses_gitfile(path))
1120 prepare_submodule_repo_env(&cp.env_array);
1125 if (start_command(&cp))
1126 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
1128 len = strbuf_read(&buf, cp.out, 1024);
1133 if (finish_command(&cp))
1134 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
1136 strbuf_release(&buf);
1137 return ok_to_remove;
1140 static int find_first_merges(struct object_array *result, const char *path,
1141 struct commit *a, struct commit *b)
1144 struct object_array merges = OBJECT_ARRAY_INIT;
1145 struct commit *commit;
1146 int contains_another;
1148 char merged_revision[42];
1149 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1150 "--all", merged_revision, NULL };
1151 struct rev_info revs;
1152 struct setup_revision_opt rev_opts;
1154 memset(result, 0, sizeof(struct object_array));
1155 memset(&rev_opts, 0, sizeof(rev_opts));
1157 /* get all revisions that merge commit a */
1158 snprintf(merged_revision, sizeof(merged_revision), "^%s",
1159 oid_to_hex(&a->object.oid));
1160 init_revisions(&revs, NULL);
1161 rev_opts.submodule = path;
1162 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1164 /* save all revisions from the above list that contain b */
1165 if (prepare_revision_walk(&revs))
1166 die("revision walk setup failed");
1167 while ((commit = get_revision(&revs)) != NULL) {
1168 struct object *o = &(commit->object);
1169 if (in_merge_bases(b, commit))
1170 add_object_array(o, NULL, &merges);
1172 reset_revision_walk();
1174 /* Now we've got all merges that contain a and b. Prune all
1175 * merges that contain another found merge and save them in
1178 for (i = 0; i < merges.nr; i++) {
1179 struct commit *m1 = (struct commit *) merges.objects[i].item;
1181 contains_another = 0;
1182 for (j = 0; j < merges.nr; j++) {
1183 struct commit *m2 = (struct commit *) merges.objects[j].item;
1184 if (i != j && in_merge_bases(m2, m1)) {
1185 contains_another = 1;
1190 if (!contains_another)
1191 add_object_array(merges.objects[i].item, NULL, result);
1194 free(merges.objects);
1198 static void print_commit(struct commit *commit)
1200 struct strbuf sb = STRBUF_INIT;
1201 struct pretty_print_context ctx = {0};
1202 ctx.date_mode.type = DATE_NORMAL;
1203 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1204 fprintf(stderr, "%s\n", sb.buf);
1205 strbuf_release(&sb);
1208 #define MERGE_WARNING(path, msg) \
1209 warning("Failed to merge submodule %s (%s)", path, msg);
1211 int merge_submodule(unsigned char result[20], const char *path,
1212 const unsigned char base[20], const unsigned char a[20],
1213 const unsigned char b[20], int search)
1215 struct commit *commit_base, *commit_a, *commit_b;
1217 struct object_array merges;
1221 /* store a in result in case we fail */
1224 /* we can not handle deletion conflicts */
1225 if (is_null_sha1(base))
1227 if (is_null_sha1(a))
1229 if (is_null_sha1(b))
1232 if (add_submodule_odb(path)) {
1233 MERGE_WARNING(path, "not checked out");
1237 if (!(commit_base = lookup_commit_reference(base)) ||
1238 !(commit_a = lookup_commit_reference(a)) ||
1239 !(commit_b = lookup_commit_reference(b))) {
1240 MERGE_WARNING(path, "commits not present");
1244 /* check whether both changes are forward */
1245 if (!in_merge_bases(commit_base, commit_a) ||
1246 !in_merge_bases(commit_base, commit_b)) {
1247 MERGE_WARNING(path, "commits don't follow merge-base");
1251 /* Case #1: a is contained in b or vice versa */
1252 if (in_merge_bases(commit_a, commit_b)) {
1256 if (in_merge_bases(commit_b, commit_a)) {
1262 * Case #2: There are one or more merges that contain a and b in
1263 * the submodule. If there is only one, then present it as a
1264 * suggestion to the user, but leave it marked unmerged so the
1265 * user needs to confirm the resolution.
1268 /* Skip the search if makes no sense to the calling context. */
1272 /* find commit which merges them */
1273 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1274 switch (parent_count) {
1276 MERGE_WARNING(path, "merge following commits not found");
1280 MERGE_WARNING(path, "not fast-forward");
1281 fprintf(stderr, "Found a possible merge resolution "
1282 "for the submodule:\n");
1283 print_commit((struct commit *) merges.objects[0].item);
1285 "If this is correct simply add it to the index "
1288 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1289 "which will accept this suggestion.\n",
1290 oid_to_hex(&merges.objects[0].item->oid), path);
1294 MERGE_WARNING(path, "multiple merges found");
1295 for (i = 0; i < merges.nr; i++)
1296 print_commit((struct commit *) merges.objects[i].item);
1299 free(merges.objects);
1303 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1304 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1306 struct strbuf file_name = STRBUF_INIT;
1307 struct strbuf rel_path = STRBUF_INIT;
1308 const char *real_work_tree = xstrdup(real_path(work_tree));
1310 /* Update gitfile */
1311 strbuf_addf(&file_name, "%s/.git", work_tree);
1312 write_file(file_name.buf, "gitdir: %s",
1313 relative_path(git_dir, real_work_tree, &rel_path));
1315 /* Update core.worktree setting */
1316 strbuf_reset(&file_name);
1317 strbuf_addf(&file_name, "%s/config", git_dir);
1318 git_config_set_in_file(file_name.buf, "core.worktree",
1319 relative_path(real_work_tree, git_dir,
1322 strbuf_release(&file_name);
1323 strbuf_release(&rel_path);
1324 free((void *)real_work_tree);
1327 int parallel_submodules(void)
1329 return parallel_jobs;
1332 void prepare_submodule_repo_env(struct argv_array *out)
1334 const char * const *var;
1336 for (var = local_repo_env; *var; var++) {
1337 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
1338 argv_array_push(out, *var);
1340 argv_array_push(out, "GIT_DIR=.git");