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