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