]> git.ipfire.org Git - thirdparty/git.git/blame - transport-helper.c
Move WebDAV HTTP push under remote-curl
[thirdparty/git.git] / transport-helper.c
CommitLineData
6eb996b5
DB
1#include "cache.h"
2#include "transport.h"
ae4efe19 3#include "quote.h"
6eb996b5
DB
4#include "run-command.h"
5#include "commit.h"
6#include "diff.h"
7#include "revision.h"
ef08ef9e 8#include "quote.h"
6eb996b5
DB
9
10struct helper_data
11{
12 const char *name;
13 struct child_process *helper;
292ce46b 14 FILE *out;
ef08ef9e 15 unsigned fetch : 1,
ae4efe19
SP
16 option : 1,
17 push : 1;
6eb996b5
DB
18};
19
20static struct child_process *get_helper(struct transport *transport)
21{
22 struct helper_data *data = transport->data;
23 struct strbuf buf = STRBUF_INIT;
24 struct child_process *helper;
6eb996b5
DB
25
26 if (data->helper)
27 return data->helper;
28
29 helper = xcalloc(1, sizeof(*helper));
30 helper->in = -1;
31 helper->out = -1;
32 helper->err = 0;
33 helper->argv = xcalloc(4, sizeof(*helper->argv));
34 strbuf_addf(&buf, "remote-%s", data->name);
35 helper->argv[0] = strbuf_detach(&buf, NULL);
36 helper->argv[1] = transport->remote->name;
37 helper->argv[2] = transport->url;
38 helper->git_cmd = 1;
39 if (start_command(helper))
40 die("Unable to run helper: git %s", helper->argv[0]);
41 data->helper = helper;
42
3d913526 43 write_str_in_full(helper->in, "capabilities\n");
2d14d65c 44
292ce46b 45 data->out = xfdopen(helper->out, "r");
6eb996b5 46 while (1) {
292ce46b 47 if (strbuf_getline(&buf, data->out, '\n') == EOF)
6eb996b5
DB
48 exit(128); /* child died, message supplied already */
49
50 if (!*buf.buf)
51 break;
52 if (!strcmp(buf.buf, "fetch"))
53 data->fetch = 1;
ef08ef9e
SP
54 if (!strcmp(buf.buf, "option"))
55 data->option = 1;
ae4efe19
SP
56 if (!strcmp(buf.buf, "push"))
57 data->push = 1;
6eb996b5
DB
58 }
59 return data->helper;
60}
61
62static int disconnect_helper(struct transport *transport)
63{
64 struct helper_data *data = transport->data;
65 if (data->helper) {
3d913526 66 write_str_in_full(data->helper->in, "\n");
6eb996b5 67 close(data->helper->in);
292ce46b 68 fclose(data->out);
6eb996b5
DB
69 finish_command(data->helper);
70 free((char *)data->helper->argv[0]);
71 free(data->helper->argv);
72 free(data->helper);
73 data->helper = NULL;
74 }
ef08ef9e 75 free(data);
6eb996b5
DB
76 return 0;
77}
78
ef08ef9e
SP
79static const char *unsupported_options[] = {
80 TRANS_OPT_UPLOADPACK,
81 TRANS_OPT_RECEIVEPACK,
82 TRANS_OPT_THIN,
83 TRANS_OPT_KEEP
84 };
85static const char *boolean_options[] = {
86 TRANS_OPT_THIN,
87 TRANS_OPT_KEEP,
88 TRANS_OPT_FOLLOWTAGS
89 };
90
91static int set_helper_option(struct transport *transport,
92 const char *name, const char *value)
93{
94 struct helper_data *data = transport->data;
95 struct child_process *helper = get_helper(transport);
96 struct strbuf buf = STRBUF_INIT;
97 int i, ret, is_bool = 0;
98
99 if (!data->option)
100 return 1;
101
102 for (i = 0; i < ARRAY_SIZE(unsupported_options); i++) {
103 if (!strcmp(name, unsupported_options[i]))
104 return 1;
105 }
106
107 for (i = 0; i < ARRAY_SIZE(boolean_options); i++) {
108 if (!strcmp(name, boolean_options[i])) {
109 is_bool = 1;
110 break;
111 }
112 }
113
114 strbuf_addf(&buf, "option %s ", name);
115 if (is_bool)
116 strbuf_addstr(&buf, value ? "true" : "false");
117 else
118 quote_c_style(value, &buf, NULL, 0);
119 strbuf_addch(&buf, '\n');
120
121 if (write_in_full(helper->in, buf.buf, buf.len) != buf.len)
122 die_errno("cannot send option to %s", data->name);
123
124 strbuf_reset(&buf);
125 if (strbuf_getline(&buf, data->out, '\n') == EOF)
126 exit(128); /* child died, message supplied already */
127
128 if (!strcmp(buf.buf, "ok"))
129 ret = 0;
130 else if (!prefixcmp(buf.buf, "error")) {
131 ret = -1;
132 } else if (!strcmp(buf.buf, "unsupported"))
133 ret = 1;
134 else {
135 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
136 ret = 1;
137 }
138 strbuf_release(&buf);
139 return ret;
140}
141
142static void standard_options(struct transport *t)
143{
144 char buf[16];
145 int n;
146 int v = t->verbose;
147 int no_progress = v < 0 || (!t->progress && !isatty(1));
148
149 set_helper_option(t, "progress", !no_progress ? "true" : "false");
150
151 n = snprintf(buf, sizeof(buf), "%d", v + 1);
152 if (n >= sizeof(buf))
153 die("impossibly large verbosity value");
154 set_helper_option(t, "verbosity", buf);
155}
156
6eb996b5
DB
157static int fetch_with_fetch(struct transport *transport,
158 int nr_heads, const struct ref **to_fetch)
159{
292ce46b 160 struct helper_data *data = transport->data;
6eb996b5
DB
161 int i;
162 struct strbuf buf = STRBUF_INIT;
163
ef08ef9e
SP
164 standard_options(transport);
165
6eb996b5 166 for (i = 0; i < nr_heads; i++) {
1088261f 167 const struct ref *posn = to_fetch[i];
6eb996b5
DB
168 if (posn->status & REF_STATUS_UPTODATE)
169 continue;
2d14d65c
DB
170
171 strbuf_addf(&buf, "fetch %s %s\n",
172 sha1_to_hex(posn->old_sha1), posn->name);
292ce46b 173 }
2d14d65c 174
292ce46b
SP
175 strbuf_addch(&buf, '\n');
176 if (write_in_full(data->helper->in, buf.buf, buf.len) != buf.len)
177 die_errno("cannot send fetch to %s", data->name);
178
179 while (1) {
180 strbuf_reset(&buf);
181 if (strbuf_getline(&buf, data->out, '\n') == EOF)
6eb996b5 182 exit(128); /* child died, message supplied already */
292ce46b
SP
183
184 if (!prefixcmp(buf.buf, "lock ")) {
185 const char *name = buf.buf + 5;
186 if (transport->pack_lockfile)
187 warning("%s also locked %s", data->name, name);
188 else
189 transport->pack_lockfile = xstrdup(name);
190 }
191 else if (!buf.len)
192 break;
193 else
194 warning("%s unexpectedly said: '%s'", data->name, buf.buf);
6eb996b5 195 }
292ce46b 196 strbuf_release(&buf);
6eb996b5
DB
197 return 0;
198}
199
200static int fetch(struct transport *transport,
201 int nr_heads, const struct ref **to_fetch)
202{
203 struct helper_data *data = transport->data;
204 int i, count;
205
206 count = 0;
207 for (i = 0; i < nr_heads; i++)
208 if (!(to_fetch[i]->status & REF_STATUS_UPTODATE))
209 count++;
210
211 if (!count)
212 return 0;
213
214 if (data->fetch)
215 return fetch_with_fetch(transport, nr_heads, to_fetch);
216
217 return -1;
218}
219
ae4efe19
SP
220static int push_refs(struct transport *transport,
221 struct ref *remote_refs, int flags)
222{
223 int force_all = flags & TRANSPORT_PUSH_FORCE;
224 int mirror = flags & TRANSPORT_PUSH_MIRROR;
225 struct helper_data *data = transport->data;
226 struct strbuf buf = STRBUF_INIT;
227 struct child_process *helper;
228 struct ref *ref;
229
230 if (!remote_refs)
231 return 0;
232
233 helper = get_helper(transport);
234 if (!data->push)
235 return 1;
236
237 for (ref = remote_refs; ref; ref = ref->next) {
238 if (ref->peer_ref)
239 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
240 else if (!mirror)
241 continue;
242
243 ref->deletion = is_null_sha1(ref->new_sha1);
244 if (!ref->deletion &&
245 !hashcmp(ref->old_sha1, ref->new_sha1)) {
246 ref->status = REF_STATUS_UPTODATE;
247 continue;
248 }
249
250 if (force_all)
251 ref->force = 1;
252
253 strbuf_addstr(&buf, "push ");
254 if (!ref->deletion) {
255 if (ref->force)
256 strbuf_addch(&buf, '+');
257 if (ref->peer_ref)
258 strbuf_addstr(&buf, ref->peer_ref->name);
259 else
260 strbuf_addstr(&buf, sha1_to_hex(ref->new_sha1));
261 }
262 strbuf_addch(&buf, ':');
263 strbuf_addstr(&buf, ref->name);
264 strbuf_addch(&buf, '\n');
265 }
266
267 transport->verbose = flags & TRANSPORT_PUSH_VERBOSE ? 1 : 0;
268 standard_options(transport);
269
270 if (flags & TRANSPORT_PUSH_DRY_RUN) {
271 if (set_helper_option(transport, "dry-run", "true") != 0)
272 die("helper %s does not support dry-run", data->name);
273 }
274
275 strbuf_addch(&buf, '\n');
276 if (write_in_full(helper->in, buf.buf, buf.len) != buf.len)
277 exit(128);
278
279 ref = remote_refs;
280 while (1) {
281 char *refname, *msg;
282 int status;
283
284 strbuf_reset(&buf);
285 if (strbuf_getline(&buf, data->out, '\n') == EOF)
286 exit(128); /* child died, message supplied already */
287 if (!buf.len)
288 break;
289
290 if (!prefixcmp(buf.buf, "ok ")) {
291 status = REF_STATUS_OK;
292 refname = buf.buf + 3;
293 } else if (!prefixcmp(buf.buf, "error ")) {
294 status = REF_STATUS_REMOTE_REJECT;
295 refname = buf.buf + 6;
296 } else
297 die("expected ok/error, helper said '%s'\n", buf.buf);
298
299 msg = strchr(refname, ' ');
300 if (msg) {
301 struct strbuf msg_buf = STRBUF_INIT;
302 const char *end;
303
304 *msg++ = '\0';
305 if (!unquote_c_style(&msg_buf, msg, &end))
306 msg = strbuf_detach(&msg_buf, NULL);
307 else
308 msg = xstrdup(msg);
309 strbuf_release(&msg_buf);
310
311 if (!strcmp(msg, "no match")) {
312 status = REF_STATUS_NONE;
313 free(msg);
314 msg = NULL;
315 }
316 else if (!strcmp(msg, "up to date")) {
317 status = REF_STATUS_UPTODATE;
318 free(msg);
319 msg = NULL;
320 }
321 else if (!strcmp(msg, "non-fast forward")) {
322 status = REF_STATUS_REJECT_NONFASTFORWARD;
323 free(msg);
324 msg = NULL;
325 }
326 }
327
328 if (ref)
329 ref = find_ref_by_name(ref, refname);
330 if (!ref)
331 ref = find_ref_by_name(remote_refs, refname);
332 if (!ref) {
333 warning("helper reported unexpected status of %s", refname);
334 continue;
335 }
336
337 ref->status = status;
338 ref->remote_status = msg;
339 }
340 strbuf_release(&buf);
341 return 0;
342}
343
6eb996b5
DB
344static struct ref *get_refs_list(struct transport *transport, int for_push)
345{
292ce46b 346 struct helper_data *data = transport->data;
6eb996b5
DB
347 struct child_process *helper;
348 struct ref *ret = NULL;
349 struct ref **tail = &ret;
350 struct ref *posn;
351 struct strbuf buf = STRBUF_INIT;
6eb996b5
DB
352
353 helper = get_helper(transport);
2d14d65c 354
ae4efe19
SP
355 if (data->push && for_push)
356 write_str_in_full(helper->in, "list for-push\n");
357 else
358 write_str_in_full(helper->in, "list\n");
6eb996b5 359
6eb996b5
DB
360 while (1) {
361 char *eov, *eon;
292ce46b 362 if (strbuf_getline(&buf, data->out, '\n') == EOF)
6eb996b5
DB
363 exit(128); /* child died, message supplied already */
364
365 if (!*buf.buf)
366 break;
367
368 eov = strchr(buf.buf, ' ');
369 if (!eov)
370 die("Malformed response in ref list: %s", buf.buf);
371 eon = strchr(eov + 1, ' ');
372 *eov = '\0';
373 if (eon)
374 *eon = '\0';
375 *tail = alloc_ref(eov + 1);
376 if (buf.buf[0] == '@')
377 (*tail)->symref = xstrdup(buf.buf + 1);
378 else if (buf.buf[0] != '?')
379 get_sha1_hex(buf.buf, (*tail)->old_sha1);
380 tail = &((*tail)->next);
381 }
382 strbuf_release(&buf);
383
384 for (posn = ret; posn; posn = posn->next)
385 resolve_remote_symref(posn, ret);
386
387 return ret;
388}
389
c9e388bb 390int transport_helper_init(struct transport *transport, const char *name)
6eb996b5
DB
391{
392 struct helper_data *data = xcalloc(sizeof(*data), 1);
c9e388bb 393 data->name = name;
6eb996b5
DB
394
395 transport->data = data;
ef08ef9e 396 transport->set_option = set_helper_option;
6eb996b5
DB
397 transport->get_refs_list = get_refs_list;
398 transport->fetch = fetch;
ae4efe19 399 transport->push_refs = push_refs;
6eb996b5
DB
400 transport->disconnect = disconnect_helper;
401 return 0;
402}