Move declaration for alias.c to alias.h
[git/git.git] / git.c
CommitLineData
85023577 1#include "builtin.h"
b2141fc1 2#include "config.h"
fd5c363d
TF
3#include "exec_cmd.h"
4#include "help.h"
d8e96fd8 5#include "run-command.h"
65b5f948 6#include "alias.h"
8e49d503 7
007aa8d8
NTND
8#define RUN_SETUP (1<<0)
9#define RUN_SETUP_GENTLY (1<<1)
10#define USE_PAGER (1<<2)
11/*
12 * require working tree to be present -- anything uses this needs
13 * RUN_SETUP for reading from the configuration file.
14 */
15#define NEED_WORK_TREE (1<<3)
16#define SUPPORT_SUPER_PREFIX (1<<4)
17#define DELAY_PAGER_CONFIG (1<<5)
e9547947 18#define NO_PARSEOPT (1<<6) /* parse-options is not used */
007aa8d8
NTND
19
20struct cmd_struct {
21 const char *cmd;
22 int (*fn)(int, const char **, const char *);
e9547947 23 unsigned int option;
007aa8d8
NTND
24};
25
822a7d50 26const char git_usage_string[] =
fc045fe7
AS
27 N_("git [--version] [--help] [-C <path>] [-c <name>=<value>]\n"
28 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
29 " [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]\n"
30 " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
31 " <command> [<args>]");
822a7d50 32
b7d96819 33const char git_more_info_string[] =
ad5fe377 34 N_("'git help -a' and 'git help -g' list available subcommands and some\n"
73903d0b
PO
35 "concept guides. See 'git help <command>' or 'git help <concept>'\n"
36 "to read about a specific subcommand or concept.");
b7d96819 37
4e10738a 38static int use_pager = -1;
c0562611 39
e5d7a619 40static void list_builtins(struct string_list *list, unsigned int exclude_option);
8893fd95 41
0089521c
NTND
42static int match_token(const char *spec, int len, const char *token)
43{
44 int token_len = strlen(token);
45
46 return len == token_len && !strncmp(spec, token, token_len);
47}
48
49static int list_cmds(const char *spec)
50{
e5d7a619
NTND
51 struct string_list list = STRING_LIST_INIT_DUP;
52 int i;
53
0089521c
NTND
54 while (*spec) {
55 const char *sep = strchrnul(spec, ',');
56 int len = sep - spec;
57
58 if (match_token(spec, len, "builtins"))
e5d7a619 59 list_builtins(&list, 0);
6bb2dc0b
NTND
60 else if (match_token(spec, len, "main"))
61 list_all_main_cmds(&list);
62 else if (match_token(spec, len, "others"))
63 list_all_other_cmds(&list);
3c777767
NTND
64 else if (len > 5 && !strncmp(spec, "list-", 5)) {
65 struct strbuf sb = STRBUF_INIT;
66
67 strbuf_add(&sb, spec + 5, len - 5);
68 list_cmds_by_category(&list, sb.buf);
69 strbuf_release(&sb);
70 }
0089521c
NTND
71 else
72 die(_("unsupported command listing type '%s'"), spec);
73 spec += len;
74 if (*spec == ',')
75 spec++;
76 }
e5d7a619
NTND
77 for (i = 0; i < list.nr; i++)
78 puts(list.items[i].string);
79 string_list_clear(&list, 0);
0089521c
NTND
80 return 0;
81}
82
4e10738a
JK
83static void commit_pager_choice(void) {
84 switch (use_pager) {
85 case 0:
86 setenv("GIT_PAGER", "cat", 1);
87 break;
88 case 1:
89 setup_pager();
90 break;
91 default:
92 break;
93 }
94}
95
033fe3d9
96void setup_auto_pager(const char *cmd, int def)
97{
98 if (use_pager != -1 || pager_in_use())
99 return;
100 use_pager = check_pager_config(cmd);
101 if (use_pager == -1)
102 use_pager = def;
103 commit_pager_choice();
104}
105
4b25d091 106static int handle_options(const char ***argv, int *argc, int *envchanged)
4ab243a9 107{
73546c08 108 const char **orig_argv = *argv;
4ab243a9
JS
109
110 while (*argc > 0) {
111 const char *cmd = (*argv)[0];
112 if (cmd[0] != '-')
113 break;
114
6acbcb92
JS
115 /*
116 * For legacy reasons, the "version" and "help"
117 * commands can be written with "--" prepended
118 * to make them look like flags.
119 */
120 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
121 break;
122
123 /*
124 * Check remaining flags.
125 */
ae021d87 126 if (skip_prefix(cmd, "--exec-path", &cmd)) {
6acbcb92 127 if (*cmd == '=')
384df833 128 git_set_argv_exec_path(cmd + 1);
6acbcb92
JS
129 else {
130 puts(git_exec_path());
131 exit(0);
132 }
89a56bfb
MH
133 } else if (!strcmp(cmd, "--html-path")) {
134 puts(system_path(GIT_HTML_PATH));
135 exit(0);
f2dd8c37
JS
136 } else if (!strcmp(cmd, "--man-path")) {
137 puts(system_path(GIT_MAN_PATH));
138 exit(0);
139 } else if (!strcmp(cmd, "--info-path")) {
140 puts(system_path(GIT_INFO_PATH));
141 exit(0);
6acbcb92 142 } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
4e10738a 143 use_pager = 1;
463a849d 144 } else if (!strcmp(cmd, "--no-pager")) {
4e10738a 145 use_pager = 0;
463a849d
MM
146 if (envchanged)
147 *envchanged = 1;
b0fa7ab5 148 } else if (!strcmp(cmd, "--no-replace-objects")) {
afc711b8 149 check_replace_refs = 0;
6476b38b
CC
150 setenv(NO_REPLACE_OBJECTS_ENVIRONMENT, "1", 1);
151 if (envchanged)
152 *envchanged = 1;
6acbcb92 153 } else if (!strcmp(cmd, "--git-dir")) {
c321f00d 154 if (*argc < 2) {
fc045fe7 155 fprintf(stderr, _("no directory given for --git-dir\n" ));
c321f00d
BG
156 usage(git_usage_string);
157 }
45b09794 158 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
4394efec
ML
159 if (envchanged)
160 *envchanged = 1;
6acbcb92
JS
161 (*argv)++;
162 (*argc)--;
ae021d87
JK
163 } else if (skip_prefix(cmd, "--git-dir=", &cmd)) {
164 setenv(GIT_DIR_ENVIRONMENT, cmd, 1);
4394efec
ML
165 if (envchanged)
166 *envchanged = 1;
a1bea2c1
JT
167 } else if (!strcmp(cmd, "--namespace")) {
168 if (*argc < 2) {
fc045fe7 169 fprintf(stderr, _("no namespace given for --namespace\n" ));
a1bea2c1
JT
170 usage(git_usage_string);
171 }
172 setenv(GIT_NAMESPACE_ENVIRONMENT, (*argv)[1], 1);
173 if (envchanged)
174 *envchanged = 1;
175 (*argv)++;
176 (*argc)--;
ae021d87
JK
177 } else if (skip_prefix(cmd, "--namespace=", &cmd)) {
178 setenv(GIT_NAMESPACE_ENVIRONMENT, cmd, 1);
a1bea2c1
JT
179 if (envchanged)
180 *envchanged = 1;
892c41b9
ML
181 } else if (!strcmp(cmd, "--work-tree")) {
182 if (*argc < 2) {
fc045fe7 183 fprintf(stderr, _("no directory given for --work-tree\n" ));
892c41b9
ML
184 usage(git_usage_string);
185 }
186 setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
4394efec
ML
187 if (envchanged)
188 *envchanged = 1;
892c41b9
ML
189 (*argv)++;
190 (*argc)--;
ae021d87
JK
191 } else if (skip_prefix(cmd, "--work-tree=", &cmd)) {
192 setenv(GIT_WORK_TREE_ENVIRONMENT, cmd, 1);
4394efec
ML
193 if (envchanged)
194 *envchanged = 1;
74866d75
BW
195 } else if (!strcmp(cmd, "--super-prefix")) {
196 if (*argc < 2) {
fc045fe7 197 fprintf(stderr, _("no prefix given for --super-prefix\n" ));
74866d75
BW
198 usage(git_usage_string);
199 }
200 setenv(GIT_SUPER_PREFIX_ENVIRONMENT, (*argv)[1], 1);
201 if (envchanged)
202 *envchanged = 1;
203 (*argv)++;
204 (*argc)--;
205 } else if (skip_prefix(cmd, "--super-prefix=", &cmd)) {
206 setenv(GIT_SUPER_PREFIX_ENVIRONMENT, cmd, 1);
207 if (envchanged)
208 *envchanged = 1;
6acbcb92 209 } else if (!strcmp(cmd, "--bare")) {
4d3ab44d 210 char *cwd = xgetcwd();
6adcca3f 211 is_bare_repository_cfg = 1;
4d3ab44d
RS
212 setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
213 free(cwd);
2cd83d10 214 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
4394efec
ML
215 if (envchanged)
216 *envchanged = 1;
8b1fa778
AR
217 } else if (!strcmp(cmd, "-c")) {
218 if (*argc < 2) {
fc045fe7 219 fprintf(stderr, _("-c expects a configuration string\n" ));
8b1fa778
AR
220 usage(git_usage_string);
221 }
2b64fc89 222 git_config_push_parameter((*argv)[1]);
8b1fa778
AR
223 (*argv)++;
224 (*argc)--;
823ab40f
JK
225 } else if (!strcmp(cmd, "--literal-pathspecs")) {
226 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "1", 1);
227 if (envchanged)
228 *envchanged = 1;
229 } else if (!strcmp(cmd, "--no-literal-pathspecs")) {
230 setenv(GIT_LITERAL_PATHSPECS_ENVIRONMENT, "0", 1);
231 if (envchanged)
232 *envchanged = 1;
bd30c2e4
NTND
233 } else if (!strcmp(cmd, "--glob-pathspecs")) {
234 setenv(GIT_GLOB_PATHSPECS_ENVIRONMENT, "1", 1);
235 if (envchanged)
236 *envchanged = 1;
237 } else if (!strcmp(cmd, "--noglob-pathspecs")) {
238 setenv(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, "1", 1);
239 if (envchanged)
240 *envchanged = 1;
93d93537
NTND
241 } else if (!strcmp(cmd, "--icase-pathspecs")) {
242 setenv(GIT_ICASE_PATHSPECS_ENVIRONMENT, "1", 1);
243 if (envchanged)
244 *envchanged = 1;
27344d6a
JK
245 } else if (!strcmp(cmd, "--no-optional-locks")) {
246 setenv(GIT_OPTIONAL_LOCKS_ENVIRONMENT, "0", 1);
247 if (envchanged)
248 *envchanged = 1;
6035d6aa
NTND
249 } else if (!strcmp(cmd, "--shallow-file")) {
250 (*argv)++;
251 (*argc)--;
069c0532 252 set_alternate_shallow_file((*argv)[0], 1);
6035d6aa
NTND
253 if (envchanged)
254 *envchanged = 1;
44e1e4d6
NR
255 } else if (!strcmp(cmd, "-C")) {
256 if (*argc < 2) {
fc045fe7 257 fprintf(stderr, _("no directory given for -C\n" ));
44e1e4d6
NR
258 usage(git_usage_string);
259 }
6a536e20
KN
260 if ((*argv)[1][0]) {
261 if (chdir((*argv)[1]))
fc045fe7 262 die_errno("cannot change to '%s'", (*argv)[1]);
6a536e20
KN
263 if (envchanged)
264 *envchanged = 1;
265 }
44e1e4d6
NR
266 (*argv)++;
267 (*argc)--;
0089521c
NTND
268 } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
269 if (!strcmp(cmd, "parseopt")) {
e5d7a619
NTND
270 struct string_list list = STRING_LIST_INIT_DUP;
271 int i;
272
273 list_builtins(&list, NO_PARSEOPT);
274 for (i = 0; i < list.nr; i++)
275 printf("%s ", list.items[i].string);
276 string_list_clear(&list, 0);
0089521c
NTND
277 exit(0);
278 } else {
279 exit(list_cmds(cmd));
280 }
6acbcb92 281 } else {
fc045fe7 282 fprintf(stderr, _("unknown option: %s\n"), cmd);
822a7d50 283 usage(git_usage_string);
6acbcb92 284 }
4ab243a9
JS
285
286 (*argv)++;
287 (*argc)--;
4ab243a9 288 }
73546c08 289 return (*argv) - orig_argv;
4ab243a9
JS
290}
291
2b11e317
JS
292static int handle_alias(int *argcp, const char ***argv)
293{
af05d679 294 int envchanged = 0, ret = 0, saved_errno = errno;
0347a8c7 295 int count, option_count;
4b25d091 296 const char **new_argv;
94351118
JK
297 const char *alias_command;
298 char *alias_string;
0347a8c7
ML
299
300 alias_command = (*argv)[0];
94351118 301 alias_string = alias_lookup(alias_command);
0347a8c7 302 if (alias_string) {
dfd42a3c 303 if (alias_string[0] == '!') {
850d2fec 304 struct child_process child = CHILD_PROCESS_INIT;
a9bcf658
JS
305 int nongit_ok;
306
307 /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */
308 setup_git_directory_gently(&nongit_ok);
7f51f8bc 309
030149a4 310 commit_pager_choice();
7f51f8bc 311
850d2fec
JK
312 child.use_shell = 1;
313 argv_array_push(&child.args, alias_string + 1);
314 argv_array_pushv(&child.args, (*argv) + 1);
7f51f8bc 315
850d2fec 316 ret = run_command(&child);
7f51f8bc
EFL
317 if (ret >= 0) /* normal exit */
318 exit(ret);
319
fc045fe7 320 die_errno("while expanding alias '%s': '%s'",
7f51f8bc 321 alias_command, alias_string + 1);
dfd42a3c 322 }
0347a8c7 323 count = split_cmdline(alias_string, &new_argv);
dc4179f9 324 if (count < 0)
ad9ac6db
GB
325 die("Bad alias.%s string: %s", alias_command,
326 split_cmdline_strerror(count));
4394efec
ML
327 option_count = handle_options(&new_argv, &count, &envchanged);
328 if (envchanged)
fc045fe7
AS
329 die("alias '%s' changes environment variables.\n"
330 "You can use '!git' in the alias to do this",
4394efec 331 alias_command);
0347a8c7
ML
332 memmove(new_argv - option_count, new_argv,
333 count * sizeof(char *));
334 new_argv -= option_count;
335
336 if (count < 1)
337 die("empty alias for %s", alias_command);
338
339 if (!strcmp(alias_command, new_argv[0]))
340 die("recursive alias: %s", alias_command);
341
b319ce4c 342 trace_argv_printf(new_argv,
6ce4e61f
CC
343 "trace: alias expansion: %s =>",
344 alias_command);
575ba9d6 345
2756ca43 346 REALLOC_ARRAY(new_argv, count + *argcp);
0347a8c7 347 /* insert after command name */
4b25d091 348 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
2b11e317 349
0347a8c7
ML
350 *argv = new_argv;
351 *argcp += count - 1;
2b11e317 352
0347a8c7 353 ret = 1;
2b11e317
JS
354 }
355
47e5c0ca
JS
356 errno = saved_errno;
357
2b11e317
JS
358 return ret;
359}
360
f172f334 361static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
47d0b4ff 362{
99caeed0 363 int status, help;
0f157315 364 struct stat st;
47d0b4ff
LT
365 const char *prefix;
366
367 prefix = NULL;
99caeed0
JN
368 help = argc == 2 && !strcmp(argv[1], "-h");
369 if (!help) {
370 if (p->option & RUN_SETUP)
371 prefix = setup_git_directory();
27bd38d4 372 else if (p->option & RUN_SETUP_GENTLY) {
ee38dfb8
NTND
373 int nongit_ok;
374 prefix = setup_git_directory_gently(&nongit_ok);
375 }
99caeed0 376
c409824c
377 if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY) &&
378 !(p->option & DELAY_PAGER_CONFIG))
99caeed0
JN
379 use_pager = check_pager_config(p->cmd);
380 if (use_pager == -1 && p->option & USE_PAGER)
381 use_pager = 1;
a9ca8a85
NTND
382
383 if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
384 startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
385 trace_repo_setup(prefix);
99caeed0 386 }
4e10738a
JK
387 commit_pager_choice();
388
74866d75
BW
389 if (!help && get_super_prefix()) {
390 if (!(p->option & SUPPORT_SUPER_PREFIX))
391 die("%s doesn't support --super-prefix", p->cmd);
74866d75
BW
392 }
393
99caeed0 394 if (!help && p->option & NEED_WORK_TREE)
59f0f2f3
MH
395 setup_work_tree();
396
b319ce4c 397 trace_argv_printf(argv, "trace: built-in: git");
47d0b4ff 398
0f157315
LT
399 status = p->fn(argc, argv, prefix);
400 if (status)
47e3de0e 401 return status;
0f157315
LT
402
403 /* Somebody closed stdout? */
404 if (fstat(fileno(stdout), &st))
405 return 0;
406 /* Ignore write errors for pipes and sockets.. */
407 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
408 return 0;
409
410 /* Check for ENOSPC and EIO errors.. */
0227f988 411 if (fflush(stdout))
d824cbba 412 die_errno("write failure on standard output");
0227f988
LT
413 if (ferror(stdout))
414 die("unknown write failure on standard output");
415 if (fclose(stdout))
d824cbba 416 die_errno("close failed on standard output");
0f157315 417 return 0;
47d0b4ff
LT
418}
419
c6127fa3
SS
420static struct cmd_struct commands[] = {
421 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
783d7e86 422 { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE },
e9547947 423 { "annotate", cmd_annotate, RUN_SETUP | NO_PARSEOPT },
c6127fa3 424 { "apply", cmd_apply, RUN_SETUP_GENTLY },
eb0224c6 425 { "archive", cmd_archive, RUN_SETUP_GENTLY },
c6127fa3
SS
426 { "bisect--helper", cmd_bisect__helper, RUN_SETUP },
427 { "blame", cmd_blame, RUN_SETUP },
d74b541e 428 { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG },
e9547947 429 { "bundle", cmd_bundle, RUN_SETUP_GENTLY | NO_PARSEOPT },
c6127fa3
SS
430 { "cat-file", cmd_cat_file, RUN_SETUP },
431 { "check-attr", cmd_check_attr, RUN_SETUP },
432 { "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
433 { "check-mailmap", cmd_check_mailmap, RUN_SETUP },
e9547947 434 { "check-ref-format", cmd_check_ref_format, NO_PARSEOPT },
0ca560cb 435 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
c6127fa3
SS
436 { "checkout-index", cmd_checkout_index,
437 RUN_SETUP | NEED_WORK_TREE},
438 { "cherry", cmd_cherry, RUN_SETUP },
439 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
440 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
86d26f24 441 { "clone", cmd_clone },
c6127fa3
SS
442 { "column", cmd_column, RUN_SETUP_GENTLY },
443 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
e9547947 444 { "commit-tree", cmd_commit_tree, RUN_SETUP | NO_PARSEOPT },
32888b8f 445 { "config", cmd_config, RUN_SETUP_GENTLY | DELAY_PAGER_CONFIG },
c6127fa3 446 { "count-objects", cmd_count_objects, RUN_SETUP },
e9547947 447 { "credential", cmd_credential, RUN_SETUP_GENTLY | NO_PARSEOPT },
c6127fa3 448 { "describe", cmd_describe, RUN_SETUP },
e9547947
NTND
449 { "diff", cmd_diff, NO_PARSEOPT },
450 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
451 { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT },
452 { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT },
019678d6 453 { "difftool", cmd_difftool, RUN_SETUP | NEED_WORK_TREE },
c6127fa3
SS
454 { "fast-export", cmd_fast_export, RUN_SETUP },
455 { "fetch", cmd_fetch, RUN_SETUP },
e9547947 456 { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT },
c6127fa3
SS
457 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
458 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
459 { "format-patch", cmd_format_patch, RUN_SETUP },
460 { "fsck", cmd_fsck, RUN_SETUP },
461 { "fsck-objects", cmd_fsck, RUN_SETUP },
462 { "gc", cmd_gc, RUN_SETUP },
e9547947 463 { "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT },
f9ee2fcd 464 { "grep", cmd_grep, RUN_SETUP_GENTLY },
c6127fa3
SS
465 { "hash-object", cmd_hash_object },
466 { "help", cmd_help },
e9547947 467 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY | NO_PARSEOPT },
86d26f24
NTND
468 { "init", cmd_init_db },
469 { "init-db", cmd_init_db },
cbd9fc23 470 { "interpret-trailers", cmd_interpret_trailers, RUN_SETUP_GENTLY },
c6127fa3 471 { "log", cmd_log, RUN_SETUP },
188dce13 472 { "ls-files", cmd_ls_files, RUN_SETUP },
c6127fa3
SS
473 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
474 { "ls-tree", cmd_ls_tree, RUN_SETUP },
e9547947
NTND
475 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY | NO_PARSEOPT },
476 { "mailsplit", cmd_mailsplit, NO_PARSEOPT },
c6127fa3
SS
477 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
478 { "merge-base", cmd_merge_base, RUN_SETUP },
479 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
e9547947
NTND
480 { "merge-index", cmd_merge_index, RUN_SETUP | NO_PARSEOPT },
481 { "merge-ours", cmd_merge_ours, RUN_SETUP | NO_PARSEOPT },
482 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
483 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
484 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
485 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT },
486 { "merge-tree", cmd_merge_tree, RUN_SETUP | NO_PARSEOPT },
487 { "mktag", cmd_mktag, RUN_SETUP | NO_PARSEOPT },
c6127fa3
SS
488 { "mktree", cmd_mktree, RUN_SETUP },
489 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
490 { "name-rev", cmd_name_rev, RUN_SETUP },
491 { "notes", cmd_notes, RUN_SETUP },
492 { "pack-objects", cmd_pack_objects, RUN_SETUP },
e9547947 493 { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT },
c6127fa3 494 { "pack-refs", cmd_pack_refs, RUN_SETUP },
e9547947 495 { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT },
c6127fa3
SS
496 { "pickaxe", cmd_blame, RUN_SETUP },
497 { "prune", cmd_prune, RUN_SETUP },
498 { "prune-packed", cmd_prune_packed, RUN_SETUP },
1e1ea69f 499 { "pull", cmd_pull, RUN_SETUP | NEED_WORK_TREE },
c6127fa3 500 { "push", cmd_push, RUN_SETUP },
3d415425 501 { "read-tree", cmd_read_tree, RUN_SETUP | SUPPORT_SUPER_PREFIX},
4557f1ad 502 { "rebase--helper", cmd_rebase__helper, RUN_SETUP | NEED_WORK_TREE },
c6127fa3
SS
503 { "receive-pack", cmd_receive_pack },
504 { "reflog", cmd_reflog, RUN_SETUP },
505 { "remote", cmd_remote, RUN_SETUP },
e9547947
NTND
506 { "remote-ext", cmd_remote_ext, NO_PARSEOPT },
507 { "remote-fd", cmd_remote_fd, NO_PARSEOPT },
c6127fa3
SS
508 { "repack", cmd_repack, RUN_SETUP },
509 { "replace", cmd_replace, RUN_SETUP },
510 { "rerere", cmd_rerere, RUN_SETUP },
511 { "reset", cmd_reset, RUN_SETUP },
e9547947
NTND
512 { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT },
513 { "rev-parse", cmd_rev_parse, NO_PARSEOPT },
c6127fa3
SS
514 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
515 { "rm", cmd_rm, RUN_SETUP },
516 { "send-pack", cmd_send_pack, RUN_SETUP },
517 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
518 { "show", cmd_show, RUN_SETUP },
519 { "show-branch", cmd_show_branch, RUN_SETUP },
520 { "show-ref", cmd_show_ref, RUN_SETUP },
521 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
522 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
523 { "stripspace", cmd_stripspace },
e9547947 524 { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT },
c6127fa3 525 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
de121ffe 526 { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG },
e9547947
NTND
527 { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT },
528 { "unpack-objects", cmd_unpack_objects, RUN_SETUP | NO_PARSEOPT },
c6127fa3
SS
529 { "update-index", cmd_update_index, RUN_SETUP },
530 { "update-ref", cmd_update_ref, RUN_SETUP },
531 { "update-server-info", cmd_update_server_info, RUN_SETUP },
e9547947
NTND
532 { "upload-archive", cmd_upload_archive, NO_PARSEOPT },
533 { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT },
534 { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT },
d07b00b7 535 { "verify-commit", cmd_verify_commit, RUN_SETUP },
c6127fa3
SS
536 { "verify-pack", cmd_verify_pack },
537 { "verify-tag", cmd_verify_tag, RUN_SETUP },
538 { "version", cmd_version },
539 { "whatchanged", cmd_whatchanged, RUN_SETUP },
e9547947 540 { "worktree", cmd_worktree, RUN_SETUP | NO_PARSEOPT },
c6127fa3
SS
541 { "write-tree", cmd_write_tree, RUN_SETUP },
542};
543
c4f901d1 544static struct cmd_struct *get_builtin(const char *s)
c6127fa3
SS
545{
546 int i;
547 for (i = 0; i < ARRAY_SIZE(commands); i++) {
c4f901d1 548 struct cmd_struct *p = commands + i;
c6127fa3 549 if (!strcmp(s, p->cmd))
c4f901d1 550 return p;
c6127fa3 551 }
c4f901d1
SV
552 return NULL;
553}
554
555int is_builtin(const char *s)
556{
557 return !!get_builtin(s);
c6127fa3
SS
558}
559
e5d7a619 560static void list_builtins(struct string_list *out, unsigned int exclude_option)
8893fd95
JK
561{
562 int i;
e9547947
NTND
563 for (i = 0; i < ARRAY_SIZE(commands); i++) {
564 if (exclude_option &&
565 (commands[i].option & exclude_option))
566 continue;
e5d7a619 567 string_list_append(out, commands[i].cmd);
e9547947 568 }
8893fd95
JK
569}
570
63ca1c09
AK
571#ifdef STRIP_EXTENSION
572static void strip_extension(const char **argv)
573{
574 size_t len;
575
576 if (strip_suffix(argv[0], STRIP_EXTENSION, &len))
577 argv[0] = xmemdupz(argv[0], len);
578}
579#else
580#define strip_extension(cmd)
581#endif
582
3f784a4d 583static void handle_builtin(int argc, const char **argv)
231af832 584{
2c6b6d9f 585 struct argv_array args = ARGV_ARRAY_INIT;
63ca1c09 586 const char *cmd;
c4f901d1 587 struct cmd_struct *builtin;
23326d14 588
63ca1c09
AK
589 strip_extension(argv);
590 cmd = argv[0];
231af832 591
2c6b6d9f 592 /* Turn "git cmd --help" into "git help --exclude-guides cmd" */
1cd95087 593 if (argc > 1 && !strcmp(argv[1], "--help")) {
2c6b6d9f
RT
594 int i;
595
1cd95087
LT
596 argv[1] = argv[0];
597 argv[0] = cmd = "help";
2c6b6d9f
RT
598
599 for (i = 0; i < argc; i++) {
600 argv_array_push(&args, argv[i]);
601 if (!i)
602 argv_array_push(&args, "--exclude-guides");
603 }
604
605 argc++;
606 argv = args.argv;
1cd95087
LT
607 }
608
c4f901d1 609 builtin = get_builtin(cmd);
441981bc
JH
610 if (builtin)
611 exit(run_builtin(builtin, argc, argv));
2c6b6d9f 612 argv_array_clear(&args);
231af832
LT
613}
614
7550be0a
JH
615static void execv_dashed_external(const char **argv)
616{
2b296c93 617 struct child_process cmd = CHILD_PROCESS_INIT;
d8e96fd8 618 int status;
7550be0a 619
74866d75
BW
620 if (get_super_prefix())
621 die("%s doesn't support --super-prefix", argv[0]);
622
595d59e2 623 if (use_pager == -1 && !is_builtin(argv[0]))
92058e4d 624 use_pager = check_pager_config(argv[0]);
030149a4
JN
625 commit_pager_choice();
626
2b296c93
JK
627 argv_array_pushf(&cmd.args, "git-%s", argv[0]);
628 argv_array_pushv(&cmd.args, argv + 1);
629 cmd.clean_on_exit = 1;
46df6906 630 cmd.wait_after_clean = 1;
2b296c93 631 cmd.silent_exec_failure = 1;
7550be0a 632
2b296c93 633 trace_argv_printf(cmd.args.argv, "trace: exec:");
7550be0a 634
d8e96fd8 635 /*
246f0ede
JK
636 * If we fail because the command is not found, it is
637 * OK to return. Otherwise, we just pass along the status code,
638 * or our usual generic code if we were not even able to exec
639 * the program.
d8e96fd8 640 */
2b296c93 641 status = run_command(&cmd);
246f0ede 642 if (status >= 0)
5709e036 643 exit(status);
246f0ede
JK
644 else if (errno != ENOENT)
645 exit(128);
7550be0a
JH
646}
647
a907e1b6
AS
648static int run_argv(int *argcp, const char ***argv)
649{
650 int done_alias = 0;
651
652 while (1) {
441981bc
JH
653 /*
654 * If we tried alias and futzed with our environment,
655 * it no longer is safe to invoke builtins directly in
656 * general. We have to spawn them as dashed externals.
657 *
658 * NEEDSWORK: if we can figure out cases
659 * where it is safe to do, we can avoid spawning a new
660 * process.
661 */
662 if (!done_alias)
663 handle_builtin(*argcp, *argv);
a907e1b6
AS
664
665 /* .. then try the external ones */
666 execv_dashed_external(*argv);
667
668 /* It could be an alias -- this works around the insanity
669 * of overriding "git log" with "git show" by having
670 * alias.log = show
671 */
c0562611
NTND
672 if (done_alias)
673 break;
c0562611 674 if (!handle_alias(argcp, argv))
a907e1b6
AS
675 break;
676 done_alias = 1;
677 }
678
679 return done_alias;
680}
681
3f2e2297 682int cmd_main(int argc, const char **argv)
8e49d503 683{
4dd47c3b 684 const char *cmd;
8fa7975b 685 int done_help = 0;
231af832 686
650c4492 687 cmd = argv[0];
2cd72b0b 688 if (!cmd)
4dd47c3b 689 cmd = "git-help";
6854a8f5
JK
690 else {
691 const char *slash = find_last_dir_sep(cmd);
692 if (slash)
693 cmd = slash + 1;
694 }
8e49d503 695
578da039
KB
696 trace_command_performance(argv);
697
231af832
LT
698 /*
699 * "git-xxxx" is the same as "git xxxx", but we obviously:
700 *
701 * - cannot take flags in between the "git" and the "xxxx".
702 * - cannot execute it externally (since it would just do
703 * the same thing over again)
704 *
3f784a4d
SS
705 * So we just directly call the builtin handler, and die if
706 * that one cannot handle it.
231af832 707 */
ae021d87 708 if (skip_prefix(cmd, "git-", &cmd)) {
231af832 709 argv[0] = cmd;
3f784a4d
SS
710 handle_builtin(argc, argv);
711 die("cannot handle %s as a builtin", cmd);
231af832 712 }
8e49d503 713
231af832 714 /* Look for flags.. */
6acbcb92
JS
715 argv++;
716 argc--;
4394efec 717 handle_options(&argv, &argc, NULL);
6acbcb92 718 if (argc > 0) {
6d877803
JK
719 /* translate --help and --version into commands */
720 skip_prefix(argv[0], "--", &argv[0]);
6acbcb92 721 } else {
3d7e2d85 722 /* The user didn't specify a command; give them help */
73e25e7c 723 commit_pager_choice();
3d7e2d85
SP
724 printf("usage: %s\n\n", git_usage_string);
725 list_common_cmds_help();
421a9769 726 printf("\n%s\n", _(git_more_info_string));
3d7e2d85 727 exit(1);
97fc6c5f 728 }
6acbcb92 729 cmd = argv[0];
231af832
LT
730
731 /*
511707d4 732 * We use PATH to find git commands, but we prepend some higher
3ea3c215 733 * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
511707d4
SP
734 * environment, and the $(gitexecdir) from the Makefile at build
735 * time.
231af832 736 */
e1464ca7 737 setup_path();
8e49d503 738
a025463b 739 while (1) {
8fa7975b 740 int was_alias = run_argv(&argc, &argv);
a907e1b6 741 if (errno != ENOENT)
a025463b 742 break;
a907e1b6 743 if (was_alias) {
fc045fe7
AS
744 fprintf(stderr, _("expansion of alias '%s' failed; "
745 "'%s' is not a git command\n"),
f3dd015c
TT
746 cmd, argv[0]);
747 exit(1);
748 }
a907e1b6
AS
749 if (!done_help) {
750 cmd = argv[0] = help_unknown_cmd(cmd);
751 done_help = 1;
752 } else
753 break;
f3dd015c 754 }
10b15b86 755
fc045fe7 756 fprintf(stderr, _("failed to run command '%s': %s\n"),
33ebb871 757 cmd, strerror(errno));
8e49d503
AE
758
759 return 1;
760}