7 #include "run-command.h"
10 #include "string-list.h"
11 #include "sha1-array.h"
13 static struct string_list config_name_for_path
;
14 static struct string_list config_fetch_recurse_submodules_for_name
;
15 static struct string_list config_ignore_for_name
;
16 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
17 static struct string_list changed_submodule_paths
;
18 static int initialized_fetch_ref_tips
;
19 static struct sha1_array ref_tips_before_fetch
;
20 static struct sha1_array ref_tips_after_fetch
;
23 * The following flag is set if the .gitmodules file is unmerged. We then
24 * disable recursion for all submodules where .git/config doesn't have a
25 * matching config entry because we can't guess what might be configured in
26 * .gitmodules unless the user resolves the conflict. When a command line
27 * option is given (which always overrides configuration) this flag will be
30 static int gitmodules_is_unmerged
;
32 static int add_submodule_odb(const char *path
)
34 struct strbuf objects_directory
= STRBUF_INIT
;
35 struct alternate_object_database
*alt_odb
;
39 strbuf_addf(&objects_directory
, "%s/.git", path
);
40 git_dir
= read_gitfile(objects_directory
.buf
);
42 strbuf_reset(&objects_directory
);
43 strbuf_addstr(&objects_directory
, git_dir
);
45 strbuf_addstr(&objects_directory
, "/objects/");
46 if (!is_directory(objects_directory
.buf
)) {
50 /* avoid adding it twice */
51 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
52 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
53 !strncmp(alt_odb
->base
, objects_directory
.buf
,
54 objects_directory
.len
))
57 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
58 alt_odb
->next
= alt_odb_list
;
59 strcpy(alt_odb
->base
, objects_directory
.buf
);
60 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
61 alt_odb
->name
[2] = '/';
62 alt_odb
->name
[40] = '\0';
63 alt_odb
->name
[41] = '\0';
64 alt_odb_list
= alt_odb
;
67 strbuf_release(&objects_directory
);
71 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
74 struct string_list_item
*path_option
, *ignore_option
;
75 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
77 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
79 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
80 else if (gitmodules_is_unmerged
)
81 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
85 int submodule_config(const char *var
, const char *value
, void *cb
)
87 if (!prefixcmp(var
, "submodule."))
88 return parse_submodule_config_option(var
, value
);
89 else if (!strcmp(var
, "fetch.recursesubmodules")) {
90 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
96 void gitmodules_config(void)
98 const char *work_tree
= get_git_work_tree();
100 struct strbuf gitmodules_path
= STRBUF_INIT
;
102 strbuf_addstr(&gitmodules_path
, work_tree
);
103 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
104 if (read_cache() < 0)
105 die("index file corrupt");
106 pos
= cache_name_pos(".gitmodules", 11);
107 if (pos
< 0) { /* .gitmodules not found or isn't merged */
109 if (active_nr
> pos
) { /* there is a .gitmodules */
110 const struct cache_entry
*ce
= active_cache
[pos
];
111 if (ce_namelen(ce
) == 11 &&
112 !memcmp(ce
->name
, ".gitmodules", 11))
113 gitmodules_is_unmerged
= 1;
117 if (!gitmodules_is_unmerged
)
118 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
119 strbuf_release(&gitmodules_path
);
123 int parse_submodule_config_option(const char *var
, const char *value
)
126 struct string_list_item
*config
;
127 struct strbuf submodname
= STRBUF_INIT
;
129 var
+= 10; /* Skip "submodule." */
132 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
133 strbuf_add(&submodname
, var
, len
- 5);
134 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
138 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
139 config
->util
= strbuf_detach(&submodname
, NULL
);
140 strbuf_release(&submodname
);
141 } else if ((len
> 23) && !strcmp(var
+ len
- 23, ".fetchrecursesubmodules")) {
142 strbuf_add(&submodname
, var
, len
- 23);
143 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, submodname
.buf
);
145 config
= string_list_append(&config_fetch_recurse_submodules_for_name
,
146 strbuf_detach(&submodname
, NULL
));
147 config
->util
= (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var
, value
);
148 strbuf_release(&submodname
);
149 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
150 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
151 strcmp(value
, "all") && strcmp(value
, "none")) {
152 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
156 strbuf_add(&submodname
, var
, len
- 7);
157 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
161 config
= string_list_append(&config_ignore_for_name
,
162 strbuf_detach(&submodname
, NULL
));
163 strbuf_release(&submodname
);
164 config
->util
= xstrdup(value
);
170 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
173 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
174 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
175 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
177 if (!strcmp(arg
, "all"))
178 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
179 else if (!strcmp(arg
, "untracked"))
180 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
181 else if (!strcmp(arg
, "dirty"))
182 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
183 else if (strcmp(arg
, "none"))
184 die("bad --ignore-submodules argument: %s", arg
);
187 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
188 struct commit
*left
, struct commit
*right
,
189 int *fast_forward
, int *fast_backward
)
191 struct commit_list
*merge_bases
, *list
;
193 init_revisions(rev
, NULL
);
194 setup_revisions(0, NULL
, rev
, NULL
);
196 rev
->first_parent_only
= 1;
197 left
->object
.flags
|= SYMMETRIC_LEFT
;
198 add_pending_object(rev
, &left
->object
, path
);
199 add_pending_object(rev
, &right
->object
, path
);
200 merge_bases
= get_merge_bases(left
, right
, 1);
202 if (merge_bases
->item
== left
)
204 else if (merge_bases
->item
== right
)
207 for (list
= merge_bases
; list
; list
= list
->next
) {
208 list
->item
->object
.flags
|= UNINTERESTING
;
209 add_pending_object(rev
, &list
->item
->object
,
210 sha1_to_hex(list
->item
->object
.sha1
));
212 return prepare_revision_walk(rev
);
215 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
216 const char *del
, const char *add
, const char *reset
)
218 static const char format
[] = " %m %s";
219 struct strbuf sb
= STRBUF_INIT
;
220 struct commit
*commit
;
222 while ((commit
= get_revision(rev
))) {
223 struct pretty_print_context ctx
= {0};
224 ctx
.date_mode
= rev
->date_mode
;
225 strbuf_setlen(&sb
, 0);
226 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
228 strbuf_addstr(&sb
, del
);
231 strbuf_addstr(&sb
, add
);
232 format_commit_message(commit
, format
, &sb
, &ctx
);
234 strbuf_addstr(&sb
, reset
);
235 strbuf_addch(&sb
, '\n');
236 fprintf(f
, "%s", sb
.buf
);
241 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
243 switch (git_config_maybe_bool(opt
, arg
)) {
245 return RECURSE_SUBMODULES_ON
;
247 return RECURSE_SUBMODULES_OFF
;
249 if (!strcmp(arg
, "on-demand"))
250 return RECURSE_SUBMODULES_ON_DEMAND
;
251 die("bad %s argument: %s", opt
, arg
);
255 void show_submodule_summary(FILE *f
, const char *path
,
256 unsigned char one
[20], unsigned char two
[20],
257 unsigned dirty_submodule
,
258 const char *del
, const char *add
, const char *reset
)
261 struct commit
*left
= left
, *right
= right
;
262 const char *message
= NULL
;
263 struct strbuf sb
= STRBUF_INIT
;
264 int fast_forward
= 0, fast_backward
= 0;
266 if (is_null_sha1(two
))
267 message
= "(submodule deleted)";
268 else if (add_submodule_odb(path
))
269 message
= "(not checked out)";
270 else if (is_null_sha1(one
))
271 message
= "(new submodule)";
272 else if (!(left
= lookup_commit_reference(one
)) ||
273 !(right
= lookup_commit_reference(two
)))
274 message
= "(commits not present)";
277 prepare_submodule_summary(&rev
, path
, left
, right
,
278 &fast_forward
, &fast_backward
))
279 message
= "(revision walker failed)";
281 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
282 fprintf(f
, "Submodule %s contains untracked content\n", path
);
283 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
284 fprintf(f
, "Submodule %s contains modified content\n", path
);
286 if (!hashcmp(one
, two
)) {
291 strbuf_addf(&sb
, "Submodule %s %s..", path
,
292 find_unique_abbrev(one
, DEFAULT_ABBREV
));
293 if (!fast_backward
&& !fast_forward
)
294 strbuf_addch(&sb
, '.');
295 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
297 strbuf_addf(&sb
, " %s\n", message
);
299 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
300 fwrite(sb
.buf
, sb
.len
, 1, f
);
303 print_submodule_summary(&rev
, f
, del
, add
, reset
);
304 clear_commit_marks(left
, ~0);
305 clear_commit_marks(right
, ~0);
311 void set_config_fetch_recurse_submodules(int value
)
313 config_fetch_recurse_submodules
= value
;
316 static int has_remote(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
321 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
323 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
326 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
327 struct child_process cp
;
328 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
329 struct strbuf buf
= STRBUF_INIT
;
330 int needs_pushing
= 0;
332 argv
[1] = sha1_to_hex(sha1
);
333 memset(&cp
, 0, sizeof(cp
));
335 cp
.env
= local_repo_env
;
340 if (start_command(&cp
))
341 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
342 sha1_to_hex(sha1
), path
);
343 if (strbuf_read(&buf
, cp
.out
, 41))
347 strbuf_release(&buf
);
348 return needs_pushing
;
354 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
355 struct diff_options
*options
,
359 int *needs_pushing
= data
;
361 for (i
= 0; i
< q
->nr
; i
++) {
362 struct diff_filepair
*p
= q
->queue
[i
];
363 if (!S_ISGITLINK(p
->two
->mode
))
365 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
)) {
373 static void commit_need_pushing(struct commit
*commit
, struct commit_list
*parent
, int *needs_pushing
)
375 const unsigned char (*parents
)[20];
379 n
= commit_list_count(parent
);
380 parents
= xmalloc(n
* sizeof(*parents
));
382 for (i
= 0; i
< n
; i
++) {
383 hashcpy((unsigned char *)(parents
+ i
), parent
->item
->object
.sha1
);
384 parent
= parent
->next
;
387 init_revisions(&rev
, NULL
);
388 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
389 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
390 rev
.diffopt
.format_callback_data
= needs_pushing
;
391 diff_tree_combined(commit
->object
.sha1
, parents
, n
, 1, &rev
);
396 int check_submodule_needs_pushing(unsigned char new_sha1
[20], const char *remotes_name
)
399 struct commit
*commit
;
400 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
401 int argc
= ARRAY_SIZE(argv
) - 1;
403 int needs_pushing
= 0;
404 struct strbuf remotes_arg
= STRBUF_INIT
;
406 strbuf_addf(&remotes_arg
, "--remotes=%s", remotes_name
);
407 init_revisions(&rev
, NULL
);
408 sha1_copy
= xstrdup(sha1_to_hex(new_sha1
));
410 argv
[3] = remotes_arg
.buf
;
411 setup_revisions(argc
, argv
, &rev
, NULL
);
412 if (prepare_revision_walk(&rev
))
413 die("revision walk setup failed");
415 while ((commit
= get_revision(&rev
)) && !needs_pushing
)
416 commit_need_pushing(commit
, commit
->parents
, &needs_pushing
);
419 strbuf_release(&remotes_arg
);
421 return needs_pushing
;
424 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
427 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
428 /* Even if the submodule is checked out and the commit is
429 * present, make sure it is reachable from a ref. */
430 struct child_process cp
;
431 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
432 struct strbuf buf
= STRBUF_INIT
;
434 argv
[3] = sha1_to_hex(sha1
);
435 memset(&cp
, 0, sizeof(cp
));
437 cp
.env
= local_repo_env
;
442 if (!run_command(&cp
) && !strbuf_read(&buf
, cp
.out
, 1024))
446 strbuf_release(&buf
);
451 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
452 struct diff_options
*options
,
456 for (i
= 0; i
< q
->nr
; i
++) {
457 struct diff_filepair
*p
= q
->queue
[i
];
458 if (!S_ISGITLINK(p
->two
->mode
))
461 if (S_ISGITLINK(p
->one
->mode
)) {
462 /* NEEDSWORK: We should honor the name configured in
463 * the .gitmodules file of the commit we are examining
464 * here to be able to correctly follow submodules
465 * being moved around. */
466 struct string_list_item
*path
;
467 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
468 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
469 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
471 /* Submodule is new or was moved here */
472 /* NEEDSWORK: When the .git directories of submodules
473 * live inside the superprojects .git directory some
474 * day we should fetch new submodules directly into
475 * that location too when config or options request
476 * that so they can be checked out from there. */
482 static int add_sha1_to_array(const char *ref
, const unsigned char *sha1
,
483 int flags
, void *data
)
485 sha1_array_append(data
, sha1
);
489 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
491 if (!initialized_fetch_ref_tips
) {
492 for_each_ref(add_sha1_to_array
, &ref_tips_before_fetch
);
493 initialized_fetch_ref_tips
= 1;
496 sha1_array_append(&ref_tips_after_fetch
, new_sha1
);
505 static void init_argv(struct argv_array
*array
)
512 static void push_argv(struct argv_array
*array
, const char *value
)
514 ALLOC_GROW(array
->argv
, array
->argc
+ 2, array
->alloc
);
515 array
->argv
[array
->argc
++] = xstrdup(value
);
516 array
->argv
[array
->argc
] = NULL
;
519 static void clear_argv(struct argv_array
*array
)
522 for (i
= 0; i
< array
->argc
; i
++)
523 free((char **)array
->argv
[i
]);
528 static void add_sha1_to_argv(const unsigned char sha1
[20], void *data
)
530 push_argv(data
, sha1_to_hex(sha1
));
533 static void calculate_changed_submodule_paths(void)
536 struct commit
*commit
;
537 struct argv_array argv
;
539 /* No need to check if there are no submodules configured */
540 if (!config_name_for_path
.nr
)
543 init_revisions(&rev
, NULL
);
545 push_argv(&argv
, "--"); /* argv[0] program name */
546 sha1_array_for_each_unique(&ref_tips_after_fetch
,
547 add_sha1_to_argv
, &argv
);
548 push_argv(&argv
, "--not");
549 sha1_array_for_each_unique(&ref_tips_before_fetch
,
550 add_sha1_to_argv
, &argv
);
551 setup_revisions(argv
.argc
, argv
.argv
, &rev
, NULL
);
552 if (prepare_revision_walk(&rev
))
553 die("revision walk setup failed");
556 * Collect all submodules (whether checked out or not) for which new
557 * commits have been recorded upstream in "changed_submodule_paths".
559 while ((commit
= get_revision(&rev
))) {
560 struct commit_list
*parent
= commit
->parents
;
562 struct diff_options diff_opts
;
563 diff_setup(&diff_opts
);
564 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
565 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
566 diff_opts
.format_callback
= submodule_collect_changed_cb
;
567 if (diff_setup_done(&diff_opts
) < 0)
568 die("diff_setup_done failed");
569 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
570 diffcore_std(&diff_opts
);
571 diff_flush(&diff_opts
);
572 parent
= parent
->next
;
577 sha1_array_clear(&ref_tips_before_fetch
);
578 sha1_array_clear(&ref_tips_after_fetch
);
579 initialized_fetch_ref_tips
= 0;
582 int fetch_populated_submodules(int num_options
, const char **options
,
583 const char *prefix
, int command_line_option
,
586 int i
, result
= 0, argc
= 0, default_argc
;
587 struct child_process cp
;
589 struct string_list_item
*name_for_path
;
590 const char *work_tree
= get_git_work_tree();
594 if (!the_index
.initialized
)
595 if (read_cache() < 0)
596 die("index file corrupt");
598 /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
599 argv
= xcalloc(num_options
+ 6, sizeof(const char *));
600 argv
[argc
++] = "fetch";
601 for (i
= 0; i
< num_options
; i
++)
602 argv
[argc
++] = options
[i
];
603 argv
[argc
++] = "--recurse-submodules-default";
604 default_argc
= argc
++;
605 argv
[argc
++] = "--submodule-prefix";
607 memset(&cp
, 0, sizeof(cp
));
609 cp
.env
= local_repo_env
;
613 calculate_changed_submodule_paths();
615 for (i
= 0; i
< active_nr
; i
++) {
616 struct strbuf submodule_path
= STRBUF_INIT
;
617 struct strbuf submodule_git_dir
= STRBUF_INIT
;
618 struct strbuf submodule_prefix
= STRBUF_INIT
;
619 struct cache_entry
*ce
= active_cache
[i
];
620 const char *git_dir
, *name
, *default_argv
;
622 if (!S_ISGITLINK(ce
->ce_mode
))
626 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
628 name
= name_for_path
->util
;
630 default_argv
= "yes";
631 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
632 struct string_list_item
*fetch_recurse_submodules_option
;
633 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
634 if (fetch_recurse_submodules_option
) {
635 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_OFF
)
637 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_ON_DEMAND
) {
638 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
640 default_argv
= "on-demand";
643 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
644 gitmodules_is_unmerged
)
646 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
647 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
649 default_argv
= "on-demand";
652 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
653 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
655 default_argv
= "on-demand";
658 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
659 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
660 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
661 git_dir
= read_gitfile(submodule_git_dir
.buf
);
663 git_dir
= submodule_git_dir
.buf
;
664 if (is_directory(git_dir
)) {
666 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
667 cp
.dir
= submodule_path
.buf
;
668 argv
[default_argc
] = default_argv
;
669 argv
[argc
] = submodule_prefix
.buf
;
670 if (run_command(&cp
))
673 strbuf_release(&submodule_path
);
674 strbuf_release(&submodule_git_dir
);
675 strbuf_release(&submodule_prefix
);
679 string_list_clear(&changed_submodule_paths
, 1);
683 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
686 struct child_process cp
;
687 const char *argv
[] = {
693 struct strbuf buf
= STRBUF_INIT
;
694 unsigned dirty_submodule
= 0;
695 const char *line
, *next_line
;
698 strbuf_addf(&buf
, "%s/.git", path
);
699 git_dir
= read_gitfile(buf
.buf
);
702 if (!is_directory(git_dir
)) {
703 strbuf_release(&buf
);
704 /* The submodule is not checked out, so it is not modified */
710 if (ignore_untracked
)
713 memset(&cp
, 0, sizeof(cp
));
715 cp
.env
= local_repo_env
;
720 if (start_command(&cp
))
721 die("Could not run git status --porcelain");
723 len
= strbuf_read(&buf
, cp
.out
, 1024);
726 if ((line
[0] == '?') && (line
[1] == '?')) {
727 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
728 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
731 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
732 if (ignore_untracked
||
733 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
736 next_line
= strchr(line
, '\n');
740 len
-= (next_line
- line
);
745 if (finish_command(&cp
))
746 die("git status --porcelain failed");
748 strbuf_release(&buf
);
749 return dirty_submodule
;
752 static int find_first_merges(struct object_array
*result
, const char *path
,
753 struct commit
*a
, struct commit
*b
)
756 struct object_array merges
;
757 struct commit
*commit
;
758 int contains_another
;
760 char merged_revision
[42];
761 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
762 "--all", merged_revision
, NULL
};
763 struct rev_info revs
;
764 struct setup_revision_opt rev_opts
;
766 memset(&merges
, 0, sizeof(merges
));
767 memset(result
, 0, sizeof(struct object_array
));
768 memset(&rev_opts
, 0, sizeof(rev_opts
));
770 /* get all revisions that merge commit a */
771 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
772 sha1_to_hex(a
->object
.sha1
));
773 init_revisions(&revs
, NULL
);
774 rev_opts
.submodule
= path
;
775 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
777 /* save all revisions from the above list that contain b */
778 if (prepare_revision_walk(&revs
))
779 die("revision walk setup failed");
780 while ((commit
= get_revision(&revs
)) != NULL
) {
781 struct object
*o
= &(commit
->object
);
782 if (in_merge_bases(b
, &commit
, 1))
783 add_object_array(o
, NULL
, &merges
);
786 /* Now we've got all merges that contain a and b. Prune all
787 * merges that contain another found merge and save them in
790 for (i
= 0; i
< merges
.nr
; i
++) {
791 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
793 contains_another
= 0;
794 for (j
= 0; j
< merges
.nr
; j
++) {
795 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
796 if (i
!= j
&& in_merge_bases(m2
, &m1
, 1)) {
797 contains_another
= 1;
802 if (!contains_another
)
803 add_object_array(merges
.objects
[i
].item
,
804 merges
.objects
[i
].name
, result
);
807 free(merges
.objects
);
811 static void print_commit(struct commit
*commit
)
813 struct strbuf sb
= STRBUF_INIT
;
814 struct pretty_print_context ctx
= {0};
815 ctx
.date_mode
= DATE_NORMAL
;
816 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
817 fprintf(stderr
, "%s\n", sb
.buf
);
821 #define MERGE_WARNING(path, msg) \
822 warning("Failed to merge submodule %s (%s)", path, msg);
824 int merge_submodule(unsigned char result
[20], const char *path
,
825 const unsigned char base
[20], const unsigned char a
[20],
826 const unsigned char b
[20])
828 struct commit
*commit_base
, *commit_a
, *commit_b
;
830 struct object_array merges
;
834 /* store a in result in case we fail */
837 /* we can not handle deletion conflicts */
838 if (is_null_sha1(base
))
845 if (add_submodule_odb(path
)) {
846 MERGE_WARNING(path
, "not checked out");
850 if (!(commit_base
= lookup_commit_reference(base
)) ||
851 !(commit_a
= lookup_commit_reference(a
)) ||
852 !(commit_b
= lookup_commit_reference(b
))) {
853 MERGE_WARNING(path
, "commits not present");
857 /* check whether both changes are forward */
858 if (!in_merge_bases(commit_base
, &commit_a
, 1) ||
859 !in_merge_bases(commit_base
, &commit_b
, 1)) {
860 MERGE_WARNING(path
, "commits don't follow merge-base");
864 /* Case #1: a is contained in b or vice versa */
865 if (in_merge_bases(commit_a
, &commit_b
, 1)) {
869 if (in_merge_bases(commit_b
, &commit_a
, 1)) {
875 * Case #2: There are one or more merges that contain a and b in
876 * the submodule. If there is only one, then present it as a
877 * suggestion to the user, but leave it marked unmerged so the
878 * user needs to confirm the resolution.
881 /* find commit which merges them */
882 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
883 switch (parent_count
) {
885 MERGE_WARNING(path
, "merge following commits not found");
889 MERGE_WARNING(path
, "not fast-forward");
890 fprintf(stderr
, "Found a possible merge resolution "
891 "for the submodule:\n");
892 print_commit((struct commit
*) merges
.objects
[0].item
);
894 "If this is correct simply add it to the index "
897 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
898 "which will accept this suggestion.\n",
899 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
903 MERGE_WARNING(path
, "multiple merges found");
904 for (i
= 0; i
< merges
.nr
; i
++)
905 print_commit((struct commit
*) merges
.objects
[i
].item
);
908 free(merges
.objects
);