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