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
)
129 struct string_list_item
*config
;
130 const char *name
, *key
;
133 if (parse_config_key(var
, "submodule", &name
, &namelen
, &key
) < 0 || !name
)
136 if (!strcmp(key
, "path")) {
138 return config_error_nonbool(var
);
140 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
144 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
145 config
->util
= xmemdupz(name
, namelen
);
146 } else if (!strcmp(key
, "fetchrecursesubmodules")) {
147 char *name_cstr
= xmemdupz(name
, namelen
);
148 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name_cstr
);
150 config
= string_list_append(&config_fetch_recurse_submodules_for_name
, name_cstr
);
153 config
->util
= (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var
, value
);
154 } else if (!strcmp(key
, "ignore")) {
158 return config_error_nonbool(var
);
160 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
161 strcmp(value
, "all") && strcmp(value
, "none")) {
162 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
166 name_cstr
= xmemdupz(name
, namelen
);
167 config
= unsorted_string_list_lookup(&config_ignore_for_name
, name_cstr
);
172 config
= string_list_append(&config_ignore_for_name
, name_cstr
);
173 config
->util
= xstrdup(value
);
179 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
182 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
183 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
184 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
186 if (!strcmp(arg
, "all"))
187 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
188 else if (!strcmp(arg
, "untracked"))
189 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
190 else if (!strcmp(arg
, "dirty"))
191 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
192 else if (strcmp(arg
, "none"))
193 die("bad --ignore-submodules argument: %s", arg
);
196 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
197 struct commit
*left
, struct commit
*right
,
198 int *fast_forward
, int *fast_backward
)
200 struct commit_list
*merge_bases
, *list
;
202 init_revisions(rev
, NULL
);
203 setup_revisions(0, NULL
, rev
, NULL
);
205 rev
->first_parent_only
= 1;
206 left
->object
.flags
|= SYMMETRIC_LEFT
;
207 add_pending_object(rev
, &left
->object
, path
);
208 add_pending_object(rev
, &right
->object
, path
);
209 merge_bases
= get_merge_bases(left
, right
, 1);
211 if (merge_bases
->item
== left
)
213 else if (merge_bases
->item
== right
)
216 for (list
= merge_bases
; list
; list
= list
->next
) {
217 list
->item
->object
.flags
|= UNINTERESTING
;
218 add_pending_object(rev
, &list
->item
->object
,
219 sha1_to_hex(list
->item
->object
.sha1
));
221 return prepare_revision_walk(rev
);
224 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
225 const char *del
, const char *add
, const char *reset
)
227 static const char format
[] = " %m %s";
228 struct strbuf sb
= STRBUF_INIT
;
229 struct commit
*commit
;
231 while ((commit
= get_revision(rev
))) {
232 struct pretty_print_context ctx
= {0};
233 ctx
.date_mode
= rev
->date_mode
;
234 strbuf_setlen(&sb
, 0);
235 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
237 strbuf_addstr(&sb
, del
);
240 strbuf_addstr(&sb
, add
);
241 format_commit_message(commit
, format
, &sb
, &ctx
);
243 strbuf_addstr(&sb
, reset
);
244 strbuf_addch(&sb
, '\n');
245 fprintf(f
, "%s", sb
.buf
);
250 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
252 switch (git_config_maybe_bool(opt
, arg
)) {
254 return RECURSE_SUBMODULES_ON
;
256 return RECURSE_SUBMODULES_OFF
;
258 if (!strcmp(arg
, "on-demand"))
259 return RECURSE_SUBMODULES_ON_DEMAND
;
260 die("bad %s argument: %s", opt
, arg
);
264 void show_submodule_summary(FILE *f
, const char *path
,
265 unsigned char one
[20], unsigned char two
[20],
266 unsigned dirty_submodule
, const char *meta
,
267 const char *del
, const char *add
, const char *reset
)
270 struct commit
*left
= NULL
, *right
= NULL
;
271 const char *message
= NULL
;
272 struct strbuf sb
= STRBUF_INIT
;
273 int fast_forward
= 0, fast_backward
= 0;
275 if (is_null_sha1(two
))
276 message
= "(submodule deleted)";
277 else if (add_submodule_odb(path
))
278 message
= "(not checked out)";
279 else if (is_null_sha1(one
))
280 message
= "(new submodule)";
281 else if (!(left
= lookup_commit_reference(one
)) ||
282 !(right
= lookup_commit_reference(two
)))
283 message
= "(commits not present)";
284 else if (prepare_submodule_summary(&rev
, path
, left
, right
,
285 &fast_forward
, &fast_backward
))
286 message
= "(revision walker failed)";
288 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
289 fprintf(f
, "Submodule %s contains untracked content\n", path
);
290 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
291 fprintf(f
, "Submodule %s contains modified content\n", path
);
293 if (!hashcmp(one
, two
)) {
298 strbuf_addf(&sb
, "%sSubmodule %s %s..", meta
, path
,
299 find_unique_abbrev(one
, DEFAULT_ABBREV
));
300 if (!fast_backward
&& !fast_forward
)
301 strbuf_addch(&sb
, '.');
302 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
304 strbuf_addf(&sb
, " %s%s\n", message
, reset
);
306 strbuf_addf(&sb
, "%s:%s\n", fast_backward
? " (rewind)" : "", reset
);
307 fwrite(sb
.buf
, sb
.len
, 1, f
);
309 if (!message
) /* only NULL if we succeeded in setting up the walk */
310 print_submodule_summary(&rev
, f
, del
, add
, reset
);
312 clear_commit_marks(left
, ~0);
314 clear_commit_marks(right
, ~0);
319 void set_config_fetch_recurse_submodules(int value
)
321 config_fetch_recurse_submodules
= value
;
324 static int has_remote(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
329 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
331 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
334 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
335 struct child_process cp
;
336 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
337 struct strbuf buf
= STRBUF_INIT
;
338 int needs_pushing
= 0;
340 argv
[1] = sha1_to_hex(sha1
);
341 memset(&cp
, 0, sizeof(cp
));
343 cp
.env
= local_repo_env
;
348 if (start_command(&cp
))
349 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
350 sha1_to_hex(sha1
), path
);
351 if (strbuf_read(&buf
, cp
.out
, 41))
355 strbuf_release(&buf
);
356 return needs_pushing
;
362 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
363 struct diff_options
*options
,
367 struct string_list
*needs_pushing
= data
;
369 for (i
= 0; i
< q
->nr
; i
++) {
370 struct diff_filepair
*p
= q
->queue
[i
];
371 if (!S_ISGITLINK(p
->two
->mode
))
373 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
))
374 string_list_insert(needs_pushing
, p
->two
->path
);
378 static void find_unpushed_submodule_commits(struct commit
*commit
,
379 struct string_list
*needs_pushing
)
383 init_revisions(&rev
, NULL
);
384 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
385 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
386 rev
.diffopt
.format_callback_data
= needs_pushing
;
387 diff_tree_combined_merge(commit
, 1, &rev
);
390 int find_unpushed_submodules(unsigned char new_sha1
[20],
391 const char *remotes_name
, struct string_list
*needs_pushing
)
394 struct commit
*commit
;
395 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
396 int argc
= ARRAY_SIZE(argv
) - 1;
399 struct strbuf remotes_arg
= STRBUF_INIT
;
401 strbuf_addf(&remotes_arg
, "--remotes=%s", remotes_name
);
402 init_revisions(&rev
, NULL
);
403 sha1_copy
= xstrdup(sha1_to_hex(new_sha1
));
405 argv
[3] = remotes_arg
.buf
;
406 setup_revisions(argc
, argv
, &rev
, NULL
);
407 if (prepare_revision_walk(&rev
))
408 die("revision walk setup failed");
410 while ((commit
= get_revision(&rev
)) != NULL
)
411 find_unpushed_submodule_commits(commit
, needs_pushing
);
413 reset_revision_walk();
415 strbuf_release(&remotes_arg
);
417 return needs_pushing
->nr
;
420 static int push_submodule(const char *path
)
422 if (add_submodule_odb(path
))
425 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
426 struct child_process cp
;
427 const char *argv
[] = {"push", NULL
};
429 memset(&cp
, 0, sizeof(cp
));
431 cp
.env
= local_repo_env
;
435 if (run_command(&cp
))
443 int push_unpushed_submodules(unsigned char new_sha1
[20], const char *remotes_name
)
446 struct string_list needs_pushing
;
448 memset(&needs_pushing
, 0, sizeof(struct string_list
));
449 needs_pushing
.strdup_strings
= 1;
451 if (!find_unpushed_submodules(new_sha1
, remotes_name
, &needs_pushing
))
454 for (i
= 0; i
< needs_pushing
.nr
; i
++) {
455 const char *path
= needs_pushing
.items
[i
].string
;
456 fprintf(stderr
, "Pushing submodule '%s'\n", path
);
457 if (!push_submodule(path
)) {
458 fprintf(stderr
, "Unable to push submodule '%s'\n", path
);
463 string_list_clear(&needs_pushing
, 0);
468 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
471 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
472 /* Even if the submodule is checked out and the commit is
473 * present, make sure it is reachable from a ref. */
474 struct child_process cp
;
475 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
476 struct strbuf buf
= STRBUF_INIT
;
478 argv
[3] = sha1_to_hex(sha1
);
479 memset(&cp
, 0, sizeof(cp
));
481 cp
.env
= local_repo_env
;
486 if (!run_command(&cp
) && !strbuf_read(&buf
, cp
.out
, 1024))
490 strbuf_release(&buf
);
495 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
496 struct diff_options
*options
,
500 for (i
= 0; i
< q
->nr
; i
++) {
501 struct diff_filepair
*p
= q
->queue
[i
];
502 if (!S_ISGITLINK(p
->two
->mode
))
505 if (S_ISGITLINK(p
->one
->mode
)) {
506 /* NEEDSWORK: We should honor the name configured in
507 * the .gitmodules file of the commit we are examining
508 * here to be able to correctly follow submodules
509 * being moved around. */
510 struct string_list_item
*path
;
511 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
512 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
513 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
515 /* Submodule is new or was moved here */
516 /* NEEDSWORK: When the .git directories of submodules
517 * live inside the superprojects .git directory some
518 * day we should fetch new submodules directly into
519 * that location too when config or options request
520 * that so they can be checked out from there. */
526 static int add_sha1_to_array(const char *ref
, const unsigned char *sha1
,
527 int flags
, void *data
)
529 sha1_array_append(data
, sha1
);
533 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
535 if (!initialized_fetch_ref_tips
) {
536 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
537 initialized_fetch_ref_tips
= 1;
540 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
543 static void add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
545 argv_array_push(data
, sha1_to_hex(sha1
));
548 static void calculate_changed_submodule_paths(void)
551 struct commit
*commit
;
552 struct argv_array argv
= ARGV_ARRAY_INIT
;
554 /* No need to check if there are no submodules configured */
555 if (!config_name_for_path
.nr
)
558 init_revisions(&rev
, NULL
);
559 argv_array_push(&argv
, "--"); /* argv[0] program name */
560 sha1_array_for_each_unique(&ref_tips_after_fetch
,
561 add_sha1_to_argv
, &argv
);
562 argv_array_push(&argv
, "--not");
563 sha1_array_for_each_unique(&ref_tips_before_fetch
,
564 add_sha1_to_argv
, &argv
);
565 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
566 if (prepare_revision_walk(&rev
))
567 die("revision walk setup failed");
570 * Collect all submodules (whether checked out or not) for which new
571 * commits have been recorded upstream in "changed_submodule_paths".
573 while ((commit
= get_revision(&rev
))) {
574 struct commit_list
*parent
= commit
->parents
;
576 struct diff_options diff_opts
;
577 diff_setup(&diff_opts
);
578 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
579 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
580 diff_opts
.format_callback
= submodule_collect_changed_cb
;
581 diff_setup_done(&diff_opts
);
582 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
583 diffcore_std(&diff_opts
);
584 diff_flush(&diff_opts
);
585 parent
= parent
->next
;
589 argv_array_clear(&argv
);
590 sha1_array_clear(&ref_tips_before_fetch
);
591 sha1_array_clear(&ref_tips_after_fetch
);
592 initialized_fetch_ref_tips
= 0;
595 int fetch_populated_submodules(const struct argv_array
*options
,
596 const char *prefix
, int command_line_option
,
600 struct child_process cp
;
601 struct argv_array argv
= ARGV_ARRAY_INIT
;
602 struct string_list_item
*name_for_path
;
603 const char *work_tree
= get_git_work_tree();
607 if (!the_index
.initialized
)
608 if (read_cache() < 0)
609 die("index file corrupt");
611 argv_array_push(&argv
, "fetch");
612 for (i
= 0; i
< options
->argc
; i
++)
613 argv_array_push(&argv
, options
->argv
[i
]);
614 argv_array_push(&argv
, "--recurse-submodules-default");
615 /* default value, "--submodule-prefix" and its value are added later */
617 memset(&cp
, 0, sizeof(cp
));
618 cp
.env
= local_repo_env
;
622 calculate_changed_submodule_paths();
624 for (i
= 0; i
< active_nr
; i
++) {
625 struct strbuf submodule_path
= STRBUF_INIT
;
626 struct strbuf submodule_git_dir
= STRBUF_INIT
;
627 struct strbuf submodule_prefix
= STRBUF_INIT
;
628 struct cache_entry
*ce
= active_cache
[i
];
629 const char *git_dir
, *name
, *default_argv
;
631 if (!S_ISGITLINK(ce
->ce_mode
))
635 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
637 name
= name_for_path
->util
;
639 default_argv
= "yes";
640 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
641 struct string_list_item
*fetch_recurse_submodules_option
;
642 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
643 if (fetch_recurse_submodules_option
) {
644 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_OFF
)
646 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_ON_DEMAND
) {
647 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
649 default_argv
= "on-demand";
652 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
653 gitmodules_is_unmerged
)
655 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
656 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
658 default_argv
= "on-demand";
661 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
662 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
664 default_argv
= "on-demand";
667 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
668 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
669 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
670 git_dir
= read_gitfile(submodule_git_dir
.buf
);
672 git_dir
= submodule_git_dir
.buf
;
673 if (is_directory(git_dir
)) {
675 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
676 cp
.dir
= submodule_path
.buf
;
677 argv_array_push(&argv
, default_argv
);
678 argv_array_push(&argv
, "--submodule-prefix");
679 argv_array_push(&argv
, submodule_prefix
.buf
);
681 if (run_command(&cp
))
683 argv_array_pop(&argv
);
684 argv_array_pop(&argv
);
685 argv_array_pop(&argv
);
687 strbuf_release(&submodule_path
);
688 strbuf_release(&submodule_git_dir
);
689 strbuf_release(&submodule_prefix
);
691 argv_array_clear(&argv
);
693 string_list_clear(&changed_submodule_paths
, 1);
697 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
700 struct child_process cp
;
701 const char *argv
[] = {
707 struct strbuf buf
= STRBUF_INIT
;
708 unsigned dirty_submodule
= 0;
709 const char *line
, *next_line
;
712 strbuf_addf(&buf
, "%s/.git", path
);
713 git_dir
= read_gitfile(buf
.buf
);
716 if (!is_directory(git_dir
)) {
717 strbuf_release(&buf
);
718 /* The submodule is not checked out, so it is not modified */
724 if (ignore_untracked
)
727 memset(&cp
, 0, sizeof(cp
));
729 cp
.env
= local_repo_env
;
734 if (start_command(&cp
))
735 die("Could not run 'git status --porcelain' in submodule %s", path
);
737 len
= strbuf_read(&buf
, cp
.out
, 1024);
740 if ((line
[0] == '?') && (line
[1] == '?')) {
741 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
742 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
745 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
746 if (ignore_untracked
||
747 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
750 next_line
= strchr(line
, '\n');
754 len
-= (next_line
- line
);
759 if (finish_command(&cp
))
760 die("'git status --porcelain' failed in submodule %s", path
);
762 strbuf_release(&buf
);
763 return dirty_submodule
;
766 int submodule_uses_gitfile(const char *path
)
768 struct child_process cp
;
769 const char *argv
[] = {
777 struct strbuf buf
= STRBUF_INIT
;
780 strbuf_addf(&buf
, "%s/.git", path
);
781 git_dir
= read_gitfile(buf
.buf
);
783 strbuf_release(&buf
);
786 strbuf_release(&buf
);
788 /* Now test that all nested submodules use a gitfile too */
789 memset(&cp
, 0, sizeof(cp
));
791 cp
.env
= local_repo_env
;
797 if (run_command(&cp
))
803 int ok_to_remove_submodule(const char *path
)
807 struct child_process cp
;
808 const char *argv
[] = {
812 "--ignore-submodules=none",
815 struct strbuf buf
= STRBUF_INIT
;
816 int ok_to_remove
= 1;
818 if ((lstat(path
, &st
) < 0) || is_empty_dir(path
))
821 if (!submodule_uses_gitfile(path
))
824 memset(&cp
, 0, sizeof(cp
));
826 cp
.env
= local_repo_env
;
831 if (start_command(&cp
))
832 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path
);
834 len
= strbuf_read(&buf
, cp
.out
, 1024);
839 if (finish_command(&cp
))
840 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path
);
842 strbuf_release(&buf
);
846 static int find_first_merges(struct object_array
*result
, const char *path
,
847 struct commit
*a
, struct commit
*b
)
850 struct object_array merges
;
851 struct commit
*commit
;
852 int contains_another
;
854 char merged_revision
[42];
855 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
856 "--all", merged_revision
, NULL
};
857 struct rev_info revs
;
858 struct setup_revision_opt rev_opts
;
860 memset(&merges
, 0, sizeof(merges
));
861 memset(result
, 0, sizeof(struct object_array
));
862 memset(&rev_opts
, 0, sizeof(rev_opts
));
864 /* get all revisions that merge commit a */
865 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
866 sha1_to_hex(a
->object
.sha1
));
867 init_revisions(&revs
, NULL
);
868 rev_opts
.submodule
= path
;
869 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
871 /* save all revisions from the above list that contain b */
872 if (prepare_revision_walk(&revs
))
873 die("revision walk setup failed");
874 while ((commit
= get_revision(&revs
)) != NULL
) {
875 struct object
*o
= &(commit
->object
);
876 if (in_merge_bases(b
, commit
))
877 add_object_array(o
, NULL
, &merges
);
879 reset_revision_walk();
881 /* Now we've got all merges that contain a and b. Prune all
882 * merges that contain another found merge and save them in
885 for (i
= 0; i
< merges
.nr
; i
++) {
886 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
888 contains_another
= 0;
889 for (j
= 0; j
< merges
.nr
; j
++) {
890 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
891 if (i
!= j
&& in_merge_bases(m2
, m1
)) {
892 contains_another
= 1;
897 if (!contains_another
)
898 add_object_array(merges
.objects
[i
].item
,
899 merges
.objects
[i
].name
, result
);
902 free(merges
.objects
);
906 static void print_commit(struct commit
*commit
)
908 struct strbuf sb
= STRBUF_INIT
;
909 struct pretty_print_context ctx
= {0};
910 ctx
.date_mode
= DATE_NORMAL
;
911 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
912 fprintf(stderr
, "%s\n", sb
.buf
);
916 #define MERGE_WARNING(path, msg) \
917 warning("Failed to merge submodule %s (%s)", path, msg);
919 int merge_submodule(unsigned char result
[20], const char *path
,
920 const unsigned char base
[20], const unsigned char a
[20],
921 const unsigned char b
[20], int search
)
923 struct commit
*commit_base
, *commit_a
, *commit_b
;
925 struct object_array merges
;
929 /* store a in result in case we fail */
932 /* we can not handle deletion conflicts */
933 if (is_null_sha1(base
))
940 if (add_submodule_odb(path
)) {
941 MERGE_WARNING(path
, "not checked out");
945 if (!(commit_base
= lookup_commit_reference(base
)) ||
946 !(commit_a
= lookup_commit_reference(a
)) ||
947 !(commit_b
= lookup_commit_reference(b
))) {
948 MERGE_WARNING(path
, "commits not present");
952 /* check whether both changes are forward */
953 if (!in_merge_bases(commit_base
, commit_a
) ||
954 !in_merge_bases(commit_base
, commit_b
)) {
955 MERGE_WARNING(path
, "commits don't follow merge-base");
959 /* Case #1: a is contained in b or vice versa */
960 if (in_merge_bases(commit_a
, commit_b
)) {
964 if (in_merge_bases(commit_b
, commit_a
)) {
970 * Case #2: There are one or more merges that contain a and b in
971 * the submodule. If there is only one, then present it as a
972 * suggestion to the user, but leave it marked unmerged so the
973 * user needs to confirm the resolution.
976 /* Skip the search if makes no sense to the calling context. */
980 /* find commit which merges them */
981 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
982 switch (parent_count
) {
984 MERGE_WARNING(path
, "merge following commits not found");
988 MERGE_WARNING(path
, "not fast-forward");
989 fprintf(stderr
, "Found a possible merge resolution "
990 "for the submodule:\n");
991 print_commit((struct commit
*) merges
.objects
[0].item
);
993 "If this is correct simply add it to the index "
996 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
997 "which will accept this suggestion.\n",
998 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
1002 MERGE_WARNING(path
, "multiple merges found");
1003 for (i
= 0; i
< merges
.nr
; i
++)
1004 print_commit((struct commit
*) merges
.objects
[i
].item
);
1007 free(merges
.objects
);