Commit | Line | Data |
---|---|---|
8e49d503 | 1 | #include <stdio.h> |
7dbc2c04 JH |
2 | #include <sys/types.h> |
3 | #include <sys/stat.h> | |
4 | #include <dirent.h> | |
8e49d503 AE |
5 | #include <unistd.h> |
6 | #include <stdlib.h> | |
7 | #include <string.h> | |
8 | #include <errno.h> | |
9 | #include <limits.h> | |
10 | #include <stdarg.h> | |
4050c0df | 11 | #include "git-compat-util.h" |
77cb17e9 | 12 | #include "exec_cmd.h" |
2b11e317 | 13 | #include "cache.h" |
8e49d503 | 14 | |
70827b15 | 15 | #include "builtin.h" |
8e49d503 AE |
16 | |
17 | static void prepend_to_path(const char *dir, int len) | |
18 | { | |
554fe20d TH |
19 | const char *old_path = getenv("PATH"); |
20 | char *path; | |
8e49d503 AE |
21 | int path_len = len; |
22 | ||
23 | if (!old_path) | |
7dbc2c04 | 24 | old_path = "/usr/local/bin:/usr/bin:/bin"; |
8e49d503 AE |
25 | |
26 | path_len = len + strlen(old_path) + 1; | |
27 | ||
28 | path = malloc(path_len + 1); | |
8e49d503 AE |
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 | ||
2b11e317 JS |
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; | |
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*) * | |
d8498500 JH |
126 | (count + *argcp)); |
127 | memcpy(new_argv + count, *argv + 1, | |
128 | sizeof(char*) * *argcp); | |
2b11e317 JS |
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 | return ret; | |
142 | } | |
143 | ||
70827b15 | 144 | const char git_version_string[] = GIT_VERSION; |
5b84f4d8 | 145 | |
9201c707 | 146 | static void handle_internal_command(int argc, const char **argv, char **envp) |
231af832 LT |
147 | { |
148 | const char *cmd = argv[0]; | |
149 | static struct cmd_struct { | |
150 | const char *cmd; | |
9201c707 | 151 | int (*fn)(int, const char **, char **); |
231af832 LT |
152 | } commands[] = { |
153 | { "version", cmd_version }, | |
154 | { "help", cmd_help }, | |
70b006b9 | 155 | { "log", cmd_log }, |
70827b15 | 156 | { "whatchanged", cmd_whatchanged }, |
ba1d4505 | 157 | { "show", cmd_show }, |
755225de | 158 | { "push", cmd_push }, |
68563738 | 159 | { "format-patch", cmd_format_patch }, |
c7432087 | 160 | { "count-objects", cmd_count_objects }, |
8ab99476 | 161 | { "diff", cmd_diff }, |
5010cb5f | 162 | { "grep", cmd_grep }, |
d9b814cc | 163 | { "rm", cmd_rm }, |
0d781539 | 164 | { "add", cmd_add }, |
5fb61b8d | 165 | { "rev-list", cmd_rev_list }, |
c3c8835f | 166 | { "init-db", cmd_init_db }, |
52ba03cb | 167 | { "get-tar-commit-id", cmd_get_tar_commit_id }, |
21754264 | 168 | { "upload-tar", cmd_upload_tar }, |
0864f264 | 169 | { "check-ref-format", cmd_check_ref_format }, |
aae01bda | 170 | { "ls-files", cmd_ls_files }, |
56d1398a | 171 | { "ls-tree", cmd_ls_tree }, |
d147e501 | 172 | { "tar-tree", cmd_tar_tree }, |
6d96ac18 | 173 | { "read-tree", cmd_read_tree }, |
ac6245e3 | 174 | { "commit-tree", cmd_commit_tree }, |
51ce34b9 | 175 | { "apply", cmd_apply }, |
e8cc9cd9 PE |
176 | { "show-branch", cmd_show_branch }, |
177 | { "diff-files", cmd_diff_files }, | |
178 | { "diff-index", cmd_diff_index }, | |
179 | { "diff-stages", cmd_diff_stages }, | |
f81daefe | 180 | { "diff-tree", cmd_diff_tree }, |
895f10c3 | 181 | { "cat-file", cmd_cat_file }, |
8ed05fb5 | 182 | { "rev-parse", cmd_rev_parse }, |
e690e843 | 183 | { "write-tree", cmd_write_tree }, |
34488e3c | 184 | { "mailsplit", cmd_mailsplit }, |
7499c996 | 185 | { "mailinfo", cmd_mailinfo }, |
fefe81c9 | 186 | { "stripspace", cmd_stripspace }, |
854b4629 LS |
187 | { "update-index", cmd_update_index }, |
188 | { "update-ref", cmd_update_ref } | |
231af832 LT |
189 | }; |
190 | int i; | |
191 | ||
1cd95087 LT |
192 | /* Turn "git cmd --help" into "git help cmd" */ |
193 | if (argc > 1 && !strcmp(argv[1], "--help")) { | |
194 | argv[1] = argv[0]; | |
195 | argv[0] = cmd = "help"; | |
196 | } | |
197 | ||
231af832 LT |
198 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
199 | struct cmd_struct *p = commands+i; | |
200 | if (strcmp(p->cmd, cmd)) | |
201 | continue; | |
202 | exit(p->fn(argc, argv, envp)); | |
203 | } | |
204 | } | |
205 | ||
9201c707 | 206 | int main(int argc, const char **argv, char **envp) |
8e49d503 | 207 | { |
9201c707 | 208 | const char *cmd = argv[0]; |
231af832 | 209 | char *slash = strrchr(cmd, '/'); |
231af832 | 210 | const char *exec_path = NULL; |
a025463b | 211 | int done_alias = 0; |
231af832 LT |
212 | |
213 | /* | |
214 | * Take the basename of argv[0] as the command | |
215 | * name, and the dirname as the default exec_path | |
216 | * if it's an absolute path and we don't have | |
217 | * anything better. | |
218 | */ | |
219 | if (slash) { | |
220 | *slash++ = 0; | |
221 | if (*cmd == '/') | |
222 | exec_path = cmd; | |
223 | cmd = slash; | |
224 | } | |
8e49d503 | 225 | |
231af832 LT |
226 | /* |
227 | * "git-xxxx" is the same as "git xxxx", but we obviously: | |
228 | * | |
229 | * - cannot take flags in between the "git" and the "xxxx". | |
230 | * - cannot execute it externally (since it would just do | |
231 | * the same thing over again) | |
232 | * | |
233 | * So we just directly call the internal command handler, and | |
234 | * die if that one cannot handle it. | |
235 | */ | |
236 | if (!strncmp(cmd, "git-", 4)) { | |
237 | cmd += 4; | |
238 | argv[0] = cmd; | |
239 | handle_internal_command(argc, argv, envp); | |
240 | die("cannot handle %s internally", cmd); | |
241 | } | |
8e49d503 | 242 | |
231af832 LT |
243 | /* Default command: "help" */ |
244 | cmd = "help"; | |
8e49d503 | 245 | |
231af832 LT |
246 | /* Look for flags.. */ |
247 | while (argc > 1) { | |
248 | cmd = *++argv; | |
249 | argc--; | |
da6bf70e | 250 | |
231af832 | 251 | if (strncmp(cmd, "--", 2)) |
8e49d503 AE |
252 | break; |
253 | ||
231af832 LT |
254 | cmd += 2; |
255 | ||
256 | /* | |
257 | * For legacy reasons, the "version" and "help" | |
258 | * commands can be written with "--" prepended | |
259 | * to make them look like flags. | |
260 | */ | |
261 | if (!strcmp(cmd, "help")) | |
262 | break; | |
263 | if (!strcmp(cmd, "version")) | |
264 | break; | |
8e49d503 | 265 | |
231af832 LT |
266 | /* |
267 | * Check remaining flags (which by now must be | |
268 | * "--exec-path", but maybe we will accept | |
269 | * other arguments some day) | |
270 | */ | |
271 | if (!strncmp(cmd, "exec-path", 9)) { | |
272 | cmd += 9; | |
273 | if (*cmd == '=') { | |
274 | git_set_exec_path(cmd + 1); | |
275 | continue; | |
8e49d503 | 276 | } |
231af832 | 277 | puts(git_exec_path()); |
8e49d503 AE |
278 | exit(0); |
279 | } | |
a87cd02c | 280 | cmd_usage(0, NULL, NULL); |
97fc6c5f | 281 | } |
231af832 LT |
282 | argv[0] = cmd; |
283 | ||
284 | /* | |
285 | * We search for git commands in the following order: | |
286 | * - git_exec_path() | |
287 | * - the path of the "git" command if we could find it | |
288 | * in $0 | |
289 | * - the regular PATH. | |
290 | */ | |
291 | if (exec_path) | |
292 | prepend_to_path(exec_path, strlen(exec_path)); | |
77cb17e9 MO |
293 | exec_path = git_exec_path(); |
294 | prepend_to_path(exec_path, strlen(exec_path)); | |
8e49d503 | 295 | |
a025463b JH |
296 | while (1) { |
297 | /* See if it's an internal command */ | |
298 | handle_internal_command(argc, argv, envp); | |
2b11e317 | 299 | |
a025463b JH |
300 | /* .. then try the external ones */ |
301 | execv_git_cmd(argv); | |
231af832 | 302 | |
a025463b JH |
303 | /* It could be an alias -- this works around the insanity |
304 | * of overriding "git log" with "git show" by having | |
305 | * alias.log = show | |
306 | */ | |
307 | if (done_alias || !handle_alias(&argc, &argv)) | |
308 | break; | |
309 | done_alias = 1; | |
310 | } | |
10b15b86 AR |
311 | |
312 | if (errno == ENOENT) | |
a87cd02c | 313 | cmd_usage(0, exec_path, "'%s' is not a git-command", cmd); |
10b15b86 AR |
314 | |
315 | fprintf(stderr, "Failed to run command '%s': %s\n", | |
33ebb871 | 316 | cmd, strerror(errno)); |
8e49d503 AE |
317 | |
318 | return 1; | |
319 | } |