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