]>
Commit | Line | Data |
---|---|---|
f8adbec9 | 1 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
74703a1e | 2 | #include "builtin.h" |
627d9342 | 3 | #include "repository.h" |
74703a1e | 4 | #include "cache.h" |
b2141fc1 | 5 | #include "config.h" |
74703a1e SB |
6 | #include "parse-options.h" |
7 | #include "quote.h" | |
8 | #include "pathspec.h" | |
9 | #include "dir.h" | |
0ea306ef SB |
10 | #include "submodule.h" |
11 | #include "submodule-config.h" | |
12 | #include "string-list.h" | |
ee8838d1 | 13 | #include "run-command.h" |
63e95beb SB |
14 | #include "remote.h" |
15 | #include "refs.h" | |
ec0cb496 | 16 | #include "refspec.h" |
63e95beb | 17 | #include "connect.h" |
a9f8a375 PC |
18 | #include "revision.h" |
19 | #include "diffcore.h" | |
20 | #include "diff.h" | |
0d4a1321 | 21 | #include "object-store.h" |
4f3e57ef | 22 | #include "advice.h" |
961b130d | 23 | #include "branch.h" |
f05da2b4 | 24 | #include "list-objects-filter-options.h" |
63e95beb | 25 | |
9f580a62 | 26 | #define OPT_QUIET (1 << 0) |
a9f8a375 PC |
27 | #define OPT_CACHED (1 << 1) |
28 | #define OPT_RECURSIVE (1 << 2) | |
2e612731 | 29 | #define OPT_FORCE (1 << 3) |
9f580a62 PC |
30 | |
31 | typedef void (*each_submodule_fn)(const struct cache_entry *list_item, | |
32 | void *cb_data); | |
63e95beb | 33 | |
a77c3fcb | 34 | static char *repo_get_default_remote(struct repository *repo) |
63e95beb SB |
35 | { |
36 | char *dest = NULL, *ret; | |
63e95beb | 37 | struct strbuf sb = STRBUF_INIT; |
a77c3fcb AR |
38 | struct ref_store *store = get_main_ref_store(repo); |
39 | const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL, | |
40 | NULL); | |
63e95beb SB |
41 | |
42 | if (!refname) | |
43 | die(_("No such ref: %s"), "HEAD"); | |
44 | ||
45 | /* detached HEAD */ | |
46 | if (!strcmp(refname, "HEAD")) | |
47 | return xstrdup("origin"); | |
48 | ||
49 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
50 | die(_("Expecting a full ref name, got %s"), refname); | |
51 | ||
52 | strbuf_addf(&sb, "branch.%s.remote", refname); | |
a77c3fcb | 53 | if (repo_config_get_string(repo, sb.buf, &dest)) |
63e95beb SB |
54 | ret = xstrdup("origin"); |
55 | else | |
56 | ret = dest; | |
57 | ||
58 | strbuf_release(&sb); | |
59 | return ret; | |
60 | } | |
61 | ||
a77c3fcb | 62 | static char *get_default_remote_submodule(const char *module_path) |
13424764 | 63 | { |
a77c3fcb | 64 | struct repository subrepo; |
13424764 | 65 | |
a77c3fcb AR |
66 | repo_submodule_init(&subrepo, the_repository, module_path, null_oid()); |
67 | return repo_get_default_remote(&subrepo); | |
68 | } | |
13424764 | 69 | |
a77c3fcb AR |
70 | static char *get_default_remote(void) |
71 | { | |
72 | return repo_get_default_remote(the_repository); | |
13424764 PC |
73 | } |
74 | ||
de0fcbe0 | 75 | static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet) |
63e95beb | 76 | { |
ab6f23b7 | 77 | char *remoteurl, *resolved_url; |
63e95beb | 78 | char *remote = get_default_remote(); |
ab6f23b7 | 79 | struct strbuf remotesb = STRBUF_INIT; |
63e95beb | 80 | |
ab6f23b7 AR |
81 | strbuf_addf(&remotesb, "remote.%s.url", remote); |
82 | if (git_config_get_string(remotesb.buf, &remoteurl)) { | |
83 | if (!quiet) | |
84 | warning(_("could not look up configuration '%s'. " | |
85 | "Assuming this repository is its own " | |
86 | "authoritative upstream."), | |
87 | remotesb.buf); | |
63e95beb | 88 | remoteurl = xgetcwd(); |
ab6f23b7 AR |
89 | } |
90 | resolved_url = relative_url(remoteurl, rel_url, up_path); | |
63e95beb | 91 | |
ab6f23b7 | 92 | free(remote); |
63e95beb | 93 | free(remoteurl); |
ab6f23b7 AR |
94 | strbuf_release(&remotesb); |
95 | ||
96 | return resolved_url; | |
63e95beb SB |
97 | } |
98 | ||
99 | static int resolve_relative_url_test(int argc, const char **argv, const char *prefix) | |
100 | { | |
101 | char *remoteurl, *res; | |
102 | const char *up_path, *url; | |
103 | ||
104 | if (argc != 4) | |
105 | die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>"); | |
106 | ||
107 | up_path = argv[1]; | |
108 | remoteurl = xstrdup(argv[2]); | |
109 | url = argv[3]; | |
110 | ||
111 | if (!strcmp(up_path, "(null)")) | |
112 | up_path = NULL; | |
113 | ||
114 | res = relative_url(remoteurl, url, up_path); | |
115 | puts(res); | |
116 | free(res); | |
117 | free(remoteurl); | |
118 | return 0; | |
119 | } | |
74703a1e | 120 | |
5312a850 AR |
121 | static char *do_get_submodule_displaypath(const char *path, |
122 | const char *prefix, | |
123 | const char *super_prefix) | |
74a10642 | 124 | { |
74a10642 PC |
125 | if (prefix && super_prefix) { |
126 | BUG("cannot have prefix '%s' and superprefix '%s'", | |
127 | prefix, super_prefix); | |
128 | } else if (prefix) { | |
129 | struct strbuf sb = STRBUF_INIT; | |
130 | char *displaypath = xstrdup(relative_path(path, prefix, &sb)); | |
131 | strbuf_release(&sb); | |
132 | return displaypath; | |
133 | } else if (super_prefix) { | |
134 | return xstrfmt("%s%s", super_prefix, path); | |
135 | } else { | |
136 | return xstrdup(path); | |
137 | } | |
138 | } | |
139 | ||
5312a850 AR |
140 | /* the result should be freed by the caller. */ |
141 | static char *get_submodule_displaypath(const char *path, const char *prefix) | |
142 | { | |
143 | const char *super_prefix = get_super_prefix(); | |
144 | return do_get_submodule_displaypath(path, prefix, super_prefix); | |
145 | } | |
146 | ||
a9f8a375 PC |
147 | static char *compute_rev_name(const char *sub_path, const char* object_id) |
148 | { | |
149 | struct strbuf sb = STRBUF_INIT; | |
150 | const char ***d; | |
151 | ||
152 | static const char *describe_bare[] = { NULL }; | |
153 | ||
154 | static const char *describe_tags[] = { "--tags", NULL }; | |
155 | ||
156 | static const char *describe_contains[] = { "--contains", NULL }; | |
157 | ||
158 | static const char *describe_all_always[] = { "--all", "--always", NULL }; | |
159 | ||
160 | static const char **describe_argv[] = { describe_bare, describe_tags, | |
161 | describe_contains, | |
162 | describe_all_always, NULL }; | |
163 | ||
164 | for (d = describe_argv; *d; d++) { | |
165 | struct child_process cp = CHILD_PROCESS_INIT; | |
29fda24d | 166 | prepare_submodule_repo_env(&cp.env); |
a9f8a375 PC |
167 | cp.dir = sub_path; |
168 | cp.git_cmd = 1; | |
169 | cp.no_stderr = 1; | |
170 | ||
22f9b7f3 JK |
171 | strvec_push(&cp.args, "describe"); |
172 | strvec_pushv(&cp.args, *d); | |
173 | strvec_push(&cp.args, object_id); | |
a9f8a375 PC |
174 | |
175 | if (!capture_command(&cp, &sb, 0)) { | |
176 | strbuf_strip_suffix(&sb, "\n"); | |
177 | return strbuf_detach(&sb, NULL); | |
178 | } | |
179 | } | |
180 | ||
181 | strbuf_release(&sb); | |
182 | return NULL; | |
183 | } | |
184 | ||
74703a1e SB |
185 | struct module_list { |
186 | const struct cache_entry **entries; | |
187 | int alloc, nr; | |
188 | }; | |
9865b6e6 | 189 | #define MODULE_LIST_INIT { 0 } |
74703a1e SB |
190 | |
191 | static int module_list_compute(int argc, const char **argv, | |
192 | const char *prefix, | |
193 | struct pathspec *pathspec, | |
194 | struct module_list *list) | |
195 | { | |
196 | int i, result = 0; | |
2b56bb7a | 197 | char *ps_matched = NULL; |
74703a1e | 198 | parse_pathspec(pathspec, 0, |
2249d4db | 199 | PATHSPEC_PREFER_FULL, |
74703a1e SB |
200 | prefix, argv); |
201 | ||
74703a1e SB |
202 | if (pathspec->nr) |
203 | ps_matched = xcalloc(pathspec->nr, 1); | |
204 | ||
205 | if (read_cache() < 0) | |
206 | die(_("index file corrupt")); | |
207 | ||
208 | for (i = 0; i < active_nr; i++) { | |
209 | const struct cache_entry *ce = active_cache[i]; | |
210 | ||
6d2df284 | 211 | if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce), |
84ba959b SB |
212 | 0, ps_matched, 1) || |
213 | !S_ISGITLINK(ce->ce_mode)) | |
74703a1e SB |
214 | continue; |
215 | ||
216 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); | |
217 | list->entries[list->nr++] = ce; | |
218 | while (i + 1 < active_nr && | |
219 | !strcmp(ce->name, active_cache[i + 1]->name)) | |
220 | /* | |
221 | * Skip entries with the same name in different stages | |
222 | * to make sure an entry is returned only once. | |
223 | */ | |
224 | i++; | |
225 | } | |
74703a1e | 226 | |
c5c33504 | 227 | if (ps_matched && report_path_error(ps_matched, pathspec)) |
74703a1e SB |
228 | result = -1; |
229 | ||
230 | free(ps_matched); | |
231 | ||
232 | return result; | |
233 | } | |
234 | ||
3e7eaed0 BW |
235 | static void module_list_active(struct module_list *list) |
236 | { | |
237 | int i; | |
238 | struct module_list active_modules = MODULE_LIST_INIT; | |
239 | ||
3e7eaed0 BW |
240 | for (i = 0; i < list->nr; i++) { |
241 | const struct cache_entry *ce = list->entries[i]; | |
242 | ||
627d9342 | 243 | if (!is_submodule_active(the_repository, ce->name)) |
3e7eaed0 BW |
244 | continue; |
245 | ||
246 | ALLOC_GROW(active_modules.entries, | |
247 | active_modules.nr + 1, | |
248 | active_modules.alloc); | |
249 | active_modules.entries[active_modules.nr++] = ce; | |
250 | } | |
251 | ||
252 | free(list->entries); | |
253 | *list = active_modules; | |
254 | } | |
255 | ||
13424764 PC |
256 | static char *get_up_path(const char *path) |
257 | { | |
258 | int i; | |
259 | struct strbuf sb = STRBUF_INIT; | |
260 | ||
261 | for (i = count_slashes(path); i; i--) | |
262 | strbuf_addstr(&sb, "../"); | |
263 | ||
264 | /* | |
265 | * Check if 'path' ends with slash or not | |
266 | * for having the same output for dir/sub_dir | |
267 | * and dir/sub_dir/ | |
268 | */ | |
269 | if (!is_dir_sep(path[strlen(path) - 1])) | |
270 | strbuf_addstr(&sb, "../"); | |
271 | ||
272 | return strbuf_detach(&sb, NULL); | |
273 | } | |
274 | ||
74703a1e SB |
275 | static int module_list(int argc, const char **argv, const char *prefix) |
276 | { | |
277 | int i; | |
278 | struct pathspec pathspec; | |
279 | struct module_list list = MODULE_LIST_INIT; | |
280 | ||
281 | struct option module_list_options[] = { | |
282 | OPT_STRING(0, "prefix", &prefix, | |
283 | N_("path"), | |
284 | N_("alternative anchor for relative paths")), | |
285 | OPT_END() | |
286 | }; | |
287 | ||
288 | const char *const git_submodule_helper_usage[] = { | |
289 | N_("git submodule--helper list [--prefix=<path>] [<path>...]"), | |
290 | NULL | |
291 | }; | |
292 | ||
293 | argc = parse_options(argc, argv, prefix, module_list_options, | |
294 | git_submodule_helper_usage, 0); | |
295 | ||
b0f4b408 | 296 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
74703a1e | 297 | return 1; |
74703a1e SB |
298 | |
299 | for (i = 0; i < list.nr; i++) { | |
300 | const struct cache_entry *ce = list.entries[i]; | |
301 | ||
302 | if (ce_stage(ce)) | |
14228447 | 303 | printf("%06o %s U\t", ce->ce_mode, |
304 | oid_to_hex(null_oid())); | |
74703a1e | 305 | else |
99d1a986 | 306 | printf("%06o %s %d\t", ce->ce_mode, |
307 | oid_to_hex(&ce->oid), ce_stage(ce)); | |
74703a1e | 308 | |
dc4b4a61 | 309 | fprintf(stdout, "%s\n", ce->name); |
74703a1e SB |
310 | } |
311 | return 0; | |
312 | } | |
313 | ||
9f580a62 PC |
314 | static void for_each_listed_submodule(const struct module_list *list, |
315 | each_submodule_fn fn, void *cb_data) | |
316 | { | |
317 | int i; | |
318 | for (i = 0; i < list->nr; i++) | |
319 | fn(list->entries[i], cb_data); | |
320 | } | |
321 | ||
d00a5bdd | 322 | struct foreach_cb { |
fc1b9243 PC |
323 | int argc; |
324 | const char **argv; | |
325 | const char *prefix; | |
326 | int quiet; | |
327 | int recursive; | |
328 | }; | |
d00a5bdd | 329 | #define FOREACH_CB_INIT { 0 } |
fc1b9243 PC |
330 | |
331 | static void runcommand_in_submodule_cb(const struct cache_entry *list_item, | |
332 | void *cb_data) | |
333 | { | |
d00a5bdd | 334 | struct foreach_cb *info = cb_data; |
fc1b9243 PC |
335 | const char *path = list_item->name; |
336 | const struct object_id *ce_oid = &list_item->oid; | |
337 | ||
338 | const struct submodule *sub; | |
339 | struct child_process cp = CHILD_PROCESS_INIT; | |
340 | char *displaypath; | |
341 | ||
342 | displaypath = get_submodule_displaypath(path, info->prefix); | |
343 | ||
14228447 | 344 | sub = submodule_from_path(the_repository, null_oid(), path); |
fc1b9243 PC |
345 | |
346 | if (!sub) | |
347 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
348 | displaypath); | |
349 | ||
350 | if (!is_submodule_populated_gently(path, NULL)) | |
351 | goto cleanup; | |
352 | ||
29fda24d | 353 | prepare_submodule_repo_env(&cp.env); |
fc1b9243 PC |
354 | |
355 | /* | |
356 | * For the purpose of executing <command> in the submodule, | |
357 | * separate shell is used for the purpose of running the | |
358 | * child process. | |
359 | */ | |
360 | cp.use_shell = 1; | |
361 | cp.dir = path; | |
362 | ||
363 | /* | |
364 | * NEEDSWORK: the command currently has access to the variables $name, | |
365 | * $sm_path, $displaypath, $sha1 and $toplevel only when the command | |
366 | * contains a single argument. This is done for maintaining a faithful | |
367 | * translation from shell script. | |
368 | */ | |
369 | if (info->argc == 1) { | |
370 | char *toplevel = xgetcwd(); | |
371 | struct strbuf sb = STRBUF_INIT; | |
372 | ||
29fda24d ÆAB |
373 | strvec_pushf(&cp.env, "name=%s", sub->name); |
374 | strvec_pushf(&cp.env, "sm_path=%s", path); | |
375 | strvec_pushf(&cp.env, "displaypath=%s", displaypath); | |
376 | strvec_pushf(&cp.env, "sha1=%s", | |
f6d8942b | 377 | oid_to_hex(ce_oid)); |
29fda24d | 378 | strvec_pushf(&cp.env, "toplevel=%s", toplevel); |
fc1b9243 PC |
379 | |
380 | /* | |
381 | * Since the path variable was accessible from the script | |
382 | * before porting, it is also made available after porting. | |
383 | * The environment variable "PATH" has a very special purpose | |
384 | * on windows. And since environment variables are | |
385 | * case-insensitive in windows, it interferes with the | |
386 | * existing PATH variable. Hence, to avoid that, we expose | |
b3193252 | 387 | * path via the args strvec and not via env. |
fc1b9243 PC |
388 | */ |
389 | sq_quote_buf(&sb, path); | |
22f9b7f3 | 390 | strvec_pushf(&cp.args, "path=%s; %s", |
f6d8942b | 391 | sb.buf, info->argv[0]); |
fc1b9243 PC |
392 | strbuf_release(&sb); |
393 | free(toplevel); | |
394 | } else { | |
22f9b7f3 | 395 | strvec_pushv(&cp.args, info->argv); |
fc1b9243 PC |
396 | } |
397 | ||
398 | if (!info->quiet) | |
399 | printf(_("Entering '%s'\n"), displaypath); | |
400 | ||
401 | if (info->argv[0] && run_command(&cp)) | |
402 | die(_("run_command returned non-zero status for %s\n."), | |
403 | displaypath); | |
404 | ||
405 | if (info->recursive) { | |
406 | struct child_process cpr = CHILD_PROCESS_INIT; | |
407 | ||
408 | cpr.git_cmd = 1; | |
409 | cpr.dir = path; | |
29fda24d | 410 | prepare_submodule_repo_env(&cpr.env); |
fc1b9243 | 411 | |
22f9b7f3 JK |
412 | strvec_pushl(&cpr.args, "--super-prefix", NULL); |
413 | strvec_pushf(&cpr.args, "%s/", displaypath); | |
414 | strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive", | |
f6d8942b | 415 | NULL); |
fc1b9243 PC |
416 | |
417 | if (info->quiet) | |
22f9b7f3 | 418 | strvec_push(&cpr.args, "--quiet"); |
fc1b9243 | 419 | |
22f9b7f3 JK |
420 | strvec_push(&cpr.args, "--"); |
421 | strvec_pushv(&cpr.args, info->argv); | |
fc1b9243 PC |
422 | |
423 | if (run_command(&cpr)) | |
27c929ed | 424 | die(_("run_command returned non-zero status while " |
fc1b9243 PC |
425 | "recursing in the nested submodules of %s\n."), |
426 | displaypath); | |
427 | } | |
428 | ||
429 | cleanup: | |
430 | free(displaypath); | |
431 | } | |
432 | ||
433 | static int module_foreach(int argc, const char **argv, const char *prefix) | |
434 | { | |
d00a5bdd | 435 | struct foreach_cb info = FOREACH_CB_INIT; |
fc1b9243 PC |
436 | struct pathspec pathspec; |
437 | struct module_list list = MODULE_LIST_INIT; | |
438 | ||
439 | struct option module_foreach_options[] = { | |
e73fe3dd | 440 | OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")), |
fc1b9243 | 441 | OPT_BOOL(0, "recursive", &info.recursive, |
e73fe3dd | 442 | N_("recurse into nested submodules")), |
fc1b9243 PC |
443 | OPT_END() |
444 | }; | |
445 | ||
446 | const char *const git_submodule_helper_usage[] = { | |
a282f5a9 | 447 | N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"), |
fc1b9243 PC |
448 | NULL |
449 | }; | |
450 | ||
451 | argc = parse_options(argc, argv, prefix, module_foreach_options, | |
a282f5a9 | 452 | git_submodule_helper_usage, 0); |
fc1b9243 PC |
453 | |
454 | if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0) | |
455 | return 1; | |
456 | ||
457 | info.argc = argc; | |
458 | info.argv = argv; | |
459 | info.prefix = prefix; | |
460 | ||
461 | for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info); | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
1d04e719 DS |
466 | static int starts_with_dot_slash(const char *const path) |
467 | { | |
468 | return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH | | |
469 | PATH_MATCH_XPLATFORM); | |
470 | } | |
471 | ||
472 | static int starts_with_dot_dot_slash(const char *const path) | |
473 | { | |
474 | return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH | | |
475 | PATH_MATCH_XPLATFORM); | |
476 | } | |
477 | ||
9f580a62 PC |
478 | struct init_cb { |
479 | const char *prefix; | |
3ce52cba | 480 | const char *superprefix; |
9f580a62 PC |
481 | unsigned int flags; |
482 | }; | |
9865b6e6 | 483 | #define INIT_CB_INIT { 0 } |
9f580a62 PC |
484 | |
485 | static void init_submodule(const char *path, const char *prefix, | |
3ce52cba | 486 | const char *superprefix, unsigned int flags) |
3604242f SB |
487 | { |
488 | const struct submodule *sub; | |
489 | struct strbuf sb = STRBUF_INIT; | |
490 | char *upd = NULL, *url = NULL, *displaypath; | |
491 | ||
3ce52cba AR |
492 | /* try superprefix from the environment, if it is not passed explicitly */ |
493 | if (!superprefix) | |
494 | superprefix = get_super_prefix(); | |
495 | displaypath = do_get_submodule_displaypath(path, prefix, superprefix); | |
d92028a5 | 496 | |
14228447 | 497 | sub = submodule_from_path(the_repository, null_oid(), path); |
d92028a5 SB |
498 | |
499 | if (!sub) | |
500 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
501 | displaypath); | |
3604242f | 502 | |
1f8d7115 BW |
503 | /* |
504 | * NEEDSWORK: In a multi-working-tree world, this needs to be | |
505 | * set in the per-worktree config. | |
506 | * | |
507 | * Set active flag for the submodule being initialized | |
508 | */ | |
627d9342 | 509 | if (!is_submodule_active(the_repository, path)) { |
1f8d7115 BW |
510 | strbuf_addf(&sb, "submodule.%s.active", sub->name); |
511 | git_config_set_gently(sb.buf, "true"); | |
74a10642 | 512 | strbuf_reset(&sb); |
1f8d7115 BW |
513 | } |
514 | ||
3604242f SB |
515 | /* |
516 | * Copy url setting when it is not set yet. | |
517 | * To look up the url in .git/config, we must not fall back to | |
518 | * .gitmodules, so look it up directly. | |
519 | */ | |
3604242f SB |
520 | strbuf_addf(&sb, "submodule.%s.url", sub->name); |
521 | if (git_config_get_string(sb.buf, &url)) { | |
627fde10 | 522 | if (!sub->url) |
3604242f SB |
523 | die(_("No url found for submodule path '%s' in .gitmodules"), |
524 | displaypath); | |
525 | ||
627fde10 JK |
526 | url = xstrdup(sub->url); |
527 | ||
3604242f SB |
528 | /* Possibly a url relative to parent */ |
529 | if (starts_with_dot_dot_slash(url) || | |
530 | starts_with_dot_slash(url)) { | |
e0a862fd | 531 | char *oldurl = url; |
de0fcbe0 | 532 | url = resolve_relative_url(oldurl, NULL, 0); |
e0a862fd | 533 | free(oldurl); |
3604242f SB |
534 | } |
535 | ||
536 | if (git_config_set_gently(sb.buf, url)) | |
537 | die(_("Failed to register url for submodule path '%s'"), | |
538 | displaypath); | |
9f580a62 | 539 | if (!(flags & OPT_QUIET)) |
c66410ed SB |
540 | fprintf(stderr, |
541 | _("Submodule '%s' (%s) registered for path '%s'\n"), | |
3604242f SB |
542 | sub->name, url, displaypath); |
543 | } | |
74a10642 | 544 | strbuf_reset(&sb); |
3604242f SB |
545 | |
546 | /* Copy "update" setting when it is not set yet */ | |
3604242f SB |
547 | strbuf_addf(&sb, "submodule.%s.update", sub->name); |
548 | if (git_config_get_string(sb.buf, &upd) && | |
549 | sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
550 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) { | |
551 | fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"), | |
552 | sub->name); | |
553 | upd = xstrdup("none"); | |
554 | } else | |
555 | upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy)); | |
556 | ||
557 | if (git_config_set_gently(sb.buf, upd)) | |
558 | die(_("Failed to register update mode for submodule path '%s'"), displaypath); | |
559 | } | |
560 | strbuf_release(&sb); | |
561 | free(displaypath); | |
562 | free(url); | |
563 | free(upd); | |
564 | } | |
565 | ||
9f580a62 PC |
566 | static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data) |
567 | { | |
568 | struct init_cb *info = cb_data; | |
3ce52cba | 569 | init_submodule(list_item->name, info->prefix, info->superprefix, info->flags); |
9f580a62 PC |
570 | } |
571 | ||
3604242f SB |
572 | static int module_init(int argc, const char **argv, const char *prefix) |
573 | { | |
9f580a62 | 574 | struct init_cb info = INIT_CB_INIT; |
3604242f SB |
575 | struct pathspec pathspec; |
576 | struct module_list list = MODULE_LIST_INIT; | |
577 | int quiet = 0; | |
3604242f SB |
578 | |
579 | struct option module_init_options[] = { | |
e73fe3dd | 580 | OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")), |
3604242f SB |
581 | OPT_END() |
582 | }; | |
583 | ||
584 | const char *const git_submodule_helper_usage[] = { | |
a282f5a9 | 585 | N_("git submodule--helper init [<options>] [<path>]"), |
3604242f SB |
586 | NULL |
587 | }; | |
588 | ||
589 | argc = parse_options(argc, argv, prefix, module_init_options, | |
590 | git_submodule_helper_usage, 0); | |
591 | ||
592 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
593 | return 1; | |
594 | ||
3e7eaed0 BW |
595 | /* |
596 | * If there are no path args and submodule.active is set then, | |
597 | * by default, only initialize 'active' modules. | |
598 | */ | |
599 | if (!argc && git_config_get_value_multi("submodule.active")) | |
600 | module_list_active(&list); | |
601 | ||
9f580a62 PC |
602 | info.prefix = prefix; |
603 | if (quiet) | |
604 | info.flags |= OPT_QUIET; | |
605 | ||
606 | for_each_listed_submodule(&list, init_submodule_cb, &info); | |
3604242f SB |
607 | |
608 | return 0; | |
609 | } | |
610 | ||
a9f8a375 PC |
611 | struct status_cb { |
612 | const char *prefix; | |
613 | unsigned int flags; | |
614 | }; | |
9865b6e6 | 615 | #define STATUS_CB_INIT { 0 } |
a9f8a375 PC |
616 | |
617 | static void print_status(unsigned int flags, char state, const char *path, | |
618 | const struct object_id *oid, const char *displaypath) | |
619 | { | |
620 | if (flags & OPT_QUIET) | |
621 | return; | |
622 | ||
623 | printf("%c%s %s", state, oid_to_hex(oid), displaypath); | |
624 | ||
0b5e2ea7 NTND |
625 | if (state == ' ' || state == '+') { |
626 | const char *name = compute_rev_name(path, oid_to_hex(oid)); | |
627 | ||
628 | if (name) | |
629 | printf(" (%s)", name); | |
630 | } | |
a9f8a375 PC |
631 | |
632 | printf("\n"); | |
633 | } | |
634 | ||
635 | static int handle_submodule_head_ref(const char *refname, | |
636 | const struct object_id *oid, int flags, | |
637 | void *cb_data) | |
638 | { | |
639 | struct object_id *output = cb_data; | |
640 | if (oid) | |
641 | oidcpy(output, oid); | |
642 | ||
643 | return 0; | |
644 | } | |
645 | ||
646 | static void status_submodule(const char *path, const struct object_id *ce_oid, | |
647 | unsigned int ce_flags, const char *prefix, | |
648 | unsigned int flags) | |
649 | { | |
650 | char *displaypath; | |
22f9b7f3 | 651 | struct strvec diff_files_args = STRVEC_INIT; |
f196c1e9 | 652 | struct rev_info rev = REV_INFO_INIT; |
a9f8a375 | 653 | int diff_files_result; |
3b2885ec PK |
654 | struct strbuf buf = STRBUF_INIT; |
655 | const char *git_dir; | |
a9f8a375 | 656 | |
14228447 | 657 | if (!submodule_from_path(the_repository, null_oid(), path)) |
a9f8a375 PC |
658 | die(_("no submodule mapping found in .gitmodules for path '%s'"), |
659 | path); | |
660 | ||
661 | displaypath = get_submodule_displaypath(path, prefix); | |
662 | ||
663 | if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) { | |
14228447 | 664 | print_status(flags, 'U', path, null_oid(), displaypath); |
a9f8a375 PC |
665 | goto cleanup; |
666 | } | |
667 | ||
3b2885ec PK |
668 | strbuf_addf(&buf, "%s/.git", path); |
669 | git_dir = read_gitfile(buf.buf); | |
670 | if (!git_dir) | |
671 | git_dir = buf.buf; | |
672 | ||
673 | if (!is_submodule_active(the_repository, path) || | |
674 | !is_git_directory(git_dir)) { | |
a9f8a375 | 675 | print_status(flags, '-', path, ce_oid, displaypath); |
3b2885ec | 676 | strbuf_release(&buf); |
a9f8a375 PC |
677 | goto cleanup; |
678 | } | |
3b2885ec | 679 | strbuf_release(&buf); |
a9f8a375 | 680 | |
22f9b7f3 | 681 | strvec_pushl(&diff_files_args, "diff-files", |
f6d8942b JK |
682 | "--ignore-submodules=dirty", "--quiet", "--", |
683 | path, NULL); | |
a9f8a375 PC |
684 | |
685 | git_config(git_diff_basic_config, NULL); | |
1f3aea22 MG |
686 | |
687 | repo_init_revisions(the_repository, &rev, NULL); | |
a9f8a375 | 688 | rev.abbrev = 0; |
d70a9eb6 JK |
689 | diff_files_args.nr = setup_revisions(diff_files_args.nr, |
690 | diff_files_args.v, | |
691 | &rev, NULL); | |
a9f8a375 PC |
692 | diff_files_result = run_diff_files(&rev, 0); |
693 | ||
694 | if (!diff_result_code(&rev.diffopt, diff_files_result)) { | |
695 | print_status(flags, ' ', path, ce_oid, | |
696 | displaypath); | |
697 | } else if (!(flags & OPT_CACHED)) { | |
698 | struct object_id oid; | |
74b6bda3 | 699 | struct ref_store *refs = get_submodule_ref_store(path); |
a9f8a375 | 700 | |
74b6bda3 RS |
701 | if (!refs) { |
702 | print_status(flags, '-', path, ce_oid, displaypath); | |
703 | goto cleanup; | |
704 | } | |
705 | if (refs_head_ref(refs, handle_submodule_head_ref, &oid)) | |
ed5bdd5b | 706 | die(_("could not resolve HEAD ref inside the " |
a9f8a375 PC |
707 | "submodule '%s'"), path); |
708 | ||
709 | print_status(flags, '+', path, &oid, displaypath); | |
710 | } else { | |
711 | print_status(flags, '+', path, ce_oid, displaypath); | |
712 | } | |
713 | ||
714 | if (flags & OPT_RECURSIVE) { | |
715 | struct child_process cpr = CHILD_PROCESS_INIT; | |
716 | ||
717 | cpr.git_cmd = 1; | |
718 | cpr.dir = path; | |
29fda24d | 719 | prepare_submodule_repo_env(&cpr.env); |
a9f8a375 | 720 | |
22f9b7f3 JK |
721 | strvec_push(&cpr.args, "--super-prefix"); |
722 | strvec_pushf(&cpr.args, "%s/", displaypath); | |
723 | strvec_pushl(&cpr.args, "submodule--helper", "status", | |
f6d8942b | 724 | "--recursive", NULL); |
a9f8a375 PC |
725 | |
726 | if (flags & OPT_CACHED) | |
22f9b7f3 | 727 | strvec_push(&cpr.args, "--cached"); |
a9f8a375 PC |
728 | |
729 | if (flags & OPT_QUIET) | |
22f9b7f3 | 730 | strvec_push(&cpr.args, "--quiet"); |
a9f8a375 PC |
731 | |
732 | if (run_command(&cpr)) | |
733 | die(_("failed to recurse into submodule '%s'"), path); | |
734 | } | |
735 | ||
736 | cleanup: | |
22f9b7f3 | 737 | strvec_clear(&diff_files_args); |
a9f8a375 | 738 | free(displaypath); |
f196c1e9 | 739 | release_revisions(&rev); |
a9f8a375 PC |
740 | } |
741 | ||
742 | static void status_submodule_cb(const struct cache_entry *list_item, | |
743 | void *cb_data) | |
744 | { | |
745 | struct status_cb *info = cb_data; | |
746 | status_submodule(list_item->name, &list_item->oid, list_item->ce_flags, | |
747 | info->prefix, info->flags); | |
748 | } | |
749 | ||
750 | static int module_status(int argc, const char **argv, const char *prefix) | |
751 | { | |
752 | struct status_cb info = STATUS_CB_INIT; | |
753 | struct pathspec pathspec; | |
754 | struct module_list list = MODULE_LIST_INIT; | |
755 | int quiet = 0; | |
756 | ||
757 | struct option module_status_options[] = { | |
e73fe3dd ZH |
758 | OPT__QUIET(&quiet, N_("suppress submodule status output")), |
759 | OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED), | |
a9f8a375 PC |
760 | OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE), |
761 | OPT_END() | |
762 | }; | |
763 | ||
764 | const char *const git_submodule_helper_usage[] = { | |
765 | N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"), | |
766 | NULL | |
767 | }; | |
768 | ||
769 | argc = parse_options(argc, argv, prefix, module_status_options, | |
770 | git_submodule_helper_usage, 0); | |
771 | ||
772 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
773 | return 1; | |
774 | ||
775 | info.prefix = prefix; | |
776 | if (quiet) | |
777 | info.flags |= OPT_QUIET; | |
778 | ||
779 | for_each_listed_submodule(&list, status_submodule_cb, &info); | |
3604242f SB |
780 | |
781 | return 0; | |
782 | } | |
783 | ||
0ea306ef SB |
784 | static int module_name(int argc, const char **argv, const char *prefix) |
785 | { | |
786 | const struct submodule *sub; | |
787 | ||
788 | if (argc != 2) | |
789 | usage(_("git submodule--helper name <path>")); | |
790 | ||
14228447 | 791 | sub = submodule_from_path(the_repository, null_oid(), argv[1]); |
0ea306ef SB |
792 | |
793 | if (!sub) | |
794 | die(_("no submodule mapping found in .gitmodules for path '%s'"), | |
795 | argv[1]); | |
796 | ||
797 | printf("%s\n", sub->name); | |
798 | ||
799 | return 0; | |
800 | } | |
14111fc4 | 801 | |
e83e3333 PC |
802 | struct module_cb { |
803 | unsigned int mod_src; | |
804 | unsigned int mod_dst; | |
805 | struct object_id oid_src; | |
806 | struct object_id oid_dst; | |
807 | char status; | |
808 | const char *sm_path; | |
809 | }; | |
9865b6e6 | 810 | #define MODULE_CB_INIT { 0 } |
e83e3333 PC |
811 | |
812 | struct module_cb_list { | |
813 | struct module_cb **entries; | |
814 | int alloc, nr; | |
815 | }; | |
9865b6e6 | 816 | #define MODULE_CB_LIST_INIT { 0 } |
e83e3333 PC |
817 | |
818 | struct summary_cb { | |
819 | int argc; | |
820 | const char **argv; | |
821 | const char *prefix; | |
822 | unsigned int cached: 1; | |
823 | unsigned int for_status: 1; | |
824 | unsigned int files: 1; | |
825 | int summary_limit; | |
826 | }; | |
9865b6e6 | 827 | #define SUMMARY_CB_INIT { 0 } |
e83e3333 PC |
828 | |
829 | enum diff_cmd { | |
830 | DIFF_INDEX, | |
831 | DIFF_FILES | |
832 | }; | |
833 | ||
f0c6b646 | 834 | static char *verify_submodule_committish(const char *sm_path, |
e83e3333 PC |
835 | const char *committish) |
836 | { | |
837 | struct child_process cp_rev_parse = CHILD_PROCESS_INIT; | |
838 | struct strbuf result = STRBUF_INIT; | |
839 | ||
840 | cp_rev_parse.git_cmd = 1; | |
841 | cp_rev_parse.dir = sm_path; | |
29fda24d | 842 | prepare_submodule_repo_env(&cp_rev_parse.env); |
e83e3333 PC |
843 | strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL); |
844 | strvec_pushf(&cp_rev_parse.args, "%s^0", committish); | |
845 | strvec_push(&cp_rev_parse.args, "--"); | |
846 | ||
847 | if (capture_command(&cp_rev_parse, &result, 0)) | |
848 | return NULL; | |
849 | ||
850 | strbuf_trim_trailing_newline(&result); | |
851 | return strbuf_detach(&result, NULL); | |
852 | } | |
853 | ||
f0c6b646 | 854 | static void print_submodule_summary(struct summary_cb *info, char *errmsg, |
e83e3333 PC |
855 | int total_commits, const char *displaypath, |
856 | const char *src_abbrev, const char *dst_abbrev, | |
e83e3333 PC |
857 | struct module_cb *p) |
858 | { | |
859 | if (p->status == 'T') { | |
860 | if (S_ISGITLINK(p->mod_dst)) | |
861 | printf(_("* %s %s(blob)->%s(submodule)"), | |
862 | displaypath, src_abbrev, dst_abbrev); | |
863 | else | |
864 | printf(_("* %s %s(submodule)->%s(blob)"), | |
865 | displaypath, src_abbrev, dst_abbrev); | |
866 | } else { | |
867 | printf("* %s %s...%s", | |
868 | displaypath, src_abbrev, dst_abbrev); | |
869 | } | |
870 | ||
871 | if (total_commits < 0) | |
872 | printf(":\n"); | |
873 | else | |
874 | printf(" (%d):\n", total_commits); | |
875 | ||
876 | if (errmsg) { | |
877 | printf(_("%s"), errmsg); | |
878 | } else if (total_commits > 0) { | |
879 | struct child_process cp_log = CHILD_PROCESS_INIT; | |
880 | ||
881 | cp_log.git_cmd = 1; | |
882 | cp_log.dir = p->sm_path; | |
29fda24d | 883 | prepare_submodule_repo_env(&cp_log.env); |
e83e3333 PC |
884 | strvec_pushl(&cp_log.args, "log", NULL); |
885 | ||
886 | if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) { | |
887 | if (info->summary_limit > 0) | |
888 | strvec_pushf(&cp_log.args, "-%d", | |
889 | info->summary_limit); | |
890 | ||
891 | strvec_pushl(&cp_log.args, "--pretty= %m %s", | |
892 | "--first-parent", NULL); | |
893 | strvec_pushf(&cp_log.args, "%s...%s", | |
894 | src_abbrev, dst_abbrev); | |
895 | } else if (S_ISGITLINK(p->mod_dst)) { | |
896 | strvec_pushl(&cp_log.args, "--pretty= > %s", | |
897 | "-1", dst_abbrev, NULL); | |
898 | } else { | |
899 | strvec_pushl(&cp_log.args, "--pretty= < %s", | |
900 | "-1", src_abbrev, NULL); | |
901 | } | |
902 | run_command(&cp_log); | |
903 | } | |
904 | printf("\n"); | |
905 | } | |
906 | ||
907 | static void generate_submodule_summary(struct summary_cb *info, | |
908 | struct module_cb *p) | |
909 | { | |
d79b1455 | 910 | char *displaypath, *src_abbrev = NULL, *dst_abbrev; |
e83e3333 PC |
911 | int missing_src = 0, missing_dst = 0; |
912 | char *errmsg = NULL; | |
913 | int total_commits = -1; | |
914 | ||
14228447 | 915 | if (!info->cached && oideq(&p->oid_dst, null_oid())) { |
e83e3333 PC |
916 | if (S_ISGITLINK(p->mod_dst)) { |
917 | struct ref_store *refs = get_submodule_ref_store(p->sm_path); | |
918 | if (refs) | |
919 | refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst); | |
920 | } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) { | |
921 | struct stat st; | |
922 | int fd = open(p->sm_path, O_RDONLY); | |
923 | ||
924 | if (fd < 0 || fstat(fd, &st) < 0 || | |
925 | index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB, | |
926 | p->sm_path, 0)) | |
927 | error(_("couldn't hash object from '%s'"), p->sm_path); | |
928 | } else { | |
929 | /* for a submodule removal (mode:0000000), don't warn */ | |
930 | if (p->mod_dst) | |
f0c6b646 | 931 | warning(_("unexpected mode %o\n"), p->mod_dst); |
e83e3333 PC |
932 | } |
933 | } | |
934 | ||
935 | if (S_ISGITLINK(p->mod_src)) { | |
d79b1455 SS |
936 | if (p->status != 'D') |
937 | src_abbrev = verify_submodule_committish(p->sm_path, | |
938 | oid_to_hex(&p->oid_src)); | |
e83e3333 PC |
939 | if (!src_abbrev) { |
940 | missing_src = 1; | |
941 | /* | |
942 | * As `rev-parse` failed, we fallback to getting | |
943 | * the abbreviated hash using oid_src. We do | |
944 | * this as we might still need the abbreviated | |
945 | * hash in cases like a submodule type change, etc. | |
946 | */ | |
947 | src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7); | |
948 | } | |
949 | } else { | |
950 | /* | |
951 | * The source does not point to a submodule. | |
952 | * So, we fallback to getting the abbreviation using | |
953 | * oid_src as we might still need the abbreviated | |
954 | * hash in cases like submodule add, etc. | |
955 | */ | |
956 | src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7); | |
957 | } | |
958 | ||
959 | if (S_ISGITLINK(p->mod_dst)) { | |
960 | dst_abbrev = verify_submodule_committish(p->sm_path, | |
961 | oid_to_hex(&p->oid_dst)); | |
962 | if (!dst_abbrev) { | |
963 | missing_dst = 1; | |
964 | /* | |
965 | * As `rev-parse` failed, we fallback to getting | |
966 | * the abbreviated hash using oid_dst. We do | |
967 | * this as we might still need the abbreviated | |
968 | * hash in cases like a submodule type change, etc. | |
969 | */ | |
970 | dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7); | |
971 | } | |
972 | } else { | |
973 | /* | |
974 | * The destination does not point to a submodule. | |
975 | * So, we fallback to getting the abbreviation using | |
976 | * oid_dst as we might still need the abbreviated | |
977 | * hash in cases like a submodule removal, etc. | |
978 | */ | |
979 | dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7); | |
980 | } | |
981 | ||
982 | displaypath = get_submodule_displaypath(p->sm_path, info->prefix); | |
983 | ||
984 | if (!missing_src && !missing_dst) { | |
985 | struct child_process cp_rev_list = CHILD_PROCESS_INIT; | |
986 | struct strbuf sb_rev_list = STRBUF_INIT; | |
987 | ||
988 | strvec_pushl(&cp_rev_list.args, "rev-list", | |
989 | "--first-parent", "--count", NULL); | |
990 | if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) | |
991 | strvec_pushf(&cp_rev_list.args, "%s...%s", | |
992 | src_abbrev, dst_abbrev); | |
993 | else | |
994 | strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ? | |
995 | src_abbrev : dst_abbrev); | |
996 | strvec_push(&cp_rev_list.args, "--"); | |
997 | ||
998 | cp_rev_list.git_cmd = 1; | |
999 | cp_rev_list.dir = p->sm_path; | |
29fda24d | 1000 | prepare_submodule_repo_env(&cp_rev_list.env); |
e83e3333 PC |
1001 | |
1002 | if (!capture_command(&cp_rev_list, &sb_rev_list, 0)) | |
1003 | total_commits = atoi(sb_rev_list.buf); | |
1004 | ||
1005 | strbuf_release(&sb_rev_list); | |
1006 | } else { | |
1007 | /* | |
1008 | * Don't give error msg for modification whose dst is not | |
1009 | * submodule, i.e., deleted or changed to blob | |
1010 | */ | |
1011 | if (S_ISGITLINK(p->mod_dst)) { | |
1012 | struct strbuf errmsg_str = STRBUF_INIT; | |
1013 | if (missing_src && missing_dst) { | |
1014 | strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n", | |
1015 | displaypath, oid_to_hex(&p->oid_src), | |
1016 | oid_to_hex(&p->oid_dst)); | |
1017 | } else { | |
1018 | strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n", | |
1019 | displaypath, missing_src ? | |
1020 | oid_to_hex(&p->oid_src) : | |
1021 | oid_to_hex(&p->oid_dst)); | |
1022 | } | |
1023 | errmsg = strbuf_detach(&errmsg_str, NULL); | |
1024 | } | |
1025 | } | |
1026 | ||
1027 | print_submodule_summary(info, errmsg, total_commits, | |
1028 | displaypath, src_abbrev, | |
e0f7ae56 | 1029 | dst_abbrev, p); |
e83e3333 PC |
1030 | |
1031 | free(displaypath); | |
1032 | free(src_abbrev); | |
1033 | free(dst_abbrev); | |
1034 | } | |
1035 | ||
1036 | static void prepare_submodule_summary(struct summary_cb *info, | |
1037 | struct module_cb_list *list) | |
1038 | { | |
1039 | int i; | |
1040 | for (i = 0; i < list->nr; i++) { | |
1041 | const struct submodule *sub; | |
1042 | struct module_cb *p = list->entries[i]; | |
1043 | struct strbuf sm_gitdir = STRBUF_INIT; | |
1044 | ||
1045 | if (p->status == 'D' || p->status == 'T') { | |
1046 | generate_submodule_summary(info, p); | |
1047 | continue; | |
1048 | } | |
1049 | ||
1050 | if (info->for_status && p->status != 'A' && | |
1051 | (sub = submodule_from_path(the_repository, | |
14228447 | 1052 | null_oid(), p->sm_path))) { |
e83e3333 PC |
1053 | char *config_key = NULL; |
1054 | const char *value; | |
1055 | int ignore_all = 0; | |
1056 | ||
1057 | config_key = xstrfmt("submodule.%s.ignore", | |
1058 | sub->name); | |
bbdba3d8 | 1059 | if (!git_config_get_string_tmp(config_key, &value)) |
e83e3333 PC |
1060 | ignore_all = !strcmp(value, "all"); |
1061 | else if (sub->ignore) | |
1062 | ignore_all = !strcmp(sub->ignore, "all"); | |
1063 | ||
1064 | free(config_key); | |
1065 | if (ignore_all) | |
1066 | continue; | |
1067 | } | |
1068 | ||
1069 | /* Also show added or modified modules which are checked out */ | |
1070 | strbuf_addstr(&sm_gitdir, p->sm_path); | |
1071 | if (is_nonbare_repository_dir(&sm_gitdir)) | |
1072 | generate_submodule_summary(info, p); | |
1073 | strbuf_release(&sm_gitdir); | |
1074 | } | |
1075 | } | |
1076 | ||
1077 | static void submodule_summary_callback(struct diff_queue_struct *q, | |
1078 | struct diff_options *options, | |
1079 | void *data) | |
1080 | { | |
1081 | int i; | |
1082 | struct module_cb_list *list = data; | |
1083 | for (i = 0; i < q->nr; i++) { | |
1084 | struct diff_filepair *p = q->queue[i]; | |
1085 | struct module_cb *temp; | |
1086 | ||
1087 | if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode)) | |
1088 | continue; | |
1089 | temp = (struct module_cb*)malloc(sizeof(struct module_cb)); | |
1090 | temp->mod_src = p->one->mode; | |
1091 | temp->mod_dst = p->two->mode; | |
1092 | temp->oid_src = p->one->oid; | |
1093 | temp->oid_dst = p->two->oid; | |
1094 | temp->status = p->status; | |
1095 | temp->sm_path = xstrdup(p->one->path); | |
1096 | ||
1097 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); | |
1098 | list->entries[list->nr++] = temp; | |
1099 | } | |
1100 | } | |
1101 | ||
1102 | static const char *get_diff_cmd(enum diff_cmd diff_cmd) | |
1103 | { | |
1104 | switch (diff_cmd) { | |
1105 | case DIFF_INDEX: return "diff-index"; | |
1106 | case DIFF_FILES: return "diff-files"; | |
1107 | default: BUG("bad diff_cmd value %d", diff_cmd); | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | static int compute_summary_module_list(struct object_id *head_oid, | |
1112 | struct summary_cb *info, | |
1113 | enum diff_cmd diff_cmd) | |
1114 | { | |
1115 | struct strvec diff_args = STRVEC_INIT; | |
1116 | struct rev_info rev; | |
1117 | struct module_cb_list list = MODULE_CB_LIST_INIT; | |
0139c58a | 1118 | int ret = 0; |
e83e3333 PC |
1119 | |
1120 | strvec_push(&diff_args, get_diff_cmd(diff_cmd)); | |
1121 | if (info->cached) | |
1122 | strvec_push(&diff_args, "--cached"); | |
1123 | strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL); | |
1124 | if (head_oid) | |
1125 | strvec_push(&diff_args, oid_to_hex(head_oid)); | |
1126 | strvec_push(&diff_args, "--"); | |
1127 | if (info->argc) | |
1128 | strvec_pushv(&diff_args, info->argv); | |
1129 | ||
1130 | git_config(git_diff_basic_config, NULL); | |
1131 | init_revisions(&rev, info->prefix); | |
1132 | rev.abbrev = 0; | |
5c327502 | 1133 | precompose_argv_prefix(diff_args.nr, diff_args.v, NULL); |
e83e3333 PC |
1134 | setup_revisions(diff_args.nr, diff_args.v, &rev, NULL); |
1135 | rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK; | |
1136 | rev.diffopt.format_callback = submodule_summary_callback; | |
1137 | rev.diffopt.format_callback_data = &list; | |
1138 | ||
1139 | if (!info->cached) { | |
1140 | if (diff_cmd == DIFF_INDEX) | |
1141 | setup_work_tree(); | |
1142 | if (read_cache_preload(&rev.diffopt.pathspec) < 0) { | |
1143 | perror("read_cache_preload"); | |
0139c58a ÆAB |
1144 | ret = -1; |
1145 | goto cleanup; | |
e83e3333 PC |
1146 | } |
1147 | } else if (read_cache() < 0) { | |
1148 | perror("read_cache"); | |
0139c58a ÆAB |
1149 | ret = -1; |
1150 | goto cleanup; | |
e83e3333 PC |
1151 | } |
1152 | ||
1153 | if (diff_cmd == DIFF_INDEX) | |
1154 | run_diff_index(&rev, info->cached); | |
1155 | else | |
1156 | run_diff_files(&rev, 0); | |
1157 | prepare_submodule_summary(info, &list); | |
0139c58a | 1158 | cleanup: |
e83e3333 | 1159 | strvec_clear(&diff_args); |
2108fe4a | 1160 | release_revisions(&rev); |
0139c58a | 1161 | return ret; |
e83e3333 PC |
1162 | } |
1163 | ||
1164 | static int module_summary(int argc, const char **argv, const char *prefix) | |
1165 | { | |
1166 | struct summary_cb info = SUMMARY_CB_INIT; | |
1167 | int cached = 0; | |
1168 | int for_status = 0; | |
1169 | int files = 0; | |
1170 | int summary_limit = -1; | |
1171 | enum diff_cmd diff_cmd = DIFF_INDEX; | |
1172 | struct object_id head_oid; | |
1173 | int ret; | |
1174 | ||
1175 | struct option module_summary_options[] = { | |
1176 | OPT_BOOL(0, "cached", &cached, | |
1177 | N_("use the commit stored in the index instead of the submodule HEAD")), | |
1178 | OPT_BOOL(0, "files", &files, | |
f5f5a61d | 1179 | N_("compare the commit in the index with that in the submodule HEAD")), |
e83e3333 PC |
1180 | OPT_BOOL(0, "for-status", &for_status, |
1181 | N_("skip submodules with 'ignore_config' value set to 'all'")), | |
1182 | OPT_INTEGER('n', "summary-limit", &summary_limit, | |
1183 | N_("limit the summary size")), | |
1184 | OPT_END() | |
1185 | }; | |
1186 | ||
1187 | const char *const git_submodule_helper_usage[] = { | |
9f443f55 | 1188 | N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"), |
e83e3333 PC |
1189 | NULL |
1190 | }; | |
1191 | ||
1192 | argc = parse_options(argc, argv, prefix, module_summary_options, | |
1193 | git_submodule_helper_usage, 0); | |
1194 | ||
1195 | if (!summary_limit) | |
1196 | return 0; | |
1197 | ||
1198 | if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) { | |
1199 | if (argc) { | |
1200 | argv++; | |
1201 | argc--; | |
1202 | } | |
1203 | } else if (!argc || !strcmp(argv[0], "HEAD")) { | |
1204 | /* before the first commit: compare with an empty tree */ | |
1205 | oidcpy(&head_oid, the_hash_algo->empty_tree); | |
1206 | if (argc) { | |
1207 | argv++; | |
1208 | argc--; | |
1209 | } | |
1210 | } else { | |
1211 | if (get_oid("HEAD", &head_oid)) | |
1212 | die(_("could not fetch a revision for HEAD")); | |
1213 | } | |
1214 | ||
1215 | if (files) { | |
1216 | if (cached) | |
43ea635c | 1217 | die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files"); |
e83e3333 PC |
1218 | diff_cmd = DIFF_FILES; |
1219 | } | |
1220 | ||
1221 | info.argc = argc; | |
1222 | info.argv = argv; | |
1223 | info.prefix = prefix; | |
1224 | info.cached = !!cached; | |
1225 | info.files = !!files; | |
1226 | info.for_status = !!for_status; | |
1227 | info.summary_limit = summary_limit; | |
1228 | ||
1229 | ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL, | |
1230 | &info, diff_cmd); | |
1231 | return ret; | |
1232 | } | |
1233 | ||
13424764 PC |
1234 | struct sync_cb { |
1235 | const char *prefix; | |
1236 | unsigned int flags; | |
1237 | }; | |
9865b6e6 | 1238 | #define SYNC_CB_INIT { 0 } |
13424764 PC |
1239 | |
1240 | static void sync_submodule(const char *path, const char *prefix, | |
1241 | unsigned int flags) | |
1242 | { | |
1243 | const struct submodule *sub; | |
1244 | char *remote_key = NULL; | |
a77c3fcb | 1245 | char *sub_origin_url, *super_config_url, *displaypath, *default_remote; |
13424764 | 1246 | struct strbuf sb = STRBUF_INIT; |
13424764 PC |
1247 | char *sub_config_path = NULL; |
1248 | ||
1249 | if (!is_submodule_active(the_repository, path)) | |
1250 | return; | |
1251 | ||
14228447 | 1252 | sub = submodule_from_path(the_repository, null_oid(), path); |
13424764 PC |
1253 | |
1254 | if (sub && sub->url) { | |
1255 | if (starts_with_dot_dot_slash(sub->url) || | |
1256 | starts_with_dot_slash(sub->url)) { | |
0c61041e | 1257 | char *up_path = get_up_path(path); |
de0fcbe0 AR |
1258 | sub_origin_url = resolve_relative_url(sub->url, up_path, 1); |
1259 | super_config_url = resolve_relative_url(sub->url, NULL, 1); | |
13424764 | 1260 | free(up_path); |
13424764 PC |
1261 | } else { |
1262 | sub_origin_url = xstrdup(sub->url); | |
1263 | super_config_url = xstrdup(sub->url); | |
1264 | } | |
1265 | } else { | |
1266 | sub_origin_url = xstrdup(""); | |
1267 | super_config_url = xstrdup(""); | |
1268 | } | |
1269 | ||
1270 | displaypath = get_submodule_displaypath(path, prefix); | |
1271 | ||
1272 | if (!(flags & OPT_QUIET)) | |
1273 | printf(_("Synchronizing submodule url for '%s'\n"), | |
1274 | displaypath); | |
1275 | ||
1276 | strbuf_reset(&sb); | |
1277 | strbuf_addf(&sb, "submodule.%s.url", sub->name); | |
1278 | if (git_config_set_gently(sb.buf, super_config_url)) | |
1279 | die(_("failed to register url for submodule path '%s'"), | |
1280 | displaypath); | |
1281 | ||
1282 | if (!is_submodule_populated_gently(path, NULL)) | |
1283 | goto cleanup; | |
1284 | ||
13424764 | 1285 | strbuf_reset(&sb); |
a77c3fcb AR |
1286 | default_remote = get_default_remote_submodule(path); |
1287 | if (!default_remote) | |
13424764 PC |
1288 | die(_("failed to get the default remote for submodule '%s'"), |
1289 | path); | |
1290 | ||
a77c3fcb AR |
1291 | remote_key = xstrfmt("remote.%s.url", default_remote); |
1292 | free(default_remote); | |
13424764 | 1293 | |
13424764 PC |
1294 | submodule_to_gitdir(&sb, path); |
1295 | strbuf_addstr(&sb, "/config"); | |
1296 | ||
1297 | if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url)) | |
1298 | die(_("failed to update remote for submodule '%s'"), | |
1299 | path); | |
1300 | ||
1301 | if (flags & OPT_RECURSIVE) { | |
1302 | struct child_process cpr = CHILD_PROCESS_INIT; | |
1303 | ||
1304 | cpr.git_cmd = 1; | |
1305 | cpr.dir = path; | |
29fda24d | 1306 | prepare_submodule_repo_env(&cpr.env); |
13424764 | 1307 | |
22f9b7f3 JK |
1308 | strvec_push(&cpr.args, "--super-prefix"); |
1309 | strvec_pushf(&cpr.args, "%s/", displaypath); | |
1310 | strvec_pushl(&cpr.args, "submodule--helper", "sync", | |
f6d8942b | 1311 | "--recursive", NULL); |
13424764 PC |
1312 | |
1313 | if (flags & OPT_QUIET) | |
22f9b7f3 | 1314 | strvec_push(&cpr.args, "--quiet"); |
13424764 PC |
1315 | |
1316 | if (run_command(&cpr)) | |
1317 | die(_("failed to recurse into submodule '%s'"), | |
1318 | path); | |
1319 | } | |
1320 | ||
1321 | cleanup: | |
1322 | free(super_config_url); | |
1323 | free(sub_origin_url); | |
1324 | strbuf_release(&sb); | |
1325 | free(remote_key); | |
1326 | free(displaypath); | |
1327 | free(sub_config_path); | |
1328 | } | |
1329 | ||
1330 | static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data) | |
1331 | { | |
1332 | struct sync_cb *info = cb_data; | |
1333 | sync_submodule(list_item->name, info->prefix, info->flags); | |
13424764 PC |
1334 | } |
1335 | ||
1336 | static int module_sync(int argc, const char **argv, const char *prefix) | |
1337 | { | |
1338 | struct sync_cb info = SYNC_CB_INIT; | |
1339 | struct pathspec pathspec; | |
1340 | struct module_list list = MODULE_LIST_INIT; | |
1341 | int quiet = 0; | |
1342 | int recursive = 0; | |
1343 | ||
1344 | struct option module_sync_options[] = { | |
e73fe3dd | 1345 | OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")), |
13424764 | 1346 | OPT_BOOL(0, "recursive", &recursive, |
e73fe3dd | 1347 | N_("recurse into nested submodules")), |
13424764 PC |
1348 | OPT_END() |
1349 | }; | |
1350 | ||
1351 | const char *const git_submodule_helper_usage[] = { | |
1352 | N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"), | |
1353 | NULL | |
1354 | }; | |
1355 | ||
1356 | argc = parse_options(argc, argv, prefix, module_sync_options, | |
1357 | git_submodule_helper_usage, 0); | |
1358 | ||
1359 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
1360 | return 1; | |
1361 | ||
1362 | info.prefix = prefix; | |
1363 | if (quiet) | |
1364 | info.flags |= OPT_QUIET; | |
1365 | if (recursive) | |
1366 | info.flags |= OPT_RECURSIVE; | |
1367 | ||
1368 | for_each_listed_submodule(&list, sync_submodule_cb, &info); | |
1369 | ||
1370 | return 0; | |
1371 | } | |
1372 | ||
2e612731 PC |
1373 | struct deinit_cb { |
1374 | const char *prefix; | |
1375 | unsigned int flags; | |
1376 | }; | |
9865b6e6 | 1377 | #define DEINIT_CB_INIT { 0 } |
2e612731 PC |
1378 | |
1379 | static void deinit_submodule(const char *path, const char *prefix, | |
1380 | unsigned int flags) | |
1381 | { | |
1382 | const struct submodule *sub; | |
1383 | char *displaypath = NULL; | |
1384 | struct child_process cp_config = CHILD_PROCESS_INIT; | |
1385 | struct strbuf sb_config = STRBUF_INIT; | |
1386 | char *sub_git_dir = xstrfmt("%s/.git", path); | |
1387 | ||
14228447 | 1388 | sub = submodule_from_path(the_repository, null_oid(), path); |
2e612731 PC |
1389 | |
1390 | if (!sub || !sub->name) | |
1391 | goto cleanup; | |
1392 | ||
1393 | displaypath = get_submodule_displaypath(path, prefix); | |
1394 | ||
1395 | /* remove the submodule work tree (unless the user already did it) */ | |
1396 | if (is_directory(path)) { | |
1397 | struct strbuf sb_rm = STRBUF_INIT; | |
1398 | const char *format; | |
1399 | ||
0adc8ba6 MP |
1400 | if (is_directory(sub_git_dir)) { |
1401 | if (!(flags & OPT_QUIET)) | |
1402 | warning(_("Submodule work tree '%s' contains a .git " | |
1403 | "directory. This will be replaced with a " | |
1404 | ".git file by using absorbgitdirs."), | |
1405 | displaypath); | |
1406 | ||
1407 | absorb_git_dir_into_superproject(path, | |
1408 | ABSORB_GITDIR_RECURSE_SUBMODULES); | |
1409 | ||
1410 | } | |
2e612731 PC |
1411 | |
1412 | if (!(flags & OPT_FORCE)) { | |
1413 | struct child_process cp_rm = CHILD_PROCESS_INIT; | |
1414 | cp_rm.git_cmd = 1; | |
22f9b7f3 | 1415 | strvec_pushl(&cp_rm.args, "rm", "-qn", |
f6d8942b | 1416 | path, NULL); |
2e612731 PC |
1417 | |
1418 | if (run_command(&cp_rm)) | |
1419 | die(_("Submodule work tree '%s' contains local " | |
1420 | "modifications; use '-f' to discard them"), | |
1421 | displaypath); | |
1422 | } | |
1423 | ||
1424 | strbuf_addstr(&sb_rm, path); | |
1425 | ||
1426 | if (!remove_dir_recursively(&sb_rm, 0)) | |
1427 | format = _("Cleared directory '%s'\n"); | |
1428 | else | |
1429 | format = _("Could not remove submodule work tree '%s'\n"); | |
1430 | ||
1431 | if (!(flags & OPT_QUIET)) | |
1432 | printf(format, displaypath); | |
1433 | ||
8eda5efa SB |
1434 | submodule_unset_core_worktree(sub); |
1435 | ||
2e612731 PC |
1436 | strbuf_release(&sb_rm); |
1437 | } | |
1438 | ||
1439 | if (mkdir(path, 0777)) | |
1440 | printf(_("could not create empty submodule directory %s"), | |
1441 | displaypath); | |
1442 | ||
1443 | cp_config.git_cmd = 1; | |
22f9b7f3 JK |
1444 | strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL); |
1445 | strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name); | |
2e612731 PC |
1446 | |
1447 | /* remove the .git/config entries (unless the user already did it) */ | |
1448 | if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) { | |
1449 | char *sub_key = xstrfmt("submodule.%s", sub->name); | |
1450 | /* | |
1451 | * remove the whole section so we have a clean state when | |
1452 | * the user later decides to init this submodule again | |
1453 | */ | |
1454 | git_config_rename_section_in_file(NULL, sub_key, NULL); | |
1455 | if (!(flags & OPT_QUIET)) | |
1456 | printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"), | |
1457 | sub->name, sub->url, displaypath); | |
1458 | free(sub_key); | |
1459 | } | |
1460 | ||
1461 | cleanup: | |
1462 | free(displaypath); | |
1463 | free(sub_git_dir); | |
1464 | strbuf_release(&sb_config); | |
1465 | } | |
1466 | ||
1467 | static void deinit_submodule_cb(const struct cache_entry *list_item, | |
1468 | void *cb_data) | |
1469 | { | |
1470 | struct deinit_cb *info = cb_data; | |
1471 | deinit_submodule(list_item->name, info->prefix, info->flags); | |
1472 | } | |
1473 | ||
1474 | static int module_deinit(int argc, const char **argv, const char *prefix) | |
1475 | { | |
1476 | struct deinit_cb info = DEINIT_CB_INIT; | |
1477 | struct pathspec pathspec; | |
1478 | struct module_list list = MODULE_LIST_INIT; | |
1479 | int quiet = 0; | |
1480 | int force = 0; | |
1481 | int all = 0; | |
1482 | ||
1483 | struct option module_deinit_options[] = { | |
e73fe3dd ZH |
1484 | OPT__QUIET(&quiet, N_("suppress submodule status output")), |
1485 | OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0), | |
1486 | OPT_BOOL(0, "all", &all, N_("unregister all submodules")), | |
2e612731 PC |
1487 | OPT_END() |
1488 | }; | |
1489 | ||
1490 | const char *const git_submodule_helper_usage[] = { | |
1491 | N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"), | |
1492 | NULL | |
1493 | }; | |
1494 | ||
1495 | argc = parse_options(argc, argv, prefix, module_deinit_options, | |
1496 | git_submodule_helper_usage, 0); | |
1497 | ||
1498 | if (all && argc) { | |
1499 | error("pathspec and --all are incompatible"); | |
1500 | usage_with_options(git_submodule_helper_usage, | |
1501 | module_deinit_options); | |
1502 | } | |
1503 | ||
1504 | if (!argc && !all) | |
1505 | die(_("Use '--all' if you really want to deinitialize all submodules")); | |
1506 | ||
1507 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
9748e39d | 1508 | return 1; |
2e612731 PC |
1509 | |
1510 | info.prefix = prefix; | |
1511 | if (quiet) | |
1512 | info.flags |= OPT_QUIET; | |
1513 | if (force) | |
1514 | info.flags |= OPT_FORCE; | |
1515 | ||
1516 | for_each_listed_submodule(&list, deinit_submodule_cb, &info); | |
1517 | ||
1518 | return 0; | |
1519 | } | |
1520 | ||
a98b02c1 AR |
1521 | struct module_clone_data { |
1522 | const char *prefix; | |
1523 | const char *path; | |
1524 | const char *name; | |
1525 | const char *url; | |
1526 | const char *depth; | |
f05da2b4 | 1527 | struct list_objects_filter_options *filter_options; |
a98b02c1 AR |
1528 | struct string_list reference; |
1529 | unsigned int quiet: 1; | |
1530 | unsigned int progress: 1; | |
1531 | unsigned int dissociate: 1; | |
1532 | unsigned int require_init: 1; | |
1533 | int single_branch; | |
1534 | }; | |
c9911c93 GC |
1535 | #define MODULE_CLONE_DATA_INIT { \ |
1536 | .reference = STRING_LIST_INIT_NODUP, \ | |
1537 | .single_branch = -1, \ | |
1538 | } | |
ee8838d1 | 1539 | |
31224cbd SB |
1540 | struct submodule_alternate_setup { |
1541 | const char *submodule_name; | |
1542 | enum SUBMODULE_ALTERNATE_ERROR_MODE { | |
1543 | SUBMODULE_ALTERNATE_ERROR_DIE, | |
1544 | SUBMODULE_ALTERNATE_ERROR_INFO, | |
1545 | SUBMODULE_ALTERNATE_ERROR_IGNORE | |
1546 | } error_mode; | |
1547 | struct string_list *reference; | |
1548 | }; | |
f69a6e4f ÆAB |
1549 | #define SUBMODULE_ALTERNATE_SETUP_INIT { \ |
1550 | .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \ | |
1551 | } | |
31224cbd | 1552 | |
4f3e57ef JT |
1553 | static const char alternate_error_advice[] = N_( |
1554 | "An alternate computed from a superproject's alternate is invalid.\n" | |
1555 | "To allow Git to clone without an alternate in such a case, set\n" | |
1556 | "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n" | |
1557 | "'--reference-if-able' instead of '--reference'." | |
1558 | ); | |
1559 | ||
31224cbd | 1560 | static int add_possible_reference_from_superproject( |
263db403 | 1561 | struct object_directory *odb, void *sas_cb) |
31224cbd SB |
1562 | { |
1563 | struct submodule_alternate_setup *sas = sas_cb; | |
b2ac148f | 1564 | size_t len; |
31224cbd | 1565 | |
31224cbd SB |
1566 | /* |
1567 | * If the alternate object store is another repository, try the | |
bf03b790 | 1568 | * standard layout with .git/(modules/<name>)+/objects |
31224cbd | 1569 | */ |
263db403 | 1570 | if (strip_suffix(odb->path, "/objects", &len)) { |
ce125d43 | 1571 | struct repository alternate; |
31224cbd SB |
1572 | char *sm_alternate; |
1573 | struct strbuf sb = STRBUF_INIT; | |
1574 | struct strbuf err = STRBUF_INIT; | |
263db403 | 1575 | strbuf_add(&sb, odb->path, len); |
597f9134 | 1576 | |
ce125d43 JT |
1577 | repo_init(&alternate, sb.buf, NULL); |
1578 | ||
31224cbd SB |
1579 | /* |
1580 | * We need to end the new path with '/' to mark it as a dir, | |
1581 | * otherwise a submodule name containing '/' will be broken | |
1582 | * as the last part of a missing submodule reference would | |
1583 | * be taken as a file name. | |
1584 | */ | |
ce125d43 JT |
1585 | strbuf_reset(&sb); |
1586 | submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name); | |
1587 | strbuf_addch(&sb, '/'); | |
1588 | repo_clear(&alternate); | |
31224cbd SB |
1589 | |
1590 | sm_alternate = compute_alternate_path(sb.buf, &err); | |
1591 | if (sm_alternate) { | |
1592 | string_list_append(sas->reference, xstrdup(sb.buf)); | |
1593 | free(sm_alternate); | |
1594 | } else { | |
1595 | switch (sas->error_mode) { | |
1596 | case SUBMODULE_ALTERNATE_ERROR_DIE: | |
ed9bff08 | 1597 | if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE)) |
4f3e57ef | 1598 | advise(_(alternate_error_advice)); |
31224cbd SB |
1599 | die(_("submodule '%s' cannot add alternate: %s"), |
1600 | sas->submodule_name, err.buf); | |
1601 | case SUBMODULE_ALTERNATE_ERROR_INFO: | |
1a878714 | 1602 | fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"), |
31224cbd SB |
1603 | sas->submodule_name, err.buf); |
1604 | case SUBMODULE_ALTERNATE_ERROR_IGNORE: | |
1605 | ; /* nothing */ | |
1606 | } | |
1607 | } | |
1608 | strbuf_release(&sb); | |
1609 | } | |
1610 | ||
31224cbd SB |
1611 | return 0; |
1612 | } | |
1613 | ||
1614 | static void prepare_possible_alternates(const char *sm_name, | |
1615 | struct string_list *reference) | |
1616 | { | |
1617 | char *sm_alternate = NULL, *error_strategy = NULL; | |
1618 | struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT; | |
1619 | ||
1620 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
1621 | if (!sm_alternate) | |
1622 | return; | |
1623 | ||
1624 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
1625 | ||
1626 | if (!error_strategy) | |
1627 | error_strategy = xstrdup("die"); | |
1628 | ||
1629 | sas.submodule_name = sm_name; | |
1630 | sas.reference = reference; | |
1631 | if (!strcmp(error_strategy, "die")) | |
1632 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE; | |
1633 | else if (!strcmp(error_strategy, "info")) | |
1634 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO; | |
1635 | else if (!strcmp(error_strategy, "ignore")) | |
1636 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE; | |
1637 | else | |
1638 | die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy); | |
1639 | ||
1640 | if (!strcmp(sm_alternate, "superproject")) | |
1641 | foreach_alt_odb(add_possible_reference_from_superproject, &sas); | |
1642 | else if (!strcmp(sm_alternate, "no")) | |
1643 | ; /* do nothing */ | |
1644 | else | |
1645 | die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate); | |
1646 | ||
1647 | free(sm_alternate); | |
1648 | free(error_strategy); | |
1649 | } | |
1650 | ||
a98b02c1 | 1651 | static int clone_submodule(struct module_clone_data *clone_data) |
ee8838d1 | 1652 | { |
a98b02c1 | 1653 | char *p, *sm_gitdir; |
bf03b790 | 1654 | char *sm_alternate = NULL, *error_strategy = NULL; |
a98b02c1 AR |
1655 | struct strbuf sb = STRBUF_INIT; |
1656 | struct child_process cp = CHILD_PROCESS_INIT; | |
1657 | ||
ce125d43 | 1658 | submodule_name_to_gitdir(&sb, the_repository, clone_data->name); |
a98b02c1 AR |
1659 | sm_gitdir = absolute_pathdup(sb.buf); |
1660 | strbuf_reset(&sb); | |
1661 | ||
1662 | if (!is_absolute_path(clone_data->path)) { | |
1663 | strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path); | |
1664 | clone_data->path = strbuf_detach(&sb, NULL); | |
1665 | } else { | |
1666 | clone_data->path = xstrdup(clone_data->path); | |
1667 | } | |
1668 | ||
1669 | if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) | |
1670 | die(_("refusing to create/use '%s' in another submodule's " | |
1671 | "git dir"), sm_gitdir); | |
1672 | ||
1673 | if (!file_exists(sm_gitdir)) { | |
1674 | if (safe_create_leading_directories_const(sm_gitdir) < 0) | |
1675 | die(_("could not create directory '%s'"), sm_gitdir); | |
1676 | ||
1677 | prepare_possible_alternates(clone_data->name, &clone_data->reference); | |
1678 | ||
1679 | strvec_push(&cp.args, "clone"); | |
1680 | strvec_push(&cp.args, "--no-checkout"); | |
1681 | if (clone_data->quiet) | |
1682 | strvec_push(&cp.args, "--quiet"); | |
1683 | if (clone_data->progress) | |
1684 | strvec_push(&cp.args, "--progress"); | |
1685 | if (clone_data->depth && *(clone_data->depth)) | |
1686 | strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL); | |
1687 | if (clone_data->reference.nr) { | |
1688 | struct string_list_item *item; | |
1689 | for_each_string_list_item(item, &clone_data->reference) | |
1690 | strvec_pushl(&cp.args, "--reference", | |
1691 | item->string, NULL); | |
1692 | } | |
1693 | if (clone_data->dissociate) | |
1694 | strvec_push(&cp.args, "--dissociate"); | |
1695 | if (sm_gitdir && *sm_gitdir) | |
1696 | strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL); | |
f05da2b4 JS |
1697 | if (clone_data->filter_options && clone_data->filter_options->choice) |
1698 | strvec_pushf(&cp.args, "--filter=%s", | |
1699 | expand_list_objects_filter_spec( | |
1700 | clone_data->filter_options)); | |
a98b02c1 AR |
1701 | if (clone_data->single_branch >= 0) |
1702 | strvec_push(&cp.args, clone_data->single_branch ? | |
1703 | "--single-branch" : | |
1704 | "--no-single-branch"); | |
1705 | ||
1706 | strvec_push(&cp.args, "--"); | |
1707 | strvec_push(&cp.args, clone_data->url); | |
1708 | strvec_push(&cp.args, clone_data->path); | |
1709 | ||
1710 | cp.git_cmd = 1; | |
29fda24d | 1711 | prepare_submodule_repo_env(&cp.env); |
a98b02c1 AR |
1712 | cp.no_stdin = 1; |
1713 | ||
1714 | if(run_command(&cp)) | |
1715 | die(_("clone of '%s' into submodule path '%s' failed"), | |
1716 | clone_data->url, clone_data->path); | |
1717 | } else { | |
1718 | if (clone_data->require_init && !access(clone_data->path, X_OK) && | |
1719 | !is_empty_dir(clone_data->path)) | |
1720 | die(_("directory not empty: '%s'"), clone_data->path); | |
1721 | if (safe_create_leading_directories_const(clone_data->path) < 0) | |
1722 | die(_("could not create directory '%s'"), clone_data->path); | |
1723 | strbuf_addf(&sb, "%s/index", sm_gitdir); | |
1724 | unlink_or_warn(sb.buf); | |
1725 | strbuf_reset(&sb); | |
1726 | } | |
1727 | ||
1728 | connect_work_tree_and_git_dir(clone_data->path, sm_gitdir, 0); | |
1729 | ||
1730 | p = git_pathdup_submodule(clone_data->path, "config"); | |
1731 | if (!p) | |
1732 | die(_("could not get submodule directory for '%s'"), clone_data->path); | |
1733 | ||
1734 | /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ | |
1735 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
1736 | if (sm_alternate) | |
1737 | git_config_set_in_file(p, "submodule.alternateLocation", | |
1738 | sm_alternate); | |
1739 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
1740 | if (error_strategy) | |
1741 | git_config_set_in_file(p, "submodule.alternateErrorStrategy", | |
1742 | error_strategy); | |
1743 | ||
1744 | free(sm_alternate); | |
1745 | free(error_strategy); | |
1746 | ||
1747 | strbuf_release(&sb); | |
1748 | free(sm_gitdir); | |
1749 | free(p); | |
1750 | return 0; | |
1751 | } | |
1752 | ||
1753 | static int module_clone(int argc, const char **argv, const char *prefix) | |
1754 | { | |
1755 | int dissociate = 0, quiet = 0, progress = 0, require_init = 0; | |
1756 | struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; | |
f05da2b4 | 1757 | struct list_objects_filter_options filter_options; |
ee8838d1 SB |
1758 | |
1759 | struct option module_clone_options[] = { | |
a98b02c1 | 1760 | OPT_STRING(0, "prefix", &clone_data.prefix, |
ee8838d1 SB |
1761 | N_("path"), |
1762 | N_("alternative anchor for relative paths")), | |
a98b02c1 | 1763 | OPT_STRING(0, "path", &clone_data.path, |
ee8838d1 SB |
1764 | N_("path"), |
1765 | N_("where the new submodule will be cloned to")), | |
a98b02c1 | 1766 | OPT_STRING(0, "name", &clone_data.name, |
ee8838d1 SB |
1767 | N_("string"), |
1768 | N_("name of the new submodule")), | |
a98b02c1 | 1769 | OPT_STRING(0, "url", &clone_data.url, |
ee8838d1 SB |
1770 | N_("string"), |
1771 | N_("url where to clone the submodule from")), | |
a98b02c1 | 1772 | OPT_STRING_LIST(0, "reference", &clone_data.reference, |
965dbea0 | 1773 | N_("repo"), |
ee8838d1 | 1774 | N_("reference repository")), |
a0ef2934 CF |
1775 | OPT_BOOL(0, "dissociate", &dissociate, |
1776 | N_("use --reference only while cloning")), | |
a98b02c1 | 1777 | OPT_STRING(0, "depth", &clone_data.depth, |
ee8838d1 SB |
1778 | N_("string"), |
1779 | N_("depth for shallow clones")), | |
9e1f22c8 | 1780 | OPT__QUIET(&quiet, "suppress output for cloning a submodule"), |
72c5f883 JK |
1781 | OPT_BOOL(0, "progress", &progress, |
1782 | N_("force cloning progress")), | |
0060fd15 JS |
1783 | OPT_BOOL(0, "require-init", &require_init, |
1784 | N_("disallow cloning into non-empty directory")), | |
a98b02c1 | 1785 | OPT_BOOL(0, "single-branch", &clone_data.single_branch, |
132f600b | 1786 | N_("clone only one branch, HEAD or --branch")), |
f05da2b4 | 1787 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
ee8838d1 SB |
1788 | OPT_END() |
1789 | }; | |
1790 | ||
1791 | const char *const git_submodule_helper_usage[] = { | |
1792 | N_("git submodule--helper clone [--prefix=<path>] [--quiet] " | |
7dad2633 | 1793 | "[--reference <repository>] [--name <name>] [--depth <depth>] " |
5da9560e | 1794 | "[--single-branch] [--filter <filter-spec>] " |
7dad2633 | 1795 | "--url <url> --path <path>"), |
ee8838d1 SB |
1796 | NULL |
1797 | }; | |
1798 | ||
f05da2b4 | 1799 | memset(&filter_options, 0, sizeof(filter_options)); |
ee8838d1 SB |
1800 | argc = parse_options(argc, argv, prefix, module_clone_options, |
1801 | git_submodule_helper_usage, 0); | |
1802 | ||
a98b02c1 AR |
1803 | clone_data.dissociate = !!dissociate; |
1804 | clone_data.quiet = !!quiet; | |
1805 | clone_data.progress = !!progress; | |
1806 | clone_data.require_init = !!require_init; | |
f05da2b4 | 1807 | clone_data.filter_options = &filter_options; |
a98b02c1 AR |
1808 | |
1809 | if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path)) | |
08e0970a JK |
1810 | usage_with_options(git_submodule_helper_usage, |
1811 | module_clone_options); | |
1812 | ||
a98b02c1 | 1813 | clone_submodule(&clone_data); |
f05da2b4 | 1814 | list_objects_filter_release(&filter_options); |
ee8838d1 SB |
1815 | return 0; |
1816 | } | |
74703a1e | 1817 | |
ee69b2a9 SB |
1818 | static void determine_submodule_update_strategy(struct repository *r, |
1819 | int just_cloned, | |
1820 | const char *path, | |
1821 | const char *update, | |
1822 | struct submodule_update_strategy *out) | |
1823 | { | |
14228447 | 1824 | const struct submodule *sub = submodule_from_path(r, null_oid(), path); |
ee69b2a9 SB |
1825 | char *key; |
1826 | const char *val; | |
1827 | ||
1828 | key = xstrfmt("submodule.%s.update", sub->name); | |
1829 | ||
1830 | if (update) { | |
ee69b2a9 SB |
1831 | if (parse_submodule_update_strategy(update, out) < 0) |
1832 | die(_("Invalid update mode '%s' for submodule path '%s'"), | |
1833 | update, path); | |
f1de981e | 1834 | } else if (!repo_config_get_string_tmp(r, key, &val)) { |
ee69b2a9 SB |
1835 | if (parse_submodule_update_strategy(val, out) < 0) |
1836 | die(_("Invalid update mode '%s' configured for submodule path '%s'"), | |
1837 | val, path); | |
1838 | } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
c1547450 JN |
1839 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) |
1840 | BUG("how did we read update = !command from .gitmodules?"); | |
ee69b2a9 SB |
1841 | out->type = sub->update_strategy.type; |
1842 | out->command = sub->update_strategy.command; | |
1843 | } else | |
1844 | out->type = SM_UPDATE_CHECKOUT; | |
1845 | ||
1846 | if (just_cloned && | |
1847 | (out->type == SM_UPDATE_MERGE || | |
1848 | out->type == SM_UPDATE_REBASE || | |
1849 | out->type == SM_UPDATE_NONE)) | |
1850 | out->type = SM_UPDATE_CHECKOUT; | |
1851 | ||
1852 | free(key); | |
1853 | } | |
1854 | ||
f1d15713 SB |
1855 | struct update_clone_data { |
1856 | const struct submodule *sub; | |
1857 | struct object_id oid; | |
1858 | unsigned just_cloned; | |
1859 | }; | |
1860 | ||
48308681 | 1861 | struct submodule_update_clone { |
c9911c93 | 1862 | /* index into 'update_data.list', the list of submodules to look into for cloning */ |
48308681 | 1863 | int current; |
48308681 SB |
1864 | |
1865 | /* configuration parameters which are passed on to the children */ | |
c9911c93 | 1866 | struct update_data *update_data; |
48308681 | 1867 | |
b3c5f5cb | 1868 | /* to be consumed by update_submodule() */ |
f1d15713 SB |
1869 | struct update_clone_data *update_clone; |
1870 | int update_clone_nr; int update_clone_alloc; | |
48308681 SB |
1871 | |
1872 | /* If we want to stop as fast as possible and return an error */ | |
1873 | unsigned quickstop : 1; | |
665b35ec SB |
1874 | |
1875 | /* failed clones to be retried again */ | |
1876 | const struct cache_entry **failed_clones; | |
1877 | int failed_clones_nr, failed_clones_alloc; | |
48308681 | 1878 | }; |
c9911c93 | 1879 | #define SUBMODULE_UPDATE_CLONE_INIT { 0 } |
48308681 | 1880 | |
c51f8f94 | 1881 | struct update_data { |
c9911c93 | 1882 | const char *prefix; |
c51f8f94 | 1883 | const char *recursive_prefix; |
c51f8f94 | 1884 | const char *displaypath; |
c9911c93 | 1885 | const char *update_default; |
c51f8f94 | 1886 | struct object_id suboid; |
c9911c93 | 1887 | struct string_list references; |
c51f8f94 | 1888 | struct submodule_update_strategy update_strategy; |
c9911c93 GC |
1889 | struct list_objects_filter_options *filter_options; |
1890 | struct module_list list; | |
c51f8f94 | 1891 | int depth; |
c9911c93 GC |
1892 | int max_jobs; |
1893 | int single_branch; | |
1894 | int recommend_shallow; | |
1895 | unsigned int require_init; | |
ed9c8485 ÆAB |
1896 | unsigned int force; |
1897 | unsigned int quiet; | |
1898 | unsigned int nofetch; | |
1012a5cb | 1899 | unsigned int remote; |
c9911c93 GC |
1900 | unsigned int progress; |
1901 | unsigned int dissociate; | |
1902 | unsigned int init; | |
1903 | unsigned int warn_if_uninitialized; | |
b3c5f5cb AR |
1904 | unsigned int recursive; |
1905 | ||
1906 | /* copied over from update_clone_data */ | |
1907 | struct object_id oid; | |
1908 | unsigned int just_cloned; | |
1909 | const char *sm_path; | |
c51f8f94 | 1910 | }; |
c9911c93 GC |
1911 | #define UPDATE_DATA_INIT { \ |
1912 | .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \ | |
1913 | .list = MODULE_LIST_INIT, \ | |
1914 | .recommend_shallow = -1, \ | |
1915 | .references = STRING_LIST_INIT_DUP, \ | |
1916 | .single_branch = -1, \ | |
1917 | .max_jobs = 1, \ | |
c9911c93 | 1918 | } |
08fdbdb1 SB |
1919 | |
1920 | static void next_submodule_warn_missing(struct submodule_update_clone *suc, | |
1921 | struct strbuf *out, const char *displaypath) | |
1922 | { | |
1923 | /* | |
1924 | * Only mention uninitialized submodules when their | |
1925 | * paths have been specified. | |
1926 | */ | |
c9911c93 | 1927 | if (suc->update_data->warn_if_uninitialized) { |
08fdbdb1 SB |
1928 | strbuf_addf(out, |
1929 | _("Submodule path '%s' not initialized"), | |
1930 | displaypath); | |
1931 | strbuf_addch(out, '\n'); | |
1932 | strbuf_addstr(out, | |
1933 | _("Maybe you want to use 'update --init'?")); | |
1934 | strbuf_addch(out, '\n'); | |
1935 | } | |
1936 | } | |
1937 | ||
48308681 SB |
1938 | /** |
1939 | * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to | |
1940 | * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise. | |
1941 | */ | |
1942 | static int prepare_to_clone_next_submodule(const struct cache_entry *ce, | |
1943 | struct child_process *child, | |
1944 | struct submodule_update_clone *suc, | |
1945 | struct strbuf *out) | |
1946 | { | |
1947 | const struct submodule *sub = NULL; | |
ec6141a0 BW |
1948 | const char *url = NULL; |
1949 | const char *update_string; | |
1950 | enum submodule_update_type update_type; | |
1951 | char *key; | |
48308681 SB |
1952 | struct strbuf displaypath_sb = STRBUF_INIT; |
1953 | struct strbuf sb = STRBUF_INIT; | |
1954 | const char *displaypath = NULL; | |
48308681 | 1955 | int needs_cloning = 0; |
e0a862fd | 1956 | int need_free_url = 0; |
48308681 SB |
1957 | |
1958 | if (ce_stage(ce)) { | |
c9911c93 GC |
1959 | if (suc->update_data->recursive_prefix) |
1960 | strbuf_addf(&sb, "%s/%s", suc->update_data->recursive_prefix, ce->name); | |
48308681 | 1961 | else |
92d52fab | 1962 | strbuf_addstr(&sb, ce->name); |
48308681 SB |
1963 | strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf); |
1964 | strbuf_addch(out, '\n'); | |
1965 | goto cleanup; | |
1966 | } | |
1967 | ||
14228447 | 1968 | sub = submodule_from_path(the_repository, null_oid(), ce->name); |
48308681 | 1969 | |
c9911c93 GC |
1970 | if (suc->update_data->recursive_prefix) |
1971 | displaypath = relative_path(suc->update_data->recursive_prefix, | |
48308681 SB |
1972 | ce->name, &displaypath_sb); |
1973 | else | |
1974 | displaypath = ce->name; | |
1975 | ||
08fdbdb1 SB |
1976 | if (!sub) { |
1977 | next_submodule_warn_missing(suc, out, displaypath); | |
1978 | goto cleanup; | |
1979 | } | |
1980 | ||
ec6141a0 | 1981 | key = xstrfmt("submodule.%s.update", sub->name); |
f1de981e | 1982 | if (!repo_config_get_string_tmp(the_repository, key, &update_string)) { |
ec6141a0 BW |
1983 | update_type = parse_submodule_update_type(update_string); |
1984 | } else { | |
1985 | update_type = sub->update_strategy.type; | |
1986 | } | |
1987 | free(key); | |
1988 | ||
c9911c93 GC |
1989 | if (suc->update_data->update_strategy.type == SM_UPDATE_NONE |
1990 | || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED | |
ec6141a0 | 1991 | && update_type == SM_UPDATE_NONE)) { |
48308681 SB |
1992 | strbuf_addf(out, _("Skipping submodule '%s'"), displaypath); |
1993 | strbuf_addch(out, '\n'); | |
1994 | goto cleanup; | |
1995 | } | |
1996 | ||
ee92ab99 | 1997 | /* Check if the submodule has been initialized. */ |
627d9342 | 1998 | if (!is_submodule_active(the_repository, ce->name)) { |
08fdbdb1 | 1999 | next_submodule_warn_missing(suc, out, displaypath); |
48308681 SB |
2000 | goto cleanup; |
2001 | } | |
2002 | ||
ec6141a0 BW |
2003 | strbuf_reset(&sb); |
2004 | strbuf_addf(&sb, "submodule.%s.url", sub->name); | |
f1de981e | 2005 | if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) { |
e0a862fd SB |
2006 | if (starts_with_dot_slash(sub->url) || |
2007 | starts_with_dot_dot_slash(sub->url)) { | |
de0fcbe0 | 2008 | url = resolve_relative_url(sub->url, NULL, 0); |
e0a862fd SB |
2009 | need_free_url = 1; |
2010 | } else | |
2011 | url = sub->url; | |
2012 | } | |
ec6141a0 | 2013 | |
48308681 SB |
2014 | strbuf_reset(&sb); |
2015 | strbuf_addf(&sb, "%s/.git", ce->name); | |
2016 | needs_cloning = !file_exists(sb.buf); | |
2017 | ||
f1d15713 SB |
2018 | ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1, |
2019 | suc->update_clone_alloc); | |
2020 | oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid); | |
2021 | suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning; | |
2022 | suc->update_clone[suc->update_clone_nr].sub = sub; | |
2023 | suc->update_clone_nr++; | |
48308681 SB |
2024 | |
2025 | if (!needs_cloning) | |
2026 | goto cleanup; | |
2027 | ||
2028 | child->git_cmd = 1; | |
2029 | child->no_stdin = 1; | |
2030 | child->stdout_to_stderr = 1; | |
2031 | child->err = -1; | |
22f9b7f3 JK |
2032 | strvec_push(&child->args, "submodule--helper"); |
2033 | strvec_push(&child->args, "clone"); | |
c9911c93 | 2034 | if (suc->update_data->progress) |
22f9b7f3 | 2035 | strvec_push(&child->args, "--progress"); |
c9911c93 | 2036 | if (suc->update_data->quiet) |
22f9b7f3 | 2037 | strvec_push(&child->args, "--quiet"); |
c9911c93 GC |
2038 | if (suc->update_data->prefix) |
2039 | strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL); | |
2040 | if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1) | |
22f9b7f3 | 2041 | strvec_push(&child->args, "--depth=1"); |
c9911c93 GC |
2042 | else if (suc->update_data->depth) |
2043 | strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth); | |
2044 | if (suc->update_data->filter_options && suc->update_data->filter_options->choice) | |
f05da2b4 | 2045 | strvec_pushf(&child->args, "--filter=%s", |
c9911c93 GC |
2046 | expand_list_objects_filter_spec(suc->update_data->filter_options)); |
2047 | if (suc->update_data->require_init) | |
22f9b7f3 JK |
2048 | strvec_push(&child->args, "--require-init"); |
2049 | strvec_pushl(&child->args, "--path", sub->path, NULL); | |
2050 | strvec_pushl(&child->args, "--name", sub->name, NULL); | |
2051 | strvec_pushl(&child->args, "--url", url, NULL); | |
c9911c93 | 2052 | if (suc->update_data->references.nr) { |
5f50f33e | 2053 | struct string_list_item *item; |
c9911c93 | 2054 | for_each_string_list_item(item, &suc->update_data->references) |
22f9b7f3 | 2055 | strvec_pushl(&child->args, "--reference", item->string, NULL); |
5f50f33e | 2056 | } |
c9911c93 | 2057 | if (suc->update_data->dissociate) |
22f9b7f3 | 2058 | strvec_push(&child->args, "--dissociate"); |
c9911c93 GC |
2059 | if (suc->update_data->single_branch >= 0) |
2060 | strvec_push(&child->args, suc->update_data->single_branch ? | |
132f600b ES |
2061 | "--single-branch" : |
2062 | "--no-single-branch"); | |
48308681 SB |
2063 | |
2064 | cleanup: | |
9101c8ea JK |
2065 | strbuf_release(&displaypath_sb); |
2066 | strbuf_release(&sb); | |
e0a862fd SB |
2067 | if (need_free_url) |
2068 | free((void*)url); | |
48308681 SB |
2069 | |
2070 | return needs_cloning; | |
2071 | } | |
2072 | ||
2073 | static int update_clone_get_next_task(struct child_process *child, | |
2074 | struct strbuf *err, | |
2075 | void *suc_cb, | |
665b35ec | 2076 | void **idx_task_cb) |
48308681 SB |
2077 | { |
2078 | struct submodule_update_clone *suc = suc_cb; | |
665b35ec SB |
2079 | const struct cache_entry *ce; |
2080 | int index; | |
48308681 | 2081 | |
c9911c93 GC |
2082 | for (; suc->current < suc->update_data->list.nr; suc->current++) { |
2083 | ce = suc->update_data->list.entries[suc->current]; | |
48308681 | 2084 | if (prepare_to_clone_next_submodule(ce, child, suc, err)) { |
665b35ec SB |
2085 | int *p = xmalloc(sizeof(*p)); |
2086 | *p = suc->current; | |
2087 | *idx_task_cb = p; | |
48308681 SB |
2088 | suc->current++; |
2089 | return 1; | |
2090 | } | |
2091 | } | |
665b35ec SB |
2092 | |
2093 | /* | |
2094 | * The loop above tried cloning each submodule once, now try the | |
2095 | * stragglers again, which we can imagine as an extension of the | |
2096 | * entry list. | |
2097 | */ | |
c9911c93 | 2098 | index = suc->current - suc->update_data->list.nr; |
665b35ec SB |
2099 | if (index < suc->failed_clones_nr) { |
2100 | int *p; | |
2101 | ce = suc->failed_clones[index]; | |
2201ee09 SB |
2102 | if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { |
2103 | suc->current ++; | |
a22ae753 RS |
2104 | strbuf_addstr(err, "BUG: submodule considered for " |
2105 | "cloning, doesn't need cloning " | |
2106 | "any more?\n"); | |
2201ee09 SB |
2107 | return 0; |
2108 | } | |
665b35ec SB |
2109 | p = xmalloc(sizeof(*p)); |
2110 | *p = suc->current; | |
2111 | *idx_task_cb = p; | |
2112 | suc->current ++; | |
2113 | return 1; | |
2114 | } | |
2115 | ||
48308681 SB |
2116 | return 0; |
2117 | } | |
2118 | ||
2119 | static int update_clone_start_failure(struct strbuf *err, | |
2120 | void *suc_cb, | |
665b35ec | 2121 | void *idx_task_cb) |
48308681 SB |
2122 | { |
2123 | struct submodule_update_clone *suc = suc_cb; | |
2124 | suc->quickstop = 1; | |
2125 | return 1; | |
2126 | } | |
2127 | ||
2128 | static int update_clone_task_finished(int result, | |
2129 | struct strbuf *err, | |
2130 | void *suc_cb, | |
665b35ec | 2131 | void *idx_task_cb) |
48308681 | 2132 | { |
665b35ec | 2133 | const struct cache_entry *ce; |
48308681 SB |
2134 | struct submodule_update_clone *suc = suc_cb; |
2135 | ||
c1e860f1 | 2136 | int *idxP = idx_task_cb; |
665b35ec SB |
2137 | int idx = *idxP; |
2138 | free(idxP); | |
2139 | ||
48308681 SB |
2140 | if (!result) |
2141 | return 0; | |
2142 | ||
c9911c93 GC |
2143 | if (idx < suc->update_data->list.nr) { |
2144 | ce = suc->update_data->list.entries[idx]; | |
665b35ec SB |
2145 | strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"), |
2146 | ce->name); | |
2147 | strbuf_addch(err, '\n'); | |
2148 | ALLOC_GROW(suc->failed_clones, | |
2149 | suc->failed_clones_nr + 1, | |
2150 | suc->failed_clones_alloc); | |
2151 | suc->failed_clones[suc->failed_clones_nr++] = ce; | |
2152 | return 0; | |
2153 | } else { | |
c9911c93 | 2154 | idx -= suc->update_data->list.nr; |
665b35ec SB |
2155 | ce = suc->failed_clones[idx]; |
2156 | strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"), | |
2157 | ce->name); | |
2158 | strbuf_addch(err, '\n'); | |
2159 | suc->quickstop = 1; | |
2160 | return 1; | |
2161 | } | |
2162 | ||
2163 | return 0; | |
48308681 SB |
2164 | } |
2165 | ||
05744997 AO |
2166 | static int git_update_clone_config(const char *var, const char *value, |
2167 | void *cb) | |
f20e7c1e BW |
2168 | { |
2169 | int *max_jobs = cb; | |
2170 | if (!strcmp(var, "submodule.fetchjobs")) | |
2171 | *max_jobs = parse_submodule_fetchjobs(var, value); | |
2172 | return 0; | |
2173 | } | |
2174 | ||
c51f8f94 AR |
2175 | static int is_tip_reachable(const char *path, struct object_id *oid) |
2176 | { | |
2177 | struct child_process cp = CHILD_PROCESS_INIT; | |
2178 | struct strbuf rev = STRBUF_INIT; | |
2179 | char *hex = oid_to_hex(oid); | |
2180 | ||
2181 | cp.git_cmd = 1; | |
2182 | cp.dir = xstrdup(path); | |
2183 | cp.no_stderr = 1; | |
2184 | strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL); | |
2185 | ||
29fda24d | 2186 | prepare_submodule_repo_env(&cp.env); |
c51f8f94 AR |
2187 | |
2188 | if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len) | |
2189 | return 0; | |
2190 | ||
2191 | return 1; | |
2192 | } | |
2193 | ||
2194 | static int fetch_in_submodule(const char *module_path, int depth, int quiet, struct object_id *oid) | |
2195 | { | |
2196 | struct child_process cp = CHILD_PROCESS_INIT; | |
2197 | ||
29fda24d | 2198 | prepare_submodule_repo_env(&cp.env); |
c51f8f94 AR |
2199 | cp.git_cmd = 1; |
2200 | cp.dir = xstrdup(module_path); | |
2201 | ||
2202 | strvec_push(&cp.args, "fetch"); | |
2203 | if (quiet) | |
2204 | strvec_push(&cp.args, "--quiet"); | |
2205 | if (depth) | |
2206 | strvec_pushf(&cp.args, "--depth=%d", depth); | |
2207 | if (oid) { | |
2208 | char *hex = oid_to_hex(oid); | |
2209 | char *remote = get_default_remote(); | |
2210 | strvec_pushl(&cp.args, remote, hex, NULL); | |
2211 | } | |
2212 | ||
2213 | return run_command(&cp); | |
2214 | } | |
2215 | ||
2216 | static int run_update_command(struct update_data *ud, int subforce) | |
2217 | { | |
3c3558f0 | 2218 | struct child_process cp = CHILD_PROCESS_INIT; |
c51f8f94 AR |
2219 | char *oid = oid_to_hex(&ud->oid); |
2220 | int must_die_on_failure = 0; | |
c51f8f94 AR |
2221 | |
2222 | switch (ud->update_strategy.type) { | |
2223 | case SM_UPDATE_CHECKOUT: | |
3c3558f0 AR |
2224 | cp.git_cmd = 1; |
2225 | strvec_pushl(&cp.args, "checkout", "-q", NULL); | |
c51f8f94 | 2226 | if (subforce) |
3c3558f0 | 2227 | strvec_push(&cp.args, "-f"); |
c51f8f94 AR |
2228 | break; |
2229 | case SM_UPDATE_REBASE: | |
3c3558f0 AR |
2230 | cp.git_cmd = 1; |
2231 | strvec_push(&cp.args, "rebase"); | |
c51f8f94 | 2232 | if (ud->quiet) |
3c3558f0 | 2233 | strvec_push(&cp.args, "--quiet"); |
c51f8f94 AR |
2234 | must_die_on_failure = 1; |
2235 | break; | |
2236 | case SM_UPDATE_MERGE: | |
3c3558f0 AR |
2237 | cp.git_cmd = 1; |
2238 | strvec_push(&cp.args, "merge"); | |
c51f8f94 | 2239 | if (ud->quiet) |
3c3558f0 | 2240 | strvec_push(&cp.args, "--quiet"); |
c51f8f94 AR |
2241 | must_die_on_failure = 1; |
2242 | break; | |
2243 | case SM_UPDATE_COMMAND: | |
3c3558f0 AR |
2244 | cp.use_shell = 1; |
2245 | strvec_push(&cp.args, ud->update_strategy.command); | |
c51f8f94 AR |
2246 | must_die_on_failure = 1; |
2247 | break; | |
2248 | default: | |
2249 | BUG("unexpected update strategy type: %s", | |
2250 | submodule_strategy_to_string(&ud->update_strategy)); | |
2251 | } | |
3c3558f0 | 2252 | strvec_push(&cp.args, oid); |
c51f8f94 | 2253 | |
3c3558f0 | 2254 | cp.dir = xstrdup(ud->sm_path); |
29fda24d | 2255 | prepare_submodule_repo_env(&cp.env); |
3c3558f0 | 2256 | if (run_command(&cp)) { |
c51f8f94 AR |
2257 | switch (ud->update_strategy.type) { |
2258 | case SM_UPDATE_CHECKOUT: | |
55b3f12c GC |
2259 | die_message(_("Unable to checkout '%s' in submodule path '%s'"), |
2260 | oid, ud->displaypath); | |
c51f8f94 AR |
2261 | break; |
2262 | case SM_UPDATE_REBASE: | |
55b3f12c GC |
2263 | die_message(_("Unable to rebase '%s' in submodule path '%s'"), |
2264 | oid, ud->displaypath); | |
c51f8f94 AR |
2265 | break; |
2266 | case SM_UPDATE_MERGE: | |
55b3f12c GC |
2267 | die_message(_("Unable to merge '%s' in submodule path '%s'"), |
2268 | oid, ud->displaypath); | |
c51f8f94 AR |
2269 | break; |
2270 | case SM_UPDATE_COMMAND: | |
55b3f12c GC |
2271 | die_message(_("Execution of '%s %s' failed in submodule path '%s'"), |
2272 | ud->update_strategy.command, oid, ud->displaypath); | |
c51f8f94 AR |
2273 | break; |
2274 | default: | |
2275 | BUG("unexpected update strategy type: %s", | |
2276 | submodule_strategy_to_string(&ud->update_strategy)); | |
2277 | } | |
c51f8f94 | 2278 | if (must_die_on_failure) |
55b3f12c GC |
2279 | exit(128); |
2280 | ||
2281 | /* the command failed, but update must continue */ | |
c51f8f94 AR |
2282 | return 1; |
2283 | } | |
2284 | ||
55b3f12c GC |
2285 | if (ud->quiet) |
2286 | return 0; | |
2287 | ||
c51f8f94 AR |
2288 | switch (ud->update_strategy.type) { |
2289 | case SM_UPDATE_CHECKOUT: | |
2290 | printf(_("Submodule path '%s': checked out '%s'\n"), | |
2291 | ud->displaypath, oid); | |
2292 | break; | |
2293 | case SM_UPDATE_REBASE: | |
2294 | printf(_("Submodule path '%s': rebased into '%s'\n"), | |
2295 | ud->displaypath, oid); | |
2296 | break; | |
2297 | case SM_UPDATE_MERGE: | |
2298 | printf(_("Submodule path '%s': merged in '%s'\n"), | |
2299 | ud->displaypath, oid); | |
2300 | break; | |
2301 | case SM_UPDATE_COMMAND: | |
2302 | printf(_("Submodule path '%s': '%s %s'\n"), | |
2303 | ud->displaypath, ud->update_strategy.command, oid); | |
2304 | break; | |
2305 | default: | |
2306 | BUG("unexpected update strategy type: %s", | |
2307 | submodule_strategy_to_string(&ud->update_strategy)); | |
2308 | } | |
2309 | ||
2310 | return 0; | |
2311 | } | |
2312 | ||
b3c5f5cb | 2313 | static int run_update_procedure(struct update_data *ud) |
c51f8f94 AR |
2314 | { |
2315 | int subforce = is_null_oid(&ud->suboid) || ud->force; | |
2316 | ||
2317 | if (!ud->nofetch) { | |
2318 | /* | |
2319 | * Run fetch only if `oid` isn't present or it | |
2320 | * is not reachable from a ref. | |
2321 | */ | |
2322 | if (!is_tip_reachable(ud->sm_path, &ud->oid) && | |
2323 | fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) && | |
2324 | !ud->quiet) | |
2325 | fprintf_ln(stderr, | |
2326 | _("Unable to fetch in submodule path '%s'; " | |
2327 | "trying to directly fetch %s:"), | |
2328 | ud->displaypath, oid_to_hex(&ud->oid)); | |
2329 | /* | |
2330 | * Now we tried the usual fetch, but `oid` may | |
2331 | * not be reachable from any of the refs. | |
2332 | */ | |
2333 | if (!is_tip_reachable(ud->sm_path, &ud->oid) && | |
2334 | fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid)) | |
2335 | die(_("Fetched in submodule path '%s', but it did not " | |
2336 | "contain %s. Direct fetching of that commit failed."), | |
2337 | ud->displaypath, oid_to_hex(&ud->oid)); | |
2338 | } | |
2339 | ||
2340 | return run_update_command(ud, subforce); | |
2341 | } | |
2342 | ||
f3875ab1 GC |
2343 | static const char *remote_submodule_branch(const char *path) |
2344 | { | |
2345 | const struct submodule *sub; | |
2346 | const char *branch = NULL; | |
2347 | char *key; | |
2348 | ||
2349 | sub = submodule_from_path(the_repository, null_oid(), path); | |
2350 | if (!sub) | |
2351 | return NULL; | |
2352 | ||
2353 | key = xstrfmt("submodule.%s.branch", sub->name); | |
2354 | if (repo_config_get_string_tmp(the_repository, key, &branch)) | |
2355 | branch = sub->branch; | |
2356 | free(key); | |
2357 | ||
2358 | if (!branch) | |
2359 | return "HEAD"; | |
2360 | ||
2361 | if (!strcmp(branch, ".")) { | |
2362 | const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); | |
2363 | ||
2364 | if (!refname) | |
2365 | die(_("No such ref: %s"), "HEAD"); | |
2366 | ||
2367 | /* detached HEAD */ | |
2368 | if (!strcmp(refname, "HEAD")) | |
2369 | die(_("Submodule (%s) branch configured to inherit " | |
2370 | "branch from superproject, but the superproject " | |
2371 | "is not on any branch"), sub->name); | |
2372 | ||
2373 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
2374 | die(_("Expecting a full ref name, got %s"), refname); | |
2375 | return refname; | |
2376 | } | |
2377 | ||
2378 | return branch; | |
2379 | } | |
2380 | ||
2381 | static void ensure_core_worktree(const char *path) | |
2382 | { | |
2383 | const char *cw; | |
2384 | struct repository subrepo; | |
2385 | ||
2386 | if (repo_submodule_init(&subrepo, the_repository, path, null_oid())) | |
2387 | die(_("could not get a repository handle for submodule '%s'"), path); | |
2388 | ||
2389 | if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) { | |
2390 | char *cfg_file, *abs_path; | |
2391 | const char *rel_path; | |
2392 | struct strbuf sb = STRBUF_INIT; | |
2393 | ||
2394 | cfg_file = repo_git_path(&subrepo, "config"); | |
2395 | ||
2396 | abs_path = absolute_pathdup(path); | |
2397 | rel_path = relative_path(abs_path, subrepo.gitdir, &sb); | |
2398 | ||
2399 | git_config_set_in_file(cfg_file, "core.worktree", rel_path); | |
2400 | ||
2401 | free(cfg_file); | |
2402 | free(abs_path); | |
2403 | strbuf_release(&sb); | |
2404 | } | |
2405 | } | |
2406 | ||
2407 | static void update_data_to_args(struct update_data *update_data, struct strvec *args) | |
2408 | { | |
2409 | strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL); | |
2410 | strvec_pushf(args, "--jobs=%d", update_data->max_jobs); | |
2411 | if (update_data->recursive_prefix) | |
2412 | strvec_pushl(args, "--recursive-prefix", | |
2413 | update_data->recursive_prefix, NULL); | |
2414 | if (update_data->quiet) | |
2415 | strvec_push(args, "--quiet"); | |
2416 | if (update_data->force) | |
2417 | strvec_push(args, "--force"); | |
2418 | if (update_data->init) | |
2419 | strvec_push(args, "--init"); | |
2420 | if (update_data->remote) | |
2421 | strvec_push(args, "--remote"); | |
2422 | if (update_data->nofetch) | |
2423 | strvec_push(args, "--no-fetch"); | |
2424 | if (update_data->dissociate) | |
2425 | strvec_push(args, "--dissociate"); | |
2426 | if (update_data->progress) | |
2427 | strvec_push(args, "--progress"); | |
2428 | if (update_data->require_init) | |
2429 | strvec_push(args, "--require-init"); | |
2430 | if (update_data->depth) | |
2431 | strvec_pushf(args, "--depth=%d", update_data->depth); | |
2432 | if (update_data->update_default) | |
2433 | strvec_pushl(args, "--update", update_data->update_default, NULL); | |
2434 | if (update_data->references.nr) { | |
2435 | struct string_list_item *item; | |
2436 | for_each_string_list_item(item, &update_data->references) | |
2437 | strvec_pushl(args, "--reference", item->string, NULL); | |
2438 | } | |
2439 | if (update_data->filter_options && update_data->filter_options->choice) | |
2440 | strvec_pushf(args, "--filter=%s", | |
2441 | expand_list_objects_filter_spec( | |
2442 | update_data->filter_options)); | |
2443 | if (update_data->recommend_shallow == 0) | |
2444 | strvec_push(args, "--no-recommend-shallow"); | |
2445 | else if (update_data->recommend_shallow == 1) | |
2446 | strvec_push(args, "--recommend-shallow"); | |
2447 | if (update_data->single_branch >= 0) | |
2448 | strvec_push(args, update_data->single_branch ? | |
2449 | "--single-branch" : | |
2450 | "--no-single-branch"); | |
2451 | } | |
2452 | ||
2453 | static int update_submodule(struct update_data *update_data) | |
2454 | { | |
2455 | char *prefixed_path; | |
2456 | ||
2457 | ensure_core_worktree(update_data->sm_path); | |
2458 | ||
2459 | if (update_data->recursive_prefix) | |
2460 | prefixed_path = xstrfmt("%s%s", update_data->recursive_prefix, | |
2461 | update_data->sm_path); | |
2462 | else | |
2463 | prefixed_path = xstrdup(update_data->sm_path); | |
2464 | ||
2465 | update_data->displaypath = get_submodule_displaypath(prefixed_path, | |
2466 | update_data->prefix); | |
2467 | free(prefixed_path); | |
2468 | ||
2469 | determine_submodule_update_strategy(the_repository, update_data->just_cloned, | |
2470 | update_data->sm_path, update_data->update_default, | |
2471 | &update_data->update_strategy); | |
2472 | ||
2473 | if (update_data->just_cloned) | |
2474 | oidcpy(&update_data->suboid, null_oid()); | |
2475 | else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid)) | |
2476 | die(_("Unable to find current revision in submodule path '%s'"), | |
2477 | update_data->displaypath); | |
2478 | ||
2479 | if (update_data->remote) { | |
2480 | char *remote_name = get_default_remote_submodule(update_data->sm_path); | |
2481 | const char *branch = remote_submodule_branch(update_data->sm_path); | |
2482 | char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch); | |
2483 | ||
2484 | if (!update_data->nofetch) { | |
2485 | if (fetch_in_submodule(update_data->sm_path, update_data->depth, | |
2486 | 0, NULL)) | |
2487 | die(_("Unable to fetch in submodule path '%s'"), | |
2488 | update_data->sm_path); | |
2489 | } | |
2490 | ||
2491 | if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid)) | |
2492 | die(_("Unable to find %s revision in submodule path '%s'"), | |
2493 | remote_ref, update_data->sm_path); | |
2494 | ||
2495 | free(remote_ref); | |
2496 | } | |
2497 | ||
2498 | if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) | |
2499 | if (run_update_procedure(update_data)) | |
2500 | return 1; | |
2501 | ||
2502 | if (update_data->recursive) { | |
2503 | struct child_process cp = CHILD_PROCESS_INIT; | |
2504 | struct update_data next = *update_data; | |
2505 | int res; | |
2506 | ||
2507 | if (update_data->recursive_prefix) | |
2508 | prefixed_path = xstrfmt("%s%s/", update_data->recursive_prefix, | |
2509 | update_data->sm_path); | |
2510 | else | |
2511 | prefixed_path = xstrfmt("%s/", update_data->sm_path); | |
2512 | ||
2513 | next.recursive_prefix = get_submodule_displaypath(prefixed_path, | |
2514 | update_data->prefix); | |
2515 | next.prefix = NULL; | |
2516 | oidcpy(&next.oid, null_oid()); | |
2517 | oidcpy(&next.suboid, null_oid()); | |
2518 | ||
2519 | cp.dir = update_data->sm_path; | |
2520 | cp.git_cmd = 1; | |
29fda24d | 2521 | prepare_submodule_repo_env(&cp.env); |
f3875ab1 GC |
2522 | update_data_to_args(&next, &cp.args); |
2523 | ||
2524 | /* die() if child process die()'d */ | |
2525 | res = run_command(&cp); | |
2526 | if (!res) | |
2527 | return 0; | |
2528 | die_message(_("Failed to recurse into submodule path '%s'"), | |
2529 | update_data->displaypath); | |
2530 | if (res == 128) | |
2531 | exit(res); | |
2532 | else if (res) | |
2533 | return 1; | |
2534 | } | |
2535 | ||
2536 | return 0; | |
2537 | } | |
2538 | ||
c9911c93 | 2539 | static int update_submodules(struct update_data *update_data) |
90efe595 | 2540 | { |
b3c5f5cb | 2541 | int i, res = 0; |
c9911c93 | 2542 | struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT; |
90efe595 | 2543 | |
c9911c93 GC |
2544 | suc.update_data = update_data; |
2545 | run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task, | |
ee4512ed | 2546 | update_clone_start_failure, |
c9911c93 | 2547 | update_clone_task_finished, &suc, "submodule", |
ee4512ed | 2548 | "parallel/update"); |
90efe595 SB |
2549 | |
2550 | /* | |
2551 | * We saved the output and put it out all at once now. | |
2552 | * That means: | |
2553 | * - the listener does not have to interleave their (checkout) | |
2554 | * work with our fetching. The writes involved in a | |
2555 | * checkout involve more straightforward sequential I/O. | |
2556 | * - the listener can avoid doing any work if fetching failed. | |
2557 | */ | |
b3c5f5cb AR |
2558 | if (suc.quickstop) { |
2559 | res = 1; | |
2560 | goto cleanup; | |
2561 | } | |
90efe595 | 2562 | |
b3c5f5cb AR |
2563 | for (i = 0; i < suc.update_clone_nr; i++) { |
2564 | struct update_clone_data ucd = suc.update_clone[i]; | |
90efe595 | 2565 | |
b3c5f5cb AR |
2566 | oidcpy(&update_data->oid, &ucd.oid); |
2567 | update_data->just_cloned = ucd.just_cloned; | |
2568 | update_data->sm_path = ucd.sub->path; | |
2569 | ||
2570 | if (update_submodule(update_data)) | |
2571 | res = 1; | |
2572 | } | |
2573 | ||
2574 | cleanup: | |
2575 | string_list_clear(&update_data->references, 0); | |
2576 | return res; | |
90efe595 SB |
2577 | } |
2578 | ||
b3c5f5cb | 2579 | static int module_update(int argc, const char **argv, const char *prefix) |
48308681 | 2580 | { |
48308681 | 2581 | struct pathspec pathspec; |
c9911c93 | 2582 | struct update_data opt = UPDATE_DATA_INIT; |
f05da2b4 JS |
2583 | struct list_objects_filter_options filter_options; |
2584 | int ret; | |
48308681 | 2585 | |
b3c5f5cb AR |
2586 | struct option module_update_options[] = { |
2587 | OPT__FORCE(&opt.force, N_("force checkout updates"), 0), | |
49fd5b99 | 2588 | OPT_BOOL(0, "init", &opt.init, |
29a5e9e1 | 2589 | N_("initialize uninitialized submodules before update")), |
b3c5f5cb AR |
2590 | OPT_BOOL(0, "remote", &opt.remote, |
2591 | N_("use SHA-1 of submodule's remote tracking branch")), | |
2592 | OPT_BOOL(0, "recursive", &opt.recursive, | |
2593 | N_("traverse submodules recursively")), | |
2594 | OPT_BOOL('N', "no-fetch", &opt.nofetch, | |
2595 | N_("don't fetch new objects from the remote site")), | |
c9911c93 | 2596 | OPT_STRING(0, "prefix", &opt.prefix, |
48308681 SB |
2597 | N_("path"), |
2598 | N_("path into the working tree")), | |
49fd5b99 | 2599 | OPT_STRING(0, "recursive-prefix", &opt.recursive_prefix, |
48308681 SB |
2600 | N_("path"), |
2601 | N_("path into the working tree, across nested " | |
2602 | "submodule boundaries")), | |
c9911c93 | 2603 | OPT_STRING(0, "update", &opt.update_default, |
48308681 SB |
2604 | N_("string"), |
2605 | N_("rebase, merge, checkout or none")), | |
49fd5b99 | 2606 | OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"), |
48308681 | 2607 | N_("reference repository")), |
49fd5b99 | 2608 | OPT_BOOL(0, "dissociate", &opt.dissociate, |
a0ef2934 | 2609 | N_("use --reference only while cloning")), |
c9911c93 | 2610 | OPT_INTEGER(0, "depth", &opt.depth, |
e73fe3dd | 2611 | N_("create a shallow clone truncated to the " |
48308681 | 2612 | "specified number of revisions")), |
49fd5b99 | 2613 | OPT_INTEGER('j', "jobs", &opt.max_jobs, |
2335b870 | 2614 | N_("parallel jobs")), |
49fd5b99 | 2615 | OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow, |
abed000a | 2616 | N_("whether the initial clone should follow the shallow recommendation")), |
49fd5b99 ÆAB |
2617 | OPT__QUIET(&opt.quiet, N_("don't print cloning progress")), |
2618 | OPT_BOOL(0, "progress", &opt.progress, | |
72c5f883 | 2619 | N_("force cloning progress")), |
49fd5b99 | 2620 | OPT_BOOL(0, "require-init", &opt.require_init, |
d9c7f69a | 2621 | N_("disallow cloning into non-empty directory, implies --init")), |
49fd5b99 | 2622 | OPT_BOOL(0, "single-branch", &opt.single_branch, |
132f600b | 2623 | N_("clone only one branch, HEAD or --branch")), |
f05da2b4 | 2624 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
48308681 SB |
2625 | OPT_END() |
2626 | }; | |
2627 | ||
2628 | const char *const git_submodule_helper_usage[] = { | |
c9d25624 GC |
2629 | N_("git submodule [--quiet] update" |
2630 | " [--init [--filter=<filter-spec>]] [--remote]" | |
2631 | " [-N|--no-fetch] [-f|--force]" | |
2632 | " [--checkout|--merge|--rebase]" | |
2633 | " [--[no-]recommend-shallow] [--reference <repository>]" | |
2634 | " [--recursive] [--[no-]single-branch] [--] [<path>...]"), | |
48308681 SB |
2635 | NULL |
2636 | }; | |
48308681 | 2637 | |
49fd5b99 ÆAB |
2638 | update_clone_config_from_gitmodules(&opt.max_jobs); |
2639 | git_config(git_update_clone_config, &opt.max_jobs); | |
f20e7c1e | 2640 | |
f05da2b4 | 2641 | memset(&filter_options, 0, sizeof(filter_options)); |
b3c5f5cb | 2642 | argc = parse_options(argc, argv, prefix, module_update_options, |
48308681 | 2643 | git_submodule_helper_usage, 0); |
c9d25624 | 2644 | |
d9c7f69a ÆAB |
2645 | if (opt.require_init) |
2646 | opt.init = 1; | |
2647 | ||
49fd5b99 | 2648 | if (filter_options.choice && !opt.init) { |
b3c5f5cb AR |
2649 | usage_with_options(git_submodule_helper_usage, |
2650 | module_update_options); | |
c9d25624 GC |
2651 | } |
2652 | ||
49fd5b99 | 2653 | opt.filter_options = &filter_options; |
48308681 | 2654 | |
c9911c93 GC |
2655 | if (opt.update_default) |
2656 | if (parse_submodule_update_strategy(opt.update_default, | |
2657 | &opt.update_strategy) < 0) | |
48308681 SB |
2658 | die(_("bad value for update parameter")); |
2659 | ||
49fd5b99 | 2660 | if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) { |
f05da2b4 | 2661 | list_objects_filter_release(&filter_options); |
48308681 | 2662 | return 1; |
f05da2b4 | 2663 | } |
48308681 SB |
2664 | |
2665 | if (pathspec.nr) | |
49fd5b99 | 2666 | opt.warn_if_uninitialized = 1; |
48308681 | 2667 | |
49fd5b99 | 2668 | if (opt.init) { |
29a5e9e1 GC |
2669 | struct module_list list = MODULE_LIST_INIT; |
2670 | struct init_cb info = INIT_CB_INIT; | |
2671 | ||
49fd5b99 | 2672 | if (module_list_compute(argc, argv, opt.prefix, |
29a5e9e1 GC |
2673 | &pathspec, &list) < 0) |
2674 | return 1; | |
2675 | ||
2676 | /* | |
2677 | * If there are no path args and submodule.active is set then, | |
2678 | * by default, only initialize 'active' modules. | |
2679 | */ | |
2680 | if (!argc && git_config_get_value_multi("submodule.active")) | |
2681 | module_list_active(&list); | |
2682 | ||
49fd5b99 ÆAB |
2683 | info.prefix = opt.prefix; |
2684 | info.superprefix = opt.recursive_prefix; | |
2685 | if (opt.quiet) | |
29a5e9e1 GC |
2686 | info.flags |= OPT_QUIET; |
2687 | ||
2688 | for_each_listed_submodule(&list, init_submodule_cb, &info); | |
2689 | } | |
2690 | ||
49fd5b99 | 2691 | ret = update_submodules(&opt); |
f05da2b4 JS |
2692 | list_objects_filter_release(&filter_options); |
2693 | return ret; | |
48308681 SB |
2694 | } |
2695 | ||
93481a6b BW |
2696 | static int push_check(int argc, const char **argv, const char *prefix) |
2697 | { | |
2698 | struct remote *remote; | |
c7be7201 BW |
2699 | const char *superproject_head; |
2700 | char *head; | |
2701 | int detached_head = 0; | |
2702 | struct object_id head_oid; | |
93481a6b | 2703 | |
c7be7201 BW |
2704 | if (argc < 3) |
2705 | die("submodule--helper push-check requires at least 2 arguments"); | |
2706 | ||
2707 | /* | |
2708 | * superproject's resolved head ref. | |
2709 | * if HEAD then the superproject is in a detached head state, otherwise | |
2710 | * it will be the resolved head ref. | |
2711 | */ | |
2712 | superproject_head = argv[1]; | |
2713 | argv++; | |
2714 | argc--; | |
2715 | /* Get the submodule's head ref and determine if it is detached */ | |
0f2dc722 | 2716 | head = resolve_refdup("HEAD", 0, &head_oid, NULL); |
c7be7201 BW |
2717 | if (!head) |
2718 | die(_("Failed to resolve HEAD as a valid ref.")); | |
2719 | if (!strcmp(head, "HEAD")) | |
2720 | detached_head = 1; | |
93481a6b BW |
2721 | |
2722 | /* | |
2723 | * The remote must be configured. | |
2724 | * This is to avoid pushing to the exact same URL as the parent. | |
2725 | */ | |
2726 | remote = pushremote_get(argv[1]); | |
2727 | if (!remote || remote->origin == REMOTE_UNCONFIGURED) | |
2728 | die("remote '%s' not configured", argv[1]); | |
2729 | ||
2730 | /* Check the refspec */ | |
2731 | if (argc > 2) { | |
9c8361b2 | 2732 | int i; |
93481a6b | 2733 | struct ref *local_refs = get_local_heads(); |
9c8361b2 | 2734 | struct refspec refspec = REFSPEC_INIT_PUSH; |
93481a6b | 2735 | |
9c8361b2 BW |
2736 | refspec_appendn(&refspec, argv + 2, argc - 2); |
2737 | ||
2738 | for (i = 0; i < refspec.nr; i++) { | |
2739 | const struct refspec_item *rs = &refspec.items[i]; | |
93481a6b BW |
2740 | |
2741 | if (rs->pattern || rs->matching) | |
2742 | continue; | |
2743 | ||
c7be7201 BW |
2744 | /* LHS must match a single ref */ |
2745 | switch (count_refspec_match(rs->src, local_refs, NULL)) { | |
2746 | case 1: | |
2747 | break; | |
2748 | case 0: | |
2749 | /* | |
2750 | * If LHS matches 'HEAD' then we need to ensure | |
2751 | * that it matches the same named branch | |
2752 | * checked out in the superproject. | |
2753 | */ | |
2754 | if (!strcmp(rs->src, "HEAD")) { | |
2755 | if (!detached_head && | |
2756 | !strcmp(head, superproject_head)) | |
2757 | break; | |
2758 | die("HEAD does not match the named branch in the superproject"); | |
2759 | } | |
1cf01a34 | 2760 | /* fallthrough */ |
c7be7201 | 2761 | default: |
93481a6b BW |
2762 | die("src refspec '%s' must name a ref", |
2763 | rs->src); | |
c7be7201 | 2764 | } |
93481a6b | 2765 | } |
9c8361b2 | 2766 | refspec_clear(&refspec); |
93481a6b | 2767 | } |
c7be7201 | 2768 | free(head); |
93481a6b BW |
2769 | |
2770 | return 0; | |
2771 | } | |
2772 | ||
f6f85861 SB |
2773 | static int absorb_git_dirs(int argc, const char **argv, const char *prefix) |
2774 | { | |
2775 | int i; | |
2776 | struct pathspec pathspec; | |
2777 | struct module_list list = MODULE_LIST_INIT; | |
2778 | unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES; | |
2779 | ||
2780 | struct option embed_gitdir_options[] = { | |
2781 | OPT_STRING(0, "prefix", &prefix, | |
2782 | N_("path"), | |
2783 | N_("path into the working tree")), | |
2784 | OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"), | |
2785 | ABSORB_GITDIR_RECURSE_SUBMODULES), | |
2786 | OPT_END() | |
2787 | }; | |
2788 | ||
2789 | const char *const git_submodule_helper_usage[] = { | |
6e556c41 | 2790 | N_("git submodule--helper absorbgitdirs [<options>] [<path>...]"), |
f6f85861 SB |
2791 | NULL |
2792 | }; | |
2793 | ||
2794 | argc = parse_options(argc, argv, prefix, embed_gitdir_options, | |
2795 | git_submodule_helper_usage, 0); | |
2796 | ||
f6f85861 SB |
2797 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
2798 | return 1; | |
2799 | ||
2800 | for (i = 0; i < list.nr; i++) | |
cf7a901a | 2801 | absorb_git_dir_into_superproject(list.entries[i]->name, flags); |
f6f85861 SB |
2802 | |
2803 | return 0; | |
2804 | } | |
2805 | ||
5c2bd8b7 BW |
2806 | static int is_active(int argc, const char **argv, const char *prefix) |
2807 | { | |
2808 | if (argc != 2) | |
9af7ec30 | 2809 | die("submodule--helper is-active takes exactly 1 argument"); |
5c2bd8b7 | 2810 | |
627d9342 | 2811 | return !is_submodule_active(the_repository, argv[1]); |
5c2bd8b7 BW |
2812 | } |
2813 | ||
0383bbb9 JK |
2814 | /* |
2815 | * Exit non-zero if any of the submodule names given on the command line is | |
2816 | * invalid. If no names are given, filter stdin to print only valid names | |
2817 | * (which is primarily intended for testing). | |
2818 | */ | |
2819 | static int check_name(int argc, const char **argv, const char *prefix) | |
2820 | { | |
2821 | if (argc > 1) { | |
2822 | while (*++argv) { | |
2823 | if (check_submodule_name(*argv) < 0) | |
2824 | return 1; | |
2825 | } | |
2826 | } else { | |
2827 | struct strbuf buf = STRBUF_INIT; | |
2828 | while (strbuf_getline(&buf, stdin) != EOF) { | |
2829 | if (!check_submodule_name(buf.buf)) | |
2830 | printf("%s\n", buf.buf); | |
2831 | } | |
2832 | strbuf_release(&buf); | |
2833 | } | |
2834 | return 0; | |
2835 | } | |
2836 | ||
2502ffc0 AO |
2837 | static int module_config(int argc, const char **argv, const char *prefix) |
2838 | { | |
b5c259f2 | 2839 | enum { |
c89c4942 DL |
2840 | CHECK_WRITEABLE = 1, |
2841 | DO_UNSET = 2 | |
b5c259f2 AO |
2842 | } command = 0; |
2843 | ||
2844 | struct option module_config_options[] = { | |
2845 | OPT_CMDMODE(0, "check-writeable", &command, | |
2846 | N_("check if it is safe to write to the .gitmodules file"), | |
2847 | CHECK_WRITEABLE), | |
c89c4942 DL |
2848 | OPT_CMDMODE(0, "unset", &command, |
2849 | N_("unset the config in the .gitmodules file"), | |
2850 | DO_UNSET), | |
b5c259f2 AO |
2851 | OPT_END() |
2852 | }; | |
2853 | const char *const git_submodule_helper_usage[] = { | |
c89c4942 DL |
2854 | N_("git submodule--helper config <name> [<value>]"), |
2855 | N_("git submodule--helper config --unset <name>"), | |
959d670d | 2856 | "git submodule--helper config --check-writeable", |
b5c259f2 AO |
2857 | NULL |
2858 | }; | |
2859 | ||
2860 | argc = parse_options(argc, argv, prefix, module_config_options, | |
2861 | git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0); | |
2862 | ||
2863 | if (argc == 1 && command == CHECK_WRITEABLE) | |
2864 | return is_writing_gitmodules_ok() ? 0 : -1; | |
2865 | ||
2502ffc0 | 2866 | /* Equivalent to ACTION_GET in builtin/config.c */ |
c89c4942 | 2867 | if (argc == 2 && command != DO_UNSET) |
2502ffc0 AO |
2868 | return print_config_from_gitmodules(the_repository, argv[1]); |
2869 | ||
2870 | /* Equivalent to ACTION_SET in builtin/config.c */ | |
c89c4942 DL |
2871 | if (argc == 3 || (argc == 2 && command == DO_UNSET)) { |
2872 | const char *value = (argc == 3) ? argv[2] : NULL; | |
2873 | ||
76e9bdc4 AO |
2874 | if (!is_writing_gitmodules_ok()) |
2875 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
2876 | ||
c89c4942 | 2877 | return config_set_in_gitmodules_file_gently(argv[1], value); |
76e9bdc4 | 2878 | } |
2502ffc0 | 2879 | |
b5c259f2 | 2880 | usage_with_options(git_submodule_helper_usage, module_config_options); |
2502ffc0 AO |
2881 | } |
2882 | ||
6417cf9c SS |
2883 | static int module_set_url(int argc, const char **argv, const char *prefix) |
2884 | { | |
2885 | int quiet = 0; | |
2886 | const char *newurl; | |
2887 | const char *path; | |
2888 | char *config_name; | |
2889 | ||
2890 | struct option options[] = { | |
e73fe3dd | 2891 | OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")), |
6417cf9c SS |
2892 | OPT_END() |
2893 | }; | |
2894 | const char *const usage[] = { | |
2895 | N_("git submodule--helper set-url [--quiet] <path> <newurl>"), | |
2896 | NULL | |
2897 | }; | |
2898 | ||
2899 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
2900 | ||
2901 | if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1])) | |
2902 | usage_with_options(usage, options); | |
2903 | ||
2904 | config_name = xstrfmt("submodule.%s.url", path); | |
2905 | ||
2906 | config_set_in_gitmodules_file_gently(config_name, newurl); | |
2907 | sync_submodule(path, prefix, quiet ? OPT_QUIET : 0); | |
2908 | ||
2909 | free(config_name); | |
2910 | ||
2911 | return 0; | |
2912 | } | |
2913 | ||
2964d6e5 SS |
2914 | static int module_set_branch(int argc, const char **argv, const char *prefix) |
2915 | { | |
2916 | int opt_default = 0, ret; | |
2917 | const char *opt_branch = NULL; | |
2918 | const char *path; | |
2919 | char *config_name; | |
2920 | ||
2921 | /* | |
2922 | * We accept the `quiet` option for uniformity across subcommands, | |
2923 | * though there is nothing to make less verbose in this subcommand. | |
2924 | */ | |
2925 | struct option options[] = { | |
2926 | OPT_NOOP_NOARG('q', "quiet"), | |
2927 | OPT_BOOL('d', "default", &opt_default, | |
2928 | N_("set the default tracking branch to master")), | |
2929 | OPT_STRING('b', "branch", &opt_branch, N_("branch"), | |
2930 | N_("set the default tracking branch")), | |
2931 | OPT_END() | |
2932 | }; | |
2933 | const char *const usage[] = { | |
2934 | N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"), | |
2935 | N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"), | |
2936 | NULL | |
2937 | }; | |
2938 | ||
2939 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
2940 | ||
2941 | if (!opt_branch && !opt_default) | |
2942 | die(_("--branch or --default required")); | |
2943 | ||
2944 | if (opt_branch && opt_default) | |
43ea635c | 2945 | die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default"); |
2964d6e5 SS |
2946 | |
2947 | if (argc != 1 || !(path = argv[0])) | |
2948 | usage_with_options(usage, options); | |
2949 | ||
2950 | config_name = xstrfmt("submodule.%s.branch", path); | |
2951 | ret = config_set_in_gitmodules_file_gently(config_name, opt_branch); | |
2952 | ||
2953 | free(config_name); | |
2954 | return !!ret; | |
2955 | } | |
2956 | ||
961b130d GC |
2957 | static int module_create_branch(int argc, const char **argv, const char *prefix) |
2958 | { | |
2959 | enum branch_track track; | |
2960 | int quiet = 0, force = 0, reflog = 0, dry_run = 0; | |
2961 | ||
2962 | struct option options[] = { | |
2963 | OPT__QUIET(&quiet, N_("print only error messages")), | |
2964 | OPT__FORCE(&force, N_("force creation"), 0), | |
2965 | OPT_BOOL(0, "create-reflog", &reflog, | |
2966 | N_("create the branch's reflog")), | |
75388bf5 GC |
2967 | OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", |
2968 | N_("set branch tracking configuration"), | |
2969 | PARSE_OPT_OPTARG, | |
2970 | parse_opt_tracking_mode), | |
961b130d GC |
2971 | OPT__DRY_RUN(&dry_run, |
2972 | N_("show whether the branch would be created")), | |
2973 | OPT_END() | |
2974 | }; | |
2975 | const char *const usage[] = { | |
af15f84d | 2976 | N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"), |
961b130d GC |
2977 | NULL |
2978 | }; | |
2979 | ||
2980 | git_config(git_default_config, NULL); | |
2981 | track = git_branch_track; | |
2982 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
2983 | ||
2984 | if (argc != 3) | |
2985 | usage_with_options(usage, options); | |
2986 | ||
2987 | if (!quiet && !dry_run) | |
2988 | printf_ln(_("creating branch '%s'"), argv[0]); | |
2989 | ||
2990 | create_branches_recursively(the_repository, argv[0], argv[1], argv[2], | |
2991 | force, reflog, quiet, track, dry_run); | |
2992 | return 0; | |
2993 | } | |
1a0b78c9 | 2994 | |
8c8195e9 AR |
2995 | struct add_data { |
2996 | const char *prefix; | |
2997 | const char *branch; | |
2998 | const char *reference_path; | |
a6226fd7 | 2999 | char *sm_path; |
8c8195e9 AR |
3000 | const char *sm_name; |
3001 | const char *repo; | |
3002 | const char *realrepo; | |
3003 | int depth; | |
3004 | unsigned int force: 1; | |
3005 | unsigned int quiet: 1; | |
3006 | unsigned int progress: 1; | |
3007 | unsigned int dissociate: 1; | |
3008 | }; | |
3009 | #define ADD_DATA_INIT { .depth = -1 } | |
3010 | ||
6b615dbe | 3011 | static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path) |
8c8195e9 AR |
3012 | { |
3013 | struct child_process cp_remote = CHILD_PROCESS_INIT; | |
3014 | struct strbuf sb_remote_out = STRBUF_INIT; | |
3015 | ||
3016 | cp_remote.git_cmd = 1; | |
29fda24d | 3017 | strvec_pushf(&cp_remote.env, |
8c8195e9 | 3018 | "GIT_DIR=%s", git_dir_path); |
29fda24d | 3019 | strvec_push(&cp_remote.env, "GIT_WORK_TREE=."); |
8c8195e9 AR |
3020 | strvec_pushl(&cp_remote.args, "remote", "-v", NULL); |
3021 | if (!capture_command(&cp_remote, &sb_remote_out, 0)) { | |
3022 | char *next_line; | |
3023 | char *line = sb_remote_out.buf; | |
3024 | while ((next_line = strchr(line, '\n')) != NULL) { | |
3025 | size_t len = next_line - line; | |
3026 | if (strip_suffix_mem(line, &len, " (fetch)")) | |
c21fb467 | 3027 | strbuf_addf(msg, " %.*s\n", (int)len, line); |
8c8195e9 AR |
3028 | line = next_line + 1; |
3029 | } | |
3030 | } | |
3031 | ||
3032 | strbuf_release(&sb_remote_out); | |
3033 | } | |
3034 | ||
3035 | static int add_submodule(const struct add_data *add_data) | |
3036 | { | |
3037 | char *submod_gitdir_path; | |
3038 | struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; | |
3039 | ||
3040 | /* perhaps the path already exists and is already a git repo, else clone it */ | |
3041 | if (is_directory(add_data->sm_path)) { | |
3042 | struct strbuf sm_path = STRBUF_INIT; | |
3043 | strbuf_addstr(&sm_path, add_data->sm_path); | |
3044 | submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path); | |
3045 | if (is_nonbare_repository_dir(&sm_path)) | |
3046 | printf(_("Adding existing repo at '%s' to the index\n"), | |
3047 | add_data->sm_path); | |
3048 | else | |
3049 | die(_("'%s' already exists and is not a valid git repo"), | |
3050 | add_data->sm_path); | |
3051 | strbuf_release(&sm_path); | |
3052 | free(submod_gitdir_path); | |
3053 | } else { | |
3054 | struct child_process cp = CHILD_PROCESS_INIT; | |
3055 | submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name); | |
3056 | ||
3057 | if (is_directory(submod_gitdir_path)) { | |
3058 | if (!add_data->force) { | |
c21fb467 KS |
3059 | struct strbuf msg = STRBUF_INIT; |
3060 | char *die_msg; | |
3061 | ||
3062 | strbuf_addf(&msg, _("A git directory for '%s' is found " | |
3063 | "locally with remote(s):\n"), | |
3064 | add_data->sm_name); | |
3065 | ||
6b615dbe | 3066 | append_fetch_remotes(&msg, submod_gitdir_path); |
8c8195e9 | 3067 | free(submod_gitdir_path); |
c21fb467 KS |
3068 | |
3069 | strbuf_addf(&msg, _("If you want to reuse this local git " | |
3070 | "directory instead of cloning again from\n" | |
3071 | " %s\n" | |
3072 | "use the '--force' option. If the local git " | |
3073 | "directory is not the correct repo\n" | |
3074 | "or you are unsure what this means choose " | |
3075 | "another name with the '--name' option."), | |
3076 | add_data->realrepo); | |
3077 | ||
3078 | die_msg = strbuf_detach(&msg, NULL); | |
3079 | die("%s", die_msg); | |
8c8195e9 AR |
3080 | } else { |
3081 | printf(_("Reactivating local git directory for " | |
3082 | "submodule '%s'\n"), add_data->sm_name); | |
3083 | } | |
3084 | } | |
3085 | free(submod_gitdir_path); | |
3086 | ||
3087 | clone_data.prefix = add_data->prefix; | |
3088 | clone_data.path = add_data->sm_path; | |
3089 | clone_data.name = add_data->sm_name; | |
3090 | clone_data.url = add_data->realrepo; | |
3091 | clone_data.quiet = add_data->quiet; | |
3092 | clone_data.progress = add_data->progress; | |
3093 | if (add_data->reference_path) | |
3094 | string_list_append(&clone_data.reference, | |
3095 | xstrdup(add_data->reference_path)); | |
3096 | clone_data.dissociate = add_data->dissociate; | |
3097 | if (add_data->depth >= 0) | |
3098 | clone_data.depth = xstrfmt("%d", add_data->depth); | |
3099 | ||
3100 | if (clone_submodule(&clone_data)) | |
3101 | return -1; | |
3102 | ||
29fda24d | 3103 | prepare_submodule_repo_env(&cp.env); |
8c8195e9 AR |
3104 | cp.git_cmd = 1; |
3105 | cp.dir = add_data->sm_path; | |
94b7f156 EN |
3106 | /* |
3107 | * NOTE: we only get here if add_data->force is true, so | |
3108 | * passing --force to checkout is reasonable. | |
3109 | */ | |
8c8195e9 AR |
3110 | strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL); |
3111 | ||
3112 | if (add_data->branch) { | |
3113 | strvec_pushl(&cp.args, "-B", add_data->branch, NULL); | |
3114 | strvec_pushf(&cp.args, "origin/%s", add_data->branch); | |
3115 | } | |
3116 | ||
3117 | if (run_command(&cp)) | |
3118 | die(_("unable to checkout submodule '%s'"), add_data->sm_path); | |
3119 | } | |
3120 | return 0; | |
3121 | } | |
3122 | ||
a452128a AR |
3123 | static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value) |
3124 | { | |
3125 | char *key; | |
3126 | int ret; | |
3127 | ||
3128 | if (!is_writing_gitmodules_ok()) | |
3129 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
3130 | ||
3131 | key = xstrfmt("submodule.%s.%s", name, var); | |
3132 | ret = config_set_in_gitmodules_file_gently(key, value); | |
3133 | free(key); | |
3134 | ||
3135 | return ret; | |
3136 | } | |
3137 | ||
3138 | static void configure_added_submodule(struct add_data *add_data) | |
3139 | { | |
3140 | char *key; | |
3141 | char *val = NULL; | |
3142 | struct child_process add_submod = CHILD_PROCESS_INIT; | |
3143 | struct child_process add_gitmodules = CHILD_PROCESS_INIT; | |
3144 | ||
3145 | key = xstrfmt("submodule.%s.url", add_data->sm_name); | |
3146 | git_config_set_gently(key, add_data->realrepo); | |
3147 | free(key); | |
3148 | ||
3149 | add_submod.git_cmd = 1; | |
3150 | strvec_pushl(&add_submod.args, "add", | |
3151 | "--no-warn-embedded-repo", NULL); | |
3152 | if (add_data->force) | |
3153 | strvec_push(&add_submod.args, "--force"); | |
3154 | strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL); | |
3155 | ||
3156 | if (run_command(&add_submod)) | |
3157 | die(_("Failed to add submodule '%s'"), add_data->sm_path); | |
3158 | ||
3159 | if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) || | |
3160 | config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo)) | |
3161 | die(_("Failed to register submodule '%s'"), add_data->sm_path); | |
3162 | ||
3163 | if (add_data->branch) { | |
3164 | if (config_submodule_in_gitmodules(add_data->sm_name, | |
3165 | "branch", add_data->branch)) | |
3166 | die(_("Failed to register submodule '%s'"), add_data->sm_path); | |
3167 | } | |
3168 | ||
3169 | add_gitmodules.git_cmd = 1; | |
3170 | strvec_pushl(&add_gitmodules.args, | |
3171 | "add", "--force", "--", ".gitmodules", NULL); | |
3172 | ||
3173 | if (run_command(&add_gitmodules)) | |
3174 | die(_("Failed to register submodule '%s'"), add_data->sm_path); | |
3175 | ||
3176 | /* | |
3177 | * NEEDSWORK: In a multi-working-tree world this needs to be | |
3178 | * set in the per-worktree config. | |
3179 | */ | |
3180 | /* | |
3181 | * NEEDSWORK: In the longer run, we need to get rid of this | |
3182 | * pattern of querying "submodule.active" before calling | |
3183 | * is_submodule_active(), since that function needs to find | |
3184 | * out the value of "submodule.active" again anyway. | |
3185 | */ | |
3186 | if (!git_config_get_string("submodule.active", &val) && val) { | |
3187 | /* | |
3188 | * If the submodule being added isn't already covered by the | |
3189 | * current configured pathspec, set the submodule's active flag | |
3190 | */ | |
3191 | if (!is_submodule_active(the_repository, add_data->sm_path)) { | |
3192 | key = xstrfmt("submodule.%s.active", add_data->sm_name); | |
3193 | git_config_set_gently(key, "true"); | |
3194 | free(key); | |
3195 | } | |
3196 | } else { | |
3197 | key = xstrfmt("submodule.%s.active", add_data->sm_name); | |
3198 | git_config_set_gently(key, "true"); | |
3199 | free(key); | |
3200 | } | |
3201 | } | |
3202 | ||
a6226fd7 | 3203 | static void die_on_index_match(const char *path, int force) |
a452128a | 3204 | { |
a6226fd7 AR |
3205 | struct pathspec ps; |
3206 | const char *args[] = { path, NULL }; | |
3207 | parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args); | |
3208 | ||
3209 | if (read_cache_preload(NULL) < 0) | |
3210 | die(_("index file corrupt")); | |
3211 | ||
3212 | if (ps.nr) { | |
3213 | int i; | |
3214 | char *ps_matched = xcalloc(ps.nr, 1); | |
3215 | ||
3216 | /* TODO: audit for interaction with sparse-index. */ | |
3217 | ensure_full_index(&the_index); | |
3218 | ||
3219 | /* | |
3220 | * Since there is only one pathspec, we just need | |
3221 | * need to check ps_matched[0] to know if a cache | |
3222 | * entry matched. | |
3223 | */ | |
3224 | for (i = 0; i < active_nr; i++) { | |
3225 | ce_path_match(&the_index, active_cache[i], &ps, | |
3226 | ps_matched); | |
3227 | ||
3228 | if (ps_matched[0]) { | |
3229 | if (!force) | |
3230 | die(_("'%s' already exists in the index"), | |
3231 | path); | |
3232 | if (!S_ISGITLINK(active_cache[i]->ce_mode)) | |
3233 | die(_("'%s' already exists in the index " | |
3234 | "and is not a submodule"), path); | |
3235 | break; | |
3236 | } | |
3237 | } | |
3238 | free(ps_matched); | |
3239 | } | |
c270b055 | 3240 | clear_pathspec(&ps); |
a6226fd7 AR |
3241 | } |
3242 | ||
3243 | static void die_on_repo_without_commits(const char *path) | |
3244 | { | |
3245 | struct strbuf sb = STRBUF_INIT; | |
3246 | strbuf_addstr(&sb, path); | |
3247 | if (is_nonbare_repository_dir(&sb)) { | |
3248 | struct object_id oid; | |
3249 | if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) | |
3250 | die(_("'%s' does not have a commit checked out"), path); | |
3251 | } | |
c270b055 | 3252 | strbuf_release(&sb); |
a6226fd7 AR |
3253 | } |
3254 | ||
3255 | static int module_add(int argc, const char **argv, const char *prefix) | |
3256 | { | |
3257 | int force = 0, quiet = 0, progress = 0, dissociate = 0; | |
a452128a | 3258 | struct add_data add_data = ADD_DATA_INIT; |
8f790151 | 3259 | char *to_free = NULL; |
a452128a AR |
3260 | |
3261 | struct option options[] = { | |
a6226fd7 AR |
3262 | OPT_STRING('b', "branch", &add_data.branch, N_("branch"), |
3263 | N_("branch of repository to add as submodule")), | |
a452128a AR |
3264 | OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"), |
3265 | PARSE_OPT_NOCOMPLETE), | |
a6226fd7 AR |
3266 | OPT__QUIET(&quiet, N_("print only error messages")), |
3267 | OPT_BOOL(0, "progress", &progress, N_("force cloning progress")), | |
3268 | OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"), | |
3269 | N_("reference repository")), | |
3270 | OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")), | |
3271 | OPT_STRING(0, "name", &add_data.sm_name, N_("name"), | |
6dd9a91c | 3272 | N_("sets the submodule's name to the given string " |
a6226fd7 AR |
3273 | "instead of defaulting to its path")), |
3274 | OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")), | |
a452128a AR |
3275 | OPT_END() |
3276 | }; | |
3277 | ||
3278 | const char *const usage[] = { | |
a6226fd7 | 3279 | N_("git submodule--helper add [<options>] [--] <repository> [<path>]"), |
a452128a AR |
3280 | NULL |
3281 | }; | |
3282 | ||
3283 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
3284 | ||
a6226fd7 AR |
3285 | if (!is_writing_gitmodules_ok()) |
3286 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
3287 | ||
3288 | if (prefix && *prefix && | |
3289 | add_data.reference_path && !is_absolute_path(add_data.reference_path)) | |
3290 | add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path); | |
3291 | ||
3292 | if (argc == 0 || argc > 2) | |
a452128a AR |
3293 | usage_with_options(usage, options); |
3294 | ||
a6226fd7 AR |
3295 | add_data.repo = argv[0]; |
3296 | if (argc == 1) | |
3297 | add_data.sm_path = git_url_basename(add_data.repo, 0, 0); | |
3298 | else | |
3299 | add_data.sm_path = xstrdup(argv[1]); | |
3300 | ||
3301 | if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) | |
3302 | add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path); | |
3303 | ||
3304 | if (starts_with_dot_dot_slash(add_data.repo) || | |
3305 | starts_with_dot_slash(add_data.repo)) { | |
3306 | if (prefix) | |
3307 | die(_("Relative path can only be used from the toplevel " | |
3308 | "of the working tree")); | |
3309 | ||
3310 | /* dereference source url relative to parent's url */ | |
8f790151 ÆAB |
3311 | to_free = resolve_relative_url(add_data.repo, NULL, 1); |
3312 | add_data.realrepo = to_free; | |
a6226fd7 AR |
3313 | } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) { |
3314 | add_data.realrepo = add_data.repo; | |
3315 | } else { | |
3316 | die(_("repo URL: '%s' must be absolute or begin with ./|../"), | |
3317 | add_data.repo); | |
3318 | } | |
3319 | ||
3320 | /* | |
3321 | * normalize path: | |
3322 | * multiple //; leading ./; /./; /../; | |
3323 | */ | |
3324 | normalize_path_copy(add_data.sm_path, add_data.sm_path); | |
3325 | strip_dir_trailing_slashes(add_data.sm_path); | |
3326 | ||
3327 | die_on_index_match(add_data.sm_path, force); | |
3328 | die_on_repo_without_commits(add_data.sm_path); | |
3329 | ||
3330 | if (!force) { | |
3331 | int exit_code = -1; | |
3332 | struct strbuf sb = STRBUF_INIT; | |
3333 | struct child_process cp = CHILD_PROCESS_INIT; | |
3334 | cp.git_cmd = 1; | |
3335 | cp.no_stdout = 1; | |
3336 | strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing", | |
3337 | "--no-warn-embedded-repo", add_data.sm_path, NULL); | |
3338 | if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) { | |
3339 | strbuf_complete_line(&sb); | |
3340 | fputs(sb.buf, stderr); | |
3341 | free(add_data.sm_path); | |
3342 | return exit_code; | |
3343 | } | |
3344 | strbuf_release(&sb); | |
3345 | } | |
3346 | ||
3347 | if(!add_data.sm_name) | |
3348 | add_data.sm_name = add_data.sm_path; | |
3349 | ||
3350 | if (check_submodule_name(add_data.sm_name)) | |
3351 | die(_("'%s' is not a valid submodule name"), add_data.sm_name); | |
3352 | ||
3353 | add_data.prefix = prefix; | |
a452128a | 3354 | add_data.force = !!force; |
a6226fd7 AR |
3355 | add_data.quiet = !!quiet; |
3356 | add_data.progress = !!progress; | |
3357 | add_data.dissociate = !!dissociate; | |
3358 | ||
3359 | if (add_submodule(&add_data)) { | |
3360 | free(add_data.sm_path); | |
3361 | return 1; | |
3362 | } | |
a452128a | 3363 | configure_added_submodule(&add_data); |
a6226fd7 | 3364 | free(add_data.sm_path); |
8f790151 | 3365 | free(to_free); |
a452128a AR |
3366 | |
3367 | return 0; | |
3368 | } | |
3369 | ||
89c86265 SB |
3370 | #define SUPPORT_SUPER_PREFIX (1<<0) |
3371 | ||
74703a1e SB |
3372 | struct cmd_struct { |
3373 | const char *cmd; | |
3374 | int (*fn)(int, const char **, const char *); | |
89c86265 | 3375 | unsigned option; |
74703a1e SB |
3376 | }; |
3377 | ||
3378 | static struct cmd_struct commands[] = { | |
89c86265 SB |
3379 | {"list", module_list, 0}, |
3380 | {"name", module_name, 0}, | |
3381 | {"clone", module_clone, 0}, | |
a6226fd7 | 3382 | {"add", module_add, SUPPORT_SUPER_PREFIX}, |
b3c5f5cb | 3383 | {"update", module_update, 0}, |
89c86265 | 3384 | {"resolve-relative-url-test", resolve_relative_url_test, 0}, |
fc1b9243 | 3385 | {"foreach", module_foreach, SUPPORT_SUPER_PREFIX}, |
6e7c14e6 | 3386 | {"init", module_init, SUPPORT_SUPER_PREFIX}, |
a9f8a375 | 3387 | {"status", module_status, SUPPORT_SUPER_PREFIX}, |
13424764 | 3388 | {"sync", module_sync, SUPPORT_SUPER_PREFIX}, |
2e612731 | 3389 | {"deinit", module_deinit, 0}, |
e83e3333 | 3390 | {"summary", module_summary, SUPPORT_SUPER_PREFIX}, |
93481a6b | 3391 | {"push-check", push_check, 0}, |
6e556c41 | 3392 | {"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX}, |
5c2bd8b7 | 3393 | {"is-active", is_active, 0}, |
0383bbb9 | 3394 | {"check-name", check_name, 0}, |
2502ffc0 | 3395 | {"config", module_config, 0}, |
6417cf9c | 3396 | {"set-url", module_set_url, 0}, |
2964d6e5 | 3397 | {"set-branch", module_set_branch, 0}, |
961b130d | 3398 | {"create-branch", module_create_branch, 0}, |
74703a1e SB |
3399 | }; |
3400 | ||
3401 | int cmd_submodule__helper(int argc, const char **argv, const char *prefix) | |
3402 | { | |
3403 | int i; | |
f0994fa8 JK |
3404 | if (argc < 2 || !strcmp(argv[1], "-h")) |
3405 | usage("git submodule--helper <command>"); | |
74703a1e | 3406 | |
89c86265 SB |
3407 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
3408 | if (!strcmp(argv[1], commands[i].cmd)) { | |
3409 | if (get_super_prefix() && | |
3410 | !(commands[i].option & SUPPORT_SUPER_PREFIX)) | |
3411 | die(_("%s doesn't support --super-prefix"), | |
3412 | commands[i].cmd); | |
74703a1e | 3413 | return commands[i].fn(argc - 1, argv + 1, prefix); |
89c86265 SB |
3414 | } |
3415 | } | |
74703a1e | 3416 | |
cdc04b65 | 3417 | die(_("'%s' is not a valid submodule--helper " |
74703a1e SB |
3418 | "subcommand"), argv[1]); |
3419 | } |