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 struct strbuf buf
= STRBUF_INIT
;
122 struct cache_entry
*ce
;
123 int namelen
= strlen(".gitmodules");
125 pos
= cache_name_pos(".gitmodules", namelen
);
127 warning(_("could not find .gitmodules in index"));
130 ce
= active_cache
[pos
];
131 ce
->ce_flags
= namelen
;
132 if (strbuf_read_file(&buf
, ".gitmodules", 0) < 0)
133 die(_("reading updated .gitmodules failed"));
134 if (lstat(".gitmodules", &st
) < 0)
135 die_errno(_("unable to stat updated .gitmodules"));
136 fill_stat_cache_info(ce
, &st
);
137 ce
->ce_mode
= ce_mode_from_stat(ce
, st
.st_mode
);
138 if (remove_cache_entry_at(pos
) < 0)
139 die(_("unable to remove .gitmodules from index"));
140 if (write_sha1_file(buf
.buf
, buf
.len
, blob_type
, ce
->sha1
))
141 die(_("adding updated .gitmodules failed"));
142 if (add_cache_entry(ce
, ADD_CACHE_OK_TO_ADD
|ADD_CACHE_OK_TO_REPLACE
))
143 die(_("staging updated .gitmodules failed"));
146 static int add_submodule_odb(const char *path
)
148 struct strbuf objects_directory
= STRBUF_INIT
;
149 struct alternate_object_database
*alt_odb
;
153 strbuf_addf(&objects_directory
, "%s/.git", path
);
154 git_dir
= read_gitfile(objects_directory
.buf
);
156 strbuf_reset(&objects_directory
);
157 strbuf_addstr(&objects_directory
, git_dir
);
159 strbuf_addstr(&objects_directory
, "/objects/");
160 if (!is_directory(objects_directory
.buf
)) {
164 /* avoid adding it twice */
165 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
166 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
167 !strncmp(alt_odb
->base
, objects_directory
.buf
,
168 objects_directory
.len
))
171 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
172 alt_odb
->next
= alt_odb_list
;
173 strcpy(alt_odb
->base
, objects_directory
.buf
);
174 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
175 alt_odb
->name
[2] = '/';
176 alt_odb
->name
[40] = '\0';
177 alt_odb
->name
[41] = '\0';
178 alt_odb_list
= alt_odb
;
180 /* add possible alternates from the submodule */
181 read_info_alternates(objects_directory
.buf
, 0);
184 strbuf_release(&objects_directory
);
188 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
191 struct string_list_item
*path_option
, *ignore_option
;
192 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
194 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
196 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
197 else if (gitmodules_is_unmerged
)
198 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
202 int submodule_config(const char *var
, const char *value
, void *cb
)
204 if (!prefixcmp(var
, "submodule."))
205 return parse_submodule_config_option(var
, value
);
206 else if (!strcmp(var
, "fetch.recursesubmodules")) {
207 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
213 void gitmodules_config(void)
215 const char *work_tree
= get_git_work_tree();
217 struct strbuf gitmodules_path
= STRBUF_INIT
;
219 strbuf_addstr(&gitmodules_path
, work_tree
);
220 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
221 if (read_cache() < 0)
222 die("index file corrupt");
223 pos
= cache_name_pos(".gitmodules", 11);
224 if (pos
< 0) { /* .gitmodules not found or isn't merged */
226 if (active_nr
> pos
) { /* there is a .gitmodules */
227 const struct cache_entry
*ce
= active_cache
[pos
];
228 if (ce_namelen(ce
) == 11 &&
229 !memcmp(ce
->name
, ".gitmodules", 11))
230 gitmodules_is_unmerged
= 1;
232 } else if (pos
< active_nr
) {
234 if (lstat(".gitmodules", &st
) == 0 &&
235 ce_match_stat(active_cache
[pos
], &st
, 0) & DATA_CHANGED
)
236 gitmodules_is_modified
= 1;
239 if (!gitmodules_is_unmerged
)
240 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
241 strbuf_release(&gitmodules_path
);
245 int parse_submodule_config_option(const char *var
, const char *value
)
247 struct string_list_item
*config
;
248 const char *name
, *key
;
251 if (parse_config_key(var
, "submodule", &name
, &namelen
, &key
) < 0 || !name
)
254 if (!strcmp(key
, "path")) {
255 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
259 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
260 config
->util
= xmemdupz(name
, namelen
);
261 } else if (!strcmp(key
, "fetchrecursesubmodules")) {
262 char *name_cstr
= xmemdupz(name
, namelen
);
263 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name_cstr
);
265 config
= string_list_append(&config_fetch_recurse_submodules_for_name
, name_cstr
);
268 config
->util
= (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var
, value
);
269 } else if (!strcmp(key
, "ignore")) {
272 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
273 strcmp(value
, "all") && strcmp(value
, "none")) {
274 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
278 name_cstr
= xmemdupz(name
, namelen
);
279 config
= unsorted_string_list_lookup(&config_ignore_for_name
, name_cstr
);
284 config
= string_list_append(&config_ignore_for_name
, name_cstr
);
285 config
->util
= xstrdup(value
);
291 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
294 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
295 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
296 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
298 if (!strcmp(arg
, "all"))
299 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
300 else if (!strcmp(arg
, "untracked"))
301 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
302 else if (!strcmp(arg
, "dirty"))
303 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
304 else if (strcmp(arg
, "none"))
305 die("bad --ignore-submodules argument: %s", arg
);
308 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
309 struct commit
*left
, struct commit
*right
,
310 int *fast_forward
, int *fast_backward
)
312 struct commit_list
*merge_bases
, *list
;
314 init_revisions(rev
, NULL
);
315 setup_revisions(0, NULL
, rev
, NULL
);
317 rev
->first_parent_only
= 1;
318 left
->object
.flags
|= SYMMETRIC_LEFT
;
319 add_pending_object(rev
, &left
->object
, path
);
320 add_pending_object(rev
, &right
->object
, path
);
321 merge_bases
= get_merge_bases(left
, right
, 1);
323 if (merge_bases
->item
== left
)
325 else if (merge_bases
->item
== right
)
328 for (list
= merge_bases
; list
; list
= list
->next
) {
329 list
->item
->object
.flags
|= UNINTERESTING
;
330 add_pending_object(rev
, &list
->item
->object
,
331 sha1_to_hex(list
->item
->object
.sha1
));
333 return prepare_revision_walk(rev
);
336 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
337 const char *line_prefix
,
338 const char *del
, const char *add
, const char *reset
)
340 static const char format
[] = " %m %s";
341 struct strbuf sb
= STRBUF_INIT
;
342 struct commit
*commit
;
344 while ((commit
= get_revision(rev
))) {
345 struct pretty_print_context ctx
= {0};
346 ctx
.date_mode
= rev
->date_mode
;
347 ctx
.output_encoding
= get_log_output_encoding();
348 strbuf_setlen(&sb
, 0);
349 strbuf_addstr(&sb
, line_prefix
);
350 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
352 strbuf_addstr(&sb
, del
);
355 strbuf_addstr(&sb
, add
);
356 format_commit_message(commit
, format
, &sb
, &ctx
);
358 strbuf_addstr(&sb
, reset
);
359 strbuf_addch(&sb
, '\n');
360 fprintf(f
, "%s", sb
.buf
);
365 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
367 switch (git_config_maybe_bool(opt
, arg
)) {
369 return RECURSE_SUBMODULES_ON
;
371 return RECURSE_SUBMODULES_OFF
;
373 if (!strcmp(arg
, "on-demand"))
374 return RECURSE_SUBMODULES_ON_DEMAND
;
375 die("bad %s argument: %s", opt
, arg
);
379 void show_submodule_summary(FILE *f
, const char *path
,
380 const char *line_prefix
,
381 unsigned char one
[20], unsigned char two
[20],
382 unsigned dirty_submodule
, const char *meta
,
383 const char *del
, const char *add
, const char *reset
)
386 struct commit
*left
= NULL
, *right
= NULL
;
387 const char *message
= NULL
;
388 struct strbuf sb
= STRBUF_INIT
;
389 int fast_forward
= 0, fast_backward
= 0;
391 if (is_null_sha1(two
))
392 message
= "(submodule deleted)";
393 else if (add_submodule_odb(path
))
394 message
= "(not checked out)";
395 else if (is_null_sha1(one
))
396 message
= "(new submodule)";
397 else if (!(left
= lookup_commit_reference(one
)) ||
398 !(right
= lookup_commit_reference(two
)))
399 message
= "(commits not present)";
400 else if (prepare_submodule_summary(&rev
, path
, left
, right
,
401 &fast_forward
, &fast_backward
))
402 message
= "(revision walker failed)";
404 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
405 fprintf(f
, "%sSubmodule %s contains untracked content\n",
407 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
408 fprintf(f
, "%sSubmodule %s contains modified content\n",
411 if (!hashcmp(one
, two
)) {
416 strbuf_addf(&sb
, "%s%sSubmodule %s %s..", line_prefix
, meta
, path
,
417 find_unique_abbrev(one
, DEFAULT_ABBREV
));
418 if (!fast_backward
&& !fast_forward
)
419 strbuf_addch(&sb
, '.');
420 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
422 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
424 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
425 fwrite(sb
.buf
, sb
.len
, 1, f
);
427 if (!message
) /* only NULL if we succeeded in setting up the walk */
428 print_submodule_summary(&rev
, f
, line_prefix
, del
, add
, reset
);
430 clear_commit_marks(left
, ~0);
432 clear_commit_marks(right
, ~0);
437 void set_config_fetch_recurse_submodules(int value
)
439 config_fetch_recurse_submodules
= value
;
442 static int has_remote(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
447 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
449 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
452 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
453 struct child_process cp
;
454 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
455 struct strbuf buf
= STRBUF_INIT
;
456 int needs_pushing
= 0;
458 argv
[1] = sha1_to_hex(sha1
);
459 memset(&cp
, 0, sizeof(cp
));
461 cp
.env
= local_repo_env
;
466 if (start_command(&cp
))
467 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
468 sha1_to_hex(sha1
), path
);
469 if (strbuf_read(&buf
, cp
.out
, 41))
473 strbuf_release(&buf
);
474 return needs_pushing
;
480 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
481 struct diff_options
*options
,
485 struct string_list
*needs_pushing
= data
;
487 for (i
= 0; i
< q
->nr
; i
++) {
488 struct diff_filepair
*p
= q
->queue
[i
];
489 if (!S_ISGITLINK(p
->two
->mode
))
491 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
))
492 string_list_insert(needs_pushing
, p
->two
->path
);
496 static void find_unpushed_submodule_commits(struct commit
*commit
,
497 struct string_list
*needs_pushing
)
501 init_revisions(&rev
, NULL
);
502 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
503 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
504 rev
.diffopt
.format_callback_data
= needs_pushing
;
505 diff_tree_combined_merge(commit
, 1, &rev
);
508 int find_unpushed_submodules(unsigned char new_sha1
[20],
509 const char *remotes_name
, struct string_list
*needs_pushing
)
512 struct commit
*commit
;
513 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
514 int argc
= ARRAY_SIZE(argv
) - 1;
517 struct strbuf remotes_arg
= STRBUF_INIT
;
519 strbuf_addf(&remotes_arg
, "--remotes=%s", remotes_name
);
520 init_revisions(&rev
, NULL
);
521 sha1_copy
= xstrdup(sha1_to_hex(new_sha1
));
523 argv
[3] = remotes_arg
.buf
;
524 setup_revisions(argc
, argv
, &rev
, NULL
);
525 if (prepare_revision_walk(&rev
))
526 die("revision walk setup failed");
528 while ((commit
= get_revision(&rev
)) != NULL
)
529 find_unpushed_submodule_commits(commit
, needs_pushing
);
531 reset_revision_walk();
533 strbuf_release(&remotes_arg
);
535 return needs_pushing
->nr
;
538 static int push_submodule(const char *path
)
540 if (add_submodule_odb(path
))
543 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
544 struct child_process cp
;
545 const char *argv
[] = {"push", NULL
};
547 memset(&cp
, 0, sizeof(cp
));
549 cp
.env
= local_repo_env
;
553 if (run_command(&cp
))
561 int push_unpushed_submodules(unsigned char new_sha1
[20], const char *remotes_name
)
564 struct string_list needs_pushing
;
566 memset(&needs_pushing
, 0, sizeof(struct string_list
));
567 needs_pushing
.strdup_strings
= 1;
569 if (!find_unpushed_submodules(new_sha1
, remotes_name
, &needs_pushing
))
572 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
573 const char *path
= needs_pushing
.items
[i
].string
;
574 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
575 if (!push_submodule(path
)) {
576 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
581 string_list_clear(&needs_pushing
, 0);
586 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
589 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
590 /* Even if the submodule is checked out and the commit is
591 * present, make sure it is reachable from a ref. */
592 struct child_process cp
;
593 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
594 struct strbuf buf
= STRBUF_INIT
;
596 argv
[3] = sha1_to_hex(sha1
);
597 memset(&cp
, 0, sizeof(cp
));
599 cp
.env
= local_repo_env
;
604 if (!run_command(&cp
) && !strbuf_read(&buf
, cp
.out
, 1024))
608 strbuf_release(&buf
);
613 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
614 struct diff_options
*options
,
618 for (i
= 0; i
< q
->nr
; i
++) {
619 struct diff_filepair
*p
= q
->queue
[i
];
620 if (!S_ISGITLINK(p
->two
->mode
))
623 if (S_ISGITLINK(p
->one
->mode
)) {
624 /* NEEDSWORK: We should honor the name configured in
625 * the .gitmodules file of the commit we are examining
626 * here to be able to correctly follow submodules
627 * being moved around. */
628 struct string_list_item
*path
;
629 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
630 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
631 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
633 /* Submodule is new or was moved here */
634 /* NEEDSWORK: When the .git directories of submodules
635 * live inside the superprojects .git directory some
636 * day we should fetch new submodules directly into
637 * that location too when config or options request
638 * that so they can be checked out from there. */
644 static int add_sha1_to_array(const char *ref
, const unsigned char *sha1
,
645 int flags
, void *data
)
647 sha1_array_append(data
, sha1
);
651 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
653 if (!initialized_fetch_ref_tips
) {
654 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
655 initialized_fetch_ref_tips
= 1;
658 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
661 static void add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
663 argv_array_push(data
, sha1_to_hex(sha1
));
666 static void calculate_changed_submodule_paths(void)
669 struct commit
*commit
;
670 struct argv_array argv
= ARGV_ARRAY_INIT
;
672 /* No need to check if there are no submodules configured */
673 if (!config_name_for_path
.nr
)
676 init_revisions(&rev
, NULL
);
677 argv_array_push(&argv
, "--"); /* argv[0] program name */
678 sha1_array_for_each_unique(&ref_tips_after_fetch
,
679 add_sha1_to_argv
, &argv
);
680 argv_array_push(&argv
, "--not");
681 sha1_array_for_each_unique(&ref_tips_before_fetch
,
682 add_sha1_to_argv
, &argv
);
683 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
684 if (prepare_revision_walk(&rev
))
685 die("revision walk setup failed");
688 * Collect all submodules (whether checked out or not) for which new
689 * commits have been recorded upstream in "changed_submodule_paths".
691 while ((commit
= get_revision(&rev
))) {
692 struct commit_list
*parent
= commit
->parents
;
694 struct diff_options diff_opts
;
695 diff_setup(&diff_opts
);
696 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
697 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
698 diff_opts
.format_callback
= submodule_collect_changed_cb
;
699 diff_setup_done(&diff_opts
);
700 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
701 diffcore_std(&diff_opts
);
702 diff_flush(&diff_opts
);
703 parent
= parent
->next
;
707 argv_array_clear(&argv
);
708 sha1_array_clear(&ref_tips_before_fetch
);
709 sha1_array_clear(&ref_tips_after_fetch
);
710 initialized_fetch_ref_tips
= 0;
713 int fetch_populated_submodules(const struct argv_array
*options
,
714 const char *prefix
, int command_line_option
,
718 struct child_process cp
;
719 struct argv_array argv
= ARGV_ARRAY_INIT
;
720 struct string_list_item
*name_for_path
;
721 const char *work_tree
= get_git_work_tree();
725 if (read_cache() < 0)
726 die("index file corrupt");
728 argv_array_push(&argv
, "fetch");
729 for (i
= 0; i
< options
->argc
; i
++)
730 argv_array_push(&argv
, options
->argv
[i
]);
731 argv_array_push(&argv
, "--recurse-submodules-default");
732 /* default value, "--submodule-prefix" and its value are added later */
734 memset(&cp
, 0, sizeof(cp
));
735 cp
.env
= local_repo_env
;
739 calculate_changed_submodule_paths();
741 for (i
= 0; i
< active_nr
; i
++) {
742 struct strbuf submodule_path
= STRBUF_INIT
;
743 struct strbuf submodule_git_dir
= STRBUF_INIT
;
744 struct strbuf submodule_prefix
= STRBUF_INIT
;
745 struct cache_entry
*ce
= active_cache
[i
];
746 const char *git_dir
, *name
, *default_argv
;
748 if (!S_ISGITLINK(ce
->ce_mode
))
752 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
754 name
= name_for_path
->util
;
756 default_argv
= "yes";
757 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
758 struct string_list_item
*fetch_recurse_submodules_option
;
759 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
760 if (fetch_recurse_submodules_option
) {
761 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_OFF
)
763 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_ON_DEMAND
) {
764 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
766 default_argv
= "on-demand";
769 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
770 gitmodules_is_unmerged
)
772 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
773 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
775 default_argv
= "on-demand";
778 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
779 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
781 default_argv
= "on-demand";
784 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
785 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
786 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
787 git_dir
= read_gitfile(submodule_git_dir
.buf
);
789 git_dir
= submodule_git_dir
.buf
;
790 if (is_directory(git_dir
)) {
792 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
793 cp
.dir
= submodule_path
.buf
;
794 argv_array_push(&argv
, default_argv
);
795 argv_array_push(&argv
, "--submodule-prefix");
796 argv_array_push(&argv
, submodule_prefix
.buf
);
798 if (run_command(&cp
))
800 argv_array_pop(&argv
);
801 argv_array_pop(&argv
);
802 argv_array_pop(&argv
);
804 strbuf_release(&submodule_path
);
805 strbuf_release(&submodule_git_dir
);
806 strbuf_release(&submodule_prefix
);
808 argv_array_clear(&argv
);
810 string_list_clear(&changed_submodule_paths
, 1);
814 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
817 struct child_process cp
;
818 const char *argv
[] = {
824 struct strbuf buf
= STRBUF_INIT
;
825 unsigned dirty_submodule
= 0;
826 const char *line
, *next_line
;
829 strbuf_addf(&buf
, "%s/.git", path
);
830 git_dir
= read_gitfile(buf
.buf
);
833 if (!is_directory(git_dir
)) {
834 strbuf_release(&buf
);
835 /* The submodule is not checked out, so it is not modified */
841 if (ignore_untracked
)
844 memset(&cp
, 0, sizeof(cp
));
846 cp
.env
= local_repo_env
;
851 if (start_command(&cp
))
852 die("Could not run 'git status --porcelain' in submodule %s", path
);
854 len
= strbuf_read(&buf
, cp
.out
, 1024);
857 if ((line
[0] == '?') && (line
[1] == '?')) {
858 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
859 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
862 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
863 if (ignore_untracked
||
864 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
867 next_line
= strchr(line
, '\n');
871 len
-= (next_line
- line
);
876 if (finish_command(&cp
))
877 die("'git status --porcelain' failed in submodule %s", path
);
879 strbuf_release(&buf
);
880 return dirty_submodule
;
883 int submodule_uses_gitfile(const char *path
)
885 struct child_process cp
;
886 const char *argv
[] = {
894 struct strbuf buf
= STRBUF_INIT
;
897 strbuf_addf(&buf
, "%s/.git", path
);
898 git_dir
= read_gitfile(buf
.buf
);
900 strbuf_release(&buf
);
903 strbuf_release(&buf
);
905 /* Now test that all nested submodules use a gitfile too */
906 memset(&cp
, 0, sizeof(cp
));
908 cp
.env
= local_repo_env
;
914 if (run_command(&cp
))
920 int ok_to_remove_submodule(const char *path
)
924 struct child_process cp
;
925 const char *argv
[] = {
929 "--ignore-submodules=none",
932 struct strbuf buf
= STRBUF_INIT
;
933 int ok_to_remove
= 1;
935 if ((lstat(path
, &st
) < 0) || is_empty_dir(path
))
938 if (!submodule_uses_gitfile(path
))
941 memset(&cp
, 0, sizeof(cp
));
943 cp
.env
= local_repo_env
;
948 if (start_command(&cp
))
949 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path
);
951 len
= strbuf_read(&buf
, cp
.out
, 1024);
956 if (finish_command(&cp
))
957 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path
);
959 strbuf_release(&buf
);
963 static int find_first_merges(struct object_array
*result
, const char *path
,
964 struct commit
*a
, struct commit
*b
)
967 struct object_array merges
= OBJECT_ARRAY_INIT
;
968 struct commit
*commit
;
969 int contains_another
;
971 char merged_revision
[42];
972 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
973 "--all", merged_revision
, NULL
};
974 struct rev_info revs
;
975 struct setup_revision_opt rev_opts
;
977 memset(result
, 0, sizeof(struct object_array
));
978 memset(&rev_opts
, 0, sizeof(rev_opts
));
980 /* get all revisions that merge commit a */
981 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
982 sha1_to_hex(a
->object
.sha1
));
983 init_revisions(&revs
, NULL
);
984 rev_opts
.submodule
= path
;
985 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
987 /* save all revisions from the above list that contain b */
988 if (prepare_revision_walk(&revs
))
989 die("revision walk setup failed");
990 while ((commit
= get_revision(&revs
)) != NULL
) {
991 struct object
*o
= &(commit
->object
);
992 if (in_merge_bases(b
, commit
))
993 add_object_array(o
, NULL
, &merges
);
995 reset_revision_walk();
997 /* Now we've got all merges that contain a and b. Prune all
998 * merges that contain another found merge and save them in
1001 for (i
= 0; i
< merges
.nr
; i
++) {
1002 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1004 contains_another
= 0;
1005 for (j
= 0; j
< merges
.nr
; j
++) {
1006 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1007 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
1008 contains_another
= 1;
1013 if (!contains_another
)
1014 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1017 free(merges
.objects
);
1021 static void print_commit(struct commit
*commit
)
1023 struct strbuf sb
= STRBUF_INIT
;
1024 struct pretty_print_context ctx
= {0};
1025 ctx
.date_mode
= DATE_NORMAL
;
1026 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1027 fprintf(stderr
, "%s\n", sb
.buf
);
1028 strbuf_release(&sb
);
1031 #define MERGE_WARNING(path, msg) \
1032 warning("Failed to merge submodule %s (%s)", path, msg);
1034 int merge_submodule(unsigned char result
[20], const char *path
,
1035 const unsigned char base
[20], const unsigned char a
[20],
1036 const unsigned char b
[20], int search
)
1038 struct commit
*commit_base
, *commit_a
, *commit_b
;
1040 struct object_array merges
;
1044 /* store a in result in case we fail */
1047 /* we can not handle deletion conflicts */
1048 if (is_null_sha1(base
))
1050 if (is_null_sha1(a
))
1052 if (is_null_sha1(b
))
1055 if (add_submodule_odb(path
)) {
1056 MERGE_WARNING(path
, "not checked out");
1060 if (!(commit_base
= lookup_commit_reference(base
)) ||
1061 !(commit_a
= lookup_commit_reference(a
)) ||
1062 !(commit_b
= lookup_commit_reference(b
))) {
1063 MERGE_WARNING(path
, "commits not present");
1067 /* check whether both changes are forward */
1068 if (!in_merge_bases(commit_base
, commit_a
) ||
1069 !in_merge_bases(commit_base
, commit_b
)) {
1070 MERGE_WARNING(path
, "commits don't follow merge-base");
1074 /* Case #1: a is contained in b or vice versa */
1075 if (in_merge_bases(commit_a
, commit_b
)) {
1079 if (in_merge_bases(commit_b
, commit_a
)) {
1085 * Case #2: There are one or more merges that contain a and b in
1086 * the submodule. If there is only one, then present it as a
1087 * suggestion to the user, but leave it marked unmerged so the
1088 * user needs to confirm the resolution.
1091 /* Skip the search if makes no sense to the calling context. */
1095 /* find commit which merges them */
1096 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
1097 switch (parent_count
) {
1099 MERGE_WARNING(path
, "merge following commits not found");
1103 MERGE_WARNING(path
, "not fast-forward");
1104 fprintf(stderr
, "Found a possible merge resolution "
1105 "for the submodule:\n");
1106 print_commit((struct commit
*) merges
.objects
[0].item
);
1108 "If this is correct simply add it to the index "
1111 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1112 "which will accept this suggestion.\n",
1113 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
1117 MERGE_WARNING(path
, "multiple merges found");
1118 for (i
= 0; i
< merges
.nr
; i
++)
1119 print_commit((struct commit
*) merges
.objects
[i
].item
);
1122 free(merges
.objects
);
1126 /* Update gitfile and core.worktree setting to connect work tree and git dir */
1127 void connect_work_tree_and_git_dir(const char *work_tree
, const char *git_dir
)
1129 struct strbuf file_name
= STRBUF_INIT
;
1130 struct strbuf rel_path
= STRBUF_INIT
;
1131 const char *real_work_tree
= xstrdup(real_path(work_tree
));
1134 /* Update gitfile */
1135 strbuf_addf(&file_name
, "%s/.git", work_tree
);
1136 fp
= fopen(file_name
.buf
, "w");
1138 die(_("Could not create git link %s"), file_name
.buf
);
1139 fprintf(fp
, "gitdir: %s\n", relative_path(git_dir
, real_work_tree
,
1143 /* Update core.worktree setting */
1144 strbuf_reset(&file_name
);
1145 strbuf_addf(&file_name
, "%s/config", git_dir
);
1146 if (git_config_set_in_file(file_name
.buf
, "core.worktree",
1147 relative_path(real_work_tree
, git_dir
,
1149 die(_("Could not set core.worktree in %s"),
1152 strbuf_release(&file_name
);
1153 strbuf_release(&rel_path
);
1154 free((void *)real_work_tree
);