3 #include "environment.h"
7 #include "run-command.h"
9 #include "replace-object.h"
13 #define RUN_SETUP (1<<0)
14 #define RUN_SETUP_GENTLY (1<<1)
15 #define USE_PAGER (1<<2)
17 * require working tree to be present -- anything uses this needs
18 * RUN_SETUP for reading from the configuration file.
20 #define NEED_WORK_TREE (1<<3)
21 #define DELAY_PAGER_CONFIG (1<<4)
22 #define NO_PARSEOPT (1<<5) /* parse-options is not used */
26 int (*fn
)(int, const char **, const char *);
30 const char git_usage_string
[] =
31 N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
32 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
33 " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
34 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
35 " [--config-env=<name>=<envvar>] <command> [<args>]");
37 const char git_more_info_string
[] =
38 N_("'git help -a' and 'git help -g' list available subcommands and some\n"
39 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
40 "to read about a specific subcommand or concept.\n"
41 "See 'git help git' for an overview of the system.");
43 static int use_pager
= -1;
45 static void list_builtins(struct string_list
*list
, unsigned int exclude_option
);
47 static void exclude_helpers_from_list(struct string_list
*list
)
51 while (i
< list
->nr
) {
52 if (strstr(list
->items
[i
].string
, "--"))
53 unsorted_string_list_delete_item(list
, i
, 0);
59 static int match_token(const char *spec
, int len
, const char *token
)
61 int token_len
= strlen(token
);
63 return len
== token_len
&& !strncmp(spec
, token
, token_len
);
66 static int list_cmds(const char *spec
)
68 struct string_list list
= STRING_LIST_INIT_DUP
;
73 * Set up the repository so we can pick up any repo-level config (like
74 * completion.commands).
76 setup_git_directory_gently(&nongit
);
79 const char *sep
= strchrnul(spec
, ',');
82 if (match_token(spec
, len
, "builtins"))
83 list_builtins(&list
, 0);
84 else if (match_token(spec
, len
, "main"))
85 list_all_main_cmds(&list
);
86 else if (match_token(spec
, len
, "others"))
87 list_all_other_cmds(&list
);
88 else if (match_token(spec
, len
, "nohelpers"))
89 exclude_helpers_from_list(&list
);
90 else if (match_token(spec
, len
, "alias"))
92 else if (match_token(spec
, len
, "config"))
93 list_cmds_by_config(&list
);
94 else if (len
> 5 && !strncmp(spec
, "list-", 5)) {
95 struct strbuf sb
= STRBUF_INIT
;
97 strbuf_add(&sb
, spec
+ 5, len
- 5);
98 list_cmds_by_category(&list
, sb
.buf
);
102 die(_("unsupported command listing type '%s'"), spec
);
107 for (i
= 0; i
< list
.nr
; i
++)
108 puts(list
.items
[i
].string
);
109 string_list_clear(&list
, 0);
113 static void commit_pager_choice(void)
117 setenv("GIT_PAGER", "cat", 1);
127 void setup_auto_pager(const char *cmd
, int def
)
129 if (use_pager
!= -1 || pager_in_use())
131 use_pager
= check_pager_config(cmd
);
134 commit_pager_choice();
137 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
139 const char **orig_argv
= *argv
;
142 const char *cmd
= (*argv
)[0];
147 * For legacy reasons, the "version" and "help"
148 * commands can be written with "--" prepended
149 * to make them look like flags.
151 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "-h") ||
152 !strcmp(cmd
, "--version") || !strcmp(cmd
, "-v"))
156 * Check remaining flags.
158 if (skip_prefix(cmd
, "--exec-path", &cmd
)) {
160 git_set_exec_path(cmd
+ 1);
162 puts(git_exec_path());
163 trace2_cmd_name("_query_");
166 } else if (!strcmp(cmd
, "--html-path")) {
167 puts(system_path(GIT_HTML_PATH
));
168 trace2_cmd_name("_query_");
170 } else if (!strcmp(cmd
, "--man-path")) {
171 puts(system_path(GIT_MAN_PATH
));
172 trace2_cmd_name("_query_");
174 } else if (!strcmp(cmd
, "--info-path")) {
175 puts(system_path(GIT_INFO_PATH
));
176 trace2_cmd_name("_query_");
178 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
180 } else if (!strcmp(cmd
, "-P") || !strcmp(cmd
, "--no-pager")) {
184 } else if (!strcmp(cmd
, "--no-replace-objects")) {
185 read_replace_refs
= 0;
186 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT
, "1", 1);
189 } else if (!strcmp(cmd
, "--git-dir")) {
191 fprintf(stderr
, _("no directory given for '%s' option\n" ), "--git-dir");
192 usage(git_usage_string
);
194 setenv(GIT_DIR_ENVIRONMENT
, (*argv
)[1], 1);
199 } else if (skip_prefix(cmd
, "--git-dir=", &cmd
)) {
200 setenv(GIT_DIR_ENVIRONMENT
, cmd
, 1);
203 } else if (!strcmp(cmd
, "--namespace")) {
205 fprintf(stderr
, _("no namespace given for --namespace\n" ));
206 usage(git_usage_string
);
208 setenv(GIT_NAMESPACE_ENVIRONMENT
, (*argv
)[1], 1);
213 } else if (skip_prefix(cmd
, "--namespace=", &cmd
)) {
214 setenv(GIT_NAMESPACE_ENVIRONMENT
, cmd
, 1);
217 } else if (!strcmp(cmd
, "--work-tree")) {
219 fprintf(stderr
, _("no directory given for '%s' option\n" ), "--work-tree");
220 usage(git_usage_string
);
222 setenv(GIT_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
227 } else if (skip_prefix(cmd
, "--work-tree=", &cmd
)) {
228 setenv(GIT_WORK_TREE_ENVIRONMENT
, cmd
, 1);
231 } else if (!strcmp(cmd
, "--bare")) {
232 char *cwd
= xgetcwd();
233 is_bare_repository_cfg
= 1;
234 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 0);
236 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
239 } else if (!strcmp(cmd
, "-c")) {
241 fprintf(stderr
, _("-c expects a configuration string\n" ));
242 usage(git_usage_string
);
244 git_config_push_parameter((*argv
)[1]);
247 } else if (!strcmp(cmd
, "--config-env")) {
249 fprintf(stderr
, _("no config key given for --config-env\n" ));
250 usage(git_usage_string
);
252 git_config_push_env((*argv
)[1]);
255 } else if (skip_prefix(cmd
, "--config-env=", &cmd
)) {
256 git_config_push_env(cmd
);
257 } else if (!strcmp(cmd
, "--literal-pathspecs")) {
258 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "1", 1);
261 } else if (!strcmp(cmd
, "--no-literal-pathspecs")) {
262 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "0", 1);
265 } else if (!strcmp(cmd
, "--glob-pathspecs")) {
266 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
269 } else if (!strcmp(cmd
, "--noglob-pathspecs")) {
270 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
273 } else if (!strcmp(cmd
, "--icase-pathspecs")) {
274 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT
, "1", 1);
277 } else if (!strcmp(cmd
, "--no-optional-locks")) {
278 setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT
, "0", 1);
281 } else if (!strcmp(cmd
, "--shallow-file")) {
284 set_alternate_shallow_file(the_repository
, (*argv
)[0], 1);
287 } else if (!strcmp(cmd
, "-C")) {
289 fprintf(stderr
, _("no directory given for '%s' option\n" ), "-C");
290 usage(git_usage_string
);
293 if (chdir((*argv
)[1]))
294 die_errno("cannot change to '%s'", (*argv
)[1]);
300 } else if (skip_prefix(cmd
, "--list-cmds=", &cmd
)) {
301 trace2_cmd_name("_query_");
302 if (!strcmp(cmd
, "parseopt")) {
303 struct string_list list
= STRING_LIST_INIT_DUP
;
306 list_builtins(&list
, NO_PARSEOPT
);
307 for (i
= 0; i
< list
.nr
; i
++)
308 printf("%s ", list
.items
[i
].string
);
309 string_list_clear(&list
, 0);
312 exit(list_cmds(cmd
));
315 fprintf(stderr
, _("unknown option: %s\n"), cmd
);
316 usage(git_usage_string
);
322 return (*argv
) - orig_argv
;
325 static int handle_alias(int *argcp
, const char ***argv
)
327 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
328 int count
, option_count
;
329 const char **new_argv
;
330 const char *alias_command
;
333 alias_command
= (*argv
)[0];
334 alias_string
= alias_lookup(alias_command
);
336 if (*argcp
> 1 && !strcmp((*argv
)[1], "-h"))
337 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"),
338 alias_command
, alias_string
);
339 if (alias_string
[0] == '!') {
340 struct child_process child
= CHILD_PROCESS_INIT
;
343 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
344 setup_git_directory_gently(&nongit_ok
);
346 commit_pager_choice();
349 child
.clean_on_exit
= 1;
350 child
.wait_after_clean
= 1;
351 child
.trace2_child_class
= "shell_alias";
352 strvec_push(&child
.args
, alias_string
+ 1);
353 strvec_pushv(&child
.args
, (*argv
) + 1);
355 trace2_cmd_alias(alias_command
, child
.args
.v
);
356 trace2_cmd_list_config();
357 trace2_cmd_list_env_vars();
358 trace2_cmd_name("_run_shell_alias_");
360 ret
= run_command(&child
);
361 if (ret
>= 0) /* normal exit */
364 die_errno(_("while expanding alias '%s': '%s'"),
365 alias_command
, alias_string
+ 1);
367 count
= split_cmdline(alias_string
, &new_argv
);
369 die(_("bad alias.%s string: %s"), alias_command
,
370 _(split_cmdline_strerror(count
)));
371 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
373 die(_("alias '%s' changes environment variables.\n"
374 "You can use '!git' in the alias to do this"),
376 MOVE_ARRAY(new_argv
- option_count
, new_argv
, count
);
377 new_argv
-= option_count
;
380 die(_("empty alias for %s"), alias_command
);
382 if (!strcmp(alias_command
, new_argv
[0]))
383 die(_("recursive alias: %s"), alias_command
);
385 trace_argv_printf(new_argv
,
386 "trace: alias expansion: %s =>",
389 REALLOC_ARRAY(new_argv
, count
+ *argcp
);
390 /* insert after command name */
391 COPY_ARRAY(new_argv
+ count
, *argv
+ 1, *argcp
);
393 trace2_cmd_alias(alias_command
, new_argv
);
394 trace2_cmd_list_config();
395 trace2_cmd_list_env_vars();
408 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
413 int run_setup
= (p
->option
& (RUN_SETUP
| RUN_SETUP_GENTLY
));
415 help
= argc
== 2 && !strcmp(argv
[1], "-h");
416 if (help
&& (run_setup
& RUN_SETUP
))
417 /* demote to GENTLY to allow 'git cmd -h' outside repo */
418 run_setup
= RUN_SETUP_GENTLY
;
420 if (run_setup
& RUN_SETUP
) {
421 prefix
= setup_git_directory();
422 } else if (run_setup
& RUN_SETUP_GENTLY
) {
424 prefix
= setup_git_directory_gently(&nongit_ok
);
428 assert(!prefix
|| *prefix
);
429 precompose_argv_prefix(argc
, argv
, NULL
);
430 if (use_pager
== -1 && run_setup
&&
431 !(p
->option
& DELAY_PAGER_CONFIG
))
432 use_pager
= check_pager_config(p
->cmd
);
433 if (use_pager
== -1 && p
->option
& USE_PAGER
)
435 if (run_setup
&& startup_info
->have_repository
)
436 /* get_git_dir() may set up repo, avoid that */
438 commit_pager_choice();
440 if (!help
&& p
->option
& NEED_WORK_TREE
)
443 trace_argv_printf(argv
, "trace: built-in: git");
444 trace2_cmd_name(p
->cmd
);
445 trace2_cmd_list_config();
446 trace2_cmd_list_env_vars();
448 validate_cache_entries(the_repository
->index
);
449 status
= p
->fn(argc
, argv
, prefix
);
450 validate_cache_entries(the_repository
->index
);
455 /* Somebody closed stdout? */
456 if (fstat(fileno(stdout
), &st
))
458 /* Ignore write errors for pipes and sockets.. */
459 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
462 /* Check for ENOSPC and EIO errors.. */
464 die_errno(_("write failure on standard output"));
466 die(_("unknown write failure on standard output"));
468 die_errno(_("close failed on standard output"));
472 static struct cmd_struct commands
[] = {
473 { "add", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
474 { "am", cmd_am
, RUN_SETUP
| NEED_WORK_TREE
},
475 { "annotate", cmd_annotate
, RUN_SETUP
},
476 { "apply", cmd_apply
, RUN_SETUP_GENTLY
},
477 { "archive", cmd_archive
, RUN_SETUP_GENTLY
},
478 { "bisect", cmd_bisect
, RUN_SETUP
},
479 { "blame", cmd_blame
, RUN_SETUP
},
480 { "branch", cmd_branch
, RUN_SETUP
| DELAY_PAGER_CONFIG
},
481 { "bugreport", cmd_bugreport
, RUN_SETUP_GENTLY
},
482 { "bundle", cmd_bundle
, RUN_SETUP_GENTLY
},
483 { "cat-file", cmd_cat_file
, RUN_SETUP
},
484 { "check-attr", cmd_check_attr
, RUN_SETUP
},
485 { "check-ignore", cmd_check_ignore
, RUN_SETUP
| NEED_WORK_TREE
},
486 { "check-mailmap", cmd_check_mailmap
, RUN_SETUP
},
487 { "check-ref-format", cmd_check_ref_format
, NO_PARSEOPT
},
488 { "checkout", cmd_checkout
, RUN_SETUP
| NEED_WORK_TREE
},
489 { "checkout--worker", cmd_checkout__worker
,
490 RUN_SETUP
| NEED_WORK_TREE
},
491 { "checkout-index", cmd_checkout_index
,
492 RUN_SETUP
| NEED_WORK_TREE
},
493 { "cherry", cmd_cherry
, RUN_SETUP
},
494 { "cherry-pick", cmd_cherry_pick
, RUN_SETUP
| NEED_WORK_TREE
},
495 { "clean", cmd_clean
, RUN_SETUP
| NEED_WORK_TREE
},
496 { "clone", cmd_clone
},
497 { "column", cmd_column
, RUN_SETUP_GENTLY
},
498 { "commit", cmd_commit
, RUN_SETUP
| NEED_WORK_TREE
},
499 { "commit-graph", cmd_commit_graph
, RUN_SETUP
},
500 { "commit-tree", cmd_commit_tree
, RUN_SETUP
},
501 { "config", cmd_config
, RUN_SETUP_GENTLY
| DELAY_PAGER_CONFIG
},
502 { "count-objects", cmd_count_objects
, RUN_SETUP
},
503 { "credential", cmd_credential
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
504 { "credential-cache", cmd_credential_cache
},
505 { "credential-cache--daemon", cmd_credential_cache_daemon
},
506 { "credential-store", cmd_credential_store
},
507 { "describe", cmd_describe
, RUN_SETUP
},
508 { "diagnose", cmd_diagnose
, RUN_SETUP_GENTLY
},
509 { "diff", cmd_diff
, NO_PARSEOPT
},
510 { "diff-files", cmd_diff_files
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
511 { "diff-index", cmd_diff_index
, RUN_SETUP
| NO_PARSEOPT
},
512 { "diff-tree", cmd_diff_tree
, RUN_SETUP
| NO_PARSEOPT
},
513 { "difftool", cmd_difftool
, RUN_SETUP_GENTLY
},
514 { "fast-export", cmd_fast_export
, RUN_SETUP
},
515 { "fast-import", cmd_fast_import
, RUN_SETUP
| NO_PARSEOPT
},
516 { "fetch", cmd_fetch
, RUN_SETUP
},
517 { "fetch-pack", cmd_fetch_pack
, RUN_SETUP
| NO_PARSEOPT
},
518 { "fmt-merge-msg", cmd_fmt_merge_msg
, RUN_SETUP
},
519 { "for-each-ref", cmd_for_each_ref
, RUN_SETUP
},
520 { "for-each-repo", cmd_for_each_repo
, RUN_SETUP_GENTLY
},
521 { "format-patch", cmd_format_patch
, RUN_SETUP
},
522 { "fsck", cmd_fsck
, RUN_SETUP
},
523 { "fsck-objects", cmd_fsck
, RUN_SETUP
},
524 { "fsmonitor--daemon", cmd_fsmonitor__daemon
, RUN_SETUP
},
525 { "gc", cmd_gc
, RUN_SETUP
},
526 { "get-tar-commit-id", cmd_get_tar_commit_id
, NO_PARSEOPT
},
527 { "grep", cmd_grep
, RUN_SETUP_GENTLY
},
528 { "hash-object", cmd_hash_object
},
529 { "help", cmd_help
},
530 { "hook", cmd_hook
, RUN_SETUP
},
531 { "index-pack", cmd_index_pack
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
532 { "init", cmd_init_db
},
533 { "init-db", cmd_init_db
},
534 { "interpret-trailers", cmd_interpret_trailers
, RUN_SETUP_GENTLY
},
535 { "log", cmd_log
, RUN_SETUP
},
536 { "ls-files", cmd_ls_files
, RUN_SETUP
},
537 { "ls-remote", cmd_ls_remote
, RUN_SETUP_GENTLY
},
538 { "ls-tree", cmd_ls_tree
, RUN_SETUP
},
539 { "mailinfo", cmd_mailinfo
, RUN_SETUP_GENTLY
},
540 { "mailsplit", cmd_mailsplit
, NO_PARSEOPT
},
541 { "maintenance", cmd_maintenance
, RUN_SETUP
},
542 { "merge", cmd_merge
, RUN_SETUP
| NEED_WORK_TREE
},
543 { "merge-base", cmd_merge_base
, RUN_SETUP
},
544 { "merge-file", cmd_merge_file
, RUN_SETUP_GENTLY
},
545 { "merge-index", cmd_merge_index
, RUN_SETUP
| NO_PARSEOPT
},
546 { "merge-ours", cmd_merge_ours
, RUN_SETUP
| NO_PARSEOPT
},
547 { "merge-recursive", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
548 { "merge-recursive-ours", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
549 { "merge-recursive-theirs", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
550 { "merge-subtree", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
551 { "merge-tree", cmd_merge_tree
, RUN_SETUP
},
552 { "mktag", cmd_mktag
, RUN_SETUP
},
553 { "mktree", cmd_mktree
, RUN_SETUP
},
554 { "multi-pack-index", cmd_multi_pack_index
, RUN_SETUP
},
555 { "mv", cmd_mv
, RUN_SETUP
| NEED_WORK_TREE
},
556 { "name-rev", cmd_name_rev
, RUN_SETUP
},
557 { "notes", cmd_notes
, RUN_SETUP
},
558 { "pack-objects", cmd_pack_objects
, RUN_SETUP
},
559 { "pack-redundant", cmd_pack_redundant
, RUN_SETUP
| NO_PARSEOPT
},
560 { "pack-refs", cmd_pack_refs
, RUN_SETUP
},
561 { "patch-id", cmd_patch_id
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
562 { "pickaxe", cmd_blame
, RUN_SETUP
},
563 { "prune", cmd_prune
, RUN_SETUP
},
564 { "prune-packed", cmd_prune_packed
, RUN_SETUP
},
565 { "pull", cmd_pull
, RUN_SETUP
| NEED_WORK_TREE
},
566 { "push", cmd_push
, RUN_SETUP
},
567 { "range-diff", cmd_range_diff
, RUN_SETUP
| USE_PAGER
},
568 { "read-tree", cmd_read_tree
, RUN_SETUP
},
569 { "rebase", cmd_rebase
, RUN_SETUP
| NEED_WORK_TREE
},
570 { "receive-pack", cmd_receive_pack
},
571 { "reflog", cmd_reflog
, RUN_SETUP
},
572 { "remote", cmd_remote
, RUN_SETUP
},
573 { "remote-ext", cmd_remote_ext
, NO_PARSEOPT
},
574 { "remote-fd", cmd_remote_fd
, NO_PARSEOPT
},
575 { "repack", cmd_repack
, RUN_SETUP
},
576 { "replace", cmd_replace
, RUN_SETUP
},
577 { "rerere", cmd_rerere
, RUN_SETUP
},
578 { "reset", cmd_reset
, RUN_SETUP
},
579 { "restore", cmd_restore
, RUN_SETUP
| NEED_WORK_TREE
},
580 { "rev-list", cmd_rev_list
, RUN_SETUP
| NO_PARSEOPT
},
581 { "rev-parse", cmd_rev_parse
, NO_PARSEOPT
},
582 { "revert", cmd_revert
, RUN_SETUP
| NEED_WORK_TREE
},
583 { "rm", cmd_rm
, RUN_SETUP
},
584 { "send-pack", cmd_send_pack
, RUN_SETUP
},
585 { "shortlog", cmd_shortlog
, RUN_SETUP_GENTLY
| USE_PAGER
},
586 { "show", cmd_show
, RUN_SETUP
},
587 { "show-branch", cmd_show_branch
, RUN_SETUP
},
588 { "show-index", cmd_show_index
, RUN_SETUP_GENTLY
},
589 { "show-ref", cmd_show_ref
, RUN_SETUP
},
590 { "sparse-checkout", cmd_sparse_checkout
, RUN_SETUP
},
591 { "stage", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
592 { "stash", cmd_stash
, RUN_SETUP
| NEED_WORK_TREE
},
593 { "status", cmd_status
, RUN_SETUP
| NEED_WORK_TREE
},
594 { "stripspace", cmd_stripspace
},
595 { "submodule--helper", cmd_submodule__helper
, RUN_SETUP
},
596 { "switch", cmd_switch
, RUN_SETUP
| NEED_WORK_TREE
},
597 { "symbolic-ref", cmd_symbolic_ref
, RUN_SETUP
},
598 { "tag", cmd_tag
, RUN_SETUP
| DELAY_PAGER_CONFIG
},
599 { "unpack-file", cmd_unpack_file
, RUN_SETUP
| NO_PARSEOPT
},
600 { "unpack-objects", cmd_unpack_objects
, RUN_SETUP
| NO_PARSEOPT
},
601 { "update-index", cmd_update_index
, RUN_SETUP
},
602 { "update-ref", cmd_update_ref
, RUN_SETUP
},
603 { "update-server-info", cmd_update_server_info
, RUN_SETUP
},
604 { "upload-archive", cmd_upload_archive
, NO_PARSEOPT
},
605 { "upload-archive--writer", cmd_upload_archive_writer
, NO_PARSEOPT
},
606 { "upload-pack", cmd_upload_pack
},
607 { "var", cmd_var
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
608 { "verify-commit", cmd_verify_commit
, RUN_SETUP
},
609 { "verify-pack", cmd_verify_pack
},
610 { "verify-tag", cmd_verify_tag
, RUN_SETUP
},
611 { "version", cmd_version
},
612 { "whatchanged", cmd_whatchanged
, RUN_SETUP
},
613 { "worktree", cmd_worktree
, RUN_SETUP
},
614 { "write-tree", cmd_write_tree
, RUN_SETUP
},
617 static struct cmd_struct
*get_builtin(const char *s
)
620 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
621 struct cmd_struct
*p
= commands
+ i
;
622 if (!strcmp(s
, p
->cmd
))
628 int is_builtin(const char *s
)
630 return !!get_builtin(s
);
633 static void list_builtins(struct string_list
*out
, unsigned int exclude_option
)
636 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
637 if (exclude_option
&&
638 (commands
[i
].option
& exclude_option
))
640 string_list_append(out
, commands
[i
].cmd
);
644 void load_builtin_commands(const char *prefix
, struct cmdnames
*cmds
)
650 * Callers can ask for a subset of the commands based on a certain
651 * prefix, which is then dropped from the added names. The names in
652 * the `commands[]` array do not have the `git-` prefix, though,
653 * therefore we must expect the `prefix` to at least start with `git-`.
655 if (!skip_prefix(prefix
, "git-", &prefix
))
656 BUG("prefix '%s' must start with 'git-'", prefix
);
658 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
659 if (skip_prefix(commands
[i
].cmd
, prefix
, &name
))
660 add_cmdname(cmds
, name
, strlen(name
));
663 #ifdef STRIP_EXTENSION
664 static void strip_extension(const char **argv
)
668 if (strip_suffix(argv
[0], STRIP_EXTENSION
, &len
))
669 argv
[0] = xmemdupz(argv
[0], len
);
672 #define strip_extension(cmd)
675 static void handle_builtin(int argc
, const char **argv
)
677 struct strvec args
= STRVEC_INIT
;
679 struct cmd_struct
*builtin
;
681 strip_extension(argv
);
684 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
685 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
689 argv
[0] = cmd
= "help";
691 for (i
= 0; i
< argc
; i
++) {
692 strvec_push(&args
, argv
[i
]);
694 strvec_push(&args
, "--exclude-guides");
701 builtin
= get_builtin(cmd
);
703 exit(run_builtin(builtin
, argc
, argv
));
707 static void execv_dashed_external(const char **argv
)
709 struct child_process cmd
= CHILD_PROCESS_INIT
;
712 if (use_pager
== -1 && !is_builtin(argv
[0]))
713 use_pager
= check_pager_config(argv
[0]);
714 commit_pager_choice();
716 strvec_pushf(&cmd
.args
, "git-%s", argv
[0]);
717 strvec_pushv(&cmd
.args
, argv
+ 1);
718 cmd
.clean_on_exit
= 1;
719 cmd
.wait_after_clean
= 1;
720 cmd
.silent_exec_failure
= 1;
721 cmd
.trace2_child_class
= "dashed";
723 trace2_cmd_name("_run_dashed_");
726 * The code in run_command() logs trace2 child_start/child_exit
727 * events, so we do not need to report exec/exec_result events here.
729 trace_argv_printf(cmd
.args
.v
, "trace: exec:");
732 * If we fail because the command is not found, it is
733 * OK to return. Otherwise, we just pass along the status code,
734 * or our usual generic code if we were not even able to exec
737 status
= run_command(&cmd
);
740 * If the child process ran and we are now going to exit, emit a
741 * generic string as our trace2 command verb to indicate that we
742 * launched a dashed command.
746 else if (errno
!= ENOENT
)
750 static int run_argv(int *argcp
, const char ***argv
)
753 struct string_list cmd_list
= STRING_LIST_INIT_NODUP
;
754 struct string_list_item
*seen
;
758 * If we tried alias and futzed with our environment,
759 * it no longer is safe to invoke builtins directly in
760 * general. We have to spawn them as dashed externals.
762 * NEEDSWORK: if we can figure out cases
763 * where it is safe to do, we can avoid spawning a new
767 handle_builtin(*argcp
, *argv
);
768 else if (get_builtin(**argv
)) {
769 struct child_process cmd
= CHILD_PROCESS_INIT
;
773 * The current process is committed to launching a
774 * child process to run the command named in (**argv)
775 * and exiting. Log a generic string as the trace2
776 * command verb to indicate this. Note that the child
777 * process will log the actual verb when it runs.
779 trace2_cmd_name("_run_git_alias_");
781 commit_pager_choice();
783 strvec_push(&cmd
.args
, "git");
784 for (i
= 0; i
< *argcp
; i
++)
785 strvec_push(&cmd
.args
, (*argv
)[i
]);
787 trace_argv_printf(cmd
.args
.v
, "trace: exec:");
790 * if we fail because the command is not found, it is
791 * OK to return. Otherwise, we just pass along the status code.
793 cmd
.silent_exec_failure
= 1;
794 cmd
.clean_on_exit
= 1;
795 cmd
.wait_after_clean
= 1;
796 cmd
.trace2_child_class
= "git_alias";
797 i
= run_command(&cmd
);
798 if (i
>= 0 || errno
!= ENOENT
)
800 die("could not execute builtin %s", **argv
);
803 /* .. then try the external ones */
804 execv_dashed_external(*argv
);
806 seen
= unsorted_string_list_lookup(&cmd_list
, *argv
[0]);
809 struct strbuf sb
= STRBUF_INIT
;
810 for (i
= 0; i
< cmd_list
.nr
; i
++) {
811 struct string_list_item
*item
= &cmd_list
.items
[i
];
813 strbuf_addf(&sb
, "\n %s", item
->string
);
815 strbuf_addstr(&sb
, " <==");
816 else if (i
== cmd_list
.nr
- 1)
817 strbuf_addstr(&sb
, " ==>");
819 die(_("alias loop detected: expansion of '%s' does"
820 " not terminate:%s"), cmd_list
.items
[0].string
, sb
.buf
);
823 string_list_append(&cmd_list
, *argv
[0]);
826 * It could be an alias -- this works around the insanity
827 * of overriding "git log" with "git show" by having
830 if (!handle_alias(argcp
, argv
))
835 string_list_clear(&cmd_list
, 0);
840 int cmd_main(int argc
, const char **argv
)
849 const char *slash
= find_last_dir_sep(cmd
);
854 trace_command_performance(argv
);
857 * "git-xxxx" is the same as "git xxxx", but we obviously:
859 * - cannot take flags in between the "git" and the "xxxx".
860 * - cannot execute it externally (since it would just do
861 * the same thing over again)
863 * So we just directly call the builtin handler, and die if
864 * that one cannot handle it.
866 if (skip_prefix(cmd
, "git-", &cmd
)) {
868 handle_builtin(argc
, argv
);
869 die(_("cannot handle %s as a builtin"), cmd
);
872 /* Look for flags.. */
875 handle_options(&argv
, &argc
, NULL
);
878 /* The user didn't specify a command; give them help */
879 commit_pager_choice();
880 printf(_("usage: %s\n\n"), git_usage_string
);
881 list_common_cmds_help();
882 printf("\n%s\n", _(git_more_info_string
));
886 if (!strcmp("--version", argv
[0]) || !strcmp("-v", argv
[0]))
888 else if (!strcmp("--help", argv
[0]) || !strcmp("-h", argv
[0]))
894 * We use PATH to find git commands, but we prepend some higher
895 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
896 * environment, and the $(gitexecdir) from the Makefile at build
902 int was_alias
= run_argv(&argc
, &argv
);
906 fprintf(stderr
, _("expansion of alias '%s' failed; "
907 "'%s' is not a git command\n"),
912 cmd
= argv
[0] = help_unknown_cmd(cmd
);
918 fprintf(stderr
, _("failed to run command '%s': %s\n"),
919 cmd
, strerror(errno
));