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