1 #define USE_THE_REPOSITORY_VARIABLE
5 #include "environment.h"
9 #include "object-file.h"
11 #include "read-cache-ll.h"
12 #include "run-command.h"
14 #include "replace-object.h"
21 #define RUN_SETUP (1<<0)
22 #define RUN_SETUP_GENTLY (1<<1)
23 #define USE_PAGER (1<<2)
25 * require working tree to be present -- anything uses this needs
26 * RUN_SETUP for reading from the configuration file.
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 */
34 int (*fn
)(int, const char **, const char *, struct repository
*);
38 const char git_usage_string
[] =
39 N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
40 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
41 " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]\n"
42 " [--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]\n"
43 " [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]\n"
44 " <command> [<args>]");
46 const char git_more_info_string
[] =
47 N_("'git help -a' and 'git help -g' list available subcommands and some\n"
48 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
49 "to read about a specific subcommand or concept.\n"
50 "See 'git help git' for an overview of the system.");
52 static int use_pager
= -1;
54 static void list_builtins(struct string_list
*list
, unsigned int exclude_option
);
56 static void exclude_helpers_from_list(struct string_list
*list
)
60 while (i
< list
->nr
) {
61 if (strstr(list
->items
[i
].string
, "--"))
62 unsorted_string_list_delete_item(list
, i
, 0);
68 static int match_token(const char *spec
, int len
, const char *token
)
70 int token_len
= strlen(token
);
72 return len
== token_len
&& !strncmp(spec
, token
, token_len
);
75 static int list_cmds(const char *spec
)
77 struct string_list list
= STRING_LIST_INIT_DUP
;
82 * Set up the repository so we can pick up any repo-level config (like
83 * completion.commands).
85 setup_git_directory_gently(&nongit
);
88 const char *sep
= strchrnul(spec
, ',');
91 if (match_token(spec
, len
, "builtins"))
92 list_builtins(&list
, 0);
93 else if (match_token(spec
, len
, "main"))
94 list_all_main_cmds(&list
);
95 else if (match_token(spec
, len
, "others"))
96 list_all_other_cmds(&list
);
97 else if (match_token(spec
, len
, "nohelpers"))
98 exclude_helpers_from_list(&list
);
99 else if (match_token(spec
, len
, "alias"))
101 else if (match_token(spec
, len
, "config"))
102 list_cmds_by_config(&list
);
103 else if (len
> 5 && !strncmp(spec
, "list-", 5)) {
104 struct strbuf sb
= STRBUF_INIT
;
106 strbuf_add(&sb
, spec
+ 5, len
- 5);
107 list_cmds_by_category(&list
, sb
.buf
);
111 die(_("unsupported command listing type '%s'"), spec
);
116 for (i
= 0; i
< list
.nr
; i
++)
117 puts(list
.items
[i
].string
);
118 string_list_clear(&list
, 0);
122 static void commit_pager_choice(void)
126 setenv("GIT_PAGER", "cat", 1);
136 void setup_auto_pager(const char *cmd
, int def
)
138 if (use_pager
!= -1 || pager_in_use())
140 use_pager
= check_pager_config(cmd
);
143 commit_pager_choice();
146 static void print_system_path(const char *path
)
148 char *s_path
= system_path(path
);
153 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
155 const char **orig_argv
= *argv
;
158 const char *cmd
= (*argv
)[0];
163 * For legacy reasons, the "version" and "help"
164 * commands can be written with "--" prepended
165 * to make them look like flags.
167 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "-h") ||
168 !strcmp(cmd
, "--version") || !strcmp(cmd
, "-v"))
172 * Check remaining flags.
174 if (skip_prefix(cmd
, "--exec-path", &cmd
)) {
176 git_set_exec_path(cmd
+ 1);
178 puts(git_exec_path());
179 trace2_cmd_name("_query_");
182 } else if (!strcmp(cmd
, "--html-path")) {
183 print_system_path(GIT_HTML_PATH
);
184 trace2_cmd_name("_query_");
186 } else if (!strcmp(cmd
, "--man-path")) {
187 print_system_path(GIT_MAN_PATH
);
188 trace2_cmd_name("_query_");
190 } else if (!strcmp(cmd
, "--info-path")) {
191 print_system_path(GIT_INFO_PATH
);
192 trace2_cmd_name("_query_");
194 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
196 } else if (!strcmp(cmd
, "-P") || !strcmp(cmd
, "--no-pager")) {
200 } else if (!strcmp(cmd
, "--no-lazy-fetch")) {
201 fetch_if_missing
= 0;
202 setenv(NO_LAZY_FETCH_ENVIRONMENT
, "1", 1);
205 } else if (!strcmp(cmd
, "--no-replace-objects")) {
206 disable_replace_refs();
207 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT
, "1", 1);
210 } else if (!strcmp(cmd
, "--git-dir")) {
212 fprintf(stderr
, _("no directory given for '%s' option\n" ), "--git-dir");
213 usage(git_usage_string
);
215 setenv(GIT_DIR_ENVIRONMENT
, (*argv
)[1], 1);
220 } else if (skip_prefix(cmd
, "--git-dir=", &cmd
)) {
221 setenv(GIT_DIR_ENVIRONMENT
, cmd
, 1);
224 } else if (!strcmp(cmd
, "--namespace")) {
226 fprintf(stderr
, _("no namespace given for --namespace\n" ));
227 usage(git_usage_string
);
229 setenv(GIT_NAMESPACE_ENVIRONMENT
, (*argv
)[1], 1);
234 } else if (skip_prefix(cmd
, "--namespace=", &cmd
)) {
235 setenv(GIT_NAMESPACE_ENVIRONMENT
, cmd
, 1);
238 } else if (!strcmp(cmd
, "--work-tree")) {
240 fprintf(stderr
, _("no directory given for '%s' option\n" ), "--work-tree");
241 usage(git_usage_string
);
243 setenv(GIT_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
248 } else if (skip_prefix(cmd
, "--work-tree=", &cmd
)) {
249 setenv(GIT_WORK_TREE_ENVIRONMENT
, cmd
, 1);
252 } else if (!strcmp(cmd
, "--bare")) {
253 char *cwd
= xgetcwd();
254 is_bare_repository_cfg
= 1;
255 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 0);
257 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
260 } else if (!strcmp(cmd
, "-c")) {
262 fprintf(stderr
, _("-c expects a configuration string\n" ));
263 usage(git_usage_string
);
265 git_config_push_parameter((*argv
)[1]);
268 } else if (!strcmp(cmd
, "--config-env")) {
270 fprintf(stderr
, _("no config key given for --config-env\n" ));
271 usage(git_usage_string
);
273 git_config_push_env((*argv
)[1]);
276 } else if (skip_prefix(cmd
, "--config-env=", &cmd
)) {
277 git_config_push_env(cmd
);
278 } else if (!strcmp(cmd
, "--literal-pathspecs")) {
279 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "1", 1);
282 } else if (!strcmp(cmd
, "--no-literal-pathspecs")) {
283 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "0", 1);
286 } else if (!strcmp(cmd
, "--glob-pathspecs")) {
287 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
290 } else if (!strcmp(cmd
, "--noglob-pathspecs")) {
291 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
294 } else if (!strcmp(cmd
, "--icase-pathspecs")) {
295 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT
, "1", 1);
298 } else if (!strcmp(cmd
, "--no-optional-locks")) {
299 setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT
, "0", 1);
302 } else if (!strcmp(cmd
, "--shallow-file")) {
305 set_alternate_shallow_file(the_repository
, (*argv
)[0], 1);
308 } else if (!strcmp(cmd
, "-C")) {
310 fprintf(stderr
, _("no directory given for '%s' option\n" ), "-C");
311 usage(git_usage_string
);
314 if (chdir((*argv
)[1]))
315 die_errno("cannot change to '%s'", (*argv
)[1]);
321 } else if (skip_prefix(cmd
, "--list-cmds=", &cmd
)) {
322 trace2_cmd_name("_query_");
323 if (!strcmp(cmd
, "parseopt")) {
324 struct string_list list
= STRING_LIST_INIT_DUP
;
327 list_builtins(&list
, NO_PARSEOPT
);
328 for (i
= 0; i
< list
.nr
; i
++)
329 printf("%s ", list
.items
[i
].string
);
330 string_list_clear(&list
, 0);
333 exit(list_cmds(cmd
));
335 } else if (!strcmp(cmd
, "--attr-source")) {
337 fprintf(stderr
, _("no attribute source given for --attr-source\n" ));
338 usage(git_usage_string
);
340 setenv(GIT_ATTR_SOURCE_ENVIRONMENT
, (*argv
)[1], 1);
345 } else if (skip_prefix(cmd
, "--attr-source=", &cmd
)) {
346 set_git_attr_source(cmd
);
347 setenv(GIT_ATTR_SOURCE_ENVIRONMENT
, cmd
, 1);
350 } else if (!strcmp(cmd
, "--no-advice")) {
351 setenv(GIT_ADVICE_ENVIRONMENT
, "0", 1);
355 fprintf(stderr
, _("unknown option: %s\n"), cmd
);
356 usage(git_usage_string
);
362 return (*argv
) - orig_argv
;
365 static int handle_alias(int *argcp
, const char ***argv
)
367 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
368 int count
, option_count
;
369 const char **new_argv
;
370 const char *alias_command
;
373 alias_command
= (*argv
)[0];
374 alias_string
= alias_lookup(alias_command
);
376 if (*argcp
> 1 && !strcmp((*argv
)[1], "-h"))
377 fprintf_ln(stderr
, _("'%s' is aliased to '%s'"),
378 alias_command
, alias_string
);
379 if (alias_string
[0] == '!') {
380 struct child_process child
= CHILD_PROCESS_INIT
;
383 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
384 setup_git_directory_gently(&nongit_ok
);
386 commit_pager_choice();
389 child
.clean_on_exit
= 1;
390 child
.wait_after_clean
= 1;
391 child
.trace2_child_class
= "shell_alias";
392 strvec_push(&child
.args
, alias_string
+ 1);
393 strvec_pushv(&child
.args
, (*argv
) + 1);
395 trace2_cmd_alias(alias_command
, child
.args
.v
);
396 trace2_cmd_name("_run_shell_alias_");
398 ret
= run_command(&child
);
399 if (ret
>= 0) /* normal exit */
402 die_errno(_("while expanding alias '%s': '%s'"),
403 alias_command
, alias_string
+ 1);
405 count
= split_cmdline(alias_string
, &new_argv
);
407 die(_("bad alias.%s string: %s"), alias_command
,
408 _(split_cmdline_strerror(count
)));
409 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
411 die(_("alias '%s' changes environment variables.\n"
412 "You can use '!git' in the alias to do this"),
414 MOVE_ARRAY(new_argv
- option_count
, new_argv
, count
);
415 new_argv
-= option_count
;
418 die(_("empty alias for %s"), alias_command
);
420 if (!strcmp(alias_command
, new_argv
[0]))
421 die(_("recursive alias: %s"), alias_command
);
423 trace_argv_printf(new_argv
,
424 "trace: alias expansion: %s =>",
427 REALLOC_ARRAY(new_argv
, count
+ *argcp
);
428 /* insert after command name */
429 COPY_ARRAY(new_argv
+ count
, *argv
+ 1, *argcp
);
431 trace2_cmd_alias(alias_command
, new_argv
);
444 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
, struct repository
*repo
)
450 int run_setup
= (p
->option
& (RUN_SETUP
| RUN_SETUP_GENTLY
));
452 help
= argc
== 2 && !strcmp(argv
[1], "-h");
453 if (help
&& (run_setup
& RUN_SETUP
))
454 /* demote to GENTLY to allow 'git cmd -h' outside repo */
455 run_setup
= RUN_SETUP_GENTLY
;
457 if (run_setup
& RUN_SETUP
) {
458 prefix
= setup_git_directory();
460 } else if (run_setup
& RUN_SETUP_GENTLY
) {
461 prefix
= setup_git_directory_gently(&no_repo
);
465 assert(!prefix
|| *prefix
);
466 precompose_argv_prefix(argc
, argv
, NULL
);
467 if (use_pager
== -1 && run_setup
&&
468 !(p
->option
& DELAY_PAGER_CONFIG
))
469 use_pager
= check_pager_config(p
->cmd
);
470 if (use_pager
== -1 && p
->option
& USE_PAGER
)
472 if (run_setup
&& startup_info
->have_repository
)
473 /* get_git_dir() may set up repo, avoid that */
475 commit_pager_choice();
477 if (!help
&& p
->option
& NEED_WORK_TREE
)
480 trace_argv_printf(argv
, "trace: built-in: git");
481 trace2_cmd_name(p
->cmd
);
483 validate_cache_entries(repo
->index
);
484 status
= p
->fn(argc
, argv
, prefix
, no_repo
? NULL
: repo
);
485 validate_cache_entries(repo
->index
);
490 /* Somebody closed stdout? */
491 if (fstat(fileno(stdout
), &st
))
493 /* Ignore write errors for pipes and sockets.. */
494 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
497 /* Check for ENOSPC and EIO errors.. */
499 die_errno(_("write failure on standard output"));
501 die(_("unknown write failure on standard output"));
503 die_errno(_("close failed on standard output"));
507 static struct cmd_struct commands
[] = {
508 { "add", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
509 { "am", cmd_am
, RUN_SETUP
| NEED_WORK_TREE
},
510 { "annotate", cmd_annotate
, RUN_SETUP
},
511 { "apply", cmd_apply
, RUN_SETUP_GENTLY
},
512 { "archive", cmd_archive
, RUN_SETUP_GENTLY
},
513 { "bisect", cmd_bisect
, RUN_SETUP
},
514 { "blame", cmd_blame
, RUN_SETUP
},
515 { "branch", cmd_branch
, RUN_SETUP
| DELAY_PAGER_CONFIG
},
516 { "bugreport", cmd_bugreport
, RUN_SETUP_GENTLY
},
517 { "bundle", cmd_bundle
, RUN_SETUP_GENTLY
},
518 { "cat-file", cmd_cat_file
, RUN_SETUP
},
519 { "check-attr", cmd_check_attr
, RUN_SETUP
},
520 { "check-ignore", cmd_check_ignore
, RUN_SETUP
| NEED_WORK_TREE
},
521 { "check-mailmap", cmd_check_mailmap
, RUN_SETUP
},
522 { "check-ref-format", cmd_check_ref_format
, NO_PARSEOPT
},
523 { "checkout", cmd_checkout
, RUN_SETUP
| NEED_WORK_TREE
},
524 { "checkout--worker", cmd_checkout__worker
,
525 RUN_SETUP
| NEED_WORK_TREE
},
526 { "checkout-index", cmd_checkout_index
,
527 RUN_SETUP
| NEED_WORK_TREE
},
528 { "cherry", cmd_cherry
, RUN_SETUP
},
529 { "cherry-pick", cmd_cherry_pick
, RUN_SETUP
| NEED_WORK_TREE
},
530 { "clean", cmd_clean
, RUN_SETUP
| NEED_WORK_TREE
},
531 { "clone", cmd_clone
},
532 { "column", cmd_column
, RUN_SETUP_GENTLY
},
533 { "commit", cmd_commit
, RUN_SETUP
| NEED_WORK_TREE
},
534 { "commit-graph", cmd_commit_graph
, RUN_SETUP
},
535 { "commit-tree", cmd_commit_tree
, RUN_SETUP
},
536 { "config", cmd_config
, RUN_SETUP_GENTLY
| DELAY_PAGER_CONFIG
},
537 { "count-objects", cmd_count_objects
, RUN_SETUP
},
538 { "credential", cmd_credential
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
539 { "credential-cache", cmd_credential_cache
},
540 { "credential-cache--daemon", cmd_credential_cache_daemon
},
541 { "credential-store", cmd_credential_store
},
542 { "describe", cmd_describe
, RUN_SETUP
},
543 { "diagnose", cmd_diagnose
, RUN_SETUP_GENTLY
},
544 { "diff", cmd_diff
, NO_PARSEOPT
},
545 { "diff-files", cmd_diff_files
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
546 { "diff-index", cmd_diff_index
, RUN_SETUP
| NO_PARSEOPT
},
547 { "diff-tree", cmd_diff_tree
, RUN_SETUP
| NO_PARSEOPT
},
548 { "difftool", cmd_difftool
, RUN_SETUP_GENTLY
},
549 { "fast-export", cmd_fast_export
, RUN_SETUP
},
550 { "fast-import", cmd_fast_import
, RUN_SETUP
| NO_PARSEOPT
},
551 { "fetch", cmd_fetch
, RUN_SETUP
},
552 { "fetch-pack", cmd_fetch_pack
, RUN_SETUP
| NO_PARSEOPT
},
553 { "fmt-merge-msg", cmd_fmt_merge_msg
, RUN_SETUP
},
554 { "for-each-ref", cmd_for_each_ref
, RUN_SETUP
},
555 { "for-each-repo", cmd_for_each_repo
, RUN_SETUP_GENTLY
},
556 { "format-patch", cmd_format_patch
, RUN_SETUP
},
557 { "fsck", cmd_fsck
, RUN_SETUP
},
558 { "fsck-objects", cmd_fsck
, RUN_SETUP
},
559 { "fsmonitor--daemon", cmd_fsmonitor__daemon
, RUN_SETUP
},
560 { "gc", cmd_gc
, RUN_SETUP
},
561 { "get-tar-commit-id", cmd_get_tar_commit_id
, NO_PARSEOPT
},
562 { "grep", cmd_grep
, RUN_SETUP_GENTLY
},
563 { "hash-object", cmd_hash_object
},
564 { "help", cmd_help
},
565 { "hook", cmd_hook
, RUN_SETUP
},
566 { "index-pack", cmd_index_pack
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
567 { "init", cmd_init_db
},
568 { "init-db", cmd_init_db
},
569 { "interpret-trailers", cmd_interpret_trailers
, RUN_SETUP_GENTLY
},
570 { "log", cmd_log
, RUN_SETUP
},
571 { "ls-files", cmd_ls_files
, RUN_SETUP
},
572 { "ls-remote", cmd_ls_remote
, RUN_SETUP_GENTLY
},
573 { "ls-tree", cmd_ls_tree
, RUN_SETUP
},
574 { "mailinfo", cmd_mailinfo
, RUN_SETUP_GENTLY
},
575 { "mailsplit", cmd_mailsplit
, NO_PARSEOPT
},
576 { "maintenance", cmd_maintenance
, RUN_SETUP
},
577 { "merge", cmd_merge
, RUN_SETUP
| NEED_WORK_TREE
},
578 { "merge-base", cmd_merge_base
, RUN_SETUP
},
579 { "merge-file", cmd_merge_file
, RUN_SETUP_GENTLY
},
580 { "merge-index", cmd_merge_index
, RUN_SETUP
| NO_PARSEOPT
},
581 { "merge-ours", cmd_merge_ours
, RUN_SETUP
| NO_PARSEOPT
},
582 { "merge-recursive", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
583 { "merge-recursive-ours", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
584 { "merge-recursive-theirs", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
585 { "merge-subtree", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
| NO_PARSEOPT
},
586 { "merge-tree", cmd_merge_tree
, RUN_SETUP
},
587 { "mktag", cmd_mktag
, RUN_SETUP
},
588 { "mktree", cmd_mktree
, RUN_SETUP
},
589 { "multi-pack-index", cmd_multi_pack_index
, RUN_SETUP
},
590 { "mv", cmd_mv
, RUN_SETUP
| NEED_WORK_TREE
},
591 { "name-rev", cmd_name_rev
, RUN_SETUP
},
592 { "notes", cmd_notes
, RUN_SETUP
},
593 { "pack-objects", cmd_pack_objects
, RUN_SETUP
},
594 { "pack-redundant", cmd_pack_redundant
, RUN_SETUP
| NO_PARSEOPT
},
595 { "pack-refs", cmd_pack_refs
, RUN_SETUP
},
596 { "patch-id", cmd_patch_id
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
597 { "pickaxe", cmd_blame
, RUN_SETUP
},
598 { "prune", cmd_prune
, RUN_SETUP
},
599 { "prune-packed", cmd_prune_packed
, RUN_SETUP
},
600 { "pull", cmd_pull
, RUN_SETUP
| NEED_WORK_TREE
},
601 { "push", cmd_push
, RUN_SETUP
},
602 { "range-diff", cmd_range_diff
, RUN_SETUP
| USE_PAGER
},
603 { "read-tree", cmd_read_tree
, RUN_SETUP
},
604 { "rebase", cmd_rebase
, RUN_SETUP
| NEED_WORK_TREE
},
605 { "receive-pack", cmd_receive_pack
},
606 { "reflog", cmd_reflog
, RUN_SETUP
},
607 { "refs", cmd_refs
, RUN_SETUP
},
608 { "remote", cmd_remote
, RUN_SETUP
},
609 { "remote-ext", cmd_remote_ext
, NO_PARSEOPT
},
610 { "remote-fd", cmd_remote_fd
, NO_PARSEOPT
},
611 { "repack", cmd_repack
, RUN_SETUP
},
612 { "replace", cmd_replace
, RUN_SETUP
},
613 { "replay", cmd_replay
, RUN_SETUP
},
614 { "rerere", cmd_rerere
, RUN_SETUP
},
615 { "reset", cmd_reset
, RUN_SETUP
},
616 { "restore", cmd_restore
, RUN_SETUP
| NEED_WORK_TREE
},
617 { "rev-list", cmd_rev_list
, RUN_SETUP
| NO_PARSEOPT
},
618 { "rev-parse", cmd_rev_parse
, NO_PARSEOPT
},
619 { "revert", cmd_revert
, RUN_SETUP
| NEED_WORK_TREE
},
620 { "rm", cmd_rm
, RUN_SETUP
},
621 { "send-pack", cmd_send_pack
, RUN_SETUP
},
622 { "shortlog", cmd_shortlog
, RUN_SETUP_GENTLY
| USE_PAGER
},
623 { "show", cmd_show
, RUN_SETUP
},
624 { "show-branch", cmd_show_branch
, RUN_SETUP
},
625 { "show-index", cmd_show_index
, RUN_SETUP_GENTLY
},
626 { "show-ref", cmd_show_ref
, RUN_SETUP
},
627 { "sparse-checkout", cmd_sparse_checkout
, RUN_SETUP
},
628 { "stage", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
629 { "stash", cmd_stash
, RUN_SETUP
| NEED_WORK_TREE
},
630 { "status", cmd_status
, RUN_SETUP
| NEED_WORK_TREE
},
631 { "stripspace", cmd_stripspace
},
632 { "submodule--helper", cmd_submodule__helper
, RUN_SETUP
},
633 { "switch", cmd_switch
, RUN_SETUP
| NEED_WORK_TREE
},
634 { "symbolic-ref", cmd_symbolic_ref
, RUN_SETUP
},
635 { "tag", cmd_tag
, RUN_SETUP
| DELAY_PAGER_CONFIG
},
636 { "unpack-file", cmd_unpack_file
, RUN_SETUP
| NO_PARSEOPT
},
637 { "unpack-objects", cmd_unpack_objects
, RUN_SETUP
| NO_PARSEOPT
},
638 { "update-index", cmd_update_index
, RUN_SETUP
},
639 { "update-ref", cmd_update_ref
, RUN_SETUP
},
640 { "update-server-info", cmd_update_server_info
, RUN_SETUP
},
641 { "upload-archive", cmd_upload_archive
, NO_PARSEOPT
},
642 { "upload-archive--writer", cmd_upload_archive_writer
, NO_PARSEOPT
},
643 { "upload-pack", cmd_upload_pack
},
644 { "var", cmd_var
, RUN_SETUP_GENTLY
| NO_PARSEOPT
},
645 { "verify-commit", cmd_verify_commit
, RUN_SETUP
},
646 { "verify-pack", cmd_verify_pack
},
647 { "verify-tag", cmd_verify_tag
, RUN_SETUP
},
648 { "version", cmd_version
},
649 { "whatchanged", cmd_whatchanged
, RUN_SETUP
},
650 { "worktree", cmd_worktree
, RUN_SETUP
},
651 { "write-tree", cmd_write_tree
, RUN_SETUP
},
654 static struct cmd_struct
*get_builtin(const char *s
)
657 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
658 struct cmd_struct
*p
= commands
+ i
;
659 if (!strcmp(s
, p
->cmd
))
665 int is_builtin(const char *s
)
667 return !!get_builtin(s
);
670 static void list_builtins(struct string_list
*out
, unsigned int exclude_option
)
673 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
674 if (exclude_option
&&
675 (commands
[i
].option
& exclude_option
))
677 string_list_append(out
, commands
[i
].cmd
);
681 void load_builtin_commands(const char *prefix
, struct cmdnames
*cmds
)
687 * Callers can ask for a subset of the commands based on a certain
688 * prefix, which is then dropped from the added names. The names in
689 * the `commands[]` array do not have the `git-` prefix, though,
690 * therefore we must expect the `prefix` to at least start with `git-`.
692 if (!skip_prefix(prefix
, "git-", &prefix
))
693 BUG("prefix '%s' must start with 'git-'", prefix
);
695 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
696 if (skip_prefix(commands
[i
].cmd
, prefix
, &name
))
697 add_cmdname(cmds
, name
, strlen(name
));
700 #ifdef STRIP_EXTENSION
701 static void strip_extension(const char **argv
)
705 if (strip_suffix(argv
[0], STRIP_EXTENSION
, &len
))
706 argv
[0] = xmemdupz(argv
[0], len
);
709 #define strip_extension(cmd)
712 static void handle_builtin(int argc
, const char **argv
)
714 struct strvec args
= STRVEC_INIT
;
715 const char **argv_copy
= NULL
;
717 struct cmd_struct
*builtin
;
719 strip_extension(argv
);
722 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
723 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
727 argv
[0] = cmd
= "help";
729 for (i
= 0; i
< argc
; i
++) {
730 strvec_push(&args
, argv
[i
]);
732 strvec_push(&args
, "--exclude-guides");
738 * `run_builtin()` will modify the argv array, so we need to
739 * create a shallow copy such that we can free all of its
742 CALLOC_ARRAY(argv_copy
, argc
+ 1);
743 COPY_ARRAY(argv_copy
, args
.v
, argc
);
748 builtin
= get_builtin(cmd
);
750 int ret
= run_builtin(builtin
, argc
, argv
, the_repository
);
760 static void execv_dashed_external(const char **argv
)
762 struct child_process cmd
= CHILD_PROCESS_INIT
;
765 if (use_pager
== -1 && !is_builtin(argv
[0]))
766 use_pager
= check_pager_config(argv
[0]);
767 commit_pager_choice();
769 strvec_pushf(&cmd
.args
, "git-%s", argv
[0]);
770 strvec_pushv(&cmd
.args
, argv
+ 1);
771 cmd
.clean_on_exit
= 1;
772 cmd
.wait_after_clean
= 1;
773 cmd
.silent_exec_failure
= 1;
774 cmd
.trace2_child_class
= "dashed";
776 trace2_cmd_name("_run_dashed_");
779 * The code in run_command() logs trace2 child_start/child_exit
780 * events, so we do not need to report exec/exec_result events here.
782 trace_argv_printf(cmd
.args
.v
, "trace: exec:");
785 * If we fail because the command is not found, it is
786 * OK to return. Otherwise, we just pass along the status code,
787 * or our usual generic code if we were not even able to exec
790 status
= run_command(&cmd
);
793 * If the child process ran and we are now going to exit, emit a
794 * generic string as our trace2 command verb to indicate that we
795 * launched a dashed command.
799 else if (errno
!= ENOENT
)
803 static int run_argv(int *argcp
, const char ***argv
)
806 struct string_list cmd_list
= STRING_LIST_INIT_NODUP
;
807 struct string_list_item
*seen
;
811 * If we tried alias and futzed with our environment,
812 * it no longer is safe to invoke builtins directly in
813 * general. We have to spawn them as dashed externals.
815 * NEEDSWORK: if we can figure out cases
816 * where it is safe to do, we can avoid spawning a new
820 handle_builtin(*argcp
, *argv
);
821 else if (get_builtin(**argv
)) {
822 struct child_process cmd
= CHILD_PROCESS_INIT
;
826 * The current process is committed to launching a
827 * child process to run the command named in (**argv)
828 * and exiting. Log a generic string as the trace2
829 * command verb to indicate this. Note that the child
830 * process will log the actual verb when it runs.
832 trace2_cmd_name("_run_git_alias_");
834 commit_pager_choice();
836 strvec_push(&cmd
.args
, "git");
837 for (i
= 0; i
< *argcp
; i
++)
838 strvec_push(&cmd
.args
, (*argv
)[i
]);
840 trace_argv_printf(cmd
.args
.v
, "trace: exec:");
843 * if we fail because the command is not found, it is
844 * OK to return. Otherwise, we just pass along the status code.
846 cmd
.silent_exec_failure
= 1;
847 cmd
.clean_on_exit
= 1;
848 cmd
.wait_after_clean
= 1;
849 cmd
.trace2_child_class
= "git_alias";
850 i
= run_command(&cmd
);
851 if (i
>= 0 || errno
!= ENOENT
)
853 die("could not execute builtin %s", **argv
);
856 /* .. then try the external ones */
857 execv_dashed_external(*argv
);
859 seen
= unsorted_string_list_lookup(&cmd_list
, *argv
[0]);
862 struct strbuf sb
= STRBUF_INIT
;
863 for (i
= 0; i
< cmd_list
.nr
; i
++) {
864 struct string_list_item
*item
= &cmd_list
.items
[i
];
866 strbuf_addf(&sb
, "\n %s", item
->string
);
868 strbuf_addstr(&sb
, " <==");
869 else if (i
== cmd_list
.nr
- 1)
870 strbuf_addstr(&sb
, " ==>");
872 die(_("alias loop detected: expansion of '%s' does"
873 " not terminate:%s"), cmd_list
.items
[0].string
, sb
.buf
);
876 string_list_append(&cmd_list
, *argv
[0]);
879 * It could be an alias -- this works around the insanity
880 * of overriding "git log" with "git show" by having
883 if (!handle_alias(argcp
, argv
))
888 string_list_clear(&cmd_list
, 0);
893 int cmd_main(int argc
, const char **argv
)
902 const char *slash
= find_last_dir_sep(cmd
);
907 trace_command_performance(argv
);
910 * "git-xxxx" is the same as "git xxxx", but we obviously:
912 * - cannot take flags in between the "git" and the "xxxx".
913 * - cannot execute it externally (since it would just do
914 * the same thing over again)
916 * So we just directly call the builtin handler, and die if
917 * that one cannot handle it.
919 if (skip_prefix(cmd
, "git-", &cmd
)) {
921 handle_builtin(argc
, argv
);
922 die(_("cannot handle %s as a builtin"), cmd
);
925 /* Look for flags.. */
928 handle_options(&argv
, &argc
, NULL
);
931 /* The user didn't specify a command; give them help */
932 commit_pager_choice();
933 printf(_("usage: %s\n\n"), git_usage_string
);
934 list_common_cmds_help();
935 printf("\n%s\n", _(git_more_info_string
));
939 if (!strcmp("--version", argv
[0]) || !strcmp("-v", argv
[0]))
941 else if (!strcmp("--help", argv
[0]) || !strcmp("-h", argv
[0]))
947 * We use PATH to find git commands, but we prepend some higher
948 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
949 * environment, and the $(gitexecdir) from the Makefile at build
955 int was_alias
= run_argv(&argc
, &argv
);
959 fprintf(stderr
, _("expansion of alias '%s' failed; "
960 "'%s' is not a git command\n"),
965 cmd
= argv
[0] = help_unknown_cmd(cmd
);
971 fprintf(stderr
, _("failed to run command '%s': %s\n"),
972 cmd
, strerror(errno
));