Code

Merge branch 'jn/gitweb-unspecified-action' into maint-1.7.8
[git.git] / submodule.c
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;
23 /*
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.
30  */
31 static int gitmodules_is_unmerged;
33 static int add_submodule_odb(const char *path)
34 {
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);
45         }
46         strbuf_addstr(&objects_directory, "/objects/");
47         if (!is_directory(objects_directory.buf)) {
48                 ret = -1;
49                 goto done;
50         }
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;
70 }
72 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
73                                              const char *path)
74 {
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);
83         }
84 }
86 int submodule_config(const char *var, const char *value, void *cb)
87 {
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;
93         }
94         return 0;
95 }
97 void gitmodules_config(void)
98 {
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;
115                         }
116                 }
118                 if (!gitmodules_is_unmerged)
119                         git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
120                 strbuf_release(&gitmodules_path);
121         }
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;
155                 }
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;
167         }
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;
207         }
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));
212         }
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);
230                 }
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);
238         }
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);
253         }
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;
290         }
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);
307         }
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;
350         }
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         int *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                         *needs_pushing = 1;
368                         break;
369                 }
370         }
374 static void commit_need_pushing(struct commit *commit, struct commit_list *parent, int *needs_pushing)
376         const unsigned char (*parents)[20];
377         unsigned int i, n;
378         struct rev_info rev;
380         n = commit_list_count(parent);
381         parents = xmalloc(n * sizeof(*parents));
383         for (i = 0; i < n; i++) {
384                 hashcpy((unsigned char *)(parents + i), parent->item->object.sha1);
385                 parent = parent->next;
386         }
388         init_revisions(&rev, NULL);
389         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
390         rev.diffopt.format_callback = collect_submodules_from_diff;
391         rev.diffopt.format_callback_data = needs_pushing;
392         diff_tree_combined(commit->object.sha1, parents, n, 1, &rev);
394         free((void *)parents);
397 int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
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;
404         int needs_pushing = 0;
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)) && !needs_pushing)
417                 commit_need_pushing(commit, commit->parents, &needs_pushing);
419         free(sha1_copy);
420         strbuf_release(&remotes_arg);
422         return needs_pushing;
425 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
427         int is_present = 0;
428         if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
429                 /* Even if the submodule is checked out and the commit is
430                  * present, make sure it is reachable from a ref. */
431                 struct child_process cp;
432                 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
433                 struct strbuf buf = STRBUF_INIT;
435                 argv[3] = sha1_to_hex(sha1);
436                 memset(&cp, 0, sizeof(cp));
437                 cp.argv = argv;
438                 cp.env = local_repo_env;
439                 cp.git_cmd = 1;
440                 cp.no_stdin = 1;
441                 cp.out = -1;
442                 cp.dir = path;
443                 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
444                         is_present = 1;
446                 close(cp.out);
447                 strbuf_release(&buf);
448         }
449         return is_present;
452 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
453                                          struct diff_options *options,
454                                          void *data)
456         int i;
457         for (i = 0; i < q->nr; i++) {
458                 struct diff_filepair *p = q->queue[i];
459                 if (!S_ISGITLINK(p->two->mode))
460                         continue;
462                 if (S_ISGITLINK(p->one->mode)) {
463                         /* NEEDSWORK: We should honor the name configured in
464                          * the .gitmodules file of the commit we are examining
465                          * here to be able to correctly follow submodules
466                          * being moved around. */
467                         struct string_list_item *path;
468                         path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
469                         if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
470                                 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
471                 } else {
472                         /* Submodule is new or was moved here */
473                         /* NEEDSWORK: When the .git directories of submodules
474                          * live inside the superprojects .git directory some
475                          * day we should fetch new submodules directly into
476                          * that location too when config or options request
477                          * that so they can be checked out from there. */
478                         continue;
479                 }
480         }
483 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
484                              int flags, void *data)
486         sha1_array_append(data, sha1);
487         return 0;
490 void check_for_new_submodule_commits(unsigned char new_sha1[20])
492         if (!initialized_fetch_ref_tips) {
493                 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
494                 initialized_fetch_ref_tips = 1;
495         }
497         sha1_array_append(&ref_tips_after_fetch, new_sha1);
500 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
502         argv_array_push(data, sha1_to_hex(sha1));
505 static void calculate_changed_submodule_paths(void)
507         struct rev_info rev;
508         struct commit *commit;
509         struct argv_array argv = ARGV_ARRAY_INIT;
511         /* No need to check if there are no submodules configured */
512         if (!config_name_for_path.nr)
513                 return;
515         init_revisions(&rev, NULL);
516         argv_array_push(&argv, "--"); /* argv[0] program name */
517         sha1_array_for_each_unique(&ref_tips_after_fetch,
518                                    add_sha1_to_argv, &argv);
519         argv_array_push(&argv, "--not");
520         sha1_array_for_each_unique(&ref_tips_before_fetch,
521                                    add_sha1_to_argv, &argv);
522         setup_revisions(argv.argc, argv.argv, &rev, NULL);
523         if (prepare_revision_walk(&rev))
524                 die("revision walk setup failed");
526         /*
527          * Collect all submodules (whether checked out or not) for which new
528          * commits have been recorded upstream in "changed_submodule_paths".
529          */
530         while ((commit = get_revision(&rev))) {
531                 struct commit_list *parent = commit->parents;
532                 while (parent) {
533                         struct diff_options diff_opts;
534                         diff_setup(&diff_opts);
535                         DIFF_OPT_SET(&diff_opts, RECURSIVE);
536                         diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
537                         diff_opts.format_callback = submodule_collect_changed_cb;
538                         if (diff_setup_done(&diff_opts) < 0)
539                                 die("diff_setup_done failed");
540                         diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
541                         diffcore_std(&diff_opts);
542                         diff_flush(&diff_opts);
543                         parent = parent->next;
544                 }
545         }
547         argv_array_clear(&argv);
548         sha1_array_clear(&ref_tips_before_fetch);
549         sha1_array_clear(&ref_tips_after_fetch);
550         initialized_fetch_ref_tips = 0;
553 int fetch_populated_submodules(int num_options, const char **options,
554                                const char *prefix, int command_line_option,
555                                int quiet)
557         int i, result = 0, argc = 0, default_argc;
558         struct child_process cp;
559         const char **argv;
560         struct string_list_item *name_for_path;
561         const char *work_tree = get_git_work_tree();
562         if (!work_tree)
563                 goto out;
565         if (!the_index.initialized)
566                 if (read_cache() < 0)
567                         die("index file corrupt");
569         /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
570         argv = xcalloc(num_options + 6, sizeof(const char *));
571         argv[argc++] = "fetch";
572         for (i = 0; i < num_options; i++)
573                 argv[argc++] = options[i];
574         argv[argc++] = "--recurse-submodules-default";
575         default_argc = argc++;
576         argv[argc++] = "--submodule-prefix";
578         memset(&cp, 0, sizeof(cp));
579         cp.argv = argv;
580         cp.env = local_repo_env;
581         cp.git_cmd = 1;
582         cp.no_stdin = 1;
584         calculate_changed_submodule_paths();
586         for (i = 0; i < active_nr; i++) {
587                 struct strbuf submodule_path = STRBUF_INIT;
588                 struct strbuf submodule_git_dir = STRBUF_INIT;
589                 struct strbuf submodule_prefix = STRBUF_INIT;
590                 struct cache_entry *ce = active_cache[i];
591                 const char *git_dir, *name, *default_argv;
593                 if (!S_ISGITLINK(ce->ce_mode))
594                         continue;
596                 name = ce->name;
597                 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
598                 if (name_for_path)
599                         name = name_for_path->util;
601                 default_argv = "yes";
602                 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
603                         struct string_list_item *fetch_recurse_submodules_option;
604                         fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
605                         if (fetch_recurse_submodules_option) {
606                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
607                                         continue;
608                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
609                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
610                                                 continue;
611                                         default_argv = "on-demand";
612                                 }
613                         } else {
614                                 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
615                                     gitmodules_is_unmerged)
616                                         continue;
617                                 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
618                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
619                                                 continue;
620                                         default_argv = "on-demand";
621                                 }
622                         }
623                 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
624                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
625                                 continue;
626                         default_argv = "on-demand";
627                 }
629                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
630                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
631                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
632                 git_dir = read_gitfile(submodule_git_dir.buf);
633                 if (!git_dir)
634                         git_dir = submodule_git_dir.buf;
635                 if (is_directory(git_dir)) {
636                         if (!quiet)
637                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
638                         cp.dir = submodule_path.buf;
639                         argv[default_argc] = default_argv;
640                         argv[argc] = submodule_prefix.buf;
641                         if (run_command(&cp))
642                                 result = 1;
643                 }
644                 strbuf_release(&submodule_path);
645                 strbuf_release(&submodule_git_dir);
646                 strbuf_release(&submodule_prefix);
647         }
648         free(argv);
649 out:
650         string_list_clear(&changed_submodule_paths, 1);
651         return result;
654 unsigned is_submodule_modified(const char *path, int ignore_untracked)
656         ssize_t len;
657         struct child_process cp;
658         const char *argv[] = {
659                 "status",
660                 "--porcelain",
661                 NULL,
662                 NULL,
663         };
664         struct strbuf buf = STRBUF_INIT;
665         unsigned dirty_submodule = 0;
666         const char *line, *next_line;
667         const char *git_dir;
669         strbuf_addf(&buf, "%s/.git", path);
670         git_dir = read_gitfile(buf.buf);
671         if (!git_dir)
672                 git_dir = buf.buf;
673         if (!is_directory(git_dir)) {
674                 strbuf_release(&buf);
675                 /* The submodule is not checked out, so it is not modified */
676                 return 0;
678         }
679         strbuf_reset(&buf);
681         if (ignore_untracked)
682                 argv[2] = "-uno";
684         memset(&cp, 0, sizeof(cp));
685         cp.argv = argv;
686         cp.env = local_repo_env;
687         cp.git_cmd = 1;
688         cp.no_stdin = 1;
689         cp.out = -1;
690         cp.dir = path;
691         if (start_command(&cp))
692                 die("Could not run 'git status --porcelain' in submodule %s", path);
694         len = strbuf_read(&buf, cp.out, 1024);
695         line = buf.buf;
696         while (len > 2) {
697                 if ((line[0] == '?') && (line[1] == '?')) {
698                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
699                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
700                                 break;
701                 } else {
702                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
703                         if (ignore_untracked ||
704                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
705                                 break;
706                 }
707                 next_line = strchr(line, '\n');
708                 if (!next_line)
709                         break;
710                 next_line++;
711                 len -= (next_line - line);
712                 line = next_line;
713         }
714         close(cp.out);
716         if (finish_command(&cp))
717                 die("'git status --porcelain' failed in submodule %s", path);
719         strbuf_release(&buf);
720         return dirty_submodule;
723 static int find_first_merges(struct object_array *result, const char *path,
724                 struct commit *a, struct commit *b)
726         int i, j;
727         struct object_array merges;
728         struct commit *commit;
729         int contains_another;
731         char merged_revision[42];
732         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
733                                    "--all", merged_revision, NULL };
734         struct rev_info revs;
735         struct setup_revision_opt rev_opts;
737         memset(&merges, 0, sizeof(merges));
738         memset(result, 0, sizeof(struct object_array));
739         memset(&rev_opts, 0, sizeof(rev_opts));
741         /* get all revisions that merge commit a */
742         snprintf(merged_revision, sizeof(merged_revision), "^%s",
743                         sha1_to_hex(a->object.sha1));
744         init_revisions(&revs, NULL);
745         rev_opts.submodule = path;
746         setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
748         /* save all revisions from the above list that contain b */
749         if (prepare_revision_walk(&revs))
750                 die("revision walk setup failed");
751         while ((commit = get_revision(&revs)) != NULL) {
752                 struct object *o = &(commit->object);
753                 if (in_merge_bases(b, &commit, 1))
754                         add_object_array(o, NULL, &merges);
755         }
757         /* Now we've got all merges that contain a and b. Prune all
758          * merges that contain another found merge and save them in
759          * result.
760          */
761         for (i = 0; i < merges.nr; i++) {
762                 struct commit *m1 = (struct commit *) merges.objects[i].item;
764                 contains_another = 0;
765                 for (j = 0; j < merges.nr; j++) {
766                         struct commit *m2 = (struct commit *) merges.objects[j].item;
767                         if (i != j && in_merge_bases(m2, &m1, 1)) {
768                                 contains_another = 1;
769                                 break;
770                         }
771                 }
773                 if (!contains_another)
774                         add_object_array(merges.objects[i].item,
775                                          merges.objects[i].name, result);
776         }
778         free(merges.objects);
779         return result->nr;
782 static void print_commit(struct commit *commit)
784         struct strbuf sb = STRBUF_INIT;
785         struct pretty_print_context ctx = {0};
786         ctx.date_mode = DATE_NORMAL;
787         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
788         fprintf(stderr, "%s\n", sb.buf);
789         strbuf_release(&sb);
792 #define MERGE_WARNING(path, msg) \
793         warning("Failed to merge submodule %s (%s)", path, msg);
795 int merge_submodule(unsigned char result[20], const char *path,
796                     const unsigned char base[20], const unsigned char a[20],
797                     const unsigned char b[20], int search)
799         struct commit *commit_base, *commit_a, *commit_b;
800         int parent_count;
801         struct object_array merges;
803         int i;
805         /* store a in result in case we fail */
806         hashcpy(result, a);
808         /* we can not handle deletion conflicts */
809         if (is_null_sha1(base))
810                 return 0;
811         if (is_null_sha1(a))
812                 return 0;
813         if (is_null_sha1(b))
814                 return 0;
816         if (add_submodule_odb(path)) {
817                 MERGE_WARNING(path, "not checked out");
818                 return 0;
819         }
821         if (!(commit_base = lookup_commit_reference(base)) ||
822             !(commit_a = lookup_commit_reference(a)) ||
823             !(commit_b = lookup_commit_reference(b))) {
824                 MERGE_WARNING(path, "commits not present");
825                 return 0;
826         }
828         /* check whether both changes are forward */
829         if (!in_merge_bases(commit_base, &commit_a, 1) ||
830             !in_merge_bases(commit_base, &commit_b, 1)) {
831                 MERGE_WARNING(path, "commits don't follow merge-base");
832                 return 0;
833         }
835         /* Case #1: a is contained in b or vice versa */
836         if (in_merge_bases(commit_a, &commit_b, 1)) {
837                 hashcpy(result, b);
838                 return 1;
839         }
840         if (in_merge_bases(commit_b, &commit_a, 1)) {
841                 hashcpy(result, a);
842                 return 1;
843         }
845         /*
846          * Case #2: There are one or more merges that contain a and b in
847          * the submodule. If there is only one, then present it as a
848          * suggestion to the user, but leave it marked unmerged so the
849          * user needs to confirm the resolution.
850          */
852         /* Skip the search if makes no sense to the calling context.  */
853         if (!search)
854                 return 0;
856         /* find commit which merges them */
857         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
858         switch (parent_count) {
859         case 0:
860                 MERGE_WARNING(path, "merge following commits not found");
861                 break;
863         case 1:
864                 MERGE_WARNING(path, "not fast-forward");
865                 fprintf(stderr, "Found a possible merge resolution "
866                                 "for the submodule:\n");
867                 print_commit((struct commit *) merges.objects[0].item);
868                 fprintf(stderr,
869                         "If this is correct simply add it to the index "
870                         "for example\n"
871                         "by using:\n\n"
872                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
873                         "which will accept this suggestion.\n",
874                         sha1_to_hex(merges.objects[0].item->sha1), path);
875                 break;
877         default:
878                 MERGE_WARNING(path, "multiple merges found");
879                 for (i = 0; i < merges.nr; i++)
880                         print_commit((struct commit *) merges.objects[i].item);
881         }
883         free(merges.objects);
884         return 0;