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