7 #include "run-command.h"
10 #include "string-list.h"
12 struct string_list config_name_for_path
;
13 struct string_list config_fetch_recurse_submodules_for_name
;
14 struct string_list config_ignore_for_name
;
15 static int config_fetch_recurse_submodules
= RECURSE_SUBMODULES_ON_DEMAND
;
16 struct string_list changed_submodule_paths
;
18 static int add_submodule_odb(const char *path
)
20 struct strbuf objects_directory
= STRBUF_INIT
;
21 struct alternate_object_database
*alt_odb
;
25 strbuf_addf(&objects_directory
, "%s/.git", path
);
26 git_dir
= read_gitfile_gently(objects_directory
.buf
);
28 strbuf_reset(&objects_directory
);
29 strbuf_addstr(&objects_directory
, git_dir
);
31 strbuf_addstr(&objects_directory
, "/objects/");
32 if (!is_directory(objects_directory
.buf
)) {
36 /* avoid adding it twice */
37 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
38 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
39 !strncmp(alt_odb
->base
, objects_directory
.buf
,
40 objects_directory
.len
))
43 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
44 alt_odb
->next
= alt_odb_list
;
45 strcpy(alt_odb
->base
, objects_directory
.buf
);
46 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
47 alt_odb
->name
[2] = '/';
48 alt_odb
->name
[40] = '\0';
49 alt_odb
->name
[41] = '\0';
50 alt_odb_list
= alt_odb
;
53 strbuf_release(&objects_directory
);
57 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
60 struct string_list_item
*path_option
, *ignore_option
;
61 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
63 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
65 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
69 int submodule_config(const char *var
, const char *value
, void *cb
)
71 if (!prefixcmp(var
, "submodule."))
72 return parse_submodule_config_option(var
, value
);
73 else if (!strcmp(var
, "fetch.recursesubmodules")) {
74 config_fetch_recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
80 void gitmodules_config(void)
82 const char *work_tree
= get_git_work_tree();
84 struct strbuf gitmodules_path
= STRBUF_INIT
;
85 strbuf_addstr(&gitmodules_path
, work_tree
);
86 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
87 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
88 strbuf_release(&gitmodules_path
);
92 int parse_submodule_config_option(const char *var
, const char *value
)
95 struct string_list_item
*config
;
96 struct strbuf submodname
= STRBUF_INIT
;
98 var
+= 10; /* Skip "submodule." */
101 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
102 strbuf_add(&submodname
, var
, len
- 5);
103 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
107 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
108 config
->util
= strbuf_detach(&submodname
, NULL
);
109 strbuf_release(&submodname
);
110 } else if ((len
> 23) && !strcmp(var
+ len
- 23, ".fetchrecursesubmodules")) {
111 strbuf_add(&submodname
, var
, len
- 23);
112 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, submodname
.buf
);
114 config
= string_list_append(&config_fetch_recurse_submodules_for_name
,
115 strbuf_detach(&submodname
, NULL
));
116 config
->util
= git_config_bool(var
, value
) ? (void *)1 : NULL
;
117 strbuf_release(&submodname
);
118 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
119 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
120 strcmp(value
, "all") && strcmp(value
, "none")) {
121 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
125 strbuf_add(&submodname
, var
, len
- 7);
126 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
130 config
= string_list_append(&config_ignore_for_name
,
131 strbuf_detach(&submodname
, NULL
));
132 strbuf_release(&submodname
);
133 config
->util
= xstrdup(value
);
139 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
142 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
143 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
144 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
146 if (!strcmp(arg
, "all"))
147 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
148 else if (!strcmp(arg
, "untracked"))
149 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
150 else if (!strcmp(arg
, "dirty"))
151 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
152 else if (strcmp(arg
, "none"))
153 die("bad --ignore-submodules argument: %s", arg
);
156 int parse_fetch_recurse_submodules_arg(const char *opt
, const char *arg
)
158 switch (git_config_maybe_bool(opt
, arg
)) {
160 return RECURSE_SUBMODULES_ON
;
162 return RECURSE_SUBMODULES_OFF
;
164 if (!strcmp(arg
, "on-demand"))
165 return RECURSE_SUBMODULES_ON_DEMAND
;
166 die("bad %s argument: %s", opt
, arg
);
170 void show_submodule_summary(FILE *f
, const char *path
,
171 unsigned char one
[20], unsigned char two
[20],
172 unsigned dirty_submodule
,
173 const char *del
, const char *add
, const char *reset
)
176 struct commit
*commit
, *left
= left
, *right
= right
;
177 struct commit_list
*merge_bases
, *list
;
178 const char *message
= NULL
;
179 struct strbuf sb
= STRBUF_INIT
;
180 static const char *format
= " %m %s";
181 int fast_forward
= 0, fast_backward
= 0;
183 if (is_null_sha1(two
))
184 message
= "(submodule deleted)";
185 else if (add_submodule_odb(path
))
186 message
= "(not checked out)";
187 else if (is_null_sha1(one
))
188 message
= "(new submodule)";
189 else if (!(left
= lookup_commit_reference(one
)) ||
190 !(right
= lookup_commit_reference(two
)))
191 message
= "(commits not present)";
194 init_revisions(&rev
, NULL
);
195 setup_revisions(0, NULL
, &rev
, NULL
);
197 rev
.first_parent_only
= 1;
198 left
->object
.flags
|= SYMMETRIC_LEFT
;
199 add_pending_object(&rev
, &left
->object
, path
);
200 add_pending_object(&rev
, &right
->object
, path
);
201 merge_bases
= get_merge_bases(left
, right
, 1);
203 if (merge_bases
->item
== left
)
205 else if (merge_bases
->item
== right
)
208 for (list
= merge_bases
; list
; list
= list
->next
) {
209 list
->item
->object
.flags
|= UNINTERESTING
;
210 add_pending_object(&rev
, &list
->item
->object
,
211 sha1_to_hex(list
->item
->object
.sha1
));
213 if (prepare_revision_walk(&rev
))
214 message
= "(revision walker failed)";
217 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
218 fprintf(f
, "Submodule %s contains untracked content\n", path
);
219 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
220 fprintf(f
, "Submodule %s contains modified content\n", path
);
222 if (!hashcmp(one
, two
)) {
227 strbuf_addf(&sb
, "Submodule %s %s..", path
,
228 find_unique_abbrev(one
, DEFAULT_ABBREV
));
229 if (!fast_backward
&& !fast_forward
)
230 strbuf_addch(&sb
, '.');
231 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
233 strbuf_addf(&sb
, " %s\n", message
);
235 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
236 fwrite(sb
.buf
, sb
.len
, 1, f
);
239 while ((commit
= get_revision(&rev
))) {
240 struct pretty_print_context ctx
= {0};
241 ctx
.date_mode
= rev
.date_mode
;
242 strbuf_setlen(&sb
, 0);
243 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
245 strbuf_addstr(&sb
, del
);
248 strbuf_addstr(&sb
, add
);
249 format_commit_message(commit
, format
, &sb
, &ctx
);
251 strbuf_addstr(&sb
, reset
);
252 strbuf_addch(&sb
, '\n');
253 fprintf(f
, "%s", sb
.buf
);
255 clear_commit_marks(left
, ~0);
256 clear_commit_marks(right
, ~0);
261 void set_config_fetch_recurse_submodules(int value
)
263 config_fetch_recurse_submodules
= value
;
266 static void submodule_collect_changed_cb(struct diff_queue_struct
*q
,
267 struct diff_options
*options
,
271 for (i
= 0; i
< q
->nr
; i
++) {
272 struct diff_filepair
*p
= q
->queue
[i
];
273 if (!S_ISGITLINK(p
->two
->mode
))
276 if (S_ISGITLINK(p
->one
->mode
)) {
277 /* NEEDSWORK: We should honor the name configured in
278 * the .gitmodules file of the commit we are examining
279 * here to be able to correctly follow submodules
280 * being moved around. */
281 struct string_list_item
*path
;
282 path
= unsorted_string_list_lookup(&changed_submodule_paths
, p
->two
->path
);
284 string_list_append(&changed_submodule_paths
, xstrdup(p
->two
->path
));
286 /* Submodule is new or was moved here */
287 /* NEEDSWORK: When the .git directories of submodules
288 * live inside the superprojects .git directory some
289 * day we should fetch new submodules directly into
290 * that location too when config or options request
291 * that so they can be checked out from there. */
297 void check_for_new_submodule_commits(unsigned char new_sha1
[20])
300 struct commit
*commit
;
301 const char *argv
[] = {NULL
, NULL
, "--not", "--all", NULL
};
302 int argc
= ARRAY_SIZE(argv
) - 1;
304 init_revisions(&rev
, NULL
);
305 argv
[1] = xstrdup(sha1_to_hex(new_sha1
));
306 setup_revisions(argc
, argv
, &rev
, NULL
);
307 if (prepare_revision_walk(&rev
))
308 die("revision walk setup failed");
311 * Collect all submodules (whether checked out or not) for which new
312 * commits have been recorded upstream in "changed_submodule_paths".
314 while ((commit
= get_revision(&rev
))) {
315 struct commit_list
*parent
= commit
->parents
;
317 struct diff_options diff_opts
;
318 diff_setup(&diff_opts
);
319 diff_opts
.output_format
|= DIFF_FORMAT_CALLBACK
;
320 diff_opts
.format_callback
= submodule_collect_changed_cb
;
321 if (diff_setup_done(&diff_opts
) < 0)
322 die("diff_setup_done failed");
323 diff_tree_sha1(parent
->item
->object
.sha1
, commit
->object
.sha1
, "", &diff_opts
);
324 diffcore_std(&diff_opts
);
325 diff_flush(&diff_opts
);
326 parent
= parent
->next
;
329 free((char *)argv
[1]);
332 int fetch_populated_submodules(int num_options
, const char **options
,
333 const char *prefix
, int command_line_option
,
336 int i
, result
= 0, argc
= 0, default_argc
;
337 struct child_process cp
;
339 struct string_list_item
*name_for_path
;
340 const char *work_tree
= get_git_work_tree();
344 if (!the_index
.initialized
)
345 if (read_cache() < 0)
346 die("index file corrupt");
348 /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */
349 argv
= xcalloc(num_options
+ 6, sizeof(const char *));
350 argv
[argc
++] = "fetch";
351 for (i
= 0; i
< num_options
; i
++)
352 argv
[argc
++] = options
[i
];
353 argv
[argc
++] = "--recurse-submodules-default";
354 default_argc
= argc
++;
355 argv
[argc
++] = "--submodule-prefix";
357 memset(&cp
, 0, sizeof(cp
));
359 cp
.env
= local_repo_env
;
363 for (i
= 0; i
< active_nr
; i
++) {
364 struct strbuf submodule_path
= STRBUF_INIT
;
365 struct strbuf submodule_git_dir
= STRBUF_INIT
;
366 struct strbuf submodule_prefix
= STRBUF_INIT
;
367 struct cache_entry
*ce
= active_cache
[i
];
368 const char *git_dir
, *name
, *default_argv
;
370 if (!S_ISGITLINK(ce
->ce_mode
))
374 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
376 name
= name_for_path
->util
;
378 default_argv
= "yes";
379 if (command_line_option
== RECURSE_SUBMODULES_DEFAULT
) {
380 struct string_list_item
*fetch_recurse_submodules_option
;
381 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
382 if (fetch_recurse_submodules_option
) {
383 if (!fetch_recurse_submodules_option
->util
)
386 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_OFF
)
388 if (config_fetch_recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
) {
389 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
391 default_argv
= "on-demand";
394 } else if (command_line_option
== RECURSE_SUBMODULES_ON_DEMAND
) {
395 if (!unsorted_string_list_lookup(&changed_submodule_paths
, ce
->name
))
397 default_argv
= "on-demand";
400 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
401 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
402 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
403 git_dir
= read_gitfile_gently(submodule_git_dir
.buf
);
405 git_dir
= submodule_git_dir
.buf
;
406 if (is_directory(git_dir
)) {
408 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
409 cp
.dir
= submodule_path
.buf
;
410 argv
[default_argc
] = default_argv
;
411 argv
[argc
] = submodule_prefix
.buf
;
412 if (run_command(&cp
))
415 strbuf_release(&submodule_path
);
416 strbuf_release(&submodule_git_dir
);
417 strbuf_release(&submodule_prefix
);
421 string_list_clear(&changed_submodule_paths
, 1);
425 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
428 struct child_process cp
;
429 const char *argv
[] = {
435 struct strbuf buf
= STRBUF_INIT
;
436 unsigned dirty_submodule
= 0;
437 const char *line
, *next_line
;
440 strbuf_addf(&buf
, "%s/.git", path
);
441 git_dir
= read_gitfile_gently(buf
.buf
);
444 if (!is_directory(git_dir
)) {
445 strbuf_release(&buf
);
446 /* The submodule is not checked out, so it is not modified */
452 if (ignore_untracked
)
455 memset(&cp
, 0, sizeof(cp
));
457 cp
.env
= local_repo_env
;
462 if (start_command(&cp
))
463 die("Could not run git status --porcelain");
465 len
= strbuf_read(&buf
, cp
.out
, 1024);
468 if ((line
[0] == '?') && (line
[1] == '?')) {
469 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
470 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
473 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
474 if (ignore_untracked
||
475 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
478 next_line
= strchr(line
, '\n');
482 len
-= (next_line
- line
);
487 if (finish_command(&cp
))
488 die("git status --porcelain failed");
490 strbuf_release(&buf
);
491 return dirty_submodule
;
494 static int find_first_merges(struct object_array
*result
, const char *path
,
495 struct commit
*a
, struct commit
*b
)
498 struct object_array merges
;
499 struct commit
*commit
;
500 int contains_another
;
502 char merged_revision
[42];
503 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
504 "--all", merged_revision
, NULL
};
505 struct rev_info revs
;
506 struct setup_revision_opt rev_opts
;
508 memset(&merges
, 0, sizeof(merges
));
509 memset(result
, 0, sizeof(struct object_array
));
510 memset(&rev_opts
, 0, sizeof(rev_opts
));
512 /* get all revisions that merge commit a */
513 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
514 sha1_to_hex(a
->object
.sha1
));
515 init_revisions(&revs
, NULL
);
516 rev_opts
.submodule
= path
;
517 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
519 /* save all revisions from the above list that contain b */
520 if (prepare_revision_walk(&revs
))
521 die("revision walk setup failed");
522 while ((commit
= get_revision(&revs
)) != NULL
) {
523 struct object
*o
= &(commit
->object
);
524 if (in_merge_bases(b
, &commit
, 1))
525 add_object_array(o
, NULL
, &merges
);
528 /* Now we've got all merges that contain a and b. Prune all
529 * merges that contain another found merge and save them in
532 for (i
= 0; i
< merges
.nr
; i
++) {
533 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
535 contains_another
= 0;
536 for (j
= 0; j
< merges
.nr
; j
++) {
537 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
538 if (i
!= j
&& in_merge_bases(m2
, &m1
, 1)) {
539 contains_another
= 1;
544 if (!contains_another
)
545 add_object_array(merges
.objects
[i
].item
,
546 merges
.objects
[i
].name
, result
);
549 free(merges
.objects
);
553 static void print_commit(struct commit
*commit
)
555 struct strbuf sb
= STRBUF_INIT
;
556 struct pretty_print_context ctx
= {0};
557 ctx
.date_mode
= DATE_NORMAL
;
558 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
559 fprintf(stderr
, "%s\n", sb
.buf
);
563 #define MERGE_WARNING(path, msg) \
564 warning("Failed to merge submodule %s (%s)", path, msg);
566 int merge_submodule(unsigned char result
[20], const char *path
,
567 const unsigned char base
[20], const unsigned char a
[20],
568 const unsigned char b
[20])
570 struct commit
*commit_base
, *commit_a
, *commit_b
;
572 struct object_array merges
;
576 /* store a in result in case we fail */
579 /* we can not handle deletion conflicts */
580 if (is_null_sha1(base
))
587 if (add_submodule_odb(path
)) {
588 MERGE_WARNING(path
, "not checked out");
592 if (!(commit_base
= lookup_commit_reference(base
)) ||
593 !(commit_a
= lookup_commit_reference(a
)) ||
594 !(commit_b
= lookup_commit_reference(b
))) {
595 MERGE_WARNING(path
, "commits not present");
599 /* check whether both changes are forward */
600 if (!in_merge_bases(commit_base
, &commit_a
, 1) ||
601 !in_merge_bases(commit_base
, &commit_b
, 1)) {
602 MERGE_WARNING(path
, "commits don't follow merge-base");
606 /* Case #1: a is contained in b or vice versa */
607 if (in_merge_bases(commit_a
, &commit_b
, 1)) {
611 if (in_merge_bases(commit_b
, &commit_a
, 1)) {
617 * Case #2: There are one or more merges that contain a and b in
618 * the submodule. If there is only one, then present it as a
619 * suggestion to the user, but leave it marked unmerged so the
620 * user needs to confirm the resolution.
623 /* find commit which merges them */
624 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
625 switch (parent_count
) {
627 MERGE_WARNING(path
, "merge following commits not found");
631 MERGE_WARNING(path
, "not fast-forward");
632 fprintf(stderr
, "Found a possible merge resolution "
633 "for the submodule:\n");
634 print_commit((struct commit
*) merges
.objects
[0].item
);
636 "If this is correct simply add it to the index "
639 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
640 "which will accept this suggestion.\n",
641 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
645 MERGE_WARNING(path
, "multiple merges found");
646 for (i
= 0; i
< merges
.nr
; i
++)
647 print_commit((struct commit
*) merges
.objects
[i
].item
);
650 free(merges
.objects
);