cd3910afea0cc0e66e720413b2e03d8f00964ff6
1#include "builtin.h"
2#include "exec_cmd.h"
3#include "cache.h"
4#include "quote.h"
5
6const char git_usage_string[] =
7 "git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]";
8
9static void prepend_to_path(const char *dir, int len)
10{
11 const char *old_path = getenv("PATH");
12 char *path;
13 int path_len = len;
14
15 if (!old_path)
16 old_path = "/usr/local/bin:/usr/bin:/bin";
17
18 path_len = len + strlen(old_path) + 1;
19
20 path = xmalloc(path_len + 1);
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);
27
28 free(path);
29}
30
31static 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
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 */
51 if (!prefixcmp(cmd, "--exec-path")) {
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")) {
60 setup_pager();
61 } else if (!strcmp(cmd, "--git-dir")) {
62 if (*argc < 2) {
63 fprintf(stderr, "No directory given for --git-dir.\n" );
64 usage(git_usage_string);
65 }
66 setenv(GIT_DIR_ENVIRONMENT, (*argv)[1], 1);
67 (*argv)++;
68 (*argc)--;
69 handled++;
70 } else if (!prefixcmp(cmd, "--git-dir=")) {
71 setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
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);
82 } else if (!strcmp(cmd, "--bare")) {
83 static char git_dir[PATH_MAX+1];
84 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
85 } else {
86 fprintf(stderr, "Unknown option: %s\n", cmd);
87 usage(git_usage_string);
88 }
89
90 (*argv)++;
91 (*argc)--;
92 handled++;
93 }
94 return handled;
95}
96
97static const char *alias_command;
98static char *alias_string;
99
100static int git_alias_config(const char *var, const char *value)
101{
102 if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) {
103 alias_string = xstrdup(value);
104 }
105 return 0;
106}
107
108static int split_cmdline(char *cmdline, const char ***argv)
109{
110 int src, dst, count = 0, size = 16;
111 char quoted = 0;
112
113 *argv = xmalloc(sizeof(char*) * size);
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;
126 *argv = xrealloc(*argv, sizeof(char*) * size);
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
161static int handle_alias(int *argcp, const char ***argv)
162{
163 int nongit = 0, ret = 0, saved_errno = errno;
164 const char *subdir;
165 int count, option_count;
166 const char** new_argv;
167
168 subdir = setup_git_directory_gently(&nongit);
169
170 alias_command = (*argv)[0];
171 git_config(git_alias_config);
172 if (alias_string) {
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 }
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
195 trace_argv_printf(new_argv, count,
196 "trace: alias expansion: %s =>",
197 alias_command);
198
199 new_argv = xrealloc(new_argv, sizeof(char*) *
200 (count + *argcp + 1));
201 /* insert after command name */
202 memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
203 new_argv[count+*argcp] = NULL;
204
205 *argv = new_argv;
206 *argcp += count - 1;
207
208 ret = 1;
209 }
210
211 if (subdir)
212 chdir(subdir);
213
214 errno = saved_errno;
215
216 return ret;
217}
218
219const char git_version_string[] = GIT_VERSION;
220
221#define RUN_SETUP (1<<0)
222#define USE_PAGER (1<<1)
223/*
224 * require working tree to be present -- anything uses this needs
225 * RUN_SETUP for reading from the configuration file.
226 */
227#define NEED_WORK_TREE (1<<2)
228
229static void handle_internal_command(int argc, const char **argv, char **envp)
230{
231 const char *cmd = argv[0];
232 static struct cmd_struct {
233 const char *cmd;
234 int (*fn)(int, const char **, const char *);
235 int option;
236 } commands[] = {
237 { "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
238 { "annotate", cmd_annotate, RUN_SETUP | USE_PAGER },
239 { "apply", cmd_apply },
240 { "archive", cmd_archive },
241 { "blame", cmd_blame, RUN_SETUP },
242 { "branch", cmd_branch, RUN_SETUP },
243 { "bundle", cmd_bundle },
244 { "cat-file", cmd_cat_file, RUN_SETUP },
245 { "checkout-index", cmd_checkout_index, RUN_SETUP },
246 { "check-ref-format", cmd_check_ref_format },
247 { "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
248 { "cherry", cmd_cherry, RUN_SETUP },
249 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
250 { "commit-tree", cmd_commit_tree, RUN_SETUP },
251 { "config", cmd_config },
252 { "count-objects", cmd_count_objects, RUN_SETUP },
253 { "describe", cmd_describe, RUN_SETUP },
254 { "diff", cmd_diff, USE_PAGER },
255 { "diff-files", cmd_diff_files },
256 { "diff-index", cmd_diff_index, RUN_SETUP },
257 { "diff-tree", cmd_diff_tree, RUN_SETUP },
258 { "fetch--tool", cmd_fetch__tool, RUN_SETUP },
259 { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP },
260 { "for-each-ref", cmd_for_each_ref, RUN_SETUP },
261 { "format-patch", cmd_format_patch, RUN_SETUP },
262 { "fsck", cmd_fsck, RUN_SETUP },
263 { "fsck-objects", cmd_fsck, RUN_SETUP },
264 { "gc", cmd_gc, RUN_SETUP },
265 { "get-tar-commit-id", cmd_get_tar_commit_id },
266 { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
267 { "help", cmd_help },
268 { "init", cmd_init_db },
269 { "init-db", cmd_init_db },
270 { "log", cmd_log, RUN_SETUP | USE_PAGER },
271 { "ls-files", cmd_ls_files, RUN_SETUP },
272 { "ls-tree", cmd_ls_tree, RUN_SETUP },
273 { "mailinfo", cmd_mailinfo },
274 { "mailsplit", cmd_mailsplit },
275 { "merge-base", cmd_merge_base, RUN_SETUP },
276 { "merge-file", cmd_merge_file },
277 { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
278 { "name-rev", cmd_name_rev, RUN_SETUP },
279 { "pack-objects", cmd_pack_objects, RUN_SETUP },
280 { "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
281 { "prune", cmd_prune, RUN_SETUP },
282 { "prune-packed", cmd_prune_packed, RUN_SETUP },
283 { "push", cmd_push, RUN_SETUP },
284 { "read-tree", cmd_read_tree, RUN_SETUP },
285 { "reflog", cmd_reflog, RUN_SETUP },
286 { "repo-config", cmd_config },
287 { "rerere", cmd_rerere, RUN_SETUP },
288 { "rev-list", cmd_rev_list, RUN_SETUP },
289 { "rev-parse", cmd_rev_parse, RUN_SETUP },
290 { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
291 { "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
292 { "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
293 { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
294 { "show-branch", cmd_show_branch, RUN_SETUP },
295 { "show", cmd_show, RUN_SETUP | USE_PAGER },
296 { "stripspace", cmd_stripspace },
297 { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP },
298 { "tar-tree", cmd_tar_tree },
299 { "unpack-objects", cmd_unpack_objects, RUN_SETUP },
300 { "update-index", cmd_update_index, RUN_SETUP },
301 { "update-ref", cmd_update_ref, RUN_SETUP },
302 { "upload-archive", cmd_upload_archive },
303 { "version", cmd_version },
304 { "whatchanged", cmd_whatchanged, RUN_SETUP | USE_PAGER },
305 { "write-tree", cmd_write_tree, RUN_SETUP },
306 { "verify-pack", cmd_verify_pack },
307 { "show-ref", cmd_show_ref, RUN_SETUP },
308 { "pack-refs", cmd_pack_refs, RUN_SETUP },
309 };
310 int i;
311
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
318 for (i = 0; i < ARRAY_SIZE(commands); i++) {
319 struct cmd_struct *p = commands+i;
320 const char *prefix;
321 if (strcmp(p->cmd, cmd))
322 continue;
323
324 prefix = NULL;
325 if (p->option & RUN_SETUP)
326 prefix = setup_git_directory();
327 if (p->option & USE_PAGER)
328 setup_pager();
329 if ((p->option & NEED_WORK_TREE) &&
330 (!is_inside_work_tree() || is_inside_git_dir()))
331 die("%s must be run in a work tree", cmd);
332 trace_argv_printf(argv, argc, "trace: built-in: git");
333
334 exit(p->fn(argc, argv, prefix));
335 }
336}
337
338int main(int argc, const char **argv, char **envp)
339{
340 const char *cmd = argv[0] ? argv[0] : "git-help";
341 char *slash = strrchr(cmd, '/');
342 const char *exec_path = NULL;
343 int done_alias = 0;
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 }
357
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 */
368 if (!prefixcmp(cmd, "git-")) {
369 cmd += 4;
370 argv[0] = cmd;
371 handle_internal_command(argc, argv, envp);
372 die("cannot handle %s internally", cmd);
373 }
374
375 /* Look for flags.. */
376 argv++;
377 argc--;
378 handle_options(&argv, &argc);
379 if (argc > 0) {
380 if (!prefixcmp(argv[0], "--"))
381 argv[0] += 2;
382 } else {
383 /* Default command: "help" */
384 argv[0] = "help";
385 argc = 1;
386 }
387 cmd = argv[0];
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));
398 exec_path = git_exec_path();
399 prepend_to_path(exec_path, strlen(exec_path));
400
401 while (1) {
402 /* See if it's an internal command */
403 handle_internal_command(argc, argv, envp);
404
405 /* .. then try the external ones */
406 execv_git_cmd(argv);
407
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 }
416
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 }
424 help_unknown_cmd(cmd);
425 }
426
427 fprintf(stderr, "Failed to run command '%s': %s\n",
428 cmd, strerror(errno));
429
430 return 1;
431}