Merge branch 'jk/lookup-object-prefer-latest' into next
[git/mjg.git] / submodule.c
blobedfc23c41afed1d49f7064519e6db2c8377dfbc3
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 * Try to update the "path" entry in the "submodule.<name>" section of the
36 * .gitmodules file.
38 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
40 struct strbuf entry = STRBUF_INIT;
41 struct string_list_item *path_option;
43 if (!file_exists(".gitmodules")) /* Do nothing whithout .gitmodules */
44 return -1;
46 if (gitmodules_is_unmerged)
47 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
49 path_option = unsorted_string_list_lookup(&config_name_for_path, oldpath);
50 if (!path_option) {
51 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
52 return -1;
54 strbuf_addstr(&entry, "submodule.");
55 strbuf_addstr(&entry, path_option->util);
56 strbuf_addstr(&entry, ".path");
57 if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
58 /* Maybe the user already did that, don't error out here */
59 warning(_("Could not update .gitmodules entry %s"), entry.buf);
60 return -1;
62 strbuf_release(&entry);
63 return 0;
67 * Try to remove the "submodule.<name>" section from .gitmodules where the
68 * given path is configured.
70 int remove_path_from_gitmodules(const char *path)
72 struct strbuf sect = STRBUF_INIT;
73 struct string_list_item *path_option;
75 if (!file_exists(".gitmodules")) /* Do nothing whithout .gitmodules */
76 return -1;
78 if (gitmodules_is_unmerged)
79 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
81 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
82 if (!path_option) {
83 warning(_("Could not find section in .gitmodules where path=%s"), path);
84 return -1;
86 strbuf_addstr(&sect, "submodule.");
87 strbuf_addstr(&sect, path_option->util);
88 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
89 /* Maybe the user already did that, don't error out here */
90 warning(_("Could not remove .gitmodules entry for %s"), path);
91 return -1;
93 strbuf_release(&sect);
94 return 0;
97 void stage_updated_gitmodules(void)
99 struct strbuf buf = STRBUF_INIT;
100 struct stat st;
101 int pos;
102 struct cache_entry *ce;
103 int namelen = strlen(".gitmodules");
105 pos = cache_name_pos(".gitmodules", strlen(".gitmodules"));
106 if (pos < 0) {
107 warning(_("could not find .gitmodules in index"));
108 return;
110 ce = active_cache[pos];
111 ce->ce_flags = namelen;
112 if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
113 die(_("reading updated .gitmodules failed"));
114 if (lstat(".gitmodules", &st) < 0)
115 die_errno(_("unable to stat updated .gitmodules"));
116 fill_stat_cache_info(ce, &st);
117 ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
118 if (remove_file_from_cache(".gitmodules") < 0)
119 die(_("unable to remove .gitmodules from index"));
120 if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
121 die(_("adding updated .gitmodules failed"));
122 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
123 die(_("staging updated .gitmodules failed"));
126 static int add_submodule_odb(const char *path)
128 struct strbuf objects_directory = STRBUF_INIT;
129 struct alternate_object_database *alt_odb;
130 int ret = 0;
131 const char *git_dir;
133 strbuf_addf(&objects_directory, "%s/.git", path);
134 git_dir = read_gitfile(objects_directory.buf);
135 if (git_dir) {
136 strbuf_reset(&objects_directory);
137 strbuf_addstr(&objects_directory, git_dir);
139 strbuf_addstr(&objects_directory, "/objects/");
140 if (!is_directory(objects_directory.buf)) {
141 ret = -1;
142 goto done;
144 /* avoid adding it twice */
145 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
146 if (alt_odb->name - alt_odb->base == objects_directory.len &&
147 !strncmp(alt_odb->base, objects_directory.buf,
148 objects_directory.len))
149 goto done;
151 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
152 alt_odb->next = alt_odb_list;
153 strcpy(alt_odb->base, objects_directory.buf);
154 alt_odb->name = alt_odb->base + objects_directory.len;
155 alt_odb->name[2] = '/';
156 alt_odb->name[40] = '\0';
157 alt_odb->name[41] = '\0';
158 alt_odb_list = alt_odb;
160 /* add possible alternates from the submodule */
161 read_info_alternates(objects_directory.buf, 0);
162 prepare_alt_odb();
163 done:
164 strbuf_release(&objects_directory);
165 return ret;
168 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
169 const char *path)
171 struct string_list_item *path_option, *ignore_option;
172 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
173 if (path_option) {
174 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
175 if (ignore_option)
176 handle_ignore_submodules_arg(diffopt, ignore_option->util);
177 else if (gitmodules_is_unmerged)
178 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
182 int submodule_config(const char *var, const char *value, void *cb)
184 if (!prefixcmp(var, "submodule."))
185 return parse_submodule_config_option(var, value);
186 else if (!strcmp(var, "fetch.recursesubmodules")) {
187 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
188 return 0;
190 return 0;
193 void gitmodules_config(void)
195 const char *work_tree = get_git_work_tree();
196 if (work_tree) {
197 struct strbuf gitmodules_path = STRBUF_INIT;
198 int pos;
199 strbuf_addstr(&gitmodules_path, work_tree);
200 strbuf_addstr(&gitmodules_path, "/.gitmodules");
201 if (read_cache() < 0)
202 die("index file corrupt");
203 pos = cache_name_pos(".gitmodules", 11);
204 if (pos < 0) { /* .gitmodules not found or isn't merged */
205 pos = -1 - pos;
206 if (active_nr > pos) { /* there is a .gitmodules */
207 const struct cache_entry *ce = active_cache[pos];
208 if (ce_namelen(ce) == 11 &&
209 !memcmp(ce->name, ".gitmodules", 11))
210 gitmodules_is_unmerged = 1;
214 if (!gitmodules_is_unmerged)
215 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
216 strbuf_release(&gitmodules_path);
220 int parse_submodule_config_option(const char *var, const char *value)
222 struct string_list_item *config;
223 const char *name, *key;
224 int namelen;
226 if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
227 return 0;
229 if (!strcmp(key, "path")) {
230 config = unsorted_string_list_lookup(&config_name_for_path, value);
231 if (config)
232 free(config->util);
233 else
234 config = string_list_append(&config_name_for_path, xstrdup(value));
235 config->util = xmemdupz(name, namelen);
236 } else if (!strcmp(key, "fetchrecursesubmodules")) {
237 char *name_cstr = xmemdupz(name, namelen);
238 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
239 if (!config)
240 config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
241 else
242 free(name_cstr);
243 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
244 } else if (!strcmp(key, "ignore")) {
245 char *name_cstr;
247 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
248 strcmp(value, "all") && strcmp(value, "none")) {
249 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
250 return 0;
253 name_cstr = xmemdupz(name, namelen);
254 config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
255 if (config) {
256 free(config->util);
257 free(name_cstr);
258 } else
259 config = string_list_append(&config_ignore_for_name, name_cstr);
260 config->util = xstrdup(value);
261 return 0;
263 return 0;
266 void handle_ignore_submodules_arg(struct diff_options *diffopt,
267 const char *arg)
269 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
270 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
271 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
273 if (!strcmp(arg, "all"))
274 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
275 else if (!strcmp(arg, "untracked"))
276 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
277 else if (!strcmp(arg, "dirty"))
278 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
279 else if (strcmp(arg, "none"))
280 die("bad --ignore-submodules argument: %s", arg);
283 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
284 struct commit *left, struct commit *right,
285 int *fast_forward, int *fast_backward)
287 struct commit_list *merge_bases, *list;
289 init_revisions(rev, NULL);
290 setup_revisions(0, NULL, rev, NULL);
291 rev->left_right = 1;
292 rev->first_parent_only = 1;
293 left->object.flags |= SYMMETRIC_LEFT;
294 add_pending_object(rev, &left->object, path);
295 add_pending_object(rev, &right->object, path);
296 merge_bases = get_merge_bases(left, right, 1);
297 if (merge_bases) {
298 if (merge_bases->item == left)
299 *fast_forward = 1;
300 else if (merge_bases->item == right)
301 *fast_backward = 1;
303 for (list = merge_bases; list; list = list->next) {
304 list->item->object.flags |= UNINTERESTING;
305 add_pending_object(rev, &list->item->object,
306 sha1_to_hex(list->item->object.sha1));
308 return prepare_revision_walk(rev);
311 static void print_submodule_summary(struct rev_info *rev, FILE *f,
312 const char *line_prefix,
313 const char *del, const char *add, const char *reset)
315 static const char format[] = " %m %s";
316 struct strbuf sb = STRBUF_INIT;
317 struct commit *commit;
319 while ((commit = get_revision(rev))) {
320 struct pretty_print_context ctx = {0};
321 ctx.date_mode = rev->date_mode;
322 strbuf_setlen(&sb, 0);
323 strbuf_addstr(&sb, line_prefix);
324 if (commit->object.flags & SYMMETRIC_LEFT) {
325 if (del)
326 strbuf_addstr(&sb, del);
328 else if (add)
329 strbuf_addstr(&sb, add);
330 format_commit_message(commit, format, &sb, &ctx);
331 if (reset)
332 strbuf_addstr(&sb, reset);
333 strbuf_addch(&sb, '\n');
334 fprintf(f, "%s", sb.buf);
336 strbuf_release(&sb);
339 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
341 switch (git_config_maybe_bool(opt, arg)) {
342 case 1:
343 return RECURSE_SUBMODULES_ON;
344 case 0:
345 return RECURSE_SUBMODULES_OFF;
346 default:
347 if (!strcmp(arg, "on-demand"))
348 return RECURSE_SUBMODULES_ON_DEMAND;
349 die("bad %s argument: %s", opt, arg);
353 void show_submodule_summary(FILE *f, const char *path,
354 const char *line_prefix,
355 unsigned char one[20], unsigned char two[20],
356 unsigned dirty_submodule, const char *meta,
357 const char *del, const char *add, const char *reset)
359 struct rev_info rev;
360 struct commit *left = NULL, *right = NULL;
361 const char *message = NULL;
362 struct strbuf sb = STRBUF_INIT;
363 int fast_forward = 0, fast_backward = 0;
365 if (is_null_sha1(two))
366 message = "(submodule deleted)";
367 else if (add_submodule_odb(path))
368 message = "(not checked out)";
369 else if (is_null_sha1(one))
370 message = "(new submodule)";
371 else if (!(left = lookup_commit_reference(one)) ||
372 !(right = lookup_commit_reference(two)))
373 message = "(commits not present)";
374 else if (prepare_submodule_summary(&rev, path, left, right,
375 &fast_forward, &fast_backward))
376 message = "(revision walker failed)";
378 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
379 fprintf(f, "%sSubmodule %s contains untracked content\n",
380 line_prefix, path);
381 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
382 fprintf(f, "%sSubmodule %s contains modified content\n",
383 line_prefix, path);
385 if (!hashcmp(one, two)) {
386 strbuf_release(&sb);
387 return;
390 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
391 find_unique_abbrev(one, DEFAULT_ABBREV));
392 if (!fast_backward && !fast_forward)
393 strbuf_addch(&sb, '.');
394 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
395 if (message)
396 strbuf_addf(&sb, " %s%s\n", message, reset);
397 else
398 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
399 fwrite(sb.buf, sb.len, 1, f);
401 if (!message) /* only NULL if we succeeded in setting up the walk */
402 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
403 if (left)
404 clear_commit_marks(left, ~0);
405 if (right)
406 clear_commit_marks(right, ~0);
408 strbuf_release(&sb);
411 void set_config_fetch_recurse_submodules(int value)
413 config_fetch_recurse_submodules = value;
416 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
418 return 1;
421 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
423 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
424 return 0;
426 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
427 struct child_process cp;
428 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
429 struct strbuf buf = STRBUF_INIT;
430 int needs_pushing = 0;
432 argv[1] = sha1_to_hex(sha1);
433 memset(&cp, 0, sizeof(cp));
434 cp.argv = argv;
435 cp.env = local_repo_env;
436 cp.git_cmd = 1;
437 cp.no_stdin = 1;
438 cp.out = -1;
439 cp.dir = path;
440 if (start_command(&cp))
441 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
442 sha1_to_hex(sha1), path);
443 if (strbuf_read(&buf, cp.out, 41))
444 needs_pushing = 1;
445 finish_command(&cp);
446 close(cp.out);
447 strbuf_release(&buf);
448 return needs_pushing;
451 return 0;
454 static void collect_submodules_from_diff(struct diff_queue_struct *q,
455 struct diff_options *options,
456 void *data)
458 int i;
459 struct string_list *needs_pushing = data;
461 for (i = 0; i < q->nr; i++) {
462 struct diff_filepair *p = q->queue[i];
463 if (!S_ISGITLINK(p->two->mode))
464 continue;
465 if (submodule_needs_pushing(p->two->path, p->two->sha1))
466 string_list_insert(needs_pushing, p->two->path);
470 static void find_unpushed_submodule_commits(struct commit *commit,
471 struct string_list *needs_pushing)
473 struct rev_info rev;
475 init_revisions(&rev, NULL);
476 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
477 rev.diffopt.format_callback = collect_submodules_from_diff;
478 rev.diffopt.format_callback_data = needs_pushing;
479 diff_tree_combined_merge(commit, 1, &rev);
482 int find_unpushed_submodules(unsigned char new_sha1[20],
483 const char *remotes_name, struct string_list *needs_pushing)
485 struct rev_info rev;
486 struct commit *commit;
487 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
488 int argc = ARRAY_SIZE(argv) - 1;
489 char *sha1_copy;
491 struct strbuf remotes_arg = STRBUF_INIT;
493 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
494 init_revisions(&rev, NULL);
495 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
496 argv[1] = sha1_copy;
497 argv[3] = remotes_arg.buf;
498 setup_revisions(argc, argv, &rev, NULL);
499 if (prepare_revision_walk(&rev))
500 die("revision walk setup failed");
502 while ((commit = get_revision(&rev)) != NULL)
503 find_unpushed_submodule_commits(commit, needs_pushing);
505 reset_revision_walk();
506 free(sha1_copy);
507 strbuf_release(&remotes_arg);
509 return needs_pushing->nr;
512 static int push_submodule(const char *path)
514 if (add_submodule_odb(path))
515 return 1;
517 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
518 struct child_process cp;
519 const char *argv[] = {"push", NULL};
521 memset(&cp, 0, sizeof(cp));
522 cp.argv = argv;
523 cp.env = local_repo_env;
524 cp.git_cmd = 1;
525 cp.no_stdin = 1;
526 cp.dir = path;
527 if (run_command(&cp))
528 return 0;
529 close(cp.out);
532 return 1;
535 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
537 int i, ret = 1;
538 struct string_list needs_pushing;
540 memset(&needs_pushing, 0, sizeof(struct string_list));
541 needs_pushing.strdup_strings = 1;
543 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
544 return 1;
546 for (i = 0; i < needs_pushing.nr; i++) {
547 const char *path = needs_pushing.items[i].string;
548 fprintf(stderr, "Pushing submodule '%s'\n", path);
549 if (!push_submodule(path)) {
550 fprintf(stderr, "Unable to push submodule '%s'\n", path);
551 ret = 0;
555 string_list_clear(&needs_pushing, 0);
557 return ret;
560 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
562 int is_present = 0;
563 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
564 /* Even if the submodule is checked out and the commit is
565 * present, make sure it is reachable from a ref. */
566 struct child_process cp;
567 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
568 struct strbuf buf = STRBUF_INIT;
570 argv[3] = sha1_to_hex(sha1);
571 memset(&cp, 0, sizeof(cp));
572 cp.argv = argv;
573 cp.env = local_repo_env;
574 cp.git_cmd = 1;
575 cp.no_stdin = 1;
576 cp.out = -1;
577 cp.dir = path;
578 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
579 is_present = 1;
581 close(cp.out);
582 strbuf_release(&buf);
584 return is_present;
587 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
588 struct diff_options *options,
589 void *data)
591 int i;
592 for (i = 0; i < q->nr; i++) {
593 struct diff_filepair *p = q->queue[i];
594 if (!S_ISGITLINK(p->two->mode))
595 continue;
597 if (S_ISGITLINK(p->one->mode)) {
598 /* NEEDSWORK: We should honor the name configured in
599 * the .gitmodules file of the commit we are examining
600 * here to be able to correctly follow submodules
601 * being moved around. */
602 struct string_list_item *path;
603 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
604 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
605 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
606 } else {
607 /* Submodule is new or was moved here */
608 /* NEEDSWORK: When the .git directories of submodules
609 * live inside the superprojects .git directory some
610 * day we should fetch new submodules directly into
611 * that location too when config or options request
612 * that so they can be checked out from there. */
613 continue;
618 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
619 int flags, void *data)
621 sha1_array_append(data, sha1);
622 return 0;
625 void check_for_new_submodule_commits(unsigned char new_sha1[20])
627 if (!initialized_fetch_ref_tips) {
628 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
629 initialized_fetch_ref_tips = 1;
632 sha1_array_append(&ref_tips_after_fetch, new_sha1);
635 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
637 argv_array_push(data, sha1_to_hex(sha1));
640 static void calculate_changed_submodule_paths(void)
642 struct rev_info rev;
643 struct commit *commit;
644 struct argv_array argv = ARGV_ARRAY_INIT;
646 /* No need to check if there are no submodules configured */
647 if (!config_name_for_path.nr)
648 return;
650 init_revisions(&rev, NULL);
651 argv_array_push(&argv, "--"); /* argv[0] program name */
652 sha1_array_for_each_unique(&ref_tips_after_fetch,
653 add_sha1_to_argv, &argv);
654 argv_array_push(&argv, "--not");
655 sha1_array_for_each_unique(&ref_tips_before_fetch,
656 add_sha1_to_argv, &argv);
657 setup_revisions(argv.argc, argv.argv, &rev, NULL);
658 if (prepare_revision_walk(&rev))
659 die("revision walk setup failed");
662 * Collect all submodules (whether checked out or not) for which new
663 * commits have been recorded upstream in "changed_submodule_paths".
665 while ((commit = get_revision(&rev))) {
666 struct commit_list *parent = commit->parents;
667 while (parent) {
668 struct diff_options diff_opts;
669 diff_setup(&diff_opts);
670 DIFF_OPT_SET(&diff_opts, RECURSIVE);
671 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
672 diff_opts.format_callback = submodule_collect_changed_cb;
673 diff_setup_done(&diff_opts);
674 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
675 diffcore_std(&diff_opts);
676 diff_flush(&diff_opts);
677 parent = parent->next;
681 argv_array_clear(&argv);
682 sha1_array_clear(&ref_tips_before_fetch);
683 sha1_array_clear(&ref_tips_after_fetch);
684 initialized_fetch_ref_tips = 0;
687 int fetch_populated_submodules(const struct argv_array *options,
688 const char *prefix, int command_line_option,
689 int quiet)
691 int i, result = 0;
692 struct child_process cp;
693 struct argv_array argv = ARGV_ARRAY_INIT;
694 struct string_list_item *name_for_path;
695 const char *work_tree = get_git_work_tree();
696 if (!work_tree)
697 goto out;
699 if (!the_index.initialized)
700 if (read_cache() < 0)
701 die("index file corrupt");
703 argv_array_push(&argv, "fetch");
704 for (i = 0; i < options->argc; i++)
705 argv_array_push(&argv, options->argv[i]);
706 argv_array_push(&argv, "--recurse-submodules-default");
707 /* default value, "--submodule-prefix" and its value are added later */
709 memset(&cp, 0, sizeof(cp));
710 cp.env = local_repo_env;
711 cp.git_cmd = 1;
712 cp.no_stdin = 1;
714 calculate_changed_submodule_paths();
716 for (i = 0; i < active_nr; i++) {
717 struct strbuf submodule_path = STRBUF_INIT;
718 struct strbuf submodule_git_dir = STRBUF_INIT;
719 struct strbuf submodule_prefix = STRBUF_INIT;
720 struct cache_entry *ce = active_cache[i];
721 const char *git_dir, *name, *default_argv;
723 if (!S_ISGITLINK(ce->ce_mode))
724 continue;
726 name = ce->name;
727 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
728 if (name_for_path)
729 name = name_for_path->util;
731 default_argv = "yes";
732 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
733 struct string_list_item *fetch_recurse_submodules_option;
734 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
735 if (fetch_recurse_submodules_option) {
736 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
737 continue;
738 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
739 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
740 continue;
741 default_argv = "on-demand";
743 } else {
744 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
745 gitmodules_is_unmerged)
746 continue;
747 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
748 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
749 continue;
750 default_argv = "on-demand";
753 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
754 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
755 continue;
756 default_argv = "on-demand";
759 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
760 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
761 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
762 git_dir = read_gitfile(submodule_git_dir.buf);
763 if (!git_dir)
764 git_dir = submodule_git_dir.buf;
765 if (is_directory(git_dir)) {
766 if (!quiet)
767 printf("Fetching submodule %s%s\n", prefix, ce->name);
768 cp.dir = submodule_path.buf;
769 argv_array_push(&argv, default_argv);
770 argv_array_push(&argv, "--submodule-prefix");
771 argv_array_push(&argv, submodule_prefix.buf);
772 cp.argv = argv.argv;
773 if (run_command(&cp))
774 result = 1;
775 argv_array_pop(&argv);
776 argv_array_pop(&argv);
777 argv_array_pop(&argv);
779 strbuf_release(&submodule_path);
780 strbuf_release(&submodule_git_dir);
781 strbuf_release(&submodule_prefix);
783 argv_array_clear(&argv);
784 out:
785 string_list_clear(&changed_submodule_paths, 1);
786 return result;
789 unsigned is_submodule_modified(const char *path, int ignore_untracked)
791 ssize_t len;
792 struct child_process cp;
793 const char *argv[] = {
794 "status",
795 "--porcelain",
796 NULL,
797 NULL,
799 struct strbuf buf = STRBUF_INIT;
800 unsigned dirty_submodule = 0;
801 const char *line, *next_line;
802 const char *git_dir;
804 strbuf_addf(&buf, "%s/.git", path);
805 git_dir = read_gitfile(buf.buf);
806 if (!git_dir)
807 git_dir = buf.buf;
808 if (!is_directory(git_dir)) {
809 strbuf_release(&buf);
810 /* The submodule is not checked out, so it is not modified */
811 return 0;
814 strbuf_reset(&buf);
816 if (ignore_untracked)
817 argv[2] = "-uno";
819 memset(&cp, 0, sizeof(cp));
820 cp.argv = argv;
821 cp.env = local_repo_env;
822 cp.git_cmd = 1;
823 cp.no_stdin = 1;
824 cp.out = -1;
825 cp.dir = path;
826 if (start_command(&cp))
827 die("Could not run 'git status --porcelain' in submodule %s", path);
829 len = strbuf_read(&buf, cp.out, 1024);
830 line = buf.buf;
831 while (len > 2) {
832 if ((line[0] == '?') && (line[1] == '?')) {
833 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
834 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
835 break;
836 } else {
837 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
838 if (ignore_untracked ||
839 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
840 break;
842 next_line = strchr(line, '\n');
843 if (!next_line)
844 break;
845 next_line++;
846 len -= (next_line - line);
847 line = next_line;
849 close(cp.out);
851 if (finish_command(&cp))
852 die("'git status --porcelain' failed in submodule %s", path);
854 strbuf_release(&buf);
855 return dirty_submodule;
858 int submodule_uses_gitfile(const char *path)
860 struct child_process cp;
861 const char *argv[] = {
862 "submodule",
863 "foreach",
864 "--quiet",
865 "--recursive",
866 "test -f .git",
867 NULL,
869 struct strbuf buf = STRBUF_INIT;
870 const char *git_dir;
872 strbuf_addf(&buf, "%s/.git", path);
873 git_dir = read_gitfile(buf.buf);
874 if (!git_dir) {
875 strbuf_release(&buf);
876 return 0;
878 strbuf_release(&buf);
880 /* Now test that all nested submodules use a gitfile too */
881 memset(&cp, 0, sizeof(cp));
882 cp.argv = argv;
883 cp.env = local_repo_env;
884 cp.git_cmd = 1;
885 cp.no_stdin = 1;
886 cp.no_stderr = 1;
887 cp.no_stdout = 1;
888 cp.dir = path;
889 if (run_command(&cp))
890 return 0;
892 return 1;
895 int ok_to_remove_submodule(const char *path)
897 struct stat st;
898 ssize_t len;
899 struct child_process cp;
900 const char *argv[] = {
901 "status",
902 "--porcelain",
903 "-u",
904 "--ignore-submodules=none",
905 NULL,
907 struct strbuf buf = STRBUF_INIT;
908 int ok_to_remove = 1;
910 if ((lstat(path, &st) < 0) || is_empty_dir(path))
911 return 1;
913 if (!submodule_uses_gitfile(path))
914 return 0;
916 memset(&cp, 0, sizeof(cp));
917 cp.argv = argv;
918 cp.env = local_repo_env;
919 cp.git_cmd = 1;
920 cp.no_stdin = 1;
921 cp.out = -1;
922 cp.dir = path;
923 if (start_command(&cp))
924 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
926 len = strbuf_read(&buf, cp.out, 1024);
927 if (len > 2)
928 ok_to_remove = 0;
929 close(cp.out);
931 if (finish_command(&cp))
932 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
934 strbuf_release(&buf);
935 return ok_to_remove;
938 static int find_first_merges(struct object_array *result, const char *path,
939 struct commit *a, struct commit *b)
941 int i, j;
942 struct object_array merges;
943 struct commit *commit;
944 int contains_another;
946 char merged_revision[42];
947 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
948 "--all", merged_revision, NULL };
949 struct rev_info revs;
950 struct setup_revision_opt rev_opts;
952 memset(&merges, 0, sizeof(merges));
953 memset(result, 0, sizeof(struct object_array));
954 memset(&rev_opts, 0, sizeof(rev_opts));
956 /* get all revisions that merge commit a */
957 snprintf(merged_revision, sizeof(merged_revision), "^%s",
958 sha1_to_hex(a->object.sha1));
959 init_revisions(&revs, NULL);
960 rev_opts.submodule = path;
961 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
963 /* save all revisions from the above list that contain b */
964 if (prepare_revision_walk(&revs))
965 die("revision walk setup failed");
966 while ((commit = get_revision(&revs)) != NULL) {
967 struct object *o = &(commit->object);
968 if (in_merge_bases(b, commit))
969 add_object_array(o, NULL, &merges);
971 reset_revision_walk();
973 /* Now we've got all merges that contain a and b. Prune all
974 * merges that contain another found merge and save them in
975 * result.
977 for (i = 0; i < merges.nr; i++) {
978 struct commit *m1 = (struct commit *) merges.objects[i].item;
980 contains_another = 0;
981 for (j = 0; j < merges.nr; j++) {
982 struct commit *m2 = (struct commit *) merges.objects[j].item;
983 if (i != j && in_merge_bases(m2, m1)) {
984 contains_another = 1;
985 break;
989 if (!contains_another)
990 add_object_array(merges.objects[i].item,
991 merges.objects[i].name, result);
994 free(merges.objects);
995 return result->nr;
998 static void print_commit(struct commit *commit)
1000 struct strbuf sb = STRBUF_INIT;
1001 struct pretty_print_context ctx = {0};
1002 ctx.date_mode = DATE_NORMAL;
1003 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1004 fprintf(stderr, "%s\n", sb.buf);
1005 strbuf_release(&sb);
1008 #define MERGE_WARNING(path, msg) \
1009 warning("Failed to merge submodule %s (%s)", path, msg);
1011 int merge_submodule(unsigned char result[20], const char *path,
1012 const unsigned char base[20], const unsigned char a[20],
1013 const unsigned char b[20], int search)
1015 struct commit *commit_base, *commit_a, *commit_b;
1016 int parent_count;
1017 struct object_array merges;
1019 int i;
1021 /* store a in result in case we fail */
1022 hashcpy(result, a);
1024 /* we can not handle deletion conflicts */
1025 if (is_null_sha1(base))
1026 return 0;
1027 if (is_null_sha1(a))
1028 return 0;
1029 if (is_null_sha1(b))
1030 return 0;
1032 if (add_submodule_odb(path)) {
1033 MERGE_WARNING(path, "not checked out");
1034 return 0;
1037 if (!(commit_base = lookup_commit_reference(base)) ||
1038 !(commit_a = lookup_commit_reference(a)) ||
1039 !(commit_b = lookup_commit_reference(b))) {
1040 MERGE_WARNING(path, "commits not present");
1041 return 0;
1044 /* check whether both changes are forward */
1045 if (!in_merge_bases(commit_base, commit_a) ||
1046 !in_merge_bases(commit_base, commit_b)) {
1047 MERGE_WARNING(path, "commits don't follow merge-base");
1048 return 0;
1051 /* Case #1: a is contained in b or vice versa */
1052 if (in_merge_bases(commit_a, commit_b)) {
1053 hashcpy(result, b);
1054 return 1;
1056 if (in_merge_bases(commit_b, commit_a)) {
1057 hashcpy(result, a);
1058 return 1;
1062 * Case #2: There are one or more merges that contain a and b in
1063 * the submodule. If there is only one, then present it as a
1064 * suggestion to the user, but leave it marked unmerged so the
1065 * user needs to confirm the resolution.
1068 /* Skip the search if makes no sense to the calling context. */
1069 if (!search)
1070 return 0;
1072 /* find commit which merges them */
1073 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1074 switch (parent_count) {
1075 case 0:
1076 MERGE_WARNING(path, "merge following commits not found");
1077 break;
1079 case 1:
1080 MERGE_WARNING(path, "not fast-forward");
1081 fprintf(stderr, "Found a possible merge resolution "
1082 "for the submodule:\n");
1083 print_commit((struct commit *) merges.objects[0].item);
1084 fprintf(stderr,
1085 "If this is correct simply add it to the index "
1086 "for example\n"
1087 "by using:\n\n"
1088 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1089 "which will accept this suggestion.\n",
1090 sha1_to_hex(merges.objects[0].item->sha1), path);
1091 break;
1093 default:
1094 MERGE_WARNING(path, "multiple merges found");
1095 for (i = 0; i < merges.nr; i++)
1096 print_commit((struct commit *) merges.objects[i].item);
1099 free(merges.objects);
1100 return 0;
1103 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1104 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1106 struct strbuf core_worktree_setting = STRBUF_INIT;
1107 struct strbuf configfile_name = STRBUF_INIT;
1108 struct strbuf gitfile_content = STRBUF_INIT;
1109 struct strbuf gitfile_name = STRBUF_INIT;
1110 char *real_work_tree = xstrdup(real_path(work_tree));
1111 char *to_free = real_work_tree;
1112 const char *pathspec[] = { real_work_tree, git_dir, NULL };
1113 const char *max_prefix = common_prefix(pathspec);
1114 FILE *fp;
1116 if (max_prefix) { /* skip common prefix */
1117 size_t max_prefix_len = strlen(max_prefix);
1118 real_work_tree += max_prefix_len;
1119 git_dir += max_prefix_len;
1123 * Update gitfile
1125 strbuf_addstr(&gitfile_content, "gitdir: ");
1126 if (real_work_tree[0]) {
1127 const char *s = real_work_tree;
1128 do {
1129 strbuf_addstr(&gitfile_content, "../");
1130 s++;
1131 } while ((s = strchr(s, '/')));
1133 strbuf_addstr(&gitfile_content, git_dir);
1134 strbuf_addch(&gitfile_content, '\n');
1136 strbuf_addf(&gitfile_name, "%s/.git", work_tree);
1137 fp = fopen(gitfile_name.buf, "w");
1138 if (!fp)
1139 die(_("Could not create git link %s"), gitfile_name.buf);
1140 fprintf(fp, "%s", gitfile_content.buf);
1141 fclose(fp);
1143 strbuf_release(&gitfile_content);
1144 strbuf_release(&gitfile_name);
1147 * Update core.worktree setting
1149 if (git_dir[0]) {
1150 const char *s = git_dir;
1151 do {
1152 strbuf_addstr(&core_worktree_setting, "../");
1153 s++;
1154 } while ((s = strchr(s, '/')));
1156 strbuf_addstr(&core_worktree_setting, real_work_tree);
1158 strbuf_addf(&configfile_name, "%s/config", git_dir);
1159 if (git_config_set_in_file(configfile_name.buf, "core.worktree",
1160 core_worktree_setting.buf))
1161 die(_("Could not set core.worktree in %s"),
1162 configfile_name.buf);
1164 strbuf_release(&core_worktree_setting);
1165 strbuf_release(&configfile_name);
1166 free(to_free);