Code

Merge branch 'jc/rerere-remaining' into pu
[git.git] / git.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "exec_cmd.h"
4 #include "help.h"
5 #include "quote.h"
6 #include "run-command.h"
7 #include "gettext.h"
9 const char git_usage_string[] =
10         "git [--version] [--exec-path[=<path>]] [--html-path]\n"
11         "           [-p|--paginate|--no-pager] [--no-replace-objects]\n"
12         "           [--bare] [--git-dir=<path>] [--work-tree=<path>]\n"
13         "           [-c name=value] [--help]\n"
14         "           <command> [<args>]";
16 const char git_more_info_string[] =
17         "See 'git help <command>' for more information on a specific command.";
19 static struct startup_info git_startup_info;
20 static int use_pager = -1;
21 struct pager_config {
22         const char *cmd;
23         int want;
24         char *value;
25 };
27 static int pager_command_config(const char *var, const char *value, void *data)
28 {
29         struct pager_config *c = data;
30         if (!prefixcmp(var, "pager.") && !strcmp(var + 6, c->cmd)) {
31                 int b = git_config_maybe_bool(var, value);
32                 if (b >= 0)
33                         c->want = b;
34                 else {
35                         c->want = 1;
36                         c->value = xstrdup(value);
37                 }
38         }
39         return 0;
40 }
42 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
43 int check_pager_config(const char *cmd)
44 {
45         struct pager_config c;
46         c.cmd = cmd;
47         c.want = -1;
48         c.value = NULL;
49         git_config(pager_command_config, &c);
50         if (c.value)
51                 pager_program = c.value;
52         return c.want;
53 }
55 static void commit_pager_choice(void) {
56         switch (use_pager) {
57         case 0:
58                 setenv("GIT_PAGER", "cat", 1);
59                 break;
60         case 1:
61                 setup_pager();
62                 break;
63         default:
64                 break;
65         }
66 }
68 static int handle_options(const char ***argv, int *argc, int *envchanged)
69 {
70         int handled = 0;
72         while (*argc > 0) {
73                 const char *cmd = (*argv)[0];
74                 if (cmd[0] != '-')
75                         break;
77                 /*
78                  * For legacy reasons, the "version" and "help"
79                  * commands can be written with "--" prepended
80                  * to make them look like flags.
81                  */
82                 if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version"))
83                         break;
85                 /*
86                  * Check remaining flags.
87                  */
88                 if (!prefixcmp(cmd, "--exec-path")) {
89                         cmd += 11;
90                         if (*cmd == '=')
91                                 git_set_argv_exec_path(cmd + 1);
92                         else {
93                                 puts(git_exec_path());
94                                 exit(0);
95                         }
96                 } else if (!strcmp(cmd, "--html-path")) {
97                         puts(system_path(GIT_HTML_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                         read_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                         handled++;
121                 } else if (!prefixcmp(cmd, "--git-dir=")) {
122                         setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
123                         if (envchanged)
124                                 *envchanged = 1;
125                 } else if (!strcmp(cmd, "--work-tree")) {
126                         if (*argc < 2) {
127                                 fprintf(stderr, "No directory given for --work-tree.\n" );
128                                 usage(git_usage_string);
129                         }
130                         setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
131                         if (envchanged)
132                                 *envchanged = 1;
133                         (*argv)++;
134                         (*argc)--;
135                 } else if (!prefixcmp(cmd, "--work-tree=")) {
136                         setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
137                         if (envchanged)
138                                 *envchanged = 1;
139                 } else if (!strcmp(cmd, "--bare")) {
140                         static char git_dir[PATH_MAX+1];
141                         is_bare_repository_cfg = 1;
142                         setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 0);
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 {
154                         fprintf(stderr, "Unknown option: %s\n", cmd);
155                         usage(git_usage_string);
156                 }
158                 (*argv)++;
159                 (*argc)--;
160                 handled++;
161         }
162         return handled;
165 static int handle_alias(int *argcp, const char ***argv)
167         int envchanged = 0, ret = 0, saved_errno = errno;
168         const char *subdir;
169         int count, option_count;
170         const char **new_argv;
171         const char *alias_command;
172         char *alias_string;
173         int unused_nongit;
175         subdir = setup_git_directory_gently(&unused_nongit);
177         alias_command = (*argv)[0];
178         alias_string = alias_lookup(alias_command);
179         if (alias_string) {
180                 if (alias_string[0] == '!') {
181                         const char **alias_argv;
182                         int argc = *argcp, i;
184                         commit_pager_choice();
186                         /* build alias_argv */
187                         alias_argv = xmalloc(sizeof(*alias_argv) * (argc + 1));
188                         alias_argv[0] = alias_string + 1;
189                         for (i = 1; i < argc; ++i)
190                                 alias_argv[i] = (*argv)[i];
191                         alias_argv[argc] = NULL;
193                         ret = run_command_v_opt(alias_argv, RUN_USING_SHELL);
194                         if (ret >= 0)   /* normal exit */
195                                 exit(ret);
197                         die_errno("While expanding alias '%s': '%s'",
198                             alias_command, alias_string + 1);
199                 }
200                 count = split_cmdline(alias_string, &new_argv);
201                 if (count < 0)
202                         die("Bad alias.%s string: %s", alias_command,
203                             split_cmdline_strerror(count));
204                 option_count = handle_options(&new_argv, &count, &envchanged);
205                 if (envchanged)
206                         die("alias '%s' changes environment variables\n"
207                                  "You can use '!git' in the alias to do this.",
208                                  alias_command);
209                 memmove(new_argv - option_count, new_argv,
210                                 count * sizeof(char *));
211                 new_argv -= option_count;
213                 if (count < 1)
214                         die("empty alias for %s", alias_command);
216                 if (!strcmp(alias_command, new_argv[0]))
217                         die("recursive alias: %s", alias_command);
219                 trace_argv_printf(new_argv,
220                                   "trace: alias expansion: %s =>",
221                                   alias_command);
223                 new_argv = xrealloc(new_argv, sizeof(char *) *
224                                     (count + *argcp));
225                 /* insert after command name */
226                 memcpy(new_argv + count, *argv + 1, sizeof(char *) * *argcp);
228                 *argv = new_argv;
229                 *argcp += count - 1;
231                 ret = 1;
232         }
234         if (subdir && chdir(subdir))
235                 die_errno("Cannot change to '%s'", subdir);
237         errno = saved_errno;
239         return ret;
242 const char git_version_string[] = GIT_VERSION;
244 #define RUN_SETUP               (1<<0)
245 #define RUN_SETUP_GENTLY        (1<<1)
246 #define USE_PAGER               (1<<2)
247 /*
248  * require working tree to be present -- anything uses this needs
249  * RUN_SETUP for reading from the configuration file.
250  */
251 #define NEED_WORK_TREE          (1<<3)
253 struct cmd_struct {
254         const char *cmd;
255         int (*fn)(int, const char **, const char *);
256         int option;
257 };
259 static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
261         int status, help;
262         struct stat st;
263         const char *prefix;
265         prefix = NULL;
266         help = argc == 2 && !strcmp(argv[1], "-h");
267         if (!help) {
268                 if (p->option & RUN_SETUP)
269                         prefix = setup_git_directory();
270                 if (p->option & RUN_SETUP_GENTLY) {
271                         int nongit_ok;
272                         prefix = setup_git_directory_gently(&nongit_ok);
273                 }
275                 if (use_pager == -1 && p->option & (RUN_SETUP | RUN_SETUP_GENTLY))
276                         use_pager = check_pager_config(p->cmd);
277                 if (use_pager == -1 && p->option & USE_PAGER)
278                         use_pager = 1;
280                 if ((p->option & (RUN_SETUP | RUN_SETUP_GENTLY)) &&
281                     startup_info->have_repository) /* get_git_dir() may set up repo, avoid that */
282                         trace_repo_setup(prefix);
283         }
284         commit_pager_choice();
286         if (!help && p->option & NEED_WORK_TREE)
287                 setup_work_tree();
289         trace_argv_printf(argv, "trace: built-in: git");
291         status = p->fn(argc, argv, prefix);
292         if (status)
293                 return status;
295         /* Somebody closed stdout? */
296         if (fstat(fileno(stdout), &st))
297                 return 0;
298         /* Ignore write errors for pipes and sockets.. */
299         if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode))
300                 return 0;
302         /* Check for ENOSPC and EIO errors.. */
303         if (fflush(stdout))
304                 die_errno("write failure on standard output");
305         if (ferror(stdout))
306                 die("unknown write failure on standard output");
307         if (fclose(stdout))
308                 die_errno("close failed on standard output");
309         return 0;
312 static void handle_internal_command(int argc, const char **argv)
314         const char *cmd = argv[0];
315         static struct cmd_struct commands[] = {
316                 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
317                 { "stage", cmd_add, RUN_SETUP | NEED_WORK_TREE },
318                 { "annotate", cmd_annotate, RUN_SETUP },
319                 { "apply", cmd_apply, RUN_SETUP_GENTLY },
320                 { "archive", cmd_archive },
321                 { "bisect--helper", cmd_bisect__helper, RUN_SETUP | NEED_WORK_TREE },
322                 { "blame", cmd_blame, RUN_SETUP },
323                 { "branch", cmd_branch, RUN_SETUP },
324                 { "bundle", cmd_bundle, RUN_SETUP_GENTLY },
325                 { "cat-file", cmd_cat_file, RUN_SETUP },
326                 { "checkout", cmd_checkout, RUN_SETUP | NEED_WORK_TREE },
327                 { "checkout-index", cmd_checkout_index,
328                         RUN_SETUP | NEED_WORK_TREE},
329                 { "check-ref-format", cmd_check_ref_format },
330                 { "check-attr", cmd_check_attr, RUN_SETUP },
331                 { "cherry", cmd_cherry, RUN_SETUP },
332                 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
333                 { "clone", cmd_clone },
334                 { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
335                 { "commit", cmd_commit, RUN_SETUP | NEED_WORK_TREE },
336                 { "commit-tree", cmd_commit_tree, RUN_SETUP },
337                 { "config", cmd_config, RUN_SETUP_GENTLY },
338                 { "count-objects", cmd_count_objects, RUN_SETUP },
339                 { "describe", cmd_describe, RUN_SETUP },
340                 { "diff", cmd_diff },
341                 { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE },
342                 { "diff-index", cmd_diff_index, RUN_SETUP },
343                 { "diff-tree", cmd_diff_tree, RUN_SETUP },
344                 { "fast-export", cmd_fast_export, RUN_SETUP },
345                 { "fetch", cmd_fetch, RUN_SETUP },
346                 { "fetch-pack", cmd_fetch_pack, RUN_SETUP },
347                 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
348                 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
349                 { "format-patch", cmd_format_patch, RUN_SETUP },
350                 { "fsck", cmd_fsck, RUN_SETUP },
351                 { "fsck-objects", cmd_fsck, RUN_SETUP },
352                 { "gc", cmd_gc, RUN_SETUP },
353                 { "get-tar-commit-id", cmd_get_tar_commit_id },
354                 { "grep", cmd_grep, RUN_SETUP_GENTLY },
355                 { "hash-object", cmd_hash_object },
356                 { "help", cmd_help },
357                 { "index-pack", cmd_index_pack, RUN_SETUP_GENTLY },
358                 { "init", cmd_init_db },
359                 { "init-db", cmd_init_db },
360                 { "log", cmd_log, RUN_SETUP },
361                 { "ls-files", cmd_ls_files, RUN_SETUP },
362                 { "ls-tree", cmd_ls_tree, RUN_SETUP },
363                 { "ls-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
364                 { "mailinfo", cmd_mailinfo },
365                 { "mailsplit", cmd_mailsplit },
366                 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
367                 { "merge-base", cmd_merge_base, RUN_SETUP },
368                 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
369                 { "merge-index", cmd_merge_index, RUN_SETUP },
370                 { "merge-ours", cmd_merge_ours, RUN_SETUP },
371                 { "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
372                 { "merge-recursive-ours", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
373                 { "merge-recursive-theirs", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
374                 { "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
375                 { "merge-tree", cmd_merge_tree, RUN_SETUP },
376                 { "mktag", cmd_mktag, RUN_SETUP },
377                 { "mktree", cmd_mktree, RUN_SETUP },
378                 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
379                 { "name-rev", cmd_name_rev, RUN_SETUP },
380                 { "notes", cmd_notes, RUN_SETUP },
381                 { "pack-objects", cmd_pack_objects, RUN_SETUP },
382                 { "pack-redundant", cmd_pack_redundant, RUN_SETUP },
383                 { "patch-id", cmd_patch_id },
384                 { "peek-remote", cmd_ls_remote, RUN_SETUP_GENTLY },
385                 { "pickaxe", cmd_blame, RUN_SETUP },
386                 { "prune", cmd_prune, RUN_SETUP },
387                 { "prune-packed", cmd_prune_packed, RUN_SETUP },
388                 { "push", cmd_push, RUN_SETUP },
389                 { "read-tree", cmd_read_tree, RUN_SETUP },
390                 { "receive-pack", cmd_receive_pack },
391                 { "reflog", cmd_reflog, RUN_SETUP },
392                 { "remote", cmd_remote, RUN_SETUP },
393                 { "remote-ext", cmd_remote_ext },
394                 { "remote-fd", cmd_remote_fd },
395                 { "replace", cmd_replace, RUN_SETUP },
396                 { "repo-config", cmd_config, RUN_SETUP_GENTLY },
397                 { "rerere", cmd_rerere, RUN_SETUP },
398                 { "reset", cmd_reset, RUN_SETUP },
399                 { "rev-list", cmd_rev_list, RUN_SETUP },
400                 { "rev-parse", cmd_rev_parse },
401                 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
402                 { "rm", cmd_rm, RUN_SETUP },
403                 { "send-pack", cmd_send_pack, RUN_SETUP },
404                 { "shortlog", cmd_shortlog, RUN_SETUP_GENTLY | USE_PAGER },
405                 { "show-branch", cmd_show_branch, RUN_SETUP },
406                 { "show", cmd_show, RUN_SETUP },
407                 { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE },
408                 { "stripspace", cmd_stripspace },
409                 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
410                 { "tag", cmd_tag, RUN_SETUP },
411                 { "tar-tree", cmd_tar_tree },
412                 { "unpack-file", cmd_unpack_file, RUN_SETUP },
413                 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
414                 { "update-index", cmd_update_index, RUN_SETUP },
415                 { "update-ref", cmd_update_ref, RUN_SETUP },
416                 { "update-server-info", cmd_update_server_info, RUN_SETUP },
417                 { "upload-archive", cmd_upload_archive },
418                 { "var", cmd_var, RUN_SETUP_GENTLY },
419                 { "verify-tag", cmd_verify_tag, RUN_SETUP },
420                 { "version", cmd_version },
421                 { "whatchanged", cmd_whatchanged, RUN_SETUP },
422                 { "write-tree", cmd_write_tree, RUN_SETUP },
423                 { "verify-pack", cmd_verify_pack },
424                 { "show-ref", cmd_show_ref, RUN_SETUP },
425                 { "pack-refs", cmd_pack_refs, RUN_SETUP },
426                 { "skew", cmd_skew, RUN_SETUP },
427         };
428         int i;
429         static const char ext[] = STRIP_EXTENSION;
431         if (sizeof(ext) > 1) {
432                 i = strlen(argv[0]) - strlen(ext);
433                 if (i > 0 && !strcmp(argv[0] + i, ext)) {
434                         char *argv0 = xstrdup(argv[0]);
435                         argv[0] = cmd = argv0;
436                         argv0[i] = '\0';
437                 }
438         }
440         /* Turn "git cmd --help" into "git help cmd" */
441         if (argc > 1 && !strcmp(argv[1], "--help")) {
442                 argv[1] = argv[0];
443                 argv[0] = cmd = "help";
444         }
446         for (i = 0; i < ARRAY_SIZE(commands); i++) {
447                 struct cmd_struct *p = commands+i;
448                 if (strcmp(p->cmd, cmd))
449                         continue;
450                 exit(run_builtin(p, argc, argv));
451         }
454 static void execv_dashed_external(const char **argv)
456         struct strbuf cmd = STRBUF_INIT;
457         const char *tmp;
458         int status;
460         commit_pager_choice();
462         strbuf_addf(&cmd, "git-%s", argv[0]);
464         /*
465          * argv[0] must be the git command, but the argv array
466          * belongs to the caller, and may be reused in
467          * subsequent loop iterations. Save argv[0] and
468          * restore it on error.
469          */
470         tmp = argv[0];
471         argv[0] = cmd.buf;
473         trace_argv_printf(argv, "trace: exec:");
475         /*
476          * if we fail because the command is not found, it is
477          * OK to return. Otherwise, we just pass along the status code.
478          */
479         status = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
480         if (status >= 0 || errno != ENOENT)
481                 exit(status);
483         argv[0] = tmp;
485         strbuf_release(&cmd);
488 static int run_argv(int *argcp, const char ***argv)
490         int done_alias = 0;
492         while (1) {
493                 /* See if it's an internal command */
494                 handle_internal_command(*argcp, *argv);
496                 /* .. then try the external ones */
497                 execv_dashed_external(*argv);
499                 /* It could be an alias -- this works around the insanity
500                  * of overriding "git log" with "git show" by having
501                  * alias.log = show
502                  */
503                 if (done_alias || !handle_alias(argcp, argv))
504                         break;
505                 done_alias = 1;
506         }
508         return done_alias;
512 int main(int argc, const char **argv)
514         const char *cmd;
516         startup_info = &git_startup_info;
518         cmd = git_extract_argv0_path(argv[0]);
519         if (!cmd)
520                 cmd = "git-help";
522         git_setup_gettext();
524         /*
525          * "git-xxxx" is the same as "git xxxx", but we obviously:
526          *
527          *  - cannot take flags in between the "git" and the "xxxx".
528          *  - cannot execute it externally (since it would just do
529          *    the same thing over again)
530          *
531          * So we just directly call the internal command handler, and
532          * die if that one cannot handle it.
533          */
534         if (!prefixcmp(cmd, "git-")) {
535                 cmd += 4;
536                 argv[0] = cmd;
537                 handle_internal_command(argc, argv);
538                 die("cannot handle %s internally", cmd);
539         }
541         /* Look for flags.. */
542         argv++;
543         argc--;
544         handle_options(&argv, &argc, NULL);
545         if (argc > 0) {
546                 if (!prefixcmp(argv[0], "--"))
547                         argv[0] += 2;
548         } else {
549                 /* The user didn't specify a command; give them help */
550                 commit_pager_choice();
551                 printf("usage: %s\n\n", git_usage_string);
552                 list_common_cmds_help();
553                 printf("\n%s\n", git_more_info_string);
554                 exit(1);
555         }
556         cmd = argv[0];
558         /*
559          * We use PATH to find git commands, but we prepend some higher
560          * precedence paths: the "--exec-path" option, the GIT_EXEC_PATH
561          * environment, and the $(gitexecdir) from the Makefile at build
562          * time.
563          */
564         setup_path();
566         while (1) {
567                 static int done_help = 0;
568                 static int was_alias = 0;
569                 was_alias = run_argv(&argc, &argv);
570                 if (errno != ENOENT)
571                         break;
572                 if (was_alias) {
573                         fprintf(stderr, "Expansion of alias '%s' failed; "
574                                 "'%s' is not a git command\n",
575                                 cmd, argv[0]);
576                         exit(1);
577                 }
578                 if (!done_help) {
579                         cmd = argv[0] = help_unknown_cmd(cmd);
580                         done_help = 1;
581                 } else
582                         break;
583         }
585         fprintf(stderr, "Failed to run command '%s': %s\n",
586                 cmd, strerror(errno));
588         return 1;