6 #include "run-command.h"
9 const char git_usage_string
[] =
10 "git [--version] [--help] [-C <path>] [-c name=value]\n"
11 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
12 " [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]\n"
13 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
14 " <command> [<args>]";
16 const char git_more_info_string
[] =
17 N_("'git help -a' and 'git help -g' lists available subcommands and some\n"
18 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
19 "to read about a specific subcommand or concept.");
21 static struct startup_info git_startup_info
;
22 static int use_pager
= -1;
24 static void commit_pager_choice(void) {
27 setenv("GIT_PAGER", "cat", 1);
37 static int handle_options(const char ***argv
, int *argc
, int *envchanged
)
39 const char **orig_argv
= *argv
;
42 const char *cmd
= (*argv
)[0];
47 * For legacy reasons, the "version" and "help"
48 * commands can be written with "--" prepended
49 * to make them look like flags.
51 if (!strcmp(cmd
, "--help") || !strcmp(cmd
, "--version"))
55 * Check remaining flags.
57 if (starts_with(cmd
, "--exec-path")) {
60 git_set_argv_exec_path(cmd
+ 1);
62 puts(git_exec_path());
65 } else if (!strcmp(cmd
, "--html-path")) {
66 puts(system_path(GIT_HTML_PATH
));
68 } else if (!strcmp(cmd
, "--man-path")) {
69 puts(system_path(GIT_MAN_PATH
));
71 } else if (!strcmp(cmd
, "--info-path")) {
72 puts(system_path(GIT_INFO_PATH
));
74 } else if (!strcmp(cmd
, "-p") || !strcmp(cmd
, "--paginate")) {
76 } else if (!strcmp(cmd
, "--no-pager")) {
80 } else if (!strcmp(cmd
, "--no-replace-objects")) {
81 check_replace_refs
= 0;
82 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT
, "1", 1);
85 } else if (!strcmp(cmd
, "--git-dir")) {
87 fprintf(stderr
, "No directory given for --git-dir.\n" );
88 usage(git_usage_string
);
90 setenv(GIT_DIR_ENVIRONMENT
, (*argv
)[1], 1);
95 } else if (starts_with(cmd
, "--git-dir=")) {
96 setenv(GIT_DIR_ENVIRONMENT
, cmd
+ 10, 1);
99 } else if (!strcmp(cmd
, "--namespace")) {
101 fprintf(stderr
, "No namespace given for --namespace.\n" );
102 usage(git_usage_string
);
104 setenv(GIT_NAMESPACE_ENVIRONMENT
, (*argv
)[1], 1);
109 } else if (starts_with(cmd
, "--namespace=")) {
110 setenv(GIT_NAMESPACE_ENVIRONMENT
, cmd
+ 12, 1);
113 } else if (!strcmp(cmd
, "--work-tree")) {
115 fprintf(stderr
, "No directory given for --work-tree.\n" );
116 usage(git_usage_string
);
118 setenv(GIT_WORK_TREE_ENVIRONMENT
, (*argv
)[1], 1);
123 } else if (starts_with(cmd
, "--work-tree=")) {
124 setenv(GIT_WORK_TREE_ENVIRONMENT
, cmd
+ 12, 1);
127 } else if (!strcmp(cmd
, "--bare")) {
128 char *cwd
= xgetcwd();
129 is_bare_repository_cfg
= 1;
130 setenv(GIT_DIR_ENVIRONMENT
, cwd
, 0);
132 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT
, "0", 1);
135 } else if (!strcmp(cmd
, "-c")) {
137 fprintf(stderr
, "-c expects a configuration string\n" );
138 usage(git_usage_string
);
140 git_config_push_parameter((*argv
)[1]);
143 } else if (!strcmp(cmd
, "--literal-pathspecs")) {
144 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "1", 1);
147 } else if (!strcmp(cmd
, "--no-literal-pathspecs")) {
148 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT
, "0", 1);
151 } else if (!strcmp(cmd
, "--glob-pathspecs")) {
152 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
155 } else if (!strcmp(cmd
, "--noglob-pathspecs")) {
156 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT
, "1", 1);
159 } else if (!strcmp(cmd
, "--icase-pathspecs")) {
160 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT
, "1", 1);
163 } else if (!strcmp(cmd
, "--shallow-file")) {
166 set_alternate_shallow_file((*argv
)[0], 1);
169 } else if (!strcmp(cmd
, "-C")) {
171 fprintf(stderr
, "No directory given for -C.\n" );
172 usage(git_usage_string
);
174 if (chdir((*argv
)[1]))
175 die_errno("Cannot change to '%s'", (*argv
)[1]);
181 fprintf(stderr
, "Unknown option: %s\n", cmd
);
182 usage(git_usage_string
);
188 return (*argv
) - orig_argv
;
191 static int handle_alias(int *argcp
, const char ***argv
)
193 int envchanged
= 0, ret
= 0, saved_errno
= errno
;
195 int count
, option_count
;
196 const char **new_argv
;
197 const char *alias_command
;
201 subdir
= setup_git_directory_gently(&unused_nongit
);
203 alias_command
= (*argv
)[0];
204 alias_string
= alias_lookup(alias_command
);
206 if (alias_string
[0] == '!') {
207 const char **alias_argv
;
208 int argc
= *argcp
, i
;
210 commit_pager_choice();
212 /* build alias_argv */
213 alias_argv
= xmalloc(sizeof(*alias_argv
) * (argc
+ 1));
214 alias_argv
[0] = alias_string
+ 1;
215 for (i
= 1; i
< argc
; ++i
)
216 alias_argv
[i
] = (*argv
)[i
];
217 alias_argv
[argc
] = NULL
;
219 ret
= run_command_v_opt(alias_argv
, RUN_USING_SHELL
);
220 if (ret
>= 0) /* normal exit */
223 die_errno("While expanding alias '%s': '%s'",
224 alias_command
, alias_string
+ 1);
226 count
= split_cmdline(alias_string
, &new_argv
);
228 die("Bad alias.%s string: %s", alias_command
,
229 split_cmdline_strerror(count
));
230 option_count
= handle_options(&new_argv
, &count
, &envchanged
);
232 die("alias '%s' changes environment variables\n"
233 "You can use '!git' in the alias to do this.",
235 memmove(new_argv
- option_count
, new_argv
,
236 count
* sizeof(char *));
237 new_argv
-= option_count
;
240 die("empty alias for %s", alias_command
);
242 if (!strcmp(alias_command
, new_argv
[0]))
243 die("recursive alias: %s", alias_command
);
245 trace_argv_printf(new_argv
,
246 "trace: alias expansion: %s =>",
249 new_argv
= xrealloc(new_argv
, sizeof(char *) *
251 /* insert after command name */
252 memcpy(new_argv
+ count
, *argv
+ 1, sizeof(char *) * *argcp
);
260 if (subdir
&& chdir(subdir
))
261 die_errno("Cannot change to '%s'", subdir
);
268 #define RUN_SETUP (1<<0)
269 #define RUN_SETUP_GENTLY (1<<1)
270 #define USE_PAGER (1<<2)
272 * require working tree to be present -- anything uses this needs
273 * RUN_SETUP for reading from the configuration file.
275 #define NEED_WORK_TREE (1<<3)
279 int (*fn
)(int, const char **, const char *);
283 static int run_builtin(struct cmd_struct
*p
, int argc
, const char **argv
)
290 help
= argc
== 2 && !strcmp(argv
[1], "-h");
292 if (p
->option
& RUN_SETUP
)
293 prefix
= setup_git_directory();
294 if (p
->option
& RUN_SETUP_GENTLY
) {
296 prefix
= setup_git_directory_gently(&nongit_ok
);
299 if (use_pager
== -1 && p
->option
& (RUN_SETUP
| RUN_SETUP_GENTLY
))
300 use_pager
= check_pager_config(p
->cmd
);
301 if (use_pager
== -1 && p
->option
& USE_PAGER
)
304 if ((p
->option
& (RUN_SETUP
| RUN_SETUP_GENTLY
)) &&
305 startup_info
->have_repository
) /* get_git_dir() may set up repo, avoid that */
306 trace_repo_setup(prefix
);
308 commit_pager_choice();
310 if (!help
&& p
->option
& NEED_WORK_TREE
)
313 trace_argv_printf(argv
, "trace: built-in: git");
315 status
= p
->fn(argc
, argv
, prefix
);
319 /* Somebody closed stdout? */
320 if (fstat(fileno(stdout
), &st
))
322 /* Ignore write errors for pipes and sockets.. */
323 if (S_ISFIFO(st
.st_mode
) || S_ISSOCK(st
.st_mode
))
326 /* Check for ENOSPC and EIO errors.. */
328 die_errno("write failure on standard output");
330 die("unknown write failure on standard output");
332 die_errno("close failed on standard output");
336 static struct cmd_struct commands
[] = {
337 { "add", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
338 { "annotate", cmd_annotate
, RUN_SETUP
},
339 { "apply", cmd_apply
, RUN_SETUP_GENTLY
},
340 { "archive", cmd_archive
},
341 { "bisect--helper", cmd_bisect__helper
, RUN_SETUP
},
342 { "blame", cmd_blame
, RUN_SETUP
},
343 { "branch", cmd_branch
, RUN_SETUP
},
344 { "bundle", cmd_bundle
, RUN_SETUP_GENTLY
},
345 { "cat-file", cmd_cat_file
, RUN_SETUP
},
346 { "check-attr", cmd_check_attr
, RUN_SETUP
},
347 { "check-ignore", cmd_check_ignore
, RUN_SETUP
| NEED_WORK_TREE
},
348 { "check-mailmap", cmd_check_mailmap
, RUN_SETUP
},
349 { "check-ref-format", cmd_check_ref_format
},
350 { "checkout", cmd_checkout
, RUN_SETUP
| NEED_WORK_TREE
},
351 { "checkout-index", cmd_checkout_index
,
352 RUN_SETUP
| NEED_WORK_TREE
},
353 { "cherry", cmd_cherry
, RUN_SETUP
},
354 { "cherry-pick", cmd_cherry_pick
, RUN_SETUP
| NEED_WORK_TREE
},
355 { "clean", cmd_clean
, RUN_SETUP
| NEED_WORK_TREE
},
356 { "clone", cmd_clone
},
357 { "column", cmd_column
, RUN_SETUP_GENTLY
},
358 { "commit", cmd_commit
, RUN_SETUP
| NEED_WORK_TREE
},
359 { "commit-tree", cmd_commit_tree
, RUN_SETUP
},
360 { "config", cmd_config
, RUN_SETUP_GENTLY
},
361 { "count-objects", cmd_count_objects
, RUN_SETUP
},
362 { "credential", cmd_credential
, RUN_SETUP_GENTLY
},
363 { "describe", cmd_describe
, RUN_SETUP
},
364 { "diff", cmd_diff
},
365 { "diff-files", cmd_diff_files
, RUN_SETUP
| NEED_WORK_TREE
},
366 { "diff-index", cmd_diff_index
, RUN_SETUP
},
367 { "diff-tree", cmd_diff_tree
, RUN_SETUP
},
368 { "fast-export", cmd_fast_export
, RUN_SETUP
},
369 { "fetch", cmd_fetch
, RUN_SETUP
},
370 { "fetch-pack", cmd_fetch_pack
, RUN_SETUP
},
371 { "fmt-merge-msg", cmd_fmt_merge_msg
, RUN_SETUP
},
372 { "for-each-ref", cmd_for_each_ref
, RUN_SETUP
},
373 { "format-patch", cmd_format_patch
, RUN_SETUP
},
374 { "fsck", cmd_fsck
, RUN_SETUP
},
375 { "fsck-objects", cmd_fsck
, RUN_SETUP
},
376 { "gc", cmd_gc
, RUN_SETUP
},
377 { "get-tar-commit-id", cmd_get_tar_commit_id
},
378 { "grep", cmd_grep
, RUN_SETUP_GENTLY
},
379 { "hash-object", cmd_hash_object
},
380 { "help", cmd_help
},
381 { "index-pack", cmd_index_pack
, RUN_SETUP_GENTLY
},
382 { "init", cmd_init_db
},
383 { "init-db", cmd_init_db
},
384 { "log", cmd_log
, RUN_SETUP
},
385 { "ls-files", cmd_ls_files
, RUN_SETUP
},
386 { "ls-remote", cmd_ls_remote
, RUN_SETUP_GENTLY
},
387 { "ls-tree", cmd_ls_tree
, RUN_SETUP
},
388 { "mailinfo", cmd_mailinfo
},
389 { "mailsplit", cmd_mailsplit
},
390 { "merge", cmd_merge
, RUN_SETUP
| NEED_WORK_TREE
},
391 { "merge-base", cmd_merge_base
, RUN_SETUP
},
392 { "merge-file", cmd_merge_file
, RUN_SETUP_GENTLY
},
393 { "merge-index", cmd_merge_index
, RUN_SETUP
},
394 { "merge-ours", cmd_merge_ours
, RUN_SETUP
},
395 { "merge-recursive", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
396 { "merge-recursive-ours", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
397 { "merge-recursive-theirs", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
398 { "merge-subtree", cmd_merge_recursive
, RUN_SETUP
| NEED_WORK_TREE
},
399 { "merge-tree", cmd_merge_tree
, RUN_SETUP
},
400 { "mktag", cmd_mktag
, RUN_SETUP
},
401 { "mktree", cmd_mktree
, RUN_SETUP
},
402 { "mv", cmd_mv
, RUN_SETUP
| NEED_WORK_TREE
},
403 { "name-rev", cmd_name_rev
, RUN_SETUP
},
404 { "notes", cmd_notes
, RUN_SETUP
},
405 { "pack-objects", cmd_pack_objects
, RUN_SETUP
},
406 { "pack-redundant", cmd_pack_redundant
, RUN_SETUP
},
407 { "pack-refs", cmd_pack_refs
, RUN_SETUP
},
408 { "patch-id", cmd_patch_id
},
409 { "pickaxe", cmd_blame
, RUN_SETUP
},
410 { "prune", cmd_prune
, RUN_SETUP
},
411 { "prune-packed", cmd_prune_packed
, RUN_SETUP
},
412 { "push", cmd_push
, RUN_SETUP
},
413 { "read-tree", cmd_read_tree
, RUN_SETUP
},
414 { "receive-pack", cmd_receive_pack
},
415 { "reflog", cmd_reflog
, RUN_SETUP
},
416 { "remote", cmd_remote
, RUN_SETUP
},
417 { "remote-ext", cmd_remote_ext
},
418 { "remote-fd", cmd_remote_fd
},
419 { "repack", cmd_repack
, RUN_SETUP
},
420 { "replace", cmd_replace
, RUN_SETUP
},
421 { "rerere", cmd_rerere
, RUN_SETUP
},
422 { "reset", cmd_reset
, RUN_SETUP
},
423 { "rev-list", cmd_rev_list
, RUN_SETUP
},
424 { "rev-parse", cmd_rev_parse
},
425 { "revert", cmd_revert
, RUN_SETUP
| NEED_WORK_TREE
},
426 { "rm", cmd_rm
, RUN_SETUP
},
427 { "send-pack", cmd_send_pack
, RUN_SETUP
},
428 { "shortlog", cmd_shortlog
, RUN_SETUP_GENTLY
| USE_PAGER
},
429 { "show", cmd_show
, RUN_SETUP
},
430 { "show-branch", cmd_show_branch
, RUN_SETUP
},
431 { "show-ref", cmd_show_ref
, RUN_SETUP
},
432 { "stage", cmd_add
, RUN_SETUP
| NEED_WORK_TREE
},
433 { "status", cmd_status
, RUN_SETUP
| NEED_WORK_TREE
},
434 { "stripspace", cmd_stripspace
},
435 { "symbolic-ref", cmd_symbolic_ref
, RUN_SETUP
},
436 { "tag", cmd_tag
, RUN_SETUP
},
437 { "unpack-file", cmd_unpack_file
, RUN_SETUP
},
438 { "unpack-objects", cmd_unpack_objects
, RUN_SETUP
},
439 { "update-index", cmd_update_index
, RUN_SETUP
},
440 { "update-ref", cmd_update_ref
, RUN_SETUP
},
441 { "update-server-info", cmd_update_server_info
, RUN_SETUP
},
442 { "upload-archive", cmd_upload_archive
},
443 { "upload-archive--writer", cmd_upload_archive_writer
},
444 { "var", cmd_var
, RUN_SETUP_GENTLY
},
445 { "verify-pack", cmd_verify_pack
},
446 { "verify-tag", cmd_verify_tag
, RUN_SETUP
},
447 { "version", cmd_version
},
448 { "whatchanged", cmd_whatchanged
, RUN_SETUP
},
449 { "write-tree", cmd_write_tree
, RUN_SETUP
},
452 int is_builtin(const char *s
)
455 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
456 struct cmd_struct
*p
= commands
+i
;
457 if (!strcmp(s
, p
->cmd
))
463 static void handle_builtin(int argc
, const char **argv
)
465 const char *cmd
= argv
[0];
467 static const char ext
[] = STRIP_EXTENSION
;
469 if (sizeof(ext
) > 1) {
470 i
= strlen(argv
[0]) - strlen(ext
);
471 if (i
> 0 && !strcmp(argv
[0] + i
, ext
)) {
472 char *argv0
= xstrdup(argv
[0]);
473 argv
[0] = cmd
= argv0
;
478 /* Turn "git cmd --help" into "git help cmd" */
479 if (argc
> 1 && !strcmp(argv
[1], "--help")) {
481 argv
[0] = cmd
= "help";
484 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++) {
485 struct cmd_struct
*p
= commands
+i
;
486 if (strcmp(p
->cmd
, cmd
))
488 exit(run_builtin(p
, argc
, argv
));
492 static void execv_dashed_external(const char **argv
)
494 struct strbuf cmd
= STRBUF_INIT
;
499 use_pager
= check_pager_config(argv
[0]);
500 commit_pager_choice();
502 strbuf_addf(&cmd
, "git-%s", argv
[0]);
505 * argv[0] must be the git command, but the argv array
506 * belongs to the caller, and may be reused in
507 * subsequent loop iterations. Save argv[0] and
508 * restore it on error.
513 trace_argv_printf(argv
, "trace: exec:");
516 * if we fail because the command is not found, it is
517 * OK to return. Otherwise, we just pass along the status code.
519 status
= run_command_v_opt(argv
, RUN_SILENT_EXEC_FAILURE
| RUN_CLEAN_ON_EXIT
);
520 if (status
>= 0 || errno
!= ENOENT
)
525 strbuf_release(&cmd
);
528 static int run_argv(int *argcp
, const char ***argv
)
533 /* See if it's a builtin */
534 handle_builtin(*argcp
, *argv
);
536 /* .. then try the external ones */
537 execv_dashed_external(*argv
);
539 /* It could be an alias -- this works around the insanity
540 * of overriding "git log" with "git show" by having
543 if (done_alias
|| !handle_alias(argcp
, argv
))
552 int main(int argc
, char **av
)
554 const char **argv
= (const char **) av
;
557 startup_info
= &git_startup_info
;
559 cmd
= git_extract_argv0_path(argv
[0]);
564 * Always open file descriptors 0/1/2 to avoid clobbering files
565 * in die(). It also avoids messing up when the pipes are dup'ed
566 * onto stdin/stdout/stderr in the child processes we spawn.
573 * "git-xxxx" is the same as "git xxxx", but we obviously:
575 * - cannot take flags in between the "git" and the "xxxx".
576 * - cannot execute it externally (since it would just do
577 * the same thing over again)
579 * So we just directly call the builtin handler, and die if
580 * that one cannot handle it.
582 if (starts_with(cmd
, "git-")) {
585 handle_builtin(argc
, argv
);
586 die("cannot handle %s as a builtin", cmd
);
589 /* Look for flags.. */
592 handle_options(&argv
, &argc
, NULL
);
594 if (starts_with(argv
[0], "--"))
597 /* The user didn't specify a command; give them help */
598 commit_pager_choice();
599 printf("usage: %s\n\n", git_usage_string
);
600 list_common_cmds_help();
601 printf("\n%s\n", _(git_more_info_string
));
607 * We use PATH to find git commands, but we prepend some higher
608 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
609 * environment, and the $(gitexecdir) from the Makefile at build
615 static int done_help
= 0;
616 static int was_alias
= 0;
617 was_alias
= run_argv(&argc
, &argv
);
621 fprintf(stderr
, "Expansion of alias '%s' failed; "
622 "'%s' is not a git command\n",
627 cmd
= argv
[0] = help_unknown_cmd(cmd
);
633 fprintf(stderr
, "Failed to run command '%s': %s\n",
634 cmd
, strerror(errno
));