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