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