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
;
17 static int add_submodule_odb(const char *path
)
19 struct strbuf objects_directory
= STRBUF_INIT
;
20 struct alternate_object_database
*alt_odb
;
24 strbuf_addf(&objects_directory
, "%s/.git", path
);
25 git_dir
= read_gitfile_gently(objects_directory
.buf
);
27 strbuf_reset(&objects_directory
);
28 strbuf_addstr(&objects_directory
, git_dir
);
30 strbuf_addstr(&objects_directory
, "/objects/");
31 if (!is_directory(objects_directory
.buf
)) {
35 /* avoid adding it twice */
36 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
37 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
38 !strncmp(alt_odb
->base
, objects_directory
.buf
,
39 objects_directory
.len
))
42 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
43 alt_odb
->next
= alt_odb_list
;
44 strcpy(alt_odb
->base
, objects_directory
.buf
);
45 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
46 alt_odb
->name
[2] = '/';
47 alt_odb
->name
[40] = '\0';
48 alt_odb
->name
[41] = '\0';
49 alt_odb_list
= alt_odb
;
52 strbuf_release(&objects_directory
);
56 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
59 struct string_list_item
*path_option
, *ignore_option
;
60 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
62 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
64 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
68 int submodule_config(const char *var
, const char *value
, void *cb
)
70 if (!prefixcmp(var
, "submodule."))
71 return parse_submodule_config_option(var
, value
);
72 else if (!strcmp(var
, "fetch.recursesubmodules")) {
73 config_fetch_recurse_submodules
= git_config_bool(var
, value
);
79 void gitmodules_config(void)
81 const char *work_tree
= get_git_work_tree();
83 struct strbuf gitmodules_path
= STRBUF_INIT
;
84 strbuf_addstr(&gitmodules_path
, work_tree
);
85 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
86 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
87 strbuf_release(&gitmodules_path
);
91 int parse_submodule_config_option(const char *var
, const char *value
)
94 struct string_list_item
*config
;
95 struct strbuf submodname
= STRBUF_INIT
;
97 var
+= 10; /* Skip "submodule." */
100 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
101 strbuf_add(&submodname
, var
, len
- 5);
102 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
106 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
107 config
->util
= strbuf_detach(&submodname
, NULL
);
108 strbuf_release(&submodname
);
109 } else if ((len
> 23) && !strcmp(var
+ len
- 23, ".fetchrecursesubmodules")) {
110 strbuf_add(&submodname
, var
, len
- 23);
111 config
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, submodname
.buf
);
113 config
= string_list_append(&config_fetch_recurse_submodules_for_name
,
114 strbuf_detach(&submodname
, NULL
));
115 config
->util
= git_config_bool(var
, value
) ? (void *)1 : NULL
;
116 strbuf_release(&submodname
);
117 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
118 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
119 strcmp(value
, "all") && strcmp(value
, "none")) {
120 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
124 strbuf_add(&submodname
, var
, len
- 7);
125 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
129 config
= string_list_append(&config_ignore_for_name
,
130 strbuf_detach(&submodname
, NULL
));
131 strbuf_release(&submodname
);
132 config
->util
= xstrdup(value
);
138 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
141 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
142 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
143 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
145 if (!strcmp(arg
, "all"))
146 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
147 else if (!strcmp(arg
, "untracked"))
148 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
149 else if (!strcmp(arg
, "dirty"))
150 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
151 else if (strcmp(arg
, "none"))
152 die("bad --ignore-submodules argument: %s", arg
);
155 void show_submodule_summary(FILE *f
, const char *path
,
156 unsigned char one
[20], unsigned char two
[20],
157 unsigned dirty_submodule
,
158 const char *del
, const char *add
, const char *reset
)
161 struct commit
*commit
, *left
= left
, *right
= right
;
162 struct commit_list
*merge_bases
, *list
;
163 const char *message
= NULL
;
164 struct strbuf sb
= STRBUF_INIT
;
165 static const char *format
= " %m %s";
166 int fast_forward
= 0, fast_backward
= 0;
168 if (is_null_sha1(two
))
169 message
= "(submodule deleted)";
170 else if (add_submodule_odb(path
))
171 message
= "(not checked out)";
172 else if (is_null_sha1(one
))
173 message
= "(new submodule)";
174 else if (!(left
= lookup_commit_reference(one
)) ||
175 !(right
= lookup_commit_reference(two
)))
176 message
= "(commits not present)";
179 init_revisions(&rev
, NULL
);
180 setup_revisions(0, NULL
, &rev
, NULL
);
182 rev
.first_parent_only
= 1;
183 left
->object
.flags
|= SYMMETRIC_LEFT
;
184 add_pending_object(&rev
, &left
->object
, path
);
185 add_pending_object(&rev
, &right
->object
, path
);
186 merge_bases
= get_merge_bases(left
, right
, 1);
188 if (merge_bases
->item
== left
)
190 else if (merge_bases
->item
== right
)
193 for (list
= merge_bases
; list
; list
= list
->next
) {
194 list
->item
->object
.flags
|= UNINTERESTING
;
195 add_pending_object(&rev
, &list
->item
->object
,
196 sha1_to_hex(list
->item
->object
.sha1
));
198 if (prepare_revision_walk(&rev
))
199 message
= "(revision walker failed)";
202 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
203 fprintf(f
, "Submodule %s contains untracked content\n", path
);
204 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
205 fprintf(f
, "Submodule %s contains modified content\n", path
);
207 if (!hashcmp(one
, two
)) {
212 strbuf_addf(&sb
, "Submodule %s %s..", path
,
213 find_unique_abbrev(one
, DEFAULT_ABBREV
));
214 if (!fast_backward
&& !fast_forward
)
215 strbuf_addch(&sb
, '.');
216 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
218 strbuf_addf(&sb
, " %s\n", message
);
220 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
221 fwrite(sb
.buf
, sb
.len
, 1, f
);
224 while ((commit
= get_revision(&rev
))) {
225 struct pretty_print_context ctx
= {0};
226 ctx
.date_mode
= rev
.date_mode
;
227 strbuf_setlen(&sb
, 0);
228 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
230 strbuf_addstr(&sb
, del
);
233 strbuf_addstr(&sb
, add
);
234 format_commit_message(commit
, format
, &sb
, &ctx
);
236 strbuf_addstr(&sb
, reset
);
237 strbuf_addch(&sb
, '\n');
238 fprintf(f
, "%s", sb
.buf
);
240 clear_commit_marks(left
, ~0);
241 clear_commit_marks(right
, ~0);
246 void set_config_fetch_recurse_submodules(int value
)
248 config_fetch_recurse_submodules
= value
;
251 int fetch_populated_submodules(int num_options
, const char **options
,
252 const char *prefix
, int ignore_config
,
255 int i
, result
= 0, argc
= 0;
256 struct child_process cp
;
258 struct string_list_item
*name_for_path
;
259 const char *work_tree
= get_git_work_tree();
263 if (!the_index
.initialized
)
264 if (read_cache() < 0)
265 die("index file corrupt");
267 /* 4: "fetch" (options) "--submodule-prefix" prefix NULL */
268 argv
= xcalloc(num_options
+ 4, sizeof(const char *));
269 argv
[argc
++] = "fetch";
270 for (i
= 0; i
< num_options
; i
++)
271 argv
[argc
++] = options
[i
];
272 argv
[argc
++] = "--submodule-prefix";
274 memset(&cp
, 0, sizeof(cp
));
276 cp
.env
= local_repo_env
;
280 for (i
= 0; i
< active_nr
; i
++) {
281 struct strbuf submodule_path
= STRBUF_INIT
;
282 struct strbuf submodule_git_dir
= STRBUF_INIT
;
283 struct strbuf submodule_prefix
= STRBUF_INIT
;
284 struct cache_entry
*ce
= active_cache
[i
];
285 const char *git_dir
, *name
;
287 if (!S_ISGITLINK(ce
->ce_mode
))
291 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
293 name
= name_for_path
->util
;
295 if (!ignore_config
) {
296 struct string_list_item
*fetch_recurse_submodules_option
;
297 fetch_recurse_submodules_option
= unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name
, name
);
298 if (fetch_recurse_submodules_option
) {
299 if (!fetch_recurse_submodules_option
->util
)
302 if (!config_fetch_recurse_submodules
)
307 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
308 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
309 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
310 git_dir
= read_gitfile_gently(submodule_git_dir
.buf
);
312 git_dir
= submodule_git_dir
.buf
;
313 if (is_directory(git_dir
)) {
315 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
316 cp
.dir
= submodule_path
.buf
;
317 argv
[argc
] = submodule_prefix
.buf
;
318 if (run_command(&cp
))
321 strbuf_release(&submodule_path
);
322 strbuf_release(&submodule_git_dir
);
323 strbuf_release(&submodule_prefix
);
329 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
332 struct child_process cp
;
333 const char *argv
[] = {
339 struct strbuf buf
= STRBUF_INIT
;
340 unsigned dirty_submodule
= 0;
341 const char *line
, *next_line
;
344 strbuf_addf(&buf
, "%s/.git", path
);
345 git_dir
= read_gitfile_gently(buf
.buf
);
348 if (!is_directory(git_dir
)) {
349 strbuf_release(&buf
);
350 /* The submodule is not checked out, so it is not modified */
356 if (ignore_untracked
)
359 memset(&cp
, 0, sizeof(cp
));
361 cp
.env
= local_repo_env
;
366 if (start_command(&cp
))
367 die("Could not run git status --porcelain");
369 len
= strbuf_read(&buf
, cp
.out
, 1024);
372 if ((line
[0] == '?') && (line
[1] == '?')) {
373 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
374 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
377 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
378 if (ignore_untracked
||
379 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
382 next_line
= strchr(line
, '\n');
386 len
-= (next_line
- line
);
391 if (finish_command(&cp
))
392 die("git status --porcelain failed");
394 strbuf_release(&buf
);
395 return dirty_submodule
;
398 static int find_first_merges(struct object_array
*result
, const char *path
,
399 struct commit
*a
, struct commit
*b
)
402 struct object_array merges
;
403 struct commit
*commit
;
404 int contains_another
;
406 char merged_revision
[42];
407 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
408 "--all", merged_revision
, NULL
};
409 struct rev_info revs
;
410 struct setup_revision_opt rev_opts
;
412 memset(&merges
, 0, sizeof(merges
));
413 memset(result
, 0, sizeof(struct object_array
));
414 memset(&rev_opts
, 0, sizeof(rev_opts
));
416 /* get all revisions that merge commit a */
417 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
418 sha1_to_hex(a
->object
.sha1
));
419 init_revisions(&revs
, NULL
);
420 rev_opts
.submodule
= path
;
421 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
423 /* save all revisions from the above list that contain b */
424 if (prepare_revision_walk(&revs
))
425 die("revision walk setup failed");
426 while ((commit
= get_revision(&revs
)) != NULL
) {
427 struct object
*o
= &(commit
->object
);
428 if (in_merge_bases(b
, &commit
, 1))
429 add_object_array(o
, NULL
, &merges
);
432 /* Now we've got all merges that contain a and b. Prune all
433 * merges that contain another found merge and save them in
436 for (i
= 0; i
< merges
.nr
; i
++) {
437 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
439 contains_another
= 0;
440 for (j
= 0; j
< merges
.nr
; j
++) {
441 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
442 if (i
!= j
&& in_merge_bases(m2
, &m1
, 1)) {
443 contains_another
= 1;
448 if (!contains_another
)
449 add_object_array(merges
.objects
[i
].item
,
450 merges
.objects
[i
].name
, result
);
453 free(merges
.objects
);
457 static void print_commit(struct commit
*commit
)
459 struct strbuf sb
= STRBUF_INIT
;
460 struct pretty_print_context ctx
= {0};
461 ctx
.date_mode
= DATE_NORMAL
;
462 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
463 fprintf(stderr
, "%s\n", sb
.buf
);
467 #define MERGE_WARNING(path, msg) \
468 warning("Failed to merge submodule %s (%s)", path, msg);
470 int merge_submodule(unsigned char result
[20], const char *path
,
471 const unsigned char base
[20], const unsigned char a
[20],
472 const unsigned char b
[20])
474 struct commit
*commit_base
, *commit_a
, *commit_b
;
476 struct object_array merges
;
480 /* store a in result in case we fail */
483 /* we can not handle deletion conflicts */
484 if (is_null_sha1(base
))
491 if (add_submodule_odb(path
)) {
492 MERGE_WARNING(path
, "not checked out");
496 if (!(commit_base
= lookup_commit_reference(base
)) ||
497 !(commit_a
= lookup_commit_reference(a
)) ||
498 !(commit_b
= lookup_commit_reference(b
))) {
499 MERGE_WARNING(path
, "commits not present");
503 /* check whether both changes are forward */
504 if (!in_merge_bases(commit_base
, &commit_a
, 1) ||
505 !in_merge_bases(commit_base
, &commit_b
, 1)) {
506 MERGE_WARNING(path
, "commits don't follow merge-base");
510 /* Case #1: a is contained in b or vice versa */
511 if (in_merge_bases(commit_a
, &commit_b
, 1)) {
515 if (in_merge_bases(commit_b
, &commit_a
, 1)) {
521 * Case #2: There are one or more merges that contain a and b in
522 * the submodule. If there is only one, then present it as a
523 * suggestion to the user, but leave it marked unmerged so the
524 * user needs to confirm the resolution.
527 /* find commit which merges them */
528 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
529 switch (parent_count
) {
531 MERGE_WARNING(path
, "merge following commits not found");
535 MERGE_WARNING(path
, "not fast-forward");
536 fprintf(stderr
, "Found a possible merge resolution "
537 "for the submodule:\n");
538 print_commit((struct commit
*) merges
.objects
[0].item
);
540 "If this is correct simply add it to the index "
543 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
544 "which will accept this suggestion.\n",
545 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
549 MERGE_WARNING(path
, "multiple merges found");
550 for (i
= 0; i
< merges
.nr
; i
++)
551 print_commit((struct commit
*) merges
.objects
[i
].item
);
554 free(merges
.objects
);