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