Merge branch 'jc/maint-bundle-too-quiet'
[git/mingw.git] / submodule.c
blob08756387e207700861cfa6dac039334ecd53c7a1
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"
13 static struct string_list config_name_for_path;
14 static struct string_list config_fetch_recurse_submodules_for_name;
15 static struct string_list config_ignore_for_name;
16 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
17 static struct string_list changed_submodule_paths;
18 static int initialized_fetch_ref_tips;
19 static struct sha1_array ref_tips_before_fetch;
20 static struct sha1_array ref_tips_after_fetch;
23 * The following flag is set if the .gitmodules file is unmerged. We then
24 * disable recursion for all submodules where .git/config doesn't have a
25 * matching config entry because we can't guess what might be configured in
26 * .gitmodules unless the user resolves the conflict. When a command line
27 * option is given (which always overrides configuration) this flag will be
28 * ignored.
30 static int gitmodules_is_unmerged;
32 static int add_submodule_odb(const char *path)
34 struct strbuf objects_directory = STRBUF_INIT;
35 struct alternate_object_database *alt_odb;
36 int ret = 0;
37 const char *git_dir;
39 strbuf_addf(&objects_directory, "%s/.git", path);
40 git_dir = read_gitfile(objects_directory.buf);
41 if (git_dir) {
42 strbuf_reset(&objects_directory);
43 strbuf_addstr(&objects_directory, git_dir);
45 strbuf_addstr(&objects_directory, "/objects/");
46 if (!is_directory(objects_directory.buf)) {
47 ret = -1;
48 goto done;
50 /* avoid adding it twice */
51 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
52 if (alt_odb->name - alt_odb->base == objects_directory.len &&
53 !strncmp(alt_odb->base, objects_directory.buf,
54 objects_directory.len))
55 goto done;
57 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
58 alt_odb->next = alt_odb_list;
59 strcpy(alt_odb->base, objects_directory.buf);
60 alt_odb->name = alt_odb->base + objects_directory.len;
61 alt_odb->name[2] = '/';
62 alt_odb->name[40] = '\0';
63 alt_odb->name[41] = '\0';
64 alt_odb_list = alt_odb;
65 prepare_alt_odb();
66 done:
67 strbuf_release(&objects_directory);
68 return ret;
71 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
72 const char *path)
74 struct string_list_item *path_option, *ignore_option;
75 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
76 if (path_option) {
77 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
78 if (ignore_option)
79 handle_ignore_submodules_arg(diffopt, ignore_option->util);
80 else if (gitmodules_is_unmerged)
81 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
85 int submodule_config(const char *var, const char *value, void *cb)
87 if (!prefixcmp(var, "submodule."))
88 return parse_submodule_config_option(var, value);
89 else if (!strcmp(var, "fetch.recursesubmodules")) {
90 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
91 return 0;
93 return 0;
96 void gitmodules_config(void)
98 const char *work_tree = get_git_work_tree();
99 if (work_tree) {
100 struct strbuf gitmodules_path = STRBUF_INIT;
101 int pos;
102 strbuf_addstr(&gitmodules_path, work_tree);
103 strbuf_addstr(&gitmodules_path, "/.gitmodules");
104 if (read_cache() < 0)
105 die("index file corrupt");
106 pos = cache_name_pos(".gitmodules", 11);
107 if (pos < 0) { /* .gitmodules not found or isn't merged */
108 pos = -1 - pos;
109 if (active_nr > pos) { /* there is a .gitmodules */
110 const struct cache_entry *ce = active_cache[pos];
111 if (ce_namelen(ce) == 11 &&
112 !memcmp(ce->name, ".gitmodules", 11))
113 gitmodules_is_unmerged = 1;
117 if (!gitmodules_is_unmerged)
118 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
119 strbuf_release(&gitmodules_path);
123 int parse_submodule_config_option(const char *var, const char *value)
125 int len;
126 struct string_list_item *config;
127 struct strbuf submodname = STRBUF_INIT;
129 var += 10; /* Skip "submodule." */
131 len = strlen(var);
132 if ((len > 5) && !strcmp(var + len - 5, ".path")) {
133 strbuf_add(&submodname, var, len - 5);
134 config = unsorted_string_list_lookup(&config_name_for_path, value);
135 if (config)
136 free(config->util);
137 else
138 config = string_list_append(&config_name_for_path, xstrdup(value));
139 config->util = strbuf_detach(&submodname, NULL);
140 strbuf_release(&submodname);
141 } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
142 strbuf_add(&submodname, var, len - 23);
143 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
144 if (!config)
145 config = string_list_append(&config_fetch_recurse_submodules_for_name,
146 strbuf_detach(&submodname, NULL));
147 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
148 strbuf_release(&submodname);
149 } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
150 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
151 strcmp(value, "all") && strcmp(value, "none")) {
152 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
153 return 0;
156 strbuf_add(&submodname, var, len - 7);
157 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
158 if (config)
159 free(config->util);
160 else
161 config = string_list_append(&config_ignore_for_name,
162 strbuf_detach(&submodname, NULL));
163 strbuf_release(&submodname);
164 config->util = xstrdup(value);
165 return 0;
167 return 0;
170 void handle_ignore_submodules_arg(struct diff_options *diffopt,
171 const char *arg)
173 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
174 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
175 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
177 if (!strcmp(arg, "all"))
178 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
179 else if (!strcmp(arg, "untracked"))
180 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
181 else if (!strcmp(arg, "dirty"))
182 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
183 else if (strcmp(arg, "none"))
184 die("bad --ignore-submodules argument: %s", arg);
187 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
188 struct commit *left, struct commit *right,
189 int *fast_forward, int *fast_backward)
191 struct commit_list *merge_bases, *list;
193 init_revisions(rev, NULL);
194 setup_revisions(0, NULL, rev, NULL);
195 rev->left_right = 1;
196 rev->first_parent_only = 1;
197 left->object.flags |= SYMMETRIC_LEFT;
198 add_pending_object(rev, &left->object, path);
199 add_pending_object(rev, &right->object, path);
200 merge_bases = get_merge_bases(left, right, 1);
201 if (merge_bases) {
202 if (merge_bases->item == left)
203 *fast_forward = 1;
204 else if (merge_bases->item == right)
205 *fast_backward = 1;
207 for (list = merge_bases; list; list = list->next) {
208 list->item->object.flags |= UNINTERESTING;
209 add_pending_object(rev, &list->item->object,
210 sha1_to_hex(list->item->object.sha1));
212 return prepare_revision_walk(rev);
215 static void print_submodule_summary(struct rev_info *rev, FILE *f,
216 const char *del, const char *add, const char *reset)
218 static const char format[] = " %m %s";
219 struct strbuf sb = STRBUF_INIT;
220 struct commit *commit;
222 while ((commit = get_revision(rev))) {
223 struct pretty_print_context ctx = {0};
224 ctx.date_mode = rev->date_mode;
225 strbuf_setlen(&sb, 0);
226 if (commit->object.flags & SYMMETRIC_LEFT) {
227 if (del)
228 strbuf_addstr(&sb, del);
230 else if (add)
231 strbuf_addstr(&sb, add);
232 format_commit_message(commit, format, &sb, &ctx);
233 if (reset)
234 strbuf_addstr(&sb, reset);
235 strbuf_addch(&sb, '\n');
236 fprintf(f, "%s", sb.buf);
238 strbuf_release(&sb);
241 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
243 switch (git_config_maybe_bool(opt, arg)) {
244 case 1:
245 return RECURSE_SUBMODULES_ON;
246 case 0:
247 return RECURSE_SUBMODULES_OFF;
248 default:
249 if (!strcmp(arg, "on-demand"))
250 return RECURSE_SUBMODULES_ON_DEMAND;
251 die("bad %s argument: %s", opt, arg);
255 void show_submodule_summary(FILE *f, const char *path,
256 unsigned char one[20], unsigned char two[20],
257 unsigned dirty_submodule,
258 const char *del, const char *add, const char *reset)
260 struct rev_info rev;
261 struct commit *left = left, *right = right;
262 const char *message = NULL;
263 struct strbuf sb = STRBUF_INIT;
264 int fast_forward = 0, fast_backward = 0;
266 if (is_null_sha1(two))
267 message = "(submodule deleted)";
268 else if (add_submodule_odb(path))
269 message = "(not checked out)";
270 else if (is_null_sha1(one))
271 message = "(new submodule)";
272 else if (!(left = lookup_commit_reference(one)) ||
273 !(right = lookup_commit_reference(two)))
274 message = "(commits not present)";
276 if (!message &&
277 prepare_submodule_summary(&rev, path, left, right,
278 &fast_forward, &fast_backward))
279 message = "(revision walker failed)";
281 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
282 fprintf(f, "Submodule %s contains untracked content\n", path);
283 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
284 fprintf(f, "Submodule %s contains modified content\n", path);
286 if (!hashcmp(one, two)) {
287 strbuf_release(&sb);
288 return;
291 strbuf_addf(&sb, "Submodule %s %s..", path,
292 find_unique_abbrev(one, DEFAULT_ABBREV));
293 if (!fast_backward && !fast_forward)
294 strbuf_addch(&sb, '.');
295 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
296 if (message)
297 strbuf_addf(&sb, " %s\n", message);
298 else
299 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
300 fwrite(sb.buf, sb.len, 1, f);
302 if (!message) {
303 print_submodule_summary(&rev, f, del, add, reset);
304 clear_commit_marks(left, ~0);
305 clear_commit_marks(right, ~0);
308 strbuf_release(&sb);
311 void set_config_fetch_recurse_submodules(int value)
313 config_fetch_recurse_submodules = value;
316 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
318 return 1;
321 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
323 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
324 return 0;
326 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
327 struct child_process cp;
328 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
329 struct strbuf buf = STRBUF_INIT;
330 int needs_pushing = 0;
332 argv[1] = sha1_to_hex(sha1);
333 memset(&cp, 0, sizeof(cp));
334 cp.argv = argv;
335 cp.env = local_repo_env;
336 cp.git_cmd = 1;
337 cp.no_stdin = 1;
338 cp.out = -1;
339 cp.dir = path;
340 if (start_command(&cp))
341 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
342 sha1_to_hex(sha1), path);
343 if (strbuf_read(&buf, cp.out, 41))
344 needs_pushing = 1;
345 finish_command(&cp);
346 close(cp.out);
347 strbuf_release(&buf);
348 return needs_pushing;
351 return 0;
354 static void collect_submodules_from_diff(struct diff_queue_struct *q,
355 struct diff_options *options,
356 void *data)
358 int i;
359 int *needs_pushing = data;
361 for (i = 0; i < q->nr; i++) {
362 struct diff_filepair *p = q->queue[i];
363 if (!S_ISGITLINK(p->two->mode))
364 continue;
365 if (submodule_needs_pushing(p->two->path, p->two->sha1)) {
366 *needs_pushing = 1;
367 break;
373 static void commit_need_pushing(struct commit *commit, struct commit_list *parent, int *needs_pushing)
375 const unsigned char (*parents)[20];
376 unsigned int i, n;
377 struct rev_info rev;
379 n = commit_list_count(parent);
380 parents = xmalloc(n * sizeof(*parents));
382 for (i = 0; i < n; i++) {
383 hashcpy((unsigned char *)(parents + i), parent->item->object.sha1);
384 parent = parent->next;
387 init_revisions(&rev, NULL);
388 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
389 rev.diffopt.format_callback = collect_submodules_from_diff;
390 rev.diffopt.format_callback_data = needs_pushing;
391 diff_tree_combined(commit->object.sha1, parents, n, 1, &rev);
393 free(parents);
396 int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
398 struct rev_info rev;
399 struct commit *commit;
400 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
401 int argc = ARRAY_SIZE(argv) - 1;
402 char *sha1_copy;
403 int needs_pushing = 0;
404 struct strbuf remotes_arg = STRBUF_INIT;
406 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
407 init_revisions(&rev, NULL);
408 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
409 argv[1] = sha1_copy;
410 argv[3] = remotes_arg.buf;
411 setup_revisions(argc, argv, &rev, NULL);
412 if (prepare_revision_walk(&rev))
413 die("revision walk setup failed");
415 while ((commit = get_revision(&rev)) && !needs_pushing)
416 commit_need_pushing(commit, commit->parents, &needs_pushing);
418 free(sha1_copy);
419 strbuf_release(&remotes_arg);
421 return needs_pushing;
424 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
426 int is_present = 0;
427 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
428 /* Even if the submodule is checked out and the commit is
429 * present, make sure it is reachable from a ref. */
430 struct child_process cp;
431 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
432 struct strbuf buf = STRBUF_INIT;
434 argv[3] = sha1_to_hex(sha1);
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.out = -1;
441 cp.dir = path;
442 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
443 is_present = 1;
445 close(cp.out);
446 strbuf_release(&buf);
448 return is_present;
451 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
452 struct diff_options *options,
453 void *data)
455 int i;
456 for (i = 0; i < q->nr; i++) {
457 struct diff_filepair *p = q->queue[i];
458 if (!S_ISGITLINK(p->two->mode))
459 continue;
461 if (S_ISGITLINK(p->one->mode)) {
462 /* NEEDSWORK: We should honor the name configured in
463 * the .gitmodules file of the commit we are examining
464 * here to be able to correctly follow submodules
465 * being moved around. */
466 struct string_list_item *path;
467 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
468 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
469 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
470 } else {
471 /* Submodule is new or was moved here */
472 /* NEEDSWORK: When the .git directories of submodules
473 * live inside the superprojects .git directory some
474 * day we should fetch new submodules directly into
475 * that location too when config or options request
476 * that so they can be checked out from there. */
477 continue;
482 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
483 int flags, void *data)
485 sha1_array_append(data, sha1);
486 return 0;
489 void check_for_new_submodule_commits(unsigned char new_sha1[20])
491 if (!initialized_fetch_ref_tips) {
492 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
493 initialized_fetch_ref_tips = 1;
496 sha1_array_append(&ref_tips_after_fetch, new_sha1);
499 struct argv_array {
500 const char **argv;
501 unsigned int argc;
502 unsigned int alloc;
505 static void init_argv(struct argv_array *array)
507 array->argv = NULL;
508 array->argc = 0;
509 array->alloc = 0;
512 static void push_argv(struct argv_array *array, const char *value)
514 ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
515 array->argv[array->argc++] = xstrdup(value);
516 array->argv[array->argc] = NULL;
519 static void clear_argv(struct argv_array *array)
521 int i;
522 for (i = 0; i < array->argc; i++)
523 free((char **)array->argv[i]);
524 free(array->argv);
525 init_argv(array);
528 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
530 push_argv(data, sha1_to_hex(sha1));
533 static void calculate_changed_submodule_paths(void)
535 struct rev_info rev;
536 struct commit *commit;
537 struct argv_array argv;
539 /* No need to check if there are no submodules configured */
540 if (!config_name_for_path.nr)
541 return;
543 init_revisions(&rev, NULL);
544 init_argv(&argv);
545 push_argv(&argv, "--"); /* argv[0] program name */
546 sha1_array_for_each_unique(&ref_tips_after_fetch,
547 add_sha1_to_argv, &argv);
548 push_argv(&argv, "--not");
549 sha1_array_for_each_unique(&ref_tips_before_fetch,
550 add_sha1_to_argv, &argv);
551 setup_revisions(argv.argc, argv.argv, &rev, NULL);
552 if (prepare_revision_walk(&rev))
553 die("revision walk setup failed");
556 * Collect all submodules (whether checked out or not) for which new
557 * commits have been recorded upstream in "changed_submodule_paths".
559 while ((commit = get_revision(&rev))) {
560 struct commit_list *parent = commit->parents;
561 while (parent) {
562 struct diff_options diff_opts;
563 diff_setup(&diff_opts);
564 DIFF_OPT_SET(&diff_opts, RECURSIVE);
565 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
566 diff_opts.format_callback = submodule_collect_changed_cb;
567 if (diff_setup_done(&diff_opts) < 0)
568 die("diff_setup_done failed");
569 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
570 diffcore_std(&diff_opts);
571 diff_flush(&diff_opts);
572 parent = parent->next;
576 clear_argv(&argv);
577 sha1_array_clear(&ref_tips_before_fetch);
578 sha1_array_clear(&ref_tips_after_fetch);
579 initialized_fetch_ref_tips = 0;
582 int fetch_populated_submodules(int num_options, const char **options,
583 const char *prefix, int command_line_option,
584 int quiet)
586 int i, result = 0, argc = 0, default_argc;
587 struct child_process cp;
588 const char **argv;
589 struct string_list_item *name_for_path;
590 const char *work_tree = get_git_work_tree();
591 if (!work_tree)
592 goto out;
594 if (!the_index.initialized)
595 if (read_cache() < 0)
596 die("index file corrupt");
598 /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
599 argv = xcalloc(num_options + 6, sizeof(const char *));
600 argv[argc++] = "fetch";
601 for (i = 0; i < num_options; i++)
602 argv[argc++] = options[i];
603 argv[argc++] = "--recurse-submodules-default";
604 default_argc = argc++;
605 argv[argc++] = "--submodule-prefix";
607 memset(&cp, 0, sizeof(cp));
608 cp.argv = argv;
609 cp.env = local_repo_env;
610 cp.git_cmd = 1;
611 cp.no_stdin = 1;
613 calculate_changed_submodule_paths();
615 for (i = 0; i < active_nr; i++) {
616 struct strbuf submodule_path = STRBUF_INIT;
617 struct strbuf submodule_git_dir = STRBUF_INIT;
618 struct strbuf submodule_prefix = STRBUF_INIT;
619 struct cache_entry *ce = active_cache[i];
620 const char *git_dir, *name, *default_argv;
622 if (!S_ISGITLINK(ce->ce_mode))
623 continue;
625 name = ce->name;
626 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
627 if (name_for_path)
628 name = name_for_path->util;
630 default_argv = "yes";
631 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
632 struct string_list_item *fetch_recurse_submodules_option;
633 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
634 if (fetch_recurse_submodules_option) {
635 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
636 continue;
637 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
638 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
639 continue;
640 default_argv = "on-demand";
642 } else {
643 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
644 gitmodules_is_unmerged)
645 continue;
646 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
647 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
648 continue;
649 default_argv = "on-demand";
652 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
653 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
654 continue;
655 default_argv = "on-demand";
658 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
659 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
660 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
661 git_dir = read_gitfile(submodule_git_dir.buf);
662 if (!git_dir)
663 git_dir = submodule_git_dir.buf;
664 if (is_directory(git_dir)) {
665 if (!quiet)
666 printf("Fetching submodule %s%s\n", prefix, ce->name);
667 cp.dir = submodule_path.buf;
668 argv[default_argc] = default_argv;
669 argv[argc] = submodule_prefix.buf;
670 if (run_command(&cp))
671 result = 1;
673 strbuf_release(&submodule_path);
674 strbuf_release(&submodule_git_dir);
675 strbuf_release(&submodule_prefix);
677 free(argv);
678 out:
679 string_list_clear(&changed_submodule_paths, 1);
680 return result;
683 unsigned is_submodule_modified(const char *path, int ignore_untracked)
685 ssize_t len;
686 struct child_process cp;
687 const char *argv[] = {
688 "status",
689 "--porcelain",
690 NULL,
691 NULL,
693 struct strbuf buf = STRBUF_INIT;
694 unsigned dirty_submodule = 0;
695 const char *line, *next_line;
696 const char *git_dir;
698 strbuf_addf(&buf, "%s/.git", path);
699 git_dir = read_gitfile(buf.buf);
700 if (!git_dir)
701 git_dir = buf.buf;
702 if (!is_directory(git_dir)) {
703 strbuf_release(&buf);
704 /* The submodule is not checked out, so it is not modified */
705 return 0;
708 strbuf_reset(&buf);
710 if (ignore_untracked)
711 argv[2] = "-uno";
713 memset(&cp, 0, sizeof(cp));
714 cp.argv = argv;
715 cp.env = local_repo_env;
716 cp.git_cmd = 1;
717 cp.no_stdin = 1;
718 cp.out = -1;
719 cp.dir = path;
720 if (start_command(&cp))
721 die("Could not run git status --porcelain");
723 len = strbuf_read(&buf, cp.out, 1024);
724 line = buf.buf;
725 while (len > 2) {
726 if ((line[0] == '?') && (line[1] == '?')) {
727 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
728 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
729 break;
730 } else {
731 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
732 if (ignore_untracked ||
733 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
734 break;
736 next_line = strchr(line, '\n');
737 if (!next_line)
738 break;
739 next_line++;
740 len -= (next_line - line);
741 line = next_line;
743 close(cp.out);
745 if (finish_command(&cp))
746 die("git status --porcelain failed");
748 strbuf_release(&buf);
749 return dirty_submodule;
752 static int find_first_merges(struct object_array *result, const char *path,
753 struct commit *a, struct commit *b)
755 int i, j;
756 struct object_array merges;
757 struct commit *commit;
758 int contains_another;
760 char merged_revision[42];
761 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
762 "--all", merged_revision, NULL };
763 struct rev_info revs;
764 struct setup_revision_opt rev_opts;
766 memset(&merges, 0, sizeof(merges));
767 memset(result, 0, sizeof(struct object_array));
768 memset(&rev_opts, 0, sizeof(rev_opts));
770 /* get all revisions that merge commit a */
771 snprintf(merged_revision, sizeof(merged_revision), "^%s",
772 sha1_to_hex(a->object.sha1));
773 init_revisions(&revs, NULL);
774 rev_opts.submodule = path;
775 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
777 /* save all revisions from the above list that contain b */
778 if (prepare_revision_walk(&revs))
779 die("revision walk setup failed");
780 while ((commit = get_revision(&revs)) != NULL) {
781 struct object *o = &(commit->object);
782 if (in_merge_bases(b, &commit, 1))
783 add_object_array(o, NULL, &merges);
786 /* Now we've got all merges that contain a and b. Prune all
787 * merges that contain another found merge and save them in
788 * result.
790 for (i = 0; i < merges.nr; i++) {
791 struct commit *m1 = (struct commit *) merges.objects[i].item;
793 contains_another = 0;
794 for (j = 0; j < merges.nr; j++) {
795 struct commit *m2 = (struct commit *) merges.objects[j].item;
796 if (i != j && in_merge_bases(m2, &m1, 1)) {
797 contains_another = 1;
798 break;
802 if (!contains_another)
803 add_object_array(merges.objects[i].item,
804 merges.objects[i].name, result);
807 free(merges.objects);
808 return result->nr;
811 static void print_commit(struct commit *commit)
813 struct strbuf sb = STRBUF_INIT;
814 struct pretty_print_context ctx = {0};
815 ctx.date_mode = DATE_NORMAL;
816 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
817 fprintf(stderr, "%s\n", sb.buf);
818 strbuf_release(&sb);
821 #define MERGE_WARNING(path, msg) \
822 warning("Failed to merge submodule %s (%s)", path, msg);
824 int merge_submodule(unsigned char result[20], const char *path,
825 const unsigned char base[20], const unsigned char a[20],
826 const unsigned char b[20])
828 struct commit *commit_base, *commit_a, *commit_b;
829 int parent_count;
830 struct object_array merges;
832 int i;
834 /* store a in result in case we fail */
835 hashcpy(result, a);
837 /* we can not handle deletion conflicts */
838 if (is_null_sha1(base))
839 return 0;
840 if (is_null_sha1(a))
841 return 0;
842 if (is_null_sha1(b))
843 return 0;
845 if (add_submodule_odb(path)) {
846 MERGE_WARNING(path, "not checked out");
847 return 0;
850 if (!(commit_base = lookup_commit_reference(base)) ||
851 !(commit_a = lookup_commit_reference(a)) ||
852 !(commit_b = lookup_commit_reference(b))) {
853 MERGE_WARNING(path, "commits not present");
854 return 0;
857 /* check whether both changes are forward */
858 if (!in_merge_bases(commit_base, &commit_a, 1) ||
859 !in_merge_bases(commit_base, &commit_b, 1)) {
860 MERGE_WARNING(path, "commits don't follow merge-base");
861 return 0;
864 /* Case #1: a is contained in b or vice versa */
865 if (in_merge_bases(commit_a, &commit_b, 1)) {
866 hashcpy(result, b);
867 return 1;
869 if (in_merge_bases(commit_b, &commit_a, 1)) {
870 hashcpy(result, a);
871 return 1;
875 * Case #2: There are one or more merges that contain a and b in
876 * the submodule. If there is only one, then present it as a
877 * suggestion to the user, but leave it marked unmerged so the
878 * user needs to confirm the resolution.
881 /* find commit which merges them */
882 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
883 switch (parent_count) {
884 case 0:
885 MERGE_WARNING(path, "merge following commits not found");
886 break;
888 case 1:
889 MERGE_WARNING(path, "not fast-forward");
890 fprintf(stderr, "Found a possible merge resolution "
891 "for the submodule:\n");
892 print_commit((struct commit *) merges.objects[0].item);
893 fprintf(stderr,
894 "If this is correct simply add it to the index "
895 "for example\n"
896 "by using:\n\n"
897 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
898 "which will accept this suggestion.\n",
899 sha1_to_hex(merges.objects[0].item->sha1), path);
900 break;
902 default:
903 MERGE_WARNING(path, "multiple merges found");
904 for (i = 0; i < merges.nr; i++)
905 print_commit((struct commit *) merges.objects[i].item);
908 free(merges.objects);
909 return 0;