Merge branch 'jc/fetch-sorted'
[git.git] / git.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <limits.h>
10 #include <stdarg.h>
11 #include "git-compat-util.h"
12 #include "exec_cmd.h"
13
14 #include "builtin.h"
15
16 static void prepend_to_path(const char *dir, int len)
17 {
18         char *path, *old_path = getenv("PATH");
19         int path_len = len;
20
21         if (!old_path)
22                 old_path = "/usr/local/bin:/usr/bin:/bin";
23
24         path_len = len + strlen(old_path) + 1;
25
26         path = malloc(path_len + 1);
27
28         memcpy(path, dir, len);
29         path[len] = ':';
30         memcpy(path + len + 1, old_path, path_len - len);
31
32         setenv("PATH", path, 1);
33 }
34
35 const char git_version_string[] = GIT_VERSION;
36
37 static void handle_internal_command(int argc, const char **argv, char **envp)
38 {
39         const char *cmd = argv[0];
40         static struct cmd_struct {
41                 const char *cmd;
42                 int (*fn)(int, const char **, char **);
43         } commands[] = {
44                 { "version", cmd_version },
45                 { "help", cmd_help },
46                 { "log", cmd_log },
47                 { "whatchanged", cmd_whatchanged },
48                 { "show", cmd_show },
49                 { "push", cmd_push },
50                 { "count-objects", cmd_count_objects },
51                 { "diff", cmd_diff },
52                 { "grep", cmd_grep },
53                 { "rm", cmd_rm },
54                 { "add", cmd_add },
55                 { "rev-list", cmd_rev_list },
56                 { "init-db", cmd_init_db },
57                 { "check-ref-format", cmd_check_ref_format }
58         };
59         int i;
60
61         /* Turn "git cmd --help" into "git help cmd" */
62         if (argc > 1 && !strcmp(argv[1], "--help")) {
63                 argv[1] = argv[0];
64                 argv[0] = cmd = "help";
65         }
66
67         for (i = 0; i < ARRAY_SIZE(commands); i++) {
68                 struct cmd_struct *p = commands+i;
69                 if (strcmp(p->cmd, cmd))
70                         continue;
71                 exit(p->fn(argc, argv, envp));
72         }
73 }
74
75 int main(int argc, const char **argv, char **envp)
76 {
77         const char *cmd = argv[0];
78         char *slash = strrchr(cmd, '/');
79         char git_command[PATH_MAX + 1];
80         const char *exec_path = NULL;
81
82         /*
83          * Take the basename of argv[0] as the command
84          * name, and the dirname as the default exec_path
85          * if it's an absolute path and we don't have
86          * anything better.
87          */
88         if (slash) {
89                 *slash++ = 0;
90                 if (*cmd == '/')
91                         exec_path = cmd;
92                 cmd = slash;
93         }
94
95         /*
96          * "git-xxxx" is the same as "git xxxx", but we obviously:
97          *
98          *  - cannot take flags in between the "git" and the "xxxx".
99          *  - cannot execute it externally (since it would just do
100          *    the same thing over again)
101          *
102          * So we just directly call the internal command handler, and
103          * die if that one cannot handle it.
104          */
105         if (!strncmp(cmd, "git-", 4)) {
106                 cmd += 4;
107                 argv[0] = cmd;
108                 handle_internal_command(argc, argv, envp);
109                 die("cannot handle %s internally", cmd);
110         }
111
112         /* Default command: "help" */
113         cmd = "help";
114
115         /* Look for flags.. */
116         while (argc > 1) {
117                 cmd = *++argv;
118                 argc--;
119
120                 if (strncmp(cmd, "--", 2))
121                         break;
122
123                 cmd += 2;
124
125                 /*
126                  * For legacy reasons, the "version" and "help"
127                  * commands can be written with "--" prepended
128                  * to make them look like flags.
129                  */
130                 if (!strcmp(cmd, "help"))
131                         break;
132                 if (!strcmp(cmd, "version"))
133                         break;
134
135                 /*
136                  * Check remaining flags (which by now must be
137                  * "--exec-path", but maybe we will accept
138                  * other arguments some day)
139                  */
140                 if (!strncmp(cmd, "exec-path", 9)) {
141                         cmd += 9;
142                         if (*cmd == '=') {
143                                 git_set_exec_path(cmd + 1);
144                                 continue;
145                         }
146                         puts(git_exec_path());
147                         exit(0);
148                 }
149                 cmd_usage(0, NULL, NULL);
150         }
151         argv[0] = cmd;
152
153         /*
154          * We search for git commands in the following order:
155          *  - git_exec_path()
156          *  - the path of the "git" command if we could find it
157          *    in $0
158          *  - the regular PATH.
159          */
160         if (exec_path)
161                 prepend_to_path(exec_path, strlen(exec_path));
162         exec_path = git_exec_path();
163         prepend_to_path(exec_path, strlen(exec_path));
164
165         /* See if it's an internal command */
166         handle_internal_command(argc, argv, envp);
167
168         /* .. then try the external ones */
169         execv_git_cmd(argv);
170
171         if (errno == ENOENT)
172                 cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
173
174         fprintf(stderr, "Failed to run command '%s': %s\n",
175                 git_command, strerror(errno));
176
177         return 1;
178 }