]> git.ipfire.org Git - thirdparty/git.git/blob - submodule.c
fetch/pull: Add the --recurse-submodules option
[thirdparty/git.git] / submodule.c
1 #include "cache.h"
2 #include "submodule.h"
3 #include "dir.h"
4 #include "diff.h"
5 #include "commit.h"
6 #include "revision.h"
7 #include "run-command.h"
8 #include "diffcore.h"
9 #include "refs.h"
10 #include "string-list.h"
11
12 struct string_list config_name_for_path;
13 struct string_list config_ignore_for_name;
14
15 static int add_submodule_odb(const char *path)
16 {
17 struct strbuf objects_directory = STRBUF_INIT;
18 struct alternate_object_database *alt_odb;
19 int ret = 0;
20 const char *git_dir;
21
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/");
29 if (!is_directory(objects_directory.buf)) {
30 ret = -1;
31 goto done;
32 }
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))
38 goto done;
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();
49 done:
50 strbuf_release(&objects_directory);
51 return ret;
52 }
53
54 void 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
66 int submodule_config(const char *var, const char *value, void *cb)
67 {
68 if (!prefixcmp(var, "submodule."))
69 return parse_submodule_config_option(var, value);
70 return 0;
71 }
72
73 void 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
85 int 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
124 void handle_ignore_submodules_arg(struct diff_options *diffopt,
125 const char *arg)
126 {
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
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);
137 else if (strcmp(arg, "none"))
138 die("bad --ignore-submodules argument: %s", arg);
139 }
140
141 void show_submodule_summary(FILE *f, const char *path,
142 unsigned char one[20], unsigned char two[20],
143 unsigned dirty_submodule,
144 const char *del, const char *add, const char *reset)
145 {
146 struct rev_info rev;
147 struct commit *commit, *left = left, *right = right;
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
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
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))) {
211 struct pretty_print_context ctx = {0};
212 ctx.date_mode = rev.date_mode;
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);
220 format_commit_message(commit, format, &sb, &ctx);
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 }
231
232 int 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
296 unsigned is_submodule_modified(const char *path, int ignore_untracked)
297 {
298 ssize_t len;
299 struct child_process cp;
300 const char *argv[] = {
301 "status",
302 "--porcelain",
303 NULL,
304 NULL,
305 };
306 struct strbuf buf = STRBUF_INIT;
307 unsigned dirty_submodule = 0;
308 const char *line, *next_line;
309 const char *git_dir;
310
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)) {
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
323 if (ignore_untracked)
324 argv[2] = "-uno";
325
326 memset(&cp, 0, sizeof(cp));
327 cp.argv = argv;
328 cp.env = local_repo_env;
329 cp.git_cmd = 1;
330 cp.no_stdin = 1;
331 cp.out = -1;
332 cp.dir = path;
333 if (start_command(&cp))
334 die("Could not run git status --porcelain");
335
336 len = strbuf_read(&buf, cp.out, 1024);
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;
345 if (ignore_untracked ||
346 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
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 }
356 close(cp.out);
357
358 if (finish_command(&cp))
359 die("git status --porcelain failed");
360
361 strbuf_release(&buf);
362 return dirty_submodule;
363 }
364
365 static 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
424 static 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
437 int 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 }