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