2 #include "submodule-config.h"
8 #include "run-command.h"
11 #include "string-list.h"
12 #include "sha1-array.h"
13 #include "argv-array.h"
15 #include "thread-utils.h"
19 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
20 static int parallel_jobs
= 1;
21 static struct string_list changed_submodule_paths
= STRING_LIST_INIT_NODUP
;
22 static int initialized_fetch_ref_tips
;
23 static struct sha1_array ref_tips_before_fetch
;
24 static struct sha1_array ref_tips_after_fetch
;
27 * The following flag is set if the .gitmodules file is unmerged. We then
28 * disable recursion for all submodules where .git/config doesn't have a
29 * matching config entry because we can't guess what might be configured in
30 * .gitmodules unless the user resolves the conflict. When a command line
31 * option is given (which always overrides configuration) this flag will be
34 static int gitmodules_is_unmerged
;
37 * This flag is set if the .gitmodules file had unstaged modifications on
38 * startup. This must be checked before allowing modifications to the
39 * .gitmodules file with the intention to stage them later, because when
40 * continuing we would stage the modifications the user didn't stage herself
41 * too. That might change in a future version when we learn to stage the
42 * changes we do ourselves without staging any previous modifications.
44 static int gitmodules_is_modified
;
46 int is_staging_gitmodules_ok(void)
48 return !gitmodules_is_modified
;
52 * Try to update the "path" entry in the "submodule.<name>" section of the
53 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
54 * with the correct path=<oldpath> setting was found and we could update it.
56 int update_path_in_gitmodules(const char *oldpath
, const char *newpath
)
58 struct strbuf entry
= STRBUF_INIT
;
59 const struct submodule
*submodule
;
61 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
64 if (gitmodules_is_unmerged
)
65 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
67 submodule
= submodule_from_path(null_sha1
, oldpath
);
68 if (!submodule
|| !submodule
->name
) {
69 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
72 strbuf_addstr(&entry
, "submodule.");
73 strbuf_addstr(&entry
, submodule
->name
);
74 strbuf_addstr(&entry
, ".path");
75 if (git_config_set_in_file_gently(".gitmodules", entry
.buf
, newpath
) < 0) {
76 /* Maybe the user already did that, don't error out here */
77 warning(_("Could not update .gitmodules entry %s"), entry
.buf
);
78 strbuf_release(&entry
);
81 strbuf_release(&entry
);
86 * Try to remove the "submodule.<name>" section from .gitmodules where the given
87 * path is configured. Return 0 only if a .gitmodules file was found, a section
88 * with the correct path=<path> setting was found and we could remove it.
90 int remove_path_from_gitmodules(const char *path
)
92 struct strbuf sect
= STRBUF_INIT
;
93 const struct submodule
*submodule
;
95 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
98 if (gitmodules_is_unmerged
)
99 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
101 submodule
= submodule_from_path(null_sha1
, path
);
102 if (!submodule
|| !submodule
->name
) {
103 warning(_("Could not find section in .gitmodules where path=%s"), path
);
106 strbuf_addstr(§
, "submodule.");
107 strbuf_addstr(§
, submodule
->name
);
108 if (git_config_rename_section_in_file(".gitmodules", sect
.buf
, NULL
) < 0) {
109 /* Maybe the user already did that, don't error out here */
110 warning(_("Could not remove .gitmodules entry for %s"), path
);
111 strbuf_release(§
);
114 strbuf_release(§
);
118 void stage_updated_gitmodules(void)
120 if (add_file_to_cache(".gitmodules", 0))
121 die(_("staging updated .gitmodules failed"));
124 static int add_submodule_odb(const char *path
)
126 struct strbuf objects_directory
= STRBUF_INIT
;
129 ret
= strbuf_git_path_submodule(&objects_directory
, path
, "objects/");
132 if (!is_directory(objects_directory
.buf
)) {
136 add_to_alternates_memory(objects_directory
.buf
);
138 strbuf_release(&objects_directory
);
142 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
145 const struct submodule
*submodule
= submodule_from_path(null_sha1
, path
);
147 if (submodule
->ignore
)
148 handle_ignore_submodules_arg(diffopt
, submodule
->ignore
);
149 else if (gitmodules_is_unmerged
)
150 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
154 int submodule_config(const char *var
, const char *value
, void *cb
)
156 if (!strcmp(var
, "submodule.fetchjobs")) {
157 parallel_jobs
= git_config_int(var
, value
);
158 if (parallel_jobs
< 0)
159 die(_("negative values not allowed for submodule.fetchJobs"));
161 } else if (starts_with(var
, "submodule."))
162 return parse_submodule_config_option(var
, value
);
163 else if (!strcmp(var
, "fetch.recursesubmodules")) {
164 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
170 void gitmodules_config(void)
172 const char *work_tree
= get_git_work_tree();
174 struct strbuf gitmodules_path
= STRBUF_INIT
;
176 strbuf_addstr(&gitmodules_path
, work_tree
);
177 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
178 if (read_cache() < 0)
179 die("index file corrupt");
180 pos
= cache_name_pos(".gitmodules", 11);
181 if (pos
< 0) { /* .gitmodules not found or isn't merged */
183 if (active_nr
> pos
) { /* there is a .gitmodules */
184 const struct cache_entry
*ce
= active_cache
[pos
];
185 if (ce_namelen(ce
) == 11 &&
186 !memcmp(ce
->name
, ".gitmodules", 11))
187 gitmodules_is_unmerged
= 1;
189 } else if (pos
< active_nr
) {
191 if (lstat(".gitmodules", &st
) == 0 &&
192 ce_match_stat(active_cache
[pos
], &st
, 0) & DATA_CHANGED
)
193 gitmodules_is_modified
= 1;
196 if (!gitmodules_is_unmerged
)
197 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
198 strbuf_release(&gitmodules_path
);
202 void gitmodules_config_sha1(const unsigned char *commit_sha1
)
204 struct strbuf rev
= STRBUF_INIT
;
205 unsigned char sha1
[20];
207 if (gitmodule_sha1_from_commit(commit_sha1
, sha1
, &rev
)) {
208 git_config_from_blob_sha1(submodule_config
, rev
.buf
,
211 strbuf_release(&rev
);
215 * Determine if a submodule has been initialized at a given 'path'
217 int is_submodule_initialized(const char *path
)
220 const struct submodule
*module
= NULL
;
222 module
= submodule_from_path(null_sha1
, path
);
225 char *key
= xstrfmt("submodule.%s.url", module
->name
);
228 ret
= !git_config_get_string(key
, &value
);
237 int is_submodule_populated_gently(const char *path
, int *return_error_code
)
240 char *gitdir
= xstrfmt("%s/.git", path
);
242 if (resolve_gitdir_gently(gitdir
, return_error_code
))
249 int parse_submodule_update_strategy(const char *value
,
250 struct submodule_update_strategy
*dst
)
252 free((void*)dst
->command
);
254 if (!strcmp(value
, "none"))
255 dst
->type
= SM_UPDATE_NONE
;
256 else if (!strcmp(value
, "checkout"))
257 dst
->type
= SM_UPDATE_CHECKOUT
;
258 else if (!strcmp(value
, "rebase"))
259 dst
->type
= SM_UPDATE_REBASE
;
260 else if (!strcmp(value
, "merge"))
261 dst
->type
= SM_UPDATE_MERGE
;
262 else if (skip_prefix(value
, "!", &value
)) {
263 dst
->type
= SM_UPDATE_COMMAND
;
264 dst
->command
= xstrdup(value
);
270 const char *submodule_strategy_to_string(const struct submodule_update_strategy
*s
)
272 struct strbuf sb
= STRBUF_INIT
;
274 case SM_UPDATE_CHECKOUT
:
276 case SM_UPDATE_MERGE
:
278 case SM_UPDATE_REBASE
:
282 case SM_UPDATE_UNSPECIFIED
:
284 case SM_UPDATE_COMMAND
:
285 strbuf_addf(&sb
, "!%s", s
->command
);
286 return strbuf_detach(&sb
, NULL
);
291 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
294 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
295 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
296 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
298 if (!strcmp(arg
, "all"))
299 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
300 else if (!strcmp(arg
, "untracked"))
301 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
302 else if (!strcmp(arg
, "dirty"))
303 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
304 else if (strcmp(arg
, "none"))
305 die("bad --ignore-submodules argument: %s", arg
);
308 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
309 struct commit
*left
, struct commit
*right
,
310 struct commit_list
*merge_bases
)
312 struct commit_list
*list
;
314 init_revisions(rev
, NULL
);
315 setup_revisions(0, NULL
, rev
, NULL
);
317 rev
->first_parent_only
= 1;
318 left
->object
.flags
|= SYMMETRIC_LEFT
;
319 add_pending_object(rev
, &left
->object
, path
);
320 add_pending_object(rev
, &right
->object
, path
);
321 for (list
= merge_bases
; list
; list
= list
->next
) {
322 list
->item
->object
.flags
|= UNINTERESTING
;
323 add_pending_object(rev
, &list
->item
->object
,
324 oid_to_hex(&list
->item
->object
.oid
));
326 return prepare_revision_walk(rev
);
329 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
330 const char *line_prefix
,
331 const char *del
, const char *add
, const char *reset
)
333 static const char format
[] = " %m %s";
334 struct strbuf sb
= STRBUF_INIT
;
335 struct commit
*commit
;
337 while ((commit
= get_revision(rev
))) {
338 struct pretty_print_context ctx
= {0};
339 ctx
.date_mode
= rev
->date_mode
;
340 ctx
.output_encoding
= get_log_output_encoding();
341 strbuf_setlen(&sb
, 0);
342 strbuf_addstr(&sb
, line_prefix
);
343 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
345 strbuf_addstr(&sb
, del
);
348 strbuf_addstr(&sb
, add
);
349 format_commit_message(commit
, format
, &sb
, &ctx
);
351 strbuf_addstr(&sb
, reset
);
352 strbuf_addch(&sb
, '\n');
353 fprintf(f
, "%s", sb
.buf
);
358 /* Helper function to display the submodule header line prior to the full
359 * summary output. If it can locate the submodule objects directory it will
360 * attempt to lookup both the left and right commits and put them into the
361 * left and right pointers.
363 static void show_submodule_header(FILE *f
, const char *path
,
364 const char *line_prefix
,
365 struct object_id
*one
, struct object_id
*two
,
366 unsigned dirty_submodule
, const char *meta
,
368 struct commit
**left
, struct commit
**right
,
369 struct commit_list
**merge_bases
)
371 const char *message
= NULL
;
372 struct strbuf sb
= STRBUF_INIT
;
373 int fast_forward
= 0, fast_backward
= 0;
375 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
376 fprintf(f
, "%sSubmodule %s contains untracked content\n",
378 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
379 fprintf(f
, "%sSubmodule %s contains modified content\n",
382 if (is_null_oid(one
))
383 message
= "(new submodule)";
384 else if (is_null_oid(two
))
385 message
= "(submodule deleted)";
387 if (add_submodule_odb(path
)) {
389 message
= "(not initialized)";
394 * Attempt to lookup the commit references, and determine if this is
395 * a fast forward or fast backwards update.
397 *left
= lookup_commit_reference(one
->hash
);
398 *right
= lookup_commit_reference(two
->hash
);
401 * Warn about missing commits in the submodule project, but only if
404 if ((!is_null_oid(one
) && !*left
) ||
405 (!is_null_oid(two
) && !*right
))
406 message
= "(commits not present)";
408 *merge_bases
= get_merge_bases(*left
, *right
);
410 if ((*merge_bases
)->item
== *left
)
412 else if ((*merge_bases
)->item
== *right
)
416 if (!oidcmp(one
, two
)) {
422 strbuf_addf(&sb
, "%s%sSubmodule %s ", line_prefix
, meta
, path
);
423 strbuf_add_unique_abbrev(&sb
, one
->hash
, DEFAULT_ABBREV
);
424 strbuf_addstr(&sb
, (fast_backward
|| fast_forward
) ? ".." : "...");
425 strbuf_add_unique_abbrev(&sb
, two
->hash
, DEFAULT_ABBREV
);
427 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
429 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
430 fwrite(sb
.buf
, sb
.len
, 1, f
);
435 void show_submodule_summary(FILE *f
, const char *path
,
436 const char *line_prefix
,
437 struct object_id
*one
, struct object_id
*two
,
438 unsigned dirty_submodule
, const char *meta
,
439 const char *del
, const char *add
, const char *reset
)
442 struct commit
*left
= NULL
, *right
= NULL
;
443 struct commit_list
*merge_bases
= NULL
;
445 show_submodule_header(f
, path
, line_prefix
, one
, two
, dirty_submodule
,
446 meta
, reset
, &left
, &right
, &merge_bases
);
449 * If we don't have both a left and a right pointer, there is no
450 * reason to try and display a summary. The header line should contain
451 * all the information the user needs.
456 /* Treat revision walker failure the same as missing commits */
457 if (prepare_submodule_summary(&rev
, path
, left
, right
, merge_bases
)) {
458 fprintf(f
, "%s(revision walker failed)\n", line_prefix
);
462 print_submodule_summary(&rev
, f
, line_prefix
, del
, add
, reset
);
466 free_commit_list(merge_bases
);
467 clear_commit_marks(left
, ~0);
468 clear_commit_marks(right
, ~0);
471 void show_submodule_inline_diff(FILE *f
, const char *path
,
472 const char *line_prefix
,
473 struct object_id
*one
, struct object_id
*two
,
474 unsigned dirty_submodule
, const char *meta
,
475 const char *del
, const char *add
, const char *reset
,
476 const struct diff_options
*o
)
478 const struct object_id
*old
= &empty_tree_oid
, *new = &empty_tree_oid
;
479 struct commit
*left
= NULL
, *right
= NULL
;
480 struct commit_list
*merge_bases
= NULL
;
481 struct strbuf submodule_dir
= STRBUF_INIT
;
482 struct child_process cp
= CHILD_PROCESS_INIT
;
484 show_submodule_header(f
, path
, line_prefix
, one
, two
, dirty_submodule
,
485 meta
, reset
, &left
, &right
, &merge_bases
);
487 /* We need a valid left and right commit to display a difference */
488 if (!(left
|| is_null_oid(one
)) ||
489 !(right
|| is_null_oid(two
)))
500 cp
.out
= dup(fileno(f
));
503 /* TODO: other options may need to be passed here. */
504 argv_array_push(&cp
.args
, "diff");
505 argv_array_pushf(&cp
.args
, "--line-prefix=%s", line_prefix
);
506 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
507 argv_array_pushf(&cp
.args
, "--src-prefix=%s%s/",
509 argv_array_pushf(&cp
.args
, "--dst-prefix=%s%s/",
512 argv_array_pushf(&cp
.args
, "--src-prefix=%s%s/",
514 argv_array_pushf(&cp
.args
, "--dst-prefix=%s%s/",
517 argv_array_push(&cp
.args
, oid_to_hex(old
));
519 * If the submodule has modified content, we will diff against the
520 * work tree, under the assumption that the user has asked for the
521 * diff format and wishes to actually see all differences even if they
522 * haven't yet been committed to the submodule yet.
524 if (!(dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
))
525 argv_array_push(&cp
.args
, oid_to_hex(new));
527 if (run_command(&cp
))
528 fprintf(f
, "(diff failed)\n");
531 strbuf_release(&submodule_dir
);
533 free_commit_list(merge_bases
);
535 clear_commit_marks(left
, ~0);
537 clear_commit_marks(right
, ~0);
540 void set_config_fetch_recurse_submodules(int value
)
542 config_fetch_recurse_submodules
= value
;
545 static int has_remote(const char *refname
, const struct object_id
*oid
,
546 int flags
, void *cb_data
)
551 static int append_sha1_to_argv(const unsigned char sha1
[20], void *data
)
553 struct argv_array
*argv
= data
;
554 argv_array_push(argv
, sha1_to_hex(sha1
));
558 static int check_has_commit(const unsigned char sha1
[20], void *data
)
560 int *has_commit
= data
;
562 if (!lookup_commit_reference(sha1
))
568 static int submodule_has_commits(const char *path
, struct sha1_array
*commits
)
572 if (add_submodule_odb(path
))
575 sha1_array_for_each_unique(commits
, check_has_commit
, &has_commit
);
579 static int submodule_needs_pushing(const char *path
, struct sha1_array
*commits
)
581 if (!submodule_has_commits(path
, commits
))
583 * NOTE: We do consider it safe to return "no" here. The
584 * correct answer would be "We do not know" instead of
585 * "No push needed", but it is quite hard to change
586 * the submodule pointer without having the submodule
587 * around. If a user did however change the submodules
588 * without having the submodule around, this indicates
589 * an expert who knows what they are doing or a
590 * maintainer integrating work from other people. In
591 * both cases it should be safe to skip this check.
595 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
596 struct child_process cp
= CHILD_PROCESS_INIT
;
597 struct strbuf buf
= STRBUF_INIT
;
598 int needs_pushing
= 0;
600 argv_array_push(&cp
.args
, "rev-list");
601 sha1_array_for_each_unique(commits
, append_sha1_to_argv
, &cp
.args
);
602 argv_array_pushl(&cp
.args
, "--not", "--remotes", "-n", "1" , NULL
);
604 prepare_submodule_repo_env(&cp
.env_array
);
609 if (start_command(&cp
))
610 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
612 if (strbuf_read(&buf
, cp
.out
, 41))
616 strbuf_release(&buf
);
617 return needs_pushing
;
623 static struct sha1_array
*submodule_commits(struct string_list
*submodules
,
626 struct string_list_item
*item
;
628 item
= string_list_insert(submodules
, path
);
630 return (struct sha1_array
*) item
->util
;
632 /* NEEDSWORK: should we have sha1_array_init()? */
633 item
->util
= xcalloc(1, sizeof(struct sha1_array
));
634 return (struct sha1_array
*) item
->util
;
637 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
638 struct diff_options
*options
,
642 struct string_list
*submodules
= data
;
644 for (i
= 0; i
< q
->nr
; i
++) {
645 struct diff_filepair
*p
= q
->queue
[i
];
646 struct sha1_array
*commits
;
647 if (!S_ISGITLINK(p
->two
->mode
))
649 commits
= submodule_commits(submodules
, p
->two
->path
);
650 sha1_array_append(commits
, p
->two
->oid
.hash
);
654 static void find_unpushed_submodule_commits(struct commit
*commit
,
655 struct string_list
*needs_pushing
)
659 init_revisions(&rev
, NULL
);
660 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
661 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
662 rev
.diffopt
.format_callback_data
= needs_pushing
;
663 diff_tree_combined_merge(commit
, 1, &rev
);
666 static void free_submodules_sha1s(struct string_list
*submodules
)
668 struct string_list_item
*item
;
669 for_each_string_list_item(item
, submodules
)
670 sha1_array_clear((struct sha1_array
*) item
->util
);
671 string_list_clear(submodules
, 1);
674 int find_unpushed_submodules(struct sha1_array
*commits
,
675 const char *remotes_name
, struct string_list
*needs_pushing
)
678 struct commit
*commit
;
679 struct string_list submodules
= STRING_LIST_INIT_DUP
;
680 struct string_list_item
*submodule
;
681 struct argv_array argv
= ARGV_ARRAY_INIT
;
683 init_revisions(&rev
, NULL
);
685 /* argv.argv[0] will be ignored by setup_revisions */
686 argv_array_push(&argv
, "find_unpushed_submodules");
687 sha1_array_for_each_unique(commits
, append_sha1_to_argv
, &argv
);
688 argv_array_push(&argv
, "--not");
689 argv_array_pushf(&argv
, "--remotes=%s", remotes_name
);
691 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
692 if (prepare_revision_walk(&rev
))
693 die("revision walk setup failed");
695 while ((commit
= get_revision(&rev
)) != NULL
)
696 find_unpushed_submodule_commits(commit
, &submodules
);
698 reset_revision_walk();
699 argv_array_clear(&argv
);
701 for_each_string_list_item(submodule
, &submodules
) {
702 struct sha1_array
*commits
= (struct sha1_array
*) submodule
->util
;
704 if (submodule_needs_pushing(submodule
->string
, commits
))
705 string_list_insert(needs_pushing
, submodule
->string
);
707 free_submodules_sha1s(&submodules
);
709 return needs_pushing
->nr
;
712 static int push_submodule(const char *path
, int dry_run
)
714 if (add_submodule_odb(path
))
717 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
718 struct child_process cp
= CHILD_PROCESS_INIT
;
719 argv_array_push(&cp
.args
, "push");
721 argv_array_push(&cp
.args
, "--dry-run");
723 prepare_submodule_repo_env(&cp
.env_array
);
727 if (run_command(&cp
))
735 int push_unpushed_submodules(struct sha1_array
*commits
,
736 const char *remotes_name
,
740 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
742 if (!find_unpushed_submodules(commits
, remotes_name
, &needs_pushing
))
745 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
746 const char *path
= needs_pushing
.items
[i
].string
;
747 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
748 if (!push_submodule(path
, dry_run
)) {
749 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
754 string_list_clear(&needs_pushing
, 0);
759 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
762 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
763 /* Even if the submodule is checked out and the commit is
764 * present, make sure it is reachable from a ref. */
765 struct child_process cp
= CHILD_PROCESS_INIT
;
766 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
767 struct strbuf buf
= STRBUF_INIT
;
769 argv
[3] = sha1_to_hex(sha1
);
771 prepare_submodule_repo_env(&cp
.env_array
);
775 if (!capture_command(&cp
, &buf
, 1024) && !buf
.len
)
778 strbuf_release(&buf
);
783 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
784 struct diff_options
*options
,
788 for (i
= 0; i
< q
->nr
; i
++) {
789 struct diff_filepair
*p
= q
->queue
[i
];
790 if (!S_ISGITLINK(p
->two
->mode
))
793 if (S_ISGITLINK(p
->one
->mode
)) {
794 /* NEEDSWORK: We should honor the name configured in
795 * the .gitmodules file of the commit we are examining
796 * here to be able to correctly follow submodules
797 * being moved around. */
798 struct string_list_item
*path
;
799 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
800 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->oid
.hash
))
801 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
803 /* Submodule is new or was moved here */
804 /* NEEDSWORK: When the .git directories of submodules
805 * live inside the superprojects .git directory some
806 * day we should fetch new submodules directly into
807 * that location too when config or options request
808 * that so they can be checked out from there. */
814 static int add_sha1_to_array(const char *ref
, const struct object_id
*oid
,
815 int flags
, void *data
)
817 sha1_array_append(data
, oid
->hash
);
821 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
823 if (!initialized_fetch_ref_tips
) {
824 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
825 initialized_fetch_ref_tips
= 1;
828 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
831 static int add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
833 argv_array_push(data
, sha1_to_hex(sha1
));
837 static void calculate_changed_submodule_paths(void)
840 struct commit
*commit
;
841 struct argv_array argv
= ARGV_ARRAY_INIT
;
843 /* No need to check if there are no submodules configured */
844 if (!submodule_from_path(NULL
, NULL
))
847 init_revisions(&rev
, NULL
);
848 argv_array_push(&argv
, "--"); /* argv[0] program name */
849 sha1_array_for_each_unique(&ref_tips_after_fetch
,
850 add_sha1_to_argv
, &argv
);
851 argv_array_push(&argv
, "--not");
852 sha1_array_for_each_unique(&ref_tips_before_fetch
,
853 add_sha1_to_argv
, &argv
);
854 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
855 if (prepare_revision_walk(&rev
))
856 die("revision walk setup failed");
859 * Collect all submodules (whether checked out or not) for which new
860 * commits have been recorded upstream in "changed_submodule_paths".
862 while ((commit
= get_revision(&rev
))) {
863 struct commit_list
*parent
= commit
->parents
;
865 struct diff_options diff_opts
;
866 diff_setup(&diff_opts
);
867 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
868 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
869 diff_opts
.format_callback
= submodule_collect_changed_cb
;
870 diff_setup_done(&diff_opts
);
871 diff_tree_sha1(parent
->item
->object
.oid
.hash
, commit
->object
.oid
.hash
, "", &diff_opts
);
872 diffcore_std(&diff_opts
);
873 diff_flush(&diff_opts
);
874 parent
= parent
->next
;
878 argv_array_clear(&argv
);
879 sha1_array_clear(&ref_tips_before_fetch
);
880 sha1_array_clear(&ref_tips_after_fetch
);
881 initialized_fetch_ref_tips
= 0;
884 struct submodule_parallel_fetch
{
886 struct argv_array args
;
887 const char *work_tree
;
889 int command_line_option
;
893 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
895 static int get_next_submodule(struct child_process
*cp
,
896 struct strbuf
*err
, void *data
, void **task_cb
)
899 struct submodule_parallel_fetch
*spf
= data
;
901 for (; spf
->count
< active_nr
; spf
->count
++) {
902 struct strbuf submodule_path
= STRBUF_INIT
;
903 struct strbuf submodule_git_dir
= STRBUF_INIT
;
904 struct strbuf submodule_prefix
= STRBUF_INIT
;
905 const struct cache_entry
*ce
= active_cache
[spf
->count
];
906 const char *git_dir
, *default_argv
;
907 const struct submodule
*submodule
;
909 if (!S_ISGITLINK(ce
->ce_mode
))
912 submodule
= submodule_from_path(null_sha1
, ce
->name
);
914 submodule
= submodule_from_name(null_sha1
, ce
->name
);
916 default_argv
= "yes";
917 if (spf
->command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
919 submodule
->fetch_recurse
!=
920 RECURSE_SUBMODULES_NONE
) {
921 if (submodule
->fetch_recurse
==
922 RECURSE_SUBMODULES_OFF
)
924 if (submodule
->fetch_recurse
==
925 RECURSE_SUBMODULES_ON_DEMAND
) {
926 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
928 default_argv
= "on-demand";
931 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
932 gitmodules_is_unmerged
)
934 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
935 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
937 default_argv
= "on-demand";
940 } else if (spf
->command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
941 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
943 default_argv
= "on-demand";
946 strbuf_addf(&submodule_path
, "%s/%s", spf
->work_tree
, ce
->name
);
947 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
948 strbuf_addf(&submodule_prefix
, "%s%s/", spf
->prefix
, ce
->name
);
949 git_dir
= read_gitfile(submodule_git_dir
.buf
);
951 git_dir
= submodule_git_dir
.buf
;
952 if (is_directory(git_dir
)) {
953 child_process_init(cp
);
954 cp
->dir
= strbuf_detach(&submodule_path
, NULL
);
955 prepare_submodule_repo_env(&cp
->env_array
);
958 strbuf_addf(err
, "Fetching submodule %s%s\n",
959 spf
->prefix
, ce
->name
);
960 argv_array_init(&cp
->args
);
961 argv_array_pushv(&cp
->args
, spf
->args
.argv
);
962 argv_array_push(&cp
->args
, default_argv
);
963 argv_array_push(&cp
->args
, "--submodule-prefix");
964 argv_array_push(&cp
->args
, submodule_prefix
.buf
);
967 strbuf_release(&submodule_path
);
968 strbuf_release(&submodule_git_dir
);
969 strbuf_release(&submodule_prefix
);
978 static int fetch_start_failure(struct strbuf
*err
,
979 void *cb
, void *task_cb
)
981 struct submodule_parallel_fetch
*spf
= cb
;
988 static int fetch_finish(int retvalue
, struct strbuf
*err
,
989 void *cb
, void *task_cb
)
991 struct submodule_parallel_fetch
*spf
= cb
;
999 int fetch_populated_submodules(const struct argv_array
*options
,
1000 const char *prefix
, int command_line_option
,
1001 int quiet
, int max_parallel_jobs
)
1004 struct submodule_parallel_fetch spf
= SPF_INIT
;
1006 spf
.work_tree
= get_git_work_tree();
1007 spf
.command_line_option
= command_line_option
;
1009 spf
.prefix
= prefix
;
1014 if (read_cache() < 0)
1015 die("index file corrupt");
1017 argv_array_push(&spf
.args
, "fetch");
1018 for (i
= 0; i
< options
->argc
; i
++)
1019 argv_array_push(&spf
.args
, options
->argv
[i
]);
1020 argv_array_push(&spf
.args
, "--recurse-submodules-default");
1021 /* default value, "--submodule-prefix" and its value are added later */
1023 if (max_parallel_jobs
< 0)
1024 max_parallel_jobs
= parallel_jobs
;
1026 calculate_changed_submodule_paths();
1027 run_processes_parallel(max_parallel_jobs
,
1029 fetch_start_failure
,
1033 argv_array_clear(&spf
.args
);
1035 string_list_clear(&changed_submodule_paths
, 1);
1039 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
1042 struct child_process cp
= CHILD_PROCESS_INIT
;
1043 const char *argv
[] = {
1049 struct strbuf buf
= STRBUF_INIT
;
1050 unsigned dirty_submodule
= 0;
1051 const char *line
, *next_line
;
1052 const char *git_dir
;
1054 strbuf_addf(&buf
, "%s/.git", path
);
1055 git_dir
= read_gitfile(buf
.buf
);
1058 if (!is_directory(git_dir
)) {
1059 strbuf_release(&buf
);
1060 /* The submodule is not checked out, so it is not modified */
1066 if (ignore_untracked
)
1070 prepare_submodule_repo_env(&cp
.env_array
);
1075 if (start_command(&cp
))
1076 die("Could not run 'git status --porcelain' in submodule %s", path
);
1078 len
= strbuf_read(&buf
, cp
.out
, 1024);
1081 if ((line
[0] == '?') && (line
[1] == '?')) {
1082 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1083 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
1086 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
1087 if (ignore_untracked
||
1088 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
1091 next_line
= strchr(line
, '\n');
1095 len
-= (next_line
- line
);
1100 if (finish_command(&cp
))
1101 die("'git status --porcelain' failed in submodule %s", path
);
1103 strbuf_release(&buf
);
1104 return dirty_submodule
;
1107 int submodule_uses_gitfile(const char *path
)
1109 struct child_process cp
= CHILD_PROCESS_INIT
;
1110 const char *argv
[] = {
1118 struct strbuf buf
= STRBUF_INIT
;
1119 const char *git_dir
;
1121 strbuf_addf(&buf
, "%s/.git", path
);
1122 git_dir
= read_gitfile(buf
.buf
);
1124 strbuf_release(&buf
);
1127 strbuf_release(&buf
);
1129 /* Now test that all nested submodules use a gitfile too */
1131 prepare_submodule_repo_env(&cp
.env_array
);
1137 if (run_command(&cp
))
1144 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1147 * Return 1 if we'd lose data, return 0 if the removal is fine,
1148 * and negative values for errors.
1150 int bad_to_remove_submodule(const char *path
, unsigned flags
)
1153 struct child_process cp
= CHILD_PROCESS_INIT
;
1154 struct strbuf buf
= STRBUF_INIT
;
1157 if (!file_exists(path
) || is_empty_dir(path
))
1160 if (!submodule_uses_gitfile(path
))
1163 argv_array_pushl(&cp
.args
, "status", "--porcelain",
1164 "--ignore-submodules=none", NULL
);
1166 if (flags
& SUBMODULE_REMOVAL_IGNORE_UNTRACKED
)
1167 argv_array_push(&cp
.args
, "-uno");
1169 argv_array_push(&cp
.args
, "-uall");
1171 if (!(flags
& SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
))
1172 argv_array_push(&cp
.args
, "--ignored");
1174 prepare_submodule_repo_env(&cp
.env_array
);
1179 if (start_command(&cp
)) {
1180 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
1181 die(_("could not start 'git status in submodule '%s'"),
1187 len
= strbuf_read(&buf
, cp
.out
, 1024);
1192 if (finish_command(&cp
)) {
1193 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
1194 die(_("could not run 'git status in submodule '%s'"),
1199 strbuf_release(&buf
);
1203 static int find_first_merges(struct object_array
*result
, const char *path
,
1204 struct commit
*a
, struct commit
*b
)
1207 struct object_array merges
= OBJECT_ARRAY_INIT
;
1208 struct commit
*commit
;
1209 int contains_another
;
1211 char merged_revision
[42];
1212 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1213 "--all", merged_revision
, NULL
};
1214 struct rev_info revs
;
1215 struct setup_revision_opt rev_opts
;
1217 memset(result
, 0, sizeof(struct object_array
));
1218 memset(&rev_opts
, 0, sizeof(rev_opts
));
1220 /* get all revisions that merge commit a */
1221 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1222 oid_to_hex(&a
->object
.oid
));
1223 init_revisions(&revs
, NULL
);
1224 rev_opts
.submodule
= path
;
1225 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1227 /* save all revisions from the above list that contain b */
1228 if (prepare_revision_walk(&revs
))
1229 die("revision walk setup failed");
1230 while ((commit
= get_revision(&revs
)) != NULL
) {
1231 struct object
*o
= &(commit
->object
);
1232 if (in_merge_bases(b
, commit
))
1233 add_object_array(o
, NULL
, &merges
);
1235 reset_revision_walk();
1237 /* Now we've got all merges that contain a and b. Prune all
1238 * merges that contain another found merge and save them in
1241 for (i
= 0; i
< merges
.nr
; i
++) {
1242 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1244 contains_another
= 0;
1245 for (j
= 0; j
< merges
.nr
; j
++) {
1246 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1247 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
1248 contains_another
= 1;
1253 if (!contains_another
)
1254 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1257 free(merges
.objects
);
1261 static void print_commit(struct commit
*commit
)
1263 struct strbuf sb
= STRBUF_INIT
;
1264 struct pretty_print_context ctx
= {0};
1265 ctx
.date_mode
.type
= DATE_NORMAL
;
1266 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1267 fprintf(stderr
, "%s\n", sb
.buf
);
1268 strbuf_release(&sb
);
1271 #define MERGE_WARNING(path, msg) \
1272 warning("Failed to merge submodule %s (%s)", path, msg);
1274 int merge_submodule(unsigned char result
[20], const char *path
,
1275 const unsigned char base
[20], const unsigned char a
[20],
1276 const unsigned char b
[20], int search
)
1278 struct commit
*commit_base
, *commit_a
, *commit_b
;
1280 struct object_array merges
;
1284 /* store a in result in case we fail */
1287 /* we can not handle deletion conflicts */
1288 if (is_null_sha1(base
))
1290 if (is_null_sha1(a
))
1292 if (is_null_sha1(b
))
1295 if (add_submodule_odb(path
)) {
1296 MERGE_WARNING(path
, "not checked out");
1300 if (!(commit_base
= lookup_commit_reference(base
)) ||
1301 !(commit_a
= lookup_commit_reference(a
)) ||
1302 !(commit_b
= lookup_commit_reference(b
))) {
1303 MERGE_WARNING(path
, "commits not present");
1307 /* check whether both changes are forward */
1308 if (!in_merge_bases(commit_base
, commit_a
) ||
1309 !in_merge_bases(commit_base
, commit_b
)) {
1310 MERGE_WARNING(path
, "commits don't follow merge-base");
1314 /* Case #1: a is contained in b or vice versa */
1315 if (in_merge_bases(commit_a
, commit_b
)) {
1319 if (in_merge_bases(commit_b
, commit_a
)) {
1325 * Case #2: There are one or more merges that contain a and b in
1326 * the submodule. If there is only one, then present it as a
1327 * suggestion to the user, but leave it marked unmerged so the
1328 * user needs to confirm the resolution.
1331 /* Skip the search if makes no sense to the calling context. */
1335 /* find commit which merges them */
1336 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
1337 switch (parent_count
) {
1339 MERGE_WARNING(path
, "merge following commits not found");
1343 MERGE_WARNING(path
, "not fast-forward");
1344 fprintf(stderr
, "Found a possible merge resolution "
1345 "for the submodule:\n");
1346 print_commit((struct commit
*) merges
.objects
[0].item
);
1348 "If this is correct simply add it to the index "
1351 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1352 "which will accept this suggestion.\n",
1353 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1357 MERGE_WARNING(path
, "multiple merges found");
1358 for (i
= 0; i
< merges
.nr
; i
++)
1359 print_commit((struct commit
*) merges
.objects
[i
].item
);
1362 free(merges
.objects
);
1366 int parallel_submodules(void)
1368 return parallel_jobs
;
1371 void prepare_submodule_repo_env(struct argv_array
*out
)
1373 const char * const *var
;
1375 for (var
= local_repo_env
; *var
; var
++) {
1376 if (strcmp(*var
, CONFIG_DATA_ENVIRONMENT
))
1377 argv_array_push(out
, *var
);
1379 argv_array_pushf(out
, "%s=%s", GIT_DIR_ENVIRONMENT
,
1380 DEFAULT_GIT_DIR_ENVIRONMENT
);
1384 * Embeds a single submodules git directory into the superprojects git dir,
1387 static void relocate_single_git_dir_into_superproject(const char *prefix
,
1390 char *old_git_dir
= NULL
, *real_old_git_dir
= NULL
, *real_new_git_dir
= NULL
;
1391 const char *new_git_dir
;
1392 const struct submodule
*sub
;
1394 if (submodule_uses_worktrees(path
))
1395 die(_("relocate_gitdir for submodule '%s' with "
1396 "more than one worktree not supported"), path
);
1398 old_git_dir
= xstrfmt("%s/.git", path
);
1399 if (read_gitfile(old_git_dir
))
1400 /* If it is an actual gitfile, it doesn't need migration. */
1403 real_old_git_dir
= real_pathdup(old_git_dir
);
1405 sub
= submodule_from_path(null_sha1
, path
);
1407 die(_("could not lookup name for submodule '%s'"), path
);
1409 new_git_dir
= git_path("modules/%s", sub
->name
);
1410 if (safe_create_leading_directories_const(new_git_dir
) < 0)
1411 die(_("could not create directory '%s'"), new_git_dir
);
1412 real_new_git_dir
= real_pathdup(new_git_dir
);
1415 prefix
= get_super_prefix();
1417 fprintf(stderr
, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1418 prefix
? prefix
: "", path
,
1419 real_old_git_dir
, real_new_git_dir
);
1421 relocate_gitdir(path
, real_old_git_dir
, real_new_git_dir
);
1424 free(real_old_git_dir
);
1425 free(real_new_git_dir
);
1429 * Migrate the git directory of the submodule given by path from
1430 * having its git directory within the working tree to the git dir nested
1431 * in its superprojects git dir under modules/.
1433 void absorb_git_dir_into_superproject(const char *prefix
,
1438 const char *sub_git_dir
;
1439 struct strbuf gitdir
= STRBUF_INIT
;
1440 strbuf_addf(&gitdir
, "%s/.git", path
);
1441 sub_git_dir
= resolve_gitdir_gently(gitdir
.buf
, &err_code
);
1443 /* Not populated? */
1445 const struct submodule
*sub
;
1447 if (err_code
== READ_GITFILE_ERR_STAT_FAILED
) {
1448 /* unpopulated as expected */
1449 strbuf_release(&gitdir
);
1453 if (err_code
!= READ_GITFILE_ERR_NOT_A_REPO
)
1454 /* We don't know what broke here. */
1455 read_gitfile_error_die(err_code
, path
, NULL
);
1458 * Maybe populated, but no git directory was found?
1459 * This can happen if the superproject is a submodule
1460 * itself and was just absorbed. The absorption of the
1461 * superproject did not rewrite the git file links yet,
1464 sub
= submodule_from_path(null_sha1
, path
);
1466 die(_("could not lookup name for submodule '%s'"), path
);
1467 connect_work_tree_and_git_dir(path
,
1468 git_path("modules/%s", sub
->name
));
1470 /* Is it already absorbed into the superprojects git dir? */
1471 char *real_sub_git_dir
= real_pathdup(sub_git_dir
);
1472 char *real_common_git_dir
= real_pathdup(get_git_common_dir());
1474 if (!starts_with(real_sub_git_dir
, real_common_git_dir
))
1475 relocate_single_git_dir_into_superproject(prefix
, path
);
1477 free(real_sub_git_dir
);
1478 free(real_common_git_dir
);
1480 strbuf_release(&gitdir
);
1482 if (flags
& ABSORB_GITDIR_RECURSE_SUBMODULES
) {
1483 struct child_process cp
= CHILD_PROCESS_INIT
;
1484 struct strbuf sb
= STRBUF_INIT
;
1486 if (flags
& ~ABSORB_GITDIR_RECURSE_SUBMODULES
)
1487 die("BUG: we don't know how to pass the flags down?");
1489 if (get_super_prefix())
1490 strbuf_addstr(&sb
, get_super_prefix());
1491 strbuf_addstr(&sb
, path
);
1492 strbuf_addch(&sb
, '/');
1497 argv_array_pushl(&cp
.args
, "--super-prefix", sb
.buf
,
1498 "submodule--helper",
1499 "absorb-git-dirs", NULL
);
1500 prepare_submodule_repo_env(&cp
.env_array
);
1501 if (run_command(&cp
))
1502 die(_("could not recurse into submodule '%s'"), path
);
1504 strbuf_release(&sb
);