doc: describe git svn init --ignore-refs
[git/git-svn.git] / builtin / remote-ext.c
blob11b48bfb41193387d53588c879ee50431f77b9cd
1 #include "builtin.h"
2 #include "transport.h"
3 #include "run-command.h"
4 #include "pkt-line.h"
6 /*
7 * URL syntax:
8 * 'command [arg1 [arg2 [...]]]' Invoke command with given arguments.
9 * Special characters:
10 * '% ': Literal space in argument.
11 * '%%': Literal percent sign.
12 * '%S': Name of service (git-upload-pack/git-upload-archive/
13 * git-receive-pack.
14 * '%s': Same as \s, but with possible git- prefix stripped.
15 * '%G': Only allowed as first 'character' of argument. Do not pass this
16 * Argument to command, instead send this as name of repository
17 * in in-line git://-style request (also activates sending this
18 * style of request).
19 * '%V': Only allowed as first 'character' of argument. Used in
20 * conjunction with '%G': Do not pass this argument to command,
21 * instead send this as vhost in git://-style request (note: does
22 * not activate sending git:// style request).
25 static char *git_req;
26 static char *git_req_vhost;
28 static char *strip_escapes(const char *str, const char *service,
29 const char **next)
31 size_t rpos = 0;
32 int escape = 0;
33 char special = 0;
34 const char *service_noprefix = service;
35 struct strbuf ret = STRBUF_INIT;
37 skip_prefix(service_noprefix, "git-", &service_noprefix);
39 /* Pass the service to command. */
40 setenv("GIT_EXT_SERVICE", service, 1);
41 setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix, 1);
43 /* Scan the length of argument. */
44 while (str[rpos] && (escape || str[rpos] != ' ')) {
45 if (escape) {
46 switch (str[rpos]) {
47 case ' ':
48 case '%':
49 case 's':
50 case 'S':
51 break;
52 case 'G':
53 case 'V':
54 special = str[rpos];
55 if (rpos == 1)
56 break;
57 /* Fall-through to error. */
58 default:
59 die("Bad remote-ext placeholder '%%%c'.",
60 str[rpos]);
62 escape = 0;
63 } else
64 escape = (str[rpos] == '%');
65 rpos++;
67 if (escape && !str[rpos])
68 die("remote-ext command has incomplete placeholder");
69 *next = str + rpos;
70 if (**next == ' ')
71 ++*next; /* Skip over space */
74 * Do the actual placeholder substitution. The string will be short
75 * enough not to overflow integers.
77 rpos = special ? 2 : 0; /* Skip first 2 bytes in specials. */
78 escape = 0;
79 while (str[rpos] && (escape || str[rpos] != ' ')) {
80 if (escape) {
81 switch (str[rpos]) {
82 case ' ':
83 case '%':
84 strbuf_addch(&ret, str[rpos]);
85 break;
86 case 's':
87 strbuf_addstr(&ret, service_noprefix);
88 break;
89 case 'S':
90 strbuf_addstr(&ret, service);
91 break;
93 escape = 0;
94 } else
95 switch (str[rpos]) {
96 case '%':
97 escape = 1;
98 break;
99 default:
100 strbuf_addch(&ret, str[rpos]);
101 break;
103 rpos++;
105 switch (special) {
106 case 'G':
107 git_req = strbuf_detach(&ret, NULL);
108 return NULL;
109 case 'V':
110 git_req_vhost = strbuf_detach(&ret, NULL);
111 return NULL;
112 default:
113 return strbuf_detach(&ret, NULL);
117 static void parse_argv(struct argv_array *out, const char *arg, const char *service)
119 while (*arg) {
120 char *expanded = strip_escapes(arg, service, &arg);
121 if (expanded)
122 argv_array_push(out, expanded);
123 free(expanded);
127 static void send_git_request(int stdin_fd, const char *serv, const char *repo,
128 const char *vhost)
130 if (!vhost)
131 packet_write_fmt(stdin_fd, "%s %s%c", serv, repo, 0);
132 else
133 packet_write_fmt(stdin_fd, "%s %s%chost=%s%c", serv, repo, 0,
134 vhost, 0);
137 static int run_child(const char *arg, const char *service)
139 int r;
140 struct child_process child = CHILD_PROCESS_INIT;
142 child.in = -1;
143 child.out = -1;
144 child.err = 0;
145 parse_argv(&child.args, arg, service);
147 if (start_command(&child) < 0)
148 die("Can't run specified command");
150 if (git_req)
151 send_git_request(child.in, service, git_req, git_req_vhost);
153 r = bidirectional_transfer_loop(child.out, child.in);
154 if (!r)
155 r = finish_command(&child);
156 else
157 finish_command(&child);
158 return r;
161 #define MAXCOMMAND 4096
163 static int command_loop(const char *child)
165 char buffer[MAXCOMMAND];
167 while (1) {
168 size_t i;
169 if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
170 if (ferror(stdin))
171 die("Command input error");
172 exit(0);
174 /* Strip end of line characters. */
175 i = strlen(buffer);
176 while (i > 0 && isspace(buffer[i - 1]))
177 buffer[--i] = 0;
179 if (!strcmp(buffer, "capabilities")) {
180 printf("*connect\n\n");
181 fflush(stdout);
182 } else if (!strncmp(buffer, "connect ", 8)) {
183 printf("\n");
184 fflush(stdout);
185 return run_child(child, buffer + 8);
186 } else {
187 fprintf(stderr, "Bad command");
188 return 1;
193 int cmd_remote_ext(int argc, const char **argv, const char *prefix)
195 if (argc != 3)
196 die("Expected two arguments");
198 return command_loop(argv[2]);