]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/remote-ext.c
typofix: assorted typofixes in comments, documentation and messages
[thirdparty/git.git] / builtin / remote-ext.c
CommitLineData
c2e86add 1#include "builtin.h"
7f3ecebf
IL
2#include "transport.h"
3#include "run-command.h"
df1ed03a 4#include "pkt-line.h"
7f3ecebf
IL
5
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).
23 */
24
25static char *git_req;
26static char *git_req_vhost;
27
28static char *strip_escapes(const char *str, const char *service,
29 const char **next)
30{
31 size_t rpos = 0;
32 int escape = 0;
33 char special = 0;
e3f1da98 34 const char *service_noprefix = service;
7f3ecebf
IL
35 struct strbuf ret = STRBUF_INIT;
36
e3f1da98 37 skip_prefix(service_noprefix, "git-", &service_noprefix);
7f3ecebf
IL
38
39 /* Pass the service to command. */
40 setenv("GIT_EXT_SERVICE", service, 1);
e3f1da98 41 setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix, 1);
7f3ecebf
IL
42
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]);
61 }
62 escape = 0;
63 } else
64 escape = (str[rpos] == '%');
65 rpos++;
66 }
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 */
72
73 /*
74 * Do the actual placeholder substitution. The string will be short
75 * enough not to overflow integers.
76 */
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':
e3f1da98 87 strbuf_addstr(&ret, service_noprefix);
7f3ecebf
IL
88 break;
89 case 'S':
90 strbuf_addstr(&ret, service);
91 break;
92 }
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;
102 }
103 rpos++;
104 }
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);
114 }
115}
116
850d2fec 117static void parse_argv(struct argv_array *out, const char *arg, const char *service)
7f3ecebf 118{
7f3ecebf 119 while (*arg) {
850d2fec 120 char *expanded = strip_escapes(arg, service, &arg);
7f3ecebf 121 if (expanded)
850d2fec
JK
122 argv_array_push(out, expanded);
123 free(expanded);
7f3ecebf 124 }
7f3ecebf
IL
125}
126
127static void send_git_request(int stdin_fd, const char *serv, const char *repo,
128 const char *vhost)
129{
df1ed03a
JK
130 if (!vhost)
131 packet_write(stdin_fd, "%s %s%c", serv, repo, 0);
7f3ecebf 132 else
df1ed03a
JK
133 packet_write(stdin_fd, "%s %s%chost=%s%c", serv, repo, 0,
134 vhost, 0);
7f3ecebf
IL
135}
136
137static int run_child(const char *arg, const char *service)
138{
139 int r;
d3180279 140 struct child_process child = CHILD_PROCESS_INIT;
7f3ecebf 141
7f3ecebf
IL
142 child.in = -1;
143 child.out = -1;
144 child.err = 0;
850d2fec 145 parse_argv(&child.args, arg, service);
7f3ecebf
IL
146
147 if (start_command(&child) < 0)
148 die("Can't run specified command");
149
150 if (git_req)
151 send_git_request(child.in, service, git_req, git_req_vhost);
152
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;
159}
160
161#define MAXCOMMAND 4096
162
163static int command_loop(const char *child)
164{
165 char buffer[MAXCOMMAND];
166
167 while (1) {
60a2e332 168 size_t i;
7f3ecebf
IL
169 if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
170 if (ferror(stdin))
832c0e5e 171 die("Command input error");
7f3ecebf
IL
172 exit(0);
173 }
174 /* Strip end of line characters. */
60a2e332
JN
175 i = strlen(buffer);
176 while (i > 0 && isspace(buffer[i - 1]))
177 buffer[--i] = 0;
7f3ecebf
IL
178
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;
189 }
190 }
191}
192
193int cmd_remote_ext(int argc, const char **argv, const char *prefix)
194{
7851b1e6
IL
195 if (argc != 3)
196 die("Expected two arguments");
7f3ecebf
IL
197
198 return command_loop(argv[2]);
199}