repack: respect pack.writebitmaps
[git/git-svn.git] / submodule.c
blob1905d75b2b09ee7e1503b35bf072710a659ea1ef
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 #include "sha1-array.h"
12 #include "argv-array.h"
13 #include "blob.h"
15 static struct string_list config_name_for_path;
16 static struct string_list config_fetch_recurse_submodules_for_name;
17 static struct string_list config_ignore_for_name;
18 static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
19 static struct string_list changed_submodule_paths;
20 static int initialized_fetch_ref_tips;
21 static struct sha1_array ref_tips_before_fetch;
22 static struct sha1_array ref_tips_after_fetch;
25 * The following flag is set if the .gitmodules file is unmerged. We then
26 * disable recursion for all submodules where .git/config doesn't have a
27 * matching config entry because we can't guess what might be configured in
28 * .gitmodules unless the user resolves the conflict. When a command line
29 * option is given (which always overrides configuration) this flag will be
30 * ignored.
32 static int gitmodules_is_unmerged;
35 * This flag is set if the .gitmodules file had unstaged modifications on
36 * startup. This must be checked before allowing modifications to the
37 * .gitmodules file with the intention to stage them later, because when
38 * continuing we would stage the modifications the user didn't stage herself
39 * too. That might change in a future version when we learn to stage the
40 * changes we do ourselves without staging any previous modifications.
42 static int gitmodules_is_modified;
45 int is_staging_gitmodules_ok(void)
47 return !gitmodules_is_modified;
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.
55 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
57 struct strbuf entry = STRBUF_INIT;
58 struct string_list_item *path_option;
60 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
61 return -1;
63 if (gitmodules_is_unmerged)
64 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
66 path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);
67 if (!path_option) {
68 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
69 return -1;
71 strbuf_addstr(&entry, "submodule.");
72 strbuf_addstr(&entry, path_option->util);
73 strbuf_addstr(&entry, ".path");
74 if (git_config_set_in_file(".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;
80 strbuf_release(&entry);
81 return 0;
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.
89 int remove_path_from_gitmodules(const char *path)
91 struct strbuf sect = STRBUF_INIT;
92 struct string_list_item *path_option;
94 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
95 return -1;
97 if (gitmodules_is_unmerged)
98 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
100 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
101 if (!path_option) {
102 warning(_("Could not find section in .gitmodules where path=%s"), path);
103 return -1;
105 strbuf_addstr(&sect, "submodule.");
106 strbuf_addstr(&sect, path_option->util);
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;
113 strbuf_release(&sect);
114 return 0;
117 void stage_updated_gitmodules(void)
119 struct strbuf buf = STRBUF_INIT;
120 struct stat st;
121 int pos;
122 struct cache_entry *ce;
123 int namelen = strlen(".gitmodules");
125 pos = cache_name_pos(".gitmodules", namelen);
126 if (pos < 0) {
127 warning(_("could not find .gitmodules in index"));
128 return;
130 ce = active_cache[pos];
131 ce->ce_flags = namelen;
132 if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
133 die(_("reading updated .gitmodules failed"));
134 if (lstat(".gitmodules", &st) < 0)
135 die_errno(_("unable to stat updated .gitmodules"));
136 fill_stat_cache_info(ce, &st);
137 ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
138 if (remove_cache_entry_at(pos) < 0)
139 die(_("unable to remove .gitmodules from index"));
140 if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
141 die(_("adding updated .gitmodules failed"));
142 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
143 die(_("staging updated .gitmodules failed"));
146 static int add_submodule_odb(const char *path)
148 struct strbuf objects_directory = STRBUF_INIT;
149 struct alternate_object_database *alt_odb;
150 int ret = 0;
151 const char *git_dir;
153 strbuf_addf(&objects_directory, "%s/.git", path);
154 git_dir = read_gitfile(objects_directory.buf);
155 if (git_dir) {
156 strbuf_reset(&objects_directory);
157 strbuf_addstr(&objects_directory, git_dir);
159 strbuf_addstr(&objects_directory, "/objects/");
160 if (!is_directory(objects_directory.buf)) {
161 ret = -1;
162 goto done;
164 /* avoid adding it twice */
165 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
166 if (alt_odb->name - alt_odb->base == objects_directory.len &&
167 !strncmp(alt_odb->base, objects_directory.buf,
168 objects_directory.len))
169 goto done;
171 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
172 alt_odb->next = alt_odb_list;
173 strcpy(alt_odb->base, objects_directory.buf);
174 alt_odb->name = alt_odb->base + objects_directory.len;
175 alt_odb->name[2] = '/';
176 alt_odb->name[40] = '\0';
177 alt_odb->name[41] = '\0';
178 alt_odb_list = alt_odb;
180 /* add possible alternates from the submodule */
181 read_info_alternates(objects_directory.buf, 0);
182 prepare_alt_odb();
183 done:
184 strbuf_release(&objects_directory);
185 return ret;
188 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
189 const char *path)
191 struct string_list_item *path_option, *ignore_option;
192 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
193 if (path_option) {
194 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
195 if (ignore_option)
196 handle_ignore_submodules_arg(diffopt, ignore_option->util);
197 else if (gitmodules_is_unmerged)
198 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
202 int submodule_config(const char *var, const char *value, void *cb)
204 if (!prefixcmp(var, "submodule."))
205 return parse_submodule_config_option(var, value);
206 else if (!strcmp(var, "fetch.recursesubmodules")) {
207 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
208 return 0;
210 return 0;
213 void gitmodules_config(void)
215 const char *work_tree = get_git_work_tree();
216 if (work_tree) {
217 struct strbuf gitmodules_path = STRBUF_INIT;
218 int pos;
219 strbuf_addstr(&gitmodules_path, work_tree);
220 strbuf_addstr(&gitmodules_path, "/.gitmodules");
221 if (read_cache() < 0)
222 die("index file corrupt");
223 pos = cache_name_pos(".gitmodules", 11);
224 if (pos < 0) { /* .gitmodules not found or isn't merged */
225 pos = -1 - pos;
226 if (active_nr > pos) { /* there is a .gitmodules */
227 const struct cache_entry *ce = active_cache[pos];
228 if (ce_namelen(ce) == 11 &&
229 !memcmp(ce->name, ".gitmodules", 11))
230 gitmodules_is_unmerged = 1;
232 } else if (pos < active_nr) {
233 struct stat st;
234 if (lstat(".gitmodules", &st) == 0 &&
235 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
236 gitmodules_is_modified = 1;
239 if (!gitmodules_is_unmerged)
240 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
241 strbuf_release(&gitmodules_path);
245 int parse_submodule_config_option(const char *var, const char *value)
247 struct string_list_item *config;
248 const char *name, *key;
249 int namelen;
251 if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
252 return 0;
254 if (!strcmp(key, "path")) {
255 if (!value)
256 return config_error_nonbool(var);
258 config = unsorted_string_list_lookup(&config_name_for_path, value);
259 if (config)
260 free(config->util);
261 else
262 config = string_list_append(&config_name_for_path, xstrdup(value));
263 config->util = xmemdupz(name, namelen);
264 } else if (!strcmp(key, "fetchrecursesubmodules")) {
265 char *name_cstr = xmemdupz(name, namelen);
266 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
267 if (!config)
268 config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
269 else
270 free(name_cstr);
271 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
272 } else if (!strcmp(key, "ignore")) {
273 char *name_cstr;
275 if (!value)
276 return config_error_nonbool(var);
278 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
279 strcmp(value, "all") && strcmp(value, "none")) {
280 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
281 return 0;
284 name_cstr = xmemdupz(name, namelen);
285 config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
286 if (config) {
287 free(config->util);
288 free(name_cstr);
289 } else
290 config = string_list_append(&config_ignore_for_name, name_cstr);
291 config->util = xstrdup(value);
292 return 0;
294 return 0;
297 void handle_ignore_submodules_arg(struct diff_options *diffopt,
298 const char *arg)
300 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
301 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
302 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
304 if (!strcmp(arg, "all"))
305 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
306 else if (!strcmp(arg, "untracked"))
307 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
308 else if (!strcmp(arg, "dirty"))
309 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
310 else if (strcmp(arg, "none"))
311 die("bad --ignore-submodules argument: %s", arg);
314 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
315 struct commit *left, struct commit *right,
316 int *fast_forward, int *fast_backward)
318 struct commit_list *merge_bases, *list;
320 init_revisions(rev, NULL);
321 setup_revisions(0, NULL, rev, NULL);
322 rev->left_right = 1;
323 rev->first_parent_only = 1;
324 left->object.flags |= SYMMETRIC_LEFT;
325 add_pending_object(rev, &left->object, path);
326 add_pending_object(rev, &right->object, path);
327 merge_bases = get_merge_bases(left, right, 1);
328 if (merge_bases) {
329 if (merge_bases->item == left)
330 *fast_forward = 1;
331 else if (merge_bases->item == right)
332 *fast_backward = 1;
334 for (list = merge_bases; list; list = list->next) {
335 list->item->object.flags |= UNINTERESTING;
336 add_pending_object(rev, &list->item->object,
337 sha1_to_hex(list->item->object.sha1));
339 return prepare_revision_walk(rev);
342 static void print_submodule_summary(struct rev_info *rev, FILE *f,
343 const char *line_prefix,
344 const char *del, const char *add, const char *reset)
346 static const char format[] = " %m %s";
347 struct strbuf sb = STRBUF_INIT;
348 struct commit *commit;
350 while ((commit = get_revision(rev))) {
351 struct pretty_print_context ctx = {0};
352 ctx.date_mode = rev->date_mode;
353 ctx.output_encoding = get_log_output_encoding();
354 strbuf_setlen(&sb, 0);
355 strbuf_addstr(&sb, line_prefix);
356 if (commit->object.flags & SYMMETRIC_LEFT) {
357 if (del)
358 strbuf_addstr(&sb, del);
360 else if (add)
361 strbuf_addstr(&sb, add);
362 format_commit_message(commit, format, &sb, &ctx);
363 if (reset)
364 strbuf_addstr(&sb, reset);
365 strbuf_addch(&sb, '\n');
366 fprintf(f, "%s", sb.buf);
368 strbuf_release(&sb);
371 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
373 switch (git_config_maybe_bool(opt, arg)) {
374 case 1:
375 return RECURSE_SUBMODULES_ON;
376 case 0:
377 return RECURSE_SUBMODULES_OFF;
378 default:
379 if (!strcmp(arg, "on-demand"))
380 return RECURSE_SUBMODULES_ON_DEMAND;
381 die("bad %s argument: %s", opt, arg);
385 void show_submodule_summary(FILE *f, const char *path,
386 const char *line_prefix,
387 unsigned char one[20], unsigned char two[20],
388 unsigned dirty_submodule, const char *meta,
389 const char *del, const char *add, const char *reset)
391 struct rev_info rev;
392 struct commit *left = NULL, *right = NULL;
393 const char *message = NULL;
394 struct strbuf sb = STRBUF_INIT;
395 int fast_forward = 0, fast_backward = 0;
397 if (is_null_sha1(two))
398 message = "(submodule deleted)";
399 else if (add_submodule_odb(path))
400 message = "(not checked out)";
401 else if (is_null_sha1(one))
402 message = "(new submodule)";
403 else if (!(left = lookup_commit_reference(one)) ||
404 !(right = lookup_commit_reference(two)))
405 message = "(commits not present)";
406 else if (prepare_submodule_summary(&rev, path, left, right,
407 &fast_forward, &fast_backward))
408 message = "(revision walker failed)";
410 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
411 fprintf(f, "%sSubmodule %s contains untracked content\n",
412 line_prefix, path);
413 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
414 fprintf(f, "%sSubmodule %s contains modified content\n",
415 line_prefix, path);
417 if (!hashcmp(one, two)) {
418 strbuf_release(&sb);
419 return;
422 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
423 find_unique_abbrev(one, DEFAULT_ABBREV));
424 if (!fast_backward && !fast_forward)
425 strbuf_addch(&sb, '.');
426 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
427 if (message)
428 strbuf_addf(&sb, " %s%s\n", message, reset);
429 else
430 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
431 fwrite(sb.buf, sb.len, 1, f);
433 if (!message) /* only NULL if we succeeded in setting up the walk */
434 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
435 if (left)
436 clear_commit_marks(left, ~0);
437 if (right)
438 clear_commit_marks(right, ~0);
440 strbuf_release(&sb);
443 void set_config_fetch_recurse_submodules(int value)
445 config_fetch_recurse_submodules = value;
448 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
450 return 1;
453 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
455 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
456 return 0;
458 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
459 struct child_process cp;
460 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
461 struct strbuf buf = STRBUF_INIT;
462 int needs_pushing = 0;
464 argv[1] = sha1_to_hex(sha1);
465 memset(&cp, 0, sizeof(cp));
466 cp.argv = argv;
467 cp.env = local_repo_env;
468 cp.git_cmd = 1;
469 cp.no_stdin = 1;
470 cp.out = -1;
471 cp.dir = path;
472 if (start_command(&cp))
473 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
474 sha1_to_hex(sha1), path);
475 if (strbuf_read(&buf, cp.out, 41))
476 needs_pushing = 1;
477 finish_command(&cp);
478 close(cp.out);
479 strbuf_release(&buf);
480 return needs_pushing;
483 return 0;
486 static void collect_submodules_from_diff(struct diff_queue_struct *q,
487 struct diff_options *options,
488 void *data)
490 int i;
491 struct string_list *needs_pushing = data;
493 for (i = 0; i < q->nr; i++) {
494 struct diff_filepair *p = q->queue[i];
495 if (!S_ISGITLINK(p->two->mode))
496 continue;
497 if (submodule_needs_pushing(p->two->path, p->two->sha1))
498 string_list_insert(needs_pushing, p->two->path);
502 static void find_unpushed_submodule_commits(struct commit *commit,
503 struct string_list *needs_pushing)
505 struct rev_info rev;
507 init_revisions(&rev, NULL);
508 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
509 rev.diffopt.format_callback = collect_submodules_from_diff;
510 rev.diffopt.format_callback_data = needs_pushing;
511 diff_tree_combined_merge(commit, 1, &rev);
514 int find_unpushed_submodules(unsigned char new_sha1[20],
515 const char *remotes_name, struct string_list *needs_pushing)
517 struct rev_info rev;
518 struct commit *commit;
519 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
520 int argc = ARRAY_SIZE(argv) - 1;
521 char *sha1_copy;
523 struct strbuf remotes_arg = STRBUF_INIT;
525 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
526 init_revisions(&rev, NULL);
527 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
528 argv[1] = sha1_copy;
529 argv[3] = remotes_arg.buf;
530 setup_revisions(argc, argv, &rev, NULL);
531 if (prepare_revision_walk(&rev))
532 die("revision walk setup failed");
534 while ((commit = get_revision(&rev)) != NULL)
535 find_unpushed_submodule_commits(commit, needs_pushing);
537 reset_revision_walk();
538 free(sha1_copy);
539 strbuf_release(&remotes_arg);
541 return needs_pushing->nr;
544 static int push_submodule(const char *path)
546 if (add_submodule_odb(path))
547 return 1;
549 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
550 struct child_process cp;
551 const char *argv[] = {"push", NULL};
553 memset(&cp, 0, sizeof(cp));
554 cp.argv = argv;
555 cp.env = local_repo_env;
556 cp.git_cmd = 1;
557 cp.no_stdin = 1;
558 cp.dir = path;
559 if (run_command(&cp))
560 return 0;
561 close(cp.out);
564 return 1;
567 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
569 int i, ret = 1;
570 struct string_list needs_pushing;
572 memset(&needs_pushing, 0, sizeof(struct string_list));
573 needs_pushing.strdup_strings = 1;
575 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
576 return 1;
578 for (i = 0; i < needs_pushing.nr; i++) {
579 const char *path = needs_pushing.items[i].string;
580 fprintf(stderr, "Pushing submodule '%s'\n", path);
581 if (!push_submodule(path)) {
582 fprintf(stderr, "Unable to push submodule '%s'\n", path);
583 ret = 0;
587 string_list_clear(&needs_pushing, 0);
589 return ret;
592 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
594 int is_present = 0;
595 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
596 /* Even if the submodule is checked out and the commit is
597 * present, make sure it is reachable from a ref. */
598 struct child_process cp;
599 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
600 struct strbuf buf = STRBUF_INIT;
602 argv[3] = sha1_to_hex(sha1);
603 memset(&cp, 0, sizeof(cp));
604 cp.argv = argv;
605 cp.env = local_repo_env;
606 cp.git_cmd = 1;
607 cp.no_stdin = 1;
608 cp.out = -1;
609 cp.dir = path;
610 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
611 is_present = 1;
613 close(cp.out);
614 strbuf_release(&buf);
616 return is_present;
619 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
620 struct diff_options *options,
621 void *data)
623 int i;
624 for (i = 0; i < q->nr; i++) {
625 struct diff_filepair *p = q->queue[i];
626 if (!S_ISGITLINK(p->two->mode))
627 continue;
629 if (S_ISGITLINK(p->one->mode)) {
630 /* NEEDSWORK: We should honor the name configured in
631 * the .gitmodules file of the commit we are examining
632 * here to be able to correctly follow submodules
633 * being moved around. */
634 struct string_list_item *path;
635 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
636 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
637 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
638 } else {
639 /* Submodule is new or was moved here */
640 /* NEEDSWORK: When the .git directories of submodules
641 * live inside the superprojects .git directory some
642 * day we should fetch new submodules directly into
643 * that location too when config or options request
644 * that so they can be checked out from there. */
645 continue;
650 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
651 int flags, void *data)
653 sha1_array_append(data, sha1);
654 return 0;
657 void check_for_new_submodule_commits(unsigned char new_sha1[20])
659 if (!initialized_fetch_ref_tips) {
660 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
661 initialized_fetch_ref_tips = 1;
664 sha1_array_append(&ref_tips_after_fetch, new_sha1);
667 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
669 argv_array_push(data, sha1_to_hex(sha1));
672 static void calculate_changed_submodule_paths(void)
674 struct rev_info rev;
675 struct commit *commit;
676 struct argv_array argv = ARGV_ARRAY_INIT;
678 /* No need to check if there are no submodules configured */
679 if (!config_name_for_path.nr)
680 return;
682 init_revisions(&rev, NULL);
683 argv_array_push(&argv, "--"); /* argv[0] program name */
684 sha1_array_for_each_unique(&ref_tips_after_fetch,
685 add_sha1_to_argv, &argv);
686 argv_array_push(&argv, "--not");
687 sha1_array_for_each_unique(&ref_tips_before_fetch,
688 add_sha1_to_argv, &argv);
689 setup_revisions(argv.argc, argv.argv, &rev, NULL);
690 if (prepare_revision_walk(&rev))
691 die("revision walk setup failed");
694 * Collect all submodules (whether checked out or not) for which new
695 * commits have been recorded upstream in "changed_submodule_paths".
697 while ((commit = get_revision(&rev))) {
698 struct commit_list *parent = commit->parents;
699 while (parent) {
700 struct diff_options diff_opts;
701 diff_setup(&diff_opts);
702 DIFF_OPT_SET(&diff_opts, RECURSIVE);
703 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
704 diff_opts.format_callback = submodule_collect_changed_cb;
705 diff_setup_done(&diff_opts);
706 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
707 diffcore_std(&diff_opts);
708 diff_flush(&diff_opts);
709 parent = parent->next;
713 argv_array_clear(&argv);
714 sha1_array_clear(&ref_tips_before_fetch);
715 sha1_array_clear(&ref_tips_after_fetch);
716 initialized_fetch_ref_tips = 0;
719 int fetch_populated_submodules(const struct argv_array *options,
720 const char *prefix, int command_line_option,
721 int quiet)
723 int i, result = 0;
724 struct child_process cp;
725 struct argv_array argv = ARGV_ARRAY_INIT;
726 struct string_list_item *name_for_path;
727 const char *work_tree = get_git_work_tree();
728 if (!work_tree)
729 goto out;
731 if (read_cache() < 0)
732 die("index file corrupt");
734 argv_array_push(&argv, "fetch");
735 for (i = 0; i < options->argc; i++)
736 argv_array_push(&argv, options->argv[i]);
737 argv_array_push(&argv, "--recurse-submodules-default");
738 /* default value, "--submodule-prefix" and its value are added later */
740 memset(&cp, 0, sizeof(cp));
741 cp.env = local_repo_env;
742 cp.git_cmd = 1;
743 cp.no_stdin = 1;
745 calculate_changed_submodule_paths();
747 for (i = 0; i < active_nr; i++) {
748 struct strbuf submodule_path = STRBUF_INIT;
749 struct strbuf submodule_git_dir = STRBUF_INIT;
750 struct strbuf submodule_prefix = STRBUF_INIT;
751 const struct cache_entry *ce = active_cache[i];
752 const char *git_dir, *name, *default_argv;
754 if (!S_ISGITLINK(ce->ce_mode))
755 continue;
757 name = ce->name;
758 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
759 if (name_for_path)
760 name = name_for_path->util;
762 default_argv = "yes";
763 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
764 struct string_list_item *fetch_recurse_submodules_option;
765 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
766 if (fetch_recurse_submodules_option) {
767 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
768 continue;
769 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
770 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
771 continue;
772 default_argv = "on-demand";
774 } else {
775 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
776 gitmodules_is_unmerged)
777 continue;
778 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
779 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
780 continue;
781 default_argv = "on-demand";
784 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
785 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
786 continue;
787 default_argv = "on-demand";
790 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
791 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
792 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
793 git_dir = read_gitfile(submodule_git_dir.buf);
794 if (!git_dir)
795 git_dir = submodule_git_dir.buf;
796 if (is_directory(git_dir)) {
797 if (!quiet)
798 printf("Fetching submodule %s%s\n", prefix, ce->name);
799 cp.dir = submodule_path.buf;
800 argv_array_push(&argv, default_argv);
801 argv_array_push(&argv, "--submodule-prefix");
802 argv_array_push(&argv, submodule_prefix.buf);
803 cp.argv = argv.argv;
804 if (run_command(&cp))
805 result = 1;
806 argv_array_pop(&argv);
807 argv_array_pop(&argv);
808 argv_array_pop(&argv);
810 strbuf_release(&submodule_path);
811 strbuf_release(&submodule_git_dir);
812 strbuf_release(&submodule_prefix);
814 argv_array_clear(&argv);
815 out:
816 string_list_clear(&changed_submodule_paths, 1);
817 return result;
820 unsigned is_submodule_modified(const char *path, int ignore_untracked)
822 ssize_t len;
823 struct child_process cp;
824 const char *argv[] = {
825 "status",
826 "--porcelain",
827 NULL,
828 NULL,
830 struct strbuf buf = STRBUF_INIT;
831 unsigned dirty_submodule = 0;
832 const char *line, *next_line;
833 const char *git_dir;
835 strbuf_addf(&buf, "%s/.git", path);
836 git_dir = read_gitfile(buf.buf);
837 if (!git_dir)
838 git_dir = buf.buf;
839 if (!is_directory(git_dir)) {
840 strbuf_release(&buf);
841 /* The submodule is not checked out, so it is not modified */
842 return 0;
845 strbuf_reset(&buf);
847 if (ignore_untracked)
848 argv[2] = "-uno";
850 memset(&cp, 0, sizeof(cp));
851 cp.argv = argv;
852 cp.env = local_repo_env;
853 cp.git_cmd = 1;
854 cp.no_stdin = 1;
855 cp.out = -1;
856 cp.dir = path;
857 if (start_command(&cp))
858 die("Could not run 'git status --porcelain' in submodule %s", path);
860 len = strbuf_read(&buf, cp.out, 1024);
861 line = buf.buf;
862 while (len > 2) {
863 if ((line[0] == '?') && (line[1] == '?')) {
864 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
865 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
866 break;
867 } else {
868 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
869 if (ignore_untracked ||
870 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
871 break;
873 next_line = strchr(line, '\n');
874 if (!next_line)
875 break;
876 next_line++;
877 len -= (next_line - line);
878 line = next_line;
880 close(cp.out);
882 if (finish_command(&cp))
883 die("'git status --porcelain' failed in submodule %s", path);
885 strbuf_release(&buf);
886 return dirty_submodule;
889 int submodule_uses_gitfile(const char *path)
891 struct child_process cp;
892 const char *argv[] = {
893 "submodule",
894 "foreach",
895 "--quiet",
896 "--recursive",
897 "test -f .git",
898 NULL,
900 struct strbuf buf = STRBUF_INIT;
901 const char *git_dir;
903 strbuf_addf(&buf, "%s/.git", path);
904 git_dir = read_gitfile(buf.buf);
905 if (!git_dir) {
906 strbuf_release(&buf);
907 return 0;
909 strbuf_release(&buf);
911 /* Now test that all nested submodules use a gitfile too */
912 memset(&cp, 0, sizeof(cp));
913 cp.argv = argv;
914 cp.env = local_repo_env;
915 cp.git_cmd = 1;
916 cp.no_stdin = 1;
917 cp.no_stderr = 1;
918 cp.no_stdout = 1;
919 cp.dir = path;
920 if (run_command(&cp))
921 return 0;
923 return 1;
926 int ok_to_remove_submodule(const char *path)
928 struct stat st;
929 ssize_t len;
930 struct child_process cp;
931 const char *argv[] = {
932 "status",
933 "--porcelain",
934 "-u",
935 "--ignore-submodules=none",
936 NULL,
938 struct strbuf buf = STRBUF_INIT;
939 int ok_to_remove = 1;
941 if ((lstat(path, &st) < 0) || is_empty_dir(path))
942 return 1;
944 if (!submodule_uses_gitfile(path))
945 return 0;
947 memset(&cp, 0, sizeof(cp));
948 cp.argv = argv;
949 cp.env = local_repo_env;
950 cp.git_cmd = 1;
951 cp.no_stdin = 1;
952 cp.out = -1;
953 cp.dir = path;
954 if (start_command(&cp))
955 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
957 len = strbuf_read(&buf, cp.out, 1024);
958 if (len > 2)
959 ok_to_remove = 0;
960 close(cp.out);
962 if (finish_command(&cp))
963 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
965 strbuf_release(&buf);
966 return ok_to_remove;
969 static int find_first_merges(struct object_array *result, const char *path,
970 struct commit *a, struct commit *b)
972 int i, j;
973 struct object_array merges = OBJECT_ARRAY_INIT;
974 struct commit *commit;
975 int contains_another;
977 char merged_revision[42];
978 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
979 "--all", merged_revision, NULL };
980 struct rev_info revs;
981 struct setup_revision_opt rev_opts;
983 memset(result, 0, sizeof(struct object_array));
984 memset(&rev_opts, 0, sizeof(rev_opts));
986 /* get all revisions that merge commit a */
987 snprintf(merged_revision, sizeof(merged_revision), "^%s",
988 sha1_to_hex(a->object.sha1));
989 init_revisions(&revs, NULL);
990 rev_opts.submodule = path;
991 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
993 /* save all revisions from the above list that contain b */
994 if (prepare_revision_walk(&revs))
995 die("revision walk setup failed");
996 while ((commit = get_revision(&revs)) != NULL) {
997 struct object *o = &(commit->object);
998 if (in_merge_bases(b, commit))
999 add_object_array(o, NULL, &merges);
1001 reset_revision_walk();
1003 /* Now we've got all merges that contain a and b. Prune all
1004 * merges that contain another found merge and save them in
1005 * result.
1007 for (i = 0; i < merges.nr; i++) {
1008 struct commit *m1 = (struct commit *) merges.objects[i].item;
1010 contains_another = 0;
1011 for (j = 0; j < merges.nr; j++) {
1012 struct commit *m2 = (struct commit *) merges.objects[j].item;
1013 if (i != j && in_merge_bases(m2, m1)) {
1014 contains_another = 1;
1015 break;
1019 if (!contains_another)
1020 add_object_array(merges.objects[i].item, NULL, result);
1023 free(merges.objects);
1024 return result->nr;
1027 static void print_commit(struct commit *commit)
1029 struct strbuf sb = STRBUF_INIT;
1030 struct pretty_print_context ctx = {0};
1031 ctx.date_mode = DATE_NORMAL;
1032 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1033 fprintf(stderr, "%s\n", sb.buf);
1034 strbuf_release(&sb);
1037 #define MERGE_WARNING(path, msg) \
1038 warning("Failed to merge submodule %s (%s)", path, msg);
1040 int merge_submodule(unsigned char result[20], const char *path,
1041 const unsigned char base[20], const unsigned char a[20],
1042 const unsigned char b[20], int search)
1044 struct commit *commit_base, *commit_a, *commit_b;
1045 int parent_count;
1046 struct object_array merges;
1048 int i;
1050 /* store a in result in case we fail */
1051 hashcpy(result, a);
1053 /* we can not handle deletion conflicts */
1054 if (is_null_sha1(base))
1055 return 0;
1056 if (is_null_sha1(a))
1057 return 0;
1058 if (is_null_sha1(b))
1059 return 0;
1061 if (add_submodule_odb(path)) {
1062 MERGE_WARNING(path, "not checked out");
1063 return 0;
1066 if (!(commit_base = lookup_commit_reference(base)) ||
1067 !(commit_a = lookup_commit_reference(a)) ||
1068 !(commit_b = lookup_commit_reference(b))) {
1069 MERGE_WARNING(path, "commits not present");
1070 return 0;
1073 /* check whether both changes are forward */
1074 if (!in_merge_bases(commit_base, commit_a) ||
1075 !in_merge_bases(commit_base, commit_b)) {
1076 MERGE_WARNING(path, "commits don't follow merge-base");
1077 return 0;
1080 /* Case #1: a is contained in b or vice versa */
1081 if (in_merge_bases(commit_a, commit_b)) {
1082 hashcpy(result, b);
1083 return 1;
1085 if (in_merge_bases(commit_b, commit_a)) {
1086 hashcpy(result, a);
1087 return 1;
1091 * Case #2: There are one or more merges that contain a and b in
1092 * the submodule. If there is only one, then present it as a
1093 * suggestion to the user, but leave it marked unmerged so the
1094 * user needs to confirm the resolution.
1097 /* Skip the search if makes no sense to the calling context. */
1098 if (!search)
1099 return 0;
1101 /* find commit which merges them */
1102 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1103 switch (parent_count) {
1104 case 0:
1105 MERGE_WARNING(path, "merge following commits not found");
1106 break;
1108 case 1:
1109 MERGE_WARNING(path, "not fast-forward");
1110 fprintf(stderr, "Found a possible merge resolution "
1111 "for the submodule:\n");
1112 print_commit((struct commit *) merges.objects[0].item);
1113 fprintf(stderr,
1114 "If this is correct simply add it to the index "
1115 "for example\n"
1116 "by using:\n\n"
1117 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1118 "which will accept this suggestion.\n",
1119 sha1_to_hex(merges.objects[0].item->sha1), path);
1120 break;
1122 default:
1123 MERGE_WARNING(path, "multiple merges found");
1124 for (i = 0; i < merges.nr; i++)
1125 print_commit((struct commit *) merges.objects[i].item);
1128 free(merges.objects);
1129 return 0;
1132 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1133 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1135 struct strbuf file_name = STRBUF_INIT;
1136 struct strbuf rel_path = STRBUF_INIT;
1137 const char *real_work_tree = xstrdup(real_path(work_tree));
1138 FILE *fp;
1140 /* Update gitfile */
1141 strbuf_addf(&file_name, "%s/.git", work_tree);
1142 fp = fopen(file_name.buf, "w");
1143 if (!fp)
1144 die(_("Could not create git link %s"), file_name.buf);
1145 fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree,
1146 &rel_path));
1147 fclose(fp);
1149 /* Update core.worktree setting */
1150 strbuf_reset(&file_name);
1151 strbuf_addf(&file_name, "%s/config", git_dir);
1152 if (git_config_set_in_file(file_name.buf, "core.worktree",
1153 relative_path(real_work_tree, git_dir,
1154 &rel_path)))
1155 die(_("Could not set core.worktree in %s"),
1156 file_name.buf);
1158 strbuf_release(&file_name);
1159 strbuf_release(&rel_path);
1160 free((void *)real_work_tree);