]>
Commit | Line | Data |
---|---|---|
85023577 | 1 | #include "builtin.h" |
77cb17e9 | 2 | #include "exec_cmd.h" |
2b11e317 | 3 | #include "cache.h" |
575ba9d6 | 4 | #include "quote.h" |
8e49d503 | 5 | |
822a7d50 | 6 | const char git_usage_string[] = |
892c41b9 | 7 | "git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]"; |
822a7d50 | 8 | |
8e49d503 AE |
9 | static void prepend_to_path(const char *dir, int len) |
10 | { | |
554fe20d TH |
11 | const char *old_path = getenv("PATH"); |
12 | char *path; | |
8e49d503 AE |
13 | int path_len = len; |
14 | ||
15 | if (!old_path) | |
7dbc2c04 | 16 | old_path = "/usr/local/bin:/usr/bin:/bin"; |
8e49d503 AE |
17 | |
18 | path_len = len + strlen(old_path) + 1; | |
19 | ||
2d7320d0 | 20 | path = xmalloc(path_len + 1); |
8e49d503 AE |
21 | |
22 | memcpy(path, dir, len); | |
23 | path[len] = ':'; | |
24 | memcpy(path + len + 1, old_path, path_len - len); | |
25 | ||
26 | setenv("PATH", path, 1); | |
0caea90b CC |
27 | |
28 | free(path); | |
8e49d503 AE |
29 | } |
30 | ||
4ab243a9 JS |
31 | static int handle_options(const char*** argv, int* argc) |
32 | { | |
33 | int handled = 0; | |
34 | ||
35 | while (*argc > 0) { | |
36 | const char *cmd = (*argv)[0]; | |
37 | if (cmd[0] != '-') | |
38 | break; | |
39 | ||
6acbcb92 JS |
40 | /* |
41 | * For legacy reasons, the "version" and "help" | |
42 | * commands can be written with "--" prepended | |
43 | * to make them look like flags. | |
44 | */ | |
45 | if (!strcmp(cmd, "--help") || !strcmp(cmd, "--version")) | |
46 | break; | |
47 | ||
48 | /* | |
49 | * Check remaining flags. | |
50 | */ | |
cc44c765 | 51 | if (!prefixcmp(cmd, "--exec-path")) { |
6acbcb92 JS |
52 | cmd += 11; |
53 | if (*cmd == '=') | |
54 | git_set_exec_path(cmd + 1); | |
55 | else { | |
56 | puts(git_exec_path()); | |
57 | exit(0); | |
58 | } | |
59 | } else if (!strcmp(cmd, "-p") || !strcmp(cmd, "--paginate")) { | |
4ab243a9 | 60 | setup_pager(); |
6acbcb92 | 61 | } else if (!strcmp(cmd, "--git-dir")) { |
c321f00d BG |
62 | if (*argc < 2) { |
63 | fprintf(stderr, "No directory given for --git-dir.\n" ); | |
64 | usage(git_usage_string); | |
65 | } | |
45b09794 | 66 | setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1); |
6acbcb92 JS |
67 | (*argv)++; |
68 | (*argc)--; | |
e4b02333 | 69 | handled++; |
cc44c765 | 70 | } else if (!prefixcmp(cmd, "--git-dir=")) { |
45b09794 | 71 | setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1); |
892c41b9 ML |
72 | } else if (!strcmp(cmd, "--work-tree")) { |
73 | if (*argc < 2) { | |
74 | fprintf(stderr, "No directory given for --work-tree.\n" ); | |
75 | usage(git_usage_string); | |
76 | } | |
77 | setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1); | |
78 | (*argv)++; | |
79 | (*argc)--; | |
80 | } else if (!prefixcmp(cmd, "--work-tree=")) { | |
81 | setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1); | |
6acbcb92 | 82 | } else if (!strcmp(cmd, "--bare")) { |
ef5ddb2f | 83 | static char git_dir[PATH_MAX+1]; |
45b09794 | 84 | setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1); |
6acbcb92 JS |
85 | } else { |
86 | fprintf(stderr, "Unknown option: %s\n", cmd); | |
822a7d50 | 87 | usage(git_usage_string); |
6acbcb92 | 88 | } |
4ab243a9 JS |
89 | |
90 | (*argv)++; | |
91 | (*argc)--; | |
92 | handled++; | |
93 | } | |
94 | return handled; | |
95 | } | |
96 | ||
2b11e317 | 97 | static const char *alias_command; |
96f1e58f | 98 | static char *alias_string; |
2b11e317 JS |
99 | |
100 | static int git_alias_config(const char *var, const char *value) | |
101 | { | |
cc44c765 | 102 | if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) { |
9befac47 | 103 | alias_string = xstrdup(value); |
2b11e317 JS |
104 | } |
105 | return 0; | |
106 | } | |
107 | ||
108 | static int split_cmdline(char *cmdline, const char ***argv) | |
109 | { | |
110 | int src, dst, count = 0, size = 16; | |
111 | char quoted = 0; | |
112 | ||
3301521a | 113 | *argv = xmalloc(sizeof(char*) * size); |
2b11e317 JS |
114 | |
115 | /* split alias_string */ | |
116 | (*argv)[count++] = cmdline; | |
117 | for (src = dst = 0; cmdline[src];) { | |
118 | char c = cmdline[src]; | |
119 | if (!quoted && isspace(c)) { | |
120 | cmdline[dst++] = 0; | |
121 | while (cmdline[++src] | |
122 | && isspace(cmdline[src])) | |
123 | ; /* skip */ | |
124 | if (count >= size) { | |
125 | size += 16; | |
83572c1a | 126 | *argv = xrealloc(*argv, sizeof(char*) * size); |
2b11e317 JS |
127 | } |
128 | (*argv)[count++] = cmdline + dst; | |
129 | } else if(!quoted && (c == '\'' || c == '"')) { | |
130 | quoted = c; | |
131 | src++; | |
132 | } else if (c == quoted) { | |
133 | quoted = 0; | |
134 | src++; | |
135 | } else { | |
136 | if (c == '\\' && quoted != '\'') { | |
137 | src++; | |
138 | c = cmdline[src]; | |
139 | if (!c) { | |
140 | free(*argv); | |
141 | *argv = NULL; | |
142 | return error("cmdline ends with \\"); | |
143 | } | |
144 | } | |
145 | cmdline[dst++] = c; | |
146 | src++; | |
147 | } | |
148 | } | |
149 | ||
150 | cmdline[dst] = 0; | |
151 | ||
152 | if (quoted) { | |
153 | free(*argv); | |
154 | *argv = NULL; | |
155 | return error("unclosed quote"); | |
156 | } | |
157 | ||
158 | return count; | |
159 | } | |
160 | ||
161 | static int handle_alias(int *argcp, const char ***argv) | |
162 | { | |
47e5c0ca | 163 | int nongit = 0, ret = 0, saved_errno = errno; |
2b11e317 | 164 | const char *subdir; |
0347a8c7 ML |
165 | int count, option_count; |
166 | const char** new_argv; | |
2b11e317 JS |
167 | |
168 | subdir = setup_git_directory_gently(&nongit); | |
0347a8c7 ML |
169 | |
170 | alias_command = (*argv)[0]; | |
171 | git_config(git_alias_config); | |
172 | if (alias_string) { | |
dfd42a3c TT |
173 | if (alias_string[0] == '!') { |
174 | trace_printf("trace: alias to shell cmd: %s => %s\n", | |
175 | alias_command, alias_string + 1); | |
176 | ret = system(alias_string + 1); | |
177 | if (ret >= 0 && WIFEXITED(ret) && | |
178 | WEXITSTATUS(ret) != 127) | |
179 | exit(WEXITSTATUS(ret)); | |
180 | die("Failed to run '%s' when expanding alias '%s'\n", | |
181 | alias_string + 1, alias_command); | |
182 | } | |
0347a8c7 ML |
183 | count = split_cmdline(alias_string, &new_argv); |
184 | option_count = handle_options(&new_argv, &count); | |
185 | memmove(new_argv - option_count, new_argv, | |
186 | count * sizeof(char *)); | |
187 | new_argv -= option_count; | |
188 | ||
189 | if (count < 1) | |
190 | die("empty alias for %s", alias_command); | |
191 | ||
192 | if (!strcmp(alias_command, new_argv[0])) | |
193 | die("recursive alias: %s", alias_command); | |
194 | ||
6ce4e61f CC |
195 | trace_argv_printf(new_argv, count, |
196 | "trace: alias expansion: %s =>", | |
197 | alias_command); | |
575ba9d6 | 198 | |
83572c1a JF |
199 | new_argv = xrealloc(new_argv, sizeof(char*) * |
200 | (count + *argcp + 1)); | |
0347a8c7 ML |
201 | /* insert after command name */ |
202 | memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp); | |
203 | new_argv[count+*argcp] = NULL; | |
2b11e317 | 204 | |
0347a8c7 ML |
205 | *argv = new_argv; |
206 | *argcp += count - 1; | |
2b11e317 | 207 | |
0347a8c7 | 208 | ret = 1; |
2b11e317 JS |
209 | } |
210 | ||
211 | if (subdir) | |
212 | chdir(subdir); | |
213 | ||
47e5c0ca JS |
214 | errno = saved_errno; |
215 | ||
2b11e317 JS |
216 | return ret; |
217 | } | |
218 | ||
70827b15 | 219 | const char git_version_string[] = GIT_VERSION; |
5b84f4d8 | 220 | |
efffea03 JH |
221 | #define RUN_SETUP (1<<0) |
222 | #define USE_PAGER (1<<1) | |
7eff28a9 SP |
223 | /* |
224 | * require working tree to be present -- anything uses this needs | |
225 | * RUN_SETUP for reading from the configuration file. | |
226 | */ | |
227 | #define NOT_BARE (1<<2) | |
a633fca0 | 228 | |
9201c707 | 229 | static void handle_internal_command(int argc, const char **argv, char **envp) |
231af832 LT |
230 | { |
231 | const char *cmd = argv[0]; | |
232 | static struct cmd_struct { | |
233 | const char *cmd; | |
a633fca0 | 234 | int (*fn)(int, const char **, const char *); |
9590b041 | 235 | int option; |
231af832 | 236 | } commands[] = { |
7eff28a9 | 237 | { "add", cmd_add, RUN_SETUP | NOT_BARE }, |
5b6dedd6 | 238 | { "annotate", cmd_annotate, RUN_SETUP | USE_PAGER }, |
f754fa9c | 239 | { "apply", cmd_apply }, |
265d5280 | 240 | { "archive", cmd_archive }, |
4f0219a4 | 241 | { "blame", cmd_blame, RUN_SETUP }, |
1da1b3a3 | 242 | { "branch", cmd_branch, RUN_SETUP }, |
2e0afafe | 243 | { "bundle", cmd_bundle }, |
efffea03 JH |
244 | { "cat-file", cmd_cat_file, RUN_SETUP }, |
245 | { "checkout-index", cmd_checkout_index, RUN_SETUP }, | |
0864f264 | 246 | { "check-ref-format", cmd_check_ref_format }, |
d0bfd026 | 247 | { "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE }, |
e827633a | 248 | { "cherry", cmd_cherry, RUN_SETUP }, |
9509af68 | 249 | { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE }, |
efffea03 | 250 | { "commit-tree", cmd_commit_tree, RUN_SETUP }, |
e0d10e1c | 251 | { "config", cmd_config }, |
8112894d | 252 | { "count-objects", cmd_count_objects, RUN_SETUP }, |
9a0eaf83 | 253 | { "describe", cmd_describe, RUN_SETUP }, |
d516c2d1 JS |
254 | { "diff", cmd_diff, USE_PAGER }, |
255 | { "diff-files", cmd_diff_files }, | |
efffea03 | 256 | { "diff-index", cmd_diff_index, RUN_SETUP }, |
efffea03 | 257 | { "diff-tree", cmd_diff_tree, RUN_SETUP }, |
d4289fff | 258 | { "fetch--tool", cmd_fetch__tool, RUN_SETUP }, |
efffea03 | 259 | { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, |
9f613ddd | 260 | { "for-each-ref", cmd_for_each_ref, RUN_SETUP }, |
efffea03 | 261 | { "format-patch", cmd_format_patch, RUN_SETUP }, |
b4dfefe0 MW |
262 | { "fsck", cmd_fsck, RUN_SETUP }, |
263 | { "fsck-objects", cmd_fsck, RUN_SETUP }, | |
6757ada4 | 264 | { "gc", cmd_gc, RUN_SETUP }, |
f754fa9c | 265 | { "get-tar-commit-id", cmd_get_tar_commit_id }, |
34c6a82b | 266 | { "grep", cmd_grep, RUN_SETUP | USE_PAGER }, |
f754fa9c | 267 | { "help", cmd_help }, |
515377ea | 268 | { "init", cmd_init_db }, |
f754fa9c | 269 | { "init-db", cmd_init_db }, |
efffea03 JH |
270 | { "log", cmd_log, RUN_SETUP | USE_PAGER }, |
271 | { "ls-files", cmd_ls_files, RUN_SETUP }, | |
272 | { "ls-tree", cmd_ls_tree, RUN_SETUP }, | |
f754fa9c JH |
273 | { "mailinfo", cmd_mailinfo }, |
274 | { "mailsplit", cmd_mailsplit }, | |
71dfbf22 | 275 | { "merge-base", cmd_merge_base, RUN_SETUP }, |
ba1f5f35 | 276 | { "merge-file", cmd_merge_file }, |
7eff28a9 | 277 | { "mv", cmd_mv, RUN_SETUP | NOT_BARE }, |
efffea03 JH |
278 | { "name-rev", cmd_name_rev, RUN_SETUP }, |
279 | { "pack-objects", cmd_pack_objects, RUN_SETUP }, | |
acca687f | 280 | { "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER }, |
efffea03 JH |
281 | { "prune", cmd_prune, RUN_SETUP }, |
282 | { "prune-packed", cmd_prune_packed, RUN_SETUP }, | |
102cb085 | 283 | { "push", cmd_push, RUN_SETUP }, |
efffea03 | 284 | { "read-tree", cmd_read_tree, RUN_SETUP }, |
4264dc15 | 285 | { "reflog", cmd_reflog, RUN_SETUP }, |
e0d10e1c | 286 | { "repo-config", cmd_config }, |
658f3650 | 287 | { "rerere", cmd_rerere, RUN_SETUP }, |
efffea03 JH |
288 | { "rev-list", cmd_rev_list, RUN_SETUP }, |
289 | { "rev-parse", cmd_rev_parse, RUN_SETUP }, | |
9509af68 | 290 | { "revert", cmd_revert, RUN_SETUP | NOT_BARE }, |
7eff28a9 SP |
291 | { "rm", cmd_rm, RUN_SETUP | NOT_BARE }, |
292 | { "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE }, | |
1d541c12 | 293 | { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER }, |
efffea03 JH |
294 | { "show-branch", cmd_show_branch, RUN_SETUP }, |
295 | { "show", cmd_show, RUN_SETUP | USE_PAGER }, | |
f754fa9c | 296 | { "stripspace", cmd_stripspace }, |
efffea03 | 297 | { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, |
9cb90b80 | 298 | { "tar-tree", cmd_tar_tree }, |
efffea03 JH |
299 | { "unpack-objects", cmd_unpack_objects, RUN_SETUP }, |
300 | { "update-index", cmd_update_index, RUN_SETUP }, | |
301 | { "update-ref", cmd_update_ref, RUN_SETUP }, | |
39345a21 | 302 | { "upload-archive", cmd_upload_archive }, |
f754fa9c | 303 | { "version", cmd_version }, |
efffea03 JH |
304 | { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER }, |
305 | { "write-tree", cmd_write_tree, RUN_SETUP }, | |
2e3ed670 | 306 | { "verify-pack", cmd_verify_pack }, |
358ddb62 | 307 | { "show-ref", cmd_show_ref, RUN_SETUP }, |
e1e22e37 | 308 | { "pack-refs", cmd_pack_refs, RUN_SETUP }, |
231af832 LT |
309 | }; |
310 | int i; | |
311 | ||
1cd95087 LT |
312 | /* Turn "git cmd --help" into "git help cmd" */ |
313 | if (argc > 1 && !strcmp(argv[1], "--help")) { | |
314 | argv[1] = argv[0]; | |
315 | argv[0] = cmd = "help"; | |
316 | } | |
317 | ||
231af832 LT |
318 | for (i = 0; i < ARRAY_SIZE(commands); i++) { |
319 | struct cmd_struct *p = commands+i; | |
a633fca0 | 320 | const char *prefix; |
231af832 LT |
321 | if (strcmp(p->cmd, cmd)) |
322 | continue; | |
575ba9d6 | 323 | |
a633fca0 | 324 | prefix = NULL; |
efffea03 | 325 | if (p->option & RUN_SETUP) |
a633fca0 | 326 | prefix = setup_git_directory(); |
9590b041 JH |
327 | if (p->option & USE_PAGER) |
328 | setup_pager(); | |
6d9ba67b JS |
329 | if ((p->option & NOT_BARE) && |
330 | (is_bare_repository() || is_inside_git_dir())) | |
331 | die("%s must be run in a work tree", cmd); | |
6ce4e61f | 332 | trace_argv_printf(argv, argc, "trace: built-in: git"); |
575ba9d6 | 333 | |
a633fca0 | 334 | exit(p->fn(argc, argv, prefix)); |
231af832 LT |
335 | } |
336 | } | |
337 | ||
9201c707 | 338 | int main(int argc, const char **argv, char **envp) |
8e49d503 | 339 | { |
5b6df8e4 | 340 | const char *cmd = argv[0] ? argv[0] : "git-help"; |
231af832 | 341 | char *slash = strrchr(cmd, '/'); |
231af832 | 342 | const char *exec_path = NULL; |
a025463b | 343 | int done_alias = 0; |
231af832 LT |
344 | |
345 | /* | |
346 | * Take the basename of argv[0] as the command | |
347 | * name, and the dirname as the default exec_path | |
348 | * if it's an absolute path and we don't have | |
349 | * anything better. | |
350 | */ | |
351 | if (slash) { | |
352 | *slash++ = 0; | |
353 | if (*cmd == '/') | |
354 | exec_path = cmd; | |
355 | cmd = slash; | |
356 | } | |
8e49d503 | 357 | |
231af832 LT |
358 | /* |
359 | * "git-xxxx" is the same as "git xxxx", but we obviously: | |
360 | * | |
361 | * - cannot take flags in between the "git" and the "xxxx". | |
362 | * - cannot execute it externally (since it would just do | |
363 | * the same thing over again) | |
364 | * | |
365 | * So we just directly call the internal command handler, and | |
366 | * die if that one cannot handle it. | |
367 | */ | |
cc44c765 | 368 | if (!prefixcmp(cmd, "git-")) { |
231af832 LT |
369 | cmd += 4; |
370 | argv[0] = cmd; | |
371 | handle_internal_command(argc, argv, envp); | |
372 | die("cannot handle %s internally", cmd); | |
373 | } | |
8e49d503 | 374 | |
231af832 | 375 | /* Look for flags.. */ |
6acbcb92 JS |
376 | argv++; |
377 | argc--; | |
378 | handle_options(&argv, &argc); | |
379 | if (argc > 0) { | |
cc44c765 | 380 | if (!prefixcmp(argv[0], "--")) |
6acbcb92 JS |
381 | argv[0] += 2; |
382 | } else { | |
383 | /* Default command: "help" */ | |
384 | argv[0] = "help"; | |
385 | argc = 1; | |
97fc6c5f | 386 | } |
6acbcb92 | 387 | cmd = argv[0]; |
231af832 LT |
388 | |
389 | /* | |
390 | * We search for git commands in the following order: | |
391 | * - git_exec_path() | |
392 | * - the path of the "git" command if we could find it | |
393 | * in $0 | |
394 | * - the regular PATH. | |
395 | */ | |
396 | if (exec_path) | |
397 | prepend_to_path(exec_path, strlen(exec_path)); | |
77cb17e9 MO |
398 | exec_path = git_exec_path(); |
399 | prepend_to_path(exec_path, strlen(exec_path)); | |
8e49d503 | 400 | |
a025463b JH |
401 | while (1) { |
402 | /* See if it's an internal command */ | |
403 | handle_internal_command(argc, argv, envp); | |
2b11e317 | 404 | |
a025463b JH |
405 | /* .. then try the external ones */ |
406 | execv_git_cmd(argv); | |
231af832 | 407 | |
a025463b JH |
408 | /* It could be an alias -- this works around the insanity |
409 | * of overriding "git log" with "git show" by having | |
410 | * alias.log = show | |
411 | */ | |
412 | if (done_alias || !handle_alias(&argc, &argv)) | |
413 | break; | |
414 | done_alias = 1; | |
415 | } | |
10b15b86 | 416 | |
f3dd015c TT |
417 | if (errno == ENOENT) { |
418 | if (done_alias) { | |
419 | fprintf(stderr, "Expansion of alias '%s' failed; " | |
420 | "'%s' is not a git-command\n", | |
421 | cmd, argv[0]); | |
422 | exit(1); | |
423 | } | |
822a7d50 | 424 | help_unknown_cmd(cmd); |
f3dd015c | 425 | } |
10b15b86 AR |
426 | |
427 | fprintf(stderr, "Failed to run command '%s': %s\n", | |
33ebb871 | 428 | cmd, strerror(errno)); |
8e49d503 AE |
429 | |
430 | return 1; | |
431 | } |