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