3 #include "run-command.h"
7 * 'command [arg1 [arg2 [...]]]' Invoke command with given arguments.
9 * '% ': Literal space in argument.
10 * '%%': Literal percent sign.
11 * '%S': Name of service (git-upload-pack/git-upload-archive/
13 * '%s': Same as \s, but with possible git- prefix stripped.
14 * '%G': Only allowed as first 'character' of argument. Do not pass this
15 * Argument to command, instead send this as name of repository
16 * in in-line git://-style request (also activates sending this
18 * '%V': Only allowed as first 'character' of argument. Used in
19 * conjunction with '%G': Do not pass this argument to command,
20 * instead send this as vhost in git://-style request (note: does
21 * not activate sending git:// style request).
25 static char *git_req_vhost
;
27 static char *strip_escapes(const char *str
, const char *service
,
33 const char *service_noprefix
= service
;
34 struct strbuf ret
= STRBUF_INIT
;
36 skip_prefix(service_noprefix
, "git-", &service_noprefix
);
38 /* Pass the service to command. */
39 setenv("GIT_EXT_SERVICE", service
, 1);
40 setenv("GIT_EXT_SERVICE_NOPREFIX", service_noprefix
, 1);
42 /* Scan the length of argument. */
43 while (str
[rpos
] && (escape
|| str
[rpos
] != ' ')) {
56 /* Fall-through to error. */
58 die("Bad remote-ext placeholder '%%%c'.",
63 escape
= (str
[rpos
] == '%');
66 if (escape
&& !str
[rpos
])
67 die("remote-ext command has incomplete placeholder");
70 ++*next
; /* Skip over space */
73 * Do the actual placeholder substitution. The string will be short
74 * enough not to overflow integers.
76 rpos
= special
? 2 : 0; /* Skip first 2 bytes in specials. */
78 while (str
[rpos
] && (escape
|| str
[rpos
] != ' ')) {
83 strbuf_addch(&ret
, str
[rpos
]);
86 strbuf_addstr(&ret
, service_noprefix
);
89 strbuf_addstr(&ret
, service
);
99 strbuf_addch(&ret
, str
[rpos
]);
106 git_req
= strbuf_detach(&ret
, NULL
);
109 git_req_vhost
= strbuf_detach(&ret
, NULL
);
112 return strbuf_detach(&ret
, NULL
);
116 /* Should be enough... */
117 #define MAXARGUMENTS 256
119 static const char **parse_argv(const char *arg
, const char *service
)
124 char *temparray
[MAXARGUMENTS
+ 1];
128 if (arguments
== MAXARGUMENTS
)
129 die("remote-ext command has too many arguments");
130 expanded
= strip_escapes(arg
, service
, &arg
);
132 temparray
[arguments
++] = expanded
;
135 ret
= xmalloc((arguments
+ 1) * sizeof(char *));
136 for (i
= 0; i
< arguments
; i
++)
137 ret
[i
] = temparray
[i
];
138 ret
[arguments
] = NULL
;
142 static void send_git_request(int stdin_fd
, const char *serv
, const char *repo
,
150 * Request needs 12 bytes extra if there is vhost (xxxx \0host=\0) and
151 * 6 bytes extra (xxxx \0) if there is no vhost.
154 bufferspace
= strlen(serv
) + strlen(repo
) + strlen(vhost
) + 12;
156 bufferspace
= strlen(serv
) + strlen(repo
) + 6;
158 if (bufferspace
> 0xFFFF)
159 die("Request too large to send");
160 buffer
= xmalloc(bufferspace
);
162 /* Make the packet. */
163 wpos
= sprintf(buffer
, "%04x%s %s%c", (unsigned)bufferspace
,
166 /* Add vhost if any. */
168 sprintf(buffer
+ wpos
, "host=%s%c", vhost
, 0);
170 /* Send the request */
171 if (write_in_full(stdin_fd
, buffer
, bufferspace
) < 0)
172 die_errno("Failed to send request");
177 static int run_child(const char *arg
, const char *service
)
180 struct child_process child
= CHILD_PROCESS_INIT
;
185 child
.argv
= parse_argv(arg
, service
);
187 if (start_command(&child
) < 0)
188 die("Can't run specified command");
191 send_git_request(child
.in
, service
, git_req
, git_req_vhost
);
193 r
= bidirectional_transfer_loop(child
.out
, child
.in
);
195 r
= finish_command(&child
);
197 finish_command(&child
);
201 #define MAXCOMMAND 4096
203 static int command_loop(const char *child
)
205 char buffer
[MAXCOMMAND
];
209 if (!fgets(buffer
, MAXCOMMAND
- 1, stdin
)) {
211 die("Comammand input error");
214 /* Strip end of line characters. */
216 while (i
> 0 && isspace(buffer
[i
- 1]))
219 if (!strcmp(buffer
, "capabilities")) {
220 printf("*connect\n\n");
222 } else if (!strncmp(buffer
, "connect ", 8)) {
225 return run_child(child
, buffer
+ 8);
227 fprintf(stderr
, "Bad command");
233 int cmd_remote_ext(int argc
, const char **argv
, const char *prefix
)
236 die("Expected two arguments");
238 return command_loop(argv
[2]);