Merge branch 'dw/subtree-split-do-not-drop-merge' into maint
[git] / submodule.c
1 #include "cache.h"
2 #include "submodule-config.h"
3 #include "submodule.h"
4 #include "dir.h"
5 #include "diff.h"
6 #include "commit.h"
7 #include "revision.h"
8 #include "run-command.h"
9 #include "diffcore.h"
10 #include "refs.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "argv-array.h"
14 #include "blob.h"
15
16 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
17 static struct string_list changed_submodule_paths;
18 static int initialized_fetch_ref_tips;
19 static struct sha1_array ref_tips_before_fetch;
20 static struct sha1_array ref_tips_after_fetch;
21
22 /*
23  * The following flag is set if the .gitmodules file is unmerged. We then
24  * disable recursion for all submodules where .git/config doesn't have a
25  * matching config entry because we can't guess what might be configured in
26  * .gitmodules unless the user resolves the conflict. When a command line
27  * option is given (which always overrides configuration) this flag will be
28  * ignored.
29  */
30 static int gitmodules_is_unmerged;
31
32 /*
33  * This flag is set if the .gitmodules file had unstaged modifications on
34  * startup. This must be checked before allowing modifications to the
35  * .gitmodules file with the intention to stage them later, because when
36  * continuing we would stage the modifications the user didn't stage herself
37  * too. That might change in a future version when we learn to stage the
38  * changes we do ourselves without staging any previous modifications.
39  */
40 static int gitmodules_is_modified;
41
42 int is_staging_gitmodules_ok(void)
43 {
44         return !gitmodules_is_modified;
45 }
46
47 /*
48  * Try to update the "path" entry in the "submodule.<name>" section of the
49  * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
50  * with the correct path=<oldpath> setting was found and we could update it.
51  */
52 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
53 {
54         struct strbuf entry = STRBUF_INIT;
55         const struct submodule *submodule;
56
57         if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
58                 return -1;
59
60         if (gitmodules_is_unmerged)
61                 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
62
63         submodule = submodule_from_path(null_sha1, oldpath);
64         if (!submodule || !submodule->name) {
65                 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
66                 return -1;
67         }
68         strbuf_addstr(&entry, "submodule.");
69         strbuf_addstr(&entry, submodule->name);
70         strbuf_addstr(&entry, ".path");
71         if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
72                 /* Maybe the user already did that, don't error out here */
73                 warning(_("Could not update .gitmodules entry %s"), entry.buf);
74                 strbuf_release(&entry);
75                 return -1;
76         }
77         strbuf_release(&entry);
78         return 0;
79 }
80
81 /*
82  * Try to remove the "submodule.<name>" section from .gitmodules where the given
83  * path is configured. Return 0 only if a .gitmodules file was found, a section
84  * with the correct path=<path> setting was found and we could remove it.
85  */
86 int remove_path_from_gitmodules(const char *path)
87 {
88         struct strbuf sect = STRBUF_INIT;
89         const struct submodule *submodule;
90
91         if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
92                 return -1;
93
94         if (gitmodules_is_unmerged)
95                 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
96
97         submodule = submodule_from_path(null_sha1, path);
98         if (!submodule || !submodule->name) {
99                 warning(_("Could not find section in .gitmodules where path=%s"), path);
100                 return -1;
101         }
102         strbuf_addstr(&sect, "submodule.");
103         strbuf_addstr(&sect, submodule->name);
104         if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
105                 /* Maybe the user already did that, don't error out here */
106                 warning(_("Could not remove .gitmodules entry for %s"), path);
107                 strbuf_release(&sect);
108                 return -1;
109         }
110         strbuf_release(&sect);
111         return 0;
112 }
113
114 void stage_updated_gitmodules(void)
115 {
116         if (add_file_to_cache(".gitmodules", 0))
117                 die(_("staging updated .gitmodules failed"));
118 }
119
120 static int add_submodule_odb(const char *path)
121 {
122         struct strbuf objects_directory = STRBUF_INIT;
123         struct alternate_object_database *alt_odb;
124         int ret = 0;
125         int alloc;
126
127         strbuf_git_path_submodule(&objects_directory, path, "objects/");
128         if (!is_directory(objects_directory.buf)) {
129                 ret = -1;
130                 goto done;
131         }
132         /* avoid adding it twice */
133         prepare_alt_odb();
134         for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
135                 if (alt_odb->name - alt_odb->base == objects_directory.len &&
136                                 !strncmp(alt_odb->base, objects_directory.buf,
137                                         objects_directory.len))
138                         goto done;
139
140         alloc = objects_directory.len + 42; /* for "12/345..." sha1 */
141         alt_odb = xmalloc(sizeof(*alt_odb) + alloc);
142         alt_odb->next = alt_odb_list;
143         xsnprintf(alt_odb->base, alloc, "%s", objects_directory.buf);
144         alt_odb->name = alt_odb->base + objects_directory.len;
145         alt_odb->name[2] = '/';
146         alt_odb->name[40] = '\0';
147         alt_odb->name[41] = '\0';
148         alt_odb_list = alt_odb;
149
150         /* add possible alternates from the submodule */
151         read_info_alternates(objects_directory.buf, 0);
152 done:
153         strbuf_release(&objects_directory);
154         return ret;
155 }
156
157 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
158                                              const char *path)
159 {
160         const struct submodule *submodule = submodule_from_path(null_sha1, path);
161         if (submodule) {
162                 if (submodule->ignore)
163                         handle_ignore_submodules_arg(diffopt, submodule->ignore);
164                 else if (gitmodules_is_unmerged)
165                         DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
166         }
167 }
168
169 int submodule_config(const char *var, const char *value, void *cb)
170 {
171         if (starts_with(var, "submodule."))
172                 return parse_submodule_config_option(var, value);
173         else if (!strcmp(var, "fetch.recursesubmodules")) {
174                 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
175                 return 0;
176         }
177         return 0;
178 }
179
180 void gitmodules_config(void)
181 {
182         const char *work_tree = get_git_work_tree();
183         if (work_tree) {
184                 struct strbuf gitmodules_path = STRBUF_INIT;
185                 int pos;
186                 strbuf_addstr(&gitmodules_path, work_tree);
187                 strbuf_addstr(&gitmodules_path, "/.gitmodules");
188                 if (read_cache() < 0)
189                         die("index file corrupt");
190                 pos = cache_name_pos(".gitmodules", 11);
191                 if (pos < 0) { /* .gitmodules not found or isn't merged */
192                         pos = -1 - pos;
193                         if (active_nr > pos) {  /* there is a .gitmodules */
194                                 const struct cache_entry *ce = active_cache[pos];
195                                 if (ce_namelen(ce) == 11 &&
196                                     !memcmp(ce->name, ".gitmodules", 11))
197                                         gitmodules_is_unmerged = 1;
198                         }
199                 } else if (pos < active_nr) {
200                         struct stat st;
201                         if (lstat(".gitmodules", &st) == 0 &&
202                             ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
203                                 gitmodules_is_modified = 1;
204                 }
205
206                 if (!gitmodules_is_unmerged)
207                         git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
208                 strbuf_release(&gitmodules_path);
209         }
210 }
211
212 void handle_ignore_submodules_arg(struct diff_options *diffopt,
213                                   const char *arg)
214 {
215         DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
216         DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
217         DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
218
219         if (!strcmp(arg, "all"))
220                 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
221         else if (!strcmp(arg, "untracked"))
222                 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
223         else if (!strcmp(arg, "dirty"))
224                 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
225         else if (strcmp(arg, "none"))
226                 die("bad --ignore-submodules argument: %s", arg);
227 }
228
229 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
230                 struct commit *left, struct commit *right,
231                 int *fast_forward, int *fast_backward)
232 {
233         struct commit_list *merge_bases, *list;
234
235         init_revisions(rev, NULL);
236         setup_revisions(0, NULL, rev, NULL);
237         rev->left_right = 1;
238         rev->first_parent_only = 1;
239         left->object.flags |= SYMMETRIC_LEFT;
240         add_pending_object(rev, &left->object, path);
241         add_pending_object(rev, &right->object, path);
242         merge_bases = get_merge_bases(left, right);
243         if (merge_bases) {
244                 if (merge_bases->item == left)
245                         *fast_forward = 1;
246                 else if (merge_bases->item == right)
247                         *fast_backward = 1;
248         }
249         for (list = merge_bases; list; list = list->next) {
250                 list->item->object.flags |= UNINTERESTING;
251                 add_pending_object(rev, &list->item->object,
252                         oid_to_hex(&list->item->object.oid));
253         }
254         return prepare_revision_walk(rev);
255 }
256
257 static void print_submodule_summary(struct rev_info *rev, FILE *f,
258                 const char *line_prefix,
259                 const char *del, const char *add, const char *reset)
260 {
261         static const char format[] = "  %m %s";
262         struct strbuf sb = STRBUF_INIT;
263         struct commit *commit;
264
265         while ((commit = get_revision(rev))) {
266                 struct pretty_print_context ctx = {0};
267                 ctx.date_mode = rev->date_mode;
268                 ctx.output_encoding = get_log_output_encoding();
269                 strbuf_setlen(&sb, 0);
270                 strbuf_addstr(&sb, line_prefix);
271                 if (commit->object.flags & SYMMETRIC_LEFT) {
272                         if (del)
273                                 strbuf_addstr(&sb, del);
274                 }
275                 else if (add)
276                         strbuf_addstr(&sb, add);
277                 format_commit_message(commit, format, &sb, &ctx);
278                 if (reset)
279                         strbuf_addstr(&sb, reset);
280                 strbuf_addch(&sb, '\n');
281                 fprintf(f, "%s", sb.buf);
282         }
283         strbuf_release(&sb);
284 }
285
286 void show_submodule_summary(FILE *f, const char *path,
287                 const char *line_prefix,
288                 unsigned char one[20], unsigned char two[20],
289                 unsigned dirty_submodule, const char *meta,
290                 const char *del, const char *add, const char *reset)
291 {
292         struct rev_info rev;
293         struct commit *left = NULL, *right = NULL;
294         const char *message = NULL;
295         struct strbuf sb = STRBUF_INIT;
296         int fast_forward = 0, fast_backward = 0;
297
298         if (is_null_sha1(two))
299                 message = "(submodule deleted)";
300         else if (add_submodule_odb(path))
301                 message = "(not checked out)";
302         else if (is_null_sha1(one))
303                 message = "(new submodule)";
304         else if (!(left = lookup_commit_reference(one)) ||
305                  !(right = lookup_commit_reference(two)))
306                 message = "(commits not present)";
307         else if (prepare_submodule_summary(&rev, path, left, right,
308                                            &fast_forward, &fast_backward))
309                 message = "(revision walker failed)";
310
311         if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
312                 fprintf(f, "%sSubmodule %s contains untracked content\n",
313                         line_prefix, path);
314         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
315                 fprintf(f, "%sSubmodule %s contains modified content\n",
316                         line_prefix, path);
317
318         if (!hashcmp(one, two)) {
319                 strbuf_release(&sb);
320                 return;
321         }
322
323         strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
324                         find_unique_abbrev(one, DEFAULT_ABBREV));
325         if (!fast_backward && !fast_forward)
326                 strbuf_addch(&sb, '.');
327         strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
328         if (message)
329                 strbuf_addf(&sb, " %s%s\n", message, reset);
330         else
331                 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
332         fwrite(sb.buf, sb.len, 1, f);
333
334         if (!message) /* only NULL if we succeeded in setting up the walk */
335                 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
336         if (left)
337                 clear_commit_marks(left, ~0);
338         if (right)
339                 clear_commit_marks(right, ~0);
340
341         strbuf_release(&sb);
342 }
343
344 void set_config_fetch_recurse_submodules(int value)
345 {
346         config_fetch_recurse_submodules = value;
347 }
348
349 static int has_remote(const char *refname, const struct object_id *oid,
350                       int flags, void *cb_data)
351 {
352         return 1;
353 }
354
355 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
356 {
357         if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
358                 return 0;
359
360         if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
361                 struct child_process cp = CHILD_PROCESS_INIT;
362                 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
363                 struct strbuf buf = STRBUF_INIT;
364                 int needs_pushing = 0;
365
366                 argv[1] = sha1_to_hex(sha1);
367                 cp.argv = argv;
368                 cp.env = local_repo_env;
369                 cp.git_cmd = 1;
370                 cp.no_stdin = 1;
371                 cp.out = -1;
372                 cp.dir = path;
373                 if (start_command(&cp))
374                         die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
375                                 sha1_to_hex(sha1), path);
376                 if (strbuf_read(&buf, cp.out, 41))
377                         needs_pushing = 1;
378                 finish_command(&cp);
379                 close(cp.out);
380                 strbuf_release(&buf);
381                 return needs_pushing;
382         }
383
384         return 0;
385 }
386
387 static void collect_submodules_from_diff(struct diff_queue_struct *q,
388                                          struct diff_options *options,
389                                          void *data)
390 {
391         int i;
392         struct string_list *needs_pushing = data;
393
394         for (i = 0; i < q->nr; i++) {
395                 struct diff_filepair *p = q->queue[i];
396                 if (!S_ISGITLINK(p->two->mode))
397                         continue;
398                 if (submodule_needs_pushing(p->two->path, p->two->sha1))
399                         string_list_insert(needs_pushing, p->two->path);
400         }
401 }
402
403 static void find_unpushed_submodule_commits(struct commit *commit,
404                 struct string_list *needs_pushing)
405 {
406         struct rev_info rev;
407
408         init_revisions(&rev, NULL);
409         rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
410         rev.diffopt.format_callback = collect_submodules_from_diff;
411         rev.diffopt.format_callback_data = needs_pushing;
412         diff_tree_combined_merge(commit, 1, &rev);
413 }
414
415 int find_unpushed_submodules(unsigned char new_sha1[20],
416                 const char *remotes_name, struct string_list *needs_pushing)
417 {
418         struct rev_info rev;
419         struct commit *commit;
420         const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
421         int argc = ARRAY_SIZE(argv) - 1;
422         char *sha1_copy;
423
424         struct strbuf remotes_arg = STRBUF_INIT;
425
426         strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
427         init_revisions(&rev, NULL);
428         sha1_copy = xstrdup(sha1_to_hex(new_sha1));
429         argv[1] = sha1_copy;
430         argv[3] = remotes_arg.buf;
431         setup_revisions(argc, argv, &rev, NULL);
432         if (prepare_revision_walk(&rev))
433                 die("revision walk setup failed");
434
435         while ((commit = get_revision(&rev)) != NULL)
436                 find_unpushed_submodule_commits(commit, needs_pushing);
437
438         reset_revision_walk();
439         free(sha1_copy);
440         strbuf_release(&remotes_arg);
441
442         return needs_pushing->nr;
443 }
444
445 static int push_submodule(const char *path)
446 {
447         if (add_submodule_odb(path))
448                 return 1;
449
450         if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
451                 struct child_process cp = CHILD_PROCESS_INIT;
452                 const char *argv[] = {"push", NULL};
453
454                 cp.argv = argv;
455                 cp.env = local_repo_env;
456                 cp.git_cmd = 1;
457                 cp.no_stdin = 1;
458                 cp.dir = path;
459                 if (run_command(&cp))
460                         return 0;
461                 close(cp.out);
462         }
463
464         return 1;
465 }
466
467 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
468 {
469         int i, ret = 1;
470         struct string_list needs_pushing = STRING_LIST_INIT_DUP;
471
472         if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
473                 return 1;
474
475         for (i = 0; i < needs_pushing.nr; i++) {
476                 const char *path = needs_pushing.items[i].string;
477                 fprintf(stderr, "Pushing submodule '%s'\n", path);
478                 if (!push_submodule(path)) {
479                         fprintf(stderr, "Unable to push submodule '%s'\n", path);
480                         ret = 0;
481                 }
482         }
483
484         string_list_clear(&needs_pushing, 0);
485
486         return ret;
487 }
488
489 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
490 {
491         int is_present = 0;
492         if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
493                 /* Even if the submodule is checked out and the commit is
494                  * present, make sure it is reachable from a ref. */
495                 struct child_process cp = CHILD_PROCESS_INIT;
496                 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
497                 struct strbuf buf = STRBUF_INIT;
498
499                 argv[3] = sha1_to_hex(sha1);
500                 cp.argv = argv;
501                 cp.env = local_repo_env;
502                 cp.git_cmd = 1;
503                 cp.no_stdin = 1;
504                 cp.dir = path;
505                 if (!capture_command(&cp, &buf, 1024) && !buf.len)
506                         is_present = 1;
507
508                 strbuf_release(&buf);
509         }
510         return is_present;
511 }
512
513 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
514                                          struct diff_options *options,
515                                          void *data)
516 {
517         int i;
518         for (i = 0; i < q->nr; i++) {
519                 struct diff_filepair *p = q->queue[i];
520                 if (!S_ISGITLINK(p->two->mode))
521                         continue;
522
523                 if (S_ISGITLINK(p->one->mode)) {
524                         /* NEEDSWORK: We should honor the name configured in
525                          * the .gitmodules file of the commit we are examining
526                          * here to be able to correctly follow submodules
527                          * being moved around. */
528                         struct string_list_item *path;
529                         path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
530                         if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
531                                 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
532                 } else {
533                         /* Submodule is new or was moved here */
534                         /* NEEDSWORK: When the .git directories of submodules
535                          * live inside the superprojects .git directory some
536                          * day we should fetch new submodules directly into
537                          * that location too when config or options request
538                          * that so they can be checked out from there. */
539                         continue;
540                 }
541         }
542 }
543
544 static int add_sha1_to_array(const char *ref, const struct object_id *oid,
545                              int flags, void *data)
546 {
547         sha1_array_append(data, oid->hash);
548         return 0;
549 }
550
551 void check_for_new_submodule_commits(unsigned char new_sha1[20])
552 {
553         if (!initialized_fetch_ref_tips) {
554                 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
555                 initialized_fetch_ref_tips = 1;
556         }
557
558         sha1_array_append(&ref_tips_after_fetch, new_sha1);
559 }
560
561 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
562 {
563         argv_array_push(data, sha1_to_hex(sha1));
564 }
565
566 static void calculate_changed_submodule_paths(void)
567 {
568         struct rev_info rev;
569         struct commit *commit;
570         struct argv_array argv = ARGV_ARRAY_INIT;
571
572         /* No need to check if there are no submodules configured */
573         if (!submodule_from_path(NULL, NULL))
574                 return;
575
576         init_revisions(&rev, NULL);
577         argv_array_push(&argv, "--"); /* argv[0] program name */
578         sha1_array_for_each_unique(&ref_tips_after_fetch,
579                                    add_sha1_to_argv, &argv);
580         argv_array_push(&argv, "--not");
581         sha1_array_for_each_unique(&ref_tips_before_fetch,
582                                    add_sha1_to_argv, &argv);
583         setup_revisions(argv.argc, argv.argv, &rev, NULL);
584         if (prepare_revision_walk(&rev))
585                 die("revision walk setup failed");
586
587         /*
588          * Collect all submodules (whether checked out or not) for which new
589          * commits have been recorded upstream in "changed_submodule_paths".
590          */
591         while ((commit = get_revision(&rev))) {
592                 struct commit_list *parent = commit->parents;
593                 while (parent) {
594                         struct diff_options diff_opts;
595                         diff_setup(&diff_opts);
596                         DIFF_OPT_SET(&diff_opts, RECURSIVE);
597                         diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
598                         diff_opts.format_callback = submodule_collect_changed_cb;
599                         diff_setup_done(&diff_opts);
600                         diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
601                         diffcore_std(&diff_opts);
602                         diff_flush(&diff_opts);
603                         parent = parent->next;
604                 }
605         }
606
607         argv_array_clear(&argv);
608         sha1_array_clear(&ref_tips_before_fetch);
609         sha1_array_clear(&ref_tips_after_fetch);
610         initialized_fetch_ref_tips = 0;
611 }
612
613 int fetch_populated_submodules(const struct argv_array *options,
614                                const char *prefix, int command_line_option,
615                                int quiet)
616 {
617         int i, result = 0;
618         struct child_process cp = CHILD_PROCESS_INIT;
619         struct argv_array argv = ARGV_ARRAY_INIT;
620         const char *work_tree = get_git_work_tree();
621         if (!work_tree)
622                 goto out;
623
624         if (read_cache() < 0)
625                 die("index file corrupt");
626
627         argv_array_push(&argv, "fetch");
628         for (i = 0; i < options->argc; i++)
629                 argv_array_push(&argv, options->argv[i]);
630         argv_array_push(&argv, "--recurse-submodules-default");
631         /* default value, "--submodule-prefix" and its value are added later */
632
633         cp.env = local_repo_env;
634         cp.git_cmd = 1;
635         cp.no_stdin = 1;
636
637         calculate_changed_submodule_paths();
638
639         for (i = 0; i < active_nr; i++) {
640                 struct strbuf submodule_path = STRBUF_INIT;
641                 struct strbuf submodule_git_dir = STRBUF_INIT;
642                 struct strbuf submodule_prefix = STRBUF_INIT;
643                 const struct cache_entry *ce = active_cache[i];
644                 const char *git_dir, *default_argv;
645                 const struct submodule *submodule;
646
647                 if (!S_ISGITLINK(ce->ce_mode))
648                         continue;
649
650                 submodule = submodule_from_path(null_sha1, ce->name);
651                 if (!submodule)
652                         submodule = submodule_from_name(null_sha1, ce->name);
653
654                 default_argv = "yes";
655                 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
656                         if (submodule &&
657                             submodule->fetch_recurse !=
658                                                 RECURSE_SUBMODULES_NONE) {
659                                 if (submodule->fetch_recurse ==
660                                                 RECURSE_SUBMODULES_OFF)
661                                         continue;
662                                 if (submodule->fetch_recurse ==
663                                                 RECURSE_SUBMODULES_ON_DEMAND) {
664                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
665                                                 continue;
666                                         default_argv = "on-demand";
667                                 }
668                         } else {
669                                 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
670                                     gitmodules_is_unmerged)
671                                         continue;
672                                 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
673                                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
674                                                 continue;
675                                         default_argv = "on-demand";
676                                 }
677                         }
678                 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
679                         if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
680                                 continue;
681                         default_argv = "on-demand";
682                 }
683
684                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
685                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
686                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
687                 git_dir = read_gitfile(submodule_git_dir.buf);
688                 if (!git_dir)
689                         git_dir = submodule_git_dir.buf;
690                 if (is_directory(git_dir)) {
691                         if (!quiet)
692                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
693                         cp.dir = submodule_path.buf;
694                         argv_array_push(&argv, default_argv);
695                         argv_array_push(&argv, "--submodule-prefix");
696                         argv_array_push(&argv, submodule_prefix.buf);
697                         cp.argv = argv.argv;
698                         if (run_command(&cp))
699                                 result = 1;
700                         argv_array_pop(&argv);
701                         argv_array_pop(&argv);
702                         argv_array_pop(&argv);
703                 }
704                 strbuf_release(&submodule_path);
705                 strbuf_release(&submodule_git_dir);
706                 strbuf_release(&submodule_prefix);
707         }
708         argv_array_clear(&argv);
709 out:
710         string_list_clear(&changed_submodule_paths, 1);
711         return result;
712 }
713
714 unsigned is_submodule_modified(const char *path, int ignore_untracked)
715 {
716         ssize_t len;
717         struct child_process cp = CHILD_PROCESS_INIT;
718         const char *argv[] = {
719                 "status",
720                 "--porcelain",
721                 NULL,
722                 NULL,
723         };
724         struct strbuf buf = STRBUF_INIT;
725         unsigned dirty_submodule = 0;
726         const char *line, *next_line;
727         const char *git_dir;
728
729         strbuf_addf(&buf, "%s/.git", path);
730         git_dir = read_gitfile(buf.buf);
731         if (!git_dir)
732                 git_dir = buf.buf;
733         if (!is_directory(git_dir)) {
734                 strbuf_release(&buf);
735                 /* The submodule is not checked out, so it is not modified */
736                 return 0;
737
738         }
739         strbuf_reset(&buf);
740
741         if (ignore_untracked)
742                 argv[2] = "-uno";
743
744         cp.argv = argv;
745         cp.env = local_repo_env;
746         cp.git_cmd = 1;
747         cp.no_stdin = 1;
748         cp.out = -1;
749         cp.dir = path;
750         if (start_command(&cp))
751                 die("Could not run 'git status --porcelain' in submodule %s", path);
752
753         len = strbuf_read(&buf, cp.out, 1024);
754         line = buf.buf;
755         while (len > 2) {
756                 if ((line[0] == '?') && (line[1] == '?')) {
757                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
758                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
759                                 break;
760                 } else {
761                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
762                         if (ignore_untracked ||
763                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
764                                 break;
765                 }
766                 next_line = strchr(line, '\n');
767                 if (!next_line)
768                         break;
769                 next_line++;
770                 len -= (next_line - line);
771                 line = next_line;
772         }
773         close(cp.out);
774
775         if (finish_command(&cp))
776                 die("'git status --porcelain' failed in submodule %s", path);
777
778         strbuf_release(&buf);
779         return dirty_submodule;
780 }
781
782 int submodule_uses_gitfile(const char *path)
783 {
784         struct child_process cp = CHILD_PROCESS_INIT;
785         const char *argv[] = {
786                 "submodule",
787                 "foreach",
788                 "--quiet",
789                 "--recursive",
790                 "test -f .git",
791                 NULL,
792         };
793         struct strbuf buf = STRBUF_INIT;
794         const char *git_dir;
795
796         strbuf_addf(&buf, "%s/.git", path);
797         git_dir = read_gitfile(buf.buf);
798         if (!git_dir) {
799                 strbuf_release(&buf);
800                 return 0;
801         }
802         strbuf_release(&buf);
803
804         /* Now test that all nested submodules use a gitfile too */
805         cp.argv = argv;
806         cp.env = local_repo_env;
807         cp.git_cmd = 1;
808         cp.no_stdin = 1;
809         cp.no_stderr = 1;
810         cp.no_stdout = 1;
811         cp.dir = path;
812         if (run_command(&cp))
813                 return 0;
814
815         return 1;
816 }
817
818 int ok_to_remove_submodule(const char *path)
819 {
820         ssize_t len;
821         struct child_process cp = CHILD_PROCESS_INIT;
822         const char *argv[] = {
823                 "status",
824                 "--porcelain",
825                 "-u",
826                 "--ignore-submodules=none",
827                 NULL,
828         };
829         struct strbuf buf = STRBUF_INIT;
830         int ok_to_remove = 1;
831
832         if (!file_exists(path) || is_empty_dir(path))
833                 return 1;
834
835         if (!submodule_uses_gitfile(path))
836                 return 0;
837
838         cp.argv = argv;
839         cp.env = local_repo_env;
840         cp.git_cmd = 1;
841         cp.no_stdin = 1;
842         cp.out = -1;
843         cp.dir = path;
844         if (start_command(&cp))
845                 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
846
847         len = strbuf_read(&buf, cp.out, 1024);
848         if (len > 2)
849                 ok_to_remove = 0;
850         close(cp.out);
851
852         if (finish_command(&cp))
853                 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
854
855         strbuf_release(&buf);
856         return ok_to_remove;
857 }
858
859 static int find_first_merges(struct object_array *result, const char *path,
860                 struct commit *a, struct commit *b)
861 {
862         int i, j;
863         struct object_array merges = OBJECT_ARRAY_INIT;
864         struct commit *commit;
865         int contains_another;
866
867         char merged_revision[42];
868         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
869                                    "--all", merged_revision, NULL };
870         struct rev_info revs;
871         struct setup_revision_opt rev_opts;
872
873         memset(result, 0, sizeof(struct object_array));
874         memset(&rev_opts, 0, sizeof(rev_opts));
875
876         /* get all revisions that merge commit a */
877         snprintf(merged_revision, sizeof(merged_revision), "^%s",
878                         oid_to_hex(&a->object.oid));
879         init_revisions(&revs, NULL);
880         rev_opts.submodule = path;
881         setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
882
883         /* save all revisions from the above list that contain b */
884         if (prepare_revision_walk(&revs))
885                 die("revision walk setup failed");
886         while ((commit = get_revision(&revs)) != NULL) {
887                 struct object *o = &(commit->object);
888                 if (in_merge_bases(b, commit))
889                         add_object_array(o, NULL, &merges);
890         }
891         reset_revision_walk();
892
893         /* Now we've got all merges that contain a and b. Prune all
894          * merges that contain another found merge and save them in
895          * result.
896          */
897         for (i = 0; i < merges.nr; i++) {
898                 struct commit *m1 = (struct commit *) merges.objects[i].item;
899
900                 contains_another = 0;
901                 for (j = 0; j < merges.nr; j++) {
902                         struct commit *m2 = (struct commit *) merges.objects[j].item;
903                         if (i != j && in_merge_bases(m2, m1)) {
904                                 contains_another = 1;
905                                 break;
906                         }
907                 }
908
909                 if (!contains_another)
910                         add_object_array(merges.objects[i].item, NULL, result);
911         }
912
913         free(merges.objects);
914         return result->nr;
915 }
916
917 static void print_commit(struct commit *commit)
918 {
919         struct strbuf sb = STRBUF_INIT;
920         struct pretty_print_context ctx = {0};
921         ctx.date_mode.type = DATE_NORMAL;
922         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
923         fprintf(stderr, "%s\n", sb.buf);
924         strbuf_release(&sb);
925 }
926
927 #define MERGE_WARNING(path, msg) \
928         warning("Failed to merge submodule %s (%s)", path, msg);
929
930 int merge_submodule(unsigned char result[20], const char *path,
931                     const unsigned char base[20], const unsigned char a[20],
932                     const unsigned char b[20], int search)
933 {
934         struct commit *commit_base, *commit_a, *commit_b;
935         int parent_count;
936         struct object_array merges;
937
938         int i;
939
940         /* store a in result in case we fail */
941         hashcpy(result, a);
942
943         /* we can not handle deletion conflicts */
944         if (is_null_sha1(base))
945                 return 0;
946         if (is_null_sha1(a))
947                 return 0;
948         if (is_null_sha1(b))
949                 return 0;
950
951         if (add_submodule_odb(path)) {
952                 MERGE_WARNING(path, "not checked out");
953                 return 0;
954         }
955
956         if (!(commit_base = lookup_commit_reference(base)) ||
957             !(commit_a = lookup_commit_reference(a)) ||
958             !(commit_b = lookup_commit_reference(b))) {
959                 MERGE_WARNING(path, "commits not present");
960                 return 0;
961         }
962
963         /* check whether both changes are forward */
964         if (!in_merge_bases(commit_base, commit_a) ||
965             !in_merge_bases(commit_base, commit_b)) {
966                 MERGE_WARNING(path, "commits don't follow merge-base");
967                 return 0;
968         }
969
970         /* Case #1: a is contained in b or vice versa */
971         if (in_merge_bases(commit_a, commit_b)) {
972                 hashcpy(result, b);
973                 return 1;
974         }
975         if (in_merge_bases(commit_b, commit_a)) {
976                 hashcpy(result, a);
977                 return 1;
978         }
979
980         /*
981          * Case #2: There are one or more merges that contain a and b in
982          * the submodule. If there is only one, then present it as a
983          * suggestion to the user, but leave it marked unmerged so the
984          * user needs to confirm the resolution.
985          */
986
987         /* Skip the search if makes no sense to the calling context.  */
988         if (!search)
989                 return 0;
990
991         /* find commit which merges them */
992         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
993         switch (parent_count) {
994         case 0:
995                 MERGE_WARNING(path, "merge following commits not found");
996                 break;
997
998         case 1:
999                 MERGE_WARNING(path, "not fast-forward");
1000                 fprintf(stderr, "Found a possible merge resolution "
1001                                 "for the submodule:\n");
1002                 print_commit((struct commit *) merges.objects[0].item);
1003                 fprintf(stderr,
1004                         "If this is correct simply add it to the index "
1005                         "for example\n"
1006                         "by using:\n\n"
1007                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1008                         "which will accept this suggestion.\n",
1009                         oid_to_hex(&merges.objects[0].item->oid), path);
1010                 break;
1011
1012         default:
1013                 MERGE_WARNING(path, "multiple merges found");
1014                 for (i = 0; i < merges.nr; i++)
1015                         print_commit((struct commit *) merges.objects[i].item);
1016         }
1017
1018         free(merges.objects);
1019         return 0;
1020 }
1021
1022 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1023 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1024 {
1025         struct strbuf file_name = STRBUF_INIT;
1026         struct strbuf rel_path = STRBUF_INIT;
1027         const char *real_work_tree = xstrdup(real_path(work_tree));
1028
1029         /* Update gitfile */
1030         strbuf_addf(&file_name, "%s/.git", work_tree);
1031         write_file(file_name.buf, "gitdir: %s",
1032                    relative_path(git_dir, real_work_tree, &rel_path));
1033
1034         /* Update core.worktree setting */
1035         strbuf_reset(&file_name);
1036         strbuf_addf(&file_name, "%s/config", git_dir);
1037         if (git_config_set_in_file(file_name.buf, "core.worktree",
1038                                    relative_path(real_work_tree, git_dir,
1039                                                  &rel_path)))
1040                 die(_("Could not set core.worktree in %s"),
1041                     file_name.buf);
1042
1043         strbuf_release(&file_name);
1044         strbuf_release(&rel_path);
1045         free((void *)real_work_tree);
1046 }