]>
Commit | Line | Data |
---|---|---|
35eb2d36 LT |
1 | #include "cache.h" |
2 | #include "quote.h" | |
77cb17e9 | 3 | #include "exec_cmd.h" |
0c696fe7 | 4 | #include "strbuf.h" |
daa0cc9a | 5 | #include "builtin.h" |
35eb2d36 LT |
6 | |
7 | static int do_generic_cmd(const char *me, char *arg) | |
8 | { | |
9 | const char *my_argv[4]; | |
10 | ||
e1464ca7 | 11 | setup_path(); |
ab5f8627 | 12 | if (!arg || !(arg = sq_dequote(arg))) |
35eb2d36 | 13 | die("bad argument"); |
cc44c765 | 14 | if (prefixcmp(me, "git-")) |
77cb17e9 | 15 | die("bad command"); |
35eb2d36 | 16 | |
77cb17e9 | 17 | my_argv[0] = me + 4; |
35eb2d36 LT |
18 | my_argv[1] = arg; |
19 | my_argv[2] = NULL; | |
20 | ||
9201c707 | 21 | return execv_git_cmd(my_argv); |
35eb2d36 LT |
22 | } |
23 | ||
0c696fe7 JS |
24 | static int do_cvs_cmd(const char *me, char *arg) |
25 | { | |
26 | const char *cvsserver_argv[3] = { | |
27 | "cvsserver", "server", NULL | |
28 | }; | |
0c696fe7 JS |
29 | |
30 | if (!arg || strcmp(arg, "server")) | |
31 | die("git-cvsserver only handles server: %s", arg); | |
32 | ||
e1464ca7 | 33 | setup_path(); |
0c696fe7 JS |
34 | return execv_git_cmd(cvsserver_argv); |
35 | } | |
36 | ||
37 | ||
35eb2d36 LT |
38 | static struct commands { |
39 | const char *name; | |
40 | int (*exec)(const char *me, char *arg); | |
41 | } cmd_list[] = { | |
42 | { "git-receive-pack", do_generic_cmd }, | |
43 | { "git-upload-pack", do_generic_cmd }, | |
0c696fe7 | 44 | { "cvs", do_cvs_cmd }, |
35eb2d36 LT |
45 | { NULL }, |
46 | }; | |
47 | ||
daa0cc9a | 48 | int cmd_shell(int argc, const char **argv, const char *prefix) |
35eb2d36 LT |
49 | { |
50 | char *prog; | |
51 | struct commands *cmd; | |
52 | ||
bc7c73e2 JH |
53 | /* |
54 | * Special hack to pretend to be a CVS server | |
55 | */ | |
0c696fe7 JS |
56 | if (argc == 2 && !strcmp(argv[1], "cvs server")) |
57 | argv--; | |
bc7c73e2 JH |
58 | |
59 | /* | |
60 | * We do not accept anything but "-c" followed by "cmd arg", | |
61 | * where "cmd" is a very limited subset of git commands. | |
62 | */ | |
0c696fe7 | 63 | else if (argc != 3 || strcmp(argv[1], "-c")) |
35eb2d36 LT |
64 | die("What do you think I am? A shell?"); |
65 | ||
daa0cc9a | 66 | prog = xstrdup(argv[2]); |
bc7c73e2 JH |
67 | if (!strncmp(prog, "git", 3) && isspace(prog[3])) |
68 | /* Accept "git foo" as if the caller said "git-foo". */ | |
69 | prog[3] = '-'; | |
70 | ||
35eb2d36 LT |
71 | for (cmd = cmd_list ; cmd->name ; cmd++) { |
72 | int len = strlen(cmd->name); | |
73 | char *arg; | |
74 | if (strncmp(cmd->name, prog, len)) | |
75 | continue; | |
76 | arg = NULL; | |
77 | switch (prog[len]) { | |
78 | case '\0': | |
79 | arg = NULL; | |
80 | break; | |
81 | case ' ': | |
82 | arg = prog + len + 1; | |
83 | break; | |
84 | default: | |
85 | continue; | |
86 | } | |
87 | exit(cmd->exec(cmd->name, arg)); | |
88 | } | |
89 | die("unrecognized command '%s'", prog); | |
90 | } |