]>
Commit | Line | Data |
---|---|---|
74703a1e SB |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
3 | #include "parse-options.h" | |
4 | #include "quote.h" | |
5 | #include "pathspec.h" | |
6 | #include "dir.h" | |
7 | #include "utf8.h" | |
0ea306ef SB |
8 | #include "submodule.h" |
9 | #include "submodule-config.h" | |
10 | #include "string-list.h" | |
ee8838d1 | 11 | #include "run-command.h" |
63e95beb SB |
12 | #include "remote.h" |
13 | #include "refs.h" | |
14 | #include "connect.h" | |
15 | ||
16 | static char *get_default_remote(void) | |
17 | { | |
18 | char *dest = NULL, *ret; | |
19 | unsigned char sha1[20]; | |
20 | struct strbuf sb = STRBUF_INIT; | |
21 | const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL); | |
22 | ||
23 | if (!refname) | |
24 | die(_("No such ref: %s"), "HEAD"); | |
25 | ||
26 | /* detached HEAD */ | |
27 | if (!strcmp(refname, "HEAD")) | |
28 | return xstrdup("origin"); | |
29 | ||
30 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
31 | die(_("Expecting a full ref name, got %s"), refname); | |
32 | ||
33 | strbuf_addf(&sb, "branch.%s.remote", refname); | |
34 | if (git_config_get_string(sb.buf, &dest)) | |
35 | ret = xstrdup("origin"); | |
36 | else | |
37 | ret = dest; | |
38 | ||
39 | strbuf_release(&sb); | |
40 | return ret; | |
41 | } | |
42 | ||
43 | static int starts_with_dot_slash(const char *str) | |
44 | { | |
45 | return str[0] == '.' && is_dir_sep(str[1]); | |
46 | } | |
47 | ||
48 | static int starts_with_dot_dot_slash(const char *str) | |
49 | { | |
50 | return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]); | |
51 | } | |
52 | ||
53 | /* | |
54 | * Returns 1 if it was the last chop before ':'. | |
55 | */ | |
56 | static int chop_last_dir(char **remoteurl, int is_relative) | |
57 | { | |
58 | char *rfind = find_last_dir_sep(*remoteurl); | |
59 | if (rfind) { | |
60 | *rfind = '\0'; | |
61 | return 0; | |
62 | } | |
63 | ||
64 | rfind = strrchr(*remoteurl, ':'); | |
65 | if (rfind) { | |
66 | *rfind = '\0'; | |
67 | return 1; | |
68 | } | |
69 | ||
70 | if (is_relative || !strcmp(".", *remoteurl)) | |
71 | die(_("cannot strip one component off url '%s'"), | |
72 | *remoteurl); | |
73 | ||
74 | free(*remoteurl); | |
75 | *remoteurl = xstrdup("."); | |
76 | return 0; | |
77 | } | |
78 | ||
79 | /* | |
80 | * The `url` argument is the URL that navigates to the submodule origin | |
81 | * repo. When relative, this URL is relative to the superproject origin | |
82 | * URL repo. The `up_path` argument, if specified, is the relative | |
83 | * path that navigates from the submodule working tree to the superproject | |
84 | * working tree. Returns the origin URL of the submodule. | |
85 | * | |
86 | * Return either an absolute URL or filesystem path (if the superproject | |
87 | * origin URL is an absolute URL or filesystem path, respectively) or a | |
88 | * relative file system path (if the superproject origin URL is a relative | |
89 | * file system path). | |
90 | * | |
91 | * When the output is a relative file system path, the path is either | |
92 | * relative to the submodule working tree, if up_path is specified, or to | |
93 | * the superproject working tree otherwise. | |
94 | * | |
95 | * NEEDSWORK: This works incorrectly on the domain and protocol part. | |
96 | * remote_url url outcome expectation | |
97 | * http://a.com/b ../c http://a.com/c as is | |
08788504 SB |
98 | * http://a.com/b/ ../c http://a.com/c same as previous line, but |
99 | * ignore trailing slash in url | |
63e95beb SB |
100 | * http://a.com/b ../../c http://c error out |
101 | * http://a.com/b ../../../c http:/c error out | |
102 | * http://a.com/b ../../../../c http:c error out | |
103 | * http://a.com/b ../../../../../c .:c error out | |
104 | * NEEDSWORK: Given how chop_last_dir() works, this function is broken | |
105 | * when a local part has a colon in its path component, too. | |
106 | */ | |
107 | static char *relative_url(const char *remote_url, | |
108 | const char *url, | |
109 | const char *up_path) | |
110 | { | |
111 | int is_relative = 0; | |
112 | int colonsep = 0; | |
113 | char *out; | |
114 | char *remoteurl = xstrdup(remote_url); | |
115 | struct strbuf sb = STRBUF_INIT; | |
116 | size_t len = strlen(remoteurl); | |
117 | ||
08788504 SB |
118 | if (is_dir_sep(remoteurl[len-1])) |
119 | remoteurl[len-1] = '\0'; | |
63e95beb SB |
120 | |
121 | if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl)) | |
122 | is_relative = 0; | |
123 | else { | |
124 | is_relative = 1; | |
125 | /* | |
126 | * Prepend a './' to ensure all relative | |
127 | * remoteurls start with './' or '../' | |
128 | */ | |
129 | if (!starts_with_dot_slash(remoteurl) && | |
130 | !starts_with_dot_dot_slash(remoteurl)) { | |
131 | strbuf_reset(&sb); | |
132 | strbuf_addf(&sb, "./%s", remoteurl); | |
133 | free(remoteurl); | |
134 | remoteurl = strbuf_detach(&sb, NULL); | |
135 | } | |
136 | } | |
137 | /* | |
138 | * When the url starts with '../', remove that and the | |
139 | * last directory in remoteurl. | |
140 | */ | |
141 | while (url) { | |
142 | if (starts_with_dot_dot_slash(url)) { | |
143 | url += 3; | |
144 | colonsep |= chop_last_dir(&remoteurl, is_relative); | |
145 | } else if (starts_with_dot_slash(url)) | |
146 | url += 2; | |
147 | else | |
148 | break; | |
149 | } | |
150 | strbuf_reset(&sb); | |
151 | strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url); | |
3389e78e SB |
152 | if (ends_with(url, "/")) |
153 | strbuf_setlen(&sb, sb.len - 1); | |
63e95beb SB |
154 | free(remoteurl); |
155 | ||
156 | if (starts_with_dot_slash(sb.buf)) | |
157 | out = xstrdup(sb.buf + 2); | |
158 | else | |
159 | out = xstrdup(sb.buf); | |
160 | strbuf_reset(&sb); | |
161 | ||
162 | if (!up_path || !is_relative) | |
163 | return out; | |
164 | ||
165 | strbuf_addf(&sb, "%s%s", up_path, out); | |
166 | free(out); | |
167 | return strbuf_detach(&sb, NULL); | |
168 | } | |
169 | ||
170 | static int resolve_relative_url(int argc, const char **argv, const char *prefix) | |
171 | { | |
172 | char *remoteurl = NULL; | |
173 | char *remote = get_default_remote(); | |
174 | const char *up_path = NULL; | |
175 | char *res; | |
176 | const char *url; | |
177 | struct strbuf sb = STRBUF_INIT; | |
178 | ||
179 | if (argc != 2 && argc != 3) | |
180 | die("resolve-relative-url only accepts one or two arguments"); | |
181 | ||
182 | url = argv[1]; | |
183 | strbuf_addf(&sb, "remote.%s.url", remote); | |
184 | free(remote); | |
185 | ||
186 | if (git_config_get_string(sb.buf, &remoteurl)) | |
187 | /* the repository is its own authoritative upstream */ | |
188 | remoteurl = xgetcwd(); | |
189 | ||
190 | if (argc == 3) | |
191 | up_path = argv[2]; | |
192 | ||
193 | res = relative_url(remoteurl, url, up_path); | |
194 | puts(res); | |
195 | free(res); | |
196 | free(remoteurl); | |
197 | return 0; | |
198 | } | |
199 | ||
200 | static int resolve_relative_url_test(int argc, const char **argv, const char *prefix) | |
201 | { | |
202 | char *remoteurl, *res; | |
203 | const char *up_path, *url; | |
204 | ||
205 | if (argc != 4) | |
206 | die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>"); | |
207 | ||
208 | up_path = argv[1]; | |
209 | remoteurl = xstrdup(argv[2]); | |
210 | url = argv[3]; | |
211 | ||
212 | if (!strcmp(up_path, "(null)")) | |
213 | up_path = NULL; | |
214 | ||
215 | res = relative_url(remoteurl, url, up_path); | |
216 | puts(res); | |
217 | free(res); | |
218 | free(remoteurl); | |
219 | return 0; | |
220 | } | |
74703a1e SB |
221 | |
222 | struct module_list { | |
223 | const struct cache_entry **entries; | |
224 | int alloc, nr; | |
225 | }; | |
226 | #define MODULE_LIST_INIT { NULL, 0, 0 } | |
227 | ||
228 | static int module_list_compute(int argc, const char **argv, | |
229 | const char *prefix, | |
230 | struct pathspec *pathspec, | |
231 | struct module_list *list) | |
232 | { | |
233 | int i, result = 0; | |
2b56bb7a | 234 | char *ps_matched = NULL; |
74703a1e SB |
235 | parse_pathspec(pathspec, 0, |
236 | PATHSPEC_PREFER_FULL | | |
237 | PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP, | |
238 | prefix, argv); | |
239 | ||
74703a1e SB |
240 | if (pathspec->nr) |
241 | ps_matched = xcalloc(pathspec->nr, 1); | |
242 | ||
243 | if (read_cache() < 0) | |
244 | die(_("index file corrupt")); | |
245 | ||
246 | for (i = 0; i < active_nr; i++) { | |
247 | const struct cache_entry *ce = active_cache[i]; | |
248 | ||
84ba959b SB |
249 | if (!match_pathspec(pathspec, ce->name, ce_namelen(ce), |
250 | 0, ps_matched, 1) || | |
251 | !S_ISGITLINK(ce->ce_mode)) | |
74703a1e SB |
252 | continue; |
253 | ||
254 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); | |
255 | list->entries[list->nr++] = ce; | |
256 | while (i + 1 < active_nr && | |
257 | !strcmp(ce->name, active_cache[i + 1]->name)) | |
258 | /* | |
259 | * Skip entries with the same name in different stages | |
260 | * to make sure an entry is returned only once. | |
261 | */ | |
262 | i++; | |
263 | } | |
74703a1e SB |
264 | |
265 | if (ps_matched && report_path_error(ps_matched, pathspec, prefix)) | |
266 | result = -1; | |
267 | ||
268 | free(ps_matched); | |
269 | ||
270 | return result; | |
271 | } | |
272 | ||
3e7eaed0 BW |
273 | static void module_list_active(struct module_list *list) |
274 | { | |
275 | int i; | |
276 | struct module_list active_modules = MODULE_LIST_INIT; | |
277 | ||
278 | gitmodules_config(); | |
279 | ||
280 | for (i = 0; i < list->nr; i++) { | |
281 | const struct cache_entry *ce = list->entries[i]; | |
282 | ||
283 | if (!is_submodule_initialized(ce->name)) | |
284 | continue; | |
285 | ||
286 | ALLOC_GROW(active_modules.entries, | |
287 | active_modules.nr + 1, | |
288 | active_modules.alloc); | |
289 | active_modules.entries[active_modules.nr++] = ce; | |
290 | } | |
291 | ||
292 | free(list->entries); | |
293 | *list = active_modules; | |
294 | } | |
295 | ||
74703a1e SB |
296 | static int module_list(int argc, const char **argv, const char *prefix) |
297 | { | |
298 | int i; | |
299 | struct pathspec pathspec; | |
300 | struct module_list list = MODULE_LIST_INIT; | |
301 | ||
302 | struct option module_list_options[] = { | |
303 | OPT_STRING(0, "prefix", &prefix, | |
304 | N_("path"), | |
305 | N_("alternative anchor for relative paths")), | |
306 | OPT_END() | |
307 | }; | |
308 | ||
309 | const char *const git_submodule_helper_usage[] = { | |
310 | N_("git submodule--helper list [--prefix=<path>] [<path>...]"), | |
311 | NULL | |
312 | }; | |
313 | ||
314 | argc = parse_options(argc, argv, prefix, module_list_options, | |
315 | git_submodule_helper_usage, 0); | |
316 | ||
b0f4b408 | 317 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
74703a1e | 318 | return 1; |
74703a1e SB |
319 | |
320 | for (i = 0; i < list.nr; i++) { | |
321 | const struct cache_entry *ce = list.entries[i]; | |
322 | ||
323 | if (ce_stage(ce)) | |
324 | printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1)); | |
325 | else | |
99d1a986 | 326 | printf("%06o %s %d\t", ce->ce_mode, |
327 | oid_to_hex(&ce->oid), ce_stage(ce)); | |
74703a1e SB |
328 | |
329 | utf8_fprintf(stdout, "%s\n", ce->name); | |
330 | } | |
331 | return 0; | |
332 | } | |
333 | ||
3604242f SB |
334 | static void init_submodule(const char *path, const char *prefix, int quiet) |
335 | { | |
336 | const struct submodule *sub; | |
337 | struct strbuf sb = STRBUF_INIT; | |
338 | char *upd = NULL, *url = NULL, *displaypath; | |
339 | ||
340 | /* Only loads from .gitmodules, no overlay with .git/config */ | |
341 | gitmodules_config(); | |
342 | ||
6e7c14e6 SB |
343 | if (prefix && get_super_prefix()) |
344 | die("BUG: cannot have prefix and superprefix"); | |
345 | else if (prefix) | |
346 | displaypath = xstrdup(relative_path(path, prefix, &sb)); | |
347 | else if (get_super_prefix()) { | |
348 | strbuf_addf(&sb, "%s%s", get_super_prefix(), path); | |
3604242f SB |
349 | displaypath = strbuf_detach(&sb, NULL); |
350 | } else | |
d92028a5 SB |
351 | displaypath = xstrdup(path); |
352 | ||
353 | sub = submodule_from_path(null_sha1, path); | |
354 | ||
355 | if (!sub) | |
356 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
357 | displaypath); | |
3604242f SB |
358 | |
359 | /* | |
360 | * Copy url setting when it is not set yet. | |
361 | * To look up the url in .git/config, we must not fall back to | |
362 | * .gitmodules, so look it up directly. | |
363 | */ | |
364 | strbuf_reset(&sb); | |
365 | strbuf_addf(&sb, "submodule.%s.url", sub->name); | |
366 | if (git_config_get_string(sb.buf, &url)) { | |
367 | url = xstrdup(sub->url); | |
368 | ||
369 | if (!url) | |
370 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
371 | displaypath); | |
372 | ||
373 | /* Possibly a url relative to parent */ | |
374 | if (starts_with_dot_dot_slash(url) || | |
375 | starts_with_dot_slash(url)) { | |
376 | char *remoteurl, *relurl; | |
377 | char *remote = get_default_remote(); | |
378 | struct strbuf remotesb = STRBUF_INIT; | |
379 | strbuf_addf(&remotesb, "remote.%s.url", remote); | |
380 | free(remote); | |
381 | ||
d1b3b81a SB |
382 | if (git_config_get_string(remotesb.buf, &remoteurl)) { |
383 | warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf); | |
3604242f | 384 | remoteurl = xgetcwd(); |
d1b3b81a | 385 | } |
3604242f SB |
386 | relurl = relative_url(remoteurl, url, NULL); |
387 | strbuf_release(&remotesb); | |
388 | free(remoteurl); | |
389 | free(url); | |
390 | url = relurl; | |
391 | } | |
392 | ||
393 | if (git_config_set_gently(sb.buf, url)) | |
394 | die(_("Failed to register url for submodule path '%s'"), | |
395 | displaypath); | |
396 | if (!quiet) | |
c66410ed SB |
397 | fprintf(stderr, |
398 | _("Submodule '%s' (%s) registered for path '%s'\n"), | |
3604242f SB |
399 | sub->name, url, displaypath); |
400 | } | |
401 | ||
402 | /* Copy "update" setting when it is not set yet */ | |
403 | strbuf_reset(&sb); | |
404 | strbuf_addf(&sb, "submodule.%s.update", sub->name); | |
405 | if (git_config_get_string(sb.buf, &upd) && | |
406 | sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
407 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) { | |
408 | fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"), | |
409 | sub->name); | |
410 | upd = xstrdup("none"); | |
411 | } else | |
412 | upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy)); | |
413 | ||
414 | if (git_config_set_gently(sb.buf, upd)) | |
415 | die(_("Failed to register update mode for submodule path '%s'"), displaypath); | |
416 | } | |
417 | strbuf_release(&sb); | |
418 | free(displaypath); | |
419 | free(url); | |
420 | free(upd); | |
421 | } | |
422 | ||
423 | static int module_init(int argc, const char **argv, const char *prefix) | |
424 | { | |
425 | struct pathspec pathspec; | |
426 | struct module_list list = MODULE_LIST_INIT; | |
427 | int quiet = 0; | |
428 | int i; | |
429 | ||
430 | struct option module_init_options[] = { | |
3604242f SB |
431 | OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")), |
432 | OPT_END() | |
433 | }; | |
434 | ||
435 | const char *const git_submodule_helper_usage[] = { | |
436 | N_("git submodule--helper init [<path>]"), | |
437 | NULL | |
438 | }; | |
439 | ||
440 | argc = parse_options(argc, argv, prefix, module_init_options, | |
441 | git_submodule_helper_usage, 0); | |
442 | ||
443 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
444 | return 1; | |
445 | ||
3e7eaed0 BW |
446 | /* |
447 | * If there are no path args and submodule.active is set then, | |
448 | * by default, only initialize 'active' modules. | |
449 | */ | |
450 | if (!argc && git_config_get_value_multi("submodule.active")) | |
451 | module_list_active(&list); | |
452 | ||
3604242f SB |
453 | for (i = 0; i < list.nr; i++) |
454 | init_submodule(list.entries[i]->name, prefix, quiet); | |
455 | ||
456 | return 0; | |
457 | } | |
458 | ||
0ea306ef SB |
459 | static int module_name(int argc, const char **argv, const char *prefix) |
460 | { | |
461 | const struct submodule *sub; | |
462 | ||
463 | if (argc != 2) | |
464 | usage(_("git submodule--helper name <path>")); | |
465 | ||
466 | gitmodules_config(); | |
467 | sub = submodule_from_path(null_sha1, argv[1]); | |
468 | ||
469 | if (!sub) | |
470 | die(_("no submodule mapping found in .gitmodules for path '%s'"), | |
471 | argv[1]); | |
472 | ||
473 | printf("%s\n", sub->name); | |
474 | ||
475 | return 0; | |
476 | } | |
14111fc4 | 477 | |
ee8838d1 | 478 | static int clone_submodule(const char *path, const char *gitdir, const char *url, |
72c5f883 JK |
479 | const char *depth, struct string_list *reference, |
480 | int quiet, int progress) | |
ee8838d1 | 481 | { |
542aa25d | 482 | struct child_process cp = CHILD_PROCESS_INIT; |
ee8838d1 SB |
483 | |
484 | argv_array_push(&cp.args, "clone"); | |
485 | argv_array_push(&cp.args, "--no-checkout"); | |
486 | if (quiet) | |
487 | argv_array_push(&cp.args, "--quiet"); | |
72c5f883 JK |
488 | if (progress) |
489 | argv_array_push(&cp.args, "--progress"); | |
ee8838d1 SB |
490 | if (depth && *depth) |
491 | argv_array_pushl(&cp.args, "--depth", depth, NULL); | |
965dbea0 SB |
492 | if (reference->nr) { |
493 | struct string_list_item *item; | |
494 | for_each_string_list_item(item, reference) | |
495 | argv_array_pushl(&cp.args, "--reference", | |
496 | item->string, NULL); | |
497 | } | |
ee8838d1 SB |
498 | if (gitdir && *gitdir) |
499 | argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL); | |
500 | ||
501 | argv_array_push(&cp.args, url); | |
502 | argv_array_push(&cp.args, path); | |
503 | ||
504 | cp.git_cmd = 1; | |
14111fc4 | 505 | prepare_submodule_repo_env(&cp.env_array); |
ee8838d1 SB |
506 | cp.no_stdin = 1; |
507 | ||
508 | return run_command(&cp); | |
509 | } | |
510 | ||
31224cbd SB |
511 | struct submodule_alternate_setup { |
512 | const char *submodule_name; | |
513 | enum SUBMODULE_ALTERNATE_ERROR_MODE { | |
514 | SUBMODULE_ALTERNATE_ERROR_DIE, | |
515 | SUBMODULE_ALTERNATE_ERROR_INFO, | |
516 | SUBMODULE_ALTERNATE_ERROR_IGNORE | |
517 | } error_mode; | |
518 | struct string_list *reference; | |
519 | }; | |
520 | #define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \ | |
521 | SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL } | |
522 | ||
523 | static int add_possible_reference_from_superproject( | |
524 | struct alternate_object_database *alt, void *sas_cb) | |
525 | { | |
526 | struct submodule_alternate_setup *sas = sas_cb; | |
527 | ||
31224cbd SB |
528 | /* |
529 | * If the alternate object store is another repository, try the | |
bf03b790 | 530 | * standard layout with .git/(modules/<name>)+/objects |
31224cbd | 531 | */ |
bf03b790 | 532 | if (ends_with(alt->path, "/objects")) { |
31224cbd SB |
533 | char *sm_alternate; |
534 | struct strbuf sb = STRBUF_INIT; | |
535 | struct strbuf err = STRBUF_INIT; | |
597f9134 JK |
536 | strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects")); |
537 | ||
31224cbd SB |
538 | /* |
539 | * We need to end the new path with '/' to mark it as a dir, | |
540 | * otherwise a submodule name containing '/' will be broken | |
541 | * as the last part of a missing submodule reference would | |
542 | * be taken as a file name. | |
543 | */ | |
544 | strbuf_addf(&sb, "modules/%s/", sas->submodule_name); | |
545 | ||
546 | sm_alternate = compute_alternate_path(sb.buf, &err); | |
547 | if (sm_alternate) { | |
548 | string_list_append(sas->reference, xstrdup(sb.buf)); | |
549 | free(sm_alternate); | |
550 | } else { | |
551 | switch (sas->error_mode) { | |
552 | case SUBMODULE_ALTERNATE_ERROR_DIE: | |
553 | die(_("submodule '%s' cannot add alternate: %s"), | |
554 | sas->submodule_name, err.buf); | |
555 | case SUBMODULE_ALTERNATE_ERROR_INFO: | |
556 | fprintf(stderr, _("submodule '%s' cannot add alternate: %s"), | |
557 | sas->submodule_name, err.buf); | |
558 | case SUBMODULE_ALTERNATE_ERROR_IGNORE: | |
559 | ; /* nothing */ | |
560 | } | |
561 | } | |
562 | strbuf_release(&sb); | |
563 | } | |
564 | ||
31224cbd SB |
565 | return 0; |
566 | } | |
567 | ||
568 | static void prepare_possible_alternates(const char *sm_name, | |
569 | struct string_list *reference) | |
570 | { | |
571 | char *sm_alternate = NULL, *error_strategy = NULL; | |
572 | struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT; | |
573 | ||
574 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
575 | if (!sm_alternate) | |
576 | return; | |
577 | ||
578 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
579 | ||
580 | if (!error_strategy) | |
581 | error_strategy = xstrdup("die"); | |
582 | ||
583 | sas.submodule_name = sm_name; | |
584 | sas.reference = reference; | |
585 | if (!strcmp(error_strategy, "die")) | |
586 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE; | |
587 | else if (!strcmp(error_strategy, "info")) | |
588 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO; | |
589 | else if (!strcmp(error_strategy, "ignore")) | |
590 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE; | |
591 | else | |
592 | die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy); | |
593 | ||
594 | if (!strcmp(sm_alternate, "superproject")) | |
595 | foreach_alt_odb(add_possible_reference_from_superproject, &sas); | |
596 | else if (!strcmp(sm_alternate, "no")) | |
597 | ; /* do nothing */ | |
598 | else | |
599 | die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate); | |
600 | ||
601 | free(sm_alternate); | |
602 | free(error_strategy); | |
603 | } | |
604 | ||
ee8838d1 SB |
605 | static int module_clone(int argc, const char **argv, const char *prefix) |
606 | { | |
965dbea0 | 607 | const char *name = NULL, *url = NULL, *depth = NULL; |
ee8838d1 | 608 | int quiet = 0; |
72c5f883 | 609 | int progress = 0; |
ee8838d1 | 610 | FILE *submodule_dot_git; |
1ea4d9b7 | 611 | char *p, *path = NULL, *sm_gitdir; |
ee8838d1 SB |
612 | struct strbuf rel_path = STRBUF_INIT; |
613 | struct strbuf sb = STRBUF_INIT; | |
965dbea0 | 614 | struct string_list reference = STRING_LIST_INIT_NODUP; |
bf03b790 | 615 | char *sm_alternate = NULL, *error_strategy = NULL; |
ee8838d1 SB |
616 | |
617 | struct option module_clone_options[] = { | |
618 | OPT_STRING(0, "prefix", &prefix, | |
619 | N_("path"), | |
620 | N_("alternative anchor for relative paths")), | |
621 | OPT_STRING(0, "path", &path, | |
622 | N_("path"), | |
623 | N_("where the new submodule will be cloned to")), | |
624 | OPT_STRING(0, "name", &name, | |
625 | N_("string"), | |
626 | N_("name of the new submodule")), | |
627 | OPT_STRING(0, "url", &url, | |
628 | N_("string"), | |
629 | N_("url where to clone the submodule from")), | |
965dbea0 SB |
630 | OPT_STRING_LIST(0, "reference", &reference, |
631 | N_("repo"), | |
ee8838d1 SB |
632 | N_("reference repository")), |
633 | OPT_STRING(0, "depth", &depth, | |
634 | N_("string"), | |
635 | N_("depth for shallow clones")), | |
636 | OPT__QUIET(&quiet, "Suppress output for cloning a submodule"), | |
72c5f883 JK |
637 | OPT_BOOL(0, "progress", &progress, |
638 | N_("force cloning progress")), | |
ee8838d1 SB |
639 | OPT_END() |
640 | }; | |
641 | ||
642 | const char *const git_submodule_helper_usage[] = { | |
643 | N_("git submodule--helper clone [--prefix=<path>] [--quiet] " | |
7dad2633 JK |
644 | "[--reference <repository>] [--name <name>] [--depth <depth>] " |
645 | "--url <url> --path <path>"), | |
ee8838d1 SB |
646 | NULL |
647 | }; | |
648 | ||
649 | argc = parse_options(argc, argv, prefix, module_clone_options, | |
650 | git_submodule_helper_usage, 0); | |
651 | ||
deef3cdc | 652 | if (argc || !url || !path || !*path) |
08e0970a JK |
653 | usage_with_options(git_submodule_helper_usage, |
654 | module_clone_options); | |
655 | ||
ee8838d1 | 656 | strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name); |
0aaad415 | 657 | sm_gitdir = absolute_pathdup(sb.buf); |
1ea4d9b7 | 658 | strbuf_reset(&sb); |
f8eaa0ba SB |
659 | |
660 | if (!is_absolute_path(path)) { | |
661 | strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path); | |
662 | path = strbuf_detach(&sb, NULL); | |
663 | } else | |
664 | path = xstrdup(path); | |
ee8838d1 SB |
665 | |
666 | if (!file_exists(sm_gitdir)) { | |
667 | if (safe_create_leading_directories_const(sm_gitdir) < 0) | |
668 | die(_("could not create directory '%s'"), sm_gitdir); | |
31224cbd SB |
669 | |
670 | prepare_possible_alternates(name, &reference); | |
671 | ||
72c5f883 JK |
672 | if (clone_submodule(path, sm_gitdir, url, depth, &reference, |
673 | quiet, progress)) | |
ee8838d1 SB |
674 | die(_("clone of '%s' into submodule path '%s' failed"), |
675 | url, path); | |
676 | } else { | |
677 | if (safe_create_leading_directories_const(path) < 0) | |
678 | die(_("could not create directory '%s'"), path); | |
679 | strbuf_addf(&sb, "%s/index", sm_gitdir); | |
680 | unlink_or_warn(sb.buf); | |
681 | strbuf_reset(&sb); | |
682 | } | |
683 | ||
684 | /* Write a .git file in the submodule to redirect to the superproject. */ | |
3c0663e1 | 685 | strbuf_addf(&sb, "%s/.git", path); |
ee8838d1 SB |
686 | if (safe_create_leading_directories_const(sb.buf) < 0) |
687 | die(_("could not create leading directories of '%s'"), sb.buf); | |
688 | submodule_dot_git = fopen(sb.buf, "w"); | |
689 | if (!submodule_dot_git) | |
690 | die_errno(_("cannot open file '%s'"), sb.buf); | |
691 | ||
1f15ba1f SB |
692 | fprintf_or_die(submodule_dot_git, "gitdir: %s\n", |
693 | relative_path(sm_gitdir, path, &rel_path)); | |
ee8838d1 SB |
694 | if (fclose(submodule_dot_git)) |
695 | die(_("could not close file %s"), sb.buf); | |
696 | strbuf_reset(&sb); | |
697 | strbuf_reset(&rel_path); | |
698 | ||
ee8838d1 | 699 | /* Redirect the worktree of the submodule in the superproject's config */ |
ee8838d1 SB |
700 | p = git_pathdup_submodule(path, "config"); |
701 | if (!p) | |
702 | die(_("could not get submodule directory for '%s'"), path); | |
703 | git_config_set_in_file(p, "core.worktree", | |
f8eaa0ba | 704 | relative_path(path, sm_gitdir, &rel_path)); |
bf03b790 VS |
705 | |
706 | /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ | |
707 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
708 | if (sm_alternate) | |
709 | git_config_set_in_file(p, "submodule.alternateLocation", | |
710 | sm_alternate); | |
711 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
712 | if (error_strategy) | |
713 | git_config_set_in_file(p, "submodule.alternateErrorStrategy", | |
714 | error_strategy); | |
715 | ||
716 | free(sm_alternate); | |
717 | free(error_strategy); | |
718 | ||
ee8838d1 SB |
719 | strbuf_release(&sb); |
720 | strbuf_release(&rel_path); | |
721 | free(sm_gitdir); | |
f8eaa0ba | 722 | free(path); |
ee8838d1 SB |
723 | free(p); |
724 | return 0; | |
725 | } | |
74703a1e | 726 | |
48308681 SB |
727 | struct submodule_update_clone { |
728 | /* index into 'list', the list of submodules to look into for cloning */ | |
729 | int current; | |
730 | struct module_list list; | |
731 | unsigned warn_if_uninitialized : 1; | |
732 | ||
733 | /* update parameter passed via commandline */ | |
734 | struct submodule_update_strategy update; | |
735 | ||
736 | /* configuration parameters which are passed on to the children */ | |
72c5f883 | 737 | int progress; |
48308681 | 738 | int quiet; |
abed000a | 739 | int recommend_shallow; |
5f50f33e | 740 | struct string_list references; |
48308681 SB |
741 | const char *depth; |
742 | const char *recursive_prefix; | |
743 | const char *prefix; | |
744 | ||
745 | /* Machine-readable status lines to be consumed by git-submodule.sh */ | |
746 | struct string_list projectlines; | |
747 | ||
748 | /* If we want to stop as fast as possible and return an error */ | |
749 | unsigned quickstop : 1; | |
665b35ec SB |
750 | |
751 | /* failed clones to be retried again */ | |
752 | const struct cache_entry **failed_clones; | |
753 | int failed_clones_nr, failed_clones_alloc; | |
48308681 SB |
754 | }; |
755 | #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \ | |
72c5f883 | 756 | SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \ |
5f50f33e | 757 | NULL, NULL, NULL, \ |
665b35ec | 758 | STRING_LIST_INIT_DUP, 0, NULL, 0, 0} |
48308681 | 759 | |
08fdbdb1 SB |
760 | |
761 | static void next_submodule_warn_missing(struct submodule_update_clone *suc, | |
762 | struct strbuf *out, const char *displaypath) | |
763 | { | |
764 | /* | |
765 | * Only mention uninitialized submodules when their | |
766 | * paths have been specified. | |
767 | */ | |
768 | if (suc->warn_if_uninitialized) { | |
769 | strbuf_addf(out, | |
770 | _("Submodule path '%s' not initialized"), | |
771 | displaypath); | |
772 | strbuf_addch(out, '\n'); | |
773 | strbuf_addstr(out, | |
774 | _("Maybe you want to use 'update --init'?")); | |
775 | strbuf_addch(out, '\n'); | |
776 | } | |
777 | } | |
778 | ||
48308681 SB |
779 | /** |
780 | * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to | |
781 | * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise. | |
782 | */ | |
783 | static int prepare_to_clone_next_submodule(const struct cache_entry *ce, | |
784 | struct child_process *child, | |
785 | struct submodule_update_clone *suc, | |
786 | struct strbuf *out) | |
787 | { | |
788 | const struct submodule *sub = NULL; | |
789 | struct strbuf displaypath_sb = STRBUF_INIT; | |
790 | struct strbuf sb = STRBUF_INIT; | |
791 | const char *displaypath = NULL; | |
48308681 SB |
792 | int needs_cloning = 0; |
793 | ||
794 | if (ce_stage(ce)) { | |
795 | if (suc->recursive_prefix) | |
796 | strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name); | |
797 | else | |
92d52fab | 798 | strbuf_addstr(&sb, ce->name); |
48308681 SB |
799 | strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf); |
800 | strbuf_addch(out, '\n'); | |
801 | goto cleanup; | |
802 | } | |
803 | ||
804 | sub = submodule_from_path(null_sha1, ce->name); | |
805 | ||
806 | if (suc->recursive_prefix) | |
807 | displaypath = relative_path(suc->recursive_prefix, | |
808 | ce->name, &displaypath_sb); | |
809 | else | |
810 | displaypath = ce->name; | |
811 | ||
08fdbdb1 SB |
812 | if (!sub) { |
813 | next_submodule_warn_missing(suc, out, displaypath); | |
814 | goto cleanup; | |
815 | } | |
816 | ||
48308681 SB |
817 | if (suc->update.type == SM_UPDATE_NONE |
818 | || (suc->update.type == SM_UPDATE_UNSPECIFIED | |
819 | && sub->update_strategy.type == SM_UPDATE_NONE)) { | |
820 | strbuf_addf(out, _("Skipping submodule '%s'"), displaypath); | |
821 | strbuf_addch(out, '\n'); | |
822 | goto cleanup; | |
823 | } | |
824 | ||
ee92ab99 BW |
825 | /* Check if the submodule has been initialized. */ |
826 | if (!is_submodule_initialized(ce->name)) { | |
08fdbdb1 | 827 | next_submodule_warn_missing(suc, out, displaypath); |
48308681 SB |
828 | goto cleanup; |
829 | } | |
830 | ||
831 | strbuf_reset(&sb); | |
832 | strbuf_addf(&sb, "%s/.git", ce->name); | |
833 | needs_cloning = !file_exists(sb.buf); | |
834 | ||
835 | strbuf_reset(&sb); | |
836 | strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode, | |
99d1a986 | 837 | oid_to_hex(&ce->oid), ce_stage(ce), |
48308681 SB |
838 | needs_cloning, ce->name); |
839 | string_list_append(&suc->projectlines, sb.buf); | |
840 | ||
841 | if (!needs_cloning) | |
842 | goto cleanup; | |
843 | ||
844 | child->git_cmd = 1; | |
845 | child->no_stdin = 1; | |
846 | child->stdout_to_stderr = 1; | |
847 | child->err = -1; | |
848 | argv_array_push(&child->args, "submodule--helper"); | |
849 | argv_array_push(&child->args, "clone"); | |
72c5f883 JK |
850 | if (suc->progress) |
851 | argv_array_push(&child->args, "--progress"); | |
48308681 SB |
852 | if (suc->quiet) |
853 | argv_array_push(&child->args, "--quiet"); | |
854 | if (suc->prefix) | |
855 | argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL); | |
abed000a SB |
856 | if (suc->recommend_shallow && sub->recommend_shallow == 1) |
857 | argv_array_push(&child->args, "--depth=1"); | |
48308681 SB |
858 | argv_array_pushl(&child->args, "--path", sub->path, NULL); |
859 | argv_array_pushl(&child->args, "--name", sub->name, NULL); | |
ee92ab99 | 860 | argv_array_pushl(&child->args, "--url", sub->url, NULL); |
5f50f33e SB |
861 | if (suc->references.nr) { |
862 | struct string_list_item *item; | |
863 | for_each_string_list_item(item, &suc->references) | |
864 | argv_array_pushl(&child->args, "--reference", item->string, NULL); | |
865 | } | |
48308681 SB |
866 | if (suc->depth) |
867 | argv_array_push(&child->args, suc->depth); | |
868 | ||
869 | cleanup: | |
48308681 SB |
870 | strbuf_reset(&displaypath_sb); |
871 | strbuf_reset(&sb); | |
872 | ||
873 | return needs_cloning; | |
874 | } | |
875 | ||
876 | static int update_clone_get_next_task(struct child_process *child, | |
877 | struct strbuf *err, | |
878 | void *suc_cb, | |
665b35ec | 879 | void **idx_task_cb) |
48308681 SB |
880 | { |
881 | struct submodule_update_clone *suc = suc_cb; | |
665b35ec SB |
882 | const struct cache_entry *ce; |
883 | int index; | |
48308681 SB |
884 | |
885 | for (; suc->current < suc->list.nr; suc->current++) { | |
665b35ec | 886 | ce = suc->list.entries[suc->current]; |
48308681 | 887 | if (prepare_to_clone_next_submodule(ce, child, suc, err)) { |
665b35ec SB |
888 | int *p = xmalloc(sizeof(*p)); |
889 | *p = suc->current; | |
890 | *idx_task_cb = p; | |
48308681 SB |
891 | suc->current++; |
892 | return 1; | |
893 | } | |
894 | } | |
665b35ec SB |
895 | |
896 | /* | |
897 | * The loop above tried cloning each submodule once, now try the | |
898 | * stragglers again, which we can imagine as an extension of the | |
899 | * entry list. | |
900 | */ | |
901 | index = suc->current - suc->list.nr; | |
902 | if (index < suc->failed_clones_nr) { | |
903 | int *p; | |
904 | ce = suc->failed_clones[index]; | |
2201ee09 SB |
905 | if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { |
906 | suc->current ++; | |
a22ae753 RS |
907 | strbuf_addstr(err, "BUG: submodule considered for " |
908 | "cloning, doesn't need cloning " | |
909 | "any more?\n"); | |
2201ee09 SB |
910 | return 0; |
911 | } | |
665b35ec SB |
912 | p = xmalloc(sizeof(*p)); |
913 | *p = suc->current; | |
914 | *idx_task_cb = p; | |
915 | suc->current ++; | |
916 | return 1; | |
917 | } | |
918 | ||
48308681 SB |
919 | return 0; |
920 | } | |
921 | ||
922 | static int update_clone_start_failure(struct strbuf *err, | |
923 | void *suc_cb, | |
665b35ec | 924 | void *idx_task_cb) |
48308681 SB |
925 | { |
926 | struct submodule_update_clone *suc = suc_cb; | |
927 | suc->quickstop = 1; | |
928 | return 1; | |
929 | } | |
930 | ||
931 | static int update_clone_task_finished(int result, | |
932 | struct strbuf *err, | |
933 | void *suc_cb, | |
665b35ec | 934 | void *idx_task_cb) |
48308681 | 935 | { |
665b35ec | 936 | const struct cache_entry *ce; |
48308681 SB |
937 | struct submodule_update_clone *suc = suc_cb; |
938 | ||
665b35ec SB |
939 | int *idxP = *(int**)idx_task_cb; |
940 | int idx = *idxP; | |
941 | free(idxP); | |
942 | ||
48308681 SB |
943 | if (!result) |
944 | return 0; | |
945 | ||
665b35ec SB |
946 | if (idx < suc->list.nr) { |
947 | ce = suc->list.entries[idx]; | |
948 | strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"), | |
949 | ce->name); | |
950 | strbuf_addch(err, '\n'); | |
951 | ALLOC_GROW(suc->failed_clones, | |
952 | suc->failed_clones_nr + 1, | |
953 | suc->failed_clones_alloc); | |
954 | suc->failed_clones[suc->failed_clones_nr++] = ce; | |
955 | return 0; | |
956 | } else { | |
eb09121b | 957 | idx -= suc->list.nr; |
665b35ec SB |
958 | ce = suc->failed_clones[idx]; |
959 | strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"), | |
960 | ce->name); | |
961 | strbuf_addch(err, '\n'); | |
962 | suc->quickstop = 1; | |
963 | return 1; | |
964 | } | |
965 | ||
966 | return 0; | |
48308681 SB |
967 | } |
968 | ||
969 | static int update_clone(int argc, const char **argv, const char *prefix) | |
970 | { | |
971 | const char *update = NULL; | |
2335b870 | 972 | int max_jobs = -1; |
48308681 SB |
973 | struct string_list_item *item; |
974 | struct pathspec pathspec; | |
975 | struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT; | |
976 | ||
977 | struct option module_update_clone_options[] = { | |
978 | OPT_STRING(0, "prefix", &prefix, | |
979 | N_("path"), | |
980 | N_("path into the working tree")), | |
981 | OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix, | |
982 | N_("path"), | |
983 | N_("path into the working tree, across nested " | |
984 | "submodule boundaries")), | |
985 | OPT_STRING(0, "update", &update, | |
986 | N_("string"), | |
987 | N_("rebase, merge, checkout or none")), | |
5f50f33e | 988 | OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"), |
48308681 SB |
989 | N_("reference repository")), |
990 | OPT_STRING(0, "depth", &suc.depth, "<depth>", | |
991 | N_("Create a shallow clone truncated to the " | |
992 | "specified number of revisions")), | |
2335b870 SB |
993 | OPT_INTEGER('j', "jobs", &max_jobs, |
994 | N_("parallel jobs")), | |
abed000a SB |
995 | OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow, |
996 | N_("whether the initial clone should follow the shallow recommendation")), | |
48308681 | 997 | OPT__QUIET(&suc.quiet, N_("don't print cloning progress")), |
72c5f883 JK |
998 | OPT_BOOL(0, "progress", &suc.progress, |
999 | N_("force cloning progress")), | |
48308681 SB |
1000 | OPT_END() |
1001 | }; | |
1002 | ||
1003 | const char *const git_submodule_helper_usage[] = { | |
1004 | N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"), | |
1005 | NULL | |
1006 | }; | |
1007 | suc.prefix = prefix; | |
1008 | ||
1009 | argc = parse_options(argc, argv, prefix, module_update_clone_options, | |
1010 | git_submodule_helper_usage, 0); | |
1011 | ||
1012 | if (update) | |
1013 | if (parse_submodule_update_strategy(update, &suc.update) < 0) | |
1014 | die(_("bad value for update parameter")); | |
1015 | ||
1016 | if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0) | |
1017 | return 1; | |
1018 | ||
1019 | if (pathspec.nr) | |
1020 | suc.warn_if_uninitialized = 1; | |
1021 | ||
1022 | /* Overlay the parsed .gitmodules file with .git/config */ | |
1023 | gitmodules_config(); | |
1024 | git_config(submodule_config, NULL); | |
1025 | ||
2335b870 SB |
1026 | if (max_jobs < 0) |
1027 | max_jobs = parallel_submodules(); | |
1028 | ||
1029 | run_processes_parallel(max_jobs, | |
48308681 SB |
1030 | update_clone_get_next_task, |
1031 | update_clone_start_failure, | |
1032 | update_clone_task_finished, | |
1033 | &suc); | |
1034 | ||
1035 | /* | |
1036 | * We saved the output and put it out all at once now. | |
1037 | * That means: | |
1038 | * - the listener does not have to interleave their (checkout) | |
1039 | * work with our fetching. The writes involved in a | |
1040 | * checkout involve more straightforward sequential I/O. | |
1041 | * - the listener can avoid doing any work if fetching failed. | |
1042 | */ | |
1043 | if (suc.quickstop) | |
1044 | return 1; | |
1045 | ||
1046 | for_each_string_list_item(item, &suc.projectlines) | |
1047 | utf8_fprintf(stdout, "%s", item->string); | |
1048 | ||
1049 | return 0; | |
1050 | } | |
1051 | ||
44431df0 SB |
1052 | static int resolve_relative_path(int argc, const char **argv, const char *prefix) |
1053 | { | |
1054 | struct strbuf sb = STRBUF_INIT; | |
1055 | if (argc != 3) | |
2de26ae1 | 1056 | die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc); |
44431df0 SB |
1057 | |
1058 | printf("%s", relative_path(argv[1], argv[2], &sb)); | |
1059 | strbuf_release(&sb); | |
1060 | return 0; | |
1061 | } | |
1062 | ||
92bbe7cc SB |
1063 | static const char *remote_submodule_branch(const char *path) |
1064 | { | |
1065 | const struct submodule *sub; | |
1066 | gitmodules_config(); | |
1067 | git_config(submodule_config, NULL); | |
1068 | ||
1069 | sub = submodule_from_path(null_sha1, path); | |
1070 | if (!sub) | |
1071 | return NULL; | |
1072 | ||
1073 | if (!sub->branch) | |
1074 | return "master"; | |
1075 | ||
4d7bc52b SB |
1076 | if (!strcmp(sub->branch, ".")) { |
1077 | unsigned char sha1[20]; | |
1078 | const char *refname = resolve_ref_unsafe("HEAD", 0, sha1, NULL); | |
1079 | ||
1080 | if (!refname) | |
1081 | die(_("No such ref: %s"), "HEAD"); | |
1082 | ||
1083 | /* detached HEAD */ | |
1084 | if (!strcmp(refname, "HEAD")) | |
1085 | die(_("Submodule (%s) branch configured to inherit " | |
1086 | "branch from superproject, but the superproject " | |
1087 | "is not on any branch"), sub->name); | |
1088 | ||
1089 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
1090 | die(_("Expecting a full ref name, got %s"), refname); | |
1091 | return refname; | |
1092 | } | |
1093 | ||
92bbe7cc SB |
1094 | return sub->branch; |
1095 | } | |
1096 | ||
1097 | static int resolve_remote_submodule_branch(int argc, const char **argv, | |
1098 | const char *prefix) | |
1099 | { | |
1100 | const char *ret; | |
1101 | struct strbuf sb = STRBUF_INIT; | |
1102 | if (argc != 2) | |
1103 | die("submodule--helper remote-branch takes exactly one arguments, got %d", argc); | |
1104 | ||
1105 | ret = remote_submodule_branch(argv[1]); | |
1106 | if (!ret) | |
1107 | die("submodule %s doesn't exist", argv[1]); | |
1108 | ||
1109 | printf("%s", ret); | |
1110 | strbuf_release(&sb); | |
1111 | return 0; | |
1112 | } | |
1113 | ||
f6f85861 SB |
1114 | static int absorb_git_dirs(int argc, const char **argv, const char *prefix) |
1115 | { | |
1116 | int i; | |
1117 | struct pathspec pathspec; | |
1118 | struct module_list list = MODULE_LIST_INIT; | |
1119 | unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES; | |
1120 | ||
1121 | struct option embed_gitdir_options[] = { | |
1122 | OPT_STRING(0, "prefix", &prefix, | |
1123 | N_("path"), | |
1124 | N_("path into the working tree")), | |
1125 | OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"), | |
1126 | ABSORB_GITDIR_RECURSE_SUBMODULES), | |
1127 | OPT_END() | |
1128 | }; | |
1129 | ||
1130 | const char *const git_submodule_helper_usage[] = { | |
1131 | N_("git submodule--helper embed-git-dir [<path>...]"), | |
1132 | NULL | |
1133 | }; | |
1134 | ||
1135 | argc = parse_options(argc, argv, prefix, embed_gitdir_options, | |
1136 | git_submodule_helper_usage, 0); | |
1137 | ||
1138 | gitmodules_config(); | |
1139 | git_config(submodule_config, NULL); | |
1140 | ||
1141 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
1142 | return 1; | |
1143 | ||
1144 | for (i = 0; i < list.nr; i++) | |
1145 | absorb_git_dir_into_superproject(prefix, | |
1146 | list.entries[i]->name, flags); | |
1147 | ||
1148 | return 0; | |
1149 | } | |
1150 | ||
5c2bd8b7 BW |
1151 | static int is_active(int argc, const char **argv, const char *prefix) |
1152 | { | |
1153 | if (argc != 2) | |
1154 | die("submodule--helper is-active takes exactly 1 arguments"); | |
1155 | ||
1156 | gitmodules_config(); | |
1157 | ||
1158 | return !is_submodule_initialized(argv[1]); | |
1159 | } | |
1160 | ||
89c86265 SB |
1161 | #define SUPPORT_SUPER_PREFIX (1<<0) |
1162 | ||
74703a1e SB |
1163 | struct cmd_struct { |
1164 | const char *cmd; | |
1165 | int (*fn)(int, const char **, const char *); | |
89c86265 | 1166 | unsigned option; |
74703a1e SB |
1167 | }; |
1168 | ||
1169 | static struct cmd_struct commands[] = { | |
89c86265 SB |
1170 | {"list", module_list, 0}, |
1171 | {"name", module_name, 0}, | |
1172 | {"clone", module_clone, 0}, | |
1173 | {"update-clone", update_clone, 0}, | |
1174 | {"relative-path", resolve_relative_path, 0}, | |
1175 | {"resolve-relative-url", resolve_relative_url, 0}, | |
1176 | {"resolve-relative-url-test", resolve_relative_url_test, 0}, | |
6e7c14e6 | 1177 | {"init", module_init, SUPPORT_SUPER_PREFIX}, |
89c86265 | 1178 | {"remote-branch", resolve_remote_submodule_branch, 0}, |
f6f85861 | 1179 | {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX}, |
5c2bd8b7 | 1180 | {"is-active", is_active, 0}, |
74703a1e SB |
1181 | }; |
1182 | ||
1183 | int cmd_submodule__helper(int argc, const char **argv, const char *prefix) | |
1184 | { | |
1185 | int i; | |
1186 | if (argc < 2) | |
cdc04b65 | 1187 | die(_("submodule--helper subcommand must be " |
74703a1e SB |
1188 | "called with a subcommand")); |
1189 | ||
89c86265 SB |
1190 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
1191 | if (!strcmp(argv[1], commands[i].cmd)) { | |
1192 | if (get_super_prefix() && | |
1193 | !(commands[i].option & SUPPORT_SUPER_PREFIX)) | |
1194 | die(_("%s doesn't support --super-prefix"), | |
1195 | commands[i].cmd); | |
74703a1e | 1196 | return commands[i].fn(argc - 1, argv + 1, prefix); |
89c86265 SB |
1197 | } |
1198 | } | |
74703a1e | 1199 | |
cdc04b65 | 1200 | die(_("'%s' is not a valid submodule--helper " |
74703a1e SB |
1201 | "subcommand"), argv[1]); |
1202 | } |