]>
Commit | Line | Data |
---|---|---|
752c0c24 | 1 | #include "cache.h" |
851e18c3 | 2 | #include "submodule-config.h" |
752c0c24 JS |
3 | #include "submodule.h" |
4 | #include "dir.h" | |
5 | #include "diff.h" | |
6 | #include "commit.h" | |
7 | #include "revision.h" | |
ee6fc514 | 8 | #include "run-command.h" |
c7e1a736 | 9 | #include "diffcore.h" |
68d03e4a | 10 | #include "refs.h" |
aee9c7d6 | 11 | #include "string-list.h" |
6859de45 | 12 | #include "sha1-array.h" |
c1189cae | 13 | #include "argv-array.h" |
5fee9952 | 14 | #include "blob.h" |
fe85ee6e | 15 | #include "thread-utils.h" |
4638728c | 16 | #include "quote.h" |
f6f85861 | 17 | #include "worktree.h" |
aee9c7d6 | 18 | |
88a21979 | 19 | static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND; |
a028a193 | 20 | static int parallel_jobs = 1; |
2721ce21 | 21 | static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP; |
6859de45 JK |
22 | static int initialized_fetch_ref_tips; |
23 | static struct sha1_array ref_tips_before_fetch; | |
24 | static struct sha1_array ref_tips_after_fetch; | |
25 | ||
d4e98b58 JL |
26 | /* |
27 | * The following flag is set if the .gitmodules file is unmerged. We then | |
28 | * disable recursion for all submodules where .git/config doesn't have a | |
29 | * matching config entry because we can't guess what might be configured in | |
30 | * .gitmodules unless the user resolves the conflict. When a command line | |
31 | * option is given (which always overrides configuration) this flag will be | |
32 | * ignored. | |
33 | */ | |
34 | static int gitmodules_is_unmerged; | |
752c0c24 | 35 | |
5fee9952 JL |
36 | /* |
37 | * This flag is set if the .gitmodules file had unstaged modifications on | |
38 | * startup. This must be checked before allowing modifications to the | |
39 | * .gitmodules file with the intention to stage them later, because when | |
40 | * continuing we would stage the modifications the user didn't stage herself | |
41 | * too. That might change in a future version when we learn to stage the | |
42 | * changes we do ourselves without staging any previous modifications. | |
43 | */ | |
44 | static int gitmodules_is_modified; | |
45 | ||
5fee9952 JL |
46 | int is_staging_gitmodules_ok(void) |
47 | { | |
48 | return !gitmodules_is_modified; | |
49 | } | |
50 | ||
0656781f JL |
51 | /* |
52 | * Try to update the "path" entry in the "submodule.<name>" section of the | |
53 | * .gitmodules file. Return 0 only if a .gitmodules file was found, a section | |
54 | * with the correct path=<oldpath> setting was found and we could update it. | |
55 | */ | |
56 | int update_path_in_gitmodules(const char *oldpath, const char *newpath) | |
57 | { | |
58 | struct strbuf entry = STRBUF_INIT; | |
851e18c3 | 59 | const struct submodule *submodule; |
0656781f JL |
60 | |
61 | if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ | |
62 | return -1; | |
63 | ||
64 | if (gitmodules_is_unmerged) | |
65 | die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); | |
66 | ||
851e18c3 HV |
67 | submodule = submodule_from_path(null_sha1, oldpath); |
68 | if (!submodule || !submodule->name) { | |
0656781f JL |
69 | warning(_("Could not find section in .gitmodules where path=%s"), oldpath); |
70 | return -1; | |
71 | } | |
72 | strbuf_addstr(&entry, "submodule."); | |
851e18c3 | 73 | strbuf_addstr(&entry, submodule->name); |
0656781f | 74 | strbuf_addstr(&entry, ".path"); |
30598ad0 | 75 | if (git_config_set_in_file_gently(".gitmodules", entry.buf, newpath) < 0) { |
0656781f JL |
76 | /* Maybe the user already did that, don't error out here */ |
77 | warning(_("Could not update .gitmodules entry %s"), entry.buf); | |
78 | strbuf_release(&entry); | |
79 | return -1; | |
80 | } | |
81 | strbuf_release(&entry); | |
82 | return 0; | |
83 | } | |
84 | ||
95c16418 JL |
85 | /* |
86 | * Try to remove the "submodule.<name>" section from .gitmodules where the given | |
87 | * path is configured. Return 0 only if a .gitmodules file was found, a section | |
88 | * with the correct path=<path> setting was found and we could remove it. | |
89 | */ | |
90 | int remove_path_from_gitmodules(const char *path) | |
91 | { | |
92 | struct strbuf sect = STRBUF_INIT; | |
851e18c3 | 93 | const struct submodule *submodule; |
95c16418 JL |
94 | |
95 | if (!file_exists(".gitmodules")) /* Do nothing without .gitmodules */ | |
96 | return -1; | |
97 | ||
98 | if (gitmodules_is_unmerged) | |
99 | die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); | |
100 | ||
851e18c3 HV |
101 | submodule = submodule_from_path(null_sha1, path); |
102 | if (!submodule || !submodule->name) { | |
95c16418 JL |
103 | warning(_("Could not find section in .gitmodules where path=%s"), path); |
104 | return -1; | |
105 | } | |
106 | strbuf_addstr(§, "submodule."); | |
851e18c3 | 107 | strbuf_addstr(§, submodule->name); |
95c16418 JL |
108 | if (git_config_rename_section_in_file(".gitmodules", sect.buf, NULL) < 0) { |
109 | /* Maybe the user already did that, don't error out here */ | |
110 | warning(_("Could not remove .gitmodules entry for %s"), path); | |
111 | strbuf_release(§); | |
112 | return -1; | |
113 | } | |
114 | strbuf_release(§); | |
115 | return 0; | |
116 | } | |
117 | ||
5fee9952 JL |
118 | void stage_updated_gitmodules(void) |
119 | { | |
bc8d6b9b | 120 | if (add_file_to_cache(".gitmodules", 0)) |
5fee9952 JL |
121 | die(_("staging updated .gitmodules failed")); |
122 | } | |
123 | ||
cb58c932 | 124 | static int add_submodule_odb(const char *path) |
752c0c24 JS |
125 | { |
126 | struct strbuf objects_directory = STRBUF_INIT; | |
de7a7960 | 127 | int ret = 0; |
752c0c24 | 128 | |
99b43a61 JK |
129 | ret = strbuf_git_path_submodule(&objects_directory, path, "objects/"); |
130 | if (ret) | |
131 | goto done; | |
de7a7960 JL |
132 | if (!is_directory(objects_directory.buf)) { |
133 | ret = -1; | |
134 | goto done; | |
135 | } | |
a5b34d21 | 136 | add_to_alternates_memory(objects_directory.buf); |
de7a7960 JL |
137 | done: |
138 | strbuf_release(&objects_directory); | |
139 | return ret; | |
752c0c24 JS |
140 | } |
141 | ||
aee9c7d6 JL |
142 | void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, |
143 | const char *path) | |
144 | { | |
851e18c3 HV |
145 | const struct submodule *submodule = submodule_from_path(null_sha1, path); |
146 | if (submodule) { | |
147 | if (submodule->ignore) | |
148 | handle_ignore_submodules_arg(diffopt, submodule->ignore); | |
d4e98b58 JL |
149 | else if (gitmodules_is_unmerged) |
150 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); | |
aee9c7d6 JL |
151 | } |
152 | } | |
153 | ||
7dce19d3 | 154 | int submodule_config(const char *var, const char *value, void *cb) |
302ad7a9 | 155 | { |
a028a193 SB |
156 | if (!strcmp(var, "submodule.fetchjobs")) { |
157 | parallel_jobs = git_config_int(var, value); | |
158 | if (parallel_jobs < 0) | |
159 | die(_("negative values not allowed for submodule.fetchJobs")); | |
160 | return 0; | |
161 | } else if (starts_with(var, "submodule.")) | |
302ad7a9 | 162 | return parse_submodule_config_option(var, value); |
be254a0e | 163 | else if (!strcmp(var, "fetch.recursesubmodules")) { |
1fb25502 | 164 | config_fetch_recurse_submodules = parse_fetch_recurse_submodules_arg(var, value); |
be254a0e JL |
165 | return 0; |
166 | } | |
302ad7a9 JL |
167 | return 0; |
168 | } | |
169 | ||
170 | void gitmodules_config(void) | |
171 | { | |
172 | const char *work_tree = get_git_work_tree(); | |
173 | if (work_tree) { | |
174 | struct strbuf gitmodules_path = STRBUF_INIT; | |
d4e98b58 | 175 | int pos; |
302ad7a9 JL |
176 | strbuf_addstr(&gitmodules_path, work_tree); |
177 | strbuf_addstr(&gitmodules_path, "/.gitmodules"); | |
d4e98b58 JL |
178 | if (read_cache() < 0) |
179 | die("index file corrupt"); | |
180 | pos = cache_name_pos(".gitmodules", 11); | |
181 | if (pos < 0) { /* .gitmodules not found or isn't merged */ | |
182 | pos = -1 - pos; | |
183 | if (active_nr > pos) { /* there is a .gitmodules */ | |
184 | const struct cache_entry *ce = active_cache[pos]; | |
185 | if (ce_namelen(ce) == 11 && | |
186 | !memcmp(ce->name, ".gitmodules", 11)) | |
187 | gitmodules_is_unmerged = 1; | |
188 | } | |
5fee9952 JL |
189 | } else if (pos < active_nr) { |
190 | struct stat st; | |
191 | if (lstat(".gitmodules", &st) == 0 && | |
192 | ce_match_stat(active_cache[pos], &st, 0) & DATA_CHANGED) | |
193 | gitmodules_is_modified = 1; | |
d4e98b58 JL |
194 | } |
195 | ||
196 | if (!gitmodules_is_unmerged) | |
197 | git_config_from_file(submodule_config, gitmodules_path.buf, NULL); | |
302ad7a9 JL |
198 | strbuf_release(&gitmodules_path); |
199 | } | |
200 | } | |
201 | ||
9ebf689a BW |
202 | void gitmodules_config_sha1(const unsigned char *commit_sha1) |
203 | { | |
204 | struct strbuf rev = STRBUF_INIT; | |
205 | unsigned char sha1[20]; | |
206 | ||
207 | if (gitmodule_sha1_from_commit(commit_sha1, sha1, &rev)) { | |
208 | git_config_from_blob_sha1(submodule_config, rev.buf, | |
209 | sha1, NULL); | |
210 | } | |
211 | strbuf_release(&rev); | |
212 | } | |
213 | ||
f9f42560 BW |
214 | /* |
215 | * Determine if a submodule has been initialized at a given 'path' | |
216 | */ | |
217 | int is_submodule_initialized(const char *path) | |
218 | { | |
219 | int ret = 0; | |
220 | const struct submodule *module = NULL; | |
221 | ||
222 | module = submodule_from_path(null_sha1, path); | |
223 | ||
224 | if (module) { | |
225 | char *key = xstrfmt("submodule.%s.url", module->name); | |
226 | char *value = NULL; | |
227 | ||
228 | ret = !git_config_get_string(key, &value); | |
229 | ||
230 | free(value); | |
231 | free(key); | |
232 | } | |
233 | ||
234 | return ret; | |
235 | } | |
236 | ||
5688c28d BW |
237 | /* |
238 | * Determine if a submodule has been populated at a given 'path' | |
239 | */ | |
240 | int is_submodule_populated(const char *path) | |
241 | { | |
242 | int ret = 0; | |
243 | char *gitdir = xstrfmt("%s/.git", path); | |
244 | ||
245 | if (resolve_gitdir(gitdir)) | |
246 | ret = 1; | |
247 | ||
248 | free(gitdir); | |
249 | return ret; | |
250 | } | |
251 | ||
ea2fa5a3 SB |
252 | int parse_submodule_update_strategy(const char *value, |
253 | struct submodule_update_strategy *dst) | |
254 | { | |
255 | free((void*)dst->command); | |
256 | dst->command = NULL; | |
257 | if (!strcmp(value, "none")) | |
258 | dst->type = SM_UPDATE_NONE; | |
259 | else if (!strcmp(value, "checkout")) | |
260 | dst->type = SM_UPDATE_CHECKOUT; | |
261 | else if (!strcmp(value, "rebase")) | |
262 | dst->type = SM_UPDATE_REBASE; | |
263 | else if (!strcmp(value, "merge")) | |
264 | dst->type = SM_UPDATE_MERGE; | |
265 | else if (skip_prefix(value, "!", &value)) { | |
266 | dst->type = SM_UPDATE_COMMAND; | |
267 | dst->command = xstrdup(value); | |
268 | } else | |
269 | return -1; | |
270 | return 0; | |
271 | } | |
272 | ||
3604242f SB |
273 | const char *submodule_strategy_to_string(const struct submodule_update_strategy *s) |
274 | { | |
275 | struct strbuf sb = STRBUF_INIT; | |
276 | switch (s->type) { | |
277 | case SM_UPDATE_CHECKOUT: | |
278 | return "checkout"; | |
279 | case SM_UPDATE_MERGE: | |
280 | return "merge"; | |
281 | case SM_UPDATE_REBASE: | |
282 | return "rebase"; | |
283 | case SM_UPDATE_NONE: | |
284 | return "none"; | |
285 | case SM_UPDATE_UNSPECIFIED: | |
286 | return NULL; | |
287 | case SM_UPDATE_COMMAND: | |
288 | strbuf_addf(&sb, "!%s", s->command); | |
289 | return strbuf_detach(&sb, NULL); | |
290 | } | |
291 | return NULL; | |
292 | } | |
293 | ||
46a958b3 JL |
294 | void handle_ignore_submodules_arg(struct diff_options *diffopt, |
295 | const char *arg) | |
296 | { | |
be4f2b40 JS |
297 | DIFF_OPT_CLR(diffopt, IGNORE_SUBMODULES); |
298 | DIFF_OPT_CLR(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
299 | DIFF_OPT_CLR(diffopt, IGNORE_DIRTY_SUBMODULES); | |
300 | ||
46a958b3 JL |
301 | if (!strcmp(arg, "all")) |
302 | DIFF_OPT_SET(diffopt, IGNORE_SUBMODULES); | |
303 | else if (!strcmp(arg, "untracked")) | |
304 | DIFF_OPT_SET(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES); | |
305 | else if (!strcmp(arg, "dirty")) | |
306 | DIFF_OPT_SET(diffopt, IGNORE_DIRTY_SUBMODULES); | |
aee9c7d6 | 307 | else if (strcmp(arg, "none")) |
46a958b3 JL |
308 | die("bad --ignore-submodules argument: %s", arg); |
309 | } | |
310 | ||
808a95dc JN |
311 | static int prepare_submodule_summary(struct rev_info *rev, const char *path, |
312 | struct commit *left, struct commit *right, | |
8e6df650 | 313 | struct commit_list *merge_bases) |
808a95dc | 314 | { |
8e6df650 | 315 | struct commit_list *list; |
808a95dc JN |
316 | |
317 | init_revisions(rev, NULL); | |
318 | setup_revisions(0, NULL, rev, NULL); | |
319 | rev->left_right = 1; | |
320 | rev->first_parent_only = 1; | |
321 | left->object.flags |= SYMMETRIC_LEFT; | |
322 | add_pending_object(rev, &left->object, path); | |
323 | add_pending_object(rev, &right->object, path); | |
808a95dc JN |
324 | for (list = merge_bases; list; list = list->next) { |
325 | list->item->object.flags |= UNINTERESTING; | |
326 | add_pending_object(rev, &list->item->object, | |
f2fd0760 | 327 | oid_to_hex(&list->item->object.oid)); |
808a95dc JN |
328 | } |
329 | return prepare_revision_walk(rev); | |
330 | } | |
331 | ||
332 | static void print_submodule_summary(struct rev_info *rev, FILE *f, | |
0f33a067 | 333 | const char *line_prefix, |
808a95dc JN |
334 | const char *del, const char *add, const char *reset) |
335 | { | |
336 | static const char format[] = " %m %s"; | |
337 | struct strbuf sb = STRBUF_INIT; | |
338 | struct commit *commit; | |
339 | ||
340 | while ((commit = get_revision(rev))) { | |
341 | struct pretty_print_context ctx = {0}; | |
342 | ctx.date_mode = rev->date_mode; | |
ecaee805 | 343 | ctx.output_encoding = get_log_output_encoding(); |
808a95dc | 344 | strbuf_setlen(&sb, 0); |
0f33a067 | 345 | strbuf_addstr(&sb, line_prefix); |
808a95dc JN |
346 | if (commit->object.flags & SYMMETRIC_LEFT) { |
347 | if (del) | |
348 | strbuf_addstr(&sb, del); | |
349 | } | |
350 | else if (add) | |
351 | strbuf_addstr(&sb, add); | |
352 | format_commit_message(commit, format, &sb, &ctx); | |
353 | if (reset) | |
354 | strbuf_addstr(&sb, reset); | |
355 | strbuf_addch(&sb, '\n'); | |
356 | fprintf(f, "%s", sb.buf); | |
357 | } | |
358 | strbuf_release(&sb); | |
359 | } | |
360 | ||
8e6df650 JK |
361 | /* Helper function to display the submodule header line prior to the full |
362 | * summary output. If it can locate the submodule objects directory it will | |
363 | * attempt to lookup both the left and right commits and put them into the | |
364 | * left and right pointers. | |
365 | */ | |
366 | static void show_submodule_header(FILE *f, const char *path, | |
0f33a067 | 367 | const char *line_prefix, |
602a283a | 368 | struct object_id *one, struct object_id *two, |
4e215131 | 369 | unsigned dirty_submodule, const char *meta, |
8e6df650 JK |
370 | const char *reset, |
371 | struct commit **left, struct commit **right, | |
372 | struct commit_list **merge_bases) | |
752c0c24 | 373 | { |
752c0c24 JS |
374 | const char *message = NULL; |
375 | struct strbuf sb = STRBUF_INIT; | |
752c0c24 JS |
376 | int fast_forward = 0, fast_backward = 0; |
377 | ||
c7e1a736 | 378 | if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) |
0f33a067 JK |
379 | fprintf(f, "%sSubmodule %s contains untracked content\n", |
380 | line_prefix, path); | |
c7e1a736 | 381 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) |
0f33a067 JK |
382 | fprintf(f, "%sSubmodule %s contains modified content\n", |
383 | line_prefix, path); | |
c7e1a736 | 384 | |
8e6df650 JK |
385 | if (is_null_oid(one)) |
386 | message = "(new submodule)"; | |
387 | else if (is_null_oid(two)) | |
388 | message = "(submodule deleted)"; | |
389 | ||
390 | if (add_submodule_odb(path)) { | |
391 | if (!message) | |
392 | message = "(not initialized)"; | |
393 | goto output_header; | |
394 | } | |
395 | ||
396 | /* | |
397 | * Attempt to lookup the commit references, and determine if this is | |
398 | * a fast forward or fast backwards update. | |
399 | */ | |
400 | *left = lookup_commit_reference(one->hash); | |
401 | *right = lookup_commit_reference(two->hash); | |
402 | ||
403 | /* | |
404 | * Warn about missing commits in the submodule project, but only if | |
405 | * they aren't null. | |
406 | */ | |
407 | if ((!is_null_oid(one) && !*left) || | |
408 | (!is_null_oid(two) && !*right)) | |
409 | message = "(commits not present)"; | |
410 | ||
411 | *merge_bases = get_merge_bases(*left, *right); | |
412 | if (*merge_bases) { | |
413 | if ((*merge_bases)->item == *left) | |
414 | fast_forward = 1; | |
415 | else if ((*merge_bases)->item == *right) | |
416 | fast_backward = 1; | |
417 | } | |
418 | ||
602a283a | 419 | if (!oidcmp(one, two)) { |
c7e1a736 JL |
420 | strbuf_release(&sb); |
421 | return; | |
422 | } | |
423 | ||
8e6df650 | 424 | output_header: |
a94bb683 | 425 | strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path); |
69e65449 | 426 | strbuf_add_unique_abbrev(&sb, one->hash, DEFAULT_ABBREV); |
a94bb683 | 427 | strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "..."); |
f0798e6c | 428 | strbuf_add_unique_abbrev(&sb, two->hash, DEFAULT_ABBREV); |
752c0c24 | 429 | if (message) |
4e215131 | 430 | strbuf_addf(&sb, " %s%s\n", message, reset); |
752c0c24 | 431 | else |
4e215131 | 432 | strbuf_addf(&sb, "%s:%s\n", fast_backward ? " (rewind)" : "", reset); |
752c0c24 JS |
433 | fwrite(sb.buf, sb.len, 1, f); |
434 | ||
752c0c24 JS |
435 | strbuf_release(&sb); |
436 | } | |
ee6fc514 | 437 | |
8e6df650 JK |
438 | void show_submodule_summary(FILE *f, const char *path, |
439 | const char *line_prefix, | |
440 | struct object_id *one, struct object_id *two, | |
441 | unsigned dirty_submodule, const char *meta, | |
442 | const char *del, const char *add, const char *reset) | |
443 | { | |
444 | struct rev_info rev; | |
445 | struct commit *left = NULL, *right = NULL; | |
446 | struct commit_list *merge_bases = NULL; | |
447 | ||
448 | show_submodule_header(f, path, line_prefix, one, two, dirty_submodule, | |
449 | meta, reset, &left, &right, &merge_bases); | |
450 | ||
451 | /* | |
452 | * If we don't have both a left and a right pointer, there is no | |
453 | * reason to try and display a summary. The header line should contain | |
454 | * all the information the user needs. | |
455 | */ | |
456 | if (!left || !right) | |
457 | goto out; | |
458 | ||
459 | /* Treat revision walker failure the same as missing commits */ | |
460 | if (prepare_submodule_summary(&rev, path, left, right, merge_bases)) { | |
461 | fprintf(f, "%s(revision walker failed)\n", line_prefix); | |
462 | goto out; | |
463 | } | |
464 | ||
465 | print_submodule_summary(&rev, f, line_prefix, del, add, reset); | |
466 | ||
467 | out: | |
468 | if (merge_bases) | |
469 | free_commit_list(merge_bases); | |
470 | clear_commit_marks(left, ~0); | |
471 | clear_commit_marks(right, ~0); | |
472 | } | |
473 | ||
fd47ae6a JK |
474 | void show_submodule_inline_diff(FILE *f, const char *path, |
475 | const char *line_prefix, | |
476 | struct object_id *one, struct object_id *two, | |
477 | unsigned dirty_submodule, const char *meta, | |
478 | const char *del, const char *add, const char *reset, | |
479 | const struct diff_options *o) | |
480 | { | |
481 | const struct object_id *old = &empty_tree_oid, *new = &empty_tree_oid; | |
482 | struct commit *left = NULL, *right = NULL; | |
483 | struct commit_list *merge_bases = NULL; | |
484 | struct strbuf submodule_dir = STRBUF_INIT; | |
485 | struct child_process cp = CHILD_PROCESS_INIT; | |
486 | ||
487 | show_submodule_header(f, path, line_prefix, one, two, dirty_submodule, | |
488 | meta, reset, &left, &right, &merge_bases); | |
489 | ||
490 | /* We need a valid left and right commit to display a difference */ | |
491 | if (!(left || is_null_oid(one)) || | |
492 | !(right || is_null_oid(two))) | |
493 | goto done; | |
494 | ||
495 | if (left) | |
496 | old = one; | |
497 | if (right) | |
498 | new = two; | |
499 | ||
500 | fflush(f); | |
501 | cp.git_cmd = 1; | |
502 | cp.dir = path; | |
503 | cp.out = dup(fileno(f)); | |
504 | cp.no_stdin = 1; | |
505 | ||
506 | /* TODO: other options may need to be passed here. */ | |
507 | argv_array_push(&cp.args, "diff"); | |
508 | argv_array_pushf(&cp.args, "--line-prefix=%s", line_prefix); | |
509 | if (DIFF_OPT_TST(o, REVERSE_DIFF)) { | |
510 | argv_array_pushf(&cp.args, "--src-prefix=%s%s/", | |
511 | o->b_prefix, path); | |
512 | argv_array_pushf(&cp.args, "--dst-prefix=%s%s/", | |
513 | o->a_prefix, path); | |
514 | } else { | |
515 | argv_array_pushf(&cp.args, "--src-prefix=%s%s/", | |
516 | o->a_prefix, path); | |
517 | argv_array_pushf(&cp.args, "--dst-prefix=%s%s/", | |
518 | o->b_prefix, path); | |
519 | } | |
520 | argv_array_push(&cp.args, oid_to_hex(old)); | |
521 | /* | |
522 | * If the submodule has modified content, we will diff against the | |
523 | * work tree, under the assumption that the user has asked for the | |
524 | * diff format and wishes to actually see all differences even if they | |
525 | * haven't yet been committed to the submodule yet. | |
526 | */ | |
527 | if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED)) | |
528 | argv_array_push(&cp.args, oid_to_hex(new)); | |
529 | ||
530 | if (run_command(&cp)) | |
531 | fprintf(f, "(diff failed)\n"); | |
532 | ||
533 | done: | |
534 | strbuf_release(&submodule_dir); | |
535 | if (merge_bases) | |
536 | free_commit_list(merge_bases); | |
537 | if (left) | |
538 | clear_commit_marks(left, ~0); | |
539 | if (right) | |
540 | clear_commit_marks(right, ~0); | |
541 | } | |
542 | ||
be254a0e JL |
543 | void set_config_fetch_recurse_submodules(int value) |
544 | { | |
545 | config_fetch_recurse_submodules = value; | |
546 | } | |
547 | ||
7290ef58 MH |
548 | static int has_remote(const char *refname, const struct object_id *oid, |
549 | int flags, void *cb_data) | |
d2b17b32 FG |
550 | { |
551 | return 1; | |
552 | } | |
553 | ||
9cfa1c26 | 554 | static int append_sha1_to_argv(const unsigned char sha1[20], void *data) |
d2b17b32 | 555 | { |
9cfa1c26 HV |
556 | struct argv_array *argv = data; |
557 | argv_array_push(argv, sha1_to_hex(sha1)); | |
558 | return 0; | |
559 | } | |
560 | ||
5b6607d2 | 561 | static int check_has_commit(const unsigned char sha1[20], void *data) |
d2b17b32 | 562 | { |
5b6607d2 HV |
563 | int *has_commit = data; |
564 | ||
565 | if (!lookup_commit_reference(sha1)) | |
566 | *has_commit = 0; | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
571 | static int submodule_has_commits(const char *path, struct sha1_array *commits) | |
572 | { | |
573 | int has_commit = 1; | |
574 | ||
575 | if (add_submodule_odb(path)) | |
576 | return 0; | |
577 | ||
578 | sha1_array_for_each_unique(commits, check_has_commit, &has_commit); | |
579 | return has_commit; | |
580 | } | |
581 | ||
582 | static int submodule_needs_pushing(const char *path, struct sha1_array *commits) | |
583 | { | |
584 | if (!submodule_has_commits(path, commits)) | |
250ab24a HV |
585 | /* |
586 | * NOTE: We do consider it safe to return "no" here. The | |
587 | * correct answer would be "We do not know" instead of | |
588 | * "No push needed", but it is quite hard to change | |
589 | * the submodule pointer without having the submodule | |
590 | * around. If a user did however change the submodules | |
591 | * without having the submodule around, this indicates | |
592 | * an expert who knows what they are doing or a | |
593 | * maintainer integrating work from other people. In | |
594 | * both cases it should be safe to skip this check. | |
595 | */ | |
d2b17b32 FG |
596 | return 0; |
597 | ||
598 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { | |
d3180279 | 599 | struct child_process cp = CHILD_PROCESS_INIT; |
d2b17b32 FG |
600 | struct strbuf buf = STRBUF_INIT; |
601 | int needs_pushing = 0; | |
602 | ||
5b6607d2 HV |
603 | argv_array_push(&cp.args, "rev-list"); |
604 | sha1_array_for_each_unique(commits, append_sha1_to_argv, &cp.args); | |
605 | argv_array_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL); | |
606 | ||
c12e8656 | 607 | prepare_submodule_repo_env(&cp.env_array); |
d2b17b32 FG |
608 | cp.git_cmd = 1; |
609 | cp.no_stdin = 1; | |
610 | cp.out = -1; | |
611 | cp.dir = path; | |
612 | if (start_command(&cp)) | |
5b6607d2 HV |
613 | die("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s", |
614 | path); | |
d2b17b32 FG |
615 | if (strbuf_read(&buf, cp.out, 41)) |
616 | needs_pushing = 1; | |
617 | finish_command(&cp); | |
618 | close(cp.out); | |
619 | strbuf_release(&buf); | |
620 | return needs_pushing; | |
621 | } | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
14739447 HV |
626 | static struct sha1_array *submodule_commits(struct string_list *submodules, |
627 | const char *path) | |
628 | { | |
629 | struct string_list_item *item; | |
630 | ||
631 | item = string_list_insert(submodules, path); | |
632 | if (item->util) | |
633 | return (struct sha1_array *) item->util; | |
634 | ||
635 | /* NEEDSWORK: should we have sha1_array_init()? */ | |
636 | item->util = xcalloc(1, sizeof(struct sha1_array)); | |
637 | return (struct sha1_array *) item->util; | |
638 | } | |
639 | ||
d2b17b32 FG |
640 | static void collect_submodules_from_diff(struct diff_queue_struct *q, |
641 | struct diff_options *options, | |
642 | void *data) | |
643 | { | |
644 | int i; | |
14739447 | 645 | struct string_list *submodules = data; |
d2b17b32 FG |
646 | |
647 | for (i = 0; i < q->nr; i++) { | |
648 | struct diff_filepair *p = q->queue[i]; | |
14739447 | 649 | struct sha1_array *commits; |
d2b17b32 FG |
650 | if (!S_ISGITLINK(p->two->mode)) |
651 | continue; | |
14739447 HV |
652 | commits = submodule_commits(submodules, p->two->path); |
653 | sha1_array_append(commits, p->two->oid.hash); | |
d2b17b32 FG |
654 | } |
655 | } | |
656 | ||
a762e51e HV |
657 | static void find_unpushed_submodule_commits(struct commit *commit, |
658 | struct string_list *needs_pushing) | |
d2b17b32 | 659 | { |
d2b17b32 FG |
660 | struct rev_info rev; |
661 | ||
d2b17b32 FG |
662 | init_revisions(&rev, NULL); |
663 | rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; | |
664 | rev.diffopt.format_callback = collect_submodules_from_diff; | |
665 | rev.diffopt.format_callback_data = needs_pushing; | |
78e98eaf | 666 | diff_tree_combined_merge(commit, 1, &rev); |
d2b17b32 FG |
667 | } |
668 | ||
14739447 HV |
669 | static void free_submodules_sha1s(struct string_list *submodules) |
670 | { | |
671 | struct string_list_item *item; | |
672 | for_each_string_list_item(item, submodules) | |
673 | sha1_array_clear((struct sha1_array *) item->util); | |
674 | string_list_clear(submodules, 1); | |
675 | } | |
676 | ||
9cfa1c26 | 677 | int find_unpushed_submodules(struct sha1_array *commits, |
a762e51e | 678 | const char *remotes_name, struct string_list *needs_pushing) |
d2b17b32 FG |
679 | { |
680 | struct rev_info rev; | |
681 | struct commit *commit; | |
14739447 HV |
682 | struct string_list submodules = STRING_LIST_INIT_DUP; |
683 | struct string_list_item *submodule; | |
9cfa1c26 | 684 | struct argv_array argv = ARGV_ARRAY_INIT; |
a762e51e | 685 | |
d2b17b32 | 686 | init_revisions(&rev, NULL); |
9cfa1c26 HV |
687 | |
688 | /* argv.argv[0] will be ignored by setup_revisions */ | |
689 | argv_array_push(&argv, "find_unpushed_submodules"); | |
690 | sha1_array_for_each_unique(commits, append_sha1_to_argv, &argv); | |
691 | argv_array_push(&argv, "--not"); | |
692 | argv_array_pushf(&argv, "--remotes=%s", remotes_name); | |
693 | ||
694 | setup_revisions(argv.argc, argv.argv, &rev, NULL); | |
d2b17b32 FG |
695 | if (prepare_revision_walk(&rev)) |
696 | die("revision walk setup failed"); | |
697 | ||
a762e51e | 698 | while ((commit = get_revision(&rev)) != NULL) |
14739447 | 699 | find_unpushed_submodule_commits(commit, &submodules); |
d2b17b32 | 700 | |
bcc0a3ea | 701 | reset_revision_walk(); |
9cfa1c26 | 702 | argv_array_clear(&argv); |
d2b17b32 | 703 | |
14739447 | 704 | for_each_string_list_item(submodule, &submodules) { |
5b6607d2 HV |
705 | struct sha1_array *commits = (struct sha1_array *) submodule->util; |
706 | ||
707 | if (submodule_needs_pushing(submodule->string, commits)) | |
708 | string_list_insert(needs_pushing, submodule->string); | |
14739447 HV |
709 | } |
710 | free_submodules_sha1s(&submodules); | |
d2b17b32 | 711 | |
a762e51e | 712 | return needs_pushing->nr; |
d2b17b32 FG |
713 | } |
714 | ||
0301c821 | 715 | static int push_submodule(const char *path, int dry_run) |
eb21c732 HV |
716 | { |
717 | if (add_submodule_odb(path)) | |
718 | return 1; | |
719 | ||
720 | if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { | |
d3180279 | 721 | struct child_process cp = CHILD_PROCESS_INIT; |
0301c821 BW |
722 | argv_array_push(&cp.args, "push"); |
723 | if (dry_run) | |
724 | argv_array_push(&cp.args, "--dry-run"); | |
eb21c732 | 725 | |
c12e8656 | 726 | prepare_submodule_repo_env(&cp.env_array); |
eb21c732 HV |
727 | cp.git_cmd = 1; |
728 | cp.no_stdin = 1; | |
729 | cp.dir = path; | |
730 | if (run_command(&cp)) | |
731 | return 0; | |
732 | close(cp.out); | |
733 | } | |
734 | ||
735 | return 1; | |
736 | } | |
737 | ||
0301c821 BW |
738 | int push_unpushed_submodules(struct sha1_array *commits, |
739 | const char *remotes_name, | |
740 | int dry_run) | |
eb21c732 HV |
741 | { |
742 | int i, ret = 1; | |
f93d7c6f | 743 | struct string_list needs_pushing = STRING_LIST_INIT_DUP; |
eb21c732 | 744 | |
9cfa1c26 | 745 | if (!find_unpushed_submodules(commits, remotes_name, &needs_pushing)) |
eb21c732 HV |
746 | return 1; |
747 | ||
748 | for (i = 0; i < needs_pushing.nr; i++) { | |
749 | const char *path = needs_pushing.items[i].string; | |
750 | fprintf(stderr, "Pushing submodule '%s'\n", path); | |
0301c821 | 751 | if (!push_submodule(path, dry_run)) { |
eb21c732 HV |
752 | fprintf(stderr, "Unable to push submodule '%s'\n", path); |
753 | ret = 0; | |
754 | } | |
755 | } | |
756 | ||
757 | string_list_clear(&needs_pushing, 0); | |
758 | ||
759 | return ret; | |
760 | } | |
761 | ||
c16c3e40 JL |
762 | static int is_submodule_commit_present(const char *path, unsigned char sha1[20]) |
763 | { | |
764 | int is_present = 0; | |
765 | if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) { | |
766 | /* Even if the submodule is checked out and the commit is | |
767 | * present, make sure it is reachable from a ref. */ | |
d3180279 | 768 | struct child_process cp = CHILD_PROCESS_INIT; |
c16c3e40 JL |
769 | const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL}; |
770 | struct strbuf buf = STRBUF_INIT; | |
771 | ||
772 | argv[3] = sha1_to_hex(sha1); | |
c16c3e40 | 773 | cp.argv = argv; |
c12e8656 | 774 | prepare_submodule_repo_env(&cp.env_array); |
c16c3e40 JL |
775 | cp.git_cmd = 1; |
776 | cp.no_stdin = 1; | |
c16c3e40 | 777 | cp.dir = path; |
1d4974c9 | 778 | if (!capture_command(&cp, &buf, 1024) && !buf.len) |
c16c3e40 JL |
779 | is_present = 1; |
780 | ||
c16c3e40 JL |
781 | strbuf_release(&buf); |
782 | } | |
783 | return is_present; | |
784 | } | |
785 | ||
88a21979 JL |
786 | static void submodule_collect_changed_cb(struct diff_queue_struct *q, |
787 | struct diff_options *options, | |
788 | void *data) | |
789 | { | |
790 | int i; | |
791 | for (i = 0; i < q->nr; i++) { | |
792 | struct diff_filepair *p = q->queue[i]; | |
793 | if (!S_ISGITLINK(p->two->mode)) | |
794 | continue; | |
795 | ||
796 | if (S_ISGITLINK(p->one->mode)) { | |
797 | /* NEEDSWORK: We should honor the name configured in | |
798 | * the .gitmodules file of the commit we are examining | |
799 | * here to be able to correctly follow submodules | |
800 | * being moved around. */ | |
801 | struct string_list_item *path; | |
802 | path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path); | |
a0d12c44 | 803 | if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash)) |
88a21979 JL |
804 | string_list_append(&changed_submodule_paths, xstrdup(p->two->path)); |
805 | } else { | |
806 | /* Submodule is new or was moved here */ | |
807 | /* NEEDSWORK: When the .git directories of submodules | |
808 | * live inside the superprojects .git directory some | |
809 | * day we should fetch new submodules directly into | |
810 | * that location too when config or options request | |
811 | * that so they can be checked out from there. */ | |
812 | continue; | |
813 | } | |
814 | } | |
815 | } | |
816 | ||
7290ef58 | 817 | static int add_sha1_to_array(const char *ref, const struct object_id *oid, |
6859de45 JK |
818 | int flags, void *data) |
819 | { | |
7290ef58 | 820 | sha1_array_append(data, oid->hash); |
6859de45 JK |
821 | return 0; |
822 | } | |
823 | ||
88a21979 | 824 | void check_for_new_submodule_commits(unsigned char new_sha1[20]) |
6859de45 JK |
825 | { |
826 | if (!initialized_fetch_ref_tips) { | |
827 | for_each_ref(add_sha1_to_array, &ref_tips_before_fetch); | |
828 | initialized_fetch_ref_tips = 1; | |
829 | } | |
830 | ||
831 | sha1_array_append(&ref_tips_after_fetch, new_sha1); | |
832 | } | |
833 | ||
16ddcd40 | 834 | static int add_sha1_to_argv(const unsigned char sha1[20], void *data) |
6859de45 | 835 | { |
c1189cae | 836 | argv_array_push(data, sha1_to_hex(sha1)); |
16ddcd40 | 837 | return 0; |
6859de45 JK |
838 | } |
839 | ||
840 | static void calculate_changed_submodule_paths(void) | |
88a21979 JL |
841 | { |
842 | struct rev_info rev; | |
843 | struct commit *commit; | |
c1189cae | 844 | struct argv_array argv = ARGV_ARRAY_INIT; |
88a21979 | 845 | |
18322bad | 846 | /* No need to check if there are no submodules configured */ |
851e18c3 | 847 | if (!submodule_from_path(NULL, NULL)) |
18322bad JL |
848 | return; |
849 | ||
88a21979 | 850 | init_revisions(&rev, NULL); |
c1189cae | 851 | argv_array_push(&argv, "--"); /* argv[0] program name */ |
6859de45 JK |
852 | sha1_array_for_each_unique(&ref_tips_after_fetch, |
853 | add_sha1_to_argv, &argv); | |
c1189cae | 854 | argv_array_push(&argv, "--not"); |
6859de45 JK |
855 | sha1_array_for_each_unique(&ref_tips_before_fetch, |
856 | add_sha1_to_argv, &argv); | |
857 | setup_revisions(argv.argc, argv.argv, &rev, NULL); | |
88a21979 JL |
858 | if (prepare_revision_walk(&rev)) |
859 | die("revision walk setup failed"); | |
860 | ||
861 | /* | |
862 | * Collect all submodules (whether checked out or not) for which new | |
863 | * commits have been recorded upstream in "changed_submodule_paths". | |
864 | */ | |
865 | while ((commit = get_revision(&rev))) { | |
866 | struct commit_list *parent = commit->parents; | |
867 | while (parent) { | |
868 | struct diff_options diff_opts; | |
869 | diff_setup(&diff_opts); | |
ea2d325b | 870 | DIFF_OPT_SET(&diff_opts, RECURSIVE); |
88a21979 JL |
871 | diff_opts.output_format |= DIFF_FORMAT_CALLBACK; |
872 | diff_opts.format_callback = submodule_collect_changed_cb; | |
28452655 | 873 | diff_setup_done(&diff_opts); |
ed1c9977 | 874 | diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts); |
88a21979 JL |
875 | diffcore_std(&diff_opts); |
876 | diff_flush(&diff_opts); | |
877 | parent = parent->next; | |
878 | } | |
879 | } | |
6859de45 | 880 | |
c1189cae | 881 | argv_array_clear(&argv); |
6859de45 JK |
882 | sha1_array_clear(&ref_tips_before_fetch); |
883 | sha1_array_clear(&ref_tips_after_fetch); | |
884 | initialized_fetch_ref_tips = 0; | |
88a21979 JL |
885 | } |
886 | ||
fe85ee6e SB |
887 | struct submodule_parallel_fetch { |
888 | int count; | |
889 | struct argv_array args; | |
890 | const char *work_tree; | |
891 | const char *prefix; | |
892 | int command_line_option; | |
893 | int quiet; | |
894 | int result; | |
895 | }; | |
896 | #define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0} | |
897 | ||
898 | static int get_next_submodule(struct child_process *cp, | |
899 | struct strbuf *err, void *data, void **task_cb) | |
7dce19d3 | 900 | { |
fe85ee6e SB |
901 | int ret = 0; |
902 | struct submodule_parallel_fetch *spf = data; | |
6859de45 | 903 | |
fe85ee6e | 904 | for (; spf->count < active_nr; spf->count++) { |
7dce19d3 JL |
905 | struct strbuf submodule_path = STRBUF_INIT; |
906 | struct strbuf submodule_git_dir = STRBUF_INIT; | |
907 | struct strbuf submodule_prefix = STRBUF_INIT; | |
fe85ee6e | 908 | const struct cache_entry *ce = active_cache[spf->count]; |
851e18c3 HV |
909 | const char *git_dir, *default_argv; |
910 | const struct submodule *submodule; | |
7dce19d3 JL |
911 | |
912 | if (!S_ISGITLINK(ce->ce_mode)) | |
913 | continue; | |
914 | ||
851e18c3 HV |
915 | submodule = submodule_from_path(null_sha1, ce->name); |
916 | if (!submodule) | |
917 | submodule = submodule_from_name(null_sha1, ce->name); | |
7dce19d3 | 918 | |
88a21979 | 919 | default_argv = "yes"; |
fe85ee6e | 920 | if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) { |
851e18c3 HV |
921 | if (submodule && |
922 | submodule->fetch_recurse != | |
923 | RECURSE_SUBMODULES_NONE) { | |
924 | if (submodule->fetch_recurse == | |
925 | RECURSE_SUBMODULES_OFF) | |
c1a3c364 | 926 | continue; |
851e18c3 HV |
927 | if (submodule->fetch_recurse == |
928 | RECURSE_SUBMODULES_ON_DEMAND) { | |
bf42b384 JL |
929 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
930 | continue; | |
931 | default_argv = "on-demand"; | |
932 | } | |
c1a3c364 | 933 | } else { |
d4e98b58 JL |
934 | if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) || |
935 | gitmodules_is_unmerged) | |
c1a3c364 | 936 | continue; |
88a21979 JL |
937 | if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) { |
938 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) | |
939 | continue; | |
940 | default_argv = "on-demand"; | |
941 | } | |
c1a3c364 | 942 | } |
fe85ee6e | 943 | } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) { |
8f0700dd JL |
944 | if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name)) |
945 | continue; | |
946 | default_argv = "on-demand"; | |
be254a0e JL |
947 | } |
948 | ||
fe85ee6e | 949 | strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name); |
7dce19d3 | 950 | strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf); |
fe85ee6e | 951 | strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name); |
13d6ec91 | 952 | git_dir = read_gitfile(submodule_git_dir.buf); |
7dce19d3 JL |
953 | if (!git_dir) |
954 | git_dir = submodule_git_dir.buf; | |
955 | if (is_directory(git_dir)) { | |
fe85ee6e SB |
956 | child_process_init(cp); |
957 | cp->dir = strbuf_detach(&submodule_path, NULL); | |
c12e8656 | 958 | prepare_submodule_repo_env(&cp->env_array); |
fe85ee6e SB |
959 | cp->git_cmd = 1; |
960 | if (!spf->quiet) | |
961 | strbuf_addf(err, "Fetching submodule %s%s\n", | |
962 | spf->prefix, ce->name); | |
963 | argv_array_init(&cp->args); | |
964 | argv_array_pushv(&cp->args, spf->args.argv); | |
965 | argv_array_push(&cp->args, default_argv); | |
966 | argv_array_push(&cp->args, "--submodule-prefix"); | |
967 | argv_array_push(&cp->args, submodule_prefix.buf); | |
968 | ret = 1; | |
7dce19d3 JL |
969 | } |
970 | strbuf_release(&submodule_path); | |
971 | strbuf_release(&submodule_git_dir); | |
972 | strbuf_release(&submodule_prefix); | |
fe85ee6e SB |
973 | if (ret) { |
974 | spf->count++; | |
975 | return 1; | |
976 | } | |
7dce19d3 | 977 | } |
fe85ee6e SB |
978 | return 0; |
979 | } | |
980 | ||
2a73b3da | 981 | static int fetch_start_failure(struct strbuf *err, |
fe85ee6e SB |
982 | void *cb, void *task_cb) |
983 | { | |
984 | struct submodule_parallel_fetch *spf = cb; | |
985 | ||
986 | spf->result = 1; | |
987 | ||
988 | return 0; | |
989 | } | |
990 | ||
2a73b3da SB |
991 | static int fetch_finish(int retvalue, struct strbuf *err, |
992 | void *cb, void *task_cb) | |
fe85ee6e SB |
993 | { |
994 | struct submodule_parallel_fetch *spf = cb; | |
995 | ||
996 | if (retvalue) | |
997 | spf->result = 1; | |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | ||
1002 | int fetch_populated_submodules(const struct argv_array *options, | |
1003 | const char *prefix, int command_line_option, | |
62104ba1 | 1004 | int quiet, int max_parallel_jobs) |
fe85ee6e SB |
1005 | { |
1006 | int i; | |
fe85ee6e SB |
1007 | struct submodule_parallel_fetch spf = SPF_INIT; |
1008 | ||
1009 | spf.work_tree = get_git_work_tree(); | |
1010 | spf.command_line_option = command_line_option; | |
1011 | spf.quiet = quiet; | |
1012 | spf.prefix = prefix; | |
1013 | ||
1014 | if (!spf.work_tree) | |
1015 | goto out; | |
1016 | ||
1017 | if (read_cache() < 0) | |
1018 | die("index file corrupt"); | |
1019 | ||
1020 | argv_array_push(&spf.args, "fetch"); | |
1021 | for (i = 0; i < options->argc; i++) | |
1022 | argv_array_push(&spf.args, options->argv[i]); | |
1023 | argv_array_push(&spf.args, "--recurse-submodules-default"); | |
1024 | /* default value, "--submodule-prefix" and its value are added later */ | |
1025 | ||
a028a193 SB |
1026 | if (max_parallel_jobs < 0) |
1027 | max_parallel_jobs = parallel_jobs; | |
1028 | ||
fe85ee6e SB |
1029 | calculate_changed_submodule_paths(); |
1030 | run_processes_parallel(max_parallel_jobs, | |
1031 | get_next_submodule, | |
1032 | fetch_start_failure, | |
1033 | fetch_finish, | |
1034 | &spf); | |
1035 | ||
1036 | argv_array_clear(&spf.args); | |
88a21979 JL |
1037 | out: |
1038 | string_list_clear(&changed_submodule_paths, 1); | |
fe85ee6e | 1039 | return spf.result; |
7dce19d3 JL |
1040 | } |
1041 | ||
3bfc4504 | 1042 | unsigned is_submodule_modified(const char *path, int ignore_untracked) |
ee6fc514 | 1043 | { |
c7e1a736 | 1044 | ssize_t len; |
d3180279 | 1045 | struct child_process cp = CHILD_PROCESS_INIT; |
ee6fc514 JL |
1046 | const char *argv[] = { |
1047 | "status", | |
1048 | "--porcelain", | |
1049 | NULL, | |
3bfc4504 | 1050 | NULL, |
ee6fc514 | 1051 | }; |
ee6fc514 | 1052 | struct strbuf buf = STRBUF_INIT; |
c7e1a736 JL |
1053 | unsigned dirty_submodule = 0; |
1054 | const char *line, *next_line; | |
eee49b6c | 1055 | const char *git_dir; |
ee6fc514 | 1056 | |
eee49b6c | 1057 | strbuf_addf(&buf, "%s/.git", path); |
13d6ec91 | 1058 | git_dir = read_gitfile(buf.buf); |
eee49b6c JL |
1059 | if (!git_dir) |
1060 | git_dir = buf.buf; | |
1061 | if (!is_directory(git_dir)) { | |
ee6fc514 JL |
1062 | strbuf_release(&buf); |
1063 | /* The submodule is not checked out, so it is not modified */ | |
1064 | return 0; | |
1065 | ||
1066 | } | |
1067 | strbuf_reset(&buf); | |
1068 | ||
3bfc4504 JL |
1069 | if (ignore_untracked) |
1070 | argv[2] = "-uno"; | |
1071 | ||
ee6fc514 | 1072 | cp.argv = argv; |
c12e8656 | 1073 | prepare_submodule_repo_env(&cp.env_array); |
ee6fc514 JL |
1074 | cp.git_cmd = 1; |
1075 | cp.no_stdin = 1; | |
1076 | cp.out = -1; | |
eee49b6c | 1077 | cp.dir = path; |
ee6fc514 | 1078 | if (start_command(&cp)) |
6a5cedac | 1079 | die("Could not run 'git status --porcelain' in submodule %s", path); |
ee6fc514 JL |
1080 | |
1081 | len = strbuf_read(&buf, cp.out, 1024); | |
c7e1a736 JL |
1082 | line = buf.buf; |
1083 | while (len > 2) { | |
1084 | if ((line[0] == '?') && (line[1] == '?')) { | |
1085 | dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; | |
1086 | if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) | |
1087 | break; | |
1088 | } else { | |
1089 | dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; | |
3bfc4504 JL |
1090 | if (ignore_untracked || |
1091 | (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)) | |
c7e1a736 JL |
1092 | break; |
1093 | } | |
1094 | next_line = strchr(line, '\n'); | |
1095 | if (!next_line) | |
1096 | break; | |
1097 | next_line++; | |
1098 | len -= (next_line - line); | |
1099 | line = next_line; | |
1100 | } | |
ee6fc514 JL |
1101 | close(cp.out); |
1102 | ||
1103 | if (finish_command(&cp)) | |
6a5cedac | 1104 | die("'git status --porcelain' failed in submodule %s", path); |
ee6fc514 | 1105 | |
ee6fc514 | 1106 | strbuf_release(&buf); |
c7e1a736 | 1107 | return dirty_submodule; |
ee6fc514 | 1108 | } |
68d03e4a | 1109 | |
293ab15e JL |
1110 | int submodule_uses_gitfile(const char *path) |
1111 | { | |
d3180279 | 1112 | struct child_process cp = CHILD_PROCESS_INIT; |
293ab15e JL |
1113 | const char *argv[] = { |
1114 | "submodule", | |
1115 | "foreach", | |
1116 | "--quiet", | |
1117 | "--recursive", | |
1118 | "test -f .git", | |
1119 | NULL, | |
1120 | }; | |
1121 | struct strbuf buf = STRBUF_INIT; | |
1122 | const char *git_dir; | |
1123 | ||
1124 | strbuf_addf(&buf, "%s/.git", path); | |
1125 | git_dir = read_gitfile(buf.buf); | |
1126 | if (!git_dir) { | |
1127 | strbuf_release(&buf); | |
1128 | return 0; | |
1129 | } | |
1130 | strbuf_release(&buf); | |
1131 | ||
1132 | /* Now test that all nested submodules use a gitfile too */ | |
293ab15e | 1133 | cp.argv = argv; |
c12e8656 | 1134 | prepare_submodule_repo_env(&cp.env_array); |
293ab15e JL |
1135 | cp.git_cmd = 1; |
1136 | cp.no_stdin = 1; | |
1137 | cp.no_stderr = 1; | |
1138 | cp.no_stdout = 1; | |
1139 | cp.dir = path; | |
1140 | if (run_command(&cp)) | |
1141 | return 0; | |
1142 | ||
1143 | return 1; | |
1144 | } | |
1145 | ||
83b76966 SB |
1146 | /* |
1147 | * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data | |
1148 | * when doing so. | |
1149 | * | |
1150 | * Return 1 if we'd lose data, return 0 if the removal is fine, | |
1151 | * and negative values for errors. | |
1152 | */ | |
1153 | int bad_to_remove_submodule(const char *path, unsigned flags) | |
293ab15e | 1154 | { |
293ab15e | 1155 | ssize_t len; |
d3180279 | 1156 | struct child_process cp = CHILD_PROCESS_INIT; |
293ab15e | 1157 | struct strbuf buf = STRBUF_INIT; |
83b76966 | 1158 | int ret = 0; |
293ab15e | 1159 | |
dbe44faa | 1160 | if (!file_exists(path) || is_empty_dir(path)) |
83b76966 | 1161 | return 0; |
293ab15e JL |
1162 | |
1163 | if (!submodule_uses_gitfile(path)) | |
83b76966 | 1164 | return 1; |
293ab15e | 1165 | |
83b76966 | 1166 | argv_array_pushl(&cp.args, "status", "--porcelain", |
5a1c824f | 1167 | "--ignore-submodules=none", NULL); |
83b76966 SB |
1168 | |
1169 | if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED) | |
1170 | argv_array_push(&cp.args, "-uno"); | |
1171 | else | |
1172 | argv_array_push(&cp.args, "-uall"); | |
1173 | ||
1174 | if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)) | |
1175 | argv_array_push(&cp.args, "--ignored"); | |
293ab15e | 1176 | |
c12e8656 | 1177 | prepare_submodule_repo_env(&cp.env_array); |
293ab15e JL |
1178 | cp.git_cmd = 1; |
1179 | cp.no_stdin = 1; | |
1180 | cp.out = -1; | |
1181 | cp.dir = path; | |
83b76966 SB |
1182 | if (start_command(&cp)) { |
1183 | if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR) | |
1184 | die(_("could not start 'git status in submodule '%s'"), | |
1185 | path); | |
1186 | ret = -1; | |
1187 | goto out; | |
1188 | } | |
293ab15e JL |
1189 | |
1190 | len = strbuf_read(&buf, cp.out, 1024); | |
1191 | if (len > 2) | |
83b76966 | 1192 | ret = 1; |
293ab15e JL |
1193 | close(cp.out); |
1194 | ||
83b76966 SB |
1195 | if (finish_command(&cp)) { |
1196 | if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR) | |
1197 | die(_("could not run 'git status in submodule '%s'"), | |
1198 | path); | |
1199 | ret = -1; | |
1200 | } | |
1201 | out: | |
293ab15e | 1202 | strbuf_release(&buf); |
83b76966 | 1203 | return ret; |
293ab15e JL |
1204 | } |
1205 | ||
68d03e4a HV |
1206 | static int find_first_merges(struct object_array *result, const char *path, |
1207 | struct commit *a, struct commit *b) | |
1208 | { | |
1209 | int i, j; | |
3826902d | 1210 | struct object_array merges = OBJECT_ARRAY_INIT; |
68d03e4a HV |
1211 | struct commit *commit; |
1212 | int contains_another; | |
1213 | ||
1214 | char merged_revision[42]; | |
1215 | const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | |
1216 | "--all", merged_revision, NULL }; | |
1217 | struct rev_info revs; | |
1218 | struct setup_revision_opt rev_opts; | |
1219 | ||
68d03e4a HV |
1220 | memset(result, 0, sizeof(struct object_array)); |
1221 | memset(&rev_opts, 0, sizeof(rev_opts)); | |
1222 | ||
1223 | /* get all revisions that merge commit a */ | |
1224 | snprintf(merged_revision, sizeof(merged_revision), "^%s", | |
f2fd0760 | 1225 | oid_to_hex(&a->object.oid)); |
68d03e4a HV |
1226 | init_revisions(&revs, NULL); |
1227 | rev_opts.submodule = path; | |
94c0cc8f | 1228 | setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts); |
68d03e4a HV |
1229 | |
1230 | /* save all revisions from the above list that contain b */ | |
1231 | if (prepare_revision_walk(&revs)) | |
1232 | die("revision walk setup failed"); | |
1233 | while ((commit = get_revision(&revs)) != NULL) { | |
1234 | struct object *o = &(commit->object); | |
a20efee9 | 1235 | if (in_merge_bases(b, commit)) |
68d03e4a HV |
1236 | add_object_array(o, NULL, &merges); |
1237 | } | |
bcc0a3ea | 1238 | reset_revision_walk(); |
68d03e4a HV |
1239 | |
1240 | /* Now we've got all merges that contain a and b. Prune all | |
1241 | * merges that contain another found merge and save them in | |
1242 | * result. | |
1243 | */ | |
1244 | for (i = 0; i < merges.nr; i++) { | |
1245 | struct commit *m1 = (struct commit *) merges.objects[i].item; | |
1246 | ||
1247 | contains_another = 0; | |
1248 | for (j = 0; j < merges.nr; j++) { | |
1249 | struct commit *m2 = (struct commit *) merges.objects[j].item; | |
a20efee9 | 1250 | if (i != j && in_merge_bases(m2, m1)) { |
68d03e4a HV |
1251 | contains_another = 1; |
1252 | break; | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | if (!contains_another) | |
5de0c015 | 1257 | add_object_array(merges.objects[i].item, NULL, result); |
68d03e4a HV |
1258 | } |
1259 | ||
1260 | free(merges.objects); | |
1261 | return result->nr; | |
1262 | } | |
1263 | ||
1264 | static void print_commit(struct commit *commit) | |
1265 | { | |
1266 | struct strbuf sb = STRBUF_INIT; | |
1267 | struct pretty_print_context ctx = {0}; | |
a5481a6c | 1268 | ctx.date_mode.type = DATE_NORMAL; |
68d03e4a HV |
1269 | format_commit_message(commit, " %h: %m %s", &sb, &ctx); |
1270 | fprintf(stderr, "%s\n", sb.buf); | |
1271 | strbuf_release(&sb); | |
1272 | } | |
1273 | ||
1274 | #define MERGE_WARNING(path, msg) \ | |
1275 | warning("Failed to merge submodule %s (%s)", path, msg); | |
1276 | ||
1277 | int merge_submodule(unsigned char result[20], const char *path, | |
1278 | const unsigned char base[20], const unsigned char a[20], | |
80988783 | 1279 | const unsigned char b[20], int search) |
68d03e4a HV |
1280 | { |
1281 | struct commit *commit_base, *commit_a, *commit_b; | |
1282 | int parent_count; | |
1283 | struct object_array merges; | |
1284 | ||
1285 | int i; | |
1286 | ||
1287 | /* store a in result in case we fail */ | |
1288 | hashcpy(result, a); | |
1289 | ||
1290 | /* we can not handle deletion conflicts */ | |
1291 | if (is_null_sha1(base)) | |
1292 | return 0; | |
1293 | if (is_null_sha1(a)) | |
1294 | return 0; | |
1295 | if (is_null_sha1(b)) | |
1296 | return 0; | |
1297 | ||
1298 | if (add_submodule_odb(path)) { | |
1299 | MERGE_WARNING(path, "not checked out"); | |
1300 | return 0; | |
1301 | } | |
1302 | ||
1303 | if (!(commit_base = lookup_commit_reference(base)) || | |
1304 | !(commit_a = lookup_commit_reference(a)) || | |
1305 | !(commit_b = lookup_commit_reference(b))) { | |
1306 | MERGE_WARNING(path, "commits not present"); | |
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | /* check whether both changes are forward */ | |
a20efee9 JH |
1311 | if (!in_merge_bases(commit_base, commit_a) || |
1312 | !in_merge_bases(commit_base, commit_b)) { | |
68d03e4a HV |
1313 | MERGE_WARNING(path, "commits don't follow merge-base"); |
1314 | return 0; | |
1315 | } | |
1316 | ||
1317 | /* Case #1: a is contained in b or vice versa */ | |
a20efee9 | 1318 | if (in_merge_bases(commit_a, commit_b)) { |
68d03e4a HV |
1319 | hashcpy(result, b); |
1320 | return 1; | |
1321 | } | |
a20efee9 | 1322 | if (in_merge_bases(commit_b, commit_a)) { |
68d03e4a HV |
1323 | hashcpy(result, a); |
1324 | return 1; | |
1325 | } | |
1326 | ||
1327 | /* | |
1328 | * Case #2: There are one or more merges that contain a and b in | |
1329 | * the submodule. If there is only one, then present it as a | |
1330 | * suggestion to the user, but leave it marked unmerged so the | |
1331 | * user needs to confirm the resolution. | |
1332 | */ | |
1333 | ||
80988783 BK |
1334 | /* Skip the search if makes no sense to the calling context. */ |
1335 | if (!search) | |
1336 | return 0; | |
1337 | ||
68d03e4a HV |
1338 | /* find commit which merges them */ |
1339 | parent_count = find_first_merges(&merges, path, commit_a, commit_b); | |
1340 | switch (parent_count) { | |
1341 | case 0: | |
1342 | MERGE_WARNING(path, "merge following commits not found"); | |
1343 | break; | |
1344 | ||
1345 | case 1: | |
1346 | MERGE_WARNING(path, "not fast-forward"); | |
1347 | fprintf(stderr, "Found a possible merge resolution " | |
1348 | "for the submodule:\n"); | |
1349 | print_commit((struct commit *) merges.objects[0].item); | |
1350 | fprintf(stderr, | |
1351 | "If this is correct simply add it to the index " | |
1352 | "for example\n" | |
1353 | "by using:\n\n" | |
1354 | " git update-index --cacheinfo 160000 %s \"%s\"\n\n" | |
1355 | "which will accept this suggestion.\n", | |
f2fd0760 | 1356 | oid_to_hex(&merges.objects[0].item->oid), path); |
68d03e4a HV |
1357 | break; |
1358 | ||
1359 | default: | |
1360 | MERGE_WARNING(path, "multiple merges found"); | |
1361 | for (i = 0; i < merges.nr; i++) | |
1362 | print_commit((struct commit *) merges.objects[i].item); | |
1363 | } | |
1364 | ||
1365 | free(merges.objects); | |
1366 | return 0; | |
1367 | } | |
a88c915d | 1368 | |
a028a193 SB |
1369 | int parallel_submodules(void) |
1370 | { | |
1371 | return parallel_jobs; | |
1372 | } | |
e059388f | 1373 | |
4638728c JK |
1374 | void prepare_submodule_repo_env(struct argv_array *out) |
1375 | { | |
1376 | const char * const *var; | |
1377 | ||
1378 | for (var = local_repo_env; *var; var++) { | |
89044baa | 1379 | if (strcmp(*var, CONFIG_DATA_ENVIRONMENT)) |
4638728c | 1380 | argv_array_push(out, *var); |
4638728c | 1381 | } |
c5f3cba1 SB |
1382 | argv_array_pushf(out, "%s=%s", GIT_DIR_ENVIRONMENT, |
1383 | DEFAULT_GIT_DIR_ENVIRONMENT); | |
4638728c | 1384 | } |
f6f85861 SB |
1385 | |
1386 | /* | |
1387 | * Embeds a single submodules git directory into the superprojects git dir, | |
1388 | * non recursively. | |
1389 | */ | |
1390 | static void relocate_single_git_dir_into_superproject(const char *prefix, | |
1391 | const char *path) | |
1392 | { | |
1393 | char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL; | |
1394 | const char *new_git_dir; | |
1395 | const struct submodule *sub; | |
1396 | ||
1397 | if (submodule_uses_worktrees(path)) | |
1398 | die(_("relocate_gitdir for submodule '%s' with " | |
1399 | "more than one worktree not supported"), path); | |
1400 | ||
1401 | old_git_dir = xstrfmt("%s/.git", path); | |
1402 | if (read_gitfile(old_git_dir)) | |
1403 | /* If it is an actual gitfile, it doesn't need migration. */ | |
1404 | return; | |
1405 | ||
55d128ae | 1406 | real_old_git_dir = real_pathdup(old_git_dir); |
f6f85861 SB |
1407 | |
1408 | sub = submodule_from_path(null_sha1, path); | |
1409 | if (!sub) | |
1410 | die(_("could not lookup name for submodule '%s'"), path); | |
1411 | ||
1412 | new_git_dir = git_path("modules/%s", sub->name); | |
1413 | if (safe_create_leading_directories_const(new_git_dir) < 0) | |
1414 | die(_("could not create directory '%s'"), new_git_dir); | |
55d128ae | 1415 | real_new_git_dir = real_pathdup(new_git_dir); |
f6f85861 SB |
1416 | |
1417 | if (!prefix) | |
1418 | prefix = get_super_prefix(); | |
1419 | ||
1420 | fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"), | |
1421 | prefix ? prefix : "", path, | |
1422 | real_old_git_dir, real_new_git_dir); | |
1423 | ||
1424 | relocate_gitdir(path, real_old_git_dir, real_new_git_dir); | |
1425 | ||
1426 | free(old_git_dir); | |
1427 | free(real_old_git_dir); | |
1428 | free(real_new_git_dir); | |
1429 | } | |
1430 | ||
1431 | /* | |
1432 | * Migrate the git directory of the submodule given by path from | |
1433 | * having its git directory within the working tree to the git dir nested | |
1434 | * in its superprojects git dir under modules/. | |
1435 | */ | |
1436 | void absorb_git_dir_into_superproject(const char *prefix, | |
1437 | const char *path, | |
1438 | unsigned flags) | |
1439 | { | |
ec9629b3 SB |
1440 | int err_code; |
1441 | const char *sub_git_dir; | |
f6f85861 | 1442 | struct strbuf gitdir = STRBUF_INIT; |
f6f85861 | 1443 | strbuf_addf(&gitdir, "%s/.git", path); |
ec9629b3 | 1444 | sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code); |
f6f85861 SB |
1445 | |
1446 | /* Not populated? */ | |
ec9629b3 SB |
1447 | if (!sub_git_dir) { |
1448 | char *real_new_git_dir; | |
1449 | const char *new_git_dir; | |
1450 | const struct submodule *sub; | |
1451 | ||
1452 | if (err_code == READ_GITFILE_ERR_STAT_FAILED) { | |
1453 | /* unpopulated as expected */ | |
1454 | strbuf_release(&gitdir); | |
1455 | return; | |
1456 | } | |
1457 | ||
1458 | if (err_code != READ_GITFILE_ERR_NOT_A_REPO) | |
1459 | /* We don't know what broke here. */ | |
1460 | read_gitfile_error_die(err_code, path, NULL); | |
1461 | ||
1462 | /* | |
1463 | * Maybe populated, but no git directory was found? | |
1464 | * This can happen if the superproject is a submodule | |
1465 | * itself and was just absorbed. The absorption of the | |
1466 | * superproject did not rewrite the git file links yet, | |
1467 | * fix it now. | |
1468 | */ | |
1469 | sub = submodule_from_path(null_sha1, path); | |
1470 | if (!sub) | |
1471 | die(_("could not lookup name for submodule '%s'"), path); | |
1472 | new_git_dir = git_path("modules/%s", sub->name); | |
1473 | if (safe_create_leading_directories_const(new_git_dir) < 0) | |
1474 | die(_("could not create directory '%s'"), new_git_dir); | |
1475 | real_new_git_dir = real_pathdup(new_git_dir); | |
1476 | connect_work_tree_and_git_dir(path, real_new_git_dir); | |
1477 | ||
1478 | free(real_new_git_dir); | |
1479 | } else { | |
1480 | /* Is it already absorbed into the superprojects git dir? */ | |
1481 | char *real_sub_git_dir = real_pathdup(sub_git_dir); | |
1482 | char *real_common_git_dir = real_pathdup(get_git_common_dir()); | |
f6f85861 | 1483 | |
ec9629b3 SB |
1484 | if (!starts_with(real_sub_git_dir, real_common_git_dir)) |
1485 | relocate_single_git_dir_into_superproject(prefix, path); | |
1486 | ||
1487 | free(real_sub_git_dir); | |
1488 | free(real_common_git_dir); | |
1489 | } | |
1490 | strbuf_release(&gitdir); | |
f6f85861 SB |
1491 | |
1492 | if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) { | |
1493 | struct child_process cp = CHILD_PROCESS_INIT; | |
1494 | struct strbuf sb = STRBUF_INIT; | |
1495 | ||
1496 | if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES) | |
1497 | die("BUG: we don't know how to pass the flags down?"); | |
1498 | ||
1499 | if (get_super_prefix()) | |
1500 | strbuf_addstr(&sb, get_super_prefix()); | |
1501 | strbuf_addstr(&sb, path); | |
1502 | strbuf_addch(&sb, '/'); | |
1503 | ||
1504 | cp.dir = path; | |
1505 | cp.git_cmd = 1; | |
1506 | cp.no_stdin = 1; | |
1507 | argv_array_pushl(&cp.args, "--super-prefix", sb.buf, | |
1508 | "submodule--helper", | |
1509 | "absorb-git-dirs", NULL); | |
1510 | prepare_submodule_repo_env(&cp.env_array); | |
1511 | if (run_command(&cp)) | |
1512 | die(_("could not recurse into submodule '%s'"), path); | |
1513 | ||
1514 | strbuf_release(&sb); | |
1515 | } | |
f6f85861 | 1516 | } |