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