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