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