7 #include "run-command.h"
10 #include "string-list.h"
12 static struct string_list config_name_for_path
;
13 static struct string_list config_fetch_recurse_submodules_for_name
;
14 static struct string_list config_ignore_for_name
;
15 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
16 static struct string_list changed_submodule_paths
;
18 * The following flag is set if the .gitmodules file is unmerged. We then
19 * disable recursion for all submodules where .git/config doesn't have a
20 * matching config entry because we can't guess what might be configured in
21 * .gitmodules unless the user resolves the conflict. When a command line
22 * option is given (which always overrides configuration) this flag will be
25 static int gitmodules_is_unmerged
;
27 static int add_submodule_odb(const char *path
)
29 struct strbuf objects_directory
= STRBUF_INIT
;
30 struct alternate_object_database
*alt_odb
;
34 strbuf_addf(&objects_directory
, "%s/.git", path
);
35 git_dir
= read_gitfile(objects_directory
.buf
);
37 strbuf_reset(&objects_directory
);
38 strbuf_addstr(&objects_directory
, git_dir
);
40 strbuf_addstr(&objects_directory
, "/objects/");
41 if (!is_directory(objects_directory
.buf
)) {
45 /* avoid adding it twice */
46 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
47 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
48 !strncmp(alt_odb
->base
, objects_directory
.buf
,
49 objects_directory
.len
))
52 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
53 alt_odb
->next
= alt_odb_list
;
54 strcpy(alt_odb
->base
, objects_directory
.buf
);
55 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
56 alt_odb
->name
[2] = '/';
57 alt_odb
->name
[40] = '\0';
58 alt_odb
->name
[41] = '\0';
59 alt_odb_list
= alt_odb
;
62 strbuf_release(&objects_directory
);
66 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
69 struct string_list_item
*path_option
, *ignore_option
;
70 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
72 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
74 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
75 else if (gitmodules_is_unmerged
)
76 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
80 int submodule_config(const char *var
, const char *value
, void *cb
)
82 if (!prefixcmp(var
, "submodule."))
83 return parse_submodule_config_option(var
, value
);
84 else if (!strcmp(var
, "fetch.recursesubmodules")) {
85 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
91 void gitmodules_config(void)
93 const char *work_tree
= get_git_work_tree();
95 struct strbuf gitmodules_path
= STRBUF_INIT
;
97 strbuf_addstr(&gitmodules_path
, work_tree
);
98 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
100 die("index file corrupt");
101 pos
= cache_name_pos(".gitmodules", 11);
102 if (pos
< 0) { /* .gitmodules not found or isn't merged */
104 if (active_nr
> pos
) { /* there is a .gitmodules */
105 const struct cache_entry
*ce
= active_cache
[pos
];
106 if (ce_namelen(ce
) == 11 &&
107 !memcmp(ce
->name
, ".gitmodules", 11))
108 gitmodules_is_unmerged
= 1;
112 if (!gitmodules_is_unmerged
)
113 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
114 strbuf_release(&gitmodules_path
);
118 int parse_submodule_config_option(const char *var
, const char *value
)
121 struct string_list_item
*config
;
122 struct strbuf submodname
= STRBUF_INIT
;
124 var
+= 10; /* Skip "submodule." */
127 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
128 strbuf_add(&submodname
, var
, len
- 5);
129 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
133 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
134 config
->util
= strbuf_detach(&submodname
, NULL
);
135 strbuf_release(&submodname
);
136 } else if ((len
> 23) && !strcmp(var
+ len
- 23, ".fetchrecursesubmodules")) {
137 strbuf_add(&submodname
, var
, len
- 23);
138 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, submodname
.buf
);
140 config
= string_list_append(&config_fetch_recurse_submodules_for_name
,
141 strbuf_detach(&submodname
, NULL
));
142 config
->util
= (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var
, value
);
143 strbuf_release(&submodname
);
144 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
145 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
146 strcmp(value
, "all") && strcmp(value
, "none")) {
147 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
151 strbuf_add(&submodname
, var
, len
- 7);
152 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
156 config
= string_list_append(&config_ignore_for_name
,
157 strbuf_detach(&submodname
, NULL
));
158 strbuf_release(&submodname
);
159 config
->util
= xstrdup(value
);
165 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
168 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
169 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
170 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
172 if (!strcmp(arg
, "all"))
173 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
174 else if (!strcmp(arg
, "untracked"))
175 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
176 else if (!strcmp(arg
, "dirty"))
177 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
178 else if (strcmp(arg
, "none"))
179 die("bad --ignore-submodules argument: %s", arg
);
182 static int prepare_submodule_summary(struct rev_info
*rev
, const char *path
,
183 struct commit
*left
, struct commit
*right
,
184 int *fast_forward
, int *fast_backward
)
186 struct commit_list
*merge_bases
, *list
;
188 init_revisions(rev
, NULL
);
189 setup_revisions(0, NULL
, rev
, NULL
);
191 rev
->first_parent_only
= 1;
192 left
->object
.flags
|= SYMMETRIC_LEFT
;
193 add_pending_object(rev
, &left
->object
, path
);
194 add_pending_object(rev
, &right
->object
, path
);
195 merge_bases
= get_merge_bases(left
, right
, 1);
197 if (merge_bases
->item
== left
)
199 else if (merge_bases
->item
== right
)
202 for (list
= merge_bases
; list
; list
= list
->next
) {
203 list
->item
->object
.flags
|= UNINTERESTING
;
204 add_pending_object(rev
, &list
->item
->object
,
205 sha1_to_hex(list
->item
->object
.sha1
));
207 return prepare_revision_walk(rev
);
210 static void print_submodule_summary(struct rev_info
*rev
, FILE *f
,
211 const char *del
, const char *add
, const char *reset
)
213 static const char format
[] = " %m %s";
214 struct strbuf sb
= STRBUF_INIT
;
215 struct commit
*commit
;
217 while ((commit
= get_revision(rev
))) {
218 struct pretty_print_context ctx
= {0};
219 ctx
.date_mode
= rev
->date_mode
;
220 strbuf_setlen(&sb
, 0);
221 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
223 strbuf_addstr(&sb
, del
);
226 strbuf_addstr(&sb
, add
);
227 format_commit_message(commit
, format
, &sb
, &ctx
);
229 strbuf_addstr(&sb
, reset
);
230 strbuf_addch(&sb
, '\n');
231 fprintf(f
, "%s", sb
.buf
);
236 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
238 switch (git_config_maybe_bool(opt
, arg
)) {
240 return RECURSE_SUBMODULES_ON
;
242 return RECURSE_SUBMODULES_OFF
;
244 if (!strcmp(arg
, "on-demand"))
245 return RECURSE_SUBMODULES_ON_DEMAND
;
246 die("bad %s argument: %s", opt
, arg
);
250 void show_submodule_summary(FILE *f
, const char *path
,
251 unsigned char one
[20], unsigned char two
[20],
252 unsigned dirty_submodule
,
253 const char *del
, const char *add
, const char *reset
)
256 struct commit
*left
= left
, *right
= right
;
257 const char *message
= NULL
;
258 struct strbuf sb
= STRBUF_INIT
;
259 int fast_forward
= 0, fast_backward
= 0;
261 if (is_null_sha1(two
))
262 message
= "(submodule deleted)";
263 else if (add_submodule_odb(path
))
264 message
= "(not checked out)";
265 else if (is_null_sha1(one
))
266 message
= "(new submodule)";
267 else if (!(left
= lookup_commit_reference(one
)) ||
268 !(right
= lookup_commit_reference(two
)))
269 message
= "(commits not present)";
272 prepare_submodule_summary(&rev
, path
, left
, right
,
273 &fast_forward
, &fast_backward
))
274 message
= "(revision walker failed)";
276 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
277 fprintf(f
, "Submodule %s contains untracked content\n", path
);
278 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
279 fprintf(f
, "Submodule %s contains modified content\n", path
);
281 if (!hashcmp(one
, two
)) {
286 strbuf_addf(&sb
, "Submodule %s %s..", path
,
287 find_unique_abbrev(one
, DEFAULT_ABBREV
));
288 if (!fast_backward
&& !fast_forward
)
289 strbuf_addch(&sb
, '.');
290 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
292 strbuf_addf(&sb
, " %s\n", message
);
294 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
295 fwrite(sb
.buf
, sb
.len
, 1, f
);
298 print_submodule_summary(&rev
, f
, del
, add
, reset
);
299 clear_commit_marks(left
, ~0);
300 clear_commit_marks(right
, ~0);
306 void set_config_fetch_recurse_submodules(int value
)
308 config_fetch_recurse_submodules
= value
;
311 static int has_remote(const char *refname
, const unsigned char *sha1
, int flags
, void *cb_data
)
316 static int submodule_needs_pushing(const char *path
, const unsigned char sha1
[20])
318 if (add_submodule_odb(path
) || !lookup_commit_reference(sha1
))
321 if (for_each_remote_ref_submodule(path
, has_remote
, NULL
) > 0) {
322 struct child_process cp
;
323 const char *argv
[] = {"rev-list", NULL
, "--not", "--remotes", "-n", "1" , NULL
};
324 struct strbuf buf
= STRBUF_INIT
;
325 int needs_pushing
= 0;
327 argv
[1] = sha1_to_hex(sha1
);
328 memset(&cp
, 0, sizeof(cp
));
330 cp
.env
= local_repo_env
;
335 if (start_command(&cp
))
336 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
337 sha1_to_hex(sha1
), path
);
338 if (strbuf_read(&buf
, cp
.out
, 41))
342 strbuf_release(&buf
);
343 return needs_pushing
;
349 static void collect_submodules_from_diff(struct diff_queue_struct
*q
,
350 struct diff_options
*options
,
354 int *needs_pushing
= data
;
356 for (i
= 0; i
< q
->nr
; i
++) {
357 struct diff_filepair
*p
= q
->queue
[i
];
358 if (!S_ISGITLINK(p
->two
->mode
))
360 if (submodule_needs_pushing(p
->two
->path
, p
->two
->sha1
)) {
368 static void commit_need_pushing(struct commit
*commit
, struct commit_list
*parent
, int *needs_pushing
)
370 const unsigned char (*parents
)[20];
374 n
= commit_list_count(parent
);
375 parents
= xmalloc(n
* sizeof(*parents
));
377 for (i
= 0; i
< n
; i
++) {
378 hashcpy((unsigned char *)(parents
+ i
), parent
->item
->object
.sha1
);
379 parent
= parent
->next
;
382 init_revisions(&rev
, NULL
);
383 rev
.diffopt
.output_format
|= DIFF_FORMAT_CALLBACK
;
384 rev
.diffopt
.format_callback
= collect_submodules_from_diff
;
385 rev
.diffopt
.format_callback_data
= needs_pushing
;
386 diff_tree_combined(commit
->object
.sha1
, parents
, n
, 1, &rev
);
391 int check_submodule_needs_pushing(unsigned char new_sha1
[20], const char *remotes_name
)
394 struct commit
*commit
;
395 const char *argv
[] = {NULL
, NULL
, "--not", "NULL", NULL
};
396 int argc
= ARRAY_SIZE(argv
) - 1;
398 int needs_pushing
= 0;
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
)) && !needs_pushing
)
411 commit_need_pushing(commit
, commit
->parents
, &needs_pushing
);
414 strbuf_release(&remotes_arg
);
416 return needs_pushing
;
419 static int is_submodule_commit_present(const char *path
, unsigned char sha1
[20])
422 if (!add_submodule_odb(path
) && lookup_commit_reference(sha1
)) {
423 /* Even if the submodule is checked out and the commit is
424 * present, make sure it is reachable from a ref. */
425 struct child_process cp
;
426 const char *argv
[] = {"rev-list", "-n", "1", NULL
, "--not", "--all", NULL
};
427 struct strbuf buf
= STRBUF_INIT
;
429 argv
[3] = sha1_to_hex(sha1
);
430 memset(&cp
, 0, sizeof(cp
));
432 cp
.env
= local_repo_env
;
437 if (!run_command(&cp
) && !strbuf_read(&buf
, cp
.out
, 1024))
441 strbuf_release(&buf
);
446 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
447 struct diff_options
*options
,
451 for (i
= 0; i
< q
->nr
; i
++) {
452 struct diff_filepair
*p
= q
->queue
[i
];
453 if (!S_ISGITLINK(p
->two
->mode
))
456 if (S_ISGITLINK(p
->one
->mode
)) {
457 /* NEEDSWORK: We should honor the name configured in
458 * the .gitmodules file of the commit we are examining
459 * here to be able to correctly follow submodules
460 * being moved around. */
461 struct string_list_item
*path
;
462 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
463 if (!path
&& !is_submodule_commit_present(p
->two
->path
, p
->two
->sha1
))
464 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
466 /* Submodule is new or was moved here */
467 /* NEEDSWORK: When the .git directories of submodules
468 * live inside the superprojects .git directory some
469 * day we should fetch new submodules directly into
470 * that location too when config or options request
471 * that so they can be checked out from there. */
477 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
480 struct commit
*commit
;
481 const char *argv
[] = {NULL
, NULL
, "--not", "--all", NULL
};
482 int argc
= ARRAY_SIZE(argv
) - 1;
484 /* No need to check if there are no submodules configured */
485 if (!config_name_for_path
.nr
)
488 init_revisions(&rev
, NULL
);
489 argv
[1] = xstrdup(sha1_to_hex(new_sha1
));
490 setup_revisions(argc
, argv
, &rev
, NULL
);
491 if (prepare_revision_walk(&rev
))
492 die("revision walk setup failed");
495 * Collect all submodules (whether checked out or not) for which new
496 * commits have been recorded upstream in "changed_submodule_paths".
498 while ((commit
= get_revision(&rev
))) {
499 struct commit_list
*parent
= commit
->parents
;
501 struct diff_options diff_opts
;
502 diff_setup(&diff_opts
);
503 DIFF_OPT_SET(&diff_opts
, RECURSIVE
);
504 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
505 diff_opts
.format_callback
= submodule_collect_changed_cb
;
506 if (diff_setup_done(&diff_opts
) < 0)
507 die("diff_setup_done failed");
508 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
509 diffcore_std(&diff_opts
);
510 diff_flush(&diff_opts
);
511 parent
= parent
->next
;
514 free((char *)argv
[1]);
517 int fetch_populated_submodules(int num_options
, const char **options
,
518 const char *prefix
, int command_line_option
,
521 int i
, result
= 0, argc
= 0, default_argc
;
522 struct child_process cp
;
524 struct string_list_item
*name_for_path
;
525 const char *work_tree
= get_git_work_tree();
529 if (!the_index
.initialized
)
530 if (read_cache() < 0)
531 die("index file corrupt");
533 /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
534 argv
= xcalloc(num_options
+ 6, sizeof(const char *));
535 argv
[argc
++] = "fetch";
536 for (i
= 0; i
< num_options
; i
++)
537 argv
[argc
++] = options
[i
];
538 argv
[argc
++] = "--recurse-submodules-default";
539 default_argc
= argc
++;
540 argv
[argc
++] = "--submodule-prefix";
542 memset(&cp
, 0, sizeof(cp
));
544 cp
.env
= local_repo_env
;
548 for (i
= 0; i
< active_nr
; i
++) {
549 struct strbuf submodule_path
= STRBUF_INIT
;
550 struct strbuf submodule_git_dir
= STRBUF_INIT
;
551 struct strbuf submodule_prefix
= STRBUF_INIT
;
552 struct cache_entry
*ce
= active_cache
[i
];
553 const char *git_dir
, *name
, *default_argv
;
555 if (!S_ISGITLINK(ce
->ce_mode
))
559 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
561 name
= name_for_path
->util
;
563 default_argv
= "yes";
564 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
565 struct string_list_item
*fetch_recurse_submodules_option
;
566 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
567 if (fetch_recurse_submodules_option
) {
568 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_OFF
)
570 if ((intptr_t)fetch_recurse_submodules_option
->util
== RECURSE_SUBMODULES_ON_DEMAND
) {
571 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
573 default_argv
= "on-demand";
576 if ((config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
) ||
577 gitmodules_is_unmerged
)
579 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
580 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
582 default_argv
= "on-demand";
585 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
586 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
588 default_argv
= "on-demand";
591 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
592 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
593 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
594 git_dir
= read_gitfile(submodule_git_dir
.buf
);
596 git_dir
= submodule_git_dir
.buf
;
597 if (is_directory(git_dir
)) {
599 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
600 cp
.dir
= submodule_path
.buf
;
601 argv
[default_argc
] = default_argv
;
602 argv
[argc
] = submodule_prefix
.buf
;
603 if (run_command(&cp
))
606 strbuf_release(&submodule_path
);
607 strbuf_release(&submodule_git_dir
);
608 strbuf_release(&submodule_prefix
);
612 string_list_clear(&changed_submodule_paths
, 1);
616 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
619 struct child_process cp
;
620 const char *argv
[] = {
626 struct strbuf buf
= STRBUF_INIT
;
627 unsigned dirty_submodule
= 0;
628 const char *line
, *next_line
;
631 strbuf_addf(&buf
, "%s/.git", path
);
632 git_dir
= read_gitfile(buf
.buf
);
635 if (!is_directory(git_dir
)) {
636 strbuf_release(&buf
);
637 /* The submodule is not checked out, so it is not modified */
643 if (ignore_untracked
)
646 memset(&cp
, 0, sizeof(cp
));
648 cp
.env
= local_repo_env
;
653 if (start_command(&cp
))
654 die("Could not run git status --porcelain");
656 len
= strbuf_read(&buf
, cp
.out
, 1024);
659 if ((line
[0] == '?') && (line
[1] == '?')) {
660 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
661 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
664 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
665 if (ignore_untracked
||
666 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
669 next_line
= strchr(line
, '\n');
673 len
-= (next_line
- line
);
678 if (finish_command(&cp
))
679 die("git status --porcelain failed");
681 strbuf_release(&buf
);
682 return dirty_submodule
;
685 static int find_first_merges(struct object_array
*result
, const char *path
,
686 struct commit
*a
, struct commit
*b
)
689 struct object_array merges
;
690 struct commit
*commit
;
691 int contains_another
;
693 char merged_revision
[42];
694 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
695 "--all", merged_revision
, NULL
};
696 struct rev_info revs
;
697 struct setup_revision_opt rev_opts
;
699 memset(&merges
, 0, sizeof(merges
));
700 memset(result
, 0, sizeof(struct object_array
));
701 memset(&rev_opts
, 0, sizeof(rev_opts
));
703 /* get all revisions that merge commit a */
704 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
705 sha1_to_hex(a
->object
.sha1
));
706 init_revisions(&revs
, NULL
);
707 rev_opts
.submodule
= path
;
708 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
710 /* save all revisions from the above list that contain b */
711 if (prepare_revision_walk(&revs
))
712 die("revision walk setup failed");
713 while ((commit
= get_revision(&revs
)) != NULL
) {
714 struct object
*o
= &(commit
->object
);
715 if (in_merge_bases(b
, &commit
, 1))
716 add_object_array(o
, NULL
, &merges
);
719 /* Now we've got all merges that contain a and b. Prune all
720 * merges that contain another found merge and save them in
723 for (i
= 0; i
< merges
.nr
; i
++) {
724 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
726 contains_another
= 0;
727 for (j
= 0; j
< merges
.nr
; j
++) {
728 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
729 if (i
!= j
&& in_merge_bases(m2
, &m1
, 1)) {
730 contains_another
= 1;
735 if (!contains_another
)
736 add_object_array(merges
.objects
[i
].item
,
737 merges
.objects
[i
].name
, result
);
740 free(merges
.objects
);
744 static void print_commit(struct commit
*commit
)
746 struct strbuf sb
= STRBUF_INIT
;
747 struct pretty_print_context ctx
= {0};
748 ctx
.date_mode
= DATE_NORMAL
;
749 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
750 fprintf(stderr
, "%s\n", sb
.buf
);
754 #define MERGE_WARNING(path, msg) \
755 warning("Failed to merge submodule %s (%s)", path, msg);
757 int merge_submodule(unsigned char result
[20], const char *path
,
758 const unsigned char base
[20], const unsigned char a
[20],
759 const unsigned char b
[20])
761 struct commit
*commit_base
, *commit_a
, *commit_b
;
763 struct object_array merges
;
767 /* store a in result in case we fail */
770 /* we can not handle deletion conflicts */
771 if (is_null_sha1(base
))
778 if (add_submodule_odb(path
)) {
779 MERGE_WARNING(path
, "not checked out");
783 if (!(commit_base
= lookup_commit_reference(base
)) ||
784 !(commit_a
= lookup_commit_reference(a
)) ||
785 !(commit_b
= lookup_commit_reference(b
))) {
786 MERGE_WARNING(path
, "commits not present");
790 /* check whether both changes are forward */
791 if (!in_merge_bases(commit_base
, &commit_a
, 1) ||
792 !in_merge_bases(commit_base
, &commit_b
, 1)) {
793 MERGE_WARNING(path
, "commits don't follow merge-base");
797 /* Case #1: a is contained in b or vice versa */
798 if (in_merge_bases(commit_a
, &commit_b
, 1)) {
802 if (in_merge_bases(commit_b
, &commit_a
, 1)) {
808 * Case #2: There are one or more merges that contain a and b in
809 * the submodule. If there is only one, then present it as a
810 * suggestion to the user, but leave it marked unmerged so the
811 * user needs to confirm the resolution.
814 /* find commit which merges them */
815 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
816 switch (parent_count
) {
818 MERGE_WARNING(path
, "merge following commits not found");
822 MERGE_WARNING(path
, "not fast-forward");
823 fprintf(stderr
, "Found a possible merge resolution "
824 "for the submodule:\n");
825 print_commit((struct commit
*) merges
.objects
[0].item
);
827 "If this is correct simply add it to the index "
830 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
831 "which will accept this suggestion.\n",
832 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
836 MERGE_WARNING(path
, "multiple merges found");
837 for (i
= 0; i
< merges
.nr
; i
++)
838 print_commit((struct commit
*) merges
.objects
[i
].item
);
841 free(merges
.objects
);