]> git.ipfire.org Git - thirdparty/git.git/blame - shell.c
Makefile: Include subdirectories in "make cover" reports
[thirdparty/git.git] / shell.c
CommitLineData
35eb2d36
LT
1#include "cache.h"
2#include "quote.h"
77cb17e9 3#include "exec_cmd.h"
0c696fe7 4#include "strbuf.h"
35eb2d36
LT
5
6static int do_generic_cmd(const char *me, char *arg)
7{
8 const char *my_argv[4];
9
e1464ca7 10 setup_path();
ab5f8627 11 if (!arg || !(arg = sq_dequote(arg)))
35eb2d36 12 die("bad argument");
cc44c765 13 if (prefixcmp(me, "git-"))
77cb17e9 14 die("bad command");
35eb2d36 15
77cb17e9 16 my_argv[0] = me + 4;
35eb2d36
LT
17 my_argv[1] = arg;
18 my_argv[2] = NULL;
19
9201c707 20 return execv_git_cmd(my_argv);
35eb2d36
LT
21}
22
0c696fe7
JS
23static int do_cvs_cmd(const char *me, char *arg)
24{
25 const char *cvsserver_argv[3] = {
26 "cvsserver", "server", NULL
27 };
0c696fe7
JS
28
29 if (!arg || strcmp(arg, "server"))
30 die("git-cvsserver only handles server: %s", arg);
31
e1464ca7 32 setup_path();
0c696fe7
JS
33 return execv_git_cmd(cvsserver_argv);
34}
35
36
35eb2d36
LT
37static struct commands {
38 const char *name;
39 int (*exec)(const char *me, char *arg);
40} cmd_list[] = {
41 { "git-receive-pack", do_generic_cmd },
42 { "git-upload-pack", do_generic_cmd },
79f72b97 43 { "git-upload-archive", do_generic_cmd },
0c696fe7 44 { "cvs", do_cvs_cmd },
35eb2d36
LT
45 { NULL },
46};
47
1e7abc59 48int main(int argc, char **argv)
35eb2d36
LT
49{
50 char *prog;
51 struct commands *cmd;
0cfeed2e
PB
52 int devnull_fd;
53
54 /*
55 * Always open file descriptors 0/1/2 to avoid clobbering files
56 * in die(). It also avoids not messing up when the pipes are
57 * dup'ed onto stdin/stdout/stderr in the child processes we spawn.
58 */
59 devnull_fd = open("/dev/null", O_RDWR);
60 while (devnull_fd >= 0 && devnull_fd <= 2)
61 devnull_fd = dup(devnull_fd);
62 if (devnull_fd == -1)
d824cbba 63 die_errno("opening /dev/null failed");
0cfeed2e 64 close (devnull_fd);
35eb2d36 65
bc7c73e2
JH
66 /*
67 * Special hack to pretend to be a CVS server
68 */
0c696fe7
JS
69 if (argc == 2 && !strcmp(argv[1], "cvs server"))
70 argv--;
bc7c73e2
JH
71
72 /*
73 * We do not accept anything but "-c" followed by "cmd arg",
74 * where "cmd" is a very limited subset of git commands.
75 */
0c696fe7 76 else if (argc != 3 || strcmp(argv[1], "-c"))
35eb2d36
LT
77 die("What do you think I am? A shell?");
78
1e7abc59 79 prog = argv[2];
bc7c73e2
JH
80 if (!strncmp(prog, "git", 3) && isspace(prog[3]))
81 /* Accept "git foo" as if the caller said "git-foo". */
82 prog[3] = '-';
83
35eb2d36
LT
84 for (cmd = cmd_list ; cmd->name ; cmd++) {
85 int len = strlen(cmd->name);
86 char *arg;
87 if (strncmp(cmd->name, prog, len))
88 continue;
89 arg = NULL;
90 switch (prog[len]) {
91 case '\0':
92 arg = NULL;
93 break;
94 case ' ':
95 arg = prog + len + 1;
96 break;
97 default:
98 continue;
99 }
100 exit(cmd->exec(cmd->name, arg));
101 }
102 die("unrecognized command '%s'", prog);
103}