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