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