FIXME: hotpatch for compatibility with latest hg
[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 "transport.h"
12
13 typedef int (*needs_push_func_t)(const char *path, const unsigned char sha1[20],
14                 void *data);
15 static struct string_list config_name_for_path;
16 static struct string_list config_fetch_recurse_submodules_for_name;
17 static struct string_list config_ignore_for_name;
18 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
19 static struct string_list changed_submodule_paths;
20 /*
21  * The following flag is set if the .gitmodules file is unmerged. We then
22  * disable recursion for all submodules where .git/config doesn't have a
23  * matching config entry because we can't guess what might be configured in
24  * .gitmodules unless the user resolves the conflict. When a command line
25  * option is given (which always overrides configuration) this flag will be
26  * ignored.
27  */
28 static int gitmodules_is_unmerged;
29
30 static int add_submodule_odb(const char *path)
31 {
32         struct strbuf objects_directory = STRBUF_INIT;
33         struct alternate_object_database *alt_odb;
34         int ret = 0;
35         const char *git_dir;
36
37         strbuf_addf(&objects_directory, "%s/.git", path);
38         git_dir = read_gitfile(objects_directory.buf);
39         if (git_dir) {
40                 strbuf_reset(&objects_directory);
41                 strbuf_addstr(&objects_directory, git_dir);
42         }
43         strbuf_addstr(&objects_directory, "/objects/");
44         if (!is_directory(objects_directory.buf)) {
45                 ret = -1;
46                 goto done;
47         }
48         /* avoid adding it twice */
49         for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
50                 if (alt_odb->name - alt_odb->base == objects_directory.len &&
51                                 !strncmp(alt_odb->base, objects_directory.buf,
52                                         objects_directory.len))
53                         goto done;
54
55         alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
56         alt_odb->next = alt_odb_list;
57         strcpy(alt_odb->base, objects_directory.buf);
58         alt_odb->name = alt_odb->base + objects_directory.len;
59         alt_odb->name[2] = '/';
60         alt_odb->name[40] = '\0';
61         alt_odb->name[41] = '\0';
62         alt_odb_list = alt_odb;
63         prepare_alt_odb();
64 done:
65         strbuf_release(&objects_directory);
66         return ret;
67 }
68
69 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
70                                              const char *path)
71 {
72         struct string_list_item *path_option, *ignore_option;
73         path_option = unsorted_string_list_lookup(&config_name_for_path, path);
74         if (path_option) {
75                 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
76                 if (ignore_option)
77                         handle_ignore_submodules_arg(diffopt, ignore_option->util);
78                 else if (gitmodules_is_unmerged)
79                         DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
80         }
81 }
82
83 int submodule_config(const char *var, const char *value, void *cb)
84 {
85         if (!prefixcmp(var, "submodule."))
86                 return parse_submodule_config_option(var, value);
87         else if (!strcmp(var, "fetch.recursesubmodules")) {
88                 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
89                 return 0;
90         }
91         return 0;
92 }
93
94 void gitmodules_config(void)
95 {
96         const char *work_tree = get_git_work_tree();
97         if (work_tree) {
98                 struct strbuf gitmodules_path = STRBUF_INIT;
99                 int pos;
100                 strbuf_addstr(&gitmodules_path, work_tree);
101                 strbuf_addstr(&gitmodules_path, "/.gitmodules");
102                 if (read_cache() < 0)
103                         die("index file corrupt");
104                 pos = cache_name_pos(".gitmodules", 11);
105                 if (pos < 0) { /* .gitmodules not found or isn't merged */
106                         pos = -1 - pos;
107                         if (active_nr > pos) {  /* there is a .gitmodules */
108                                 const struct cache_entry *ce = active_cache[pos];
109                                 if (ce_namelen(ce) == 11 &&
110                                     !memcmp(ce->name, ".gitmodules", 11))
111                                         gitmodules_is_unmerged = 1;
112                         }
113                 }
114
115                 if (!gitmodules_is_unmerged)
116                         git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
117                 strbuf_release(&gitmodules_path);
118         }
119 }
120
121 int parse_submodule_config_option(const char *var, const char *value)
122 {
123         int len;
124         struct string_list_item *config;
125         struct strbuf submodname = STRBUF_INIT;
126
127         var += 10;              /* Skip "submodule." */
128
129         len = strlen(var);
130         if ((len > 5) && !strcmp(var + len - 5, ".path")) {
131                 strbuf_add(&submodname, var, len - 5);
132                 config = unsorted_string_list_lookup(&config_name_for_path, value);
133                 if (config)
134                         free(config->util);
135                 else
136                         config = string_list_append(&config_name_for_path, xstrdup(value));
137                 config->util = strbuf_detach(&submodname, NULL);
138                 strbuf_release(&submodname);
139         } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
140                 strbuf_add(&submodname, var, len - 23);
141                 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
142                 if (!config)
143                         config = string_list_append(&config_fetch_recurse_submodules_for_name,
144                                                     strbuf_detach(&submodname, NULL));
145                 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
146                 strbuf_release(&submodname);
147         } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
148                 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
149                     strcmp(value, "all") && strcmp(value, "none")) {
150                         warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
151                         return 0;
152                 }
153
154                 strbuf_add(&submodname, var, len - 7);
155                 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
156                 if (config)
157                         free(config->util);
158                 else
159                         config = string_list_append(&config_ignore_for_name,
160                                                     strbuf_detach(&submodname, NULL));
161                 strbuf_release(&submodname);
162                 config->util = xstrdup(value);
163                 return 0;
164         }
165         return 0;
166 }
167
168 void handle_ignore_submodules_arg(struct diff_options *diffopt,
169                                   const char *arg)
170 {
171         DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
172         DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
173         DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
174
175         if (!strcmp(arg, "all"))
176                 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
177         else if (!strcmp(arg, "untracked"))
178                 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
179         else if (!strcmp(arg, "dirty"))
180                 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
181         else if (strcmp(arg, "none"))
182                 die("bad --ignore-submodules argument: %s", arg);
183 }
184
185 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
186                 struct commit *left, struct commit *right,
187                 int *fast_forward, int *fast_backward)
188 {
189         struct commit_list *merge_bases, *list;
190
191         init_revisions(rev, NULL);
192         setup_revisions(0, NULL, rev, NULL);
193         rev->left_right = 1;
194         rev->first_parent_only = 1;
195         left->object.flags |= SYMMETRIC_LEFT;
196         add_pending_object(rev, &left->object, path);
197         add_pending_object(rev, &right->object, path);
198         merge_bases = get_merge_bases(left, right, 1);
199         if (merge_bases) {
200                 if (merge_bases->item == left)
201                         *fast_forward = 1;
202                 else if (merge_bases->item == right)
203                         *fast_backward = 1;
204         }
205         for (list = merge_bases; list; list = list->next) {
206                 list->item->object.flags |= UNINTERESTING;
207                 add_pending_object(rev, &list->item->object,
208                         sha1_to_hex(list->item->object.sha1));
209         }
210         return prepare_revision_walk(rev);
211 }
212
213 static void print_submodule_summary(struct rev_info *rev, FILE *f,
214                 const char *del, const char *add, const char *reset)
215 {
216         static const char format[] = "  %m %s";
217         struct strbuf sb = STRBUF_INIT;
218         struct commit *commit;
219
220         while ((commit = get_revision(rev))) {
221                 struct pretty_print_context ctx = {0};
222                 ctx.date_mode = rev->date_mode;
223                 strbuf_setlen(&sb, 0);
224                 if (commit->object.flags & SYMMETRIC_LEFT) {
225                         if (del)
226                                 strbuf_addstr(&sb, del);
227                 }
228                 else if (add)
229                         strbuf_addstr(&sb, add);
230                 format_commit_message(commit, format, &sb, &ctx);
231                 if (reset)
232                         strbuf_addstr(&sb, reset);
233                 strbuf_addch(&sb, '\n');
234                 fprintf(f, "%s", sb.buf);
235         }
236         strbuf_release(&sb);
237 }
238
239 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
240 {
241         switch (git_config_maybe_bool(opt, arg)) {
242         case 1:
243                 return RECURSE_SUBMODULES_ON;
244         case 0:
245                 return RECURSE_SUBMODULES_OFF;
246         default:
247                 if (!strcmp(arg, "on-demand"))
248                         return RECURSE_SUBMODULES_ON_DEMAND;
249                 die("bad %s argument: %s", opt, arg);
250         }
251 }
252
253 void show_submodule_summary(FILE *f, const char *path,
254                 unsigned char one[20], unsigned char two[20],
255                 unsigned dirty_submodule,
256                 const char *del, const char *add, const char *reset)
257 {
258         struct rev_info rev;
259         struct commit *left = left, *right = right;
260         const char *message = NULL;
261         struct strbuf sb = STRBUF_INIT;
262         int fast_forward = 0, fast_backward = 0;
263
264         if (is_null_sha1(two))
265                 message = "(submodule deleted)";
266         else if (add_submodule_odb(path))
267                 message = "(not checked out)";
268         else if (is_null_sha1(one))
269                 message = "(new submodule)";
270         else if (!(left = lookup_commit_reference(one)) ||
271                  !(right = lookup_commit_reference(two)))
272                 message = "(commits not present)";
273
274         if (!message &&
275             prepare_submodule_summary(&rev, path, left, right,
276                                         &fast_forward, &fast_backward))
277                 message = "(revision walker failed)";
278
279         if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
280                 fprintf(f, "Submodule %s contains untracked content\n", path);
281         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
282                 fprintf(f, "Submodule %s contains modified content\n", path);
283
284         if (!hashcmp(one, two)) {
285                 strbuf_release(&sb);
286                 return;
287         }
288
289         strbuf_addf(&sb, "Submodule %s %s..", path,
290                         find_unique_abbrev(one, DEFAULT_ABBREV));
291         if (!fast_backward && !fast_forward)
292                 strbuf_addch(&sb, '.');
293         strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
294         if (message)
295                 strbuf_addf(&sb, " %s\n", message);
296         else
297                 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
298         fwrite(sb.buf, sb.len, 1, f);
299
300         if (!message) {
301                 print_submodule_summary(&rev, f, del, add, reset);
302                 clear_commit_marks(left, ~0);
303                 clear_commit_marks(right, ~0);
304         }
305
306         strbuf_release(&sb);
307 }
308
309 void set_config_fetch_recurse_submodules(int value)
310 {
311         config_fetch_recurse_submodules = value;
312 }
313
314 typedef int (*module_func_t)(const char *path, const unsigned char sha1[20], void *data);
315
316 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
317 {
318         return 1;
319 }
320
321 int submodule_needs_pushing(const char *path, const unsigned char sha1[20], void *data)
322 {
323         int *needs_pushing = data;
324
325         if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
326                 return 1;
327
328         if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
329                 struct child_process cp;
330                 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
331                 struct strbuf buf = STRBUF_INIT;
332
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         }
351
352         return 1;
353 }
354
355 int push_submodule(const char *path, const unsigned char sha1[20], void *data)
356 {
357         if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
358                 return 1;
359
360         if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
361                 struct child_process cp;
362                 const char *argv[] = {"push", NULL};
363
364                 memset(&cp, 0, sizeof(cp));
365                 cp.argv = argv;
366                 cp.env = local_repo_env;
367                 cp.git_cmd = 1;
368                 cp.no_stdin = 1;
369                 cp.out = -1;
370                 cp.dir = path;
371                 if (run_command(&cp))
372                         die("Could not run 'git push' command in submodule %s", path);
373                 close(cp.out);
374         }
375
376         return 1;
377 }
378
379 struct collect_submodules_data {
380         module_func_t func;
381         void *data;
382         int ret;
383 };
384
385 static void collect_submodules_from_diff(struct diff_queue_struct *q,
386                                          struct diff_options *options,
387                                          void *data)
388 {
389         int i;
390         struct collect_submodules_data *me = data;
391
392         for (i = 0; i < q->nr; i++) {
393                 struct diff_filepair *p = q->queue[i];
394                 if (!S_ISGITLINK(p->two->mode))
395                         continue;
396                 if (!(me->ret = me->func(p->two->path, p->two->sha1, me->data)))
397                         break;
398         }
399 }
400
401 static int commit_need_pushing(struct commit *commit, struct commit_list *parent,
402         module_func_t func, void *data)
403 {
404         const unsigned char (*parents)[20];
405         unsigned int i, n;
406         struct rev_info rev;
407
408         struct collect_submodules_data cb;
409         cb.func = func;
410         cb.data = data;
411         cb.ret = 1;
412
413         n = commit_list_count(parent);
414         parents = xmalloc(n * sizeof(*parents));
415
416         for (i = 0; i < n; i++) {
417                 hashcpy((unsigned char *)(parents + i), parent->item->object.sha1);
418                 parent = parent->next;
419         }
420
421         init_revisions(&rev, NULL);
422         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
423         rev.diffopt.format_callback = collect_submodules_from_diff;
424         rev.diffopt.format_callback_data = &cb;
425         diff_tree_combined(commit->object.sha1, parents, n, 1, &rev);
426
427         free(parents);
428         return cb.ret;
429 }
430
431 static int inspect_superproject_commits(unsigned char new_sha1[20], const char *remotes_name,
432         module_func_t func, void *data)
433 {
434         struct rev_info rev;
435         struct commit *commit;
436         const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
437         int argc = ARRAY_SIZE(argv) - 1;
438         char *sha1_copy;
439         struct strbuf remotes_arg = STRBUF_INIT;
440         int do_continue = 1;
441
442         strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
443         init_revisions(&rev, NULL);
444         sha1_copy = xstrdup(sha1_to_hex(new_sha1));
445         argv[1] = sha1_copy;
446         argv[3] = remotes_arg.buf;
447         setup_revisions(argc, argv, &rev, NULL);
448         if (prepare_revision_walk(&rev))
449                 die("revision walk setup failed");
450
451         while ((commit = get_revision(&rev)) && do_continue)
452                 do_continue = commit_need_pushing(commit, commit->parents, func, data);
453
454         free(sha1_copy);
455         strbuf_release(&remotes_arg);
456
457         return do_continue;
458 }
459
460 int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name)
461 {
462         int needs_push = 0;
463         inspect_superproject_commits(new_sha1, remotes_name, submodule_needs_pushing, &needs_push);
464         return needs_push;
465 }
466
467 void push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
468 {
469         inspect_superproject_commits(new_sha1, remotes_name, push_submodule, NULL);
470 }
471
472 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
473 {
474         int is_present = 0;
475         if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
476                 /* Even if the submodule is checked out and the commit is
477                  * present, make sure it is reachable from a ref. */
478                 struct child_process cp;
479                 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
480                 struct strbuf buf = STRBUF_INIT;
481
482                 argv[3] = sha1_to_hex(sha1);
483                 memset(&cp, 0, sizeof(cp));
484                 cp.argv = argv;
485                 cp.env = local_repo_env;
486                 cp.git_cmd = 1;
487                 cp.no_stdin = 1;
488                 cp.out = -1;
489                 cp.dir = path;
490                 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
491                         is_present = 1;
492
493                 close(cp.out);
494                 strbuf_release(&buf);
495         }
496         return is_present;
497 }
498
499 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
500                                          struct diff_options *options,
501                                          void *data)
502 {
503         int i;
504         for (i = 0; i < q->nr; i++) {
505                 struct diff_filepair *p = q->queue[i];
506                 if (!S_ISGITLINK(p->two->mode))
507                         continue;
508
509                 if (S_ISGITLINK(p->one->mode)) {
510                         /* NEEDSWORK: We should honor the name configured in
511                          * the .gitmodules file of the commit we are examining
512                          * here to be able to correctly follow submodules
513                          * being moved around. */
514                         struct string_list_item *path;
515                         path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
516                         if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
517                                 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
518                 } else {
519                         /* Submodule is new or was moved here */
520                         /* NEEDSWORK: When the .git directories of submodules
521                          * live inside the superprojects .git directory some
522                          * day we should fetch new submodules directly into
523                          * that location too when config or options request
524                          * that so they can be checked out from there. */
525                         continue;
526                 }
527         }
528 }
529
530 void check_for_new_submodule_commits(unsigned char new_sha1[20])
531 {
532         struct rev_info rev;
533         struct commit *commit;
534         const char *argv[] = {NULL, NULL, "--not", "--all", NULL};
535         int argc = ARRAY_SIZE(argv) - 1;
536
537         init_revisions(&rev, NULL);
538         argv[1] = xstrdup(sha1_to_hex(new_sha1));
539         setup_revisions(argc, argv, &rev, NULL);
540         if (prepare_revision_walk(&rev))
541                 die("revision walk setup failed");
542
543         /*
544          * Collect all submodules (whether checked out or not) for which new
545          * commits have been recorded upstream in "changed_submodule_paths".
546          */
547         while ((commit = get_revision(&rev))) {
548                 struct commit_list *parent = commit->parents;
549                 while (parent) {
550                         struct diff_options diff_opts;
551                         diff_setup(&diff_opts);
552                         DIFF_OPT_SET(&diff_opts, RECURSIVE);
553                         diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
554                         diff_opts.format_callback = submodule_collect_changed_cb;
555                         if (diff_setup_done(&diff_opts) < 0)
556                                 die("diff_setup_done failed");
557                         diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
558                         diffcore_std(&diff_opts);
559                         diff_flush(&diff_opts);
560                         parent = parent->next;
561                 }
562         }
563         free((char *)argv[1]);
564 }
565
566 int fetch_populated_submodules(int num_options, const char **options,
567                                const char *prefix, int command_line_option,
568                                int quiet)
569 {
570         int i, result = 0, argc = 0, default_argc;
571         struct child_process cp;
572         const char **argv;
573         struct string_list_item *name_for_path;
574         const char *work_tree = get_git_work_tree();
575         if (!work_tree)
576                 goto out;
577
578         if (!the_index.initialized)
579                 if (read_cache() < 0)
580                         die("index file corrupt");
581
582         /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
583         argv = xcalloc(num_options + 6, sizeof(const char *));
584         argv[argc++] = "fetch";
585         for (i = 0; i < num_options; i++)
586                 argv[argc++] = options[i];
587         argv[argc++] = "--recurse-submodules-default";
588         default_argc = argc++;
589         argv[argc++] = "--submodule-prefix";
590
591         memset(&cp, 0, sizeof(cp));
592         cp.argv = argv;
593         cp.env = local_repo_env;
594         cp.git_cmd = 1;
595         cp.no_stdin = 1;
596
597         for (i = 0; i < active_nr; i++) {
598                 struct strbuf submodule_path = STRBUF_INIT;
599                 struct strbuf submodule_git_dir = STRBUF_INIT;
600                 struct strbuf submodule_prefix = STRBUF_INIT;
601                 struct cache_entry *ce = active_cache[i];
602                 const char *git_dir, *name, *default_argv;
603
604                 if (!S_ISGITLINK(ce->ce_mode))
605                         continue;
606
607                 name = ce->name;
608                 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
609                 if (name_for_path)
610                         name = name_for_path->util;
611
612                 default_argv = "yes";
613                 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
614                         struct string_list_item *fetch_recurse_submodules_option;
615                         fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
616                         if (fetch_recurse_submodules_option) {
617                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
618                                         continue;
619                                 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
620                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
621                                                 continue;
622                                         default_argv = "on-demand";
623                                 }
624                         } else {
625                                 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
626                                     gitmodules_is_unmerged)
627                                         continue;
628                                 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
629                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
630                                                 continue;
631                                         default_argv = "on-demand";
632                                 }
633                         }
634                 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
635                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
636                                 continue;
637                         default_argv = "on-demand";
638                 }
639
640                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
641                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
642                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
643                 git_dir = read_gitfile(submodule_git_dir.buf);
644                 if (!git_dir)
645                         git_dir = submodule_git_dir.buf;
646                 if (is_directory(git_dir)) {
647                         if (!quiet)
648                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
649                         cp.dir = submodule_path.buf;
650                         argv[default_argc] = default_argv;
651                         argv[argc] = submodule_prefix.buf;
652                         if (run_command(&cp))
653                                 result = 1;
654                 }
655                 strbuf_release(&submodule_path);
656                 strbuf_release(&submodule_git_dir);
657                 strbuf_release(&submodule_prefix);
658         }
659         free(argv);
660 out:
661         string_list_clear(&changed_submodule_paths, 1);
662         return result;
663 }
664
665 unsigned is_submodule_modified(const char *path, int ignore_untracked)
666 {
667         ssize_t len;
668         struct child_process cp;
669         const char *argv[] = {
670                 "status",
671                 "--porcelain",
672                 NULL,
673                 NULL,
674         };
675         struct strbuf buf = STRBUF_INIT;
676         unsigned dirty_submodule = 0;
677         const char *line, *next_line;
678         const char *git_dir;
679
680         strbuf_addf(&buf, "%s/.git", path);
681         git_dir = read_gitfile(buf.buf);
682         if (!git_dir)
683                 git_dir = buf.buf;
684         if (!is_directory(git_dir)) {
685                 strbuf_release(&buf);
686                 /* The submodule is not checked out, so it is not modified */
687                 return 0;
688
689         }
690         strbuf_reset(&buf);
691
692         if (ignore_untracked)
693                 argv[2] = "-uno";
694
695         memset(&cp, 0, sizeof(cp));
696         cp.argv = argv;
697         cp.env = local_repo_env;
698         cp.git_cmd = 1;
699         cp.no_stdin = 1;
700         cp.out = -1;
701         cp.dir = path;
702         if (start_command(&cp))
703                 die("Could not run git status --porcelain");
704
705         len = strbuf_read(&buf, cp.out, 1024);
706         line = buf.buf;
707         while (len > 2) {
708                 if ((line[0] == '?') && (line[1] == '?')) {
709                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
710                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
711                                 break;
712                 } else {
713                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
714                         if (ignore_untracked ||
715                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
716                                 break;
717                 }
718                 next_line = strchr(line, '\n');
719                 if (!next_line)
720                         break;
721                 next_line++;
722                 len -= (next_line - line);
723                 line = next_line;
724         }
725         close(cp.out);
726
727         if (finish_command(&cp))
728                 die("git status --porcelain failed");
729
730         strbuf_release(&buf);
731         return dirty_submodule;
732 }
733
734 static int find_first_merges(struct object_array *result, const char *path,
735                 struct commit *a, struct commit *b)
736 {
737         int i, j;
738         struct object_array merges;
739         struct commit *commit;
740         int contains_another;
741
742         char merged_revision[42];
743         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
744                                    "--all", merged_revision, NULL };
745         struct rev_info revs;
746         struct setup_revision_opt rev_opts;
747
748         memset(&merges, 0, sizeof(merges));
749         memset(result, 0, sizeof(struct object_array));
750         memset(&rev_opts, 0, sizeof(rev_opts));
751
752         /* get all revisions that merge commit a */
753         snprintf(merged_revision, sizeof(merged_revision), "^%s",
754                         sha1_to_hex(a->object.sha1));
755         init_revisions(&revs, NULL);
756         rev_opts.submodule = path;
757         setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
758
759         /* save all revisions from the above list that contain b */
760         if (prepare_revision_walk(&revs))
761                 die("revision walk setup failed");
762         while ((commit = get_revision(&revs)) != NULL) {
763                 struct object *o = &(commit->object);
764                 if (in_merge_bases(b, &commit, 1))
765                         add_object_array(o, NULL, &merges);
766         }
767
768         /* Now we've got all merges that contain a and b. Prune all
769          * merges that contain another found merge and save them in
770          * result.
771          */
772         for (i = 0; i < merges.nr; i++) {
773                 struct commit *m1 = (struct commit *) merges.objects[i].item;
774
775                 contains_another = 0;
776                 for (j = 0; j < merges.nr; j++) {
777                         struct commit *m2 = (struct commit *) merges.objects[j].item;
778                         if (i != j && in_merge_bases(m2, &m1, 1)) {
779                                 contains_another = 1;
780                                 break;
781                         }
782                 }
783
784                 if (!contains_another)
785                         add_object_array(merges.objects[i].item,
786                                          merges.objects[i].name, result);
787         }
788
789         free(merges.objects);
790         return result->nr;
791 }
792
793 static void print_commit(struct commit *commit)
794 {
795         struct strbuf sb = STRBUF_INIT;
796         struct pretty_print_context ctx = {0};
797         ctx.date_mode = DATE_NORMAL;
798         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
799         fprintf(stderr, "%s\n", sb.buf);
800         strbuf_release(&sb);
801 }
802
803 #define MERGE_WARNING(path, msg) \
804         warning("Failed to merge submodule %s (%s)", path, msg);
805
806 int merge_submodule(unsigned char result[20], const char *path,
807                     const unsigned char base[20], const unsigned char a[20],
808                     const unsigned char b[20])
809 {
810         struct commit *commit_base, *commit_a, *commit_b;
811         int parent_count;
812         struct object_array merges;
813
814         int i;
815
816         /* store a in result in case we fail */
817         hashcpy(result, a);
818
819         /* we can not handle deletion conflicts */
820         if (is_null_sha1(base))
821                 return 0;
822         if (is_null_sha1(a))
823                 return 0;
824         if (is_null_sha1(b))
825                 return 0;
826
827         if (add_submodule_odb(path)) {
828                 MERGE_WARNING(path, "not checked out");
829                 return 0;
830         }
831
832         if (!(commit_base = lookup_commit_reference(base)) ||
833             !(commit_a = lookup_commit_reference(a)) ||
834             !(commit_b = lookup_commit_reference(b))) {
835                 MERGE_WARNING(path, "commits not present");
836                 return 0;
837         }
838
839         /* check whether both changes are forward */
840         if (!in_merge_bases(commit_base, &commit_a, 1) ||
841             !in_merge_bases(commit_base, &commit_b, 1)) {
842                 MERGE_WARNING(path, "commits don't follow merge-base");
843                 return 0;
844         }
845
846         /* Case #1: a is contained in b or vice versa */
847         if (in_merge_bases(commit_a, &commit_b, 1)) {
848                 hashcpy(result, b);
849                 return 1;
850         }
851         if (in_merge_bases(commit_b, &commit_a, 1)) {
852                 hashcpy(result, a);
853                 return 1;
854         }
855
856         /*
857          * Case #2: There are one or more merges that contain a and b in
858          * the submodule. If there is only one, then present it as a
859          * suggestion to the user, but leave it marked unmerged so the
860          * user needs to confirm the resolution.
861          */
862
863         /* find commit which merges them */
864         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
865         switch (parent_count) {
866         case 0:
867                 MERGE_WARNING(path, "merge following commits not found");
868                 break;
869
870         case 1:
871                 MERGE_WARNING(path, "not fast-forward");
872                 fprintf(stderr, "Found a possible merge resolution "
873                                 "for the submodule:\n");
874                 print_commit((struct commit *) merges.objects[0].item);
875                 fprintf(stderr,
876                         "If this is correct simply add it to the index "
877                         "for example\n"
878                         "by using:\n\n"
879                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
880                         "which will accept this suggestion.\n",
881                         sha1_to_hex(merges.objects[0].item->sha1), path);
882                 break;
883
884         default:
885                 MERGE_WARNING(path, "multiple merges found");
886                 for (i = 0; i < merges.nr; i++)
887                         print_commit((struct commit *) merges.objects[i].item);
888         }
889
890         free(merges.objects);
891         return 0;
892 }