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