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"
19 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
20 static int parallel_jobs = 1;
21 static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP;
22 static int initialized_fetch_ref_tips;
23 static struct sha1_array ref_tips_before_fetch;
24 static struct sha1_array ref_tips_after_fetch;
27 * The following flag is set if the .gitmodules file is unmerged. We then
28 * disable recursion for all submodules where .git/config doesn't have a
29 * matching config entry because we can't guess what might be configured in
30 * .gitmodules unless the user resolves the conflict. When a command line
31 * option is given (which always overrides configuration) this flag will be
34 static int gitmodules_is_unmerged;
37 * This flag is set if the .gitmodules file had unstaged modifications on
38 * startup. This must be checked before allowing modifications to the
39 * .gitmodules file with the intention to stage them later, because when
40 * continuing we would stage the modifications the user didn't stage herself
41 * too. That might change in a future version when we learn to stage the
42 * changes we do ourselves without staging any previous modifications.
44 static int gitmodules_is_modified;
46 int is_staging_gitmodules_ok(void)
48 return !gitmodules_is_modified;
52 * Try to update the "path" entry in the "submodule.<name>" section of the
53 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
54 * with the correct path=<oldpath> setting was found and we could update it.
56 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
58 struct strbuf entry = STRBUF_INIT;
59 const struct submodule *submodule;
61 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
64 if (gitmodules_is_unmerged)
65 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
67 submodule = submodule_from_path(null_sha1, oldpath);
68 if (!submodule || !submodule->name) {
69 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
72 strbuf_addstr(&entry, "submodule.");
73 strbuf_addstr(&entry, submodule->name);
74 strbuf_addstr(&entry, ".path");
75 if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) {
76 /* Maybe the user already did that, don't error out here */
77 warning(_("Could not update .gitmodules entry %s"), entry.buf);
78 strbuf_release(&entry);
81 strbuf_release(&entry);
86 * Try to remove the "submodule.<name>" section from .gitmodules where the given
87 * path is configured. Return 0 only if a .gitmodules file was found, a section
88 * with the correct path=<path> setting was found and we could remove it.
90 int remove_path_from_gitmodules(const char *path)
92 struct strbuf sect = STRBUF_INIT;
93 const struct submodule *submodule;
95 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
98 if (gitmodules_is_unmerged)
99 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
101 submodule = submodule_from_path(null_sha1, path);
102 if (!submodule || !submodule->name) {
103 warning(_("Could not find section in .gitmodules where path=%s"), path);
106 strbuf_addstr(§, "submodule.");
107 strbuf_addstr(§, submodule->name);
108 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
109 /* Maybe the user already did that, don't error out here */
110 warning(_("Could not remove .gitmodules entry for %s"), path);
111 strbuf_release(§);
114 strbuf_release(§);
118 void stage_updated_gitmodules(void)
120 if (add_file_to_cache(".gitmodules", 0))
121 die(_("staging updated .gitmodules failed"));
124 static int add_submodule_odb(const char *path)
126 struct strbuf objects_directory = STRBUF_INIT;
129 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
132 if (!is_directory(objects_directory.buf)) {
136 add_to_alternates_memory(objects_directory.buf);
138 strbuf_release(&objects_directory);
142 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
145 const struct submodule *submodule = submodule_from_path(null_sha1, path);
147 if (submodule->ignore)
148 handle_ignore_submodules_arg(diffopt, submodule->ignore);
149 else if (gitmodules_is_unmerged)
150 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
154 int submodule_config(const char *var, const char *value, void *cb)
156 if (!strcmp(var, "submodule.fetchjobs")) {
157 parallel_jobs = git_config_int(var, value);
158 if (parallel_jobs < 0)
159 die(_("negative values not allowed for submodule.fetchJobs"));
161 } else if (starts_with(var, "submodule."))
162 return parse_submodule_config_option(var, value);
163 else if (!strcmp(var, "fetch.recursesubmodules")) {
164 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
170 void gitmodules_config(void)
172 const char *work_tree = get_git_work_tree();
174 struct strbuf gitmodules_path = STRBUF_INIT;
176 strbuf_addstr(&gitmodules_path, work_tree);
177 strbuf_addstr(&gitmodules_path, "/.gitmodules");
178 if (read_cache() < 0)
179 die("index file corrupt");
180 pos = cache_name_pos(".gitmodules", 11);
181 if (pos < 0) { /* .gitmodules not found or isn't merged */
183 if (active_nr > pos) { /* there is a .gitmodules */
184 const struct cache_entry *ce = active_cache[pos];
185 if (ce_namelen(ce) == 11 &&
186 !memcmp(ce->name, ".gitmodules", 11))
187 gitmodules_is_unmerged = 1;
189 } else if (pos < active_nr) {
191 if (lstat(".gitmodules", &st) == 0 &&
192 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
193 gitmodules_is_modified = 1;
196 if (!gitmodules_is_unmerged)
197 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
198 strbuf_release(&gitmodules_path);
202 int parse_submodule_update_strategy(const char *value,
203 struct submodule_update_strategy *dst)
205 free((void*)dst->command);
207 if (!strcmp(value, "none"))
208 dst->type = SM_UPDATE_NONE;
209 else if (!strcmp(value, "checkout"))
210 dst->type = SM_UPDATE_CHECKOUT;
211 else if (!strcmp(value, "rebase"))
212 dst->type = SM_UPDATE_REBASE;
213 else if (!strcmp(value, "merge"))
214 dst->type = SM_UPDATE_MERGE;
215 else if (skip_prefix(value, "!", &value)) {
216 dst->type = SM_UPDATE_COMMAND;
217 dst->command = xstrdup(value);
223 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
225 struct strbuf sb = STRBUF_INIT;
227 case SM_UPDATE_CHECKOUT:
229 case SM_UPDATE_MERGE:
231 case SM_UPDATE_REBASE:
235 case SM_UPDATE_UNSPECIFIED:
237 case SM_UPDATE_COMMAND:
238 strbuf_addf(&sb, "!%s", s->command);
239 return strbuf_detach(&sb, NULL);
244 void handle_ignore_submodules_arg(struct diff_options *diffopt,
247 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
248 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
249 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
251 if (!strcmp(arg, "all"))
252 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
253 else if (!strcmp(arg, "untracked"))
254 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
255 else if (!strcmp(arg, "dirty"))
256 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
257 else if (strcmp(arg, "none"))
258 die("bad --ignore-submodules argument: %s", arg);
261 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
262 struct commit *left, struct commit *right,
263 struct commit_list *merge_bases)
265 struct commit_list *list;
267 init_revisions(rev, NULL);
268 setup_revisions(0, NULL, rev, NULL);
270 rev->first_parent_only = 1;
271 left->object.flags |= SYMMETRIC_LEFT;
272 add_pending_object(rev, &left->object, path);
273 add_pending_object(rev, &right->object, path);
274 for (list = merge_bases; list; list = list->next) {
275 list->item->object.flags |= UNINTERESTING;
276 add_pending_object(rev, &list->item->object,
277 oid_to_hex(&list->item->object.oid));
279 return prepare_revision_walk(rev);
282 static void print_submodule_summary(struct rev_info *rev, FILE *f,
283 const char *line_prefix,
284 const char *del, const char *add, const char *reset)
286 static const char format[] = " %m %s";
287 struct strbuf sb = STRBUF_INIT;
288 struct commit *commit;
290 while ((commit = get_revision(rev))) {
291 struct pretty_print_context ctx = {0};
292 ctx.date_mode = rev->date_mode;
293 ctx.output_encoding = get_log_output_encoding();
294 strbuf_setlen(&sb, 0);
295 strbuf_addstr(&sb, line_prefix);
296 if (commit->object.flags & SYMMETRIC_LEFT) {
298 strbuf_addstr(&sb, del);
301 strbuf_addstr(&sb, add);
302 format_commit_message(commit, format, &sb, &ctx);
304 strbuf_addstr(&sb, reset);
305 strbuf_addch(&sb, '\n');
306 fprintf(f, "%s", sb.buf);
311 /* Helper function to display the submodule header line prior to the full
312 * summary output. If it can locate the submodule objects directory it will
313 * attempt to lookup both the left and right commits and put them into the
314 * left and right pointers.
316 static void show_submodule_header(FILE *f, const char *path,
317 const char *line_prefix,
318 struct object_id *one, struct object_id *two,
319 unsigned dirty_submodule, const char *meta,
321 struct commit **left, struct commit **right,
322 struct commit_list **merge_bases)
324 const char *message = NULL;
325 struct strbuf sb = STRBUF_INIT;
326 int fast_forward = 0, fast_backward = 0;
328 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
329 fprintf(f, "%sSubmodule %s contains untracked content\n",
331 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
332 fprintf(f, "%sSubmodule %s contains modified content\n",
335 if (is_null_oid(one))
336 message = "(new submodule)";
337 else if (is_null_oid(two))
338 message = "(submodule deleted)";
340 if (add_submodule_odb(path)) {
342 message = "(not initialized)";
347 * Attempt to lookup the commit references, and determine if this is
348 * a fast forward or fast backwards update.
350 *left = lookup_commit_reference(one->hash);
351 *right = lookup_commit_reference(two->hash);
354 * Warn about missing commits in the submodule project, but only if
357 if ((!is_null_oid(one) && !*left) ||
358 (!is_null_oid(two) && !*right))
359 message = "(commits not present)";
361 *merge_bases = get_merge_bases(*left, *right);
363 if ((*merge_bases)->item == *left)
365 else if ((*merge_bases)->item == *right)
369 if (!oidcmp(one, two)) {
375 strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path);
376 strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
377 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
378 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
380 strbuf_addf(&sb, " %s%s\n", message, reset);
382 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
383 fwrite(sb.buf, sb.len, 1, f);
388 void show_submodule_summary(FILE *f, const char *path,
389 const char *line_prefix,
390 struct object_id *one, struct object_id *two,
391 unsigned dirty_submodule, const char *meta,
392 const char *del, const char *add, const char *reset)
395 struct commit *left = NULL, *right = NULL;
396 struct commit_list *merge_bases = NULL;
398 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
399 meta, reset, &left, &right, &merge_bases);
402 * If we don't have both a left and a right pointer, there is no
403 * reason to try and display a summary. The header line should contain
404 * all the information the user needs.
409 /* Treat revision walker failure the same as missing commits */
410 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
411 fprintf(f, "%s(revision walker failed)\n", line_prefix);
415 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
419 free_commit_list(merge_bases);
420 clear_commit_marks(left, ~0);
421 clear_commit_marks(right, ~0);
424 void show_submodule_inline_diff(FILE *f, const char *path,
425 const char *line_prefix,
426 struct object_id *one, struct object_id *two,
427 unsigned dirty_submodule, const char *meta,
428 const char *del, const char *add, const char *reset,
429 const struct diff_options *o)
431 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
432 struct commit *left = NULL, *right = NULL;
433 struct commit_list *merge_bases = NULL;
434 struct strbuf submodule_dir = STRBUF_INIT;
435 struct child_process cp = CHILD_PROCESS_INIT;
437 show_submodule_header(f, path, line_prefix, one, two, dirty_submodule,
438 meta, reset, &left, &right, &merge_bases);
440 /* We need a valid left and right commit to display a difference */
441 if (!(left || is_null_oid(one)) ||
442 !(right || is_null_oid(two)))
453 cp.out = dup(fileno(f));
456 /* TODO: other options may need to be passed here. */
457 argv_array_push(&cp.args, "diff");
458 argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix);
459 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
460 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
462 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
465 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
467 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
470 argv_array_push(&cp.args, oid_to_hex(old));
472 * If the submodule has modified content, we will diff against the
473 * work tree, under the assumption that the user has asked for the
474 * diff format and wishes to actually see all differences even if they
475 * haven't yet been committed to the submodule yet.
477 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
478 argv_array_push(&cp.args, oid_to_hex(new));
480 if (run_command(&cp))
481 fprintf(f, "(diff failed)\n");
484 strbuf_release(&submodule_dir);
486 free_commit_list(merge_bases);
488 clear_commit_marks(left, ~0);
490 clear_commit_marks(right, ~0);
493 void set_config_fetch_recurse_submodules(int value)
495 config_fetch_recurse_submodules = value;
498 static int has_remote(const char *refname, const struct object_id *oid,
499 int flags, void *cb_data)
504 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
506 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
509 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
510 struct child_process cp = CHILD_PROCESS_INIT;
511 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
512 struct strbuf buf = STRBUF_INIT;
513 int needs_pushing = 0;
515 argv[1] = sha1_to_hex(sha1);
517 prepare_submodule_repo_env(&cp.env_array);
522 if (start_command(&cp))
523 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
524 sha1_to_hex(sha1), path);
525 if (strbuf_read(&buf, cp.out, 41))
529 strbuf_release(&buf);
530 return needs_pushing;
536 static void collect_submodules_from_diff(struct diff_queue_struct *q,
537 struct diff_options *options,
541 struct string_list *needs_pushing = data;
543 for (i = 0; i < q->nr; i++) {
544 struct diff_filepair *p = q->queue[i];
545 if (!S_ISGITLINK(p->two->mode))
547 if (submodule_needs_pushing(p->two->path, p->two->oid.hash))
548 string_list_insert(needs_pushing, p->two->path);
552 static void find_unpushed_submodule_commits(struct commit *commit,
553 struct string_list *needs_pushing)
557 init_revisions(&rev, NULL);
558 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
559 rev.diffopt.format_callback = collect_submodules_from_diff;
560 rev.diffopt.format_callback_data = needs_pushing;
561 diff_tree_combined_merge(commit, 1, &rev);
564 int find_unpushed_submodules(unsigned char new_sha1[20],
565 const char *remotes_name, struct string_list *needs_pushing)
568 struct commit *commit;
569 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
570 int argc = ARRAY_SIZE(argv) - 1;
573 struct strbuf remotes_arg = STRBUF_INIT;
575 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
576 init_revisions(&rev, NULL);
577 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
579 argv[3] = remotes_arg.buf;
580 setup_revisions(argc, argv, &rev, NULL);
581 if (prepare_revision_walk(&rev))
582 die("revision walk setup failed");
584 while ((commit = get_revision(&rev)) != NULL)
585 find_unpushed_submodule_commits(commit, needs_pushing);
587 reset_revision_walk();
589 strbuf_release(&remotes_arg);
591 return needs_pushing->nr;
594 static int push_submodule(const char *path)
596 if (add_submodule_odb(path))
599 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
600 struct child_process cp = CHILD_PROCESS_INIT;
601 const char *argv[] = {"push", NULL};
604 prepare_submodule_repo_env(&cp.env_array);
608 if (run_command(&cp))
616 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
619 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
621 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
624 for (i = 0; i < needs_pushing.nr; i++) {
625 const char *path = needs_pushing.items[i].string;
626 fprintf(stderr, "Pushing submodule '%s'\n", path);
627 if (!push_submodule(path)) {
628 fprintf(stderr, "Unable to push submodule '%s'\n", path);
633 string_list_clear(&needs_pushing, 0);
638 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
641 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
642 /* Even if the submodule is checked out and the commit is
643 * present, make sure it is reachable from a ref. */
644 struct child_process cp = CHILD_PROCESS_INIT;
645 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
646 struct strbuf buf = STRBUF_INIT;
648 argv[3] = sha1_to_hex(sha1);
650 prepare_submodule_repo_env(&cp.env_array);
654 if (!capture_command(&cp, &buf, 1024) && !buf.len)
657 strbuf_release(&buf);
662 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
663 struct diff_options *options,
667 for (i = 0; i < q->nr; i++) {
668 struct diff_filepair *p = q->queue[i];
669 if (!S_ISGITLINK(p->two->mode))
672 if (S_ISGITLINK(p->one->mode)) {
673 /* NEEDSWORK: We should honor the name configured in
674 * the .gitmodules file of the commit we are examining
675 * here to be able to correctly follow submodules
676 * being moved around. */
677 struct string_list_item *path;
678 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
679 if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash))
680 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
682 /* Submodule is new or was moved here */
683 /* NEEDSWORK: When the .git directories of submodules
684 * live inside the superprojects .git directory some
685 * day we should fetch new submodules directly into
686 * that location too when config or options request
687 * that so they can be checked out from there. */
693 static int add_sha1_to_array(const char *ref, const struct object_id *oid,
694 int flags, void *data)
696 sha1_array_append(data, oid->hash);
700 void check_for_new_submodule_commits(unsigned char new_sha1[20])
702 if (!initialized_fetch_ref_tips) {
703 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
704 initialized_fetch_ref_tips = 1;
707 sha1_array_append(&ref_tips_after_fetch, new_sha1);
710 static int add_sha1_to_argv(const unsigned char sha1[20], void *data)
712 argv_array_push(data, sha1_to_hex(sha1));
716 static void calculate_changed_submodule_paths(void)
719 struct commit *commit;
720 struct argv_array argv = ARGV_ARRAY_INIT;
722 /* No need to check if there are no submodules configured */
723 if (!submodule_from_path(NULL, NULL))
726 init_revisions(&rev, NULL);
727 argv_array_push(&argv, "--"); /* argv[0] program name */
728 sha1_array_for_each_unique(&ref_tips_after_fetch,
729 add_sha1_to_argv, &argv);
730 argv_array_push(&argv, "--not");
731 sha1_array_for_each_unique(&ref_tips_before_fetch,
732 add_sha1_to_argv, &argv);
733 setup_revisions(argv.argc, argv.argv, &rev, NULL);
734 if (prepare_revision_walk(&rev))
735 die("revision walk setup failed");
738 * Collect all submodules (whether checked out or not) for which new
739 * commits have been recorded upstream in "changed_submodule_paths".
741 while ((commit = get_revision(&rev))) {
742 struct commit_list *parent = commit->parents;
744 struct diff_options diff_opts;
745 diff_setup(&diff_opts);
746 DIFF_OPT_SET(&diff_opts, RECURSIVE);
747 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
748 diff_opts.format_callback = submodule_collect_changed_cb;
749 diff_setup_done(&diff_opts);
750 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
751 diffcore_std(&diff_opts);
752 diff_flush(&diff_opts);
753 parent = parent->next;
757 argv_array_clear(&argv);
758 sha1_array_clear(&ref_tips_before_fetch);
759 sha1_array_clear(&ref_tips_after_fetch);
760 initialized_fetch_ref_tips = 0;
763 struct submodule_parallel_fetch {
765 struct argv_array args;
766 const char *work_tree;
768 int command_line_option;
772 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
774 static int get_next_submodule(struct child_process *cp,
775 struct strbuf *err, void *data, void **task_cb)
778 struct submodule_parallel_fetch *spf = data;
780 for (; spf->count < active_nr; spf->count++) {
781 struct strbuf submodule_path = STRBUF_INIT;
782 struct strbuf submodule_git_dir = STRBUF_INIT;
783 struct strbuf submodule_prefix = STRBUF_INIT;
784 const struct cache_entry *ce = active_cache[spf->count];
785 const char *git_dir, *default_argv;
786 const struct submodule *submodule;
788 if (!S_ISGITLINK(ce->ce_mode))
791 submodule = submodule_from_path(null_sha1, ce->name);
793 submodule = submodule_from_name(null_sha1, ce->name);
795 default_argv = "yes";
796 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
798 submodule->fetch_recurse !=
799 RECURSE_SUBMODULES_NONE) {
800 if (submodule->fetch_recurse ==
801 RECURSE_SUBMODULES_OFF)
803 if (submodule->fetch_recurse ==
804 RECURSE_SUBMODULES_ON_DEMAND) {
805 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
807 default_argv = "on-demand";
810 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
811 gitmodules_is_unmerged)
813 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
814 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
816 default_argv = "on-demand";
819 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
820 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
822 default_argv = "on-demand";
825 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
826 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
827 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
828 git_dir = read_gitfile(submodule_git_dir.buf);
830 git_dir = submodule_git_dir.buf;
831 if (is_directory(git_dir)) {
832 child_process_init(cp);
833 cp->dir = strbuf_detach(&submodule_path, NULL);
834 prepare_submodule_repo_env(&cp->env_array);
837 strbuf_addf(err, "Fetching submodule %s%s\n",
838 spf->prefix, ce->name);
839 argv_array_init(&cp->args);
840 argv_array_pushv(&cp->args, spf->args.argv);
841 argv_array_push(&cp->args, default_argv);
842 argv_array_push(&cp->args, "--submodule-prefix");
843 argv_array_push(&cp->args, submodule_prefix.buf);
846 strbuf_release(&submodule_path);
847 strbuf_release(&submodule_git_dir);
848 strbuf_release(&submodule_prefix);
857 static int fetch_start_failure(struct strbuf *err,
858 void *cb, void *task_cb)
860 struct submodule_parallel_fetch *spf = cb;
867 static int fetch_finish(int retvalue, struct strbuf *err,
868 void *cb, void *task_cb)
870 struct submodule_parallel_fetch *spf = cb;
878 int fetch_populated_submodules(const struct argv_array *options,
879 const char *prefix, int command_line_option,
880 int quiet, int max_parallel_jobs)
883 struct submodule_parallel_fetch spf = SPF_INIT;
885 spf.work_tree = get_git_work_tree();
886 spf.command_line_option = command_line_option;
893 if (read_cache() < 0)
894 die("index file corrupt");
896 argv_array_push(&spf.args, "fetch");
897 for (i = 0; i < options->argc; i++)
898 argv_array_push(&spf.args, options->argv[i]);
899 argv_array_push(&spf.args, "--recurse-submodules-default");
900 /* default value, "--submodule-prefix" and its value are added later */
902 if (max_parallel_jobs < 0)
903 max_parallel_jobs = parallel_jobs;
905 calculate_changed_submodule_paths();
906 run_processes_parallel(max_parallel_jobs,
912 argv_array_clear(&spf.args);
914 string_list_clear(&changed_submodule_paths, 1);
918 unsigned is_submodule_modified(const char *path, int ignore_untracked)
921 struct child_process cp = CHILD_PROCESS_INIT;
922 const char *argv[] = {
928 struct strbuf buf = STRBUF_INIT;
929 unsigned dirty_submodule = 0;
930 const char *line, *next_line;
933 strbuf_addf(&buf, "%s/.git", path);
934 git_dir = read_gitfile(buf.buf);
937 if (!is_directory(git_dir)) {
938 strbuf_release(&buf);
939 /* The submodule is not checked out, so it is not modified */
945 if (ignore_untracked)
949 prepare_submodule_repo_env(&cp.env_array);
954 if (start_command(&cp))
955 die("Could not run 'git status --porcelain' in submodule %s", path);
957 len = strbuf_read(&buf, cp.out, 1024);
960 if ((line[0] == '?') && (line[1] == '?')) {
961 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
962 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
965 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
966 if (ignore_untracked ||
967 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
970 next_line = strchr(line, '\n');
974 len -= (next_line - line);
979 if (finish_command(&cp))
980 die("'git status --porcelain' failed in submodule %s", path);
982 strbuf_release(&buf);
983 return dirty_submodule;
986 int submodule_uses_gitfile(const char *path)
988 struct child_process cp = CHILD_PROCESS_INIT;
989 const char *argv[] = {
997 struct strbuf buf = STRBUF_INIT;
1000 strbuf_addf(&buf, "%s/.git", path);
1001 git_dir = read_gitfile(buf.buf);
1003 strbuf_release(&buf);
1006 strbuf_release(&buf);
1008 /* Now test that all nested submodules use a gitfile too */
1010 prepare_submodule_repo_env(&cp.env_array);
1016 if (run_command(&cp))
1022 int ok_to_remove_submodule(const char *path)
1025 struct child_process cp = CHILD_PROCESS_INIT;
1026 struct strbuf buf = STRBUF_INIT;
1027 int ok_to_remove = 1;
1029 if (!file_exists(path) || is_empty_dir(path))
1032 if (!submodule_uses_gitfile(path))
1035 argv_array_pushl(&cp.args, "status", "--porcelain", "-u",
1036 "--ignore-submodules=none", NULL);
1037 prepare_submodule_repo_env(&cp.env_array);
1042 if (start_command(&cp))
1043 die(_("could not run 'git status --porcelain -u --ignore-submodules=none' in submodule %s"), path);
1045 len = strbuf_read(&buf, cp.out, 1024);
1050 if (finish_command(&cp))
1051 die(_("'git status --porcelain -u --ignore-submodules=none' failed in submodule %s"), path);
1053 strbuf_release(&buf);
1054 return ok_to_remove;
1057 static int find_first_merges(struct object_array *result, const char *path,
1058 struct commit *a, struct commit *b)
1061 struct object_array merges = OBJECT_ARRAY_INIT;
1062 struct commit *commit;
1063 int contains_another;
1065 char merged_revision[42];
1066 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1067 "--all", merged_revision, NULL };
1068 struct rev_info revs;
1069 struct setup_revision_opt rev_opts;
1071 memset(result, 0, sizeof(struct object_array));
1072 memset(&rev_opts, 0, sizeof(rev_opts));
1074 /* get all revisions that merge commit a */
1075 snprintf(merged_revision, sizeof(merged_revision), "^%s",
1076 oid_to_hex(&a->object.oid));
1077 init_revisions(&revs, NULL);
1078 rev_opts.submodule = path;
1079 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1081 /* save all revisions from the above list that contain b */
1082 if (prepare_revision_walk(&revs))
1083 die("revision walk setup failed");
1084 while ((commit = get_revision(&revs)) != NULL) {
1085 struct object *o = &(commit->object);
1086 if (in_merge_bases(b, commit))
1087 add_object_array(o, NULL, &merges);
1089 reset_revision_walk();
1091 /* Now we've got all merges that contain a and b. Prune all
1092 * merges that contain another found merge and save them in
1095 for (i = 0; i < merges.nr; i++) {
1096 struct commit *m1 = (struct commit *) merges.objects[i].item;
1098 contains_another = 0;
1099 for (j = 0; j < merges.nr; j++) {
1100 struct commit *m2 = (struct commit *) merges.objects[j].item;
1101 if (i != j && in_merge_bases(m2, m1)) {
1102 contains_another = 1;
1107 if (!contains_another)
1108 add_object_array(merges.objects[i].item, NULL, result);
1111 free(merges.objects);
1115 static void print_commit(struct commit *commit)
1117 struct strbuf sb = STRBUF_INIT;
1118 struct pretty_print_context ctx = {0};
1119 ctx.date_mode.type = DATE_NORMAL;
1120 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1121 fprintf(stderr, "%s\n", sb.buf);
1122 strbuf_release(&sb);
1125 #define MERGE_WARNING(path, msg) \
1126 warning("Failed to merge submodule %s (%s)", path, msg);
1128 int merge_submodule(unsigned char result[20], const char *path,
1129 const unsigned char base[20], const unsigned char a[20],
1130 const unsigned char b[20], int search)
1132 struct commit *commit_base, *commit_a, *commit_b;
1134 struct object_array merges;
1138 /* store a in result in case we fail */
1141 /* we can not handle deletion conflicts */
1142 if (is_null_sha1(base))
1144 if (is_null_sha1(a))
1146 if (is_null_sha1(b))
1149 if (add_submodule_odb(path)) {
1150 MERGE_WARNING(path, "not checked out");
1154 if (!(commit_base = lookup_commit_reference(base)) ||
1155 !(commit_a = lookup_commit_reference(a)) ||
1156 !(commit_b = lookup_commit_reference(b))) {
1157 MERGE_WARNING(path, "commits not present");
1161 /* check whether both changes are forward */
1162 if (!in_merge_bases(commit_base, commit_a) ||
1163 !in_merge_bases(commit_base, commit_b)) {
1164 MERGE_WARNING(path, "commits don't follow merge-base");
1168 /* Case #1: a is contained in b or vice versa */
1169 if (in_merge_bases(commit_a, commit_b)) {
1173 if (in_merge_bases(commit_b, commit_a)) {
1179 * Case #2: There are one or more merges that contain a and b in
1180 * the submodule. If there is only one, then present it as a
1181 * suggestion to the user, but leave it marked unmerged so the
1182 * user needs to confirm the resolution.
1185 /* Skip the search if makes no sense to the calling context. */
1189 /* find commit which merges them */
1190 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1191 switch (parent_count) {
1193 MERGE_WARNING(path, "merge following commits not found");
1197 MERGE_WARNING(path, "not fast-forward");
1198 fprintf(stderr, "Found a possible merge resolution "
1199 "for the submodule:\n");
1200 print_commit((struct commit *) merges.objects[0].item);
1202 "If this is correct simply add it to the index "
1205 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1206 "which will accept this suggestion.\n",
1207 oid_to_hex(&merges.objects[0].item->oid), path);
1211 MERGE_WARNING(path, "multiple merges found");
1212 for (i = 0; i < merges.nr; i++)
1213 print_commit((struct commit *) merges.objects[i].item);
1216 free(merges.objects);
1220 int parallel_submodules(void)
1222 return parallel_jobs;
1225 void prepare_submodule_repo_env(struct argv_array *out)
1227 const char * const *var;
1229 for (var = local_repo_env; *var; var++) {
1230 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
1231 argv_array_push(out, *var);
1233 argv_array_push(out, "GIT_DIR=.git");
1237 * Embeds a single submodules git directory into the superprojects git dir,
1240 static void relocate_single_git_dir_into_superproject(const char *prefix,
1243 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1244 const char *new_git_dir;
1245 const struct submodule *sub;
1247 if (submodule_uses_worktrees(path))
1248 die(_("relocate_gitdir for submodule '%s' with "
1249 "more than one worktree not supported"), path);
1251 old_git_dir = xstrfmt("%s/.git", path);
1252 if (read_gitfile(old_git_dir))
1253 /* If it is an actual gitfile, it doesn't need migration. */
1256 real_old_git_dir = xstrdup(real_path(old_git_dir));
1258 sub = submodule_from_path(null_sha1, path);
1260 die(_("could not lookup name for submodule '%s'"), path);
1262 new_git_dir = git_path("modules/%s", sub->name);
1263 if (safe_create_leading_directories_const(new_git_dir) < 0)
1264 die(_("could not create directory '%s'"), new_git_dir);
1265 real_new_git_dir = xstrdup(real_path(new_git_dir));
1268 prefix = get_super_prefix();
1270 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1271 prefix ? prefix : "", path,
1272 real_old_git_dir, real_new_git_dir);
1274 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
1277 free(real_old_git_dir);
1278 free(real_new_git_dir);
1282 * Migrate the git directory of the submodule given by path from
1283 * having its git directory within the working tree to the git dir nested
1284 * in its superprojects git dir under modules/.
1286 void absorb_git_dir_into_superproject(const char *prefix,
1290 const char *sub_git_dir, *v;
1291 char *real_sub_git_dir = NULL, *real_common_git_dir = NULL;
1292 struct strbuf gitdir = STRBUF_INIT;
1294 strbuf_addf(&gitdir, "%s/.git", path);
1295 sub_git_dir = resolve_gitdir(gitdir.buf);
1297 /* Not populated? */
1301 /* Is it already absorbed into the superprojects git dir? */
1302 real_sub_git_dir = xstrdup(real_path(sub_git_dir));
1303 real_common_git_dir = xstrdup(real_path(get_git_common_dir()));
1304 if (!skip_prefix(real_sub_git_dir, real_common_git_dir, &v))
1305 relocate_single_git_dir_into_superproject(prefix, path);
1307 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1308 struct child_process cp = CHILD_PROCESS_INIT;
1309 struct strbuf sb = STRBUF_INIT;
1311 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
1312 die("BUG: we don't know how to pass the flags down?");
1314 if (get_super_prefix())
1315 strbuf_addstr(&sb, get_super_prefix());
1316 strbuf_addstr(&sb, path);
1317 strbuf_addch(&sb, '/');
1322 argv_array_pushl(&cp.args, "--super-prefix", sb.buf,
1323 "submodule--helper",
1324 "absorb-git-dirs", NULL);
1325 prepare_submodule_repo_env(&cp.env_array);
1326 if (run_command(&cp))
1327 die(_("could not recurse into submodule '%s'"), path);
1329 strbuf_release(&sb);
1333 strbuf_release(&gitdir);
1334 free(real_sub_git_dir);
1335 free(real_common_git_dir);