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