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