Merge branch 'jc/fetch-progressive-stride'
[git] / submodule.c
1 #include "cache.h"
2 #include "submodule.h"
3 #include "dir.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "run-command.h"
8 #include "diffcore.h"
9 #include "refs.h"
10 #include "string-list.h"
11
12 static struct string_list config_name_for_path;
13 static struct string_list config_fetch_recurse_submodules_for_name;
14 static struct string_list config_ignore_for_name;
15 static int config_fetch_recurse_submodules;
16
17 static int add_submodule_odb(const char *path)
18 {
19         struct strbuf objects_directory = STRBUF_INIT;
20         struct alternate_object_database *alt_odb;
21         int ret = 0;
22         const char *git_dir;
23
24         strbuf_addf(&objects_directory, "%s/.git", path);
25         git_dir = read_gitfile_gently(objects_directory.buf);
26         if (git_dir) {
27                 strbuf_reset(&objects_directory);
28                 strbuf_addstr(&objects_directory, git_dir);
29         }
30         strbuf_addstr(&objects_directory, "/objects/");
31         if (!is_directory(objects_directory.buf)) {
32                 ret = -1;
33                 goto done;
34         }
35         /* avoid adding it twice */
36         for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
37                 if (alt_odb->name - alt_odb->base == objects_directory.len &&
38                                 !strncmp(alt_odb->base, objects_directory.buf,
39                                         objects_directory.len))
40                         goto done;
41
42         alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
43         alt_odb->next = alt_odb_list;
44         strcpy(alt_odb->base, objects_directory.buf);
45         alt_odb->name = alt_odb->base + objects_directory.len;
46         alt_odb->name[2] = '/';
47         alt_odb->name[40] = '\0';
48         alt_odb->name[41] = '\0';
49         alt_odb_list = alt_odb;
50         prepare_alt_odb();
51 done:
52         strbuf_release(&objects_directory);
53         return ret;
54 }
55
56 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
57                                              const char *path)
58 {
59         struct string_list_item *path_option, *ignore_option;
60         path_option = unsorted_string_list_lookup(&config_name_for_path, path);
61         if (path_option) {
62                 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
63                 if (ignore_option)
64                         handle_ignore_submodules_arg(diffopt, ignore_option->util);
65         }
66 }
67
68 int submodule_config(const char *var, const char *value, void *cb)
69 {
70         if (!prefixcmp(var, "submodule."))
71                 return parse_submodule_config_option(var, value);
72         else if (!strcmp(var, "fetch.recursesubmodules")) {
73                 config_fetch_recurse_submodules = git_config_bool(var, value);
74                 return 0;
75         }
76         return 0;
77 }
78
79 void gitmodules_config(void)
80 {
81         const char *work_tree = get_git_work_tree();
82         if (work_tree) {
83                 struct strbuf gitmodules_path = STRBUF_INIT;
84                 strbuf_addstr(&gitmodules_path, work_tree);
85                 strbuf_addstr(&gitmodules_path, "/.gitmodules");
86                 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
87                 strbuf_release(&gitmodules_path);
88         }
89 }
90
91 int parse_submodule_config_option(const char *var, const char *value)
92 {
93         int len;
94         struct string_list_item *config;
95         struct strbuf submodname = STRBUF_INIT;
96
97         var += 10;              /* Skip "submodule." */
98
99         len = strlen(var);
100         if ((len > 5) && !strcmp(var + len - 5, ".path")) {
101                 strbuf_add(&submodname, var, len - 5);
102                 config = unsorted_string_list_lookup(&config_name_for_path, value);
103                 if (config)
104                         free(config->util);
105                 else
106                         config = string_list_append(&config_name_for_path, xstrdup(value));
107                 config->util = strbuf_detach(&submodname, NULL);
108                 strbuf_release(&submodname);
109         } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) {
110                 strbuf_add(&submodname, var, len - 23);
111                 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf);
112                 if (!config)
113                         config = string_list_append(&config_fetch_recurse_submodules_for_name,
114                                                     strbuf_detach(&submodname, NULL));
115                 config->util = git_config_bool(var, value) ? (void *)1 : NULL;
116                 strbuf_release(&submodname);
117         } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) {
118                 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
119                     strcmp(value, "all") && strcmp(value, "none")) {
120                         warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
121                         return 0;
122                 }
123
124                 strbuf_add(&submodname, var, len - 7);
125                 config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf);
126                 if (config)
127                         free(config->util);
128                 else
129                         config = string_list_append(&config_ignore_for_name,
130                                                     strbuf_detach(&submodname, NULL));
131                 strbuf_release(&submodname);
132                 config->util = xstrdup(value);
133                 return 0;
134         }
135         return 0;
136 }
137
138 void handle_ignore_submodules_arg(struct diff_options *diffopt,
139                                   const char *arg)
140 {
141         DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
142         DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
143         DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
144
145         if (!strcmp(arg, "all"))
146                 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
147         else if (!strcmp(arg, "untracked"))
148                 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
149         else if (!strcmp(arg, "dirty"))
150                 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
151         else if (strcmp(arg, "none"))
152                 die("bad --ignore-submodules argument: %s", arg);
153 }
154
155 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
156                 struct commit *left, struct commit *right,
157                 int *fast_forward, int *fast_backward)
158 {
159         struct commit_list *merge_bases, *list;
160
161         init_revisions(rev, NULL);
162         setup_revisions(0, NULL, rev, NULL);
163         rev->left_right = 1;
164         rev->first_parent_only = 1;
165         left->object.flags |= SYMMETRIC_LEFT;
166         add_pending_object(rev, &left->object, path);
167         add_pending_object(rev, &right->object, path);
168         merge_bases = get_merge_bases(left, right, 1);
169         if (merge_bases) {
170                 if (merge_bases->item == left)
171                         *fast_forward = 1;
172                 else if (merge_bases->item == right)
173                         *fast_backward = 1;
174         }
175         for (list = merge_bases; list; list = list->next) {
176                 list->item->object.flags |= UNINTERESTING;
177                 add_pending_object(rev, &list->item->object,
178                         sha1_to_hex(list->item->object.sha1));
179         }
180         return prepare_revision_walk(rev);
181 }
182
183 static void print_submodule_summary(struct rev_info *rev, FILE *f,
184                 const char *del, const char *add, const char *reset)
185 {
186         static const char format[] = "  %m %s";
187         struct strbuf sb = STRBUF_INIT;
188         struct commit *commit;
189
190         while ((commit = get_revision(rev))) {
191                 struct pretty_print_context ctx = {0};
192                 ctx.date_mode = rev->date_mode;
193                 strbuf_setlen(&sb, 0);
194                 if (commit->object.flags & SYMMETRIC_LEFT) {
195                         if (del)
196                                 strbuf_addstr(&sb, del);
197                 }
198                 else if (add)
199                         strbuf_addstr(&sb, add);
200                 format_commit_message(commit, format, &sb, &ctx);
201                 if (reset)
202                         strbuf_addstr(&sb, reset);
203                 strbuf_addch(&sb, '\n');
204                 fprintf(f, "%s", sb.buf);
205         }
206         strbuf_release(&sb);
207 }
208
209 void show_submodule_summary(FILE *f, const char *path,
210                 unsigned char one[20], unsigned char two[20],
211                 unsigned dirty_submodule,
212                 const char *del, const char *add, const char *reset)
213 {
214         struct rev_info rev;
215         struct commit *left = left, *right = right;
216         const char *message = NULL;
217         struct strbuf sb = STRBUF_INIT;
218         int fast_forward = 0, fast_backward = 0;
219
220         if (is_null_sha1(two))
221                 message = "(submodule deleted)";
222         else if (add_submodule_odb(path))
223                 message = "(not checked out)";
224         else if (is_null_sha1(one))
225                 message = "(new submodule)";
226         else if (!(left = lookup_commit_reference(one)) ||
227                  !(right = lookup_commit_reference(two)))
228                 message = "(commits not present)";
229
230         if (!message &&
231             prepare_submodule_summary(&rev, path, left, right,
232                                         &fast_forward, &fast_backward))
233                 message = "(revision walker failed)";
234
235         if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
236                 fprintf(f, "Submodule %s contains untracked content\n", path);
237         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
238                 fprintf(f, "Submodule %s contains modified content\n", path);
239
240         if (!hashcmp(one, two)) {
241                 strbuf_release(&sb);
242                 return;
243         }
244
245         strbuf_addf(&sb, "Submodule %s %s..", path,
246                         find_unique_abbrev(one, DEFAULT_ABBREV));
247         if (!fast_backward && !fast_forward)
248                 strbuf_addch(&sb, '.');
249         strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
250         if (message)
251                 strbuf_addf(&sb, " %s\n", message);
252         else
253                 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
254         fwrite(sb.buf, sb.len, 1, f);
255
256         if (!message) {
257                 print_submodule_summary(&rev, f, del, add, reset);
258                 clear_commit_marks(left, ~0);
259                 clear_commit_marks(right, ~0);
260         }
261
262         strbuf_release(&sb);
263 }
264
265 void set_config_fetch_recurse_submodules(int value)
266 {
267         config_fetch_recurse_submodules = value;
268 }
269
270 int fetch_populated_submodules(int num_options, const char **options,
271                                const char *prefix, int ignore_config,
272                                int quiet)
273 {
274         int i, result = 0, argc = 0;
275         struct child_process cp;
276         const char **argv;
277         struct string_list_item *name_for_path;
278         const char *work_tree = get_git_work_tree();
279         if (!work_tree)
280                 return 0;
281
282         if (!the_index.initialized)
283                 if (read_cache() < 0)
284                         die("index file corrupt");
285
286         /* 4: "fetch" (options) "--submodule-prefix" prefix NULL */
287         argv = xcalloc(num_options + 4, sizeof(const char *));
288         argv[argc++] = "fetch";
289         for (i = 0; i < num_options; i++)
290                 argv[argc++] = options[i];
291         argv[argc++] = "--submodule-prefix";
292
293         memset(&cp, 0, sizeof(cp));
294         cp.argv = argv;
295         cp.env = local_repo_env;
296         cp.git_cmd = 1;
297         cp.no_stdin = 1;
298
299         for (i = 0; i < active_nr; i++) {
300                 struct strbuf submodule_path = STRBUF_INIT;
301                 struct strbuf submodule_git_dir = STRBUF_INIT;
302                 struct strbuf submodule_prefix = STRBUF_INIT;
303                 struct cache_entry *ce = active_cache[i];
304                 const char *git_dir, *name;
305
306                 if (!S_ISGITLINK(ce->ce_mode))
307                         continue;
308
309                 name = ce->name;
310                 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
311                 if (name_for_path)
312                         name = name_for_path->util;
313
314                 if (!ignore_config) {
315                         struct string_list_item *fetch_recurse_submodules_option;
316                         fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
317                         if (fetch_recurse_submodules_option) {
318                                 if (!fetch_recurse_submodules_option->util)
319                                         continue;
320                         } else {
321                                 if (!config_fetch_recurse_submodules)
322                                         continue;
323                         }
324                 }
325
326                 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
327                 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
328                 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
329                 git_dir = read_gitfile_gently(submodule_git_dir.buf);
330                 if (!git_dir)
331                         git_dir = submodule_git_dir.buf;
332                 if (is_directory(git_dir)) {
333                         if (!quiet)
334                                 printf("Fetching submodule %s%s\n", prefix, ce->name);
335                         cp.dir = submodule_path.buf;
336                         argv[argc] = submodule_prefix.buf;
337                         if (run_command(&cp))
338                                 result = 1;
339                 }
340                 strbuf_release(&submodule_path);
341                 strbuf_release(&submodule_git_dir);
342                 strbuf_release(&submodule_prefix);
343         }
344         free(argv);
345         return result;
346 }
347
348 unsigned is_submodule_modified(const char *path, int ignore_untracked)
349 {
350         ssize_t len;
351         struct child_process cp;
352         const char *argv[] = {
353                 "status",
354                 "--porcelain",
355                 NULL,
356                 NULL,
357         };
358         struct strbuf buf = STRBUF_INIT;
359         unsigned dirty_submodule = 0;
360         const char *line, *next_line;
361         const char *git_dir;
362
363         strbuf_addf(&buf, "%s/.git", path);
364         git_dir = read_gitfile_gently(buf.buf);
365         if (!git_dir)
366                 git_dir = buf.buf;
367         if (!is_directory(git_dir)) {
368                 strbuf_release(&buf);
369                 /* The submodule is not checked out, so it is not modified */
370                 return 0;
371
372         }
373         strbuf_reset(&buf);
374
375         if (ignore_untracked)
376                 argv[2] = "-uno";
377
378         memset(&cp, 0, sizeof(cp));
379         cp.argv = argv;
380         cp.env = local_repo_env;
381         cp.git_cmd = 1;
382         cp.no_stdin = 1;
383         cp.out = -1;
384         cp.dir = path;
385         if (start_command(&cp))
386                 die("Could not run git status --porcelain");
387
388         len = strbuf_read(&buf, cp.out, 1024);
389         line = buf.buf;
390         while (len > 2) {
391                 if ((line[0] == '?') && (line[1] == '?')) {
392                         dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
393                         if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
394                                 break;
395                 } else {
396                         dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
397                         if (ignore_untracked ||
398                             (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
399                                 break;
400                 }
401                 next_line = strchr(line, '\n');
402                 if (!next_line)
403                         break;
404                 next_line++;
405                 len -= (next_line - line);
406                 line = next_line;
407         }
408         close(cp.out);
409
410         if (finish_command(&cp))
411                 die("git status --porcelain failed");
412
413         strbuf_release(&buf);
414         return dirty_submodule;
415 }
416
417 static int find_first_merges(struct object_array *result, const char *path,
418                 struct commit *a, struct commit *b)
419 {
420         int i, j;
421         struct object_array merges;
422         struct commit *commit;
423         int contains_another;
424
425         char merged_revision[42];
426         const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
427                                    "--all", merged_revision, NULL };
428         struct rev_info revs;
429         struct setup_revision_opt rev_opts;
430
431         memset(&merges, 0, sizeof(merges));
432         memset(result, 0, sizeof(struct object_array));
433         memset(&rev_opts, 0, sizeof(rev_opts));
434
435         /* get all revisions that merge commit a */
436         snprintf(merged_revision, sizeof(merged_revision), "^%s",
437                         sha1_to_hex(a->object.sha1));
438         init_revisions(&revs, NULL);
439         rev_opts.submodule = path;
440         setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
441
442         /* save all revisions from the above list that contain b */
443         if (prepare_revision_walk(&revs))
444                 die("revision walk setup failed");
445         while ((commit = get_revision(&revs)) != NULL) {
446                 struct object *o = &(commit->object);
447                 if (in_merge_bases(b, &commit, 1))
448                         add_object_array(o, NULL, &merges);
449         }
450
451         /* Now we've got all merges that contain a and b. Prune all
452          * merges that contain another found merge and save them in
453          * result.
454          */
455         for (i = 0; i < merges.nr; i++) {
456                 struct commit *m1 = (struct commit *) merges.objects[i].item;
457
458                 contains_another = 0;
459                 for (j = 0; j < merges.nr; j++) {
460                         struct commit *m2 = (struct commit *) merges.objects[j].item;
461                         if (i != j && in_merge_bases(m2, &m1, 1)) {
462                                 contains_another = 1;
463                                 break;
464                         }
465                 }
466
467                 if (!contains_another)
468                         add_object_array(merges.objects[i].item,
469                                          merges.objects[i].name, result);
470         }
471
472         free(merges.objects);
473         return result->nr;
474 }
475
476 static void print_commit(struct commit *commit)
477 {
478         struct strbuf sb = STRBUF_INIT;
479         struct pretty_print_context ctx = {0};
480         ctx.date_mode = DATE_NORMAL;
481         format_commit_message(commit, " %h: %m %s", &sb, &ctx);
482         fprintf(stderr, "%s\n", sb.buf);
483         strbuf_release(&sb);
484 }
485
486 #define MERGE_WARNING(path, msg) \
487         warning("Failed to merge submodule %s (%s)", path, msg);
488
489 int merge_submodule(unsigned char result[20], const char *path,
490                     const unsigned char base[20], const unsigned char a[20],
491                     const unsigned char b[20])
492 {
493         struct commit *commit_base, *commit_a, *commit_b;
494         int parent_count;
495         struct object_array merges;
496
497         int i;
498
499         /* store a in result in case we fail */
500         hashcpy(result, a);
501
502         /* we can not handle deletion conflicts */
503         if (is_null_sha1(base))
504                 return 0;
505         if (is_null_sha1(a))
506                 return 0;
507         if (is_null_sha1(b))
508                 return 0;
509
510         if (add_submodule_odb(path)) {
511                 MERGE_WARNING(path, "not checked out");
512                 return 0;
513         }
514
515         if (!(commit_base = lookup_commit_reference(base)) ||
516             !(commit_a = lookup_commit_reference(a)) ||
517             !(commit_b = lookup_commit_reference(b))) {
518                 MERGE_WARNING(path, "commits not present");
519                 return 0;
520         }
521
522         /* check whether both changes are forward */
523         if (!in_merge_bases(commit_base, &commit_a, 1) ||
524             !in_merge_bases(commit_base, &commit_b, 1)) {
525                 MERGE_WARNING(path, "commits don't follow merge-base");
526                 return 0;
527         }
528
529         /* Case #1: a is contained in b or vice versa */
530         if (in_merge_bases(commit_a, &commit_b, 1)) {
531                 hashcpy(result, b);
532                 return 1;
533         }
534         if (in_merge_bases(commit_b, &commit_a, 1)) {
535                 hashcpy(result, a);
536                 return 1;
537         }
538
539         /*
540          * Case #2: There are one or more merges that contain a and b in
541          * the submodule. If there is only one, then present it as a
542          * suggestion to the user, but leave it marked unmerged so the
543          * user needs to confirm the resolution.
544          */
545
546         /* find commit which merges them */
547         parent_count = find_first_merges(&merges, path, commit_a, commit_b);
548         switch (parent_count) {
549         case 0:
550                 MERGE_WARNING(path, "merge following commits not found");
551                 break;
552
553         case 1:
554                 MERGE_WARNING(path, "not fast-forward");
555                 fprintf(stderr, "Found a possible merge resolution "
556                                 "for the submodule:\n");
557                 print_commit((struct commit *) merges.objects[0].item);
558                 fprintf(stderr,
559                         "If this is correct simply add it to the index "
560                         "for example\n"
561                         "by using:\n\n"
562                         "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"
563                         "which will accept this suggestion.\n",
564                         sha1_to_hex(merges.objects[0].item->sha1), path);
565                 break;
566
567         default:
568                 MERGE_WARNING(path, "multiple merges found");
569                 for (i = 0; i < merges.nr; i++)
570                         print_commit((struct commit *) merges.objects[i].item);
571         }
572
573         free(merges.objects);
574         return 0;
575 }