]>
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" |
575ba9d6 | 14 | #include "quote.h" |
8e49d503 | 15 | |
70827b15 | 16 | #include "builtin.h" |
8e49d503 AE |
17 | |
18 | static void prepend_to_path(const char *dir, int len) | |
19 | { | |
554fe20d TH |
20 | const char *old_path = getenv("PATH"); |
21 | char *path; | |
8e49d503 AE |
22 | int path_len = len; |
23 | ||
24 | if (!old_path) | |
7dbc2c04 | 25 | old_path = "/usr/local/bin:/usr/bin:/bin"; |
8e49d503 AE |
26 | |
27 | path_len = len + strlen(old_path) + 1; | |
28 | ||
29 | path = malloc(path_len + 1); | |
8e49d503 AE |
30 | |
31 | memcpy(path, dir, len); | |
32 | path[len] = ':'; | |
33 | memcpy(path + len + 1, old_path, path_len - len); | |
34 | ||
35 | setenv("PATH", path, 1); | |
36 | } | |
37 | ||
4ab243a9 JS |
38 | static int handle_options(const char*** argv, int* argc) |
39 | { | |
40 | int handled = 0; | |
41 | ||
42 | while (*argc > 0) { | |
43 | const char *cmd = (*argv)[0]; | |
44 | if (cmd[0] != '-') | |
45 | break; | |
46 | ||
6acbcb92 JS |
47 | /* |
48 | * For legacy reasons, the "version" and "help" | |
49 | * commands can be written with "--" prepended | |
50 | * to make them look like flags. | |
51 | */ | |
52 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) | |
53 | break; | |
54 | ||
55 | /* | |
56 | * Check remaining flags. | |
57 | */ | |
58 | if (!strncmp(cmd, "--exec-path", 11)) { | |
59 | cmd += 11; | |
60 | if (*cmd == '=') | |
61 | git_set_exec_path(cmd + 1); | |
62 | else { | |
63 | puts(git_exec_path()); | |
64 | exit(0); | |
65 | } | |
66 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { | |
4ab243a9 | 67 | setup_pager(); |
6acbcb92 JS |
68 | } else if (!strcmp(cmd, "--git-dir")) { |
69 | if (*argc < 1) | |
70 | return -1; | |
71 | setenv("GIT_DIR", (*argv)[1], 1); | |
72 | (*argv)++; | |
73 | (*argc)--; | |
74 | } else if (!strncmp(cmd, "--git-dir=", 10)) { | |
75 | setenv("GIT_DIR", cmd + 10, 1); | |
76 | } else if (!strcmp(cmd, "--bare")) { | |
77 | static char git_dir[1024]; | |
78 | setenv("GIT_DIR", getcwd(git_dir, 1024), 1); | |
79 | } else { | |
80 | fprintf(stderr, "Unknown option: %s\n", cmd); | |
81 | cmd_usage(0, NULL, NULL); | |
82 | } | |
4ab243a9 JS |
83 | |
84 | (*argv)++; | |
85 | (*argc)--; | |
86 | handled++; | |
87 | } | |
88 | return handled; | |
89 | } | |
90 | ||
2b11e317 JS |
91 | static const char *alias_command; |
92 | static char *alias_string = NULL; | |
93 | ||
94 | static int git_alias_config(const char *var, const char *value) | |
95 | { | |
96 | if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) { | |
97 | alias_string = strdup(value); | |
98 | } | |
99 | return 0; | |
100 | } | |
101 | ||
102 | static int split_cmdline(char *cmdline, const char ***argv) | |
103 | { | |
104 | int src, dst, count = 0, size = 16; | |
105 | char quoted = 0; | |
106 | ||
107 | *argv = malloc(sizeof(char*) * size); | |
108 | ||
109 | /* split alias_string */ | |
110 | (*argv)[count++] = cmdline; | |
111 | for (src = dst = 0; cmdline[src];) { | |
112 | char c = cmdline[src]; | |
113 | if (!quoted && isspace(c)) { | |
114 | cmdline[dst++] = 0; | |
115 | while (cmdline[++src] | |
116 | && isspace(cmdline[src])) | |
117 | ; /* skip */ | |
118 | if (count >= size) { | |
119 | size += 16; | |
120 | *argv = realloc(*argv, sizeof(char*) * size); | |
121 | } | |
122 | (*argv)[count++] = cmdline + dst; | |
123 | } else if(!quoted && (c == '\'' || c == '"')) { | |
124 | quoted = c; | |
125 | src++; | |
126 | } else if (c == quoted) { | |
127 | quoted = 0; | |
128 | src++; | |
129 | } else { | |
130 | if (c == '\\' && quoted != '\'') { | |
131 | src++; | |
132 | c = cmdline[src]; | |
133 | if (!c) { | |
134 | free(*argv); | |
135 | *argv = NULL; | |
136 | return error("cmdline ends with \\"); | |
137 | } | |
138 | } | |
139 | cmdline[dst++] = c; | |
140 | src++; | |
141 | } | |
142 | } | |
143 | ||
144 | cmdline[dst] = 0; | |
145 | ||
146 | if (quoted) { | |
147 | free(*argv); | |
148 | *argv = NULL; | |
149 | return error("unclosed quote"); | |
150 | } | |
151 | ||
152 | return count; | |
153 | } | |
154 | ||
155 | static int handle_alias(int *argcp, const char ***argv) | |
156 | { | |
47e5c0ca | 157 | int nongit = 0, ret = 0, saved_errno = errno; |
2b11e317 JS |
158 | const char *subdir; |
159 | ||
160 | subdir = setup_git_directory_gently(&nongit); | |
161 | if (!nongit) { | |
4ab243a9 | 162 | int count, option_count; |
2b11e317 JS |
163 | const char** new_argv; |
164 | ||
165 | alias_command = (*argv)[0]; | |
166 | git_config(git_alias_config); | |
167 | if (alias_string) { | |
168 | ||
169 | count = split_cmdline(alias_string, &new_argv); | |
4ab243a9 JS |
170 | option_count = handle_options(&new_argv, &count); |
171 | memmove(new_argv - option_count, new_argv, | |
172 | count * sizeof(char *)); | |
173 | new_argv -= option_count; | |
2b11e317 JS |
174 | |
175 | if (count < 1) | |
176 | die("empty alias for %s", alias_command); | |
177 | ||
178 | if (!strcmp(alias_command, new_argv[0])) | |
179 | die("recursive alias: %s", alias_command); | |
180 | ||
575ba9d6 ML |
181 | if (getenv("GIT_TRACE")) { |
182 | int i; | |
183 | fprintf(stderr, "trace: alias expansion: %s =>", | |
184 | alias_command); | |
185 | for (i = 0; i < count; ++i) { | |
186 | fputc(' ', stderr); | |
187 | sq_quote_print(stderr, new_argv[i]); | |
188 | } | |
189 | fputc('\n', stderr); | |
190 | fflush(stderr); | |
191 | } | |
192 | ||
d5b9e6cf ML |
193 | new_argv = realloc(new_argv, sizeof(char*) * |
194 | (count + *argcp + 1)); | |
2b11e317 | 195 | /* insert after command name */ |
d5b9e6cf ML |
196 | memcpy(new_argv + count, *argv + 1, |
197 | sizeof(char*) * *argcp); | |
198 | new_argv[count+*argcp] = NULL; | |
2b11e317 JS |
199 | |
200 | *argv = new_argv; | |
201 | *argcp += count - 1; | |
202 | ||
203 | ret = 1; | |
204 | } | |
205 | } | |
206 | ||
207 | if (subdir) | |
208 | chdir(subdir); | |
209 | ||
47e5c0ca JS |
210 | errno = saved_errno; |
211 | ||
2b11e317 JS |
212 | return ret; |
213 | } | |
214 | ||
70827b15 | 215 | const char git_version_string[] = GIT_VERSION; |
5b84f4d8 | 216 | |
9201c707 | 217 | static void handle_internal_command(int argc, const char **argv, char **envp) |
231af832 LT |
218 | { |
219 | const char *cmd = argv[0]; | |
220 | static struct cmd_struct { | |
221 | const char *cmd; | |
9201c707 | 222 | int (*fn)(int, const char **, char **); |
231af832 LT |
223 | } commands[] = { |
224 | { "version", cmd_version }, | |
225 | { "help", cmd_help }, | |
70b006b9 | 226 | { "log", cmd_log }, |
70827b15 | 227 | { "whatchanged", cmd_whatchanged }, |
ba1d4505 | 228 | { "show", cmd_show }, |
755225de | 229 | { "push", cmd_push }, |
68563738 | 230 | { "format-patch", cmd_format_patch }, |
c7432087 | 231 | { "count-objects", cmd_count_objects }, |
8ab99476 | 232 | { "diff", cmd_diff }, |
5010cb5f | 233 | { "grep", cmd_grep }, |
d9b814cc | 234 | { "rm", cmd_rm }, |
0d781539 | 235 | { "add", cmd_add }, |
5fb61b8d | 236 | { "rev-list", cmd_rev_list }, |
c3c8835f | 237 | { "init-db", cmd_init_db }, |
52ba03cb | 238 | { "get-tar-commit-id", cmd_get_tar_commit_id }, |
21754264 | 239 | { "upload-tar", cmd_upload_tar }, |
0864f264 | 240 | { "check-ref-format", cmd_check_ref_format }, |
aae01bda | 241 | { "ls-files", cmd_ls_files }, |
56d1398a | 242 | { "ls-tree", cmd_ls_tree }, |
d147e501 | 243 | { "tar-tree", cmd_tar_tree }, |
6d96ac18 | 244 | { "read-tree", cmd_read_tree }, |
ac6245e3 | 245 | { "commit-tree", cmd_commit_tree }, |
51ce34b9 | 246 | { "apply", cmd_apply }, |
e8cc9cd9 PE |
247 | { "show-branch", cmd_show_branch }, |
248 | { "diff-files", cmd_diff_files }, | |
249 | { "diff-index", cmd_diff_index }, | |
250 | { "diff-stages", cmd_diff_stages }, | |
f81daefe | 251 | { "diff-tree", cmd_diff_tree }, |
895f10c3 | 252 | { "cat-file", cmd_cat_file }, |
8ed05fb5 | 253 | { "rev-parse", cmd_rev_parse }, |
e690e843 | 254 | { "write-tree", cmd_write_tree }, |
34488e3c | 255 | { "mailsplit", cmd_mailsplit }, |
7499c996 | 256 | { "mailinfo", cmd_mailinfo }, |
fefe81c9 | 257 | { "stripspace", cmd_stripspace }, |
854b4629 | 258 | { "update-index", cmd_update_index }, |
00449f99 | 259 | { "update-ref", cmd_update_ref }, |
ba84a797 LT |
260 | { "fmt-merge-msg", cmd_fmt_merge_msg }, |
261 | { "prune", cmd_prune }, | |
231af832 LT |
262 | }; |
263 | int i; | |
264 | ||
1cd95087 LT |
265 | /* Turn "git cmd --help" into "git help cmd" */ |
266 | if (argc > 1 && !strcmp(argv[1], "--help")) { | |
267 | argv[1] = argv[0]; | |
268 | argv[0] = cmd = "help"; | |
269 | } | |
270 | ||
231af832 LT |
271 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
272 | struct cmd_struct *p = commands+i; | |
273 | if (strcmp(p->cmd, cmd)) | |
274 | continue; | |
575ba9d6 ML |
275 | |
276 | if (getenv("GIT_TRACE")) { | |
277 | int i; | |
278 | fprintf(stderr, "trace: built-in: git"); | |
279 | for (i = 0; i < argc; ++i) { | |
280 | fputc(' ', stderr); | |
281 | sq_quote_print(stderr, argv[i]); | |
282 | } | |
283 | putc('\n', stderr); | |
284 | fflush(stderr); | |
285 | } | |
286 | ||
231af832 LT |
287 | exit(p->fn(argc, argv, envp)); |
288 | } | |
289 | } | |
290 | ||
9201c707 | 291 | int main(int argc, const char **argv, char **envp) |
8e49d503 | 292 | { |
9201c707 | 293 | const char *cmd = argv[0]; |
231af832 | 294 | char *slash = strrchr(cmd, '/'); |
231af832 | 295 | const char *exec_path = NULL; |
a025463b | 296 | int done_alias = 0; |
231af832 LT |
297 | |
298 | /* | |
299 | * Take the basename of argv[0] as the command | |
300 | * name, and the dirname as the default exec_path | |
301 | * if it's an absolute path and we don't have | |
302 | * anything better. | |
303 | */ | |
304 | if (slash) { | |
305 | *slash++ = 0; | |
306 | if (*cmd == '/') | |
307 | exec_path = cmd; | |
308 | cmd = slash; | |
309 | } | |
8e49d503 | 310 | |
231af832 LT |
311 | /* |
312 | * "git-xxxx" is the same as "git xxxx", but we obviously: | |
313 | * | |
314 | * - cannot take flags in between the "git" and the "xxxx". | |
315 | * - cannot execute it externally (since it would just do | |
316 | * the same thing over again) | |
317 | * | |
318 | * So we just directly call the internal command handler, and | |
319 | * die if that one cannot handle it. | |
320 | */ | |
321 | if (!strncmp(cmd, "git-", 4)) { | |
322 | cmd += 4; | |
323 | argv[0] = cmd; | |
324 | handle_internal_command(argc, argv, envp); | |
325 | die("cannot handle %s internally", cmd); | |
326 | } | |
8e49d503 | 327 | |
231af832 | 328 | /* Look for flags.. */ |
6acbcb92 JS |
329 | argv++; |
330 | argc--; | |
331 | handle_options(&argv, &argc); | |
332 | if (argc > 0) { | |
333 | if (!strncmp(argv[0], "--", 2)) | |
334 | argv[0] += 2; | |
335 | } else { | |
336 | /* Default command: "help" */ | |
337 | argv[0] = "help"; | |
338 | argc = 1; | |
97fc6c5f | 339 | } |
6acbcb92 | 340 | cmd = argv[0]; |
231af832 LT |
341 | |
342 | /* | |
343 | * We search for git commands in the following order: | |
344 | * - git_exec_path() | |
345 | * - the path of the "git" command if we could find it | |
346 | * in $0 | |
347 | * - the regular PATH. | |
348 | */ | |
349 | if (exec_path) | |
350 | prepend_to_path(exec_path, strlen(exec_path)); | |
77cb17e9 MO |
351 | exec_path = git_exec_path(); |
352 | prepend_to_path(exec_path, strlen(exec_path)); | |
8e49d503 | 353 | |
a025463b JH |
354 | while (1) { |
355 | /* See if it's an internal command */ | |
356 | handle_internal_command(argc, argv, envp); | |
2b11e317 | 357 | |
a025463b JH |
358 | /* .. then try the external ones */ |
359 | execv_git_cmd(argv); | |
231af832 | 360 | |
a025463b JH |
361 | /* It could be an alias -- this works around the insanity |
362 | * of overriding "git log" with "git show" by having | |
363 | * alias.log = show | |
364 | */ | |
365 | if (done_alias || !handle_alias(&argc, &argv)) | |
366 | break; | |
367 | done_alias = 1; | |
368 | } | |
10b15b86 AR |
369 | |
370 | if (errno == ENOENT) | |
a87cd02c | 371 | cmd_usage(0, exec_path, "'%s' is not a git-command", cmd); |
10b15b86 AR |
372 | |
373 | fprintf(stderr, "Failed to run command '%s': %s\n", | |
33ebb871 | 374 | cmd, strerror(errno)); |
8e49d503 AE |
375 | |
376 | return 1; | |
377 | } |