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"
20 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
21 static int config_update_recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
22 static int parallel_jobs
= 1;
23 static struct string_list changed_submodule_paths
= STRING_LIST_INIT_NODUP
;
24 static int initialized_fetch_ref_tips
;
25 static struct oid_array ref_tips_before_fetch
;
26 static struct oid_array ref_tips_after_fetch
;
29 * The following flag is set if the .gitmodules file is unmerged. We then
30 * disable recursion for all submodules where .git/config doesn't have a
31 * matching config entry because we can't guess what might be configured in
32 * .gitmodules unless the user resolves the conflict. When a command line
33 * option is given (which always overrides configuration) this flag will be
36 static int gitmodules_is_unmerged
;
39 * This flag is set if the .gitmodules file had unstaged modifications on
40 * startup. This must be checked before allowing modifications to the
41 * .gitmodules file with the intention to stage them later, because when
42 * continuing we would stage the modifications the user didn't stage herself
43 * too. That might change in a future version when we learn to stage the
44 * changes we do ourselves without staging any previous modifications.
46 static int gitmodules_is_modified
;
48 int is_staging_gitmodules_ok(void)
50 return !gitmodules_is_modified
;
54 * Try to update the "path" entry in the "submodule.<name>" section of the
55 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
56 * with the correct path=<oldpath> setting was found and we could update it.
58 int update_path_in_gitmodules(const char *oldpath
, const char *newpath
)
60 struct strbuf entry
= STRBUF_INIT
;
61 const struct submodule
*submodule
;
63 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
66 if (gitmodules_is_unmerged
)
67 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
69 submodule
= submodule_from_path(null_sha1
, oldpath
);
70 if (!submodule
|| !submodule
->name
) {
71 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
74 strbuf_addstr(&entry
, "submodule.");
75 strbuf_addstr(&entry
, submodule
->name
);
76 strbuf_addstr(&entry
, ".path");
77 if (git_config_set_in_file_gently(".gitmodules", entry
.buf
, newpath
) < 0) {
78 /* Maybe the user already did that, don't error out here */
79 warning(_("Could not update .gitmodules entry %s"), entry
.buf
);
80 strbuf_release(&entry
);
83 strbuf_release(&entry
);
88 * Try to remove the "submodule.<name>" section from .gitmodules where the given
89 * path is configured. Return 0 only if a .gitmodules file was found, a section
90 * with the correct path=<path> setting was found and we could remove it.
92 int remove_path_from_gitmodules(const char *path
)
94 struct strbuf sect
= STRBUF_INIT
;
95 const struct submodule
*submodule
;
97 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
100 if (gitmodules_is_unmerged
)
101 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
103 submodule
= submodule_from_path(null_sha1
, path
);
104 if (!submodule
|| !submodule
->name
) {
105 warning(_("Could not find section in .gitmodules where path=%s"), path
);
108 strbuf_addstr(§
, "submodule.");
109 strbuf_addstr(§
, submodule
->name
);
110 if (git_config_rename_section_in_file(".gitmodules", sect
.buf
, NULL
) < 0) {
111 /* Maybe the user already did that, don't error out here */
112 warning(_("Could not remove .gitmodules entry for %s"), path
);
113 strbuf_release(§
);
116 strbuf_release(§
);
120 void stage_updated_gitmodules(void)
122 if (add_file_to_cache(".gitmodules", 0))
123 die(_("staging updated .gitmodules failed"));
126 static int add_submodule_odb(const char *path
)
128 struct strbuf objects_directory
= STRBUF_INIT
;
131 ret
= strbuf_git_path_submodule(&objects_directory
, path
, "objects/");
134 if (!is_directory(objects_directory
.buf
)) {
138 add_to_alternates_memory(objects_directory
.buf
);
140 strbuf_release(&objects_directory
);
144 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
147 const struct submodule
*submodule
= submodule_from_path(null_sha1
, path
);
149 if (submodule
->ignore
)
150 handle_ignore_submodules_arg(diffopt
, submodule
->ignore
);
151 else if (gitmodules_is_unmerged
)
152 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
156 int submodule_config(const char *var
, const char *value
, void *cb
)
158 if (!strcmp(var
, "submodule.fetchjobs")) {
159 parallel_jobs
= git_config_int(var
, value
);
160 if (parallel_jobs
< 0)
161 die(_("negative values not allowed for submodule.fetchJobs"));
163 } else if (starts_with(var
, "submodule."))
164 return parse_submodule_config_option(var
, value
);
165 else if (!strcmp(var
, "fetch.recursesubmodules")) {
166 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
172 void gitmodules_config(void)
174 const char *work_tree
= get_git_work_tree();
176 struct strbuf gitmodules_path
= STRBUF_INIT
;
178 strbuf_addstr(&gitmodules_path
, work_tree
);
179 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
180 if (read_cache() < 0)
181 die("index file corrupt");
182 pos
= cache_name_pos(".gitmodules", 11);
183 if (pos
< 0) { /* .gitmodules not found or isn't merged */
185 if (active_nr
> pos
) { /* there is a .gitmodules */
186 const struct cache_entry
*ce
= active_cache
[pos
];
187 if (ce_namelen(ce
) == 11 &&
188 !memcmp(ce
->name
, ".gitmodules", 11))
189 gitmodules_is_unmerged
= 1;
191 } else if (pos
< active_nr
) {
193 if (lstat(".gitmodules", &st
) == 0 &&
194 ce_match_stat(active_cache
[pos
], &st
, 0) & DATA_CHANGED
)
195 gitmodules_is_modified
= 1;
198 if (!gitmodules_is_unmerged
)
199 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
200 strbuf_release(&gitmodules_path
);
204 void gitmodules_config_sha1(const unsigned char *commit_sha1
)
206 struct strbuf rev
= STRBUF_INIT
;
207 unsigned char sha1
[20];
209 if (gitmodule_sha1_from_commit(commit_sha1
, sha1
, &rev
)) {
210 git_config_from_blob_sha1(submodule_config
, rev
.buf
,
213 strbuf_release(&rev
);
217 * NEEDSWORK: With the addition of different configuration options to determine
218 * if a submodule is of interests, the validity of this function's name comes
219 * into question. Once the dust has settled and more concrete terminology is
220 * decided upon, come up with a more proper name for this function. One
221 * potential candidate could be 'is_submodule_active()'.
223 * Determine if a submodule has been initialized at a given 'path'
225 int is_submodule_initialized(const char *path
)
230 const struct string_list
*sl
;
231 const struct submodule
*module
= submodule_from_path(null_sha1
, path
);
233 /* early return if there isn't a path->module mapping */
237 /* submodule.<name>.active is set */
238 key
= xstrfmt("submodule.%s.active", module
->name
);
239 if (!git_config_get_bool(key
, &ret
)) {
245 /* submodule.active is set */
246 sl
= git_config_get_value_multi("submodule.active");
249 struct argv_array args
= ARGV_ARRAY_INIT
;
250 const struct string_list_item
*item
;
252 for_each_string_list_item(item
, sl
) {
253 argv_array_push(&args
, item
->string
);
256 parse_pathspec(&ps
, 0, 0, NULL
, args
.argv
);
257 ret
= match_pathspec(&ps
, path
, strlen(path
), 0, NULL
, 1);
259 argv_array_clear(&args
);
264 /* fallback to checking if the URL is set */
265 key
= xstrfmt("submodule.%s.url", module
->name
);
266 ret
= !git_config_get_string(key
, &value
);
273 int is_submodule_populated_gently(const char *path
, int *return_error_code
)
276 char *gitdir
= xstrfmt("%s/.git", path
);
278 if (resolve_gitdir_gently(gitdir
, return_error_code
))
286 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
288 void die_in_unpopulated_submodule(const struct index_state
*istate
,
296 prefixlen
= strlen(prefix
);
298 for (i
= 0; i
< istate
->cache_nr
; i
++) {
299 struct cache_entry
*ce
= istate
->cache
[i
];
300 int ce_len
= ce_namelen(ce
);
302 if (!S_ISGITLINK(ce
->ce_mode
))
304 if (prefixlen
<= ce_len
)
306 if (strncmp(ce
->name
, prefix
, ce_len
))
308 if (prefix
[ce_len
] != '/')
311 die(_("in unpopulated submodule '%s'"), ce
->name
);
316 * Dies if any paths in the provided pathspec descends into a submodule
318 void die_path_inside_submodule(const struct index_state
*istate
,
319 const struct pathspec
*ps
)
323 for (i
= 0; i
< istate
->cache_nr
; i
++) {
324 struct cache_entry
*ce
= istate
->cache
[i
];
325 int ce_len
= ce_namelen(ce
);
327 if (!S_ISGITLINK(ce
->ce_mode
))
330 for (j
= 0; j
< ps
->nr
; j
++) {
331 const struct pathspec_item
*item
= &ps
->items
[j
];
333 if (item
->len
<= ce_len
)
335 if (item
->match
[ce_len
] != '/')
337 if (strncmp(ce
->name
, item
->match
, ce_len
))
339 if (item
->len
== ce_len
+ 1)
342 die(_("Pathspec '%s' is in submodule '%.*s'"),
343 item
->original
, ce_len
, ce
->name
);
348 int parse_submodule_update_strategy(const char *value
,
349 struct submodule_update_strategy
*dst
)
351 free((void*)dst
->command
);
353 if (!strcmp(value
, "none"))
354 dst
->type
= SM_UPDATE_NONE
;
355 else if (!strcmp(value
, "checkout"))
356 dst
->type
= SM_UPDATE_CHECKOUT
;
357 else if (!strcmp(value
, "rebase"))
358 dst
->type
= SM_UPDATE_REBASE
;
359 else if (!strcmp(value
, "merge"))
360 dst
->type
= SM_UPDATE_MERGE
;
361 else if (skip_prefix(value
, "!", &value
)) {
362 dst
->type
= SM_UPDATE_COMMAND
;
363 dst
->command
= xstrdup(value
);
369 const char *submodule_strategy_to_string(const struct submodule_update_strategy
*s
)
371 struct strbuf sb
= STRBUF_INIT
;
373 case SM_UPDATE_CHECKOUT
:
375 case SM_UPDATE_MERGE
:
377 case SM_UPDATE_REBASE
:
381 case SM_UPDATE_UNSPECIFIED
:
383 case SM_UPDATE_COMMAND
:
384 strbuf_addf(&sb
, "!%s", s
->command
);
385 return strbuf_detach(&sb
, NULL
);
390 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
393 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
394 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
395 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
397 if (!strcmp(arg
, "all"))
398 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
399 else if (!strcmp(arg
, "untracked"))
400 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
401 else if (!strcmp(arg
, "dirty"))
402 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
403 else if (strcmp(arg
, "none"))
404 die("bad --ignore-submodules argument: %s", arg
);
407 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
408 struct commit
*left
, struct commit
*right
,
409 struct commit_list
*merge_bases
)
411 struct commit_list
*list
;
413 init_revisions(rev
, NULL
);
414 setup_revisions(0, NULL
, rev
, NULL
);
416 rev
->first_parent_only
= 1;
417 left
->object
.flags
|= SYMMETRIC_LEFT
;
418 add_pending_object(rev
, &left
->object
, path
);
419 add_pending_object(rev
, &right
->object
, path
);
420 for (list
= merge_bases
; list
; list
= list
->next
) {
421 list
->item
->object
.flags
|= UNINTERESTING
;
422 add_pending_object(rev
, &list
->item
->object
,
423 oid_to_hex(&list
->item
->object
.oid
));
425 return prepare_revision_walk(rev
);
428 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
429 const char *line_prefix
,
430 const char *del
, const char *add
, const char *reset
)
432 static const char format
[] = " %m %s";
433 struct strbuf sb
= STRBUF_INIT
;
434 struct commit
*commit
;
436 while ((commit
= get_revision(rev
))) {
437 struct pretty_print_context ctx
= {0};
438 ctx
.date_mode
= rev
->date_mode
;
439 ctx
.output_encoding
= get_log_output_encoding();
440 strbuf_setlen(&sb
, 0);
441 strbuf_addstr(&sb
, line_prefix
);
442 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
444 strbuf_addstr(&sb
, del
);
447 strbuf_addstr(&sb
, add
);
448 format_commit_message(commit
, format
, &sb
, &ctx
);
450 strbuf_addstr(&sb
, reset
);
451 strbuf_addch(&sb
, '\n');
452 fprintf(f
, "%s", sb
.buf
);
457 static void prepare_submodule_repo_env_no_git_dir(struct argv_array
*out
)
459 const char * const *var
;
461 for (var
= local_repo_env
; *var
; var
++) {
462 if (strcmp(*var
, CONFIG_DATA_ENVIRONMENT
))
463 argv_array_push(out
, *var
);
467 void prepare_submodule_repo_env(struct argv_array
*out
)
469 prepare_submodule_repo_env_no_git_dir(out
);
470 argv_array_pushf(out
, "%s=%s", GIT_DIR_ENVIRONMENT
,
471 DEFAULT_GIT_DIR_ENVIRONMENT
);
474 /* Helper function to display the submodule header line prior to the full
475 * summary output. If it can locate the submodule objects directory it will
476 * attempt to lookup both the left and right commits and put them into the
477 * left and right pointers.
479 static void show_submodule_header(FILE *f
, const char *path
,
480 const char *line_prefix
,
481 struct object_id
*one
, struct object_id
*two
,
482 unsigned dirty_submodule
, const char *meta
,
484 struct commit
**left
, struct commit
**right
,
485 struct commit_list
**merge_bases
)
487 const char *message
= NULL
;
488 struct strbuf sb
= STRBUF_INIT
;
489 int fast_forward
= 0, fast_backward
= 0;
491 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
492 fprintf(f
, "%sSubmodule %s contains untracked content\n",
494 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
495 fprintf(f
, "%sSubmodule %s contains modified content\n",
498 if (is_null_oid(one
))
499 message
= "(new submodule)";
500 else if (is_null_oid(two
))
501 message
= "(submodule deleted)";
503 if (add_submodule_odb(path
)) {
505 message
= "(not initialized)";
510 * Attempt to lookup the commit references, and determine if this is
511 * a fast forward or fast backwards update.
513 *left
= lookup_commit_reference(one
->hash
);
514 *right
= lookup_commit_reference(two
->hash
);
517 * Warn about missing commits in the submodule project, but only if
520 if ((!is_null_oid(one
) && !*left
) ||
521 (!is_null_oid(two
) && !*right
))
522 message
= "(commits not present)";
524 *merge_bases
= get_merge_bases(*left
, *right
);
526 if ((*merge_bases
)->item
== *left
)
528 else if ((*merge_bases
)->item
== *right
)
532 if (!oidcmp(one
, two
)) {
538 strbuf_addf(&sb
, "%s%sSubmodule %s ", line_prefix
, meta
, path
);
539 strbuf_add_unique_abbrev(&sb
, one
->hash
, DEFAULT_ABBREV
);
540 strbuf_addstr(&sb
, (fast_backward
|| fast_forward
) ? ".." : "...");
541 strbuf_add_unique_abbrev(&sb
, two
->hash
, DEFAULT_ABBREV
);
543 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
545 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
546 fwrite(sb
.buf
, sb
.len
, 1, f
);
551 void show_submodule_summary(FILE *f
, const char *path
,
552 const char *line_prefix
,
553 struct object_id
*one
, struct object_id
*two
,
554 unsigned dirty_submodule
, const char *meta
,
555 const char *del
, const char *add
, const char *reset
)
558 struct commit
*left
= NULL
, *right
= NULL
;
559 struct commit_list
*merge_bases
= NULL
;
561 show_submodule_header(f
, path
, line_prefix
, one
, two
, dirty_submodule
,
562 meta
, reset
, &left
, &right
, &merge_bases
);
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.
572 /* Treat revision walker failure the same as missing commits */
573 if (prepare_submodule_summary(&rev
, path
, left
, right
, merge_bases
)) {
574 fprintf(f
, "%s(revision walker failed)\n", line_prefix
);
578 print_submodule_summary(&rev
, f
, line_prefix
, del
, add
, reset
);
582 free_commit_list(merge_bases
);
583 clear_commit_marks(left
, ~0);
584 clear_commit_marks(right
, ~0);
587 void show_submodule_inline_diff(FILE *f
, const char *path
,
588 const char *line_prefix
,
589 struct object_id
*one
, struct object_id
*two
,
590 unsigned dirty_submodule
, const char *meta
,
591 const char *del
, const char *add
, const char *reset
,
592 const struct diff_options
*o
)
594 const struct object_id
*old
= &empty_tree_oid
, *new = &empty_tree_oid
;
595 struct commit
*left
= NULL
, *right
= NULL
;
596 struct commit_list
*merge_bases
= NULL
;
597 struct strbuf submodule_dir
= STRBUF_INIT
;
598 struct child_process cp
= CHILD_PROCESS_INIT
;
600 show_submodule_header(f
, path
, line_prefix
, one
, two
, dirty_submodule
,
601 meta
, reset
, &left
, &right
, &merge_bases
);
603 /* We need a valid left and right commit to display a difference */
604 if (!(left
|| is_null_oid(one
)) ||
605 !(right
|| is_null_oid(two
)))
616 cp
.out
= dup(fileno(f
));
619 /* TODO: other options may need to be passed here. */
620 argv_array_push(&cp
.args
, "diff");
621 argv_array_pushf(&cp
.args
, "--line-prefix=%s", line_prefix
);
622 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
623 argv_array_pushf(&cp
.args
, "--src-prefix=%s%s/",
625 argv_array_pushf(&cp
.args
, "--dst-prefix=%s%s/",
628 argv_array_pushf(&cp
.args
, "--src-prefix=%s%s/",
630 argv_array_pushf(&cp
.args
, "--dst-prefix=%s%s/",
633 argv_array_push(&cp
.args
, oid_to_hex(old
));
635 * If the submodule has modified content, we will diff against the
636 * work tree, under the assumption that the user has asked for the
637 * diff format and wishes to actually see all differences even if they
638 * haven't yet been committed to the submodule yet.
640 if (!(dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
))
641 argv_array_push(&cp
.args
, oid_to_hex(new));
643 prepare_submodule_repo_env(&cp
.env_array
);
644 if (run_command(&cp
))
645 fprintf(f
, "(diff failed)\n");
648 strbuf_release(&submodule_dir
);
650 free_commit_list(merge_bases
);
652 clear_commit_marks(left
, ~0);
654 clear_commit_marks(right
, ~0);
657 void set_config_fetch_recurse_submodules(int value
)
659 config_fetch_recurse_submodules
= value
;
662 void set_config_update_recurse_submodules(int value
)
664 config_update_recurse_submodules
= value
;
667 int should_update_submodules(void)
669 return config_update_recurse_submodules
== RECURSE_SUBMODULES_ON
;
672 const struct submodule
*submodule_from_ce(const struct cache_entry
*ce
)
674 if (!S_ISGITLINK(ce
->ce_mode
))
677 if (!should_update_submodules())
680 return submodule_from_path(null_sha1
, ce
->name
);
683 static int has_remote(const char *refname
, const struct object_id
*oid
,
684 int flags
, void *cb_data
)
689 static int append_oid_to_argv(const struct object_id
*oid
, void *data
)
691 struct argv_array
*argv
= data
;
692 argv_array_push(argv
, oid_to_hex(oid
));
696 static int check_has_commit(const struct object_id
*oid
, void *data
)
698 int *has_commit
= data
;
700 if (!lookup_commit_reference(oid
->hash
))
706 static int submodule_has_commits(const char *path
, struct oid_array
*commits
)
710 if (add_submodule_odb(path
))
713 oid_array_for_each_unique(commits
, check_has_commit
, &has_commit
);
717 static int submodule_needs_pushing(const char *path
, struct oid_array
*commits
)
719 if (!submodule_has_commits(path
, commits
))
721 * NOTE: We do consider it safe to return "no" here. The
722 * correct answer would be "We do not know" instead of
723 * "No push needed", but it is quite hard to change
724 * the submodule pointer without having the submodule
725 * around. If a user did however change the submodules
726 * without having the submodule around, this indicates
727 * an expert who knows what they are doing or a
728 * maintainer integrating work from other people. In
729 * both cases it should be safe to skip this check.
733 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
734 struct child_process cp
= CHILD_PROCESS_INIT
;
735 struct strbuf buf
= STRBUF_INIT
;
736 int needs_pushing
= 0;
738 argv_array_push(&cp
.args
, "rev-list");
739 oid_array_for_each_unique(commits
, append_oid_to_argv
, &cp
.args
);
740 argv_array_pushl(&cp
.args
, "--not", "--remotes", "-n", "1" , NULL
);
742 prepare_submodule_repo_env(&cp
.env_array
);
747 if (start_command(&cp
))
748 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
750 if (strbuf_read(&buf
, cp
.out
, 41))
754 strbuf_release(&buf
);
755 return needs_pushing
;
761 static struct oid_array
*submodule_commits(struct string_list
*submodules
,
764 struct string_list_item
*item
;
766 item
= string_list_insert(submodules
, path
);
768 return (struct oid_array
*) item
->util
;
770 /* NEEDSWORK: should we have oid_array_init()? */
771 item
->util
= xcalloc(1, sizeof(struct oid_array
));
772 return (struct oid_array
*) item
->util
;
775 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
776 struct diff_options
*options
,
780 struct string_list
*submodules
= data
;
782 for (i
= 0; i
< q
->nr
; i
++) {
783 struct diff_filepair
*p
= q
->queue
[i
];
784 struct oid_array
*commits
;
785 if (!S_ISGITLINK(p
->two
->mode
))
787 commits
= submodule_commits(submodules
, p
->two
->path
);
788 oid_array_append(commits
, &p
->two
->oid
);
792 static void find_unpushed_submodule_commits(struct commit
*commit
,
793 struct string_list
*needs_pushing
)
797 init_revisions(&rev
, NULL
);
798 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
799 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
800 rev
.diffopt
.format_callback_data
= needs_pushing
;
801 diff_tree_combined_merge(commit
, 1, &rev
);
804 static void free_submodules_sha1s(struct string_list
*submodules
)
806 struct string_list_item
*item
;
807 for_each_string_list_item(item
, submodules
)
808 oid_array_clear((struct oid_array
*) item
->util
);
809 string_list_clear(submodules
, 1);
812 int find_unpushed_submodules(struct oid_array
*commits
,
813 const char *remotes_name
, struct string_list
*needs_pushing
)
816 struct commit
*commit
;
817 struct string_list submodules
= STRING_LIST_INIT_DUP
;
818 struct string_list_item
*submodule
;
819 struct argv_array argv
= ARGV_ARRAY_INIT
;
821 init_revisions(&rev
, NULL
);
823 /* argv.argv[0] will be ignored by setup_revisions */
824 argv_array_push(&argv
, "find_unpushed_submodules");
825 oid_array_for_each_unique(commits
, append_oid_to_argv
, &argv
);
826 argv_array_push(&argv
, "--not");
827 argv_array_pushf(&argv
, "--remotes=%s", remotes_name
);
829 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
830 if (prepare_revision_walk(&rev
))
831 die("revision walk setup failed");
833 while ((commit
= get_revision(&rev
)) != NULL
)
834 find_unpushed_submodule_commits(commit
, &submodules
);
836 reset_revision_walk();
837 argv_array_clear(&argv
);
839 for_each_string_list_item(submodule
, &submodules
) {
840 struct oid_array
*commits
= (struct oid_array
*) submodule
->util
;
842 if (submodule_needs_pushing(submodule
->string
, commits
))
843 string_list_insert(needs_pushing
, submodule
->string
);
845 free_submodules_sha1s(&submodules
);
847 return needs_pushing
->nr
;
850 static int push_submodule(const char *path
,
851 const struct remote
*remote
,
852 const char **refspec
, int refspec_nr
,
853 const struct string_list
*push_options
,
856 if (add_submodule_odb(path
))
859 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
860 struct child_process cp
= CHILD_PROCESS_INIT
;
861 argv_array_push(&cp
.args
, "push");
863 argv_array_push(&cp
.args
, "--dry-run");
865 if (push_options
&& push_options
->nr
) {
866 const struct string_list_item
*item
;
867 for_each_string_list_item(item
, push_options
)
868 argv_array_pushf(&cp
.args
, "--push-option=%s",
872 if (remote
->origin
!= REMOTE_UNCONFIGURED
) {
874 argv_array_push(&cp
.args
, remote
->name
);
875 for (i
= 0; i
< refspec_nr
; i
++)
876 argv_array_push(&cp
.args
, refspec
[i
]);
879 prepare_submodule_repo_env(&cp
.env_array
);
883 if (run_command(&cp
))
892 * Perform a check in the submodule to see if the remote and refspec work.
893 * Die if the submodule can't be pushed.
895 static void submodule_push_check(const char *path
, const struct remote
*remote
,
896 const char **refspec
, int refspec_nr
)
898 struct child_process cp
= CHILD_PROCESS_INIT
;
901 argv_array_push(&cp
.args
, "submodule--helper");
902 argv_array_push(&cp
.args
, "push-check");
903 argv_array_push(&cp
.args
, remote
->name
);
905 for (i
= 0; i
< refspec_nr
; i
++)
906 argv_array_push(&cp
.args
, refspec
[i
]);
908 prepare_submodule_repo_env(&cp
.env_array
);
915 * Simply indicate if 'submodule--helper push-check' failed.
916 * More detailed error information will be provided by the
919 if (run_command(&cp
))
920 die("process for submodule '%s' failed", path
);
923 int push_unpushed_submodules(struct oid_array
*commits
,
924 const struct remote
*remote
,
925 const char **refspec
, int refspec_nr
,
926 const struct string_list
*push_options
,
930 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
932 if (!find_unpushed_submodules(commits
, remote
->name
, &needs_pushing
))
936 * Verify that the remote and refspec can be propagated to all
937 * submodules. This check can be skipped if the remote and refspec
938 * won't be propagated due to the remote being unconfigured (e.g. a URL
939 * instead of a remote name).
941 if (remote
->origin
!= REMOTE_UNCONFIGURED
)
942 for (i
= 0; i
< needs_pushing
.nr
; i
++)
943 submodule_push_check(needs_pushing
.items
[i
].string
,
944 remote
, refspec
, refspec_nr
);
946 /* Actually push the submodules */
947 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
948 const char *path
= needs_pushing
.items
[i
].string
;
949 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
950 if (!push_submodule(path
, remote
, refspec
, refspec_nr
,
951 push_options
, dry_run
)) {
952 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
957 string_list_clear(&needs_pushing
, 0);
962 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
965 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
966 /* Even if the submodule is checked out and the commit is
967 * present, make sure it is reachable from a ref. */
968 struct child_process cp
= CHILD_PROCESS_INIT
;
969 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
970 struct strbuf buf
= STRBUF_INIT
;
972 argv
[3] = sha1_to_hex(sha1
);
974 prepare_submodule_repo_env(&cp
.env_array
);
978 if (!capture_command(&cp
, &buf
, 1024) && !buf
.len
)
981 strbuf_release(&buf
);
986 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
987 struct diff_options
*options
,
991 for (i
= 0; i
< q
->nr
; i
++) {
992 struct diff_filepair
*p
= q
->queue
[i
];
993 if (!S_ISGITLINK(p
->two
->mode
))
996 if (S_ISGITLINK(p
->one
->mode
)) {
997 /* NEEDSWORK: We should honor the name configured in
998 * the .gitmodules file of the commit we are examining
999 * here to be able to correctly follow submodules
1000 * being moved around. */
1001 struct string_list_item
*path
;
1002 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
1003 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->oid
.hash
))
1004 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
1006 /* Submodule is new or was moved here */
1007 /* NEEDSWORK: When the .git directories of submodules
1008 * live inside the superprojects .git directory some
1009 * day we should fetch new submodules directly into
1010 * that location too when config or options request
1011 * that so they can be checked out from there. */
1017 static int add_sha1_to_array(const char *ref
, const struct object_id
*oid
,
1018 int flags
, void *data
)
1020 oid_array_append(data
, oid
);
1024 void check_for_new_submodule_commits(struct object_id
*oid
)
1026 if (!initialized_fetch_ref_tips
) {
1027 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
1028 initialized_fetch_ref_tips
= 1;
1031 oid_array_append(&ref_tips_after_fetch
, oid
);
1034 static int add_oid_to_argv(const struct object_id
*oid
, void *data
)
1036 argv_array_push(data
, oid_to_hex(oid
));
1040 static void calculate_changed_submodule_paths(void)
1042 struct rev_info rev
;
1043 struct commit
*commit
;
1044 struct argv_array argv
= ARGV_ARRAY_INIT
;
1046 /* No need to check if there are no submodules configured */
1047 if (!submodule_from_path(NULL
, NULL
))
1050 init_revisions(&rev
, NULL
);
1051 argv_array_push(&argv
, "--"); /* argv[0] program name */
1052 oid_array_for_each_unique(&ref_tips_after_fetch
,
1053 add_oid_to_argv
, &argv
);
1054 argv_array_push(&argv
, "--not");
1055 oid_array_for_each_unique(&ref_tips_before_fetch
,
1056 add_oid_to_argv
, &argv
);
1057 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
1058 if (prepare_revision_walk(&rev
))
1059 die("revision walk setup failed");
1062 * Collect all submodules (whether checked out or not) for which new
1063 * commits have been recorded upstream in "changed_submodule_paths".
1065 while ((commit
= get_revision(&rev
))) {
1066 struct commit_list
*parent
= commit
->parents
;
1068 struct diff_options diff_opts
;
1069 diff_setup(&diff_opts
);
1070 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
1071 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
1072 diff_opts
.format_callback
= submodule_collect_changed_cb
;
1073 diff_setup_done(&diff_opts
);
1074 diff_tree_sha1(parent
->item
->object
.oid
.hash
, commit
->object
.oid
.hash
, "", &diff_opts
);
1075 diffcore_std(&diff_opts
);
1076 diff_flush(&diff_opts
);
1077 parent
= parent
->next
;
1081 argv_array_clear(&argv
);
1082 oid_array_clear(&ref_tips_before_fetch
);
1083 oid_array_clear(&ref_tips_after_fetch
);
1084 initialized_fetch_ref_tips
= 0;
1087 struct submodule_parallel_fetch
{
1089 struct argv_array args
;
1090 const char *work_tree
;
1092 int command_line_option
;
1096 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
1098 static int get_next_submodule(struct child_process
*cp
,
1099 struct strbuf
*err
, void *data
, void **task_cb
)
1102 struct submodule_parallel_fetch
*spf
= data
;
1104 for (; spf
->count
< active_nr
; spf
->count
++) {
1105 struct strbuf submodule_path
= STRBUF_INIT
;
1106 struct strbuf submodule_git_dir
= STRBUF_INIT
;
1107 struct strbuf submodule_prefix
= STRBUF_INIT
;
1108 const struct cache_entry
*ce
= active_cache
[spf
->count
];
1109 const char *git_dir
, *default_argv
;
1110 const struct submodule
*submodule
;
1112 if (!S_ISGITLINK(ce
->ce_mode
))
1115 submodule
= submodule_from_path(null_sha1
, ce
->name
);
1117 submodule
= submodule_from_name(null_sha1
, ce
->name
);
1119 default_argv
= "yes";
1120 if (spf
->command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
1122 submodule
->fetch_recurse
!=
1123 RECURSE_SUBMODULES_NONE
) {
1124 if (submodule
->fetch_recurse
==
1125 RECURSE_SUBMODULES_OFF
)
1127 if (submodule
->fetch_recurse
==
1128 RECURSE_SUBMODULES_ON_DEMAND
) {
1129 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
1131 default_argv
= "on-demand";
1134 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
1135 gitmodules_is_unmerged
)
1137 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
1138 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
1140 default_argv
= "on-demand";
1143 } else if (spf
->command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
1144 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
1146 default_argv
= "on-demand";
1149 strbuf_addf(&submodule_path
, "%s/%s", spf
->work_tree
, ce
->name
);
1150 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
1151 strbuf_addf(&submodule_prefix
, "%s%s/", spf
->prefix
, ce
->name
);
1152 git_dir
= read_gitfile(submodule_git_dir
.buf
);
1154 git_dir
= submodule_git_dir
.buf
;
1155 if (is_directory(git_dir
)) {
1156 child_process_init(cp
);
1157 cp
->dir
= strbuf_detach(&submodule_path
, NULL
);
1158 prepare_submodule_repo_env(&cp
->env_array
);
1161 strbuf_addf(err
, "Fetching submodule %s%s\n",
1162 spf
->prefix
, ce
->name
);
1163 argv_array_init(&cp
->args
);
1164 argv_array_pushv(&cp
->args
, spf
->args
.argv
);
1165 argv_array_push(&cp
->args
, default_argv
);
1166 argv_array_push(&cp
->args
, "--submodule-prefix");
1167 argv_array_push(&cp
->args
, submodule_prefix
.buf
);
1170 strbuf_release(&submodule_path
);
1171 strbuf_release(&submodule_git_dir
);
1172 strbuf_release(&submodule_prefix
);
1181 static int fetch_start_failure(struct strbuf
*err
,
1182 void *cb
, void *task_cb
)
1184 struct submodule_parallel_fetch
*spf
= cb
;
1191 static int fetch_finish(int retvalue
, struct strbuf
*err
,
1192 void *cb
, void *task_cb
)
1194 struct submodule_parallel_fetch
*spf
= cb
;
1202 int fetch_populated_submodules(const struct argv_array
*options
,
1203 const char *prefix
, int command_line_option
,
1204 int quiet
, int max_parallel_jobs
)
1207 struct submodule_parallel_fetch spf
= SPF_INIT
;
1209 spf
.work_tree
= get_git_work_tree();
1210 spf
.command_line_option
= command_line_option
;
1212 spf
.prefix
= prefix
;
1217 if (read_cache() < 0)
1218 die("index file corrupt");
1220 argv_array_push(&spf
.args
, "fetch");
1221 for (i
= 0; i
< options
->argc
; i
++)
1222 argv_array_push(&spf
.args
, options
->argv
[i
]);
1223 argv_array_push(&spf
.args
, "--recurse-submodules-default");
1224 /* default value, "--submodule-prefix" and its value are added later */
1226 if (max_parallel_jobs
< 0)
1227 max_parallel_jobs
= parallel_jobs
;
1229 calculate_changed_submodule_paths();
1230 run_processes_parallel(max_parallel_jobs
,
1232 fetch_start_failure
,
1236 argv_array_clear(&spf
.args
);
1238 string_list_clear(&changed_submodule_paths
, 1);
1242 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
1244 struct child_process cp
= CHILD_PROCESS_INIT
;
1245 struct strbuf buf
= STRBUF_INIT
;
1247 unsigned dirty_submodule
= 0;
1248 const char *git_dir
;
1249 int ignore_cp_exit_code
= 0;
1251 strbuf_addf(&buf
, "%s/.git", path
);
1252 git_dir
= read_gitfile(buf
.buf
);
1255 if (!is_git_directory(git_dir
)) {
1256 if (is_directory(git_dir
))
1257 die(_("'%s' not recognized as a git repository"), git_dir
);
1258 strbuf_release(&buf
);
1259 /* The submodule is not checked out, so it is not modified */
1264 argv_array_pushl(&cp
.args
, "status", "--porcelain=2", NULL
);
1265 if (ignore_untracked
)
1266 argv_array_push(&cp
.args
, "-uno");
1268 prepare_submodule_repo_env(&cp
.env_array
);
1273 if (start_command(&cp
))
1274 die("Could not run 'git status --porcelain=2' in submodule %s", path
);
1276 fp
= xfdopen(cp
.out
, "r");
1277 while (strbuf_getwholeline(&buf
, fp
, '\n') != EOF
) {
1278 /* regular untracked files */
1279 if (buf
.buf
[0] == '?')
1280 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1282 if (buf
.buf
[0] == 'u' ||
1283 buf
.buf
[0] == '1' ||
1284 buf
.buf
[0] == '2') {
1285 /* T = line type, XY = status, SSSS = submodule state */
1286 if (buf
.len
< strlen("T XY SSSS"))
1287 die("BUG: invalid status --porcelain=2 line %s",
1290 if (buf
.buf
[5] == 'S' && buf
.buf
[8] == 'U')
1291 /* nested untracked file */
1292 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1294 if (buf
.buf
[0] == 'u' ||
1295 buf
.buf
[0] == '2' ||
1296 memcmp(buf
.buf
+ 5, "S..U", 4))
1298 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
1301 if ((dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
) &&
1302 ((dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
) ||
1303 ignore_untracked
)) {
1305 * We're not interested in any further information from
1306 * the child any more, neither output nor its exit code.
1308 ignore_cp_exit_code
= 1;
1314 if (finish_command(&cp
) && !ignore_cp_exit_code
)
1315 die("'git status --porcelain=2' failed in submodule %s", path
);
1317 strbuf_release(&buf
);
1318 return dirty_submodule
;
1321 int submodule_uses_gitfile(const char *path
)
1323 struct child_process cp
= CHILD_PROCESS_INIT
;
1324 const char *argv
[] = {
1332 struct strbuf buf
= STRBUF_INIT
;
1333 const char *git_dir
;
1335 strbuf_addf(&buf
, "%s/.git", path
);
1336 git_dir
= read_gitfile(buf
.buf
);
1338 strbuf_release(&buf
);
1341 strbuf_release(&buf
);
1343 /* Now test that all nested submodules use a gitfile too */
1345 prepare_submodule_repo_env(&cp
.env_array
);
1351 if (run_command(&cp
))
1358 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1361 * Return 1 if we'd lose data, return 0 if the removal is fine,
1362 * and negative values for errors.
1364 int bad_to_remove_submodule(const char *path
, unsigned flags
)
1367 struct child_process cp
= CHILD_PROCESS_INIT
;
1368 struct strbuf buf
= STRBUF_INIT
;
1371 if (!file_exists(path
) || is_empty_dir(path
))
1374 if (!submodule_uses_gitfile(path
))
1377 argv_array_pushl(&cp
.args
, "status", "--porcelain",
1378 "--ignore-submodules=none", NULL
);
1380 if (flags
& SUBMODULE_REMOVAL_IGNORE_UNTRACKED
)
1381 argv_array_push(&cp
.args
, "-uno");
1383 argv_array_push(&cp
.args
, "-uall");
1385 if (!(flags
& SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED
))
1386 argv_array_push(&cp
.args
, "--ignored");
1388 prepare_submodule_repo_env(&cp
.env_array
);
1393 if (start_command(&cp
)) {
1394 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
1395 die(_("could not start 'git status' in submodule '%s'"),
1401 len
= strbuf_read(&buf
, cp
.out
, 1024);
1406 if (finish_command(&cp
)) {
1407 if (flags
& SUBMODULE_REMOVAL_DIE_ON_ERROR
)
1408 die(_("could not run 'git status' in submodule '%s'"),
1413 strbuf_release(&buf
);
1417 static const char *get_super_prefix_or_empty(void)
1419 const char *s
= get_super_prefix();
1425 static int submodule_has_dirty_index(const struct submodule
*sub
)
1427 struct child_process cp
= CHILD_PROCESS_INIT
;
1429 prepare_submodule_repo_env_no_git_dir(&cp
.env_array
);
1432 argv_array_pushl(&cp
.args
, "diff-index", "--quiet",
1433 "--cached", "HEAD", NULL
);
1437 if (start_command(&cp
))
1438 die("could not recurse into submodule '%s'", sub
->path
);
1440 return finish_command(&cp
);
1443 static void submodule_reset_index(const char *path
)
1445 struct child_process cp
= CHILD_PROCESS_INIT
;
1446 prepare_submodule_repo_env_no_git_dir(&cp
.env_array
);
1452 argv_array_pushf(&cp
.args
, "--super-prefix=%s%s/",
1453 get_super_prefix_or_empty(), path
);
1454 argv_array_pushl(&cp
.args
, "read-tree", "-u", "--reset", NULL
);
1456 argv_array_push(&cp
.args
, EMPTY_TREE_SHA1_HEX
);
1458 if (run_command(&cp
))
1459 die("could not reset submodule index");
1463 * Moves a submodule at a given path from a given head to another new head.
1464 * For edge cases (a submodule coming into existence or removing a submodule)
1465 * pass NULL for old or new respectively.
1467 int submodule_move_head(const char *path
,
1473 struct child_process cp
= CHILD_PROCESS_INIT
;
1474 const struct submodule
*sub
;
1476 sub
= submodule_from_path(null_sha1
, path
);
1479 die("BUG: could not get submodule information for '%s'", path
);
1481 if (old
&& !(flags
& SUBMODULE_MOVE_HEAD_FORCE
)) {
1482 /* Check if the submodule has a dirty index. */
1483 if (submodule_has_dirty_index(sub
))
1484 return error(_("submodule '%s' has dirty index"), path
);
1487 if (!(flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)) {
1489 if (!submodule_uses_gitfile(path
))
1490 absorb_git_dir_into_superproject("", path
,
1491 ABSORB_GITDIR_RECURSE_SUBMODULES
);
1493 struct strbuf sb
= STRBUF_INIT
;
1494 strbuf_addf(&sb
, "%s/modules/%s",
1495 get_git_common_dir(), sub
->name
);
1496 connect_work_tree_and_git_dir(path
, sb
.buf
);
1497 strbuf_release(&sb
);
1499 /* make sure the index is clean as well */
1500 submodule_reset_index(path
);
1504 prepare_submodule_repo_env_no_git_dir(&cp
.env_array
);
1510 argv_array_pushf(&cp
.args
, "--super-prefix=%s%s/",
1511 get_super_prefix_or_empty(), path
);
1512 argv_array_pushl(&cp
.args
, "read-tree", NULL
);
1514 if (flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)
1515 argv_array_push(&cp
.args
, "-n");
1517 argv_array_push(&cp
.args
, "-u");
1519 if (flags
& SUBMODULE_MOVE_HEAD_FORCE
)
1520 argv_array_push(&cp
.args
, "--reset");
1522 argv_array_push(&cp
.args
, "-m");
1524 argv_array_push(&cp
.args
, old
? old
: EMPTY_TREE_SHA1_HEX
);
1525 argv_array_push(&cp
.args
, new ? new : EMPTY_TREE_SHA1_HEX
);
1527 if (run_command(&cp
)) {
1532 if (!(flags
& SUBMODULE_MOVE_HEAD_DRY_RUN
)) {
1534 struct child_process cp1
= CHILD_PROCESS_INIT
;
1535 /* also set the HEAD accordingly */
1540 argv_array_pushl(&cp1
.args
, "update-ref", "HEAD", new, NULL
);
1542 if (run_command(&cp1
)) {
1547 struct strbuf sb
= STRBUF_INIT
;
1549 strbuf_addf(&sb
, "%s/.git", path
);
1550 unlink_or_warn(sb
.buf
);
1551 strbuf_release(&sb
);
1553 if (is_empty_dir(path
))
1554 rmdir_or_warn(path
);
1561 static int find_first_merges(struct object_array
*result
, const char *path
,
1562 struct commit
*a
, struct commit
*b
)
1565 struct object_array merges
= OBJECT_ARRAY_INIT
;
1566 struct commit
*commit
;
1567 int contains_another
;
1569 char merged_revision
[42];
1570 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1571 "--all", merged_revision
, NULL
};
1572 struct rev_info revs
;
1573 struct setup_revision_opt rev_opts
;
1575 memset(result
, 0, sizeof(struct object_array
));
1576 memset(&rev_opts
, 0, sizeof(rev_opts
));
1578 /* get all revisions that merge commit a */
1579 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1580 oid_to_hex(&a
->object
.oid
));
1581 init_revisions(&revs
, NULL
);
1582 rev_opts
.submodule
= path
;
1583 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1585 /* save all revisions from the above list that contain b */
1586 if (prepare_revision_walk(&revs
))
1587 die("revision walk setup failed");
1588 while ((commit
= get_revision(&revs
)) != NULL
) {
1589 struct object
*o
= &(commit
->object
);
1590 if (in_merge_bases(b
, commit
))
1591 add_object_array(o
, NULL
, &merges
);
1593 reset_revision_walk();
1595 /* Now we've got all merges that contain a and b. Prune all
1596 * merges that contain another found merge and save them in
1599 for (i
= 0; i
< merges
.nr
; i
++) {
1600 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1602 contains_another
= 0;
1603 for (j
= 0; j
< merges
.nr
; j
++) {
1604 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1605 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
1606 contains_another
= 1;
1611 if (!contains_another
)
1612 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1615 free(merges
.objects
);
1619 static void print_commit(struct commit
*commit
)
1621 struct strbuf sb
= STRBUF_INIT
;
1622 struct pretty_print_context ctx
= {0};
1623 ctx
.date_mode
.type
= DATE_NORMAL
;
1624 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1625 fprintf(stderr
, "%s\n", sb
.buf
);
1626 strbuf_release(&sb
);
1629 #define MERGE_WARNING(path, msg) \
1630 warning("Failed to merge submodule %s (%s)", path, msg);
1632 int merge_submodule(unsigned char result
[20], const char *path
,
1633 const unsigned char base
[20], const unsigned char a
[20],
1634 const unsigned char b
[20], int search
)
1636 struct commit
*commit_base
, *commit_a
, *commit_b
;
1638 struct object_array merges
;
1642 /* store a in result in case we fail */
1645 /* we can not handle deletion conflicts */
1646 if (is_null_sha1(base
))
1648 if (is_null_sha1(a
))
1650 if (is_null_sha1(b
))
1653 if (add_submodule_odb(path
)) {
1654 MERGE_WARNING(path
, "not checked out");
1658 if (!(commit_base
= lookup_commit_reference(base
)) ||
1659 !(commit_a
= lookup_commit_reference(a
)) ||
1660 !(commit_b
= lookup_commit_reference(b
))) {
1661 MERGE_WARNING(path
, "commits not present");
1665 /* check whether both changes are forward */
1666 if (!in_merge_bases(commit_base
, commit_a
) ||
1667 !in_merge_bases(commit_base
, commit_b
)) {
1668 MERGE_WARNING(path
, "commits don't follow merge-base");
1672 /* Case #1: a is contained in b or vice versa */
1673 if (in_merge_bases(commit_a
, commit_b
)) {
1677 if (in_merge_bases(commit_b
, commit_a
)) {
1683 * Case #2: There are one or more merges that contain a and b in
1684 * the submodule. If there is only one, then present it as a
1685 * suggestion to the user, but leave it marked unmerged so the
1686 * user needs to confirm the resolution.
1689 /* Skip the search if makes no sense to the calling context. */
1693 /* find commit which merges them */
1694 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
1695 switch (parent_count
) {
1697 MERGE_WARNING(path
, "merge following commits not found");
1701 MERGE_WARNING(path
, "not fast-forward");
1702 fprintf(stderr
, "Found a possible merge resolution "
1703 "for the submodule:\n");
1704 print_commit((struct commit
*) merges
.objects
[0].item
);
1706 "If this is correct simply add it to the index "
1709 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1710 "which will accept this suggestion.\n",
1711 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1715 MERGE_WARNING(path
, "multiple merges found");
1716 for (i
= 0; i
< merges
.nr
; i
++)
1717 print_commit((struct commit
*) merges
.objects
[i
].item
);
1720 free(merges
.objects
);
1724 int parallel_submodules(void)
1726 return parallel_jobs
;
1730 * Embeds a single submodules git directory into the superprojects git dir,
1733 static void relocate_single_git_dir_into_superproject(const char *prefix
,
1736 char *old_git_dir
= NULL
, *real_old_git_dir
= NULL
, *real_new_git_dir
= NULL
;
1737 const char *new_git_dir
;
1738 const struct submodule
*sub
;
1740 if (submodule_uses_worktrees(path
))
1741 die(_("relocate_gitdir for submodule '%s' with "
1742 "more than one worktree not supported"), path
);
1744 old_git_dir
= xstrfmt("%s/.git", path
);
1745 if (read_gitfile(old_git_dir
))
1746 /* If it is an actual gitfile, it doesn't need migration. */
1749 real_old_git_dir
= real_pathdup(old_git_dir
, 1);
1751 sub
= submodule_from_path(null_sha1
, path
);
1753 die(_("could not lookup name for submodule '%s'"), path
);
1755 new_git_dir
= git_path("modules/%s", sub
->name
);
1756 if (safe_create_leading_directories_const(new_git_dir
) < 0)
1757 die(_("could not create directory '%s'"), new_git_dir
);
1758 real_new_git_dir
= real_pathdup(new_git_dir
, 1);
1760 fprintf(stderr
, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
1761 get_super_prefix_or_empty(), path
,
1762 real_old_git_dir
, real_new_git_dir
);
1764 relocate_gitdir(path
, real_old_git_dir
, real_new_git_dir
);
1767 free(real_old_git_dir
);
1768 free(real_new_git_dir
);
1772 * Migrate the git directory of the submodule given by path from
1773 * having its git directory within the working tree to the git dir nested
1774 * in its superprojects git dir under modules/.
1776 void absorb_git_dir_into_superproject(const char *prefix
,
1781 const char *sub_git_dir
;
1782 struct strbuf gitdir
= STRBUF_INIT
;
1783 strbuf_addf(&gitdir
, "%s/.git", path
);
1784 sub_git_dir
= resolve_gitdir_gently(gitdir
.buf
, &err_code
);
1786 /* Not populated? */
1788 const struct submodule
*sub
;
1790 if (err_code
== READ_GITFILE_ERR_STAT_FAILED
) {
1791 /* unpopulated as expected */
1792 strbuf_release(&gitdir
);
1796 if (err_code
!= READ_GITFILE_ERR_NOT_A_REPO
)
1797 /* We don't know what broke here. */
1798 read_gitfile_error_die(err_code
, path
, NULL
);
1801 * Maybe populated, but no git directory was found?
1802 * This can happen if the superproject is a submodule
1803 * itself and was just absorbed. The absorption of the
1804 * superproject did not rewrite the git file links yet,
1807 sub
= submodule_from_path(null_sha1
, path
);
1809 die(_("could not lookup name for submodule '%s'"), path
);
1810 connect_work_tree_and_git_dir(path
,
1811 git_path("modules/%s", sub
->name
));
1813 /* Is it already absorbed into the superprojects git dir? */
1814 char *real_sub_git_dir
= real_pathdup(sub_git_dir
, 1);
1815 char *real_common_git_dir
= real_pathdup(get_git_common_dir(), 1);
1817 if (!starts_with(real_sub_git_dir
, real_common_git_dir
))
1818 relocate_single_git_dir_into_superproject(prefix
, path
);
1820 free(real_sub_git_dir
);
1821 free(real_common_git_dir
);
1823 strbuf_release(&gitdir
);
1825 if (flags
& ABSORB_GITDIR_RECURSE_SUBMODULES
) {
1826 struct child_process cp
= CHILD_PROCESS_INIT
;
1827 struct strbuf sb
= STRBUF_INIT
;
1829 if (flags
& ~ABSORB_GITDIR_RECURSE_SUBMODULES
)
1830 die("BUG: we don't know how to pass the flags down?");
1832 strbuf_addstr(&sb
, get_super_prefix_or_empty());
1833 strbuf_addstr(&sb
, path
);
1834 strbuf_addch(&sb
, '/');
1839 argv_array_pushl(&cp
.args
, "--super-prefix", sb
.buf
,
1840 "submodule--helper",
1841 "absorb-git-dirs", NULL
);
1842 prepare_submodule_repo_env(&cp
.env_array
);
1843 if (run_command(&cp
))
1844 die(_("could not recurse into submodule '%s'"), path
);
1846 strbuf_release(&sb
);
1850 const char *get_superproject_working_tree(void)
1852 struct child_process cp
= CHILD_PROCESS_INIT
;
1853 struct strbuf sb
= STRBUF_INIT
;
1854 const char *one_up
= real_path_if_valid("../");
1855 const char *cwd
= xgetcwd();
1856 const char *ret
= NULL
;
1857 const char *subpath
;
1861 if (!is_inside_work_tree())
1864 * We might have a superproject, but it is harder
1872 subpath
= relative_path(cwd
, one_up
, &sb
);
1874 prepare_submodule_repo_env(&cp
.env_array
);
1875 argv_array_pop(&cp
.env_array
);
1877 argv_array_pushl(&cp
.args
, "--literal-pathspecs", "-C", "..",
1878 "ls-files", "-z", "--stage", "--full-name", "--",
1887 if (start_command(&cp
))
1888 die(_("could not start ls-files in .."));
1890 len
= strbuf_read(&sb
, cp
.out
, PATH_MAX
);
1893 if (starts_with(sb
.buf
, "160000")) {
1895 int cwd_len
= strlen(cwd
);
1896 char *super_sub
, *super_wt
;
1899 * There is a superproject having this repo as a submodule.
1900 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
1901 * We're only interested in the name after the tab.
1903 super_sub
= strchr(sb
.buf
, '\t') + 1;
1904 super_sub_len
= sb
.buf
+ sb
.len
- super_sub
- 1;
1906 if (super_sub_len
> cwd_len
||
1907 strcmp(&cwd
[cwd_len
- super_sub_len
], super_sub
))
1908 die (_("BUG: returned path string doesn't match cwd?"));
1910 super_wt
= xstrdup(cwd
);
1911 super_wt
[cwd_len
- super_sub_len
] = '\0';
1913 ret
= real_path(super_wt
);
1916 strbuf_release(&sb
);
1918 code
= finish_command(&cp
);
1921 /* '../' is not a git repository */
1923 if (code
== 0 && len
== 0)
1924 /* There is an unrelated git repository at '../' */
1927 die(_("ls-tree returned unexpected return code %d"), code
);
1932 int submodule_to_gitdir(struct strbuf
*buf
, const char *submodule
)
1934 const struct submodule
*sub
;
1935 const char *git_dir
;
1939 strbuf_addstr(buf
, submodule
);
1940 strbuf_complete(buf
, '/');
1941 strbuf_addstr(buf
, ".git");
1943 git_dir
= read_gitfile(buf
->buf
);
1946 strbuf_addstr(buf
, git_dir
);
1948 if (!is_git_directory(buf
->buf
)) {
1949 gitmodules_config();
1950 sub
= submodule_from_path(null_sha1
, submodule
);
1956 strbuf_git_path(buf
, "%s/%s", "modules", sub
->name
);