mv: update the path entry in .gitmodules for moved submodules
[git/jrn.git] / submodule.c
blobd4e8276f17440bf3886578fdd3f882b770e0e97f
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;
84 void stage_updated_gitmodules(void)
86 struct strbuf buf = STRBUF_INIT;
87 struct stat st;
88 int pos;
89 struct cache_entry *ce;
90 int namelen = strlen(".gitmodules");
92 pos = cache_name_pos(".gitmodules", namelen);
93 if (pos < 0) {
94 warning(_("could not find .gitmodules in index"));
95 return;
97 ce = active_cache[pos];
98 ce->ce_flags = namelen;
99 if (strbuf_read_file(&buf, ".gitmodules", 0) < 0)
100 die(_("reading updated .gitmodules failed"));
101 if (lstat(".gitmodules", &st) < 0)
102 die_errno(_("unable to stat updated .gitmodules"));
103 fill_stat_cache_info(ce, &st);
104 ce->ce_mode = ce_mode_from_stat(ce, st.st_mode);
105 if (remove_cache_entry_at(pos) < 0)
106 die(_("unable to remove .gitmodules from index"));
107 if (write_sha1_file(buf.buf, buf.len, blob_type, ce->sha1))
108 die(_("adding updated .gitmodules failed"));
109 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))
110 die(_("staging updated .gitmodules failed"));
113 static int add_submodule_odb(const char *path)
115 struct strbuf objects_directory = STRBUF_INIT;
116 struct alternate_object_database *alt_odb;
117 int ret = 0;
118 const char *git_dir;
120 strbuf_addf(&objects_directory, "%s/.git", path);
121 git_dir = read_gitfile(objects_directory.buf);
122 if (git_dir) {
123 strbuf_reset(&objects_directory);
124 strbuf_addstr(&objects_directory, git_dir);
126 strbuf_addstr(&objects_directory, "/objects/");
127 if (!is_directory(objects_directory.buf)) {
128 ret = -1;
129 goto done;
131 /* avoid adding it twice */
132 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
133 if (alt_odb->name - alt_odb->base == objects_directory.len &&
134 !strncmp(alt_odb->base, objects_directory.buf,
135 objects_directory.len))
136 goto done;
138 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
139 alt_odb->next = alt_odb_list;
140 strcpy(alt_odb->base, objects_directory.buf);
141 alt_odb->name = alt_odb->base + objects_directory.len;
142 alt_odb->name[2] = '/';
143 alt_odb->name[40] = '\0';
144 alt_odb->name[41] = '\0';
145 alt_odb_list = alt_odb;
147 /* add possible alternates from the submodule */
148 read_info_alternates(objects_directory.buf, 0);
149 prepare_alt_odb();
150 done:
151 strbuf_release(&objects_directory);
152 return ret;
155 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
156 const char *path)
158 struct string_list_item *path_option, *ignore_option;
159 path_option = unsorted_string_list_lookup(&config_name_for_path, path);
160 if (path_option) {
161 ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util);
162 if (ignore_option)
163 handle_ignore_submodules_arg(diffopt, ignore_option->util);
164 else if (gitmodules_is_unmerged)
165 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
169 int submodule_config(const char *var, const char *value, void *cb)
171 if (!prefixcmp(var, "submodule."))
172 return parse_submodule_config_option(var, value);
173 else if (!strcmp(var, "fetch.recursesubmodules")) {
174 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
175 return 0;
177 return 0;
180 void gitmodules_config(void)
182 const char *work_tree = get_git_work_tree();
183 if (work_tree) {
184 struct strbuf gitmodules_path = STRBUF_INIT;
185 int pos;
186 strbuf_addstr(&gitmodules_path, work_tree);
187 strbuf_addstr(&gitmodules_path, "/.gitmodules");
188 if (read_cache() < 0)
189 die("index file corrupt");
190 pos = cache_name_pos(".gitmodules", 11);
191 if (pos < 0) { /* .gitmodules not found or isn't merged */
192 pos = -1 - pos;
193 if (active_nr > pos) { /* there is a .gitmodules */
194 const struct cache_entry *ce = active_cache[pos];
195 if (ce_namelen(ce) == 11 &&
196 !memcmp(ce->name, ".gitmodules", 11))
197 gitmodules_is_unmerged = 1;
199 } else if (pos < active_nr) {
200 struct stat st;
201 if (lstat(".gitmodules", &st) == 0 &&
202 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
203 gitmodules_is_modified = 1;
206 if (!gitmodules_is_unmerged)
207 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
208 strbuf_release(&gitmodules_path);
212 int parse_submodule_config_option(const char *var, const char *value)
214 struct string_list_item *config;
215 const char *name, *key;
216 int namelen;
218 if (parse_config_key(var, "submodule", &name, &namelen, &key) < 0 || !name)
219 return 0;
221 if (!strcmp(key, "path")) {
222 config = unsorted_string_list_lookup(&config_name_for_path, value);
223 if (config)
224 free(config->util);
225 else
226 config = string_list_append(&config_name_for_path, xstrdup(value));
227 config->util = xmemdupz(name, namelen);
228 } else if (!strcmp(key, "fetchrecursesubmodules")) {
229 char *name_cstr = xmemdupz(name, namelen);
230 config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name_cstr);
231 if (!config)
232 config = string_list_append(&config_fetch_recurse_submodules_for_name, name_cstr);
233 else
234 free(name_cstr);
235 config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value);
236 } else if (!strcmp(key, "ignore")) {
237 char *name_cstr;
239 if (strcmp(value, "untracked") && strcmp(value, "dirty") &&
240 strcmp(value, "all") && strcmp(value, "none")) {
241 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var);
242 return 0;
245 name_cstr = xmemdupz(name, namelen);
246 config = unsorted_string_list_lookup(&config_ignore_for_name, name_cstr);
247 if (config) {
248 free(config->util);
249 free(name_cstr);
250 } else
251 config = string_list_append(&config_ignore_for_name, name_cstr);
252 config->util = xstrdup(value);
253 return 0;
255 return 0;
258 void handle_ignore_submodules_arg(struct diff_options *diffopt,
259 const char *arg)
261 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
262 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
263 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
265 if (!strcmp(arg, "all"))
266 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
267 else if (!strcmp(arg, "untracked"))
268 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
269 else if (!strcmp(arg, "dirty"))
270 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
271 else if (strcmp(arg, "none"))
272 die("bad --ignore-submodules argument: %s", arg);
275 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
276 struct commit *left, struct commit *right,
277 int *fast_forward, int *fast_backward)
279 struct commit_list *merge_bases, *list;
281 init_revisions(rev, NULL);
282 setup_revisions(0, NULL, rev, NULL);
283 rev->left_right = 1;
284 rev->first_parent_only = 1;
285 left->object.flags |= SYMMETRIC_LEFT;
286 add_pending_object(rev, &left->object, path);
287 add_pending_object(rev, &right->object, path);
288 merge_bases = get_merge_bases(left, right, 1);
289 if (merge_bases) {
290 if (merge_bases->item == left)
291 *fast_forward = 1;
292 else if (merge_bases->item == right)
293 *fast_backward = 1;
295 for (list = merge_bases; list; list = list->next) {
296 list->item->object.flags |= UNINTERESTING;
297 add_pending_object(rev, &list->item->object,
298 sha1_to_hex(list->item->object.sha1));
300 return prepare_revision_walk(rev);
303 static void print_submodule_summary(struct rev_info *rev, FILE *f,
304 const char *line_prefix,
305 const char *del, const char *add, const char *reset)
307 static const char format[] = " %m %s";
308 struct strbuf sb = STRBUF_INIT;
309 struct commit *commit;
311 while ((commit = get_revision(rev))) {
312 struct pretty_print_context ctx = {0};
313 ctx.date_mode = rev->date_mode;
314 ctx.output_encoding = get_log_output_encoding();
315 strbuf_setlen(&sb, 0);
316 strbuf_addstr(&sb, line_prefix);
317 if (commit->object.flags & SYMMETRIC_LEFT) {
318 if (del)
319 strbuf_addstr(&sb, del);
321 else if (add)
322 strbuf_addstr(&sb, add);
323 format_commit_message(commit, format, &sb, &ctx);
324 if (reset)
325 strbuf_addstr(&sb, reset);
326 strbuf_addch(&sb, '\n');
327 fprintf(f, "%s", sb.buf);
329 strbuf_release(&sb);
332 int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
334 switch (git_config_maybe_bool(opt, arg)) {
335 case 1:
336 return RECURSE_SUBMODULES_ON;
337 case 0:
338 return RECURSE_SUBMODULES_OFF;
339 default:
340 if (!strcmp(arg, "on-demand"))
341 return RECURSE_SUBMODULES_ON_DEMAND;
342 die("bad %s argument: %s", opt, arg);
346 void show_submodule_summary(FILE *f, const char *path,
347 const char *line_prefix,
348 unsigned char one[20], unsigned char two[20],
349 unsigned dirty_submodule, const char *meta,
350 const char *del, const char *add, const char *reset)
352 struct rev_info rev;
353 struct commit *left = NULL, *right = NULL;
354 const char *message = NULL;
355 struct strbuf sb = STRBUF_INIT;
356 int fast_forward = 0, fast_backward = 0;
358 if (is_null_sha1(two))
359 message = "(submodule deleted)";
360 else if (add_submodule_odb(path))
361 message = "(not checked out)";
362 else if (is_null_sha1(one))
363 message = "(new submodule)";
364 else if (!(left = lookup_commit_reference(one)) ||
365 !(right = lookup_commit_reference(two)))
366 message = "(commits not present)";
367 else if (prepare_submodule_summary(&rev, path, left, right,
368 &fast_forward, &fast_backward))
369 message = "(revision walker failed)";
371 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
372 fprintf(f, "%sSubmodule %s contains untracked content\n",
373 line_prefix, path);
374 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
375 fprintf(f, "%sSubmodule %s contains modified content\n",
376 line_prefix, path);
378 if (!hashcmp(one, two)) {
379 strbuf_release(&sb);
380 return;
383 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
384 find_unique_abbrev(one, DEFAULT_ABBREV));
385 if (!fast_backward && !fast_forward)
386 strbuf_addch(&sb, '.');
387 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
388 if (message)
389 strbuf_addf(&sb, " %s%s\n", message, reset);
390 else
391 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
392 fwrite(sb.buf, sb.len, 1, f);
394 if (!message) /* only NULL if we succeeded in setting up the walk */
395 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
396 if (left)
397 clear_commit_marks(left, ~0);
398 if (right)
399 clear_commit_marks(right, ~0);
401 strbuf_release(&sb);
404 void set_config_fetch_recurse_submodules(int value)
406 config_fetch_recurse_submodules = value;
409 static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
411 return 1;
414 static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
416 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
417 return 0;
419 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
420 struct child_process cp;
421 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
422 struct strbuf buf = STRBUF_INIT;
423 int needs_pushing = 0;
425 argv[1] = sha1_to_hex(sha1);
426 memset(&cp, 0, sizeof(cp));
427 cp.argv = argv;
428 cp.env = local_repo_env;
429 cp.git_cmd = 1;
430 cp.no_stdin = 1;
431 cp.out = -1;
432 cp.dir = path;
433 if (start_command(&cp))
434 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
435 sha1_to_hex(sha1), path);
436 if (strbuf_read(&buf, cp.out, 41))
437 needs_pushing = 1;
438 finish_command(&cp);
439 close(cp.out);
440 strbuf_release(&buf);
441 return needs_pushing;
444 return 0;
447 static void collect_submodules_from_diff(struct diff_queue_struct *q,
448 struct diff_options *options,
449 void *data)
451 int i;
452 struct string_list *needs_pushing = data;
454 for (i = 0; i < q->nr; i++) {
455 struct diff_filepair *p = q->queue[i];
456 if (!S_ISGITLINK(p->two->mode))
457 continue;
458 if (submodule_needs_pushing(p->two->path, p->two->sha1))
459 string_list_insert(needs_pushing, p->two->path);
463 static void find_unpushed_submodule_commits(struct commit *commit,
464 struct string_list *needs_pushing)
466 struct rev_info rev;
468 init_revisions(&rev, NULL);
469 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
470 rev.diffopt.format_callback = collect_submodules_from_diff;
471 rev.diffopt.format_callback_data = needs_pushing;
472 diff_tree_combined_merge(commit, 1, &rev);
475 int find_unpushed_submodules(unsigned char new_sha1[20],
476 const char *remotes_name, struct string_list *needs_pushing)
478 struct rev_info rev;
479 struct commit *commit;
480 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
481 int argc = ARRAY_SIZE(argv) - 1;
482 char *sha1_copy;
484 struct strbuf remotes_arg = STRBUF_INIT;
486 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
487 init_revisions(&rev, NULL);
488 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
489 argv[1] = sha1_copy;
490 argv[3] = remotes_arg.buf;
491 setup_revisions(argc, argv, &rev, NULL);
492 if (prepare_revision_walk(&rev))
493 die("revision walk setup failed");
495 while ((commit = get_revision(&rev)) != NULL)
496 find_unpushed_submodule_commits(commit, needs_pushing);
498 reset_revision_walk();
499 free(sha1_copy);
500 strbuf_release(&remotes_arg);
502 return needs_pushing->nr;
505 static int push_submodule(const char *path)
507 if (add_submodule_odb(path))
508 return 1;
510 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
511 struct child_process cp;
512 const char *argv[] = {"push", NULL};
514 memset(&cp, 0, sizeof(cp));
515 cp.argv = argv;
516 cp.env = local_repo_env;
517 cp.git_cmd = 1;
518 cp.no_stdin = 1;
519 cp.dir = path;
520 if (run_command(&cp))
521 return 0;
522 close(cp.out);
525 return 1;
528 int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
530 int i, ret = 1;
531 struct string_list needs_pushing;
533 memset(&needs_pushing, 0, sizeof(struct string_list));
534 needs_pushing.strdup_strings = 1;
536 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
537 return 1;
539 for (i = 0; i < needs_pushing.nr; i++) {
540 const char *path = needs_pushing.items[i].string;
541 fprintf(stderr, "Pushing submodule '%s'\n", path);
542 if (!push_submodule(path)) {
543 fprintf(stderr, "Unable to push submodule '%s'\n", path);
544 ret = 0;
548 string_list_clear(&needs_pushing, 0);
550 return ret;
553 static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
555 int is_present = 0;
556 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
557 /* Even if the submodule is checked out and the commit is
558 * present, make sure it is reachable from a ref. */
559 struct child_process cp;
560 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
561 struct strbuf buf = STRBUF_INIT;
563 argv[3] = sha1_to_hex(sha1);
564 memset(&cp, 0, sizeof(cp));
565 cp.argv = argv;
566 cp.env = local_repo_env;
567 cp.git_cmd = 1;
568 cp.no_stdin = 1;
569 cp.out = -1;
570 cp.dir = path;
571 if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024))
572 is_present = 1;
574 close(cp.out);
575 strbuf_release(&buf);
577 return is_present;
580 static void submodule_collect_changed_cb(struct diff_queue_struct *q,
581 struct diff_options *options,
582 void *data)
584 int i;
585 for (i = 0; i < q->nr; i++) {
586 struct diff_filepair *p = q->queue[i];
587 if (!S_ISGITLINK(p->two->mode))
588 continue;
590 if (S_ISGITLINK(p->one->mode)) {
591 /* NEEDSWORK: We should honor the name configured in
592 * the .gitmodules file of the commit we are examining
593 * here to be able to correctly follow submodules
594 * being moved around. */
595 struct string_list_item *path;
596 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
597 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
598 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
599 } else {
600 /* Submodule is new or was moved here */
601 /* NEEDSWORK: When the .git directories of submodules
602 * live inside the superprojects .git directory some
603 * day we should fetch new submodules directly into
604 * that location too when config or options request
605 * that so they can be checked out from there. */
606 continue;
611 static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
612 int flags, void *data)
614 sha1_array_append(data, sha1);
615 return 0;
618 void check_for_new_submodule_commits(unsigned char new_sha1[20])
620 if (!initialized_fetch_ref_tips) {
621 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
622 initialized_fetch_ref_tips = 1;
625 sha1_array_append(&ref_tips_after_fetch, new_sha1);
628 static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
630 argv_array_push(data, sha1_to_hex(sha1));
633 static void calculate_changed_submodule_paths(void)
635 struct rev_info rev;
636 struct commit *commit;
637 struct argv_array argv = ARGV_ARRAY_INIT;
639 /* No need to check if there are no submodules configured */
640 if (!config_name_for_path.nr)
641 return;
643 init_revisions(&rev, NULL);
644 argv_array_push(&argv, "--"); /* argv[0] program name */
645 sha1_array_for_each_unique(&ref_tips_after_fetch,
646 add_sha1_to_argv, &argv);
647 argv_array_push(&argv, "--not");
648 sha1_array_for_each_unique(&ref_tips_before_fetch,
649 add_sha1_to_argv, &argv);
650 setup_revisions(argv.argc, argv.argv, &rev, NULL);
651 if (prepare_revision_walk(&rev))
652 die("revision walk setup failed");
655 * Collect all submodules (whether checked out or not) for which new
656 * commits have been recorded upstream in "changed_submodule_paths".
658 while ((commit = get_revision(&rev))) {
659 struct commit_list *parent = commit->parents;
660 while (parent) {
661 struct diff_options diff_opts;
662 diff_setup(&diff_opts);
663 DIFF_OPT_SET(&diff_opts, RECURSIVE);
664 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
665 diff_opts.format_callback = submodule_collect_changed_cb;
666 diff_setup_done(&diff_opts);
667 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
668 diffcore_std(&diff_opts);
669 diff_flush(&diff_opts);
670 parent = parent->next;
674 argv_array_clear(&argv);
675 sha1_array_clear(&ref_tips_before_fetch);
676 sha1_array_clear(&ref_tips_after_fetch);
677 initialized_fetch_ref_tips = 0;
680 int fetch_populated_submodules(const struct argv_array *options,
681 const char *prefix, int command_line_option,
682 int quiet)
684 int i, result = 0;
685 struct child_process cp;
686 struct argv_array argv = ARGV_ARRAY_INIT;
687 struct string_list_item *name_for_path;
688 const char *work_tree = get_git_work_tree();
689 if (!work_tree)
690 goto out;
692 if (read_cache() < 0)
693 die("index file corrupt");
695 argv_array_push(&argv, "fetch");
696 for (i = 0; i < options->argc; i++)
697 argv_array_push(&argv, options->argv[i]);
698 argv_array_push(&argv, "--recurse-submodules-default");
699 /* default value, "--submodule-prefix" and its value are added later */
701 memset(&cp, 0, sizeof(cp));
702 cp.env = local_repo_env;
703 cp.git_cmd = 1;
704 cp.no_stdin = 1;
706 calculate_changed_submodule_paths();
708 for (i = 0; i < active_nr; i++) {
709 struct strbuf submodule_path = STRBUF_INIT;
710 struct strbuf submodule_git_dir = STRBUF_INIT;
711 struct strbuf submodule_prefix = STRBUF_INIT;
712 struct cache_entry *ce = active_cache[i];
713 const char *git_dir, *name, *default_argv;
715 if (!S_ISGITLINK(ce->ce_mode))
716 continue;
718 name = ce->name;
719 name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name);
720 if (name_for_path)
721 name = name_for_path->util;
723 default_argv = "yes";
724 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
725 struct string_list_item *fetch_recurse_submodules_option;
726 fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name);
727 if (fetch_recurse_submodules_option) {
728 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF)
729 continue;
730 if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) {
731 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
732 continue;
733 default_argv = "on-demand";
735 } else {
736 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
737 gitmodules_is_unmerged)
738 continue;
739 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
740 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
741 continue;
742 default_argv = "on-demand";
745 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
746 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
747 continue;
748 default_argv = "on-demand";
751 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
752 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
753 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
754 git_dir = read_gitfile(submodule_git_dir.buf);
755 if (!git_dir)
756 git_dir = submodule_git_dir.buf;
757 if (is_directory(git_dir)) {
758 if (!quiet)
759 printf("Fetching submodule %s%s\n", prefix, ce->name);
760 cp.dir = submodule_path.buf;
761 argv_array_push(&argv, default_argv);
762 argv_array_push(&argv, "--submodule-prefix");
763 argv_array_push(&argv, submodule_prefix.buf);
764 cp.argv = argv.argv;
765 if (run_command(&cp))
766 result = 1;
767 argv_array_pop(&argv);
768 argv_array_pop(&argv);
769 argv_array_pop(&argv);
771 strbuf_release(&submodule_path);
772 strbuf_release(&submodule_git_dir);
773 strbuf_release(&submodule_prefix);
775 argv_array_clear(&argv);
776 out:
777 string_list_clear(&changed_submodule_paths, 1);
778 return result;
781 unsigned is_submodule_modified(const char *path, int ignore_untracked)
783 ssize_t len;
784 struct child_process cp;
785 const char *argv[] = {
786 "status",
787 "--porcelain",
788 NULL,
789 NULL,
791 struct strbuf buf = STRBUF_INIT;
792 unsigned dirty_submodule = 0;
793 const char *line, *next_line;
794 const char *git_dir;
796 strbuf_addf(&buf, "%s/.git", path);
797 git_dir = read_gitfile(buf.buf);
798 if (!git_dir)
799 git_dir = buf.buf;
800 if (!is_directory(git_dir)) {
801 strbuf_release(&buf);
802 /* The submodule is not checked out, so it is not modified */
803 return 0;
806 strbuf_reset(&buf);
808 if (ignore_untracked)
809 argv[2] = "-uno";
811 memset(&cp, 0, sizeof(cp));
812 cp.argv = argv;
813 cp.env = local_repo_env;
814 cp.git_cmd = 1;
815 cp.no_stdin = 1;
816 cp.out = -1;
817 cp.dir = path;
818 if (start_command(&cp))
819 die("Could not run 'git status --porcelain' in submodule %s", path);
821 len = strbuf_read(&buf, cp.out, 1024);
822 line = buf.buf;
823 while (len > 2) {
824 if ((line[0] == '?') && (line[1] == '?')) {
825 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
826 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
827 break;
828 } else {
829 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
830 if (ignore_untracked ||
831 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
832 break;
834 next_line = strchr(line, '\n');
835 if (!next_line)
836 break;
837 next_line++;
838 len -= (next_line - line);
839 line = next_line;
841 close(cp.out);
843 if (finish_command(&cp))
844 die("'git status --porcelain' failed in submodule %s", path);
846 strbuf_release(&buf);
847 return dirty_submodule;
850 int submodule_uses_gitfile(const char *path)
852 struct child_process cp;
853 const char *argv[] = {
854 "submodule",
855 "foreach",
856 "--quiet",
857 "--recursive",
858 "test -f .git",
859 NULL,
861 struct strbuf buf = STRBUF_INIT;
862 const char *git_dir;
864 strbuf_addf(&buf, "%s/.git", path);
865 git_dir = read_gitfile(buf.buf);
866 if (!git_dir) {
867 strbuf_release(&buf);
868 return 0;
870 strbuf_release(&buf);
872 /* Now test that all nested submodules use a gitfile too */
873 memset(&cp, 0, sizeof(cp));
874 cp.argv = argv;
875 cp.env = local_repo_env;
876 cp.git_cmd = 1;
877 cp.no_stdin = 1;
878 cp.no_stderr = 1;
879 cp.no_stdout = 1;
880 cp.dir = path;
881 if (run_command(&cp))
882 return 0;
884 return 1;
887 int ok_to_remove_submodule(const char *path)
889 struct stat st;
890 ssize_t len;
891 struct child_process cp;
892 const char *argv[] = {
893 "status",
894 "--porcelain",
895 "-u",
896 "--ignore-submodules=none",
897 NULL,
899 struct strbuf buf = STRBUF_INIT;
900 int ok_to_remove = 1;
902 if ((lstat(path, &st) < 0) || is_empty_dir(path))
903 return 1;
905 if (!submodule_uses_gitfile(path))
906 return 0;
908 memset(&cp, 0, sizeof(cp));
909 cp.argv = argv;
910 cp.env = local_repo_env;
911 cp.git_cmd = 1;
912 cp.no_stdin = 1;
913 cp.out = -1;
914 cp.dir = path;
915 if (start_command(&cp))
916 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
918 len = strbuf_read(&buf, cp.out, 1024);
919 if (len > 2)
920 ok_to_remove = 0;
921 close(cp.out);
923 if (finish_command(&cp))
924 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
926 strbuf_release(&buf);
927 return ok_to_remove;
930 static int find_first_merges(struct object_array *result, const char *path,
931 struct commit *a, struct commit *b)
933 int i, j;
934 struct object_array merges = OBJECT_ARRAY_INIT;
935 struct commit *commit;
936 int contains_another;
938 char merged_revision[42];
939 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
940 "--all", merged_revision, NULL };
941 struct rev_info revs;
942 struct setup_revision_opt rev_opts;
944 memset(result, 0, sizeof(struct object_array));
945 memset(&rev_opts, 0, sizeof(rev_opts));
947 /* get all revisions that merge commit a */
948 snprintf(merged_revision, sizeof(merged_revision), "^%s",
949 sha1_to_hex(a->object.sha1));
950 init_revisions(&revs, NULL);
951 rev_opts.submodule = path;
952 setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts);
954 /* save all revisions from the above list that contain b */
955 if (prepare_revision_walk(&revs))
956 die("revision walk setup failed");
957 while ((commit = get_revision(&revs)) != NULL) {
958 struct object *o = &(commit->object);
959 if (in_merge_bases(b, commit))
960 add_object_array(o, NULL, &merges);
962 reset_revision_walk();
964 /* Now we've got all merges that contain a and b. Prune all
965 * merges that contain another found merge and save them in
966 * result.
968 for (i = 0; i < merges.nr; i++) {
969 struct commit *m1 = (struct commit *) merges.objects[i].item;
971 contains_another = 0;
972 for (j = 0; j < merges.nr; j++) {
973 struct commit *m2 = (struct commit *) merges.objects[j].item;
974 if (i != j && in_merge_bases(m2, m1)) {
975 contains_another = 1;
976 break;
980 if (!contains_another)
981 add_object_array(merges.objects[i].item, NULL, result);
984 free(merges.objects);
985 return result->nr;
988 static void print_commit(struct commit *commit)
990 struct strbuf sb = STRBUF_INIT;
991 struct pretty_print_context ctx = {0};
992 ctx.date_mode = DATE_NORMAL;
993 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
994 fprintf(stderr, "%s\n", sb.buf);
995 strbuf_release(&sb);
998 #define MERGE_WARNING(path, msg) \
999 warning("Failed to merge submodule %s (%s)", path, msg);
1001 int merge_submodule(unsigned char result[20], const char *path,
1002 const unsigned char base[20], const unsigned char a[20],
1003 const unsigned char b[20], int search)
1005 struct commit *commit_base, *commit_a, *commit_b;
1006 int parent_count;
1007 struct object_array merges;
1009 int i;
1011 /* store a in result in case we fail */
1012 hashcpy(result, a);
1014 /* we can not handle deletion conflicts */
1015 if (is_null_sha1(base))
1016 return 0;
1017 if (is_null_sha1(a))
1018 return 0;
1019 if (is_null_sha1(b))
1020 return 0;
1022 if (add_submodule_odb(path)) {
1023 MERGE_WARNING(path, "not checked out");
1024 return 0;
1027 if (!(commit_base = lookup_commit_reference(base)) ||
1028 !(commit_a = lookup_commit_reference(a)) ||
1029 !(commit_b = lookup_commit_reference(b))) {
1030 MERGE_WARNING(path, "commits not present");
1031 return 0;
1034 /* check whether both changes are forward */
1035 if (!in_merge_bases(commit_base, commit_a) ||
1036 !in_merge_bases(commit_base, commit_b)) {
1037 MERGE_WARNING(path, "commits don't follow merge-base");
1038 return 0;
1041 /* Case #1: a is contained in b or vice versa */
1042 if (in_merge_bases(commit_a, commit_b)) {
1043 hashcpy(result, b);
1044 return 1;
1046 if (in_merge_bases(commit_b, commit_a)) {
1047 hashcpy(result, a);
1048 return 1;
1052 * Case #2: There are one or more merges that contain a and b in
1053 * the submodule. If there is only one, then present it as a
1054 * suggestion to the user, but leave it marked unmerged so the
1055 * user needs to confirm the resolution.
1058 /* Skip the search if makes no sense to the calling context. */
1059 if (!search)
1060 return 0;
1062 /* find commit which merges them */
1063 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1064 switch (parent_count) {
1065 case 0:
1066 MERGE_WARNING(path, "merge following commits not found");
1067 break;
1069 case 1:
1070 MERGE_WARNING(path, "not fast-forward");
1071 fprintf(stderr, "Found a possible merge resolution "
1072 "for the submodule:\n");
1073 print_commit((struct commit *) merges.objects[0].item);
1074 fprintf(stderr,
1075 "If this is correct simply add it to the index "
1076 "for example\n"
1077 "by using:\n\n"
1078 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1079 "which will accept this suggestion.\n",
1080 sha1_to_hex(merges.objects[0].item->sha1), path);
1081 break;
1083 default:
1084 MERGE_WARNING(path, "multiple merges found");
1085 for (i = 0; i < merges.nr; i++)
1086 print_commit((struct commit *) merges.objects[i].item);
1089 free(merges.objects);
1090 return 0;
1093 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1094 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1096 struct strbuf file_name = STRBUF_INIT;
1097 struct strbuf rel_path = STRBUF_INIT;
1098 const char *real_work_tree = xstrdup(real_path(work_tree));
1099 FILE *fp;
1101 /* Update gitfile */
1102 strbuf_addf(&file_name, "%s/.git", work_tree);
1103 fp = fopen(file_name.buf, "w");
1104 if (!fp)
1105 die(_("Could not create git link %s"), file_name.buf);
1106 fprintf(fp, "gitdir: %s\n", relative_path(git_dir, real_work_tree,
1107 &rel_path));
1108 fclose(fp);
1110 /* Update core.worktree setting */
1111 strbuf_reset(&file_name);
1112 strbuf_addf(&file_name, "%s/config", git_dir);
1113 if (git_config_set_in_file(file_name.buf, "core.worktree",
1114 relative_path(real_work_tree, git_dir,
1115 &rel_path)))
1116 die(_("Could not set core.worktree in %s"),
1117 file_name.buf);
1119 strbuf_release(&file_name);
1120 strbuf_release(&rel_path);
1121 free((void *)real_work_tree);