7 #include "run-command.h"
10 #include "string-list.h"
12 struct string_list config_name_for_path
;
13 struct string_list config_ignore_for_name
;
15 static int add_submodule_odb(const char *path
)
17 struct strbuf objects_directory
= STRBUF_INIT
;
18 struct alternate_object_database
*alt_odb
;
22 strbuf_addf(&objects_directory
, "%s/.git", path
);
23 git_dir
= read_gitfile_gently(objects_directory
.buf
);
25 strbuf_reset(&objects_directory
);
26 strbuf_addstr(&objects_directory
, git_dir
);
28 strbuf_addstr(&objects_directory
, "/objects/");
29 if (!is_directory(objects_directory
.buf
)) {
33 /* avoid adding it twice */
34 for (alt_odb
= alt_odb_list
; alt_odb
; alt_odb
= alt_odb
->next
)
35 if (alt_odb
->name
- alt_odb
->base
== objects_directory
.len
&&
36 !strncmp(alt_odb
->base
, objects_directory
.buf
,
37 objects_directory
.len
))
40 alt_odb
= xmalloc(objects_directory
.len
+ 42 + sizeof(*alt_odb
));
41 alt_odb
->next
= alt_odb_list
;
42 strcpy(alt_odb
->base
, objects_directory
.buf
);
43 alt_odb
->name
= alt_odb
->base
+ objects_directory
.len
;
44 alt_odb
->name
[2] = '/';
45 alt_odb
->name
[40] = '\0';
46 alt_odb
->name
[41] = '\0';
47 alt_odb_list
= alt_odb
;
50 strbuf_release(&objects_directory
);
54 void set_diffopt_flags_from_submodule_config(struct diff_options
*diffopt
,
57 struct string_list_item
*path_option
, *ignore_option
;
58 path_option
= unsorted_string_list_lookup(&config_name_for_path
, path
);
60 ignore_option
= unsorted_string_list_lookup(&config_ignore_for_name
, path_option
->util
);
62 handle_ignore_submodules_arg(diffopt
, ignore_option
->util
);
66 int submodule_config(const char *var
, const char *value
, void *cb
)
68 if (!prefixcmp(var
, "submodule."))
69 return parse_submodule_config_option(var
, value
);
73 void gitmodules_config(void)
75 const char *work_tree
= get_git_work_tree();
77 struct strbuf gitmodules_path
= STRBUF_INIT
;
78 strbuf_addstr(&gitmodules_path
, work_tree
);
79 strbuf_addstr(&gitmodules_path
, "/.gitmodules");
80 git_config_from_file(submodule_config
, gitmodules_path
.buf
, NULL
);
81 strbuf_release(&gitmodules_path
);
85 int parse_submodule_config_option(const char *var
, const char *value
)
88 struct string_list_item
*config
;
89 struct strbuf submodname
= STRBUF_INIT
;
91 var
+= 10; /* Skip "submodule." */
94 if ((len
> 5) && !strcmp(var
+ len
- 5, ".path")) {
95 strbuf_add(&submodname
, var
, len
- 5);
96 config
= unsorted_string_list_lookup(&config_name_for_path
, value
);
100 config
= string_list_append(&config_name_for_path
, xstrdup(value
));
101 config
->util
= strbuf_detach(&submodname
, NULL
);
102 strbuf_release(&submodname
);
103 } else if ((len
> 7) && !strcmp(var
+ len
- 7, ".ignore")) {
104 if (strcmp(value
, "untracked") && strcmp(value
, "dirty") &&
105 strcmp(value
, "all") && strcmp(value
, "none")) {
106 warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value
, var
);
110 strbuf_add(&submodname
, var
, len
- 7);
111 config
= unsorted_string_list_lookup(&config_ignore_for_name
, submodname
.buf
);
115 config
= string_list_append(&config_ignore_for_name
,
116 strbuf_detach(&submodname
, NULL
));
117 strbuf_release(&submodname
);
118 config
->util
= xstrdup(value
);
124 void handle_ignore_submodules_arg(struct diff_options
*diffopt
,
127 DIFF_OPT_CLR(diffopt
, IGNORE_SUBMODULES
);
128 DIFF_OPT_CLR(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
129 DIFF_OPT_CLR(diffopt
, IGNORE_DIRTY_SUBMODULES
);
131 if (!strcmp(arg
, "all"))
132 DIFF_OPT_SET(diffopt
, IGNORE_SUBMODULES
);
133 else if (!strcmp(arg
, "untracked"))
134 DIFF_OPT_SET(diffopt
, IGNORE_UNTRACKED_IN_SUBMODULES
);
135 else if (!strcmp(arg
, "dirty"))
136 DIFF_OPT_SET(diffopt
, IGNORE_DIRTY_SUBMODULES
);
137 else if (strcmp(arg
, "none"))
138 die("bad --ignore-submodules argument: %s", arg
);
141 void show_submodule_summary(FILE *f
, const char *path
,
142 unsigned char one
[20], unsigned char two
[20],
143 unsigned dirty_submodule
,
144 const char *del
, const char *add
, const char *reset
)
147 struct commit
*commit
, *left
= left
, *right
= right
;
148 struct commit_list
*merge_bases
, *list
;
149 const char *message
= NULL
;
150 struct strbuf sb
= STRBUF_INIT
;
151 static const char *format
= " %m %s";
152 int fast_forward
= 0, fast_backward
= 0;
154 if (is_null_sha1(two
))
155 message
= "(submodule deleted)";
156 else if (add_submodule_odb(path
))
157 message
= "(not checked out)";
158 else if (is_null_sha1(one
))
159 message
= "(new submodule)";
160 else if (!(left
= lookup_commit_reference(one
)) ||
161 !(right
= lookup_commit_reference(two
)))
162 message
= "(commits not present)";
165 init_revisions(&rev
, NULL
);
166 setup_revisions(0, NULL
, &rev
, NULL
);
168 rev
.first_parent_only
= 1;
169 left
->object
.flags
|= SYMMETRIC_LEFT
;
170 add_pending_object(&rev
, &left
->object
, path
);
171 add_pending_object(&rev
, &right
->object
, path
);
172 merge_bases
= get_merge_bases(left
, right
, 1);
174 if (merge_bases
->item
== left
)
176 else if (merge_bases
->item
== right
)
179 for (list
= merge_bases
; list
; list
= list
->next
) {
180 list
->item
->object
.flags
|= UNINTERESTING
;
181 add_pending_object(&rev
, &list
->item
->object
,
182 sha1_to_hex(list
->item
->object
.sha1
));
184 if (prepare_revision_walk(&rev
))
185 message
= "(revision walker failed)";
188 if (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
)
189 fprintf(f
, "Submodule %s contains untracked content\n", path
);
190 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
191 fprintf(f
, "Submodule %s contains modified content\n", path
);
193 if (!hashcmp(one
, two
)) {
198 strbuf_addf(&sb
, "Submodule %s %s..", path
,
199 find_unique_abbrev(one
, DEFAULT_ABBREV
));
200 if (!fast_backward
&& !fast_forward
)
201 strbuf_addch(&sb
, '.');
202 strbuf_addf(&sb
, "%s", find_unique_abbrev(two
, DEFAULT_ABBREV
));
204 strbuf_addf(&sb
, " %s\n", message
);
206 strbuf_addf(&sb
, "%s:\n", fast_backward
? " (rewind)" : "");
207 fwrite(sb
.buf
, sb
.len
, 1, f
);
210 while ((commit
= get_revision(&rev
))) {
211 struct pretty_print_context ctx
= {0};
212 ctx
.date_mode
= rev
.date_mode
;
213 strbuf_setlen(&sb
, 0);
214 if (commit
->object
.flags
& SYMMETRIC_LEFT
) {
216 strbuf_addstr(&sb
, del
);
219 strbuf_addstr(&sb
, add
);
220 format_commit_message(commit
, format
, &sb
, &ctx
);
222 strbuf_addstr(&sb
, reset
);
223 strbuf_addch(&sb
, '\n');
224 fprintf(f
, "%s", sb
.buf
);
226 clear_commit_marks(left
, ~0);
227 clear_commit_marks(right
, ~0);
232 int fetch_populated_submodules(int num_options
, const char **options
,
233 const char *prefix
, int quiet
)
235 int i
, result
= 0, argc
= 0;
236 struct child_process cp
;
238 struct string_list_item
*name_for_path
;
239 const char *work_tree
= get_git_work_tree();
243 if (!the_index
.initialized
)
244 if (read_cache() < 0)
245 die("index file corrupt");
247 argv
= xcalloc(num_options
+ 5, sizeof(const char *));
248 argv
[argc
++] = "fetch";
249 for (i
= 0; i
< num_options
; i
++)
250 argv
[argc
++] = options
[i
];
251 argv
[argc
++] = "--submodule-prefix";
253 memset(&cp
, 0, sizeof(cp
));
255 cp
.env
= local_repo_env
;
259 for (i
= 0; i
< active_nr
; i
++) {
260 struct strbuf submodule_path
= STRBUF_INIT
;
261 struct strbuf submodule_git_dir
= STRBUF_INIT
;
262 struct strbuf submodule_prefix
= STRBUF_INIT
;
263 struct cache_entry
*ce
= active_cache
[i
];
264 const char *git_dir
, *name
;
266 if (!S_ISGITLINK(ce
->ce_mode
))
270 name_for_path
= unsorted_string_list_lookup(&config_name_for_path
, ce
->name
);
272 name
= name_for_path
->util
;
274 strbuf_addf(&submodule_path
, "%s/%s", work_tree
, ce
->name
);
275 strbuf_addf(&submodule_git_dir
, "%s/.git", submodule_path
.buf
);
276 strbuf_addf(&submodule_prefix
, "%s%s/", prefix
, ce
->name
);
277 git_dir
= read_gitfile_gently(submodule_git_dir
.buf
);
279 git_dir
= submodule_git_dir
.buf
;
280 if (is_directory(git_dir
)) {
282 printf("Fetching submodule %s%s\n", prefix
, ce
->name
);
283 cp
.dir
= submodule_path
.buf
;
284 argv
[argc
] = submodule_prefix
.buf
;
285 if (run_command(&cp
))
288 strbuf_release(&submodule_path
);
289 strbuf_release(&submodule_git_dir
);
290 strbuf_release(&submodule_prefix
);
296 unsigned is_submodule_modified(const char *path
, int ignore_untracked
)
299 struct child_process cp
;
300 const char *argv
[] = {
306 struct strbuf buf
= STRBUF_INIT
;
307 unsigned dirty_submodule
= 0;
308 const char *line
, *next_line
;
311 strbuf_addf(&buf
, "%s/.git", path
);
312 git_dir
= read_gitfile_gently(buf
.buf
);
315 if (!is_directory(git_dir
)) {
316 strbuf_release(&buf
);
317 /* The submodule is not checked out, so it is not modified */
323 if (ignore_untracked
)
326 memset(&cp
, 0, sizeof(cp
));
328 cp
.env
= local_repo_env
;
333 if (start_command(&cp
))
334 die("Could not run git status --porcelain");
336 len
= strbuf_read(&buf
, cp
.out
, 1024);
339 if ((line
[0] == '?') && (line
[1] == '?')) {
340 dirty_submodule
|= DIRTY_SUBMODULE_UNTRACKED
;
341 if (dirty_submodule
& DIRTY_SUBMODULE_MODIFIED
)
344 dirty_submodule
|= DIRTY_SUBMODULE_MODIFIED
;
345 if (ignore_untracked
||
346 (dirty_submodule
& DIRTY_SUBMODULE_UNTRACKED
))
349 next_line
= strchr(line
, '\n');
353 len
-= (next_line
- line
);
358 if (finish_command(&cp
))
359 die("git status --porcelain failed");
361 strbuf_release(&buf
);
362 return dirty_submodule
;
365 static int find_first_merges(struct object_array
*result
, const char *path
,
366 struct commit
*a
, struct commit
*b
)
369 struct object_array merges
;
370 struct commit
*commit
;
371 int contains_another
;
373 char merged_revision
[42];
374 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
375 "--all", merged_revision
, NULL
};
376 struct rev_info revs
;
377 struct setup_revision_opt rev_opts
;
379 memset(&merges
, 0, sizeof(merges
));
380 memset(result
, 0, sizeof(struct object_array
));
381 memset(&rev_opts
, 0, sizeof(rev_opts
));
383 /* get all revisions that merge commit a */
384 snprintf(merged_revision
, sizeof(merged_revision
), "^%s",
385 sha1_to_hex(a
->object
.sha1
));
386 init_revisions(&revs
, NULL
);
387 rev_opts
.submodule
= path
;
388 setup_revisions(sizeof(rev_args
)/sizeof(char *)-1, rev_args
, &revs
, &rev_opts
);
390 /* save all revisions from the above list that contain b */
391 if (prepare_revision_walk(&revs
))
392 die("revision walk setup failed");
393 while ((commit
= get_revision(&revs
)) != NULL
) {
394 struct object
*o
= &(commit
->object
);
395 if (in_merge_bases(b
, &commit
, 1))
396 add_object_array(o
, NULL
, &merges
);
399 /* Now we've got all merges that contain a and b. Prune all
400 * merges that contain another found merge and save them in
403 for (i
= 0; i
< merges
.nr
; i
++) {
404 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
406 contains_another
= 0;
407 for (j
= 0; j
< merges
.nr
; j
++) {
408 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
409 if (i
!= j
&& in_merge_bases(m2
, &m1
, 1)) {
410 contains_another
= 1;
415 if (!contains_another
)
416 add_object_array(merges
.objects
[i
].item
,
417 merges
.objects
[i
].name
, result
);
420 free(merges
.objects
);
424 static void print_commit(struct commit
*commit
)
426 struct strbuf sb
= STRBUF_INIT
;
427 struct pretty_print_context ctx
= {0};
428 ctx
.date_mode
= DATE_NORMAL
;
429 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
430 fprintf(stderr
, "%s\n", sb
.buf
);
434 #define MERGE_WARNING(path, msg) \
435 warning("Failed to merge submodule %s (%s)", path, msg);
437 int merge_submodule(unsigned char result
[20], const char *path
,
438 const unsigned char base
[20], const unsigned char a
[20],
439 const unsigned char b
[20])
441 struct commit
*commit_base
, *commit_a
, *commit_b
;
443 struct object_array merges
;
447 /* store a in result in case we fail */
450 /* we can not handle deletion conflicts */
451 if (is_null_sha1(base
))
458 if (add_submodule_odb(path
)) {
459 MERGE_WARNING(path
, "not checked out");
463 if (!(commit_base
= lookup_commit_reference(base
)) ||
464 !(commit_a
= lookup_commit_reference(a
)) ||
465 !(commit_b
= lookup_commit_reference(b
))) {
466 MERGE_WARNING(path
, "commits not present");
470 /* check whether both changes are forward */
471 if (!in_merge_bases(commit_base
, &commit_a
, 1) ||
472 !in_merge_bases(commit_base
, &commit_b
, 1)) {
473 MERGE_WARNING(path
, "commits don't follow merge-base");
477 /* Case #1: a is contained in b or vice versa */
478 if (in_merge_bases(commit_a
, &commit_b
, 1)) {
482 if (in_merge_bases(commit_b
, &commit_a
, 1)) {
488 * Case #2: There are one or more merges that contain a and b in
489 * the submodule. If there is only one, then present it as a
490 * suggestion to the user, but leave it marked unmerged so the
491 * user needs to confirm the resolution.
494 /* find commit which merges them */
495 parent_count
= find_first_merges(&merges
, path
, commit_a
, commit_b
);
496 switch (parent_count
) {
498 MERGE_WARNING(path
, "merge following commits not found");
502 MERGE_WARNING(path
, "not fast-forward");
503 fprintf(stderr
, "Found a possible merge resolution "
504 "for the submodule:\n");
505 print_commit((struct commit
*) merges
.objects
[0].item
);
507 "If this is correct simply add it to the index "
510 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
511 "which will accept this suggestion.\n",
512 sha1_to_hex(merges
.objects
[0].item
->sha1
), path
);
516 MERGE_WARNING(path
, "multiple merges found");
517 for (i
= 0; i
< merges
.nr
; i
++)
518 print_commit((struct commit
*) merges
.objects
[i
].item
);
521 free(merges
.objects
);