]>
Commit | Line | Data |
---|---|---|
752c0c24 JS |
1 | #include "cache.h" |
2 | #include "submodule.h" | |
3 | #include "dir.h" | |
4 | #include "diff.h" | |
5 | #include "commit.h" | |
6 | #include "revision.h" | |
ee6fc514 | 7 | #include "run-command.h" |
c7e1a736 | 8 | #include "diffcore.h" |
68d03e4a | 9 | #include "refs.h" |
aee9c7d6 | 10 | #include "string-list.h" |
6859de45 | 11 | #include "sha1-array.h" |
c1189cae | 12 | #include "argv-array.h" |
aee9c7d6 | 13 | |
c2e86add SB |
14 | static struct string_list config_name_for_path; |
15 | static struct string_list config_fetch_recurse_submodules_for_name; | |
16 | static struct string_list config_ignore_for_name; | |
88a21979 | 17 | static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; |
2071fb01 | 18 | static struct string_list changed_submodule_paths; |
6859de45 JK |
19 | static int initialized_fetch_ref_tips; |
20 | static struct sha1_array ref_tips_before_fetch; | |
21 | static struct sha1_array ref_tips_after_fetch; | |
22 | ||
d4e98b58 JL |
23 | /* |
24 | * The following flag is set if the .gitmodules file is unmerged. We then | |
25 | * disable recursion for all submodules where .git/config doesn't have a | |
26 | * matching config entry because we can't guess what might be configured in | |
27 | * .gitmodules unless the user resolves the conflict. When a command line | |
28 | * option is given (which always overrides configuration) this flag will be | |
29 | * ignored. | |
30 | */ | |
31 | static int gitmodules_is_unmerged; | |
752c0c24 | 32 | |
cb58c932 | 33 | static int add_submodule_odb(const char *path) |
752c0c24 JS |
34 | { |
35 | struct strbuf objects_directory = STRBUF_INIT; | |
36 | struct alternate_object_database *alt_odb; | |
de7a7960 | 37 | int ret = 0; |
eee49b6c | 38 | const char *git_dir; |
752c0c24 | 39 | |
eee49b6c | 40 | strbuf_addf(&objects_directory, "%s/.git", path); |
13d6ec91 | 41 | git_dir = read_gitfile(objects_directory.buf); |
eee49b6c JL |
42 | if (git_dir) { |
43 | strbuf_reset(&objects_directory); | |
44 | strbuf_addstr(&objects_directory, git_dir); | |
45 | } | |
46 | strbuf_addstr(&objects_directory, "/objects/"); | |
de7a7960 JL |
47 | if (!is_directory(objects_directory.buf)) { |
48 | ret = -1; | |
49 | goto done; | |
50 | } | |
752c0c24 JS |
51 | /* avoid adding it twice */ |
52 | for (alt_odb = alt_odb_list; alt_odb; alt_odb = alt_odb->next) | |
53 | if (alt_odb->name - alt_odb->base == objects_directory.len && | |
54 | !strncmp(alt_odb->base, objects_directory.buf, | |
55 | objects_directory.len)) | |
de7a7960 | 56 | goto done; |
752c0c24 JS |
57 | |
58 | alt_odb = xmalloc(objects_directory.len + 42 + sizeof(*alt_odb)); | |
59 | alt_odb->next = alt_odb_list; | |
60 | strcpy(alt_odb->base, objects_directory.buf); | |
61 | alt_odb->name = alt_odb->base + objects_directory.len; | |
62 | alt_odb->name[2] = '/'; | |
63 | alt_odb->name[40] = '\0'; | |
64 | alt_odb->name[41] = '\0'; | |
65 | alt_odb_list = alt_odb; | |
5e73633d HV |
66 | |
67 | /* add possible alternates from the submodule */ | |
68 | read_info_alternates(objects_directory.buf, 0); | |
752c0c24 | 69 | prepare_alt_odb(); |
de7a7960 JL |
70 | done: |
71 | strbuf_release(&objects_directory); | |
72 | return ret; | |
752c0c24 JS |
73 | } |
74 | ||
aee9c7d6 JL |
75 | void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, |
76 | const char *path) | |
77 | { | |
78 | struct string_list_item *path_option, *ignore_option; | |
79 | path_option = unsorted_string_list_lookup(&config_name_for_path, path); | |
80 | if (path_option) { | |
81 | ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util); | |
82 | if (ignore_option) | |
83 | handle_ignore_submodules_arg(diffopt, ignore_option->util); | |
d4e98b58 JL |
84 | else if (gitmodules_is_unmerged) |
85 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); | |
aee9c7d6 JL |
86 | } |
87 | } | |
88 | ||
7dce19d3 | 89 | int submodule_config(const char *var, const char *value, void *cb) |
302ad7a9 JL |
90 | { |
91 | if (!prefixcmp(var, "submodule.")) | |
92 | return parse_submodule_config_option(var, value); | |
be254a0e | 93 | else if (!strcmp(var, "fetch.recursesubmodules")) { |
1fb25502 | 94 | config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); |
be254a0e JL |
95 | return 0; |
96 | } | |
302ad7a9 JL |
97 | return 0; |
98 | } | |
99 | ||
100 | void gitmodules_config(void) | |
101 | { | |
102 | const char *work_tree = get_git_work_tree(); | |
103 | if (work_tree) { | |
104 | struct strbuf gitmodules_path = STRBUF_INIT; | |
d4e98b58 | 105 | int pos; |
302ad7a9 JL |
106 | strbuf_addstr(&gitmodules_path, work_tree); |
107 | strbuf_addstr(&gitmodules_path, "/.gitmodules"); | |
d4e98b58 JL |
108 | if (read_cache() < 0) |
109 | die("index file corrupt"); | |
110 | pos = cache_name_pos(".gitmodules", 11); | |
111 | if (pos < 0) { /* .gitmodules not found or isn't merged */ | |
112 | pos = -1 - pos; | |
113 | if (active_nr > pos) { /* there is a .gitmodules */ | |
114 | const struct cache_entry *ce = active_cache[pos]; | |
115 | if (ce_namelen(ce) == 11 && | |
116 | !memcmp(ce->name, ".gitmodules", 11)) | |
117 | gitmodules_is_unmerged = 1; | |
118 | } | |
119 | } | |
120 | ||
121 | if (!gitmodules_is_unmerged) | |
122 | git_config_from_file(submodule_config, gitmodules_path.buf, NULL); | |
302ad7a9 JL |
123 | strbuf_release(&gitmodules_path); |
124 | } | |
125 | } | |
126 | ||
aee9c7d6 JL |
127 | int parse_submodule_config_option(const char *var, const char *value) |
128 | { | |
129 | int len; | |
130 | struct string_list_item *config; | |
131 | struct strbuf submodname = STRBUF_INIT; | |
132 | ||
133 | var += 10; /* Skip "submodule." */ | |
134 | ||
135 | len = strlen(var); | |
136 | if ((len > 5) && !strcmp(var + len - 5, ".path")) { | |
137 | strbuf_add(&submodname, var, len - 5); | |
138 | config = unsorted_string_list_lookup(&config_name_for_path, value); | |
139 | if (config) | |
140 | free(config->util); | |
141 | else | |
142 | config = string_list_append(&config_name_for_path, xstrdup(value)); | |
143 | config->util = strbuf_detach(&submodname, NULL); | |
144 | strbuf_release(&submodname); | |
c1a3c364 JL |
145 | } else if ((len > 23) && !strcmp(var + len - 23, ".fetchrecursesubmodules")) { |
146 | strbuf_add(&submodname, var, len - 23); | |
147 | config = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, submodname.buf); | |
148 | if (!config) | |
149 | config = string_list_append(&config_fetch_recurse_submodules_for_name, | |
150 | strbuf_detach(&submodname, NULL)); | |
bf42b384 | 151 | config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value); |
c1a3c364 | 152 | strbuf_release(&submodname); |
aee9c7d6 JL |
153 | } else if ((len > 7) && !strcmp(var + len - 7, ".ignore")) { |
154 | if (strcmp(value, "untracked") && strcmp(value, "dirty") && | |
155 | strcmp(value, "all") && strcmp(value, "none")) { | |
156 | warning("Invalid parameter \"%s\" for config option \"submodule.%s.ignore\"", value, var); | |
157 | return 0; | |
158 | } | |
159 | ||
160 | strbuf_add(&submodname, var, len - 7); | |
161 | config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf); | |
162 | if (config) | |
163 | free(config->util); | |
164 | else | |
165 | config = string_list_append(&config_ignore_for_name, | |
166 | strbuf_detach(&submodname, NULL)); | |
167 | strbuf_release(&submodname); | |
168 | config->util = xstrdup(value); | |
169 | return 0; | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
46a958b3 JL |
174 | void handle_ignore_submodules_arg(struct diff_options *diffopt, |
175 | const char *arg) | |
176 | { | |
be4f2b40 JS |
177 | DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES); |
178 | DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
179 | DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES); | |
180 | ||
46a958b3 JL |
181 | if (!strcmp(arg, "all")) |
182 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); | |
183 | else if (!strcmp(arg, "untracked")) | |
184 | DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
185 | else if (!strcmp(arg, "dirty")) | |
186 | DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES); | |
aee9c7d6 | 187 | else if (strcmp(arg, "none")) |
46a958b3 JL |
188 | die("bad --ignore-submodules argument: %s", arg); |
189 | } | |
190 | ||
808a95dc JN |
191 | static int prepare_submodule_summary(struct rev_info *rev, const char *path, |
192 | struct commit *left, struct commit *right, | |
193 | int *fast_forward, int *fast_backward) | |
194 | { | |
195 | struct commit_list *merge_bases, *list; | |
196 | ||
197 | init_revisions(rev, NULL); | |
198 | setup_revisions(0, NULL, rev, NULL); | |
199 | rev->left_right = 1; | |
200 | rev->first_parent_only = 1; | |
201 | left->object.flags |= SYMMETRIC_LEFT; | |
202 | add_pending_object(rev, &left->object, path); | |
203 | add_pending_object(rev, &right->object, path); | |
204 | merge_bases = get_merge_bases(left, right, 1); | |
205 | if (merge_bases) { | |
206 | if (merge_bases->item == left) | |
207 | *fast_forward = 1; | |
208 | else if (merge_bases->item == right) | |
209 | *fast_backward = 1; | |
210 | } | |
211 | for (list = merge_bases; list; list = list->next) { | |
212 | list->item->object.flags |= UNINTERESTING; | |
213 | add_pending_object(rev, &list->item->object, | |
214 | sha1_to_hex(list->item->object.sha1)); | |
215 | } | |
216 | return prepare_revision_walk(rev); | |
217 | } | |
218 | ||
219 | static void print_submodule_summary(struct rev_info *rev, FILE *f, | |
220 | const char *del, const char *add, const char *reset) | |
221 | { | |
222 | static const char format[] = " %m %s"; | |
223 | struct strbuf sb = STRBUF_INIT; | |
224 | struct commit *commit; | |
225 | ||
226 | while ((commit = get_revision(rev))) { | |
227 | struct pretty_print_context ctx = {0}; | |
228 | ctx.date_mode = rev->date_mode; | |
229 | strbuf_setlen(&sb, 0); | |
230 | if (commit->object.flags & SYMMETRIC_LEFT) { | |
231 | if (del) | |
232 | strbuf_addstr(&sb, del); | |
233 | } | |
234 | else if (add) | |
235 | strbuf_addstr(&sb, add); | |
236 | format_commit_message(commit, format, &sb, &ctx); | |
237 | if (reset) | |
238 | strbuf_addstr(&sb, reset); | |
239 | strbuf_addch(&sb, '\n'); | |
240 | fprintf(f, "%s", sb.buf); | |
241 | } | |
242 | strbuf_release(&sb); | |
243 | } | |
244 | ||
88a21979 JL |
245 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
246 | { | |
247 | switch (git_config_maybe_bool(opt, arg)) { | |
248 | case 1: | |
249 | return RECURSE_SUBMODULES_ON; | |
250 | case 0: | |
251 | return RECURSE_SUBMODULES_OFF; | |
252 | default: | |
253 | if (!strcmp(arg, "on-demand")) | |
254 | return RECURSE_SUBMODULES_ON_DEMAND; | |
255 | die("bad %s argument: %s", opt, arg); | |
256 | } | |
257 | } | |
258 | ||
752c0c24 JS |
259 | void show_submodule_summary(FILE *f, const char *path, |
260 | unsigned char one[20], unsigned char two[20], | |
721ceec1 | 261 | unsigned dirty_submodule, |
752c0c24 JS |
262 | const char *del, const char *add, const char *reset) |
263 | { | |
264 | struct rev_info rev; | |
808a95dc | 265 | struct commit *left = left, *right = right; |
752c0c24 JS |
266 | const char *message = NULL; |
267 | struct strbuf sb = STRBUF_INIT; | |
752c0c24 JS |
268 | int fast_forward = 0, fast_backward = 0; |
269 | ||
270 | if (is_null_sha1(two)) | |
271 | message = "(submodule deleted)"; | |
272 | else if (add_submodule_odb(path)) | |
273 | message = "(not checked out)"; | |
274 | else if (is_null_sha1(one)) | |
275 | message = "(new submodule)"; | |
276 | else if (!(left = lookup_commit_reference(one)) || | |
277 | !(right = lookup_commit_reference(two))) | |
278 | message = "(commits not present)"; | |
279 | ||
808a95dc JN |
280 | if (!message && |
281 | prepare_submodule_summary(&rev, path, left, right, | |
282 | &fast_forward, &fast_backward)) | |
283 | message = "(revision walker failed)"; | |
752c0c24 | 284 | |
c7e1a736 JL |
285 | if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
286 | fprintf(f, "Submodule %s contains untracked content\n", path); | |
287 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
288 | fprintf(f, "Submodule %s contains modified content\n", path); | |
289 | ||
290 | if (!hashcmp(one, two)) { | |
291 | strbuf_release(&sb); | |
292 | return; | |
293 | } | |
294 | ||
752c0c24 JS |
295 | strbuf_addf(&sb, "Submodule %s %s..", path, |
296 | find_unique_abbrev(one, DEFAULT_ABBREV)); | |
297 | if (!fast_backward && !fast_forward) | |
298 | strbuf_addch(&sb, '.'); | |
299 | strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV)); | |
300 | if (message) | |
301 | strbuf_addf(&sb, " %s\n", message); | |
302 | else | |
303 | strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); | |
304 | fwrite(sb.buf, sb.len, 1, f); | |
305 | ||
306 | if (!message) { | |
808a95dc | 307 | print_submodule_summary(&rev, f, del, add, reset); |
752c0c24 JS |
308 | clear_commit_marks(left, ~0); |
309 | clear_commit_marks(right, ~0); | |
310 | } | |
808a95dc | 311 | |
752c0c24 JS |
312 | strbuf_release(&sb); |
313 | } | |
ee6fc514 | 314 | |
be254a0e JL |
315 | void set_config_fetch_recurse_submodules(int value) |
316 | { | |
317 | config_fetch_recurse_submodules = value; | |
318 | } | |
319 | ||
d2b17b32 FG |
320 | static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data) |
321 | { | |
322 | return 1; | |
323 | } | |
324 | ||
325 | static int submodule_needs_pushing(const char *path, const unsigned char sha1[20]) | |
326 | { | |
327 | if (add_submodule_odb(path) || !lookup_commit_reference(sha1)) | |
328 | return 0; | |
329 | ||
330 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { | |
331 | struct child_process cp; | |
332 | const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL}; | |
333 | struct strbuf buf = STRBUF_INIT; | |
334 | int needs_pushing = 0; | |
335 | ||
336 | argv[1] = sha1_to_hex(sha1); | |
337 | memset(&cp, 0, sizeof(cp)); | |
338 | cp.argv = argv; | |
339 | cp.env = local_repo_env; | |
340 | cp.git_cmd = 1; | |
341 | cp.no_stdin = 1; | |
342 | cp.out = -1; | |
343 | cp.dir = path; | |
344 | if (start_command(&cp)) | |
345 | die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s", | |
346 | sha1_to_hex(sha1), path); | |
347 | if (strbuf_read(&buf, cp.out, 41)) | |
348 | needs_pushing = 1; | |
349 | finish_command(&cp); | |
350 | close(cp.out); | |
351 | strbuf_release(&buf); | |
352 | return needs_pushing; | |
353 | } | |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
358 | static void collect_submodules_from_diff(struct diff_queue_struct *q, | |
359 | struct diff_options *options, | |
360 | void *data) | |
361 | { | |
362 | int i; | |
a762e51e | 363 | struct string_list *needs_pushing = data; |
d2b17b32 FG |
364 | |
365 | for (i = 0; i < q->nr; i++) { | |
366 | struct diff_filepair *p = q->queue[i]; | |
367 | if (!S_ISGITLINK(p->two->mode)) | |
368 | continue; | |
a762e51e HV |
369 | if (submodule_needs_pushing(p->two->path, p->two->sha1)) |
370 | string_list_insert(needs_pushing, p->two->path); | |
d2b17b32 FG |
371 | } |
372 | } | |
373 | ||
a762e51e HV |
374 | static void find_unpushed_submodule_commits(struct commit *commit, |
375 | struct string_list *needs_pushing) | |
d2b17b32 | 376 | { |
d2b17b32 FG |
377 | struct rev_info rev; |
378 | ||
d2b17b32 FG |
379 | init_revisions(&rev, NULL); |
380 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
381 | rev.diffopt.format_callback = collect_submodules_from_diff; | |
382 | rev.diffopt.format_callback_data = needs_pushing; | |
78e98eaf | 383 | diff_tree_combined_merge(commit, 1, &rev); |
d2b17b32 FG |
384 | } |
385 | ||
a762e51e HV |
386 | int find_unpushed_submodules(unsigned char new_sha1[20], |
387 | const char *remotes_name, struct string_list *needs_pushing) | |
d2b17b32 FG |
388 | { |
389 | struct rev_info rev; | |
390 | struct commit *commit; | |
391 | const char *argv[] = {NULL, NULL, "--not", "NULL", NULL}; | |
392 | int argc = ARRAY_SIZE(argv) - 1; | |
393 | char *sha1_copy; | |
a762e51e | 394 | |
d2b17b32 FG |
395 | struct strbuf remotes_arg = STRBUF_INIT; |
396 | ||
397 | strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name); | |
398 | init_revisions(&rev, NULL); | |
399 | sha1_copy = xstrdup(sha1_to_hex(new_sha1)); | |
400 | argv[1] = sha1_copy; | |
401 | argv[3] = remotes_arg.buf; | |
402 | setup_revisions(argc, argv, &rev, NULL); | |
403 | if (prepare_revision_walk(&rev)) | |
404 | die("revision walk setup failed"); | |
405 | ||
a762e51e HV |
406 | while ((commit = get_revision(&rev)) != NULL) |
407 | find_unpushed_submodule_commits(commit, needs_pushing); | |
d2b17b32 | 408 | |
bcc0a3ea | 409 | reset_revision_walk(); |
d2b17b32 FG |
410 | free(sha1_copy); |
411 | strbuf_release(&remotes_arg); | |
412 | ||
a762e51e | 413 | return needs_pushing->nr; |
d2b17b32 FG |
414 | } |
415 | ||
eb21c732 HV |
416 | static int push_submodule(const char *path) |
417 | { | |
418 | if (add_submodule_odb(path)) | |
419 | return 1; | |
420 | ||
421 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { | |
422 | struct child_process cp; | |
423 | const char *argv[] = {"push", NULL}; | |
424 | ||
425 | memset(&cp, 0, sizeof(cp)); | |
426 | cp.argv = argv; | |
427 | cp.env = local_repo_env; | |
428 | cp.git_cmd = 1; | |
429 | cp.no_stdin = 1; | |
430 | cp.dir = path; | |
431 | if (run_command(&cp)) | |
432 | return 0; | |
433 | close(cp.out); | |
434 | } | |
435 | ||
436 | return 1; | |
437 | } | |
438 | ||
439 | int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name) | |
440 | { | |
441 | int i, ret = 1; | |
442 | struct string_list needs_pushing; | |
443 | ||
444 | memset(&needs_pushing, 0, sizeof(struct string_list)); | |
445 | needs_pushing.strdup_strings = 1; | |
446 | ||
447 | if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing)) | |
448 | return 1; | |
449 | ||
450 | for (i = 0; i < needs_pushing.nr; i++) { | |
451 | const char *path = needs_pushing.items[i].string; | |
452 | fprintf(stderr, "Pushing submodule '%s'\n", path); | |
453 | if (!push_submodule(path)) { | |
454 | fprintf(stderr, "Unable to push submodule '%s'\n", path); | |
455 | ret = 0; | |
456 | } | |
457 | } | |
458 | ||
459 | string_list_clear(&needs_pushing, 0); | |
460 | ||
461 | return ret; | |
462 | } | |
463 | ||
c16c3e40 JL |
464 | static int is_submodule_commit_present(const char *path, unsigned char sha1[20]) |
465 | { | |
466 | int is_present = 0; | |
467 | if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) { | |
468 | /* Even if the submodule is checked out and the commit is | |
469 | * present, make sure it is reachable from a ref. */ | |
470 | struct child_process cp; | |
471 | const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL}; | |
472 | struct strbuf buf = STRBUF_INIT; | |
473 | ||
474 | argv[3] = sha1_to_hex(sha1); | |
475 | memset(&cp, 0, sizeof(cp)); | |
476 | cp.argv = argv; | |
477 | cp.env = local_repo_env; | |
478 | cp.git_cmd = 1; | |
479 | cp.no_stdin = 1; | |
480 | cp.out = -1; | |
481 | cp.dir = path; | |
482 | if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024)) | |
483 | is_present = 1; | |
484 | ||
485 | close(cp.out); | |
486 | strbuf_release(&buf); | |
487 | } | |
488 | return is_present; | |
489 | } | |
490 | ||
88a21979 JL |
491 | static void submodule_collect_changed_cb(struct diff_queue_struct *q, |
492 | struct diff_options *options, | |
493 | void *data) | |
494 | { | |
495 | int i; | |
496 | for (i = 0; i < q->nr; i++) { | |
497 | struct diff_filepair *p = q->queue[i]; | |
498 | if (!S_ISGITLINK(p->two->mode)) | |
499 | continue; | |
500 | ||
501 | if (S_ISGITLINK(p->one->mode)) { | |
502 | /* NEEDSWORK: We should honor the name configured in | |
503 | * the .gitmodules file of the commit we are examining | |
504 | * here to be able to correctly follow submodules | |
505 | * being moved around. */ | |
506 | struct string_list_item *path; | |
507 | path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path); | |
c16c3e40 | 508 | if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1)) |
88a21979 JL |
509 | string_list_append(&changed_submodule_paths, xstrdup(p->two->path)); |
510 | } else { | |
511 | /* Submodule is new or was moved here */ | |
512 | /* NEEDSWORK: When the .git directories of submodules | |
513 | * live inside the superprojects .git directory some | |
514 | * day we should fetch new submodules directly into | |
515 | * that location too when config or options request | |
516 | * that so they can be checked out from there. */ | |
517 | continue; | |
518 | } | |
519 | } | |
520 | } | |
521 | ||
6859de45 JK |
522 | static int add_sha1_to_array(const char *ref, const unsigned char *sha1, |
523 | int flags, void *data) | |
524 | { | |
525 | sha1_array_append(data, sha1); | |
526 | return 0; | |
527 | } | |
528 | ||
88a21979 | 529 | void check_for_new_submodule_commits(unsigned char new_sha1[20]) |
6859de45 JK |
530 | { |
531 | if (!initialized_fetch_ref_tips) { | |
532 | for_each_ref(add_sha1_to_array, &ref_tips_before_fetch); | |
533 | initialized_fetch_ref_tips = 1; | |
534 | } | |
535 | ||
536 | sha1_array_append(&ref_tips_after_fetch, new_sha1); | |
537 | } | |
538 | ||
6859de45 JK |
539 | static void add_sha1_to_argv(const unsigned char sha1[20], void *data) |
540 | { | |
c1189cae | 541 | argv_array_push(data, sha1_to_hex(sha1)); |
6859de45 JK |
542 | } |
543 | ||
544 | static void calculate_changed_submodule_paths(void) | |
88a21979 JL |
545 | { |
546 | struct rev_info rev; | |
547 | struct commit *commit; | |
c1189cae | 548 | struct argv_array argv = ARGV_ARRAY_INIT; |
88a21979 | 549 | |
18322bad JL |
550 | /* No need to check if there are no submodules configured */ |
551 | if (!config_name_for_path.nr) | |
552 | return; | |
553 | ||
88a21979 | 554 | init_revisions(&rev, NULL); |
c1189cae | 555 | argv_array_push(&argv, "--"); /* argv[0] program name */ |
6859de45 JK |
556 | sha1_array_for_each_unique(&ref_tips_after_fetch, |
557 | add_sha1_to_argv, &argv); | |
c1189cae | 558 | argv_array_push(&argv, "--not"); |
6859de45 JK |
559 | sha1_array_for_each_unique(&ref_tips_before_fetch, |
560 | add_sha1_to_argv, &argv); | |
561 | setup_revisions(argv.argc, argv.argv, &rev, NULL); | |
88a21979 JL |
562 | if (prepare_revision_walk(&rev)) |
563 | die("revision walk setup failed"); | |
564 | ||
565 | /* | |
566 | * Collect all submodules (whether checked out or not) for which new | |
567 | * commits have been recorded upstream in "changed_submodule_paths". | |
568 | */ | |
569 | while ((commit = get_revision(&rev))) { | |
570 | struct commit_list *parent = commit->parents; | |
571 | while (parent) { | |
572 | struct diff_options diff_opts; | |
573 | diff_setup(&diff_opts); | |
ea2d325b | 574 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
88a21979 JL |
575 | diff_opts.output_format |= DIFF_FORMAT_CALLBACK; |
576 | diff_opts.format_callback = submodule_collect_changed_cb; | |
28452655 | 577 | diff_setup_done(&diff_opts); |
88a21979 JL |
578 | diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts); |
579 | diffcore_std(&diff_opts); | |
580 | diff_flush(&diff_opts); | |
581 | parent = parent->next; | |
582 | } | |
583 | } | |
6859de45 | 584 | |
c1189cae | 585 | argv_array_clear(&argv); |
6859de45 JK |
586 | sha1_array_clear(&ref_tips_before_fetch); |
587 | sha1_array_clear(&ref_tips_after_fetch); | |
588 | initialized_fetch_ref_tips = 0; | |
88a21979 JL |
589 | } |
590 | ||
50d89ad6 | 591 | int fetch_populated_submodules(const struct argv_array *options, |
8f0700dd | 592 | const char *prefix, int command_line_option, |
be254a0e | 593 | int quiet) |
7dce19d3 | 594 | { |
50d89ad6 | 595 | int i, result = 0; |
7dce19d3 | 596 | struct child_process cp; |
50d89ad6 | 597 | struct argv_array argv = ARGV_ARRAY_INIT; |
7dce19d3 JL |
598 | struct string_list_item *name_for_path; |
599 | const char *work_tree = get_git_work_tree(); | |
600 | if (!work_tree) | |
88a21979 | 601 | goto out; |
7dce19d3 JL |
602 | |
603 | if (!the_index.initialized) | |
604 | if (read_cache() < 0) | |
605 | die("index file corrupt"); | |
606 | ||
50d89ad6 JL |
607 | argv_array_push(&argv, "fetch"); |
608 | for (i = 0; i < options->argc; i++) | |
609 | argv_array_push(&argv, options->argv[i]); | |
610 | argv_array_push(&argv, "--recurse-submodules-default"); | |
611 | /* default value, "--submodule-prefix" and its value are added later */ | |
7dce19d3 JL |
612 | |
613 | memset(&cp, 0, sizeof(cp)); | |
7dce19d3 JL |
614 | cp.env = local_repo_env; |
615 | cp.git_cmd = 1; | |
616 | cp.no_stdin = 1; | |
617 | ||
6859de45 JK |
618 | calculate_changed_submodule_paths(); |
619 | ||
7dce19d3 JL |
620 | for (i = 0; i < active_nr; i++) { |
621 | struct strbuf submodule_path = STRBUF_INIT; | |
622 | struct strbuf submodule_git_dir = STRBUF_INIT; | |
623 | struct strbuf submodule_prefix = STRBUF_INIT; | |
624 | struct cache_entry *ce = active_cache[i]; | |
88a21979 | 625 | const char *git_dir, *name, *default_argv; |
7dce19d3 JL |
626 | |
627 | if (!S_ISGITLINK(ce->ce_mode)) | |
628 | continue; | |
629 | ||
630 | name = ce->name; | |
631 | name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name); | |
632 | if (name_for_path) | |
633 | name = name_for_path->util; | |
634 | ||
88a21979 | 635 | default_argv = "yes"; |
8f0700dd | 636 | if (command_line_option == RECURSE_SUBMODULES_DEFAULT) { |
c1a3c364 JL |
637 | struct string_list_item *fetch_recurse_submodules_option; |
638 | fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name); | |
639 | if (fetch_recurse_submodules_option) { | |
bf42b384 | 640 | if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF) |
c1a3c364 | 641 | continue; |
bf42b384 JL |
642 | if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) { |
643 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
644 | continue; | |
645 | default_argv = "on-demand"; | |
646 | } | |
c1a3c364 | 647 | } else { |
d4e98b58 JL |
648 | if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || |
649 | gitmodules_is_unmerged) | |
c1a3c364 | 650 | continue; |
88a21979 JL |
651 | if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { |
652 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
653 | continue; | |
654 | default_argv = "on-demand"; | |
655 | } | |
c1a3c364 | 656 | } |
8f0700dd JL |
657 | } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) { |
658 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
659 | continue; | |
660 | default_argv = "on-demand"; | |
be254a0e JL |
661 | } |
662 | ||
7dce19d3 JL |
663 | strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name); |
664 | strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); | |
665 | strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name); | |
13d6ec91 | 666 | git_dir = read_gitfile(submodule_git_dir.buf); |
7dce19d3 JL |
667 | if (!git_dir) |
668 | git_dir = submodule_git_dir.buf; | |
669 | if (is_directory(git_dir)) { | |
670 | if (!quiet) | |
671 | printf("Fetching submodule %s%s\n", prefix, ce->name); | |
672 | cp.dir = submodule_path.buf; | |
50d89ad6 JL |
673 | argv_array_push(&argv, default_argv); |
674 | argv_array_push(&argv, "--submodule-prefix"); | |
675 | argv_array_push(&argv, submodule_prefix.buf); | |
676 | cp.argv = argv.argv; | |
7dce19d3 JL |
677 | if (run_command(&cp)) |
678 | result = 1; | |
50d89ad6 JL |
679 | argv_array_pop(&argv); |
680 | argv_array_pop(&argv); | |
681 | argv_array_pop(&argv); | |
7dce19d3 JL |
682 | } |
683 | strbuf_release(&submodule_path); | |
684 | strbuf_release(&submodule_git_dir); | |
685 | strbuf_release(&submodule_prefix); | |
686 | } | |
50d89ad6 | 687 | argv_array_clear(&argv); |
88a21979 JL |
688 | out: |
689 | string_list_clear(&changed_submodule_paths, 1); | |
7dce19d3 JL |
690 | return result; |
691 | } | |
692 | ||
3bfc4504 | 693 | unsigned is_submodule_modified(const char *path, int ignore_untracked) |
ee6fc514 | 694 | { |
c7e1a736 | 695 | ssize_t len; |
ee6fc514 JL |
696 | struct child_process cp; |
697 | const char *argv[] = { | |
698 | "status", | |
699 | "--porcelain", | |
700 | NULL, | |
3bfc4504 | 701 | NULL, |
ee6fc514 | 702 | }; |
ee6fc514 | 703 | struct strbuf buf = STRBUF_INIT; |
c7e1a736 JL |
704 | unsigned dirty_submodule = 0; |
705 | const char *line, *next_line; | |
eee49b6c | 706 | const char *git_dir; |
ee6fc514 | 707 | |
eee49b6c | 708 | strbuf_addf(&buf, "%s/.git", path); |
13d6ec91 | 709 | git_dir = read_gitfile(buf.buf); |
eee49b6c JL |
710 | if (!git_dir) |
711 | git_dir = buf.buf; | |
712 | if (!is_directory(git_dir)) { | |
ee6fc514 JL |
713 | strbuf_release(&buf); |
714 | /* The submodule is not checked out, so it is not modified */ | |
715 | return 0; | |
716 | ||
717 | } | |
718 | strbuf_reset(&buf); | |
719 | ||
3bfc4504 JL |
720 | if (ignore_untracked) |
721 | argv[2] = "-uno"; | |
722 | ||
ee6fc514 JL |
723 | memset(&cp, 0, sizeof(cp)); |
724 | cp.argv = argv; | |
eee49b6c | 725 | cp.env = local_repo_env; |
ee6fc514 JL |
726 | cp.git_cmd = 1; |
727 | cp.no_stdin = 1; | |
728 | cp.out = -1; | |
eee49b6c | 729 | cp.dir = path; |
ee6fc514 | 730 | if (start_command(&cp)) |
6a5cedac | 731 | die("Could not run 'git status --porcelain' in submodule %s", path); |
ee6fc514 JL |
732 | |
733 | len = strbuf_read(&buf, cp.out, 1024); | |
c7e1a736 JL |
734 | line = buf.buf; |
735 | while (len > 2) { | |
736 | if ((line[0] == '?') && (line[1] == '?')) { | |
737 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; | |
738 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
739 | break; | |
740 | } else { | |
741 | dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; | |
3bfc4504 JL |
742 | if (ignore_untracked || |
743 | (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)) | |
c7e1a736 JL |
744 | break; |
745 | } | |
746 | next_line = strchr(line, '\n'); | |
747 | if (!next_line) | |
748 | break; | |
749 | next_line++; | |
750 | len -= (next_line - line); | |
751 | line = next_line; | |
752 | } | |
ee6fc514 JL |
753 | close(cp.out); |
754 | ||
755 | if (finish_command(&cp)) | |
6a5cedac | 756 | die("'git status --porcelain' failed in submodule %s", path); |
ee6fc514 | 757 | |
ee6fc514 | 758 | strbuf_release(&buf); |
c7e1a736 | 759 | return dirty_submodule; |
ee6fc514 | 760 | } |
68d03e4a HV |
761 | |
762 | static int find_first_merges(struct object_array *result, const char *path, | |
763 | struct commit *a, struct commit *b) | |
764 | { | |
765 | int i, j; | |
766 | struct object_array merges; | |
767 | struct commit *commit; | |
768 | int contains_another; | |
769 | ||
770 | char merged_revision[42]; | |
771 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | |
772 | "--all", merged_revision, NULL }; | |
773 | struct rev_info revs; | |
774 | struct setup_revision_opt rev_opts; | |
775 | ||
776 | memset(&merges, 0, sizeof(merges)); | |
777 | memset(result, 0, sizeof(struct object_array)); | |
778 | memset(&rev_opts, 0, sizeof(rev_opts)); | |
779 | ||
780 | /* get all revisions that merge commit a */ | |
781 | snprintf(merged_revision, sizeof(merged_revision), "^%s", | |
782 | sha1_to_hex(a->object.sha1)); | |
783 | init_revisions(&revs, NULL); | |
784 | rev_opts.submodule = path; | |
785 | setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts); | |
786 | ||
787 | /* save all revisions from the above list that contain b */ | |
788 | if (prepare_revision_walk(&revs)) | |
789 | die("revision walk setup failed"); | |
790 | while ((commit = get_revision(&revs)) != NULL) { | |
791 | struct object *o = &(commit->object); | |
a20efee9 | 792 | if (in_merge_bases(b, commit)) |
68d03e4a HV |
793 | add_object_array(o, NULL, &merges); |
794 | } | |
bcc0a3ea | 795 | reset_revision_walk(); |
68d03e4a HV |
796 | |
797 | /* Now we've got all merges that contain a and b. Prune all | |
798 | * merges that contain another found merge and save them in | |
799 | * result. | |
800 | */ | |
801 | for (i = 0; i < merges.nr; i++) { | |
802 | struct commit *m1 = (struct commit *) merges.objects[i].item; | |
803 | ||
804 | contains_another = 0; | |
805 | for (j = 0; j < merges.nr; j++) { | |
806 | struct commit *m2 = (struct commit *) merges.objects[j].item; | |
a20efee9 | 807 | if (i != j && in_merge_bases(m2, m1)) { |
68d03e4a HV |
808 | contains_another = 1; |
809 | break; | |
810 | } | |
811 | } | |
812 | ||
813 | if (!contains_another) | |
814 | add_object_array(merges.objects[i].item, | |
815 | merges.objects[i].name, result); | |
816 | } | |
817 | ||
818 | free(merges.objects); | |
819 | return result->nr; | |
820 | } | |
821 | ||
822 | static void print_commit(struct commit *commit) | |
823 | { | |
824 | struct strbuf sb = STRBUF_INIT; | |
825 | struct pretty_print_context ctx = {0}; | |
826 | ctx.date_mode = DATE_NORMAL; | |
827 | format_commit_message(commit, " %h: %m %s", &sb, &ctx); | |
828 | fprintf(stderr, "%s\n", sb.buf); | |
829 | strbuf_release(&sb); | |
830 | } | |
831 | ||
832 | #define MERGE_WARNING(path, msg) \ | |
833 | warning("Failed to merge submodule %s (%s)", path, msg); | |
834 | ||
835 | int merge_submodule(unsigned char result[20], const char *path, | |
836 | const unsigned char base[20], const unsigned char a[20], | |
80988783 | 837 | const unsigned char b[20], int search) |
68d03e4a HV |
838 | { |
839 | struct commit *commit_base, *commit_a, *commit_b; | |
840 | int parent_count; | |
841 | struct object_array merges; | |
842 | ||
843 | int i; | |
844 | ||
845 | /* store a in result in case we fail */ | |
846 | hashcpy(result, a); | |
847 | ||
848 | /* we can not handle deletion conflicts */ | |
849 | if (is_null_sha1(base)) | |
850 | return 0; | |
851 | if (is_null_sha1(a)) | |
852 | return 0; | |
853 | if (is_null_sha1(b)) | |
854 | return 0; | |
855 | ||
856 | if (add_submodule_odb(path)) { | |
857 | MERGE_WARNING(path, "not checked out"); | |
858 | return 0; | |
859 | } | |
860 | ||
861 | if (!(commit_base = lookup_commit_reference(base)) || | |
862 | !(commit_a = lookup_commit_reference(a)) || | |
863 | !(commit_b = lookup_commit_reference(b))) { | |
864 | MERGE_WARNING(path, "commits not present"); | |
865 | return 0; | |
866 | } | |
867 | ||
868 | /* check whether both changes are forward */ | |
a20efee9 JH |
869 | if (!in_merge_bases(commit_base, commit_a) || |
870 | !in_merge_bases(commit_base, commit_b)) { | |
68d03e4a HV |
871 | MERGE_WARNING(path, "commits don't follow merge-base"); |
872 | return 0; | |
873 | } | |
874 | ||
875 | /* Case #1: a is contained in b or vice versa */ | |
a20efee9 | 876 | if (in_merge_bases(commit_a, commit_b)) { |
68d03e4a HV |
877 | hashcpy(result, b); |
878 | return 1; | |
879 | } | |
a20efee9 | 880 | if (in_merge_bases(commit_b, commit_a)) { |
68d03e4a HV |
881 | hashcpy(result, a); |
882 | return 1; | |
883 | } | |
884 | ||
885 | /* | |
886 | * Case #2: There are one or more merges that contain a and b in | |
887 | * the submodule. If there is only one, then present it as a | |
888 | * suggestion to the user, but leave it marked unmerged so the | |
889 | * user needs to confirm the resolution. | |
890 | */ | |
891 | ||
80988783 BK |
892 | /* Skip the search if makes no sense to the calling context. */ |
893 | if (!search) | |
894 | return 0; | |
895 | ||
68d03e4a HV |
896 | /* find commit which merges them */ |
897 | parent_count = find_first_merges(&merges, path, commit_a, commit_b); | |
898 | switch (parent_count) { | |
899 | case 0: | |
900 | MERGE_WARNING(path, "merge following commits not found"); | |
901 | break; | |
902 | ||
903 | case 1: | |
904 | MERGE_WARNING(path, "not fast-forward"); | |
905 | fprintf(stderr, "Found a possible merge resolution " | |
906 | "for the submodule:\n"); | |
907 | print_commit((struct commit *) merges.objects[0].item); | |
908 | fprintf(stderr, | |
909 | "If this is correct simply add it to the index " | |
910 | "for example\n" | |
911 | "by using:\n\n" | |
912 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" | |
913 | "which will accept this suggestion.\n", | |
914 | sha1_to_hex(merges.objects[0].item->sha1), path); | |
915 | break; | |
916 | ||
917 | default: | |
918 | MERGE_WARNING(path, "multiple merges found"); | |
919 | for (i = 0; i < merges.nr; i++) | |
920 | print_commit((struct commit *) merges.objects[i].item); | |
921 | } | |
922 | ||
923 | free(merges.objects); | |
924 | return 0; | |
925 | } |