]>
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 JL |
10 | #include "string-list.h" |
11 | ||
c2e86add SB |
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; | |
88a21979 | 15 | static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; |
2071fb01 | 16 | static struct string_list changed_submodule_paths; |
d4e98b58 JL |
17 | /* |
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 | |
23 | * ignored. | |
24 | */ | |
25 | static int gitmodules_is_unmerged; | |
752c0c24 | 26 | |
cb58c932 | 27 | static int add_submodule_odb(const char *path) |
752c0c24 JS |
28 | { |
29 | struct strbuf objects_directory = STRBUF_INIT; | |
30 | struct alternate_object_database *alt_odb; | |
de7a7960 | 31 | int ret = 0; |
eee49b6c | 32 | const char *git_dir; |
752c0c24 | 33 | |
eee49b6c | 34 | strbuf_addf(&objects_directory, "%s/.git", path); |
13d6ec91 | 35 | git_dir = read_gitfile(objects_directory.buf); |
eee49b6c JL |
36 | if (git_dir) { |
37 | strbuf_reset(&objects_directory); | |
38 | strbuf_addstr(&objects_directory, git_dir); | |
39 | } | |
40 | strbuf_addstr(&objects_directory, "/objects/"); | |
de7a7960 JL |
41 | if (!is_directory(objects_directory.buf)) { |
42 | ret = -1; | |
43 | goto done; | |
44 | } | |
752c0c24 JS |
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)) | |
de7a7960 | 50 | goto done; |
752c0c24 JS |
51 | |
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; | |
60 | prepare_alt_odb(); | |
de7a7960 JL |
61 | done: |
62 | strbuf_release(&objects_directory); | |
63 | return ret; | |
752c0c24 JS |
64 | } |
65 | ||
aee9c7d6 JL |
66 | void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, |
67 | const char *path) | |
68 | { | |
69 | struct string_list_item *path_option, *ignore_option; | |
70 | path_option = unsorted_string_list_lookup(&config_name_for_path, path); | |
71 | if (path_option) { | |
72 | ignore_option = unsorted_string_list_lookup(&config_ignore_for_name, path_option->util); | |
73 | if (ignore_option) | |
74 | handle_ignore_submodules_arg(diffopt, ignore_option->util); | |
d4e98b58 JL |
75 | else if (gitmodules_is_unmerged) |
76 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); | |
aee9c7d6 JL |
77 | } |
78 | } | |
79 | ||
7dce19d3 | 80 | int submodule_config(const char *var, const char *value, void *cb) |
302ad7a9 JL |
81 | { |
82 | if (!prefixcmp(var, "submodule.")) | |
83 | return parse_submodule_config_option(var, value); | |
be254a0e | 84 | else if (!strcmp(var, "fetch.recursesubmodules")) { |
1fb25502 | 85 | config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); |
be254a0e JL |
86 | return 0; |
87 | } | |
302ad7a9 JL |
88 | return 0; |
89 | } | |
90 | ||
91 | void gitmodules_config(void) | |
92 | { | |
93 | const char *work_tree = get_git_work_tree(); | |
94 | if (work_tree) { | |
95 | struct strbuf gitmodules_path = STRBUF_INIT; | |
d4e98b58 | 96 | int pos; |
302ad7a9 JL |
97 | strbuf_addstr(&gitmodules_path, work_tree); |
98 | strbuf_addstr(&gitmodules_path, "/.gitmodules"); | |
d4e98b58 JL |
99 | if (read_cache() < 0) |
100 | die("index file corrupt"); | |
101 | pos = cache_name_pos(".gitmodules", 11); | |
102 | if (pos < 0) { /* .gitmodules not found or isn't merged */ | |
103 | pos = -1 - pos; | |
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; | |
109 | } | |
110 | } | |
111 | ||
112 | if (!gitmodules_is_unmerged) | |
113 | git_config_from_file(submodule_config, gitmodules_path.buf, NULL); | |
302ad7a9 JL |
114 | strbuf_release(&gitmodules_path); |
115 | } | |
116 | } | |
117 | ||
aee9c7d6 JL |
118 | int parse_submodule_config_option(const char *var, const char *value) |
119 | { | |
120 | int len; | |
121 | struct string_list_item *config; | |
122 | struct strbuf submodname = STRBUF_INIT; | |
123 | ||
124 | var += 10; /* Skip "submodule." */ | |
125 | ||
126 | len = strlen(var); | |
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); | |
130 | if (config) | |
131 | free(config->util); | |
132 | else | |
133 | config = string_list_append(&config_name_for_path, xstrdup(value)); | |
134 | config->util = strbuf_detach(&submodname, NULL); | |
135 | strbuf_release(&submodname); | |
c1a3c364 JL |
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); | |
139 | if (!config) | |
140 | config = string_list_append(&config_fetch_recurse_submodules_for_name, | |
141 | strbuf_detach(&submodname, NULL)); | |
bf42b384 | 142 | config->util = (void *)(intptr_t)parse_fetch_recurse_submodules_arg(var, value); |
c1a3c364 | 143 | strbuf_release(&submodname); |
aee9c7d6 JL |
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); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | strbuf_add(&submodname, var, len - 7); | |
152 | config = unsorted_string_list_lookup(&config_ignore_for_name, submodname.buf); | |
153 | if (config) | |
154 | free(config->util); | |
155 | else | |
156 | config = string_list_append(&config_ignore_for_name, | |
157 | strbuf_detach(&submodname, NULL)); | |
158 | strbuf_release(&submodname); | |
159 | config->util = xstrdup(value); | |
160 | return 0; | |
161 | } | |
162 | return 0; | |
163 | } | |
164 | ||
46a958b3 JL |
165 | void handle_ignore_submodules_arg(struct diff_options *diffopt, |
166 | const char *arg) | |
167 | { | |
be4f2b40 JS |
168 | DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES); |
169 | DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
170 | DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES); | |
171 | ||
46a958b3 JL |
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); | |
aee9c7d6 | 178 | else if (strcmp(arg, "none")) |
46a958b3 JL |
179 | die("bad --ignore-submodules argument: %s", arg); |
180 | } | |
181 | ||
808a95dc JN |
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) | |
185 | { | |
186 | struct commit_list *merge_bases, *list; | |
187 | ||
188 | init_revisions(rev, NULL); | |
189 | setup_revisions(0, NULL, rev, NULL); | |
190 | rev->left_right = 1; | |
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); | |
196 | if (merge_bases) { | |
197 | if (merge_bases->item == left) | |
198 | *fast_forward = 1; | |
199 | else if (merge_bases->item == right) | |
200 | *fast_backward = 1; | |
201 | } | |
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)); | |
206 | } | |
207 | return prepare_revision_walk(rev); | |
208 | } | |
209 | ||
210 | static void print_submodule_summary(struct rev_info *rev, FILE *f, | |
211 | const char *del, const char *add, const char *reset) | |
212 | { | |
213 | static const char format[] = " %m %s"; | |
214 | struct strbuf sb = STRBUF_INIT; | |
215 | struct commit *commit; | |
216 | ||
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) { | |
222 | if (del) | |
223 | strbuf_addstr(&sb, del); | |
224 | } | |
225 | else if (add) | |
226 | strbuf_addstr(&sb, add); | |
227 | format_commit_message(commit, format, &sb, &ctx); | |
228 | if (reset) | |
229 | strbuf_addstr(&sb, reset); | |
230 | strbuf_addch(&sb, '\n'); | |
231 | fprintf(f, "%s", sb.buf); | |
232 | } | |
233 | strbuf_release(&sb); | |
234 | } | |
235 | ||
88a21979 JL |
236 | int parse_fetch_recurse_submodules_arg(const char *opt, const char *arg) |
237 | { | |
238 | switch (git_config_maybe_bool(opt, arg)) { | |
239 | case 1: | |
240 | return RECURSE_SUBMODULES_ON; | |
241 | case 0: | |
242 | return RECURSE_SUBMODULES_OFF; | |
243 | default: | |
244 | if (!strcmp(arg, "on-demand")) | |
245 | return RECURSE_SUBMODULES_ON_DEMAND; | |
246 | die("bad %s argument: %s", opt, arg); | |
247 | } | |
248 | } | |
249 | ||
752c0c24 JS |
250 | void show_submodule_summary(FILE *f, const char *path, |
251 | unsigned char one[20], unsigned char two[20], | |
721ceec1 | 252 | unsigned dirty_submodule, |
752c0c24 JS |
253 | const char *del, const char *add, const char *reset) |
254 | { | |
255 | struct rev_info rev; | |
808a95dc | 256 | struct commit *left = left, *right = right; |
752c0c24 JS |
257 | const char *message = NULL; |
258 | struct strbuf sb = STRBUF_INIT; | |
752c0c24 JS |
259 | int fast_forward = 0, fast_backward = 0; |
260 | ||
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)"; | |
270 | ||
808a95dc JN |
271 | if (!message && |
272 | prepare_submodule_summary(&rev, path, left, right, | |
273 | &fast_forward, &fast_backward)) | |
274 | message = "(revision walker failed)"; | |
752c0c24 | 275 | |
c7e1a736 JL |
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); | |
280 | ||
281 | if (!hashcmp(one, two)) { | |
282 | strbuf_release(&sb); | |
283 | return; | |
284 | } | |
285 | ||
752c0c24 JS |
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)); | |
291 | if (message) | |
292 | strbuf_addf(&sb, " %s\n", message); | |
293 | else | |
294 | strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); | |
295 | fwrite(sb.buf, sb.len, 1, f); | |
296 | ||
297 | if (!message) { | |
808a95dc | 298 | print_submodule_summary(&rev, f, del, add, reset); |
752c0c24 JS |
299 | clear_commit_marks(left, ~0); |
300 | clear_commit_marks(right, ~0); | |
301 | } | |
808a95dc | 302 | |
752c0c24 JS |
303 | strbuf_release(&sb); |
304 | } | |
ee6fc514 | 305 | |
be254a0e JL |
306 | void set_config_fetch_recurse_submodules(int value) |
307 | { | |
308 | config_fetch_recurse_submodules = value; | |
309 | } | |
310 | ||
d2b17b32 FG |
311 | static int has_remote(const char *refname, const unsigned char *sha1, int flags, void *cb_data) |
312 | { | |
313 | return 1; | |
314 | } | |
315 | ||
316 | static int submodule_needs_pushing(const char *path, const unsigned char sha1[20]) | |
317 | { | |
318 | if (add_submodule_odb(path) || !lookup_commit_reference(sha1)) | |
319 | return 0; | |
320 | ||
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; | |
326 | ||
327 | argv[1] = sha1_to_hex(sha1); | |
328 | memset(&cp, 0, sizeof(cp)); | |
329 | cp.argv = argv; | |
330 | cp.env = local_repo_env; | |
331 | cp.git_cmd = 1; | |
332 | cp.no_stdin = 1; | |
333 | cp.out = -1; | |
334 | cp.dir = path; | |
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)) | |
339 | needs_pushing = 1; | |
340 | finish_command(&cp); | |
341 | close(cp.out); | |
342 | strbuf_release(&buf); | |
343 | return needs_pushing; | |
344 | } | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | static void collect_submodules_from_diff(struct diff_queue_struct *q, | |
350 | struct diff_options *options, | |
351 | void *data) | |
352 | { | |
353 | int i; | |
354 | int *needs_pushing = data; | |
355 | ||
356 | for (i = 0; i < q->nr; i++) { | |
357 | struct diff_filepair *p = q->queue[i]; | |
358 | if (!S_ISGITLINK(p->two->mode)) | |
359 | continue; | |
360 | if (submodule_needs_pushing(p->two->path, p->two->sha1)) { | |
361 | *needs_pushing = 1; | |
362 | break; | |
363 | } | |
364 | } | |
365 | } | |
366 | ||
367 | ||
368 | static void commit_need_pushing(struct commit *commit, struct commit_list *parent, int *needs_pushing) | |
369 | { | |
370 | const unsigned char (*parents)[20]; | |
371 | unsigned int i, n; | |
372 | struct rev_info rev; | |
373 | ||
374 | n = commit_list_count(parent); | |
375 | parents = xmalloc(n * sizeof(*parents)); | |
376 | ||
377 | for (i = 0; i < n; i++) { | |
378 | hashcpy((unsigned char *)(parents + i), parent->item->object.sha1); | |
379 | parent = parent->next; | |
380 | } | |
381 | ||
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); | |
387 | ||
388 | free(parents); | |
389 | } | |
390 | ||
391 | int check_submodule_needs_pushing(unsigned char new_sha1[20], const char *remotes_name) | |
392 | { | |
393 | struct rev_info rev; | |
394 | struct commit *commit; | |
395 | const char *argv[] = {NULL, NULL, "--not", "NULL", NULL}; | |
396 | int argc = ARRAY_SIZE(argv) - 1; | |
397 | char *sha1_copy; | |
398 | int needs_pushing = 0; | |
399 | struct strbuf remotes_arg = STRBUF_INIT; | |
400 | ||
401 | strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name); | |
402 | init_revisions(&rev, NULL); | |
403 | sha1_copy = xstrdup(sha1_to_hex(new_sha1)); | |
404 | argv[1] = sha1_copy; | |
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"); | |
409 | ||
410 | while ((commit = get_revision(&rev)) && !needs_pushing) | |
411 | commit_need_pushing(commit, commit->parents, &needs_pushing); | |
412 | ||
413 | free(sha1_copy); | |
414 | strbuf_release(&remotes_arg); | |
415 | ||
416 | return needs_pushing; | |
417 | } | |
418 | ||
c16c3e40 JL |
419 | static int is_submodule_commit_present(const char *path, unsigned char sha1[20]) |
420 | { | |
421 | int is_present = 0; | |
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; | |
428 | ||
429 | argv[3] = sha1_to_hex(sha1); | |
430 | memset(&cp, 0, sizeof(cp)); | |
431 | cp.argv = argv; | |
432 | cp.env = local_repo_env; | |
433 | cp.git_cmd = 1; | |
434 | cp.no_stdin = 1; | |
435 | cp.out = -1; | |
436 | cp.dir = path; | |
437 | if (!run_command(&cp) && !strbuf_read(&buf, cp.out, 1024)) | |
438 | is_present = 1; | |
439 | ||
440 | close(cp.out); | |
441 | strbuf_release(&buf); | |
442 | } | |
443 | return is_present; | |
444 | } | |
445 | ||
88a21979 JL |
446 | static void submodule_collect_changed_cb(struct diff_queue_struct *q, |
447 | struct diff_options *options, | |
448 | void *data) | |
449 | { | |
450 | int i; | |
451 | for (i = 0; i < q->nr; i++) { | |
452 | struct diff_filepair *p = q->queue[i]; | |
453 | if (!S_ISGITLINK(p->two->mode)) | |
454 | continue; | |
455 | ||
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); | |
c16c3e40 | 463 | if (!path && !is_submodule_commit_present(p->two->path, p->two->sha1)) |
88a21979 JL |
464 | string_list_append(&changed_submodule_paths, xstrdup(p->two->path)); |
465 | } else { | |
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. */ | |
472 | continue; | |
473 | } | |
474 | } | |
475 | } | |
476 | ||
477 | void check_for_new_submodule_commits(unsigned char new_sha1[20]) | |
478 | { | |
479 | struct rev_info rev; | |
480 | struct commit *commit; | |
481 | const char *argv[] = {NULL, NULL, "--not", "--all", NULL}; | |
482 | int argc = ARRAY_SIZE(argv) - 1; | |
483 | ||
484 | init_revisions(&rev, NULL); | |
485 | argv[1] = xstrdup(sha1_to_hex(new_sha1)); | |
486 | setup_revisions(argc, argv, &rev, NULL); | |
487 | if (prepare_revision_walk(&rev)) | |
488 | die("revision walk setup failed"); | |
489 | ||
490 | /* | |
491 | * Collect all submodules (whether checked out or not) for which new | |
492 | * commits have been recorded upstream in "changed_submodule_paths". | |
493 | */ | |
494 | while ((commit = get_revision(&rev))) { | |
495 | struct commit_list *parent = commit->parents; | |
496 | while (parent) { | |
497 | struct diff_options diff_opts; | |
498 | diff_setup(&diff_opts); | |
ea2d325b | 499 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
88a21979 JL |
500 | diff_opts.output_format |= DIFF_FORMAT_CALLBACK; |
501 | diff_opts.format_callback = submodule_collect_changed_cb; | |
502 | if (diff_setup_done(&diff_opts) < 0) | |
503 | die("diff_setup_done failed"); | |
504 | diff_tree_sha1(parent->item->object.sha1, commit->object.sha1, "", &diff_opts); | |
505 | diffcore_std(&diff_opts); | |
506 | diff_flush(&diff_opts); | |
507 | parent = parent->next; | |
508 | } | |
509 | } | |
510 | free((char *)argv[1]); | |
511 | } | |
512 | ||
7dce19d3 | 513 | int fetch_populated_submodules(int num_options, const char **options, |
8f0700dd | 514 | const char *prefix, int command_line_option, |
be254a0e | 515 | int quiet) |
7dce19d3 | 516 | { |
88a21979 | 517 | int i, result = 0, argc = 0, default_argc; |
7dce19d3 JL |
518 | struct child_process cp; |
519 | const char **argv; | |
520 | struct string_list_item *name_for_path; | |
521 | const char *work_tree = get_git_work_tree(); | |
522 | if (!work_tree) | |
88a21979 | 523 | goto out; |
7dce19d3 JL |
524 | |
525 | if (!the_index.initialized) | |
526 | if (read_cache() < 0) | |
527 | die("index file corrupt"); | |
528 | ||
88a21979 JL |
529 | /* 6: "fetch" (options) --recurse-submodules-default default "--submodule-prefix" prefix NULL */ |
530 | argv = xcalloc(num_options + 6, sizeof(const char *)); | |
7dce19d3 JL |
531 | argv[argc++] = "fetch"; |
532 | for (i = 0; i < num_options; i++) | |
533 | argv[argc++] = options[i]; | |
88a21979 JL |
534 | argv[argc++] = "--recurse-submodules-default"; |
535 | default_argc = argc++; | |
7dce19d3 JL |
536 | argv[argc++] = "--submodule-prefix"; |
537 | ||
538 | memset(&cp, 0, sizeof(cp)); | |
539 | cp.argv = argv; | |
540 | cp.env = local_repo_env; | |
541 | cp.git_cmd = 1; | |
542 | cp.no_stdin = 1; | |
543 | ||
544 | for (i = 0; i < active_nr; i++) { | |
545 | struct strbuf submodule_path = STRBUF_INIT; | |
546 | struct strbuf submodule_git_dir = STRBUF_INIT; | |
547 | struct strbuf submodule_prefix = STRBUF_INIT; | |
548 | struct cache_entry *ce = active_cache[i]; | |
88a21979 | 549 | const char *git_dir, *name, *default_argv; |
7dce19d3 JL |
550 | |
551 | if (!S_ISGITLINK(ce->ce_mode)) | |
552 | continue; | |
553 | ||
554 | name = ce->name; | |
555 | name_for_path = unsorted_string_list_lookup(&config_name_for_path, ce->name); | |
556 | if (name_for_path) | |
557 | name = name_for_path->util; | |
558 | ||
88a21979 | 559 | default_argv = "yes"; |
8f0700dd | 560 | if (command_line_option == RECURSE_SUBMODULES_DEFAULT) { |
c1a3c364 JL |
561 | struct string_list_item *fetch_recurse_submodules_option; |
562 | fetch_recurse_submodules_option = unsorted_string_list_lookup(&config_fetch_recurse_submodules_for_name, name); | |
563 | if (fetch_recurse_submodules_option) { | |
bf42b384 | 564 | if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_OFF) |
c1a3c364 | 565 | continue; |
bf42b384 JL |
566 | if ((intptr_t)fetch_recurse_submodules_option->util == RECURSE_SUBMODULES_ON_DEMAND) { |
567 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
568 | continue; | |
569 | default_argv = "on-demand"; | |
570 | } | |
c1a3c364 | 571 | } else { |
d4e98b58 JL |
572 | if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || |
573 | gitmodules_is_unmerged) | |
c1a3c364 | 574 | continue; |
88a21979 JL |
575 | if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { |
576 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
577 | continue; | |
578 | default_argv = "on-demand"; | |
579 | } | |
c1a3c364 | 580 | } |
8f0700dd JL |
581 | } else if (command_line_option == RECURSE_SUBMODULES_ON_DEMAND) { |
582 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
583 | continue; | |
584 | default_argv = "on-demand"; | |
be254a0e JL |
585 | } |
586 | ||
7dce19d3 JL |
587 | strbuf_addf(&submodule_path, "%s/%s", work_tree, ce->name); |
588 | strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); | |
589 | strbuf_addf(&submodule_prefix, "%s%s/", prefix, ce->name); | |
13d6ec91 | 590 | git_dir = read_gitfile(submodule_git_dir.buf); |
7dce19d3 JL |
591 | if (!git_dir) |
592 | git_dir = submodule_git_dir.buf; | |
593 | if (is_directory(git_dir)) { | |
594 | if (!quiet) | |
595 | printf("Fetching submodule %s%s\n", prefix, ce->name); | |
596 | cp.dir = submodule_path.buf; | |
88a21979 | 597 | argv[default_argc] = default_argv; |
7dce19d3 JL |
598 | argv[argc] = submodule_prefix.buf; |
599 | if (run_command(&cp)) | |
600 | result = 1; | |
601 | } | |
602 | strbuf_release(&submodule_path); | |
603 | strbuf_release(&submodule_git_dir); | |
604 | strbuf_release(&submodule_prefix); | |
605 | } | |
606 | free(argv); | |
88a21979 JL |
607 | out: |
608 | string_list_clear(&changed_submodule_paths, 1); | |
7dce19d3 JL |
609 | return result; |
610 | } | |
611 | ||
3bfc4504 | 612 | unsigned is_submodule_modified(const char *path, int ignore_untracked) |
ee6fc514 | 613 | { |
c7e1a736 | 614 | ssize_t len; |
ee6fc514 JL |
615 | struct child_process cp; |
616 | const char *argv[] = { | |
617 | "status", | |
618 | "--porcelain", | |
619 | NULL, | |
3bfc4504 | 620 | NULL, |
ee6fc514 | 621 | }; |
ee6fc514 | 622 | struct strbuf buf = STRBUF_INIT; |
c7e1a736 JL |
623 | unsigned dirty_submodule = 0; |
624 | const char *line, *next_line; | |
eee49b6c | 625 | const char *git_dir; |
ee6fc514 | 626 | |
eee49b6c | 627 | strbuf_addf(&buf, "%s/.git", path); |
13d6ec91 | 628 | git_dir = read_gitfile(buf.buf); |
eee49b6c JL |
629 | if (!git_dir) |
630 | git_dir = buf.buf; | |
631 | if (!is_directory(git_dir)) { | |
ee6fc514 JL |
632 | strbuf_release(&buf); |
633 | /* The submodule is not checked out, so it is not modified */ | |
634 | return 0; | |
635 | ||
636 | } | |
637 | strbuf_reset(&buf); | |
638 | ||
3bfc4504 JL |
639 | if (ignore_untracked) |
640 | argv[2] = "-uno"; | |
641 | ||
ee6fc514 JL |
642 | memset(&cp, 0, sizeof(cp)); |
643 | cp.argv = argv; | |
eee49b6c | 644 | cp.env = local_repo_env; |
ee6fc514 JL |
645 | cp.git_cmd = 1; |
646 | cp.no_stdin = 1; | |
647 | cp.out = -1; | |
eee49b6c | 648 | cp.dir = path; |
ee6fc514 JL |
649 | if (start_command(&cp)) |
650 | die("Could not run git status --porcelain"); | |
651 | ||
652 | len = strbuf_read(&buf, cp.out, 1024); | |
c7e1a736 JL |
653 | line = buf.buf; |
654 | while (len > 2) { | |
655 | if ((line[0] == '?') && (line[1] == '?')) { | |
656 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; | |
657 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
658 | break; | |
659 | } else { | |
660 | dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; | |
3bfc4504 JL |
661 | if (ignore_untracked || |
662 | (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)) | |
c7e1a736 JL |
663 | break; |
664 | } | |
665 | next_line = strchr(line, '\n'); | |
666 | if (!next_line) | |
667 | break; | |
668 | next_line++; | |
669 | len -= (next_line - line); | |
670 | line = next_line; | |
671 | } | |
ee6fc514 JL |
672 | close(cp.out); |
673 | ||
674 | if (finish_command(&cp)) | |
675 | die("git status --porcelain failed"); | |
676 | ||
ee6fc514 | 677 | strbuf_release(&buf); |
c7e1a736 | 678 | return dirty_submodule; |
ee6fc514 | 679 | } |
68d03e4a HV |
680 | |
681 | static int find_first_merges(struct object_array *result, const char *path, | |
682 | struct commit *a, struct commit *b) | |
683 | { | |
684 | int i, j; | |
685 | struct object_array merges; | |
686 | struct commit *commit; | |
687 | int contains_another; | |
688 | ||
689 | char merged_revision[42]; | |
690 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | |
691 | "--all", merged_revision, NULL }; | |
692 | struct rev_info revs; | |
693 | struct setup_revision_opt rev_opts; | |
694 | ||
695 | memset(&merges, 0, sizeof(merges)); | |
696 | memset(result, 0, sizeof(struct object_array)); | |
697 | memset(&rev_opts, 0, sizeof(rev_opts)); | |
698 | ||
699 | /* get all revisions that merge commit a */ | |
700 | snprintf(merged_revision, sizeof(merged_revision), "^%s", | |
701 | sha1_to_hex(a->object.sha1)); | |
702 | init_revisions(&revs, NULL); | |
703 | rev_opts.submodule = path; | |
704 | setup_revisions(sizeof(rev_args)/sizeof(char *)-1, rev_args, &revs, &rev_opts); | |
705 | ||
706 | /* save all revisions from the above list that contain b */ | |
707 | if (prepare_revision_walk(&revs)) | |
708 | die("revision walk setup failed"); | |
709 | while ((commit = get_revision(&revs)) != NULL) { | |
710 | struct object *o = &(commit->object); | |
711 | if (in_merge_bases(b, &commit, 1)) | |
712 | add_object_array(o, NULL, &merges); | |
713 | } | |
714 | ||
715 | /* Now we've got all merges that contain a and b. Prune all | |
716 | * merges that contain another found merge and save them in | |
717 | * result. | |
718 | */ | |
719 | for (i = 0; i < merges.nr; i++) { | |
720 | struct commit *m1 = (struct commit *) merges.objects[i].item; | |
721 | ||
722 | contains_another = 0; | |
723 | for (j = 0; j < merges.nr; j++) { | |
724 | struct commit *m2 = (struct commit *) merges.objects[j].item; | |
725 | if (i != j && in_merge_bases(m2, &m1, 1)) { | |
726 | contains_another = 1; | |
727 | break; | |
728 | } | |
729 | } | |
730 | ||
731 | if (!contains_another) | |
732 | add_object_array(merges.objects[i].item, | |
733 | merges.objects[i].name, result); | |
734 | } | |
735 | ||
736 | free(merges.objects); | |
737 | return result->nr; | |
738 | } | |
739 | ||
740 | static void print_commit(struct commit *commit) | |
741 | { | |
742 | struct strbuf sb = STRBUF_INIT; | |
743 | struct pretty_print_context ctx = {0}; | |
744 | ctx.date_mode = DATE_NORMAL; | |
745 | format_commit_message(commit, " %h: %m %s", &sb, &ctx); | |
746 | fprintf(stderr, "%s\n", sb.buf); | |
747 | strbuf_release(&sb); | |
748 | } | |
749 | ||
750 | #define MERGE_WARNING(path, msg) \ | |
751 | warning("Failed to merge submodule %s (%s)", path, msg); | |
752 | ||
753 | int merge_submodule(unsigned char result[20], const char *path, | |
754 | const unsigned char base[20], const unsigned char a[20], | |
755 | const unsigned char b[20]) | |
756 | { | |
757 | struct commit *commit_base, *commit_a, *commit_b; | |
758 | int parent_count; | |
759 | struct object_array merges; | |
760 | ||
761 | int i; | |
762 | ||
763 | /* store a in result in case we fail */ | |
764 | hashcpy(result, a); | |
765 | ||
766 | /* we can not handle deletion conflicts */ | |
767 | if (is_null_sha1(base)) | |
768 | return 0; | |
769 | if (is_null_sha1(a)) | |
770 | return 0; | |
771 | if (is_null_sha1(b)) | |
772 | return 0; | |
773 | ||
774 | if (add_submodule_odb(path)) { | |
775 | MERGE_WARNING(path, "not checked out"); | |
776 | return 0; | |
777 | } | |
778 | ||
779 | if (!(commit_base = lookup_commit_reference(base)) || | |
780 | !(commit_a = lookup_commit_reference(a)) || | |
781 | !(commit_b = lookup_commit_reference(b))) { | |
782 | MERGE_WARNING(path, "commits not present"); | |
783 | return 0; | |
784 | } | |
785 | ||
786 | /* check whether both changes are forward */ | |
787 | if (!in_merge_bases(commit_base, &commit_a, 1) || | |
788 | !in_merge_bases(commit_base, &commit_b, 1)) { | |
789 | MERGE_WARNING(path, "commits don't follow merge-base"); | |
790 | return 0; | |
791 | } | |
792 | ||
793 | /* Case #1: a is contained in b or vice versa */ | |
794 | if (in_merge_bases(commit_a, &commit_b, 1)) { | |
795 | hashcpy(result, b); | |
796 | return 1; | |
797 | } | |
798 | if (in_merge_bases(commit_b, &commit_a, 1)) { | |
799 | hashcpy(result, a); | |
800 | return 1; | |
801 | } | |
802 | ||
803 | /* | |
804 | * Case #2: There are one or more merges that contain a and b in | |
805 | * the submodule. If there is only one, then present it as a | |
806 | * suggestion to the user, but leave it marked unmerged so the | |
807 | * user needs to confirm the resolution. | |
808 | */ | |
809 | ||
810 | /* find commit which merges them */ | |
811 | parent_count = find_first_merges(&merges, path, commit_a, commit_b); | |
812 | switch (parent_count) { | |
813 | case 0: | |
814 | MERGE_WARNING(path, "merge following commits not found"); | |
815 | break; | |
816 | ||
817 | case 1: | |
818 | MERGE_WARNING(path, "not fast-forward"); | |
819 | fprintf(stderr, "Found a possible merge resolution " | |
820 | "for the submodule:\n"); | |
821 | print_commit((struct commit *) merges.objects[0].item); | |
822 | fprintf(stderr, | |
823 | "If this is correct simply add it to the index " | |
824 | "for example\n" | |
825 | "by using:\n\n" | |
826 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" | |
827 | "which will accept this suggestion.\n", | |
828 | sha1_to_hex(merges.objects[0].item->sha1), path); | |
829 | break; | |
830 | ||
831 | default: | |
832 | MERGE_WARNING(path, "multiple merges found"); | |
833 | for (i = 0; i < merges.nr; i++) | |
834 | print_commit((struct commit *) merges.objects[i].item); | |
835 | } | |
836 | ||
837 | free(merges.objects); | |
838 | return 0; | |
839 | } |