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