git-repack: avoid redirecting stderr into git-pack-objects
[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 #include "cache.h"
14
15 #include "builtin.h"
16
17 static void prepend_to_path(const char *dir, int len)
18 {
19         const char *old_path = getenv("PATH");
20         char *path;
21         int path_len = len;
22
23         if (!old_path)
24                 old_path = "/usr/local/bin:/usr/bin:/bin";
25
26         path_len = len + strlen(old_path) + 1;
27
28         path = malloc(path_len + 1);
29
30         memcpy(path, dir, len);
31         path[len] = ':';
32         memcpy(path + len + 1, old_path, path_len - len);
33
34         setenv("PATH", path, 1);
35 }
36
37 static const char *alias_command;
38 static char *alias_string = NULL;
39
40 static int git_alias_config(const char *var, const char *value)
41 {
42         if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) {
43                 alias_string = strdup(value);
44         }
45         return 0;
46 }
47
48 static int split_cmdline(char *cmdline, const char ***argv)
49 {
50         int src, dst, count = 0, size = 16;
51         char quoted = 0;
52
53         *argv = malloc(sizeof(char*) * size);
54
55         /* split alias_string */
56         (*argv)[count++] = cmdline;
57         for (src = dst = 0; cmdline[src];) {
58                 char c = cmdline[src];
59                 if (!quoted && isspace(c)) {
60                         cmdline[dst++] = 0;
61                         while (cmdline[++src]
62                                         && isspace(cmdline[src]))
63                                 ; /* skip */
64                         if (count >= size) {
65                                 size += 16;
66                                 *argv = realloc(*argv, sizeof(char*) * size);
67                         }
68                         (*argv)[count++] = cmdline + dst;
69                 } else if(!quoted && (c == '\'' || c == '"')) {
70                         quoted = c;
71                         src++;
72                 } else if (c == quoted) {
73                         quoted = 0;
74                         src++;
75                 } else {
76                         if (c == '\\' && quoted != '\'') {
77                                 src++;
78                                 c = cmdline[src];
79                                 if (!c) {
80                                         free(*argv);
81                                         *argv = NULL;
82                                         return error("cmdline ends with \\");
83                                 }
84                         }
85                         cmdline[dst++] = c;
86                         src++;
87                 }
88         }
89
90         cmdline[dst] = 0;
91
92         if (quoted) {
93                 free(*argv);
94                 *argv = NULL;
95                 return error("unclosed quote");
96         }
97
98         return count;
99 }
100
101 static int handle_alias(int *argcp, const char ***argv)
102 {
103         int nongit = 0, ret = 0, saved_errno = errno;
104         const char *subdir;
105
106         subdir = setup_git_directory_gently(&nongit);
107         if (!nongit) {
108                 int count;
109                 const char** new_argv;
110
111                 alias_command = (*argv)[0];
112                 git_config(git_alias_config);
113                 if (alias_string) {
114
115                         count = split_cmdline(alias_string, &new_argv);
116
117                         if (count < 1)
118                                 die("empty alias for %s", alias_command);
119
120                         if (!strcmp(alias_command, new_argv[0]))
121                                 die("recursive alias: %s", alias_command);
122
123                         /* insert after command name */
124                         if (*argcp > 1) {
125                                 new_argv = realloc(new_argv, sizeof(char*) *
126                                                    (count + *argcp));
127                                 memcpy(new_argv + count, *argv + 1,
128                                        sizeof(char*) * *argcp);
129                         }
130
131                         *argv = new_argv;
132                         *argcp += count - 1;
133
134                         ret = 1;
135                 }
136         }
137
138         if (subdir)
139                 chdir(subdir);
140
141         errno = saved_errno;
142
143         return ret;
144 }
145
146 const char git_version_string[] = GIT_VERSION;
147
148 static void handle_internal_command(int argc, const char **argv, char **envp)
149 {
150         const char *cmd = argv[0];
151         static struct cmd_struct {
152                 const char *cmd;
153                 int (*fn)(int, const char **, char **);
154         } commands[] = {
155                 { "version", cmd_version },
156                 { "help", cmd_help },
157                 { "log", cmd_log },
158                 { "whatchanged", cmd_whatchanged },
159                 { "show", cmd_show },
160                 { "push", cmd_push },
161                 { "format-patch", cmd_format_patch },
162                 { "count-objects", cmd_count_objects },
163                 { "diff", cmd_diff },
164                 { "grep", cmd_grep },
165                 { "rm", cmd_rm },
166                 { "add", cmd_add },
167                 { "rev-list", cmd_rev_list },
168                 { "init-db", cmd_init_db },
169                 { "get-tar-commit-id", cmd_get_tar_commit_id },
170                 { "upload-tar", cmd_upload_tar },
171                 { "check-ref-format", cmd_check_ref_format },
172                 { "ls-files", cmd_ls_files },
173                 { "ls-tree", cmd_ls_tree },
174                 { "tar-tree", cmd_tar_tree },
175                 { "read-tree", cmd_read_tree },
176                 { "commit-tree", cmd_commit_tree },
177                 { "apply", cmd_apply },
178                 { "show-branch", cmd_show_branch },
179                 { "diff-files", cmd_diff_files },
180                 { "diff-index", cmd_diff_index },
181                 { "diff-stages", cmd_diff_stages },
182                 { "diff-tree", cmd_diff_tree },
183                 { "cat-file", cmd_cat_file },
184                 { "rev-parse", cmd_rev_parse },
185                 { "write-tree", cmd_write_tree },
186                 { "mailsplit", cmd_mailsplit },
187                 { "mailinfo", cmd_mailinfo },
188                 { "stripspace", cmd_stripspace },
189                 { "update-index", cmd_update_index },
190                 { "update-ref", cmd_update_ref },
191                 { "fmt-merge-msg", cmd_fmt_merge_msg },
192                 { "prune", cmd_prune },
193         };
194         int i;
195
196         /* Turn "git cmd --help" into "git help cmd" */
197         if (argc > 1 && !strcmp(argv[1], "--help")) {
198                 argv[1] = argv[0];
199                 argv[0] = cmd = "help";
200         }
201
202         for (i = 0; i < ARRAY_SIZE(commands); i++) {
203                 struct cmd_struct *p = commands+i;
204                 if (strcmp(p->cmd, cmd))
205                         continue;
206                 exit(p->fn(argc, argv, envp));
207         }
208 }
209
210 int main(int argc, const char **argv, char **envp)
211 {
212         const char *cmd = argv[0];
213         char *slash = strrchr(cmd, '/');
214         const char *exec_path = NULL;
215         int done_alias = 0;
216
217         /*
218          * Take the basename of argv[0] as the command
219          * name, and the dirname as the default exec_path
220          * if it's an absolute path and we don't have
221          * anything better.
222          */
223         if (slash) {
224                 *slash++ = 0;
225                 if (*cmd == '/')
226                         exec_path = cmd;
227                 cmd = slash;
228         }
229
230         /*
231          * "git-xxxx" is the same as "git xxxx", but we obviously:
232          *
233          *  - cannot take flags in between the "git" and the "xxxx".
234          *  - cannot execute it externally (since it would just do
235          *    the same thing over again)
236          *
237          * So we just directly call the internal command handler, and
238          * die if that one cannot handle it.
239          */
240         if (!strncmp(cmd, "git-", 4)) {
241                 cmd += 4;
242                 argv[0] = cmd;
243                 handle_internal_command(argc, argv, envp);
244                 die("cannot handle %s internally", cmd);
245         }
246
247         /* Default command: "help" */
248         cmd = "help";
249
250         /* Look for flags.. */
251         while (argc > 1) {
252                 cmd = *++argv;
253                 argc--;
254
255                 if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) {
256                         setup_pager();
257                         continue;
258                 }
259
260                 if (strncmp(cmd, "--", 2))
261                         break;
262
263                 cmd += 2;
264
265                 /*
266                  * For legacy reasons, the "version" and "help"
267                  * commands can be written with "--" prepended
268                  * to make them look like flags.
269                  */
270                 if (!strcmp(cmd, "help"))
271                         break;
272                 if (!strcmp(cmd, "version"))
273                         break;
274
275                 /*
276                  * Check remaining flags (which by now must be
277                  * "--exec-path", but maybe we will accept
278                  * other arguments some day)
279                  */
280                 if (!strncmp(cmd, "exec-path", 9)) {
281                         cmd += 9;
282                         if (*cmd == '=') {
283                                 git_set_exec_path(cmd + 1);
284                                 continue;
285                         }
286                         puts(git_exec_path());
287                         exit(0);
288                 }
289                 cmd_usage(0, NULL, NULL);
290         }
291         argv[0] = cmd;
292
293         /*
294          * We search for git commands in the following order:
295          *  - git_exec_path()
296          *  - the path of the "git" command if we could find it
297          *    in $0
298          *  - the regular PATH.
299          */
300         if (exec_path)
301                 prepend_to_path(exec_path, strlen(exec_path));
302         exec_path = git_exec_path();
303         prepend_to_path(exec_path, strlen(exec_path));
304
305         while (1) {
306                 /* See if it's an internal command */
307                 handle_internal_command(argc, argv, envp);
308
309                 /* .. then try the external ones */
310                 execv_git_cmd(argv);
311
312                 /* It could be an alias -- this works around the insanity
313                  * of overriding "git log" with "git show" by having
314                  * alias.log = show
315                  */
316                 if (done_alias || !handle_alias(&argc, &argv))
317                         break;
318                 done_alias = 1;
319         }
320
321         if (errno == ENOENT)
322                 cmd_usage(0, exec_path, "'%s' is not a git-command", cmd);
323
324         fprintf(stderr, "Failed to run command '%s': %s\n",
325                 cmd, strerror(errno));
326
327         return 1;
328 }