submodule: used correct index in is_staging_gitmodules_ok
[git/git.git] / submodule.c
1 #include "cache.h"
2 #include "repository.h"
3 #include "config.h"
4 #include "submodule-config.h"
5 #include "submodule.h"
6 #include "dir.h"
7 #include "diff.h"
8 #include "commit.h"
9 #include "revision.h"
10 #include "run-command.h"
11 #include "diffcore.h"
12 #include "refs.h"
13 #include "string-list.h"
14 #include "sha1-array.h"
15 #include "argv-array.h"
16 #include "blob.h"
17 #include "thread-utils.h"
18 #include "quote.h"
19 #include "remote.h"
20 #include "worktree.h"
21 #include "parse-options.h"
22
23 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
24 static struct string_list changed_submodule_names = STRING_LIST_INIT_DUP;
25 static int initialized_fetch_ref_tips;
26 static struct oid_array ref_tips_before_fetch;
27 static struct oid_array ref_tips_after_fetch;
28
29 /*
30 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
31 * will be disabled because we can't guess what might be configured in
32 * .gitmodules unless the user resolves the conflict.
33 */
34 int is_gitmodules_unmerged(const struct index_state *istate)
35 {
36 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
37 if (pos < 0) { /* .gitmodules not found or isn't merged */
38 pos = -1 - pos;
39 if (istate->cache_nr > pos) { /* there is a .gitmodules */
40 const struct cache_entry *ce = istate->cache[pos];
41 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
42 !strcmp(ce->name, GITMODULES_FILE))
43 return 1;
44 }
45 }
46
47 return 0;
48 }
49
50 /*
51 * Check if the .gitmodules file has unstaged modifications. This must be
52 * checked before allowing modifications to the .gitmodules file with the
53 * intention to stage them later, because when continuing we would stage the
54 * modifications the user didn't stage herself too. That might change in a
55 * future version when we learn to stage the changes we do ourselves without
56 * staging any previous modifications.
57 */
58 int is_staging_gitmodules_ok(struct index_state *istate)
59 {
60 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
61
62 if ((pos >= 0) && (pos < istate->cache_nr)) {
63 struct stat st;
64 if (lstat(GITMODULES_FILE, &st) == 0 &&
65 ie_match_stat(istate, istate->cache[pos], &st,
66 CE_MATCH_IGNORE_FSMONITOR) & DATA_CHANGED)
67 return 0;
68 }
69
70 return 1;
71 }
72
73 static int for_each_remote_ref_submodule(const char *submodule,
74 each_ref_fn fn, void *cb_data)
75 {
76 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
77 fn, cb_data);
78 }
79
80 /*
81 * Try to update the "path" entry in the "submodule.<name>" section of the
82 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
83 * with the correct path=<oldpath> setting was found and we could update it.
84 */
85 int update_path_in_gitmodules(const char *oldpath, const char *newpath)
86 {
87 struct strbuf entry = STRBUF_INIT;
88 const struct submodule *submodule;
89
90 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
91 return -1;
92
93 if (is_gitmodules_unmerged(&the_index))
94 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
95
96 submodule = submodule_from_path(&null_oid, oldpath);
97 if (!submodule || !submodule->name) {
98 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
99 return -1;
100 }
101 strbuf_addstr(&entry, "submodule.");
102 strbuf_addstr(&entry, submodule->name);
103 strbuf_addstr(&entry, ".path");
104 if (git_config_set_in_file_gently(GITMODULES_FILE, entry.buf, newpath) < 0) {
105 /* Maybe the user already did that, don't error out here */
106 warning(_("Could not update .gitmodules entry %s"), entry.buf);
107 strbuf_release(&entry);
108 return -1;
109 }
110 strbuf_release(&entry);
111 return 0;
112 }
113
114 /*
115 * Try to remove the "submodule.<name>" section from .gitmodules where the given
116 * path is configured. Return 0 only if a .gitmodules file was found, a section
117 * with the correct path=<path> setting was found and we could remove it.
118 */
119 int remove_path_from_gitmodules(const char *path)
120 {
121 struct strbuf sect = STRBUF_INIT;
122 const struct submodule *submodule;
123
124 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
125 return -1;
126
127 if (is_gitmodules_unmerged(&the_index))
128 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
129
130 submodule = submodule_from_path(&null_oid, path);
131 if (!submodule || !submodule->name) {
132 warning(_("Could not find section in .gitmodules where path=%s"), path);
133 return -1;
134 }
135 strbuf_addstr(&sect, "submodule.");
136 strbuf_addstr(&sect, submodule->name);
137 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
138 /* Maybe the user already did that, don't error out here */
139 warning(_("Could not remove .gitmodules entry for %s"), path);
140 strbuf_release(&sect);
141 return -1;
142 }
143 strbuf_release(&sect);
144 return 0;
145 }
146
147 void stage_updated_gitmodules(struct index_state *istate)
148 {
149 if (add_file_to_index(istate, GITMODULES_FILE, 0))
150 die(_("staging updated .gitmodules failed"));
151 }
152
153 static int add_submodule_odb(const char *path)
154 {
155 struct strbuf objects_directory = STRBUF_INIT;
156 int ret = 0;
157
158 ret = strbuf_git_path_submodule(&objects_directory, path, "objects/");
159 if (ret)
160 goto done;
161 if (!is_directory(objects_directory.buf)) {
162 ret = -1;
163 goto done;
164 }
165 add_to_alternates_memory(objects_directory.buf);
166 done:
167 strbuf_release(&objects_directory);
168 return ret;
169 }
170
171 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
172 const char *path)
173 {
174 const struct submodule *submodule = submodule_from_path(&null_oid, path);
175 if (submodule) {
176 const char *ignore;
177 char *key;
178
179 key = xstrfmt("submodule.%s.ignore", submodule->name);
180 if (repo_config_get_string_const(the_repository, key, &ignore))
181 ignore = submodule->ignore;
182 free(key);
183
184 if (ignore)
185 handle_ignore_submodules_arg(diffopt, ignore);
186 else if (is_gitmodules_unmerged(&the_index))
187 diffopt->flags.ignore_submodules = 1;
188 }
189 }
190
191 /* Cheap function that only determines if we're interested in submodules at all */
192 int git_default_submodule_config(const char *var, const char *value, void *cb)
193 {
194 if (!strcmp(var, "submodule.recurse")) {
195 int v = git_config_bool(var, value) ?
196 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
197 config_update_recurse_submodules = v;
198 }
199 return 0;
200 }
201
202 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
203 const char *arg, int unset)
204 {
205 if (unset) {
206 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
207 return 0;
208 }
209 if (arg)
210 config_update_recurse_submodules =
211 parse_update_recurse_submodules_arg(opt->long_name,
212 arg);
213 else
214 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
215
216 return 0;
217 }
218
219 /*
220 * Determine if a submodule has been initialized at a given 'path'
221 */
222 int is_submodule_active(struct repository *repo, const char *path)
223 {
224 int ret = 0;
225 char *key = NULL;
226 char *value = NULL;
227 const struct string_list *sl;
228 const struct submodule *module;
229
230 module = submodule_from_cache(repo, &null_oid, path);
231
232 /* early return if there isn't a path->module mapping */
233 if (!module)
234 return 0;
235
236 /* submodule.<name>.active is set */
237 key = xstrfmt("submodule.%s.active", module->name);
238 if (!repo_config_get_bool(repo, key, &ret)) {
239 free(key);
240 return ret;
241 }
242 free(key);
243
244 /* submodule.active is set */
245 sl = repo_config_get_value_multi(repo, "submodule.active");
246 if (sl) {
247 struct pathspec ps;
248 struct argv_array args = ARGV_ARRAY_INIT;
249 const struct string_list_item *item;
250
251 for_each_string_list_item(item, sl) {
252 argv_array_push(&args, item->string);
253 }
254
255 parse_pathspec(&ps, 0, 0, NULL, args.argv);
256 ret = match_pathspec(&ps, path, strlen(path), 0, NULL, 1);
257
258 argv_array_clear(&args);
259 clear_pathspec(&ps);
260 return ret;
261 }
262
263 /* fallback to checking if the URL is set */
264 key = xstrfmt("submodule.%s.url", module->name);
265 ret = !repo_config_get_string(repo, key, &value);
266
267 free(value);
268 free(key);
269 return ret;
270 }
271
272 int is_submodule_populated_gently(const char *path, int *return_error_code)
273 {
274 int ret = 0;
275 char *gitdir = xstrfmt("%s/.git", path);
276
277 if (resolve_gitdir_gently(gitdir, return_error_code))
278 ret = 1;
279
280 free(gitdir);
281 return ret;
282 }
283
284 /*
285 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
286 */
287 void die_in_unpopulated_submodule(const struct index_state *istate,
288 const char *prefix)
289 {
290 int i, prefixlen;
291
292 if (!prefix)
293 return;
294
295 prefixlen = strlen(prefix);
296
297 for (i = 0; i < istate->cache_nr; i++) {
298 struct cache_entry *ce = istate->cache[i];
299 int ce_len = ce_namelen(ce);
300
301 if (!S_ISGITLINK(ce->ce_mode))
302 continue;
303 if (prefixlen <= ce_len)
304 continue;
305 if (strncmp(ce->name, prefix, ce_len))
306 continue;
307 if (prefix[ce_len] != '/')
308 continue;
309
310 die(_("in unpopulated submodule '%s'"), ce->name);
311 }
312 }
313
314 /*
315 * Dies if any paths in the provided pathspec descends into a submodule
316 */
317 void die_path_inside_submodule(const struct index_state *istate,
318 const struct pathspec *ps)
319 {
320 int i, j;
321
322 for (i = 0; i < istate->cache_nr; i++) {
323 struct cache_entry *ce = istate->cache[i];
324 int ce_len = ce_namelen(ce);
325
326 if (!S_ISGITLINK(ce->ce_mode))
327 continue;
328
329 for (j = 0; j < ps->nr ; j++) {
330 const struct pathspec_item *item = &ps->items[j];
331
332 if (item->len <= ce_len)
333 continue;
334 if (item->match[ce_len] != '/')
335 continue;
336 if (strncmp(ce->name, item->match, ce_len))
337 continue;
338 if (item->len == ce_len + 1)
339 continue;
340
341 die(_("Pathspec '%s' is in submodule '%.*s'"),
342 item->original, ce_len, ce->name);
343 }
344 }
345 }
346
347 enum submodule_update_type parse_submodule_update_type(const char *value)
348 {
349 if (!strcmp(value, "none"))
350 return SM_UPDATE_NONE;
351 else if (!strcmp(value, "checkout"))
352 return SM_UPDATE_CHECKOUT;
353 else if (!strcmp(value, "rebase"))
354 return SM_UPDATE_REBASE;
355 else if (!strcmp(value, "merge"))
356 return SM_UPDATE_MERGE;
357 else if (*value == '!')
358 return SM_UPDATE_COMMAND;
359 else
360 return SM_UPDATE_UNSPECIFIED;
361 }
362
363 int parse_submodule_update_strategy(const char *value,
364 struct submodule_update_strategy *dst)
365 {
366 enum submodule_update_type type;
367
368 free((void*)dst->command);
369 dst->command = NULL;
370
371 type = parse_submodule_update_type(value);
372 if (type == SM_UPDATE_UNSPECIFIED)
373 return -1;
374
375 dst->type = type;
376 if (type == SM_UPDATE_COMMAND)
377 dst->command = xstrdup(value + 1);
378
379 return 0;
380 }
381
382 const char *submodule_strategy_to_string(const struct submodule_update_strategy *s)
383 {
384 struct strbuf sb = STRBUF_INIT;
385 switch (s->type) {
386 case SM_UPDATE_CHECKOUT:
387 return "checkout";
388 case SM_UPDATE_MERGE:
389 return "merge";
390 case SM_UPDATE_REBASE:
391 return "rebase";
392 case SM_UPDATE_NONE:
393 return "none";
394 case SM_UPDATE_UNSPECIFIED:
395 return NULL;
396 case SM_UPDATE_COMMAND:
397 strbuf_addf(&sb, "!%s", s->command);
398 return strbuf_detach(&sb, NULL);
399 }
400 return NULL;
401 }
402
403 void handle_ignore_submodules_arg(struct diff_options *diffopt,
404 const char *arg)
405 {
406 diffopt->flags.ignore_submodules = 0;
407 diffopt->flags.ignore_untracked_in_submodules = 0;
408 diffopt->flags.ignore_dirty_submodules = 0;
409
410 if (!strcmp(arg, "all"))
411 diffopt->flags.ignore_submodules = 1;
412 else if (!strcmp(arg, "untracked"))
413 diffopt->flags.ignore_untracked_in_submodules = 1;
414 else if (!strcmp(arg, "dirty"))
415 diffopt->flags.ignore_dirty_submodules = 1;
416 else if (strcmp(arg, "none"))
417 die("bad --ignore-submodules argument: %s", arg);
418 }
419
420 static int prepare_submodule_summary(struct rev_info *rev, const char *path,
421 struct commit *left, struct commit *right,
422 struct commit_list *merge_bases)
423 {
424 struct commit_list *list;
425
426 init_revisions(rev, NULL);
427 setup_revisions(0, NULL, rev, NULL);
428 rev->left_right = 1;
429 rev->first_parent_only = 1;
430 left->object.flags |= SYMMETRIC_LEFT;
431 add_pending_object(rev, &left->object, path);
432 add_pending_object(rev, &right->object, path);
433 for (list = merge_bases; list; list = list->next) {
434 list->item->object.flags |= UNINTERESTING;
435 add_pending_object(rev, &list->item->object,
436 oid_to_hex(&list->item->object.oid));
437 }
438 return prepare_revision_walk(rev);
439 }
440
441 static void print_submodule_summary(struct rev_info *rev, struct diff_options *o)
442 {
443 static const char format[] = " %m %s";
444 struct strbuf sb = STRBUF_INIT;
445 struct commit *commit;
446
447 while ((commit = get_revision(rev))) {
448 struct pretty_print_context ctx = {0};
449 ctx.date_mode = rev->date_mode;
450 ctx.output_encoding = get_log_output_encoding();
451 strbuf_setlen(&sb, 0);
452 format_commit_message(commit, format, &sb, &ctx);
453 strbuf_addch(&sb, '\n');
454 if (commit->object.flags & SYMMETRIC_LEFT)
455 diff_emit_submodule_del(o, sb.buf);
456 else
457 diff_emit_submodule_add(o, sb.buf);
458 }
459 strbuf_release(&sb);
460 }
461
462 static void prepare_submodule_repo_env_no_git_dir(struct argv_array *out)
463 {
464 const char * const *var;
465
466 for (var = local_repo_env; *var; var++) {
467 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
468 argv_array_push(out, *var);
469 }
470 }
471
472 void prepare_submodule_repo_env(struct argv_array *out)
473 {
474 prepare_submodule_repo_env_no_git_dir(out);
475 argv_array_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT,
476 DEFAULT_GIT_DIR_ENVIRONMENT);
477 }
478
479 /* Helper function to display the submodule header line prior to the full
480 * summary output. If it can locate the submodule objects directory it will
481 * attempt to lookup both the left and right commits and put them into the
482 * left and right pointers.
483 */
484 static void show_submodule_header(struct diff_options *o, const char *path,
485 struct object_id *one, struct object_id *two,
486 unsigned dirty_submodule,
487 struct commit **left, struct commit **right,
488 struct commit_list **merge_bases)
489 {
490 const char *message = NULL;
491 struct strbuf sb = STRBUF_INIT;
492 int fast_forward = 0, fast_backward = 0;
493
494 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
495 diff_emit_submodule_untracked(o, path);
496
497 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
498 diff_emit_submodule_modified(o, path);
499
500 if (is_null_oid(one))
501 message = "(new submodule)";
502 else if (is_null_oid(two))
503 message = "(submodule deleted)";
504
505 if (add_submodule_odb(path)) {
506 if (!message)
507 message = "(commits not present)";
508 goto output_header;
509 }
510
511 /*
512 * Attempt to lookup the commit references, and determine if this is
513 * a fast forward or fast backwards update.
514 */
515 *left = lookup_commit_reference(one);
516 *right = lookup_commit_reference(two);
517
518 /*
519 * Warn about missing commits in the submodule project, but only if
520 * they aren't null.
521 */
522 if ((!is_null_oid(one) && !*left) ||
523 (!is_null_oid(two) && !*right))
524 message = "(commits not present)";
525
526 *merge_bases = get_merge_bases(*left, *right);
527 if (*merge_bases) {
528 if ((*merge_bases)->item == *left)
529 fast_forward = 1;
530 else if ((*merge_bases)->item == *right)
531 fast_backward = 1;
532 }
533
534 if (!oidcmp(one, two)) {
535 strbuf_release(&sb);
536 return;
537 }
538
539 output_header:
540 strbuf_addf(&sb, "Submodule %s ", path);
541 strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV);
542 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
543 strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV);
544 if (message)
545 strbuf_addf(&sb, " %s\n", message);
546 else
547 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
548 diff_emit_submodule_header(o, sb.buf);
549
550 strbuf_release(&sb);
551 }
552
553 void show_submodule_summary(struct diff_options *o, const char *path,
554 struct object_id *one, struct object_id *two,
555 unsigned dirty_submodule)
556 {
557 struct rev_info rev;
558 struct commit *left = NULL, *right = NULL;
559 struct commit_list *merge_bases = NULL;
560
561 show_submodule_header(o, path, one, two, dirty_submodule,
562 &left, &right, &merge_bases);
563
564 /*
565 * If we don't have both a left and a right pointer, there is no
566 * reason to try and display a summary. The header line should contain
567 * all the information the user needs.
568 */
569 if (!left || !right)
570 goto out;
571
572 /* Treat revision walker failure the same as missing commits */
573 if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) {
574 diff_emit_submodule_error(o, "(revision walker failed)\n");
575 goto out;
576 }
577
578 print_submodule_summary(&rev, o);
579
580 out:
581 if (merge_bases)
582 free_commit_list(merge_bases);
583 clear_commit_marks(left, ~0);
584 clear_commit_marks(right, ~0);
585 }
586
587 void show_submodule_inline_diff(struct diff_options *o, const char *path,
588 struct object_id *one, struct object_id *two,
589 unsigned dirty_submodule)
590 {
591 const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid;
592 struct commit *left = NULL, *right = NULL;
593 struct commit_list *merge_bases = NULL;
594 struct child_process cp = CHILD_PROCESS_INIT;
595 struct strbuf sb = STRBUF_INIT;
596
597 show_submodule_header(o, path, one, two, dirty_submodule,
598 &left, &right, &merge_bases);
599
600 /* We need a valid left and right commit to display a difference */
601 if (!(left || is_null_oid(one)) ||
602 !(right || is_null_oid(two)))
603 goto done;
604
605 if (left)
606 old = one;
607 if (right)
608 new = two;
609
610 cp.git_cmd = 1;
611 cp.dir = path;
612 cp.out = -1;
613 cp.no_stdin = 1;
614
615 /* TODO: other options may need to be passed here. */
616 argv_array_pushl(&cp.args, "diff", "--submodule=diff", NULL);
617 argv_array_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
618 "always" : "never");
619
620 if (o->flags.reverse_diff) {
621 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
622 o->b_prefix, path);
623 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
624 o->a_prefix, path);
625 } else {
626 argv_array_pushf(&cp.args, "--src-prefix=%s%s/",
627 o->a_prefix, path);
628 argv_array_pushf(&cp.args, "--dst-prefix=%s%s/",
629 o->b_prefix, path);
630 }
631 argv_array_push(&cp.args, oid_to_hex(old));
632 /*
633 * If the submodule has modified content, we will diff against the
634 * work tree, under the assumption that the user has asked for the
635 * diff format and wishes to actually see all differences even if they
636 * haven't yet been committed to the submodule yet.
637 */
638 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
639 argv_array_push(&cp.args, oid_to_hex(new));
640
641 prepare_submodule_repo_env(&cp.env_array);
642 if (start_command(&cp))
643 diff_emit_submodule_error(o, "(diff failed)\n");
644
645 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
646 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
647
648 if (finish_command(&cp))
649 diff_emit_submodule_error(o, "(diff failed)\n");
650
651 done:
652 strbuf_release(&sb);
653 if (merge_bases)
654 free_commit_list(merge_bases);
655 if (left)
656 clear_commit_marks(left, ~0);
657 if (right)
658 clear_commit_marks(right, ~0);
659 }
660
661 int should_update_submodules(void)
662 {
663 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
664 }
665
666 const struct submodule *submodule_from_ce(const struct cache_entry *ce)
667 {
668 if (!S_ISGITLINK(ce->ce_mode))
669 return NULL;
670
671 if (!should_update_submodules())
672 return NULL;
673
674 return submodule_from_path(&null_oid, ce->name);
675 }
676
677 static struct oid_array *submodule_commits(struct string_list *submodules,
678 const char *name)
679 {
680 struct string_list_item *item;
681
682 item = string_list_insert(submodules, name);
683 if (item->util)
684 return (struct oid_array *) item->util;
685
686 /* NEEDSWORK: should we have oid_array_init()? */
687 item->util = xcalloc(1, sizeof(struct oid_array));
688 return (struct oid_array *) item->util;
689 }
690
691 struct collect_changed_submodules_cb_data {
692 struct string_list *changed;
693 const struct object_id *commit_oid;
694 };
695
696 /*
697 * this would normally be two functions: default_name_from_path() and
698 * path_from_default_name(). Since the default name is the same as
699 * the submodule path we can get away with just one function which only
700 * checks whether there is a submodule in the working directory at that
701 * location.
702 */
703 static const char *default_name_or_path(const char *path_or_name)
704 {
705 int error_code;
706
707 if (!is_submodule_populated_gently(path_or_name, &error_code))
708 return NULL;
709
710 return path_or_name;
711 }
712
713 static void collect_changed_submodules_cb(struct diff_queue_struct *q,
714 struct diff_options *options,
715 void *data)
716 {
717 struct collect_changed_submodules_cb_data *me = data;
718 struct string_list *changed = me->changed;
719 const struct object_id *commit_oid = me->commit_oid;
720 int i;
721
722 for (i = 0; i < q->nr; i++) {
723 struct diff_filepair *p = q->queue[i];
724 struct oid_array *commits;
725 const struct submodule *submodule;
726 const char *name;
727
728 if (!S_ISGITLINK(p->two->mode))
729 continue;
730
731 submodule = submodule_from_path(commit_oid, p->two->path);
732 if (submodule)
733 name = submodule->name;
734 else {
735 name = default_name_or_path(p->two->path);
736 /* make sure name does not collide with existing one */
737 submodule = submodule_from_name(commit_oid, name);
738 if (submodule) {
739 warning("Submodule in commit %s at path: "
740 "'%s' collides with a submodule named "
741 "the same. Skipping it.",
742 oid_to_hex(commit_oid), name);
743 name = NULL;
744 }
745 }
746
747 if (!name)
748 continue;
749
750 commits = submodule_commits(changed, name);
751 oid_array_append(commits, &p->two->oid);
752 }
753 }
754
755 /*
756 * Collect the paths of submodules in 'changed' which have changed based on
757 * the revisions as specified in 'argv'. Each entry in 'changed' will also
758 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
759 * what the submodule pointers were updated to during the change.
760 */
761 static void collect_changed_submodules(struct string_list *changed,
762 struct argv_array *argv)
763 {
764 struct rev_info rev;
765 const struct commit *commit;
766
767 init_revisions(&rev, NULL);
768 setup_revisions(argv->argc, argv->argv, &rev, NULL);
769 if (prepare_revision_walk(&rev))
770 die("revision walk setup failed");
771
772 while ((commit = get_revision(&rev))) {
773 struct rev_info diff_rev;
774 struct collect_changed_submodules_cb_data data;
775 data.changed = changed;
776 data.commit_oid = &commit->object.oid;
777
778 init_revisions(&diff_rev, NULL);
779 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
780 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
781 diff_rev.diffopt.format_callback_data = &data;
782 diff_tree_combined_merge(commit, 1, &diff_rev);
783 }
784
785 reset_revision_walk();
786 }
787
788 static void free_submodules_oids(struct string_list *submodules)
789 {
790 struct string_list_item *item;
791 for_each_string_list_item(item, submodules)
792 oid_array_clear((struct oid_array *) item->util);
793 string_list_clear(submodules, 1);
794 }
795
796 static int has_remote(const char *refname, const struct object_id *oid,
797 int flags, void *cb_data)
798 {
799 return 1;
800 }
801
802 static int append_oid_to_argv(const struct object_id *oid, void *data)
803 {
804 struct argv_array *argv = data;
805 argv_array_push(argv, oid_to_hex(oid));
806 return 0;
807 }
808
809 struct has_commit_data {
810 int result;
811 const char *path;
812 };
813
814 static int check_has_commit(const struct object_id *oid, void *data)
815 {
816 struct has_commit_data *cb = data;
817
818 enum object_type type = sha1_object_info(oid->hash, NULL);
819
820 switch (type) {
821 case OBJ_COMMIT:
822 return 0;
823 case OBJ_BAD:
824 /*
825 * Object is missing or invalid. If invalid, an error message
826 * has already been printed.
827 */
828 cb->result = 0;
829 return 0;
830 default:
831 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
832 cb->path, oid_to_hex(oid), typename(type));
833 }
834 }
835
836 static int submodule_has_commits(const char *path, struct oid_array *commits)
837 {
838 struct has_commit_data has_commit = { 1, path };
839
840 /*
841 * Perform a cheap, but incorrect check for the existence of 'commits'.
842 * This is done by adding the submodule's object store to the in-core
843 * object store, and then querying for each commit's existence. If we
844 * do not have the commit object anywhere, there is no chance we have
845 * it in the object store of the correct submodule and have it
846 * reachable from a ref, so we can fail early without spawning rev-list
847 * which is expensive.
848 */
849 if (add_submodule_odb(path))
850 return 0;
851
852 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
853
854 if (has_commit.result) {
855 /*
856 * Even if the submodule is checked out and the commit is
857 * present, make sure it exists in the submodule's object store
858 * and that it is reachable from a ref.
859 */
860 struct child_process cp = CHILD_PROCESS_INIT;
861 struct strbuf out = STRBUF_INIT;
862
863 argv_array_pushl(&cp.args, "rev-list", "-n", "1", NULL);
864 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
865 argv_array_pushl(&cp.args, "--not", "--all", NULL);
866
867 prepare_submodule_repo_env(&cp.env_array);
868 cp.git_cmd = 1;
869 cp.no_stdin = 1;
870 cp.dir = path;
871
872 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
873 has_commit.result = 0;
874
875 strbuf_release(&out);
876 }
877
878 return has_commit.result;
879 }
880
881 static int submodule_needs_pushing(const char *path, struct oid_array *commits)
882 {
883 if (!submodule_has_commits(path, commits))
884 /*
885 * NOTE: We do consider it safe to return "no" here. The
886 * correct answer would be "We do not know" instead of
887 * "No push needed", but it is quite hard to change
888 * the submodule pointer without having the submodule
889 * around. If a user did however change the submodules
890 * without having the submodule around, this indicates
891 * an expert who knows what they are doing or a
892 * maintainer integrating work from other people. In
893 * both cases it should be safe to skip this check.
894 */
895 return 0;
896
897 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
898 struct child_process cp = CHILD_PROCESS_INIT;
899 struct strbuf buf = STRBUF_INIT;
900 int needs_pushing = 0;
901
902 argv_array_push(&cp.args, "rev-list");
903 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
904 argv_array_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
905
906 prepare_submodule_repo_env(&cp.env_array);
907 cp.git_cmd = 1;
908 cp.no_stdin = 1;
909 cp.out = -1;
910 cp.dir = path;
911 if (start_command(&cp))
912 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
913 path);
914 if (strbuf_read(&buf, cp.out, 41))
915 needs_pushing = 1;
916 finish_command(&cp);
917 close(cp.out);
918 strbuf_release(&buf);
919 return needs_pushing;
920 }
921
922 return 0;
923 }
924
925 int find_unpushed_submodules(struct oid_array *commits,
926 const char *remotes_name, struct string_list *needs_pushing)
927 {
928 struct string_list submodules = STRING_LIST_INIT_DUP;
929 struct string_list_item *name;
930 struct argv_array argv = ARGV_ARRAY_INIT;
931
932 /* argv.argv[0] will be ignored by setup_revisions */
933 argv_array_push(&argv, "find_unpushed_submodules");
934 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
935 argv_array_push(&argv, "--not");
936 argv_array_pushf(&argv, "--remotes=%s", remotes_name);
937
938 collect_changed_submodules(&submodules, &argv);
939
940 for_each_string_list_item(name, &submodules) {
941 struct oid_array *commits = name->util;
942 const struct submodule *submodule;
943 const char *path = NULL;
944
945 submodule = submodule_from_name(&null_oid, name->string);
946 if (submodule)
947 path = submodule->path;
948 else
949 path = default_name_or_path(name->string);
950
951 if (!path)
952 continue;
953
954 if (submodule_needs_pushing(path, commits))
955 string_list_insert(needs_pushing, path);
956 }
957
958 free_submodules_oids(&submodules);
959 argv_array_clear(&argv);
960
961 return needs_pushing->nr;
962 }
963
964 static int push_submodule(const char *path,
965 const struct remote *remote,
966 const char **refspec, int refspec_nr,
967 const struct string_list *push_options,
968 int dry_run)
969 {
970 if (add_submodule_odb(path))
971 return 1;
972
973 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
974 struct child_process cp = CHILD_PROCESS_INIT;
975 argv_array_push(&cp.args, "push");
976 if (dry_run)
977 argv_array_push(&cp.args, "--dry-run");
978
979 if (push_options && push_options->nr) {
980 const struct string_list_item *item;
981 for_each_string_list_item(item, push_options)
982 argv_array_pushf(&cp.args, "--push-option=%s",
983 item->string);
984 }
985
986 if (remote->origin != REMOTE_UNCONFIGURED) {
987 int i;
988 argv_array_push(&cp.args, remote->name);
989 for (i = 0; i < refspec_nr; i++)
990 argv_array_push(&cp.args, refspec[i]);
991 }
992
993 prepare_submodule_repo_env(&cp.env_array);
994 cp.git_cmd = 1;
995 cp.no_stdin = 1;
996 cp.dir = path;
997 if (run_command(&cp))
998 return 0;
999 close(cp.out);
1000 }
1001
1002 return 1;
1003 }
1004
1005 /*
1006 * Perform a check in the submodule to see if the remote and refspec work.
1007 * Die if the submodule can't be pushed.
1008 */
1009 static void submodule_push_check(const char *path, const char *head,
1010 const struct remote *remote,
1011 const char **refspec, int refspec_nr)
1012 {
1013 struct child_process cp = CHILD_PROCESS_INIT;
1014 int i;
1015
1016 argv_array_push(&cp.args, "submodule--helper");
1017 argv_array_push(&cp.args, "push-check");
1018 argv_array_push(&cp.args, head);
1019 argv_array_push(&cp.args, remote->name);
1020
1021 for (i = 0; i < refspec_nr; i++)
1022 argv_array_push(&cp.args, refspec[i]);
1023
1024 prepare_submodule_repo_env(&cp.env_array);
1025 cp.git_cmd = 1;
1026 cp.no_stdin = 1;
1027 cp.no_stdout = 1;
1028 cp.dir = path;
1029
1030 /*
1031 * Simply indicate if 'submodule--helper push-check' failed.
1032 * More detailed error information will be provided by the
1033 * child process.
1034 */
1035 if (run_command(&cp))
1036 die("process for submodule '%s' failed", path);
1037 }
1038
1039 int push_unpushed_submodules(struct oid_array *commits,
1040 const struct remote *remote,
1041 const char **refspec, int refspec_nr,
1042 const struct string_list *push_options,
1043 int dry_run)
1044 {
1045 int i, ret = 1;
1046 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
1047
1048 if (!find_unpushed_submodules(commits, remote->name, &needs_pushing))
1049 return 1;
1050
1051 /*
1052 * Verify that the remote and refspec can be propagated to all
1053 * submodules. This check can be skipped if the remote and refspec
1054 * won't be propagated due to the remote being unconfigured (e.g. a URL
1055 * instead of a remote name).
1056 */
1057 if (remote->origin != REMOTE_UNCONFIGURED) {
1058 char *head;
1059 struct object_id head_oid;
1060
1061 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1062 if (!head)
1063 die(_("Failed to resolve HEAD as a valid ref."));
1064
1065 for (i = 0; i < needs_pushing.nr; i++)
1066 submodule_push_check(needs_pushing.items[i].string,
1067 head, remote,
1068 refspec, refspec_nr);
1069 free(head);
1070 }
1071
1072 /* Actually push the submodules */
1073 for (i = 0; i < needs_pushing.nr; i++) {
1074 const char *path = needs_pushing.items[i].string;
1075 fprintf(stderr, "Pushing submodule '%s'\n", path);
1076 if (!push_submodule(path, remote, refspec, refspec_nr,
1077 push_options, dry_run)) {
1078 fprintf(stderr, "Unable to push submodule '%s'\n", path);
1079 ret = 0;
1080 }
1081 }
1082
1083 string_list_clear(&needs_pushing, 0);
1084
1085 return ret;
1086 }
1087
1088 static int append_oid_to_array(const char *ref, const struct object_id *oid,
1089 int flags, void *data)
1090 {
1091 struct oid_array *array = data;
1092 oid_array_append(array, oid);
1093 return 0;
1094 }
1095
1096 void check_for_new_submodule_commits(struct object_id *oid)
1097 {
1098 if (!initialized_fetch_ref_tips) {
1099 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
1100 initialized_fetch_ref_tips = 1;
1101 }
1102
1103 oid_array_append(&ref_tips_after_fetch, oid);
1104 }
1105
1106 static void calculate_changed_submodule_paths(void)
1107 {
1108 struct argv_array argv = ARGV_ARRAY_INIT;
1109 struct string_list changed_submodules = STRING_LIST_INIT_DUP;
1110 const struct string_list_item *name;
1111
1112 /* No need to check if there are no submodules configured */
1113 if (!submodule_from_path(NULL, NULL))
1114 return;
1115
1116 argv_array_push(&argv, "--"); /* argv[0] program name */
1117 oid_array_for_each_unique(&ref_tips_after_fetch,
1118 append_oid_to_argv, &argv);
1119 argv_array_push(&argv, "--not");
1120 oid_array_for_each_unique(&ref_tips_before_fetch,
1121 append_oid_to_argv, &argv);
1122
1123 /*
1124 * Collect all submodules (whether checked out or not) for which new
1125 * commits have been recorded upstream in "changed_submodule_names".
1126 */
1127 collect_changed_submodules(&changed_submodules, &argv);
1128
1129 for_each_string_list_item(name, &changed_submodules) {
1130 struct oid_array *commits = name->util;
1131 const struct submodule *submodule;
1132 const char *path = NULL;
1133
1134 submodule = submodule_from_name(&null_oid, name->string);
1135 if (submodule)
1136 path = submodule->path;
1137 else
1138 path = default_name_or_path(name->string);
1139
1140 if (!path)
1141 continue;
1142
1143 if (!submodule_has_commits(path, commits))
1144 string_list_append(&changed_submodule_names, name->string);
1145 }
1146
1147 free_submodules_oids(&changed_submodules);
1148 argv_array_clear(&argv);
1149 oid_array_clear(&ref_tips_before_fetch);
1150 oid_array_clear(&ref_tips_after_fetch);
1151 initialized_fetch_ref_tips = 0;
1152 }
1153
1154 int submodule_touches_in_range(struct object_id *excl_oid,
1155 struct object_id *incl_oid)
1156 {
1157 struct string_list subs = STRING_LIST_INIT_DUP;
1158 struct argv_array args = ARGV_ARRAY_INIT;
1159 int ret;
1160
1161 /* No need to check if there are no submodules configured */
1162 if (!submodule_from_path(NULL, NULL))
1163 return 0;
1164
1165 argv_array_push(&args, "--"); /* args[0] program name */
1166 argv_array_push(&args, oid_to_hex(incl_oid));
1167 argv_array_push(&args, "--not");
1168 argv_array_push(&args, oid_to_hex(excl_oid));
1169
1170 collect_changed_submodules(&subs, &args);
1171 ret = subs.nr;
1172
1173 argv_array_clear(&args);
1174
1175 free_submodules_oids(&subs);
1176 return ret;
1177 }
1178
1179 struct submodule_parallel_fetch {
1180 int count;
1181 struct argv_array args;
1182 const char *work_tree;
1183 const char *prefix;
1184 int command_line_option;
1185 int default_option;
1186 int quiet;
1187 int result;
1188 };
1189 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0, 0}
1190
1191 static int get_fetch_recurse_config(const struct submodule *submodule,
1192 struct submodule_parallel_fetch *spf)
1193 {
1194 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1195 return spf->command_line_option;
1196
1197 if (submodule) {
1198 char *key;
1199 const char *value;
1200
1201 int fetch_recurse = submodule->fetch_recurse;
1202 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
1203 if (!repo_config_get_string_const(the_repository, key, &value)) {
1204 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1205 }
1206 free(key);
1207
1208 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1209 /* local config overrules everything except commandline */
1210 return fetch_recurse;
1211 }
1212
1213 return spf->default_option;
1214 }
1215
1216 static int get_next_submodule(struct child_process *cp,
1217 struct strbuf *err, void *data, void **task_cb)
1218 {
1219 int ret = 0;
1220 struct submodule_parallel_fetch *spf = data;
1221
1222 for (; spf->count < active_nr; spf->count++) {
1223 struct strbuf submodule_path = STRBUF_INIT;
1224 struct strbuf submodule_git_dir = STRBUF_INIT;
1225 struct strbuf submodule_prefix = STRBUF_INIT;
1226 const struct cache_entry *ce = active_cache[spf->count];
1227 const char *git_dir, *default_argv;
1228 const struct submodule *submodule;
1229 struct submodule default_submodule = SUBMODULE_INIT;
1230
1231 if (!S_ISGITLINK(ce->ce_mode))
1232 continue;
1233
1234 submodule = submodule_from_path(&null_oid, ce->name);
1235 if (!submodule) {
1236 const char *name = default_name_or_path(ce->name);
1237 if (name) {
1238 default_submodule.path = default_submodule.name = name;
1239 submodule = &default_submodule;
1240 }
1241 }
1242
1243 switch (get_fetch_recurse_config(submodule, spf))
1244 {
1245 default:
1246 case RECURSE_SUBMODULES_DEFAULT:
1247 case RECURSE_SUBMODULES_ON_DEMAND:
1248 if (!submodule || !unsorted_string_list_lookup(&changed_submodule_names,
1249 submodule->name))
1250 continue;
1251 default_argv = "on-demand";
1252 break;
1253 case RECURSE_SUBMODULES_ON:
1254 default_argv = "yes";
1255 break;
1256 case RECURSE_SUBMODULES_OFF:
1257 continue;
1258 }
1259
1260 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
1261 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
1262 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
1263 git_dir = read_gitfile(submodule_git_dir.buf);
1264 if (!git_dir)
1265 git_dir = submodule_git_dir.buf;
1266 if (is_directory(git_dir)) {
1267 child_process_init(cp);
1268 cp->dir = strbuf_detach(&submodule_path, NULL);
1269 prepare_submodule_repo_env(&cp->env_array);
1270 cp->git_cmd = 1;
1271 if (!spf->quiet)
1272 strbuf_addf(err, "Fetching submodule %s%s\n",
1273 spf->prefix, ce->name);
1274 argv_array_init(&cp->args);
1275 argv_array_pushv(&cp->args, spf->args.argv);
1276 argv_array_push(&cp->args, default_argv);
1277 argv_array_push(&cp->args, "--submodule-prefix");
1278 argv_array_push(&cp->args, submodule_prefix.buf);
1279 ret = 1;
1280 }
1281 strbuf_release(&submodule_path);
1282 strbuf_release(&submodule_git_dir);
1283 strbuf_release(&submodule_prefix);
1284 if (ret) {
1285 spf->count++;
1286 return 1;
1287 }
1288 }
1289 return 0;
1290 }
1291
1292 static int fetch_start_failure(struct strbuf *err,
1293 void *cb, void *task_cb)
1294 {
1295 struct submodule_parallel_fetch *spf = cb;
1296
1297 spf->result = 1;
1298
1299 return 0;
1300 }
1301
1302 static int fetch_finish(int retvalue, struct strbuf *err,
1303 void *cb, void *task_cb)
1304 {
1305 struct submodule_parallel_fetch *spf = cb;
1306
1307 if (retvalue)
1308 spf->result = 1;
1309
1310 return 0;
1311 }
1312
1313 int fetch_populated_submodules(const struct argv_array *options,
1314 const char *prefix, int command_line_option,
1315 int default_option,
1316 int quiet, int max_parallel_jobs)
1317 {
1318 int i;
1319 struct submodule_parallel_fetch spf = SPF_INIT;
1320
1321 spf.work_tree = get_git_work_tree();
1322 spf.command_line_option = command_line_option;
1323 spf.default_option = default_option;
1324 spf.quiet = quiet;
1325 spf.prefix = prefix;
1326
1327 if (!spf.work_tree)
1328 goto out;
1329
1330 if (read_cache() < 0)
1331 die("index file corrupt");
1332
1333 argv_array_push(&spf.args, "fetch");
1334 for (i = 0; i < options->argc; i++)
1335 argv_array_push(&spf.args, options->argv[i]);
1336 argv_array_push(&spf.args, "--recurse-submodules-default");
1337 /* default value, "--submodule-prefix" and its value are added later */
1338
1339 calculate_changed_submodule_paths();
1340 run_processes_parallel(max_parallel_jobs,
1341 get_next_submodule,
1342 fetch_start_failure,
1343 fetch_finish,
1344 &spf);
1345
1346 argv_array_clear(&spf.args);
1347 out:
1348 string_list_clear(&changed_submodule_names, 1);
1349 return spf.result;
1350 }
1351
1352 unsigned is_submodule_modified(const char *path, int ignore_untracked)
1353 {
1354 struct child_process cp = CHILD_PROCESS_INIT;
1355 struct strbuf buf = STRBUF_INIT;
1356 FILE *fp;
1357 unsigned dirty_submodule = 0;
1358 const char *git_dir;
1359 int ignore_cp_exit_code = 0;
1360
1361 strbuf_addf(&buf, "%s/.git", path);
1362 git_dir = read_gitfile(buf.buf);
1363 if (!git_dir)
1364 git_dir = buf.buf;
1365 if (!is_git_directory(git_dir)) {
1366 if (is_directory(git_dir))
1367 die(_("'%s' not recognized as a git repository"), git_dir);
1368 strbuf_release(&buf);
1369 /* The submodule is not checked out, so it is not modified */
1370 return 0;
1371 }
1372 strbuf_reset(&buf);
1373
1374 argv_array_pushl(&cp.args, "status", "--porcelain=2", NULL);
1375 if (ignore_untracked)
1376 argv_array_push(&cp.args, "-uno");
1377
1378 prepare_submodule_repo_env(&cp.env_array);
1379 cp.git_cmd = 1;
1380 cp.no_stdin = 1;
1381 cp.out = -1;
1382 cp.dir = path;
1383 if (start_command(&cp))
1384 die("Could not run 'git status --porcelain=2' in submodule %s", path);
1385
1386 fp = xfdopen(cp.out, "r");
1387 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
1388 /* regular untracked files */
1389 if (buf.buf[0] == '?')
1390 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1391
1392 if (buf.buf[0] == 'u' ||
1393 buf.buf[0] == '1' ||
1394 buf.buf[0] == '2') {
1395 /* T = line type, XY = status, SSSS = submodule state */
1396 if (buf.len < strlen("T XY SSSS"))
1397 die("BUG: invalid status --porcelain=2 line %s",
1398 buf.buf);
1399
1400 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1401 /* nested untracked file */
1402 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1403
1404 if (buf.buf[0] == 'u' ||
1405 buf.buf[0] == '2' ||
1406 memcmp(buf.buf + 5, "S..U", 4))
1407 /* other change */
1408 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
1409 }
1410
1411 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1412 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
1413 ignore_untracked)) {
1414 /*
1415 * We're not interested in any further information from
1416 * the child any more, neither output nor its exit code.
1417 */
1418 ignore_cp_exit_code = 1;
1419 break;
1420 }
1421 }
1422 fclose(fp);
1423
1424 if (finish_command(&cp) && !ignore_cp_exit_code)
1425 die("'git status --porcelain=2' failed in submodule %s", path);
1426
1427 strbuf_release(&buf);
1428 return dirty_submodule;
1429 }
1430
1431 int submodule_uses_gitfile(const char *path)
1432 {
1433 struct child_process cp = CHILD_PROCESS_INIT;
1434 const char *argv[] = {
1435 "submodule",
1436 "foreach",
1437 "--quiet",
1438 "--recursive",
1439 "test -f .git",
1440 NULL,
1441 };
1442 struct strbuf buf = STRBUF_INIT;
1443 const char *git_dir;
1444
1445 strbuf_addf(&buf, "%s/.git", path);
1446 git_dir = read_gitfile(buf.buf);
1447 if (!git_dir) {
1448 strbuf_release(&buf);
1449 return 0;
1450 }
1451 strbuf_release(&buf);
1452
1453 /* Now test that all nested submodules use a gitfile too */
1454 cp.argv = argv;
1455 prepare_submodule_repo_env(&cp.env_array);
1456 cp.git_cmd = 1;
1457 cp.no_stdin = 1;
1458 cp.no_stderr = 1;
1459 cp.no_stdout = 1;
1460 cp.dir = path;
1461 if (run_command(&cp))
1462 return 0;
1463
1464 return 1;
1465 }
1466
1467 /*
1468 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1469 * when doing so.
1470 *
1471 * Return 1 if we'd lose data, return 0 if the removal is fine,
1472 * and negative values for errors.
1473 */
1474 int bad_to_remove_submodule(const char *path, unsigned flags)
1475 {
1476 ssize_t len;
1477 struct child_process cp = CHILD_PROCESS_INIT;
1478 struct strbuf buf = STRBUF_INIT;
1479 int ret = 0;
1480
1481 if (!file_exists(path) || is_empty_dir(path))
1482 return 0;
1483
1484 if (!submodule_uses_gitfile(path))
1485 return 1;
1486
1487 argv_array_pushl(&cp.args, "status", "--porcelain",
1488 "--ignore-submodules=none", NULL);
1489
1490 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
1491 argv_array_push(&cp.args, "-uno");
1492 else
1493 argv_array_push(&cp.args, "-uall");
1494
1495 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
1496 argv_array_push(&cp.args, "--ignored");
1497
1498 prepare_submodule_repo_env(&cp.env_array);
1499 cp.git_cmd = 1;
1500 cp.no_stdin = 1;
1501 cp.out = -1;
1502 cp.dir = path;
1503 if (start_command(&cp)) {
1504 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1505 die(_("could not start 'git status' in submodule '%s'"),
1506 path);
1507 ret = -1;
1508 goto out;
1509 }
1510
1511 len = strbuf_read(&buf, cp.out, 1024);
1512 if (len > 2)
1513 ret = 1;
1514 close(cp.out);
1515
1516 if (finish_command(&cp)) {
1517 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
1518 die(_("could not run 'git status' in submodule '%s'"),
1519 path);
1520 ret = -1;
1521 }
1522 out:
1523 strbuf_release(&buf);
1524 return ret;
1525 }
1526
1527 static const char *get_super_prefix_or_empty(void)
1528 {
1529 const char *s = get_super_prefix();
1530 if (!s)
1531 s = "";
1532 return s;
1533 }
1534
1535 static int submodule_has_dirty_index(const struct submodule *sub)
1536 {
1537 struct child_process cp = CHILD_PROCESS_INIT;
1538
1539 prepare_submodule_repo_env(&cp.env_array);
1540
1541 cp.git_cmd = 1;
1542 argv_array_pushl(&cp.args, "diff-index", "--quiet",
1543 "--cached", "HEAD", NULL);
1544 cp.no_stdin = 1;
1545 cp.no_stdout = 1;
1546 cp.dir = sub->path;
1547 if (start_command(&cp))
1548 die("could not recurse into submodule '%s'", sub->path);
1549
1550 return finish_command(&cp);
1551 }
1552
1553 static void submodule_reset_index(const char *path)
1554 {
1555 struct child_process cp = CHILD_PROCESS_INIT;
1556 prepare_submodule_repo_env(&cp.env_array);
1557
1558 cp.git_cmd = 1;
1559 cp.no_stdin = 1;
1560 cp.dir = path;
1561
1562 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1563 get_super_prefix_or_empty(), path);
1564 argv_array_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
1565
1566 argv_array_push(&cp.args, EMPTY_TREE_SHA1_HEX);
1567
1568 if (run_command(&cp))
1569 die("could not reset submodule index");
1570 }
1571
1572 /**
1573 * Moves a submodule at a given path from a given head to another new head.
1574 * For edge cases (a submodule coming into existence or removing a submodule)
1575 * pass NULL for old or new respectively.
1576 */
1577 int submodule_move_head(const char *path,
1578 const char *old,
1579 const char *new,
1580 unsigned flags)
1581 {
1582 int ret = 0;
1583 struct child_process cp = CHILD_PROCESS_INIT;
1584 const struct submodule *sub;
1585 int *error_code_ptr, error_code;
1586
1587 if (!is_submodule_active(the_repository, path))
1588 return 0;
1589
1590 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1591 /*
1592 * Pass non NULL pointer to is_submodule_populated_gently
1593 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
1594 * to fixup the submodule in the force case later.
1595 */
1596 error_code_ptr = &error_code;
1597 else
1598 error_code_ptr = NULL;
1599
1600 if (old && !is_submodule_populated_gently(path, error_code_ptr))
1601 return 0;
1602
1603 sub = submodule_from_path(&null_oid, path);
1604
1605 if (!sub)
1606 die("BUG: could not get submodule information for '%s'", path);
1607
1608 if (old && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1609 /* Check if the submodule has a dirty index. */
1610 if (submodule_has_dirty_index(sub))
1611 return error(_("submodule '%s' has dirty index"), path);
1612 }
1613
1614 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1615 if (old) {
1616 if (!submodule_uses_gitfile(path))
1617 absorb_git_dir_into_superproject("", path,
1618 ABSORB_GITDIR_RECURSE_SUBMODULES);
1619 } else {
1620 char *gitdir = xstrfmt("%s/modules/%s",
1621 get_git_common_dir(), sub->name);
1622 connect_work_tree_and_git_dir(path, gitdir);
1623 free(gitdir);
1624
1625 /* make sure the index is clean as well */
1626 submodule_reset_index(path);
1627 }
1628
1629 if (old && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
1630 char *gitdir = xstrfmt("%s/modules/%s",
1631 get_git_common_dir(), sub->name);
1632 connect_work_tree_and_git_dir(path, gitdir);
1633 free(gitdir);
1634 }
1635 }
1636
1637 prepare_submodule_repo_env(&cp.env_array);
1638
1639 cp.git_cmd = 1;
1640 cp.no_stdin = 1;
1641 cp.dir = path;
1642
1643 argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
1644 get_super_prefix_or_empty(), path);
1645 argv_array_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
1646
1647 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
1648 argv_array_push(&cp.args, "-n");
1649 else
1650 argv_array_push(&cp.args, "-u");
1651
1652 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
1653 argv_array_push(&cp.args, "--reset");
1654 else
1655 argv_array_push(&cp.args, "-m");
1656
1657 argv_array_push(&cp.args, old ? old : EMPTY_TREE_SHA1_HEX);
1658 argv_array_push(&cp.args, new ? new : EMPTY_TREE_SHA1_HEX);
1659
1660 if (run_command(&cp)) {
1661 ret = -1;
1662 goto out;
1663 }
1664
1665 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
1666 if (new) {
1667 child_process_init(&cp);
1668 /* also set the HEAD accordingly */
1669 cp.git_cmd = 1;
1670 cp.no_stdin = 1;
1671 cp.dir = path;
1672
1673 prepare_submodule_repo_env(&cp.env_array);
1674 argv_array_pushl(&cp.args, "update-ref", "HEAD",
1675 "--no-deref", new, NULL);
1676
1677 if (run_command(&cp)) {
1678 ret = -1;
1679 goto out;
1680 }
1681 } else {
1682 struct strbuf sb = STRBUF_INIT;
1683
1684 strbuf_addf(&sb, "%s/.git", path);
1685 unlink_or_warn(sb.buf);
1686 strbuf_release(&sb);
1687
1688 if (is_empty_dir(path))
1689 rmdir_or_warn(path);
1690 }
1691 }
1692 out:
1693 return ret;
1694 }
1695
1696 static int find_first_merges(struct object_array *result, const char *path,
1697 struct commit *a, struct commit *b)
1698 {
1699 int i, j;
1700 struct object_array merges = OBJECT_ARRAY_INIT;
1701 struct commit *commit;
1702 int contains_another;
1703
1704 char merged_revision[42];
1705 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1706 "--all", merged_revision, NULL };
1707 struct rev_info revs;
1708 struct setup_revision_opt rev_opts;
1709
1710 memset(result, 0, sizeof(struct object_array));
1711 memset(&rev_opts, 0, sizeof(rev_opts));
1712
1713 /* get all revisions that merge commit a */
1714 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1715 oid_to_hex(&a->object.oid));
1716 init_revisions(&revs, NULL);
1717 rev_opts.submodule = path;
1718 /* FIXME: can't handle linked worktrees in submodules yet */
1719 revs.single_worktree = path != NULL;
1720 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1721
1722 /* save all revisions from the above list that contain b */
1723 if (prepare_revision_walk(&revs))
1724 die("revision walk setup failed");
1725 while ((commit = get_revision(&revs)) != NULL) {
1726 struct object *o = &(commit->object);
1727 if (in_merge_bases(b, commit))
1728 add_object_array(o, NULL, &merges);
1729 }
1730 reset_revision_walk();
1731
1732 /* Now we've got all merges that contain a and b. Prune all
1733 * merges that contain another found merge and save them in
1734 * result.
1735 */
1736 for (i = 0; i < merges.nr; i++) {
1737 struct commit *m1 = (struct commit *) merges.objects[i].item;
1738
1739 contains_another = 0;
1740 for (j = 0; j < merges.nr; j++) {
1741 struct commit *m2 = (struct commit *) merges.objects[j].item;
1742 if (i != j && in_merge_bases(m2, m1)) {
1743 contains_another = 1;
1744 break;
1745 }
1746 }
1747
1748 if (!contains_another)
1749 add_object_array(merges.objects[i].item, NULL, result);
1750 }
1751
1752 object_array_clear(&merges);
1753 return result->nr;
1754 }
1755
1756 static void print_commit(struct commit *commit)
1757 {
1758 struct strbuf sb = STRBUF_INIT;
1759 struct pretty_print_context ctx = {0};
1760 ctx.date_mode.type = DATE_NORMAL;
1761 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1762 fprintf(stderr, "%s\n", sb.buf);
1763 strbuf_release(&sb);
1764 }
1765
1766 #define MERGE_WARNING(path, msg) \
1767 warning("Failed to merge submodule %s (%s)", path, msg);
1768
1769 int merge_submodule(struct object_id *result, const char *path,
1770 const struct object_id *base, const struct object_id *a,
1771 const struct object_id *b, int search)
1772 {
1773 struct commit *commit_base, *commit_a, *commit_b;
1774 int parent_count;
1775 struct object_array merges;
1776
1777 int i;
1778
1779 /* store a in result in case we fail */
1780 oidcpy(result, a);
1781
1782 /* we can not handle deletion conflicts */
1783 if (is_null_oid(base))
1784 return 0;
1785 if (is_null_oid(a))
1786 return 0;
1787 if (is_null_oid(b))
1788 return 0;
1789
1790 if (add_submodule_odb(path)) {
1791 MERGE_WARNING(path, "not checked out");
1792 return 0;
1793 }
1794
1795 if (!(commit_base = lookup_commit_reference(base)) ||
1796 !(commit_a = lookup_commit_reference(a)) ||
1797 !(commit_b = lookup_commit_reference(b))) {
1798 MERGE_WARNING(path, "commits not present");
1799 return 0;
1800 }
1801
1802 /* check whether both changes are forward */
1803 if (!in_merge_bases(commit_base, commit_a) ||
1804 !in_merge_bases(commit_base, commit_b)) {
1805 MERGE_WARNING(path, "commits don't follow merge-base");
1806 return 0;
1807 }
1808
1809 /* Case #1: a is contained in b or vice versa */
1810 if (in_merge_bases(commit_a, commit_b)) {
1811 oidcpy(result, b);
1812 return 1;
1813 }
1814 if (in_merge_bases(commit_b, commit_a)) {
1815 oidcpy(result, a);
1816 return 1;
1817 }
1818
1819 /*
1820 * Case #2: There are one or more merges that contain a and b in
1821 * the submodule. If there is only one, then present it as a
1822 * suggestion to the user, but leave it marked unmerged so the
1823 * user needs to confirm the resolution.
1824 */
1825
1826 /* Skip the search if makes no sense to the calling context. */
1827 if (!search)
1828 return 0;
1829
1830 /* find commit which merges them */
1831 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1832 switch (parent_count) {
1833 case 0:
1834 MERGE_WARNING(path, "merge following commits not found");
1835 break;
1836
1837 case 1:
1838 MERGE_WARNING(path, "not fast-forward");
1839 fprintf(stderr, "Found a possible merge resolution "
1840 "for the submodule:\n");
1841 print_commit((struct commit *) merges.objects[0].item);
1842 fprintf(stderr,
1843 "If this is correct simply add it to the index "
1844 "for example\n"
1845 "by using:\n\n"
1846 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1847 "which will accept this suggestion.\n",
1848 oid_to_hex(&merges.objects[0].item->oid), path);
1849 break;
1850
1851 default:
1852 MERGE_WARNING(path, "multiple merges found");
1853 for (i = 0; i < merges.nr; i++)
1854 print_commit((struct commit *) merges.objects[i].item);
1855 }
1856
1857 object_array_clear(&merges);
1858 return 0;
1859 }
1860
1861 /*
1862 * Embeds a single submodules git directory into the superprojects git dir,
1863 * non recursively.
1864 */
1865 static void relocate_single_git_dir_into_superproject(const char *prefix,
1866 const char *path)
1867 {
1868 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
1869 const char *new_git_dir;
1870 const struct submodule *sub;
1871
1872 if (submodule_uses_worktrees(path))
1873 die(_("relocate_gitdir for submodule '%s' with "
1874 "more than one worktree not supported"), path);
1875
1876 old_git_dir = xstrfmt("%s/.git", path);
1877 if (read_gitfile(old_git_dir))
1878 /* If it is an actual gitfile, it doesn't need migration. */
1879 return;
1880
1881 real_old_git_dir = real_pathdup(old_git_dir, 1);
1882
1883 sub = submodule_from_path(&null_oid, path);
1884 if (!sub)
1885 die(_("could not lookup name for submodule '%s'"), path);
1886
1887 new_git_dir = git_path("modules/%s", sub->name);
1888 if (safe_create_leading_directories_const(new_git_dir) < 0)
1889 die(_("could not create directory '%s'"), new_git_dir);
1890 real_new_git_dir = real_pathdup(new_git_dir, 1);
1891
1892 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1893 get_super_prefix_or_empty(), path,
1894 real_old_git_dir, real_new_git_dir);
1895
1896 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
1897
1898 free(old_git_dir);
1899 free(real_old_git_dir);
1900 free(real_new_git_dir);
1901 }
1902
1903 /*
1904 * Migrate the git directory of the submodule given by path from
1905 * having its git directory within the working tree to the git dir nested
1906 * in its superprojects git dir under modules/.
1907 */
1908 void absorb_git_dir_into_superproject(const char *prefix,
1909 const char *path,
1910 unsigned flags)
1911 {
1912 int err_code;
1913 const char *sub_git_dir;
1914 struct strbuf gitdir = STRBUF_INIT;
1915 strbuf_addf(&gitdir, "%s/.git", path);
1916 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
1917
1918 /* Not populated? */
1919 if (!sub_git_dir) {
1920 const struct submodule *sub;
1921
1922 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
1923 /* unpopulated as expected */
1924 strbuf_release(&gitdir);
1925 return;
1926 }
1927
1928 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
1929 /* We don't know what broke here. */
1930 read_gitfile_error_die(err_code, path, NULL);
1931
1932 /*
1933 * Maybe populated, but no git directory was found?
1934 * This can happen if the superproject is a submodule
1935 * itself and was just absorbed. The absorption of the
1936 * superproject did not rewrite the git file links yet,
1937 * fix it now.
1938 */
1939 sub = submodule_from_path(&null_oid, path);
1940 if (!sub)
1941 die(_("could not lookup name for submodule '%s'"), path);
1942 connect_work_tree_and_git_dir(path,
1943 git_path("modules/%s", sub->name));
1944 } else {
1945 /* Is it already absorbed into the superprojects git dir? */
1946 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
1947 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
1948
1949 if (!starts_with(real_sub_git_dir, real_common_git_dir))
1950 relocate_single_git_dir_into_superproject(prefix, path);
1951
1952 free(real_sub_git_dir);
1953 free(real_common_git_dir);
1954 }
1955 strbuf_release(&gitdir);
1956
1957 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
1958 struct child_process cp = CHILD_PROCESS_INIT;
1959 struct strbuf sb = STRBUF_INIT;
1960
1961 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
1962 die("BUG: we don't know how to pass the flags down?");
1963
1964 strbuf_addstr(&sb, get_super_prefix_or_empty());
1965 strbuf_addstr(&sb, path);
1966 strbuf_addch(&sb, '/');
1967
1968 cp.dir = path;
1969 cp.git_cmd = 1;
1970 cp.no_stdin = 1;
1971 argv_array_pushl(&cp.args, "--super-prefix", sb.buf,
1972 "submodule--helper",
1973 "absorb-git-dirs", NULL);
1974 prepare_submodule_repo_env(&cp.env_array);
1975 if (run_command(&cp))
1976 die(_("could not recurse into submodule '%s'"), path);
1977
1978 strbuf_release(&sb);
1979 }
1980 }
1981
1982 const char *get_superproject_working_tree(void)
1983 {
1984 struct child_process cp = CHILD_PROCESS_INIT;
1985 struct strbuf sb = STRBUF_INIT;
1986 const char *one_up = real_path_if_valid("../");
1987 const char *cwd = xgetcwd();
1988 const char *ret = NULL;
1989 const char *subpath;
1990 int code;
1991 ssize_t len;
1992
1993 if (!is_inside_work_tree())
1994 /*
1995 * FIXME:
1996 * We might have a superproject, but it is harder
1997 * to determine.
1998 */
1999 return NULL;
2000
2001 if (!one_up)
2002 return NULL;
2003
2004 subpath = relative_path(cwd, one_up, &sb);
2005
2006 prepare_submodule_repo_env(&cp.env_array);
2007 argv_array_pop(&cp.env_array);
2008
2009 argv_array_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
2010 "ls-files", "-z", "--stage", "--full-name", "--",
2011 subpath, NULL);
2012 strbuf_reset(&sb);
2013
2014 cp.no_stdin = 1;
2015 cp.no_stderr = 1;
2016 cp.out = -1;
2017 cp.git_cmd = 1;
2018
2019 if (start_command(&cp))
2020 die(_("could not start ls-files in .."));
2021
2022 len = strbuf_read(&sb, cp.out, PATH_MAX);
2023 close(cp.out);
2024
2025 if (starts_with(sb.buf, "160000")) {
2026 int super_sub_len;
2027 int cwd_len = strlen(cwd);
2028 char *super_sub, *super_wt;
2029
2030 /*
2031 * There is a superproject having this repo as a submodule.
2032 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2033 * We're only interested in the name after the tab.
2034 */
2035 super_sub = strchr(sb.buf, '\t') + 1;
2036 super_sub_len = sb.buf + sb.len - super_sub - 1;
2037
2038 if (super_sub_len > cwd_len ||
2039 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
2040 die (_("BUG: returned path string doesn't match cwd?"));
2041
2042 super_wt = xstrdup(cwd);
2043 super_wt[cwd_len - super_sub_len] = '\0';
2044
2045 ret = real_path(super_wt);
2046 free(super_wt);
2047 }
2048 strbuf_release(&sb);
2049
2050 code = finish_command(&cp);
2051
2052 if (code == 128)
2053 /* '../' is not a git repository */
2054 return NULL;
2055 if (code == 0 && len == 0)
2056 /* There is an unrelated git repository at '../' */
2057 return NULL;
2058 if (code)
2059 die(_("ls-tree returned unexpected return code %d"), code);
2060
2061 return ret;
2062 }
2063
2064 /*
2065 * Put the gitdir for a submodule (given relative to the main
2066 * repository worktree) into `buf`, or return -1 on error.
2067 */
2068 int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2069 {
2070 const struct submodule *sub;
2071 const char *git_dir;
2072 int ret = 0;
2073
2074 strbuf_reset(buf);
2075 strbuf_addstr(buf, submodule);
2076 strbuf_complete(buf, '/');
2077 strbuf_addstr(buf, ".git");
2078
2079 git_dir = read_gitfile(buf->buf);
2080 if (git_dir) {
2081 strbuf_reset(buf);
2082 strbuf_addstr(buf, git_dir);
2083 }
2084 if (!is_git_directory(buf->buf)) {
2085 sub = submodule_from_path(&null_oid, submodule);
2086 if (!sub) {
2087 ret = -1;
2088 goto cleanup;
2089 }
2090 strbuf_reset(buf);
2091 strbuf_git_path(buf, "%s/%s", "modules", sub->name);
2092 }
2093
2094 cleanup:
2095 return ret;
2096 }