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