]> git.ipfire.org Git - thirdparty/git.git/blame - submodule.c
refs: unify parse_worktree_ref() and ref_type()
[thirdparty/git.git] / submodule.c
CommitLineData
e724197f 1
752c0c24 2#include "cache.h"
69aba532 3#include "repository.h"
b2141fc1 4#include "config.h"
851e18c3 5#include "submodule-config.h"
752c0c24
JS
6#include "submodule.h"
7#include "dir.h"
8#include "diff.h"
9#include "commit.h"
10#include "revision.h"
ee6fc514 11#include "run-command.h"
c7e1a736 12#include "diffcore.h"
68d03e4a 13#include "refs.h"
aee9c7d6 14#include "string-list.h"
fe299ec5 15#include "oid-array.h"
dbbcd44f 16#include "strvec.h"
5fee9952 17#include "blob.h"
fe85ee6e 18#include "thread-utils.h"
4638728c 19#include "quote.h"
06bf4ad1 20#include "remote.h"
f6f85861 21#include "worktree.h"
046b4823 22#include "parse-options.h"
0d4a1321 23#include "object-store.h"
64043556 24#include "commit-reach.h"
2a69ff09 25#include "shallow.h"
aee9c7d6 26
d7a3803f 27static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
6859de45 28static int initialized_fetch_ref_tips;
910650d2 29static struct oid_array ref_tips_before_fetch;
30static struct oid_array ref_tips_after_fetch;
6859de45 31
d4e98b58 32/*
34e2ba04
BW
33 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file
34 * will be disabled because we can't guess what might be configured in
35 * .gitmodules unless the user resolves the conflict.
d4e98b58 36 */
847a9e5d 37int is_gitmodules_unmerged(struct index_state *istate)
34e2ba04
BW
38{
39 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
40 if (pos < 0) { /* .gitmodules not found or isn't merged */
41 pos = -1 - pos;
42 if (istate->cache_nr > pos) { /* there is a .gitmodules */
43 const struct cache_entry *ce = istate->cache[pos];
44 if (ce_namelen(ce) == strlen(GITMODULES_FILE) &&
45 !strcmp(ce->name, GITMODULES_FILE))
46 return 1;
47 }
48 }
49
50 return 0;
51}
752c0c24 52
b5c259f2
AO
53/*
54 * Check if the .gitmodules file is safe to write.
55 *
56 * Writing to the .gitmodules file requires that the file exists in the
57 * working tree or, if it doesn't, that a brand new .gitmodules file is going
58 * to be created (i.e. it's neither in the index nor in the current branch).
59 *
60 * It is not safe to write to .gitmodules if it's not in the working tree but
61 * it is in the index or in the current branch, because writing new values
62 * (and staging them) would blindly overwrite ALL the old content.
63 */
64int is_writing_gitmodules_ok(void)
65{
66 struct object_id oid;
67 return file_exists(GITMODULES_FILE) ||
68 (get_oid(GITMODULES_INDEX, &oid) < 0 && get_oid(GITMODULES_HEAD, &oid) < 0);
69}
70
5fee9952 71/*
91b83480
BW
72 * Check if the .gitmodules file has unstaged modifications. This must be
73 * checked before allowing modifications to the .gitmodules file with the
74 * intention to stage them later, because when continuing we would stage the
75 * modifications the user didn't stage herself too. That might change in a
76 * future version when we learn to stage the changes we do ourselves without
77 * staging any previous modifications.
5fee9952 78 */
7da9aba4 79int is_staging_gitmodules_ok(struct index_state *istate)
5fee9952 80{
91b83480
BW
81 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE));
82
83 if ((pos >= 0) && (pos < istate->cache_nr)) {
84 struct stat st;
85 if (lstat(GITMODULES_FILE, &st) == 0 &&
7edee329 86 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED)
91b83480
BW
87 return 0;
88 }
89
90 return 1;
5fee9952
JL
91}
92
2e2d4040
NTND
93static int for_each_remote_ref_submodule(const char *submodule,
94 each_ref_fn fn, void *cb_data)
95{
96 return refs_for_each_remote_ref(get_submodule_ref_store(submodule),
97 fn, cb_data);
98}
99
0656781f
JL
100/*
101 * Try to update the "path" entry in the "submodule.<name>" section of the
102 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
103 * with the correct path=<oldpath> setting was found and we could update it.
104 */
105int update_path_in_gitmodules(const char *oldpath, const char *newpath)
106{
107 struct strbuf entry = STRBUF_INIT;
851e18c3 108 const struct submodule *submodule;
45f5ef3d 109 int ret;
0656781f 110
4c0eeafe 111 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
0656781f
JL
112 return -1;
113
68f08b4b 114 if (is_gitmodules_unmerged(the_repository->index))
0656781f
JL
115 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
116
14228447 117 submodule = submodule_from_path(the_repository, null_oid(), oldpath);
851e18c3 118 if (!submodule || !submodule->name) {
0656781f
JL
119 warning(_("Could not find section in .gitmodules where path=%s"), oldpath);
120 return -1;
121 }
122 strbuf_addstr(&entry, "submodule.");
851e18c3 123 strbuf_addstr(&entry, submodule->name);
0656781f 124 strbuf_addstr(&entry, ".path");
45f5ef3d 125 ret = config_set_in_gitmodules_file_gently(entry.buf, newpath);
0656781f 126 strbuf_release(&entry);
45f5ef3d 127 return ret;
0656781f
JL
128}
129
95c16418
JL
130/*
131 * Try to remove the "submodule.<name>" section from .gitmodules where the given
132 * path is configured. Return 0 only if a .gitmodules file was found, a section
133 * with the correct path=<path> setting was found and we could remove it.
134 */
135int remove_path_from_gitmodules(const char *path)
136{
137 struct strbuf sect = STRBUF_INIT;
851e18c3 138 const struct submodule *submodule;
95c16418 139
4c0eeafe 140 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */
95c16418
JL
141 return -1;
142
68f08b4b 143 if (is_gitmodules_unmerged(the_repository->index))
95c16418
JL
144 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
145
14228447 146 submodule = submodule_from_path(the_repository, null_oid(), path);
851e18c3 147 if (!submodule || !submodule->name) {
95c16418
JL
148 warning(_("Could not find section in .gitmodules where path=%s"), path);
149 return -1;
150 }
151 strbuf_addstr(&sect, "submodule.");
851e18c3 152 strbuf_addstr(&sect, submodule->name);
4c0eeafe 153 if (git_config_rename_section_in_file(GITMODULES_FILE, sect.buf, NULL) < 0) {
95c16418
JL
154 /* Maybe the user already did that, don't error out here */
155 warning(_("Could not remove .gitmodules entry for %s"), path);
156 strbuf_release(&sect);
157 return -1;
158 }
159 strbuf_release(&sect);
160 return 0;
161}
162
3b8317a9 163void stage_updated_gitmodules(struct index_state *istate)
5fee9952 164{
3b8317a9 165 if (add_file_to_index(istate, GITMODULES_FILE, 0))
5fee9952
JL
166 die(_("staging updated .gitmodules failed"));
167}
168
a35e03de
JT
169static struct string_list added_submodule_odb_paths = STRING_LIST_INIT_NODUP;
170
8d33c3af
JT
171void add_submodule_odb_by_path(const char *path)
172{
173 string_list_insert(&added_submodule_odb_paths, xstrdup(path));
174}
175
a35e03de
JT
176int register_all_submodule_odb_as_alternates(void)
177{
178 int i;
179 int ret = added_submodule_odb_paths.nr;
180
181 for (i = 0; i < added_submodule_odb_paths.nr; i++)
182 add_to_alternates_memory(added_submodule_odb_paths.items[i].string);
183 if (ret) {
184 string_list_clear(&added_submodule_odb_paths, 0);
71ef66d7
JT
185 trace2_data_intmax("submodule", the_repository,
186 "register_all_submodule_odb_as_alternates/registered", ret);
a35e03de
JT
187 if (git_env_bool("GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB", 0))
188 BUG("register_all_submodule_odb_as_alternates() called");
189 }
190 return ret;
191}
192
aee9c7d6
JL
193void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt,
194 const char *path)
195{
3b8fb393 196 const struct submodule *submodule = submodule_from_path(the_repository,
14228447 197 null_oid(),
198 path);
851e18c3 199 if (submodule) {
fdfa9e97
BW
200 const char *ignore;
201 char *key;
aee9c7d6 202
fdfa9e97 203 key = xstrfmt("submodule.%s.ignore", submodule->name);
f1de981e 204 if (repo_config_get_string_tmp(the_repository, key, &ignore))
fdfa9e97
BW
205 ignore = submodule->ignore;
206 free(key);
302ad7a9 207
fdfa9e97
BW
208 if (ignore)
209 handle_ignore_submodules_arg(diffopt, ignore);
68f08b4b 210 else if (is_gitmodules_unmerged(the_repository->index))
0d1e0e78 211 diffopt->flags.ignore_submodules = 1;
046b4823
SB
212 }
213}
214
215/* Cheap function that only determines if we're interested in submodules at all */
783a86c1 216int git_default_submodule_config(const char *var, const char *value,
5cf88fd8 217 void *cb UNUSED)
046b4823
SB
218{
219 if (!strcmp(var, "submodule.recurse")) {
220 int v = git_config_bool(var, value) ?
221 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
222 config_update_recurse_submodules = v;
223 }
224 return 0;
1d789d08
SB
225}
226
d7a3803f
SB
227int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
228 const char *arg, int unset)
229{
230 if (unset) {
231 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
232 return 0;
233 }
234 if (arg)
235 config_update_recurse_submodules =
236 parse_update_recurse_submodules_arg(opt->long_name,
237 arg);
238 else
239 config_update_recurse_submodules = RECURSE_SUBMODULES_ON;
240
241 return 0;
242}
243
f9f42560
BW
244/*
245 * Determine if a submodule has been initialized at a given 'path'
246 */
a452128a
AR
247/*
248 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless,
249 * ie, the config looks like: "[submodule] active\n".
250 * Since that is an invalid pathspec, we should inform the user.
251 */
961b130d
GC
252int is_tree_submodule_active(struct repository *repo,
253 const struct object_id *treeish_name,
254 const char *path)
f9f42560
BW
255{
256 int ret = 0;
a086f921
BW
257 char *key = NULL;
258 char *value = NULL;
259 const struct string_list *sl;
627d9342
BW
260 const struct submodule *module;
261
961b130d 262 module = submodule_from_path(repo, treeish_name, path);
f9f42560 263
a086f921
BW
264 /* early return if there isn't a path->module mapping */
265 if (!module)
266 return 0;
f9f42560 267
a086f921
BW
268 /* submodule.<name>.active is set */
269 key = xstrfmt("submodule.%s.active", module->name);
627d9342 270 if (!repo_config_get_bool(repo, key, &ret)) {
a086f921
BW
271 free(key);
272 return ret;
273 }
274 free(key);
f9f42560 275
a086f921 276 /* submodule.active is set */
627d9342 277 sl = repo_config_get_value_multi(repo, "submodule.active");
a086f921
BW
278 if (sl) {
279 struct pathspec ps;
c972bf4c 280 struct strvec args = STRVEC_INIT;
a086f921 281 const struct string_list_item *item;
f9f42560 282
a086f921 283 for_each_string_list_item(item, sl) {
c972bf4c 284 strvec_push(&args, item->string);
a086f921
BW
285 }
286
d70a9eb6 287 parse_pathspec(&ps, 0, 0, NULL, args.v);
68f08b4b 288 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1);
a086f921 289
c972bf4c 290 strvec_clear(&args);
a086f921
BW
291 clear_pathspec(&ps);
292 return ret;
f9f42560
BW
293 }
294
a086f921
BW
295 /* fallback to checking if the URL is set */
296 key = xstrfmt("submodule.%s.url", module->name);
627d9342 297 ret = !repo_config_get_string(repo, key, &value);
a086f921
BW
298
299 free(value);
300 free(key);
f9f42560
BW
301 return ret;
302}
303
961b130d
GC
304int is_submodule_active(struct repository *repo, const char *path)
305{
306 return is_tree_submodule_active(repo, null_oid(), path);
307}
308
15cdc647 309int is_submodule_populated_gently(const char *path, int *return_error_code)
5688c28d
BW
310{
311 int ret = 0;
312 char *gitdir = xstrfmt("%s/.git", path);
313
15cdc647 314 if (resolve_gitdir_gently(gitdir, return_error_code))
5688c28d
BW
315 ret = 1;
316
317 free(gitdir);
318 return ret;
319}
320
bdab9721
BW
321/*
322 * Dies if the provided 'prefix' corresponds to an unpopulated submodule
323 */
847a9e5d 324void die_in_unpopulated_submodule(struct index_state *istate,
bdab9721
BW
325 const char *prefix)
326{
327 int i, prefixlen;
328
329 if (!prefix)
330 return;
331
332 prefixlen = strlen(prefix);
333
334 for (i = 0; i < istate->cache_nr; i++) {
335 struct cache_entry *ce = istate->cache[i];
336 int ce_len = ce_namelen(ce);
337
338 if (!S_ISGITLINK(ce->ce_mode))
339 continue;
340 if (prefixlen <= ce_len)
341 continue;
342 if (strncmp(ce->name, prefix, ce_len))
343 continue;
344 if (prefix[ce_len] != '/')
345 continue;
346
347 die(_("in unpopulated submodule '%s'"), ce->name);
348 }
349}
350
c08397e3
BW
351/*
352 * Dies if any paths in the provided pathspec descends into a submodule
353 */
847a9e5d 354void die_path_inside_submodule(struct index_state *istate,
c08397e3
BW
355 const struct pathspec *ps)
356{
357 int i, j;
358
359 for (i = 0; i < istate->cache_nr; i++) {
360 struct cache_entry *ce = istate->cache[i];
361 int ce_len = ce_namelen(ce);
362
363 if (!S_ISGITLINK(ce->ce_mode))
364 continue;
365
366 for (j = 0; j < ps->nr ; j++) {
367 const struct pathspec_item *item = &ps->items[j];
368
369 if (item->len <= ce_len)
370 continue;
371 if (item->match[ce_len] != '/')
372 continue;
373 if (strncmp(ce->name, item->match, ce_len))
374 continue;
375 if (item->len == ce_len + 1)
376 continue;
377
378 die(_("Pathspec '%s' is in submodule '%.*s'"),
379 item->original, ce_len, ce->name);
380 }
381 }
382}
383
ec6141a0 384enum submodule_update_type parse_submodule_update_type(const char *value)
ea2fa5a3 385{
ea2fa5a3 386 if (!strcmp(value, "none"))
ec6141a0 387 return SM_UPDATE_NONE;
ea2fa5a3 388 else if (!strcmp(value, "checkout"))
ec6141a0 389 return SM_UPDATE_CHECKOUT;
ea2fa5a3 390 else if (!strcmp(value, "rebase"))
ec6141a0 391 return SM_UPDATE_REBASE;
ea2fa5a3 392 else if (!strcmp(value, "merge"))
ec6141a0
BW
393 return SM_UPDATE_MERGE;
394 else if (*value == '!')
395 return SM_UPDATE_COMMAND;
396 else
397 return SM_UPDATE_UNSPECIFIED;
398}
399
400int parse_submodule_update_strategy(const char *value,
401 struct submodule_update_strategy *dst)
402{
403 enum submodule_update_type type;
404
405 free((void*)dst->command);
406 dst->command = NULL;
407
408 type = parse_submodule_update_type(value);
409 if (type == SM_UPDATE_UNSPECIFIED)
ea2fa5a3 410 return -1;
ec6141a0
BW
411
412 dst->type = type;
413 if (type == SM_UPDATE_COMMAND)
414 dst->command = xstrdup(value + 1);
415
ea2fa5a3
SB
416 return 0;
417}
418
b9dd63ff 419const char *submodule_update_type_to_string(enum submodule_update_type type)
3604242f 420{
b9dd63ff 421 switch (type) {
3604242f
SB
422 case SM_UPDATE_CHECKOUT:
423 return "checkout";
424 case SM_UPDATE_MERGE:
425 return "merge";
426 case SM_UPDATE_REBASE:
427 return "rebase";
428 case SM_UPDATE_NONE:
429 return "none";
430 case SM_UPDATE_UNSPECIFIED:
3604242f 431 case SM_UPDATE_COMMAND:
b9dd63ff
ÆAB
432 BUG("init_submodule() should handle type %d", type);
433 default:
434 BUG("unexpected update strategy type: %d", type);
3604242f 435 }
3604242f
SB
436}
437
46a958b3
JL
438void handle_ignore_submodules_arg(struct diff_options *diffopt,
439 const char *arg)
440{
8ef93124 441 diffopt->flags.ignore_submodule_set = 1;
0d1e0e78
BW
442 diffopt->flags.ignore_submodules = 0;
443 diffopt->flags.ignore_untracked_in_submodules = 0;
444 diffopt->flags.ignore_dirty_submodules = 0;
be4f2b40 445
46a958b3 446 if (!strcmp(arg, "all"))
0d1e0e78 447 diffopt->flags.ignore_submodules = 1;
46a958b3 448 else if (!strcmp(arg, "untracked"))
0d1e0e78 449 diffopt->flags.ignore_untracked_in_submodules = 1;
46a958b3 450 else if (!strcmp(arg, "dirty"))
0d1e0e78 451 diffopt->flags.ignore_dirty_submodules = 1;
aee9c7d6 452 else if (strcmp(arg, "none"))
a4ffbbbb 453 die(_("bad --ignore-submodules argument: %s"), arg);
5a59a230
NTND
454 /*
455 * Please update _git_status() in git-completion.bash when you
456 * add new options
457 */
46a958b3
JL
458}
459
4831c23f
JH
460static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev,
461 const char *path,
462 struct commit *left, struct commit *right,
463 struct commit_list *merge_bases)
808a95dc 464{
8e6df650 465 struct commit_list *list;
808a95dc 466
85a1ec2c 467 repo_init_revisions(r, rev, NULL);
808a95dc
JN
468 setup_revisions(0, NULL, rev, NULL);
469 rev->left_right = 1;
470 rev->first_parent_only = 1;
471 left->object.flags |= SYMMETRIC_LEFT;
472 add_pending_object(rev, &left->object, path);
473 add_pending_object(rev, &right->object, path);
808a95dc
JN
474 for (list = merge_bases; list; list = list->next) {
475 list->item->object.flags |= UNINTERESTING;
476 add_pending_object(rev, &list->item->object,
f2fd0760 477 oid_to_hex(&list->item->object.oid));
808a95dc
JN
478 }
479 return prepare_revision_walk(rev);
480}
481
180b154b 482static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o)
808a95dc
JN
483{
484 static const char format[] = " %m %s";
485 struct strbuf sb = STRBUF_INIT;
486 struct commit *commit;
487
488 while ((commit = get_revision(rev))) {
489 struct pretty_print_context ctx = {0};
490 ctx.date_mode = rev->date_mode;
ecaee805 491 ctx.output_encoding = get_log_output_encoding();
808a95dc 492 strbuf_setlen(&sb, 0);
605f0ec1
SB
493 repo_format_commit_message(r, commit, format, &sb,
494 &ctx);
808a95dc 495 strbuf_addch(&sb, '\n');
f3597138
SB
496 if (commit->object.flags & SYMMETRIC_LEFT)
497 diff_emit_submodule_del(o, sb.buf);
498 else
499 diff_emit_submodule_add(o, sb.buf);
808a95dc
JN
500 }
501 strbuf_release(&sb);
502}
503
c972bf4c 504void prepare_submodule_repo_env(struct strvec *out)
6cd5757c 505{
d1fa9435 506 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT);
6cd5757c
SB
507}
508
c972bf4c 509static void prepare_submodule_repo_env_in_gitdir(struct strvec *out)
a62387b3 510{
d1fa9435 511 prepare_other_repo_env(out, ".");
a62387b3
SB
512}
513
605f0ec1
SB
514/*
515 * Initialize a repository struct for a submodule based on the provided 'path'.
516 *
605f0ec1
SB
517 * Returns the repository struct on success,
518 * NULL when the submodule is not present.
8e6df650 519 */
605f0ec1
SB
520static struct repository *open_submodule(const char *path)
521{
522 struct strbuf sb = STRBUF_INIT;
523 struct repository *out = xmalloc(sizeof(*out));
524
525 if (submodule_to_gitdir(&sb, path) || repo_init(out, sb.buf, NULL)) {
526 strbuf_release(&sb);
527 free(out);
528 return NULL;
529 }
530
531 /* Mark it as a submodule */
532 out->submodule_prefix = xstrdup(path);
533
534 strbuf_release(&sb);
535 return out;
536}
537
538/*
539 * Helper function to display the submodule header line prior to the full
540 * summary output.
541 *
542 * If it can locate the submodule git directory it will create a repository
543 * handle for the submodule and lookup both the left and right commits and
544 * put them into the left and right pointers.
8e6df650 545 */
605f0ec1
SB
546static void show_submodule_header(struct diff_options *o,
547 const char *path,
602a283a 548 struct object_id *one, struct object_id *two,
f3597138 549 unsigned dirty_submodule,
605f0ec1 550 struct repository *sub,
8e6df650
JK
551 struct commit **left, struct commit **right,
552 struct commit_list **merge_bases)
752c0c24 553{
752c0c24
JS
554 const char *message = NULL;
555 struct strbuf sb = STRBUF_INIT;
752c0c24
JS
556 int fast_forward = 0, fast_backward = 0;
557
c7e1a736 558 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED)
f3597138
SB
559 diff_emit_submodule_untracked(o, path);
560
c7e1a736 561 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
f3597138 562 diff_emit_submodule_modified(o, path);
c7e1a736 563
8e6df650
JK
564 if (is_null_oid(one))
565 message = "(new submodule)";
566 else if (is_null_oid(two))
567 message = "(submodule deleted)";
568
605f0ec1 569 if (!sub) {
8e6df650 570 if (!message)
2d94dd2f 571 message = "(commits not present)";
8e6df650
JK
572 goto output_header;
573 }
574
575 /*
576 * Attempt to lookup the commit references, and determine if this is
577 * a fast forward or fast backwards update.
578 */
605f0ec1
SB
579 *left = lookup_commit_reference(sub, one);
580 *right = lookup_commit_reference(sub, two);
8e6df650
JK
581
582 /*
583 * Warn about missing commits in the submodule project, but only if
584 * they aren't null.
585 */
586 if ((!is_null_oid(one) && !*left) ||
587 (!is_null_oid(two) && !*right))
588 message = "(commits not present)";
589
605f0ec1 590 *merge_bases = repo_get_merge_bases(sub, *left, *right);
8e6df650
JK
591 if (*merge_bases) {
592 if ((*merge_bases)->item == *left)
593 fast_forward = 1;
594 else if ((*merge_bases)->item == *right)
595 fast_backward = 1;
596 }
597
4a7e27e9 598 if (oideq(one, two)) {
c7e1a736
JL
599 strbuf_release(&sb);
600 return;
601 }
602
8e6df650 603output_header:
f3597138 604 strbuf_addf(&sb, "Submodule %s ", path);
30e677e0 605 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV);
a94bb683 606 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "...");
30e677e0 607 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV);
752c0c24 608 if (message)
f3597138 609 strbuf_addf(&sb, " %s\n", message);
752c0c24 610 else
f3597138
SB
611 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : "");
612 diff_emit_submodule_header(o, sb.buf);
752c0c24 613
752c0c24
JS
614 strbuf_release(&sb);
615}
ee6fc514 616
180b154b 617void show_submodule_diff_summary(struct diff_options *o, const char *path,
8e6df650 618 struct object_id *one, struct object_id *two,
f3597138 619 unsigned dirty_submodule)
8e6df650 620{
f196c1e9 621 struct rev_info rev = REV_INFO_INIT;
8e6df650
JK
622 struct commit *left = NULL, *right = NULL;
623 struct commit_list *merge_bases = NULL;
605f0ec1 624 struct repository *sub;
8e6df650 625
605f0ec1 626 sub = open_submodule(path);
f3597138 627 show_submodule_header(o, path, one, two, dirty_submodule,
605f0ec1 628 sub, &left, &right, &merge_bases);
8e6df650
JK
629
630 /*
631 * If we don't have both a left and a right pointer, there is no
632 * reason to try and display a summary. The header line should contain
633 * all the information the user needs.
634 */
605f0ec1 635 if (!left || !right || !sub)
8e6df650
JK
636 goto out;
637
638 /* Treat revision walker failure the same as missing commits */
4831c23f 639 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) {
f3597138 640 diff_emit_submodule_error(o, "(revision walker failed)\n");
8e6df650
JK
641 goto out;
642 }
643
180b154b 644 print_submodule_diff_summary(sub, &rev, o);
8e6df650
JK
645
646out:
bf20fe4c 647 free_commit_list(merge_bases);
f196c1e9 648 release_revisions(&rev);
8e6df650
JK
649 clear_commit_marks(left, ~0);
650 clear_commit_marks(right, ~0);
605f0ec1
SB
651 if (sub) {
652 repo_clear(sub);
653 free(sub);
654 }
8e6df650
JK
655}
656
f3597138 657void show_submodule_inline_diff(struct diff_options *o, const char *path,
fd47ae6a 658 struct object_id *one, struct object_id *two,
f3597138 659 unsigned dirty_submodule)
fd47ae6a 660{
bc099914 661 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree;
fd47ae6a
JK
662 struct commit *left = NULL, *right = NULL;
663 struct commit_list *merge_bases = NULL;
fd47ae6a 664 struct child_process cp = CHILD_PROCESS_INIT;
f3597138 665 struct strbuf sb = STRBUF_INIT;
605f0ec1 666 struct repository *sub;
fd47ae6a 667
605f0ec1 668 sub = open_submodule(path);
f3597138 669 show_submodule_header(o, path, one, two, dirty_submodule,
605f0ec1 670 sub, &left, &right, &merge_bases);
fd47ae6a
JK
671
672 /* We need a valid left and right commit to display a difference */
673 if (!(left || is_null_oid(one)) ||
674 !(right || is_null_oid(two)))
675 goto done;
676
677 if (left)
bc099914 678 old_oid = one;
fd47ae6a 679 if (right)
bc099914 680 new_oid = two;
fd47ae6a 681
fd47ae6a
JK
682 cp.git_cmd = 1;
683 cp.dir = path;
f3597138 684 cp.out = -1;
fd47ae6a
JK
685 cp.no_stdin = 1;
686
687 /* TODO: other options may need to be passed here. */
c972bf4c
JK
688 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL);
689 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ?
f3597138 690 "always" : "never");
5a522142 691
0d1e0e78 692 if (o->flags.reverse_diff) {
c972bf4c 693 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
f6d8942b 694 o->b_prefix, path);
c972bf4c 695 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
f6d8942b 696 o->a_prefix, path);
fd47ae6a 697 } else {
c972bf4c 698 strvec_pushf(&cp.args, "--src-prefix=%s%s/",
f6d8942b 699 o->a_prefix, path);
c972bf4c 700 strvec_pushf(&cp.args, "--dst-prefix=%s%s/",
f6d8942b 701 o->b_prefix, path);
fd47ae6a 702 }
c972bf4c 703 strvec_push(&cp.args, oid_to_hex(old_oid));
fd47ae6a
JK
704 /*
705 * If the submodule has modified content, we will diff against the
706 * work tree, under the assumption that the user has asked for the
707 * diff format and wishes to actually see all differences even if they
708 * haven't yet been committed to the submodule yet.
709 */
710 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED))
c972bf4c 711 strvec_push(&cp.args, oid_to_hex(new_oid));
fd47ae6a 712
29fda24d 713 prepare_submodule_repo_env(&cp.env);
f1c0368d
DT
714
715 if (!is_directory(path)) {
716 /* fall back to absorbed git dir, if any */
717 if (!sub)
718 goto done;
719 cp.dir = sub->gitdir;
29fda24d
ÆAB
720 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=.");
721 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=.");
f1c0368d
DT
722 }
723
67f61efb 724 if (start_command(&cp)) {
f3597138 725 diff_emit_submodule_error(o, "(diff failed)\n");
67f61efb
DT
726 goto done;
727 }
f3597138
SB
728
729 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF)
730 diff_emit_submodule_pipethrough(o, sb.buf, sb.len);
731
732 if (finish_command(&cp))
733 diff_emit_submodule_error(o, "(diff failed)\n");
fd47ae6a
JK
734
735done:
f3597138 736 strbuf_release(&sb);
bf20fe4c 737 free_commit_list(merge_bases);
fd47ae6a
JK
738 if (left)
739 clear_commit_marks(left, ~0);
740 if (right)
741 clear_commit_marks(right, ~0);
605f0ec1
SB
742 if (sub) {
743 repo_clear(sub);
744 free(sub);
745 }
fd47ae6a
JK
746}
747
84f8925e
SB
748int should_update_submodules(void)
749{
750 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON;
751}
752
753const struct submodule *submodule_from_ce(const struct cache_entry *ce)
754{
755 if (!S_ISGITLINK(ce->ce_mode))
756 return NULL;
757
758 if (!should_update_submodules())
759 return NULL;
760
14228447 761 return submodule_from_path(the_repository, null_oid(), ce->name);
84f8925e
SB
762}
763
aacc5c1a 764
c68f8375 765struct collect_changed_submodules_cb_data {
6245b98b 766 struct repository *repo;
c68f8375
HV
767 struct string_list *changed;
768 const struct object_id *commit_oid;
769};
770
771/*
772 * this would normally be two functions: default_name_from_path() and
773 * path_from_default_name(). Since the default name is the same as
774 * the submodule path we can get away with just one function which only
775 * checks whether there is a submodule in the working directory at that
776 * location.
777 */
778static const char *default_name_or_path(const char *path_or_name)
779{
780 int error_code;
781
782 if (!is_submodule_populated_gently(path_or_name, &error_code))
783 return NULL;
784
785 return path_or_name;
786}
787
6e1e0c99
GC
788/*
789 * Holds relevant information for a changed submodule. Used as the .util
b90d9f76
GC
790 * member of the changed submodule name string_list_item.
791 *
792 * (super_oid, path) allows the submodule config to be read from _some_
793 * .gitmodules file. We store this information the first time we find a
794 * superproject commit that points to the submodule, but this is
795 * arbitrary - we can choose any (super_oid, path) that matches the
796 * submodule's name.
797 *
798 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't
799 * guarantee that we're reading the commit that the user would expect. A better
800 * scheme would be to just fetch a submodule by its name. This requires two
801 * steps:
802 * - Create a function that behaves like repo_submodule_init(), but accepts a
803 * submodule name instead of treeish_name and path. This should be easy
804 * because repo_submodule_init() internally uses the submodule's name.
805 *
806 * - Replace most instances of 'struct submodule' (which is the .gitmodules
807 * config) with just the submodule name. This is OK because we expect
808 * submodule settings to be stored in .git/config (via "git submodule init"),
809 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(),
810 * which constructs a bogus 'struct submodule' for the sake of giving a
811 * placeholder name to a gitlink.
6e1e0c99
GC
812 */
813struct changed_submodule_data {
b90d9f76
GC
814 /*
815 * The first superproject commit in the rev walk that points to
816 * the submodule.
817 */
818 const struct object_id *super_oid;
819 /*
820 * Path to the submodule in the superproject commit referenced
821 * by 'super_oid'.
822 */
823 char *path;
6e1e0c99
GC
824 /* The submodule commits that have changed in the rev walk. */
825 struct oid_array new_commits;
826};
827
828static void changed_submodule_data_clear(struct changed_submodule_data *cs_data)
829{
830 oid_array_clear(&cs_data->new_commits);
b90d9f76 831 free(cs_data->path);
6e1e0c99
GC
832}
833
aacc5c1a
BW
834static void collect_changed_submodules_cb(struct diff_queue_struct *q,
835 struct diff_options *options,
836 void *data)
837{
c68f8375
HV
838 struct collect_changed_submodules_cb_data *me = data;
839 struct string_list *changed = me->changed;
840 const struct object_id *commit_oid = me->commit_oid;
aacc5c1a 841 int i;
aacc5c1a
BW
842
843 for (i = 0; i < q->nr; i++) {
844 struct diff_filepair *p = q->queue[i];
c68f8375
HV
845 const struct submodule *submodule;
846 const char *name;
1e5dd3a1 847 struct string_list_item *item;
6e1e0c99 848 struct changed_submodule_data *cs_data;
c68f8375 849
aacc5c1a
BW
850 if (!S_ISGITLINK(p->two->mode))
851 continue;
852
6245b98b 853 submodule = submodule_from_path(me->repo,
3b8fb393 854 commit_oid, p->two->path);
c68f8375
HV
855 if (submodule)
856 name = submodule->name;
857 else {
858 name = default_name_or_path(p->two->path);
859 /* make sure name does not collide with existing one */
5fc84755 860 if (name)
6245b98b 861 submodule = submodule_from_name(me->repo,
5fc84755 862 commit_oid, name);
c68f8375 863 if (submodule) {
a4ffbbbb 864 warning(_("Submodule in commit %s at path: "
c68f8375 865 "'%s' collides with a submodule named "
a4ffbbbb 866 "the same. Skipping it."),
5fc84755 867 oid_to_hex(commit_oid), p->two->path);
c68f8375
HV
868 name = NULL;
869 }
aacc5c1a 870 }
c68f8375
HV
871
872 if (!name)
873 continue;
874
1e5dd3a1 875 item = string_list_insert(changed, name);
b90d9f76
GC
876 if (item->util)
877 cs_data = item->util;
878 else {
6e1e0c99 879 item->util = xcalloc(1, sizeof(struct changed_submodule_data));
b90d9f76
GC
880 cs_data = item->util;
881 cs_data->super_oid = commit_oid;
882 cs_data->path = xstrdup(p->two->path);
883 }
6e1e0c99 884 oid_array_append(&cs_data->new_commits, &p->two->oid);
aacc5c1a
BW
885 }
886}
887
888/*
889 * Collect the paths of submodules in 'changed' which have changed based on
890 * the revisions as specified in 'argv'. Each entry in 'changed' will also
891 * have a corresponding 'struct oid_array' (in the 'util' field) which lists
892 * what the submodule pointers were updated to during the change.
893 */
6245b98b 894static void collect_changed_submodules(struct repository *r,
174d131f 895 struct string_list *changed,
c972bf4c 896 struct strvec *argv)
aacc5c1a
BW
897{
898 struct rev_info rev;
899 const struct commit *commit;
a462bee5
OS
900 int save_warning;
901 struct setup_revision_opt s_r_opt = {
902 .assume_dashdash = 1,
903 };
aacc5c1a 904
a462bee5
OS
905 save_warning = warn_on_object_refname_ambiguity;
906 warn_on_object_refname_ambiguity = 0;
6245b98b 907 repo_init_revisions(r, &rev, NULL);
a462bee5
OS
908 setup_revisions(argv->nr, argv->v, &rev, &s_r_opt);
909 warn_on_object_refname_ambiguity = save_warning;
aacc5c1a 910 if (prepare_revision_walk(&rev))
a4ffbbbb 911 die(_("revision walk setup failed"));
aacc5c1a
BW
912
913 while ((commit = get_revision(&rev))) {
914 struct rev_info diff_rev;
c68f8375 915 struct collect_changed_submodules_cb_data data;
6245b98b 916 data.repo = r;
c68f8375
HV
917 data.changed = changed;
918 data.commit_oid = &commit->object.oid;
aacc5c1a 919
6245b98b 920 repo_init_revisions(r, &diff_rev, NULL);
aacc5c1a
BW
921 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK;
922 diff_rev.diffopt.format_callback = collect_changed_submodules_cb;
c68f8375 923 diff_rev.diffopt.format_callback_data = &data;
d01141de
SO
924 diff_rev.dense_combined_merges = 1;
925 diff_tree_combined_merge(commit, &diff_rev);
2108fe4a 926 release_revisions(&diff_rev);
aacc5c1a
BW
927 }
928
929 reset_revision_walk();
2108fe4a 930 release_revisions(&rev);
aacc5c1a
BW
931}
932
6e1e0c99 933static void free_submodules_data(struct string_list *submodules)
aacc5c1a
BW
934{
935 struct string_list_item *item;
936 for_each_string_list_item(item, submodules)
6e1e0c99
GC
937 changed_submodule_data_clear(item->util);
938
aacc5c1a
BW
939 string_list_clear(submodules, 1);
940}
941
5cf88fd8
ÆAB
942static int has_remote(const char *refname UNUSED,
943 const struct object_id *oid UNUSED,
944 int flags UNUSED, void *cb_data UNUSED)
d2b17b32
FG
945{
946 return 1;
947}
948
1b7ba794 949static int append_oid_to_argv(const struct object_id *oid, void *data)
d2b17b32 950{
c972bf4c
JK
951 struct strvec *argv = data;
952 strvec_push(argv, oid_to_hex(oid));
9cfa1c26
HV
953 return 0;
954}
955
3c96aa97 956struct has_commit_data {
6245b98b 957 struct repository *repo;
3c96aa97
SB
958 int result;
959 const char *path;
7c2f8cc5 960 const struct object_id *super_oid;
3c96aa97
SB
961};
962
1b7ba794 963static int check_has_commit(const struct object_id *oid, void *data)
d2b17b32 964{
3c96aa97 965 struct has_commit_data *cb = data;
13a2f620
JT
966 struct repository subrepo;
967 enum object_type type;
5b6607d2 968
7c2f8cc5 969 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) {
13a2f620 970 cb->result = 0;
5fff35d8
GC
971 /* subrepo failed to init, so don't clean it up. */
972 return 0;
13a2f620
JT
973 }
974
975 type = oid_object_info(&subrepo, oid, NULL);
5b6607d2 976
3c96aa97
SB
977 switch (type) {
978 case OBJ_COMMIT:
13a2f620 979 goto cleanup;
3c96aa97
SB
980 case OBJ_BAD:
981 /*
982 * Object is missing or invalid. If invalid, an error message
983 * has already been printed.
984 */
985 cb->result = 0;
13a2f620 986 goto cleanup;
3c96aa97
SB
987 default:
988 die(_("submodule entry '%s' (%s) is a %s, not a commit"),
debca9d2 989 cb->path, oid_to_hex(oid), type_name(type));
3c96aa97 990 }
13a2f620
JT
991cleanup:
992 repo_clear(&subrepo);
993 return 0;
5b6607d2
HV
994}
995
6245b98b
NTND
996static int submodule_has_commits(struct repository *r,
997 const char *path,
7c2f8cc5 998 const struct object_id *super_oid,
6245b98b 999 struct oid_array *commits)
5b6607d2 1000{
7c2f8cc5
GC
1001 struct has_commit_data has_commit = {
1002 .repo = r,
1003 .result = 1,
1004 .path = path,
1005 .super_oid = super_oid
1006 };
5b6607d2 1007
910650d2 1008 oid_array_for_each_unique(commits, check_has_commit, &has_commit);
7c8d2b00 1009
3c96aa97 1010 if (has_commit.result) {
7c8d2b00
BW
1011 /*
1012 * Even if the submodule is checked out and the commit is
1013 * present, make sure it exists in the submodule's object store
1014 * and that it is reachable from a ref.
1015 */
1016 struct child_process cp = CHILD_PROCESS_INIT;
1017 struct strbuf out = STRBUF_INIT;
1018
c972bf4c 1019 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL);
7c8d2b00 1020 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
c972bf4c 1021 strvec_pushl(&cp.args, "--not", "--all", NULL);
7c8d2b00 1022
29fda24d 1023 prepare_submodule_repo_env(&cp.env);
7c8d2b00
BW
1024 cp.git_cmd = 1;
1025 cp.no_stdin = 1;
1026 cp.dir = path;
1027
1028 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len)
3c96aa97 1029 has_commit.result = 0;
7c8d2b00
BW
1030
1031 strbuf_release(&out);
1032 }
1033
3c96aa97 1034 return has_commit.result;
5b6607d2
HV
1035}
1036
6245b98b
NTND
1037static int submodule_needs_pushing(struct repository *r,
1038 const char *path,
1039 struct oid_array *commits)
5b6607d2 1040{
7c2f8cc5 1041 if (!submodule_has_commits(r, path, null_oid(), commits))
250ab24a
HV
1042 /*
1043 * NOTE: We do consider it safe to return "no" here. The
1044 * correct answer would be "We do not know" instead of
1045 * "No push needed", but it is quite hard to change
1046 * the submodule pointer without having the submodule
1047 * around. If a user did however change the submodules
1048 * without having the submodule around, this indicates
1049 * an expert who knows what they are doing or a
1050 * maintainer integrating work from other people. In
1051 * both cases it should be safe to skip this check.
1052 */
d2b17b32
FG
1053 return 0;
1054
1055 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 1056 struct child_process cp = CHILD_PROCESS_INIT;
d2b17b32
FG
1057 struct strbuf buf = STRBUF_INIT;
1058 int needs_pushing = 0;
1059
c972bf4c 1060 strvec_push(&cp.args, "rev-list");
910650d2 1061 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args);
c972bf4c 1062 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL);
5b6607d2 1063
29fda24d 1064 prepare_submodule_repo_env(&cp.env);
d2b17b32
FG
1065 cp.git_cmd = 1;
1066 cp.no_stdin = 1;
1067 cp.out = -1;
1068 cp.dir = path;
1069 if (start_command(&cp))
a4ffbbbb 1070 die(_("Could not run 'git rev-list <commits> --not --remotes -n 1' command in submodule %s"),
5b6607d2 1071 path);
db1ba2a2 1072 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1))
d2b17b32
FG
1073 needs_pushing = 1;
1074 finish_command(&cp);
1075 close(cp.out);
1076 strbuf_release(&buf);
1077 return needs_pushing;
1078 }
1079
1080 return 0;
1081}
1082
6245b98b 1083int find_unpushed_submodules(struct repository *r,
174d131f
NTND
1084 struct oid_array *commits,
1085 const char *remotes_name,
1086 struct string_list *needs_pushing)
d2b17b32 1087{
14739447 1088 struct string_list submodules = STRING_LIST_INIT_DUP;
c68f8375 1089 struct string_list_item *name;
c972bf4c 1090 struct strvec argv = STRVEC_INIT;
a762e51e 1091
d70a9eb6 1092 /* argv.v[0] will be ignored by setup_revisions */
c972bf4c 1093 strvec_push(&argv, "find_unpushed_submodules");
910650d2 1094 oid_array_for_each_unique(commits, append_oid_to_argv, &argv);
c972bf4c
JK
1095 strvec_push(&argv, "--not");
1096 strvec_pushf(&argv, "--remotes=%s", remotes_name);
9cfa1c26 1097
6245b98b 1098 collect_changed_submodules(r, &submodules, &argv);
d2b17b32 1099
c68f8375 1100 for_each_string_list_item(name, &submodules) {
6e1e0c99 1101 struct changed_submodule_data *cs_data = name->util;
c68f8375
HV
1102 const struct submodule *submodule;
1103 const char *path = NULL;
1104
14228447 1105 submodule = submodule_from_name(r, null_oid(), name->string);
c68f8375
HV
1106 if (submodule)
1107 path = submodule->path;
1108 else
1109 path = default_name_or_path(name->string);
1110
1111 if (!path)
1112 continue;
5b6607d2 1113
6e1e0c99 1114 if (submodule_needs_pushing(r, path, &cs_data->new_commits))
aacc5c1a 1115 string_list_insert(needs_pushing, path);
14739447 1116 }
610b2337 1117
6e1e0c99 1118 free_submodules_data(&submodules);
c972bf4c 1119 strvec_clear(&argv);
d2b17b32 1120
a762e51e 1121 return needs_pushing->nr;
d2b17b32
FG
1122}
1123
2a90556d 1124static int push_submodule(const char *path,
06bf4ad1 1125 const struct remote *remote,
60fba4bf 1126 const struct refspec *rs,
2a90556d
BW
1127 const struct string_list *push_options,
1128 int dry_run)
eb21c732 1129{
eb21c732 1130 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
d3180279 1131 struct child_process cp = CHILD_PROCESS_INIT;
c972bf4c 1132 strvec_push(&cp.args, "push");
0301c821 1133 if (dry_run)
c972bf4c 1134 strvec_push(&cp.args, "--dry-run");
eb21c732 1135
2a90556d
BW
1136 if (push_options && push_options->nr) {
1137 const struct string_list_item *item;
1138 for_each_string_list_item(item, push_options)
c972bf4c 1139 strvec_pushf(&cp.args, "--push-option=%s",
f6d8942b 1140 item->string);
2a90556d 1141 }
06bf4ad1
BW
1142
1143 if (remote->origin != REMOTE_UNCONFIGURED) {
1144 int i;
c972bf4c 1145 strvec_push(&cp.args, remote->name);
60fba4bf 1146 for (i = 0; i < rs->raw_nr; i++)
c972bf4c 1147 strvec_push(&cp.args, rs->raw[i]);
06bf4ad1
BW
1148 }
1149
29fda24d 1150 prepare_submodule_repo_env(&cp.env);
eb21c732
HV
1151 cp.git_cmd = 1;
1152 cp.no_stdin = 1;
1153 cp.dir = path;
1154 if (run_command(&cp))
1155 return 0;
1156 close(cp.out);
1157 }
1158
1159 return 1;
1160}
1161
06bf4ad1
BW
1162/*
1163 * Perform a check in the submodule to see if the remote and refspec work.
1164 * Die if the submodule can't be pushed.
1165 */
c7be7201
BW
1166static void submodule_push_check(const char *path, const char *head,
1167 const struct remote *remote,
60fba4bf 1168 const struct refspec *rs)
06bf4ad1
BW
1169{
1170 struct child_process cp = CHILD_PROCESS_INIT;
1171 int i;
1172
c972bf4c
JK
1173 strvec_push(&cp.args, "submodule--helper");
1174 strvec_push(&cp.args, "push-check");
1175 strvec_push(&cp.args, head);
1176 strvec_push(&cp.args, remote->name);
06bf4ad1 1177
60fba4bf 1178 for (i = 0; i < rs->raw_nr; i++)
c972bf4c 1179 strvec_push(&cp.args, rs->raw[i]);
06bf4ad1 1180
29fda24d 1181 prepare_submodule_repo_env(&cp.env);
06bf4ad1
BW
1182 cp.git_cmd = 1;
1183 cp.no_stdin = 1;
1184 cp.no_stdout = 1;
1185 cp.dir = path;
1186
1187 /*
1188 * Simply indicate if 'submodule--helper push-check' failed.
1189 * More detailed error information will be provided by the
1190 * child process.
1191 */
1192 if (run_command(&cp))
a4ffbbbb 1193 die(_("process for submodule '%s' failed"), path);
06bf4ad1
BW
1194}
1195
6245b98b 1196int push_unpushed_submodules(struct repository *r,
174d131f 1197 struct oid_array *commits,
06bf4ad1 1198 const struct remote *remote,
60fba4bf 1199 const struct refspec *rs,
2a90556d 1200 const struct string_list *push_options,
0301c821 1201 int dry_run)
eb21c732
HV
1202{
1203 int i, ret = 1;
f93d7c6f 1204 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
eb21c732 1205
6245b98b 1206 if (!find_unpushed_submodules(r, commits,
174d131f 1207 remote->name, &needs_pushing))
eb21c732
HV
1208 return 1;
1209
06bf4ad1
BW
1210 /*
1211 * Verify that the remote and refspec can be propagated to all
1212 * submodules. This check can be skipped if the remote and refspec
1213 * won't be propagated due to the remote being unconfigured (e.g. a URL
1214 * instead of a remote name).
1215 */
c7be7201
BW
1216 if (remote->origin != REMOTE_UNCONFIGURED) {
1217 char *head;
1218 struct object_id head_oid;
1219
0f2dc722 1220 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
c7be7201
BW
1221 if (!head)
1222 die(_("Failed to resolve HEAD as a valid ref."));
1223
06bf4ad1
BW
1224 for (i = 0; i < needs_pushing.nr; i++)
1225 submodule_push_check(needs_pushing.items[i].string,
60fba4bf 1226 head, remote, rs);
c7be7201
BW
1227 free(head);
1228 }
06bf4ad1
BW
1229
1230 /* Actually push the submodules */
eb21c732
HV
1231 for (i = 0; i < needs_pushing.nr; i++) {
1232 const char *path = needs_pushing.items[i].string;
a4ffbbbb 1233 fprintf(stderr, _("Pushing submodule '%s'\n"), path);
60fba4bf 1234 if (!push_submodule(path, remote, rs,
06bf4ad1 1235 push_options, dry_run)) {
a4ffbbbb 1236 fprintf(stderr, _("Unable to push submodule '%s'\n"), path);
eb21c732
HV
1237 ret = 0;
1238 }
1239 }
1240
1241 string_list_clear(&needs_pushing, 0);
1242
1243 return ret;
1244}
1245
5cf88fd8 1246static int append_oid_to_array(const char *ref UNUSED,
63e14ee2 1247 const struct object_id *oid,
5cf88fd8 1248 int flags UNUSED, void *data)
6859de45 1249{
419fd786
BW
1250 struct oid_array *array = data;
1251 oid_array_append(array, oid);
6859de45
JK
1252 return 0;
1253}
1254
2eb80bcd 1255void check_for_new_submodule_commits(struct object_id *oid)
6859de45
JK
1256{
1257 if (!initialized_fetch_ref_tips) {
419fd786 1258 for_each_ref(append_oid_to_array, &ref_tips_before_fetch);
6859de45
JK
1259 initialized_fetch_ref_tips = 1;
1260 }
1261
910650d2 1262 oid_array_append(&ref_tips_after_fetch, oid);
6859de45
JK
1263}
1264
b90d9f76
GC
1265/*
1266 * Returns 1 if there is at least one submodule gitdir in
1267 * $GIT_DIR/modules and 0 otherwise. This follows
1268 * submodule_name_to_gitdir(), which looks for submodules in
1269 * $GIT_DIR/modules, not $GIT_COMMON_DIR.
1270 *
1271 * A submodule can be moved to $GIT_DIR/modules manually by running "git
1272 * submodule absorbgitdirs", or it may be initialized there by "git
1273 * submodule update".
1274 */
1275static int repo_has_absorbed_submodules(struct repository *r)
1276{
1277 int ret;
1278 struct strbuf buf = STRBUF_INIT;
1279
1280 strbuf_repo_git_path(&buf, r, "modules/");
1281 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf);
1282 strbuf_release(&buf);
1283 return ret;
1284}
1285
16dd6fe1
SB
1286static void calculate_changed_submodule_paths(struct repository *r,
1287 struct string_list *changed_submodule_names)
88a21979 1288{
c972bf4c 1289 struct strvec argv = STRVEC_INIT;
bcd73372 1290 struct string_list_item *name;
88a21979 1291
b90d9f76
GC
1292 /* No need to check if no submodules would be fetched */
1293 if (!submodule_from_path(r, NULL, NULL) &&
1294 !repo_has_absorbed_submodules(r))
18322bad
JL
1295 return;
1296
c972bf4c 1297 strvec_push(&argv, "--"); /* argv[0] program name */
910650d2 1298 oid_array_for_each_unique(&ref_tips_after_fetch,
d1a8460c 1299 append_oid_to_argv, &argv);
c972bf4c 1300 strvec_push(&argv, "--not");
910650d2 1301 oid_array_for_each_unique(&ref_tips_before_fetch,
d1a8460c 1302 append_oid_to_argv, &argv);
88a21979
JL
1303
1304 /*
1305 * Collect all submodules (whether checked out or not) for which new
c68f8375 1306 * commits have been recorded upstream in "changed_submodule_names".
88a21979 1307 */
bcd73372 1308 collect_changed_submodules(r, changed_submodule_names, &argv);
aacc5c1a 1309
bcd73372 1310 for_each_string_list_item(name, changed_submodule_names) {
6e1e0c99 1311 struct changed_submodule_data *cs_data = name->util;
c68f8375
HV
1312 const struct submodule *submodule;
1313 const char *path = NULL;
1314
14228447 1315 submodule = submodule_from_name(r, null_oid(), name->string);
c68f8375
HV
1316 if (submodule)
1317 path = submodule->path;
1318 else
1319 path = default_name_or_path(name->string);
1320
1321 if (!path)
1322 continue;
aacc5c1a 1323
6e1e0c99
GC
1324 if (submodule_has_commits(r, path, null_oid(), &cs_data->new_commits)) {
1325 changed_submodule_data_clear(cs_data);
bcd73372
SB
1326 *name->string = '\0';
1327 }
88a21979 1328 }
6859de45 1329
bcd73372
SB
1330 string_list_remove_empty_items(changed_submodule_names, 1);
1331
c972bf4c 1332 strvec_clear(&argv);
910650d2 1333 oid_array_clear(&ref_tips_before_fetch);
1334 oid_array_clear(&ref_tips_after_fetch);
6859de45 1335 initialized_fetch_ref_tips = 0;
88a21979
JL
1336}
1337
6245b98b 1338int submodule_touches_in_range(struct repository *r,
174d131f 1339 struct object_id *excl_oid,
a6d7eb2c
SB
1340 struct object_id *incl_oid)
1341{
1342 struct string_list subs = STRING_LIST_INIT_DUP;
c972bf4c 1343 struct strvec args = STRVEC_INIT;
a6d7eb2c
SB
1344 int ret;
1345
a6d7eb2c 1346 /* No need to check if there are no submodules configured */
6245b98b 1347 if (!submodule_from_path(r, NULL, NULL))
a6d7eb2c
SB
1348 return 0;
1349
c972bf4c
JK
1350 strvec_push(&args, "--"); /* args[0] program name */
1351 strvec_push(&args, oid_to_hex(incl_oid));
4d36f88b 1352 if (!is_null_oid(excl_oid)) {
c972bf4c
JK
1353 strvec_push(&args, "--not");
1354 strvec_push(&args, oid_to_hex(excl_oid));
4d36f88b 1355 }
a6d7eb2c 1356
6245b98b 1357 collect_changed_submodules(r, &subs, &args);
a6d7eb2c
SB
1358 ret = subs.nr;
1359
c972bf4c 1360 strvec_clear(&args);
a6d7eb2c 1361
6e1e0c99 1362 free_submodules_data(&subs);
a6d7eb2c
SB
1363 return ret;
1364}
1365
fe85ee6e 1366struct submodule_parallel_fetch {
b90d9f76
GC
1367 /*
1368 * The index of the last index entry processed by
1369 * get_fetch_task_from_index().
1370 */
1371 int index_count;
1372 /*
1373 * The index of the last string_list entry processed by
1374 * get_fetch_task_from_changed().
1375 */
1376 int changed_count;
c972bf4c 1377 struct strvec args;
e724197f 1378 struct repository *r;
fe85ee6e
SB
1379 const char *prefix;
1380 int command_line_option;
8fa29159 1381 int default_option;
fe85ee6e
SB
1382 int quiet;
1383 int result;
16dd6fe1 1384
b90d9f76
GC
1385 /*
1386 * Names of submodules that have new commits. Generated by
1387 * walking the newly fetched superproject commits.
1388 */
16dd6fe1 1389 struct string_list changed_submodule_names;
b90d9f76
GC
1390 /*
1391 * Names of submodules that have already been processed. Lets us
1392 * avoid fetching the same submodule more than once.
1393 */
1394 struct string_list seen_submodule_names;
be76c212
SB
1395
1396 /* Pending fetches by OIDs */
1397 struct fetch_task **oid_fetch_tasks;
1398 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc;
02225408
ES
1399
1400 struct strbuf submodules_with_errors;
fe85ee6e 1401};
f69a6e4f
ÆAB
1402#define SPF_INIT { \
1403 .args = STRVEC_INIT, \
1404 .changed_submodule_names = STRING_LIST_INIT_DUP, \
b90d9f76 1405 .seen_submodule_names = STRING_LIST_INIT_DUP, \
f69a6e4f
ÆAB
1406 .submodules_with_errors = STRBUF_INIT, \
1407}
fe85ee6e 1408
4b4acedd
HV
1409static int get_fetch_recurse_config(const struct submodule *submodule,
1410 struct submodule_parallel_fetch *spf)
1411{
1412 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT)
1413 return spf->command_line_option;
1414
1415 if (submodule) {
1416 char *key;
1417 const char *value;
1418
1419 int fetch_recurse = submodule->fetch_recurse;
1420 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name);
f1de981e 1421 if (!repo_config_get_string_tmp(spf->r, key, &value)) {
4b4acedd
HV
1422 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value);
1423 }
1424 free(key);
1425
1426 if (fetch_recurse != RECURSE_SUBMODULES_NONE)
1427 /* local config overrules everything except commandline */
1428 return fetch_recurse;
1429 }
1430
1431 return spf->default_option;
1432}
1433
be76c212
SB
1434/*
1435 * Fetch in progress (if callback data) or
1436 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch)
1437 */
1438struct fetch_task {
1439 struct repository *repo;
1440 const struct submodule *sub;
1441 unsigned free_sub : 1; /* Do we need to free the submodule? */
73bc90d7 1442 const char *default_argv; /* The default fetch mode. */
b90d9f76 1443 struct strvec git_args; /* Args for the child git process. */
be76c212
SB
1444
1445 struct oid_array *commits; /* Ensure these commits are fetched */
1446};
1447
1448/**
1449 * When a submodule is not defined in .gitmodules, we cannot access it
1450 * via the regular submodule-config. Create a fake submodule, which we can
1451 * work on.
1452 */
1453static const struct submodule *get_non_gitmodules_submodule(const char *path)
1454{
1455 struct submodule *ret = NULL;
1456 const char *name = default_name_or_path(path);
1457
1458 if (!name)
1459 return NULL;
1460
1461 ret = xmalloc(sizeof(*ret));
1462 memset(ret, 0, sizeof(*ret));
1463 ret->path = name;
1464 ret->name = name;
1465
1466 return (const struct submodule *) ret;
1467}
1468
be76c212
SB
1469static void fetch_task_release(struct fetch_task *p)
1470{
1471 if (p->free_sub)
1472 free((void*)p->sub);
1473 p->free_sub = 0;
1474 p->sub = NULL;
1475
1476 if (p->repo)
1477 repo_clear(p->repo);
1478 FREE_AND_NULL(p->repo);
b90d9f76
GC
1479
1480 strvec_clear(&p->git_args);
be76c212
SB
1481}
1482
26f80ccf 1483static struct repository *get_submodule_repo_for(struct repository *r,
7c2f8cc5
GC
1484 const char *path,
1485 const struct object_id *treeish_name)
26f80ccf
SB
1486{
1487 struct repository *ret = xmalloc(sizeof(*ret));
1488
7c2f8cc5 1489 if (repo_submodule_init(ret, r, path, treeish_name)) {
5df5106e
JT
1490 free(ret);
1491 return NULL;
26f80ccf
SB
1492 }
1493
1494 return ret;
1495}
1496
5370b91f
GC
1497static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf,
1498 const char *path,
1499 const struct object_id *treeish_name)
1500{
1501 struct fetch_task *task = xmalloc(sizeof(*task));
1502 memset(task, 0, sizeof(*task));
1503
1504 task->sub = submodule_from_path(spf->r, treeish_name, path);
1505
1506 if (!task->sub) {
1507 /*
1508 * No entry in .gitmodules? Technically not a submodule,
1509 * but historically we supported repositories that happen to be
1510 * in-place where a gitlink is. Keep supporting them.
1511 */
1512 task->sub = get_non_gitmodules_submodule(path);
1513 if (!task->sub)
1514 goto cleanup;
1515
1516 task->free_sub = 1;
1517 }
1518
b90d9f76
GC
1519 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name))
1520 goto cleanup;
1521
5370b91f
GC
1522 switch (get_fetch_recurse_config(task->sub, spf))
1523 {
1524 default:
1525 case RECURSE_SUBMODULES_DEFAULT:
1526 case RECURSE_SUBMODULES_ON_DEMAND:
1527 if (!task->sub ||
1528 !string_list_lookup(
1529 &spf->changed_submodule_names,
1530 task->sub->name))
1531 goto cleanup;
1532 task->default_argv = "on-demand";
1533 break;
1534 case RECURSE_SUBMODULES_ON:
1535 task->default_argv = "yes";
1536 break;
1537 case RECURSE_SUBMODULES_OFF:
1538 goto cleanup;
1539 }
1540
1541 task->repo = get_submodule_repo_for(spf->r, path, treeish_name);
1542
1543 return task;
1544
1545 cleanup:
1546 fetch_task_release(task);
1547 free(task);
1548 return NULL;
1549}
1550
73bc90d7 1551static struct fetch_task *
b90d9f76
GC
1552get_fetch_task_from_index(struct submodule_parallel_fetch *spf,
1553 struct strbuf *err)
7dce19d3 1554{
b90d9f76
GC
1555 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) {
1556 const struct cache_entry *ce =
1557 spf->r->index->cache[spf->index_count];
be76c212 1558 struct fetch_task *task;
7dce19d3
JL
1559
1560 if (!S_ISGITLINK(ce->ce_mode))
1561 continue;
1562
5370b91f 1563 task = fetch_task_create(spf, ce->name, null_oid());
be76c212
SB
1564 if (!task)
1565 continue;
492c6c46 1566
be76c212 1567 if (task->repo) {
fe85ee6e 1568 if (!spf->quiet)
a4ffbbbb 1569 strbuf_addf(err, _("Fetching submodule %s%s\n"),
fe85ee6e 1570 spf->prefix, ce->name);
26f80ccf 1571
b90d9f76 1572 spf->index_count++;
73bc90d7 1573 return task;
26f80ccf 1574 } else {
505a2765 1575 struct strbuf empty_submodule_path = STRBUF_INIT;
be76c212
SB
1576
1577 fetch_task_release(task);
1578 free(task);
1579
26f80ccf
SB
1580 /*
1581 * An empty directory is normal,
1582 * the submodule is not initialized
1583 */
505a2765
PK
1584 strbuf_addf(&empty_submodule_path, "%s/%s/",
1585 spf->r->worktree,
1586 ce->name);
26f80ccf 1587 if (S_ISGITLINK(ce->ce_mode) &&
505a2765 1588 !is_empty_dir(empty_submodule_path.buf)) {
26f80ccf
SB
1589 spf->result = 1;
1590 strbuf_addf(err,
303b3c1c 1591 _("Could not access submodule '%s'\n"),
26f80ccf
SB
1592 ce->name);
1593 }
505a2765 1594 strbuf_release(&empty_submodule_path);
fe85ee6e 1595 }
7dce19d3 1596 }
73bc90d7
GC
1597 return NULL;
1598}
1599
b90d9f76
GC
1600static struct fetch_task *
1601get_fetch_task_from_changed(struct submodule_parallel_fetch *spf,
1602 struct strbuf *err)
1603{
1604 for (; spf->changed_count < spf->changed_submodule_names.nr;
1605 spf->changed_count++) {
1606 struct string_list_item item =
1607 spf->changed_submodule_names.items[spf->changed_count];
1608 struct changed_submodule_data *cs_data = item.util;
1609 struct fetch_task *task;
1610
1611 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path))
1612 continue;
1613
1614 task = fetch_task_create(spf, cs_data->path,
1615 cs_data->super_oid);
1616 if (!task)
1617 continue;
1618
1619 if (!task->repo) {
1620 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"),
1621 cs_data->path,
1622 find_unique_abbrev(cs_data->super_oid, DEFAULT_ABBREV));
1623
1624 fetch_task_release(task);
1625 free(task);
1626 continue;
1627 }
1628
1629 if (!spf->quiet)
1630 strbuf_addf(err,
1631 _("Fetching submodule %s%s at commit %s\n"),
1632 spf->prefix, task->sub->path,
1633 find_unique_abbrev(cs_data->super_oid,
1634 DEFAULT_ABBREV));
1635
1636 spf->changed_count++;
1637 /*
1638 * NEEDSWORK: Submodules set/unset a value for
1639 * core.worktree when they are populated/unpopulated by
1640 * "git checkout" (and similar commands, see
1641 * submodule_move_head() and
1642 * connect_work_tree_and_git_dir()), but if the
1643 * submodule is unpopulated in another way (e.g. "git
1644 * rm", "rm -r"), core.worktree will still be set even
1645 * though the directory doesn't exist, and the child
1646 * process will crash while trying to chdir into the
1647 * nonexistent directory.
1648 *
1649 * In this case, we know that the submodule has no
1650 * working tree, so we can work around this by
1651 * setting "--work-tree=." (--bare does not work because
1652 * worktree settings take precedence over bare-ness).
1653 * However, this is not necessarily true in other cases,
1654 * so a generalized solution is still necessary.
1655 *
1656 * Possible solutions:
1657 * - teach "git [add|rm]" to unset core.worktree and
1658 * discourage users from removing submodules without
1659 * using a Git command.
1660 * - teach submodule child processes to ignore stale
1661 * core.worktree values.
1662 */
1663 strvec_push(&task->git_args, "--work-tree=.");
1664 return task;
1665 }
1666 return NULL;
1667}
1668
73bc90d7
GC
1669static int get_next_submodule(struct child_process *cp, struct strbuf *err,
1670 void *data, void **task_cb)
1671{
1672 struct submodule_parallel_fetch *spf = data;
b90d9f76
GC
1673 struct fetch_task *task =
1674 get_fetch_task_from_index(spf, err);
1675 if (!task)
1676 task = get_fetch_task_from_changed(spf, err);
73bc90d7
GC
1677
1678 if (task) {
1679 struct strbuf submodule_prefix = STRBUF_INIT;
1680
1681 child_process_init(cp);
1682 cp->dir = task->repo->gitdir;
29fda24d 1683 prepare_submodule_repo_env_in_gitdir(&cp->env);
73bc90d7
GC
1684 cp->git_cmd = 1;
1685 strvec_init(&cp->args);
b90d9f76
GC
1686 if (task->git_args.nr)
1687 strvec_pushv(&cp->args, task->git_args.v);
73bc90d7
GC
1688 strvec_pushv(&cp->args, spf->args.v);
1689 strvec_push(&cp->args, task->default_argv);
1690 strvec_push(&cp->args, "--submodule-prefix");
1691
1692 strbuf_addf(&submodule_prefix, "%s%s/",
1693 spf->prefix,
1694 task->sub->path);
1695 strvec_push(&cp->args, submodule_prefix.buf);
1696 *task_cb = task;
1697
1698 strbuf_release(&submodule_prefix);
b90d9f76 1699 string_list_insert(&spf->seen_submodule_names, task->sub->name);
73bc90d7
GC
1700 return 1;
1701 }
be76c212
SB
1702
1703 if (spf->oid_fetch_tasks_nr) {
1704 struct fetch_task *task =
1705 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1];
1706 struct strbuf submodule_prefix = STRBUF_INIT;
1707 spf->oid_fetch_tasks_nr--;
1708
1709 strbuf_addf(&submodule_prefix, "%s%s/",
1710 spf->prefix, task->sub->path);
1711
1712 child_process_init(cp);
29fda24d 1713 prepare_submodule_repo_env_in_gitdir(&cp->env);
be76c212
SB
1714 cp->git_cmd = 1;
1715 cp->dir = task->repo->gitdir;
1716
c972bf4c 1717 strvec_init(&cp->args);
d70a9eb6 1718 strvec_pushv(&cp->args, spf->args.v);
c972bf4c
JK
1719 strvec_push(&cp->args, "on-demand");
1720 strvec_push(&cp->args, "--submodule-prefix");
1721 strvec_push(&cp->args, submodule_prefix.buf);
be76c212
SB
1722
1723 /* NEEDSWORK: have get_default_remote from submodule--helper */
c972bf4c 1724 strvec_push(&cp->args, "origin");
be76c212
SB
1725 oid_array_for_each_unique(task->commits,
1726 append_oid_to_argv, &cp->args);
1727
1728 *task_cb = task;
7dce19d3 1729 strbuf_release(&submodule_prefix);
be76c212 1730 return 1;
7dce19d3 1731 }
be76c212 1732
fe85ee6e
SB
1733 return 0;
1734}
1735
2a73b3da 1736static int fetch_start_failure(struct strbuf *err,
fe85ee6e
SB
1737 void *cb, void *task_cb)
1738{
1739 struct submodule_parallel_fetch *spf = cb;
be76c212 1740 struct fetch_task *task = task_cb;
fe85ee6e
SB
1741
1742 spf->result = 1;
1743
be76c212 1744 fetch_task_release(task);
fe85ee6e
SB
1745 return 0;
1746}
1747
be76c212
SB
1748static int commit_missing_in_sub(const struct object_id *oid, void *data)
1749{
1750 struct repository *subrepo = data;
1751
1752 enum object_type type = oid_object_info(subrepo, oid, NULL);
1753
1754 return type != OBJ_COMMIT;
1755}
1756
2a73b3da
SB
1757static int fetch_finish(int retvalue, struct strbuf *err,
1758 void *cb, void *task_cb)
fe85ee6e
SB
1759{
1760 struct submodule_parallel_fetch *spf = cb;
be76c212
SB
1761 struct fetch_task *task = task_cb;
1762
1763 struct string_list_item *it;
6e1e0c99 1764 struct changed_submodule_data *cs_data;
fe85ee6e 1765
02225408
ES
1766 if (!task || !task->sub)
1767 BUG("callback cookie bogus");
1768
1769 if (retvalue) {
bd5e567d
JT
1770 /*
1771 * NEEDSWORK: This indicates that the overall fetch
1772 * failed, even though there may be a subsequent fetch
1773 * by commit hash that might work. It may be a good
1774 * idea to not indicate failure in this case, and only
1775 * indicate failure if the subsequent fetch fails.
1776 */
fe85ee6e
SB
1777 spf->result = 1;
1778
02225408
ES
1779 strbuf_addf(&spf->submodules_with_errors, "\t%s\n",
1780 task->sub->name);
1781 }
be76c212
SB
1782
1783 /* Is this the second time we process this submodule? */
1784 if (task->commits)
1785 goto out;
1786
1787 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name);
1788 if (!it)
1789 /* Could be an unchanged submodule, not contained in the list */
1790 goto out;
1791
6e1e0c99
GC
1792 cs_data = it->util;
1793 oid_array_filter(&cs_data->new_commits,
be76c212
SB
1794 commit_missing_in_sub,
1795 task->repo);
1796
1797 /* Are there commits we want, but do not exist? */
6e1e0c99
GC
1798 if (cs_data->new_commits.nr) {
1799 task->commits = &cs_data->new_commits;
be76c212
SB
1800 ALLOC_GROW(spf->oid_fetch_tasks,
1801 spf->oid_fetch_tasks_nr + 1,
1802 spf->oid_fetch_tasks_alloc);
1803 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task;
1804 spf->oid_fetch_tasks_nr++;
1805 return 0;
1806 }
1807
1808out:
1809 fetch_task_release(task);
1810
fe85ee6e
SB
1811 return 0;
1812}
1813
b90d9f76
GC
1814int fetch_submodules(struct repository *r,
1815 const struct strvec *options,
1816 const char *prefix, int command_line_option,
1817 int default_option,
1818 int quiet, int max_parallel_jobs)
fe85ee6e
SB
1819{
1820 int i;
fe85ee6e
SB
1821 struct submodule_parallel_fetch spf = SPF_INIT;
1822
e724197f 1823 spf.r = r;
fe85ee6e 1824 spf.command_line_option = command_line_option;
8fa29159 1825 spf.default_option = default_option;
fe85ee6e
SB
1826 spf.quiet = quiet;
1827 spf.prefix = prefix;
1828
e724197f 1829 if (!r->worktree)
fe85ee6e
SB
1830 goto out;
1831
e724197f 1832 if (repo_read_index(r) < 0)
a4ffbbbb 1833 die(_("index file corrupt"));
fe85ee6e 1834
c972bf4c 1835 strvec_push(&spf.args, "fetch");
d70a9eb6
JK
1836 for (i = 0; i < options->nr; i++)
1837 strvec_push(&spf.args, options->v[i]);
c972bf4c 1838 strvec_push(&spf.args, "--recurse-submodules-default");
fe85ee6e
SB
1839 /* default value, "--submodule-prefix" and its value are added later */
1840
16dd6fe1
SB
1841 calculate_changed_submodule_paths(r, &spf.changed_submodule_names);
1842 string_list_sort(&spf.changed_submodule_names);
ee4512ed
JH
1843 run_processes_parallel_tr2(max_parallel_jobs,
1844 get_next_submodule,
1845 fetch_start_failure,
1846 fetch_finish,
1847 &spf,
1848 "submodule", "parallel/fetch");
fe85ee6e 1849
02225408
ES
1850 if (spf.submodules_with_errors.len > 0)
1851 fprintf(stderr, _("Errors during submodule fetch:\n%s"),
1852 spf.submodules_with_errors.buf);
1853
1854
c972bf4c 1855 strvec_clear(&spf.args);
88a21979 1856out:
6e1e0c99 1857 free_submodules_data(&spf.changed_submodule_names);
fe85ee6e 1858 return spf.result;
7dce19d3
JL
1859}
1860
3bfc4504 1861unsigned is_submodule_modified(const char *path, int ignore_untracked)
ee6fc514 1862{
d3180279 1863 struct child_process cp = CHILD_PROCESS_INIT;
ee6fc514 1864 struct strbuf buf = STRBUF_INIT;
af6865a7 1865 FILE *fp;
c7e1a736 1866 unsigned dirty_submodule = 0;
eee49b6c 1867 const char *git_dir;
af6865a7 1868 int ignore_cp_exit_code = 0;
ee6fc514 1869
eee49b6c 1870 strbuf_addf(&buf, "%s/.git", path);
13d6ec91 1871 git_dir = read_gitfile(buf.buf);
eee49b6c
JL
1872 if (!git_dir)
1873 git_dir = buf.buf;
5c896f7c
SB
1874 if (!is_git_directory(git_dir)) {
1875 if (is_directory(git_dir))
1876 die(_("'%s' not recognized as a git repository"), git_dir);
ee6fc514
JL
1877 strbuf_release(&buf);
1878 /* The submodule is not checked out, so it is not modified */
1879 return 0;
ee6fc514
JL
1880 }
1881 strbuf_reset(&buf);
1882
c972bf4c 1883 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL);
3bfc4504 1884 if (ignore_untracked)
c972bf4c 1885 strvec_push(&cp.args, "-uno");
3bfc4504 1886
29fda24d 1887 prepare_submodule_repo_env(&cp.env);
ee6fc514
JL
1888 cp.git_cmd = 1;
1889 cp.no_stdin = 1;
1890 cp.out = -1;
eee49b6c 1891 cp.dir = path;
ee6fc514 1892 if (start_command(&cp))
a4ffbbbb 1893 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path);
ee6fc514 1894
af6865a7
SB
1895 fp = xfdopen(cp.out, "r");
1896 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
fcecf0b9
SB
1897 /* regular untracked files */
1898 if (buf.buf[0] == '?')
c7e1a736 1899 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
40069d6e
SB
1900
1901 if (buf.buf[0] == 'u' ||
1902 buf.buf[0] == '1' ||
1903 buf.buf[0] == '2') {
1904 /* T = line type, XY = status, SSSS = submodule state */
1905 if (buf.len < strlen("T XY SSSS"))
033abf97 1906 BUG("invalid status --porcelain=2 line %s",
40069d6e
SB
1907 buf.buf);
1908
1909 if (buf.buf[5] == 'S' && buf.buf[8] == 'U')
1910 /* nested untracked file */
1911 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
1912
1913 if (buf.buf[0] == 'u' ||
1914 buf.buf[0] == '2' ||
1915 memcmp(buf.buf + 5, "S..U", 4))
1916 /* other change */
1917 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED;
c7e1a736 1918 }
64f9a946
SB
1919
1920 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) &&
1921 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) ||
af6865a7
SB
1922 ignore_untracked)) {
1923 /*
1924 * We're not interested in any further information from
1925 * the child any more, neither output nor its exit code.
1926 */
1927 ignore_cp_exit_code = 1;
c7e1a736 1928 break;
af6865a7 1929 }
c7e1a736 1930 }
af6865a7 1931 fclose(fp);
ee6fc514 1932
af6865a7 1933 if (finish_command(&cp) && !ignore_cp_exit_code)
a4ffbbbb 1934 die(_("'git status --porcelain=2' failed in submodule %s"), path);
ee6fc514 1935
ee6fc514 1936 strbuf_release(&buf);
c7e1a736 1937 return dirty_submodule;
ee6fc514 1938}
68d03e4a 1939
293ab15e
JL
1940int submodule_uses_gitfile(const char *path)
1941{
d3180279 1942 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e
JL
1943 struct strbuf buf = STRBUF_INIT;
1944 const char *git_dir;
1945
1946 strbuf_addf(&buf, "%s/.git", path);
1947 git_dir = read_gitfile(buf.buf);
1948 if (!git_dir) {
1949 strbuf_release(&buf);
1950 return 0;
1951 }
1952 strbuf_release(&buf);
1953
1954 /* Now test that all nested submodules use a gitfile too */
afbdba39
JH
1955 strvec_pushl(&cp.args,
1956 "submodule", "foreach", "--quiet", "--recursive",
1957 "test -f .git", NULL);
1958
29fda24d 1959 prepare_submodule_repo_env(&cp.env);
293ab15e
JL
1960 cp.git_cmd = 1;
1961 cp.no_stdin = 1;
1962 cp.no_stderr = 1;
1963 cp.no_stdout = 1;
1964 cp.dir = path;
1965 if (run_command(&cp))
1966 return 0;
1967
1968 return 1;
1969}
1970
83b76966
SB
1971/*
1972 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data
1973 * when doing so.
1974 *
1975 * Return 1 if we'd lose data, return 0 if the removal is fine,
1976 * and negative values for errors.
1977 */
1978int bad_to_remove_submodule(const char *path, unsigned flags)
293ab15e 1979{
293ab15e 1980 ssize_t len;
d3180279 1981 struct child_process cp = CHILD_PROCESS_INIT;
293ab15e 1982 struct strbuf buf = STRBUF_INIT;
83b76966 1983 int ret = 0;
293ab15e 1984
dbe44faa 1985 if (!file_exists(path) || is_empty_dir(path))
83b76966 1986 return 0;
293ab15e
JL
1987
1988 if (!submodule_uses_gitfile(path))
83b76966 1989 return 1;
293ab15e 1990
c972bf4c 1991 strvec_pushl(&cp.args, "status", "--porcelain",
f6d8942b 1992 "--ignore-submodules=none", NULL);
83b76966
SB
1993
1994 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED)
c972bf4c 1995 strvec_push(&cp.args, "-uno");
83b76966 1996 else
c972bf4c 1997 strvec_push(&cp.args, "-uall");
83b76966
SB
1998
1999 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED))
c972bf4c 2000 strvec_push(&cp.args, "--ignored");
293ab15e 2001
29fda24d 2002 prepare_submodule_repo_env(&cp.env);
293ab15e
JL
2003 cp.git_cmd = 1;
2004 cp.no_stdin = 1;
2005 cp.out = -1;
2006 cp.dir = path;
83b76966
SB
2007 if (start_command(&cp)) {
2008 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
35ad44cb 2009 die(_("could not start 'git status' in submodule '%s'"),
83b76966
SB
2010 path);
2011 ret = -1;
2012 goto out;
2013 }
293ab15e
JL
2014
2015 len = strbuf_read(&buf, cp.out, 1024);
2016 if (len > 2)
83b76966 2017 ret = 1;
293ab15e
JL
2018 close(cp.out);
2019
83b76966
SB
2020 if (finish_command(&cp)) {
2021 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR)
35ad44cb 2022 die(_("could not run 'git status' in submodule '%s'"),
83b76966
SB
2023 path);
2024 ret = -1;
2025 }
2026out:
293ab15e 2027 strbuf_release(&buf);
83b76966 2028 return ret;
293ab15e
JL
2029}
2030
898c2e65
SB
2031void submodule_unset_core_worktree(const struct submodule *sub)
2032{
ce125d43 2033 struct strbuf config_path = STRBUF_INIT;
898c2e65 2034
ce125d43
JT
2035 submodule_name_to_gitdir(&config_path, the_repository, sub->name);
2036 strbuf_addstr(&config_path, "/config");
2037
2038 if (git_config_set_in_file_gently(config_path.buf, "core.worktree", NULL))
898c2e65
SB
2039 warning(_("Could not unset core.worktree setting in submodule '%s'"),
2040 sub->path);
2041
ce125d43 2042 strbuf_release(&config_path);
898c2e65
SB
2043}
2044
202275b9
SB
2045static const char *get_super_prefix_or_empty(void)
2046{
2047 const char *s = get_super_prefix();
2048 if (!s)
2049 s = "";
2050 return s;
2051}
2052
6e3c1595
SB
2053static int submodule_has_dirty_index(const struct submodule *sub)
2054{
2055 struct child_process cp = CHILD_PROCESS_INIT;
2056
29fda24d 2057 prepare_submodule_repo_env(&cp.env);
6e3c1595
SB
2058
2059 cp.git_cmd = 1;
c972bf4c 2060 strvec_pushl(&cp.args, "diff-index", "--quiet",
f6d8942b 2061 "--cached", "HEAD", NULL);
6e3c1595
SB
2062 cp.no_stdin = 1;
2063 cp.no_stdout = 1;
2064 cp.dir = sub->path;
2065 if (start_command(&cp))
a4ffbbbb 2066 die(_("could not recurse into submodule '%s'"), sub->path);
6e3c1595
SB
2067
2068 return finish_command(&cp);
2069}
2070
2071static void submodule_reset_index(const char *path)
2072{
2073 struct child_process cp = CHILD_PROCESS_INIT;
29fda24d 2074 prepare_submodule_repo_env(&cp.env);
6e3c1595
SB
2075
2076 cp.git_cmd = 1;
2077 cp.no_stdin = 1;
2078 cp.dir = path;
2079
c972bf4c 2080 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
f6d8942b 2081 get_super_prefix_or_empty(), path);
94b7f156 2082 /* TODO: determine if this might overwright untracked files */
c972bf4c 2083 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL);
6e3c1595 2084
c972bf4c 2085 strvec_push(&cp.args, empty_tree_oid_hex());
6e3c1595
SB
2086
2087 if (run_command(&cp))
a4ffbbbb 2088 die(_("could not reset submodule index"));
6e3c1595
SB
2089}
2090
2091/**
2092 * Moves a submodule at a given path from a given head to another new head.
2093 * For edge cases (a submodule coming into existence or removing a submodule)
2094 * pass NULL for old or new respectively.
2095 */
2096int submodule_move_head(const char *path,
bc099914
BW
2097 const char *old_head,
2098 const char *new_head,
6e3c1595
SB
2099 unsigned flags)
2100{
2101 int ret = 0;
2102 struct child_process cp = CHILD_PROCESS_INIT;
2103 const struct submodule *sub;
f2d48994 2104 int *error_code_ptr, error_code;
6e3c1595 2105
627d9342 2106 if (!is_submodule_active(the_repository, path))
823bab09
SB
2107 return 0;
2108
f2d48994
SB
2109 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
2110 /*
2111 * Pass non NULL pointer to is_submodule_populated_gently
2112 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir
2113 * to fixup the submodule in the force case later.
2114 */
2115 error_code_ptr = &error_code;
2116 else
2117 error_code_ptr = NULL;
2118
bc099914 2119 if (old_head && !is_submodule_populated_gently(path, error_code_ptr))
f2d48994 2120 return 0;
6e3c1595 2121
14228447 2122 sub = submodule_from_path(the_repository, null_oid(), path);
6e3c1595
SB
2123
2124 if (!sub)
033abf97 2125 BUG("could not get submodule information for '%s'", path);
6e3c1595 2126
bc099914 2127 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) {
6e3c1595
SB
2128 /* Check if the submodule has a dirty index. */
2129 if (submodule_has_dirty_index(sub))
2130 return error(_("submodule '%s' has dirty index"), path);
2131 }
2132
2133 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
bc099914 2134 if (old_head) {
6e3c1595 2135 if (!submodule_uses_gitfile(path))
cf7a901a 2136 absorb_git_dir_into_superproject(path,
6e3c1595
SB
2137 ABSORB_GITDIR_RECURSE_SUBMODULES);
2138 } else {
ce125d43
JT
2139 struct strbuf gitdir = STRBUF_INIT;
2140 submodule_name_to_gitdir(&gitdir, the_repository,
2141 sub->name);
2142 connect_work_tree_and_git_dir(path, gitdir.buf, 0);
2143 strbuf_release(&gitdir);
6e3c1595
SB
2144
2145 /* make sure the index is clean as well */
2146 submodule_reset_index(path);
2147 }
f2d48994 2148
bc099914 2149 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
ce125d43
JT
2150 struct strbuf gitdir = STRBUF_INIT;
2151 submodule_name_to_gitdir(&gitdir, the_repository,
2152 sub->name);
2153 connect_work_tree_and_git_dir(path, gitdir.buf, 1);
2154 strbuf_release(&gitdir);
f2d48994 2155 }
6e3c1595
SB
2156 }
2157
29fda24d 2158 prepare_submodule_repo_env(&cp.env);
6e3c1595
SB
2159
2160 cp.git_cmd = 1;
2161 cp.no_stdin = 1;
2162 cp.dir = path;
2163
c972bf4c 2164 strvec_pushf(&cp.args, "--super-prefix=%s%s/",
f6d8942b 2165 get_super_prefix_or_empty(), path);
c972bf4c 2166 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL);
6e3c1595
SB
2167
2168 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN)
c972bf4c 2169 strvec_push(&cp.args, "-n");
6e3c1595 2170 else
c972bf4c 2171 strvec_push(&cp.args, "-u");
6e3c1595
SB
2172
2173 if (flags & SUBMODULE_MOVE_HEAD_FORCE)
c972bf4c 2174 strvec_push(&cp.args, "--reset");
6e3c1595 2175 else
c972bf4c 2176 strvec_push(&cp.args, "-m");
6e3c1595 2177
7dcc1f4d 2178 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE))
c972bf4c 2179 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex());
7dcc1f4d 2180
c972bf4c 2181 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex());
6e3c1595
SB
2182
2183 if (run_command(&cp)) {
ba95d4e4 2184 ret = error(_("Submodule '%s' could not be updated."), path);
6e3c1595
SB
2185 goto out;
2186 }
2187
2188 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) {
bc099914 2189 if (new_head) {
a17062cf 2190 child_process_init(&cp);
6e3c1595 2191 /* also set the HEAD accordingly */
a17062cf
SB
2192 cp.git_cmd = 1;
2193 cp.no_stdin = 1;
2194 cp.dir = path;
6e3c1595 2195
29fda24d 2196 prepare_submodule_repo_env(&cp.env);
c972bf4c 2197 strvec_pushl(&cp.args, "update-ref", "HEAD",
f6d8942b 2198 "--no-deref", new_head, NULL);
6e3c1595 2199
a17062cf 2200 if (run_command(&cp)) {
6e3c1595
SB
2201 ret = -1;
2202 goto out;
2203 }
2204 } else {
2205 struct strbuf sb = STRBUF_INIT;
2206
2207 strbuf_addf(&sb, "%s/.git", path);
2208 unlink_or_warn(sb.buf);
2209 strbuf_release(&sb);
2210
2211 if (is_empty_dir(path))
2212 rmdir_or_warn(path);
898c2e65
SB
2213
2214 submodule_unset_core_worktree(sub);
6e3c1595
SB
2215 }
2216 }
2217out:
2218 return ret;
2219}
2220
a8dee3ca
JS
2221int validate_submodule_git_dir(char *git_dir, const char *submodule_name)
2222{
2223 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name);
2224 char *p;
2225 int ret = 0;
2226
2227 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' ||
2228 strcmp(p, submodule_name))
2229 BUG("submodule name '%s' not a suffix of git dir '%s'",
2230 submodule_name, git_dir);
2231
2232 /*
2233 * We prevent the contents of sibling submodules' git directories to
2234 * clash.
2235 *
2236 * Example: having a submodule named `hippo` and another one named
2237 * `hippo/hooks` would result in the git directories
2238 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively,
2239 * but the latter directory is already designated to contain the hooks
2240 * of the former.
2241 */
2242 for (; *p; p++) {
2243 if (is_dir_sep(*p)) {
2244 char c = *p;
2245
2246 *p = '\0';
2247 if (is_git_directory(git_dir))
2248 ret = -1;
2249 *p = c;
2250
2251 if (ret < 0)
2252 return error(_("submodule git dir '%s' is "
2253 "inside git dir '%.*s'"),
2254 git_dir,
2255 (int)(p - git_dir), git_dir);
2256 }
2257 }
2258
2259 return 0;
2260}
2261
f6f85861
SB
2262/*
2263 * Embeds a single submodules git directory into the superprojects git dir,
2264 * non recursively.
2265 */
cf7a901a 2266static void relocate_single_git_dir_into_superproject(const char *path)
f6f85861
SB
2267{
2268 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;
ce125d43 2269 struct strbuf new_gitdir = STRBUF_INIT;
f6f85861
SB
2270 const struct submodule *sub;
2271
2272 if (submodule_uses_worktrees(path))
2273 die(_("relocate_gitdir for submodule '%s' with "
2274 "more than one worktree not supported"), path);
2275
2276 old_git_dir = xstrfmt("%s/.git", path);
2277 if (read_gitfile(old_git_dir))
2278 /* If it is an actual gitfile, it doesn't need migration. */
2279 return;
2280
ce83eadd 2281 real_old_git_dir = real_pathdup(old_git_dir, 1);
f6f85861 2282
14228447 2283 sub = submodule_from_path(the_repository, null_oid(), path);
f6f85861
SB
2284 if (!sub)
2285 die(_("could not lookup name for submodule '%s'"), path);
2286
ce125d43
JT
2287 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name);
2288 if (validate_submodule_git_dir(new_gitdir.buf, sub->name) < 0)
a8dee3ca
JS
2289 die(_("refusing to move '%s' into an existing git dir"),
2290 real_old_git_dir);
ce125d43
JT
2291 if (safe_create_leading_directories_const(new_gitdir.buf) < 0)
2292 die(_("could not create directory '%s'"), new_gitdir.buf);
2293 real_new_git_dir = real_pathdup(new_gitdir.buf, 1);
f6f85861 2294
f6f85861 2295 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"),
202275b9 2296 get_super_prefix_or_empty(), path,
f6f85861
SB
2297 real_old_git_dir, real_new_git_dir);
2298
2299 relocate_gitdir(path, real_old_git_dir, real_new_git_dir);
2300
2301 free(old_git_dir);
2302 free(real_old_git_dir);
2303 free(real_new_git_dir);
ce125d43 2304 strbuf_release(&new_gitdir);
f6f85861
SB
2305}
2306
2307/*
2308 * Migrate the git directory of the submodule given by path from
2309 * having its git directory within the working tree to the git dir nested
2310 * in its superprojects git dir under modules/.
2311 */
cf7a901a 2312void absorb_git_dir_into_superproject(const char *path,
f6f85861
SB
2313 unsigned flags)
2314{
ec9629b3
SB
2315 int err_code;
2316 const char *sub_git_dir;
f6f85861 2317 struct strbuf gitdir = STRBUF_INIT;
f6f85861 2318 strbuf_addf(&gitdir, "%s/.git", path);
ec9629b3 2319 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code);
f6f85861
SB
2320
2321 /* Not populated? */
ec9629b3 2322 if (!sub_git_dir) {
ec9629b3 2323 const struct submodule *sub;
ce125d43 2324 struct strbuf sub_gitdir = STRBUF_INIT;
ec9629b3
SB
2325
2326 if (err_code == READ_GITFILE_ERR_STAT_FAILED) {
2327 /* unpopulated as expected */
2328 strbuf_release(&gitdir);
2329 return;
2330 }
2331
2332 if (err_code != READ_GITFILE_ERR_NOT_A_REPO)
2333 /* We don't know what broke here. */
2334 read_gitfile_error_die(err_code, path, NULL);
2335
2336 /*
2337 * Maybe populated, but no git directory was found?
2338 * This can happen if the superproject is a submodule
2339 * itself and was just absorbed. The absorption of the
2340 * superproject did not rewrite the git file links yet,
2341 * fix it now.
2342 */
14228447 2343 sub = submodule_from_path(the_repository, null_oid(), path);
ec9629b3
SB
2344 if (!sub)
2345 die(_("could not lookup name for submodule '%s'"), path);
ce125d43
JT
2346 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name);
2347 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0);
2348 strbuf_release(&sub_gitdir);
ec9629b3
SB
2349 } else {
2350 /* Is it already absorbed into the superprojects git dir? */
ce83eadd
JS
2351 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1);
2352 char *real_common_git_dir = real_pathdup(get_git_common_dir(), 1);
f6f85861 2353
ec9629b3 2354 if (!starts_with(real_sub_git_dir, real_common_git_dir))
cf7a901a 2355 relocate_single_git_dir_into_superproject(path);
ec9629b3
SB
2356
2357 free(real_sub_git_dir);
2358 free(real_common_git_dir);
2359 }
2360 strbuf_release(&gitdir);
f6f85861
SB
2361
2362 if (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {
2363 struct child_process cp = CHILD_PROCESS_INIT;
2364 struct strbuf sb = STRBUF_INIT;
2365
2366 if (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)
033abf97 2367 BUG("we don't know how to pass the flags down?");
f6f85861 2368
202275b9 2369 strbuf_addstr(&sb, get_super_prefix_or_empty());
f6f85861
SB
2370 strbuf_addstr(&sb, path);
2371 strbuf_addch(&sb, '/');
2372
2373 cp.dir = path;
2374 cp.git_cmd = 1;
2375 cp.no_stdin = 1;
c972bf4c 2376 strvec_pushl(&cp.args, "--super-prefix", sb.buf,
f6d8942b 2377 "submodule--helper",
6e556c41 2378 "absorbgitdirs", NULL);
29fda24d 2379 prepare_submodule_repo_env(&cp.env);
f6f85861
SB
2380 if (run_command(&cp))
2381 die(_("could not recurse into submodule '%s'"), path);
2382
2383 strbuf_release(&sb);
2384 }
f6f85861 2385}
bf0231c6 2386
49d3c4b4 2387int get_superproject_working_tree(struct strbuf *buf)
bf0231c6
SB
2388{
2389 struct child_process cp = CHILD_PROCESS_INIT;
2390 struct strbuf sb = STRBUF_INIT;
4530a85b 2391 struct strbuf one_up = STRBUF_INIT;
bc57ba1d 2392 char *cwd = xgetcwd();
49d3c4b4 2393 int ret = 0;
bf0231c6
SB
2394 const char *subpath;
2395 int code;
2396 ssize_t len;
2397
2398 if (!is_inside_work_tree())
2399 /*
2400 * FIXME:
2401 * We might have a superproject, but it is harder
2402 * to determine.
2403 */
49d3c4b4 2404 return 0;
bf0231c6 2405
4530a85b 2406 if (!strbuf_realpath(&one_up, "../", 0))
49d3c4b4 2407 return 0;
bf0231c6 2408
4530a85b
AM
2409 subpath = relative_path(cwd, one_up.buf, &sb);
2410 strbuf_release(&one_up);
bf0231c6 2411
29fda24d
ÆAB
2412 prepare_submodule_repo_env(&cp.env);
2413 strvec_pop(&cp.env);
bf0231c6 2414
c972bf4c 2415 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..",
f6d8942b
JK
2416 "ls-files", "-z", "--stage", "--full-name", "--",
2417 subpath, NULL);
bf0231c6
SB
2418 strbuf_reset(&sb);
2419
2420 cp.no_stdin = 1;
2421 cp.no_stderr = 1;
2422 cp.out = -1;
2423 cp.git_cmd = 1;
2424
2425 if (start_command(&cp))
2426 die(_("could not start ls-files in .."));
2427
2428 len = strbuf_read(&sb, cp.out, PATH_MAX);
2429 close(cp.out);
2430
2431 if (starts_with(sb.buf, "160000")) {
2432 int super_sub_len;
2433 int cwd_len = strlen(cwd);
2434 char *super_sub, *super_wt;
2435
2436 /*
2437 * There is a superproject having this repo as a submodule.
2438 * The format is <mode> SP <hash> SP <stage> TAB <full name> \0,
2439 * We're only interested in the name after the tab.
2440 */
2441 super_sub = strchr(sb.buf, '\t') + 1;
c5cbb27c 2442 super_sub_len = strlen(super_sub);
bf0231c6
SB
2443
2444 if (super_sub_len > cwd_len ||
2445 strcmp(&cwd[cwd_len - super_sub_len], super_sub))
c3c3486b 2446 BUG("returned path string doesn't match cwd?");
bf0231c6
SB
2447
2448 super_wt = xstrdup(cwd);
2449 super_wt[cwd_len - super_sub_len] = '\0';
2450
49d3c4b4
AM
2451 strbuf_realpath(buf, super_wt, 1);
2452 ret = 1;
bf0231c6
SB
2453 free(super_wt);
2454 }
bc57ba1d 2455 free(cwd);
bf0231c6
SB
2456 strbuf_release(&sb);
2457
2458 code = finish_command(&cp);
2459
2460 if (code == 128)
2461 /* '../' is not a git repository */
49d3c4b4 2462 return 0;
bf0231c6
SB
2463 if (code == 0 && len == 0)
2464 /* There is an unrelated git repository at '../' */
49d3c4b4 2465 return 0;
bf0231c6
SB
2466 if (code)
2467 die(_("ls-tree returned unexpected return code %d"), code);
2468
2469 return ret;
2470}
bbbb7de7 2471
3ce08548
HWN
2472/*
2473 * Put the gitdir for a submodule (given relative to the main
2474 * repository worktree) into `buf`, or return -1 on error.
2475 */
bbbb7de7
NTND
2476int submodule_to_gitdir(struct strbuf *buf, const char *submodule)
2477{
2478 const struct submodule *sub;
2479 const char *git_dir;
2480 int ret = 0;
2481
2482 strbuf_reset(buf);
2483 strbuf_addstr(buf, submodule);
2484 strbuf_complete(buf, '/');
2485 strbuf_addstr(buf, ".git");
2486
2487 git_dir = read_gitfile(buf->buf);
2488 if (git_dir) {
2489 strbuf_reset(buf);
2490 strbuf_addstr(buf, git_dir);
2491 }
2492 if (!is_git_directory(buf->buf)) {
14228447 2493 sub = submodule_from_path(the_repository, null_oid(),
2494 submodule);
bbbb7de7
NTND
2495 if (!sub) {
2496 ret = -1;
2497 goto cleanup;
2498 }
2499 strbuf_reset(buf);
ce125d43 2500 submodule_name_to_gitdir(buf, the_repository, sub->name);
bbbb7de7
NTND
2501 }
2502
2503cleanup:
2504 return ret;
2505}
ce125d43
JT
2506
2507void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r,
2508 const char *submodule_name)
2509{
2510 /*
2511 * NEEDSWORK: The current way of mapping a submodule's name to
2512 * its location in .git/modules/ has problems with some naming
2513 * schemes. For example, if a submodule is named "foo" and
2514 * another is named "foo/bar" (whether present in the same
2515 * superproject commit or not - the problem will arise if both
2516 * superproject commits have been checked out at any point in
2517 * time), or if two submodule names only have different cases in
2518 * a case-insensitive filesystem.
2519 *
2520 * There are several solutions, including encoding the path in
2521 * some way, introducing a submodule.<name>.gitdir config in
2522 * .git/config (not .gitmodules) that allows overriding what the
2523 * gitdir of a submodule would be (and teach Git, upon noticing
2524 * a clash, to automatically determine a non-clashing name and
2525 * to write such a config), or introducing a
2526 * submodule.<name>.gitdir config in .gitmodules that repo
2527 * administrators can explicitly set. Nothing has been decided,
2528 * so for now, just append the name at the end of the path.
2529 */
2530 strbuf_repo_git_path(buf, r, "modules/");
2531 strbuf_addstr(buf, submodule_name);
2532}