]>
Commit | Line | Data |
---|---|---|
f8adbec9 | 1 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
74703a1e | 2 | #include "builtin.h" |
627d9342 | 3 | #include "repository.h" |
74703a1e | 4 | #include "cache.h" |
b2141fc1 | 5 | #include "config.h" |
74703a1e SB |
6 | #include "parse-options.h" |
7 | #include "quote.h" | |
8 | #include "pathspec.h" | |
9 | #include "dir.h" | |
0ea306ef SB |
10 | #include "submodule.h" |
11 | #include "submodule-config.h" | |
12 | #include "string-list.h" | |
ee8838d1 | 13 | #include "run-command.h" |
63e95beb SB |
14 | #include "remote.h" |
15 | #include "refs.h" | |
ec0cb496 | 16 | #include "refspec.h" |
63e95beb | 17 | #include "connect.h" |
a9f8a375 PC |
18 | #include "revision.h" |
19 | #include "diffcore.h" | |
20 | #include "diff.h" | |
0d4a1321 | 21 | #include "object-store.h" |
4f3e57ef | 22 | #include "advice.h" |
961b130d | 23 | #include "branch.h" |
f05da2b4 | 24 | #include "list-objects-filter-options.h" |
63e95beb | 25 | |
9f580a62 | 26 | #define OPT_QUIET (1 << 0) |
a9f8a375 PC |
27 | #define OPT_CACHED (1 << 1) |
28 | #define OPT_RECURSIVE (1 << 2) | |
2e612731 | 29 | #define OPT_FORCE (1 << 3) |
9f580a62 PC |
30 | |
31 | typedef void (*each_submodule_fn)(const struct cache_entry *list_item, | |
32 | void *cb_data); | |
63e95beb | 33 | |
a77c3fcb | 34 | static char *repo_get_default_remote(struct repository *repo) |
63e95beb SB |
35 | { |
36 | char *dest = NULL, *ret; | |
63e95beb | 37 | struct strbuf sb = STRBUF_INIT; |
a77c3fcb AR |
38 | struct ref_store *store = get_main_ref_store(repo); |
39 | const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL, | |
40 | NULL); | |
63e95beb SB |
41 | |
42 | if (!refname) | |
43 | die(_("No such ref: %s"), "HEAD"); | |
44 | ||
45 | /* detached HEAD */ | |
46 | if (!strcmp(refname, "HEAD")) | |
47 | return xstrdup("origin"); | |
48 | ||
49 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
50 | die(_("Expecting a full ref name, got %s"), refname); | |
51 | ||
52 | strbuf_addf(&sb, "branch.%s.remote", refname); | |
a77c3fcb | 53 | if (repo_config_get_string(repo, sb.buf, &dest)) |
63e95beb SB |
54 | ret = xstrdup("origin"); |
55 | else | |
56 | ret = dest; | |
57 | ||
58 | strbuf_release(&sb); | |
59 | return ret; | |
60 | } | |
61 | ||
a77c3fcb | 62 | static char *get_default_remote_submodule(const char *module_path) |
13424764 | 63 | { |
a77c3fcb | 64 | struct repository subrepo; |
13424764 | 65 | |
a77c3fcb AR |
66 | repo_submodule_init(&subrepo, the_repository, module_path, null_oid()); |
67 | return repo_get_default_remote(&subrepo); | |
68 | } | |
13424764 | 69 | |
a77c3fcb AR |
70 | static char *get_default_remote(void) |
71 | { | |
72 | return repo_get_default_remote(the_repository); | |
13424764 PC |
73 | } |
74 | ||
63e95beb SB |
75 | static int starts_with_dot_slash(const char *str) |
76 | { | |
77 | return str[0] == '.' && is_dir_sep(str[1]); | |
78 | } | |
79 | ||
80 | static int starts_with_dot_dot_slash(const char *str) | |
81 | { | |
82 | return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]); | |
83 | } | |
84 | ||
85 | /* | |
86 | * Returns 1 if it was the last chop before ':'. | |
87 | */ | |
88 | static int chop_last_dir(char **remoteurl, int is_relative) | |
89 | { | |
90 | char *rfind = find_last_dir_sep(*remoteurl); | |
91 | if (rfind) { | |
92 | *rfind = '\0'; | |
93 | return 0; | |
94 | } | |
95 | ||
96 | rfind = strrchr(*remoteurl, ':'); | |
97 | if (rfind) { | |
98 | *rfind = '\0'; | |
99 | return 1; | |
100 | } | |
101 | ||
102 | if (is_relative || !strcmp(".", *remoteurl)) | |
103 | die(_("cannot strip one component off url '%s'"), | |
104 | *remoteurl); | |
105 | ||
106 | free(*remoteurl); | |
107 | *remoteurl = xstrdup("."); | |
108 | return 0; | |
109 | } | |
110 | ||
111 | /* | |
112 | * The `url` argument is the URL that navigates to the submodule origin | |
113 | * repo. When relative, this URL is relative to the superproject origin | |
114 | * URL repo. The `up_path` argument, if specified, is the relative | |
115 | * path that navigates from the submodule working tree to the superproject | |
116 | * working tree. Returns the origin URL of the submodule. | |
117 | * | |
118 | * Return either an absolute URL or filesystem path (if the superproject | |
119 | * origin URL is an absolute URL or filesystem path, respectively) or a | |
120 | * relative file system path (if the superproject origin URL is a relative | |
121 | * file system path). | |
122 | * | |
123 | * When the output is a relative file system path, the path is either | |
124 | * relative to the submodule working tree, if up_path is specified, or to | |
125 | * the superproject working tree otherwise. | |
126 | * | |
127 | * NEEDSWORK: This works incorrectly on the domain and protocol part. | |
128 | * remote_url url outcome expectation | |
129 | * http://a.com/b ../c http://a.com/c as is | |
08788504 SB |
130 | * http://a.com/b/ ../c http://a.com/c same as previous line, but |
131 | * ignore trailing slash in url | |
63e95beb SB |
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 http:c error out | |
135 | * http://a.com/b ../../../../../c .:c error out | |
136 | * NEEDSWORK: Given how chop_last_dir() works, this function is broken | |
137 | * when a local part has a colon in its path component, too. | |
138 | */ | |
139 | static char *relative_url(const char *remote_url, | |
140 | const char *url, | |
141 | const char *up_path) | |
142 | { | |
143 | int is_relative = 0; | |
144 | int colonsep = 0; | |
145 | char *out; | |
146 | char *remoteurl = xstrdup(remote_url); | |
147 | struct strbuf sb = STRBUF_INIT; | |
148 | size_t len = strlen(remoteurl); | |
149 | ||
08788504 SB |
150 | if (is_dir_sep(remoteurl[len-1])) |
151 | remoteurl[len-1] = '\0'; | |
63e95beb SB |
152 | |
153 | if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl)) | |
154 | is_relative = 0; | |
155 | else { | |
156 | is_relative = 1; | |
157 | /* | |
158 | * Prepend a './' to ensure all relative | |
159 | * remoteurls start with './' or '../' | |
160 | */ | |
161 | if (!starts_with_dot_slash(remoteurl) && | |
162 | !starts_with_dot_dot_slash(remoteurl)) { | |
163 | strbuf_reset(&sb); | |
164 | strbuf_addf(&sb, "./%s", remoteurl); | |
165 | free(remoteurl); | |
166 | remoteurl = strbuf_detach(&sb, NULL); | |
167 | } | |
168 | } | |
169 | /* | |
170 | * When the url starts with '../', remove that and the | |
171 | * last directory in remoteurl. | |
172 | */ | |
173 | while (url) { | |
174 | if (starts_with_dot_dot_slash(url)) { | |
175 | url += 3; | |
176 | colonsep |= chop_last_dir(&remoteurl, is_relative); | |
177 | } else if (starts_with_dot_slash(url)) | |
178 | url += 2; | |
179 | else | |
180 | break; | |
181 | } | |
182 | strbuf_reset(&sb); | |
183 | strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url); | |
3389e78e SB |
184 | if (ends_with(url, "/")) |
185 | strbuf_setlen(&sb, sb.len - 1); | |
63e95beb SB |
186 | free(remoteurl); |
187 | ||
188 | if (starts_with_dot_slash(sb.buf)) | |
189 | out = xstrdup(sb.buf + 2); | |
190 | else | |
191 | out = xstrdup(sb.buf); | |
63e95beb | 192 | |
edfc7449 AH |
193 | if (!up_path || !is_relative) { |
194 | strbuf_release(&sb); | |
63e95beb | 195 | return out; |
edfc7449 | 196 | } |
63e95beb | 197 | |
edfc7449 | 198 | strbuf_reset(&sb); |
63e95beb SB |
199 | strbuf_addf(&sb, "%s%s", up_path, out); |
200 | free(out); | |
201 | return strbuf_detach(&sb, NULL); | |
202 | } | |
203 | ||
de0fcbe0 | 204 | static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet) |
63e95beb | 205 | { |
ab6f23b7 | 206 | char *remoteurl, *resolved_url; |
63e95beb | 207 | char *remote = get_default_remote(); |
ab6f23b7 | 208 | struct strbuf remotesb = STRBUF_INIT; |
63e95beb | 209 | |
ab6f23b7 AR |
210 | strbuf_addf(&remotesb, "remote.%s.url", remote); |
211 | if (git_config_get_string(remotesb.buf, &remoteurl)) { | |
212 | if (!quiet) | |
213 | warning(_("could not look up configuration '%s'. " | |
214 | "Assuming this repository is its own " | |
215 | "authoritative upstream."), | |
216 | remotesb.buf); | |
63e95beb | 217 | remoteurl = xgetcwd(); |
ab6f23b7 AR |
218 | } |
219 | resolved_url = relative_url(remoteurl, rel_url, up_path); | |
63e95beb | 220 | |
ab6f23b7 | 221 | free(remote); |
63e95beb | 222 | free(remoteurl); |
ab6f23b7 AR |
223 | strbuf_release(&remotesb); |
224 | ||
225 | return resolved_url; | |
63e95beb SB |
226 | } |
227 | ||
228 | static int resolve_relative_url_test(int argc, const char **argv, const char *prefix) | |
229 | { | |
230 | char *remoteurl, *res; | |
231 | const char *up_path, *url; | |
232 | ||
233 | if (argc != 4) | |
234 | die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>"); | |
235 | ||
236 | up_path = argv[1]; | |
237 | remoteurl = xstrdup(argv[2]); | |
238 | url = argv[3]; | |
239 | ||
240 | if (!strcmp(up_path, "(null)")) | |
241 | up_path = NULL; | |
242 | ||
243 | res = relative_url(remoteurl, url, up_path); | |
244 | puts(res); | |
245 | free(res); | |
246 | free(remoteurl); | |
247 | return 0; | |
248 | } | |
74703a1e | 249 | |
5312a850 AR |
250 | static char *do_get_submodule_displaypath(const char *path, |
251 | const char *prefix, | |
252 | const char *super_prefix) | |
74a10642 | 253 | { |
74a10642 PC |
254 | if (prefix && super_prefix) { |
255 | BUG("cannot have prefix '%s' and superprefix '%s'", | |
256 | prefix, super_prefix); | |
257 | } else if (prefix) { | |
258 | struct strbuf sb = STRBUF_INIT; | |
259 | char *displaypath = xstrdup(relative_path(path, prefix, &sb)); | |
260 | strbuf_release(&sb); | |
261 | return displaypath; | |
262 | } else if (super_prefix) { | |
263 | return xstrfmt("%s%s", super_prefix, path); | |
264 | } else { | |
265 | return xstrdup(path); | |
266 | } | |
267 | } | |
268 | ||
5312a850 AR |
269 | /* the result should be freed by the caller. */ |
270 | static char *get_submodule_displaypath(const char *path, const char *prefix) | |
271 | { | |
272 | const char *super_prefix = get_super_prefix(); | |
273 | return do_get_submodule_displaypath(path, prefix, super_prefix); | |
274 | } | |
275 | ||
a9f8a375 PC |
276 | static char *compute_rev_name(const char *sub_path, const char* object_id) |
277 | { | |
278 | struct strbuf sb = STRBUF_INIT; | |
279 | const char ***d; | |
280 | ||
281 | static const char *describe_bare[] = { NULL }; | |
282 | ||
283 | static const char *describe_tags[] = { "--tags", NULL }; | |
284 | ||
285 | static const char *describe_contains[] = { "--contains", NULL }; | |
286 | ||
287 | static const char *describe_all_always[] = { "--all", "--always", NULL }; | |
288 | ||
289 | static const char **describe_argv[] = { describe_bare, describe_tags, | |
290 | describe_contains, | |
291 | describe_all_always, NULL }; | |
292 | ||
293 | for (d = describe_argv; *d; d++) { | |
294 | struct child_process cp = CHILD_PROCESS_INIT; | |
295 | prepare_submodule_repo_env(&cp.env_array); | |
296 | cp.dir = sub_path; | |
297 | cp.git_cmd = 1; | |
298 | cp.no_stderr = 1; | |
299 | ||
22f9b7f3 JK |
300 | strvec_push(&cp.args, "describe"); |
301 | strvec_pushv(&cp.args, *d); | |
302 | strvec_push(&cp.args, object_id); | |
a9f8a375 PC |
303 | |
304 | if (!capture_command(&cp, &sb, 0)) { | |
305 | strbuf_strip_suffix(&sb, "\n"); | |
306 | return strbuf_detach(&sb, NULL); | |
307 | } | |
308 | } | |
309 | ||
310 | strbuf_release(&sb); | |
311 | return NULL; | |
312 | } | |
313 | ||
74703a1e SB |
314 | struct module_list { |
315 | const struct cache_entry **entries; | |
316 | int alloc, nr; | |
317 | }; | |
9865b6e6 | 318 | #define MODULE_LIST_INIT { 0 } |
74703a1e SB |
319 | |
320 | static int module_list_compute(int argc, const char **argv, | |
321 | const char *prefix, | |
322 | struct pathspec *pathspec, | |
323 | struct module_list *list) | |
324 | { | |
325 | int i, result = 0; | |
2b56bb7a | 326 | char *ps_matched = NULL; |
74703a1e | 327 | parse_pathspec(pathspec, 0, |
2249d4db | 328 | PATHSPEC_PREFER_FULL, |
74703a1e SB |
329 | prefix, argv); |
330 | ||
74703a1e SB |
331 | if (pathspec->nr) |
332 | ps_matched = xcalloc(pathspec->nr, 1); | |
333 | ||
334 | if (read_cache() < 0) | |
335 | die(_("index file corrupt")); | |
336 | ||
337 | for (i = 0; i < active_nr; i++) { | |
338 | const struct cache_entry *ce = active_cache[i]; | |
339 | ||
6d2df284 | 340 | if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce), |
84ba959b SB |
341 | 0, ps_matched, 1) || |
342 | !S_ISGITLINK(ce->ce_mode)) | |
74703a1e SB |
343 | continue; |
344 | ||
345 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); | |
346 | list->entries[list->nr++] = ce; | |
347 | while (i + 1 < active_nr && | |
348 | !strcmp(ce->name, active_cache[i + 1]->name)) | |
349 | /* | |
350 | * Skip entries with the same name in different stages | |
351 | * to make sure an entry is returned only once. | |
352 | */ | |
353 | i++; | |
354 | } | |
74703a1e | 355 | |
c5c33504 | 356 | if (ps_matched && report_path_error(ps_matched, pathspec)) |
74703a1e SB |
357 | result = -1; |
358 | ||
359 | free(ps_matched); | |
360 | ||
361 | return result; | |
362 | } | |
363 | ||
3e7eaed0 BW |
364 | static void module_list_active(struct module_list *list) |
365 | { | |
366 | int i; | |
367 | struct module_list active_modules = MODULE_LIST_INIT; | |
368 | ||
3e7eaed0 BW |
369 | for (i = 0; i < list->nr; i++) { |
370 | const struct cache_entry *ce = list->entries[i]; | |
371 | ||
627d9342 | 372 | if (!is_submodule_active(the_repository, ce->name)) |
3e7eaed0 BW |
373 | continue; |
374 | ||
375 | ALLOC_GROW(active_modules.entries, | |
376 | active_modules.nr + 1, | |
377 | active_modules.alloc); | |
378 | active_modules.entries[active_modules.nr++] = ce; | |
379 | } | |
380 | ||
381 | free(list->entries); | |
382 | *list = active_modules; | |
383 | } | |
384 | ||
13424764 PC |
385 | static char *get_up_path(const char *path) |
386 | { | |
387 | int i; | |
388 | struct strbuf sb = STRBUF_INIT; | |
389 | ||
390 | for (i = count_slashes(path); i; i--) | |
391 | strbuf_addstr(&sb, "../"); | |
392 | ||
393 | /* | |
394 | * Check if 'path' ends with slash or not | |
395 | * for having the same output for dir/sub_dir | |
396 | * and dir/sub_dir/ | |
397 | */ | |
398 | if (!is_dir_sep(path[strlen(path) - 1])) | |
399 | strbuf_addstr(&sb, "../"); | |
400 | ||
401 | return strbuf_detach(&sb, NULL); | |
402 | } | |
403 | ||
74703a1e SB |
404 | static int module_list(int argc, const char **argv, const char *prefix) |
405 | { | |
406 | int i; | |
407 | struct pathspec pathspec; | |
408 | struct module_list list = MODULE_LIST_INIT; | |
409 | ||
410 | struct option module_list_options[] = { | |
411 | OPT_STRING(0, "prefix", &prefix, | |
412 | N_("path"), | |
413 | N_("alternative anchor for relative paths")), | |
414 | OPT_END() | |
415 | }; | |
416 | ||
417 | const char *const git_submodule_helper_usage[] = { | |
418 | N_("git submodule--helper list [--prefix=<path>] [<path>...]"), | |
419 | NULL | |
420 | }; | |
421 | ||
422 | argc = parse_options(argc, argv, prefix, module_list_options, | |
423 | git_submodule_helper_usage, 0); | |
424 | ||
b0f4b408 | 425 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
74703a1e | 426 | return 1; |
74703a1e SB |
427 | |
428 | for (i = 0; i < list.nr; i++) { | |
429 | const struct cache_entry *ce = list.entries[i]; | |
430 | ||
431 | if (ce_stage(ce)) | |
14228447 | 432 | printf("%06o %s U\t", ce->ce_mode, |
433 | oid_to_hex(null_oid())); | |
74703a1e | 434 | else |
99d1a986 | 435 | printf("%06o %s %d\t", ce->ce_mode, |
436 | oid_to_hex(&ce->oid), ce_stage(ce)); | |
74703a1e | 437 | |
dc4b4a61 | 438 | fprintf(stdout, "%s\n", ce->name); |
74703a1e SB |
439 | } |
440 | return 0; | |
441 | } | |
442 | ||
9f580a62 PC |
443 | static void for_each_listed_submodule(const struct module_list *list, |
444 | each_submodule_fn fn, void *cb_data) | |
445 | { | |
446 | int i; | |
447 | for (i = 0; i < list->nr; i++) | |
448 | fn(list->entries[i], cb_data); | |
449 | } | |
450 | ||
d00a5bdd | 451 | struct foreach_cb { |
fc1b9243 PC |
452 | int argc; |
453 | const char **argv; | |
454 | const char *prefix; | |
455 | int quiet; | |
456 | int recursive; | |
457 | }; | |
d00a5bdd | 458 | #define FOREACH_CB_INIT { 0 } |
fc1b9243 PC |
459 | |
460 | static void runcommand_in_submodule_cb(const struct cache_entry *list_item, | |
461 | void *cb_data) | |
462 | { | |
d00a5bdd | 463 | struct foreach_cb *info = cb_data; |
fc1b9243 PC |
464 | const char *path = list_item->name; |
465 | const struct object_id *ce_oid = &list_item->oid; | |
466 | ||
467 | const struct submodule *sub; | |
468 | struct child_process cp = CHILD_PROCESS_INIT; | |
469 | char *displaypath; | |
470 | ||
471 | displaypath = get_submodule_displaypath(path, info->prefix); | |
472 | ||
14228447 | 473 | sub = submodule_from_path(the_repository, null_oid(), path); |
fc1b9243 PC |
474 | |
475 | if (!sub) | |
476 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
477 | displaypath); | |
478 | ||
479 | if (!is_submodule_populated_gently(path, NULL)) | |
480 | goto cleanup; | |
481 | ||
482 | prepare_submodule_repo_env(&cp.env_array); | |
483 | ||
484 | /* | |
485 | * For the purpose of executing <command> in the submodule, | |
486 | * separate shell is used for the purpose of running the | |
487 | * child process. | |
488 | */ | |
489 | cp.use_shell = 1; | |
490 | cp.dir = path; | |
491 | ||
492 | /* | |
493 | * NEEDSWORK: the command currently has access to the variables $name, | |
494 | * $sm_path, $displaypath, $sha1 and $toplevel only when the command | |
495 | * contains a single argument. This is done for maintaining a faithful | |
496 | * translation from shell script. | |
497 | */ | |
498 | if (info->argc == 1) { | |
499 | char *toplevel = xgetcwd(); | |
500 | struct strbuf sb = STRBUF_INIT; | |
501 | ||
22f9b7f3 JK |
502 | strvec_pushf(&cp.env_array, "name=%s", sub->name); |
503 | strvec_pushf(&cp.env_array, "sm_path=%s", path); | |
504 | strvec_pushf(&cp.env_array, "displaypath=%s", displaypath); | |
505 | strvec_pushf(&cp.env_array, "sha1=%s", | |
f6d8942b | 506 | oid_to_hex(ce_oid)); |
22f9b7f3 | 507 | strvec_pushf(&cp.env_array, "toplevel=%s", toplevel); |
fc1b9243 PC |
508 | |
509 | /* | |
510 | * Since the path variable was accessible from the script | |
511 | * before porting, it is also made available after porting. | |
512 | * The environment variable "PATH" has a very special purpose | |
513 | * on windows. And since environment variables are | |
514 | * case-insensitive in windows, it interferes with the | |
515 | * existing PATH variable. Hence, to avoid that, we expose | |
22f9b7f3 | 516 | * path via the args strvec and not via env_array. |
fc1b9243 PC |
517 | */ |
518 | sq_quote_buf(&sb, path); | |
22f9b7f3 | 519 | strvec_pushf(&cp.args, "path=%s; %s", |
f6d8942b | 520 | sb.buf, info->argv[0]); |
fc1b9243 PC |
521 | strbuf_release(&sb); |
522 | free(toplevel); | |
523 | } else { | |
22f9b7f3 | 524 | strvec_pushv(&cp.args, info->argv); |
fc1b9243 PC |
525 | } |
526 | ||
527 | if (!info->quiet) | |
528 | printf(_("Entering '%s'\n"), displaypath); | |
529 | ||
530 | if (info->argv[0] && run_command(&cp)) | |
531 | die(_("run_command returned non-zero status for %s\n."), | |
532 | displaypath); | |
533 | ||
534 | if (info->recursive) { | |
535 | struct child_process cpr = CHILD_PROCESS_INIT; | |
536 | ||
537 | cpr.git_cmd = 1; | |
538 | cpr.dir = path; | |
539 | prepare_submodule_repo_env(&cpr.env_array); | |
540 | ||
22f9b7f3 JK |
541 | strvec_pushl(&cpr.args, "--super-prefix", NULL); |
542 | strvec_pushf(&cpr.args, "%s/", displaypath); | |
543 | strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive", | |
f6d8942b | 544 | NULL); |
fc1b9243 PC |
545 | |
546 | if (info->quiet) | |
22f9b7f3 | 547 | strvec_push(&cpr.args, "--quiet"); |
fc1b9243 | 548 | |
22f9b7f3 JK |
549 | strvec_push(&cpr.args, "--"); |
550 | strvec_pushv(&cpr.args, info->argv); | |
fc1b9243 PC |
551 | |
552 | if (run_command(&cpr)) | |
27c929ed | 553 | die(_("run_command returned non-zero status while " |
fc1b9243 PC |
554 | "recursing in the nested submodules of %s\n."), |
555 | displaypath); | |
556 | } | |
557 | ||
558 | cleanup: | |
559 | free(displaypath); | |
560 | } | |
561 | ||
562 | static int module_foreach(int argc, const char **argv, const char *prefix) | |
563 | { | |
d00a5bdd | 564 | struct foreach_cb info = FOREACH_CB_INIT; |
fc1b9243 PC |
565 | struct pathspec pathspec; |
566 | struct module_list list = MODULE_LIST_INIT; | |
567 | ||
568 | struct option module_foreach_options[] = { | |
e73fe3dd | 569 | OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")), |
fc1b9243 | 570 | OPT_BOOL(0, "recursive", &info.recursive, |
e73fe3dd | 571 | N_("recurse into nested submodules")), |
fc1b9243 PC |
572 | OPT_END() |
573 | }; | |
574 | ||
575 | const char *const git_submodule_helper_usage[] = { | |
a282f5a9 | 576 | N_("git submodule--helper foreach [--quiet] [--recursive] [--] <command>"), |
fc1b9243 PC |
577 | NULL |
578 | }; | |
579 | ||
580 | argc = parse_options(argc, argv, prefix, module_foreach_options, | |
a282f5a9 | 581 | git_submodule_helper_usage, 0); |
fc1b9243 PC |
582 | |
583 | if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0) | |
584 | return 1; | |
585 | ||
586 | info.argc = argc; | |
587 | info.argv = argv; | |
588 | info.prefix = prefix; | |
589 | ||
590 | for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info); | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
9f580a62 PC |
595 | struct init_cb { |
596 | const char *prefix; | |
3ce52cba | 597 | const char *superprefix; |
9f580a62 PC |
598 | unsigned int flags; |
599 | }; | |
9865b6e6 | 600 | #define INIT_CB_INIT { 0 } |
9f580a62 PC |
601 | |
602 | static void init_submodule(const char *path, const char *prefix, | |
3ce52cba | 603 | const char *superprefix, unsigned int flags) |
3604242f SB |
604 | { |
605 | const struct submodule *sub; | |
606 | struct strbuf sb = STRBUF_INIT; | |
607 | char *upd = NULL, *url = NULL, *displaypath; | |
608 | ||
3ce52cba AR |
609 | /* try superprefix from the environment, if it is not passed explicitly */ |
610 | if (!superprefix) | |
611 | superprefix = get_super_prefix(); | |
612 | displaypath = do_get_submodule_displaypath(path, prefix, superprefix); | |
d92028a5 | 613 | |
14228447 | 614 | sub = submodule_from_path(the_repository, null_oid(), path); |
d92028a5 SB |
615 | |
616 | if (!sub) | |
617 | die(_("No url found for submodule path '%s' in .gitmodules"), | |
618 | displaypath); | |
3604242f | 619 | |
1f8d7115 BW |
620 | /* |
621 | * NEEDSWORK: In a multi-working-tree world, this needs to be | |
622 | * set in the per-worktree config. | |
623 | * | |
624 | * Set active flag for the submodule being initialized | |
625 | */ | |
627d9342 | 626 | if (!is_submodule_active(the_repository, path)) { |
1f8d7115 BW |
627 | strbuf_addf(&sb, "submodule.%s.active", sub->name); |
628 | git_config_set_gently(sb.buf, "true"); | |
74a10642 | 629 | strbuf_reset(&sb); |
1f8d7115 BW |
630 | } |
631 | ||
3604242f SB |
632 | /* |
633 | * Copy url setting when it is not set yet. | |
634 | * To look up the url in .git/config, we must not fall back to | |
635 | * .gitmodules, so look it up directly. | |
636 | */ | |
3604242f SB |
637 | strbuf_addf(&sb, "submodule.%s.url", sub->name); |
638 | if (git_config_get_string(sb.buf, &url)) { | |
627fde10 | 639 | if (!sub->url) |
3604242f SB |
640 | die(_("No url found for submodule path '%s' in .gitmodules"), |
641 | displaypath); | |
642 | ||
627fde10 JK |
643 | url = xstrdup(sub->url); |
644 | ||
3604242f SB |
645 | /* Possibly a url relative to parent */ |
646 | if (starts_with_dot_dot_slash(url) || | |
647 | starts_with_dot_slash(url)) { | |
e0a862fd | 648 | char *oldurl = url; |
de0fcbe0 | 649 | url = resolve_relative_url(oldurl, NULL, 0); |
e0a862fd | 650 | free(oldurl); |
3604242f SB |
651 | } |
652 | ||
653 | if (git_config_set_gently(sb.buf, url)) | |
654 | die(_("Failed to register url for submodule path '%s'"), | |
655 | displaypath); | |
9f580a62 | 656 | if (!(flags & OPT_QUIET)) |
c66410ed SB |
657 | fprintf(stderr, |
658 | _("Submodule '%s' (%s) registered for path '%s'\n"), | |
3604242f SB |
659 | sub->name, url, displaypath); |
660 | } | |
74a10642 | 661 | strbuf_reset(&sb); |
3604242f SB |
662 | |
663 | /* Copy "update" setting when it is not set yet */ | |
3604242f SB |
664 | strbuf_addf(&sb, "submodule.%s.update", sub->name); |
665 | if (git_config_get_string(sb.buf, &upd) && | |
666 | sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
667 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) { | |
668 | fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"), | |
669 | sub->name); | |
670 | upd = xstrdup("none"); | |
671 | } else | |
672 | upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy)); | |
673 | ||
674 | if (git_config_set_gently(sb.buf, upd)) | |
675 | die(_("Failed to register update mode for submodule path '%s'"), displaypath); | |
676 | } | |
677 | strbuf_release(&sb); | |
678 | free(displaypath); | |
679 | free(url); | |
680 | free(upd); | |
681 | } | |
682 | ||
9f580a62 PC |
683 | static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data) |
684 | { | |
685 | struct init_cb *info = cb_data; | |
3ce52cba | 686 | init_submodule(list_item->name, info->prefix, info->superprefix, info->flags); |
9f580a62 PC |
687 | } |
688 | ||
3604242f SB |
689 | static int module_init(int argc, const char **argv, const char *prefix) |
690 | { | |
9f580a62 | 691 | struct init_cb info = INIT_CB_INIT; |
3604242f SB |
692 | struct pathspec pathspec; |
693 | struct module_list list = MODULE_LIST_INIT; | |
694 | int quiet = 0; | |
3604242f SB |
695 | |
696 | struct option module_init_options[] = { | |
e73fe3dd | 697 | OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")), |
3604242f SB |
698 | OPT_END() |
699 | }; | |
700 | ||
701 | const char *const git_submodule_helper_usage[] = { | |
a282f5a9 | 702 | N_("git submodule--helper init [<options>] [<path>]"), |
3604242f SB |
703 | NULL |
704 | }; | |
705 | ||
706 | argc = parse_options(argc, argv, prefix, module_init_options, | |
707 | git_submodule_helper_usage, 0); | |
708 | ||
709 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
710 | return 1; | |
711 | ||
3e7eaed0 BW |
712 | /* |
713 | * If there are no path args and submodule.active is set then, | |
714 | * by default, only initialize 'active' modules. | |
715 | */ | |
716 | if (!argc && git_config_get_value_multi("submodule.active")) | |
717 | module_list_active(&list); | |
718 | ||
9f580a62 PC |
719 | info.prefix = prefix; |
720 | if (quiet) | |
721 | info.flags |= OPT_QUIET; | |
722 | ||
723 | for_each_listed_submodule(&list, init_submodule_cb, &info); | |
3604242f SB |
724 | |
725 | return 0; | |
726 | } | |
727 | ||
a9f8a375 PC |
728 | struct status_cb { |
729 | const char *prefix; | |
730 | unsigned int flags; | |
731 | }; | |
9865b6e6 | 732 | #define STATUS_CB_INIT { 0 } |
a9f8a375 PC |
733 | |
734 | static void print_status(unsigned int flags, char state, const char *path, | |
735 | const struct object_id *oid, const char *displaypath) | |
736 | { | |
737 | if (flags & OPT_QUIET) | |
738 | return; | |
739 | ||
740 | printf("%c%s %s", state, oid_to_hex(oid), displaypath); | |
741 | ||
0b5e2ea7 NTND |
742 | if (state == ' ' || state == '+') { |
743 | const char *name = compute_rev_name(path, oid_to_hex(oid)); | |
744 | ||
745 | if (name) | |
746 | printf(" (%s)", name); | |
747 | } | |
a9f8a375 PC |
748 | |
749 | printf("\n"); | |
750 | } | |
751 | ||
752 | static int handle_submodule_head_ref(const char *refname, | |
753 | const struct object_id *oid, int flags, | |
754 | void *cb_data) | |
755 | { | |
756 | struct object_id *output = cb_data; | |
757 | if (oid) | |
758 | oidcpy(output, oid); | |
759 | ||
760 | return 0; | |
761 | } | |
762 | ||
763 | static void status_submodule(const char *path, const struct object_id *ce_oid, | |
764 | unsigned int ce_flags, const char *prefix, | |
765 | unsigned int flags) | |
766 | { | |
767 | char *displaypath; | |
22f9b7f3 | 768 | struct strvec diff_files_args = STRVEC_INIT; |
a9f8a375 PC |
769 | struct rev_info rev; |
770 | int diff_files_result; | |
3b2885ec PK |
771 | struct strbuf buf = STRBUF_INIT; |
772 | const char *git_dir; | |
a9f8a375 | 773 | |
14228447 | 774 | if (!submodule_from_path(the_repository, null_oid(), path)) |
a9f8a375 PC |
775 | die(_("no submodule mapping found in .gitmodules for path '%s'"), |
776 | path); | |
777 | ||
778 | displaypath = get_submodule_displaypath(path, prefix); | |
779 | ||
780 | if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) { | |
14228447 | 781 | print_status(flags, 'U', path, null_oid(), displaypath); |
a9f8a375 PC |
782 | goto cleanup; |
783 | } | |
784 | ||
3b2885ec PK |
785 | strbuf_addf(&buf, "%s/.git", path); |
786 | git_dir = read_gitfile(buf.buf); | |
787 | if (!git_dir) | |
788 | git_dir = buf.buf; | |
789 | ||
790 | if (!is_submodule_active(the_repository, path) || | |
791 | !is_git_directory(git_dir)) { | |
a9f8a375 | 792 | print_status(flags, '-', path, ce_oid, displaypath); |
3b2885ec | 793 | strbuf_release(&buf); |
a9f8a375 PC |
794 | goto cleanup; |
795 | } | |
3b2885ec | 796 | strbuf_release(&buf); |
a9f8a375 | 797 | |
22f9b7f3 | 798 | strvec_pushl(&diff_files_args, "diff-files", |
f6d8942b JK |
799 | "--ignore-submodules=dirty", "--quiet", "--", |
800 | path, NULL); | |
a9f8a375 PC |
801 | |
802 | git_config(git_diff_basic_config, NULL); | |
1f3aea22 MG |
803 | |
804 | repo_init_revisions(the_repository, &rev, NULL); | |
a9f8a375 | 805 | rev.abbrev = 0; |
d70a9eb6 JK |
806 | diff_files_args.nr = setup_revisions(diff_files_args.nr, |
807 | diff_files_args.v, | |
808 | &rev, NULL); | |
a9f8a375 PC |
809 | diff_files_result = run_diff_files(&rev, 0); |
810 | ||
811 | if (!diff_result_code(&rev.diffopt, diff_files_result)) { | |
812 | print_status(flags, ' ', path, ce_oid, | |
813 | displaypath); | |
814 | } else if (!(flags & OPT_CACHED)) { | |
815 | struct object_id oid; | |
74b6bda3 | 816 | struct ref_store *refs = get_submodule_ref_store(path); |
a9f8a375 | 817 | |
74b6bda3 RS |
818 | if (!refs) { |
819 | print_status(flags, '-', path, ce_oid, displaypath); | |
820 | goto cleanup; | |
821 | } | |
822 | if (refs_head_ref(refs, handle_submodule_head_ref, &oid)) | |
ed5bdd5b | 823 | die(_("could not resolve HEAD ref inside the " |
a9f8a375 PC |
824 | "submodule '%s'"), path); |
825 | ||
826 | print_status(flags, '+', path, &oid, displaypath); | |
827 | } else { | |
828 | print_status(flags, '+', path, ce_oid, displaypath); | |
829 | } | |
830 | ||
831 | if (flags & OPT_RECURSIVE) { | |
832 | struct child_process cpr = CHILD_PROCESS_INIT; | |
833 | ||
834 | cpr.git_cmd = 1; | |
835 | cpr.dir = path; | |
836 | prepare_submodule_repo_env(&cpr.env_array); | |
837 | ||
22f9b7f3 JK |
838 | strvec_push(&cpr.args, "--super-prefix"); |
839 | strvec_pushf(&cpr.args, "%s/", displaypath); | |
840 | strvec_pushl(&cpr.args, "submodule--helper", "status", | |
f6d8942b | 841 | "--recursive", NULL); |
a9f8a375 PC |
842 | |
843 | if (flags & OPT_CACHED) | |
22f9b7f3 | 844 | strvec_push(&cpr.args, "--cached"); |
a9f8a375 PC |
845 | |
846 | if (flags & OPT_QUIET) | |
22f9b7f3 | 847 | strvec_push(&cpr.args, "--quiet"); |
a9f8a375 PC |
848 | |
849 | if (run_command(&cpr)) | |
850 | die(_("failed to recurse into submodule '%s'"), path); | |
851 | } | |
852 | ||
853 | cleanup: | |
22f9b7f3 | 854 | strvec_clear(&diff_files_args); |
a9f8a375 PC |
855 | free(displaypath); |
856 | } | |
857 | ||
858 | static void status_submodule_cb(const struct cache_entry *list_item, | |
859 | void *cb_data) | |
860 | { | |
861 | struct status_cb *info = cb_data; | |
862 | status_submodule(list_item->name, &list_item->oid, list_item->ce_flags, | |
863 | info->prefix, info->flags); | |
864 | } | |
865 | ||
866 | static int module_status(int argc, const char **argv, const char *prefix) | |
867 | { | |
868 | struct status_cb info = STATUS_CB_INIT; | |
869 | struct pathspec pathspec; | |
870 | struct module_list list = MODULE_LIST_INIT; | |
871 | int quiet = 0; | |
872 | ||
873 | struct option module_status_options[] = { | |
e73fe3dd ZH |
874 | OPT__QUIET(&quiet, N_("suppress submodule status output")), |
875 | 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 |
876 | OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE), |
877 | OPT_END() | |
878 | }; | |
879 | ||
880 | const char *const git_submodule_helper_usage[] = { | |
881 | N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"), | |
882 | NULL | |
883 | }; | |
884 | ||
885 | argc = parse_options(argc, argv, prefix, module_status_options, | |
886 | git_submodule_helper_usage, 0); | |
887 | ||
888 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
889 | return 1; | |
890 | ||
891 | info.prefix = prefix; | |
892 | if (quiet) | |
893 | info.flags |= OPT_QUIET; | |
894 | ||
895 | for_each_listed_submodule(&list, status_submodule_cb, &info); | |
3604242f SB |
896 | |
897 | return 0; | |
898 | } | |
899 | ||
0ea306ef SB |
900 | static int module_name(int argc, const char **argv, const char *prefix) |
901 | { | |
902 | const struct submodule *sub; | |
903 | ||
904 | if (argc != 2) | |
905 | usage(_("git submodule--helper name <path>")); | |
906 | ||
14228447 | 907 | sub = submodule_from_path(the_repository, null_oid(), argv[1]); |
0ea306ef SB |
908 | |
909 | if (!sub) | |
910 | die(_("no submodule mapping found in .gitmodules for path '%s'"), | |
911 | argv[1]); | |
912 | ||
913 | printf("%s\n", sub->name); | |
914 | ||
915 | return 0; | |
916 | } | |
14111fc4 | 917 | |
e83e3333 PC |
918 | struct module_cb { |
919 | unsigned int mod_src; | |
920 | unsigned int mod_dst; | |
921 | struct object_id oid_src; | |
922 | struct object_id oid_dst; | |
923 | char status; | |
924 | const char *sm_path; | |
925 | }; | |
9865b6e6 | 926 | #define MODULE_CB_INIT { 0 } |
e83e3333 PC |
927 | |
928 | struct module_cb_list { | |
929 | struct module_cb **entries; | |
930 | int alloc, nr; | |
931 | }; | |
9865b6e6 | 932 | #define MODULE_CB_LIST_INIT { 0 } |
e83e3333 PC |
933 | |
934 | struct summary_cb { | |
935 | int argc; | |
936 | const char **argv; | |
937 | const char *prefix; | |
938 | unsigned int cached: 1; | |
939 | unsigned int for_status: 1; | |
940 | unsigned int files: 1; | |
941 | int summary_limit; | |
942 | }; | |
9865b6e6 | 943 | #define SUMMARY_CB_INIT { 0 } |
e83e3333 PC |
944 | |
945 | enum diff_cmd { | |
946 | DIFF_INDEX, | |
947 | DIFF_FILES | |
948 | }; | |
949 | ||
f0c6b646 | 950 | static char *verify_submodule_committish(const char *sm_path, |
e83e3333 PC |
951 | const char *committish) |
952 | { | |
953 | struct child_process cp_rev_parse = CHILD_PROCESS_INIT; | |
954 | struct strbuf result = STRBUF_INIT; | |
955 | ||
956 | cp_rev_parse.git_cmd = 1; | |
957 | cp_rev_parse.dir = sm_path; | |
958 | prepare_submodule_repo_env(&cp_rev_parse.env_array); | |
959 | strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL); | |
960 | strvec_pushf(&cp_rev_parse.args, "%s^0", committish); | |
961 | strvec_push(&cp_rev_parse.args, "--"); | |
962 | ||
963 | if (capture_command(&cp_rev_parse, &result, 0)) | |
964 | return NULL; | |
965 | ||
966 | strbuf_trim_trailing_newline(&result); | |
967 | return strbuf_detach(&result, NULL); | |
968 | } | |
969 | ||
f0c6b646 | 970 | static void print_submodule_summary(struct summary_cb *info, char *errmsg, |
e83e3333 PC |
971 | int total_commits, const char *displaypath, |
972 | const char *src_abbrev, const char *dst_abbrev, | |
e83e3333 PC |
973 | struct module_cb *p) |
974 | { | |
975 | if (p->status == 'T') { | |
976 | if (S_ISGITLINK(p->mod_dst)) | |
977 | printf(_("* %s %s(blob)->%s(submodule)"), | |
978 | displaypath, src_abbrev, dst_abbrev); | |
979 | else | |
980 | printf(_("* %s %s(submodule)->%s(blob)"), | |
981 | displaypath, src_abbrev, dst_abbrev); | |
982 | } else { | |
983 | printf("* %s %s...%s", | |
984 | displaypath, src_abbrev, dst_abbrev); | |
985 | } | |
986 | ||
987 | if (total_commits < 0) | |
988 | printf(":\n"); | |
989 | else | |
990 | printf(" (%d):\n", total_commits); | |
991 | ||
992 | if (errmsg) { | |
993 | printf(_("%s"), errmsg); | |
994 | } else if (total_commits > 0) { | |
995 | struct child_process cp_log = CHILD_PROCESS_INIT; | |
996 | ||
997 | cp_log.git_cmd = 1; | |
998 | cp_log.dir = p->sm_path; | |
999 | prepare_submodule_repo_env(&cp_log.env_array); | |
1000 | strvec_pushl(&cp_log.args, "log", NULL); | |
1001 | ||
1002 | if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) { | |
1003 | if (info->summary_limit > 0) | |
1004 | strvec_pushf(&cp_log.args, "-%d", | |
1005 | info->summary_limit); | |
1006 | ||
1007 | strvec_pushl(&cp_log.args, "--pretty= %m %s", | |
1008 | "--first-parent", NULL); | |
1009 | strvec_pushf(&cp_log.args, "%s...%s", | |
1010 | src_abbrev, dst_abbrev); | |
1011 | } else if (S_ISGITLINK(p->mod_dst)) { | |
1012 | strvec_pushl(&cp_log.args, "--pretty= > %s", | |
1013 | "-1", dst_abbrev, NULL); | |
1014 | } else { | |
1015 | strvec_pushl(&cp_log.args, "--pretty= < %s", | |
1016 | "-1", src_abbrev, NULL); | |
1017 | } | |
1018 | run_command(&cp_log); | |
1019 | } | |
1020 | printf("\n"); | |
1021 | } | |
1022 | ||
1023 | static void generate_submodule_summary(struct summary_cb *info, | |
1024 | struct module_cb *p) | |
1025 | { | |
d79b1455 | 1026 | char *displaypath, *src_abbrev = NULL, *dst_abbrev; |
e83e3333 PC |
1027 | int missing_src = 0, missing_dst = 0; |
1028 | char *errmsg = NULL; | |
1029 | int total_commits = -1; | |
1030 | ||
14228447 | 1031 | if (!info->cached && oideq(&p->oid_dst, null_oid())) { |
e83e3333 PC |
1032 | if (S_ISGITLINK(p->mod_dst)) { |
1033 | struct ref_store *refs = get_submodule_ref_store(p->sm_path); | |
1034 | if (refs) | |
1035 | refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst); | |
1036 | } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) { | |
1037 | struct stat st; | |
1038 | int fd = open(p->sm_path, O_RDONLY); | |
1039 | ||
1040 | if (fd < 0 || fstat(fd, &st) < 0 || | |
1041 | index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB, | |
1042 | p->sm_path, 0)) | |
1043 | error(_("couldn't hash object from '%s'"), p->sm_path); | |
1044 | } else { | |
1045 | /* for a submodule removal (mode:0000000), don't warn */ | |
1046 | if (p->mod_dst) | |
f0c6b646 | 1047 | warning(_("unexpected mode %o\n"), p->mod_dst); |
e83e3333 PC |
1048 | } |
1049 | } | |
1050 | ||
1051 | if (S_ISGITLINK(p->mod_src)) { | |
d79b1455 SS |
1052 | if (p->status != 'D') |
1053 | src_abbrev = verify_submodule_committish(p->sm_path, | |
1054 | oid_to_hex(&p->oid_src)); | |
e83e3333 PC |
1055 | if (!src_abbrev) { |
1056 | missing_src = 1; | |
1057 | /* | |
1058 | * As `rev-parse` failed, we fallback to getting | |
1059 | * the abbreviated hash using oid_src. We do | |
1060 | * this as we might still need the abbreviated | |
1061 | * hash in cases like a submodule type change, etc. | |
1062 | */ | |
1063 | src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7); | |
1064 | } | |
1065 | } else { | |
1066 | /* | |
1067 | * The source does not point to a submodule. | |
1068 | * So, we fallback to getting the abbreviation using | |
1069 | * oid_src as we might still need the abbreviated | |
1070 | * hash in cases like submodule add, etc. | |
1071 | */ | |
1072 | src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7); | |
1073 | } | |
1074 | ||
1075 | if (S_ISGITLINK(p->mod_dst)) { | |
1076 | dst_abbrev = verify_submodule_committish(p->sm_path, | |
1077 | oid_to_hex(&p->oid_dst)); | |
1078 | if (!dst_abbrev) { | |
1079 | missing_dst = 1; | |
1080 | /* | |
1081 | * As `rev-parse` failed, we fallback to getting | |
1082 | * the abbreviated hash using oid_dst. We do | |
1083 | * this as we might still need the abbreviated | |
1084 | * hash in cases like a submodule type change, etc. | |
1085 | */ | |
1086 | dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7); | |
1087 | } | |
1088 | } else { | |
1089 | /* | |
1090 | * The destination does not point to a submodule. | |
1091 | * So, we fallback to getting the abbreviation using | |
1092 | * oid_dst as we might still need the abbreviated | |
1093 | * hash in cases like a submodule removal, etc. | |
1094 | */ | |
1095 | dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7); | |
1096 | } | |
1097 | ||
1098 | displaypath = get_submodule_displaypath(p->sm_path, info->prefix); | |
1099 | ||
1100 | if (!missing_src && !missing_dst) { | |
1101 | struct child_process cp_rev_list = CHILD_PROCESS_INIT; | |
1102 | struct strbuf sb_rev_list = STRBUF_INIT; | |
1103 | ||
1104 | strvec_pushl(&cp_rev_list.args, "rev-list", | |
1105 | "--first-parent", "--count", NULL); | |
1106 | if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) | |
1107 | strvec_pushf(&cp_rev_list.args, "%s...%s", | |
1108 | src_abbrev, dst_abbrev); | |
1109 | else | |
1110 | strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ? | |
1111 | src_abbrev : dst_abbrev); | |
1112 | strvec_push(&cp_rev_list.args, "--"); | |
1113 | ||
1114 | cp_rev_list.git_cmd = 1; | |
1115 | cp_rev_list.dir = p->sm_path; | |
1116 | prepare_submodule_repo_env(&cp_rev_list.env_array); | |
1117 | ||
1118 | if (!capture_command(&cp_rev_list, &sb_rev_list, 0)) | |
1119 | total_commits = atoi(sb_rev_list.buf); | |
1120 | ||
1121 | strbuf_release(&sb_rev_list); | |
1122 | } else { | |
1123 | /* | |
1124 | * Don't give error msg for modification whose dst is not | |
1125 | * submodule, i.e., deleted or changed to blob | |
1126 | */ | |
1127 | if (S_ISGITLINK(p->mod_dst)) { | |
1128 | struct strbuf errmsg_str = STRBUF_INIT; | |
1129 | if (missing_src && missing_dst) { | |
1130 | strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commits %s and %s\n", | |
1131 | displaypath, oid_to_hex(&p->oid_src), | |
1132 | oid_to_hex(&p->oid_dst)); | |
1133 | } else { | |
1134 | strbuf_addf(&errmsg_str, " Warn: %s doesn't contain commit %s\n", | |
1135 | displaypath, missing_src ? | |
1136 | oid_to_hex(&p->oid_src) : | |
1137 | oid_to_hex(&p->oid_dst)); | |
1138 | } | |
1139 | errmsg = strbuf_detach(&errmsg_str, NULL); | |
1140 | } | |
1141 | } | |
1142 | ||
1143 | print_submodule_summary(info, errmsg, total_commits, | |
1144 | displaypath, src_abbrev, | |
e0f7ae56 | 1145 | dst_abbrev, p); |
e83e3333 PC |
1146 | |
1147 | free(displaypath); | |
1148 | free(src_abbrev); | |
1149 | free(dst_abbrev); | |
1150 | } | |
1151 | ||
1152 | static void prepare_submodule_summary(struct summary_cb *info, | |
1153 | struct module_cb_list *list) | |
1154 | { | |
1155 | int i; | |
1156 | for (i = 0; i < list->nr; i++) { | |
1157 | const struct submodule *sub; | |
1158 | struct module_cb *p = list->entries[i]; | |
1159 | struct strbuf sm_gitdir = STRBUF_INIT; | |
1160 | ||
1161 | if (p->status == 'D' || p->status == 'T') { | |
1162 | generate_submodule_summary(info, p); | |
1163 | continue; | |
1164 | } | |
1165 | ||
1166 | if (info->for_status && p->status != 'A' && | |
1167 | (sub = submodule_from_path(the_repository, | |
14228447 | 1168 | null_oid(), p->sm_path))) { |
e83e3333 PC |
1169 | char *config_key = NULL; |
1170 | const char *value; | |
1171 | int ignore_all = 0; | |
1172 | ||
1173 | config_key = xstrfmt("submodule.%s.ignore", | |
1174 | sub->name); | |
bbdba3d8 | 1175 | if (!git_config_get_string_tmp(config_key, &value)) |
e83e3333 PC |
1176 | ignore_all = !strcmp(value, "all"); |
1177 | else if (sub->ignore) | |
1178 | ignore_all = !strcmp(sub->ignore, "all"); | |
1179 | ||
1180 | free(config_key); | |
1181 | if (ignore_all) | |
1182 | continue; | |
1183 | } | |
1184 | ||
1185 | /* Also show added or modified modules which are checked out */ | |
1186 | strbuf_addstr(&sm_gitdir, p->sm_path); | |
1187 | if (is_nonbare_repository_dir(&sm_gitdir)) | |
1188 | generate_submodule_summary(info, p); | |
1189 | strbuf_release(&sm_gitdir); | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | static void submodule_summary_callback(struct diff_queue_struct *q, | |
1194 | struct diff_options *options, | |
1195 | void *data) | |
1196 | { | |
1197 | int i; | |
1198 | struct module_cb_list *list = data; | |
1199 | for (i = 0; i < q->nr; i++) { | |
1200 | struct diff_filepair *p = q->queue[i]; | |
1201 | struct module_cb *temp; | |
1202 | ||
1203 | if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode)) | |
1204 | continue; | |
1205 | temp = (struct module_cb*)malloc(sizeof(struct module_cb)); | |
1206 | temp->mod_src = p->one->mode; | |
1207 | temp->mod_dst = p->two->mode; | |
1208 | temp->oid_src = p->one->oid; | |
1209 | temp->oid_dst = p->two->oid; | |
1210 | temp->status = p->status; | |
1211 | temp->sm_path = xstrdup(p->one->path); | |
1212 | ||
1213 | ALLOC_GROW(list->entries, list->nr + 1, list->alloc); | |
1214 | list->entries[list->nr++] = temp; | |
1215 | } | |
1216 | } | |
1217 | ||
1218 | static const char *get_diff_cmd(enum diff_cmd diff_cmd) | |
1219 | { | |
1220 | switch (diff_cmd) { | |
1221 | case DIFF_INDEX: return "diff-index"; | |
1222 | case DIFF_FILES: return "diff-files"; | |
1223 | default: BUG("bad diff_cmd value %d", diff_cmd); | |
1224 | } | |
1225 | } | |
1226 | ||
1227 | static int compute_summary_module_list(struct object_id *head_oid, | |
1228 | struct summary_cb *info, | |
1229 | enum diff_cmd diff_cmd) | |
1230 | { | |
1231 | struct strvec diff_args = STRVEC_INIT; | |
1232 | struct rev_info rev; | |
1233 | struct module_cb_list list = MODULE_CB_LIST_INIT; | |
1234 | ||
1235 | strvec_push(&diff_args, get_diff_cmd(diff_cmd)); | |
1236 | if (info->cached) | |
1237 | strvec_push(&diff_args, "--cached"); | |
1238 | strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL); | |
1239 | if (head_oid) | |
1240 | strvec_push(&diff_args, oid_to_hex(head_oid)); | |
1241 | strvec_push(&diff_args, "--"); | |
1242 | if (info->argc) | |
1243 | strvec_pushv(&diff_args, info->argv); | |
1244 | ||
1245 | git_config(git_diff_basic_config, NULL); | |
1246 | init_revisions(&rev, info->prefix); | |
1247 | rev.abbrev = 0; | |
5c327502 | 1248 | precompose_argv_prefix(diff_args.nr, diff_args.v, NULL); |
e83e3333 PC |
1249 | setup_revisions(diff_args.nr, diff_args.v, &rev, NULL); |
1250 | rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK; | |
1251 | rev.diffopt.format_callback = submodule_summary_callback; | |
1252 | rev.diffopt.format_callback_data = &list; | |
1253 | ||
1254 | if (!info->cached) { | |
1255 | if (diff_cmd == DIFF_INDEX) | |
1256 | setup_work_tree(); | |
1257 | if (read_cache_preload(&rev.diffopt.pathspec) < 0) { | |
1258 | perror("read_cache_preload"); | |
1259 | return -1; | |
1260 | } | |
1261 | } else if (read_cache() < 0) { | |
1262 | perror("read_cache"); | |
1263 | return -1; | |
1264 | } | |
1265 | ||
1266 | if (diff_cmd == DIFF_INDEX) | |
1267 | run_diff_index(&rev, info->cached); | |
1268 | else | |
1269 | run_diff_files(&rev, 0); | |
1270 | prepare_submodule_summary(info, &list); | |
1271 | strvec_clear(&diff_args); | |
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | static int module_summary(int argc, const char **argv, const char *prefix) | |
1276 | { | |
1277 | struct summary_cb info = SUMMARY_CB_INIT; | |
1278 | int cached = 0; | |
1279 | int for_status = 0; | |
1280 | int files = 0; | |
1281 | int summary_limit = -1; | |
1282 | enum diff_cmd diff_cmd = DIFF_INDEX; | |
1283 | struct object_id head_oid; | |
1284 | int ret; | |
1285 | ||
1286 | struct option module_summary_options[] = { | |
1287 | OPT_BOOL(0, "cached", &cached, | |
1288 | N_("use the commit stored in the index instead of the submodule HEAD")), | |
1289 | OPT_BOOL(0, "files", &files, | |
f5f5a61d | 1290 | N_("compare the commit in the index with that in the submodule HEAD")), |
e83e3333 PC |
1291 | OPT_BOOL(0, "for-status", &for_status, |
1292 | N_("skip submodules with 'ignore_config' value set to 'all'")), | |
1293 | OPT_INTEGER('n', "summary-limit", &summary_limit, | |
1294 | N_("limit the summary size")), | |
1295 | OPT_END() | |
1296 | }; | |
1297 | ||
1298 | const char *const git_submodule_helper_usage[] = { | |
9f443f55 | 1299 | N_("git submodule--helper summary [<options>] [<commit>] [--] [<path>]"), |
e83e3333 PC |
1300 | NULL |
1301 | }; | |
1302 | ||
1303 | argc = parse_options(argc, argv, prefix, module_summary_options, | |
1304 | git_submodule_helper_usage, 0); | |
1305 | ||
1306 | if (!summary_limit) | |
1307 | return 0; | |
1308 | ||
1309 | if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) { | |
1310 | if (argc) { | |
1311 | argv++; | |
1312 | argc--; | |
1313 | } | |
1314 | } else if (!argc || !strcmp(argv[0], "HEAD")) { | |
1315 | /* before the first commit: compare with an empty tree */ | |
1316 | oidcpy(&head_oid, the_hash_algo->empty_tree); | |
1317 | if (argc) { | |
1318 | argv++; | |
1319 | argc--; | |
1320 | } | |
1321 | } else { | |
1322 | if (get_oid("HEAD", &head_oid)) | |
1323 | die(_("could not fetch a revision for HEAD")); | |
1324 | } | |
1325 | ||
1326 | if (files) { | |
1327 | if (cached) | |
43ea635c | 1328 | die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files"); |
e83e3333 PC |
1329 | diff_cmd = DIFF_FILES; |
1330 | } | |
1331 | ||
1332 | info.argc = argc; | |
1333 | info.argv = argv; | |
1334 | info.prefix = prefix; | |
1335 | info.cached = !!cached; | |
1336 | info.files = !!files; | |
1337 | info.for_status = !!for_status; | |
1338 | info.summary_limit = summary_limit; | |
1339 | ||
1340 | ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL, | |
1341 | &info, diff_cmd); | |
1342 | return ret; | |
1343 | } | |
1344 | ||
13424764 PC |
1345 | struct sync_cb { |
1346 | const char *prefix; | |
1347 | unsigned int flags; | |
1348 | }; | |
9865b6e6 | 1349 | #define SYNC_CB_INIT { 0 } |
13424764 PC |
1350 | |
1351 | static void sync_submodule(const char *path, const char *prefix, | |
1352 | unsigned int flags) | |
1353 | { | |
1354 | const struct submodule *sub; | |
1355 | char *remote_key = NULL; | |
a77c3fcb | 1356 | char *sub_origin_url, *super_config_url, *displaypath, *default_remote; |
13424764 | 1357 | struct strbuf sb = STRBUF_INIT; |
13424764 PC |
1358 | char *sub_config_path = NULL; |
1359 | ||
1360 | if (!is_submodule_active(the_repository, path)) | |
1361 | return; | |
1362 | ||
14228447 | 1363 | sub = submodule_from_path(the_repository, null_oid(), path); |
13424764 PC |
1364 | |
1365 | if (sub && sub->url) { | |
1366 | if (starts_with_dot_dot_slash(sub->url) || | |
1367 | starts_with_dot_slash(sub->url)) { | |
0c61041e | 1368 | char *up_path = get_up_path(path); |
de0fcbe0 AR |
1369 | sub_origin_url = resolve_relative_url(sub->url, up_path, 1); |
1370 | super_config_url = resolve_relative_url(sub->url, NULL, 1); | |
13424764 | 1371 | free(up_path); |
13424764 PC |
1372 | } else { |
1373 | sub_origin_url = xstrdup(sub->url); | |
1374 | super_config_url = xstrdup(sub->url); | |
1375 | } | |
1376 | } else { | |
1377 | sub_origin_url = xstrdup(""); | |
1378 | super_config_url = xstrdup(""); | |
1379 | } | |
1380 | ||
1381 | displaypath = get_submodule_displaypath(path, prefix); | |
1382 | ||
1383 | if (!(flags & OPT_QUIET)) | |
1384 | printf(_("Synchronizing submodule url for '%s'\n"), | |
1385 | displaypath); | |
1386 | ||
1387 | strbuf_reset(&sb); | |
1388 | strbuf_addf(&sb, "submodule.%s.url", sub->name); | |
1389 | if (git_config_set_gently(sb.buf, super_config_url)) | |
1390 | die(_("failed to register url for submodule path '%s'"), | |
1391 | displaypath); | |
1392 | ||
1393 | if (!is_submodule_populated_gently(path, NULL)) | |
1394 | goto cleanup; | |
1395 | ||
13424764 | 1396 | strbuf_reset(&sb); |
a77c3fcb AR |
1397 | default_remote = get_default_remote_submodule(path); |
1398 | if (!default_remote) | |
13424764 PC |
1399 | die(_("failed to get the default remote for submodule '%s'"), |
1400 | path); | |
1401 | ||
a77c3fcb AR |
1402 | remote_key = xstrfmt("remote.%s.url", default_remote); |
1403 | free(default_remote); | |
13424764 | 1404 | |
13424764 PC |
1405 | submodule_to_gitdir(&sb, path); |
1406 | strbuf_addstr(&sb, "/config"); | |
1407 | ||
1408 | if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url)) | |
1409 | die(_("failed to update remote for submodule '%s'"), | |
1410 | path); | |
1411 | ||
1412 | if (flags & OPT_RECURSIVE) { | |
1413 | struct child_process cpr = CHILD_PROCESS_INIT; | |
1414 | ||
1415 | cpr.git_cmd = 1; | |
1416 | cpr.dir = path; | |
1417 | prepare_submodule_repo_env(&cpr.env_array); | |
1418 | ||
22f9b7f3 JK |
1419 | strvec_push(&cpr.args, "--super-prefix"); |
1420 | strvec_pushf(&cpr.args, "%s/", displaypath); | |
1421 | strvec_pushl(&cpr.args, "submodule--helper", "sync", | |
f6d8942b | 1422 | "--recursive", NULL); |
13424764 PC |
1423 | |
1424 | if (flags & OPT_QUIET) | |
22f9b7f3 | 1425 | strvec_push(&cpr.args, "--quiet"); |
13424764 PC |
1426 | |
1427 | if (run_command(&cpr)) | |
1428 | die(_("failed to recurse into submodule '%s'"), | |
1429 | path); | |
1430 | } | |
1431 | ||
1432 | cleanup: | |
1433 | free(super_config_url); | |
1434 | free(sub_origin_url); | |
1435 | strbuf_release(&sb); | |
1436 | free(remote_key); | |
1437 | free(displaypath); | |
1438 | free(sub_config_path); | |
1439 | } | |
1440 | ||
1441 | static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data) | |
1442 | { | |
1443 | struct sync_cb *info = cb_data; | |
1444 | sync_submodule(list_item->name, info->prefix, info->flags); | |
13424764 PC |
1445 | } |
1446 | ||
1447 | static int module_sync(int argc, const char **argv, const char *prefix) | |
1448 | { | |
1449 | struct sync_cb info = SYNC_CB_INIT; | |
1450 | struct pathspec pathspec; | |
1451 | struct module_list list = MODULE_LIST_INIT; | |
1452 | int quiet = 0; | |
1453 | int recursive = 0; | |
1454 | ||
1455 | struct option module_sync_options[] = { | |
e73fe3dd | 1456 | OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")), |
13424764 | 1457 | OPT_BOOL(0, "recursive", &recursive, |
e73fe3dd | 1458 | N_("recurse into nested submodules")), |
13424764 PC |
1459 | OPT_END() |
1460 | }; | |
1461 | ||
1462 | const char *const git_submodule_helper_usage[] = { | |
1463 | N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"), | |
1464 | NULL | |
1465 | }; | |
1466 | ||
1467 | argc = parse_options(argc, argv, prefix, module_sync_options, | |
1468 | git_submodule_helper_usage, 0); | |
1469 | ||
1470 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
1471 | return 1; | |
1472 | ||
1473 | info.prefix = prefix; | |
1474 | if (quiet) | |
1475 | info.flags |= OPT_QUIET; | |
1476 | if (recursive) | |
1477 | info.flags |= OPT_RECURSIVE; | |
1478 | ||
1479 | for_each_listed_submodule(&list, sync_submodule_cb, &info); | |
1480 | ||
1481 | return 0; | |
1482 | } | |
1483 | ||
2e612731 PC |
1484 | struct deinit_cb { |
1485 | const char *prefix; | |
1486 | unsigned int flags; | |
1487 | }; | |
9865b6e6 | 1488 | #define DEINIT_CB_INIT { 0 } |
2e612731 PC |
1489 | |
1490 | static void deinit_submodule(const char *path, const char *prefix, | |
1491 | unsigned int flags) | |
1492 | { | |
1493 | const struct submodule *sub; | |
1494 | char *displaypath = NULL; | |
1495 | struct child_process cp_config = CHILD_PROCESS_INIT; | |
1496 | struct strbuf sb_config = STRBUF_INIT; | |
1497 | char *sub_git_dir = xstrfmt("%s/.git", path); | |
1498 | ||
14228447 | 1499 | sub = submodule_from_path(the_repository, null_oid(), path); |
2e612731 PC |
1500 | |
1501 | if (!sub || !sub->name) | |
1502 | goto cleanup; | |
1503 | ||
1504 | displaypath = get_submodule_displaypath(path, prefix); | |
1505 | ||
1506 | /* remove the submodule work tree (unless the user already did it) */ | |
1507 | if (is_directory(path)) { | |
1508 | struct strbuf sb_rm = STRBUF_INIT; | |
1509 | const char *format; | |
1510 | ||
0adc8ba6 MP |
1511 | if (is_directory(sub_git_dir)) { |
1512 | if (!(flags & OPT_QUIET)) | |
1513 | warning(_("Submodule work tree '%s' contains a .git " | |
1514 | "directory. This will be replaced with a " | |
1515 | ".git file by using absorbgitdirs."), | |
1516 | displaypath); | |
1517 | ||
1518 | absorb_git_dir_into_superproject(path, | |
1519 | ABSORB_GITDIR_RECURSE_SUBMODULES); | |
1520 | ||
1521 | } | |
2e612731 PC |
1522 | |
1523 | if (!(flags & OPT_FORCE)) { | |
1524 | struct child_process cp_rm = CHILD_PROCESS_INIT; | |
1525 | cp_rm.git_cmd = 1; | |
22f9b7f3 | 1526 | strvec_pushl(&cp_rm.args, "rm", "-qn", |
f6d8942b | 1527 | path, NULL); |
2e612731 PC |
1528 | |
1529 | if (run_command(&cp_rm)) | |
1530 | die(_("Submodule work tree '%s' contains local " | |
1531 | "modifications; use '-f' to discard them"), | |
1532 | displaypath); | |
1533 | } | |
1534 | ||
1535 | strbuf_addstr(&sb_rm, path); | |
1536 | ||
1537 | if (!remove_dir_recursively(&sb_rm, 0)) | |
1538 | format = _("Cleared directory '%s'\n"); | |
1539 | else | |
1540 | format = _("Could not remove submodule work tree '%s'\n"); | |
1541 | ||
1542 | if (!(flags & OPT_QUIET)) | |
1543 | printf(format, displaypath); | |
1544 | ||
8eda5efa SB |
1545 | submodule_unset_core_worktree(sub); |
1546 | ||
2e612731 PC |
1547 | strbuf_release(&sb_rm); |
1548 | } | |
1549 | ||
1550 | if (mkdir(path, 0777)) | |
1551 | printf(_("could not create empty submodule directory %s"), | |
1552 | displaypath); | |
1553 | ||
1554 | cp_config.git_cmd = 1; | |
22f9b7f3 JK |
1555 | strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL); |
1556 | strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name); | |
2e612731 PC |
1557 | |
1558 | /* remove the .git/config entries (unless the user already did it) */ | |
1559 | if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) { | |
1560 | char *sub_key = xstrfmt("submodule.%s", sub->name); | |
1561 | /* | |
1562 | * remove the whole section so we have a clean state when | |
1563 | * the user later decides to init this submodule again | |
1564 | */ | |
1565 | git_config_rename_section_in_file(NULL, sub_key, NULL); | |
1566 | if (!(flags & OPT_QUIET)) | |
1567 | printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"), | |
1568 | sub->name, sub->url, displaypath); | |
1569 | free(sub_key); | |
1570 | } | |
1571 | ||
1572 | cleanup: | |
1573 | free(displaypath); | |
1574 | free(sub_git_dir); | |
1575 | strbuf_release(&sb_config); | |
1576 | } | |
1577 | ||
1578 | static void deinit_submodule_cb(const struct cache_entry *list_item, | |
1579 | void *cb_data) | |
1580 | { | |
1581 | struct deinit_cb *info = cb_data; | |
1582 | deinit_submodule(list_item->name, info->prefix, info->flags); | |
1583 | } | |
1584 | ||
1585 | static int module_deinit(int argc, const char **argv, const char *prefix) | |
1586 | { | |
1587 | struct deinit_cb info = DEINIT_CB_INIT; | |
1588 | struct pathspec pathspec; | |
1589 | struct module_list list = MODULE_LIST_INIT; | |
1590 | int quiet = 0; | |
1591 | int force = 0; | |
1592 | int all = 0; | |
1593 | ||
1594 | struct option module_deinit_options[] = { | |
e73fe3dd ZH |
1595 | OPT__QUIET(&quiet, N_("suppress submodule status output")), |
1596 | OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0), | |
1597 | OPT_BOOL(0, "all", &all, N_("unregister all submodules")), | |
2e612731 PC |
1598 | OPT_END() |
1599 | }; | |
1600 | ||
1601 | const char *const git_submodule_helper_usage[] = { | |
1602 | N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"), | |
1603 | NULL | |
1604 | }; | |
1605 | ||
1606 | argc = parse_options(argc, argv, prefix, module_deinit_options, | |
1607 | git_submodule_helper_usage, 0); | |
1608 | ||
1609 | if (all && argc) { | |
1610 | error("pathspec and --all are incompatible"); | |
1611 | usage_with_options(git_submodule_helper_usage, | |
1612 | module_deinit_options); | |
1613 | } | |
1614 | ||
1615 | if (!argc && !all) | |
1616 | die(_("Use '--all' if you really want to deinitialize all submodules")); | |
1617 | ||
1618 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) | |
9748e39d | 1619 | return 1; |
2e612731 PC |
1620 | |
1621 | info.prefix = prefix; | |
1622 | if (quiet) | |
1623 | info.flags |= OPT_QUIET; | |
1624 | if (force) | |
1625 | info.flags |= OPT_FORCE; | |
1626 | ||
1627 | for_each_listed_submodule(&list, deinit_submodule_cb, &info); | |
1628 | ||
1629 | return 0; | |
1630 | } | |
1631 | ||
a98b02c1 AR |
1632 | struct module_clone_data { |
1633 | const char *prefix; | |
1634 | const char *path; | |
1635 | const char *name; | |
1636 | const char *url; | |
1637 | const char *depth; | |
f05da2b4 | 1638 | struct list_objects_filter_options *filter_options; |
a98b02c1 AR |
1639 | struct string_list reference; |
1640 | unsigned int quiet: 1; | |
1641 | unsigned int progress: 1; | |
1642 | unsigned int dissociate: 1; | |
1643 | unsigned int require_init: 1; | |
1644 | int single_branch; | |
1645 | }; | |
c9911c93 GC |
1646 | #define MODULE_CLONE_DATA_INIT { \ |
1647 | .reference = STRING_LIST_INIT_NODUP, \ | |
1648 | .single_branch = -1, \ | |
1649 | } | |
ee8838d1 | 1650 | |
31224cbd SB |
1651 | struct submodule_alternate_setup { |
1652 | const char *submodule_name; | |
1653 | enum SUBMODULE_ALTERNATE_ERROR_MODE { | |
1654 | SUBMODULE_ALTERNATE_ERROR_DIE, | |
1655 | SUBMODULE_ALTERNATE_ERROR_INFO, | |
1656 | SUBMODULE_ALTERNATE_ERROR_IGNORE | |
1657 | } error_mode; | |
1658 | struct string_list *reference; | |
1659 | }; | |
f69a6e4f ÆAB |
1660 | #define SUBMODULE_ALTERNATE_SETUP_INIT { \ |
1661 | .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \ | |
1662 | } | |
31224cbd | 1663 | |
4f3e57ef JT |
1664 | static const char alternate_error_advice[] = N_( |
1665 | "An alternate computed from a superproject's alternate is invalid.\n" | |
1666 | "To allow Git to clone without an alternate in such a case, set\n" | |
1667 | "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n" | |
1668 | "'--reference-if-able' instead of '--reference'." | |
1669 | ); | |
1670 | ||
31224cbd | 1671 | static int add_possible_reference_from_superproject( |
263db403 | 1672 | struct object_directory *odb, void *sas_cb) |
31224cbd SB |
1673 | { |
1674 | struct submodule_alternate_setup *sas = sas_cb; | |
b2ac148f | 1675 | size_t len; |
31224cbd | 1676 | |
31224cbd SB |
1677 | /* |
1678 | * If the alternate object store is another repository, try the | |
bf03b790 | 1679 | * standard layout with .git/(modules/<name>)+/objects |
31224cbd | 1680 | */ |
263db403 | 1681 | if (strip_suffix(odb->path, "/objects", &len)) { |
ce125d43 | 1682 | struct repository alternate; |
31224cbd SB |
1683 | char *sm_alternate; |
1684 | struct strbuf sb = STRBUF_INIT; | |
1685 | struct strbuf err = STRBUF_INIT; | |
263db403 | 1686 | strbuf_add(&sb, odb->path, len); |
597f9134 | 1687 | |
ce125d43 JT |
1688 | repo_init(&alternate, sb.buf, NULL); |
1689 | ||
31224cbd SB |
1690 | /* |
1691 | * We need to end the new path with '/' to mark it as a dir, | |
1692 | * otherwise a submodule name containing '/' will be broken | |
1693 | * as the last part of a missing submodule reference would | |
1694 | * be taken as a file name. | |
1695 | */ | |
ce125d43 JT |
1696 | strbuf_reset(&sb); |
1697 | submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name); | |
1698 | strbuf_addch(&sb, '/'); | |
1699 | repo_clear(&alternate); | |
31224cbd SB |
1700 | |
1701 | sm_alternate = compute_alternate_path(sb.buf, &err); | |
1702 | if (sm_alternate) { | |
1703 | string_list_append(sas->reference, xstrdup(sb.buf)); | |
1704 | free(sm_alternate); | |
1705 | } else { | |
1706 | switch (sas->error_mode) { | |
1707 | case SUBMODULE_ALTERNATE_ERROR_DIE: | |
ed9bff08 | 1708 | if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE)) |
4f3e57ef | 1709 | advise(_(alternate_error_advice)); |
31224cbd SB |
1710 | die(_("submodule '%s' cannot add alternate: %s"), |
1711 | sas->submodule_name, err.buf); | |
1712 | case SUBMODULE_ALTERNATE_ERROR_INFO: | |
1a878714 | 1713 | fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"), |
31224cbd SB |
1714 | sas->submodule_name, err.buf); |
1715 | case SUBMODULE_ALTERNATE_ERROR_IGNORE: | |
1716 | ; /* nothing */ | |
1717 | } | |
1718 | } | |
1719 | strbuf_release(&sb); | |
1720 | } | |
1721 | ||
31224cbd SB |
1722 | return 0; |
1723 | } | |
1724 | ||
1725 | static void prepare_possible_alternates(const char *sm_name, | |
1726 | struct string_list *reference) | |
1727 | { | |
1728 | char *sm_alternate = NULL, *error_strategy = NULL; | |
1729 | struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT; | |
1730 | ||
1731 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
1732 | if (!sm_alternate) | |
1733 | return; | |
1734 | ||
1735 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
1736 | ||
1737 | if (!error_strategy) | |
1738 | error_strategy = xstrdup("die"); | |
1739 | ||
1740 | sas.submodule_name = sm_name; | |
1741 | sas.reference = reference; | |
1742 | if (!strcmp(error_strategy, "die")) | |
1743 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE; | |
1744 | else if (!strcmp(error_strategy, "info")) | |
1745 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO; | |
1746 | else if (!strcmp(error_strategy, "ignore")) | |
1747 | sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE; | |
1748 | else | |
1749 | die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy); | |
1750 | ||
1751 | if (!strcmp(sm_alternate, "superproject")) | |
1752 | foreach_alt_odb(add_possible_reference_from_superproject, &sas); | |
1753 | else if (!strcmp(sm_alternate, "no")) | |
1754 | ; /* do nothing */ | |
1755 | else | |
1756 | die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate); | |
1757 | ||
1758 | free(sm_alternate); | |
1759 | free(error_strategy); | |
1760 | } | |
1761 | ||
a98b02c1 | 1762 | static int clone_submodule(struct module_clone_data *clone_data) |
ee8838d1 | 1763 | { |
a98b02c1 | 1764 | char *p, *sm_gitdir; |
bf03b790 | 1765 | char *sm_alternate = NULL, *error_strategy = NULL; |
a98b02c1 AR |
1766 | struct strbuf sb = STRBUF_INIT; |
1767 | struct child_process cp = CHILD_PROCESS_INIT; | |
1768 | ||
ce125d43 | 1769 | submodule_name_to_gitdir(&sb, the_repository, clone_data->name); |
a98b02c1 AR |
1770 | sm_gitdir = absolute_pathdup(sb.buf); |
1771 | strbuf_reset(&sb); | |
1772 | ||
1773 | if (!is_absolute_path(clone_data->path)) { | |
1774 | strbuf_addf(&sb, "%s/%s", get_git_work_tree(), clone_data->path); | |
1775 | clone_data->path = strbuf_detach(&sb, NULL); | |
1776 | } else { | |
1777 | clone_data->path = xstrdup(clone_data->path); | |
1778 | } | |
1779 | ||
1780 | if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) | |
1781 | die(_("refusing to create/use '%s' in another submodule's " | |
1782 | "git dir"), sm_gitdir); | |
1783 | ||
1784 | if (!file_exists(sm_gitdir)) { | |
1785 | if (safe_create_leading_directories_const(sm_gitdir) < 0) | |
1786 | die(_("could not create directory '%s'"), sm_gitdir); | |
1787 | ||
1788 | prepare_possible_alternates(clone_data->name, &clone_data->reference); | |
1789 | ||
1790 | strvec_push(&cp.args, "clone"); | |
1791 | strvec_push(&cp.args, "--no-checkout"); | |
1792 | if (clone_data->quiet) | |
1793 | strvec_push(&cp.args, "--quiet"); | |
1794 | if (clone_data->progress) | |
1795 | strvec_push(&cp.args, "--progress"); | |
1796 | if (clone_data->depth && *(clone_data->depth)) | |
1797 | strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL); | |
1798 | if (clone_data->reference.nr) { | |
1799 | struct string_list_item *item; | |
1800 | for_each_string_list_item(item, &clone_data->reference) | |
1801 | strvec_pushl(&cp.args, "--reference", | |
1802 | item->string, NULL); | |
1803 | } | |
1804 | if (clone_data->dissociate) | |
1805 | strvec_push(&cp.args, "--dissociate"); | |
1806 | if (sm_gitdir && *sm_gitdir) | |
1807 | strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL); | |
f05da2b4 JS |
1808 | if (clone_data->filter_options && clone_data->filter_options->choice) |
1809 | strvec_pushf(&cp.args, "--filter=%s", | |
1810 | expand_list_objects_filter_spec( | |
1811 | clone_data->filter_options)); | |
a98b02c1 AR |
1812 | if (clone_data->single_branch >= 0) |
1813 | strvec_push(&cp.args, clone_data->single_branch ? | |
1814 | "--single-branch" : | |
1815 | "--no-single-branch"); | |
1816 | ||
1817 | strvec_push(&cp.args, "--"); | |
1818 | strvec_push(&cp.args, clone_data->url); | |
1819 | strvec_push(&cp.args, clone_data->path); | |
1820 | ||
1821 | cp.git_cmd = 1; | |
1822 | prepare_submodule_repo_env(&cp.env_array); | |
1823 | cp.no_stdin = 1; | |
1824 | ||
1825 | if(run_command(&cp)) | |
1826 | die(_("clone of '%s' into submodule path '%s' failed"), | |
1827 | clone_data->url, clone_data->path); | |
1828 | } else { | |
1829 | if (clone_data->require_init && !access(clone_data->path, X_OK) && | |
1830 | !is_empty_dir(clone_data->path)) | |
1831 | die(_("directory not empty: '%s'"), clone_data->path); | |
1832 | if (safe_create_leading_directories_const(clone_data->path) < 0) | |
1833 | die(_("could not create directory '%s'"), clone_data->path); | |
1834 | strbuf_addf(&sb, "%s/index", sm_gitdir); | |
1835 | unlink_or_warn(sb.buf); | |
1836 | strbuf_reset(&sb); | |
1837 | } | |
1838 | ||
1839 | connect_work_tree_and_git_dir(clone_data->path, sm_gitdir, 0); | |
1840 | ||
1841 | p = git_pathdup_submodule(clone_data->path, "config"); | |
1842 | if (!p) | |
1843 | die(_("could not get submodule directory for '%s'"), clone_data->path); | |
1844 | ||
1845 | /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */ | |
1846 | git_config_get_string("submodule.alternateLocation", &sm_alternate); | |
1847 | if (sm_alternate) | |
1848 | git_config_set_in_file(p, "submodule.alternateLocation", | |
1849 | sm_alternate); | |
1850 | git_config_get_string("submodule.alternateErrorStrategy", &error_strategy); | |
1851 | if (error_strategy) | |
1852 | git_config_set_in_file(p, "submodule.alternateErrorStrategy", | |
1853 | error_strategy); | |
1854 | ||
1855 | free(sm_alternate); | |
1856 | free(error_strategy); | |
1857 | ||
1858 | strbuf_release(&sb); | |
1859 | free(sm_gitdir); | |
1860 | free(p); | |
1861 | return 0; | |
1862 | } | |
1863 | ||
1864 | static int module_clone(int argc, const char **argv, const char *prefix) | |
1865 | { | |
1866 | int dissociate = 0, quiet = 0, progress = 0, require_init = 0; | |
1867 | struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; | |
f05da2b4 | 1868 | struct list_objects_filter_options filter_options; |
ee8838d1 SB |
1869 | |
1870 | struct option module_clone_options[] = { | |
a98b02c1 | 1871 | OPT_STRING(0, "prefix", &clone_data.prefix, |
ee8838d1 SB |
1872 | N_("path"), |
1873 | N_("alternative anchor for relative paths")), | |
a98b02c1 | 1874 | OPT_STRING(0, "path", &clone_data.path, |
ee8838d1 SB |
1875 | N_("path"), |
1876 | N_("where the new submodule will be cloned to")), | |
a98b02c1 | 1877 | OPT_STRING(0, "name", &clone_data.name, |
ee8838d1 SB |
1878 | N_("string"), |
1879 | N_("name of the new submodule")), | |
a98b02c1 | 1880 | OPT_STRING(0, "url", &clone_data.url, |
ee8838d1 SB |
1881 | N_("string"), |
1882 | N_("url where to clone the submodule from")), | |
a98b02c1 | 1883 | OPT_STRING_LIST(0, "reference", &clone_data.reference, |
965dbea0 | 1884 | N_("repo"), |
ee8838d1 | 1885 | N_("reference repository")), |
a0ef2934 CF |
1886 | OPT_BOOL(0, "dissociate", &dissociate, |
1887 | N_("use --reference only while cloning")), | |
a98b02c1 | 1888 | OPT_STRING(0, "depth", &clone_data.depth, |
ee8838d1 SB |
1889 | N_("string"), |
1890 | N_("depth for shallow clones")), | |
9e1f22c8 | 1891 | OPT__QUIET(&quiet, "suppress output for cloning a submodule"), |
72c5f883 JK |
1892 | OPT_BOOL(0, "progress", &progress, |
1893 | N_("force cloning progress")), | |
0060fd15 JS |
1894 | OPT_BOOL(0, "require-init", &require_init, |
1895 | N_("disallow cloning into non-empty directory")), | |
a98b02c1 | 1896 | OPT_BOOL(0, "single-branch", &clone_data.single_branch, |
132f600b | 1897 | N_("clone only one branch, HEAD or --branch")), |
f05da2b4 | 1898 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
ee8838d1 SB |
1899 | OPT_END() |
1900 | }; | |
1901 | ||
1902 | const char *const git_submodule_helper_usage[] = { | |
1903 | N_("git submodule--helper clone [--prefix=<path>] [--quiet] " | |
7dad2633 | 1904 | "[--reference <repository>] [--name <name>] [--depth <depth>] " |
f05da2b4 | 1905 | "[--single-branch] [--filter <filter-spec>]" |
7dad2633 | 1906 | "--url <url> --path <path>"), |
ee8838d1 SB |
1907 | NULL |
1908 | }; | |
1909 | ||
f05da2b4 | 1910 | memset(&filter_options, 0, sizeof(filter_options)); |
ee8838d1 SB |
1911 | argc = parse_options(argc, argv, prefix, module_clone_options, |
1912 | git_submodule_helper_usage, 0); | |
1913 | ||
a98b02c1 AR |
1914 | clone_data.dissociate = !!dissociate; |
1915 | clone_data.quiet = !!quiet; | |
1916 | clone_data.progress = !!progress; | |
1917 | clone_data.require_init = !!require_init; | |
f05da2b4 | 1918 | clone_data.filter_options = &filter_options; |
a98b02c1 AR |
1919 | |
1920 | if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path)) | |
08e0970a JK |
1921 | usage_with_options(git_submodule_helper_usage, |
1922 | module_clone_options); | |
1923 | ||
a98b02c1 | 1924 | clone_submodule(&clone_data); |
f05da2b4 | 1925 | list_objects_filter_release(&filter_options); |
ee8838d1 SB |
1926 | return 0; |
1927 | } | |
74703a1e | 1928 | |
ee69b2a9 SB |
1929 | static void determine_submodule_update_strategy(struct repository *r, |
1930 | int just_cloned, | |
1931 | const char *path, | |
1932 | const char *update, | |
1933 | struct submodule_update_strategy *out) | |
1934 | { | |
14228447 | 1935 | const struct submodule *sub = submodule_from_path(r, null_oid(), path); |
ee69b2a9 SB |
1936 | char *key; |
1937 | const char *val; | |
1938 | ||
1939 | key = xstrfmt("submodule.%s.update", sub->name); | |
1940 | ||
1941 | if (update) { | |
ee69b2a9 SB |
1942 | if (parse_submodule_update_strategy(update, out) < 0) |
1943 | die(_("Invalid update mode '%s' for submodule path '%s'"), | |
1944 | update, path); | |
f1de981e | 1945 | } else if (!repo_config_get_string_tmp(r, key, &val)) { |
ee69b2a9 SB |
1946 | if (parse_submodule_update_strategy(val, out) < 0) |
1947 | die(_("Invalid update mode '%s' configured for submodule path '%s'"), | |
1948 | val, path); | |
1949 | } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) { | |
c1547450 JN |
1950 | if (sub->update_strategy.type == SM_UPDATE_COMMAND) |
1951 | BUG("how did we read update = !command from .gitmodules?"); | |
ee69b2a9 SB |
1952 | out->type = sub->update_strategy.type; |
1953 | out->command = sub->update_strategy.command; | |
1954 | } else | |
1955 | out->type = SM_UPDATE_CHECKOUT; | |
1956 | ||
1957 | if (just_cloned && | |
1958 | (out->type == SM_UPDATE_MERGE || | |
1959 | out->type == SM_UPDATE_REBASE || | |
1960 | out->type == SM_UPDATE_NONE)) | |
1961 | out->type = SM_UPDATE_CHECKOUT; | |
1962 | ||
1963 | free(key); | |
1964 | } | |
1965 | ||
f1d15713 SB |
1966 | struct update_clone_data { |
1967 | const struct submodule *sub; | |
1968 | struct object_id oid; | |
1969 | unsigned just_cloned; | |
1970 | }; | |
1971 | ||
48308681 | 1972 | struct submodule_update_clone { |
c9911c93 | 1973 | /* index into 'update_data.list', the list of submodules to look into for cloning */ |
48308681 | 1974 | int current; |
48308681 SB |
1975 | |
1976 | /* configuration parameters which are passed on to the children */ | |
c9911c93 | 1977 | struct update_data *update_data; |
48308681 | 1978 | |
b3c5f5cb | 1979 | /* to be consumed by update_submodule() */ |
f1d15713 SB |
1980 | struct update_clone_data *update_clone; |
1981 | int update_clone_nr; int update_clone_alloc; | |
48308681 SB |
1982 | |
1983 | /* If we want to stop as fast as possible and return an error */ | |
1984 | unsigned quickstop : 1; | |
665b35ec SB |
1985 | |
1986 | /* failed clones to be retried again */ | |
1987 | const struct cache_entry **failed_clones; | |
1988 | int failed_clones_nr, failed_clones_alloc; | |
48308681 | 1989 | }; |
c9911c93 | 1990 | #define SUBMODULE_UPDATE_CLONE_INIT { 0 } |
48308681 | 1991 | |
c51f8f94 | 1992 | struct update_data { |
c9911c93 | 1993 | const char *prefix; |
c51f8f94 | 1994 | const char *recursive_prefix; |
c51f8f94 | 1995 | const char *displaypath; |
c9911c93 | 1996 | const char *update_default; |
c51f8f94 | 1997 | struct object_id suboid; |
c9911c93 | 1998 | struct string_list references; |
c51f8f94 | 1999 | struct submodule_update_strategy update_strategy; |
c9911c93 GC |
2000 | struct list_objects_filter_options *filter_options; |
2001 | struct module_list list; | |
c51f8f94 | 2002 | int depth; |
c9911c93 GC |
2003 | int max_jobs; |
2004 | int single_branch; | |
2005 | int recommend_shallow; | |
2006 | unsigned int require_init; | |
ed9c8485 ÆAB |
2007 | unsigned int force; |
2008 | unsigned int quiet; | |
2009 | unsigned int nofetch; | |
1012a5cb | 2010 | unsigned int remote; |
c9911c93 GC |
2011 | unsigned int progress; |
2012 | unsigned int dissociate; | |
2013 | unsigned int init; | |
2014 | unsigned int warn_if_uninitialized; | |
b3c5f5cb AR |
2015 | unsigned int recursive; |
2016 | ||
2017 | /* copied over from update_clone_data */ | |
2018 | struct object_id oid; | |
2019 | unsigned int just_cloned; | |
2020 | const char *sm_path; | |
c51f8f94 | 2021 | }; |
c9911c93 GC |
2022 | #define UPDATE_DATA_INIT { \ |
2023 | .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \ | |
2024 | .list = MODULE_LIST_INIT, \ | |
2025 | .recommend_shallow = -1, \ | |
2026 | .references = STRING_LIST_INIT_DUP, \ | |
2027 | .single_branch = -1, \ | |
2028 | .max_jobs = 1, \ | |
2029 | .warn_if_uninitialized = 1, \ | |
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 | */ | |
c9911c93 | 2039 | if (suc->update_data->warn_if_uninitialized) { |
08fdbdb1 SB |
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)) { | |
c9911c93 GC |
2071 | if (suc->update_data->recursive_prefix) |
2072 | strbuf_addf(&sb, "%s/%s", suc->update_data->recursive_prefix, ce->name); | |
48308681 | 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 | ||
14228447 | 2080 | sub = submodule_from_path(the_repository, null_oid(), ce->name); |
48308681 | 2081 | |
c9911c93 GC |
2082 | if (suc->update_data->recursive_prefix) |
2083 | displaypath = relative_path(suc->update_data->recursive_prefix, | |
48308681 SB |
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 | ||
c9911c93 GC |
2101 | if (suc->update_data->update_strategy.type == SM_UPDATE_NONE |
2102 | || (suc->update_data->update_strategy.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)) { | |
de0fcbe0 | 2120 | url = resolve_relative_url(sub->url, NULL, 0); |
e0a862fd SB |
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"); | |
c9911c93 | 2146 | if (suc->update_data->progress) |
22f9b7f3 | 2147 | strvec_push(&child->args, "--progress"); |
c9911c93 | 2148 | if (suc->update_data->quiet) |
22f9b7f3 | 2149 | strvec_push(&child->args, "--quiet"); |
c9911c93 GC |
2150 | if (suc->update_data->prefix) |
2151 | strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL); | |
2152 | if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1) | |
22f9b7f3 | 2153 | strvec_push(&child->args, "--depth=1"); |
c9911c93 GC |
2154 | else if (suc->update_data->depth) |
2155 | strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth); | |
2156 | if (suc->update_data->filter_options && suc->update_data->filter_options->choice) | |
f05da2b4 | 2157 | strvec_pushf(&child->args, "--filter=%s", |
c9911c93 GC |
2158 | expand_list_objects_filter_spec(suc->update_data->filter_options)); |
2159 | if (suc->update_data->require_init) | |
22f9b7f3 JK |
2160 | strvec_push(&child->args, "--require-init"); |
2161 | strvec_pushl(&child->args, "--path", sub->path, NULL); | |
2162 | strvec_pushl(&child->args, "--name", sub->name, NULL); | |
2163 | strvec_pushl(&child->args, "--url", url, NULL); | |
c9911c93 | 2164 | if (suc->update_data->references.nr) { |
5f50f33e | 2165 | struct string_list_item *item; |
c9911c93 | 2166 | for_each_string_list_item(item, &suc->update_data->references) |
22f9b7f3 | 2167 | strvec_pushl(&child->args, "--reference", item->string, NULL); |
5f50f33e | 2168 | } |
c9911c93 | 2169 | if (suc->update_data->dissociate) |
22f9b7f3 | 2170 | strvec_push(&child->args, "--dissociate"); |
c9911c93 GC |
2171 | if (suc->update_data->single_branch >= 0) |
2172 | strvec_push(&child->args, suc->update_data->single_branch ? | |
132f600b ES |
2173 | "--single-branch" : |
2174 | "--no-single-branch"); | |
48308681 SB |
2175 | |
2176 | cleanup: | |
9101c8ea JK |
2177 | strbuf_release(&displaypath_sb); |
2178 | strbuf_release(&sb); | |
e0a862fd SB |
2179 | if (need_free_url) |
2180 | free((void*)url); | |
48308681 SB |
2181 | |
2182 | return needs_cloning; | |
2183 | } | |
2184 | ||
2185 | static int update_clone_get_next_task(struct child_process *child, | |
2186 | struct strbuf *err, | |
2187 | void *suc_cb, | |
665b35ec | 2188 | void **idx_task_cb) |
48308681 SB |
2189 | { |
2190 | struct submodule_update_clone *suc = suc_cb; | |
665b35ec SB |
2191 | const struct cache_entry *ce; |
2192 | int index; | |
48308681 | 2193 | |
c9911c93 GC |
2194 | for (; suc->current < suc->update_data->list.nr; suc->current++) { |
2195 | ce = suc->update_data->list.entries[suc->current]; | |
48308681 | 2196 | if (prepare_to_clone_next_submodule(ce, child, suc, err)) { |
665b35ec SB |
2197 | int *p = xmalloc(sizeof(*p)); |
2198 | *p = suc->current; | |
2199 | *idx_task_cb = p; | |
48308681 SB |
2200 | suc->current++; |
2201 | return 1; | |
2202 | } | |
2203 | } | |
665b35ec SB |
2204 | |
2205 | /* | |
2206 | * The loop above tried cloning each submodule once, now try the | |
2207 | * stragglers again, which we can imagine as an extension of the | |
2208 | * entry list. | |
2209 | */ | |
c9911c93 | 2210 | index = suc->current - suc->update_data->list.nr; |
665b35ec SB |
2211 | if (index < suc->failed_clones_nr) { |
2212 | int *p; | |
2213 | ce = suc->failed_clones[index]; | |
2201ee09 SB |
2214 | if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { |
2215 | suc->current ++; | |
a22ae753 RS |
2216 | strbuf_addstr(err, "BUG: submodule considered for " |
2217 | "cloning, doesn't need cloning " | |
2218 | "any more?\n"); | |
2201ee09 SB |
2219 | return 0; |
2220 | } | |
665b35ec SB |
2221 | p = xmalloc(sizeof(*p)); |
2222 | *p = suc->current; | |
2223 | *idx_task_cb = p; | |
2224 | suc->current ++; | |
2225 | return 1; | |
2226 | } | |
2227 | ||
48308681 SB |
2228 | return 0; |
2229 | } | |
2230 | ||
2231 | static int update_clone_start_failure(struct strbuf *err, | |
2232 | void *suc_cb, | |
665b35ec | 2233 | void *idx_task_cb) |
48308681 SB |
2234 | { |
2235 | struct submodule_update_clone *suc = suc_cb; | |
2236 | suc->quickstop = 1; | |
2237 | return 1; | |
2238 | } | |
2239 | ||
2240 | static int update_clone_task_finished(int result, | |
2241 | struct strbuf *err, | |
2242 | void *suc_cb, | |
665b35ec | 2243 | void *idx_task_cb) |
48308681 | 2244 | { |
665b35ec | 2245 | const struct cache_entry *ce; |
48308681 SB |
2246 | struct submodule_update_clone *suc = suc_cb; |
2247 | ||
c1e860f1 | 2248 | int *idxP = idx_task_cb; |
665b35ec SB |
2249 | int idx = *idxP; |
2250 | free(idxP); | |
2251 | ||
48308681 SB |
2252 | if (!result) |
2253 | return 0; | |
2254 | ||
c9911c93 GC |
2255 | if (idx < suc->update_data->list.nr) { |
2256 | ce = suc->update_data->list.entries[idx]; | |
665b35ec SB |
2257 | strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"), |
2258 | ce->name); | |
2259 | strbuf_addch(err, '\n'); | |
2260 | ALLOC_GROW(suc->failed_clones, | |
2261 | suc->failed_clones_nr + 1, | |
2262 | suc->failed_clones_alloc); | |
2263 | suc->failed_clones[suc->failed_clones_nr++] = ce; | |
2264 | return 0; | |
2265 | } else { | |
c9911c93 | 2266 | idx -= suc->update_data->list.nr; |
665b35ec SB |
2267 | ce = suc->failed_clones[idx]; |
2268 | strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"), | |
2269 | ce->name); | |
2270 | strbuf_addch(err, '\n'); | |
2271 | suc->quickstop = 1; | |
2272 | return 1; | |
2273 | } | |
2274 | ||
2275 | return 0; | |
48308681 SB |
2276 | } |
2277 | ||
05744997 AO |
2278 | static int git_update_clone_config(const char *var, const char *value, |
2279 | void *cb) | |
f20e7c1e BW |
2280 | { |
2281 | int *max_jobs = cb; | |
2282 | if (!strcmp(var, "submodule.fetchjobs")) | |
2283 | *max_jobs = parse_submodule_fetchjobs(var, value); | |
2284 | return 0; | |
2285 | } | |
2286 | ||
c51f8f94 AR |
2287 | static int is_tip_reachable(const char *path, struct object_id *oid) |
2288 | { | |
2289 | struct child_process cp = CHILD_PROCESS_INIT; | |
2290 | struct strbuf rev = STRBUF_INIT; | |
2291 | char *hex = oid_to_hex(oid); | |
2292 | ||
2293 | cp.git_cmd = 1; | |
2294 | cp.dir = xstrdup(path); | |
2295 | cp.no_stderr = 1; | |
2296 | strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL); | |
2297 | ||
2298 | prepare_submodule_repo_env(&cp.env_array); | |
2299 | ||
2300 | if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len) | |
2301 | return 0; | |
2302 | ||
2303 | return 1; | |
2304 | } | |
2305 | ||
2306 | static int fetch_in_submodule(const char *module_path, int depth, int quiet, struct object_id *oid) | |
2307 | { | |
2308 | struct child_process cp = CHILD_PROCESS_INIT; | |
2309 | ||
2310 | prepare_submodule_repo_env(&cp.env_array); | |
2311 | cp.git_cmd = 1; | |
2312 | cp.dir = xstrdup(module_path); | |
2313 | ||
2314 | strvec_push(&cp.args, "fetch"); | |
2315 | if (quiet) | |
2316 | strvec_push(&cp.args, "--quiet"); | |
2317 | if (depth) | |
2318 | strvec_pushf(&cp.args, "--depth=%d", depth); | |
2319 | if (oid) { | |
2320 | char *hex = oid_to_hex(oid); | |
2321 | char *remote = get_default_remote(); | |
2322 | strvec_pushl(&cp.args, remote, hex, NULL); | |
2323 | } | |
2324 | ||
2325 | return run_command(&cp); | |
2326 | } | |
2327 | ||
2328 | static int run_update_command(struct update_data *ud, int subforce) | |
2329 | { | |
3c3558f0 | 2330 | struct child_process cp = CHILD_PROCESS_INIT; |
c51f8f94 AR |
2331 | char *oid = oid_to_hex(&ud->oid); |
2332 | int must_die_on_failure = 0; | |
c51f8f94 AR |
2333 | |
2334 | switch (ud->update_strategy.type) { | |
2335 | case SM_UPDATE_CHECKOUT: | |
3c3558f0 AR |
2336 | cp.git_cmd = 1; |
2337 | strvec_pushl(&cp.args, "checkout", "-q", NULL); | |
c51f8f94 | 2338 | if (subforce) |
3c3558f0 | 2339 | strvec_push(&cp.args, "-f"); |
c51f8f94 AR |
2340 | break; |
2341 | case SM_UPDATE_REBASE: | |
3c3558f0 AR |
2342 | cp.git_cmd = 1; |
2343 | strvec_push(&cp.args, "rebase"); | |
c51f8f94 | 2344 | if (ud->quiet) |
3c3558f0 | 2345 | strvec_push(&cp.args, "--quiet"); |
c51f8f94 AR |
2346 | must_die_on_failure = 1; |
2347 | break; | |
2348 | case SM_UPDATE_MERGE: | |
3c3558f0 AR |
2349 | cp.git_cmd = 1; |
2350 | strvec_push(&cp.args, "merge"); | |
c51f8f94 | 2351 | if (ud->quiet) |
3c3558f0 | 2352 | strvec_push(&cp.args, "--quiet"); |
c51f8f94 AR |
2353 | must_die_on_failure = 1; |
2354 | break; | |
2355 | case SM_UPDATE_COMMAND: | |
3c3558f0 AR |
2356 | cp.use_shell = 1; |
2357 | strvec_push(&cp.args, ud->update_strategy.command); | |
c51f8f94 AR |
2358 | must_die_on_failure = 1; |
2359 | break; | |
2360 | default: | |
2361 | BUG("unexpected update strategy type: %s", | |
2362 | submodule_strategy_to_string(&ud->update_strategy)); | |
2363 | } | |
3c3558f0 | 2364 | strvec_push(&cp.args, oid); |
c51f8f94 | 2365 | |
3c3558f0 AR |
2366 | cp.dir = xstrdup(ud->sm_path); |
2367 | prepare_submodule_repo_env(&cp.env_array); | |
2368 | if (run_command(&cp)) { | |
c51f8f94 AR |
2369 | switch (ud->update_strategy.type) { |
2370 | case SM_UPDATE_CHECKOUT: | |
55b3f12c GC |
2371 | die_message(_("Unable to checkout '%s' in submodule path '%s'"), |
2372 | oid, ud->displaypath); | |
c51f8f94 AR |
2373 | break; |
2374 | case SM_UPDATE_REBASE: | |
55b3f12c GC |
2375 | die_message(_("Unable to rebase '%s' in submodule path '%s'"), |
2376 | oid, ud->displaypath); | |
c51f8f94 AR |
2377 | break; |
2378 | case SM_UPDATE_MERGE: | |
55b3f12c GC |
2379 | die_message(_("Unable to merge '%s' in submodule path '%s'"), |
2380 | oid, ud->displaypath); | |
c51f8f94 AR |
2381 | break; |
2382 | case SM_UPDATE_COMMAND: | |
55b3f12c GC |
2383 | die_message(_("Execution of '%s %s' failed in submodule path '%s'"), |
2384 | ud->update_strategy.command, oid, ud->displaypath); | |
c51f8f94 AR |
2385 | break; |
2386 | default: | |
2387 | BUG("unexpected update strategy type: %s", | |
2388 | submodule_strategy_to_string(&ud->update_strategy)); | |
2389 | } | |
c51f8f94 | 2390 | if (must_die_on_failure) |
55b3f12c GC |
2391 | exit(128); |
2392 | ||
2393 | /* the command failed, but update must continue */ | |
c51f8f94 AR |
2394 | return 1; |
2395 | } | |
2396 | ||
55b3f12c GC |
2397 | if (ud->quiet) |
2398 | return 0; | |
2399 | ||
c51f8f94 AR |
2400 | switch (ud->update_strategy.type) { |
2401 | case SM_UPDATE_CHECKOUT: | |
2402 | printf(_("Submodule path '%s': checked out '%s'\n"), | |
2403 | ud->displaypath, oid); | |
2404 | break; | |
2405 | case SM_UPDATE_REBASE: | |
2406 | printf(_("Submodule path '%s': rebased into '%s'\n"), | |
2407 | ud->displaypath, oid); | |
2408 | break; | |
2409 | case SM_UPDATE_MERGE: | |
2410 | printf(_("Submodule path '%s': merged in '%s'\n"), | |
2411 | ud->displaypath, oid); | |
2412 | break; | |
2413 | case SM_UPDATE_COMMAND: | |
2414 | printf(_("Submodule path '%s': '%s %s'\n"), | |
2415 | ud->displaypath, ud->update_strategy.command, oid); | |
2416 | break; | |
2417 | default: | |
2418 | BUG("unexpected update strategy type: %s", | |
2419 | submodule_strategy_to_string(&ud->update_strategy)); | |
2420 | } | |
2421 | ||
2422 | return 0; | |
2423 | } | |
2424 | ||
b3c5f5cb | 2425 | static int run_update_procedure(struct update_data *ud) |
c51f8f94 AR |
2426 | { |
2427 | int subforce = is_null_oid(&ud->suboid) || ud->force; | |
2428 | ||
2429 | if (!ud->nofetch) { | |
2430 | /* | |
2431 | * Run fetch only if `oid` isn't present or it | |
2432 | * is not reachable from a ref. | |
2433 | */ | |
2434 | if (!is_tip_reachable(ud->sm_path, &ud->oid) && | |
2435 | fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) && | |
2436 | !ud->quiet) | |
2437 | fprintf_ln(stderr, | |
2438 | _("Unable to fetch in submodule path '%s'; " | |
2439 | "trying to directly fetch %s:"), | |
2440 | ud->displaypath, oid_to_hex(&ud->oid)); | |
2441 | /* | |
2442 | * Now we tried the usual fetch, but `oid` may | |
2443 | * not be reachable from any of the refs. | |
2444 | */ | |
2445 | if (!is_tip_reachable(ud->sm_path, &ud->oid) && | |
2446 | fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid)) | |
2447 | die(_("Fetched in submodule path '%s', but it did not " | |
2448 | "contain %s. Direct fetching of that commit failed."), | |
2449 | ud->displaypath, oid_to_hex(&ud->oid)); | |
2450 | } | |
2451 | ||
2452 | return run_update_command(ud, subforce); | |
2453 | } | |
2454 | ||
f3875ab1 GC |
2455 | static const char *remote_submodule_branch(const char *path) |
2456 | { | |
2457 | const struct submodule *sub; | |
2458 | const char *branch = NULL; | |
2459 | char *key; | |
2460 | ||
2461 | sub = submodule_from_path(the_repository, null_oid(), path); | |
2462 | if (!sub) | |
2463 | return NULL; | |
2464 | ||
2465 | key = xstrfmt("submodule.%s.branch", sub->name); | |
2466 | if (repo_config_get_string_tmp(the_repository, key, &branch)) | |
2467 | branch = sub->branch; | |
2468 | free(key); | |
2469 | ||
2470 | if (!branch) | |
2471 | return "HEAD"; | |
2472 | ||
2473 | if (!strcmp(branch, ".")) { | |
2474 | const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); | |
2475 | ||
2476 | if (!refname) | |
2477 | die(_("No such ref: %s"), "HEAD"); | |
2478 | ||
2479 | /* detached HEAD */ | |
2480 | if (!strcmp(refname, "HEAD")) | |
2481 | die(_("Submodule (%s) branch configured to inherit " | |
2482 | "branch from superproject, but the superproject " | |
2483 | "is not on any branch"), sub->name); | |
2484 | ||
2485 | if (!skip_prefix(refname, "refs/heads/", &refname)) | |
2486 | die(_("Expecting a full ref name, got %s"), refname); | |
2487 | return refname; | |
2488 | } | |
2489 | ||
2490 | return branch; | |
2491 | } | |
2492 | ||
2493 | static void ensure_core_worktree(const char *path) | |
2494 | { | |
2495 | const char *cw; | |
2496 | struct repository subrepo; | |
2497 | ||
2498 | if (repo_submodule_init(&subrepo, the_repository, path, null_oid())) | |
2499 | die(_("could not get a repository handle for submodule '%s'"), path); | |
2500 | ||
2501 | if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) { | |
2502 | char *cfg_file, *abs_path; | |
2503 | const char *rel_path; | |
2504 | struct strbuf sb = STRBUF_INIT; | |
2505 | ||
2506 | cfg_file = repo_git_path(&subrepo, "config"); | |
2507 | ||
2508 | abs_path = absolute_pathdup(path); | |
2509 | rel_path = relative_path(abs_path, subrepo.gitdir, &sb); | |
2510 | ||
2511 | git_config_set_in_file(cfg_file, "core.worktree", rel_path); | |
2512 | ||
2513 | free(cfg_file); | |
2514 | free(abs_path); | |
2515 | strbuf_release(&sb); | |
2516 | } | |
2517 | } | |
2518 | ||
2519 | static void update_data_to_args(struct update_data *update_data, struct strvec *args) | |
2520 | { | |
2521 | strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL); | |
2522 | strvec_pushf(args, "--jobs=%d", update_data->max_jobs); | |
2523 | if (update_data->recursive_prefix) | |
2524 | strvec_pushl(args, "--recursive-prefix", | |
2525 | update_data->recursive_prefix, NULL); | |
2526 | if (update_data->quiet) | |
2527 | strvec_push(args, "--quiet"); | |
2528 | if (update_data->force) | |
2529 | strvec_push(args, "--force"); | |
2530 | if (update_data->init) | |
2531 | strvec_push(args, "--init"); | |
2532 | if (update_data->remote) | |
2533 | strvec_push(args, "--remote"); | |
2534 | if (update_data->nofetch) | |
2535 | strvec_push(args, "--no-fetch"); | |
2536 | if (update_data->dissociate) | |
2537 | strvec_push(args, "--dissociate"); | |
2538 | if (update_data->progress) | |
2539 | strvec_push(args, "--progress"); | |
2540 | if (update_data->require_init) | |
2541 | strvec_push(args, "--require-init"); | |
2542 | if (update_data->depth) | |
2543 | strvec_pushf(args, "--depth=%d", update_data->depth); | |
2544 | if (update_data->update_default) | |
2545 | strvec_pushl(args, "--update", update_data->update_default, NULL); | |
2546 | if (update_data->references.nr) { | |
2547 | struct string_list_item *item; | |
2548 | for_each_string_list_item(item, &update_data->references) | |
2549 | strvec_pushl(args, "--reference", item->string, NULL); | |
2550 | } | |
2551 | if (update_data->filter_options && update_data->filter_options->choice) | |
2552 | strvec_pushf(args, "--filter=%s", | |
2553 | expand_list_objects_filter_spec( | |
2554 | update_data->filter_options)); | |
2555 | if (update_data->recommend_shallow == 0) | |
2556 | strvec_push(args, "--no-recommend-shallow"); | |
2557 | else if (update_data->recommend_shallow == 1) | |
2558 | strvec_push(args, "--recommend-shallow"); | |
2559 | if (update_data->single_branch >= 0) | |
2560 | strvec_push(args, update_data->single_branch ? | |
2561 | "--single-branch" : | |
2562 | "--no-single-branch"); | |
2563 | } | |
2564 | ||
2565 | static int update_submodule(struct update_data *update_data) | |
2566 | { | |
2567 | char *prefixed_path; | |
2568 | ||
2569 | ensure_core_worktree(update_data->sm_path); | |
2570 | ||
2571 | if (update_data->recursive_prefix) | |
2572 | prefixed_path = xstrfmt("%s%s", update_data->recursive_prefix, | |
2573 | update_data->sm_path); | |
2574 | else | |
2575 | prefixed_path = xstrdup(update_data->sm_path); | |
2576 | ||
2577 | update_data->displaypath = get_submodule_displaypath(prefixed_path, | |
2578 | update_data->prefix); | |
2579 | free(prefixed_path); | |
2580 | ||
2581 | determine_submodule_update_strategy(the_repository, update_data->just_cloned, | |
2582 | update_data->sm_path, update_data->update_default, | |
2583 | &update_data->update_strategy); | |
2584 | ||
2585 | if (update_data->just_cloned) | |
2586 | oidcpy(&update_data->suboid, null_oid()); | |
2587 | else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid)) | |
2588 | die(_("Unable to find current revision in submodule path '%s'"), | |
2589 | update_data->displaypath); | |
2590 | ||
2591 | if (update_data->remote) { | |
2592 | char *remote_name = get_default_remote_submodule(update_data->sm_path); | |
2593 | const char *branch = remote_submodule_branch(update_data->sm_path); | |
2594 | char *remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch); | |
2595 | ||
2596 | if (!update_data->nofetch) { | |
2597 | if (fetch_in_submodule(update_data->sm_path, update_data->depth, | |
2598 | 0, NULL)) | |
2599 | die(_("Unable to fetch in submodule path '%s'"), | |
2600 | update_data->sm_path); | |
2601 | } | |
2602 | ||
2603 | if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid)) | |
2604 | die(_("Unable to find %s revision in submodule path '%s'"), | |
2605 | remote_ref, update_data->sm_path); | |
2606 | ||
2607 | free(remote_ref); | |
2608 | } | |
2609 | ||
2610 | if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) | |
2611 | if (run_update_procedure(update_data)) | |
2612 | return 1; | |
2613 | ||
2614 | if (update_data->recursive) { | |
2615 | struct child_process cp = CHILD_PROCESS_INIT; | |
2616 | struct update_data next = *update_data; | |
2617 | int res; | |
2618 | ||
2619 | if (update_data->recursive_prefix) | |
2620 | prefixed_path = xstrfmt("%s%s/", update_data->recursive_prefix, | |
2621 | update_data->sm_path); | |
2622 | else | |
2623 | prefixed_path = xstrfmt("%s/", update_data->sm_path); | |
2624 | ||
2625 | next.recursive_prefix = get_submodule_displaypath(prefixed_path, | |
2626 | update_data->prefix); | |
2627 | next.prefix = NULL; | |
2628 | oidcpy(&next.oid, null_oid()); | |
2629 | oidcpy(&next.suboid, null_oid()); | |
2630 | ||
2631 | cp.dir = update_data->sm_path; | |
2632 | cp.git_cmd = 1; | |
2633 | prepare_submodule_repo_env(&cp.env_array); | |
2634 | update_data_to_args(&next, &cp.args); | |
2635 | ||
2636 | /* die() if child process die()'d */ | |
2637 | res = run_command(&cp); | |
2638 | if (!res) | |
2639 | return 0; | |
2640 | die_message(_("Failed to recurse into submodule path '%s'"), | |
2641 | update_data->displaypath); | |
2642 | if (res == 128) | |
2643 | exit(res); | |
2644 | else if (res) | |
2645 | return 1; | |
2646 | } | |
2647 | ||
2648 | return 0; | |
2649 | } | |
2650 | ||
c9911c93 | 2651 | static int update_submodules(struct update_data *update_data) |
90efe595 | 2652 | { |
b3c5f5cb | 2653 | int i, res = 0; |
c9911c93 | 2654 | struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT; |
90efe595 | 2655 | |
c9911c93 GC |
2656 | suc.update_data = update_data; |
2657 | run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task, | |
ee4512ed | 2658 | update_clone_start_failure, |
c9911c93 | 2659 | update_clone_task_finished, &suc, "submodule", |
ee4512ed | 2660 | "parallel/update"); |
90efe595 SB |
2661 | |
2662 | /* | |
2663 | * We saved the output and put it out all at once now. | |
2664 | * That means: | |
2665 | * - the listener does not have to interleave their (checkout) | |
2666 | * work with our fetching. The writes involved in a | |
2667 | * checkout involve more straightforward sequential I/O. | |
2668 | * - the listener can avoid doing any work if fetching failed. | |
2669 | */ | |
b3c5f5cb AR |
2670 | if (suc.quickstop) { |
2671 | res = 1; | |
2672 | goto cleanup; | |
2673 | } | |
90efe595 | 2674 | |
b3c5f5cb AR |
2675 | for (i = 0; i < suc.update_clone_nr; i++) { |
2676 | struct update_clone_data ucd = suc.update_clone[i]; | |
90efe595 | 2677 | |
b3c5f5cb AR |
2678 | oidcpy(&update_data->oid, &ucd.oid); |
2679 | update_data->just_cloned = ucd.just_cloned; | |
2680 | update_data->sm_path = ucd.sub->path; | |
2681 | ||
2682 | if (update_submodule(update_data)) | |
2683 | res = 1; | |
2684 | } | |
2685 | ||
2686 | cleanup: | |
2687 | string_list_clear(&update_data->references, 0); | |
2688 | return res; | |
90efe595 SB |
2689 | } |
2690 | ||
b3c5f5cb | 2691 | static int module_update(int argc, const char **argv, const char *prefix) |
48308681 | 2692 | { |
48308681 | 2693 | struct pathspec pathspec; |
c9911c93 | 2694 | struct update_data opt = UPDATE_DATA_INIT; |
f05da2b4 JS |
2695 | struct list_objects_filter_options filter_options; |
2696 | int ret; | |
48308681 | 2697 | |
b3c5f5cb AR |
2698 | struct option module_update_options[] = { |
2699 | OPT__FORCE(&opt.force, N_("force checkout updates"), 0), | |
49fd5b99 | 2700 | OPT_BOOL(0, "init", &opt.init, |
29a5e9e1 | 2701 | N_("initialize uninitialized submodules before update")), |
b3c5f5cb AR |
2702 | OPT_BOOL(0, "remote", &opt.remote, |
2703 | N_("use SHA-1 of submodule's remote tracking branch")), | |
2704 | OPT_BOOL(0, "recursive", &opt.recursive, | |
2705 | N_("traverse submodules recursively")), | |
2706 | OPT_BOOL('N', "no-fetch", &opt.nofetch, | |
2707 | N_("don't fetch new objects from the remote site")), | |
c9911c93 | 2708 | OPT_STRING(0, "prefix", &opt.prefix, |
48308681 SB |
2709 | N_("path"), |
2710 | N_("path into the working tree")), | |
49fd5b99 | 2711 | OPT_STRING(0, "recursive-prefix", &opt.recursive_prefix, |
48308681 SB |
2712 | N_("path"), |
2713 | N_("path into the working tree, across nested " | |
2714 | "submodule boundaries")), | |
c9911c93 | 2715 | OPT_STRING(0, "update", &opt.update_default, |
48308681 SB |
2716 | N_("string"), |
2717 | N_("rebase, merge, checkout or none")), | |
49fd5b99 | 2718 | OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"), |
48308681 | 2719 | N_("reference repository")), |
49fd5b99 | 2720 | OPT_BOOL(0, "dissociate", &opt.dissociate, |
a0ef2934 | 2721 | N_("use --reference only while cloning")), |
c9911c93 | 2722 | OPT_INTEGER(0, "depth", &opt.depth, |
e73fe3dd | 2723 | N_("create a shallow clone truncated to the " |
48308681 | 2724 | "specified number of revisions")), |
49fd5b99 | 2725 | OPT_INTEGER('j', "jobs", &opt.max_jobs, |
2335b870 | 2726 | N_("parallel jobs")), |
49fd5b99 | 2727 | OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow, |
abed000a | 2728 | N_("whether the initial clone should follow the shallow recommendation")), |
49fd5b99 ÆAB |
2729 | OPT__QUIET(&opt.quiet, N_("don't print cloning progress")), |
2730 | OPT_BOOL(0, "progress", &opt.progress, | |
72c5f883 | 2731 | N_("force cloning progress")), |
49fd5b99 | 2732 | OPT_BOOL(0, "require-init", &opt.require_init, |
0060fd15 | 2733 | N_("disallow cloning into non-empty directory")), |
49fd5b99 | 2734 | OPT_BOOL(0, "single-branch", &opt.single_branch, |
132f600b | 2735 | N_("clone only one branch, HEAD or --branch")), |
f05da2b4 | 2736 | OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options), |
48308681 SB |
2737 | OPT_END() |
2738 | }; | |
2739 | ||
2740 | const char *const git_submodule_helper_usage[] = { | |
c9d25624 GC |
2741 | N_("git submodule [--quiet] update" |
2742 | " [--init [--filter=<filter-spec>]] [--remote]" | |
2743 | " [-N|--no-fetch] [-f|--force]" | |
2744 | " [--checkout|--merge|--rebase]" | |
2745 | " [--[no-]recommend-shallow] [--reference <repository>]" | |
2746 | " [--recursive] [--[no-]single-branch] [--] [<path>...]"), | |
48308681 SB |
2747 | NULL |
2748 | }; | |
48308681 | 2749 | |
49fd5b99 ÆAB |
2750 | update_clone_config_from_gitmodules(&opt.max_jobs); |
2751 | git_config(git_update_clone_config, &opt.max_jobs); | |
f20e7c1e | 2752 | |
f05da2b4 | 2753 | memset(&filter_options, 0, sizeof(filter_options)); |
b3c5f5cb | 2754 | argc = parse_options(argc, argv, prefix, module_update_options, |
48308681 | 2755 | git_submodule_helper_usage, 0); |
c9d25624 | 2756 | |
49fd5b99 | 2757 | if (filter_options.choice && !opt.init) { |
b3c5f5cb AR |
2758 | usage_with_options(git_submodule_helper_usage, |
2759 | module_update_options); | |
c9d25624 GC |
2760 | } |
2761 | ||
49fd5b99 | 2762 | opt.filter_options = &filter_options; |
48308681 | 2763 | |
c9911c93 GC |
2764 | if (opt.update_default) |
2765 | if (parse_submodule_update_strategy(opt.update_default, | |
2766 | &opt.update_strategy) < 0) | |
48308681 SB |
2767 | die(_("bad value for update parameter")); |
2768 | ||
49fd5b99 | 2769 | if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) { |
f05da2b4 | 2770 | list_objects_filter_release(&filter_options); |
48308681 | 2771 | return 1; |
f05da2b4 | 2772 | } |
48308681 SB |
2773 | |
2774 | if (pathspec.nr) | |
49fd5b99 | 2775 | opt.warn_if_uninitialized = 1; |
48308681 | 2776 | |
49fd5b99 | 2777 | if (opt.init) { |
29a5e9e1 GC |
2778 | struct module_list list = MODULE_LIST_INIT; |
2779 | struct init_cb info = INIT_CB_INIT; | |
2780 | ||
49fd5b99 | 2781 | if (module_list_compute(argc, argv, opt.prefix, |
29a5e9e1 GC |
2782 | &pathspec, &list) < 0) |
2783 | return 1; | |
2784 | ||
2785 | /* | |
2786 | * If there are no path args and submodule.active is set then, | |
2787 | * by default, only initialize 'active' modules. | |
2788 | */ | |
2789 | if (!argc && git_config_get_value_multi("submodule.active")) | |
2790 | module_list_active(&list); | |
2791 | ||
49fd5b99 ÆAB |
2792 | info.prefix = opt.prefix; |
2793 | info.superprefix = opt.recursive_prefix; | |
2794 | if (opt.quiet) | |
29a5e9e1 GC |
2795 | info.flags |= OPT_QUIET; |
2796 | ||
2797 | for_each_listed_submodule(&list, init_submodule_cb, &info); | |
2798 | } | |
2799 | ||
49fd5b99 | 2800 | ret = update_submodules(&opt); |
f05da2b4 JS |
2801 | list_objects_filter_release(&filter_options); |
2802 | return ret; | |
48308681 SB |
2803 | } |
2804 | ||
93481a6b BW |
2805 | static int push_check(int argc, const char **argv, const char *prefix) |
2806 | { | |
2807 | struct remote *remote; | |
c7be7201 BW |
2808 | const char *superproject_head; |
2809 | char *head; | |
2810 | int detached_head = 0; | |
2811 | struct object_id head_oid; | |
93481a6b | 2812 | |
c7be7201 BW |
2813 | if (argc < 3) |
2814 | die("submodule--helper push-check requires at least 2 arguments"); | |
2815 | ||
2816 | /* | |
2817 | * superproject's resolved head ref. | |
2818 | * if HEAD then the superproject is in a detached head state, otherwise | |
2819 | * it will be the resolved head ref. | |
2820 | */ | |
2821 | superproject_head = argv[1]; | |
2822 | argv++; | |
2823 | argc--; | |
2824 | /* Get the submodule's head ref and determine if it is detached */ | |
0f2dc722 | 2825 | head = resolve_refdup("HEAD", 0, &head_oid, NULL); |
c7be7201 BW |
2826 | if (!head) |
2827 | die(_("Failed to resolve HEAD as a valid ref.")); | |
2828 | if (!strcmp(head, "HEAD")) | |
2829 | detached_head = 1; | |
93481a6b BW |
2830 | |
2831 | /* | |
2832 | * The remote must be configured. | |
2833 | * This is to avoid pushing to the exact same URL as the parent. | |
2834 | */ | |
2835 | remote = pushremote_get(argv[1]); | |
2836 | if (!remote || remote->origin == REMOTE_UNCONFIGURED) | |
2837 | die("remote '%s' not configured", argv[1]); | |
2838 | ||
2839 | /* Check the refspec */ | |
2840 | if (argc > 2) { | |
9c8361b2 | 2841 | int i; |
93481a6b | 2842 | struct ref *local_refs = get_local_heads(); |
9c8361b2 | 2843 | struct refspec refspec = REFSPEC_INIT_PUSH; |
93481a6b | 2844 | |
9c8361b2 BW |
2845 | refspec_appendn(&refspec, argv + 2, argc - 2); |
2846 | ||
2847 | for (i = 0; i < refspec.nr; i++) { | |
2848 | const struct refspec_item *rs = &refspec.items[i]; | |
93481a6b BW |
2849 | |
2850 | if (rs->pattern || rs->matching) | |
2851 | continue; | |
2852 | ||
c7be7201 BW |
2853 | /* LHS must match a single ref */ |
2854 | switch (count_refspec_match(rs->src, local_refs, NULL)) { | |
2855 | case 1: | |
2856 | break; | |
2857 | case 0: | |
2858 | /* | |
2859 | * If LHS matches 'HEAD' then we need to ensure | |
2860 | * that it matches the same named branch | |
2861 | * checked out in the superproject. | |
2862 | */ | |
2863 | if (!strcmp(rs->src, "HEAD")) { | |
2864 | if (!detached_head && | |
2865 | !strcmp(head, superproject_head)) | |
2866 | break; | |
2867 | die("HEAD does not match the named branch in the superproject"); | |
2868 | } | |
1cf01a34 | 2869 | /* fallthrough */ |
c7be7201 | 2870 | default: |
93481a6b BW |
2871 | die("src refspec '%s' must name a ref", |
2872 | rs->src); | |
c7be7201 | 2873 | } |
93481a6b | 2874 | } |
9c8361b2 | 2875 | refspec_clear(&refspec); |
93481a6b | 2876 | } |
c7be7201 | 2877 | free(head); |
93481a6b BW |
2878 | |
2879 | return 0; | |
2880 | } | |
2881 | ||
f6f85861 SB |
2882 | static int absorb_git_dirs(int argc, const char **argv, const char *prefix) |
2883 | { | |
2884 | int i; | |
2885 | struct pathspec pathspec; | |
2886 | struct module_list list = MODULE_LIST_INIT; | |
2887 | unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES; | |
2888 | ||
2889 | struct option embed_gitdir_options[] = { | |
2890 | OPT_STRING(0, "prefix", &prefix, | |
2891 | N_("path"), | |
2892 | N_("path into the working tree")), | |
2893 | OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"), | |
2894 | ABSORB_GITDIR_RECURSE_SUBMODULES), | |
2895 | OPT_END() | |
2896 | }; | |
2897 | ||
2898 | const char *const git_submodule_helper_usage[] = { | |
a282f5a9 | 2899 | N_("git submodule--helper absorb-git-dirs [<options>] [<path>...]"), |
f6f85861 SB |
2900 | NULL |
2901 | }; | |
2902 | ||
2903 | argc = parse_options(argc, argv, prefix, embed_gitdir_options, | |
2904 | git_submodule_helper_usage, 0); | |
2905 | ||
f6f85861 SB |
2906 | if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) |
2907 | return 1; | |
2908 | ||
2909 | for (i = 0; i < list.nr; i++) | |
cf7a901a | 2910 | absorb_git_dir_into_superproject(list.entries[i]->name, flags); |
f6f85861 SB |
2911 | |
2912 | return 0; | |
2913 | } | |
2914 | ||
5c2bd8b7 BW |
2915 | static int is_active(int argc, const char **argv, const char *prefix) |
2916 | { | |
2917 | if (argc != 2) | |
9af7ec30 | 2918 | die("submodule--helper is-active takes exactly 1 argument"); |
5c2bd8b7 | 2919 | |
627d9342 | 2920 | return !is_submodule_active(the_repository, argv[1]); |
5c2bd8b7 BW |
2921 | } |
2922 | ||
0383bbb9 JK |
2923 | /* |
2924 | * Exit non-zero if any of the submodule names given on the command line is | |
2925 | * invalid. If no names are given, filter stdin to print only valid names | |
2926 | * (which is primarily intended for testing). | |
2927 | */ | |
2928 | static int check_name(int argc, const char **argv, const char *prefix) | |
2929 | { | |
2930 | if (argc > 1) { | |
2931 | while (*++argv) { | |
2932 | if (check_submodule_name(*argv) < 0) | |
2933 | return 1; | |
2934 | } | |
2935 | } else { | |
2936 | struct strbuf buf = STRBUF_INIT; | |
2937 | while (strbuf_getline(&buf, stdin) != EOF) { | |
2938 | if (!check_submodule_name(buf.buf)) | |
2939 | printf("%s\n", buf.buf); | |
2940 | } | |
2941 | strbuf_release(&buf); | |
2942 | } | |
2943 | return 0; | |
2944 | } | |
2945 | ||
2502ffc0 AO |
2946 | static int module_config(int argc, const char **argv, const char *prefix) |
2947 | { | |
b5c259f2 | 2948 | enum { |
c89c4942 DL |
2949 | CHECK_WRITEABLE = 1, |
2950 | DO_UNSET = 2 | |
b5c259f2 AO |
2951 | } command = 0; |
2952 | ||
2953 | struct option module_config_options[] = { | |
2954 | OPT_CMDMODE(0, "check-writeable", &command, | |
2955 | N_("check if it is safe to write to the .gitmodules file"), | |
2956 | CHECK_WRITEABLE), | |
c89c4942 DL |
2957 | OPT_CMDMODE(0, "unset", &command, |
2958 | N_("unset the config in the .gitmodules file"), | |
2959 | DO_UNSET), | |
b5c259f2 AO |
2960 | OPT_END() |
2961 | }; | |
2962 | const char *const git_submodule_helper_usage[] = { | |
c89c4942 DL |
2963 | N_("git submodule--helper config <name> [<value>]"), |
2964 | N_("git submodule--helper config --unset <name>"), | |
959d670d | 2965 | "git submodule--helper config --check-writeable", |
b5c259f2 AO |
2966 | NULL |
2967 | }; | |
2968 | ||
2969 | argc = parse_options(argc, argv, prefix, module_config_options, | |
2970 | git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0); | |
2971 | ||
2972 | if (argc == 1 && command == CHECK_WRITEABLE) | |
2973 | return is_writing_gitmodules_ok() ? 0 : -1; | |
2974 | ||
2502ffc0 | 2975 | /* Equivalent to ACTION_GET in builtin/config.c */ |
c89c4942 | 2976 | if (argc == 2 && command != DO_UNSET) |
2502ffc0 AO |
2977 | return print_config_from_gitmodules(the_repository, argv[1]); |
2978 | ||
2979 | /* Equivalent to ACTION_SET in builtin/config.c */ | |
c89c4942 DL |
2980 | if (argc == 3 || (argc == 2 && command == DO_UNSET)) { |
2981 | const char *value = (argc == 3) ? argv[2] : NULL; | |
2982 | ||
76e9bdc4 AO |
2983 | if (!is_writing_gitmodules_ok()) |
2984 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
2985 | ||
c89c4942 | 2986 | return config_set_in_gitmodules_file_gently(argv[1], value); |
76e9bdc4 | 2987 | } |
2502ffc0 | 2988 | |
b5c259f2 | 2989 | usage_with_options(git_submodule_helper_usage, module_config_options); |
2502ffc0 AO |
2990 | } |
2991 | ||
6417cf9c SS |
2992 | static int module_set_url(int argc, const char **argv, const char *prefix) |
2993 | { | |
2994 | int quiet = 0; | |
2995 | const char *newurl; | |
2996 | const char *path; | |
2997 | char *config_name; | |
2998 | ||
2999 | struct option options[] = { | |
e73fe3dd | 3000 | OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")), |
6417cf9c SS |
3001 | OPT_END() |
3002 | }; | |
3003 | const char *const usage[] = { | |
3004 | N_("git submodule--helper set-url [--quiet] <path> <newurl>"), | |
3005 | NULL | |
3006 | }; | |
3007 | ||
3008 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
3009 | ||
3010 | if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1])) | |
3011 | usage_with_options(usage, options); | |
3012 | ||
3013 | config_name = xstrfmt("submodule.%s.url", path); | |
3014 | ||
3015 | config_set_in_gitmodules_file_gently(config_name, newurl); | |
3016 | sync_submodule(path, prefix, quiet ? OPT_QUIET : 0); | |
3017 | ||
3018 | free(config_name); | |
3019 | ||
3020 | return 0; | |
3021 | } | |
3022 | ||
2964d6e5 SS |
3023 | static int module_set_branch(int argc, const char **argv, const char *prefix) |
3024 | { | |
3025 | int opt_default = 0, ret; | |
3026 | const char *opt_branch = NULL; | |
3027 | const char *path; | |
3028 | char *config_name; | |
3029 | ||
3030 | /* | |
3031 | * We accept the `quiet` option for uniformity across subcommands, | |
3032 | * though there is nothing to make less verbose in this subcommand. | |
3033 | */ | |
3034 | struct option options[] = { | |
3035 | OPT_NOOP_NOARG('q', "quiet"), | |
3036 | OPT_BOOL('d', "default", &opt_default, | |
3037 | N_("set the default tracking branch to master")), | |
3038 | OPT_STRING('b', "branch", &opt_branch, N_("branch"), | |
3039 | N_("set the default tracking branch")), | |
3040 | OPT_END() | |
3041 | }; | |
3042 | const char *const usage[] = { | |
3043 | N_("git submodule--helper set-branch [-q|--quiet] (-d|--default) <path>"), | |
3044 | N_("git submodule--helper set-branch [-q|--quiet] (-b|--branch) <branch> <path>"), | |
3045 | NULL | |
3046 | }; | |
3047 | ||
3048 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
3049 | ||
3050 | if (!opt_branch && !opt_default) | |
3051 | die(_("--branch or --default required")); | |
3052 | ||
3053 | if (opt_branch && opt_default) | |
43ea635c | 3054 | die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default"); |
2964d6e5 SS |
3055 | |
3056 | if (argc != 1 || !(path = argv[0])) | |
3057 | usage_with_options(usage, options); | |
3058 | ||
3059 | config_name = xstrfmt("submodule.%s.branch", path); | |
3060 | ret = config_set_in_gitmodules_file_gently(config_name, opt_branch); | |
3061 | ||
3062 | free(config_name); | |
3063 | return !!ret; | |
3064 | } | |
3065 | ||
961b130d GC |
3066 | static int module_create_branch(int argc, const char **argv, const char *prefix) |
3067 | { | |
3068 | enum branch_track track; | |
3069 | int quiet = 0, force = 0, reflog = 0, dry_run = 0; | |
3070 | ||
3071 | struct option options[] = { | |
3072 | OPT__QUIET(&quiet, N_("print only error messages")), | |
3073 | OPT__FORCE(&force, N_("force creation"), 0), | |
3074 | OPT_BOOL(0, "create-reflog", &reflog, | |
3075 | N_("create the branch's reflog")), | |
75388bf5 GC |
3076 | OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", |
3077 | N_("set branch tracking configuration"), | |
3078 | PARSE_OPT_OPTARG, | |
3079 | parse_opt_tracking_mode), | |
961b130d GC |
3080 | OPT__DRY_RUN(&dry_run, |
3081 | N_("show whether the branch would be created")), | |
3082 | OPT_END() | |
3083 | }; | |
3084 | const char *const usage[] = { | |
3085 | N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start_oid> <start_name>"), | |
3086 | NULL | |
3087 | }; | |
3088 | ||
3089 | git_config(git_default_config, NULL); | |
3090 | track = git_branch_track; | |
3091 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
3092 | ||
3093 | if (argc != 3) | |
3094 | usage_with_options(usage, options); | |
3095 | ||
3096 | if (!quiet && !dry_run) | |
3097 | printf_ln(_("creating branch '%s'"), argv[0]); | |
3098 | ||
3099 | create_branches_recursively(the_repository, argv[0], argv[1], argv[2], | |
3100 | force, reflog, quiet, track, dry_run); | |
3101 | return 0; | |
3102 | } | |
1a0b78c9 | 3103 | |
8c8195e9 AR |
3104 | struct add_data { |
3105 | const char *prefix; | |
3106 | const char *branch; | |
3107 | const char *reference_path; | |
a6226fd7 | 3108 | char *sm_path; |
8c8195e9 AR |
3109 | const char *sm_name; |
3110 | const char *repo; | |
3111 | const char *realrepo; | |
3112 | int depth; | |
3113 | unsigned int force: 1; | |
3114 | unsigned int quiet: 1; | |
3115 | unsigned int progress: 1; | |
3116 | unsigned int dissociate: 1; | |
3117 | }; | |
3118 | #define ADD_DATA_INIT { .depth = -1 } | |
3119 | ||
6b615dbe | 3120 | static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path) |
8c8195e9 AR |
3121 | { |
3122 | struct child_process cp_remote = CHILD_PROCESS_INIT; | |
3123 | struct strbuf sb_remote_out = STRBUF_INIT; | |
3124 | ||
3125 | cp_remote.git_cmd = 1; | |
3126 | strvec_pushf(&cp_remote.env_array, | |
3127 | "GIT_DIR=%s", git_dir_path); | |
3128 | strvec_push(&cp_remote.env_array, "GIT_WORK_TREE=."); | |
3129 | strvec_pushl(&cp_remote.args, "remote", "-v", NULL); | |
3130 | if (!capture_command(&cp_remote, &sb_remote_out, 0)) { | |
3131 | char *next_line; | |
3132 | char *line = sb_remote_out.buf; | |
3133 | while ((next_line = strchr(line, '\n')) != NULL) { | |
3134 | size_t len = next_line - line; | |
3135 | if (strip_suffix_mem(line, &len, " (fetch)")) | |
c21fb467 | 3136 | strbuf_addf(msg, " %.*s\n", (int)len, line); |
8c8195e9 AR |
3137 | line = next_line + 1; |
3138 | } | |
3139 | } | |
3140 | ||
3141 | strbuf_release(&sb_remote_out); | |
3142 | } | |
3143 | ||
3144 | static int add_submodule(const struct add_data *add_data) | |
3145 | { | |
3146 | char *submod_gitdir_path; | |
3147 | struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT; | |
3148 | ||
3149 | /* perhaps the path already exists and is already a git repo, else clone it */ | |
3150 | if (is_directory(add_data->sm_path)) { | |
3151 | struct strbuf sm_path = STRBUF_INIT; | |
3152 | strbuf_addstr(&sm_path, add_data->sm_path); | |
3153 | submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path); | |
3154 | if (is_nonbare_repository_dir(&sm_path)) | |
3155 | printf(_("Adding existing repo at '%s' to the index\n"), | |
3156 | add_data->sm_path); | |
3157 | else | |
3158 | die(_("'%s' already exists and is not a valid git repo"), | |
3159 | add_data->sm_path); | |
3160 | strbuf_release(&sm_path); | |
3161 | free(submod_gitdir_path); | |
3162 | } else { | |
3163 | struct child_process cp = CHILD_PROCESS_INIT; | |
3164 | submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name); | |
3165 | ||
3166 | if (is_directory(submod_gitdir_path)) { | |
3167 | if (!add_data->force) { | |
c21fb467 KS |
3168 | struct strbuf msg = STRBUF_INIT; |
3169 | char *die_msg; | |
3170 | ||
3171 | strbuf_addf(&msg, _("A git directory for '%s' is found " | |
3172 | "locally with remote(s):\n"), | |
3173 | add_data->sm_name); | |
3174 | ||
6b615dbe | 3175 | append_fetch_remotes(&msg, submod_gitdir_path); |
8c8195e9 | 3176 | free(submod_gitdir_path); |
c21fb467 KS |
3177 | |
3178 | strbuf_addf(&msg, _("If you want to reuse this local git " | |
3179 | "directory instead of cloning again from\n" | |
3180 | " %s\n" | |
3181 | "use the '--force' option. If the local git " | |
3182 | "directory is not the correct repo\n" | |
3183 | "or you are unsure what this means choose " | |
3184 | "another name with the '--name' option."), | |
3185 | add_data->realrepo); | |
3186 | ||
3187 | die_msg = strbuf_detach(&msg, NULL); | |
3188 | die("%s", die_msg); | |
8c8195e9 AR |
3189 | } else { |
3190 | printf(_("Reactivating local git directory for " | |
3191 | "submodule '%s'\n"), add_data->sm_name); | |
3192 | } | |
3193 | } | |
3194 | free(submod_gitdir_path); | |
3195 | ||
3196 | clone_data.prefix = add_data->prefix; | |
3197 | clone_data.path = add_data->sm_path; | |
3198 | clone_data.name = add_data->sm_name; | |
3199 | clone_data.url = add_data->realrepo; | |
3200 | clone_data.quiet = add_data->quiet; | |
3201 | clone_data.progress = add_data->progress; | |
3202 | if (add_data->reference_path) | |
3203 | string_list_append(&clone_data.reference, | |
3204 | xstrdup(add_data->reference_path)); | |
3205 | clone_data.dissociate = add_data->dissociate; | |
3206 | if (add_data->depth >= 0) | |
3207 | clone_data.depth = xstrfmt("%d", add_data->depth); | |
3208 | ||
3209 | if (clone_submodule(&clone_data)) | |
3210 | return -1; | |
3211 | ||
3212 | prepare_submodule_repo_env(&cp.env_array); | |
3213 | cp.git_cmd = 1; | |
3214 | cp.dir = add_data->sm_path; | |
94b7f156 EN |
3215 | /* |
3216 | * NOTE: we only get here if add_data->force is true, so | |
3217 | * passing --force to checkout is reasonable. | |
3218 | */ | |
8c8195e9 AR |
3219 | strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL); |
3220 | ||
3221 | if (add_data->branch) { | |
3222 | strvec_pushl(&cp.args, "-B", add_data->branch, NULL); | |
3223 | strvec_pushf(&cp.args, "origin/%s", add_data->branch); | |
3224 | } | |
3225 | ||
3226 | if (run_command(&cp)) | |
3227 | die(_("unable to checkout submodule '%s'"), add_data->sm_path); | |
3228 | } | |
3229 | return 0; | |
3230 | } | |
3231 | ||
a452128a AR |
3232 | static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value) |
3233 | { | |
3234 | char *key; | |
3235 | int ret; | |
3236 | ||
3237 | if (!is_writing_gitmodules_ok()) | |
3238 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
3239 | ||
3240 | key = xstrfmt("submodule.%s.%s", name, var); | |
3241 | ret = config_set_in_gitmodules_file_gently(key, value); | |
3242 | free(key); | |
3243 | ||
3244 | return ret; | |
3245 | } | |
3246 | ||
3247 | static void configure_added_submodule(struct add_data *add_data) | |
3248 | { | |
3249 | char *key; | |
3250 | char *val = NULL; | |
3251 | struct child_process add_submod = CHILD_PROCESS_INIT; | |
3252 | struct child_process add_gitmodules = CHILD_PROCESS_INIT; | |
3253 | ||
3254 | key = xstrfmt("submodule.%s.url", add_data->sm_name); | |
3255 | git_config_set_gently(key, add_data->realrepo); | |
3256 | free(key); | |
3257 | ||
3258 | add_submod.git_cmd = 1; | |
3259 | strvec_pushl(&add_submod.args, "add", | |
3260 | "--no-warn-embedded-repo", NULL); | |
3261 | if (add_data->force) | |
3262 | strvec_push(&add_submod.args, "--force"); | |
3263 | strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL); | |
3264 | ||
3265 | if (run_command(&add_submod)) | |
3266 | die(_("Failed to add submodule '%s'"), add_data->sm_path); | |
3267 | ||
3268 | if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) || | |
3269 | config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo)) | |
3270 | die(_("Failed to register submodule '%s'"), add_data->sm_path); | |
3271 | ||
3272 | if (add_data->branch) { | |
3273 | if (config_submodule_in_gitmodules(add_data->sm_name, | |
3274 | "branch", add_data->branch)) | |
3275 | die(_("Failed to register submodule '%s'"), add_data->sm_path); | |
3276 | } | |
3277 | ||
3278 | add_gitmodules.git_cmd = 1; | |
3279 | strvec_pushl(&add_gitmodules.args, | |
3280 | "add", "--force", "--", ".gitmodules", NULL); | |
3281 | ||
3282 | if (run_command(&add_gitmodules)) | |
3283 | die(_("Failed to register submodule '%s'"), add_data->sm_path); | |
3284 | ||
3285 | /* | |
3286 | * NEEDSWORK: In a multi-working-tree world this needs to be | |
3287 | * set in the per-worktree config. | |
3288 | */ | |
3289 | /* | |
3290 | * NEEDSWORK: In the longer run, we need to get rid of this | |
3291 | * pattern of querying "submodule.active" before calling | |
3292 | * is_submodule_active(), since that function needs to find | |
3293 | * out the value of "submodule.active" again anyway. | |
3294 | */ | |
3295 | if (!git_config_get_string("submodule.active", &val) && val) { | |
3296 | /* | |
3297 | * If the submodule being added isn't already covered by the | |
3298 | * current configured pathspec, set the submodule's active flag | |
3299 | */ | |
3300 | if (!is_submodule_active(the_repository, add_data->sm_path)) { | |
3301 | key = xstrfmt("submodule.%s.active", add_data->sm_name); | |
3302 | git_config_set_gently(key, "true"); | |
3303 | free(key); | |
3304 | } | |
3305 | } else { | |
3306 | key = xstrfmt("submodule.%s.active", add_data->sm_name); | |
3307 | git_config_set_gently(key, "true"); | |
3308 | free(key); | |
3309 | } | |
3310 | } | |
3311 | ||
a6226fd7 | 3312 | static void die_on_index_match(const char *path, int force) |
a452128a | 3313 | { |
a6226fd7 AR |
3314 | struct pathspec ps; |
3315 | const char *args[] = { path, NULL }; | |
3316 | parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args); | |
3317 | ||
3318 | if (read_cache_preload(NULL) < 0) | |
3319 | die(_("index file corrupt")); | |
3320 | ||
3321 | if (ps.nr) { | |
3322 | int i; | |
3323 | char *ps_matched = xcalloc(ps.nr, 1); | |
3324 | ||
3325 | /* TODO: audit for interaction with sparse-index. */ | |
3326 | ensure_full_index(&the_index); | |
3327 | ||
3328 | /* | |
3329 | * Since there is only one pathspec, we just need | |
3330 | * need to check ps_matched[0] to know if a cache | |
3331 | * entry matched. | |
3332 | */ | |
3333 | for (i = 0; i < active_nr; i++) { | |
3334 | ce_path_match(&the_index, active_cache[i], &ps, | |
3335 | ps_matched); | |
3336 | ||
3337 | if (ps_matched[0]) { | |
3338 | if (!force) | |
3339 | die(_("'%s' already exists in the index"), | |
3340 | path); | |
3341 | if (!S_ISGITLINK(active_cache[i]->ce_mode)) | |
3342 | die(_("'%s' already exists in the index " | |
3343 | "and is not a submodule"), path); | |
3344 | break; | |
3345 | } | |
3346 | } | |
3347 | free(ps_matched); | |
3348 | } | |
c270b055 | 3349 | clear_pathspec(&ps); |
a6226fd7 AR |
3350 | } |
3351 | ||
3352 | static void die_on_repo_without_commits(const char *path) | |
3353 | { | |
3354 | struct strbuf sb = STRBUF_INIT; | |
3355 | strbuf_addstr(&sb, path); | |
3356 | if (is_nonbare_repository_dir(&sb)) { | |
3357 | struct object_id oid; | |
3358 | if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) | |
3359 | die(_("'%s' does not have a commit checked out"), path); | |
3360 | } | |
c270b055 | 3361 | strbuf_release(&sb); |
a6226fd7 AR |
3362 | } |
3363 | ||
3364 | static int module_add(int argc, const char **argv, const char *prefix) | |
3365 | { | |
3366 | int force = 0, quiet = 0, progress = 0, dissociate = 0; | |
a452128a | 3367 | struct add_data add_data = ADD_DATA_INIT; |
8f790151 | 3368 | char *to_free = NULL; |
a452128a AR |
3369 | |
3370 | struct option options[] = { | |
a6226fd7 AR |
3371 | OPT_STRING('b', "branch", &add_data.branch, N_("branch"), |
3372 | N_("branch of repository to add as submodule")), | |
a452128a AR |
3373 | OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"), |
3374 | PARSE_OPT_NOCOMPLETE), | |
a6226fd7 AR |
3375 | OPT__QUIET(&quiet, N_("print only error messages")), |
3376 | OPT_BOOL(0, "progress", &progress, N_("force cloning progress")), | |
3377 | OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"), | |
3378 | N_("reference repository")), | |
3379 | OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")), | |
3380 | OPT_STRING(0, "name", &add_data.sm_name, N_("name"), | |
3381 | N_("sets the submodule’s name to the given string " | |
3382 | "instead of defaulting to its path")), | |
3383 | OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")), | |
a452128a AR |
3384 | OPT_END() |
3385 | }; | |
3386 | ||
3387 | const char *const usage[] = { | |
a6226fd7 | 3388 | N_("git submodule--helper add [<options>] [--] <repository> [<path>]"), |
a452128a AR |
3389 | NULL |
3390 | }; | |
3391 | ||
3392 | argc = parse_options(argc, argv, prefix, options, usage, 0); | |
3393 | ||
a6226fd7 AR |
3394 | if (!is_writing_gitmodules_ok()) |
3395 | die(_("please make sure that the .gitmodules file is in the working tree")); | |
3396 | ||
3397 | if (prefix && *prefix && | |
3398 | add_data.reference_path && !is_absolute_path(add_data.reference_path)) | |
3399 | add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path); | |
3400 | ||
3401 | if (argc == 0 || argc > 2) | |
a452128a AR |
3402 | usage_with_options(usage, options); |
3403 | ||
a6226fd7 AR |
3404 | add_data.repo = argv[0]; |
3405 | if (argc == 1) | |
3406 | add_data.sm_path = git_url_basename(add_data.repo, 0, 0); | |
3407 | else | |
3408 | add_data.sm_path = xstrdup(argv[1]); | |
3409 | ||
3410 | if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) | |
3411 | add_data.sm_path = xstrfmt("%s%s", prefix, add_data.sm_path); | |
3412 | ||
3413 | if (starts_with_dot_dot_slash(add_data.repo) || | |
3414 | starts_with_dot_slash(add_data.repo)) { | |
3415 | if (prefix) | |
3416 | die(_("Relative path can only be used from the toplevel " | |
3417 | "of the working tree")); | |
3418 | ||
3419 | /* dereference source url relative to parent's url */ | |
8f790151 ÆAB |
3420 | to_free = resolve_relative_url(add_data.repo, NULL, 1); |
3421 | add_data.realrepo = to_free; | |
a6226fd7 AR |
3422 | } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) { |
3423 | add_data.realrepo = add_data.repo; | |
3424 | } else { | |
3425 | die(_("repo URL: '%s' must be absolute or begin with ./|../"), | |
3426 | add_data.repo); | |
3427 | } | |
3428 | ||
3429 | /* | |
3430 | * normalize path: | |
3431 | * multiple //; leading ./; /./; /../; | |
3432 | */ | |
3433 | normalize_path_copy(add_data.sm_path, add_data.sm_path); | |
3434 | strip_dir_trailing_slashes(add_data.sm_path); | |
3435 | ||
3436 | die_on_index_match(add_data.sm_path, force); | |
3437 | die_on_repo_without_commits(add_data.sm_path); | |
3438 | ||
3439 | if (!force) { | |
3440 | int exit_code = -1; | |
3441 | struct strbuf sb = STRBUF_INIT; | |
3442 | struct child_process cp = CHILD_PROCESS_INIT; | |
3443 | cp.git_cmd = 1; | |
3444 | cp.no_stdout = 1; | |
3445 | strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing", | |
3446 | "--no-warn-embedded-repo", add_data.sm_path, NULL); | |
3447 | if ((exit_code = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) { | |
3448 | strbuf_complete_line(&sb); | |
3449 | fputs(sb.buf, stderr); | |
3450 | free(add_data.sm_path); | |
3451 | return exit_code; | |
3452 | } | |
3453 | strbuf_release(&sb); | |
3454 | } | |
3455 | ||
3456 | if(!add_data.sm_name) | |
3457 | add_data.sm_name = add_data.sm_path; | |
3458 | ||
3459 | if (check_submodule_name(add_data.sm_name)) | |
3460 | die(_("'%s' is not a valid submodule name"), add_data.sm_name); | |
3461 | ||
3462 | add_data.prefix = prefix; | |
a452128a | 3463 | add_data.force = !!force; |
a6226fd7 AR |
3464 | add_data.quiet = !!quiet; |
3465 | add_data.progress = !!progress; | |
3466 | add_data.dissociate = !!dissociate; | |
3467 | ||
3468 | if (add_submodule(&add_data)) { | |
3469 | free(add_data.sm_path); | |
3470 | return 1; | |
3471 | } | |
a452128a | 3472 | configure_added_submodule(&add_data); |
a6226fd7 | 3473 | free(add_data.sm_path); |
8f790151 | 3474 | free(to_free); |
a452128a AR |
3475 | |
3476 | return 0; | |
3477 | } | |
3478 | ||
89c86265 SB |
3479 | #define SUPPORT_SUPER_PREFIX (1<<0) |
3480 | ||
74703a1e SB |
3481 | struct cmd_struct { |
3482 | const char *cmd; | |
3483 | int (*fn)(int, const char **, const char *); | |
89c86265 | 3484 | unsigned option; |
74703a1e SB |
3485 | }; |
3486 | ||
3487 | static struct cmd_struct commands[] = { | |
89c86265 SB |
3488 | {"list", module_list, 0}, |
3489 | {"name", module_name, 0}, | |
3490 | {"clone", module_clone, 0}, | |
a6226fd7 | 3491 | {"add", module_add, SUPPORT_SUPER_PREFIX}, |
b3c5f5cb | 3492 | {"update", module_update, 0}, |
89c86265 | 3493 | {"resolve-relative-url-test", resolve_relative_url_test, 0}, |
fc1b9243 | 3494 | {"foreach", module_foreach, SUPPORT_SUPER_PREFIX}, |
6e7c14e6 | 3495 | {"init", module_init, SUPPORT_SUPER_PREFIX}, |
a9f8a375 | 3496 | {"status", module_status, SUPPORT_SUPER_PREFIX}, |
13424764 | 3497 | {"sync", module_sync, SUPPORT_SUPER_PREFIX}, |
2e612731 | 3498 | {"deinit", module_deinit, 0}, |
e83e3333 | 3499 | {"summary", module_summary, SUPPORT_SUPER_PREFIX}, |
93481a6b | 3500 | {"push-check", push_check, 0}, |
f6f85861 | 3501 | {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX}, |
5c2bd8b7 | 3502 | {"is-active", is_active, 0}, |
0383bbb9 | 3503 | {"check-name", check_name, 0}, |
2502ffc0 | 3504 | {"config", module_config, 0}, |
6417cf9c | 3505 | {"set-url", module_set_url, 0}, |
2964d6e5 | 3506 | {"set-branch", module_set_branch, 0}, |
961b130d | 3507 | {"create-branch", module_create_branch, 0}, |
74703a1e SB |
3508 | }; |
3509 | ||
3510 | int cmd_submodule__helper(int argc, const char **argv, const char *prefix) | |
3511 | { | |
3512 | int i; | |
f0994fa8 JK |
3513 | if (argc < 2 || !strcmp(argv[1], "-h")) |
3514 | usage("git submodule--helper <command>"); | |
74703a1e | 3515 | |
89c86265 SB |
3516 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
3517 | if (!strcmp(argv[1], commands[i].cmd)) { | |
3518 | if (get_super_prefix() && | |
3519 | !(commands[i].option & SUPPORT_SUPER_PREFIX)) | |
3520 | die(_("%s doesn't support --super-prefix"), | |
3521 | commands[i].cmd); | |
74703a1e | 3522 | return commands[i].fn(argc - 1, argv + 1, prefix); |
89c86265 SB |
3523 | } |
3524 | } | |
74703a1e | 3525 | |
cdc04b65 | 3526 | die(_("'%s' is not a valid submodule--helper " |
74703a1e SB |
3527 | "subcommand"), argv[1]); |
3528 | } |