Merge branch 'jl/some-submodule-config-are-not-boolean' into maint
[git/mjg.git] / submodule.c
blobc0f93c208c11b2dad0d508fcb7adf14873484385
1 #include "cache.h"
2 #include "submodule.h"
3 #include "dir.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "run-command.h"
8 #include "diffcore.h"
9 #include "refs.h"
10 #include "string-list.h"
11 #include "sha1-array.h"
12 #include "argv-array.h"
14 static struct string_list config_name_for_path;
15 static struct string_list config_fetch_recurse_submodules_for_name;
16 static struct string_list config_ignore_for_name;
17 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
18 static struct string_list changed_submodule_paths;
19 static int initialized_fetch_ref_tips;
20 static struct sha1_array ref_tips_before_fetch;
21 static struct sha1_array ref_tips_after_fetch;
24 * The following flag is set if the .gitmodules file is unmerged. We then
25 * disable recursion for all submodules where .git/config doesn't have a
26 * matching config entry because we can't guess what might be configured in
27 * .gitmodules unless the user resolves the conflict. When a command line
28 * option is given (which always overrides configuration) this flag will be
29 * ignored.
31 static int gitmodules_is_unmerged;
33 static int add_submodule_odb(const char *path)
35 struct strbuf objects_directory = STRBUF_INIT;
36 struct alternate_object_database *alt_odb;
37 int ret = 0;
38 const char *git_dir;
40 strbuf_addf(&objects_directory, "%s/.git", path);
41 git_dir = read_gitfile(objects_directory.buf);
42 if (git_dir) {
43 strbuf_reset(&objects_directory);
44 strbuf_addstr(&objects_directory, git_dir);
46 strbuf_addstr(&objects_directory, "/objects/");
47 if (!is_directory(objects_directory.buf)) {
48 ret = -1;
49 goto done;
51 /* avoid adding it twice */
52 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
53 if (alt_odb->name - alt_odb->base == objects_directory.len &&
54 !strncmp(alt_odb->base, objects_directory.buf,
55 objects_directory.len))
56 goto done;
58 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
59 alt_odb->next = alt_odb_list;
60 strcpy(alt_odb->base, objects_directory.buf);
61 alt_odb->name = alt_odb->base + objects_directory.len;
62 alt_odb->name[2] = '/';
63 alt_odb->name[40] = '\0';
64 alt_odb->name[41] = '\0';
65 alt_odb_list = alt_odb;
67 /* add possible alternates from the submodule */
68 read_info_alternates(objects_directory.buf, 0);
69 prepare_alt_odb();
70 done:
71 strbuf_release(&objects_directory);
72 return ret;
75 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
76 const char *path)
78 struct string_list_item *path_option, *ignore_option;
79 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
80 if (path_option) {
81 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
82 if (ignore_option)
83 handle_ignore_submodules_arg(diffopt, ignore_option->util);
84 else if (gitmodules_is_unmerged)
85 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
89 int submodule_config(const char *var, const char *value, void *cb)
91 if (!prefixcmp(var, "submodule."))
92 return parse_submodule_config_option(var, value);
93 else if (!strcmp(var, "fetch.recursesubmodules")) {
94 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
95 return 0;
97 return 0;
100 void gitmodules_config(void)
102 const char *work_tree = get_git_work_tree();
103 if (work_tree) {
104 struct strbuf gitmodules_path = STRBUF_INIT;
105 int pos;
106 strbuf_addstr(&gitmodules_path, work_tree);
107 strbuf_addstr(&gitmodules_path, "/.gitmodules");
108 if (read_cache() < 0)
109 die("index file corrupt");
110 pos = cache_name_pos(".gitmodules", 11);
111 if (pos < 0) { /* .gitmodules not found or isn't merged */
112 pos = -1 - pos;
113 if (active_nr > pos) { /* there is a .gitmodules */
114 const struct cache_entry *ce = active_cache[pos];
115 if (ce_namelen(ce) == 11 &&
116 !memcmp(ce->name, ".gitmodules", 11))
117 gitmodules_is_unmerged = 1;
121 if (!gitmodules_is_unmerged)
122 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
123 strbuf_release(&gitmodules_path);
127 int parse_submodule_config_option(const char *var, const char *value)
129 struct string_list_item *config;
130 const char *name, *key;
131 int namelen;
133 if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
134 return 0;
136 if (!strcmp(key, "path")) {
137 if (!value)
138 return config_error_nonbool(var);
140 config = unsorted_string_list_lookup(&config_name_for_path, value);
141 if (config)
142 free(config->util);
143 else
144 config = string_list_append(&config_name_for_path, xstrdup(value));
145 config->util = xmemdupz(name, namelen);
146 } else if (!strcmp(key, "fetchrecursesubmodules")) {
147 char *name_cstr = xmemdupz(name, namelen);
148 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
149 if (!config)
150 config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
151 else
152 free(name_cstr);
153 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
154 } else if (!strcmp(key, "ignore")) {
155 char *name_cstr;
157 if (!value)
158 return config_error_nonbool(var);
160 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
161 strcmp(value, "all") && strcmp(value, "none")) {
162 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
163 return 0;
166 name_cstr = xmemdupz(name, namelen);
167 config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
168 if (config) {
169 free(config->util);
170 free(name_cstr);
171 } else
172 config = string_list_append(&config_ignore_for_name, name_cstr);
173 config->util = xstrdup(value);
174 return 0;
176 return 0;
179 void handle_ignore_submodules_arg(struct diff_options *diffopt,
180 const char *arg)
182 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
183 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
184 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
186 if (!strcmp(arg, "all"))
187 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
188 else if (!strcmp(arg, "untracked"))
189 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
190 else if (!strcmp(arg, "dirty"))
191 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
192 else if (strcmp(arg, "none"))
193 die("bad --ignore-submodules argument: %s", arg);
196 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
197 struct commit *left, struct commit *right,
198 int *fast_forward, int *fast_backward)
200 struct commit_list *merge_bases, *list;
202 init_revisions(rev, NULL);
203 setup_revisions(0, NULL, rev, NULL);
204 rev->left_right = 1;
205 rev->first_parent_only = 1;
206 left->object.flags |= SYMMETRIC_LEFT;
207 add_pending_object(rev, &left->object, path);
208 add_pending_object(rev, &right->object, path);
209 merge_bases = get_merge_bases(left, right, 1);
210 if (merge_bases) {
211 if (merge_bases->item == left)
212 *fast_forward = 1;
213 else if (merge_bases->item == right)
214 *fast_backward = 1;
216 for (list = merge_bases; list; list = list->next) {
217 list->item->object.flags |= UNINTERESTING;
218 add_pending_object(rev, &list->item->object,
219 sha1_to_hex(list->item->object.sha1));
221 return prepare_revision_walk(rev);
224 static void print_submodule_summary(struct rev_info *rev, FILE *f,
225 const char *line_prefix,
226 const char *del, const char *add, const char *reset)
228 static const char format[] = " %m %s";
229 struct strbuf sb = STRBUF_INIT;
230 struct commit *commit;
232 while ((commit = get_revision(rev))) {
233 struct pretty_print_context ctx = {0};
234 ctx.date_mode = rev->date_mode;
235 ctx.output_encoding = get_log_output_encoding();
236 strbuf_setlen(&sb, 0);
237 strbuf_addstr(&sb, line_prefix);
238 if (commit->object.flags & SYMMETRIC_LEFT) {
239 if (del)
240 strbuf_addstr(&sb, del);
242 else if (add)
243 strbuf_addstr(&sb, add);
244 format_commit_message(commit, format, &sb, &ctx);
245 if (reset)
246 strbuf_addstr(&sb, reset);
247 strbuf_addch(&sb, '\n');
248 fprintf(f, "%s", sb.buf);
250 strbuf_release(&sb);
253 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
255 switch (git_config_maybe_bool(opt, arg)) {
256 case 1:
257 return RECURSE_SUBMODULES_ON;
258 case 0:
259 return RECURSE_SUBMODULES_OFF;
260 default:
261 if (!strcmp(arg, "on-demand"))
262 return RECURSE_SUBMODULES_ON_DEMAND;
263 die("bad %s argument: %s", opt, arg);
267 void show_submodule_summary(FILE *f, const char *path,
268 const char *line_prefix,
269 unsigned char one[20], unsigned char two[20],
270 unsigned dirty_submodule, const char *meta,
271 const char *del, const char *add, const char *reset)
273 struct rev_info rev;
274 struct commit *left = NULL, *right = NULL;
275 const char *message = NULL;
276 struct strbuf sb = STRBUF_INIT;
277 int fast_forward = 0, fast_backward = 0;
279 if (is_null_sha1(two))
280 message = "(submodule deleted)";
281 else if (add_submodule_odb(path))
282 message = "(not checked out)";
283 else if (is_null_sha1(one))
284 message = "(new submodule)";
285 else if (!(left = lookup_commit_reference(one)) ||
286 !(right = lookup_commit_reference(two)))
287 message = "(commits not present)";
288 else if (prepare_submodule_summary(&rev, path, left, right,
289 &fast_forward, &fast_backward))
290 message = "(revision walker failed)";
292 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
293 fprintf(f, "%sSubmodule %s contains untracked content\n",
294 line_prefix, path);
295 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
296 fprintf(f, "%sSubmodule %s contains modified content\n",
297 line_prefix, path);
299 if (!hashcmp(one, two)) {
300 strbuf_release(&sb);
301 return;
304 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
305 find_unique_abbrev(one, DEFAULT_ABBREV));
306 if (!fast_backward && !fast_forward)
307 strbuf_addch(&sb, '.');
308 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
309 if (message)
310 strbuf_addf(&sb, " %s%s\n", message, reset);
311 else
312 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
313 fwrite(sb.buf, sb.len, 1, f);
315 if (!message) /* only NULL if we succeeded in setting up the walk */
316 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
317 if (left)
318 clear_commit_marks(left, ~0);
319 if (right)
320 clear_commit_marks(right, ~0);
322 strbuf_release(&sb);
325 void set_config_fetch_recurse_submodules(int value)
327 config_fetch_recurse_submodules = value;
330 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
332 return 1;
335 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
337 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
338 return 0;
340 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
341 struct child_process cp;
342 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
343 struct strbuf buf = STRBUF_INIT;
344 int needs_pushing = 0;
346 argv[1] = sha1_to_hex(sha1);
347 memset(&cp, 0, sizeof(cp));
348 cp.argv = argv;
349 cp.env = local_repo_env;
350 cp.git_cmd = 1;
351 cp.no_stdin = 1;
352 cp.out = -1;
353 cp.dir = path;
354 if (start_command(&cp))
355 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
356 sha1_to_hex(sha1), path);
357 if (strbuf_read(&buf, cp.out, 41))
358 needs_pushing = 1;
359 finish_command(&cp);
360 close(cp.out);
361 strbuf_release(&buf);
362 return needs_pushing;
365 return 0;
368 static void collect_submodules_from_diff(struct diff_queue_struct *q,
369 struct diff_options *options,
370 void *data)
372 int i;
373 struct string_list *needs_pushing = data;
375 for (i = 0; i < q->nr; i++) {
376 struct diff_filepair *p = q->queue[i];
377 if (!S_ISGITLINK(p->two->mode))
378 continue;
379 if (submodule_needs_pushing(p->two->path, p->two->sha1))
380 string_list_insert(needs_pushing, p->two->path);
384 static void find_unpushed_submodule_commits(struct commit *commit,
385 struct string_list *needs_pushing)
387 struct rev_info rev;
389 init_revisions(&rev, NULL);
390 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
391 rev.diffopt.format_callback = collect_submodules_from_diff;
392 rev.diffopt.format_callback_data = needs_pushing;
393 diff_tree_combined_merge(commit, 1, &rev);
396 int find_unpushed_submodules(unsigned char new_sha1[20],
397 const char *remotes_name, struct string_list *needs_pushing)
399 struct rev_info rev;
400 struct commit *commit;
401 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
402 int argc = ARRAY_SIZE(argv) - 1;
403 char *sha1_copy;
405 struct strbuf remotes_arg = STRBUF_INIT;
407 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
408 init_revisions(&rev, NULL);
409 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
410 argv[1] = sha1_copy;
411 argv[3] = remotes_arg.buf;
412 setup_revisions(argc, argv, &rev, NULL);
413 if (prepare_revision_walk(&rev))
414 die("revision walk setup failed");
416 while ((commit = get_revision(&rev)) != NULL)
417 find_unpushed_submodule_commits(commit, needs_pushing);
419 reset_revision_walk();
420 free(sha1_copy);
421 strbuf_release(&remotes_arg);
423 return needs_pushing->nr;
426 static int push_submodule(const char *path)
428 if (add_submodule_odb(path))
429 return 1;
431 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
432 struct child_process cp;
433 const char *argv[] = {"push", NULL};
435 memset(&cp, 0, sizeof(cp));
436 cp.argv = argv;
437 cp.env = local_repo_env;
438 cp.git_cmd = 1;
439 cp.no_stdin = 1;
440 cp.dir = path;
441 if (run_command(&cp))
442 return 0;
443 close(cp.out);
446 return 1;
449 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
451 int i, ret = 1;
452 struct string_list needs_pushing;
454 memset(&needs_pushing, 0, sizeof(struct string_list));
455 needs_pushing.strdup_strings = 1;
457 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
458 return 1;
460 for (i = 0; i < needs_pushing.nr; i++) {
461 const char *path = needs_pushing.items[i].string;
462 fprintf(stderr, "Pushing submodule '%s'\n", path);
463 if (!push_submodule(path)) {
464 fprintf(stderr, "Unable to push submodule '%s'\n", path);
465 ret = 0;
469 string_list_clear(&needs_pushing, 0);
471 return ret;
474 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
476 int is_present = 0;
477 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
478 /* Even if the submodule is checked out and the commit is
479 * present, make sure it is reachable from a ref. */
480 struct child_process cp;
481 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
482 struct strbuf buf = STRBUF_INIT;
484 argv[3] = sha1_to_hex(sha1);
485 memset(&cp, 0, sizeof(cp));
486 cp.argv = argv;
487 cp.env = local_repo_env;
488 cp.git_cmd = 1;
489 cp.no_stdin = 1;
490 cp.out = -1;
491 cp.dir = path;
492 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
493 is_present = 1;
495 close(cp.out);
496 strbuf_release(&buf);
498 return is_present;
501 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
502 struct diff_options *options,
503 void *data)
505 int i;
506 for (i = 0; i < q->nr; i++) {
507 struct diff_filepair *p = q->queue[i];
508 if (!S_ISGITLINK(p->two->mode))
509 continue;
511 if (S_ISGITLINK(p->one->mode)) {
512 /* NEEDSWORK: We should honor the name configured in
513 * the .gitmodules file of the commit we are examining
514 * here to be able to correctly follow submodules
515 * being moved around. */
516 struct string_list_item *path;
517 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
518 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
519 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
520 } else {
521 /* Submodule is new or was moved here */
522 /* NEEDSWORK: When the .git directories of submodules
523 * live inside the superprojects .git directory some
524 * day we should fetch new submodules directly into
525 * that location too when config or options request
526 * that so they can be checked out from there. */
527 continue;
532 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
533 int flags, void *data)
535 sha1_array_append(data, sha1);
536 return 0;
539 void check_for_new_submodule_commits(unsigned char new_sha1[20])
541 if (!initialized_fetch_ref_tips) {
542 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
543 initialized_fetch_ref_tips = 1;
546 sha1_array_append(&ref_tips_after_fetch, new_sha1);
549 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
551 argv_array_push(data, sha1_to_hex(sha1));
554 static void calculate_changed_submodule_paths(void)
556 struct rev_info rev;
557 struct commit *commit;
558 struct argv_array argv = ARGV_ARRAY_INIT;
560 /* No need to check if there are no submodules configured */
561 if (!config_name_for_path.nr)
562 return;
564 init_revisions(&rev, NULL);
565 argv_array_push(&argv, "--"); /* argv[0] program name */
566 sha1_array_for_each_unique(&ref_tips_after_fetch,
567 add_sha1_to_argv, &argv);
568 argv_array_push(&argv, "--not");
569 sha1_array_for_each_unique(&ref_tips_before_fetch,
570 add_sha1_to_argv, &argv);
571 setup_revisions(argv.argc, argv.argv, &rev, NULL);
572 if (prepare_revision_walk(&rev))
573 die("revision walk setup failed");
576 * Collect all submodules (whether checked out or not) for which new
577 * commits have been recorded upstream in "changed_submodule_paths".
579 while ((commit = get_revision(&rev))) {
580 struct commit_list *parent = commit->parents;
581 while (parent) {
582 struct diff_options diff_opts;
583 diff_setup(&diff_opts);
584 DIFF_OPT_SET(&diff_opts, RECURSIVE);
585 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
586 diff_opts.format_callback = submodule_collect_changed_cb;
587 diff_setup_done(&diff_opts);
588 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
589 diffcore_std(&diff_opts);
590 diff_flush(&diff_opts);
591 parent = parent->next;
595 argv_array_clear(&argv);
596 sha1_array_clear(&ref_tips_before_fetch);
597 sha1_array_clear(&ref_tips_after_fetch);
598 initialized_fetch_ref_tips = 0;
601 int fetch_populated_submodules(const struct argv_array *options,
602 const char *prefix, int command_line_option,
603 int quiet)
605 int i, result = 0;
606 struct child_process cp;
607 struct argv_array argv = ARGV_ARRAY_INIT;
608 struct string_list_item *name_for_path;
609 const char *work_tree = get_git_work_tree();
610 if (!work_tree)
611 goto out;
613 if (read_cache() < 0)
614 die("index file corrupt");
616 argv_array_push(&argv, "fetch");
617 for (i = 0; i < options->argc; i++)
618 argv_array_push(&argv, options->argv[i]);
619 argv_array_push(&argv, "--recurse-submodules-default");
620 /* default value, "--submodule-prefix" and its value are added later */
622 memset(&cp, 0, sizeof(cp));
623 cp.env = local_repo_env;
624 cp.git_cmd = 1;
625 cp.no_stdin = 1;
627 calculate_changed_submodule_paths();
629 for (i = 0; i < active_nr; i++) {
630 struct strbuf submodule_path = STRBUF_INIT;
631 struct strbuf submodule_git_dir = STRBUF_INIT;
632 struct strbuf submodule_prefix = STRBUF_INIT;
633 const struct cache_entry *ce = active_cache[i];
634 const char *git_dir, *name, *default_argv;
636 if (!S_ISGITLINK(ce->ce_mode))
637 continue;
639 name = ce->name;
640 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
641 if (name_for_path)
642 name = name_for_path->util;
644 default_argv = "yes";
645 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
646 struct string_list_item *fetch_recurse_submodules_option;
647 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
648 if (fetch_recurse_submodules_option) {
649 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
650 continue;
651 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
652 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
653 continue;
654 default_argv = "on-demand";
656 } else {
657 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
658 gitmodules_is_unmerged)
659 continue;
660 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
661 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
662 continue;
663 default_argv = "on-demand";
666 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
667 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
668 continue;
669 default_argv = "on-demand";
672 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
673 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
674 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
675 git_dir = read_gitfile(submodule_git_dir.buf);
676 if (!git_dir)
677 git_dir = submodule_git_dir.buf;
678 if (is_directory(git_dir)) {
679 if (!quiet)
680 printf("Fetching submodule %s%s\n", prefix, ce->name);
681 cp.dir = submodule_path.buf;
682 argv_array_push(&argv, default_argv);
683 argv_array_push(&argv, "--submodule-prefix");
684 argv_array_push(&argv, submodule_prefix.buf);
685 cp.argv = argv.argv;
686 if (run_command(&cp))
687 result = 1;
688 argv_array_pop(&argv);
689 argv_array_pop(&argv);
690 argv_array_pop(&argv);
692 strbuf_release(&submodule_path);
693 strbuf_release(&submodule_git_dir);
694 strbuf_release(&submodule_prefix);
696 argv_array_clear(&argv);
697 out:
698 string_list_clear(&changed_submodule_paths, 1);
699 return result;
702 unsigned is_submodule_modified(const char *path, int ignore_untracked)
704 ssize_t len;
705 struct child_process cp;
706 const char *argv[] = {
707 "status",
708 "--porcelain",
709 NULL,
710 NULL,
712 struct strbuf buf = STRBUF_INIT;
713 unsigned dirty_submodule = 0;
714 const char *line, *next_line;
715 const char *git_dir;
717 strbuf_addf(&buf, "%s/.git", path);
718 git_dir = read_gitfile(buf.buf);
719 if (!git_dir)
720 git_dir = buf.buf;
721 if (!is_directory(git_dir)) {
722 strbuf_release(&buf);
723 /* The submodule is not checked out, so it is not modified */
724 return 0;
727 strbuf_reset(&buf);
729 if (ignore_untracked)
730 argv[2] = "-uno";
732 memset(&cp, 0, sizeof(cp));
733 cp.argv = argv;
734 cp.env = local_repo_env;
735 cp.git_cmd = 1;
736 cp.no_stdin = 1;
737 cp.out = -1;
738 cp.dir = path;
739 if (start_command(&cp))
740 die("Could not run 'git status --porcelain' in submodule %s", path);
742 len = strbuf_read(&buf, cp.out, 1024);
743 line = buf.buf;
744 while (len > 2) {
745 if ((line[0] == '?') && (line[1] == '?')) {
746 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
747 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
748 break;
749 } else {
750 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
751 if (ignore_untracked ||
752 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
753 break;
755 next_line = strchr(line, '\n');
756 if (!next_line)
757 break;
758 next_line++;
759 len -= (next_line - line);
760 line = next_line;
762 close(cp.out);
764 if (finish_command(&cp))
765 die("'git status --porcelain' failed in submodule %s", path);
767 strbuf_release(&buf);
768 return dirty_submodule;
771 int submodule_uses_gitfile(const char *path)
773 struct child_process cp;
774 const char *argv[] = {
775 "submodule",
776 "foreach",
777 "--quiet",
778 "--recursive",
779 "test -f .git",
780 NULL,
782 struct strbuf buf = STRBUF_INIT;
783 const char *git_dir;
785 strbuf_addf(&buf, "%s/.git", path);
786 git_dir = read_gitfile(buf.buf);
787 if (!git_dir) {
788 strbuf_release(&buf);
789 return 0;
791 strbuf_release(&buf);
793 /* Now test that all nested submodules use a gitfile too */
794 memset(&cp, 0, sizeof(cp));
795 cp.argv = argv;
796 cp.env = local_repo_env;
797 cp.git_cmd = 1;
798 cp.no_stdin = 1;
799 cp.no_stderr = 1;
800 cp.no_stdout = 1;
801 cp.dir = path;
802 if (run_command(&cp))
803 return 0;
805 return 1;
808 int ok_to_remove_submodule(const char *path)
810 struct stat st;
811 ssize_t len;
812 struct child_process cp;
813 const char *argv[] = {
814 "status",
815 "--porcelain",
816 "-u",
817 "--ignore-submodules=none",
818 NULL,
820 struct strbuf buf = STRBUF_INIT;
821 int ok_to_remove = 1;
823 if ((lstat(path, &st) < 0) || is_empty_dir(path))
824 return 1;
826 if (!submodule_uses_gitfile(path))
827 return 0;
829 memset(&cp, 0, sizeof(cp));
830 cp.argv = argv;
831 cp.env = local_repo_env;
832 cp.git_cmd = 1;
833 cp.no_stdin = 1;
834 cp.out = -1;
835 cp.dir = path;
836 if (start_command(&cp))
837 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
839 len = strbuf_read(&buf, cp.out, 1024);
840 if (len > 2)
841 ok_to_remove = 0;
842 close(cp.out);
844 if (finish_command(&cp))
845 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
847 strbuf_release(&buf);
848 return ok_to_remove;
851 static int find_first_merges(struct object_array *result, const char *path,
852 struct commit *a, struct commit *b)
854 int i, j;
855 struct object_array merges = OBJECT_ARRAY_INIT;
856 struct commit *commit;
857 int contains_another;
859 char merged_revision[42];
860 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
861 "--all", merged_revision, NULL };
862 struct rev_info revs;
863 struct setup_revision_opt rev_opts;
865 memset(result, 0, sizeof(struct object_array));
866 memset(&rev_opts, 0, sizeof(rev_opts));
868 /* get all revisions that merge commit a */
869 snprintf(merged_revision, sizeof(merged_revision), "^%s",
870 sha1_to_hex(a->object.sha1));
871 init_revisions(&revs, NULL);
872 rev_opts.submodule = path;
873 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
875 /* save all revisions from the above list that contain b */
876 if (prepare_revision_walk(&revs))
877 die("revision walk setup failed");
878 while ((commit = get_revision(&revs)) != NULL) {
879 struct object *o = &(commit->object);
880 if (in_merge_bases(b, commit))
881 add_object_array(o, NULL, &merges);
883 reset_revision_walk();
885 /* Now we've got all merges that contain a and b. Prune all
886 * merges that contain another found merge and save them in
887 * result.
889 for (i = 0; i < merges.nr; i++) {
890 struct commit *m1 = (struct commit *) merges.objects[i].item;
892 contains_another = 0;
893 for (j = 0; j < merges.nr; j++) {
894 struct commit *m2 = (struct commit *) merges.objects[j].item;
895 if (i != j && in_merge_bases(m2, m1)) {
896 contains_another = 1;
897 break;
901 if (!contains_another)
902 add_object_array(merges.objects[i].item, NULL, result);
905 free(merges.objects);
906 return result->nr;
909 static void print_commit(struct commit *commit)
911 struct strbuf sb = STRBUF_INIT;
912 struct pretty_print_context ctx = {0};
913 ctx.date_mode = DATE_NORMAL;
914 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
915 fprintf(stderr, "%s\n", sb.buf);
916 strbuf_release(&sb);
919 #define MERGE_WARNING(path, msg) \
920 warning("Failed to merge submodule %s (%s)", path, msg);
922 int merge_submodule(unsigned char result[20], const char *path,
923 const unsigned char base[20], const unsigned char a[20],
924 const unsigned char b[20], int search)
926 struct commit *commit_base, *commit_a, *commit_b;
927 int parent_count;
928 struct object_array merges;
930 int i;
932 /* store a in result in case we fail */
933 hashcpy(result, a);
935 /* we can not handle deletion conflicts */
936 if (is_null_sha1(base))
937 return 0;
938 if (is_null_sha1(a))
939 return 0;
940 if (is_null_sha1(b))
941 return 0;
943 if (add_submodule_odb(path)) {
944 MERGE_WARNING(path, "not checked out");
945 return 0;
948 if (!(commit_base = lookup_commit_reference(base)) ||
949 !(commit_a = lookup_commit_reference(a)) ||
950 !(commit_b = lookup_commit_reference(b))) {
951 MERGE_WARNING(path, "commits not present");
952 return 0;
955 /* check whether both changes are forward */
956 if (!in_merge_bases(commit_base, commit_a) ||
957 !in_merge_bases(commit_base, commit_b)) {
958 MERGE_WARNING(path, "commits don't follow merge-base");
959 return 0;
962 /* Case #1: a is contained in b or vice versa */
963 if (in_merge_bases(commit_a, commit_b)) {
964 hashcpy(result, b);
965 return 1;
967 if (in_merge_bases(commit_b, commit_a)) {
968 hashcpy(result, a);
969 return 1;
973 * Case #2: There are one or more merges that contain a and b in
974 * the submodule. If there is only one, then present it as a
975 * suggestion to the user, but leave it marked unmerged so the
976 * user needs to confirm the resolution.
979 /* Skip the search if makes no sense to the calling context. */
980 if (!search)
981 return 0;
983 /* find commit which merges them */
984 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
985 switch (parent_count) {
986 case 0:
987 MERGE_WARNING(path, "merge following commits not found");
988 break;
990 case 1:
991 MERGE_WARNING(path, "not fast-forward");
992 fprintf(stderr, "Found a possible merge resolution "
993 "for the submodule:\n");
994 print_commit((struct commit *) merges.objects[0].item);
995 fprintf(stderr,
996 "If this is correct simply add it to the index "
997 "for example\n"
998 "by using:\n\n"
999 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1000 "which will accept this suggestion.\n",
1001 sha1_to_hex(merges.objects[0].item->sha1), path);
1002 break;
1004 default:
1005 MERGE_WARNING(path, "multiple merges found");
1006 for (i = 0; i < merges.nr; i++)
1007 print_commit((struct commit *) merges.objects[i].item);
1010 free(merges.objects);
1011 return 0;