Skip to content

Commit b5624a4

Browse files
derrickstoleegitster
authored andcommitted
remote-curl: add 'get' capability
A future change will want a way to download a file over HTTP(S) using the simplest of download mechanisms. We do not want to assume that the server on the other side understands anything about the Git protocol but could be a simple static web server. Create the new 'get' capability for the remote helpers which advertises that the 'get' command is avalable. A caller can send a line containing 'get <url> <path>' to download the file at <url> into the file at <path>. Reviewed-by: Josh Steadmon <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a475b7 commit b5624a4

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Documentation/gitremote-helpers.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ Supported commands: 'list', 'import'.
168168
Can guarantee that when a clone is requested, the received
169169
pack is self contained and is connected.
170170

171+
'get'::
172+
Can use the 'get' command to download a file from a given URI.
173+
171174
If a helper advertises 'connect', Git will use it if possible and
172175
fall back to another capability if the helper requests so when
173176
connecting (see the 'connect' command under COMMANDS).
@@ -418,6 +421,12 @@ Supported if the helper has the "connect" capability.
418421
+
419422
Supported if the helper has the "stateless-connect" capability.
420423

424+
'get' <uri> <path>::
425+
Downloads the file from the given `<uri>` to the given `<path>`. If
426+
`<path>.temp` exists, then Git assumes that the `.temp` file is a
427+
partial download from a previous attempt and will resume the
428+
download from that position.
429+
421430
If a fatal error occurs, the program writes the error message to
422431
stderr and exits. The caller should expect that a suitable error
423432
message has been printed if the child closes the connection without

remote-curl.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,29 @@ static void parse_fetch(struct strbuf *buf)
12861286
strbuf_reset(buf);
12871287
}
12881288

1289+
static void parse_get(const char *arg)
1290+
{
1291+
struct strbuf url = STRBUF_INIT;
1292+
struct strbuf path = STRBUF_INIT;
1293+
const char *space;
1294+
1295+
space = strchr(arg, ' ');
1296+
1297+
if (!space)
1298+
die(_("protocol error: expected '<url> <path>', missing space"));
1299+
1300+
strbuf_add(&url, arg, space - arg);
1301+
strbuf_addstr(&path, space + 1);
1302+
1303+
if (http_get_file(url.buf, path.buf, NULL))
1304+
die(_("failed to download file at URL '%s'"), url.buf);
1305+
1306+
strbuf_release(&url);
1307+
strbuf_release(&path);
1308+
printf("\n");
1309+
fflush(stdout);
1310+
}
1311+
12891312
static int push_dav(int nr_spec, const char **specs)
12901313
{
12911314
struct child_process child = CHILD_PROCESS_INIT;
@@ -1564,9 +1587,14 @@ int cmd_main(int argc, const char **argv)
15641587
printf("unsupported\n");
15651588
fflush(stdout);
15661589

1590+
} else if (skip_prefix(buf.buf, "get ", &arg)) {
1591+
parse_get(arg);
1592+
fflush(stdout);
1593+
15671594
} else if (!strcmp(buf.buf, "capabilities")) {
15681595
printf("stateless-connect\n");
15691596
printf("fetch\n");
1597+
printf("get\n");
15701598
printf("option\n");
15711599
printf("push\n");
15721600
printf("check-connectivity\n");

t/t5557-http-get.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/sh
2+
3+
test_description='test downloading a file by URL'
4+
5+
. ./test-lib.sh
6+
7+
. "$TEST_DIRECTORY"/lib-httpd.sh
8+
start_httpd
9+
10+
test_expect_success 'get by URL: 404' '
11+
test_when_finished "rm -f file.temp" &&
12+
url="$HTTPD_URL/none.txt" &&
13+
cat >input <<-EOF &&
14+
capabilities
15+
get $url file1
16+
EOF
17+
18+
test_must_fail git remote-http $url <input 2>err &&
19+
test_path_is_missing file1 &&
20+
grep "failed to download file at URL" err
21+
'
22+
23+
test_expect_success 'get by URL: 200' '
24+
echo data >"$HTTPD_DOCUMENT_ROOT_PATH/exists.txt" &&
25+
26+
url="$HTTPD_URL/exists.txt" &&
27+
cat >input <<-EOF &&
28+
capabilities
29+
get $url file2
30+
31+
EOF
32+
33+
git remote-http $url <input &&
34+
test_cmp "$HTTPD_DOCUMENT_ROOT_PATH/exists.txt" file2
35+
'
36+
37+
test_done

0 commit comments

Comments
 (0)