97355eb7ad82ac16c2b6317409e57615f75c5740
1#include "cache.h"
2#include "submodule-config.h"
3#include "submodule.h"
4#include "dir.h"
5#include "diff.h"
6#include "commit.h"
7#include "revision.h"
8#include "run-command.h"
9#include "diffcore.h"
10#include "refs.h"
11#include "string-list.h"
12#include "sha1-array.h"
13#include "argv-array.h"
14#include "blob.h"
15
16static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
17static struct string_list changed_submodule_paths;
18static int initialized_fetch_ref_tips;
19static struct sha1_array ref_tips_before_fetch;
20static struct sha1_array ref_tips_after_fetch;
21
22/*
23 * The following flag is set if the .gitmodules file is unmerged. We then
24 * disable recursion for all submodules where .git/config doesn't have a
25 * matching config entry because we can't guess what might be configured in
26 * .gitmodules unless the user resolves the conflict. When a command line
27 * option is given (which always overrides configuration) this flag will be
28 * ignored.
29 */
30static int gitmodules_is_unmerged;
31
32/*
33 * This flag is set if the .gitmodules file had unstaged modifications on
34 * startup. This must be checked before allowing modifications to the
35 * .gitmodules file with the intention to stage them later, because when
36 * continuing we would stage the modifications the user didn't stage herself
37 * too. That might change in a future version when we learn to stage the
38 * changes we do ourselves without staging any previous modifications.
39 */
40static int gitmodules_is_modified;
41
42int is_staging_gitmodules_ok(void)
43{
44 return !gitmodules_is_modified;
45}
46
47/*
48 * Try to update the "path" entry in the "submodule.<name>" section of the
49 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
50 * with the correct path=<oldpath> setting was found and we could update it.
51 */
52int update_path_in_gitmodules(const char *oldpath, const char *newpath)
53{
54 struct strbuf entry = STRBUF_INIT;
55 const struct submodule *submodule;
56
57 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
58 return -1;
59
60 if (gitmodules_is_unmerged)
61 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
62
63 submodule = submodule_from_path(null_sha1, oldpath);
64 if (!submodule || !submodule->name) {
65 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
66 return -1;
67 }
68 strbuf_addstr(&entry, "submodule.");
69 strbuf_addstr(&entry, submodule->name);
70 strbuf_addstr(&entry, ".path");
71 if (git_config_set_in_file(".gitmodules", entry.buf, newpath) < 0) {
72 /* Maybe the user already did that, don't error out here */
73 warning(_("Could not update .gitmodules entry %s"), entry.buf);
74 strbuf_release(&entry);
75 return -1;
76 }
77 strbuf_release(&entry);
78 return 0;
79}
80
81/*
82 * Try to remove the "submodule.<name>" section from .gitmodules where the given
83 * path is configured. Return 0 only if a .gitmodules file was found, a section
84 * with the correct path=<path> setting was found and we could remove it.
85 */
86int remove_path_from_gitmodules(const char *path)
87{
88 struct strbuf sect = STRBUF_INIT;
89 const struct submodule *submodule;
90
91 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
92 return -1;
93
94 if (gitmodules_is_unmerged)
95 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
96
97 submodule = submodule_from_path(null_sha1, path);
98 if (!submodule || !submodule->name) {
99 warning(_("Could not find section in .gitmodules where path=%s"), path);
100 return -1;
101 }
102 strbuf_addstr(§, "submodule.");
103 strbuf_addstr(§, submodule->name);
104 if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) {
105 /* Maybe the user already did that, don't error out here */
106 warning(_("Could not remove .gitmodules entry for %s"), path);
107 strbuf_release(§);
108 return -1;
109 }
110 strbuf_release(§);
111 return 0;
112}
113
114void stage_updated_gitmodules(void)
115{
116 if (add_file_to_cache(".gitmodules", 0))
117 die(_("staging updated .gitmodules failed"));
118}
119
120static int add_submodule_odb(const char *path)
121{
122 struct strbuf objects_directory = STRBUF_INIT;
123 struct alternate_object_database *alt_odb;
124 int ret = 0;
125 const char *git_dir;
126
127 strbuf_addf(&objects_directory, "%s/.git", path);
128 git_dir = read_gitfile(objects_directory.buf);
129 if (git_dir) {
130 strbuf_reset(&objects_directory);
131 strbuf_addstr(&objects_directory, git_dir);
132 }
133 strbuf_addstr(&objects_directory, "/objects/");
134 if (!is_directory(objects_directory.buf)) {
135 ret = -1;
136 goto done;
137 }
138 /* avoid adding it twice */
139 for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next)
140 if (alt_odb->name - alt_odb->base == objects_directory.len &&
141 !strncmp(alt_odb->base, objects_directory.buf,
142 objects_directory.len))
143 goto done;
144
145 alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb));
146 alt_odb->next = alt_odb_list;
147 strcpy(alt_odb->base, objects_directory.buf);
148 alt_odb->name = alt_odb->base + objects_directory.len;
149 alt_odb->name[2] = '/';
150 alt_odb->name[40] = '\0';
151 alt_odb->name[41] = '\0';
152 alt_odb_list = alt_odb;
153
154 /* add possible alternates from the submodule */
155 read_info_alternates(objects_directory.buf, 0);
156 prepare_alt_odb();
157done:
158 strbuf_release(&objects_directory);
159 return ret;
160}
161
162void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
163 const char *path)
164{
165 const struct submodule *submodule = submodule_from_path(null_sha1, path);
166 if (submodule) {
167 if (submodule->ignore)
168 handle_ignore_submodules_arg(diffopt, submodule->ignore);
169 else if (gitmodules_is_unmerged)
170 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
171 }
172}
173
174int submodule_config(const char *var, const char *value, void *cb)
175{
176 if (starts_with(var, "submodule."))
177 return parse_submodule_config_option(var, value);
178 else if (!strcmp(var, "fetch.recursesubmodules")) {
179 config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
180 return 0;
181 }
182 return 0;
183}
184
185void gitmodules_config(void)
186{
187 const char *work_tree = get_git_work_tree();
188 if (work_tree) {
189 struct strbuf gitmodules_path = STRBUF_INIT;
190 int pos;
191 strbuf_addstr(&gitmodules_path, work_tree);
192 strbuf_addstr(&gitmodules_path, "/.gitmodules");
193 if (read_cache() < 0)
194 die("index file corrupt");
195 pos = cache_name_pos(".gitmodules", 11);
196 if (pos < 0) { /* .gitmodules not found or isn't merged */
197 pos = -1 - pos;
198 if (active_nr > pos) { /* there is a .gitmodules */
199 const struct cache_entry *ce = active_cache[pos];
200 if (ce_namelen(ce) == 11 &&
201 !memcmp(ce->name, ".gitmodules", 11))
202 gitmodules_is_unmerged = 1;
203 }
204 } else if (pos < active_nr) {
205 struct stat st;
206 if (lstat(".gitmodules", &st) == 0 &&
207 ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED)
208 gitmodules_is_modified = 1;
209 }
210
211 if (!gitmodules_is_unmerged)
212 git_config_from_file(submodule_config, gitmodules_path.buf, NULL);
213 strbuf_release(&gitmodules_path);
214 }
215}
216
217void handle_ignore_submodules_arg(struct diff_options *diffopt,
218 const char *arg)
219{
220 DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES);
221 DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
222 DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES);
223
224 if (!strcmp(arg, "all"))
225 DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES);
226 else if (!strcmp(arg, "untracked"))
227 DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES);
228 else if (!strcmp(arg, "dirty"))
229 DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES);
230 else if (strcmp(arg, "none"))
231 die("bad --ignore-submodules argument: %s", arg);
232}
233
234static int prepare_submodule_summary(struct rev_info *rev, const char *path,
235 struct commit *left, struct commit *right,
236 int *fast_forward, int *fast_backward)
237{
238 struct commit_list *merge_bases, *list;
239
240 init_revisions(rev, NULL);
241 setup_revisions(0, NULL, rev, NULL);
242 rev->left_right = 1;
243 rev->first_parent_only = 1;
244 left->object.flags |= SYMMETRIC_LEFT;
245 add_pending_object(rev, &left->object, path);
246 add_pending_object(rev, &right->object, path);
247 merge_bases = get_merge_bases(left, right);
248 if (merge_bases) {
249 if (merge_bases->item == left)
250 *fast_forward = 1;
251 else if (merge_bases->item == right)
252 *fast_backward = 1;
253 }
254 for (list = merge_bases; list; list = list->next) {
255 list->item->object.flags |= UNINTERESTING;
256 add_pending_object(rev, &list->item->object,
257 sha1_to_hex(list->item->object.sha1));
258 }
259 return prepare_revision_walk(rev);
260}
261
262static void print_submodule_summary(struct rev_info *rev, FILE *f,
263 const char *line_prefix,
264 const char *del, const char *add, const char *reset)
265{
266 static const char format[] = " %m %s";
267 struct strbuf sb = STRBUF_INIT;
268 struct commit *commit;
269
270 while ((commit = get_revision(rev))) {
271 struct pretty_print_context ctx = {0};
272 ctx.date_mode = rev->date_mode;
273 ctx.output_encoding = get_log_output_encoding();
274 strbuf_setlen(&sb, 0);
275 strbuf_addstr(&sb, line_prefix);
276 if (commit->object.flags & SYMMETRIC_LEFT) {
277 if (del)
278 strbuf_addstr(&sb, del);
279 }
280 else if (add)
281 strbuf_addstr(&sb, add);
282 format_commit_message(commit, format, &sb, &ctx);
283 if (reset)
284 strbuf_addstr(&sb, reset);
285 strbuf_addch(&sb, '\n');
286 fprintf(f, "%s", sb.buf);
287 }
288 strbuf_release(&sb);
289}
290
291int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg)
292{
293 switch (git_config_maybe_bool(opt, arg)) {
294 case 1:
295 return RECURSE_SUBMODULES_ON;
296 case 0:
297 return RECURSE_SUBMODULES_OFF;
298 default:
299 if (!strcmp(arg, "on-demand"))
300 return RECURSE_SUBMODULES_ON_DEMAND;
301 /* TODO: remove the die for history parsing here */
302 die("bad %s argument: %s", opt, arg);
303 }
304}
305
306void show_submodule_summary(FILE *f, const char *path,
307 const char *line_prefix,
308 unsigned char one[20], unsigned char two[20],
309 unsigned dirty_submodule, const char *meta,
310 const char *del, const char *add, const char *reset)
311{
312 struct rev_info rev;
313 struct commit *left = NULL, *right = NULL;
314 const char *message = NULL;
315 struct strbuf sb = STRBUF_INIT;
316 int fast_forward = 0, fast_backward = 0;
317
318 if (is_null_sha1(two))
319 message = "(submodule deleted)";
320 else if (add_submodule_odb(path))
321 message = "(not checked out)";
322 else if (is_null_sha1(one))
323 message = "(new submodule)";
324 else if (!(left = lookup_commit_reference(one)) ||
325 !(right = lookup_commit_reference(two)))
326 message = "(commits not present)";
327 else if (prepare_submodule_summary(&rev, path, left, right,
328 &fast_forward, &fast_backward))
329 message = "(revision walker failed)";
330
331 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
332 fprintf(f, "%sSubmodule %s contains untracked content\n",
333 line_prefix, path);
334 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
335 fprintf(f, "%sSubmodule %s contains modified content\n",
336 line_prefix, path);
337
338 if (!hashcmp(one, two)) {
339 strbuf_release(&sb);
340 return;
341 }
342
343 strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path,
344 find_unique_abbrev(one, DEFAULT_ABBREV));
345 if (!fast_backward && !fast_forward)
346 strbuf_addch(&sb, '.');
347 strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV));
348 if (message)
349 strbuf_addf(&sb, " %s%s\n", message, reset);
350 else
351 strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset);
352 fwrite(sb.buf, sb.len, 1, f);
353
354 if (!message) /* only NULL if we succeeded in setting up the walk */
355 print_submodule_summary(&rev, f, line_prefix, del, add, reset);
356 if (left)
357 clear_commit_marks(left, ~0);
358 if (right)
359 clear_commit_marks(right, ~0);
360
361 strbuf_release(&sb);
362}
363
364void set_config_fetch_recurse_submodules(int value)
365{
366 config_fetch_recurse_submodules = value;
367}
368
369static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
370{
371 return 1;
372}
373
374static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
375{
376 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
377 return 0;
378
379 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
380 struct child_process cp = CHILD_PROCESS_INIT;
381 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
382 struct strbuf buf = STRBUF_INIT;
383 int needs_pushing = 0;
384
385 argv[1] = sha1_to_hex(sha1);
386 cp.argv = argv;
387 cp.env = local_repo_env;
388 cp.git_cmd = 1;
389 cp.no_stdin = 1;
390 cp.out = -1;
391 cp.dir = path;
392 if (start_command(&cp))
393 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
394 sha1_to_hex(sha1), path);
395 if (strbuf_read(&buf, cp.out, 41))
396 needs_pushing = 1;
397 finish_command(&cp);
398 close(cp.out);
399 strbuf_release(&buf);
400 return needs_pushing;
401 }
402
403 return 0;
404}
405
406static void collect_submodules_from_diff(struct diff_queue_struct *q,
407 struct diff_options *options,
408 void *data)
409{
410 int i;
411 struct string_list *needs_pushing = data;
412
413 for (i = 0; i < q->nr; i++) {
414 struct diff_filepair *p = q->queue[i];
415 if (!S_ISGITLINK(p->two->mode))
416 continue;
417 if (submodule_needs_pushing(p->two->path, p->two->sha1))
418 string_list_insert(needs_pushing, p->two->path);
419 }
420}
421
422static void find_unpushed_submodule_commits(struct commit *commit,
423 struct string_list *needs_pushing)
424{
425 struct rev_info rev;
426
427 init_revisions(&rev, NULL);
428 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
429 rev.diffopt.format_callback = collect_submodules_from_diff;
430 rev.diffopt.format_callback_data = needs_pushing;
431 diff_tree_combined_merge(commit, 1, &rev);
432}
433
434int find_unpushed_submodules(unsigned char new_sha1[20],
435 const char *remotes_name, struct string_list *needs_pushing)
436{
437 struct rev_info rev;
438 struct commit *commit;
439 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
440 int argc = ARRAY_SIZE(argv) - 1;
441 char *sha1_copy;
442
443 struct strbuf remotes_arg = STRBUF_INIT;
444
445 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
446 init_revisions(&rev, NULL);
447 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
448 argv[1] = sha1_copy;
449 argv[3] = remotes_arg.buf;
450 setup_revisions(argc, argv, &rev, NULL);
451 if (prepare_revision_walk(&rev))
452 die("revision walk setup failed");
453
454 while ((commit = get_revision(&rev)) != NULL)
455 find_unpushed_submodule_commits(commit, needs_pushing);
456
457 reset_revision_walk();
458 free(sha1_copy);
459 strbuf_release(&remotes_arg);
460
461 return needs_pushing->nr;
462}
463
464static int push_submodule(const char *path)
465{
466 if (add_submodule_odb(path))
467 return 1;
468
469 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
470 struct child_process cp = CHILD_PROCESS_INIT;
471 const char *argv[] = {"push", NULL};
472
473 cp.argv = argv;
474 cp.env = local_repo_env;
475 cp.git_cmd = 1;
476 cp.no_stdin = 1;
477 cp.dir = path;
478 if (run_command(&cp))
479 return 0;
480 close(cp.out);
481 }
482
483 return 1;
484}
485
486int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
487{
488 int i, ret = 1;
489 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
490
491 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
492 return 1;
493
494 for (i = 0; i < needs_pushing.nr; i++) {
495 const char *path = needs_pushing.items[i].string;
496 fprintf(stderr, "Pushing submodule '%s'\n", path);
497 if (!push_submodule(path)) {
498 fprintf(stderr, "Unable to push submodule '%s'\n", path);
499 ret = 0;
500 }
501 }
502
503 string_list_clear(&needs_pushing, 0);
504
505 return ret;
506}
507
508static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
509{
510 int is_present = 0;
511 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
512 /* Even if the submodule is checked out and the commit is
513 * present, make sure it is reachable from a ref. */
514 struct child_process cp = CHILD_PROCESS_INIT;
515 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
516 struct strbuf buf = STRBUF_INIT;
517
518 argv[3] = sha1_to_hex(sha1);
519 cp.argv = argv;
520 cp.env = local_repo_env;
521 cp.git_cmd = 1;
522 cp.no_stdin = 1;
523 cp.dir = path;
524 if (!capture_command(&cp, &buf, 1024) && !buf.len)
525 is_present = 1;
526
527 strbuf_release(&buf);
528 }
529 return is_present;
530}
531
532static void submodule_collect_changed_cb(struct diff_queue_struct *q,
533 struct diff_options *options,
534 void *data)
535{
536 int i;
537 for (i = 0; i < q->nr; i++) {
538 struct diff_filepair *p = q->queue[i];
539 if (!S_ISGITLINK(p->two->mode))
540 continue;
541
542 if (S_ISGITLINK(p->one->mode)) {
543 /* NEEDSWORK: We should honor the name configured in
544 * the .gitmodules file of the commit we are examining
545 * here to be able to correctly follow submodules
546 * being moved around. */
547 struct string_list_item *path;
548 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
549 if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1))
550 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
551 } else {
552 /* Submodule is new or was moved here */
553 /* NEEDSWORK: When the .git directories of submodules
554 * live inside the superprojects .git directory some
555 * day we should fetch new submodules directly into
556 * that location too when config or options request
557 * that so they can be checked out from there. */
558 continue;
559 }
560 }
561}
562
563static int add_sha1_to_array(const char *ref, const unsigned char *sha1,
564 int flags, void *data)
565{
566 sha1_array_append(data, sha1);
567 return 0;
568}
569
570void check_for_new_submodule_commits(unsigned char new_sha1[20])
571{
572 if (!initialized_fetch_ref_tips) {
573 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
574 initialized_fetch_ref_tips = 1;
575 }
576
577 sha1_array_append(&ref_tips_after_fetch, new_sha1);
578}
579
580static void add_sha1_to_argv(const unsigned char sha1[20], void *data)
581{
582 argv_array_push(data, sha1_to_hex(sha1));
583}
584
585static void calculate_changed_submodule_paths(void)
586{
587 struct rev_info rev;
588 struct commit *commit;
589 struct argv_array argv = ARGV_ARRAY_INIT;
590
591 /* No need to check if there are no submodules configured */
592 if (!submodule_from_path(NULL, NULL))
593 return;
594
595 init_revisions(&rev, NULL);
596 argv_array_push(&argv, "--"); /* argv[0] program name */
597 sha1_array_for_each_unique(&ref_tips_after_fetch,
598 add_sha1_to_argv, &argv);
599 argv_array_push(&argv, "--not");
600 sha1_array_for_each_unique(&ref_tips_before_fetch,
601 add_sha1_to_argv, &argv);
602 setup_revisions(argv.argc, argv.argv, &rev, NULL);
603 if (prepare_revision_walk(&rev))
604 die("revision walk setup failed");
605
606 /*
607 * Collect all submodules (whether checked out or not) for which new
608 * commits have been recorded upstream in "changed_submodule_paths".
609 */
610 while ((commit = get_revision(&rev))) {
611 struct commit_list *parent = commit->parents;
612 while (parent) {
613 struct diff_options diff_opts;
614 diff_setup(&diff_opts);
615 DIFF_OPT_SET(&diff_opts, RECURSIVE);
616 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
617 diff_opts.format_callback = submodule_collect_changed_cb;
618 diff_setup_done(&diff_opts);
619 diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts);
620 diffcore_std(&diff_opts);
621 diff_flush(&diff_opts);
622 parent = parent->next;
623 }
624 }
625
626 argv_array_clear(&argv);
627 sha1_array_clear(&ref_tips_before_fetch);
628 sha1_array_clear(&ref_tips_after_fetch);
629 initialized_fetch_ref_tips = 0;
630}
631
632int fetch_populated_submodules(const struct argv_array *options,
633 const char *prefix, int command_line_option,
634 int quiet)
635{
636 int i, result = 0;
637 struct child_process cp = CHILD_PROCESS_INIT;
638 struct argv_array argv = ARGV_ARRAY_INIT;
639 const char *work_tree = get_git_work_tree();
640 if (!work_tree)
641 goto out;
642
643 if (read_cache() < 0)
644 die("index file corrupt");
645
646 argv_array_push(&argv, "fetch");
647 for (i = 0; i < options->argc; i++)
648 argv_array_push(&argv, options->argv[i]);
649 argv_array_push(&argv, "--recurse-submodules-default");
650 /* default value, "--submodule-prefix" and its value are added later */
651
652 cp.env = local_repo_env;
653 cp.git_cmd = 1;
654 cp.no_stdin = 1;
655
656 calculate_changed_submodule_paths();
657
658 for (i = 0; i < active_nr; i++) {
659 struct strbuf submodule_path = STRBUF_INIT;
660 struct strbuf submodule_git_dir = STRBUF_INIT;
661 struct strbuf submodule_prefix = STRBUF_INIT;
662 const struct cache_entry *ce = active_cache[i];
663 const char *git_dir, *default_argv;
664 const struct submodule *submodule;
665
666 if (!S_ISGITLINK(ce->ce_mode))
667 continue;
668
669 submodule = submodule_from_path(null_sha1, ce->name);
670 if (!submodule)
671 submodule = submodule_from_name(null_sha1, ce->name);
672
673 default_argv = "yes";
674 if (command_line_option == RECURSE_SUBMODULES_DEFAULT) {
675 if (submodule &&
676 submodule->fetch_recurse !=
677 RECURSE_SUBMODULES_NONE) {
678 if (submodule->fetch_recurse ==
679 RECURSE_SUBMODULES_OFF)
680 continue;
681 if (submodule->fetch_recurse ==
682 RECURSE_SUBMODULES_ON_DEMAND) {
683 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
684 continue;
685 default_argv = "on-demand";
686 }
687 } else {
688 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
689 gitmodules_is_unmerged)
690 continue;
691 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
692 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
693 continue;
694 default_argv = "on-demand";
695 }
696 }
697 } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
698 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
699 continue;
700 default_argv = "on-demand";
701 }
702
703 strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name);
704 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
705 strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name);
706 git_dir = read_gitfile(submodule_git_dir.buf);
707 if (!git_dir)
708 git_dir = submodule_git_dir.buf;
709 if (is_directory(git_dir)) {
710 if (!quiet)
711 printf("Fetching submodule %s%s\n", prefix, ce->name);
712 cp.dir = submodule_path.buf;
713 argv_array_push(&argv, default_argv);
714 argv_array_push(&argv, "--submodule-prefix");
715 argv_array_push(&argv, submodule_prefix.buf);
716 cp.argv = argv.argv;
717 if (run_command(&cp))
718 result = 1;
719 argv_array_pop(&argv);
720 argv_array_pop(&argv);
721 argv_array_pop(&argv);
722 }
723 strbuf_release(&submodule_path);
724 strbuf_release(&submodule_git_dir);
725 strbuf_release(&submodule_prefix);
726 }
727 argv_array_clear(&argv);
728out:
729 string_list_clear(&changed_submodule_paths, 1);
730 return result;
731}
732
733unsigned is_submodule_modified(const char *path, int ignore_untracked)
734{
735 ssize_t len;
736 struct child_process cp = CHILD_PROCESS_INIT;
737 const char *argv[] = {
738 "status",
739 "--porcelain",
740 NULL,
741 NULL,
742 };
743 struct strbuf buf = STRBUF_INIT;
744 unsigned dirty_submodule = 0;
745 const char *line, *next_line;
746 const char *git_dir;
747
748 strbuf_addf(&buf, "%s/.git", path);
749 git_dir = read_gitfile(buf.buf);
750 if (!git_dir)
751 git_dir = buf.buf;
752 if (!is_directory(git_dir)) {
753 strbuf_release(&buf);
754 /* The submodule is not checked out, so it is not modified */
755 return 0;
756
757 }
758 strbuf_reset(&buf);
759
760 if (ignore_untracked)
761 argv[2] = "-uno";
762
763 cp.argv = argv;
764 cp.env = local_repo_env;
765 cp.git_cmd = 1;
766 cp.no_stdin = 1;
767 cp.out = -1;
768 cp.dir = path;
769 if (start_command(&cp))
770 die("Could not run 'git status --porcelain' in submodule %s", path);
771
772 len = strbuf_read(&buf, cp.out, 1024);
773 line = buf.buf;
774 while (len > 2) {
775 if ((line[0] == '?') && (line[1] == '?')) {
776 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
777 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
778 break;
779 } else {
780 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
781 if (ignore_untracked ||
782 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
783 break;
784 }
785 next_line = strchr(line, '\n');
786 if (!next_line)
787 break;
788 next_line++;
789 len -= (next_line - line);
790 line = next_line;
791 }
792 close(cp.out);
793
794 if (finish_command(&cp))
795 die("'git status --porcelain' failed in submodule %s", path);
796
797 strbuf_release(&buf);
798 return dirty_submodule;
799}
800
801int submodule_uses_gitfile(const char *path)
802{
803 struct child_process cp = CHILD_PROCESS_INIT;
804 const char *argv[] = {
805 "submodule",
806 "foreach",
807 "--quiet",
808 "--recursive",
809 "test -f .git",
810 NULL,
811 };
812 struct strbuf buf = STRBUF_INIT;
813 const char *git_dir;
814
815 strbuf_addf(&buf, "%s/.git", path);
816 git_dir = read_gitfile(buf.buf);
817 if (!git_dir) {
818 strbuf_release(&buf);
819 return 0;
820 }
821 strbuf_release(&buf);
822
823 /* Now test that all nested submodules use a gitfile too */
824 cp.argv = argv;
825 cp.env = local_repo_env;
826 cp.git_cmd = 1;
827 cp.no_stdin = 1;
828 cp.no_stderr = 1;
829 cp.no_stdout = 1;
830 cp.dir = path;
831 if (run_command(&cp))
832 return 0;
833
834 return 1;
835}
836
837int ok_to_remove_submodule(const char *path)
838{
839 ssize_t len;
840 struct child_process cp = CHILD_PROCESS_INIT;
841 const char *argv[] = {
842 "status",
843 "--porcelain",
844 "-u",
845 "--ignore-submodules=none",
846 NULL,
847 };
848 struct strbuf buf = STRBUF_INIT;
849 int ok_to_remove = 1;
850
851 if (!file_exists(path) || is_empty_dir(path))
852 return 1;
853
854 if (!submodule_uses_gitfile(path))
855 return 0;
856
857 cp.argv = argv;
858 cp.env = local_repo_env;
859 cp.git_cmd = 1;
860 cp.no_stdin = 1;
861 cp.out = -1;
862 cp.dir = path;
863 if (start_command(&cp))
864 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
865
866 len = strbuf_read(&buf, cp.out, 1024);
867 if (len > 2)
868 ok_to_remove = 0;
869 close(cp.out);
870
871 if (finish_command(&cp))
872 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
873
874 strbuf_release(&buf);
875 return ok_to_remove;
876}
877
878static int find_first_merges(struct object_array *result, const char *path,
879 struct commit *a, struct commit *b)
880{
881 int i, j;
882 struct object_array merges = OBJECT_ARRAY_INIT;
883 struct commit *commit;
884 int contains_another;
885
886 char merged_revision[42];
887 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
888 "--all", merged_revision, NULL };
889 struct rev_info revs;
890 struct setup_revision_opt rev_opts;
891
892 memset(result, 0, sizeof(struct object_array));
893 memset(&rev_opts, 0, sizeof(rev_opts));
894
895 /* get all revisions that merge commit a */
896 snprintf(merged_revision, sizeof(merged_revision), "^%s",
897 sha1_to_hex(a->object.sha1));
898 init_revisions(&revs, NULL);
899 rev_opts.submodule = path;
900 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
901
902 /* save all revisions from the above list that contain b */
903 if (prepare_revision_walk(&revs))
904 die("revision walk setup failed");
905 while ((commit = get_revision(&revs)) != NULL) {
906 struct object *o = &(commit->object);
907 if (in_merge_bases(b, commit))
908 add_object_array(o, NULL, &merges);
909 }
910 reset_revision_walk();
911
912 /* Now we've got all merges that contain a and b. Prune all
913 * merges that contain another found merge and save them in
914 * result.
915 */
916 for (i = 0; i < merges.nr; i++) {
917 struct commit *m1 = (struct commit *) merges.objects[i].item;
918
919 contains_another = 0;
920 for (j = 0; j < merges.nr; j++) {
921 struct commit *m2 = (struct commit *) merges.objects[j].item;
922 if (i != j && in_merge_bases(m2, m1)) {
923 contains_another = 1;
924 break;
925 }
926 }
927
928 if (!contains_another)
929 add_object_array(merges.objects[i].item, NULL, result);
930 }
931
932 free(merges.objects);
933 return result->nr;
934}
935
936static void print_commit(struct commit *commit)
937{
938 struct strbuf sb = STRBUF_INIT;
939 struct pretty_print_context ctx = {0};
940 ctx.date_mode = DATE_NORMAL;
941 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
942 fprintf(stderr, "%s\n", sb.buf);
943 strbuf_release(&sb);
944}
945
946#define MERGE_WARNING(path, msg) \
947 warning("Failed to merge submodule %s (%s)", path, msg);
948
949int merge_submodule(unsigned char result[20], const char *path,
950 const unsigned char base[20], const unsigned char a[20],
951 const unsigned char b[20], int search)
952{
953 struct commit *commit_base, *commit_a, *commit_b;
954 int parent_count;
955 struct object_array merges;
956
957 int i;
958
959 /* store a in result in case we fail */
960 hashcpy(result, a);
961
962 /* we can not handle deletion conflicts */
963 if (is_null_sha1(base))
964 return 0;
965 if (is_null_sha1(a))
966 return 0;
967 if (is_null_sha1(b))
968 return 0;
969
970 if (add_submodule_odb(path)) {
971 MERGE_WARNING(path, "not checked out");
972 return 0;
973 }
974
975 if (!(commit_base = lookup_commit_reference(base)) ||
976 !(commit_a = lookup_commit_reference(a)) ||
977 !(commit_b = lookup_commit_reference(b))) {
978 MERGE_WARNING(path, "commits not present");
979 return 0;
980 }
981
982 /* check whether both changes are forward */
983 if (!in_merge_bases(commit_base, commit_a) ||
984 !in_merge_bases(commit_base, commit_b)) {
985 MERGE_WARNING(path, "commits don't follow merge-base");
986 return 0;
987 }
988
989 /* Case #1: a is contained in b or vice versa */
990 if (in_merge_bases(commit_a, commit_b)) {
991 hashcpy(result, b);
992 return 1;
993 }
994 if (in_merge_bases(commit_b, commit_a)) {
995 hashcpy(result, a);
996 return 1;
997 }
998
999 /*
1000 * Case #2: There are one or more merges that contain a and b in
1001 * the submodule. If there is only one, then present it as a
1002 * suggestion to the user, but leave it marked unmerged so the
1003 * user needs to confirm the resolution.
1004 */
1005
1006 /* Skip the search if makes no sense to the calling context. */
1007 if (!search)
1008 return 0;
1009
1010 /* find commit which merges them */
1011 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1012 switch (parent_count) {
1013 case 0:
1014 MERGE_WARNING(path, "merge following commits not found");
1015 break;
1016
1017 case 1:
1018 MERGE_WARNING(path, "not fast-forward");
1019 fprintf(stderr, "Found a possible merge resolution "
1020 "for the submodule:\n");
1021 print_commit((struct commit *) merges.objects[0].item);
1022 fprintf(stderr,
1023 "If this is correct simply add it to the index "
1024 "for example\n"
1025 "by using:\n\n"
1026 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1027 "which will accept this suggestion.\n",
1028 sha1_to_hex(merges.objects[0].item->sha1), path);
1029 break;
1030
1031 default:
1032 MERGE_WARNING(path, "multiple merges found");
1033 for (i = 0; i < merges.nr; i++)
1034 print_commit((struct commit *) merges.objects[i].item);
1035 }
1036
1037 free(merges.objects);
1038 return 0;
1039}
1040
1041/* Update gitfile and core.worktree setting to connect work tree and git dir */
1042void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1043{
1044 struct strbuf file_name = STRBUF_INIT;
1045 struct strbuf rel_path = STRBUF_INIT;
1046 const char *real_work_tree = xstrdup(real_path(work_tree));
1047
1048 /* Update gitfile */
1049 strbuf_addf(&file_name, "%s/.git", work_tree);
1050 write_file(file_name.buf, 1, "gitdir: %s\n",
1051 relative_path(git_dir, real_work_tree, &rel_path));
1052
1053 /* Update core.worktree setting */
1054 strbuf_reset(&file_name);
1055 strbuf_addf(&file_name, "%s/config", git_dir);
1056 if (git_config_set_in_file(file_name.buf, "core.worktree",
1057 relative_path(real_work_tree, git_dir,
1058 &rel_path)))
1059 die(_("Could not set core.worktree in %s"),
1060 file_name.buf);
1061
1062 strbuf_release(&file_name);
1063 strbuf_release(&rel_path);
1064 free((void *)real_work_tree);
1065}