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"
18 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
19 static int parallel_jobs
= 1;
20 static struct string_list changed_submodule_paths
= STRING_LIST_INIT_NODUP
;
21 static int initialized_fetch_ref_tips
;
22 static struct sha1_array ref_tips_before_fetch
;
23 static struct sha1_array ref_tips_after_fetch
;
26 * The following flag is set if the .gitmodules file is unmerged. We then
27 * disable recursion for all submodules where .git/config doesn't have a
28 * matching config entry because we can't guess what might be configured in
29 * .gitmodules unless the user resolves the conflict. When a command line
30 * option is given (which always overrides configuration) this flag will be
33 static int gitmodules_is_unmerged
;
36 * This flag is set if the .gitmodules file had unstaged modifications on
37 * startup. This must be checked before allowing modifications to the
38 * .gitmodules file with the intention to stage them later, because when
39 * continuing we would stage the modifications the user didn't stage herself
40 * too. That might change in a future version when we learn to stage the
41 * changes we do ourselves without staging any previous modifications.
43 static int gitmodules_is_modified
;
45 int is_staging_gitmodules_ok(void)
47 return !gitmodules_is_modified
;
51 * Try to update the "path" entry in the "submodule.<name>" section of the
52 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
53 * with the correct path=<oldpath> setting was found and we could update it.
55 int update_path_in_gitmodules(const char *oldpath
, const char *newpath
)
57 struct strbuf entry
= STRBUF_INIT
;
58 const struct submodule
*submodule
;
60 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
63 if (gitmodules_is_unmerged
)
64 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
66 submodule
= submodule_from_path(null_sha1
, oldpath
);
67 if (!submodule
|| !submodule
->name
) {
68 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
71 strbuf_addstr(&entry
, "submodule.");
72 strbuf_addstr(&entry
, submodule
->name
);
73 strbuf_addstr(&entry
, ".path");
74 if (git_config_set_in_file_gently(".gitmodules", entry
.buf
, newpath
) < 0) {
75 /* Maybe the user already did that, don't error out here */
76 warning(_("Could not update .gitmodules entry %s"), entry
.buf
);
77 strbuf_release(&entry
);
80 strbuf_release(&entry
);
85 * Try to remove the "submodule.<name>" section from .gitmodules where the given
86 * path is configured. Return 0 only if a .gitmodules file was found, a section
87 * with the correct path=<path> setting was found and we could remove it.
89 int remove_path_from_gitmodules(const char *path
)
91 struct strbuf sect
= STRBUF_INIT
;
92 const struct submodule
*submodule
;
94 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
97 if (gitmodules_is_unmerged
)
98 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
100 submodule
= submodule_from_path(null_sha1
, path
);
101 if (!submodule
|| !submodule
->name
) {
102 warning(_("Could not find section in .gitmodules where path=%s"), path
);
105 strbuf_addstr(§
, "submodule.");
106 strbuf_addstr(§
, submodule
->name
);
107 if (git_config_rename_section_in_file(".gitmodules", sect
.buf
, NULL
) < 0) {
108 /* Maybe the user already did that, don't error out here */
109 warning(_("Could not remove .gitmodules entry for %s"), path
);
110 strbuf_release(§
);
113 strbuf_release(§
);
117 void stage_updated_gitmodules(void)
119 if (add_file_to_cache(".gitmodules", 0))
120 die(_("staging updated .gitmodules failed"));
123 static int add_submodule_odb(const char *path
)
125 struct strbuf objects_directory
= STRBUF_INIT
;
128 ret
= strbuf_git_path_submodule(&objects_directory
, path
, "objects/");
131 if (!is_directory(objects_directory
.buf
)) {
135 add_to_alternates_memory(objects_directory
.buf
);
137 strbuf_release(&objects_directory
);
141 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
144 const struct submodule
*submodule
= submodule_from_path(null_sha1
, path
);
146 if (submodule
->ignore
)
147 handle_ignore_submodules_arg(diffopt
, submodule
->ignore
);
148 else if (gitmodules_is_unmerged
)
149 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
153 int submodule_config(const char *var
, const char *value
, void *cb
)
155 if (!strcmp(var
, "submodule.fetchjobs")) {
156 parallel_jobs
= git_config_int(var
, value
);
157 if (parallel_jobs
< 0)
158 die(_("negative values not allowed for submodule.fetchJobs"));
160 } else if (starts_with(var
, "submodule."))
161 return parse_submodule_config_option(var
, value
);
162 else if (!strcmp(var
, "fetch.recursesubmodules")) {
163 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
169 void gitmodules_config(void)
171 const char *work_tree
= get_git_work_tree();
173 struct strbuf gitmodules_path
= STRBUF_INIT
;
175 strbuf_addstr(&gitmodules_path
, work_tree
);
176 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
177 if (read_cache() < 0)
178 die("index file corrupt");
179 pos
= cache_name_pos(".gitmodules", 11);
180 if (pos
< 0) { /* .gitmodules not found or isn't merged */
182 if (active_nr
> pos
) { /* there is a .gitmodules */
183 const struct cache_entry
*ce
= active_cache
[pos
];
184 if (ce_namelen(ce
) == 11 &&
185 !memcmp(ce
->name
, ".gitmodules", 11))
186 gitmodules_is_unmerged
= 1;
188 } else if (pos
< active_nr
) {
190 if (lstat(".gitmodules", &st
) == 0 &&
191 ce_match_stat(active_cache
[pos
], &st
, 0) & DATA_CHANGED
)
192 gitmodules_is_modified
= 1;
195 if (!gitmodules_is_unmerged
)
196 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
197 strbuf_release(&gitmodules_path
);
201 int parse_submodule_update_strategy(const char *value
,
202 struct submodule_update_strategy
*dst
)
204 free((void*)dst
->command
);
206 if (!strcmp(value
, "none"))
207 dst
->type
= SM_UPDATE_NONE
;
208 else if (!strcmp(value
, "checkout"))
209 dst
->type
= SM_UPDATE_CHECKOUT
;
210 else if (!strcmp(value
, "rebase"))
211 dst
->type
= SM_UPDATE_REBASE
;
212 else if (!strcmp(value
, "merge"))
213 dst
->type
= SM_UPDATE_MERGE
;
214 else if (skip_prefix(value
, "!", &value
)) {
215 dst
->type
= SM_UPDATE_COMMAND
;
216 dst
->command
= xstrdup(value
);
222 const char *submodule_strategy_to_string(const struct submodule_update_strategy
*s
)
224 struct strbuf sb
= STRBUF_INIT
;
226 case SM_UPDATE_CHECKOUT
:
228 case SM_UPDATE_MERGE
:
230 case SM_UPDATE_REBASE
:
234 case SM_UPDATE_UNSPECIFIED
:
236 case SM_UPDATE_COMMAND
:
237 strbuf_addf(&sb
, "!%s", s
->command
);
238 return strbuf_detach(&sb
, NULL
);
243 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
246 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
247 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
248 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
250 if (!strcmp(arg
, "all"))
251 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
252 else if (!strcmp(arg
, "untracked"))
253 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
254 else if (!strcmp(arg
, "dirty"))
255 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
256 else if (strcmp(arg
, "none"))
257 die("bad --ignore-submodules argument: %s", arg
);
260 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
261 struct commit
*left
, struct commit
*right
,
262 struct commit_list
*merge_bases
)
264 struct commit_list
*list
;
266 init_revisions(rev
, NULL
);
267 setup_revisions(0, NULL
, rev
, NULL
);
269 rev
->first_parent_only
= 1;
270 left
->object
.flags
|= SYMMETRIC_LEFT
;
271 add_pending_object(rev
, &left
->object
, path
);
272 add_pending_object(rev
, &right
->object
, path
);
273 for (list
= merge_bases
; list
; list
= list
->next
) {
274 list
->item
->object
.flags
|= UNINTERESTING
;
275 add_pending_object(rev
, &list
->item
->object
,
276 oid_to_hex(&list
->item
->object
.oid
));
278 return prepare_revision_walk(rev
);
281 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
282 const char *line_prefix
,
283 const char *del
, const char *add
, const char *reset
)
285 static const char format
[] = " %m %s";
286 struct strbuf sb
= STRBUF_INIT
;
287 struct commit
*commit
;
289 while ((commit
= get_revision(rev
))) {
290 struct pretty_print_context ctx
= {0};
291 ctx
.date_mode
= rev
->date_mode
;
292 ctx
.output_encoding
= get_log_output_encoding();
293 strbuf_setlen(&sb
, 0);
294 strbuf_addstr(&sb
, line_prefix
);
295 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
297 strbuf_addstr(&sb
, del
);
300 strbuf_addstr(&sb
, add
);
301 format_commit_message(commit
, format
, &sb
, &ctx
);
303 strbuf_addstr(&sb
, reset
);
304 strbuf_addch(&sb
, '\n');
305 fprintf(f
, "%s", sb
.buf
);
310 /* Helper function to display the submodule header line prior to the full
311 * summary output. If it can locate the submodule objects directory it will
312 * attempt to lookup both the left and right commits and put them into the
313 * left and right pointers.
315 static void show_submodule_header(FILE *f
, const char *path
,
316 const char *line_prefix
,
317 struct object_id
*one
, struct object_id
*two
,
318 unsigned dirty_submodule
, const char *meta
,
320 struct commit
**left
, struct commit
**right
,
321 struct commit_list
**merge_bases
)
323 const char *message
= NULL
;
324 struct strbuf sb
= STRBUF_INIT
;
325 int fast_forward
= 0, fast_backward
= 0;
327 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
328 fprintf(f
, "%sSubmodule %s contains untracked content\n",
330 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
331 fprintf(f
, "%sSubmodule %s contains modified content\n",
334 if (is_null_oid(one
))
335 message
= "(new submodule)";
336 else if (is_null_oid(two
))
337 message
= "(submodule deleted)";
339 if (add_submodule_odb(path
)) {
341 message
= "(not initialized)";
346 * Attempt to lookup the commit references, and determine if this is
347 * a fast forward or fast backwards update.
349 *left
= lookup_commit_reference(one
->hash
);
350 *right
= lookup_commit_reference(two
->hash
);
353 * Warn about missing commits in the submodule project, but only if
356 if ((!is_null_oid(one
) && !*left
) ||
357 (!is_null_oid(two
) && !*right
))
358 message
= "(commits not present)";
360 *merge_bases
= get_merge_bases(*left
, *right
);
362 if ((*merge_bases
)->item
== *left
)
364 else if ((*merge_bases
)->item
== *right
)
368 if (!oidcmp(one
, two
)) {
374 strbuf_addf(&sb
, "%s%sSubmodule %s ", line_prefix
, meta
, path
);
375 strbuf_add_unique_abbrev(&sb
, one
->hash
, DEFAULT_ABBREV
);
376 strbuf_addstr(&sb
, (fast_backward
|| fast_forward
) ? ".." : "...");
377 strbuf_add_unique_abbrev(&sb
, two
->hash
, DEFAULT_ABBREV
);
379 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
381 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
382 fwrite(sb
.buf
, sb
.len
, 1, f
);
387 void show_submodule_summary(FILE *f
, const char *path
,
388 const char *line_prefix
,
389 struct object_id
*one
, struct object_id
*two
,
390 unsigned dirty_submodule
, const char *meta
,
391 const char *del
, const char *add
, const char *reset
)
394 struct commit
*left
= NULL
, *right
= NULL
;
395 struct commit_list
*merge_bases
= NULL
;
397 show_submodule_header(f
, path
, line_prefix
, one
, two
, dirty_submodule
,
398 meta
, reset
, &left
, &right
, &merge_bases
);
401 * If we don't have both a left and a right pointer, there is no
402 * reason to try and display a summary. The header line should contain
403 * all the information the user needs.
408 /* Treat revision walker failure the same as missing commits */
409 if (prepare_submodule_summary(&rev
, path
, left
, right
, merge_bases
)) {
410 fprintf(f
, "%s(revision walker failed)\n", line_prefix
);
414 print_submodule_summary(&rev
, f
, line_prefix
, del
, add
, reset
);
418 free_commit_list(merge_bases
);
419 clear_commit_marks(left
, ~0);
420 clear_commit_marks(right
, ~0);
423 void show_submodule_inline_diff(FILE *f
, const char *path
,
424 const char *line_prefix
,
425 struct object_id
*one
, struct object_id
*two
,
426 unsigned dirty_submodule
, const char *meta
,
427 const char *del
, const char *add
, const char *reset
,
428 const struct diff_options
*o
)
430 const struct object_id
*old
= &empty_tree_oid
, *new = &empty_tree_oid
;
431 struct commit
*left
= NULL
, *right
= NULL
;
432 struct commit_list
*merge_bases
= NULL
;
433 struct strbuf submodule_dir
= STRBUF_INIT
;
434 struct child_process cp
= CHILD_PROCESS_INIT
;
436 show_submodule_header(f
, path
, line_prefix
, one
, two
, dirty_submodule
,
437 meta
, reset
, &left
, &right
, &merge_bases
);
439 /* We need a valid left and right commit to display a difference */
440 if (!(left
|| is_null_oid(one
)) ||
441 !(right
|| is_null_oid(two
)))
452 cp
.out
= dup(fileno(f
));
455 /* TODO: other options may need to be passed here. */
456 argv_array_push(&cp
.args
, "diff");
457 argv_array_pushf(&cp
.args
, "--line-prefix=%s", line_prefix
);
458 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
459 argv_array_pushf(&cp
.args
, "--src-prefix=%s%s/",
461 argv_array_pushf(&cp
.args
, "--dst-prefix=%s%s/",
464 argv_array_pushf(&cp
.args
, "--src-prefix=%s%s/",
466 argv_array_pushf(&cp
.args
, "--dst-prefix=%s%s/",
469 argv_array_push(&cp
.args
, oid_to_hex(old
));
471 * If the submodule has modified content, we will diff against the
472 * work tree, under the assumption that the user has asked for the
473 * diff format and wishes to actually see all differences even if they
474 * haven't yet been committed to the submodule yet.
476 if (!(dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
))
477 argv_array_push(&cp
.args
, oid_to_hex(new));
479 if (run_command(&cp
))
480 fprintf(f
, "(diff failed)\n");
483 strbuf_release(&submodule_dir
);
485 free_commit_list(merge_bases
);
487 clear_commit_marks(left
, ~0);
489 clear_commit_marks(right
, ~0);
492 void set_config_fetch_recurse_submodules(int value
)
494 config_fetch_recurse_submodules
= value
;
497 static int has_remote(const char *refname
, const struct object_id
*oid
,
498 int flags
, void *cb_data
)
503 static int append_sha1_to_argv(const unsigned char sha1
[20], void *data
)
505 struct argv_array
*argv
= data
;
506 argv_array_push(argv
, sha1_to_hex(sha1
));
510 static int check_has_commit(const unsigned char sha1
[20], void *data
)
512 int *has_commit
= data
;
514 if (!lookup_commit_reference(sha1
))
520 static int submodule_has_commits(const char *path
, struct sha1_array
*commits
)
524 if (add_submodule_odb(path
))
527 sha1_array_for_each_unique(commits
, check_has_commit
, &has_commit
);
531 static int submodule_needs_pushing(const char *path
, struct sha1_array
*commits
)
533 if (!submodule_has_commits(path
, commits
))
535 * NOTE: We do consider it safe to return "no" here. The
536 * correct answer would be "We do not know" instead of
537 * "No push needed", but it is quite hard to change
538 * the submodule pointer without having the submodule
539 * around. If a user did however change the submodules
540 * without having the submodule around, this indicates
541 * an expert who knows what they are doing or a
542 * maintainer integrating work from other people. In
543 * both cases it should be safe to skip this check.
547 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
548 struct child_process cp
= CHILD_PROCESS_INIT
;
549 struct strbuf buf
= STRBUF_INIT
;
550 int needs_pushing
= 0;
552 argv_array_push(&cp
.args
, "rev-list");
553 sha1_array_for_each_unique(commits
, append_sha1_to_argv
, &cp
.args
);
554 argv_array_pushl(&cp
.args
, "--not", "--remotes", "-n", "1" , NULL
);
556 prepare_submodule_repo_env(&cp
.env_array
);
561 if (start_command(&cp
))
562 die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s",
564 if (strbuf_read(&buf
, cp
.out
, 41))
568 strbuf_release(&buf
);
569 return needs_pushing
;
575 static struct sha1_array
*submodule_commits(struct string_list
*submodules
,
578 struct string_list_item
*item
;
580 item
= string_list_insert(submodules
, path
);
582 return (struct sha1_array
*) item
->util
;
584 /* NEEDSWORK: should we have sha1_array_init()? */
585 item
->util
= xcalloc(1, sizeof(struct sha1_array
));
586 return (struct sha1_array
*) item
->util
;
589 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
590 struct diff_options
*options
,
594 struct string_list
*submodules
= data
;
596 for (i
= 0; i
< q
->nr
; i
++) {
597 struct diff_filepair
*p
= q
->queue
[i
];
598 struct sha1_array
*commits
;
599 if (!S_ISGITLINK(p
->two
->mode
))
601 commits
= submodule_commits(submodules
, p
->two
->path
);
602 sha1_array_append(commits
, p
->two
->oid
.hash
);
606 static void find_unpushed_submodule_commits(struct commit
*commit
,
607 struct string_list
*needs_pushing
)
611 init_revisions(&rev
, NULL
);
612 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
613 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
614 rev
.diffopt
.format_callback_data
= needs_pushing
;
615 diff_tree_combined_merge(commit
, 1, &rev
);
618 static void free_submodules_sha1s(struct string_list
*submodules
)
620 struct string_list_item
*item
;
621 for_each_string_list_item(item
, submodules
)
622 sha1_array_clear((struct sha1_array
*) item
->util
);
623 string_list_clear(submodules
, 1);
626 int find_unpushed_submodules(struct sha1_array
*commits
,
627 const char *remotes_name
, struct string_list
*needs_pushing
)
630 struct commit
*commit
;
631 struct string_list submodules
= STRING_LIST_INIT_DUP
;
632 struct string_list_item
*submodule
;
633 struct argv_array argv
= ARGV_ARRAY_INIT
;
635 init_revisions(&rev
, NULL
);
637 /* argv.argv[0] will be ignored by setup_revisions */
638 argv_array_push(&argv
, "find_unpushed_submodules");
639 sha1_array_for_each_unique(commits
, append_sha1_to_argv
, &argv
);
640 argv_array_push(&argv
, "--not");
641 argv_array_pushf(&argv
, "--remotes=%s", remotes_name
);
643 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
644 if (prepare_revision_walk(&rev
))
645 die("revision walk setup failed");
647 while ((commit
= get_revision(&rev
)) != NULL
)
648 find_unpushed_submodule_commits(commit
, &submodules
);
650 reset_revision_walk();
651 argv_array_clear(&argv
);
653 for_each_string_list_item(submodule
, &submodules
) {
654 struct sha1_array
*commits
= (struct sha1_array
*) submodule
->util
;
656 if (submodule_needs_pushing(submodule
->string
, commits
))
657 string_list_insert(needs_pushing
, submodule
->string
);
659 free_submodules_sha1s(&submodules
);
661 return needs_pushing
->nr
;
664 static int push_submodule(const char *path
)
666 if (add_submodule_odb(path
))
669 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
670 struct child_process cp
= CHILD_PROCESS_INIT
;
671 const char *argv
[] = {"push", NULL
};
674 prepare_submodule_repo_env(&cp
.env_array
);
678 if (run_command(&cp
))
686 int push_unpushed_submodules(struct sha1_array
*commits
, const char *remotes_name
)
689 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
691 if (!find_unpushed_submodules(commits
, remotes_name
, &needs_pushing
))
694 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
695 const char *path
= needs_pushing
.items
[i
].string
;
696 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
697 if (!push_submodule(path
)) {
698 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
703 string_list_clear(&needs_pushing
, 0);
708 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
711 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
712 /* Even if the submodule is checked out and the commit is
713 * present, make sure it is reachable from a ref. */
714 struct child_process cp
= CHILD_PROCESS_INIT
;
715 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
716 struct strbuf buf
= STRBUF_INIT
;
718 argv
[3] = sha1_to_hex(sha1
);
720 prepare_submodule_repo_env(&cp
.env_array
);
724 if (!capture_command(&cp
, &buf
, 1024) && !buf
.len
)
727 strbuf_release(&buf
);
732 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
733 struct diff_options
*options
,
737 for (i
= 0; i
< q
->nr
; i
++) {
738 struct diff_filepair
*p
= q
->queue
[i
];
739 if (!S_ISGITLINK(p
->two
->mode
))
742 if (S_ISGITLINK(p
->one
->mode
)) {
743 /* NEEDSWORK: We should honor the name configured in
744 * the .gitmodules file of the commit we are examining
745 * here to be able to correctly follow submodules
746 * being moved around. */
747 struct string_list_item
*path
;
748 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
749 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->oid
.hash
))
750 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
752 /* Submodule is new or was moved here */
753 /* NEEDSWORK: When the .git directories of submodules
754 * live inside the superprojects .git directory some
755 * day we should fetch new submodules directly into
756 * that location too when config or options request
757 * that so they can be checked out from there. */
763 static int add_sha1_to_array(const char *ref
, const struct object_id
*oid
,
764 int flags
, void *data
)
766 sha1_array_append(data
, oid
->hash
);
770 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
772 if (!initialized_fetch_ref_tips
) {
773 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
774 initialized_fetch_ref_tips
= 1;
777 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
780 static int add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
782 argv_array_push(data
, sha1_to_hex(sha1
));
786 static void calculate_changed_submodule_paths(void)
789 struct commit
*commit
;
790 struct argv_array argv
= ARGV_ARRAY_INIT
;
792 /* No need to check if there are no submodules configured */
793 if (!submodule_from_path(NULL
, NULL
))
796 init_revisions(&rev
, NULL
);
797 argv_array_push(&argv
, "--"); /* argv[0] program name */
798 sha1_array_for_each_unique(&ref_tips_after_fetch
,
799 add_sha1_to_argv
, &argv
);
800 argv_array_push(&argv
, "--not");
801 sha1_array_for_each_unique(&ref_tips_before_fetch
,
802 add_sha1_to_argv
, &argv
);
803 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
804 if (prepare_revision_walk(&rev
))
805 die("revision walk setup failed");
808 * Collect all submodules (whether checked out or not) for which new
809 * commits have been recorded upstream in "changed_submodule_paths".
811 while ((commit
= get_revision(&rev
))) {
812 struct commit_list
*parent
= commit
->parents
;
814 struct diff_options diff_opts
;
815 diff_setup(&diff_opts
);
816 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
817 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
818 diff_opts
.format_callback
= submodule_collect_changed_cb
;
819 diff_setup_done(&diff_opts
);
820 diff_tree_sha1(parent
->item
->object
.oid
.hash
, commit
->object
.oid
.hash
, "", &diff_opts
);
821 diffcore_std(&diff_opts
);
822 diff_flush(&diff_opts
);
823 parent
= parent
->next
;
827 argv_array_clear(&argv
);
828 sha1_array_clear(&ref_tips_before_fetch
);
829 sha1_array_clear(&ref_tips_after_fetch
);
830 initialized_fetch_ref_tips
= 0;
833 struct submodule_parallel_fetch
{
835 struct argv_array args
;
836 const char *work_tree
;
838 int command_line_option
;
842 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
844 static int get_next_submodule(struct child_process
*cp
,
845 struct strbuf
*err
, void *data
, void **task_cb
)
848 struct submodule_parallel_fetch
*spf
= data
;
850 for (; spf
->count
< active_nr
; spf
->count
++) {
851 struct strbuf submodule_path
= STRBUF_INIT
;
852 struct strbuf submodule_git_dir
= STRBUF_INIT
;
853 struct strbuf submodule_prefix
= STRBUF_INIT
;
854 const struct cache_entry
*ce
= active_cache
[spf
->count
];
855 const char *git_dir
, *default_argv
;
856 const struct submodule
*submodule
;
858 if (!S_ISGITLINK(ce
->ce_mode
))
861 submodule
= submodule_from_path(null_sha1
, ce
->name
);
863 submodule
= submodule_from_name(null_sha1
, ce
->name
);
865 default_argv
= "yes";
866 if (spf
->command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
868 submodule
->fetch_recurse
!=
869 RECURSE_SUBMODULES_NONE
) {
870 if (submodule
->fetch_recurse
==
871 RECURSE_SUBMODULES_OFF
)
873 if (submodule
->fetch_recurse
==
874 RECURSE_SUBMODULES_ON_DEMAND
) {
875 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
877 default_argv
= "on-demand";
880 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
881 gitmodules_is_unmerged
)
883 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
884 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
886 default_argv
= "on-demand";
889 } else if (spf
->command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
890 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
892 default_argv
= "on-demand";
895 strbuf_addf(&submodule_path
, "%s/%s", spf
->work_tree
, ce
->name
);
896 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
897 strbuf_addf(&submodule_prefix
, "%s%s/", spf
->prefix
, ce
->name
);
898 git_dir
= read_gitfile(submodule_git_dir
.buf
);
900 git_dir
= submodule_git_dir
.buf
;
901 if (is_directory(git_dir
)) {
902 child_process_init(cp
);
903 cp
->dir
= strbuf_detach(&submodule_path
, NULL
);
904 prepare_submodule_repo_env(&cp
->env_array
);
907 strbuf_addf(err
, "Fetching submodule %s%s\n",
908 spf
->prefix
, ce
->name
);
909 argv_array_init(&cp
->args
);
910 argv_array_pushv(&cp
->args
, spf
->args
.argv
);
911 argv_array_push(&cp
->args
, default_argv
);
912 argv_array_push(&cp
->args
, "--submodule-prefix");
913 argv_array_push(&cp
->args
, submodule_prefix
.buf
);
916 strbuf_release(&submodule_path
);
917 strbuf_release(&submodule_git_dir
);
918 strbuf_release(&submodule_prefix
);
927 static int fetch_start_failure(struct strbuf
*err
,
928 void *cb
, void *task_cb
)
930 struct submodule_parallel_fetch
*spf
= cb
;
937 static int fetch_finish(int retvalue
, struct strbuf
*err
,
938 void *cb
, void *task_cb
)
940 struct submodule_parallel_fetch
*spf
= cb
;
948 int fetch_populated_submodules(const struct argv_array
*options
,
949 const char *prefix
, int command_line_option
,
950 int quiet
, int max_parallel_jobs
)
953 struct submodule_parallel_fetch spf
= SPF_INIT
;
955 spf
.work_tree
= get_git_work_tree();
956 spf
.command_line_option
= command_line_option
;
963 if (read_cache() < 0)
964 die("index file corrupt");
966 argv_array_push(&spf
.args
, "fetch");
967 for (i
= 0; i
< options
->argc
; i
++)
968 argv_array_push(&spf
.args
, options
->argv
[i
]);
969 argv_array_push(&spf
.args
, "--recurse-submodules-default");
970 /* default value, "--submodule-prefix" and its value are added later */
972 if (max_parallel_jobs
< 0)
973 max_parallel_jobs
= parallel_jobs
;
975 calculate_changed_submodule_paths();
976 run_processes_parallel(max_parallel_jobs
,
982 argv_array_clear(&spf
.args
);
984 string_list_clear(&changed_submodule_paths
, 1);
988 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
991 struct child_process cp
= CHILD_PROCESS_INIT
;
992 const char *argv
[] = {
998 struct strbuf buf
= STRBUF_INIT
;
999 unsigned dirty_submodule
= 0;
1000 const char *line
, *next_line
;
1001 const char *git_dir
;
1003 strbuf_addf(&buf
, "%s/.git", path
);
1004 git_dir
= read_gitfile(buf
.buf
);
1007 if (!is_directory(git_dir
)) {
1008 strbuf_release(&buf
);
1009 /* The submodule is not checked out, so it is not modified */
1015 if (ignore_untracked
)
1019 prepare_submodule_repo_env(&cp
.env_array
);
1024 if (start_command(&cp
))
1025 die("Could not run 'git status --porcelain' in submodule %s", path
);
1027 len
= strbuf_read(&buf
, cp
.out
, 1024);
1030 if ((line
[0] == '?') && (line
[1] == '?')) {
1031 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
1032 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
1035 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
1036 if (ignore_untracked
||
1037 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
1040 next_line
= strchr(line
, '\n');
1044 len
-= (next_line
- line
);
1049 if (finish_command(&cp
))
1050 die("'git status --porcelain' failed in submodule %s", path
);
1052 strbuf_release(&buf
);
1053 return dirty_submodule
;
1056 int submodule_uses_gitfile(const char *path
)
1058 struct child_process cp
= CHILD_PROCESS_INIT
;
1059 const char *argv
[] = {
1067 struct strbuf buf
= STRBUF_INIT
;
1068 const char *git_dir
;
1070 strbuf_addf(&buf
, "%s/.git", path
);
1071 git_dir
= read_gitfile(buf
.buf
);
1073 strbuf_release(&buf
);
1076 strbuf_release(&buf
);
1078 /* Now test that all nested submodules use a gitfile too */
1080 prepare_submodule_repo_env(&cp
.env_array
);
1086 if (run_command(&cp
))
1092 int ok_to_remove_submodule(const char *path
)
1095 struct child_process cp
= CHILD_PROCESS_INIT
;
1096 const char *argv
[] = {
1100 "--ignore-submodules=none",
1103 struct strbuf buf
= STRBUF_INIT
;
1104 int ok_to_remove
= 1;
1106 if (!file_exists(path
) || is_empty_dir(path
))
1109 if (!submodule_uses_gitfile(path
))
1113 prepare_submodule_repo_env(&cp
.env_array
);
1118 if (start_command(&cp
))
1119 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path
);
1121 len
= strbuf_read(&buf
, cp
.out
, 1024);
1126 if (finish_command(&cp
))
1127 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path
);
1129 strbuf_release(&buf
);
1130 return ok_to_remove
;
1133 static int find_first_merges(struct object_array
*result
, const char *path
,
1134 struct commit
*a
, struct commit
*b
)
1137 struct object_array merges
= OBJECT_ARRAY_INIT
;
1138 struct commit
*commit
;
1139 int contains_another
;
1141 char merged_revision
[42];
1142 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1143 "--all", merged_revision
, NULL
};
1144 struct rev_info revs
;
1145 struct setup_revision_opt rev_opts
;
1147 memset(result
, 0, sizeof(struct object_array
));
1148 memset(&rev_opts
, 0, sizeof(rev_opts
));
1150 /* get all revisions that merge commit a */
1151 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1152 oid_to_hex(&a
->object
.oid
));
1153 init_revisions(&revs
, NULL
);
1154 rev_opts
.submodule
= path
;
1155 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1157 /* save all revisions from the above list that contain b */
1158 if (prepare_revision_walk(&revs
))
1159 die("revision walk setup failed");
1160 while ((commit
= get_revision(&revs
)) != NULL
) {
1161 struct object
*o
= &(commit
->object
);
1162 if (in_merge_bases(b
, commit
))
1163 add_object_array(o
, NULL
, &merges
);
1165 reset_revision_walk();
1167 /* Now we've got all merges that contain a and b. Prune all
1168 * merges that contain another found merge and save them in
1171 for (i
= 0; i
< merges
.nr
; i
++) {
1172 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1174 contains_another
= 0;
1175 for (j
= 0; j
< merges
.nr
; j
++) {
1176 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1177 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
1178 contains_another
= 1;
1183 if (!contains_another
)
1184 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1187 free(merges
.objects
);
1191 static void print_commit(struct commit
*commit
)
1193 struct strbuf sb
= STRBUF_INIT
;
1194 struct pretty_print_context ctx
= {0};
1195 ctx
.date_mode
.type
= DATE_NORMAL
;
1196 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1197 fprintf(stderr
, "%s\n", sb
.buf
);
1198 strbuf_release(&sb
);
1201 #define MERGE_WARNING(path, msg) \
1202 warning("Failed to merge submodule %s (%s)", path, msg);
1204 int merge_submodule(unsigned char result
[20], const char *path
,
1205 const unsigned char base
[20], const unsigned char a
[20],
1206 const unsigned char b
[20], int search
)
1208 struct commit
*commit_base
, *commit_a
, *commit_b
;
1210 struct object_array merges
;
1214 /* store a in result in case we fail */
1217 /* we can not handle deletion conflicts */
1218 if (is_null_sha1(base
))
1220 if (is_null_sha1(a
))
1222 if (is_null_sha1(b
))
1225 if (add_submodule_odb(path
)) {
1226 MERGE_WARNING(path
, "not checked out");
1230 if (!(commit_base
= lookup_commit_reference(base
)) ||
1231 !(commit_a
= lookup_commit_reference(a
)) ||
1232 !(commit_b
= lookup_commit_reference(b
))) {
1233 MERGE_WARNING(path
, "commits not present");
1237 /* check whether both changes are forward */
1238 if (!in_merge_bases(commit_base
, commit_a
) ||
1239 !in_merge_bases(commit_base
, commit_b
)) {
1240 MERGE_WARNING(path
, "commits don't follow merge-base");
1244 /* Case #1: a is contained in b or vice versa */
1245 if (in_merge_bases(commit_a
, commit_b
)) {
1249 if (in_merge_bases(commit_b
, commit_a
)) {
1255 * Case #2: There are one or more merges that contain a and b in
1256 * the submodule. If there is only one, then present it as a
1257 * suggestion to the user, but leave it marked unmerged so the
1258 * user needs to confirm the resolution.
1261 /* Skip the search if makes no sense to the calling context. */
1265 /* find commit which merges them */
1266 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
1267 switch (parent_count
) {
1269 MERGE_WARNING(path
, "merge following commits not found");
1273 MERGE_WARNING(path
, "not fast-forward");
1274 fprintf(stderr
, "Found a possible merge resolution "
1275 "for the submodule:\n");
1276 print_commit((struct commit
*) merges
.objects
[0].item
);
1278 "If this is correct simply add it to the index "
1281 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1282 "which will accept this suggestion.\n",
1283 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1287 MERGE_WARNING(path
, "multiple merges found");
1288 for (i
= 0; i
< merges
.nr
; i
++)
1289 print_commit((struct commit
*) merges
.objects
[i
].item
);
1292 free(merges
.objects
);
1296 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1297 void connect_work_tree_and_git_dir(const char *work_tree
, const char *git_dir
)
1299 struct strbuf file_name
= STRBUF_INIT
;
1300 struct strbuf rel_path
= STRBUF_INIT
;
1301 const char *real_work_tree
= xstrdup(real_path(work_tree
));
1303 /* Update gitfile */
1304 strbuf_addf(&file_name
, "%s/.git", work_tree
);
1305 write_file(file_name
.buf
, "gitdir: %s",
1306 relative_path(git_dir
, real_work_tree
, &rel_path
));
1308 /* Update core.worktree setting */
1309 strbuf_reset(&file_name
);
1310 strbuf_addf(&file_name
, "%s/config", git_dir
);
1311 git_config_set_in_file(file_name
.buf
, "core.worktree",
1312 relative_path(real_work_tree
, git_dir
,
1315 strbuf_release(&file_name
);
1316 strbuf_release(&rel_path
);
1317 free((void *)real_work_tree
);
1320 int parallel_submodules(void)
1322 return parallel_jobs
;
1325 void prepare_submodule_repo_env(struct argv_array
*out
)
1327 const char * const *var
;
1329 for (var
= local_repo_env
; *var
; var
++) {
1330 if (strcmp(*var
, CONFIG_DATA_ENVIRONMENT
))
1331 argv_array_push(out
, *var
);
1333 argv_array_push(out
, "GIT_DIR=.git");