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