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