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