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