Skip to content

Commit 923feef

Browse files
committed
bundle-uri: add support for http(s):// and file://
The previous change created the logic for copying a file by name. Now, first inspect the URI for an HTTP(S) prefix and use git-remote-https as the way to download the data at that URI. Otherwise, check to see if file:// is present and modify the prefix accordingly. Signed-off-by: Derrick Stolee <derrickstolee@github.com>
1 parent f9e4161 commit 923feef

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

bundle-uri.c

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,73 @@ static void find_temp_filename(struct strbuf *name)
1919
unlink(name->buf);
2020
}
2121

22+
static int download_https_uri_to_file(const char *uri, const char *file)
23+
{
24+
int result = 0;
25+
struct child_process cp = CHILD_PROCESS_INIT;
26+
FILE *child_in = NULL, *child_out = NULL;
27+
struct strbuf line = STRBUF_INIT;
28+
int found_get = 0;
29+
30+
strvec_pushl(&cp.args, "git-remote-https", "origin", uri, NULL);
31+
cp.in = -1;
32+
cp.out = -1;
33+
34+
if (start_command(&cp))
35+
return 1;
36+
37+
child_in = fdopen(cp.in, "w");
38+
if (!child_in) {
39+
result = 1;
40+
goto cleanup;
41+
}
42+
43+
child_out = fdopen(cp.out, "r");
44+
if (!child_out) {
45+
result = 1;
46+
goto cleanup;
47+
}
48+
49+
fprintf(child_in, "capabilities\n");
50+
fflush(child_in);
51+
52+
while (!strbuf_getline(&line, child_out)) {
53+
if (!line.len)
54+
break;
55+
if (!strcmp(line.buf, "get"))
56+
found_get = 1;
57+
}
58+
strbuf_release(&line);
59+
60+
if (!found_get) {
61+
result = error(_("insufficient capabilities"));
62+
goto cleanup;
63+
}
64+
65+
fprintf(child_in, "get %s %s\n\n", uri, file);
66+
67+
cleanup:
68+
if (child_in)
69+
fclose(child_in);
70+
if (finish_command(&cp))
71+
return 1;
72+
if (child_out)
73+
fclose(child_out);
74+
return result;
75+
}
76+
2277
static int copy_uri_to_file(const char *uri, const char *file)
2378
{
79+
const char *out;
80+
if (skip_prefix(uri, "https:", &out) ||
81+
skip_prefix(uri, "http:", &out))
82+
return download_https_uri_to_file(uri, file);
83+
84+
if (!skip_prefix(uri, "file://", &out))
85+
out = uri;
86+
2487
/* Copy as a file */
25-
return copy_file(file, uri, 0444);
88+
return !!copy_file(file, out, 0);
2689
}
2790

2891
static int unbundle_from_file(struct repository *r, const char *file)

t/t5558-fetch-bundle-uri.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,41 @@ test_expect_success 'fetch file bundle' '
3131
test_cmp expect actual
3232
'
3333

34+
test_expect_success 'fetch file:// bundle' '
35+
git init fetch-file &&
36+
git -C fetch-file fetch --bundle-uri="file://$(pwd)/fetch-from/B.bundle" &&
37+
git -C fetch-file rev-parse refs/bundles/topic >actual &&
38+
git -C fetch-from rev-parse topic >expect &&
39+
test_cmp expect actual
40+
'
41+
42+
#########################################################################
43+
# HTTP tests begin here
44+
45+
. "$TEST_DIRECTORY"/lib-httpd.sh
46+
start_httpd
47+
48+
test_expect_success 'fail to fetch from non-existent HTTP URL' '
49+
test_must_fail git fetch --bundle-uri="$HTTPD_URL/does-not-exist" 2>err &&
50+
grep "failed to download bundle from URI" err
51+
'
52+
53+
test_expect_success 'fail to fetch from non-bundle HTTP URL' '
54+
echo bogus >"$HTTPD_DOCUMENT_ROOT_PATH/bogus" &&
55+
test_must_fail git fetch --bundle-uri="$HTTPD_URL/bogus" 2>err &&
56+
grep "is not a bundle" err
57+
'
58+
59+
test_expect_success 'fetch HTTP bundle' '
60+
cp fetch-from/B.bundle "$HTTPD_DOCUMENT_ROOT_PATH/B.bundle" &&
61+
git init fetch-http &&
62+
git -C fetch-http fetch --bundle-uri="$HTTPD_URL/B.bundle" &&
63+
git -C fetch-http rev-parse refs/bundles/topic >actual &&
64+
git -C fetch-from rev-parse topic >expect &&
65+
test_cmp expect actual
66+
'
67+
68+
# Do not add tests here unless they use the HTTP server, as they will
69+
# not run unless the HTTP dependencies exist.
70+
3471
test_done

0 commit comments

Comments
 (0)