]>
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" |
63e95beb | 22 | |
9f580a62 | 23 | #define OPT_QUIET (1 << 0) |
a9f8a375 PC |
24 | #define OPT_CACHED (1 << 1) |
25 | #define OPT_RECURSIVE (1 << 2) | |
2e612731 | 26 | #define OPT_FORCE (1 << 3) |
9f580a62 PC |
27 | |
28 | typedef void (*each_submodule_fn)(const struct cache_entry *list_item, | |
29 | void *cb_data); | |
63e95beb SB |
30 | |
31 | static char *get_default_remote(void) | |
32 | { | |
33 | char *dest = NULL, *ret; | |
63e95beb | 34 | struct strbuf sb = STRBUF_INIT; |
744c040b | 35 | const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); |
63e95beb SB |
36 | |
37 | if (!refname) | |
38 | die(_("No such ref: %s"), "HEAD"); | |
39 | ||
40 | /* detached HEAD */ | |
41 | if (!strcmp(refname, "HEAD")) | |
42 | return xstrdup("origin"); | |
43 | ||
44 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
45 | die(_("Expecting a full ref name, got %s"), refname); | |
46 | ||
47 | strbuf_addf(&sb, "branch.%s.remote", refname); | |
48 | if (git_config_get_string(sb.buf, &dest)) | |
49 | ret = xstrdup("origin"); | |
50 | else | |
51 | ret = dest; | |
52 | ||
53 | strbuf_release(&sb); | |
54 | return ret; | |
55 | } | |
56 | ||
13424764 PC |
57 | static int print_default_remote(int argc, const char **argv, const char *prefix) |
58 | { | |
e6be8e2f | 59 | char *remote; |
13424764 PC |
60 | |
61 | if (argc != 1) | |
62 | die(_("submodule--helper print-default-remote takes no arguments")); | |
63 | ||
64 | remote = get_default_remote(); | |
65 | if (remote) | |
66 | printf("%s\n", remote); | |
67 | ||
e6be8e2f | 68 | free(remote); |
13424764 PC |
69 | return 0; |
70 | } | |
71 | ||
63e95beb SB |
72 | static int starts_with_dot_slash(const char *str) |
73 | { | |
74 | return str[0] == '.' && is_dir_sep(str[1]); | |
75 | } | |
76 | ||
77 | static int starts_with_dot_dot_slash(const char *str) | |
78 | { | |
79 | return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]); | |
80 | } | |
81 | ||
82 | /* | |
83 | * Returns 1 if it was the last chop before ':'. | |
84 | */ | |
85 | static int chop_last_dir(char **remoteurl, int is_relative) | |
86 | { | |
87 | char *rfind = find_last_dir_sep(*remoteurl); | |
88 | if (rfind) { | |
89 | *rfind = '\0'; | |
90 | return 0; | |
91 | } | |
92 | ||
93 | rfind = strrchr(*remoteurl, ':'); | |
94 | if (rfind) { | |
95 | *rfind = '\0'; | |
96 | return 1; | |
97 | } | |
98 | ||
99 | if (is_relative || !strcmp(".", *remoteurl)) | |
100 | die(_("cannot strip one component off url '%s'"), | |
101 | *remoteurl); | |
102 | ||
103 | free(*remoteurl); | |
104 | *remoteurl = xstrdup("."); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | /* | |
109 | * The `url` argument is the URL that navigates to the submodule origin | |
110 | * repo. When relative, this URL is relative to the superproject origin | |
111 | * URL repo. The `up_path` argument, if specified, is the relative | |
112 | * path that navigates from the submodule working tree to the superproject | |
113 | * working tree. Returns the origin URL of the submodule. | |
114 | * | |
115 | * Return either an absolute URL or filesystem path (if the superproject | |
116 | * origin URL is an absolute URL or filesystem path, respectively) or a | |
117 | * relative file system path (if the superproject origin URL is a relative | |
118 | * file system path). | |
119 | * | |
120 | * When the output is a relative file system path, the path is either | |
121 | * relative to the submodule working tree, if up_path is specified, or to | |
122 | * the superproject working tree otherwise. | |
123 | * | |
124 | * NEEDSWORK: This works incorrectly on the domain and protocol part. | |
125 | * remote_url url outcome expectation | |
126 | * http://a.com/b ../c http://a.com/c as is | |
08788504 SB |
127 | * http://a.com/b/ ../c http://a.com/c same as previous line, but |
128 | * ignore trailing slash in url | |
63e95beb SB |
129 | * http://a.com/b ../../c http://c error out |
130 | * http://a.com/b ../../../c http:/c error out | |
131 | * http://a.com/b ../../../../c http:c error out | |
132 | * http://a.com/b ../../../../../c .:c error out | |
133 | * NEEDSWORK: Given how chop_last_dir() works, this function is broken | |
134 | * when a local part has a colon in its path component, too. | |
135 | */ | |
136 | static char *relative_url(const char *remote_url, | |
137 | const char *url, | |
138 | const char *up_path) | |
139 | { | |
140 | int is_relative = 0; | |
141 | int colonsep = 0; | |
142 | char *out; | |
143 | char *remoteurl = xstrdup(remote_url); | |
144 | struct strbuf sb = STRBUF_INIT; | |
145 | size_t len = strlen(remoteurl); | |
146 | ||
08788504 SB |
147 | if (is_dir_sep(remoteurl[len-1])) |
148 | remoteurl[len-1] = '\0'; | |
63e95beb SB |
149 | |
150 | if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl)) | |
151 | is_relative = 0; | |
152 | else { | |
153 | is_relative = 1; | |
154 | /* | |
155 | * Prepend a './' to ensure all relative | |
156 | * remoteurls start with './' or '../' | |
157 | */ | |
158 | if (!starts_with_dot_slash(remoteurl) && | |
159 | !starts_with_dot_dot_slash(remoteurl)) { | |
160 | strbuf_reset(&sb); | |
161 | strbuf_addf(&sb, "./%s", remoteurl); | |
162 | free(remoteurl); | |
163 | remoteurl = strbuf_detach(&sb, NULL); | |
164 | } | |
165 | } | |
166 | /* | |
167 | * When the url starts with '../', remove that and the | |
168 | * last directory in remoteurl. | |
169 | */ | |
170 | while (url) { | |
171 | if (starts_with_dot_dot_slash(url)) { | |
172 | url += 3; | |
173 | colonsep |= chop_last_dir(&remoteurl, is_relative); | |
174 | } else if (starts_with_dot_slash(url)) | |
175 | url += 2; | |
176 | else | |
177 | break; | |
178 | } | |
179 | strbuf_reset(&sb); | |
180 | strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url); | |
3389e78e SB |
181 | if (ends_with(url, "/")) |
182 | strbuf_setlen(&sb, sb.len - 1); | |
63e95beb SB |
183 | free(remoteurl); |
184 | ||
185 | if (starts_with_dot_slash(sb.buf)) | |
186 | out = xstrdup(sb.buf + 2); | |
187 | else | |
188 | out = xstrdup(sb.buf); | |
189 | strbuf_reset(&sb); | |
190 | ||
191 | if (!up_path || !is_relative) | |
192 | return out; | |
193 | ||
194 | strbuf_addf(&sb, "%s%s", up_path, out); | |
195 | free(out); | |
196 | return strbuf_detach(&sb, NULL); | |
197 | } | |
198 | ||
199 | static int resolve_relative_url(int argc, const char **argv, const char *prefix) | |
200 | { | |
201 | char *remoteurl = NULL; | |
202 | char *remote = get_default_remote(); | |
203 | const char *up_path = NULL; | |
204 | char *res; | |
205 | const char *url; | |
206 | struct strbuf sb = STRBUF_INIT; | |
207 | ||
208 | if (argc != 2 && argc != 3) | |
209 | die("resolve-relative-url only accepts one or two arguments"); | |
210 | ||
211 | url = argv[1]; | |
212 | strbuf_addf(&sb, "remote.%s.url", remote); | |
213 | free(remote); | |
214 | ||
215 | if (git_config_get_string(sb.buf, &remoteurl)) | |
216 | /* the repository is its own authoritative upstream */ | |
217 | remoteurl = xgetcwd(); | |
218 | ||
219 | if (argc == 3) | |
220 | up_path = argv[2]; | |
221 | ||
222 | res = relative_url(remoteurl, url, up_path); | |
223 | puts(res); | |
224 | free(res); | |
225 | free(remoteurl); | |
226 | return 0; | |
227 | } | |
228 | ||
229 | static int resolve_relative_url_test(int argc, const char **argv, const char *prefix) | |
230 | { | |
231 | char *remoteurl, *res; | |
232 | const char *up_path, *url; | |
233 | ||
234 | if (argc != 4) | |
235 | die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>"); | |
236 | ||
237 | up_path = argv[1]; | |
238 | remoteurl = xstrdup(argv[2]); | |
239 | url = argv[3]; | |
240 | ||
241 | if (!strcmp(up_path, "(null)")) | |
242 | up_path = NULL; | |
243 | ||
244 | res = relative_url(remoteurl, url, up_path); | |
245 | puts(res); | |
246 | free(res); | |
247 | free(remoteurl); | |
248 | return 0; | |
249 | } | |
74703a1e | 250 | |
74a10642 PC |
251 | /* the result should be freed by the caller. */ |
252 | static char *get_submodule_displaypath(const char *path, const char *prefix) | |
253 | { | |
254 | const char *super_prefix = get_super_prefix(); | |
255 | ||
256 | if (prefix && super_prefix) { | |
257 | BUG("cannot have prefix '%s' and superprefix '%s'", | |
258 | prefix, super_prefix); | |
259 | } else if (prefix) { | |
260 | struct strbuf sb = STRBUF_INIT; | |
261 | char *displaypath = xstrdup(relative_path(path, prefix, &sb)); | |
262 | strbuf_release(&sb); | |
263 | return displaypath; | |
264 | } else if (super_prefix) { | |
265 | return xstrfmt("%s%s", super_prefix, path); | |
266 | } else { | |
267 | return xstrdup(path); | |
268 | } | |
269 | } | |
270 | ||
a9f8a375 PC |
271 | static char *compute_rev_name(const char *sub_path, const char* object_id) |
272 | { | |
273 | struct strbuf sb = STRBUF_INIT; | |
274 | const char ***d; | |
275 | ||
276 | static const char *describe_bare[] = { NULL }; | |
277 | ||
278 | static const char *describe_tags[] = { "--tags", NULL }; | |
279 | ||
280 | static const char *describe_contains[] = { "--contains", NULL }; | |
281 | ||
282 | static const char *describe_all_always[] = { "--all", "--always", NULL }; | |
283 | ||
284 | static const char **describe_argv[] = { describe_bare, describe_tags, | |
285 | describe_contains, | |
286 | describe_all_always, NULL }; | |
287 | ||
288 | for (d = describe_argv; *d; d++) { | |
289 | struct child_process cp = CHILD_PROCESS_INIT; | |
290 | prepare_submodule_repo_env(&cp.env_array); | |
291 | cp.dir = sub_path; | |
292 | cp.git_cmd = 1; | |
293 | cp.no_stderr = 1; | |
294 | ||
295 | argv_array_push(&cp.args, "describe"); | |
296 | argv_array_pushv(&cp.args, *d); | |
297 | argv_array_push(&cp.args, object_id); | |
298 | ||
299 | if (!capture_command(&cp, &sb, 0)) { | |
300 | strbuf_strip_suffix(&sb, "\n"); | |
301 | return strbuf_detach(&sb, NULL); | |
302 | } | |
303 | } | |
304 | ||
305 | strbuf_release(&sb); | |
306 | return NULL; | |
307 | } | |
308 | ||
74703a1e SB |
309 | struct module_list { |
310 | const struct cache_entry **entries; | |
311 | int alloc, nr; | |
312 | }; | |
313 | #define MODULE_LIST_INIT { NULL, 0, 0 } | |
314 | ||
315 | static int module_list_compute(int argc, const char **argv, | |
316 | const char *prefix, | |
317 | struct pathspec *pathspec, | |
318 | struct module_list *list) | |
319 | { | |
320 | int i, result = 0; | |
2b56bb7a | 321 | char *ps_matched = NULL; |
74703a1e | 322 | parse_pathspec(pathspec, 0, |
2249d4db | 323 | PATHSPEC_PREFER_FULL, |
74703a1e SB |
324 | prefix, argv); |
325 | ||
74703a1e SB |
326 | if (pathspec->nr) |
327 | ps_matched = xcalloc(pathspec->nr, 1); | |
328 | ||
329 | if (read_cache() < 0) | |
330 | die(_("index file corrupt")); | |
331 | ||
332 | for (i = 0; i < active_nr; i++) { | |
333 | const struct cache_entry *ce = active_cache[i]; | |
334 | ||
6d2df284 | 335 | if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce), |
84ba959b SB |
336 | 0, ps_matched, 1) || |
337 | !S_ISGITLINK(ce->ce_mode)) | |
74703a1e SB |
338 | continue; |
339 | ||
340 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); | |
341 | list->entries[list->nr++] = ce; | |
342 | while (i + 1 < active_nr && | |
343 | !strcmp(ce->name, active_cache[i + 1]->name)) | |
344 | /* | |
345 | * Skip entries with the same name in different stages | |
346 | * to make sure an entry is returned only once. | |
347 | */ | |
348 | i++; | |
349 | } | |
74703a1e SB |
350 | |
351 | if (ps_matched && report_path_error(ps_matched, pathspec, prefix)) | |
352 | result = -1; | |
353 | ||
354 | free(ps_matched); | |
355 | ||
356 | return result; | |
357 | } | |
358 | ||
3e7eaed0 BW |
359 | static void module_list_active(struct module_list *list) |
360 | { | |
361 | int i; | |
362 | struct module_list active_modules = MODULE_LIST_INIT; | |
363 | ||
3e7eaed0 BW |
364 | for (i = 0; i < list->nr; i++) { |
365 | const struct cache_entry *ce = list->entries[i]; | |
366 | ||
627d9342 | 367 | if (!is_submodule_active(the_repository, ce->name)) |
3e7eaed0 BW |
368 | continue; |
369 | ||
370 | ALLOC_GROW(active_modules.entries, | |
371 | active_modules.nr + 1, | |
372 | active_modules.alloc); | |
373 | active_modules.entries[active_modules.nr++] = ce; | |
374 | } | |
375 | ||
376 | free(list->entries); | |
377 | *list = active_modules; | |
378 | } | |
379 | ||
13424764 PC |
380 | static char *get_up_path(const char *path) |
381 | { | |
382 | int i; | |
383 | struct strbuf sb = STRBUF_INIT; | |
384 | ||
385 | for (i = count_slashes(path); i; i--) | |
386 | strbuf_addstr(&sb, "../"); | |
387 | ||
388 | /* | |
389 | * Check if 'path' ends with slash or not | |
390 | * for having the same output for dir/sub_dir | |
391 | * and dir/sub_dir/ | |
392 | */ | |
393 | if (!is_dir_sep(path[strlen(path) - 1])) | |
394 | strbuf_addstr(&sb, "../"); | |
395 | ||
396 | return strbuf_detach(&sb, NULL); | |
397 | } | |
398 | ||
74703a1e SB |
399 | static int module_list(int argc, const char **argv, const char *prefix) |
400 | { | |
401 | int i; | |
402 | struct pathspec pathspec; | |
403 | struct module_list list = MODULE_LIST_INIT; | |
404 | ||
405 | struct option module_list_options[] = { | |
406 | OPT_STRING(0, "prefix", &prefix, | |
407 | N_("path"), | |
408 | N_("alternative anchor for relative paths")), | |
409 | OPT_END() | |
410 | }; | |
411 | ||
412 | const char *const git_submodule_helper_usage[] = { | |
413 | N_("git submodule--helper list [--prefix=<path>] [<path>...]"), | |
414 | NULL | |
415 | }; | |
416 | ||
417 | argc = parse_options(argc, argv, prefix, module_list_options, | |
418 | git_submodule_helper_usage, 0); | |
419 | ||
b0f4b408 | 420 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
74703a1e | 421 | return 1; |
74703a1e SB |
422 | |
423 | for (i = 0; i < list.nr; i++) { | |
424 | const struct cache_entry *ce = list.entries[i]; | |
425 | ||
426 | if (ce_stage(ce)) | |
427 | printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1)); | |
428 | else | |
99d1a986 | 429 | printf("%06o %s %d\t", ce->ce_mode, |
430 | oid_to_hex(&ce->oid), ce_stage(ce)); | |
74703a1e | 431 | |
dc4b4a61 | 432 | fprintf(stdout, "%s\n", ce->name); |
74703a1e SB |
433 | } |
434 | return 0; | |
435 | } | |
436 | ||
9f580a62 PC |
437 | static void for_each_listed_submodule(const struct module_list *list, |
438 | each_submodule_fn fn, void *cb_data) | |
439 | { | |
440 | int i; | |
441 | for (i = 0; i < list->nr; i++) | |
442 | fn(list->entries[i], cb_data); | |
443 | } | |
444 | ||
fc1b9243 PC |
445 | struct cb_foreach { |
446 | int argc; | |
447 | const char **argv; | |
448 | const char *prefix; | |
449 | int quiet; | |
450 | int recursive; | |
451 | }; | |
452 | #define CB_FOREACH_INIT { 0 } | |
453 | ||
454 | static void runcommand_in_submodule_cb(const struct cache_entry *list_item, | |
455 | void *cb_data) | |
456 | { | |
457 | struct cb_foreach *info = cb_data; | |
458 | const char *path = list_item->name; | |
459 | const struct object_id *ce_oid = &list_item->oid; | |
460 | ||
461 | const struct submodule *sub; | |
462 | struct child_process cp = CHILD_PROCESS_INIT; | |
463 | char *displaypath; | |
464 | ||
465 | displaypath = get_submodule_displaypath(path, info->prefix); | |
466 | ||
467 | sub = submodule_from_path(the_repository, &null_oid, path); | |
468 | ||
469 | if (!sub) | |
470 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
471 | displaypath); | |
472 | ||
473 | if (!is_submodule_populated_gently(path, NULL)) | |
474 | goto cleanup; | |
475 | ||
476 | prepare_submodule_repo_env(&cp.env_array); | |
477 | ||
478 | /* | |
479 | * For the purpose of executing <command> in the submodule, | |
480 | * separate shell is used for the purpose of running the | |
481 | * child process. | |
482 | */ | |
483 | cp.use_shell = 1; | |
484 | cp.dir = path; | |
485 | ||
486 | /* | |
487 | * NEEDSWORK: the command currently has access to the variables $name, | |
488 | * $sm_path, $displaypath, $sha1 and $toplevel only when the command | |
489 | * contains a single argument. This is done for maintaining a faithful | |
490 | * translation from shell script. | |
491 | */ | |
492 | if (info->argc == 1) { | |
493 | char *toplevel = xgetcwd(); | |
494 | struct strbuf sb = STRBUF_INIT; | |
495 | ||
496 | argv_array_pushf(&cp.env_array, "name=%s", sub->name); | |
497 | argv_array_pushf(&cp.env_array, "sm_path=%s", path); | |
498 | argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath); | |
499 | argv_array_pushf(&cp.env_array, "sha1=%s", | |
500 | oid_to_hex(ce_oid)); | |
501 | argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel); | |
502 | ||
503 | /* | |
504 | * Since the path variable was accessible from the script | |
505 | * before porting, it is also made available after porting. | |
506 | * The environment variable "PATH" has a very special purpose | |
507 | * on windows. And since environment variables are | |
508 | * case-insensitive in windows, it interferes with the | |
509 | * existing PATH variable. Hence, to avoid that, we expose | |
510 | * path via the args argv_array and not via env_array. | |
511 | */ | |
512 | sq_quote_buf(&sb, path); | |
513 | argv_array_pushf(&cp.args, "path=%s; %s", | |
514 | sb.buf, info->argv[0]); | |
515 | strbuf_release(&sb); | |
516 | free(toplevel); | |
517 | } else { | |
518 | argv_array_pushv(&cp.args, info->argv); | |
519 | } | |
520 | ||
521 | if (!info->quiet) | |
522 | printf(_("Entering '%s'\n"), displaypath); | |
523 | ||
524 | if (info->argv[0] && run_command(&cp)) | |
525 | die(_("run_command returned non-zero status for %s\n."), | |
526 | displaypath); | |
527 | ||
528 | if (info->recursive) { | |
529 | struct child_process cpr = CHILD_PROCESS_INIT; | |
530 | ||
531 | cpr.git_cmd = 1; | |
532 | cpr.dir = path; | |
533 | prepare_submodule_repo_env(&cpr.env_array); | |
534 | ||
535 | argv_array_pushl(&cpr.args, "--super-prefix", NULL); | |
536 | argv_array_pushf(&cpr.args, "%s/", displaypath); | |
537 | argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive", | |
538 | NULL); | |
539 | ||
540 | if (info->quiet) | |
541 | argv_array_push(&cpr.args, "--quiet"); | |
542 | ||
543 | argv_array_pushv(&cpr.args, info->argv); | |
544 | ||
545 | if (run_command(&cpr)) | |
27c929ed | 546 | die(_("run_command returned non-zero status while " |
fc1b9243 PC |
547 | "recursing in the nested submodules of %s\n."), |
548 | displaypath); | |
549 | } | |
550 | ||
551 | cleanup: | |
552 | free(displaypath); | |
553 | } | |
554 | ||
555 | static int module_foreach(int argc, const char **argv, const char *prefix) | |
556 | { | |
557 | struct cb_foreach info = CB_FOREACH_INIT; | |
558 | struct pathspec pathspec; | |
559 | struct module_list list = MODULE_LIST_INIT; | |
560 | ||
561 | struct option module_foreach_options[] = { | |
562 | OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")), | |
563 | OPT_BOOL(0, "recursive", &info.recursive, | |
564 | N_("Recurse into nested submodules")), | |
565 | OPT_END() | |
566 | }; | |
567 | ||
568 | const char *const git_submodule_helper_usage[] = { | |
569 | N_("git submodule--helper foreach [--quiet] [--recursive] <command>"), | |
570 | NULL | |
571 | }; | |
572 | ||
573 | argc = parse_options(argc, argv, prefix, module_foreach_options, | |
574 | git_submodule_helper_usage, PARSE_OPT_KEEP_UNKNOWN); | |
575 | ||
576 | if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0) | |
577 | return 1; | |
578 | ||
579 | info.argc = argc; | |
580 | info.argv = argv; | |
581 | info.prefix = prefix; | |
582 | ||
583 | for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info); | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
e0a862fd SB |
588 | static char *compute_submodule_clone_url(const char *rel_url) |
589 | { | |
590 | char *remoteurl, *relurl; | |
591 | char *remote = get_default_remote(); | |
592 | struct strbuf remotesb = STRBUF_INIT; | |
593 | ||
594 | strbuf_addf(&remotesb, "remote.%s.url", remote); | |
595 | if (git_config_get_string(remotesb.buf, &remoteurl)) { | |
596 | warning(_("could not look up configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf); | |
597 | remoteurl = xgetcwd(); | |
598 | } | |
599 | relurl = relative_url(remoteurl, rel_url, NULL); | |
600 | ||
601 | free(remote); | |
602 | free(remoteurl); | |
603 | strbuf_release(&remotesb); | |
604 | ||
605 | return relurl; | |
606 | } | |
607 | ||
9f580a62 PC |
608 | struct init_cb { |
609 | const char *prefix; | |
610 | unsigned int flags; | |
611 | }; | |
612 | ||
613 | #define INIT_CB_INIT { NULL, 0 } | |
614 | ||
615 | static void init_submodule(const char *path, const char *prefix, | |
616 | unsigned int flags) | |
3604242f SB |
617 | { |
618 | const struct submodule *sub; | |
619 | struct strbuf sb = STRBUF_INIT; | |
620 | char *upd = NULL, *url = NULL, *displaypath; | |
621 | ||
74a10642 | 622 | displaypath = get_submodule_displaypath(path, prefix); |
d92028a5 | 623 | |
3b8fb393 | 624 | sub = submodule_from_path(the_repository, &null_oid, path); |
d92028a5 SB |
625 | |
626 | if (!sub) | |
627 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
628 | displaypath); | |
3604242f | 629 | |
1f8d7115 BW |
630 | /* |
631 | * NEEDSWORK: In a multi-working-tree world, this needs to be | |
632 | * set in the per-worktree config. | |
633 | * | |
634 | * Set active flag for the submodule being initialized | |
635 | */ | |
627d9342 | 636 | if (!is_submodule_active(the_repository, path)) { |
1f8d7115 BW |
637 | strbuf_addf(&sb, "submodule.%s.active", sub->name); |
638 | git_config_set_gently(sb.buf, "true"); | |
74a10642 | 639 | strbuf_reset(&sb); |
1f8d7115 BW |
640 | } |
641 | ||
3604242f SB |
642 | /* |
643 | * Copy url setting when it is not set yet. | |
644 | * To look up the url in .git/config, we must not fall back to | |
645 | * .gitmodules, so look it up directly. | |
646 | */ | |
3604242f SB |
647 | strbuf_addf(&sb, "submodule.%s.url", sub->name); |
648 | if (git_config_get_string(sb.buf, &url)) { | |
627fde10 | 649 | if (!sub->url) |
3604242f SB |
650 | die(_("No url found for submodule path '%s' in .gitmodules"), |
651 | displaypath); | |
652 | ||
627fde10 JK |
653 | url = xstrdup(sub->url); |
654 | ||
3604242f SB |
655 | /* Possibly a url relative to parent */ |
656 | if (starts_with_dot_dot_slash(url) || | |
657 | starts_with_dot_slash(url)) { | |
e0a862fd SB |
658 | char *oldurl = url; |
659 | url = compute_submodule_clone_url(oldurl); | |
660 | free(oldurl); | |
3604242f SB |
661 | } |
662 | ||
663 | if (git_config_set_gently(sb.buf, url)) | |
664 | die(_("Failed to register url for submodule path '%s'"), | |
665 | displaypath); | |
9f580a62 | 666 | if (!(flags & OPT_QUIET)) |
c66410ed SB |
667 | fprintf(stderr, |
668 | _("Submodule '%s' (%s) registered for path '%s'\n"), | |
3604242f SB |
669 | sub->name, url, displaypath); |
670 | } | |
74a10642 | 671 | strbuf_reset(&sb); |
3604242f SB |
672 | |
673 | /* Copy "update" setting when it is not set yet */ | |
3604242f SB |
674 | strbuf_addf(&sb, "submodule.%s.update", sub->name); |
675 | if (git_config_get_string(sb.buf, &upd) && | |
676 | sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
677 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) { | |
678 | fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"), | |
679 | sub->name); | |
680 | upd = xstrdup("none"); | |
681 | } else | |
682 | upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy)); | |
683 | ||
684 | if (git_config_set_gently(sb.buf, upd)) | |
685 | die(_("Failed to register update mode for submodule path '%s'"), displaypath); | |
686 | } | |
687 | strbuf_release(&sb); | |
688 | free(displaypath); | |
689 | free(url); | |
690 | free(upd); | |
691 | } | |
692 | ||
9f580a62 PC |
693 | static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data) |
694 | { | |
695 | struct init_cb *info = cb_data; | |
696 | init_submodule(list_item->name, info->prefix, info->flags); | |
697 | } | |
698 | ||
3604242f SB |
699 | static int module_init(int argc, const char **argv, const char *prefix) |
700 | { | |
9f580a62 | 701 | struct init_cb info = INIT_CB_INIT; |
3604242f SB |
702 | struct pathspec pathspec; |
703 | struct module_list list = MODULE_LIST_INIT; | |
704 | int quiet = 0; | |
3604242f SB |
705 | |
706 | struct option module_init_options[] = { | |
3604242f SB |
707 | OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")), |
708 | OPT_END() | |
709 | }; | |
710 | ||
711 | const char *const git_submodule_helper_usage[] = { | |
712 | N_("git submodule--helper init [<path>]"), | |
713 | NULL | |
714 | }; | |
715 | ||
716 | argc = parse_options(argc, argv, prefix, module_init_options, | |
717 | git_submodule_helper_usage, 0); | |
718 | ||
719 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
720 | return 1; | |
721 | ||
3e7eaed0 BW |
722 | /* |
723 | * If there are no path args and submodule.active is set then, | |
724 | * by default, only initialize 'active' modules. | |
725 | */ | |
726 | if (!argc && git_config_get_value_multi("submodule.active")) | |
727 | module_list_active(&list); | |
728 | ||
9f580a62 PC |
729 | info.prefix = prefix; |
730 | if (quiet) | |
731 | info.flags |= OPT_QUIET; | |
732 | ||
733 | for_each_listed_submodule(&list, init_submodule_cb, &info); | |
3604242f SB |
734 | |
735 | return 0; | |
736 | } | |
737 | ||
a9f8a375 PC |
738 | struct status_cb { |
739 | const char *prefix; | |
740 | unsigned int flags; | |
741 | }; | |
742 | ||
743 | #define STATUS_CB_INIT { NULL, 0 } | |
744 | ||
745 | static void print_status(unsigned int flags, char state, const char *path, | |
746 | const struct object_id *oid, const char *displaypath) | |
747 | { | |
748 | if (flags & OPT_QUIET) | |
749 | return; | |
750 | ||
751 | printf("%c%s %s", state, oid_to_hex(oid), displaypath); | |
752 | ||
0b5e2ea7 NTND |
753 | if (state == ' ' || state == '+') { |
754 | const char *name = compute_rev_name(path, oid_to_hex(oid)); | |
755 | ||
756 | if (name) | |
757 | printf(" (%s)", name); | |
758 | } | |
a9f8a375 PC |
759 | |
760 | printf("\n"); | |
761 | } | |
762 | ||
763 | static int handle_submodule_head_ref(const char *refname, | |
764 | const struct object_id *oid, int flags, | |
765 | void *cb_data) | |
766 | { | |
767 | struct object_id *output = cb_data; | |
768 | if (oid) | |
769 | oidcpy(output, oid); | |
770 | ||
771 | return 0; | |
772 | } | |
773 | ||
774 | static void status_submodule(const char *path, const struct object_id *ce_oid, | |
775 | unsigned int ce_flags, const char *prefix, | |
776 | unsigned int flags) | |
777 | { | |
778 | char *displaypath; | |
779 | struct argv_array diff_files_args = ARGV_ARRAY_INIT; | |
780 | struct rev_info rev; | |
781 | int diff_files_result; | |
782 | ||
3b8fb393 | 783 | if (!submodule_from_path(the_repository, &null_oid, path)) |
a9f8a375 PC |
784 | die(_("no submodule mapping found in .gitmodules for path '%s'"), |
785 | path); | |
786 | ||
787 | displaypath = get_submodule_displaypath(path, prefix); | |
788 | ||
789 | if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) { | |
790 | print_status(flags, 'U', path, &null_oid, displaypath); | |
791 | goto cleanup; | |
792 | } | |
793 | ||
794 | if (!is_submodule_active(the_repository, path)) { | |
795 | print_status(flags, '-', path, ce_oid, displaypath); | |
796 | goto cleanup; | |
797 | } | |
798 | ||
799 | argv_array_pushl(&diff_files_args, "diff-files", | |
800 | "--ignore-submodules=dirty", "--quiet", "--", | |
801 | path, NULL); | |
802 | ||
803 | git_config(git_diff_basic_config, NULL); | |
2abf3503 | 804 | repo_init_revisions(the_repository, &rev, prefix); |
a9f8a375 PC |
805 | rev.abbrev = 0; |
806 | diff_files_args.argc = setup_revisions(diff_files_args.argc, | |
807 | diff_files_args.argv, | |
808 | &rev, NULL); | |
809 | diff_files_result = run_diff_files(&rev, 0); | |
810 | ||
811 | if (!diff_result_code(&rev.diffopt, diff_files_result)) { | |
812 | print_status(flags, ' ', path, ce_oid, | |
813 | displaypath); | |
814 | } else if (!(flags & OPT_CACHED)) { | |
815 | struct object_id oid; | |
74b6bda3 | 816 | struct ref_store *refs = get_submodule_ref_store(path); |
a9f8a375 | 817 | |
74b6bda3 RS |
818 | if (!refs) { |
819 | print_status(flags, '-', path, ce_oid, displaypath); | |
820 | goto cleanup; | |
821 | } | |
822 | if (refs_head_ref(refs, handle_submodule_head_ref, &oid)) | |
ed5bdd5b | 823 | die(_("could not resolve HEAD ref inside the " |
a9f8a375 PC |
824 | "submodule '%s'"), path); |
825 | ||
826 | print_status(flags, '+', path, &oid, displaypath); | |
827 | } else { | |
828 | print_status(flags, '+', path, ce_oid, displaypath); | |
829 | } | |
830 | ||
831 | if (flags & OPT_RECURSIVE) { | |
832 | struct child_process cpr = CHILD_PROCESS_INIT; | |
833 | ||
834 | cpr.git_cmd = 1; | |
835 | cpr.dir = path; | |
836 | prepare_submodule_repo_env(&cpr.env_array); | |
837 | ||
838 | argv_array_push(&cpr.args, "--super-prefix"); | |
839 | argv_array_pushf(&cpr.args, "%s/", displaypath); | |
840 | argv_array_pushl(&cpr.args, "submodule--helper", "status", | |
841 | "--recursive", NULL); | |
842 | ||
843 | if (flags & OPT_CACHED) | |
844 | argv_array_push(&cpr.args, "--cached"); | |
845 | ||
846 | if (flags & OPT_QUIET) | |
847 | argv_array_push(&cpr.args, "--quiet"); | |
848 | ||
849 | if (run_command(&cpr)) | |
850 | die(_("failed to recurse into submodule '%s'"), path); | |
851 | } | |
852 | ||
853 | cleanup: | |
854 | argv_array_clear(&diff_files_args); | |
855 | free(displaypath); | |
856 | } | |
857 | ||
858 | static void status_submodule_cb(const struct cache_entry *list_item, | |
859 | void *cb_data) | |
860 | { | |
861 | struct status_cb *info = cb_data; | |
862 | status_submodule(list_item->name, &list_item->oid, list_item->ce_flags, | |
863 | info->prefix, info->flags); | |
864 | } | |
865 | ||
866 | static int module_status(int argc, const char **argv, const char *prefix) | |
867 | { | |
868 | struct status_cb info = STATUS_CB_INIT; | |
869 | struct pathspec pathspec; | |
870 | struct module_list list = MODULE_LIST_INIT; | |
871 | int quiet = 0; | |
872 | ||
873 | struct option module_status_options[] = { | |
874 | OPT__QUIET(&quiet, N_("Suppress submodule status output")), | |
875 | OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED), | |
876 | OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE), | |
877 | OPT_END() | |
878 | }; | |
879 | ||
880 | const char *const git_submodule_helper_usage[] = { | |
881 | N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"), | |
882 | NULL | |
883 | }; | |
884 | ||
885 | argc = parse_options(argc, argv, prefix, module_status_options, | |
886 | git_submodule_helper_usage, 0); | |
887 | ||
888 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
889 | return 1; | |
890 | ||
891 | info.prefix = prefix; | |
892 | if (quiet) | |
893 | info.flags |= OPT_QUIET; | |
894 | ||
895 | for_each_listed_submodule(&list, status_submodule_cb, &info); | |
3604242f SB |
896 | |
897 | return 0; | |
898 | } | |
899 | ||
0ea306ef SB |
900 | static int module_name(int argc, const char **argv, const char *prefix) |
901 | { | |
902 | const struct submodule *sub; | |
903 | ||
904 | if (argc != 2) | |
905 | usage(_("git submodule--helper name <path>")); | |
906 | ||
3b8fb393 | 907 | sub = submodule_from_path(the_repository, &null_oid, argv[1]); |
0ea306ef SB |
908 | |
909 | if (!sub) | |
910 | die(_("no submodule mapping found in .gitmodules for path '%s'"), | |
911 | argv[1]); | |
912 | ||
913 | printf("%s\n", sub->name); | |
914 | ||
915 | return 0; | |
916 | } | |
14111fc4 | 917 | |
13424764 PC |
918 | struct sync_cb { |
919 | const char *prefix; | |
920 | unsigned int flags; | |
921 | }; | |
922 | ||
923 | #define SYNC_CB_INIT { NULL, 0 } | |
924 | ||
925 | static void sync_submodule(const char *path, const char *prefix, | |
926 | unsigned int flags) | |
927 | { | |
928 | const struct submodule *sub; | |
929 | char *remote_key = NULL; | |
930 | char *sub_origin_url, *super_config_url, *displaypath; | |
931 | struct strbuf sb = STRBUF_INIT; | |
932 | struct child_process cp = CHILD_PROCESS_INIT; | |
933 | char *sub_config_path = NULL; | |
934 | ||
935 | if (!is_submodule_active(the_repository, path)) | |
936 | return; | |
937 | ||
3b8fb393 | 938 | sub = submodule_from_path(the_repository, &null_oid, path); |
13424764 PC |
939 | |
940 | if (sub && sub->url) { | |
941 | if (starts_with_dot_dot_slash(sub->url) || | |
942 | starts_with_dot_slash(sub->url)) { | |
943 | char *remote_url, *up_path; | |
944 | char *remote = get_default_remote(); | |
945 | strbuf_addf(&sb, "remote.%s.url", remote); | |
946 | ||
947 | if (git_config_get_string(sb.buf, &remote_url)) | |
948 | remote_url = xgetcwd(); | |
949 | ||
950 | up_path = get_up_path(path); | |
951 | sub_origin_url = relative_url(remote_url, sub->url, up_path); | |
952 | super_config_url = relative_url(remote_url, sub->url, NULL); | |
953 | ||
954 | free(remote); | |
955 | free(up_path); | |
956 | free(remote_url); | |
957 | } else { | |
958 | sub_origin_url = xstrdup(sub->url); | |
959 | super_config_url = xstrdup(sub->url); | |
960 | } | |
961 | } else { | |
962 | sub_origin_url = xstrdup(""); | |
963 | super_config_url = xstrdup(""); | |
964 | } | |
965 | ||
966 | displaypath = get_submodule_displaypath(path, prefix); | |
967 | ||
968 | if (!(flags & OPT_QUIET)) | |
969 | printf(_("Synchronizing submodule url for '%s'\n"), | |
970 | displaypath); | |
971 | ||
972 | strbuf_reset(&sb); | |
973 | strbuf_addf(&sb, "submodule.%s.url", sub->name); | |
974 | if (git_config_set_gently(sb.buf, super_config_url)) | |
975 | die(_("failed to register url for submodule path '%s'"), | |
976 | displaypath); | |
977 | ||
978 | if (!is_submodule_populated_gently(path, NULL)) | |
979 | goto cleanup; | |
980 | ||
981 | prepare_submodule_repo_env(&cp.env_array); | |
982 | cp.git_cmd = 1; | |
983 | cp.dir = path; | |
984 | argv_array_pushl(&cp.args, "submodule--helper", | |
985 | "print-default-remote", NULL); | |
986 | ||
987 | strbuf_reset(&sb); | |
988 | if (capture_command(&cp, &sb, 0)) | |
989 | die(_("failed to get the default remote for submodule '%s'"), | |
990 | path); | |
991 | ||
992 | strbuf_strip_suffix(&sb, "\n"); | |
993 | remote_key = xstrfmt("remote.%s.url", sb.buf); | |
994 | ||
995 | strbuf_reset(&sb); | |
996 | submodule_to_gitdir(&sb, path); | |
997 | strbuf_addstr(&sb, "/config"); | |
998 | ||
999 | if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url)) | |
1000 | die(_("failed to update remote for submodule '%s'"), | |
1001 | path); | |
1002 | ||
1003 | if (flags & OPT_RECURSIVE) { | |
1004 | struct child_process cpr = CHILD_PROCESS_INIT; | |
1005 | ||
1006 | cpr.git_cmd = 1; | |
1007 | cpr.dir = path; | |
1008 | prepare_submodule_repo_env(&cpr.env_array); | |
1009 | ||
1010 | argv_array_push(&cpr.args, "--super-prefix"); | |
1011 | argv_array_pushf(&cpr.args, "%s/", displaypath); | |
1012 | argv_array_pushl(&cpr.args, "submodule--helper", "sync", | |
1013 | "--recursive", NULL); | |
1014 | ||
1015 | if (flags & OPT_QUIET) | |
1016 | argv_array_push(&cpr.args, "--quiet"); | |
1017 | ||
1018 | if (run_command(&cpr)) | |
1019 | die(_("failed to recurse into submodule '%s'"), | |
1020 | path); | |
1021 | } | |
1022 | ||
1023 | cleanup: | |
1024 | free(super_config_url); | |
1025 | free(sub_origin_url); | |
1026 | strbuf_release(&sb); | |
1027 | free(remote_key); | |
1028 | free(displaypath); | |
1029 | free(sub_config_path); | |
1030 | } | |
1031 | ||
1032 | static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data) | |
1033 | { | |
1034 | struct sync_cb *info = cb_data; | |
1035 | sync_submodule(list_item->name, info->prefix, info->flags); | |
13424764 PC |
1036 | } |
1037 | ||
1038 | static int module_sync(int argc, const char **argv, const char *prefix) | |
1039 | { | |
1040 | struct sync_cb info = SYNC_CB_INIT; | |
1041 | struct pathspec pathspec; | |
1042 | struct module_list list = MODULE_LIST_INIT; | |
1043 | int quiet = 0; | |
1044 | int recursive = 0; | |
1045 | ||
1046 | struct option module_sync_options[] = { | |
1047 | OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")), | |
1048 | OPT_BOOL(0, "recursive", &recursive, | |
1049 | N_("Recurse into nested submodules")), | |
1050 | OPT_END() | |
1051 | }; | |
1052 | ||
1053 | const char *const git_submodule_helper_usage[] = { | |
1054 | N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"), | |
1055 | NULL | |
1056 | }; | |
1057 | ||
1058 | argc = parse_options(argc, argv, prefix, module_sync_options, | |
1059 | git_submodule_helper_usage, 0); | |
1060 | ||
1061 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
1062 | return 1; | |
1063 | ||
1064 | info.prefix = prefix; | |
1065 | if (quiet) | |
1066 | info.flags |= OPT_QUIET; | |
1067 | if (recursive) | |
1068 | info.flags |= OPT_RECURSIVE; | |
1069 | ||
1070 | for_each_listed_submodule(&list, sync_submodule_cb, &info); | |
1071 | ||
1072 | return 0; | |
1073 | } | |
1074 | ||
2e612731 PC |
1075 | struct deinit_cb { |
1076 | const char *prefix; | |
1077 | unsigned int flags; | |
1078 | }; | |
1079 | #define DEINIT_CB_INIT { NULL, 0 } | |
1080 | ||
1081 | static void deinit_submodule(const char *path, const char *prefix, | |
1082 | unsigned int flags) | |
1083 | { | |
1084 | const struct submodule *sub; | |
1085 | char *displaypath = NULL; | |
1086 | struct child_process cp_config = CHILD_PROCESS_INIT; | |
1087 | struct strbuf sb_config = STRBUF_INIT; | |
1088 | char *sub_git_dir = xstrfmt("%s/.git", path); | |
1089 | ||
3b8fb393 | 1090 | sub = submodule_from_path(the_repository, &null_oid, path); |
2e612731 PC |
1091 | |
1092 | if (!sub || !sub->name) | |
1093 | goto cleanup; | |
1094 | ||
1095 | displaypath = get_submodule_displaypath(path, prefix); | |
1096 | ||
1097 | /* remove the submodule work tree (unless the user already did it) */ | |
1098 | if (is_directory(path)) { | |
1099 | struct strbuf sb_rm = STRBUF_INIT; | |
1100 | const char *format; | |
1101 | ||
1102 | /* | |
1103 | * protect submodules containing a .git directory | |
1104 | * NEEDSWORK: instead of dying, automatically call | |
1105 | * absorbgitdirs and (possibly) warn. | |
1106 | */ | |
1107 | if (is_directory(sub_git_dir)) | |
1108 | die(_("Submodule work tree '%s' contains a .git " | |
1109 | "directory (use 'rm -rf' if you really want " | |
1110 | "to remove it including all of its history)"), | |
1111 | displaypath); | |
1112 | ||
1113 | if (!(flags & OPT_FORCE)) { | |
1114 | struct child_process cp_rm = CHILD_PROCESS_INIT; | |
1115 | cp_rm.git_cmd = 1; | |
1116 | argv_array_pushl(&cp_rm.args, "rm", "-qn", | |
1117 | path, NULL); | |
1118 | ||
1119 | if (run_command(&cp_rm)) | |
1120 | die(_("Submodule work tree '%s' contains local " | |
1121 | "modifications; use '-f' to discard them"), | |
1122 | displaypath); | |
1123 | } | |
1124 | ||
1125 | strbuf_addstr(&sb_rm, path); | |
1126 | ||
1127 | if (!remove_dir_recursively(&sb_rm, 0)) | |
1128 | format = _("Cleared directory '%s'\n"); | |
1129 | else | |
1130 | format = _("Could not remove submodule work tree '%s'\n"); | |
1131 | ||
1132 | if (!(flags & OPT_QUIET)) | |
1133 | printf(format, displaypath); | |
1134 | ||
1135 | strbuf_release(&sb_rm); | |
1136 | } | |
1137 | ||
1138 | if (mkdir(path, 0777)) | |
1139 | printf(_("could not create empty submodule directory %s"), | |
1140 | displaypath); | |
1141 | ||
1142 | cp_config.git_cmd = 1; | |
1143 | argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL); | |
1144 | argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name); | |
1145 | ||
1146 | /* remove the .git/config entries (unless the user already did it) */ | |
1147 | if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) { | |
1148 | char *sub_key = xstrfmt("submodule.%s", sub->name); | |
1149 | /* | |
1150 | * remove the whole section so we have a clean state when | |
1151 | * the user later decides to init this submodule again | |
1152 | */ | |
1153 | git_config_rename_section_in_file(NULL, sub_key, NULL); | |
1154 | if (!(flags & OPT_QUIET)) | |
1155 | printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"), | |
1156 | sub->name, sub->url, displaypath); | |
1157 | free(sub_key); | |
1158 | } | |
1159 | ||
1160 | cleanup: | |
1161 | free(displaypath); | |
1162 | free(sub_git_dir); | |
1163 | strbuf_release(&sb_config); | |
1164 | } | |
1165 | ||
1166 | static void deinit_submodule_cb(const struct cache_entry *list_item, | |
1167 | void *cb_data) | |
1168 | { | |
1169 | struct deinit_cb *info = cb_data; | |
1170 | deinit_submodule(list_item->name, info->prefix, info->flags); | |
1171 | } | |
1172 | ||
1173 | static int module_deinit(int argc, const char **argv, const char *prefix) | |
1174 | { | |
1175 | struct deinit_cb info = DEINIT_CB_INIT; | |
1176 | struct pathspec pathspec; | |
1177 | struct module_list list = MODULE_LIST_INIT; | |
1178 | int quiet = 0; | |
1179 | int force = 0; | |
1180 | int all = 0; | |
1181 | ||
1182 | struct option module_deinit_options[] = { | |
1183 | OPT__QUIET(&quiet, N_("Suppress submodule status output")), | |
7fb6aefd | 1184 | OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0), |
2e612731 PC |
1185 | OPT_BOOL(0, "all", &all, N_("Unregister all submodules")), |
1186 | OPT_END() | |
1187 | }; | |
1188 | ||
1189 | const char *const git_submodule_helper_usage[] = { | |
1190 | N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"), | |
1191 | NULL | |
1192 | }; | |
1193 | ||
1194 | argc = parse_options(argc, argv, prefix, module_deinit_options, | |
1195 | git_submodule_helper_usage, 0); | |
1196 | ||
1197 | if (all && argc) { | |
1198 | error("pathspec and --all are incompatible"); | |
1199 | usage_with_options(git_submodule_helper_usage, | |
1200 | module_deinit_options); | |
1201 | } | |
1202 | ||
1203 | if (!argc && !all) | |
1204 | die(_("Use '--all' if you really want to deinitialize all submodules")); | |
1205 | ||
1206 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
9748e39d | 1207 | return 1; |
2e612731 PC |
1208 | |
1209 | info.prefix = prefix; | |
1210 | if (quiet) | |
1211 | info.flags |= OPT_QUIET; | |
1212 | if (force) | |
1213 | info.flags |= OPT_FORCE; | |
1214 | ||
1215 | for_each_listed_submodule(&list, deinit_submodule_cb, &info); | |
1216 | ||
1217 | return 0; | |
1218 | } | |
1219 | ||
ee8838d1 | 1220 | static int clone_submodule(const char *path, const char *gitdir, const char *url, |
a0ef2934 | 1221 | const char *depth, struct string_list *reference, int dissociate, |
72c5f883 | 1222 | int quiet, int progress) |
ee8838d1 | 1223 | { |
542aa25d | 1224 | struct child_process cp = CHILD_PROCESS_INIT; |
ee8838d1 SB |
1225 | |
1226 | argv_array_push(&cp.args, "clone"); | |
1227 | argv_array_push(&cp.args, "--no-checkout"); | |
1228 | if (quiet) | |
1229 | argv_array_push(&cp.args, "--quiet"); | |
72c5f883 JK |
1230 | if (progress) |
1231 | argv_array_push(&cp.args, "--progress"); | |
ee8838d1 SB |
1232 | if (depth && *depth) |
1233 | argv_array_pushl(&cp.args, "--depth", depth, NULL); | |
965dbea0 SB |
1234 | if (reference->nr) { |
1235 | struct string_list_item *item; | |
1236 | for_each_string_list_item(item, reference) | |
1237 | argv_array_pushl(&cp.args, "--reference", | |
1238 | item->string, NULL); | |
1239 | } | |
a0ef2934 CF |
1240 | if (dissociate) |
1241 | argv_array_push(&cp.args, "--dissociate"); | |
ee8838d1 SB |
1242 | if (gitdir && *gitdir) |
1243 | argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL); | |
1244 | ||
98afac7a | 1245 | argv_array_push(&cp.args, "--"); |
ee8838d1 SB |
1246 | argv_array_push(&cp.args, url); |
1247 | argv_array_push(&cp.args, path); | |
1248 | ||
1249 | cp.git_cmd = 1; | |
14111fc4 | 1250 | prepare_submodule_repo_env(&cp.env_array); |
ee8838d1 SB |
1251 | cp.no_stdin = 1; |
1252 | ||
1253 | return run_command(&cp); | |
1254 | } | |
1255 | ||
31224cbd SB |
1256 | struct submodule_alternate_setup { |
1257 | const char *submodule_name; | |
1258 | enum SUBMODULE_ALTERNATE_ERROR_MODE { | |
1259 | SUBMODULE_ALTERNATE_ERROR_DIE, | |
1260 | SUBMODULE_ALTERNATE_ERROR_INFO, | |
1261 | SUBMODULE_ALTERNATE_ERROR_IGNORE | |
1262 | } error_mode; | |
1263 | struct string_list *reference; | |
1264 | }; | |
1265 | #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \ | |
1266 | SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL } | |
1267 | ||
1268 | static int add_possible_reference_from_superproject( | |
263db403 | 1269 | struct object_directory *odb, void *sas_cb) |
31224cbd SB |
1270 | { |
1271 | struct submodule_alternate_setup *sas = sas_cb; | |
b2ac148f | 1272 | size_t len; |
31224cbd | 1273 | |
31224cbd SB |
1274 | /* |
1275 | * If the alternate object store is another repository, try the | |
bf03b790 | 1276 | * standard layout with .git/(modules/<name>)+/objects |
31224cbd | 1277 | */ |
263db403 | 1278 | if (strip_suffix(odb->path, "/objects", &len)) { |
31224cbd SB |
1279 | char *sm_alternate; |
1280 | struct strbuf sb = STRBUF_INIT; | |
1281 | struct strbuf err = STRBUF_INIT; | |
263db403 | 1282 | strbuf_add(&sb, odb->path, len); |
597f9134 | 1283 | |
31224cbd SB |
1284 | /* |
1285 | * We need to end the new path with '/' to mark it as a dir, | |
1286 | * otherwise a submodule name containing '/' will be broken | |
1287 | * as the last part of a missing submodule reference would | |
1288 | * be taken as a file name. | |
1289 | */ | |
b2ac148f | 1290 | strbuf_addf(&sb, "/modules/%s/", sas->submodule_name); |
31224cbd SB |
1291 | |
1292 | sm_alternate = compute_alternate_path(sb.buf, &err); | |
1293 | if (sm_alternate) { | |
1294 | string_list_append(sas->reference, xstrdup(sb.buf)); | |
1295 | free(sm_alternate); | |
1296 | } else { | |
1297 | switch (sas->error_mode) { | |
1298 | case SUBMODULE_ALTERNATE_ERROR_DIE: | |
1299 | die(_("submodule '%s' cannot add alternate: %s"), | |
1300 | sas->submodule_name, err.buf); | |
1301 | case SUBMODULE_ALTERNATE_ERROR_INFO: | |
1302 | fprintf(stderr, _("submodule '%s' cannot add alternate: %s"), | |
1303 | sas->submodule_name, err.buf); | |
1304 | case SUBMODULE_ALTERNATE_ERROR_IGNORE: | |
1305 | ; /* nothing */ | |
1306 | } | |
1307 | } | |
1308 | strbuf_release(&sb); | |
1309 | } | |
1310 | ||
31224cbd SB |
1311 | return 0; |
1312 | } | |
1313 | ||
1314 | static void prepare_possible_alternates(const char *sm_name, | |
1315 | struct string_list *reference) | |
1316 | { | |
1317 | char *sm_alternate = NULL, *error_strategy = NULL; | |
1318 | struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT; | |
1319 | ||
1320 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
1321 | if (!sm_alternate) | |
1322 | return; | |
1323 | ||
1324 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
1325 | ||
1326 | if (!error_strategy) | |
1327 | error_strategy = xstrdup("die"); | |
1328 | ||
1329 | sas.submodule_name = sm_name; | |
1330 | sas.reference = reference; | |
1331 | if (!strcmp(error_strategy, "die")) | |
1332 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE; | |
1333 | else if (!strcmp(error_strategy, "info")) | |
1334 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO; | |
1335 | else if (!strcmp(error_strategy, "ignore")) | |
1336 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE; | |
1337 | else | |
1338 | die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy); | |
1339 | ||
1340 | if (!strcmp(sm_alternate, "superproject")) | |
1341 | foreach_alt_odb(add_possible_reference_from_superproject, &sas); | |
1342 | else if (!strcmp(sm_alternate, "no")) | |
1343 | ; /* do nothing */ | |
1344 | else | |
1345 | die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate); | |
1346 | ||
1347 | free(sm_alternate); | |
1348 | free(error_strategy); | |
1349 | } | |
1350 | ||
ee8838d1 SB |
1351 | static int module_clone(int argc, const char **argv, const char *prefix) |
1352 | { | |
965dbea0 | 1353 | const char *name = NULL, *url = NULL, *depth = NULL; |
ee8838d1 | 1354 | int quiet = 0; |
72c5f883 | 1355 | int progress = 0; |
1ea4d9b7 | 1356 | char *p, *path = NULL, *sm_gitdir; |
ee8838d1 | 1357 | struct strbuf sb = STRBUF_INIT; |
965dbea0 | 1358 | struct string_list reference = STRING_LIST_INIT_NODUP; |
a0ef2934 | 1359 | int dissociate = 0; |
bf03b790 | 1360 | char *sm_alternate = NULL, *error_strategy = NULL; |
ee8838d1 SB |
1361 | |
1362 | struct option module_clone_options[] = { | |
1363 | OPT_STRING(0, "prefix", &prefix, | |
1364 | N_("path"), | |
1365 | N_("alternative anchor for relative paths")), | |
1366 | OPT_STRING(0, "path", &path, | |
1367 | N_("path"), | |
1368 | N_("where the new submodule will be cloned to")), | |
1369 | OPT_STRING(0, "name", &name, | |
1370 | N_("string"), | |
1371 | N_("name of the new submodule")), | |
1372 | OPT_STRING(0, "url", &url, | |
1373 | N_("string"), | |
1374 | N_("url where to clone the submodule from")), | |
965dbea0 SB |
1375 | OPT_STRING_LIST(0, "reference", &reference, |
1376 | N_("repo"), | |
ee8838d1 | 1377 | N_("reference repository")), |
a0ef2934 CF |
1378 | OPT_BOOL(0, "dissociate", &dissociate, |
1379 | N_("use --reference only while cloning")), | |
ee8838d1 SB |
1380 | OPT_STRING(0, "depth", &depth, |
1381 | N_("string"), | |
1382 | N_("depth for shallow clones")), | |
1383 | OPT__QUIET(&quiet, "Suppress output for cloning a submodule"), | |
72c5f883 JK |
1384 | OPT_BOOL(0, "progress", &progress, |
1385 | N_("force cloning progress")), | |
ee8838d1 SB |
1386 | OPT_END() |
1387 | }; | |
1388 | ||
1389 | const char *const git_submodule_helper_usage[] = { | |
1390 | N_("git submodule--helper clone [--prefix=<path>] [--quiet] " | |
7dad2633 JK |
1391 | "[--reference <repository>] [--name <name>] [--depth <depth>] " |
1392 | "--url <url> --path <path>"), | |
ee8838d1 SB |
1393 | NULL |
1394 | }; | |
1395 | ||
1396 | argc = parse_options(argc, argv, prefix, module_clone_options, | |
1397 | git_submodule_helper_usage, 0); | |
1398 | ||
deef3cdc | 1399 | if (argc || !url || !path || !*path) |
08e0970a JK |
1400 | usage_with_options(git_submodule_helper_usage, |
1401 | module_clone_options); | |
1402 | ||
ee8838d1 | 1403 | strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name); |
0aaad415 | 1404 | sm_gitdir = absolute_pathdup(sb.buf); |
1ea4d9b7 | 1405 | strbuf_reset(&sb); |
f8eaa0ba SB |
1406 | |
1407 | if (!is_absolute_path(path)) { | |
1408 | strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path); | |
1409 | path = strbuf_detach(&sb, NULL); | |
1410 | } else | |
1411 | path = xstrdup(path); | |
ee8838d1 SB |
1412 | |
1413 | if (!file_exists(sm_gitdir)) { | |
1414 | if (safe_create_leading_directories_const(sm_gitdir) < 0) | |
1415 | die(_("could not create directory '%s'"), sm_gitdir); | |
31224cbd SB |
1416 | |
1417 | prepare_possible_alternates(name, &reference); | |
1418 | ||
a0ef2934 | 1419 | if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate, |
72c5f883 | 1420 | quiet, progress)) |
ee8838d1 SB |
1421 | die(_("clone of '%s' into submodule path '%s' failed"), |
1422 | url, path); | |
1423 | } else { | |
1424 | if (safe_create_leading_directories_const(path) < 0) | |
1425 | die(_("could not create directory '%s'"), path); | |
1426 | strbuf_addf(&sb, "%s/index", sm_gitdir); | |
1427 | unlink_or_warn(sb.buf); | |
1428 | strbuf_reset(&sb); | |
1429 | } | |
1430 | ||
da62f786 | 1431 | connect_work_tree_and_git_dir(path, sm_gitdir, 0); |
ee8838d1 | 1432 | |
ee8838d1 SB |
1433 | p = git_pathdup_submodule(path, "config"); |
1434 | if (!p) | |
1435 | die(_("could not get submodule directory for '%s'"), path); | |
bf03b790 VS |
1436 | |
1437 | /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ | |
1438 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
1439 | if (sm_alternate) | |
1440 | git_config_set_in_file(p, "submodule.alternateLocation", | |
1441 | sm_alternate); | |
1442 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
1443 | if (error_strategy) | |
1444 | git_config_set_in_file(p, "submodule.alternateErrorStrategy", | |
1445 | error_strategy); | |
1446 | ||
1447 | free(sm_alternate); | |
1448 | free(error_strategy); | |
1449 | ||
ee8838d1 | 1450 | strbuf_release(&sb); |
ee8838d1 | 1451 | free(sm_gitdir); |
f8eaa0ba | 1452 | free(path); |
ee8838d1 SB |
1453 | free(p); |
1454 | return 0; | |
1455 | } | |
74703a1e | 1456 | |
ee69b2a9 SB |
1457 | static void determine_submodule_update_strategy(struct repository *r, |
1458 | int just_cloned, | |
1459 | const char *path, | |
1460 | const char *update, | |
1461 | struct submodule_update_strategy *out) | |
1462 | { | |
1463 | const struct submodule *sub = submodule_from_path(r, &null_oid, path); | |
1464 | char *key; | |
1465 | const char *val; | |
1466 | ||
1467 | key = xstrfmt("submodule.%s.update", sub->name); | |
1468 | ||
1469 | if (update) { | |
ee69b2a9 SB |
1470 | if (parse_submodule_update_strategy(update, out) < 0) |
1471 | die(_("Invalid update mode '%s' for submodule path '%s'"), | |
1472 | update, path); | |
1473 | } else if (!repo_config_get_string_const(r, key, &val)) { | |
1474 | if (parse_submodule_update_strategy(val, out) < 0) | |
1475 | die(_("Invalid update mode '%s' configured for submodule path '%s'"), | |
1476 | val, path); | |
1477 | } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
ee69b2a9 SB |
1478 | out->type = sub->update_strategy.type; |
1479 | out->command = sub->update_strategy.command; | |
1480 | } else | |
1481 | out->type = SM_UPDATE_CHECKOUT; | |
1482 | ||
1483 | if (just_cloned && | |
1484 | (out->type == SM_UPDATE_MERGE || | |
1485 | out->type == SM_UPDATE_REBASE || | |
1486 | out->type == SM_UPDATE_NONE)) | |
1487 | out->type = SM_UPDATE_CHECKOUT; | |
1488 | ||
1489 | free(key); | |
1490 | } | |
1491 | ||
1492 | static int module_update_module_mode(int argc, const char **argv, const char *prefix) | |
1493 | { | |
1494 | const char *path, *update = NULL; | |
1495 | int just_cloned; | |
1496 | struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT }; | |
1497 | ||
1498 | if (argc < 3 || argc > 4) | |
1499 | die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]"); | |
1500 | ||
1501 | just_cloned = git_config_int("just_cloned", argv[1]); | |
1502 | path = argv[2]; | |
1503 | ||
1504 | if (argc == 4) | |
1505 | update = argv[3]; | |
1506 | ||
1507 | determine_submodule_update_strategy(the_repository, | |
1508 | just_cloned, path, update, | |
1509 | &update_strategy); | |
1510 | fputs(submodule_strategy_to_string(&update_strategy), stdout); | |
1511 | ||
1512 | return 0; | |
1513 | } | |
1514 | ||
f1d15713 SB |
1515 | struct update_clone_data { |
1516 | const struct submodule *sub; | |
1517 | struct object_id oid; | |
1518 | unsigned just_cloned; | |
1519 | }; | |
1520 | ||
48308681 SB |
1521 | struct submodule_update_clone { |
1522 | /* index into 'list', the list of submodules to look into for cloning */ | |
1523 | int current; | |
1524 | struct module_list list; | |
1525 | unsigned warn_if_uninitialized : 1; | |
1526 | ||
1527 | /* update parameter passed via commandline */ | |
1528 | struct submodule_update_strategy update; | |
1529 | ||
1530 | /* configuration parameters which are passed on to the children */ | |
72c5f883 | 1531 | int progress; |
48308681 | 1532 | int quiet; |
abed000a | 1533 | int recommend_shallow; |
5f50f33e | 1534 | struct string_list references; |
a0ef2934 | 1535 | int dissociate; |
48308681 SB |
1536 | const char *depth; |
1537 | const char *recursive_prefix; | |
1538 | const char *prefix; | |
1539 | ||
f1d15713 SB |
1540 | /* to be consumed by git-submodule.sh */ |
1541 | struct update_clone_data *update_clone; | |
1542 | int update_clone_nr; int update_clone_alloc; | |
48308681 SB |
1543 | |
1544 | /* If we want to stop as fast as possible and return an error */ | |
1545 | unsigned quickstop : 1; | |
665b35ec SB |
1546 | |
1547 | /* failed clones to be retried again */ | |
1548 | const struct cache_entry **failed_clones; | |
1549 | int failed_clones_nr, failed_clones_alloc; | |
90efe595 SB |
1550 | |
1551 | int max_jobs; | |
48308681 SB |
1552 | }; |
1553 | #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \ | |
a0ef2934 | 1554 | SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \ |
5f50f33e | 1555 | NULL, NULL, NULL, \ |
f1d15713 | 1556 | NULL, 0, 0, 0, NULL, 0, 0, 0} |
48308681 | 1557 | |
08fdbdb1 SB |
1558 | |
1559 | static void next_submodule_warn_missing(struct submodule_update_clone *suc, | |
1560 | struct strbuf *out, const char *displaypath) | |
1561 | { | |
1562 | /* | |
1563 | * Only mention uninitialized submodules when their | |
1564 | * paths have been specified. | |
1565 | */ | |
1566 | if (suc->warn_if_uninitialized) { | |
1567 | strbuf_addf(out, | |
1568 | _("Submodule path '%s' not initialized"), | |
1569 | displaypath); | |
1570 | strbuf_addch(out, '\n'); | |
1571 | strbuf_addstr(out, | |
1572 | _("Maybe you want to use 'update --init'?")); | |
1573 | strbuf_addch(out, '\n'); | |
1574 | } | |
1575 | } | |
1576 | ||
48308681 SB |
1577 | /** |
1578 | * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to | |
1579 | * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise. | |
1580 | */ | |
1581 | static int prepare_to_clone_next_submodule(const struct cache_entry *ce, | |
1582 | struct child_process *child, | |
1583 | struct submodule_update_clone *suc, | |
1584 | struct strbuf *out) | |
1585 | { | |
1586 | const struct submodule *sub = NULL; | |
ec6141a0 BW |
1587 | const char *url = NULL; |
1588 | const char *update_string; | |
1589 | enum submodule_update_type update_type; | |
1590 | char *key; | |
48308681 SB |
1591 | struct strbuf displaypath_sb = STRBUF_INIT; |
1592 | struct strbuf sb = STRBUF_INIT; | |
1593 | const char *displaypath = NULL; | |
48308681 | 1594 | int needs_cloning = 0; |
e0a862fd | 1595 | int need_free_url = 0; |
48308681 SB |
1596 | |
1597 | if (ce_stage(ce)) { | |
1598 | if (suc->recursive_prefix) | |
1599 | strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name); | |
1600 | else | |
92d52fab | 1601 | strbuf_addstr(&sb, ce->name); |
48308681 SB |
1602 | strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf); |
1603 | strbuf_addch(out, '\n'); | |
1604 | goto cleanup; | |
1605 | } | |
1606 | ||
3b8fb393 | 1607 | sub = submodule_from_path(the_repository, &null_oid, ce->name); |
48308681 SB |
1608 | |
1609 | if (suc->recursive_prefix) | |
1610 | displaypath = relative_path(suc->recursive_prefix, | |
1611 | ce->name, &displaypath_sb); | |
1612 | else | |
1613 | displaypath = ce->name; | |
1614 | ||
08fdbdb1 SB |
1615 | if (!sub) { |
1616 | next_submodule_warn_missing(suc, out, displaypath); | |
1617 | goto cleanup; | |
1618 | } | |
1619 | ||
ec6141a0 BW |
1620 | key = xstrfmt("submodule.%s.update", sub->name); |
1621 | if (!repo_config_get_string_const(the_repository, key, &update_string)) { | |
1622 | update_type = parse_submodule_update_type(update_string); | |
1623 | } else { | |
1624 | update_type = sub->update_strategy.type; | |
1625 | } | |
1626 | free(key); | |
1627 | ||
48308681 SB |
1628 | if (suc->update.type == SM_UPDATE_NONE |
1629 | || (suc->update.type == SM_UPDATE_UNSPECIFIED | |
ec6141a0 | 1630 | && update_type == SM_UPDATE_NONE)) { |
48308681 SB |
1631 | strbuf_addf(out, _("Skipping submodule '%s'"), displaypath); |
1632 | strbuf_addch(out, '\n'); | |
1633 | goto cleanup; | |
1634 | } | |
1635 | ||
ee92ab99 | 1636 | /* Check if the submodule has been initialized. */ |
627d9342 | 1637 | if (!is_submodule_active(the_repository, ce->name)) { |
08fdbdb1 | 1638 | next_submodule_warn_missing(suc, out, displaypath); |
48308681 SB |
1639 | goto cleanup; |
1640 | } | |
1641 | ||
ec6141a0 BW |
1642 | strbuf_reset(&sb); |
1643 | strbuf_addf(&sb, "submodule.%s.url", sub->name); | |
e0a862fd SB |
1644 | if (repo_config_get_string_const(the_repository, sb.buf, &url)) { |
1645 | if (starts_with_dot_slash(sub->url) || | |
1646 | starts_with_dot_dot_slash(sub->url)) { | |
1647 | url = compute_submodule_clone_url(sub->url); | |
1648 | need_free_url = 1; | |
1649 | } else | |
1650 | url = sub->url; | |
1651 | } | |
ec6141a0 | 1652 | |
48308681 SB |
1653 | strbuf_reset(&sb); |
1654 | strbuf_addf(&sb, "%s/.git", ce->name); | |
1655 | needs_cloning = !file_exists(sb.buf); | |
1656 | ||
f1d15713 SB |
1657 | ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1, |
1658 | suc->update_clone_alloc); | |
1659 | oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid); | |
1660 | suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning; | |
1661 | suc->update_clone[suc->update_clone_nr].sub = sub; | |
1662 | suc->update_clone_nr++; | |
48308681 SB |
1663 | |
1664 | if (!needs_cloning) | |
1665 | goto cleanup; | |
1666 | ||
1667 | child->git_cmd = 1; | |
1668 | child->no_stdin = 1; | |
1669 | child->stdout_to_stderr = 1; | |
1670 | child->err = -1; | |
1671 | argv_array_push(&child->args, "submodule--helper"); | |
1672 | argv_array_push(&child->args, "clone"); | |
72c5f883 JK |
1673 | if (suc->progress) |
1674 | argv_array_push(&child->args, "--progress"); | |
48308681 SB |
1675 | if (suc->quiet) |
1676 | argv_array_push(&child->args, "--quiet"); | |
1677 | if (suc->prefix) | |
1678 | argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL); | |
abed000a SB |
1679 | if (suc->recommend_shallow && sub->recommend_shallow == 1) |
1680 | argv_array_push(&child->args, "--depth=1"); | |
48308681 SB |
1681 | argv_array_pushl(&child->args, "--path", sub->path, NULL); |
1682 | argv_array_pushl(&child->args, "--name", sub->name, NULL); | |
ec6141a0 | 1683 | argv_array_pushl(&child->args, "--url", url, NULL); |
5f50f33e SB |
1684 | if (suc->references.nr) { |
1685 | struct string_list_item *item; | |
1686 | for_each_string_list_item(item, &suc->references) | |
1687 | argv_array_pushl(&child->args, "--reference", item->string, NULL); | |
1688 | } | |
a0ef2934 CF |
1689 | if (suc->dissociate) |
1690 | argv_array_push(&child->args, "--dissociate"); | |
48308681 SB |
1691 | if (suc->depth) |
1692 | argv_array_push(&child->args, suc->depth); | |
1693 | ||
1694 | cleanup: | |
48308681 SB |
1695 | strbuf_reset(&displaypath_sb); |
1696 | strbuf_reset(&sb); | |
e0a862fd SB |
1697 | if (need_free_url) |
1698 | free((void*)url); | |
48308681 SB |
1699 | |
1700 | return needs_cloning; | |
1701 | } | |
1702 | ||
1703 | static int update_clone_get_next_task(struct child_process *child, | |
1704 | struct strbuf *err, | |
1705 | void *suc_cb, | |
665b35ec | 1706 | void **idx_task_cb) |
48308681 SB |
1707 | { |
1708 | struct submodule_update_clone *suc = suc_cb; | |
665b35ec SB |
1709 | const struct cache_entry *ce; |
1710 | int index; | |
48308681 SB |
1711 | |
1712 | for (; suc->current < suc->list.nr; suc->current++) { | |
665b35ec | 1713 | ce = suc->list.entries[suc->current]; |
48308681 | 1714 | if (prepare_to_clone_next_submodule(ce, child, suc, err)) { |
665b35ec SB |
1715 | int *p = xmalloc(sizeof(*p)); |
1716 | *p = suc->current; | |
1717 | *idx_task_cb = p; | |
48308681 SB |
1718 | suc->current++; |
1719 | return 1; | |
1720 | } | |
1721 | } | |
665b35ec SB |
1722 | |
1723 | /* | |
1724 | * The loop above tried cloning each submodule once, now try the | |
1725 | * stragglers again, which we can imagine as an extension of the | |
1726 | * entry list. | |
1727 | */ | |
1728 | index = suc->current - suc->list.nr; | |
1729 | if (index < suc->failed_clones_nr) { | |
1730 | int *p; | |
1731 | ce = suc->failed_clones[index]; | |
2201ee09 SB |
1732 | if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { |
1733 | suc->current ++; | |
a22ae753 RS |
1734 | strbuf_addstr(err, "BUG: submodule considered for " |
1735 | "cloning, doesn't need cloning " | |
1736 | "any more?\n"); | |
2201ee09 SB |
1737 | return 0; |
1738 | } | |
665b35ec SB |
1739 | p = xmalloc(sizeof(*p)); |
1740 | *p = suc->current; | |
1741 | *idx_task_cb = p; | |
1742 | suc->current ++; | |
1743 | return 1; | |
1744 | } | |
1745 | ||
48308681 SB |
1746 | return 0; |
1747 | } | |
1748 | ||
1749 | static int update_clone_start_failure(struct strbuf *err, | |
1750 | void *suc_cb, | |
665b35ec | 1751 | void *idx_task_cb) |
48308681 SB |
1752 | { |
1753 | struct submodule_update_clone *suc = suc_cb; | |
1754 | suc->quickstop = 1; | |
1755 | return 1; | |
1756 | } | |
1757 | ||
1758 | static int update_clone_task_finished(int result, | |
1759 | struct strbuf *err, | |
1760 | void *suc_cb, | |
665b35ec | 1761 | void *idx_task_cb) |
48308681 | 1762 | { |
665b35ec | 1763 | const struct cache_entry *ce; |
48308681 SB |
1764 | struct submodule_update_clone *suc = suc_cb; |
1765 | ||
c1e860f1 | 1766 | int *idxP = idx_task_cb; |
665b35ec SB |
1767 | int idx = *idxP; |
1768 | free(idxP); | |
1769 | ||
48308681 SB |
1770 | if (!result) |
1771 | return 0; | |
1772 | ||
665b35ec SB |
1773 | if (idx < suc->list.nr) { |
1774 | ce = suc->list.entries[idx]; | |
1775 | strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"), | |
1776 | ce->name); | |
1777 | strbuf_addch(err, '\n'); | |
1778 | ALLOC_GROW(suc->failed_clones, | |
1779 | suc->failed_clones_nr + 1, | |
1780 | suc->failed_clones_alloc); | |
1781 | suc->failed_clones[suc->failed_clones_nr++] = ce; | |
1782 | return 0; | |
1783 | } else { | |
eb09121b | 1784 | idx -= suc->list.nr; |
665b35ec SB |
1785 | ce = suc->failed_clones[idx]; |
1786 | strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"), | |
1787 | ce->name); | |
1788 | strbuf_addch(err, '\n'); | |
1789 | suc->quickstop = 1; | |
1790 | return 1; | |
1791 | } | |
1792 | ||
1793 | return 0; | |
48308681 SB |
1794 | } |
1795 | ||
05744997 AO |
1796 | static int git_update_clone_config(const char *var, const char *value, |
1797 | void *cb) | |
f20e7c1e BW |
1798 | { |
1799 | int *max_jobs = cb; | |
1800 | if (!strcmp(var, "submodule.fetchjobs")) | |
1801 | *max_jobs = parse_submodule_fetchjobs(var, value); | |
1802 | return 0; | |
1803 | } | |
1804 | ||
c94d9dc2 SB |
1805 | static void update_submodule(struct update_clone_data *ucd) |
1806 | { | |
1807 | fprintf(stdout, "dummy %s %d\t%s\n", | |
1808 | oid_to_hex(&ucd->oid), | |
1809 | ucd->just_cloned, | |
1810 | ucd->sub->path); | |
1811 | } | |
1812 | ||
90efe595 SB |
1813 | static int update_submodules(struct submodule_update_clone *suc) |
1814 | { | |
f1d15713 | 1815 | int i; |
90efe595 SB |
1816 | |
1817 | run_processes_parallel(suc->max_jobs, | |
1818 | update_clone_get_next_task, | |
1819 | update_clone_start_failure, | |
1820 | update_clone_task_finished, | |
1821 | suc); | |
1822 | ||
1823 | /* | |
1824 | * We saved the output and put it out all at once now. | |
1825 | * That means: | |
1826 | * - the listener does not have to interleave their (checkout) | |
1827 | * work with our fetching. The writes involved in a | |
1828 | * checkout involve more straightforward sequential I/O. | |
1829 | * - the listener can avoid doing any work if fetching failed. | |
1830 | */ | |
1831 | if (suc->quickstop) | |
1832 | return 1; | |
1833 | ||
c94d9dc2 SB |
1834 | for (i = 0; i < suc->update_clone_nr; i++) |
1835 | update_submodule(&suc->update_clone[i]); | |
90efe595 SB |
1836 | |
1837 | return 0; | |
1838 | } | |
1839 | ||
48308681 SB |
1840 | static int update_clone(int argc, const char **argv, const char *prefix) |
1841 | { | |
1842 | const char *update = NULL; | |
48308681 SB |
1843 | struct pathspec pathspec; |
1844 | struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT; | |
1845 | ||
1846 | struct option module_update_clone_options[] = { | |
1847 | OPT_STRING(0, "prefix", &prefix, | |
1848 | N_("path"), | |
1849 | N_("path into the working tree")), | |
1850 | OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix, | |
1851 | N_("path"), | |
1852 | N_("path into the working tree, across nested " | |
1853 | "submodule boundaries")), | |
1854 | OPT_STRING(0, "update", &update, | |
1855 | N_("string"), | |
1856 | N_("rebase, merge, checkout or none")), | |
5f50f33e | 1857 | OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"), |
48308681 | 1858 | N_("reference repository")), |
a0ef2934 CF |
1859 | OPT_BOOL(0, "dissociate", &suc.dissociate, |
1860 | N_("use --reference only while cloning")), | |
48308681 SB |
1861 | OPT_STRING(0, "depth", &suc.depth, "<depth>", |
1862 | N_("Create a shallow clone truncated to the " | |
1863 | "specified number of revisions")), | |
90efe595 | 1864 | OPT_INTEGER('j', "jobs", &suc.max_jobs, |
2335b870 | 1865 | N_("parallel jobs")), |
abed000a SB |
1866 | OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow, |
1867 | N_("whether the initial clone should follow the shallow recommendation")), | |
48308681 | 1868 | OPT__QUIET(&suc.quiet, N_("don't print cloning progress")), |
72c5f883 JK |
1869 | OPT_BOOL(0, "progress", &suc.progress, |
1870 | N_("force cloning progress")), | |
48308681 SB |
1871 | OPT_END() |
1872 | }; | |
1873 | ||
1874 | const char *const git_submodule_helper_usage[] = { | |
1875 | N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"), | |
1876 | NULL | |
1877 | }; | |
1878 | suc.prefix = prefix; | |
1879 | ||
90efe595 SB |
1880 | update_clone_config_from_gitmodules(&suc.max_jobs); |
1881 | git_config(git_update_clone_config, &suc.max_jobs); | |
f20e7c1e | 1882 | |
48308681 SB |
1883 | argc = parse_options(argc, argv, prefix, module_update_clone_options, |
1884 | git_submodule_helper_usage, 0); | |
1885 | ||
1886 | if (update) | |
1887 | if (parse_submodule_update_strategy(update, &suc.update) < 0) | |
1888 | die(_("bad value for update parameter")); | |
1889 | ||
1890 | if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0) | |
1891 | return 1; | |
1892 | ||
1893 | if (pathspec.nr) | |
1894 | suc.warn_if_uninitialized = 1; | |
1895 | ||
90efe595 | 1896 | return update_submodules(&suc); |
48308681 SB |
1897 | } |
1898 | ||
44431df0 SB |
1899 | static int resolve_relative_path(int argc, const char **argv, const char *prefix) |
1900 | { | |
1901 | struct strbuf sb = STRBUF_INIT; | |
1902 | if (argc != 3) | |
2de26ae1 | 1903 | die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc); |
44431df0 SB |
1904 | |
1905 | printf("%s", relative_path(argv[1], argv[2], &sb)); | |
1906 | strbuf_release(&sb); | |
1907 | return 0; | |
1908 | } | |
1909 | ||
92bbe7cc SB |
1910 | static const char *remote_submodule_branch(const char *path) |
1911 | { | |
1912 | const struct submodule *sub; | |
177257cc BW |
1913 | const char *branch = NULL; |
1914 | char *key; | |
92bbe7cc | 1915 | |
3b8fb393 | 1916 | sub = submodule_from_path(the_repository, &null_oid, path); |
92bbe7cc SB |
1917 | if (!sub) |
1918 | return NULL; | |
1919 | ||
177257cc BW |
1920 | key = xstrfmt("submodule.%s.branch", sub->name); |
1921 | if (repo_config_get_string_const(the_repository, key, &branch)) | |
1922 | branch = sub->branch; | |
1923 | free(key); | |
1924 | ||
1925 | if (!branch) | |
92bbe7cc SB |
1926 | return "master"; |
1927 | ||
177257cc | 1928 | if (!strcmp(branch, ".")) { |
744c040b | 1929 | const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); |
4d7bc52b SB |
1930 | |
1931 | if (!refname) | |
1932 | die(_("No such ref: %s"), "HEAD"); | |
1933 | ||
1934 | /* detached HEAD */ | |
1935 | if (!strcmp(refname, "HEAD")) | |
1936 | die(_("Submodule (%s) branch configured to inherit " | |
1937 | "branch from superproject, but the superproject " | |
1938 | "is not on any branch"), sub->name); | |
1939 | ||
1940 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
1941 | die(_("Expecting a full ref name, got %s"), refname); | |
1942 | return refname; | |
1943 | } | |
1944 | ||
177257cc | 1945 | return branch; |
92bbe7cc SB |
1946 | } |
1947 | ||
1948 | static int resolve_remote_submodule_branch(int argc, const char **argv, | |
1949 | const char *prefix) | |
1950 | { | |
1951 | const char *ret; | |
1952 | struct strbuf sb = STRBUF_INIT; | |
1953 | if (argc != 2) | |
1954 | die("submodule--helper remote-branch takes exactly one arguments, got %d", argc); | |
1955 | ||
1956 | ret = remote_submodule_branch(argv[1]); | |
1957 | if (!ret) | |
1958 | die("submodule %s doesn't exist", argv[1]); | |
1959 | ||
1960 | printf("%s", ret); | |
1961 | strbuf_release(&sb); | |
1962 | return 0; | |
1963 | } | |
1964 | ||
93481a6b BW |
1965 | static int push_check(int argc, const char **argv, const char *prefix) |
1966 | { | |
1967 | struct remote *remote; | |
c7be7201 BW |
1968 | const char *superproject_head; |
1969 | char *head; | |
1970 | int detached_head = 0; | |
1971 | struct object_id head_oid; | |
93481a6b | 1972 | |
c7be7201 BW |
1973 | if (argc < 3) |
1974 | die("submodule--helper push-check requires at least 2 arguments"); | |
1975 | ||
1976 | /* | |
1977 | * superproject's resolved head ref. | |
1978 | * if HEAD then the superproject is in a detached head state, otherwise | |
1979 | * it will be the resolved head ref. | |
1980 | */ | |
1981 | superproject_head = argv[1]; | |
1982 | argv++; | |
1983 | argc--; | |
1984 | /* Get the submodule's head ref and determine if it is detached */ | |
0f2dc722 | 1985 | head = resolve_refdup("HEAD", 0, &head_oid, NULL); |
c7be7201 BW |
1986 | if (!head) |
1987 | die(_("Failed to resolve HEAD as a valid ref.")); | |
1988 | if (!strcmp(head, "HEAD")) | |
1989 | detached_head = 1; | |
93481a6b BW |
1990 | |
1991 | /* | |
1992 | * The remote must be configured. | |
1993 | * This is to avoid pushing to the exact same URL as the parent. | |
1994 | */ | |
1995 | remote = pushremote_get(argv[1]); | |
1996 | if (!remote || remote->origin == REMOTE_UNCONFIGURED) | |
1997 | die("remote '%s' not configured", argv[1]); | |
1998 | ||
1999 | /* Check the refspec */ | |
2000 | if (argc > 2) { | |
9c8361b2 | 2001 | int i; |
93481a6b | 2002 | struct ref *local_refs = get_local_heads(); |
9c8361b2 | 2003 | struct refspec refspec = REFSPEC_INIT_PUSH; |
93481a6b | 2004 | |
9c8361b2 BW |
2005 | refspec_appendn(&refspec, argv + 2, argc - 2); |
2006 | ||
2007 | for (i = 0; i < refspec.nr; i++) { | |
2008 | const struct refspec_item *rs = &refspec.items[i]; | |
93481a6b BW |
2009 | |
2010 | if (rs->pattern || rs->matching) | |
2011 | continue; | |
2012 | ||
c7be7201 BW |
2013 | /* LHS must match a single ref */ |
2014 | switch (count_refspec_match(rs->src, local_refs, NULL)) { | |
2015 | case 1: | |
2016 | break; | |
2017 | case 0: | |
2018 | /* | |
2019 | * If LHS matches 'HEAD' then we need to ensure | |
2020 | * that it matches the same named branch | |
2021 | * checked out in the superproject. | |
2022 | */ | |
2023 | if (!strcmp(rs->src, "HEAD")) { | |
2024 | if (!detached_head && | |
2025 | !strcmp(head, superproject_head)) | |
2026 | break; | |
2027 | die("HEAD does not match the named branch in the superproject"); | |
2028 | } | |
1cf01a34 | 2029 | /* fallthrough */ |
c7be7201 | 2030 | default: |
93481a6b BW |
2031 | die("src refspec '%s' must name a ref", |
2032 | rs->src); | |
c7be7201 | 2033 | } |
93481a6b | 2034 | } |
9c8361b2 | 2035 | refspec_clear(&refspec); |
93481a6b | 2036 | } |
c7be7201 | 2037 | free(head); |
93481a6b BW |
2038 | |
2039 | return 0; | |
2040 | } | |
2041 | ||
74d4731d SB |
2042 | static int ensure_core_worktree(int argc, const char **argv, const char *prefix) |
2043 | { | |
2044 | const struct submodule *sub; | |
2045 | const char *path; | |
2046 | char *cw; | |
2047 | struct repository subrepo; | |
2048 | ||
2049 | if (argc != 2) | |
2050 | BUG("submodule--helper connect-gitdir-workingtree <name> <path>"); | |
2051 | ||
2052 | path = argv[1]; | |
2053 | ||
2054 | sub = submodule_from_path(the_repository, &null_oid, path); | |
2055 | if (!sub) | |
2056 | BUG("We could get the submodule handle before?"); | |
2057 | ||
2058 | if (repo_submodule_init(&subrepo, the_repository, path)) | |
2059 | die(_("could not get a repository handle for submodule '%s'"), path); | |
2060 | ||
2061 | if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) { | |
2062 | char *cfg_file, *abs_path; | |
2063 | const char *rel_path; | |
2064 | struct strbuf sb = STRBUF_INIT; | |
2065 | ||
2066 | cfg_file = repo_git_path(&subrepo, "config"); | |
2067 | ||
2068 | abs_path = absolute_pathdup(path); | |
2069 | rel_path = relative_path(abs_path, subrepo.gitdir, &sb); | |
2070 | ||
2071 | git_config_set_in_file(cfg_file, "core.worktree", rel_path); | |
2072 | ||
2073 | free(cfg_file); | |
2074 | free(abs_path); | |
2075 | strbuf_release(&sb); | |
2076 | } | |
2077 | ||
2078 | return 0; | |
2079 | } | |
2080 | ||
f6f85861 SB |
2081 | static int absorb_git_dirs(int argc, const char **argv, const char *prefix) |
2082 | { | |
2083 | int i; | |
2084 | struct pathspec pathspec; | |
2085 | struct module_list list = MODULE_LIST_INIT; | |
2086 | unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES; | |
2087 | ||
2088 | struct option embed_gitdir_options[] = { | |
2089 | OPT_STRING(0, "prefix", &prefix, | |
2090 | N_("path"), | |
2091 | N_("path into the working tree")), | |
2092 | OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"), | |
2093 | ABSORB_GITDIR_RECURSE_SUBMODULES), | |
2094 | OPT_END() | |
2095 | }; | |
2096 | ||
2097 | const char *const git_submodule_helper_usage[] = { | |
2098 | N_("git submodule--helper embed-git-dir [<path>...]"), | |
2099 | NULL | |
2100 | }; | |
2101 | ||
2102 | argc = parse_options(argc, argv, prefix, embed_gitdir_options, | |
2103 | git_submodule_helper_usage, 0); | |
2104 | ||
f6f85861 SB |
2105 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
2106 | return 1; | |
2107 | ||
2108 | for (i = 0; i < list.nr; i++) | |
2109 | absorb_git_dir_into_superproject(prefix, | |
2110 | list.entries[i]->name, flags); | |
2111 | ||
2112 | return 0; | |
2113 | } | |
2114 | ||
5c2bd8b7 BW |
2115 | static int is_active(int argc, const char **argv, const char *prefix) |
2116 | { | |
2117 | if (argc != 2) | |
9af7ec30 | 2118 | die("submodule--helper is-active takes exactly 1 argument"); |
5c2bd8b7 | 2119 | |
627d9342 | 2120 | return !is_submodule_active(the_repository, argv[1]); |
5c2bd8b7 BW |
2121 | } |
2122 | ||
0383bbb9 JK |
2123 | /* |
2124 | * Exit non-zero if any of the submodule names given on the command line is | |
2125 | * invalid. If no names are given, filter stdin to print only valid names | |
2126 | * (which is primarily intended for testing). | |
2127 | */ | |
2128 | static int check_name(int argc, const char **argv, const char *prefix) | |
2129 | { | |
2130 | if (argc > 1) { | |
2131 | while (*++argv) { | |
2132 | if (check_submodule_name(*argv) < 0) | |
2133 | return 1; | |
2134 | } | |
2135 | } else { | |
2136 | struct strbuf buf = STRBUF_INIT; | |
2137 | while (strbuf_getline(&buf, stdin) != EOF) { | |
2138 | if (!check_submodule_name(buf.buf)) | |
2139 | printf("%s\n", buf.buf); | |
2140 | } | |
2141 | strbuf_release(&buf); | |
2142 | } | |
2143 | return 0; | |
2144 | } | |
2145 | ||
2502ffc0 AO |
2146 | static int module_config(int argc, const char **argv, const char *prefix) |
2147 | { | |
b5c259f2 AO |
2148 | enum { |
2149 | CHECK_WRITEABLE = 1 | |
2150 | } command = 0; | |
2151 | ||
2152 | struct option module_config_options[] = { | |
2153 | OPT_CMDMODE(0, "check-writeable", &command, | |
2154 | N_("check if it is safe to write to the .gitmodules file"), | |
2155 | CHECK_WRITEABLE), | |
2156 | OPT_END() | |
2157 | }; | |
2158 | const char *const git_submodule_helper_usage[] = { | |
2159 | N_("git submodule--helper config name [value]"), | |
2160 | N_("git submodule--helper config --check-writeable"), | |
2161 | NULL | |
2162 | }; | |
2163 | ||
2164 | argc = parse_options(argc, argv, prefix, module_config_options, | |
2165 | git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0); | |
2166 | ||
2167 | if (argc == 1 && command == CHECK_WRITEABLE) | |
2168 | return is_writing_gitmodules_ok() ? 0 : -1; | |
2169 | ||
2502ffc0 AO |
2170 | /* Equivalent to ACTION_GET in builtin/config.c */ |
2171 | if (argc == 2) | |
2172 | return print_config_from_gitmodules(the_repository, argv[1]); | |
2173 | ||
2174 | /* Equivalent to ACTION_SET in builtin/config.c */ | |
76e9bdc4 AO |
2175 | if (argc == 3) { |
2176 | if (!is_writing_gitmodules_ok()) | |
2177 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
2178 | ||
2502ffc0 | 2179 | return config_set_in_gitmodules_file_gently(argv[1], argv[2]); |
76e9bdc4 | 2180 | } |
2502ffc0 | 2181 | |
b5c259f2 | 2182 | usage_with_options(git_submodule_helper_usage, module_config_options); |
2502ffc0 AO |
2183 | } |
2184 | ||
89c86265 SB |
2185 | #define SUPPORT_SUPER_PREFIX (1<<0) |
2186 | ||
74703a1e SB |
2187 | struct cmd_struct { |
2188 | const char *cmd; | |
2189 | int (*fn)(int, const char **, const char *); | |
89c86265 | 2190 | unsigned option; |
74703a1e SB |
2191 | }; |
2192 | ||
2193 | static struct cmd_struct commands[] = { | |
89c86265 SB |
2194 | {"list", module_list, 0}, |
2195 | {"name", module_name, 0}, | |
2196 | {"clone", module_clone, 0}, | |
ee69b2a9 | 2197 | {"update-module-mode", module_update_module_mode, 0}, |
89c86265 | 2198 | {"update-clone", update_clone, 0}, |
74d4731d | 2199 | {"ensure-core-worktree", ensure_core_worktree, 0}, |
89c86265 SB |
2200 | {"relative-path", resolve_relative_path, 0}, |
2201 | {"resolve-relative-url", resolve_relative_url, 0}, | |
2202 | {"resolve-relative-url-test", resolve_relative_url_test, 0}, | |
fc1b9243 | 2203 | {"foreach", module_foreach, SUPPORT_SUPER_PREFIX}, |
6e7c14e6 | 2204 | {"init", module_init, SUPPORT_SUPER_PREFIX}, |
a9f8a375 | 2205 | {"status", module_status, SUPPORT_SUPER_PREFIX}, |
13424764 PC |
2206 | {"print-default-remote", print_default_remote, 0}, |
2207 | {"sync", module_sync, SUPPORT_SUPER_PREFIX}, | |
2e612731 | 2208 | {"deinit", module_deinit, 0}, |
89c86265 | 2209 | {"remote-branch", resolve_remote_submodule_branch, 0}, |
93481a6b | 2210 | {"push-check", push_check, 0}, |
f6f85861 | 2211 | {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX}, |
5c2bd8b7 | 2212 | {"is-active", is_active, 0}, |
0383bbb9 | 2213 | {"check-name", check_name, 0}, |
2502ffc0 | 2214 | {"config", module_config, 0}, |
74703a1e SB |
2215 | }; |
2216 | ||
2217 | int cmd_submodule__helper(int argc, const char **argv, const char *prefix) | |
2218 | { | |
2219 | int i; | |
f0994fa8 JK |
2220 | if (argc < 2 || !strcmp(argv[1], "-h")) |
2221 | usage("git submodule--helper <command>"); | |
74703a1e | 2222 | |
89c86265 SB |
2223 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
2224 | if (!strcmp(argv[1], commands[i].cmd)) { | |
2225 | if (get_super_prefix() && | |
2226 | !(commands[i].option & SUPPORT_SUPER_PREFIX)) | |
2227 | die(_("%s doesn't support --super-prefix"), | |
2228 | commands[i].cmd); | |
74703a1e | 2229 | return commands[i].fn(argc - 1, argv + 1, prefix); |
89c86265 SB |
2230 | } |
2231 | } | |
74703a1e | 2232 | |
cdc04b65 | 2233 | die(_("'%s' is not a valid submodule--helper " |
74703a1e SB |
2234 | "subcommand"), argv[1]); |
2235 | } |