4 #include "run-command.h"
6 const char git_usage_string
[] =
7 "git [--version] [--help] [-C <path>] [-c name=value]\n"
8 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
9 " [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\n"
10 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
11 " <command> [<args>]";
13 const char git_more_info_string
[] =
14 N_("'git help -a' and 'git help -g' list available subcommands and some\n"
15 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
16 "to read about a specific subcommand or concept.");
18 static int use_pager
= -1;
20 static void list_builtins(void);
22 static void commit_pager_choice(void) {
25 setenv("GIT_PAGER", "cat", 1);
35 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
37 const char **orig_argv
= *argv
;
40 const char *cmd
= (*argv
)[0];
45 * For legacy reasons, the "version" and "help"
46 * commands can be written with "--" prepended
47 * to make them look like flags.
49 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
53 * Check remaining flags.
55 if (skip_prefix(cmd
, "--exec-path", &cmd
)) {
57 git_set_argv_exec_path(cmd
+ 1);
59 puts(git_exec_path());
62 } else if (!strcmp(cmd
, "--html-path")) {
63 puts(system_path(GIT_HTML_PATH
));
65 } else if (!strcmp(cmd
, "--man-path")) {
66 puts(system_path(GIT_MAN_PATH
));
68 } else if (!strcmp(cmd
, "--info-path")) {
69 puts(system_path(GIT_INFO_PATH
));
71 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
73 } else if (!strcmp(cmd
, "--no-pager")) {
77 } else if (!strcmp(cmd
, "--no-replace-objects")) {
78 check_replace_refs
= 0;
79 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT
, "1", 1);
82 } else if (!strcmp(cmd
, "--git-dir")) {
84 fprintf(stderr
, "No directory given for --git-dir.\n" );
85 usage(git_usage_string
);
87 setenv(GIT_DIR_ENVIRONMENT
, (*argv
)[1], 1);
92 } else if (skip_prefix(cmd
, "--git-dir=", &cmd
)) {
93 setenv(GIT_DIR_ENVIRONMENT
, cmd
, 1);
96 } else if (!strcmp(cmd
, "--namespace")) {
98 fprintf(stderr
, "No namespace given for --namespace.\n" );
99 usage(git_usage_string
);
101 setenv(GIT_NAMESPACE_ENVIRONMENT
, (*argv
)[1], 1);
106 } else if (skip_prefix(cmd
, "--namespace=", &cmd
)) {
107 setenv(GIT_NAMESPACE_ENVIRONMENT
, cmd
, 1);
110 } else if (!strcmp(cmd
, "--work-tree")) {
112 fprintf(stderr
, "No directory given for --work-tree.\n" );
113 usage(git_usage_string
);
115 setenv(GIT_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
120 } else if (skip_prefix(cmd
, "--work-tree=", &cmd
)) {
121 setenv(GIT_WORK_TREE_ENVIRONMENT
, cmd
, 1);
124 } else if (!strcmp(cmd
, "--super-prefix")) {
126 fprintf(stderr
, "No prefix given for --super-prefix.\n" );
127 usage(git_usage_string
);
129 setenv(GIT_SUPER_PREFIX_ENVIRONMENT
, (*argv
)[1], 1);
134 } else if (skip_prefix(cmd
, "--super-prefix=", &cmd
)) {
135 setenv(GIT_SUPER_PREFIX_ENVIRONMENT
, cmd
, 1);
138 } else if (!strcmp(cmd
, "--bare")) {
139 char *cwd
= xgetcwd();
140 is_bare_repository_cfg
= 1;
141 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 0);
143 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
146 } else if (!strcmp(cmd
, "-c")) {
148 fprintf(stderr
, "-c expects a configuration string\n" );
149 usage(git_usage_string
);
151 git_config_push_parameter((*argv
)[1]);
154 } else if (!strcmp(cmd
, "--literal-pathspecs")) {
155 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "1", 1);
158 } else if (!strcmp(cmd
, "--no-literal-pathspecs")) {
159 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "0", 1);
162 } else if (!strcmp(cmd
, "--glob-pathspecs")) {
163 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
166 } else if (!strcmp(cmd
, "--noglob-pathspecs")) {
167 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
170 } else if (!strcmp(cmd
, "--icase-pathspecs")) {
171 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT
, "1", 1);
174 } else if (!strcmp(cmd
, "--shallow-file")) {
177 set_alternate_shallow_file((*argv
)[0], 1);
180 } else if (!strcmp(cmd
, "-C")) {
182 fprintf(stderr
, "No directory given for -C.\n" );
183 usage(git_usage_string
);
186 if (chdir((*argv
)[1]))
187 die_errno("Cannot change to '%s'", (*argv
)[1]);
193 } else if (!strcmp(cmd
, "--list-builtins")) {
197 fprintf(stderr
, "Unknown option: %s\n", cmd
);
198 usage(git_usage_string
);
204 return (*argv
) - orig_argv
;
207 static int handle_alias(int *argcp
, const char ***argv
)
209 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
210 int count
, option_count
;
211 const char **new_argv
;
212 const char *alias_command
;
215 alias_command
= (*argv
)[0];
216 alias_string
= alias_lookup(alias_command
);
218 if (alias_string
[0] == '!') {
219 struct child_process child
= CHILD_PROCESS_INIT
;
222 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
223 setup_git_directory_gently(&nongit_ok
);
225 commit_pager_choice();
228 argv_array_push(&child
.args
, alias_string
+ 1);
229 argv_array_pushv(&child
.args
, (*argv
) + 1);
231 ret
= run_command(&child
);
232 if (ret
>= 0) /* normal exit */
235 die_errno("While expanding alias '%s': '%s'",
236 alias_command
, alias_string
+ 1);
238 count
= split_cmdline(alias_string
, &new_argv
);
240 die("Bad alias.%s string: %s", alias_command
,
241 split_cmdline_strerror(count
));
242 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
244 die("alias '%s' changes environment variables\n"
245 "You can use '!git' in the alias to do this.",
247 memmove(new_argv
- option_count
, new_argv
,
248 count
* sizeof(char *));
249 new_argv
-= option_count
;
252 die("empty alias for %s", alias_command
);
254 if (!strcmp(alias_command
, new_argv
[0]))
255 die("recursive alias: %s", alias_command
);
257 trace_argv_printf(new_argv
,
258 "trace: alias expansion: %s =>",
261 REALLOC_ARRAY(new_argv
, count
+ *argcp
);
262 /* insert after command name */
263 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
276 #define RUN_SETUP (1<<0)
277 #define RUN_SETUP_GENTLY (1<<1)
278 #define USE_PAGER (1<<2)
280 * require working tree to be present -- anything uses this needs
281 * RUN_SETUP for reading from the configuration file.
283 #define NEED_WORK_TREE (1<<3)
284 #define SUPPORT_SUPER_PREFIX (1<<4)
288 int (*fn
)(int, const char **, const char *);
292 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
299 help
= argc
== 2 && !strcmp(argv
[1], "-h");
301 if (p
->option
& RUN_SETUP
)
302 prefix
= setup_git_directory();
303 else if (p
->option
& RUN_SETUP_GENTLY
) {
305 prefix
= setup_git_directory_gently(&nongit_ok
);
308 if (use_pager
== -1 && p
->option
& (RUN_SETUP
| RUN_SETUP_GENTLY
))
309 use_pager
= check_pager_config(p
->cmd
);
310 if (use_pager
== -1 && p
->option
& USE_PAGER
)
313 if ((p
->option
& (RUN_SETUP
| RUN_SETUP_GENTLY
)) &&
314 startup_info
->have_repository
) /* get_git_dir() may set up repo, avoid that */
315 trace_repo_setup(prefix
);
317 commit_pager_choice();
319 if (!help
&& get_super_prefix()) {
320 if (!(p
->option
& SUPPORT_SUPER_PREFIX
))
321 die("%s doesn't support --super-prefix", p
->cmd
);
324 if (!help
&& p
->option
& NEED_WORK_TREE
)
327 trace_argv_printf(argv
, "trace: built-in: git");
329 status
= p
->fn(argc
, argv
, prefix
);
333 /* Somebody closed stdout? */
334 if (fstat(fileno(stdout
), &st
))
336 /* Ignore write errors for pipes and sockets.. */
337 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
340 /* Check for ENOSPC and EIO errors.. */
342 die_errno("write failure on standard output");
344 die("unknown write failure on standard output");
346 die_errno("close failed on standard output");
350 static struct cmd_struct commands
[] = {
351 { "add", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
352 { "am", cmd_am
, RUN_SETUP
| NEED_WORK_TREE
},
353 { "annotate", cmd_annotate
, RUN_SETUP
},
354 { "apply", cmd_apply
, RUN_SETUP_GENTLY
},
355 { "archive", cmd_archive
, RUN_SETUP_GENTLY
},
356 { "bisect--helper", cmd_bisect__helper
, RUN_SETUP
},
357 { "blame", cmd_blame
, RUN_SETUP
},
358 { "branch", cmd_branch
, RUN_SETUP
},
359 { "bundle", cmd_bundle
, RUN_SETUP_GENTLY
},
360 { "cat-file", cmd_cat_file
, RUN_SETUP
},
361 { "check-attr", cmd_check_attr
, RUN_SETUP
},
362 { "check-ignore", cmd_check_ignore
, RUN_SETUP
| NEED_WORK_TREE
},
363 { "check-mailmap", cmd_check_mailmap
, RUN_SETUP
},
364 { "check-ref-format", cmd_check_ref_format
},
365 { "checkout", cmd_checkout
, RUN_SETUP
| NEED_WORK_TREE
},
366 { "checkout-index", cmd_checkout_index
,
367 RUN_SETUP
| NEED_WORK_TREE
},
368 { "cherry", cmd_cherry
, RUN_SETUP
},
369 { "cherry-pick", cmd_cherry_pick
, RUN_SETUP
| NEED_WORK_TREE
},
370 { "clean", cmd_clean
, RUN_SETUP
| NEED_WORK_TREE
},
371 { "clone", cmd_clone
},
372 { "column", cmd_column
, RUN_SETUP_GENTLY
},
373 { "commit", cmd_commit
, RUN_SETUP
| NEED_WORK_TREE
},
374 { "commit-tree", cmd_commit_tree
, RUN_SETUP
},
375 { "config", cmd_config
, RUN_SETUP_GENTLY
},
376 { "count-objects", cmd_count_objects
, RUN_SETUP
},
377 { "credential", cmd_credential
, RUN_SETUP_GENTLY
},
378 { "describe", cmd_describe
, RUN_SETUP
},
379 { "diff", cmd_diff
},
380 { "diff-files", cmd_diff_files
, RUN_SETUP
| NEED_WORK_TREE
},
381 { "diff-index", cmd_diff_index
, RUN_SETUP
},
382 { "diff-tree", cmd_diff_tree
, RUN_SETUP
},
383 { "difftool", cmd_difftool
, RUN_SETUP
| NEED_WORK_TREE
},
384 { "fast-export", cmd_fast_export
, RUN_SETUP
},
385 { "fetch", cmd_fetch
, RUN_SETUP
},
386 { "fetch-pack", cmd_fetch_pack
, RUN_SETUP
},
387 { "fmt-merge-msg", cmd_fmt_merge_msg
, RUN_SETUP
},
388 { "for-each-ref", cmd_for_each_ref
, RUN_SETUP
},
389 { "format-patch", cmd_format_patch
, RUN_SETUP
},
390 { "fsck", cmd_fsck
, RUN_SETUP
},
391 { "fsck-objects", cmd_fsck
, RUN_SETUP
},
392 { "gc", cmd_gc
, RUN_SETUP
},
393 { "get-tar-commit-id", cmd_get_tar_commit_id
},
394 { "grep", cmd_grep
, RUN_SETUP_GENTLY
| SUPPORT_SUPER_PREFIX
},
395 { "hash-object", cmd_hash_object
},
396 { "help", cmd_help
},
397 { "index-pack", cmd_index_pack
, RUN_SETUP_GENTLY
},
398 { "init", cmd_init_db
},
399 { "init-db", cmd_init_db
},
400 { "interpret-trailers", cmd_interpret_trailers
, RUN_SETUP_GENTLY
},
401 { "log", cmd_log
, RUN_SETUP
},
402 { "ls-files", cmd_ls_files
, RUN_SETUP
| SUPPORT_SUPER_PREFIX
},
403 { "ls-remote", cmd_ls_remote
, RUN_SETUP_GENTLY
},
404 { "ls-tree", cmd_ls_tree
, RUN_SETUP
},
405 { "mailinfo", cmd_mailinfo
, RUN_SETUP_GENTLY
},
406 { "mailsplit", cmd_mailsplit
},
407 { "merge", cmd_merge
, RUN_SETUP
| NEED_WORK_TREE
},
408 { "merge-base", cmd_merge_base
, RUN_SETUP
},
409 { "merge-file", cmd_merge_file
, RUN_SETUP_GENTLY
},
410 { "merge-index", cmd_merge_index
, RUN_SETUP
},
411 { "merge-ours", cmd_merge_ours
, RUN_SETUP
},
412 { "merge-recursive", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
413 { "merge-recursive-ours", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
414 { "merge-recursive-theirs", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
415 { "merge-subtree", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
416 { "merge-tree", cmd_merge_tree
, RUN_SETUP
},
417 { "mktag", cmd_mktag
, RUN_SETUP
},
418 { "mktree", cmd_mktree
, RUN_SETUP
},
419 { "mv", cmd_mv
, RUN_SETUP
| NEED_WORK_TREE
},
420 { "name-rev", cmd_name_rev
, RUN_SETUP
},
421 { "notes", cmd_notes
, RUN_SETUP
},
422 { "pack-objects", cmd_pack_objects
, RUN_SETUP
},
423 { "pack-redundant", cmd_pack_redundant
, RUN_SETUP
},
424 { "pack-refs", cmd_pack_refs
, RUN_SETUP
},
425 { "patch-id", cmd_patch_id
, RUN_SETUP_GENTLY
},
426 { "pickaxe", cmd_blame
, RUN_SETUP
},
427 { "prune", cmd_prune
, RUN_SETUP
},
428 { "prune-packed", cmd_prune_packed
, RUN_SETUP
},
429 { "pull", cmd_pull
, RUN_SETUP
| NEED_WORK_TREE
},
430 { "push", cmd_push
, RUN_SETUP
},
431 { "read-tree", cmd_read_tree
, RUN_SETUP
| SUPPORT_SUPER_PREFIX
},
432 { "rebase--helper", cmd_rebase__helper
, RUN_SETUP
| NEED_WORK_TREE
},
433 { "receive-pack", cmd_receive_pack
},
434 { "reflog", cmd_reflog
, RUN_SETUP
},
435 { "remote", cmd_remote
, RUN_SETUP
},
436 { "remote-ext", cmd_remote_ext
},
437 { "remote-fd", cmd_remote_fd
},
438 { "repack", cmd_repack
, RUN_SETUP
},
439 { "replace", cmd_replace
, RUN_SETUP
},
440 { "rerere", cmd_rerere
, RUN_SETUP
},
441 { "reset", cmd_reset
, RUN_SETUP
},
442 { "rev-list", cmd_rev_list
, RUN_SETUP
},
443 { "rev-parse", cmd_rev_parse
},
444 { "revert", cmd_revert
, RUN_SETUP
| NEED_WORK_TREE
},
445 { "rm", cmd_rm
, RUN_SETUP
},
446 { "send-pack", cmd_send_pack
, RUN_SETUP
},
447 { "shortlog", cmd_shortlog
, RUN_SETUP_GENTLY
| USE_PAGER
},
448 { "show", cmd_show
, RUN_SETUP
},
449 { "show-branch", cmd_show_branch
, RUN_SETUP
},
450 { "show-ref", cmd_show_ref
, RUN_SETUP
},
451 { "stage", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
452 { "status", cmd_status
, RUN_SETUP
| NEED_WORK_TREE
},
453 { "stripspace", cmd_stripspace
},
454 { "submodule--helper", cmd_submodule__helper
, RUN_SETUP
| SUPPORT_SUPER_PREFIX
},
455 { "symbolic-ref", cmd_symbolic_ref
, RUN_SETUP
},
456 { "tag", cmd_tag
, RUN_SETUP
},
457 { "unpack-file", cmd_unpack_file
, RUN_SETUP
},
458 { "unpack-objects", cmd_unpack_objects
, RUN_SETUP
},
459 { "update-index", cmd_update_index
, RUN_SETUP
},
460 { "update-ref", cmd_update_ref
, RUN_SETUP
},
461 { "update-server-info", cmd_update_server_info
, RUN_SETUP
},
462 { "upload-archive", cmd_upload_archive
},
463 { "upload-archive--writer", cmd_upload_archive_writer
},
464 { "var", cmd_var
, RUN_SETUP_GENTLY
},
465 { "verify-commit", cmd_verify_commit
, RUN_SETUP
},
466 { "verify-pack", cmd_verify_pack
},
467 { "verify-tag", cmd_verify_tag
, RUN_SETUP
},
468 { "version", cmd_version
},
469 { "whatchanged", cmd_whatchanged
, RUN_SETUP
},
470 { "worktree", cmd_worktree
, RUN_SETUP
},
471 { "write-tree", cmd_write_tree
, RUN_SETUP
},
474 static struct cmd_struct
*get_builtin(const char *s
)
477 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
478 struct cmd_struct
*p
= commands
+ i
;
479 if (!strcmp(s
, p
->cmd
))
485 int is_builtin(const char *s
)
487 return !!get_builtin(s
);
490 static void list_builtins(void)
493 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
494 printf("%s\n", commands
[i
].cmd
);
497 #ifdef STRIP_EXTENSION
498 static void strip_extension(const char **argv
)
502 if (strip_suffix(argv
[0], STRIP_EXTENSION
, &len
))
503 argv
[0] = xmemdupz(argv
[0], len
);
506 #define strip_extension(cmd)
509 static void handle_builtin(int argc
, const char **argv
)
511 struct argv_array args
= ARGV_ARRAY_INIT
;
513 struct cmd_struct
*builtin
;
515 strip_extension(argv
);
518 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
519 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
523 argv
[0] = cmd
= "help";
525 for (i
= 0; i
< argc
; i
++) {
526 argv_array_push(&args
, argv
[i
]);
528 argv_array_push(&args
, "--exclude-guides");
535 builtin
= get_builtin(cmd
);
537 exit(run_builtin(builtin
, argc
, argv
));
538 argv_array_clear(&args
);
541 static void execv_dashed_external(const char **argv
)
543 struct child_process cmd
= CHILD_PROCESS_INIT
;
546 if (get_super_prefix())
547 die("%s doesn't support --super-prefix", argv
[0]);
550 use_pager
= check_pager_config(argv
[0]);
551 commit_pager_choice();
553 argv_array_pushf(&cmd
.args
, "git-%s", argv
[0]);
554 argv_array_pushv(&cmd
.args
, argv
+ 1);
555 cmd
.clean_on_exit
= 1;
556 cmd
.wait_after_clean
= 1;
557 cmd
.silent_exec_failure
= 1;
559 trace_argv_printf(cmd
.args
.argv
, "trace: exec:");
562 * If we fail because the command is not found, it is
563 * OK to return. Otherwise, we just pass along the status code,
564 * or our usual generic code if we were not even able to exec
567 status
= run_command(&cmd
);
570 else if (errno
!= ENOENT
)
574 static int run_argv(int *argcp
, const char ***argv
)
580 * If we tried alias and futzed with our environment,
581 * it no longer is safe to invoke builtins directly in
582 * general. We have to spawn them as dashed externals.
584 * NEEDSWORK: if we can figure out cases
585 * where it is safe to do, we can avoid spawning a new
589 handle_builtin(*argcp
, *argv
);
591 /* .. then try the external ones */
592 execv_dashed_external(*argv
);
594 /* It could be an alias -- this works around the insanity
595 * of overriding "git log" with "git show" by having
600 if (!handle_alias(argcp
, argv
))
608 int cmd_main(int argc
, const char **argv
)
617 const char *slash
= find_last_dir_sep(cmd
);
622 trace_command_performance(argv
);
625 * "git-xxxx" is the same as "git xxxx", but we obviously:
627 * - cannot take flags in between the "git" and the "xxxx".
628 * - cannot execute it externally (since it would just do
629 * the same thing over again)
631 * So we just directly call the builtin handler, and die if
632 * that one cannot handle it.
634 if (skip_prefix(cmd
, "git-", &cmd
)) {
636 handle_builtin(argc
, argv
);
637 die("cannot handle %s as a builtin", cmd
);
640 /* Look for flags.. */
643 handle_options(&argv
, &argc
, NULL
);
645 /* translate --help and --version into commands */
646 skip_prefix(argv
[0], "--", &argv
[0]);
648 /* The user didn't specify a command; give them help */
649 commit_pager_choice();
650 printf("usage: %s\n\n", git_usage_string
);
651 list_common_cmds_help();
652 printf("\n%s\n", _(git_more_info_string
));
658 * We use PATH to find git commands, but we prepend some higher
659 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
660 * environment, and the $(gitexecdir) from the Makefile at build
666 int was_alias
= run_argv(&argc
, &argv
);
670 fprintf(stderr
, "Expansion of alias '%s' failed; "
671 "'%s' is not a git command\n",
676 cmd
= argv
[0] = help_unknown_cmd(cmd
);
682 fprintf(stderr
, "Failed to run command '%s': %s\n",
683 cmd
, strerror(errno
));