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