]> git.ipfire.org Git - thirdparty/git.git/blame - submodule.c
serialize collection of changed submodules
[thirdparty/git.git] / submodule.c
CommitLineData
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 18static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
a028a193 19static int parallel_jobs = 1;
2721ce21 20static struct string_list changed_submodule_paths = STRING_LIST_INIT_NODUP;
6859de45
JK
21static int initialized_fetch_ref_tips;
22static struct sha1_array ref_tips_before_fetch;
23static 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 */
33static 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 */
43static int gitmodules_is_modified;
44
5fee9952
JL
45int 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 */
55int 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 */
89int 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(&sect, "submodule.");
851e18c3 106 strbuf_addstr(&sect, 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(&sect);
111 return -1;
112 }
113 strbuf_release(&sect);
114 return 0;
115}
116
5fee9952
JL
117void 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 123static 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
157done:
158 strbuf_release(&objects_directory);
159 return ret;
752c0c24
JS
160}
161
aee9c7d6
JL
162void 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 174int 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
190void 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
222int 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
243const 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
264void 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
281static 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
302static 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 */
336static 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 394output_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
409void 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
438out:
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
445void 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
504done:
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
514void set_config_fetch_recurse_submodules(int value)
515{
516 config_fetch_recurse_submodules = value;
517}
518
7290ef58
MH
519static 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
525static int submodule_needs_pushing(const char *path, const unsigned char sha1[20])
526{
527 if (add_submodule_odb(path) || !lookup_commit_reference(sha1))
528 return 0;
529
530 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 531 struct child_process cp = CHILD_PROCESS_INIT;
d2b17b32
FG
532 const char *argv[] = {"rev-list", NULL, "--not", "--remotes", "-n", "1" , NULL};
533 struct strbuf buf = STRBUF_INIT;
534 int needs_pushing = 0;
535
536 argv[1] = sha1_to_hex(sha1);
d2b17b32 537 cp.argv = argv;
c12e8656 538 prepare_submodule_repo_env(&cp.env_array);
d2b17b32
FG
539 cp.git_cmd = 1;
540 cp.no_stdin = 1;
541 cp.out = -1;
542 cp.dir = path;
543 if (start_command(&cp))
544 die("Could not run 'git rev-list %s --not --remotes -n 1' command in submodule %s",
545 sha1_to_hex(sha1), path);
546 if (strbuf_read(&buf, cp.out, 41))
547 needs_pushing = 1;
548 finish_command(&cp);
549 close(cp.out);
550 strbuf_release(&buf);
551 return needs_pushing;
552 }
553
554 return 0;
555}
556
14739447
HV
557static struct sha1_array *submodule_commits(struct string_list *submodules,
558 const char *path)
559{
560 struct string_list_item *item;
561
562 item = string_list_insert(submodules, path);
563 if (item->util)
564 return (struct sha1_array *) item->util;
565
566 /* NEEDSWORK: should we have sha1_array_init()? */
567 item->util = xcalloc(1, sizeof(struct sha1_array));
568 return (struct sha1_array *) item->util;
569}
570
d2b17b32
FG
571static void collect_submodules_from_diff(struct diff_queue_struct *q,
572 struct diff_options *options,
573 void *data)
574{
575 int i;
14739447 576 struct string_list *submodules = data;
d2b17b32
FG
577
578 for (i = 0; i < q->nr; i++) {
579 struct diff_filepair *p = q->queue[i];
14739447 580 struct sha1_array *commits;
d2b17b32
FG
581 if (!S_ISGITLINK(p->two->mode))
582 continue;
14739447
HV
583 commits = submodule_commits(submodules, p->two->path);
584 sha1_array_append(commits, p->two->oid.hash);
d2b17b32
FG
585 }
586}
587
a762e51e
HV
588static void find_unpushed_submodule_commits(struct commit *commit,
589 struct string_list *needs_pushing)
d2b17b32 590{
d2b17b32
FG
591 struct rev_info rev;
592
d2b17b32
FG
593 init_revisions(&rev, NULL);
594 rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
595 rev.diffopt.format_callback = collect_submodules_from_diff;
596 rev.diffopt.format_callback_data = needs_pushing;
78e98eaf 597 diff_tree_combined_merge(commit, 1, &rev);
d2b17b32
FG
598}
599
14739447
HV
600struct collect_submodule_from_sha1s_data {
601 char *submodule_path;
602 struct string_list *needs_pushing;
603};
604
605static int collect_submodules_from_sha1s(const unsigned char sha1[20],
606 void *data)
607{
608 struct collect_submodule_from_sha1s_data *me = data;
609
610 if (submodule_needs_pushing(me->submodule_path, sha1))
611 string_list_insert(me->needs_pushing, me->submodule_path);
612
613 return 0;
614}
615
616static void free_submodules_sha1s(struct string_list *submodules)
617{
618 struct string_list_item *item;
619 for_each_string_list_item(item, submodules)
620 sha1_array_clear((struct sha1_array *) item->util);
621 string_list_clear(submodules, 1);
622}
623
a762e51e
HV
624int find_unpushed_submodules(unsigned char new_sha1[20],
625 const char *remotes_name, struct string_list *needs_pushing)
d2b17b32
FG
626{
627 struct rev_info rev;
628 struct commit *commit;
629 const char *argv[] = {NULL, NULL, "--not", "NULL", NULL};
630 int argc = ARRAY_SIZE(argv) - 1;
631 char *sha1_copy;
14739447
HV
632 struct string_list submodules = STRING_LIST_INIT_DUP;
633 struct string_list_item *submodule;
a762e51e 634
d2b17b32
FG
635 struct strbuf remotes_arg = STRBUF_INIT;
636
637 strbuf_addf(&remotes_arg, "--remotes=%s", remotes_name);
638 init_revisions(&rev, NULL);
639 sha1_copy = xstrdup(sha1_to_hex(new_sha1));
640 argv[1] = sha1_copy;
641 argv[3] = remotes_arg.buf;
642 setup_revisions(argc, argv, &rev, NULL);
643 if (prepare_revision_walk(&rev))
644 die("revision walk setup failed");
645
a762e51e 646 while ((commit = get_revision(&rev)) != NULL)
14739447 647 find_unpushed_submodule_commits(commit, &submodules);
d2b17b32 648
bcc0a3ea 649 reset_revision_walk();
d2b17b32
FG
650 free(sha1_copy);
651 strbuf_release(&remotes_arg);
652
14739447
HV
653 for_each_string_list_item(submodule, &submodules) {
654 struct collect_submodule_from_sha1s_data data;
655 data.submodule_path = submodule->string;
656 data.needs_pushing = needs_pushing;
657 sha1_array_for_each_unique((struct sha1_array *) submodule->util,
658 collect_submodules_from_sha1s,
659 &data);
660 }
661 free_submodules_sha1s(&submodules);
662
a762e51e 663 return needs_pushing->nr;
d2b17b32
FG
664}
665
eb21c732
HV
666static int push_submodule(const char *path)
667{
668 if (add_submodule_odb(path))
669 return 1;
670
671 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 672 struct child_process cp = CHILD_PROCESS_INIT;
eb21c732
HV
673 const char *argv[] = {"push", NULL};
674
eb21c732 675 cp.argv = argv;
c12e8656 676 prepare_submodule_repo_env(&cp.env_array);
eb21c732
HV
677 cp.git_cmd = 1;
678 cp.no_stdin = 1;
679 cp.dir = path;
680 if (run_command(&cp))
681 return 0;
682 close(cp.out);
683 }
684
685 return 1;
686}
687
688int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name)
689{
690 int i, ret = 1;
f93d7c6f 691 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
eb21c732
HV
692
693 if (!find_unpushed_submodules(new_sha1, remotes_name, &needs_pushing))
694 return 1;
695
696 for (i = 0; i < needs_pushing.nr; i++) {
697 const char *path = needs_pushing.items[i].string;
698 fprintf(stderr, "Pushing submodule '%s'\n", path);
699 if (!push_submodule(path)) {
700 fprintf(stderr, "Unable to push submodule '%s'\n", path);
701 ret = 0;
702 }
703 }
704
705 string_list_clear(&needs_pushing, 0);
706
707 return ret;
708}
709
c16c3e40
JL
710static int is_submodule_commit_present(const char *path, unsigned char sha1[20])
711{
712 int is_present = 0;
713 if (!add_submodule_odb(path) && lookup_commit_reference(sha1)) {
714 /* Even if the submodule is checked out and the commit is
715 * present, make sure it is reachable from a ref. */
d3180279 716 struct child_process cp = CHILD_PROCESS_INIT;
c16c3e40
JL
717 const char *argv[] = {"rev-list", "-n", "1", NULL, "--not", "--all", NULL};
718 struct strbuf buf = STRBUF_INIT;
719
720 argv[3] = sha1_to_hex(sha1);
c16c3e40 721 cp.argv = argv;
c12e8656 722 prepare_submodule_repo_env(&cp.env_array);
c16c3e40
JL
723 cp.git_cmd = 1;
724 cp.no_stdin = 1;
c16c3e40 725 cp.dir = path;
1d4974c9 726 if (!capture_command(&cp, &buf, 1024) && !buf.len)
c16c3e40
JL
727 is_present = 1;
728
c16c3e40
JL
729 strbuf_release(&buf);
730 }
731 return is_present;
732}
733
88a21979
JL
734static void submodule_collect_changed_cb(struct diff_queue_struct *q,
735 struct diff_options *options,
736 void *data)
737{
738 int i;
739 for (i = 0; i < q->nr; i++) {
740 struct diff_filepair *p = q->queue[i];
741 if (!S_ISGITLINK(p->two->mode))
742 continue;
743
744 if (S_ISGITLINK(p->one->mode)) {
745 /* NEEDSWORK: We should honor the name configured in
746 * the .gitmodules file of the commit we are examining
747 * here to be able to correctly follow submodules
748 * being moved around. */
749 struct string_list_item *path;
750 path = unsorted_string_list_lookup(&changed_submodule_paths, p->two->path);
a0d12c44 751 if (!path && !is_submodule_commit_present(p->two->path, p->two->oid.hash))
88a21979
JL
752 string_list_append(&changed_submodule_paths, xstrdup(p->two->path));
753 } else {
754 /* Submodule is new or was moved here */
755 /* NEEDSWORK: When the .git directories of submodules
756 * live inside the superprojects .git directory some
757 * day we should fetch new submodules directly into
758 * that location too when config or options request
759 * that so they can be checked out from there. */
760 continue;
761 }
762 }
763}
764
7290ef58 765static int add_sha1_to_array(const char *ref, const struct object_id *oid,
6859de45
JK
766 int flags, void *data)
767{
7290ef58 768 sha1_array_append(data, oid->hash);
6859de45
JK
769 return 0;
770}
771
88a21979 772void check_for_new_submodule_commits(unsigned char new_sha1[20])
6859de45
JK
773{
774 if (!initialized_fetch_ref_tips) {
775 for_each_ref(add_sha1_to_array, &ref_tips_before_fetch);
776 initialized_fetch_ref_tips = 1;
777 }
778
779 sha1_array_append(&ref_tips_after_fetch, new_sha1);
780}
781
16ddcd40 782static int add_sha1_to_argv(const unsigned char sha1[20], void *data)
6859de45 783{
c1189cae 784 argv_array_push(data, sha1_to_hex(sha1));
16ddcd40 785 return 0;
6859de45
JK
786}
787
788static void calculate_changed_submodule_paths(void)
88a21979
JL
789{
790 struct rev_info rev;
791 struct commit *commit;
c1189cae 792 struct argv_array argv = ARGV_ARRAY_INIT;
88a21979 793
18322bad 794 /* No need to check if there are no submodules configured */
851e18c3 795 if (!submodule_from_path(NULL, NULL))
18322bad
JL
796 return;
797
88a21979 798 init_revisions(&rev, NULL);
c1189cae 799 argv_array_push(&argv, "--"); /* argv[0] program name */
6859de45
JK
800 sha1_array_for_each_unique(&ref_tips_after_fetch,
801 add_sha1_to_argv, &argv);
c1189cae 802 argv_array_push(&argv, "--not");
6859de45
JK
803 sha1_array_for_each_unique(&ref_tips_before_fetch,
804 add_sha1_to_argv, &argv);
805 setup_revisions(argv.argc, argv.argv, &rev, NULL);
88a21979
JL
806 if (prepare_revision_walk(&rev))
807 die("revision walk setup failed");
808
809 /*
810 * Collect all submodules (whether checked out or not) for which new
811 * commits have been recorded upstream in "changed_submodule_paths".
812 */
813 while ((commit = get_revision(&rev))) {
814 struct commit_list *parent = commit->parents;
815 while (parent) {
816 struct diff_options diff_opts;
817 diff_setup(&diff_opts);
ea2d325b 818 DIFF_OPT_SET(&diff_opts, RECURSIVE);
88a21979
JL
819 diff_opts.output_format |= DIFF_FORMAT_CALLBACK;
820 diff_opts.format_callback = submodule_collect_changed_cb;
28452655 821 diff_setup_done(&diff_opts);
ed1c9977 822 diff_tree_sha1(parent->item->object.oid.hash, commit->object.oid.hash, "", &diff_opts);
88a21979
JL
823 diffcore_std(&diff_opts);
824 diff_flush(&diff_opts);
825 parent = parent->next;
826 }
827 }
6859de45 828
c1189cae 829 argv_array_clear(&argv);
6859de45
JK
830 sha1_array_clear(&ref_tips_before_fetch);
831 sha1_array_clear(&ref_tips_after_fetch);
832 initialized_fetch_ref_tips = 0;
88a21979
JL
833}
834
fe85ee6e
SB
835struct submodule_parallel_fetch {
836 int count;
837 struct argv_array args;
838 const char *work_tree;
839 const char *prefix;
840 int command_line_option;
841 int quiet;
842 int result;
843};
844#define SPF_INIT {0, ARGV_ARRAY_INIT, NULL, NULL, 0, 0, 0}
845
846static int get_next_submodule(struct child_process *cp,
847 struct strbuf *err, void *data, void **task_cb)
7dce19d3 848{
fe85ee6e
SB
849 int ret = 0;
850 struct submodule_parallel_fetch *spf = data;
6859de45 851
fe85ee6e 852 for (; spf->count < active_nr; spf->count++) {
7dce19d3
JL
853 struct strbuf submodule_path = STRBUF_INIT;
854 struct strbuf submodule_git_dir = STRBUF_INIT;
855 struct strbuf submodule_prefix = STRBUF_INIT;
fe85ee6e 856 const struct cache_entry *ce = active_cache[spf->count];
851e18c3
HV
857 const char *git_dir, *default_argv;
858 const struct submodule *submodule;
7dce19d3
JL
859
860 if (!S_ISGITLINK(ce->ce_mode))
861 continue;
862
851e18c3
HV
863 submodule = submodule_from_path(null_sha1, ce->name);
864 if (!submodule)
865 submodule = submodule_from_name(null_sha1, ce->name);
7dce19d3 866
88a21979 867 default_argv = "yes";
fe85ee6e 868 if (spf->command_line_option == RECURSE_SUBMODULES_DEFAULT) {
851e18c3
HV
869 if (submodule &&
870 submodule->fetch_recurse !=
871 RECURSE_SUBMODULES_NONE) {
872 if (submodule->fetch_recurse ==
873 RECURSE_SUBMODULES_OFF)
c1a3c364 874 continue;
851e18c3
HV
875 if (submodule->fetch_recurse ==
876 RECURSE_SUBMODULES_ON_DEMAND) {
bf42b384
JL
877 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
878 continue;
879 default_argv = "on-demand";
880 }
c1a3c364 881 } else {
d4e98b58
JL
882 if ((config_fetch_recurse_submodules == RECURSE_SUBMODULES_OFF) ||
883 gitmodules_is_unmerged)
c1a3c364 884 continue;
88a21979
JL
885 if (config_fetch_recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND) {
886 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
887 continue;
888 default_argv = "on-demand";
889 }
c1a3c364 890 }
fe85ee6e 891 } else if (spf->command_line_option == RECURSE_SUBMODULES_ON_DEMAND) {
8f0700dd
JL
892 if (!unsorted_string_list_lookup(&changed_submodule_paths, ce->name))
893 continue;
894 default_argv = "on-demand";
be254a0e
JL
895 }
896
fe85ee6e 897 strbuf_addf(&submodule_path, "%s/%s", spf->work_tree, ce->name);
7dce19d3 898 strbuf_addf(&submodule_git_dir, "%s/.git", submodule_path.buf);
fe85ee6e 899 strbuf_addf(&submodule_prefix, "%s%s/", spf->prefix, ce->name);
13d6ec91 900 git_dir = read_gitfile(submodule_git_dir.buf);
7dce19d3
JL
901 if (!git_dir)
902 git_dir = submodule_git_dir.buf;
903 if (is_directory(git_dir)) {
fe85ee6e
SB
904 child_process_init(cp);
905 cp->dir = strbuf_detach(&submodule_path, NULL);
c12e8656 906 prepare_submodule_repo_env(&cp->env_array);
fe85ee6e
SB
907 cp->git_cmd = 1;
908 if (!spf->quiet)
909 strbuf_addf(err, "Fetching submodule %s%s\n",
910 spf->prefix, ce->name);
911 argv_array_init(&cp->args);
912 argv_array_pushv(&cp->args, spf->args.argv);
913 argv_array_push(&cp->args, default_argv);
914 argv_array_push(&cp->args, "--submodule-prefix");
915 argv_array_push(&cp->args, submodule_prefix.buf);
916 ret = 1;
7dce19d3
JL
917 }
918 strbuf_release(&submodule_path);
919 strbuf_release(&submodule_git_dir);
920 strbuf_release(&submodule_prefix);
fe85ee6e
SB
921 if (ret) {
922 spf->count++;
923 return 1;
924 }
7dce19d3 925 }
fe85ee6e
SB
926 return 0;
927}
928
2a73b3da 929static int fetch_start_failure(struct strbuf *err,
fe85ee6e
SB
930 void *cb, void *task_cb)
931{
932 struct submodule_parallel_fetch *spf = cb;
933
934 spf->result = 1;
935
936 return 0;
937}
938
2a73b3da
SB
939static int fetch_finish(int retvalue, struct strbuf *err,
940 void *cb, void *task_cb)
fe85ee6e
SB
941{
942 struct submodule_parallel_fetch *spf = cb;
943
944 if (retvalue)
945 spf->result = 1;
946
947 return 0;
948}
949
950int fetch_populated_submodules(const struct argv_array *options,
951 const char *prefix, int command_line_option,
62104ba1 952 int quiet, int max_parallel_jobs)
fe85ee6e
SB
953{
954 int i;
fe85ee6e
SB
955 struct submodule_parallel_fetch spf = SPF_INIT;
956
957 spf.work_tree = get_git_work_tree();
958 spf.command_line_option = command_line_option;
959 spf.quiet = quiet;
960 spf.prefix = prefix;
961
962 if (!spf.work_tree)
963 goto out;
964
965 if (read_cache() < 0)
966 die("index file corrupt");
967
968 argv_array_push(&spf.args, "fetch");
969 for (i = 0; i < options->argc; i++)
970 argv_array_push(&spf.args, options->argv[i]);
971 argv_array_push(&spf.args, "--recurse-submodules-default");
972 /* default value, "--submodule-prefix" and its value are added later */
973
a028a193
SB
974 if (max_parallel_jobs < 0)
975 max_parallel_jobs = parallel_jobs;
976
fe85ee6e
SB
977 calculate_changed_submodule_paths();
978 run_processes_parallel(max_parallel_jobs,
979 get_next_submodule,
980 fetch_start_failure,
981 fetch_finish,
982 &spf);
983
984 argv_array_clear(&spf.args);
88a21979
JL
985out:
986 string_list_clear(&changed_submodule_paths, 1);
fe85ee6e 987 return spf.result;
7dce19d3
JL
988}
989
3bfc4504 990unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 991{
c7e1a736 992 ssize_t len;
d3180279 993 struct child_process cp = CHILD_PROCESS_INIT;
ee6fc514
JL
994 const char *argv[] = {
995 "status",
996 "--porcelain",
997 NULL,
3bfc4504 998 NULL,
ee6fc514 999 };
ee6fc514 1000 struct strbuf buf = STRBUF_INIT;
c7e1a736
JL
1001 unsigned dirty_submodule = 0;
1002 const char *line, *next_line;
eee49b6c 1003 const char *git_dir;
ee6fc514 1004
eee49b6c 1005 strbuf_addf(&buf, "%s/.git", path);
13d6ec91 1006 git_dir = read_gitfile(buf.buf);
eee49b6c
JL
1007 if (!git_dir)
1008 git_dir = buf.buf;
1009 if (!is_directory(git_dir)) {
ee6fc514
JL
1010 strbuf_release(&buf);
1011 /* The submodule is not checked out, so it is not modified */
1012 return 0;
1013
1014 }
1015 strbuf_reset(&buf);
1016
3bfc4504
JL
1017 if (ignore_untracked)
1018 argv[2] = "-uno";
1019
ee6fc514 1020 cp.argv = argv;
c12e8656 1021 prepare_submodule_repo_env(&cp.env_array);
ee6fc514
JL
1022 cp.git_cmd = 1;
1023 cp.no_stdin = 1;
1024 cp.out = -1;
eee49b6c 1025 cp.dir = path;
ee6fc514 1026 if (start_command(&cp))
6a5cedac 1027 die("Could not run 'git status --porcelain' in submodule %s", path);
ee6fc514
JL
1028
1029 len = strbuf_read(&buf, cp.out, 1024);
c7e1a736
JL
1030 line = buf.buf;
1031 while (len > 2) {
1032 if ((line[0] == '?') && (line[1] == '?')) {
1033 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1034 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
1035 break;
1036 } else {
1037 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
3bfc4504
JL
1038 if (ignore_untracked ||
1039 (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED))
c7e1a736
JL
1040 break;
1041 }
1042 next_line = strchr(line, '\n');
1043 if (!next_line)
1044 break;
1045 next_line++;
1046 len -= (next_line - line);
1047 line = next_line;
1048 }
ee6fc514
JL
1049 close(cp.out);
1050
1051 if (finish_command(&cp))
6a5cedac 1052 die("'git status --porcelain' failed in submodule %s", path);
ee6fc514 1053
ee6fc514 1054 strbuf_release(&buf);
c7e1a736 1055 return dirty_submodule;
ee6fc514 1056}
68d03e4a 1057
293ab15e
JL
1058int submodule_uses_gitfile(const char *path)
1059{
d3180279 1060 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e
JL
1061 const char *argv[] = {
1062 "submodule",
1063 "foreach",
1064 "--quiet",
1065 "--recursive",
1066 "test -f .git",
1067 NULL,
1068 };
1069 struct strbuf buf = STRBUF_INIT;
1070 const char *git_dir;
1071
1072 strbuf_addf(&buf, "%s/.git", path);
1073 git_dir = read_gitfile(buf.buf);
1074 if (!git_dir) {
1075 strbuf_release(&buf);
1076 return 0;
1077 }
1078 strbuf_release(&buf);
1079
1080 /* Now test that all nested submodules use a gitfile too */
293ab15e 1081 cp.argv = argv;
c12e8656 1082 prepare_submodule_repo_env(&cp.env_array);
293ab15e
JL
1083 cp.git_cmd = 1;
1084 cp.no_stdin = 1;
1085 cp.no_stderr = 1;
1086 cp.no_stdout = 1;
1087 cp.dir = path;
1088 if (run_command(&cp))
1089 return 0;
1090
1091 return 1;
1092}
1093
1094int ok_to_remove_submodule(const char *path)
1095{
293ab15e 1096 ssize_t len;
d3180279 1097 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e
JL
1098 const char *argv[] = {
1099 "status",
1100 "--porcelain",
1101 "-u",
1102 "--ignore-submodules=none",
1103 NULL,
1104 };
1105 struct strbuf buf = STRBUF_INIT;
1106 int ok_to_remove = 1;
1107
dbe44faa 1108 if (!file_exists(path) || is_empty_dir(path))
293ab15e
JL
1109 return 1;
1110
1111 if (!submodule_uses_gitfile(path))
1112 return 0;
1113
293ab15e 1114 cp.argv = argv;
c12e8656 1115 prepare_submodule_repo_env(&cp.env_array);
293ab15e
JL
1116 cp.git_cmd = 1;
1117 cp.no_stdin = 1;
1118 cp.out = -1;
1119 cp.dir = path;
1120 if (start_command(&cp))
1121 die("Could not run 'git status --porcelain -uall --ignore-submodules=none' in submodule %s", path);
1122
1123 len = strbuf_read(&buf, cp.out, 1024);
1124 if (len > 2)
1125 ok_to_remove = 0;
1126 close(cp.out);
1127
1128 if (finish_command(&cp))
1129 die("'git status --porcelain -uall --ignore-submodules=none' failed in submodule %s", path);
1130
1131 strbuf_release(&buf);
1132 return ok_to_remove;
1133}
1134
68d03e4a
HV
1135static int find_first_merges(struct object_array *result, const char *path,
1136 struct commit *a, struct commit *b)
1137{
1138 int i, j;
3826902d 1139 struct object_array merges = OBJECT_ARRAY_INIT;
68d03e4a
HV
1140 struct commit *commit;
1141 int contains_another;
1142
1143 char merged_revision[42];
1144 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1145 "--all", merged_revision, NULL };
1146 struct rev_info revs;
1147 struct setup_revision_opt rev_opts;
1148
68d03e4a
HV
1149 memset(result, 0, sizeof(struct object_array));
1150 memset(&rev_opts, 0, sizeof(rev_opts));
1151
1152 /* get all revisions that merge commit a */
1153 snprintf(merged_revision, sizeof(merged_revision), "^%s",
f2fd0760 1154 oid_to_hex(&a->object.oid));
68d03e4a
HV
1155 init_revisions(&revs, NULL);
1156 rev_opts.submodule = path;
94c0cc8f 1157 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
68d03e4a
HV
1158
1159 /* save all revisions from the above list that contain b */
1160 if (prepare_revision_walk(&revs))
1161 die("revision walk setup failed");
1162 while ((commit = get_revision(&revs)) != NULL) {
1163 struct object *o = &(commit->object);
a20efee9 1164 if (in_merge_bases(b, commit))
68d03e4a
HV
1165 add_object_array(o, NULL, &merges);
1166 }
bcc0a3ea 1167 reset_revision_walk();
68d03e4a
HV
1168
1169 /* Now we've got all merges that contain a and b. Prune all
1170 * merges that contain another found merge and save them in
1171 * result.
1172 */
1173 for (i = 0; i < merges.nr; i++) {
1174 struct commit *m1 = (struct commit *) merges.objects[i].item;
1175
1176 contains_another = 0;
1177 for (j = 0; j < merges.nr; j++) {
1178 struct commit *m2 = (struct commit *) merges.objects[j].item;
a20efee9 1179 if (i != j && in_merge_bases(m2, m1)) {
68d03e4a
HV
1180 contains_another = 1;
1181 break;
1182 }
1183 }
1184
1185 if (!contains_another)
5de0c015 1186 add_object_array(merges.objects[i].item, NULL, result);
68d03e4a
HV
1187 }
1188
1189 free(merges.objects);
1190 return result->nr;
1191}
1192
1193static void print_commit(struct commit *commit)
1194{
1195 struct strbuf sb = STRBUF_INIT;
1196 struct pretty_print_context ctx = {0};
a5481a6c 1197 ctx.date_mode.type = DATE_NORMAL;
68d03e4a
HV
1198 format_commit_message(commit, " %h: %m %s", &sb, &ctx);
1199 fprintf(stderr, "%s\n", sb.buf);
1200 strbuf_release(&sb);
1201}
1202
1203#define MERGE_WARNING(path, msg) \
1204 warning("Failed to merge submodule %s (%s)", path, msg);
1205
1206int merge_submodule(unsigned char result[20], const char *path,
1207 const unsigned char base[20], const unsigned char a[20],
80988783 1208 const unsigned char b[20], int search)
68d03e4a
HV
1209{
1210 struct commit *commit_base, *commit_a, *commit_b;
1211 int parent_count;
1212 struct object_array merges;
1213
1214 int i;
1215
1216 /* store a in result in case we fail */
1217 hashcpy(result, a);
1218
1219 /* we can not handle deletion conflicts */
1220 if (is_null_sha1(base))
1221 return 0;
1222 if (is_null_sha1(a))
1223 return 0;
1224 if (is_null_sha1(b))
1225 return 0;
1226
1227 if (add_submodule_odb(path)) {
1228 MERGE_WARNING(path, "not checked out");
1229 return 0;
1230 }
1231
1232 if (!(commit_base = lookup_commit_reference(base)) ||
1233 !(commit_a = lookup_commit_reference(a)) ||
1234 !(commit_b = lookup_commit_reference(b))) {
1235 MERGE_WARNING(path, "commits not present");
1236 return 0;
1237 }
1238
1239 /* check whether both changes are forward */
a20efee9
JH
1240 if (!in_merge_bases(commit_base, commit_a) ||
1241 !in_merge_bases(commit_base, commit_b)) {
68d03e4a
HV
1242 MERGE_WARNING(path, "commits don't follow merge-base");
1243 return 0;
1244 }
1245
1246 /* Case #1: a is contained in b or vice versa */
a20efee9 1247 if (in_merge_bases(commit_a, commit_b)) {
68d03e4a
HV
1248 hashcpy(result, b);
1249 return 1;
1250 }
a20efee9 1251 if (in_merge_bases(commit_b, commit_a)) {
68d03e4a
HV
1252 hashcpy(result, a);
1253 return 1;
1254 }
1255
1256 /*
1257 * Case #2: There are one or more merges that contain a and b in
1258 * the submodule. If there is only one, then present it as a
1259 * suggestion to the user, but leave it marked unmerged so the
1260 * user needs to confirm the resolution.
1261 */
1262
80988783
BK
1263 /* Skip the search if makes no sense to the calling context. */
1264 if (!search)
1265 return 0;
1266
68d03e4a
HV
1267 /* find commit which merges them */
1268 parent_count = find_first_merges(&merges, path, commit_a, commit_b);
1269 switch (parent_count) {
1270 case 0:
1271 MERGE_WARNING(path, "merge following commits not found");
1272 break;
1273
1274 case 1:
1275 MERGE_WARNING(path, "not fast-forward");
1276 fprintf(stderr, "Found a possible merge resolution "
1277 "for the submodule:\n");
1278 print_commit((struct commit *) merges.objects[0].item);
1279 fprintf(stderr,
1280 "If this is correct simply add it to the index "
1281 "for example\n"
1282 "by using:\n\n"
1283 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1284 "which will accept this suggestion.\n",
f2fd0760 1285 oid_to_hex(&merges.objects[0].item->oid), path);
68d03e4a
HV
1286 break;
1287
1288 default:
1289 MERGE_WARNING(path, "multiple merges found");
1290 for (i = 0; i < merges.nr; i++)
1291 print_commit((struct commit *) merges.objects[i].item);
1292 }
1293
1294 free(merges.objects);
1295 return 0;
1296}
a88c915d
JL
1297
1298/* Update gitfile and core.worktree setting to connect work tree and git dir */
1299void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)
1300{
1301 struct strbuf file_name = STRBUF_INIT;
1302 struct strbuf rel_path = STRBUF_INIT;
1303 const char *real_work_tree = xstrdup(real_path(work_tree));
a88c915d
JL
1304
1305 /* Update gitfile */
1306 strbuf_addf(&file_name, "%s/.git", work_tree);
1f76a10b 1307 write_file(file_name.buf, "gitdir: %s",
91aacda8 1308 relative_path(git_dir, real_work_tree, &rel_path));
a88c915d
JL
1309
1310 /* Update core.worktree setting */
1311 strbuf_reset(&file_name);
1312 strbuf_addf(&file_name, "%s/config", git_dir);
3d180648
PS
1313 git_config_set_in_file(file_name.buf, "core.worktree",
1314 relative_path(real_work_tree, git_dir,
1315 &rel_path));
a88c915d
JL
1316
1317 strbuf_release(&file_name);
1318 strbuf_release(&rel_path);
1319 free((void *)real_work_tree);
1320}
a028a193
SB
1321
1322int parallel_submodules(void)
1323{
1324 return parallel_jobs;
1325}
e059388f 1326
4638728c
JK
1327void prepare_submodule_repo_env(struct argv_array *out)
1328{
1329 const char * const *var;
1330
1331 for (var = local_repo_env; *var; var++) {
89044baa 1332 if (strcmp(*var, CONFIG_DATA_ENVIRONMENT))
4638728c 1333 argv_array_push(out, *var);
4638728c 1334 }
10f5c526 1335 argv_array_push(out, "GIT_DIR=.git");
4638728c 1336}