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