submodule: Fix t7400, t7405, t7406 for msysGit
[git/dscho.git] / submodule.c
blob784b58039dd078fc1e0c4554820cd0e99c8e41d2
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;
66 prepare_alt_odb();
67 done:
68 strbuf_release(&objects_directory);
69 return ret;
72 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
73 const char *path)
75 struct string_list_item *path_option, *ignore_option;
76 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
77 if (path_option) {
78 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
79 if (ignore_option)
80 handle_ignore_submodules_arg(diffopt, ignore_option->util);
81 else if (gitmodules_is_unmerged)
82 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
86 int submodule_config(const char *var, const char *value, void *cb)
88 if (!prefixcmp(var, "submodule."))
89 return parse_submodule_config_option(var, value);
90 else if (!strcmp(var, "fetch.recursesubmodules")) {
91 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
92 return 0;
94 return 0;
97 void gitmodules_config(void)
99 const char *work_tree = get_git_work_tree();
100 if (work_tree) {
101 struct strbuf gitmodules_path = STRBUF_INIT;
102 int pos;
103 strbuf_addstr(&gitmodules_path, work_tree);
104 strbuf_addstr(&gitmodules_path, "/.gitmodules");
105 if (read_cache() < 0)
106 die("index file corrupt");
107 pos = cache_name_pos(".gitmodules", 11);
108 if (pos < 0) { /* .gitmodules not found or isn't merged */
109 pos = -1 - pos;
110 if (active_nr > pos) { /* there is a .gitmodules */
111 const struct cache_entry *ce = active_cache[pos];
112 if (ce_namelen(ce) == 11 &&
113 !memcmp(ce->name, ".gitmodules", 11))
114 gitmodules_is_unmerged = 1;
118 if (!gitmodules_is_unmerged)
119 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
120 strbuf_release(&gitmodules_path);
124 int parse_submodule_config_option(const char *var, const char *value)
126 int len;
127 struct string_list_item *config;
128 struct strbuf submodname = STRBUF_INIT;
130 var += 10; /* Skip "submodule." */
132 len = strlen(var);
133 if ((len > 5) && !strcmp(var + len - 5, ".path")) {
134 strbuf_add(&submodname, var, len - 5);
135 config = unsorted_string_list_lookup(&config_name_for_path, value);
136 if (config)
137 free(config->util);
138 else
139 config = string_list_append(&config_name_for_path, xstrdup(value));
140 config->util = strbuf_detach(&submodname, NULL);
141 strbuf_release(&submodname);
142 } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
143 strbuf_add(&submodname, var, len - 23);
144 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
145 if (!config)
146 config = string_list_append(&config_fetch_recurse_submodules_for_name,
147 strbuf_detach(&submodname, NULL));
148 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
149 strbuf_release(&submodname);
150 } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
151 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
152 strcmp(value, "all") && strcmp(value, "none")) {
153 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
154 return 0;
157 strbuf_add(&submodname, var, len - 7);
158 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
159 if (config)
160 free(config->util);
161 else
162 config = string_list_append(&config_ignore_for_name,
163 strbuf_detach(&submodname, NULL));
164 strbuf_release(&submodname);
165 config->util = xstrdup(value);
166 return 0;
168 return 0;
171 void handle_ignore_submodules_arg(struct diff_options *diffopt,
172 const char *arg)
174 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
175 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
176 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
178 if (!strcmp(arg, "all"))
179 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
180 else if (!strcmp(arg, "untracked"))
181 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
182 else if (!strcmp(arg, "dirty"))
183 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
184 else if (strcmp(arg, "none"))
185 die("bad --ignore-submodules argument: %s", arg);
188 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
189 struct commit *left, struct commit *right,
190 int *fast_forward, int *fast_backward)
192 struct commit_list *merge_bases, *list;
194 init_revisions(rev, NULL);
195 setup_revisions(0, NULL, rev, NULL);
196 rev->left_right = 1;
197 rev->first_parent_only = 1;
198 left->object.flags |= SYMMETRIC_LEFT;
199 add_pending_object(rev, &left->object, path);
200 add_pending_object(rev, &right->object, path);
201 merge_bases = get_merge_bases(left, right, 1);
202 if (merge_bases) {
203 if (merge_bases->item == left)
204 *fast_forward = 1;
205 else if (merge_bases->item == right)
206 *fast_backward = 1;
208 for (list = merge_bases; list; list = list->next) {
209 list->item->object.flags |= UNINTERESTING;
210 add_pending_object(rev, &list->item->object,
211 sha1_to_hex(list->item->object.sha1));
213 return prepare_revision_walk(rev);
216 static void print_submodule_summary(struct rev_info *rev, FILE *f,
217 const char *del, const char *add, const char *reset)
219 static const char format[] = " %m %s";
220 struct strbuf sb = STRBUF_INIT;
221 struct commit *commit;
223 while ((commit = get_revision(rev))) {
224 struct pretty_print_context ctx = {0};
225 ctx.date_mode = rev->date_mode;
226 strbuf_setlen(&sb, 0);
227 if (commit->object.flags & SYMMETRIC_LEFT) {
228 if (del)
229 strbuf_addstr(&sb, del);
231 else if (add)
232 strbuf_addstr(&sb, add);
233 format_commit_message(commit, format, &sb, &ctx);
234 if (reset)
235 strbuf_addstr(&sb, reset);
236 strbuf_addch(&sb, '\n');
237 fprintf(f, "%s", sb.buf);
239 strbuf_release(&sb);
242 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
244 switch (git_config_maybe_bool(opt, arg)) {
245 case 1:
246 return RECURSE_SUBMODULES_ON;
247 case 0:
248 return RECURSE_SUBMODULES_OFF;
249 default:
250 if (!strcmp(arg, "on-demand"))
251 return RECURSE_SUBMODULES_ON_DEMAND;
252 die("bad %s argument: %s", opt, arg);
256 void show_submodule_summary(FILE *f, const char *path,
257 unsigned char one[20], unsigned char two[20],
258 unsigned dirty_submodule,
259 const char *del, const char *add, const char *reset)
261 struct rev_info rev;
262 struct commit *left = left, *right = right;
263 const char *message = NULL;
264 struct strbuf sb = STRBUF_INIT;
265 int fast_forward = 0, fast_backward = 0;
267 if (is_null_sha1(two))
268 message = "(submodule deleted)";
269 else if (add_submodule_odb(path))
270 message = "(not checked out)";
271 else if (is_null_sha1(one))
272 message = "(new submodule)";
273 else if (!(left = lookup_commit_reference(one)) ||
274 !(right = lookup_commit_reference(two)))
275 message = "(commits not present)";
277 if (!message &&
278 prepare_submodule_summary(&rev, path, left, right,
279 &fast_forward, &fast_backward))
280 message = "(revision walker failed)";
282 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
283 fprintf(f, "Submodule %s contains untracked content\n", path);
284 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
285 fprintf(f, "Submodule %s contains modified content\n", path);
287 if (!hashcmp(one, two)) {
288 strbuf_release(&sb);
289 return;
292 strbuf_addf(&sb, "Submodule %s %s..", path,
293 find_unique_abbrev(one, DEFAULT_ABBREV));
294 if (!fast_backward && !fast_forward)
295 strbuf_addch(&sb, '.');
296 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
297 if (message)
298 strbuf_addf(&sb, " %s\n", message);
299 else
300 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
301 fwrite(sb.buf, sb.len, 1, f);
303 if (!message) {
304 print_submodule_summary(&rev, f, del, add, reset);
305 clear_commit_marks(left, ~0);
306 clear_commit_marks(right, ~0);
309 strbuf_release(&sb);
312 void set_config_fetch_recurse_submodules(int value)
314 config_fetch_recurse_submodules = value;
317 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
319 return 1;
322 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
324 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
325 return 0;
327 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
328 struct child_process cp;
329 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
330 struct strbuf buf = STRBUF_INIT;
331 int needs_pushing = 0;
333 argv[1] = sha1_to_hex(sha1);
334 memset(&cp, 0, sizeof(cp));
335 cp.argv = argv;
336 cp.env = local_repo_env;
337 cp.git_cmd = 1;
338 cp.no_stdin = 1;
339 cp.out = -1;
340 cp.dir = path;
341 if (start_command(&cp))
342 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
343 sha1_to_hex(sha1), path);
344 if (strbuf_read(&buf, cp.out, 41))
345 needs_pushing = 1;
346 finish_command(&cp);
347 close(cp.out);
348 strbuf_release(&buf);
349 return needs_pushing;
352 return 0;
355 static void collect_submodules_from_diff(struct diff_queue_struct *q,
356 struct diff_options *options,
357 void *data)
359 int i;
360 struct string_list *needs_pushing = data;
362 for (i = 0; i < q->nr; i++) {
363 struct diff_filepair *p = q->queue[i];
364 if (!S_ISGITLINK(p->two->mode))
365 continue;
366 if (submodule_needs_pushing(p->two->path, p->two->sha1))
367 string_list_insert(needs_pushing, p->two->path);
371 static void find_unpushed_submodule_commits(struct commit *commit,
372 struct string_list *needs_pushing)
374 struct rev_info rev;
376 init_revisions(&rev, NULL);
377 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
378 rev.diffopt.format_callback = collect_submodules_from_diff;
379 rev.diffopt.format_callback_data = needs_pushing;
380 diff_tree_combined_merge(commit, 1, &rev);
383 int find_unpushed_submodules(unsigned char new_sha1[20],
384 const char *remotes_name, struct string_list *needs_pushing)
386 struct rev_info rev;
387 struct commit *commit;
388 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
389 int argc = ARRAY_SIZE(argv) - 1;
390 char *sha1_copy;
392 struct strbuf remotes_arg = STRBUF_INIT;
394 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
395 init_revisions(&rev, NULL);
396 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
397 argv[1] = sha1_copy;
398 argv[3] = remotes_arg.buf;
399 setup_revisions(argc, argv, &rev, NULL);
400 if (prepare_revision_walk(&rev))
401 die("revision walk setup failed");
403 while ((commit = get_revision(&rev)) != NULL)
404 find_unpushed_submodule_commits(commit, needs_pushing);
406 reset_revision_walk();
407 free(sha1_copy);
408 strbuf_release(&remotes_arg);
410 return needs_pushing->nr;
413 static int push_submodule(const char *path)
415 if (add_submodule_odb(path))
416 return 1;
418 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
419 struct child_process cp;
420 const char *argv[] = {"push", NULL};
422 memset(&cp, 0, sizeof(cp));
423 cp.argv = argv;
424 cp.env = local_repo_env;
425 cp.git_cmd = 1;
426 cp.no_stdin = 1;
427 cp.dir = path;
428 if (run_command(&cp))
429 return 0;
430 close(cp.out);
433 return 1;
436 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
438 int i, ret = 1;
439 struct string_list needs_pushing;
441 memset(&needs_pushing, 0, sizeof(struct string_list));
442 needs_pushing.strdup_strings = 1;
444 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
445 return 1;
447 for (i = 0; i < needs_pushing.nr; i++) {
448 const char *path = needs_pushing.items[i].string;
449 fprintf(stderr, "Pushing submodule '%s'\n", path);
450 if (!push_submodule(path)) {
451 fprintf(stderr, "Unable to push submodule '%s'\n", path);
452 ret = 0;
456 string_list_clear(&needs_pushing, 0);
458 return ret;
461 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
463 int is_present = 0;
464 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
465 /* Even if the submodule is checked out and the commit is
466 * present, make sure it is reachable from a ref. */
467 struct child_process cp;
468 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
469 struct strbuf buf = STRBUF_INIT;
471 argv[3] = sha1_to_hex(sha1);
472 memset(&cp, 0, sizeof(cp));
473 cp.argv = argv;
474 cp.env = local_repo_env;
475 cp.git_cmd = 1;
476 cp.no_stdin = 1;
477 cp.out = -1;
478 cp.dir = path;
479 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
480 is_present = 1;
482 close(cp.out);
483 strbuf_release(&buf);
485 return is_present;
488 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
489 struct diff_options *options,
490 void *data)
492 int i;
493 for (i = 0; i < q->nr; i++) {
494 struct diff_filepair *p = q->queue[i];
495 if (!S_ISGITLINK(p->two->mode))
496 continue;
498 if (S_ISGITLINK(p->one->mode)) {
499 /* NEEDSWORK: We should honor the name configured in
500 * the .gitmodules file of the commit we are examining
501 * here to be able to correctly follow submodules
502 * being moved around. */
503 struct string_list_item *path;
504 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
505 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
506 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
507 } else {
508 /* Submodule is new or was moved here */
509 /* NEEDSWORK: When the .git directories of submodules
510 * live inside the superprojects .git directory some
511 * day we should fetch new submodules directly into
512 * that location too when config or options request
513 * that so they can be checked out from there. */
514 continue;
519 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
520 int flags, void *data)
522 sha1_array_append(data, sha1);
523 return 0;
526 void check_for_new_submodule_commits(unsigned char new_sha1[20])
528 if (!initialized_fetch_ref_tips) {
529 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
530 initialized_fetch_ref_tips = 1;
533 sha1_array_append(&ref_tips_after_fetch, new_sha1);
536 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
538 argv_array_push(data, sha1_to_hex(sha1));
541 static void calculate_changed_submodule_paths(void)
543 struct rev_info rev;
544 struct commit *commit;
545 struct argv_array argv = ARGV_ARRAY_INIT;
547 /* No need to check if there are no submodules configured */
548 if (!config_name_for_path.nr)
549 return;
551 init_revisions(&rev, NULL);
552 argv_array_push(&argv, "--"); /* argv[0] program name */
553 sha1_array_for_each_unique(&ref_tips_after_fetch,
554 add_sha1_to_argv, &argv);
555 argv_array_push(&argv, "--not");
556 sha1_array_for_each_unique(&ref_tips_before_fetch,
557 add_sha1_to_argv, &argv);
558 setup_revisions(argv.argc, argv.argv, &rev, NULL);
559 if (prepare_revision_walk(&rev))
560 die("revision walk setup failed");
563 * Collect all submodules (whether checked out or not) for which new
564 * commits have been recorded upstream in "changed_submodule_paths".
566 while ((commit = get_revision(&rev))) {
567 struct commit_list *parent = commit->parents;
568 while (parent) {
569 struct diff_options diff_opts;
570 diff_setup(&diff_opts);
571 DIFF_OPT_SET(&diff_opts, RECURSIVE);
572 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
573 diff_opts.format_callback = submodule_collect_changed_cb;
574 if (diff_setup_done(&diff_opts) < 0)
575 die("diff_setup_done failed");
576 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
577 diffcore_std(&diff_opts);
578 diff_flush(&diff_opts);
579 parent = parent->next;
583 argv_array_clear(&argv);
584 sha1_array_clear(&ref_tips_before_fetch);
585 sha1_array_clear(&ref_tips_after_fetch);
586 initialized_fetch_ref_tips = 0;
589 int fetch_populated_submodules(int num_options, const char **options,
590 const char *prefix, int command_line_option,
591 int quiet)
593 int i, result = 0, argc = 0, default_argc;
594 struct child_process cp;
595 const char **argv;
596 struct string_list_item *name_for_path;
597 const char *work_tree = get_git_work_tree();
598 if (!work_tree)
599 goto out;
601 if (!the_index.initialized)
602 if (read_cache() < 0)
603 die("index file corrupt");
605 /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
606 argv = xcalloc(num_options + 6, sizeof(const char *));
607 argv[argc++] = "fetch";
608 for (i = 0; i < num_options; i++)
609 argv[argc++] = options[i];
610 argv[argc++] = "--recurse-submodules-default";
611 default_argc = argc++;
612 argv[argc++] = "--submodule-prefix";
614 memset(&cp, 0, sizeof(cp));
615 cp.argv = argv;
616 cp.env = local_repo_env;
617 cp.git_cmd = 1;
618 cp.no_stdin = 1;
620 calculate_changed_submodule_paths();
622 for (i = 0; i < active_nr; i++) {
623 struct strbuf submodule_path = STRBUF_INIT;
624 struct strbuf submodule_git_dir = STRBUF_INIT;
625 struct strbuf submodule_prefix = STRBUF_INIT;
626 struct cache_entry *ce = active_cache[i];
627 const char *git_dir, *name, *default_argv;
629 if (!S_ISGITLINK(ce->ce_mode))
630 continue;
632 name = ce->name;
633 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
634 if (name_for_path)
635 name = name_for_path->util;
637 default_argv = "yes";
638 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
639 struct string_list_item *fetch_recurse_submodules_option;
640 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
641 if (fetch_recurse_submodules_option) {
642 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
643 continue;
644 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
645 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
646 continue;
647 default_argv = "on-demand";
649 } else {
650 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
651 gitmodules_is_unmerged)
652 continue;
653 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
654 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
655 continue;
656 default_argv = "on-demand";
659 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
660 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
661 continue;
662 default_argv = "on-demand";
665 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
666 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
667 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
668 git_dir = read_gitfile(submodule_git_dir.buf);
669 if (!git_dir)
670 git_dir = submodule_git_dir.buf;
671 if (is_directory(git_dir)) {
672 if (!quiet)
673 printf("Fetching submodule %s%s\n", prefix, ce->name);
674 cp.dir = submodule_path.buf;
675 argv[default_argc] = default_argv;
676 argv[argc] = submodule_prefix.buf;
677 if (run_command(&cp))
678 result = 1;
680 strbuf_release(&submodule_path);
681 strbuf_release(&submodule_git_dir);
682 strbuf_release(&submodule_prefix);
684 free(argv);
685 out:
686 string_list_clear(&changed_submodule_paths, 1);
687 return result;
690 unsigned is_submodule_modified(const char *path, int ignore_untracked)
692 ssize_t len;
693 struct child_process cp;
694 const char *argv[] = {
695 "status",
696 "--porcelain",
697 NULL,
698 NULL,
700 struct strbuf buf = STRBUF_INIT;
701 unsigned dirty_submodule = 0;
702 const char *line, *next_line;
703 const char *git_dir;
705 strbuf_addf(&buf, "%s/.git", path);
706 git_dir = read_gitfile(buf.buf);
707 if (!git_dir)
708 git_dir = buf.buf;
709 if (!is_directory(git_dir)) {
710 strbuf_release(&buf);
711 /* The submodule is not checked out, so it is not modified */
712 return 0;
715 strbuf_reset(&buf);
717 if (ignore_untracked)
718 argv[2] = "-uno";
720 memset(&cp, 0, sizeof(cp));
721 cp.argv = argv;
722 cp.env = local_repo_env;
723 cp.git_cmd = 1;
724 cp.no_stdin = 1;
725 cp.out = -1;
726 cp.dir = path;
727 if (start_command(&cp))
728 die("Could not run 'git status --porcelain' in submodule %s", path);
730 len = strbuf_read(&buf, cp.out, 1024);
731 line = buf.buf;
732 while (len > 2) {
733 if ((line[0] == '?') && (line[1] == '?')) {
734 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
735 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
736 break;
737 } else {
738 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
739 if (ignore_untracked ||
740 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
741 break;
743 next_line = strchr(line, '\n');
744 if (!next_line)
745 break;
746 next_line++;
747 len -= (next_line - line);
748 line = next_line;
750 close(cp.out);
752 if (finish_command(&cp))
753 die("'git status --porcelain' failed in submodule %s", path);
755 strbuf_release(&buf);
756 return dirty_submodule;
759 static int find_first_merges(struct object_array *result, const char *path,
760 struct commit *a, struct commit *b)
762 int i, j;
763 struct object_array merges;
764 struct commit *commit;
765 int contains_another;
767 char merged_revision[42];
768 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
769 "--all", merged_revision, NULL };
770 struct rev_info revs;
771 struct setup_revision_opt rev_opts;
773 memset(&merges, 0, sizeof(merges));
774 memset(result, 0, sizeof(struct object_array));
775 memset(&rev_opts, 0, sizeof(rev_opts));
777 /* get all revisions that merge commit a */
778 snprintf(merged_revision, sizeof(merged_revision), "^%s",
779 sha1_to_hex(a->object.sha1));
780 init_revisions(&revs, NULL);
781 rev_opts.submodule = path;
782 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
784 /* save all revisions from the above list that contain b */
785 if (prepare_revision_walk(&revs))
786 die("revision walk setup failed");
787 while ((commit = get_revision(&revs)) != NULL) {
788 struct object *o = &(commit->object);
789 if (in_merge_bases(b, &commit, 1))
790 add_object_array(o, NULL, &merges);
792 reset_revision_walk();
794 /* Now we've got all merges that contain a and b. Prune all
795 * merges that contain another found merge and save them in
796 * result.
798 for (i = 0; i < merges.nr; i++) {
799 struct commit *m1 = (struct commit *) merges.objects[i].item;
801 contains_another = 0;
802 for (j = 0; j < merges.nr; j++) {
803 struct commit *m2 = (struct commit *) merges.objects[j].item;
804 if (i != j && in_merge_bases(m2, &m1, 1)) {
805 contains_another = 1;
806 break;
810 if (!contains_another)
811 add_object_array(merges.objects[i].item,
812 merges.objects[i].name, result);
815 free(merges.objects);
816 return result->nr;
819 static void print_commit(struct commit *commit)
821 struct strbuf sb = STRBUF_INIT;
822 struct pretty_print_context ctx = {0};
823 ctx.date_mode = DATE_NORMAL;
824 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
825 fprintf(stderr, "%s\n", sb.buf);
826 strbuf_release(&sb);
829 #define MERGE_WARNING(path, msg) \
830 warning("Failed to merge submodule %s (%s)", path, msg);
832 int merge_submodule(unsigned char result[20], const char *path,
833 const unsigned char base[20], const unsigned char a[20],
834 const unsigned char b[20], int search)
836 struct commit *commit_base, *commit_a, *commit_b;
837 int parent_count;
838 struct object_array merges;
840 int i;
842 /* store a in result in case we fail */
843 hashcpy(result, a);
845 /* we can not handle deletion conflicts */
846 if (is_null_sha1(base))
847 return 0;
848 if (is_null_sha1(a))
849 return 0;
850 if (is_null_sha1(b))
851 return 0;
853 if (add_submodule_odb(path)) {
854 MERGE_WARNING(path, "not checked out");
855 return 0;
858 if (!(commit_base = lookup_commit_reference(base)) ||
859 !(commit_a = lookup_commit_reference(a)) ||
860 !(commit_b = lookup_commit_reference(b))) {
861 MERGE_WARNING(path, "commits not present");
862 return 0;
865 /* check whether both changes are forward */
866 if (!in_merge_bases(commit_base, &commit_a, 1) ||
867 !in_merge_bases(commit_base, &commit_b, 1)) {
868 MERGE_WARNING(path, "commits don't follow merge-base");
869 return 0;
872 /* Case #1: a is contained in b or vice versa */
873 if (in_merge_bases(commit_a, &commit_b, 1)) {
874 hashcpy(result, b);
875 return 1;
877 if (in_merge_bases(commit_b, &commit_a, 1)) {
878 hashcpy(result, a);
879 return 1;
883 * Case #2: There are one or more merges that contain a and b in
884 * the submodule. If there is only one, then present it as a
885 * suggestion to the user, but leave it marked unmerged so the
886 * user needs to confirm the resolution.
889 /* Skip the search if makes no sense to the calling context. */
890 if (!search)
891 return 0;
893 /* find commit which merges them */
894 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
895 switch (parent_count) {
896 case 0:
897 MERGE_WARNING(path, "merge following commits not found");
898 break;
900 case 1:
901 MERGE_WARNING(path, "not fast-forward");
902 fprintf(stderr, "Found a possible merge resolution "
903 "for the submodule:\n");
904 print_commit((struct commit *) merges.objects[0].item);
905 fprintf(stderr,
906 "If this is correct simply add it to the index "
907 "for example\n"
908 "by using:\n\n"
909 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
910 "which will accept this suggestion.\n",
911 sha1_to_hex(merges.objects[0].item->sha1), path);
912 break;
914 default:
915 MERGE_WARNING(path, "multiple merges found");
916 for (i = 0; i < merges.nr; i++)
917 print_commit((struct commit *) merges.objects[i].item);
920 free(merges.objects);
921 return 0;