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