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"
17 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
18 static int parallel_jobs
= 1;
19 static struct string_list changed_submodule_paths
;
20 static int initialized_fetch_ref_tips
;
21 static struct sha1_array ref_tips_before_fetch
;
22 static struct sha1_array ref_tips_after_fetch
;
25 * The following flag is set if the .gitmodules file is unmerged. We then
26 * disable recursion for all submodules where .git/config doesn't have a
27 * matching config entry because we can't guess what might be configured in
28 * .gitmodules unless the user resolves the conflict. When a command line
29 * option is given (which always overrides configuration) this flag will be
32 static int gitmodules_is_unmerged
;
35 * This flag is set if the .gitmodules file had unstaged modifications on
36 * startup. This must be checked before allowing modifications to the
37 * .gitmodules file with the intention to stage them later, because when
38 * continuing we would stage the modifications the user didn't stage herself
39 * too. That might change in a future version when we learn to stage the
40 * changes we do ourselves without staging any previous modifications.
42 static int gitmodules_is_modified
;
44 int is_staging_gitmodules_ok(void)
46 return !gitmodules_is_modified
;
50 * Try to update the "path" entry in the "submodule.<name>" section of the
51 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
52 * with the correct path=<oldpath> setting was found and we could update it.
54 int update_path_in_gitmodules(const char *oldpath
, const char *newpath
)
56 struct strbuf entry
= STRBUF_INIT
;
57 const struct submodule
*submodule
;
59 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
62 if (gitmodules_is_unmerged
)
63 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
65 submodule
= submodule_from_path(null_sha1
, oldpath
);
66 if (!submodule
|| !submodule
->name
) {
67 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
70 strbuf_addstr(&entry
, "submodule.");
71 strbuf_addstr(&entry
, submodule
->name
);
72 strbuf_addstr(&entry
, ".path");
73 if (git_config_set_in_file_gently(".gitmodules", entry
.buf
, newpath
) < 0) {
74 /* Maybe the user already did that, don't error out here */
75 warning(_("Could not update .gitmodules entry %s"), entry
.buf
);
76 strbuf_release(&entry
);
79 strbuf_release(&entry
);
84 * Try to remove the "submodule.<name>" section from .gitmodules where the given
85 * path is configured. Return 0 only if a .gitmodules file was found, a section
86 * with the correct path=<path> setting was found and we could remove it.
88 int remove_path_from_gitmodules(const char *path
)
90 struct strbuf sect
= STRBUF_INIT
;
91 const struct submodule
*submodule
;
93 if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */
96 if (gitmodules_is_unmerged
)
97 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
99 submodule
= submodule_from_path(null_sha1
, path
);
100 if (!submodule
|| !submodule
->name
) {
101 warning(_("Could not find section in .gitmodules where path=%s"), path
);
104 strbuf_addstr(§
, "submodule.");
105 strbuf_addstr(§
, submodule
->name
);
106 if (git_config_rename_section_in_file(".gitmodules", sect
.buf
, NULL
) < 0) {
107 /* Maybe the user already did that, don't error out here */
108 warning(_("Could not remove .gitmodules entry for %s"), path
);
109 strbuf_release(§
);
112 strbuf_release(§
);
116 void stage_updated_gitmodules(void)
118 if (add_file_to_cache(".gitmodules", 0))
119 die(_("staging updated .gitmodules failed"));
122 static int add_submodule_odb(const char *path
)
124 struct strbuf objects_directory
= STRBUF_INIT
;
125 struct alternate_object_database
*alt_odb
;
129 strbuf_git_path_submodule(&objects_directory
, path
, "objects/");
130 if (!is_directory(objects_directory
.buf
)) {
134 /* avoid adding it twice */
136 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
137 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
138 !strncmp(alt_odb
->base
, objects_directory
.buf
,
139 objects_directory
.len
))
142 alloc
= st_add(objects_directory
.len
, 42); /* for "12/345..." sha1 */
143 alt_odb
= xmalloc(st_add(sizeof(*alt_odb
), alloc
));
144 alt_odb
->next
= alt_odb_list
;
145 xsnprintf(alt_odb
->base
, alloc
, "%s", objects_directory
.buf
);
146 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
147 alt_odb
->name
[2] = '/';
148 alt_odb
->name
[40] = '\0';
149 alt_odb
->name
[41] = '\0';
150 alt_odb_list
= alt_odb
;
152 /* add possible alternates from the submodule */
153 read_info_alternates(objects_directory
.buf
, 0);
155 strbuf_release(&objects_directory
);
159 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
162 const struct submodule
*submodule
= submodule_from_path(null_sha1
, path
);
164 if (submodule
->ignore
)
165 handle_ignore_submodules_arg(diffopt
, submodule
->ignore
);
166 else if (gitmodules_is_unmerged
)
167 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
171 int submodule_config(const char *var
, const char *value
, void *cb
)
173 if (!strcmp(var
, "submodule.fetchjobs")) {
174 parallel_jobs
= git_config_int(var
, value
);
175 if (parallel_jobs
< 0)
176 die(_("negative values not allowed for submodule.fetchJobs"));
178 } else if (starts_with(var
, "submodule."))
179 return parse_submodule_config_option(var
, value
);
180 else if (!strcmp(var
, "fetch.recursesubmodules")) {
181 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
187 void gitmodules_config(void)
189 const char *work_tree
= get_git_work_tree();
191 struct strbuf gitmodules_path
= STRBUF_INIT
;
193 strbuf_addstr(&gitmodules_path
, work_tree
);
194 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
195 if (read_cache() < 0)
196 die("index file corrupt");
197 pos
= cache_name_pos(".gitmodules", 11);
198 if (pos
< 0) { /* .gitmodules not found or isn't merged */
200 if (active_nr
> pos
) { /* there is a .gitmodules */
201 const struct cache_entry
*ce
= active_cache
[pos
];
202 if (ce_namelen(ce
) == 11 &&
203 !memcmp(ce
->name
, ".gitmodules", 11))
204 gitmodules_is_unmerged
= 1;
206 } else if (pos
< active_nr
) {
208 if (lstat(".gitmodules", &st
) == 0 &&
209 ce_match_stat(active_cache
[pos
], &st
, 0) & DATA_CHANGED
)
210 gitmodules_is_modified
= 1;
213 if (!gitmodules_is_unmerged
)
214 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
215 strbuf_release(&gitmodules_path
);
219 int parse_submodule_update_strategy(const char *value
,
220 struct submodule_update_strategy
*dst
)
222 free((void*)dst
->command
);
224 if (!strcmp(value
, "none"))
225 dst
->type
= SM_UPDATE_NONE
;
226 else if (!strcmp(value
, "checkout"))
227 dst
->type
= SM_UPDATE_CHECKOUT
;
228 else if (!strcmp(value
, "rebase"))
229 dst
->type
= SM_UPDATE_REBASE
;
230 else if (!strcmp(value
, "merge"))
231 dst
->type
= SM_UPDATE_MERGE
;
232 else if (skip_prefix(value
, "!", &value
)) {
233 dst
->type
= SM_UPDATE_COMMAND
;
234 dst
->command
= xstrdup(value
);
240 const char *submodule_strategy_to_string(const struct submodule_update_strategy
*s
)
242 struct strbuf sb
= STRBUF_INIT
;
244 case SM_UPDATE_CHECKOUT
:
246 case SM_UPDATE_MERGE
:
248 case SM_UPDATE_REBASE
:
252 case SM_UPDATE_UNSPECIFIED
:
254 case SM_UPDATE_COMMAND
:
255 strbuf_addf(&sb
, "!%s", s
->command
);
256 return strbuf_detach(&sb
, NULL
);
261 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
264 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
265 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
266 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
268 if (!strcmp(arg
, "all"))
269 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
270 else if (!strcmp(arg
, "untracked"))
271 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
272 else if (!strcmp(arg
, "dirty"))
273 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
274 else if (strcmp(arg
, "none"))
275 die("bad --ignore-submodules argument: %s", arg
);
278 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
279 struct commit
*left
, struct commit
*right
,
280 int *fast_forward
, int *fast_backward
)
282 struct commit_list
*merge_bases
, *list
;
284 init_revisions(rev
, NULL
);
285 setup_revisions(0, NULL
, rev
, NULL
);
287 rev
->first_parent_only
= 1;
288 left
->object
.flags
|= SYMMETRIC_LEFT
;
289 add_pending_object(rev
, &left
->object
, path
);
290 add_pending_object(rev
, &right
->object
, path
);
291 merge_bases
= get_merge_bases(left
, right
);
293 if (merge_bases
->item
== left
)
295 else if (merge_bases
->item
== right
)
298 for (list
= merge_bases
; list
; list
= list
->next
) {
299 list
->item
->object
.flags
|= UNINTERESTING
;
300 add_pending_object(rev
, &list
->item
->object
,
301 oid_to_hex(&list
->item
->object
.oid
));
303 return prepare_revision_walk(rev
);
306 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
307 const char *line_prefix
,
308 const char *del
, const char *add
, const char *reset
)
310 static const char format
[] = " %m %s";
311 struct strbuf sb
= STRBUF_INIT
;
312 struct commit
*commit
;
314 while ((commit
= get_revision(rev
))) {
315 struct pretty_print_context ctx
= {0};
316 ctx
.date_mode
= rev
->date_mode
;
317 ctx
.output_encoding
= get_log_output_encoding();
318 strbuf_setlen(&sb
, 0);
319 strbuf_addstr(&sb
, line_prefix
);
320 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
322 strbuf_addstr(&sb
, del
);
325 strbuf_addstr(&sb
, add
);
326 format_commit_message(commit
, format
, &sb
, &ctx
);
328 strbuf_addstr(&sb
, reset
);
329 strbuf_addch(&sb
, '\n');
330 fprintf(f
, "%s", sb
.buf
);
335 void show_submodule_summary(FILE *f
, const char *path
,
336 const char *line_prefix
,
337 unsigned char one
[20], unsigned char two
[20],
338 unsigned dirty_submodule
, const char *meta
,
339 const char *del
, const char *add
, const char *reset
)
342 struct commit
*left
= NULL
, *right
= NULL
;
343 const char *message
= NULL
;
344 struct strbuf sb
= STRBUF_INIT
;
345 int fast_forward
= 0, fast_backward
= 0;
347 if (is_null_sha1(two
))
348 message
= "(submodule deleted)";
349 else if (add_submodule_odb(path
))
350 message
= "(not checked out)";
351 else if (is_null_sha1(one
))
352 message
= "(new submodule)";
353 else if (!(left
= lookup_commit_reference(one
)) ||
354 !(right
= lookup_commit_reference(two
)))
355 message
= "(commits not present)";
356 else if (prepare_submodule_summary(&rev
, path
, left
, right
,
357 &fast_forward
, &fast_backward
))
358 message
= "(revision walker failed)";
360 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
361 fprintf(f
, "%sSubmodule %s contains untracked content\n",
363 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
364 fprintf(f
, "%sSubmodule %s contains modified content\n",
367 if (!hashcmp(one
, two
)) {
372 strbuf_addf(&sb
, "%s%sSubmodule %s %s..", line_prefix
, meta
, path
,
373 find_unique_abbrev(one
, DEFAULT_ABBREV
));
374 if (!fast_backward
&& !fast_forward
)
375 strbuf_addch(&sb
, '.');
376 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
378 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
380 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
381 fwrite(sb
.buf
, sb
.len
, 1, f
);
383 if (!message
) /* only NULL if we succeeded in setting up the walk */
384 print_submodule_summary(&rev
, f
, line_prefix
, del
, add
, reset
);
386 clear_commit_marks(left
, ~0);
388 clear_commit_marks(right
, ~0);
393 void set_config_fetch_recurse_submodules(int value
)
395 config_fetch_recurse_submodules
= value
;
398 static int has_remote(const char *refname
, const struct object_id
*oid
,
399 int flags
, void *cb_data
)
404 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
406 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
409 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
410 struct child_process cp
= CHILD_PROCESS_INIT
;
411 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
412 struct strbuf buf
= STRBUF_INIT
;
413 int needs_pushing
= 0;
415 argv
[1] = sha1_to_hex(sha1
);
417 cp
.env
= local_repo_env
;
422 if (start_command(&cp
))
423 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
424 sha1_to_hex(sha1
), path
);
425 if (strbuf_read(&buf
, cp
.out
, 41))
429 strbuf_release(&buf
);
430 return needs_pushing
;
436 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
437 struct diff_options
*options
,
441 struct string_list
*needs_pushing
= data
;
443 for (i
= 0; i
< q
->nr
; i
++) {
444 struct diff_filepair
*p
= q
->queue
[i
];
445 if (!S_ISGITLINK(p
->two
->mode
))
447 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
))
448 string_list_insert(needs_pushing
, p
->two
->path
);
452 static void find_unpushed_submodule_commits(struct commit
*commit
,
453 struct string_list
*needs_pushing
)
457 init_revisions(&rev
, NULL
);
458 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
459 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
460 rev
.diffopt
.format_callback_data
= needs_pushing
;
461 diff_tree_combined_merge(commit
, 1, &rev
);
464 int find_unpushed_submodules(unsigned char new_sha1
[20],
465 const char *remotes_name
, struct string_list
*needs_pushing
)
468 struct commit
*commit
;
469 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
470 int argc
= ARRAY_SIZE(argv
) - 1;
473 struct strbuf remotes_arg
= STRBUF_INIT
;
475 strbuf_addf(&remotes_arg
, "--remotes=%s", remotes_name
);
476 init_revisions(&rev
, NULL
);
477 sha1_copy
= xstrdup(sha1_to_hex(new_sha1
));
479 argv
[3] = remotes_arg
.buf
;
480 setup_revisions(argc
, argv
, &rev
, NULL
);
481 if (prepare_revision_walk(&rev
))
482 die("revision walk setup failed");
484 while ((commit
= get_revision(&rev
)) != NULL
)
485 find_unpushed_submodule_commits(commit
, needs_pushing
);
487 reset_revision_walk();
489 strbuf_release(&remotes_arg
);
491 return needs_pushing
->nr
;
494 static int push_submodule(const char *path
)
496 if (add_submodule_odb(path
))
499 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
500 struct child_process cp
= CHILD_PROCESS_INIT
;
501 const char *argv
[] = {"push", NULL
};
504 cp
.env
= local_repo_env
;
508 if (run_command(&cp
))
516 int push_unpushed_submodules(unsigned char new_sha1
[20], const char *remotes_name
)
519 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
521 if (!find_unpushed_submodules(new_sha1
, remotes_name
, &needs_pushing
))
524 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
525 const char *path
= needs_pushing
.items
[i
].string
;
526 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
527 if (!push_submodule(path
)) {
528 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
533 string_list_clear(&needs_pushing
, 0);
538 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
541 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
542 /* Even if the submodule is checked out and the commit is
543 * present, make sure it is reachable from a ref. */
544 struct child_process cp
= CHILD_PROCESS_INIT
;
545 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
546 struct strbuf buf
= STRBUF_INIT
;
548 argv
[3] = sha1_to_hex(sha1
);
550 cp
.env
= local_repo_env
;
554 if (!capture_command(&cp
, &buf
, 1024) && !buf
.len
)
557 strbuf_release(&buf
);
562 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
563 struct diff_options
*options
,
567 for (i
= 0; i
< q
->nr
; i
++) {
568 struct diff_filepair
*p
= q
->queue
[i
];
569 if (!S_ISGITLINK(p
->two
->mode
))
572 if (S_ISGITLINK(p
->one
->mode
)) {
573 /* NEEDSWORK: We should honor the name configured in
574 * the .gitmodules file of the commit we are examining
575 * here to be able to correctly follow submodules
576 * being moved around. */
577 struct string_list_item
*path
;
578 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
579 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
580 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
582 /* Submodule is new or was moved here */
583 /* NEEDSWORK: When the .git directories of submodules
584 * live inside the superprojects .git directory some
585 * day we should fetch new submodules directly into
586 * that location too when config or options request
587 * that so they can be checked out from there. */
593 static int add_sha1_to_array(const char *ref
, const struct object_id
*oid
,
594 int flags
, void *data
)
596 sha1_array_append(data
, oid
->hash
);
600 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
602 if (!initialized_fetch_ref_tips
) {
603 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
604 initialized_fetch_ref_tips
= 1;
607 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
610 static void add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
612 argv_array_push(data
, sha1_to_hex(sha1
));
615 static void calculate_changed_submodule_paths(void)
618 struct commit
*commit
;
619 struct argv_array argv
= ARGV_ARRAY_INIT
;
621 /* No need to check if there are no submodules configured */
622 if (!submodule_from_path(NULL
, NULL
))
625 init_revisions(&rev
, NULL
);
626 argv_array_push(&argv
, "--"); /* argv[0] program name */
627 sha1_array_for_each_unique(&ref_tips_after_fetch
,
628 add_sha1_to_argv
, &argv
);
629 argv_array_push(&argv
, "--not");
630 sha1_array_for_each_unique(&ref_tips_before_fetch
,
631 add_sha1_to_argv
, &argv
);
632 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
633 if (prepare_revision_walk(&rev
))
634 die("revision walk setup failed");
637 * Collect all submodules (whether checked out or not) for which new
638 * commits have been recorded upstream in "changed_submodule_paths".
640 while ((commit
= get_revision(&rev
))) {
641 struct commit_list
*parent
= commit
->parents
;
643 struct diff_options diff_opts
;
644 diff_setup(&diff_opts
);
645 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
646 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
647 diff_opts
.format_callback
= submodule_collect_changed_cb
;
648 diff_setup_done(&diff_opts
);
649 diff_tree_sha1(parent
->item
->object
.oid
.hash
, commit
->object
.oid
.hash
, "", &diff_opts
);
650 diffcore_std(&diff_opts
);
651 diff_flush(&diff_opts
);
652 parent
= parent
->next
;
656 argv_array_clear(&argv
);
657 sha1_array_clear(&ref_tips_before_fetch
);
658 sha1_array_clear(&ref_tips_after_fetch
);
659 initialized_fetch_ref_tips
= 0;
662 struct submodule_parallel_fetch
{
664 struct argv_array args
;
665 const char *work_tree
;
667 int command_line_option
;
671 #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
673 static int get_next_submodule(struct child_process
*cp
,
674 struct strbuf
*err
, void *data
, void **task_cb
)
677 struct submodule_parallel_fetch
*spf
= data
;
679 for (; spf
->count
< active_nr
; spf
->count
++) {
680 struct strbuf submodule_path
= STRBUF_INIT
;
681 struct strbuf submodule_git_dir
= STRBUF_INIT
;
682 struct strbuf submodule_prefix
= STRBUF_INIT
;
683 const struct cache_entry
*ce
= active_cache
[spf
->count
];
684 const char *git_dir
, *default_argv
;
685 const struct submodule
*submodule
;
687 if (!S_ISGITLINK(ce
->ce_mode
))
690 submodule
= submodule_from_path(null_sha1
, ce
->name
);
692 submodule
= submodule_from_name(null_sha1
, ce
->name
);
694 default_argv
= "yes";
695 if (spf
->command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
697 submodule
->fetch_recurse
!=
698 RECURSE_SUBMODULES_NONE
) {
699 if (submodule
->fetch_recurse
==
700 RECURSE_SUBMODULES_OFF
)
702 if (submodule
->fetch_recurse
==
703 RECURSE_SUBMODULES_ON_DEMAND
) {
704 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
706 default_argv
= "on-demand";
709 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
710 gitmodules_is_unmerged
)
712 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
713 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
715 default_argv
= "on-demand";
718 } else if (spf
->command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
719 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
721 default_argv
= "on-demand";
724 strbuf_addf(&submodule_path
, "%s/%s", spf
->work_tree
, ce
->name
);
725 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
726 strbuf_addf(&submodule_prefix
, "%s%s/", spf
->prefix
, ce
->name
);
727 git_dir
= read_gitfile(submodule_git_dir
.buf
);
729 git_dir
= submodule_git_dir
.buf
;
730 if (is_directory(git_dir
)) {
731 child_process_init(cp
);
732 cp
->dir
= strbuf_detach(&submodule_path
, NULL
);
733 cp
->env
= local_repo_env
;
736 strbuf_addf(err
, "Fetching submodule %s%s\n",
737 spf
->prefix
, ce
->name
);
738 argv_array_init(&cp
->args
);
739 argv_array_pushv(&cp
->args
, spf
->args
.argv
);
740 argv_array_push(&cp
->args
, default_argv
);
741 argv_array_push(&cp
->args
, "--submodule-prefix");
742 argv_array_push(&cp
->args
, submodule_prefix
.buf
);
745 strbuf_release(&submodule_path
);
746 strbuf_release(&submodule_git_dir
);
747 strbuf_release(&submodule_prefix
);
756 static int fetch_start_failure(struct strbuf
*err
,
757 void *cb
, void *task_cb
)
759 struct submodule_parallel_fetch
*spf
= cb
;
766 static int fetch_finish(int retvalue
, struct strbuf
*err
,
767 void *cb
, void *task_cb
)
769 struct submodule_parallel_fetch
*spf
= cb
;
777 int fetch_populated_submodules(const struct argv_array
*options
,
778 const char *prefix
, int command_line_option
,
779 int quiet
, int max_parallel_jobs
)
782 struct submodule_parallel_fetch spf
= SPF_INIT
;
784 spf
.work_tree
= get_git_work_tree();
785 spf
.command_line_option
= command_line_option
;
792 if (read_cache() < 0)
793 die("index file corrupt");
795 argv_array_push(&spf
.args
, "fetch");
796 for (i
= 0; i
< options
->argc
; i
++)
797 argv_array_push(&spf
.args
, options
->argv
[i
]);
798 argv_array_push(&spf
.args
, "--recurse-submodules-default");
799 /* default value, "--submodule-prefix" and its value are added later */
801 if (max_parallel_jobs
< 0)
802 max_parallel_jobs
= parallel_jobs
;
804 calculate_changed_submodule_paths();
805 run_processes_parallel(max_parallel_jobs
,
811 argv_array_clear(&spf
.args
);
813 string_list_clear(&changed_submodule_paths
, 1);
817 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
820 struct child_process cp
= CHILD_PROCESS_INIT
;
821 const char *argv
[] = {
827 struct strbuf buf
= STRBUF_INIT
;
828 unsigned dirty_submodule
= 0;
829 const char *line
, *next_line
;
832 strbuf_addf(&buf
, "%s/.git", path
);
833 git_dir
= read_gitfile(buf
.buf
);
836 if (!is_directory(git_dir
)) {
837 strbuf_release(&buf
);
838 /* The submodule is not checked out, so it is not modified */
844 if (ignore_untracked
)
848 cp
.env
= local_repo_env
;
853 if (start_command(&cp
))
854 die("Could not run 'git status --porcelain' in submodule %s", path
);
856 len
= strbuf_read(&buf
, cp
.out
, 1024);
859 if ((line
[0] == '?') && (line
[1] == '?')) {
860 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
861 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
864 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
865 if (ignore_untracked
||
866 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
869 next_line
= strchr(line
, '\n');
873 len
-= (next_line
- line
);
878 if (finish_command(&cp
))
879 die("'git status --porcelain' failed in submodule %s", path
);
881 strbuf_release(&buf
);
882 return dirty_submodule
;
885 int submodule_uses_gitfile(const char *path
)
887 struct child_process cp
= CHILD_PROCESS_INIT
;
888 const char *argv
[] = {
896 struct strbuf buf
= STRBUF_INIT
;
899 strbuf_addf(&buf
, "%s/.git", path
);
900 git_dir
= read_gitfile(buf
.buf
);
902 strbuf_release(&buf
);
905 strbuf_release(&buf
);
907 /* Now test that all nested submodules use a gitfile too */
909 cp
.env
= local_repo_env
;
915 if (run_command(&cp
))
921 int ok_to_remove_submodule(const char *path
)
924 struct child_process cp
= CHILD_PROCESS_INIT
;
925 const char *argv
[] = {
929 "--ignore-submodules=none",
932 struct strbuf buf
= STRBUF_INIT
;
933 int ok_to_remove
= 1;
935 if (!file_exists(path
) || is_empty_dir(path
))
938 if (!submodule_uses_gitfile(path
))
942 cp
.env
= local_repo_env
;
947 if (start_command(&cp
))
948 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path
);
950 len
= strbuf_read(&buf
, cp
.out
, 1024);
955 if (finish_command(&cp
))
956 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path
);
958 strbuf_release(&buf
);
962 static int find_first_merges(struct object_array
*result
, const char *path
,
963 struct commit
*a
, struct commit
*b
)
966 struct object_array merges
= OBJECT_ARRAY_INIT
;
967 struct commit
*commit
;
968 int contains_another
;
970 char merged_revision
[42];
971 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
972 "--all", merged_revision
, NULL
};
973 struct rev_info revs
;
974 struct setup_revision_opt rev_opts
;
976 memset(result
, 0, sizeof(struct object_array
));
977 memset(&rev_opts
, 0, sizeof(rev_opts
));
979 /* get all revisions that merge commit a */
980 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
981 oid_to_hex(&a
->object
.oid
));
982 init_revisions(&revs
, NULL
);
983 rev_opts
.submodule
= path
;
984 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
986 /* save all revisions from the above list that contain b */
987 if (prepare_revision_walk(&revs
))
988 die("revision walk setup failed");
989 while ((commit
= get_revision(&revs
)) != NULL
) {
990 struct object
*o
= &(commit
->object
);
991 if (in_merge_bases(b
, commit
))
992 add_object_array(o
, NULL
, &merges
);
994 reset_revision_walk();
996 /* Now we've got all merges that contain a and b. Prune all
997 * merges that contain another found merge and save them in
1000 for (i
= 0; i
< merges
.nr
; i
++) {
1001 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1003 contains_another
= 0;
1004 for (j
= 0; j
< merges
.nr
; j
++) {
1005 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1006 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
1007 contains_another
= 1;
1012 if (!contains_another
)
1013 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1016 free(merges
.objects
);
1020 static void print_commit(struct commit
*commit
)
1022 struct strbuf sb
= STRBUF_INIT
;
1023 struct pretty_print_context ctx
= {0};
1024 ctx
.date_mode
.type
= DATE_NORMAL
;
1025 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1026 fprintf(stderr
, "%s\n", sb
.buf
);
1027 strbuf_release(&sb
);
1030 #define MERGE_WARNING(path, msg) \
1031 warning("Failed to merge submodule %s (%s)", path, msg);
1033 int merge_submodule(unsigned char result
[20], const char *path
,
1034 const unsigned char base
[20], const unsigned char a
[20],
1035 const unsigned char b
[20], int search
)
1037 struct commit
*commit_base
, *commit_a
, *commit_b
;
1039 struct object_array merges
;
1043 /* store a in result in case we fail */
1046 /* we can not handle deletion conflicts */
1047 if (is_null_sha1(base
))
1049 if (is_null_sha1(a
))
1051 if (is_null_sha1(b
))
1054 if (add_submodule_odb(path
)) {
1055 MERGE_WARNING(path
, "not checked out");
1059 if (!(commit_base
= lookup_commit_reference(base
)) ||
1060 !(commit_a
= lookup_commit_reference(a
)) ||
1061 !(commit_b
= lookup_commit_reference(b
))) {
1062 MERGE_WARNING(path
, "commits not present");
1066 /* check whether both changes are forward */
1067 if (!in_merge_bases(commit_base
, commit_a
) ||
1068 !in_merge_bases(commit_base
, commit_b
)) {
1069 MERGE_WARNING(path
, "commits don't follow merge-base");
1073 /* Case #1: a is contained in b or vice versa */
1074 if (in_merge_bases(commit_a
, commit_b
)) {
1078 if (in_merge_bases(commit_b
, commit_a
)) {
1084 * Case #2: There are one or more merges that contain a and b in
1085 * the submodule. If there is only one, then present it as a
1086 * suggestion to the user, but leave it marked unmerged so the
1087 * user needs to confirm the resolution.
1090 /* Skip the search if makes no sense to the calling context. */
1094 /* find commit which merges them */
1095 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
1096 switch (parent_count
) {
1098 MERGE_WARNING(path
, "merge following commits not found");
1102 MERGE_WARNING(path
, "not fast-forward");
1103 fprintf(stderr
, "Found a possible merge resolution "
1104 "for the submodule:\n");
1105 print_commit((struct commit
*) merges
.objects
[0].item
);
1107 "If this is correct simply add it to the index "
1110 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1111 "which will accept this suggestion.\n",
1112 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1116 MERGE_WARNING(path
, "multiple merges found");
1117 for (i
= 0; i
< merges
.nr
; i
++)
1118 print_commit((struct commit
*) merges
.objects
[i
].item
);
1121 free(merges
.objects
);
1125 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1126 void connect_work_tree_and_git_dir(const char *work_tree
, const char *git_dir
)
1128 struct strbuf file_name
= STRBUF_INIT
;
1129 struct strbuf rel_path
= STRBUF_INIT
;
1130 const char *real_work_tree
= xstrdup(real_path(work_tree
));
1132 /* Update gitfile */
1133 strbuf_addf(&file_name
, "%s/.git", work_tree
);
1134 write_file(file_name
.buf
, "gitdir: %s",
1135 relative_path(git_dir
, real_work_tree
, &rel_path
));
1137 /* Update core.worktree setting */
1138 strbuf_reset(&file_name
);
1139 strbuf_addf(&file_name
, "%s/config", git_dir
);
1140 git_config_set_in_file(file_name
.buf
, "core.worktree",
1141 relative_path(real_work_tree
, git_dir
,
1144 strbuf_release(&file_name
);
1145 strbuf_release(&rel_path
);
1146 free((void *)real_work_tree
);
1149 int parallel_submodules(void)
1151 return parallel_jobs
;