7 #include "run-command.h"
10 #include "string-list.h"
11 #include "sha1-array.h"
12 #include "argv-array.h"
15 static struct string_list config_name_for_path
;
16 static struct string_list config_fetch_recurse_submodules_for_name
;
17 static struct string_list config_ignore_for_name
;
18 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
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
;
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 struct string_list_item
*path_option
;
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 path_option
= unsorted_string_list_lookup(&config_name_for_path
, oldpath
);
68 warning(_("Could not find section in .gitmodules where path=%s"), oldpath
);
71 strbuf_addstr(&entry
, "submodule.");
72 strbuf_addstr(&entry
, path_option
->util
);
73 strbuf_addstr(&entry
, ".path");
74 if (git_config_set_in_file(".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 struct string_list_item
*path_option
;
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 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
102 warning(_("Could not find section in .gitmodules where path=%s"), path
);
105 strbuf_addstr(§
, "submodule.");
106 strbuf_addstr(§
, path_option
->util
);
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
;
126 struct alternate_object_database
*alt_odb
;
130 strbuf_addf(&objects_directory
, "%s/.git", path
);
131 git_dir
= read_gitfile(objects_directory
.buf
);
133 strbuf_reset(&objects_directory
);
134 strbuf_addstr(&objects_directory
, git_dir
);
136 strbuf_addstr(&objects_directory
, "/objects/");
137 if (!is_directory(objects_directory
.buf
)) {
141 /* avoid adding it twice */
142 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
143 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
144 !strncmp(alt_odb
->base
, objects_directory
.buf
,
145 objects_directory
.len
))
148 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
149 alt_odb
->next
= alt_odb_list
;
150 strcpy(alt_odb
->base
, objects_directory
.buf
);
151 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
152 alt_odb
->name
[2] = '/';
153 alt_odb
->name
[40] = '\0';
154 alt_odb
->name
[41] = '\0';
155 alt_odb_list
= alt_odb
;
157 /* add possible alternates from the submodule */
158 read_info_alternates(objects_directory
.buf
, 0);
161 strbuf_release(&objects_directory
);
165 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
168 struct string_list_item
*path_option
, *ignore_option
;
169 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
171 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
173 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
174 else if (gitmodules_is_unmerged
)
175 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
179 int submodule_config(const char *var
, const char *value
, void *cb
)
181 if (starts_with(var
, "submodule."))
182 return parse_submodule_config_option(var
, value
);
183 else if (!strcmp(var
, "fetch.recursesubmodules")) {
184 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
190 void gitmodules_config(void)
192 const char *work_tree
= get_git_work_tree();
194 struct strbuf gitmodules_path
= STRBUF_INIT
;
196 strbuf_addstr(&gitmodules_path
, work_tree
);
197 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
198 if (read_cache() < 0)
199 die("index file corrupt");
200 pos
= cache_name_pos(".gitmodules", 11);
201 if (pos
< 0) { /* .gitmodules not found or isn't merged */
203 if (active_nr
> pos
) { /* there is a .gitmodules */
204 const struct cache_entry
*ce
= active_cache
[pos
];
205 if (ce_namelen(ce
) == 11 &&
206 !memcmp(ce
->name
, ".gitmodules", 11))
207 gitmodules_is_unmerged
= 1;
209 } else if (pos
< active_nr
) {
211 if (lstat(".gitmodules", &st
) == 0 &&
212 ce_match_stat(active_cache
[pos
], &st
, 0) & DATA_CHANGED
)
213 gitmodules_is_modified
= 1;
216 if (!gitmodules_is_unmerged
)
217 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
218 strbuf_release(&gitmodules_path
);
222 int parse_submodule_config_option(const char *var
, const char *value
)
224 struct string_list_item
*config
;
225 const char *name
, *key
;
228 if (parse_config_key(var
, "submodule", &name
, &namelen
, &key
) < 0 || !name
)
231 if (!strcmp(key
, "path")) {
233 return config_error_nonbool(var
);
235 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
239 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
240 config
->util
= xmemdupz(name
, namelen
);
241 } else if (!strcmp(key
, "fetchrecursesubmodules")) {
242 char *name_cstr
= xmemdupz(name
, namelen
);
243 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name_cstr
);
245 config
= string_list_append(&config_fetch_recurse_submodules_for_name
, name_cstr
);
248 config
->util
= (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var
, value
);
249 } else if (!strcmp(key
, "ignore")) {
253 return config_error_nonbool(var
);
255 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
256 strcmp(value
, "all") && strcmp(value
, "none")) {
257 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
261 name_cstr
= xmemdupz(name
, namelen
);
262 config
= unsorted_string_list_lookup(&config_ignore_for_name
, name_cstr
);
267 config
= string_list_append(&config_ignore_for_name
, name_cstr
);
268 config
->util
= xstrdup(value
);
274 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
277 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
278 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
279 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
281 if (!strcmp(arg
, "all"))
282 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
283 else if (!strcmp(arg
, "untracked"))
284 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
285 else if (!strcmp(arg
, "dirty"))
286 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
287 else if (strcmp(arg
, "none"))
288 die("bad --ignore-submodules argument: %s", arg
);
291 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
292 struct commit
*left
, struct commit
*right
,
293 int *fast_forward
, int *fast_backward
)
295 struct commit_list
*merge_bases
, *list
;
297 init_revisions(rev
, NULL
);
298 setup_revisions(0, NULL
, rev
, NULL
);
300 rev
->first_parent_only
= 1;
301 left
->object
.flags
|= SYMMETRIC_LEFT
;
302 add_pending_object(rev
, &left
->object
, path
);
303 add_pending_object(rev
, &right
->object
, path
);
304 merge_bases
= get_merge_bases(left
, right
, 1);
306 if (merge_bases
->item
== left
)
308 else if (merge_bases
->item
== right
)
311 for (list
= merge_bases
; list
; list
= list
->next
) {
312 list
->item
->object
.flags
|= UNINTERESTING
;
313 add_pending_object(rev
, &list
->item
->object
,
314 sha1_to_hex(list
->item
->object
.sha1
));
316 return prepare_revision_walk(rev
);
319 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
320 const char *line_prefix
,
321 const char *del
, const char *add
, const char *reset
)
323 static const char format
[] = " %m %s";
324 struct strbuf sb
= STRBUF_INIT
;
325 struct commit
*commit
;
327 while ((commit
= get_revision(rev
))) {
328 struct pretty_print_context ctx
= {0};
329 ctx
.date_mode
= rev
->date_mode
;
330 ctx
.output_encoding
= get_log_output_encoding();
331 strbuf_setlen(&sb
, 0);
332 strbuf_addstr(&sb
, line_prefix
);
333 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
335 strbuf_addstr(&sb
, del
);
338 strbuf_addstr(&sb
, add
);
339 format_commit_message(commit
, format
, &sb
, &ctx
);
341 strbuf_addstr(&sb
, reset
);
342 strbuf_addch(&sb
, '\n');
343 fprintf(f
, "%s", sb
.buf
);
348 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
350 switch (git_config_maybe_bool(opt
, arg
)) {
352 return RECURSE_SUBMODULES_ON
;
354 return RECURSE_SUBMODULES_OFF
;
356 if (!strcmp(arg
, "on-demand"))
357 return RECURSE_SUBMODULES_ON_DEMAND
;
358 die("bad %s argument: %s", opt
, arg
);
362 void show_submodule_summary(FILE *f
, const char *path
,
363 const char *line_prefix
,
364 unsigned char one
[20], unsigned char two
[20],
365 unsigned dirty_submodule
, const char *meta
,
366 const char *del
, const char *add
, const char *reset
)
369 struct commit
*left
= NULL
, *right
= NULL
;
370 const char *message
= NULL
;
371 struct strbuf sb
= STRBUF_INIT
;
372 int fast_forward
= 0, fast_backward
= 0;
374 if (is_null_sha1(two
))
375 message
= "(submodule deleted)";
376 else if (add_submodule_odb(path
))
377 message
= "(not checked out)";
378 else if (is_null_sha1(one
))
379 message
= "(new submodule)";
380 else if (!(left
= lookup_commit_reference(one
)) ||
381 !(right
= lookup_commit_reference(two
)))
382 message
= "(commits not present)";
383 else if (prepare_submodule_summary(&rev
, path
, left
, right
,
384 &fast_forward
, &fast_backward
))
385 message
= "(revision walker failed)";
387 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
388 fprintf(f
, "%sSubmodule %s contains untracked content\n",
390 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
391 fprintf(f
, "%sSubmodule %s contains modified content\n",
394 if (!hashcmp(one
, two
)) {
399 strbuf_addf(&sb
, "%s%sSubmodule %s %s..", line_prefix
, meta
, path
,
400 find_unique_abbrev(one
, DEFAULT_ABBREV
));
401 if (!fast_backward
&& !fast_forward
)
402 strbuf_addch(&sb
, '.');
403 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
405 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
407 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
408 fwrite(sb
.buf
, sb
.len
, 1, f
);
410 if (!message
) /* only NULL if we succeeded in setting up the walk */
411 print_submodule_summary(&rev
, f
, line_prefix
, del
, add
, reset
);
413 clear_commit_marks(left
, ~0);
415 clear_commit_marks(right
, ~0);
420 void set_config_fetch_recurse_submodules(int value
)
422 config_fetch_recurse_submodules
= value
;
425 static int has_remote(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
430 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
432 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
435 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
436 struct child_process cp
;
437 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
438 struct strbuf buf
= STRBUF_INIT
;
439 int needs_pushing
= 0;
441 argv
[1] = sha1_to_hex(sha1
);
442 memset(&cp
, 0, sizeof(cp
));
444 cp
.env
= local_repo_env
;
449 if (start_command(&cp
))
450 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
451 sha1_to_hex(sha1
), path
);
452 if (strbuf_read(&buf
, cp
.out
, 41))
456 strbuf_release(&buf
);
457 return needs_pushing
;
463 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
464 struct diff_options
*options
,
468 struct string_list
*needs_pushing
= data
;
470 for (i
= 0; i
< q
->nr
; i
++) {
471 struct diff_filepair
*p
= q
->queue
[i
];
472 if (!S_ISGITLINK(p
->two
->mode
))
474 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
))
475 string_list_insert(needs_pushing
, p
->two
->path
);
479 static void find_unpushed_submodule_commits(struct commit
*commit
,
480 struct string_list
*needs_pushing
)
484 init_revisions(&rev
, NULL
);
485 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
486 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
487 rev
.diffopt
.format_callback_data
= needs_pushing
;
488 diff_tree_combined_merge(commit
, 1, &rev
);
491 int find_unpushed_submodules(unsigned char new_sha1
[20],
492 const char *remotes_name
, struct string_list
*needs_pushing
)
495 struct commit
*commit
;
496 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
497 int argc
= ARRAY_SIZE(argv
) - 1;
500 struct strbuf remotes_arg
= STRBUF_INIT
;
502 strbuf_addf(&remotes_arg
, "--remotes=%s", remotes_name
);
503 init_revisions(&rev
, NULL
);
504 sha1_copy
= xstrdup(sha1_to_hex(new_sha1
));
506 argv
[3] = remotes_arg
.buf
;
507 setup_revisions(argc
, argv
, &rev
, NULL
);
508 if (prepare_revision_walk(&rev
))
509 die("revision walk setup failed");
511 while ((commit
= get_revision(&rev
)) != NULL
)
512 find_unpushed_submodule_commits(commit
, needs_pushing
);
514 reset_revision_walk();
516 strbuf_release(&remotes_arg
);
518 return needs_pushing
->nr
;
521 static int push_submodule(const char *path
)
523 if (add_submodule_odb(path
))
526 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
527 struct child_process cp
;
528 const char *argv
[] = {"push", NULL
};
530 memset(&cp
, 0, sizeof(cp
));
532 cp
.env
= local_repo_env
;
536 if (run_command(&cp
))
544 int push_unpushed_submodules(unsigned char new_sha1
[20], const char *remotes_name
)
547 struct string_list needs_pushing
;
549 memset(&needs_pushing
, 0, sizeof(struct string_list
));
550 needs_pushing
.strdup_strings
= 1;
552 if (!find_unpushed_submodules(new_sha1
, remotes_name
, &needs_pushing
))
555 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
556 const char *path
= needs_pushing
.items
[i
].string
;
557 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
558 if (!push_submodule(path
)) {
559 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
564 string_list_clear(&needs_pushing
, 0);
569 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
572 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
573 /* Even if the submodule is checked out and the commit is
574 * present, make sure it is reachable from a ref. */
575 struct child_process cp
;
576 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
577 struct strbuf buf
= STRBUF_INIT
;
579 argv
[3] = sha1_to_hex(sha1
);
580 memset(&cp
, 0, sizeof(cp
));
582 cp
.env
= local_repo_env
;
587 if (!run_command(&cp
) && !strbuf_read(&buf
, cp
.out
, 1024))
591 strbuf_release(&buf
);
596 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
597 struct diff_options
*options
,
601 for (i
= 0; i
< q
->nr
; i
++) {
602 struct diff_filepair
*p
= q
->queue
[i
];
603 if (!S_ISGITLINK(p
->two
->mode
))
606 if (S_ISGITLINK(p
->one
->mode
)) {
607 /* NEEDSWORK: We should honor the name configured in
608 * the .gitmodules file of the commit we are examining
609 * here to be able to correctly follow submodules
610 * being moved around. */
611 struct string_list_item
*path
;
612 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
613 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
614 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
616 /* Submodule is new or was moved here */
617 /* NEEDSWORK: When the .git directories of submodules
618 * live inside the superprojects .git directory some
619 * day we should fetch new submodules directly into
620 * that location too when config or options request
621 * that so they can be checked out from there. */
627 static int add_sha1_to_array(const char *ref
, const unsigned char *sha1
,
628 int flags
, void *data
)
630 sha1_array_append(data
, sha1
);
634 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
636 if (!initialized_fetch_ref_tips
) {
637 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
638 initialized_fetch_ref_tips
= 1;
641 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
644 static void add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
646 argv_array_push(data
, sha1_to_hex(sha1
));
649 static void calculate_changed_submodule_paths(void)
652 struct commit
*commit
;
653 struct argv_array argv
= ARGV_ARRAY_INIT
;
655 /* No need to check if there are no submodules configured */
656 if (!config_name_for_path
.nr
)
659 init_revisions(&rev
, NULL
);
660 argv_array_push(&argv
, "--"); /* argv[0] program name */
661 sha1_array_for_each_unique(&ref_tips_after_fetch
,
662 add_sha1_to_argv
, &argv
);
663 argv_array_push(&argv
, "--not");
664 sha1_array_for_each_unique(&ref_tips_before_fetch
,
665 add_sha1_to_argv
, &argv
);
666 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
667 if (prepare_revision_walk(&rev
))
668 die("revision walk setup failed");
671 * Collect all submodules (whether checked out or not) for which new
672 * commits have been recorded upstream in "changed_submodule_paths".
674 while ((commit
= get_revision(&rev
))) {
675 struct commit_list
*parent
= commit
->parents
;
677 struct diff_options diff_opts
;
678 diff_setup(&diff_opts
);
679 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
680 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
681 diff_opts
.format_callback
= submodule_collect_changed_cb
;
682 diff_setup_done(&diff_opts
);
683 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
684 diffcore_std(&diff_opts
);
685 diff_flush(&diff_opts
);
686 parent
= parent
->next
;
690 argv_array_clear(&argv
);
691 sha1_array_clear(&ref_tips_before_fetch
);
692 sha1_array_clear(&ref_tips_after_fetch
);
693 initialized_fetch_ref_tips
= 0;
696 int fetch_populated_submodules(const struct argv_array
*options
,
697 const char *prefix
, int command_line_option
,
701 struct child_process cp
;
702 struct argv_array argv
= ARGV_ARRAY_INIT
;
703 struct string_list_item
*name_for_path
;
704 const char *work_tree
= get_git_work_tree();
708 if (read_cache() < 0)
709 die("index file corrupt");
711 argv_array_push(&argv
, "fetch");
712 for (i
= 0; i
< options
->argc
; i
++)
713 argv_array_push(&argv
, options
->argv
[i
]);
714 argv_array_push(&argv
, "--recurse-submodules-default");
715 /* default value, "--submodule-prefix" and its value are added later */
717 memset(&cp
, 0, sizeof(cp
));
718 cp
.env
= local_repo_env
;
722 calculate_changed_submodule_paths();
724 for (i
= 0; i
< active_nr
; i
++) {
725 struct strbuf submodule_path
= STRBUF_INIT
;
726 struct strbuf submodule_git_dir
= STRBUF_INIT
;
727 struct strbuf submodule_prefix
= STRBUF_INIT
;
728 const struct cache_entry
*ce
= active_cache
[i
];
729 const char *git_dir
, *name
, *default_argv
;
731 if (!S_ISGITLINK(ce
->ce_mode
))
735 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
737 name
= name_for_path
->util
;
739 default_argv
= "yes";
740 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
741 struct string_list_item
*fetch_recurse_submodules_option
;
742 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
743 if (fetch_recurse_submodules_option
) {
744 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_OFF
)
746 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_ON_DEMAND
) {
747 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
749 default_argv
= "on-demand";
752 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
753 gitmodules_is_unmerged
)
755 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
756 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
758 default_argv
= "on-demand";
761 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
762 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
764 default_argv
= "on-demand";
767 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
768 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
769 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
770 git_dir
= read_gitfile(submodule_git_dir
.buf
);
772 git_dir
= submodule_git_dir
.buf
;
773 if (is_directory(git_dir
)) {
775 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
776 cp
.dir
= submodule_path
.buf
;
777 argv_array_push(&argv
, default_argv
);
778 argv_array_push(&argv
, "--submodule-prefix");
779 argv_array_push(&argv
, submodule_prefix
.buf
);
781 if (run_command(&cp
))
783 argv_array_pop(&argv
);
784 argv_array_pop(&argv
);
785 argv_array_pop(&argv
);
787 strbuf_release(&submodule_path
);
788 strbuf_release(&submodule_git_dir
);
789 strbuf_release(&submodule_prefix
);
791 argv_array_clear(&argv
);
793 string_list_clear(&changed_submodule_paths
, 1);
797 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
800 struct child_process cp
;
801 const char *argv
[] = {
807 struct strbuf buf
= STRBUF_INIT
;
808 unsigned dirty_submodule
= 0;
809 const char *line
, *next_line
;
812 strbuf_addf(&buf
, "%s/.git", path
);
813 git_dir
= read_gitfile(buf
.buf
);
816 if (!is_directory(git_dir
)) {
817 strbuf_release(&buf
);
818 /* The submodule is not checked out, so it is not modified */
824 if (ignore_untracked
)
827 memset(&cp
, 0, sizeof(cp
));
829 cp
.env
= local_repo_env
;
834 if (start_command(&cp
))
835 die("Could not run 'git status --porcelain' in submodule %s", path
);
837 len
= strbuf_read(&buf
, cp
.out
, 1024);
840 if ((line
[0] == '?') && (line
[1] == '?')) {
841 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
842 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
845 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
846 if (ignore_untracked
||
847 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
850 next_line
= strchr(line
, '\n');
854 len
-= (next_line
- line
);
859 if (finish_command(&cp
))
860 die("'git status --porcelain' failed in submodule %s", path
);
862 strbuf_release(&buf
);
863 return dirty_submodule
;
866 int submodule_uses_gitfile(const char *path
)
868 struct child_process cp
;
869 const char *argv
[] = {
877 struct strbuf buf
= STRBUF_INIT
;
880 strbuf_addf(&buf
, "%s/.git", path
);
881 git_dir
= read_gitfile(buf
.buf
);
883 strbuf_release(&buf
);
886 strbuf_release(&buf
);
888 /* Now test that all nested submodules use a gitfile too */
889 memset(&cp
, 0, sizeof(cp
));
891 cp
.env
= local_repo_env
;
897 if (run_command(&cp
))
903 int ok_to_remove_submodule(const char *path
)
907 struct child_process cp
;
908 const char *argv
[] = {
912 "--ignore-submodules=none",
915 struct strbuf buf
= STRBUF_INIT
;
916 int ok_to_remove
= 1;
918 if ((lstat(path
, &st
) < 0) || is_empty_dir(path
))
921 if (!submodule_uses_gitfile(path
))
924 memset(&cp
, 0, sizeof(cp
));
926 cp
.env
= local_repo_env
;
931 if (start_command(&cp
))
932 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path
);
934 len
= strbuf_read(&buf
, cp
.out
, 1024);
939 if (finish_command(&cp
))
940 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path
);
942 strbuf_release(&buf
);
946 static int find_first_merges(struct object_array
*result
, const char *path
,
947 struct commit
*a
, struct commit
*b
)
950 struct object_array merges
= OBJECT_ARRAY_INIT
;
951 struct commit
*commit
;
952 int contains_another
;
954 char merged_revision
[42];
955 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
956 "--all", merged_revision
, NULL
};
957 struct rev_info revs
;
958 struct setup_revision_opt rev_opts
;
960 memset(result
, 0, sizeof(struct object_array
));
961 memset(&rev_opts
, 0, sizeof(rev_opts
));
963 /* get all revisions that merge commit a */
964 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
965 sha1_to_hex(a
->object
.sha1
));
966 init_revisions(&revs
, NULL
);
967 rev_opts
.submodule
= path
;
968 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
970 /* save all revisions from the above list that contain b */
971 if (prepare_revision_walk(&revs
))
972 die("revision walk setup failed");
973 while ((commit
= get_revision(&revs
)) != NULL
) {
974 struct object
*o
= &(commit
->object
);
975 if (in_merge_bases(b
, commit
))
976 add_object_array(o
, NULL
, &merges
);
978 reset_revision_walk();
980 /* Now we've got all merges that contain a and b. Prune all
981 * merges that contain another found merge and save them in
984 for (i
= 0; i
< merges
.nr
; i
++) {
985 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
987 contains_another
= 0;
988 for (j
= 0; j
< merges
.nr
; j
++) {
989 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
990 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
991 contains_another
= 1;
996 if (!contains_another
)
997 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1000 free(merges
.objects
);
1004 static void print_commit(struct commit
*commit
)
1006 struct strbuf sb
= STRBUF_INIT
;
1007 struct pretty_print_context ctx
= {0};
1008 ctx
.date_mode
= DATE_NORMAL
;
1009 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1010 fprintf(stderr
, "%s\n", sb
.buf
);
1011 strbuf_release(&sb
);
1014 #define MERGE_WARNING(path, msg) \
1015 warning("Failed to merge submodule %s (%s)", path, msg);
1017 int merge_submodule(unsigned char result
[20], const char *path
,
1018 const unsigned char base
[20], const unsigned char a
[20],
1019 const unsigned char b
[20], int search
)
1021 struct commit
*commit_base
, *commit_a
, *commit_b
;
1023 struct object_array merges
;
1027 /* store a in result in case we fail */
1030 /* we can not handle deletion conflicts */
1031 if (is_null_sha1(base
))
1033 if (is_null_sha1(a
))
1035 if (is_null_sha1(b
))
1038 if (add_submodule_odb(path
)) {
1039 MERGE_WARNING(path
, "not checked out");
1043 if (!(commit_base
= lookup_commit_reference(base
)) ||
1044 !(commit_a
= lookup_commit_reference(a
)) ||
1045 !(commit_b
= lookup_commit_reference(b
))) {
1046 MERGE_WARNING(path
, "commits not present");
1050 /* check whether both changes are forward */
1051 if (!in_merge_bases(commit_base
, commit_a
) ||
1052 !in_merge_bases(commit_base
, commit_b
)) {
1053 MERGE_WARNING(path
, "commits don't follow merge-base");
1057 /* Case #1: a is contained in b or vice versa */
1058 if (in_merge_bases(commit_a
, commit_b
)) {
1062 if (in_merge_bases(commit_b
, commit_a
)) {
1068 * Case #2: There are one or more merges that contain a and b in
1069 * the submodule. If there is only one, then present it as a
1070 * suggestion to the user, but leave it marked unmerged so the
1071 * user needs to confirm the resolution.
1074 /* Skip the search if makes no sense to the calling context. */
1078 /* find commit which merges them */
1079 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
1080 switch (parent_count
) {
1082 MERGE_WARNING(path
, "merge following commits not found");
1086 MERGE_WARNING(path
, "not fast-forward");
1087 fprintf(stderr
, "Found a possible merge resolution "
1088 "for the submodule:\n");
1089 print_commit((struct commit
*) merges
.objects
[0].item
);
1091 "If this is correct simply add it to the index "
1094 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1095 "which will accept this suggestion.\n",
1096 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
1100 MERGE_WARNING(path
, "multiple merges found");
1101 for (i
= 0; i
< merges
.nr
; i
++)
1102 print_commit((struct commit
*) merges
.objects
[i
].item
);
1105 free(merges
.objects
);
1109 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1110 void connect_work_tree_and_git_dir(const char *work_tree
, const char *git_dir
)
1112 struct strbuf file_name
= STRBUF_INIT
;
1113 struct strbuf rel_path
= STRBUF_INIT
;
1114 const char *real_work_tree
= xstrdup(real_path(work_tree
));
1117 /* Update gitfile */
1118 strbuf_addf(&file_name
, "%s/.git", work_tree
);
1119 fp
= fopen(file_name
.buf
, "w");
1121 die(_("Could not create git link %s"), file_name
.buf
);
1122 fprintf(fp
, "gitdir: %s\n", relative_path(git_dir
, real_work_tree
,
1126 /* Update core.worktree setting */
1127 strbuf_reset(&file_name
);
1128 strbuf_addf(&file_name
, "%s/config", git_dir
);
1129 if (git_config_set_in_file(file_name
.buf
, "core.worktree",
1130 relative_path(real_work_tree
, git_dir
,
1132 die(_("Could not set core.worktree in %s"),
1135 strbuf_release(&file_name
);
1136 strbuf_release(&rel_path
);
1137 free((void *)real_work_tree
);