submodule.c: add .gitmodules staging helper functions
[git.git] / submodule.c
blobfb5134a43ba5331c05418a314e25d8f3e0333129
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;
50 void stage_updated_gitmodules(void)
52 struct strbuf buf = STRBUF_INIT;
53 struct stat st;
54 int pos;
55 struct cache_entry *ce;
56 int namelen = strlen(".gitmodules");
58 pos = cache_name_pos(".gitmodules", namelen);
59 if (pos < 0) {
60 warning(_("could not find .gitmodules in index"));
61 return;
63 ce = active_cache[pos];
64 ce->ce_flags = namelen;
65 if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
66 die(_("reading updated .gitmodules failed"));
67 if (lstat(".gitmodules", &st) < 0)
68 die_errno(_("unable to stat updated .gitmodules"));
69 fill_stat_cache_info(ce, &st);
70 ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
71 if (remove_cache_entry_at(pos) < 0)
72 die(_("unable to remove .gitmodules from index"));
73 if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
74 die(_("adding updated .gitmodules failed"));
75 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
76 die(_("staging updated .gitmodules failed"));
79 static int add_submodule_odb(const char *path)
81 struct strbuf objects_directory = STRBUF_INIT;
82 struct alternate_object_database *alt_odb;
83 int ret = 0;
84 const char *git_dir;
86 strbuf_addf(&objects_directory, "%s/.git", path);
87 git_dir = read_gitfile(objects_directory.buf);
88 if (git_dir) {
89 strbuf_reset(&objects_directory);
90 strbuf_addstr(&objects_directory, git_dir);
92 strbuf_addstr(&objects_directory, "/objects/");
93 if (!is_directory(objects_directory.buf)) {
94 ret = -1;
95 goto done;
97 /* avoid adding it twice */
98 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
99 if (alt_odb->name - alt_odb->base == objects_directory.len &&
100 !strncmp(alt_odb->base, objects_directory.buf,
101 objects_directory.len))
102 goto done;
104 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
105 alt_odb->next = alt_odb_list;
106 strcpy(alt_odb->base, objects_directory.buf);
107 alt_odb->name = alt_odb->base + objects_directory.len;
108 alt_odb->name[2] = '/';
109 alt_odb->name[40] = '\0';
110 alt_odb->name[41] = '\0';
111 alt_odb_list = alt_odb;
113 /* add possible alternates from the submodule */
114 read_info_alternates(objects_directory.buf, 0);
115 prepare_alt_odb();
116 done:
117 strbuf_release(&objects_directory);
118 return ret;
121 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
122 const char *path)
124 struct string_list_item *path_option, *ignore_option;
125 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
126 if (path_option) {
127 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
128 if (ignore_option)
129 handle_ignore_submodules_arg(diffopt, ignore_option->util);
130 else if (gitmodules_is_unmerged)
131 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
135 int submodule_config(const char *var, const char *value, void *cb)
137 if (!prefixcmp(var, "submodule."))
138 return parse_submodule_config_option(var, value);
139 else if (!strcmp(var, "fetch.recursesubmodules")) {
140 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
141 return 0;
143 return 0;
146 void gitmodules_config(void)
148 const char *work_tree = get_git_work_tree();
149 if (work_tree) {
150 struct strbuf gitmodules_path = STRBUF_INIT;
151 int pos;
152 strbuf_addstr(&gitmodules_path, work_tree);
153 strbuf_addstr(&gitmodules_path, "/.gitmodules");
154 if (read_cache() < 0)
155 die("index file corrupt");
156 pos = cache_name_pos(".gitmodules", 11);
157 if (pos < 0) { /* .gitmodules not found or isn't merged */
158 pos = -1 - pos;
159 if (active_nr > pos) { /* there is a .gitmodules */
160 const struct cache_entry *ce = active_cache[pos];
161 if (ce_namelen(ce) == 11 &&
162 !memcmp(ce->name, ".gitmodules", 11))
163 gitmodules_is_unmerged = 1;
165 } else if (pos < active_nr) {
166 struct stat st;
167 if (lstat(".gitmodules", &st) == 0 &&
168 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
169 gitmodules_is_modified = 1;
172 if (!gitmodules_is_unmerged)
173 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
174 strbuf_release(&gitmodules_path);
178 int parse_submodule_config_option(const char *var, const char *value)
180 struct string_list_item *config;
181 const char *name, *key;
182 int namelen;
184 if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
185 return 0;
187 if (!strcmp(key, "path")) {
188 config = unsorted_string_list_lookup(&config_name_for_path, value);
189 if (config)
190 free(config->util);
191 else
192 config = string_list_append(&config_name_for_path, xstrdup(value));
193 config->util = xmemdupz(name, namelen);
194 } else if (!strcmp(key, "fetchrecursesubmodules")) {
195 char *name_cstr = xmemdupz(name, namelen);
196 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
197 if (!config)
198 config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
199 else
200 free(name_cstr);
201 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
202 } else if (!strcmp(key, "ignore")) {
203 char *name_cstr;
205 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
206 strcmp(value, "all") && strcmp(value, "none")) {
207 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
208 return 0;
211 name_cstr = xmemdupz(name, namelen);
212 config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
213 if (config) {
214 free(config->util);
215 free(name_cstr);
216 } else
217 config = string_list_append(&config_ignore_for_name, name_cstr);
218 config->util = xstrdup(value);
219 return 0;
221 return 0;
224 void handle_ignore_submodules_arg(struct diff_options *diffopt,
225 const char *arg)
227 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
228 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
229 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
231 if (!strcmp(arg, "all"))
232 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
233 else if (!strcmp(arg, "untracked"))
234 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
235 else if (!strcmp(arg, "dirty"))
236 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
237 else if (strcmp(arg, "none"))
238 die("bad --ignore-submodules argument: %s", arg);
241 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
242 struct commit *left, struct commit *right,
243 int *fast_forward, int *fast_backward)
245 struct commit_list *merge_bases, *list;
247 init_revisions(rev, NULL);
248 setup_revisions(0, NULL, rev, NULL);
249 rev->left_right = 1;
250 rev->first_parent_only = 1;
251 left->object.flags |= SYMMETRIC_LEFT;
252 add_pending_object(rev, &left->object, path);
253 add_pending_object(rev, &right->object, path);
254 merge_bases = get_merge_bases(left, right, 1);
255 if (merge_bases) {
256 if (merge_bases->item == left)
257 *fast_forward = 1;
258 else if (merge_bases->item == right)
259 *fast_backward = 1;
261 for (list = merge_bases; list; list = list->next) {
262 list->item->object.flags |= UNINTERESTING;
263 add_pending_object(rev, &list->item->object,
264 sha1_to_hex(list->item->object.sha1));
266 return prepare_revision_walk(rev);
269 static void print_submodule_summary(struct rev_info *rev, FILE *f,
270 const char *line_prefix,
271 const char *del, const char *add, const char *reset)
273 static const char format[] = " %m %s";
274 struct strbuf sb = STRBUF_INIT;
275 struct commit *commit;
277 while ((commit = get_revision(rev))) {
278 struct pretty_print_context ctx = {0};
279 ctx.date_mode = rev->date_mode;
280 ctx.output_encoding = get_log_output_encoding();
281 strbuf_setlen(&sb, 0);
282 strbuf_addstr(&sb, line_prefix);
283 if (commit->object.flags & SYMMETRIC_LEFT) {
284 if (del)
285 strbuf_addstr(&sb, del);
287 else if (add)
288 strbuf_addstr(&sb, add);
289 format_commit_message(commit, format, &sb, &ctx);
290 if (reset)
291 strbuf_addstr(&sb, reset);
292 strbuf_addch(&sb, '\n');
293 fprintf(f, "%s", sb.buf);
295 strbuf_release(&sb);
298 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
300 switch (git_config_maybe_bool(opt, arg)) {
301 case 1:
302 return RECURSE_SUBMODULES_ON;
303 case 0:
304 return RECURSE_SUBMODULES_OFF;
305 default:
306 if (!strcmp(arg, "on-demand"))
307 return RECURSE_SUBMODULES_ON_DEMAND;
308 die("bad %s argument: %s", opt, arg);
312 void show_submodule_summary(FILE *f, const char *path,
313 const char *line_prefix,
314 unsigned char one[20], unsigned char two[20],
315 unsigned dirty_submodule, const char *meta,
316 const char *del, const char *add, const char *reset)
318 struct rev_info rev;
319 struct commit *left = NULL, *right = NULL;
320 const char *message = NULL;
321 struct strbuf sb = STRBUF_INIT;
322 int fast_forward = 0, fast_backward = 0;
324 if (is_null_sha1(two))
325 message = "(submodule deleted)";
326 else if (add_submodule_odb(path))
327 message = "(not checked out)";
328 else if (is_null_sha1(one))
329 message = "(new submodule)";
330 else if (!(left = lookup_commit_reference(one)) ||
331 !(right = lookup_commit_reference(two)))
332 message = "(commits not present)";
333 else if (prepare_submodule_summary(&rev, path, left, right,
334 &fast_forward, &fast_backward))
335 message = "(revision walker failed)";
337 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
338 fprintf(f, "%sSubmodule %s contains untracked content\n",
339 line_prefix, path);
340 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
341 fprintf(f, "%sSubmodule %s contains modified content\n",
342 line_prefix, path);
344 if (!hashcmp(one, two)) {
345 strbuf_release(&sb);
346 return;
349 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
350 find_unique_abbrev(one, DEFAULT_ABBREV));
351 if (!fast_backward && !fast_forward)
352 strbuf_addch(&sb, '.');
353 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
354 if (message)
355 strbuf_addf(&sb, " %s%s\n", message, reset);
356 else
357 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
358 fwrite(sb.buf, sb.len, 1, f);
360 if (!message) /* only NULL if we succeeded in setting up the walk */
361 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
362 if (left)
363 clear_commit_marks(left, ~0);
364 if (right)
365 clear_commit_marks(right, ~0);
367 strbuf_release(&sb);
370 void set_config_fetch_recurse_submodules(int value)
372 config_fetch_recurse_submodules = value;
375 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
377 return 1;
380 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
382 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
383 return 0;
385 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
386 struct child_process cp;
387 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
388 struct strbuf buf = STRBUF_INIT;
389 int needs_pushing = 0;
391 argv[1] = sha1_to_hex(sha1);
392 memset(&cp, 0, sizeof(cp));
393 cp.argv = argv;
394 cp.env = local_repo_env;
395 cp.git_cmd = 1;
396 cp.no_stdin = 1;
397 cp.out = -1;
398 cp.dir = path;
399 if (start_command(&cp))
400 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
401 sha1_to_hex(sha1), path);
402 if (strbuf_read(&buf, cp.out, 41))
403 needs_pushing = 1;
404 finish_command(&cp);
405 close(cp.out);
406 strbuf_release(&buf);
407 return needs_pushing;
410 return 0;
413 static void collect_submodules_from_diff(struct diff_queue_struct *q,
414 struct diff_options *options,
415 void *data)
417 int i;
418 struct string_list *needs_pushing = data;
420 for (i = 0; i < q->nr; i++) {
421 struct diff_filepair *p = q->queue[i];
422 if (!S_ISGITLINK(p->two->mode))
423 continue;
424 if (submodule_needs_pushing(p->two->path, p->two->sha1))
425 string_list_insert(needs_pushing, p->two->path);
429 static void find_unpushed_submodule_commits(struct commit *commit,
430 struct string_list *needs_pushing)
432 struct rev_info rev;
434 init_revisions(&rev, NULL);
435 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
436 rev.diffopt.format_callback = collect_submodules_from_diff;
437 rev.diffopt.format_callback_data = needs_pushing;
438 diff_tree_combined_merge(commit, 1, &rev);
441 int find_unpushed_submodules(unsigned char new_sha1[20],
442 const char *remotes_name, struct string_list *needs_pushing)
444 struct rev_info rev;
445 struct commit *commit;
446 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
447 int argc = ARRAY_SIZE(argv) - 1;
448 char *sha1_copy;
450 struct strbuf remotes_arg = STRBUF_INIT;
452 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
453 init_revisions(&rev, NULL);
454 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
455 argv[1] = sha1_copy;
456 argv[3] = remotes_arg.buf;
457 setup_revisions(argc, argv, &rev, NULL);
458 if (prepare_revision_walk(&rev))
459 die("revision walk setup failed");
461 while ((commit = get_revision(&rev)) != NULL)
462 find_unpushed_submodule_commits(commit, needs_pushing);
464 reset_revision_walk();
465 free(sha1_copy);
466 strbuf_release(&remotes_arg);
468 return needs_pushing->nr;
471 static int push_submodule(const char *path)
473 if (add_submodule_odb(path))
474 return 1;
476 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
477 struct child_process cp;
478 const char *argv[] = {"push", NULL};
480 memset(&cp, 0, sizeof(cp));
481 cp.argv = argv;
482 cp.env = local_repo_env;
483 cp.git_cmd = 1;
484 cp.no_stdin = 1;
485 cp.dir = path;
486 if (run_command(&cp))
487 return 0;
488 close(cp.out);
491 return 1;
494 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
496 int i, ret = 1;
497 struct string_list needs_pushing;
499 memset(&needs_pushing, 0, sizeof(struct string_list));
500 needs_pushing.strdup_strings = 1;
502 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
503 return 1;
505 for (i = 0; i < needs_pushing.nr; i++) {
506 const char *path = needs_pushing.items[i].string;
507 fprintf(stderr, "Pushing submodule '%s'\n", path);
508 if (!push_submodule(path)) {
509 fprintf(stderr, "Unable to push submodule '%s'\n", path);
510 ret = 0;
514 string_list_clear(&needs_pushing, 0);
516 return ret;
519 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
521 int is_present = 0;
522 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
523 /* Even if the submodule is checked out and the commit is
524 * present, make sure it is reachable from a ref. */
525 struct child_process cp;
526 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
527 struct strbuf buf = STRBUF_INIT;
529 argv[3] = sha1_to_hex(sha1);
530 memset(&cp, 0, sizeof(cp));
531 cp.argv = argv;
532 cp.env = local_repo_env;
533 cp.git_cmd = 1;
534 cp.no_stdin = 1;
535 cp.out = -1;
536 cp.dir = path;
537 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
538 is_present = 1;
540 close(cp.out);
541 strbuf_release(&buf);
543 return is_present;
546 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
547 struct diff_options *options,
548 void *data)
550 int i;
551 for (i = 0; i < q->nr; i++) {
552 struct diff_filepair *p = q->queue[i];
553 if (!S_ISGITLINK(p->two->mode))
554 continue;
556 if (S_ISGITLINK(p->one->mode)) {
557 /* NEEDSWORK: We should honor the name configured in
558 * the .gitmodules file of the commit we are examining
559 * here to be able to correctly follow submodules
560 * being moved around. */
561 struct string_list_item *path;
562 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
563 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
564 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
565 } else {
566 /* Submodule is new or was moved here */
567 /* NEEDSWORK: When the .git directories of submodules
568 * live inside the superprojects .git directory some
569 * day we should fetch new submodules directly into
570 * that location too when config or options request
571 * that so they can be checked out from there. */
572 continue;
577 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
578 int flags, void *data)
580 sha1_array_append(data, sha1);
581 return 0;
584 void check_for_new_submodule_commits(unsigned char new_sha1[20])
586 if (!initialized_fetch_ref_tips) {
587 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
588 initialized_fetch_ref_tips = 1;
591 sha1_array_append(&ref_tips_after_fetch, new_sha1);
594 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
596 argv_array_push(data, sha1_to_hex(sha1));
599 static void calculate_changed_submodule_paths(void)
601 struct rev_info rev;
602 struct commit *commit;
603 struct argv_array argv = ARGV_ARRAY_INIT;
605 /* No need to check if there are no submodules configured */
606 if (!config_name_for_path.nr)
607 return;
609 init_revisions(&rev, NULL);
610 argv_array_push(&argv, "--"); /* argv[0] program name */
611 sha1_array_for_each_unique(&ref_tips_after_fetch,
612 add_sha1_to_argv, &argv);
613 argv_array_push(&argv, "--not");
614 sha1_array_for_each_unique(&ref_tips_before_fetch,
615 add_sha1_to_argv, &argv);
616 setup_revisions(argv.argc, argv.argv, &rev, NULL);
617 if (prepare_revision_walk(&rev))
618 die("revision walk setup failed");
621 * Collect all submodules (whether checked out or not) for which new
622 * commits have been recorded upstream in "changed_submodule_paths".
624 while ((commit = get_revision(&rev))) {
625 struct commit_list *parent = commit->parents;
626 while (parent) {
627 struct diff_options diff_opts;
628 diff_setup(&diff_opts);
629 DIFF_OPT_SET(&diff_opts, RECURSIVE);
630 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
631 diff_opts.format_callback = submodule_collect_changed_cb;
632 diff_setup_done(&diff_opts);
633 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
634 diffcore_std(&diff_opts);
635 diff_flush(&diff_opts);
636 parent = parent->next;
640 argv_array_clear(&argv);
641 sha1_array_clear(&ref_tips_before_fetch);
642 sha1_array_clear(&ref_tips_after_fetch);
643 initialized_fetch_ref_tips = 0;
646 int fetch_populated_submodules(const struct argv_array *options,
647 const char *prefix, int command_line_option,
648 int quiet)
650 int i, result = 0;
651 struct child_process cp;
652 struct argv_array argv = ARGV_ARRAY_INIT;
653 struct string_list_item *name_for_path;
654 const char *work_tree = get_git_work_tree();
655 if (!work_tree)
656 goto out;
658 if (read_cache() < 0)
659 die("index file corrupt");
661 argv_array_push(&argv, "fetch");
662 for (i = 0; i < options->argc; i++)
663 argv_array_push(&argv, options->argv[i]);
664 argv_array_push(&argv, "--recurse-submodules-default");
665 /* default value, "--submodule-prefix" and its value are added later */
667 memset(&cp, 0, sizeof(cp));
668 cp.env = local_repo_env;
669 cp.git_cmd = 1;
670 cp.no_stdin = 1;
672 calculate_changed_submodule_paths();
674 for (i = 0; i < active_nr; i++) {
675 struct strbuf submodule_path = STRBUF_INIT;
676 struct strbuf submodule_git_dir = STRBUF_INIT;
677 struct strbuf submodule_prefix = STRBUF_INIT;
678 struct cache_entry *ce = active_cache[i];
679 const char *git_dir, *name, *default_argv;
681 if (!S_ISGITLINK(ce->ce_mode))
682 continue;
684 name = ce->name;
685 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
686 if (name_for_path)
687 name = name_for_path->util;
689 default_argv = "yes";
690 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
691 struct string_list_item *fetch_recurse_submodules_option;
692 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
693 if (fetch_recurse_submodules_option) {
694 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
695 continue;
696 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
697 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
698 continue;
699 default_argv = "on-demand";
701 } else {
702 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
703 gitmodules_is_unmerged)
704 continue;
705 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
706 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
707 continue;
708 default_argv = "on-demand";
711 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
712 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
713 continue;
714 default_argv = "on-demand";
717 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
718 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
719 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
720 git_dir = read_gitfile(submodule_git_dir.buf);
721 if (!git_dir)
722 git_dir = submodule_git_dir.buf;
723 if (is_directory(git_dir)) {
724 if (!quiet)
725 printf("Fetching submodule %s%s\n", prefix, ce->name);
726 cp.dir = submodule_path.buf;
727 argv_array_push(&argv, default_argv);
728 argv_array_push(&argv, "--submodule-prefix");
729 argv_array_push(&argv, submodule_prefix.buf);
730 cp.argv = argv.argv;
731 if (run_command(&cp))
732 result = 1;
733 argv_array_pop(&argv);
734 argv_array_pop(&argv);
735 argv_array_pop(&argv);
737 strbuf_release(&submodule_path);
738 strbuf_release(&submodule_git_dir);
739 strbuf_release(&submodule_prefix);
741 argv_array_clear(&argv);
742 out:
743 string_list_clear(&changed_submodule_paths, 1);
744 return result;
747 unsigned is_submodule_modified(const char *path, int ignore_untracked)
749 ssize_t len;
750 struct child_process cp;
751 const char *argv[] = {
752 "status",
753 "--porcelain",
754 NULL,
755 NULL,
757 struct strbuf buf = STRBUF_INIT;
758 unsigned dirty_submodule = 0;
759 const char *line, *next_line;
760 const char *git_dir;
762 strbuf_addf(&buf, "%s/.git", path);
763 git_dir = read_gitfile(buf.buf);
764 if (!git_dir)
765 git_dir = buf.buf;
766 if (!is_directory(git_dir)) {
767 strbuf_release(&buf);
768 /* The submodule is not checked out, so it is not modified */
769 return 0;
772 strbuf_reset(&buf);
774 if (ignore_untracked)
775 argv[2] = "-uno";
777 memset(&cp, 0, sizeof(cp));
778 cp.argv = argv;
779 cp.env = local_repo_env;
780 cp.git_cmd = 1;
781 cp.no_stdin = 1;
782 cp.out = -1;
783 cp.dir = path;
784 if (start_command(&cp))
785 die("Could not run 'git status --porcelain' in submodule %s", path);
787 len = strbuf_read(&buf, cp.out, 1024);
788 line = buf.buf;
789 while (len > 2) {
790 if ((line[0] == '?') && (line[1] == '?')) {
791 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
792 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
793 break;
794 } else {
795 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
796 if (ignore_untracked ||
797 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
798 break;
800 next_line = strchr(line, '\n');
801 if (!next_line)
802 break;
803 next_line++;
804 len -= (next_line - line);
805 line = next_line;
807 close(cp.out);
809 if (finish_command(&cp))
810 die("'git status --porcelain' failed in submodule %s", path);
812 strbuf_release(&buf);
813 return dirty_submodule;
816 int submodule_uses_gitfile(const char *path)
818 struct child_process cp;
819 const char *argv[] = {
820 "submodule",
821 "foreach",
822 "--quiet",
823 "--recursive",
824 "test -f .git",
825 NULL,
827 struct strbuf buf = STRBUF_INIT;
828 const char *git_dir;
830 strbuf_addf(&buf, "%s/.git", path);
831 git_dir = read_gitfile(buf.buf);
832 if (!git_dir) {
833 strbuf_release(&buf);
834 return 0;
836 strbuf_release(&buf);
838 /* Now test that all nested submodules use a gitfile too */
839 memset(&cp, 0, sizeof(cp));
840 cp.argv = argv;
841 cp.env = local_repo_env;
842 cp.git_cmd = 1;
843 cp.no_stdin = 1;
844 cp.no_stderr = 1;
845 cp.no_stdout = 1;
846 cp.dir = path;
847 if (run_command(&cp))
848 return 0;
850 return 1;
853 int ok_to_remove_submodule(const char *path)
855 struct stat st;
856 ssize_t len;
857 struct child_process cp;
858 const char *argv[] = {
859 "status",
860 "--porcelain",
861 "-u",
862 "--ignore-submodules=none",
863 NULL,
865 struct strbuf buf = STRBUF_INIT;
866 int ok_to_remove = 1;
868 if ((lstat(path, &st) < 0) || is_empty_dir(path))
869 return 1;
871 if (!submodule_uses_gitfile(path))
872 return 0;
874 memset(&cp, 0, sizeof(cp));
875 cp.argv = argv;
876 cp.env = local_repo_env;
877 cp.git_cmd = 1;
878 cp.no_stdin = 1;
879 cp.out = -1;
880 cp.dir = path;
881 if (start_command(&cp))
882 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
884 len = strbuf_read(&buf, cp.out, 1024);
885 if (len > 2)
886 ok_to_remove = 0;
887 close(cp.out);
889 if (finish_command(&cp))
890 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
892 strbuf_release(&buf);
893 return ok_to_remove;
896 static int find_first_merges(struct object_array *result, const char *path,
897 struct commit *a, struct commit *b)
899 int i, j;
900 struct object_array merges = OBJECT_ARRAY_INIT;
901 struct commit *commit;
902 int contains_another;
904 char merged_revision[42];
905 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
906 "--all", merged_revision, NULL };
907 struct rev_info revs;
908 struct setup_revision_opt rev_opts;
910 memset(result, 0, sizeof(struct object_array));
911 memset(&rev_opts, 0, sizeof(rev_opts));
913 /* get all revisions that merge commit a */
914 snprintf(merged_revision, sizeof(merged_revision), "^%s",
915 sha1_to_hex(a->object.sha1));
916 init_revisions(&revs, NULL);
917 rev_opts.submodule = path;
918 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
920 /* save all revisions from the above list that contain b */
921 if (prepare_revision_walk(&revs))
922 die("revision walk setup failed");
923 while ((commit = get_revision(&revs)) != NULL) {
924 struct object *o = &(commit->object);
925 if (in_merge_bases(b, commit))
926 add_object_array(o, NULL, &merges);
928 reset_revision_walk();
930 /* Now we've got all merges that contain a and b. Prune all
931 * merges that contain another found merge and save them in
932 * result.
934 for (i = 0; i < merges.nr; i++) {
935 struct commit *m1 = (struct commit *) merges.objects[i].item;
937 contains_another = 0;
938 for (j = 0; j < merges.nr; j++) {
939 struct commit *m2 = (struct commit *) merges.objects[j].item;
940 if (i != j && in_merge_bases(m2, m1)) {
941 contains_another = 1;
942 break;
946 if (!contains_another)
947 add_object_array(merges.objects[i].item, NULL, result);
950 free(merges.objects);
951 return result->nr;
954 static void print_commit(struct commit *commit)
956 struct strbuf sb = STRBUF_INIT;
957 struct pretty_print_context ctx = {0};
958 ctx.date_mode = DATE_NORMAL;
959 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
960 fprintf(stderr, "%s\n", sb.buf);
961 strbuf_release(&sb);
964 #define MERGE_WARNING(path, msg) \
965 warning("Failed to merge submodule %s (%s)", path, msg);
967 int merge_submodule(unsigned char result[20], const char *path,
968 const unsigned char base[20], const unsigned char a[20],
969 const unsigned char b[20], int search)
971 struct commit *commit_base, *commit_a, *commit_b;
972 int parent_count;
973 struct object_array merges;
975 int i;
977 /* store a in result in case we fail */
978 hashcpy(result, a);
980 /* we can not handle deletion conflicts */
981 if (is_null_sha1(base))
982 return 0;
983 if (is_null_sha1(a))
984 return 0;
985 if (is_null_sha1(b))
986 return 0;
988 if (add_submodule_odb(path)) {
989 MERGE_WARNING(path, "not checked out");
990 return 0;
993 if (!(commit_base = lookup_commit_reference(base)) ||
994 !(commit_a = lookup_commit_reference(a)) ||
995 !(commit_b = lookup_commit_reference(b))) {
996 MERGE_WARNING(path, "commits not present");
997 return 0;
1000 /* check whether both changes are forward */
1001 if (!in_merge_bases(commit_base, commit_a) ||
1002 !in_merge_bases(commit_base, commit_b)) {
1003 MERGE_WARNING(path, "commits don't follow merge-base");
1004 return 0;
1007 /* Case #1: a is contained in b or vice versa */
1008 if (in_merge_bases(commit_a, commit_b)) {
1009 hashcpy(result, b);
1010 return 1;
1012 if (in_merge_bases(commit_b, commit_a)) {
1013 hashcpy(result, a);
1014 return 1;
1018 * Case #2: There are one or more merges that contain a and b in
1019 * the submodule. If there is only one, then present it as a
1020 * suggestion to the user, but leave it marked unmerged so the
1021 * user needs to confirm the resolution.
1024 /* Skip the search if makes no sense to the calling context. */
1025 if (!search)
1026 return 0;
1028 /* find commit which merges them */
1029 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1030 switch (parent_count) {
1031 case 0:
1032 MERGE_WARNING(path, "merge following commits not found");
1033 break;
1035 case 1:
1036 MERGE_WARNING(path, "not fast-forward");
1037 fprintf(stderr, "Found a possible merge resolution "
1038 "for the submodule:\n");
1039 print_commit((struct commit *) merges.objects[0].item);
1040 fprintf(stderr,
1041 "If this is correct simply add it to the index "
1042 "for example\n"
1043 "by using:\n\n"
1044 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1045 "which will accept this suggestion.\n",
1046 sha1_to_hex(merges.objects[0].item->sha1), path);
1047 break;
1049 default:
1050 MERGE_WARNING(path, "multiple merges found");
1051 for (i = 0; i < merges.nr; i++)
1052 print_commit((struct commit *) merges.objects[i].item);
1055 free(merges.objects);
1056 return 0;
1059 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1060 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1062 struct strbuf file_name = STRBUF_INIT;
1063 struct strbuf rel_path = STRBUF_INIT;
1064 const char *real_work_tree = xstrdup(real_path(work_tree));
1065 FILE *fp;
1067 /* Update gitfile */
1068 strbuf_addf(&file_name, "%s/.git", work_tree);
1069 fp = fopen(file_name.buf, "w");
1070 if (!fp)
1071 die(_("Could not create git link %s"), file_name.buf);
1072 fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree,
1073 &rel_path));
1074 fclose(fp);
1076 /* Update core.worktree setting */
1077 strbuf_reset(&file_name);
1078 strbuf_addf(&file_name, "%s/config", git_dir);
1079 if (git_config_set_in_file(file_name.buf, "core.worktree",
1080 relative_path(real_work_tree, git_dir,
1081 &rel_path)))
1082 die(_("Could not set core.worktree in %s"),
1083 file_name.buf);
1085 strbuf_release(&file_name);
1086 strbuf_release(&rel_path);
1087 free((void *)real_work_tree);