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