| 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 <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 | |
| 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 <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 | |
| 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 | 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 | |
| 368 | static 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(the_repository, &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 | |
| 466 | static 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(the_repository); |
| 481 | no_repo = 0; |
| 482 | } else if (run_setup & RUN_SETUP_GENTLY) { |
| 483 | prefix = setup_git_directory_gently(the_repository, &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(the_repository); |
| 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 | |
| 529 | static 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 | { "format-rev", cmd_format_rev, RUN_SETUP }, |
| 582 | { "fsck", cmd_fsck, RUN_SETUP }, |
| 583 | { "fsck-objects", cmd_fsck, RUN_SETUP }, |
| 584 | { "fsmonitor--daemon", cmd_fsmonitor__daemon, RUN_SETUP }, |
| 585 | { "gc", cmd_gc, RUN_SETUP }, |
| 586 | { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT }, |
| 587 | { "grep", cmd_grep, RUN_SETUP_GENTLY }, |
| 588 | { "hash-object", cmd_hash_object }, |
| 589 | { "help", cmd_help }, |
| 590 | { "history", cmd_history, RUN_SETUP }, |
| 591 | { "hook", cmd_hook, RUN_SETUP_GENTLY }, |
| 592 | { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
| 593 | { "init", cmd_init_db }, |
| 594 | { "init-db", cmd_init_db }, |
| 595 | { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY }, |
| 596 | { "last-modified", cmd_last_modified, RUN_SETUP }, |
| 597 | { "log", cmd_log, RUN_SETUP }, |
| 598 | { "ls-files", cmd_ls_files, RUN_SETUP }, |
| 599 | { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY }, |
| 600 | { "ls-tree", cmd_ls_tree, RUN_SETUP }, |
| 601 | { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY }, |
| 602 | { "mailsplit", cmd_mailsplit, NO_PARSEOPT }, |
| 603 | { "maintenance", cmd_maintenance, RUN_SETUP }, |
| 604 | { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE }, |
| 605 | { "merge-base", cmd_merge_base, RUN_SETUP }, |
| 606 | { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY }, |
| 607 | { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT }, |
| 608 | { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT }, |
| 609 | { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, |
| 610 | { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, |
| 611 | { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, |
| 612 | { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, |
| 613 | { "merge-tree", cmd_merge_tree, RUN_SETUP }, |
| 614 | { "mktag", cmd_mktag, RUN_SETUP }, |
| 615 | { "mktree", cmd_mktree, RUN_SETUP }, |
| 616 | { "multi-pack-index", cmd_multi_pack_index, RUN_SETUP }, |
| 617 | { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, |
| 618 | { "name-rev", cmd_name_rev, RUN_SETUP }, |
| 619 | { "notes", cmd_notes, RUN_SETUP }, |
| 620 | { "pack-objects", cmd_pack_objects, RUN_SETUP }, |
| 621 | #ifndef WITH_BREAKING_CHANGES |
| 622 | { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT | DEPRECATED }, |
| 623 | #endif |
| 624 | { "pack-refs", cmd_pack_refs, RUN_SETUP }, |
| 625 | { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
| 626 | { "pickaxe", cmd_blame, RUN_SETUP }, |
| 627 | { "prune", cmd_prune, RUN_SETUP }, |
| 628 | { "prune-packed", cmd_prune_packed, RUN_SETUP }, |
| 629 | { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE }, |
| 630 | { "push", cmd_push, RUN_SETUP }, |
| 631 | { "range-diff", cmd_range_diff, RUN_SETUP | USE_PAGER }, |
| 632 | { "read-tree", cmd_read_tree, RUN_SETUP }, |
| 633 | { "rebase", cmd_rebase, RUN_SETUP | NEED_WORK_TREE }, |
| 634 | { "receive-pack", cmd_receive_pack }, |
| 635 | { "reflog", cmd_reflog, RUN_SETUP }, |
| 636 | { "refs", cmd_refs, RUN_SETUP }, |
| 637 | { "remote", cmd_remote, RUN_SETUP }, |
| 638 | { "remote-ext", cmd_remote_ext, NO_PARSEOPT }, |
| 639 | { "remote-fd", cmd_remote_fd, NO_PARSEOPT }, |
| 640 | { "repack", cmd_repack, RUN_SETUP }, |
| 641 | { "replace", cmd_replace, RUN_SETUP }, |
| 642 | { "replay", cmd_replay, RUN_SETUP }, |
| 643 | { "repo", cmd_repo, RUN_SETUP }, |
| 644 | { "rerere", cmd_rerere, RUN_SETUP }, |
| 645 | { "reset", cmd_reset, RUN_SETUP }, |
| 646 | { "restore", cmd_restore, RUN_SETUP | NEED_WORK_TREE }, |
| 647 | { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, |
| 648 | { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, |
| 649 | { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, |
| 650 | { "rm", cmd_rm, RUN_SETUP }, |
| 651 | { "send-pack", cmd_send_pack, RUN_SETUP }, |
| 652 | { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER }, |
| 653 | { "show", cmd_show, RUN_SETUP }, |
| 654 | { "show-branch", cmd_show_branch, RUN_SETUP }, |
| 655 | { "show-index", cmd_show_index, RUN_SETUP_GENTLY }, |
| 656 | { "show-ref", cmd_show_ref, RUN_SETUP }, |
| 657 | { "sparse-checkout", cmd_sparse_checkout, RUN_SETUP }, |
| 658 | { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE }, |
| 659 | { "stash", cmd_stash, RUN_SETUP | NEED_WORK_TREE }, |
| 660 | { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, |
| 661 | { "stripspace", cmd_stripspace }, |
| 662 | { "submodule--helper", cmd_submodule__helper, RUN_SETUP }, |
| 663 | { "switch", cmd_switch, RUN_SETUP | NEED_WORK_TREE }, |
| 664 | { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, |
| 665 | { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, |
| 666 | { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT }, |
| 667 | { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT }, |
| 668 | { "update-index", cmd_update_index, RUN_SETUP }, |
| 669 | { "update-ref", cmd_update_ref, RUN_SETUP }, |
| 670 | { "update-server-info", cmd_update_server_info, RUN_SETUP }, |
| 671 | { "upload-archive", cmd_upload_archive, NO_PARSEOPT }, |
| 672 | { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT }, |
| 673 | { "upload-pack", cmd_upload_pack }, |
| 674 | { "url-parse", cmd_url_parse }, |
| 675 | { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT }, |
| 676 | { "verify-commit", cmd_verify_commit, RUN_SETUP }, |
| 677 | { "verify-pack", cmd_verify_pack }, |
| 678 | { "verify-tag", cmd_verify_tag, RUN_SETUP }, |
| 679 | { "version", cmd_version }, |
| 680 | #ifndef WITH_BREAKING_CHANGES |
| 681 | { "whatchanged", cmd_whatchanged, RUN_SETUP | DEPRECATED }, |
| 682 | #endif |
| 683 | { "worktree", cmd_worktree, RUN_SETUP }, |
| 684 | { "write-tree", cmd_write_tree, RUN_SETUP }, |
| 685 | }; |
| 686 | |
| 687 | static struct cmd_struct *get_builtin(const char *s) |
| 688 | { |
| 689 | for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { |
| 690 | struct cmd_struct *p = commands + i; |
| 691 | if (!strcmp(s, p->cmd)) |
| 692 | return p; |
| 693 | } |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | int is_builtin(const char *s) |
| 698 | { |
| 699 | return !!get_builtin(s); |
| 700 | } |
| 701 | |
| 702 | static void list_builtins(struct string_list *out, |
| 703 | unsigned int include_option, |
| 704 | unsigned int exclude_option) |
| 705 | { |
| 706 | if (include_option && exclude_option) |
| 707 | BUG("'include_option' and 'exclude_option' are mutually exclusive"); |
| 708 | for (size_t i = 0; i < ARRAY_SIZE(commands); i++) { |
| 709 | if (include_option && !(commands[i].option & include_option)) |
| 710 | continue; |
| 711 | if (exclude_option && (commands[i].option & exclude_option)) |
| 712 | continue; |
| 713 | string_list_append(out, commands[i].cmd); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | void load_builtin_commands(const char *prefix, struct cmdnames *cmds) |
| 718 | { |
| 719 | const char *name; |
| 720 | |
| 721 | /* |
| 722 | * Callers can ask for a subset of the commands based on a certain |
| 723 | * prefix, which is then dropped from the added names. The names in |
| 724 | * the `commands[]` array do not have the `git-` prefix, though, |
| 725 | * therefore we must expect the `prefix` to at least start with `git-`. |
| 726 | */ |
| 727 | if (!skip_prefix(prefix, "git-", &prefix)) |
| 728 | BUG("prefix '%s' must start with 'git-'", prefix); |
| 729 | |
| 730 | for (size_t i = 0; i < ARRAY_SIZE(commands); i++) |
| 731 | if (skip_prefix(commands[i].cmd, prefix, &name)) |
| 732 | add_cmdname(cmds, name, strlen(name)); |
| 733 | } |
| 734 | |
| 735 | #ifdef STRIP_EXTENSION |
| 736 | static void strip_extension(struct strvec *args) |
| 737 | { |
| 738 | size_t len; |
| 739 | |
| 740 | if (strip_suffix(args->v[0], STRIP_EXTENSION, &len)) { |
| 741 | char *stripped = xmemdupz(args->v[0], len); |
| 742 | strvec_replace(args, 0, stripped); |
| 743 | free(stripped); |
| 744 | } |
| 745 | } |
| 746 | #else |
| 747 | #define strip_extension(cmd) |
| 748 | #endif |
| 749 | |
| 750 | static void handle_builtin(struct strvec *args) |
| 751 | { |
| 752 | const char *cmd; |
| 753 | struct cmd_struct *builtin; |
| 754 | |
| 755 | strip_extension(args); |
| 756 | cmd = args->v[0]; |
| 757 | |
| 758 | /* Turn "git cmd --help" into "git help --exclude-guides cmd" */ |
| 759 | if (args->nr > 1 && !strcmp(args->v[1], "--help")) { |
| 760 | const char *exclude_guides_arg[] = { "--exclude-guides" }; |
| 761 | |
| 762 | strvec_replace(args, 1, args->v[0]); |
| 763 | strvec_replace(args, 0, "help"); |
| 764 | cmd = "help"; |
| 765 | strvec_splice(args, 2, 0, exclude_guides_arg, |
| 766 | ARRAY_SIZE(exclude_guides_arg)); |
| 767 | } |
| 768 | |
| 769 | builtin = get_builtin(cmd); |
| 770 | if (builtin) { |
| 771 | const char **argv_copy = NULL; |
| 772 | int ret; |
| 773 | |
| 774 | /* |
| 775 | * `run_builtin()` will modify the argv array, so we need to |
| 776 | * create a shallow copy such that we can free all of its |
| 777 | * strings. |
| 778 | */ |
| 779 | if (args->nr) |
| 780 | DUP_ARRAY(argv_copy, args->v, args->nr + 1); |
| 781 | |
| 782 | ret = run_builtin(builtin, args->nr, argv_copy, the_repository); |
| 783 | strvec_clear(args); |
| 784 | free(argv_copy); |
| 785 | exit(ret); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | static void execv_dashed_external(const char **argv) |
| 790 | { |
| 791 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 792 | int status; |
| 793 | |
| 794 | if (use_pager == -1 && !is_builtin(argv[0])) |
| 795 | use_pager = check_pager_config(the_repository, argv[0]); |
| 796 | commit_pager_choice(); |
| 797 | |
| 798 | strvec_pushf(&cmd.args, "git-%s", argv[0]); |
| 799 | strvec_pushv(&cmd.args, argv + 1); |
| 800 | cmd.clean_on_exit = 1; |
| 801 | cmd.wait_after_clean = 1; |
| 802 | cmd.silent_exec_failure = 1; |
| 803 | cmd.trace2_child_class = "dashed"; |
| 804 | |
| 805 | trace2_cmd_name("_run_dashed_"); |
| 806 | |
| 807 | /* |
| 808 | * The code in run_command() logs trace2 child_start/child_exit |
| 809 | * events, so we do not need to report exec/exec_result events here. |
| 810 | */ |
| 811 | trace_argv_printf(cmd.args.v, "trace: exec:"); |
| 812 | |
| 813 | /* |
| 814 | * If we fail because the command is not found, it is |
| 815 | * OK to return. Otherwise, we just pass along the status code, |
| 816 | * or our usual generic code if we were not even able to exec |
| 817 | * the program. |
| 818 | */ |
| 819 | status = run_command(&cmd); |
| 820 | |
| 821 | /* |
| 822 | * If the child process ran and we are now going to exit, emit a |
| 823 | * generic string as our trace2 command verb to indicate that we |
| 824 | * launched a dashed command. |
| 825 | */ |
| 826 | if (status >= 0) |
| 827 | exit(status); |
| 828 | else if (errno != ENOENT) |
| 829 | exit(128); |
| 830 | } |
| 831 | |
| 832 | static int is_deprecated_command(const char *cmd) |
| 833 | { |
| 834 | struct cmd_struct *builtin = get_builtin(cmd); |
| 835 | return builtin && (builtin->option & DEPRECATED); |
| 836 | } |
| 837 | |
| 838 | static int run_argv(struct strvec *args) |
| 839 | { |
| 840 | int done_alias = 0; |
| 841 | struct string_list expanded_aliases = STRING_LIST_INIT_DUP; |
| 842 | |
| 843 | while (1) { |
| 844 | /* |
| 845 | * Allow deprecated commands to be overridden by aliases. This |
| 846 | * creates a seamless path forward for people who want to keep |
| 847 | * using the name after it is gone, but want to skip the |
| 848 | * deprecation complaint in the meantime. |
| 849 | */ |
| 850 | if (is_deprecated_command(args->v[0]) && |
| 851 | handle_alias(args, &expanded_aliases)) { |
| 852 | done_alias = 1; |
| 853 | continue; |
| 854 | } |
| 855 | /* |
| 856 | * If we tried alias and futzed with our environment, |
| 857 | * it no longer is safe to invoke builtins directly in |
| 858 | * general. We have to spawn them as dashed externals. |
| 859 | * |
| 860 | * NEEDSWORK: if we can figure out cases |
| 861 | * where it is safe to do, we can avoid spawning a new |
| 862 | * process. |
| 863 | */ |
| 864 | if (!done_alias) |
| 865 | handle_builtin(args); |
| 866 | else if (get_builtin(args->v[0])) { |
| 867 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 868 | int err; |
| 869 | |
| 870 | /* |
| 871 | * The current process is committed to launching a |
| 872 | * child process to run the command named in (**argv) |
| 873 | * and exiting. Log a generic string as the trace2 |
| 874 | * command verb to indicate this. Note that the child |
| 875 | * process will log the actual verb when it runs. |
| 876 | */ |
| 877 | trace2_cmd_name("_run_git_alias_"); |
| 878 | |
| 879 | commit_pager_choice(); |
| 880 | |
| 881 | strvec_push(&cmd.args, "git"); |
| 882 | strvec_pushv(&cmd.args, args->v); |
| 883 | |
| 884 | trace_argv_printf(cmd.args.v, "trace: exec:"); |
| 885 | |
| 886 | /* |
| 887 | * if we fail because the command is not found, it is |
| 888 | * OK to return. Otherwise, we just pass along the status code. |
| 889 | */ |
| 890 | cmd.silent_exec_failure = 1; |
| 891 | cmd.clean_on_exit = 1; |
| 892 | cmd.wait_after_clean = 1; |
| 893 | cmd.trace2_child_class = "git_alias"; |
| 894 | err = run_command(&cmd); |
| 895 | if (err >= 0 || errno != ENOENT) |
| 896 | exit(err); |
| 897 | die("could not execute builtin %s", args->v[0]); |
| 898 | } |
| 899 | |
| 900 | /* .. then try the external ones */ |
| 901 | execv_dashed_external(args->v); |
| 902 | |
| 903 | /* |
| 904 | * It could be an alias -- this works around the insanity |
| 905 | * of overriding "git log" with "git show" by having |
| 906 | * alias.log = show |
| 907 | */ |
| 908 | if (!handle_alias(args, &expanded_aliases)) |
| 909 | break; |
| 910 | done_alias = 1; |
| 911 | } |
| 912 | |
| 913 | string_list_clear(&expanded_aliases, 0); |
| 914 | |
| 915 | return done_alias; |
| 916 | } |
| 917 | |
| 918 | int cmd_main(int argc, const char **argv) |
| 919 | { |
| 920 | struct strvec args = STRVEC_INIT; |
| 921 | const char *cmd; |
| 922 | int done_help = 0; |
| 923 | |
| 924 | cmd = argv[0]; |
| 925 | if (!cmd) |
| 926 | cmd = "git-help"; |
| 927 | else { |
| 928 | const char *slash = find_last_dir_sep(cmd); |
| 929 | if (slash) |
| 930 | cmd = slash + 1; |
| 931 | } |
| 932 | |
| 933 | trace_command_performance(argv); |
| 934 | |
| 935 | /* |
| 936 | * "git-xxxx" is the same as "git xxxx", but we obviously: |
| 937 | * |
| 938 | * - cannot take flags in between the "git" and the "xxxx". |
| 939 | * - cannot execute it externally (since it would just do |
| 940 | * the same thing over again) |
| 941 | * |
| 942 | * So we just directly call the builtin handler, and die if |
| 943 | * that one cannot handle it. |
| 944 | */ |
| 945 | if (skip_prefix(cmd, "git-", &cmd)) { |
| 946 | strvec_push(&args, cmd); |
| 947 | strvec_pushv(&args, argv + 1); |
| 948 | handle_builtin(&args); |
| 949 | strvec_clear(&args); |
| 950 | die(_("cannot handle %s as a builtin"), cmd); |
| 951 | } |
| 952 | |
| 953 | /* Look for flags.. */ |
| 954 | argv++; |
| 955 | argc--; |
| 956 | handle_options(&argv, &argc, NULL); |
| 957 | |
| 958 | if (!argc) { |
| 959 | /* The user didn't specify a command; give them help */ |
| 960 | commit_pager_choice(); |
| 961 | printf(_("usage: %s\n\n"), git_usage_string); |
| 962 | list_common_cmds_help(); |
| 963 | printf("\n%s\n", _(git_more_info_string)); |
| 964 | exit(1); |
| 965 | } |
| 966 | |
| 967 | if (!strcmp("--version", argv[0]) || !strcmp("-v", argv[0])) |
| 968 | argv[0] = "version"; |
| 969 | else if (!strcmp("--help", argv[0]) || !strcmp("-h", argv[0])) |
| 970 | argv[0] = "help"; |
| 971 | |
| 972 | cmd = argv[0]; |
| 973 | |
| 974 | /* |
| 975 | * We use PATH to find git commands, but we prepend some higher |
| 976 | * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH |
| 977 | * environment, and the $(gitexecdir) from the Makefile at build |
| 978 | * time. |
| 979 | */ |
| 980 | setup_path(); |
| 981 | |
| 982 | for (int i = 0; i < argc; i++) |
| 983 | strvec_push(&args, argv[i]); |
| 984 | |
| 985 | while (1) { |
| 986 | int was_alias = run_argv(&args); |
| 987 | if (errno != ENOENT) |
| 988 | break; |
| 989 | if (was_alias) { |
| 990 | fprintf(stderr, _("expansion of alias '%s' failed; " |
| 991 | "'%s' is not a git command\n"), |
| 992 | cmd, args.v[0]); |
| 993 | strvec_clear(&args); |
| 994 | exit(1); |
| 995 | } |
| 996 | if (!done_help) { |
| 997 | char *assumed = help_unknown_cmd(cmd); |
| 998 | strvec_replace(&args, 0, assumed); |
| 999 | free(assumed); |
| 1000 | cmd = args.v[0]; |
| 1001 | done_help = 1; |
| 1002 | } else { |
| 1003 | break; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | fprintf(stderr, _("failed to run command '%s': %s\n"), |
| 1008 | cmd, strerror(errno)); |
| 1009 | strvec_clear(&args); |
| 1010 | |
| 1011 | return 1; |
| 1012 | } |