7 #include "run-command.h"
10 #include "string-list.h"
11 #include "sha1-array.h"
12 #include "argv-array.h"
14 static struct string_list config_name_for_path
;
15 static struct string_list config_fetch_recurse_submodules_for_name
;
16 static struct string_list config_ignore_for_name
;
17 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
18 static struct string_list changed_submodule_paths
;
19 static int initialized_fetch_ref_tips
;
20 static struct sha1_array ref_tips_before_fetch
;
21 static struct sha1_array ref_tips_after_fetch
;
24 * The following flag is set if the .gitmodules file is unmerged. We then
25 * disable recursion for all submodules where .git/config doesn't have a
26 * matching config entry because we can't guess what might be configured in
27 * .gitmodules unless the user resolves the conflict. When a command line
28 * option is given (which always overrides configuration) this flag will be
31 static int gitmodules_is_unmerged
;
33 static int add_submodule_odb(const char *path
)
35 struct strbuf objects_directory
= STRBUF_INIT
;
36 struct alternate_object_database
*alt_odb
;
40 strbuf_addf(&objects_directory
, "%s/.git", path
);
41 git_dir
= read_gitfile(objects_directory
.buf
);
43 strbuf_reset(&objects_directory
);
44 strbuf_addstr(&objects_directory
, git_dir
);
46 strbuf_addstr(&objects_directory
, "/objects/");
47 if (!is_directory(objects_directory
.buf
)) {
51 /* avoid adding it twice */
52 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
53 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
54 !strncmp(alt_odb
->base
, objects_directory
.buf
,
55 objects_directory
.len
))
58 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
59 alt_odb
->next
= alt_odb_list
;
60 strcpy(alt_odb
->base
, objects_directory
.buf
);
61 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
62 alt_odb
->name
[2] = '/';
63 alt_odb
->name
[40] = '\0';
64 alt_odb
->name
[41] = '\0';
65 alt_odb_list
= alt_odb
;
67 /* add possible alternates from the submodule */
68 read_info_alternates(objects_directory
.buf
, 0);
71 strbuf_release(&objects_directory
);
75 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
78 struct string_list_item
*path_option
, *ignore_option
;
79 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
81 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
83 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
84 else if (gitmodules_is_unmerged
)
85 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
89 int submodule_config(const char *var
, const char *value
, void *cb
)
91 if (!prefixcmp(var
, "submodule."))
92 return parse_submodule_config_option(var
, value
);
93 else if (!strcmp(var
, "fetch.recursesubmodules")) {
94 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
100 void gitmodules_config(void)
102 const char *work_tree
= get_git_work_tree();
104 struct strbuf gitmodules_path
= STRBUF_INIT
;
106 strbuf_addstr(&gitmodules_path
, work_tree
);
107 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
108 if (read_cache() < 0)
109 die("index file corrupt");
110 pos
= cache_name_pos(".gitmodules", 11);
111 if (pos
< 0) { /* .gitmodules not found or isn't merged */
113 if (active_nr
> pos
) { /* there is a .gitmodules */
114 const struct cache_entry
*ce
= active_cache
[pos
];
115 if (ce_namelen(ce
) == 11 &&
116 !memcmp(ce
->name
, ".gitmodules", 11))
117 gitmodules_is_unmerged
= 1;
121 if (!gitmodules_is_unmerged
)
122 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
123 strbuf_release(&gitmodules_path
);
127 int parse_submodule_config_option(const char *var
, const char *value
)
130 struct string_list_item
*config
;
131 struct strbuf submodname
= STRBUF_INIT
;
133 var
+= 10; /* Skip "submodule." */
136 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
137 strbuf_add(&submodname
, var
, len
- 5);
138 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
142 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
143 config
->util
= strbuf_detach(&submodname
, NULL
);
144 strbuf_release(&submodname
);
145 } else if ((len
> 23) && !strcmp(var
+ len
- 23, ".fetchrecursesubmodules")) {
146 strbuf_add(&submodname
, var
, len
- 23);
147 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, submodname
.buf
);
149 config
= string_list_append(&config_fetch_recurse_submodules_for_name
,
150 strbuf_detach(&submodname
, NULL
));
151 config
->util
= (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var
, value
);
152 strbuf_release(&submodname
);
153 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
154 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
155 strcmp(value
, "all") && strcmp(value
, "none")) {
156 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
160 strbuf_add(&submodname
, var
, len
- 7);
161 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
165 config
= string_list_append(&config_ignore_for_name
,
166 strbuf_detach(&submodname
, NULL
));
167 strbuf_release(&submodname
);
168 config
->util
= xstrdup(value
);
174 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
177 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
178 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
179 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
181 if (!strcmp(arg
, "all"))
182 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
183 else if (!strcmp(arg
, "untracked"))
184 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
185 else if (!strcmp(arg
, "dirty"))
186 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
187 else if (strcmp(arg
, "none"))
188 die("bad --ignore-submodules argument: %s", arg
);
191 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
192 struct commit
*left
, struct commit
*right
,
193 int *fast_forward
, int *fast_backward
)
195 struct commit_list
*merge_bases
, *list
;
197 init_revisions(rev
, NULL
);
198 setup_revisions(0, NULL
, rev
, NULL
);
200 rev
->first_parent_only
= 1;
201 left
->object
.flags
|= SYMMETRIC_LEFT
;
202 add_pending_object(rev
, &left
->object
, path
);
203 add_pending_object(rev
, &right
->object
, path
);
204 merge_bases
= get_merge_bases(left
, right
, 1);
206 if (merge_bases
->item
== left
)
208 else if (merge_bases
->item
== right
)
211 for (list
= merge_bases
; list
; list
= list
->next
) {
212 list
->item
->object
.flags
|= UNINTERESTING
;
213 add_pending_object(rev
, &list
->item
->object
,
214 sha1_to_hex(list
->item
->object
.sha1
));
216 return prepare_revision_walk(rev
);
219 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
220 const char *del
, const char *add
, const char *reset
)
222 static const char format
[] = " %m %s";
223 struct strbuf sb
= STRBUF_INIT
;
224 struct commit
*commit
;
226 while ((commit
= get_revision(rev
))) {
227 struct pretty_print_context ctx
= {0};
228 ctx
.date_mode
= rev
->date_mode
;
229 strbuf_setlen(&sb
, 0);
230 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
232 strbuf_addstr(&sb
, del
);
235 strbuf_addstr(&sb
, add
);
236 format_commit_message(commit
, format
, &sb
, &ctx
);
238 strbuf_addstr(&sb
, reset
);
239 strbuf_addch(&sb
, '\n');
240 fprintf(f
, "%s", sb
.buf
);
245 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
247 switch (git_config_maybe_bool(opt
, arg
)) {
249 return RECURSE_SUBMODULES_ON
;
251 return RECURSE_SUBMODULES_OFF
;
253 if (!strcmp(arg
, "on-demand"))
254 return RECURSE_SUBMODULES_ON_DEMAND
;
255 die("bad %s argument: %s", opt
, arg
);
259 void show_submodule_summary(FILE *f
, const char *path
,
260 unsigned char one
[20], unsigned char two
[20],
261 unsigned dirty_submodule
,
262 const char *del
, const char *add
, const char *reset
)
265 struct commit
*left
= left
, *right
= right
;
266 const char *message
= NULL
;
267 struct strbuf sb
= STRBUF_INIT
;
268 int fast_forward
= 0, fast_backward
= 0;
270 if (is_null_sha1(two
))
271 message
= "(submodule deleted)";
272 else if (add_submodule_odb(path
))
273 message
= "(not checked out)";
274 else if (is_null_sha1(one
))
275 message
= "(new submodule)";
276 else if (!(left
= lookup_commit_reference(one
)) ||
277 !(right
= lookup_commit_reference(two
)))
278 message
= "(commits not present)";
281 prepare_submodule_summary(&rev
, path
, left
, right
,
282 &fast_forward
, &fast_backward
))
283 message
= "(revision walker failed)";
285 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
286 fprintf(f
, "Submodule %s contains untracked content\n", path
);
287 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
288 fprintf(f
, "Submodule %s contains modified content\n", path
);
290 if (!hashcmp(one
, two
)) {
295 strbuf_addf(&sb
, "Submodule %s %s..", path
,
296 find_unique_abbrev(one
, DEFAULT_ABBREV
));
297 if (!fast_backward
&& !fast_forward
)
298 strbuf_addch(&sb
, '.');
299 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
301 strbuf_addf(&sb
, " %s\n", message
);
303 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
304 fwrite(sb
.buf
, sb
.len
, 1, f
);
307 print_submodule_summary(&rev
, f
, del
, add
, reset
);
308 clear_commit_marks(left
, ~0);
309 clear_commit_marks(right
, ~0);
315 void set_config_fetch_recurse_submodules(int value
)
317 config_fetch_recurse_submodules
= value
;
320 static int has_remote(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
325 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
327 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
330 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
331 struct child_process cp
;
332 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
333 struct strbuf buf
= STRBUF_INIT
;
334 int needs_pushing
= 0;
336 argv
[1] = sha1_to_hex(sha1
);
337 memset(&cp
, 0, sizeof(cp
));
339 cp
.env
= local_repo_env
;
344 if (start_command(&cp
))
345 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
346 sha1_to_hex(sha1
), path
);
347 if (strbuf_read(&buf
, cp
.out
, 41))
351 strbuf_release(&buf
);
352 return needs_pushing
;
358 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
359 struct diff_options
*options
,
363 struct string_list
*needs_pushing
= data
;
365 for (i
= 0; i
< q
->nr
; i
++) {
366 struct diff_filepair
*p
= q
->queue
[i
];
367 if (!S_ISGITLINK(p
->two
->mode
))
369 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
))
370 string_list_insert(needs_pushing
, p
->two
->path
);
374 static void find_unpushed_submodule_commits(struct commit
*commit
,
375 struct string_list
*needs_pushing
)
379 init_revisions(&rev
, NULL
);
380 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
381 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
382 rev
.diffopt
.format_callback_data
= needs_pushing
;
383 diff_tree_combined_merge(commit
, 1, &rev
);
386 int find_unpushed_submodules(unsigned char new_sha1
[20],
387 const char *remotes_name
, struct string_list
*needs_pushing
)
390 struct commit
*commit
;
391 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
392 int argc
= ARRAY_SIZE(argv
) - 1;
395 struct strbuf remotes_arg
= STRBUF_INIT
;
397 strbuf_addf(&remotes_arg
, "--remotes=%s", remotes_name
);
398 init_revisions(&rev
, NULL
);
399 sha1_copy
= xstrdup(sha1_to_hex(new_sha1
));
401 argv
[3] = remotes_arg
.buf
;
402 setup_revisions(argc
, argv
, &rev
, NULL
);
403 if (prepare_revision_walk(&rev
))
404 die("revision walk setup failed");
406 while ((commit
= get_revision(&rev
)) != NULL
)
407 find_unpushed_submodule_commits(commit
, needs_pushing
);
409 reset_revision_walk();
411 strbuf_release(&remotes_arg
);
413 return needs_pushing
->nr
;
416 static int push_submodule(const char *path
)
418 if (add_submodule_odb(path
))
421 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
422 struct child_process cp
;
423 const char *argv
[] = {"push", NULL
};
425 memset(&cp
, 0, sizeof(cp
));
427 cp
.env
= local_repo_env
;
431 if (run_command(&cp
))
439 int push_unpushed_submodules(unsigned char new_sha1
[20], const char *remotes_name
)
442 struct string_list needs_pushing
;
444 memset(&needs_pushing
, 0, sizeof(struct string_list
));
445 needs_pushing
.strdup_strings
= 1;
447 if (!find_unpushed_submodules(new_sha1
, remotes_name
, &needs_pushing
))
450 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
451 const char *path
= needs_pushing
.items
[i
].string
;
452 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
453 if (!push_submodule(path
)) {
454 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
459 string_list_clear(&needs_pushing
, 0);
464 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
467 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
468 /* Even if the submodule is checked out and the commit is
469 * present, make sure it is reachable from a ref. */
470 struct child_process cp
;
471 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
472 struct strbuf buf
= STRBUF_INIT
;
474 argv
[3] = sha1_to_hex(sha1
);
475 memset(&cp
, 0, sizeof(cp
));
477 cp
.env
= local_repo_env
;
482 if (!run_command(&cp
) && !strbuf_read(&buf
, cp
.out
, 1024))
486 strbuf_release(&buf
);
491 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
492 struct diff_options
*options
,
496 for (i
= 0; i
< q
->nr
; i
++) {
497 struct diff_filepair
*p
= q
->queue
[i
];
498 if (!S_ISGITLINK(p
->two
->mode
))
501 if (S_ISGITLINK(p
->one
->mode
)) {
502 /* NEEDSWORK: We should honor the name configured in
503 * the .gitmodules file of the commit we are examining
504 * here to be able to correctly follow submodules
505 * being moved around. */
506 struct string_list_item
*path
;
507 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
508 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
509 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
511 /* Submodule is new or was moved here */
512 /* NEEDSWORK: When the .git directories of submodules
513 * live inside the superprojects .git directory some
514 * day we should fetch new submodules directly into
515 * that location too when config or options request
516 * that so they can be checked out from there. */
522 static int add_sha1_to_array(const char *ref
, const unsigned char *sha1
,
523 int flags
, void *data
)
525 sha1_array_append(data
, sha1
);
529 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
531 if (!initialized_fetch_ref_tips
) {
532 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
533 initialized_fetch_ref_tips
= 1;
536 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
539 static void add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
541 argv_array_push(data
, sha1_to_hex(sha1
));
544 static void calculate_changed_submodule_paths(void)
547 struct commit
*commit
;
548 struct argv_array argv
= ARGV_ARRAY_INIT
;
550 /* No need to check if there are no submodules configured */
551 if (!config_name_for_path
.nr
)
554 init_revisions(&rev
, NULL
);
555 argv_array_push(&argv
, "--"); /* argv[0] program name */
556 sha1_array_for_each_unique(&ref_tips_after_fetch
,
557 add_sha1_to_argv
, &argv
);
558 argv_array_push(&argv
, "--not");
559 sha1_array_for_each_unique(&ref_tips_before_fetch
,
560 add_sha1_to_argv
, &argv
);
561 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
562 if (prepare_revision_walk(&rev
))
563 die("revision walk setup failed");
566 * Collect all submodules (whether checked out or not) for which new
567 * commits have been recorded upstream in "changed_submodule_paths".
569 while ((commit
= get_revision(&rev
))) {
570 struct commit_list
*parent
= commit
->parents
;
572 struct diff_options diff_opts
;
573 diff_setup(&diff_opts
);
574 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
575 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
576 diff_opts
.format_callback
= submodule_collect_changed_cb
;
577 diff_setup_done(&diff_opts
);
578 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
579 diffcore_std(&diff_opts
);
580 diff_flush(&diff_opts
);
581 parent
= parent
->next
;
585 argv_array_clear(&argv
);
586 sha1_array_clear(&ref_tips_before_fetch
);
587 sha1_array_clear(&ref_tips_after_fetch
);
588 initialized_fetch_ref_tips
= 0;
591 int fetch_populated_submodules(const struct argv_array
*options
,
592 const char *prefix
, int command_line_option
,
596 struct child_process cp
;
597 struct argv_array argv
= ARGV_ARRAY_INIT
;
598 struct string_list_item
*name_for_path
;
599 const char *work_tree
= get_git_work_tree();
603 if (!the_index
.initialized
)
604 if (read_cache() < 0)
605 die("index file corrupt");
607 argv_array_push(&argv
, "fetch");
608 for (i
= 0; i
< options
->argc
; i
++)
609 argv_array_push(&argv
, options
->argv
[i
]);
610 argv_array_push(&argv
, "--recurse-submodules-default");
611 /* default value, "--submodule-prefix" and its value are added later */
613 memset(&cp
, 0, sizeof(cp
));
614 cp
.env
= local_repo_env
;
618 calculate_changed_submodule_paths();
620 for (i
= 0; i
< active_nr
; i
++) {
621 struct strbuf submodule_path
= STRBUF_INIT
;
622 struct strbuf submodule_git_dir
= STRBUF_INIT
;
623 struct strbuf submodule_prefix
= STRBUF_INIT
;
624 struct cache_entry
*ce
= active_cache
[i
];
625 const char *git_dir
, *name
, *default_argv
;
627 if (!S_ISGITLINK(ce
->ce_mode
))
631 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
633 name
= name_for_path
->util
;
635 default_argv
= "yes";
636 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
637 struct string_list_item
*fetch_recurse_submodules_option
;
638 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
639 if (fetch_recurse_submodules_option
) {
640 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_OFF
)
642 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_ON_DEMAND
) {
643 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
645 default_argv
= "on-demand";
648 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
649 gitmodules_is_unmerged
)
651 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
652 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
654 default_argv
= "on-demand";
657 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
658 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
660 default_argv
= "on-demand";
663 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
664 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
665 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
666 git_dir
= read_gitfile(submodule_git_dir
.buf
);
668 git_dir
= submodule_git_dir
.buf
;
669 if (is_directory(git_dir
)) {
671 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
672 cp
.dir
= submodule_path
.buf
;
673 argv_array_push(&argv
, default_argv
);
674 argv_array_push(&argv
, "--submodule-prefix");
675 argv_array_push(&argv
, submodule_prefix
.buf
);
677 if (run_command(&cp
))
679 argv_array_pop(&argv
);
680 argv_array_pop(&argv
);
681 argv_array_pop(&argv
);
683 strbuf_release(&submodule_path
);
684 strbuf_release(&submodule_git_dir
);
685 strbuf_release(&submodule_prefix
);
687 argv_array_clear(&argv
);
689 string_list_clear(&changed_submodule_paths
, 1);
693 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
696 struct child_process cp
;
697 const char *argv
[] = {
703 struct strbuf buf
= STRBUF_INIT
;
704 unsigned dirty_submodule
= 0;
705 const char *line
, *next_line
;
708 strbuf_addf(&buf
, "%s/.git", path
);
709 git_dir
= read_gitfile(buf
.buf
);
712 if (!is_directory(git_dir
)) {
713 strbuf_release(&buf
);
714 /* The submodule is not checked out, so it is not modified */
720 if (ignore_untracked
)
723 memset(&cp
, 0, sizeof(cp
));
725 cp
.env
= local_repo_env
;
730 if (start_command(&cp
))
731 die("Could not run 'git status --porcelain' in submodule %s", path
);
733 len
= strbuf_read(&buf
, cp
.out
, 1024);
736 if ((line
[0] == '?') && (line
[1] == '?')) {
737 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
738 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
741 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
742 if (ignore_untracked
||
743 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
746 next_line
= strchr(line
, '\n');
750 len
-= (next_line
- line
);
755 if (finish_command(&cp
))
756 die("'git status --porcelain' failed in submodule %s", path
);
758 strbuf_release(&buf
);
759 return dirty_submodule
;
762 int submodule_uses_gitfile(const char *path
)
764 struct child_process cp
;
765 const char *argv
[] = {
773 struct strbuf buf
= STRBUF_INIT
;
776 strbuf_addf(&buf
, "%s/.git", path
);
777 git_dir
= read_gitfile(buf
.buf
);
779 strbuf_release(&buf
);
782 strbuf_release(&buf
);
784 /* Now test that all nested submodules use a gitfile too */
785 memset(&cp
, 0, sizeof(cp
));
787 cp
.env
= local_repo_env
;
793 if (run_command(&cp
))
799 int ok_to_remove_submodule(const char *path
)
803 struct child_process cp
;
804 const char *argv
[] = {
808 "--ignore-submodules=none",
811 struct strbuf buf
= STRBUF_INIT
;
812 int ok_to_remove
= 1;
814 if ((lstat(path
, &st
) < 0) || is_empty_dir(path
))
817 if (!submodule_uses_gitfile(path
))
820 memset(&cp
, 0, sizeof(cp
));
822 cp
.env
= local_repo_env
;
827 if (start_command(&cp
))
828 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path
);
830 len
= strbuf_read(&buf
, cp
.out
, 1024);
835 if (finish_command(&cp
))
836 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path
);
838 strbuf_release(&buf
);
842 static int find_first_merges(struct object_array
*result
, const char *path
,
843 struct commit
*a
, struct commit
*b
)
846 struct object_array merges
;
847 struct commit
*commit
;
848 int contains_another
;
850 char merged_revision
[42];
851 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
852 "--all", merged_revision
, NULL
};
853 struct rev_info revs
;
854 struct setup_revision_opt rev_opts
;
856 memset(&merges
, 0, sizeof(merges
));
857 memset(result
, 0, sizeof(struct object_array
));
858 memset(&rev_opts
, 0, sizeof(rev_opts
));
860 /* get all revisions that merge commit a */
861 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
862 sha1_to_hex(a
->object
.sha1
));
863 init_revisions(&revs
, NULL
);
864 rev_opts
.submodule
= path
;
865 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
867 /* save all revisions from the above list that contain b */
868 if (prepare_revision_walk(&revs
))
869 die("revision walk setup failed");
870 while ((commit
= get_revision(&revs
)) != NULL
) {
871 struct object
*o
= &(commit
->object
);
872 if (in_merge_bases(b
, commit
))
873 add_object_array(o
, NULL
, &merges
);
875 reset_revision_walk();
877 /* Now we've got all merges that contain a and b. Prune all
878 * merges that contain another found merge and save them in
881 for (i
= 0; i
< merges
.nr
; i
++) {
882 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
884 contains_another
= 0;
885 for (j
= 0; j
< merges
.nr
; j
++) {
886 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
887 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
888 contains_another
= 1;
893 if (!contains_another
)
894 add_object_array(merges
.objects
[i
].item
,
895 merges
.objects
[i
].name
, result
);
898 free(merges
.objects
);
902 static void print_commit(struct commit
*commit
)
904 struct strbuf sb
= STRBUF_INIT
;
905 struct pretty_print_context ctx
= {0};
906 ctx
.date_mode
= DATE_NORMAL
;
907 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
908 fprintf(stderr
, "%s\n", sb
.buf
);
912 #define MERGE_WARNING(path, msg) \
913 warning("Failed to merge submodule %s (%s)", path, msg);
915 int merge_submodule(unsigned char result
[20], const char *path
,
916 const unsigned char base
[20], const unsigned char a
[20],
917 const unsigned char b
[20], int search
)
919 struct commit
*commit_base
, *commit_a
, *commit_b
;
921 struct object_array merges
;
925 /* store a in result in case we fail */
928 /* we can not handle deletion conflicts */
929 if (is_null_sha1(base
))
936 if (add_submodule_odb(path
)) {
937 MERGE_WARNING(path
, "not checked out");
941 if (!(commit_base
= lookup_commit_reference(base
)) ||
942 !(commit_a
= lookup_commit_reference(a
)) ||
943 !(commit_b
= lookup_commit_reference(b
))) {
944 MERGE_WARNING(path
, "commits not present");
948 /* check whether both changes are forward */
949 if (!in_merge_bases(commit_base
, commit_a
) ||
950 !in_merge_bases(commit_base
, commit_b
)) {
951 MERGE_WARNING(path
, "commits don't follow merge-base");
955 /* Case #1: a is contained in b or vice versa */
956 if (in_merge_bases(commit_a
, commit_b
)) {
960 if (in_merge_bases(commit_b
, commit_a
)) {
966 * Case #2: There are one or more merges that contain a and b in
967 * the submodule. If there is only one, then present it as a
968 * suggestion to the user, but leave it marked unmerged so the
969 * user needs to confirm the resolution.
972 /* Skip the search if makes no sense to the calling context. */
976 /* find commit which merges them */
977 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
978 switch (parent_count
) {
980 MERGE_WARNING(path
, "merge following commits not found");
984 MERGE_WARNING(path
, "not fast-forward");
985 fprintf(stderr
, "Found a possible merge resolution "
986 "for the submodule:\n");
987 print_commit((struct commit
*) merges
.objects
[0].item
);
989 "If this is correct simply add it to the index "
992 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
993 "which will accept this suggestion.\n",
994 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
998 MERGE_WARNING(path
, "multiple merges found");
999 for (i
= 0; i
< merges
.nr
; i
++)
1000 print_commit((struct commit
*) merges
.objects
[i
].item
);
1003 free(merges
.objects
);