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