Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/git.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / git.c` 1016 lines ยท 30923 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 3 #include "builtin.h" 4 #include "config.h" 5 #include "environment.h" 6 #include "exec-cmd.h" 7 #include "gettext.h" 8 #include "help.h" 9 #include "object-file.h" 10 #include "pager.h" 11 #include "read-cache-ll.h" 12 #include "run-command.h" 13 #include "alias.h" 14 #include "replace-object.h" 15 #include "setup.h" 16 #include "attr.h" 17 #include "shallow.h" 18 #include "trace.h" 19 #include "trace2.h" 20 21 #define RUN_SETUP (1<<0) 22 #define RUN_SETUP_GENTLY (1<<1) 23 #define USE_PAGER (1<<2) 24 /* 25 * require working tree to be present -- anything uses this needs 26 * RUN_SETUP for reading from the configuration file. 27 */ 28 #define NEED_WORK_TREE (1<<3) 29 #define DELAY_PAGER_CONFIG (1<<4) 30 #define NO_PARSEOPT (1<<5) /* parse-options is not used */ 31 #define DEPRECATED (1<<6) 32 33 struct cmd_struct { 34 const char *cmd; 35 int (*fn)(int, const char **, const char *, struct repository *); 36 unsigned int option; 37 }; 38 39 const char git_usage_string[] = 40 N_("git [-v | --version] [-h | --help] [-C ] [-c =]\n" 41 " [--exec-path[=]] [--html-path] [--man-path] [--info-path]\n" 42 " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]\n" 43 " [--no-optional-locks] [--no-advice] [--bare] [--git-dir=]\n" 44 " [--work-tree=] [--namespace=] [--config-env==]\n" 45 " []"); 46 47 const char git_more_info_string[] = 48 N_("'git help -a' and 'git help -g' list available subcommands and some\n" 49 "concept guides. See 'git help ' or 'git help '\n" 50 "to read about a specific subcommand or concept.\n" 51 "See 'git help git' for an overview of the system."); 52 53 static int use_pager = -1; 54 55 static void list_builtins(struct string_list *list, 56 unsigned int include_option, 57 unsigned int exclude_option); 58 59 static void exclude_helpers_from_list(struct string_list *list) 60 { 61 size_t i = 0; 62 63 while (i < list->nr) { 64 if (strstr(list->items[i].string, "--")) 65 unsorted_string_list_delete_item(list, i, 0); 66 else 67 i++; 68 } 69 } 70 71 static int match_token(const char *spec, int len, const char *token) 72 { 73 int token_len = strlen(token); 74 75 return len == token_len && !strncmp(spec, token, token_len); 76 } 77 78 static int list_cmds(const char *spec) 79 { 80 struct string_list list = STRING_LIST_INIT_DUP; 81 int nongit; 82 83 /* 84 * Set up the repository so we can pick up any repo-level config (like 85 * completion.commands). 86 */ 87 setup_git_directory_gently(the_repository, &nongit); 88 89 while (*spec) { 90 const char *sep = strchrnul(spec, ','); 91 int len = sep - spec; 92 93 if (match_token(spec, len, "builtins")) 94 list_builtins(&list, 0, 0); 95 else if (match_token(spec, len, "main")) 96 list_all_main_cmds(&list); 97 else if (match_token(spec, len, "others")) 98 list_all_other_cmds(&list); 99 else if (match_token(spec, len, "nohelpers")) 100 exclude_helpers_from_list(&list); 101 else if (match_token(spec, len, "alias")) 102 list_aliases(&list); 103 else if (match_token(spec, len, "config")) 104 list_cmds_by_config(&list); 105 else if (match_token(spec, len, "deprecated")) 106 list_builtins(&list, DEPRECATED, 0); 107 else if (len > 5 && !strncmp(spec, "list-", 5)) { 108 struct strbuf sb = STRBUF_INIT; 109 110 strbuf_add(&sb, spec + 5, len - 5); 111 list_cmds_by_category(&list, sb.buf); 112 strbuf_release(&sb); 113 } 114 else 115 die(_("unsupported command listing type '%s'"), spec); 116 spec += len; 117 if (*spec == ',') 118 spec++; 119 } 120 for (size_t i = 0; i < list.nr; i++) 121 puts(list.items[i].string); 122 string_list_clear(&list, 1); 123 return 0; 124 } 125 126 static void commit_pager_choice(void) 127 { 128 switch (use_pager) { 129 case 0: 130 setenv("GIT_PAGER", "cat", 1); 131 break; 132 case 1: 133 setup_pager(the_repository); 134 break; 135 default: 136 break; 137 } 138 } 139 140 void setup_auto_pager(const char *cmd, int def) 141 { 142 if (use_pager != -1 || pager_in_use()) 143 return; 144 use_pager = check_pager_config(the_repository, cmd); 145 if (use_pager == -1) 146 use_pager = def; 147 commit_pager_choice(); 148 } 149 150 static void print_system_path(const char *path) 151 { 152 char *s_path = system_path(path); 153 puts(s_path); 154 free(s_path); 155 } 156 157 static int handle_options(const char ***argv, int *argc, int *envchanged) 158 { 159 const char **orig_argv = *argv; 160 161 while (*argc > 0) { 162 const char *cmd = (*argv)[0]; 163 if (cmd[0] != '-') 164 break; 165 166 /* 167 * For legacy reasons, the "version" and "help" 168 * commands can be written with "--" prepended 169 * to make them look like flags. 170 */ 171 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") || 172 !strcmp(cmd, "--version") || !strcmp(cmd, "-v")) 173 break; 174 175 /* 176 * Check remaining flags. 177 */ 178 if (skip_prefix(cmd, "--exec-path", &cmd)) { 179 if (*cmd == '=') 180 git_set_exec_path(cmd + 1); 181 else { 182 puts(git_exec_path()); 183 trace2_cmd_name("_query_"); 184 exit(0); 185 } 186 } else if (!strcmp(cmd, "--html-path")) { 187 print_system_path(GIT_HTML_PATH); 188 trace2_cmd_name("_query_"); 189 exit(0); 190 } else if (!strcmp(cmd, "--man-path")) { 191 print_system_path(GIT_MAN_PATH); 192 trace2_cmd_name("_query_"); 193 exit(0); 194 } else if (!strcmp(cmd, "--info-path")) { 195 print_system_path(GIT_INFO_PATH); 196 trace2_cmd_name("_query_"); 197 exit(0); 198 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { 199 use_pager = 1; 200 } else if (!strcmp(cmd, "-P") || !strcmp(cmd, "--no-pager")) { 201 use_pager = 0; 202 if (envchanged) 203 *envchanged = 1; 204 } else if (!strcmp(cmd, "--no-lazy-fetch")) { 205 the_repository->fetch_if_missing = 0; 206 setenv(NO_LAZY_FETCH_ENVIRONMENT, "1", 1); 207 if (envchanged) 208 *envchanged = 1; 209 } else if (!strcmp(cmd, "--no-replace-objects")) { 210 disable_replace_refs(); 211 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1); 212 if (envchanged) 213 *envchanged = 1; 214 } else if (!strcmp(cmd, "--git-dir")) { 215 if (*argc < 2) { 216 fprintf(stderr, _("no directory given for '%s' option\n" ), "--git-dir"); 217 usage(git_usage_string); 218 } 219 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); 220 if (envchanged) 221 *envchanged = 1; 222 (*argv)++; 223 (*argc)--; 224 } else if (skip_prefix(cmd, "--git-dir=", &cmd)) { 225 setenv(GIT_DIR_ENVIRONMENT, cmd, 1); 226 if (envchanged) 227 *envchanged = 1; 228 } else if (!strcmp(cmd, "--namespace")) { 229 if (*argc < 2) { 230 fprintf(stderr, _("no namespace given for --namespace\n" )); 231 usage(git_usage_string); 232 } 233 setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1); 234 if (envchanged) 235 *envchanged = 1; 236 (*argv)++; 237 (*argc)--; 238 } else if (skip_prefix(cmd, "--namespace=", &cmd)) { 239 setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1); 240 if (envchanged) 241 *envchanged = 1; 242 } else if (!strcmp(cmd, "--work-tree")) { 243 if (*argc < 2) { 244 fprintf(stderr, _("no directory given for '%s' option\n" ), "--work-tree"); 245 usage(git_usage_string); 246 } 247 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); 248 if (envchanged) 249 *envchanged = 1; 250 (*argv)++; 251 (*argc)--; 252 } else if (skip_prefix(cmd, "--work-tree=", &cmd)) { 253 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1); 254 if (envchanged) 255 *envchanged = 1; 256 } else if (!strcmp(cmd, "--bare")) { 257 char *cwd = xgetcwd(); 258 startup_info->force_bare_repository = true; 259 setenv(GIT_DIR_ENVIRONMENT, cwd, 0); 260 free(cwd); 261 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1); 262 if (envchanged) 263 *envchanged = 1; 264 } else if (!strcmp(cmd, "-c")) { 265 if (*argc < 2) { 266 fprintf(stderr, _("-c expects a configuration string\n" )); 267 usage(git_usage_string); 268 } 269 git_config_push_parameter((*argv)[1]); 270 (*argv)++; 271 (*argc)--; 272 } else if (!strcmp(cmd, "--config-env")) { 273 if (*argc < 2) { 274 fprintf(stderr, _("no config key given for --config-env\n" )); 275 usage(git_usage_string); 276 } 277 git_config_push_env((*argv)[1]); 278 (*argv)++; 279 (*argc)--; 280 } else if (skip_prefix(cmd, "--config-env=", &cmd)) { 281 git_config_push_env(cmd); 282 } else if (!strcmp(cmd, "--literal-pathspecs")) { 283 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1); 284 if (envchanged) 285 *envchanged = 1; 286 } else if (!strcmp(cmd, "--no-literal-pathspecs")) { 287 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1); 288 if (envchanged) 289 *envchanged = 1; 290 } else if (!strcmp(cmd, "--glob-pathspecs")) { 291 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1); 292 if (envchanged) 293 *envchanged = 1; 294 } else if (!strcmp(cmd, "--noglob-pathspecs")) { 295 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1); 296 if (envchanged) 297 *envchanged = 1; 298 } else if (!strcmp(cmd, "--icase-pathspecs")) { 299 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1); 300 if (envchanged) 301 *envchanged = 1; 302 } else if (!strcmp(cmd, "--no-optional-locks")) { 303 setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1); 304 if (envchanged) 305 *envchanged = 1; 306 } else if (!strcmp(cmd, "--shallow-file")) { 307 if (*argc < 2) { 308 fprintf(stderr, _("no file given for '%s' option\n" ), "--shallow-file"); 309 usage(git_usage_string); 310 } 311 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[1], 1); 312 if (envchanged) 313 *envchanged = 1; 314 (*argv)++; 315 (*argc)--; 316 } else if (!strcmp(cmd, "-C")) { 317 if (*argc < 2) { 318 fprintf(stderr, _("no directory given for '%s' option\n" ), "-C"); 319 usage(git_usage_string); 320 } 321 if ((*argv)[1][0]) { 322 if (chdir((*argv)[1])) 323 die_errno("cannot change to '%s'", (*argv)[1]); 324 if (envchanged) 325 *envchanged = 1; 326 } 327 (*argv)++; 328 (*argc)--; 329 } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) { 330 trace2_cmd_name("_query_"); 331 if (!strcmp(cmd, "parseopt")) { 332 struct string_list list = STRING_LIST_INIT_DUP; 333 334 list_builtins(&list, 0, NO_PARSEOPT); 335 for (size_t i = 0; i < list.nr; i++) 336 printf("%s ", list.items[i].string); 337 string_list_clear(&list, 0); 338 exit(0); 339 } else { 340 exit(list_cmds(cmd)); 341 } 342 } else if (!strcmp(cmd, "--attr-source")) { 343 if (*argc < 2) { 344 fprintf(stderr, _("no attribute source given for --attr-source\n" )); 345 usage(git_usage_string); 346 } 347 setenv(GIT_ATTR_SOURCE_ENVIRONMENT, (*argv)[1], 1); 348 if (envchanged) 349 *envchanged = 1; 350 (*argv)++; 351 (*argc)--; 352 } else if (skip_prefix(cmd, "--attr-source=", &cmd)) { 353 set_git_attr_source(cmd); 354 setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1); 355 if (envchanged) 356 *envchanged = 1; 357 } else if (!strcmp(cmd, "--no-advice")) { 358 setenv(GIT_ADVICE_ENVIRONMENT, "0", 1); 359 if (envchanged) 360 *envchanged = 1; 361 } else { 362 fprintf(stderr, _("unknown option: %s\n"), cmd); 363 usage(git_usage_string); 364 } 365 366 (*argv)++; 367 (*argc)--; 368 } 369 return (*argv) - orig_argv; 370 } 371 372 static int handle_alias(struct strvec *args, struct string_list *expanded_aliases) 373 { 374 int envchanged = 0, ret = 0, saved_errno = errno; 375 int count, option_count; 376 const char **new_argv; 377 const char *alias_command; 378 char *alias_string; 379 380 alias_command = args->v[0]; 381 alias_string = alias_lookup(alias_command); 382 if (alias_string) { 383 struct string_list_item *seen; 384 385 if (args->nr == 2 && !strcmp(args->v[1], "-h")) 386 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), 387 alias_command, alias_string); 388 if (alias_string[0] == '!') { 389 struct child_process child = CHILD_PROCESS_INIT; 390 int nongit_ok; 391 392 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */ 393 setup_git_directory_gently(the_repository, &nongit_ok); 394 395 commit_pager_choice(); 396 397 child.use_shell = 1; 398 child.clean_on_exit = 1; 399 child.wait_after_clean = 1; 400 child.trace2_child_class = "shell_alias"; 401 strvec_push(&child.args, alias_string + 1); 402 strvec_pushv(&child.args, args->v + 1); 403 404 trace2_cmd_alias(alias_command, child.args.v); 405 trace2_cmd_name("_run_shell_alias_"); 406 407 ret = run_command(&child); 408 if (ret >= 0) /* normal exit */ 409 exit(ret); 410 411 die_errno(_("while expanding alias '%s': '%s'"), 412 alias_command, alias_string + 1); 413 } 414 count = split_cmdline(alias_string, &new_argv); 415 if (count < 0) 416 die(_("bad alias.%s string: %s"), alias_command, 417 _(split_cmdline_strerror(count))); 418 option_count = handle_options(&new_argv, &count, &envchanged); 419 if (envchanged) 420 die(_("alias '%s' changes environment variables.\n" 421 "You can use '!git' in the alias to do this"), 422 alias_command); 423 MOVE_ARRAY(new_argv - option_count, new_argv, count); 424 new_argv -= option_count; 425 426 if (count < 1) 427 die(_("empty alias for %s"), alias_command); 428 429 if (!strcmp(alias_command, new_argv[0])) 430 die(_("recursive alias: %s"), alias_command); 431 432 string_list_append(expanded_aliases, alias_command); 433 seen = unsorted_string_list_lookup(expanded_aliases, 434 new_argv[0]); 435 436 if (seen) { 437 struct strbuf sb = STRBUF_INIT; 438 for (size_t i = 0; i < expanded_aliases->nr; i++) { 439 struct string_list_item *item = &expanded_aliases->items[i]; 440 441 strbuf_addf(&sb, "\n %s", item->string); 442 if (item == seen) 443 strbuf_addstr(&sb, " <=="); 444 else if (i == expanded_aliases->nr - 1) 445 strbuf_addstr(&sb, " ==>"); 446 } 447 die(_("alias loop detected: expansion of '%s' does" 448 " not terminate:%s"), expanded_aliases->items[0].string, sb.buf); 449 } 450 451 trace_argv_printf(new_argv, 452 "trace: alias expansion: %s =>", 453 alias_command); 454 trace2_cmd_alias(alias_command, new_argv); 455 456 /* Replace the alias with the new arguments. */ 457 strvec_splice(args, 0, 1, new_argv, count); 458 459 free(alias_string); 460 free(new_argv); 461 462 ret = 1; 463 } 464 465 errno = saved_errno; 466 467 return ret; 468 } 469 470 static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct repository *repo) 471 { 472 int status, help; 473 int no_repo = 1; 474 struct stat st; 475 const char *prefix; 476 int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY)); 477 478 help = argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help-all")); 479 if (help && (run_setup & RUN_SETUP)) 480 /* demote to GENTLY to allow 'git cmd -h' outside repo */ 481 run_setup = RUN_SETUP_GENTLY; 482 483 if (run_setup & RUN_SETUP) { 484 prefix = setup_git_directory(the_repository); 485 no_repo = 0; 486 } else if (run_setup & RUN_SETUP_GENTLY) { 487 prefix = setup_git_directory_gently(the_repository, &no_repo); 488 } else { 489 prefix = NULL; 490 } 491 assert(!prefix || *prefix); 492 precompose_argv_prefix(argc, argv, NULL); 493 if (use_pager == -1 && run_setup && 494 !(p->option & DELAY_PAGER_CONFIG)) 495 use_pager = check_pager_config(repo, p->cmd); 496 if (use_pager == -1 && p->option & USE_PAGER) 497 use_pager = 1; 498 if (run_setup && startup_info->have_repository) 499 /* get_git_dir() may set up repo, avoid that */ 500 trace_repo_setup(repo); 501 commit_pager_choice(); 502 503 if (!help && p->option & NEED_WORK_TREE) 504 setup_work_tree(the_repository); 505 506 trace_argv_printf(argv, "trace: built-in: git"); 507 trace2_cmd_name(p->cmd); 508 509 validate_cache_entries(repo->index); 510 status = p->fn(argc, argv, prefix, no_repo ? NULL : repo); 511 validate_cache_entries(repo->index); 512 513 if (status) 514 return status; 515 516 /* Somebody closed stdout? */ 517 if (fstat(fileno(stdout), &st)) 518 return 0; 519 /* Ignore write errors for pipes and sockets.. */ 520 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) 521 return 0; 522 523 /* Check for ENOSPC and EIO errors.. */ 524 if (fflush(stdout)) 525 die_errno(_("write failure on standard output")); 526 if (ferror(stdout)) 527 die(_("unknown write failure on standard output")); 528 if (fclose(stdout)) 529 die_errno(_("close failed on standard output")); 530 return 0; 531 } 532 533 static struct cmd_struct commands[] = { 534 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE }, 535 { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE }, 536 { "annotate", cmd_annotate, RUN_SETUP }, 537 { "apply", cmd_apply, RUN_SETUP_GENTLY }, 538 { "archive", cmd_archive, RUN_SETUP_GENTLY }, 539 { "backfill", cmd_backfill, RUN_SETUP }, 540 { "bisect", cmd_bisect, RUN_SETUP }, 541 { "blame", cmd_blame, RUN_SETUP }, 542 { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG }, 543 { "bugreport", cmd_bugreport, RUN_SETUP_GENTLY }, 544 { "bundle", cmd_bundle, RUN_SETUP_GENTLY }, 545 { "cat-file", cmd_cat_file, RUN_SETUP }, 546 { "check-attr", cmd_check_attr, RUN_SETUP }, 547 { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE }, 548 { "check-mailmap", cmd_check_mailmap, RUN_SETUP }, 549 { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT }, 550 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE }, 551 { "checkout--worker", cmd_checkout__worker, 552 RUN_SETUP | NEED_WORK_TREE }, 553 { "checkout-index", cmd_checkout_index, 554 RUN_SETUP | NEED_WORK_TREE}, 555 { "cherry", cmd_cherry, RUN_SETUP }, 556 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE }, 557 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE }, 558 { "clone", cmd_clone }, 559 { "column", cmd_column, RUN_SETUP_GENTLY }, 560 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE }, 561 { "commit-graph", cmd_commit_graph, RUN_SETUP }, 562 { "commit-tree", cmd_commit_tree, RUN_SETUP }, 563 { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, 564 { "count-objects", cmd_count_objects, RUN_SETUP }, 565 { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, 566 { "credential-cache", cmd_credential_cache }, 567 { "credential-cache--daemon", cmd_credential_cache_daemon }, 568 { "credential-store", cmd_credential_store }, 569 { "describe", cmd_describe, RUN_SETUP }, 570 { "diagnose", cmd_diagnose, RUN_SETUP_GENTLY }, 571 { "diff", cmd_diff, NO_PARSEOPT }, 572 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 573 { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT }, 574 { "diff-pairs", cmd_diff_pairs, RUN_SETUP | NO_PARSEOPT }, 575 { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT }, 576 { "difftool", cmd_difftool, RUN_SETUP_GENTLY }, 577 { "fast-export", cmd_fast_export, RUN_SETUP }, 578 { "fast-import", cmd_fast_import, RUN_SETUP | NO_PARSEOPT }, 579 { "fetch", cmd_fetch, RUN_SETUP }, 580 { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT }, 581 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, 582 { "for-each-ref", cmd_for_each_ref, RUN_SETUP }, 583 { "for-each-repo", cmd_for_each_repo, RUN_SETUP_GENTLY }, 584 { "format-patch", cmd_format_patch, RUN_SETUP }, 585 { "format-rev", cmd_format_rev, RUN_SETUP }, 586 { "fsck", cmd_fsck, RUN_SETUP }, 587 { "fsck-objects", cmd_fsck, RUN_SETUP }, 588 { "fsmonitor--daemon", cmd_fsmonitor__daemon, RUN_SETUP }, 589 { "gc", cmd_gc, RUN_SETUP }, 590 { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT }, 591 { "grep", cmd_grep, RUN_SETUP_GENTLY }, 592 { "hash-object", cmd_hash_object }, 593 { "help", cmd_help }, 594 { "history", cmd_history, RUN_SETUP }, 595 { "hook", cmd_hook, RUN_SETUP_GENTLY }, 596 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, 597 { "init", cmd_init_db }, 598 { "init-db", cmd_init_db }, 599 { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY }, 600 { "last-modified", cmd_last_modified, RUN_SETUP }, 601 { "log", cmd_log, RUN_SETUP }, 602 { "ls-files", cmd_ls_files, RUN_SETUP }, 603 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, 604 { "ls-tree", cmd_ls_tree, RUN_SETUP }, 605 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY }, 606 { "mailsplit", cmd_mailsplit, NO_PARSEOPT }, 607 { "maintenance", cmd_maintenance, RUN_SETUP }, 608 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE }, 609 { "merge-base", cmd_merge_base, RUN_SETUP }, 610 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY }, 611 { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT }, 612 { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT }, 613 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 614 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 615 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 616 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 617 { "merge-tree", cmd_merge_tree, RUN_SETUP }, 618 { "mktag", cmd_mktag, RUN_SETUP }, 619 { "mktree", cmd_mktree, RUN_SETUP }, 620 { "multi-pack-index", cmd_multi_pack_index, RUN_SETUP }, 621 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, 622 { "name-rev", cmd_name_rev, RUN_SETUP }, 623 { "notes", cmd_notes, RUN_SETUP }, 624 { "pack-objects", cmd_pack_objects, RUN_SETUP }, 625 #ifndef WITH_BREAKING_CHANGES 626 { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT | DEPRECATED }, 627 #endif 628 { "pack-refs", cmd_pack_refs, RUN_SETUP }, 629 { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT }, 630 { "pickaxe", cmd_blame, RUN_SETUP }, 631 { "prune", cmd_prune, RUN_SETUP }, 632 { "prune-packed", cmd_prune_packed, RUN_SETUP }, 633 { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE }, 634 { "push", cmd_push, RUN_SETUP }, 635 { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, 636 { "read-tree", cmd_read_tree, RUN_SETUP }, 637 { "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE }, 638 { "receive-pack", cmd_receive_pack }, 639 { "reflog", cmd_reflog, RUN_SETUP }, 640 { "refs", cmd_refs, RUN_SETUP }, 641 { "remote", cmd_remote, RUN_SETUP }, 642 { "remote-ext", cmd_remote_ext, NO_PARSEOPT }, 643 { "remote-fd", cmd_remote_fd, NO_PARSEOPT }, 644 { "repack", cmd_repack, RUN_SETUP }, 645 { "replace", cmd_replace, RUN_SETUP }, 646 { "replay", cmd_replay, RUN_SETUP }, 647 { "repo", cmd_repo, RUN_SETUP }, 648 { "rerere", cmd_rerere, RUN_SETUP }, 649 { "reset", cmd_reset, RUN_SETUP }, 650 { "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE }, 651 { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, 652 { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, 653 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, 654 { "rm", cmd_rm, RUN_SETUP }, 655 { "send-pack", cmd_send_pack, RUN_SETUP }, 656 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER }, 657 { "show", cmd_show, RUN_SETUP }, 658 { "show-branch", cmd_show_branch, RUN_SETUP }, 659 { "show-index", cmd_show_index, RUN_SETUP_GENTLY }, 660 { "show-ref", cmd_show_ref, RUN_SETUP }, 661 { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP }, 662 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, 663 { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE }, 664 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, 665 { "stripspace", cmd_stripspace }, 666 { "submodule--helper", cmd_submodule__helper, RUN_SETUP }, 667 { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE }, 668 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, 669 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, 670 { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT }, 671 { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT }, 672 { "update-index", cmd_update_index, RUN_SETUP }, 673 { "update-ref", cmd_update_ref, RUN_SETUP }, 674 { "update-server-info", cmd_update_server_info, RUN_SETUP }, 675 { "upload-archive", cmd_upload_archive, NO_PARSEOPT }, 676 { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT }, 677 { "upload-pack", cmd_upload_pack }, 678 { "url-parse", cmd_url_parse }, 679 { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT }, 680 { "verify-commit", cmd_verify_commit, RUN_SETUP }, 681 { "verify-pack", cmd_verify_pack }, 682 { "verify-tag", cmd_verify_tag, RUN_SETUP }, 683 { "version", cmd_version }, 684 #ifndef WITH_BREAKING_CHANGES 685 { "whatchanged", cmd_whatchanged, RUN_SETUP | DEPRECATED }, 686 #endif 687 { "worktree", cmd_worktree, RUN_SETUP }, 688 { "write-tree", cmd_write_tree, RUN_SETUP }, 689 }; 690 691 static struct cmd_struct *get_builtin(const char *s) 692 { 693 for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { 694 struct cmd_struct *p = commands + i; 695 if (!strcmp(s, p->cmd)) 696 return p; 697 } 698 return NULL; 699 } 700 701 int is_builtin(const char *s) 702 { 703 return !!get_builtin(s); 704 } 705 706 static void list_builtins(struct string_list *out, 707 unsigned int include_option, 708 unsigned int exclude_option) 709 { 710 if (include_option && exclude_option) 711 BUG("'include_option' and 'exclude_option' are mutually exclusive"); 712 for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { 713 if (include_option && !(commands[i].option & include_option)) 714 continue; 715 if (exclude_option && (commands[i].option & exclude_option)) 716 continue; 717 string_list_append(out, commands[i].cmd); 718 } 719 } 720 721 void load_builtin_commands(const char *prefix, struct cmdnames *cmds) 722 { 723 const char *name; 724 725 /* 726 * Callers can ask for a subset of the commands based on a certain 727 * prefix, which is then dropped from the added names. The names in 728 * the `commands[]` array do not have the `git-` prefix, though, 729 * therefore we must expect the `prefix` to at least start with `git-`. 730 */ 731 if (!skip_prefix(prefix, "git-", &prefix)) 732 BUG("prefix '%s' must start with 'git-'", prefix); 733 734 for (size_t i = 0; i < ARRAY_SIZE(commands); i++) 735 if (skip_prefix(commands[i].cmd, prefix, &name)) 736 add_cmdname(cmds, name, strlen(name)); 737 } 738 739 #ifdef STRIP_EXTENSION 740 static void strip_extension(struct strvec *args) 741 { 742 size_t len; 743 744 if (strip_suffix(args->v[0], STRIP_EXTENSION, &len)) { 745 char *stripped = xmemdupz(args->v[0], len); 746 strvec_replace(args, 0, stripped); 747 free(stripped); 748 } 749 } 750 #else 751 #define strip_extension(cmd) 752 #endif 753 754 static void handle_builtin(struct strvec *args) 755 { 756 const char *cmd; 757 struct cmd_struct *builtin; 758 759 strip_extension(args); 760 cmd = args->v[0]; 761 762 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */ 763 if (args->nr > 1 && !strcmp(args->v[1], "--help")) { 764 const char *exclude_guides_arg[] = { "--exclude-guides" }; 765 766 strvec_replace(args, 1, args->v[0]); 767 strvec_replace(args, 0, "help"); 768 cmd = "help"; 769 strvec_splice(args, 2, 0, exclude_guides_arg, 770 ARRAY_SIZE(exclude_guides_arg)); 771 } 772 773 builtin = get_builtin(cmd); 774 if (builtin) { 775 const char **argv_copy = NULL; 776 int ret; 777 778 /* 779 * `run_builtin()` will modify the argv array, so we need to 780 * create a shallow copy such that we can free all of its 781 * strings. 782 */ 783 if (args->nr) 784 DUP_ARRAY(argv_copy, args->v, args->nr + 1); 785 786 ret = run_builtin(builtin, args->nr, argv_copy, the_repository); 787 strvec_clear(args); 788 free(argv_copy); 789 exit(ret); 790 } 791 } 792 793 static void execv_dashed_external(const char **argv) 794 { 795 struct child_process cmd = CHILD_PROCESS_INIT; 796 int status; 797 798 if (use_pager == -1 && !is_builtin(argv[0])) 799 use_pager = check_pager_config(the_repository, argv[0]); 800 commit_pager_choice(); 801 802 strvec_pushf(&cmd.args, "git-%s", argv[0]); 803 strvec_pushv(&cmd.args, argv + 1); 804 cmd.clean_on_exit = 1; 805 cmd.wait_after_clean = 1; 806 cmd.silent_exec_failure = 1; 807 cmd.trace2_child_class = "dashed"; 808 809 trace2_cmd_name("_run_dashed_"); 810 811 /* 812 * The code in run_command() logs trace2 child_start/child_exit 813 * events, so we do not need to report exec/exec_result events here. 814 */ 815 trace_argv_printf(cmd.args.v, "trace: exec:"); 816 817 /* 818 * If we fail because the command is not found, it is 819 * OK to return. Otherwise, we just pass along the status code, 820 * or our usual generic code if we were not even able to exec 821 * the program. 822 */ 823 status = run_command(&cmd); 824 825 /* 826 * If the child process ran and we are now going to exit, emit a 827 * generic string as our trace2 command verb to indicate that we 828 * launched a dashed command. 829 */ 830 if (status >= 0) 831 exit(status); 832 else if (errno != ENOENT) 833 exit(128); 834 } 835 836 static int is_deprecated_command(const char *cmd) 837 { 838 struct cmd_struct *builtin = get_builtin(cmd); 839 return builtin && (builtin->option & DEPRECATED); 840 } 841 842 static int run_argv(struct strvec *args) 843 { 844 int done_alias = 0; 845 struct string_list expanded_aliases = STRING_LIST_INIT_DUP; 846 847 while (1) { 848 /* 849 * Allow deprecated commands to be overridden by aliases. This 850 * creates a seamless path forward for people who want to keep 851 * using the name after it is gone, but want to skip the 852 * deprecation complaint in the meantime. 853 */ 854 if (is_deprecated_command(args->v[0]) && 855 handle_alias(args, &expanded_aliases)) { 856 done_alias = 1; 857 continue; 858 } 859 /* 860 * If we tried alias and futzed with our environment, 861 * it no longer is safe to invoke builtins directly in 862 * general. We have to spawn them as dashed externals. 863 * 864 * NEEDSWORK: if we can figure out cases 865 * where it is safe to do, we can avoid spawning a new 866 * process. 867 */ 868 if (!done_alias) 869 handle_builtin(args); 870 else if (get_builtin(args->v[0])) { 871 struct child_process cmd = CHILD_PROCESS_INIT; 872 int err; 873 874 /* 875 * The current process is committed to launching a 876 * child process to run the command named in (**argv) 877 * and exiting. Log a generic string as the trace2 878 * command verb to indicate this. Note that the child 879 * process will log the actual verb when it runs. 880 */ 881 trace2_cmd_name("_run_git_alias_"); 882 883 commit_pager_choice(); 884 885 strvec_push(&cmd.args, "git"); 886 strvec_pushv(&cmd.args, args->v); 887 888 trace_argv_printf(cmd.args.v, "trace: exec:"); 889 890 /* 891 * if we fail because the command is not found, it is 892 * OK to return. Otherwise, we just pass along the status code. 893 */ 894 cmd.silent_exec_failure = 1; 895 cmd.clean_on_exit = 1; 896 cmd.wait_after_clean = 1; 897 cmd.trace2_child_class = "git_alias"; 898 err = run_command(&cmd); 899 if (err >= 0 || errno != ENOENT) 900 exit(err); 901 die("could not execute builtin %s", args->v[0]); 902 } 903 904 /* .. then try the external ones */ 905 execv_dashed_external(args->v); 906 907 /* 908 * It could be an alias -- this works around the insanity 909 * of overriding "git log" with "git show" by having 910 * alias.log = show 911 */ 912 if (!handle_alias(args, &expanded_aliases)) 913 break; 914 done_alias = 1; 915 } 916 917 string_list_clear(&expanded_aliases, 0); 918 919 return done_alias; 920 } 921 922 int cmd_main(int argc, const char **argv) 923 { 924 struct strvec args = STRVEC_INIT; 925 const char *cmd; 926 int done_help = 0; 927 928 cmd = argv[0]; 929 if (!cmd) 930 cmd = "git-help"; 931 else { 932 const char *slash = find_last_dir_sep(cmd); 933 if (slash) 934 cmd = slash + 1; 935 } 936 937 trace_command_performance(argv); 938 939 /* 940 * "git-xxxx" is the same as "git xxxx", but we obviously: 941 * 942 * - cannot take flags in between the "git" and the "xxxx". 943 * - cannot execute it externally (since it would just do 944 * the same thing over again) 945 * 946 * So we just directly call the builtin handler, and die if 947 * that one cannot handle it. 948 */ 949 if (skip_prefix(cmd, "git-", &cmd)) { 950 strvec_push(&args, cmd); 951 strvec_pushv(&args, argv + 1); 952 handle_builtin(&args); 953 strvec_clear(&args); 954 die(_("cannot handle %s as a builtin"), cmd); 955 } 956 957 /* Look for flags.. */ 958 argv++; 959 argc--; 960 handle_options(&argv, &argc, NULL); 961 962 if (!argc) { 963 /* The user didn't specify a command; give them help */ 964 commit_pager_choice(); 965 printf(_("usage: %s\n\n"), git_usage_string); 966 list_common_cmds_help(); 967 printf("\n%s\n", _(git_more_info_string)); 968 exit(1); 969 } 970 971 if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0])) 972 argv[0] = "version"; 973 else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0])) 974 argv[0] = "help"; 975 976 cmd = argv[0]; 977 978 /* 979 * We use PATH to find git commands, but we prepend some higher 980 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH 981 * environment, and the $(gitexecdir) from the Makefile at build 982 * time. 983 */ 984 setup_path(); 985 986 for (int i = 0; i < argc; i++) 987 strvec_push(&args, argv[i]); 988 989 while (1) { 990 int was_alias = run_argv(&args); 991 if (errno != ENOENT) 992 break; 993 if (was_alias) { 994 fprintf(stderr, _("expansion of alias '%s' failed; " 995 "'%s' is not a git command\n"), 996 cmd, args.v[0]); 997 strvec_clear(&args); 998 exit(1); 999 } 1000 if (!done_help) { 1001 char *assumed = help_unknown_cmd(cmd); 1002 strvec_replace(&args, 0, assumed); 1003 free(assumed); 1004 cmd = args.v[0]; 1005 done_help = 1; 1006 } else { 1007 break; 1008 } 1009 } 1010 1011 fprintf(stderr, _("failed to run command '%s': %s\n"), 1012 cmd, strerror(errno)); 1013 strvec_clear(&args); 1014 1015 return 1; 1016 }