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