Git fork
at reftables-rust 1010 lines 31 kB view raw
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 33struct cmd_struct { 34 const char *cmd; 35 int (*fn)(int, const char **, const char *, struct repository *); 36 unsigned int option; 37}; 38 39const char git_usage_string[] = 40 N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n" 41 " [--exec-path[=<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=<path>]\n" 44 " [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]\n" 45 " <command> [<args>]"); 46 47const 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 <command>' or 'git help <concept>'\n" 50 "to read about a specific subcommand or concept.\n" 51 "See 'git help git' for an overview of the system."); 52 53static int use_pager = -1; 54 55static void list_builtins(struct string_list *list, 56 unsigned int include_option, 57 unsigned int exclude_option); 58 59static 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 71static 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 78static 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(&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, 0); 123 return 0; 124} 125 126static 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 140void 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 150static 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 157static 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 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 is_bare_repository_cfg = 1; 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 (*argv)++; 308 (*argc)--; 309 set_alternate_shallow_file(the_repository, (*argv)[0], 1); 310 if (envchanged) 311 *envchanged = 1; 312 } else if (!strcmp(cmd, "-C")) { 313 if (*argc < 2) { 314 fprintf(stderr, _("no directory given for '%s' option\n" ), "-C"); 315 usage(git_usage_string); 316 } 317 if ((*argv)[1][0]) { 318 if (chdir((*argv)[1])) 319 die_errno("cannot change to '%s'", (*argv)[1]); 320 if (envchanged) 321 *envchanged = 1; 322 } 323 (*argv)++; 324 (*argc)--; 325 } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) { 326 trace2_cmd_name("_query_"); 327 if (!strcmp(cmd, "parseopt")) { 328 struct string_list list = STRING_LIST_INIT_DUP; 329 330 list_builtins(&list, 0, NO_PARSEOPT); 331 for (size_t i = 0; i < list.nr; i++) 332 printf("%s ", list.items[i].string); 333 string_list_clear(&list, 0); 334 exit(0); 335 } else { 336 exit(list_cmds(cmd)); 337 } 338 } else if (!strcmp(cmd, "--attr-source")) { 339 if (*argc < 2) { 340 fprintf(stderr, _("no attribute source given for --attr-source\n" )); 341 usage(git_usage_string); 342 } 343 setenv(GIT_ATTR_SOURCE_ENVIRONMENT, (*argv)[1], 1); 344 if (envchanged) 345 *envchanged = 1; 346 (*argv)++; 347 (*argc)--; 348 } else if (skip_prefix(cmd, "--attr-source=", &cmd)) { 349 set_git_attr_source(cmd); 350 setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1); 351 if (envchanged) 352 *envchanged = 1; 353 } else if (!strcmp(cmd, "--no-advice")) { 354 setenv(GIT_ADVICE_ENVIRONMENT, "0", 1); 355 if (envchanged) 356 *envchanged = 1; 357 } else { 358 fprintf(stderr, _("unknown option: %s\n"), cmd); 359 usage(git_usage_string); 360 } 361 362 (*argv)++; 363 (*argc)--; 364 } 365 return (*argv) - orig_argv; 366} 367 368static int handle_alias(struct strvec *args, struct string_list *expanded_aliases) 369{ 370 int envchanged = 0, ret = 0, saved_errno = errno; 371 int count, option_count; 372 const char **new_argv; 373 const char *alias_command; 374 char *alias_string; 375 376 alias_command = args->v[0]; 377 alias_string = alias_lookup(alias_command); 378 if (alias_string) { 379 struct string_list_item *seen; 380 381 if (args->nr == 2 && !strcmp(args->v[1], "-h")) 382 fprintf_ln(stderr, _("'%s' is aliased to '%s'"), 383 alias_command, alias_string); 384 if (alias_string[0] == '!') { 385 struct child_process child = CHILD_PROCESS_INIT; 386 int nongit_ok; 387 388 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */ 389 setup_git_directory_gently(&nongit_ok); 390 391 commit_pager_choice(); 392 393 child.use_shell = 1; 394 child.clean_on_exit = 1; 395 child.wait_after_clean = 1; 396 child.trace2_child_class = "shell_alias"; 397 strvec_push(&child.args, alias_string + 1); 398 strvec_pushv(&child.args, args->v + 1); 399 400 trace2_cmd_alias(alias_command, child.args.v); 401 trace2_cmd_name("_run_shell_alias_"); 402 403 ret = run_command(&child); 404 if (ret >= 0) /* normal exit */ 405 exit(ret); 406 407 die_errno(_("while expanding alias '%s': '%s'"), 408 alias_command, alias_string + 1); 409 } 410 count = split_cmdline(alias_string, &new_argv); 411 if (count < 0) 412 die(_("bad alias.%s string: %s"), alias_command, 413 _(split_cmdline_strerror(count))); 414 option_count = handle_options(&new_argv, &count, &envchanged); 415 if (envchanged) 416 die(_("alias '%s' changes environment variables.\n" 417 "You can use '!git' in the alias to do this"), 418 alias_command); 419 MOVE_ARRAY(new_argv - option_count, new_argv, count); 420 new_argv -= option_count; 421 422 if (count < 1) 423 die(_("empty alias for %s"), alias_command); 424 425 if (!strcmp(alias_command, new_argv[0])) 426 die(_("recursive alias: %s"), alias_command); 427 428 string_list_append(expanded_aliases, alias_command); 429 seen = unsorted_string_list_lookup(expanded_aliases, 430 new_argv[0]); 431 432 if (seen) { 433 struct strbuf sb = STRBUF_INIT; 434 for (size_t i = 0; i < expanded_aliases->nr; i++) { 435 struct string_list_item *item = &expanded_aliases->items[i]; 436 437 strbuf_addf(&sb, "\n %s", item->string); 438 if (item == seen) 439 strbuf_addstr(&sb, " <=="); 440 else if (i == expanded_aliases->nr - 1) 441 strbuf_addstr(&sb, " ==>"); 442 } 443 die(_("alias loop detected: expansion of '%s' does" 444 " not terminate:%s"), expanded_aliases->items[0].string, sb.buf); 445 } 446 447 trace_argv_printf(new_argv, 448 "trace: alias expansion: %s =>", 449 alias_command); 450 trace2_cmd_alias(alias_command, new_argv); 451 452 /* Replace the alias with the new arguments. */ 453 strvec_splice(args, 0, 1, new_argv, count); 454 455 free(alias_string); 456 free(new_argv); 457 458 ret = 1; 459 } 460 461 errno = saved_errno; 462 463 return ret; 464} 465 466static int run_builtin(struct cmd_struct *p, int argc, const char **argv, struct repository *repo) 467{ 468 int status, help; 469 int no_repo = 1; 470 struct stat st; 471 const char *prefix; 472 int run_setup = (p->option & (RUN_SETUP | RUN_SETUP_GENTLY)); 473 474 help = argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help-all")); 475 if (help && (run_setup & RUN_SETUP)) 476 /* demote to GENTLY to allow 'git cmd -h' outside repo */ 477 run_setup = RUN_SETUP_GENTLY; 478 479 if (run_setup & RUN_SETUP) { 480 prefix = setup_git_directory(); 481 no_repo = 0; 482 } else if (run_setup & RUN_SETUP_GENTLY) { 483 prefix = setup_git_directory_gently(&no_repo); 484 } else { 485 prefix = NULL; 486 } 487 assert(!prefix || *prefix); 488 precompose_argv_prefix(argc, argv, NULL); 489 if (use_pager == -1 && run_setup && 490 !(p->option & DELAY_PAGER_CONFIG)) 491 use_pager = check_pager_config(repo, p->cmd); 492 if (use_pager == -1 && p->option & USE_PAGER) 493 use_pager = 1; 494 if (run_setup && startup_info->have_repository) 495 /* get_git_dir() may set up repo, avoid that */ 496 trace_repo_setup(repo); 497 commit_pager_choice(); 498 499 if (!help && p->option & NEED_WORK_TREE) 500 setup_work_tree(); 501 502 trace_argv_printf(argv, "trace: built-in: git"); 503 trace2_cmd_name(p->cmd); 504 505 validate_cache_entries(repo->index); 506 status = p->fn(argc, argv, prefix, no_repo ? NULL : repo); 507 validate_cache_entries(repo->index); 508 509 if (status) 510 return status; 511 512 /* Somebody closed stdout? */ 513 if (fstat(fileno(stdout), &st)) 514 return 0; 515 /* Ignore write errors for pipes and sockets.. */ 516 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) 517 return 0; 518 519 /* Check for ENOSPC and EIO errors.. */ 520 if (fflush(stdout)) 521 die_errno(_("write failure on standard output")); 522 if (ferror(stdout)) 523 die(_("unknown write failure on standard output")); 524 if (fclose(stdout)) 525 die_errno(_("close failed on standard output")); 526 return 0; 527} 528 529static struct cmd_struct commands[] = { 530 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE }, 531 { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE }, 532 { "annotate", cmd_annotate, RUN_SETUP }, 533 { "apply", cmd_apply, RUN_SETUP_GENTLY }, 534 { "archive", cmd_archive, RUN_SETUP_GENTLY }, 535 { "backfill", cmd_backfill, RUN_SETUP }, 536 { "bisect", cmd_bisect, RUN_SETUP }, 537 { "blame", cmd_blame, RUN_SETUP }, 538 { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG }, 539 { "bugreport", cmd_bugreport, RUN_SETUP_GENTLY }, 540 { "bundle", cmd_bundle, RUN_SETUP_GENTLY }, 541 { "cat-file", cmd_cat_file, RUN_SETUP }, 542 { "check-attr", cmd_check_attr, RUN_SETUP }, 543 { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE }, 544 { "check-mailmap", cmd_check_mailmap, RUN_SETUP }, 545 { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT }, 546 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE }, 547 { "checkout--worker", cmd_checkout__worker, 548 RUN_SETUP | NEED_WORK_TREE }, 549 { "checkout-index", cmd_checkout_index, 550 RUN_SETUP | NEED_WORK_TREE}, 551 { "cherry", cmd_cherry, RUN_SETUP }, 552 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE }, 553 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE }, 554 { "clone", cmd_clone }, 555 { "column", cmd_column, RUN_SETUP_GENTLY }, 556 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE }, 557 { "commit-graph", cmd_commit_graph, RUN_SETUP }, 558 { "commit-tree", cmd_commit_tree, RUN_SETUP }, 559 { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG }, 560 { "count-objects", cmd_count_objects, RUN_SETUP }, 561 { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT }, 562 { "credential-cache", cmd_credential_cache }, 563 { "credential-cache--daemon", cmd_credential_cache_daemon }, 564 { "credential-store", cmd_credential_store }, 565 { "describe", cmd_describe, RUN_SETUP }, 566 { "diagnose", cmd_diagnose, RUN_SETUP_GENTLY }, 567 { "diff", cmd_diff, NO_PARSEOPT }, 568 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 569 { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT }, 570 { "diff-pairs", cmd_diff_pairs, RUN_SETUP | NO_PARSEOPT }, 571 { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT }, 572 { "difftool", cmd_difftool, RUN_SETUP_GENTLY }, 573 { "fast-export", cmd_fast_export, RUN_SETUP }, 574 { "fast-import", cmd_fast_import, RUN_SETUP | NO_PARSEOPT }, 575 { "fetch", cmd_fetch, RUN_SETUP }, 576 { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT }, 577 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, 578 { "for-each-ref", cmd_for_each_ref, RUN_SETUP }, 579 { "for-each-repo", cmd_for_each_repo, RUN_SETUP_GENTLY }, 580 { "format-patch", cmd_format_patch, RUN_SETUP }, 581 { "fsck", cmd_fsck, RUN_SETUP }, 582 { "fsck-objects", cmd_fsck, RUN_SETUP }, 583 { "fsmonitor--daemon", cmd_fsmonitor__daemon, RUN_SETUP }, 584 { "gc", cmd_gc, RUN_SETUP }, 585 { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT }, 586 { "grep", cmd_grep, RUN_SETUP_GENTLY }, 587 { "hash-object", cmd_hash_object }, 588 { "help", cmd_help }, 589 { "hook", cmd_hook, RUN_SETUP }, 590 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, 591 { "init", cmd_init_db }, 592 { "init-db", cmd_init_db }, 593 { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY }, 594 { "last-modified", cmd_last_modified, RUN_SETUP }, 595 { "log", cmd_log, RUN_SETUP }, 596 { "ls-files", cmd_ls_files, RUN_SETUP }, 597 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, 598 { "ls-tree", cmd_ls_tree, RUN_SETUP }, 599 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY }, 600 { "mailsplit", cmd_mailsplit, NO_PARSEOPT }, 601 { "maintenance", cmd_maintenance, RUN_SETUP }, 602 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE }, 603 { "merge-base", cmd_merge_base, RUN_SETUP }, 604 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY }, 605 { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT }, 606 { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT }, 607 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 608 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 609 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 610 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, 611 { "merge-tree", cmd_merge_tree, RUN_SETUP }, 612 { "mktag", cmd_mktag, RUN_SETUP }, 613 { "mktree", cmd_mktree, RUN_SETUP }, 614 { "multi-pack-index", cmd_multi_pack_index, RUN_SETUP }, 615 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, 616 { "name-rev", cmd_name_rev, RUN_SETUP }, 617 { "notes", cmd_notes, RUN_SETUP }, 618 { "pack-objects", cmd_pack_objects, RUN_SETUP }, 619#ifndef WITH_BREAKING_CHANGES 620 { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT | DEPRECATED }, 621#endif 622 { "pack-refs", cmd_pack_refs, RUN_SETUP }, 623 { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT }, 624 { "pickaxe", cmd_blame, RUN_SETUP }, 625 { "prune", cmd_prune, RUN_SETUP }, 626 { "prune-packed", cmd_prune_packed, RUN_SETUP }, 627 { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE }, 628 { "push", cmd_push, RUN_SETUP }, 629 { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, 630 { "read-tree", cmd_read_tree, RUN_SETUP }, 631 { "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE }, 632 { "receive-pack", cmd_receive_pack }, 633 { "reflog", cmd_reflog, RUN_SETUP }, 634 { "refs", cmd_refs, RUN_SETUP }, 635 { "remote", cmd_remote, RUN_SETUP }, 636 { "remote-ext", cmd_remote_ext, NO_PARSEOPT }, 637 { "remote-fd", cmd_remote_fd, NO_PARSEOPT }, 638 { "repack", cmd_repack, RUN_SETUP }, 639 { "replace", cmd_replace, RUN_SETUP }, 640 { "replay", cmd_replay, RUN_SETUP }, 641 { "repo", cmd_repo, RUN_SETUP }, 642 { "rerere", cmd_rerere, RUN_SETUP }, 643 { "reset", cmd_reset, RUN_SETUP }, 644 { "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE }, 645 { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, 646 { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, 647 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, 648 { "rm", cmd_rm, RUN_SETUP }, 649 { "send-pack", cmd_send_pack, RUN_SETUP }, 650 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER }, 651 { "show", cmd_show, RUN_SETUP }, 652 { "show-branch", cmd_show_branch, RUN_SETUP }, 653 { "show-index", cmd_show_index, RUN_SETUP_GENTLY }, 654 { "show-ref", cmd_show_ref, RUN_SETUP }, 655 { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP }, 656 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, 657 { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE }, 658 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, 659 { "stripspace", cmd_stripspace }, 660 { "submodule--helper", cmd_submodule__helper, RUN_SETUP }, 661 { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE }, 662 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, 663 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, 664 { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT }, 665 { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT }, 666 { "update-index", cmd_update_index, RUN_SETUP }, 667 { "update-ref", cmd_update_ref, RUN_SETUP }, 668 { "update-server-info", cmd_update_server_info, RUN_SETUP }, 669 { "upload-archive", cmd_upload_archive, NO_PARSEOPT }, 670 { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT }, 671 { "upload-pack", cmd_upload_pack }, 672 { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT }, 673 { "verify-commit", cmd_verify_commit, RUN_SETUP }, 674 { "verify-pack", cmd_verify_pack }, 675 { "verify-tag", cmd_verify_tag, RUN_SETUP }, 676 { "version", cmd_version }, 677#ifndef WITH_BREAKING_CHANGES 678 { "whatchanged", cmd_whatchanged, RUN_SETUP | DEPRECATED }, 679#endif 680 { "worktree", cmd_worktree, RUN_SETUP }, 681 { "write-tree", cmd_write_tree, RUN_SETUP }, 682}; 683 684static struct cmd_struct *get_builtin(const char *s) 685{ 686 for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { 687 struct cmd_struct *p = commands + i; 688 if (!strcmp(s, p->cmd)) 689 return p; 690 } 691 return NULL; 692} 693 694int is_builtin(const char *s) 695{ 696 return !!get_builtin(s); 697} 698 699static void list_builtins(struct string_list *out, 700 unsigned int include_option, 701 unsigned int exclude_option) 702{ 703 if (include_option && exclude_option) 704 BUG("'include_option' and 'exclude_option' are mutually exclusive"); 705 for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { 706 if (include_option && !(commands[i].option & include_option)) 707 continue; 708 if (exclude_option && (commands[i].option & exclude_option)) 709 continue; 710 string_list_append(out, commands[i].cmd); 711 } 712} 713 714void load_builtin_commands(const char *prefix, struct cmdnames *cmds) 715{ 716 const char *name; 717 718 /* 719 * Callers can ask for a subset of the commands based on a certain 720 * prefix, which is then dropped from the added names. The names in 721 * the `commands[]` array do not have the `git-` prefix, though, 722 * therefore we must expect the `prefix` to at least start with `git-`. 723 */ 724 if (!skip_prefix(prefix, "git-", &prefix)) 725 BUG("prefix '%s' must start with 'git-'", prefix); 726 727 for (size_t i = 0; i < ARRAY_SIZE(commands); i++) 728 if (skip_prefix(commands[i].cmd, prefix, &name)) 729 add_cmdname(cmds, name, strlen(name)); 730} 731 732#ifdef STRIP_EXTENSION 733static void strip_extension(struct strvec *args) 734{ 735 size_t len; 736 737 if (strip_suffix(args->v[0], STRIP_EXTENSION, &len)) { 738 char *stripped = xmemdupz(args->v[0], len); 739 strvec_replace(args, 0, stripped); 740 free(stripped); 741 } 742} 743#else 744#define strip_extension(cmd) 745#endif 746 747static void handle_builtin(struct strvec *args) 748{ 749 const char *cmd; 750 struct cmd_struct *builtin; 751 752 strip_extension(args); 753 cmd = args->v[0]; 754 755 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */ 756 if (args->nr > 1 && !strcmp(args->v[1], "--help")) { 757 const char *exclude_guides_arg[] = { "--exclude-guides" }; 758 759 strvec_replace(args, 1, args->v[0]); 760 strvec_replace(args, 0, "help"); 761 cmd = "help"; 762 strvec_splice(args, 2, 0, exclude_guides_arg, 763 ARRAY_SIZE(exclude_guides_arg)); 764 } 765 766 builtin = get_builtin(cmd); 767 if (builtin) { 768 const char **argv_copy = NULL; 769 int ret; 770 771 /* 772 * `run_builtin()` will modify the argv array, so we need to 773 * create a shallow copy such that we can free all of its 774 * strings. 775 */ 776 if (args->nr) 777 DUP_ARRAY(argv_copy, args->v, args->nr + 1); 778 779 ret = run_builtin(builtin, args->nr, argv_copy, the_repository); 780 strvec_clear(args); 781 free(argv_copy); 782 exit(ret); 783 } 784} 785 786static void execv_dashed_external(const char **argv) 787{ 788 struct child_process cmd = CHILD_PROCESS_INIT; 789 int status; 790 791 if (use_pager == -1 && !is_builtin(argv[0])) 792 use_pager = check_pager_config(the_repository, argv[0]); 793 commit_pager_choice(); 794 795 strvec_pushf(&cmd.args, "git-%s", argv[0]); 796 strvec_pushv(&cmd.args, argv + 1); 797 cmd.clean_on_exit = 1; 798 cmd.wait_after_clean = 1; 799 cmd.silent_exec_failure = 1; 800 cmd.trace2_child_class = "dashed"; 801 802 trace2_cmd_name("_run_dashed_"); 803 804 /* 805 * The code in run_command() logs trace2 child_start/child_exit 806 * events, so we do not need to report exec/exec_result events here. 807 */ 808 trace_argv_printf(cmd.args.v, "trace: exec:"); 809 810 /* 811 * If we fail because the command is not found, it is 812 * OK to return. Otherwise, we just pass along the status code, 813 * or our usual generic code if we were not even able to exec 814 * the program. 815 */ 816 status = run_command(&cmd); 817 818 /* 819 * If the child process ran and we are now going to exit, emit a 820 * generic string as our trace2 command verb to indicate that we 821 * launched a dashed command. 822 */ 823 if (status >= 0) 824 exit(status); 825 else if (errno != ENOENT) 826 exit(128); 827} 828 829static int is_deprecated_command(const char *cmd) 830{ 831 struct cmd_struct *builtin = get_builtin(cmd); 832 return builtin && (builtin->option & DEPRECATED); 833} 834 835static int run_argv(struct strvec *args) 836{ 837 int done_alias = 0; 838 struct string_list expanded_aliases = STRING_LIST_INIT_DUP; 839 840 while (1) { 841 /* 842 * Allow deprecated commands to be overridden by aliases. This 843 * creates a seamless path forward for people who want to keep 844 * using the name after it is gone, but want to skip the 845 * deprecation complaint in the meantime. 846 */ 847 if (is_deprecated_command(args->v[0]) && 848 handle_alias(args, &expanded_aliases)) { 849 done_alias = 1; 850 continue; 851 } 852 /* 853 * If we tried alias and futzed with our environment, 854 * it no longer is safe to invoke builtins directly in 855 * general. We have to spawn them as dashed externals. 856 * 857 * NEEDSWORK: if we can figure out cases 858 * where it is safe to do, we can avoid spawning a new 859 * process. 860 */ 861 if (!done_alias) 862 handle_builtin(args); 863 else if (get_builtin(args->v[0])) { 864 struct child_process cmd = CHILD_PROCESS_INIT; 865 int err; 866 867 /* 868 * The current process is committed to launching a 869 * child process to run the command named in (**argv) 870 * and exiting. Log a generic string as the trace2 871 * command verb to indicate this. Note that the child 872 * process will log the actual verb when it runs. 873 */ 874 trace2_cmd_name("_run_git_alias_"); 875 876 commit_pager_choice(); 877 878 strvec_push(&cmd.args, "git"); 879 for (size_t i = 0; i < args->nr; i++) 880 strvec_push(&cmd.args, args->v[i]); 881 882 trace_argv_printf(cmd.args.v, "trace: exec:"); 883 884 /* 885 * if we fail because the command is not found, it is 886 * OK to return. Otherwise, we just pass along the status code. 887 */ 888 cmd.silent_exec_failure = 1; 889 cmd.clean_on_exit = 1; 890 cmd.wait_after_clean = 1; 891 cmd.trace2_child_class = "git_alias"; 892 err = run_command(&cmd); 893 if (err >= 0 || errno != ENOENT) 894 exit(err); 895 die("could not execute builtin %s", args->v[0]); 896 } 897 898 /* .. then try the external ones */ 899 execv_dashed_external(args->v); 900 901 /* 902 * It could be an alias -- this works around the insanity 903 * of overriding "git log" with "git show" by having 904 * alias.log = show 905 */ 906 if (!handle_alias(args, &expanded_aliases)) 907 break; 908 done_alias = 1; 909 } 910 911 string_list_clear(&expanded_aliases, 0); 912 913 return done_alias; 914} 915 916int cmd_main(int argc, const char **argv) 917{ 918 struct strvec args = STRVEC_INIT; 919 const char *cmd; 920 int done_help = 0; 921 922 cmd = argv[0]; 923 if (!cmd) 924 cmd = "git-help"; 925 else { 926 const char *slash = find_last_dir_sep(cmd); 927 if (slash) 928 cmd = slash + 1; 929 } 930 931 trace_command_performance(argv); 932 933 /* 934 * "git-xxxx" is the same as "git xxxx", but we obviously: 935 * 936 * - cannot take flags in between the "git" and the "xxxx". 937 * - cannot execute it externally (since it would just do 938 * the same thing over again) 939 * 940 * So we just directly call the builtin handler, and die if 941 * that one cannot handle it. 942 */ 943 if (skip_prefix(cmd, "git-", &cmd)) { 944 strvec_push(&args, cmd); 945 strvec_pushv(&args, argv + 1); 946 handle_builtin(&args); 947 strvec_clear(&args); 948 die(_("cannot handle %s as a builtin"), cmd); 949 } 950 951 /* Look for flags.. */ 952 argv++; 953 argc--; 954 handle_options(&argv, &argc, NULL); 955 956 if (!argc) { 957 /* The user didn't specify a command; give them help */ 958 commit_pager_choice(); 959 printf(_("usage: %s\n\n"), git_usage_string); 960 list_common_cmds_help(); 961 printf("\n%s\n", _(git_more_info_string)); 962 exit(1); 963 } 964 965 if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0])) 966 argv[0] = "version"; 967 else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0])) 968 argv[0] = "help"; 969 970 cmd = argv[0]; 971 972 /* 973 * We use PATH to find git commands, but we prepend some higher 974 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH 975 * environment, and the $(gitexecdir) from the Makefile at build 976 * time. 977 */ 978 setup_path(); 979 980 for (int i = 0; i < argc; i++) 981 strvec_push(&args, argv[i]); 982 983 while (1) { 984 int was_alias = run_argv(&args); 985 if (errno != ENOENT) 986 break; 987 if (was_alias) { 988 fprintf(stderr, _("expansion of alias '%s' failed; " 989 "'%s' is not a git command\n"), 990 cmd, args.v[0]); 991 strvec_clear(&args); 992 exit(1); 993 } 994 if (!done_help) { 995 char *assumed = help_unknown_cmd(cmd); 996 strvec_replace(&args, 0, assumed); 997 free(assumed); 998 cmd = args.v[0]; 999 done_help = 1; 1000 } else { 1001 break; 1002 } 1003 } 1004 1005 fprintf(stderr, _("failed to run command '%s': %s\n"), 1006 cmd, strerror(errno)); 1007 strvec_clear(&args); 1008 1009 return 1; 1010}