Skip to content

Commit

Permalink
Merge branch 'ds/bundle-uri-clone' into seen
Browse files Browse the repository at this point in the history
Implement "git clone --bundle-uri".
source: <pull.1300.git.1658781277.gitgitgadget@gmail.com>

* ds/bundle-uri-clone:
  clone: --bundle-uri cannot be combined with --depth
  bundle-uri: add support for http(s):// and file://
  clone: add --bundle-uri option
  bundle-uri: create basic file-copy logic
  remote-curl: add 'get' capability
  • Loading branch information
gitster committed Jul 28, 2022
2 parents 2201340 + 1e3255b commit b1dc090
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Documentation/git-clone.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ or `--mirror` is given)
for `host.xz:foo/.git`). Cloning into an existing directory
is only allowed if the directory is empty.

--bundle-uri=<uri>::
Before fetching from the remote, fetch a bundle from the given
`<uri>` and unbundle the data into the local repository. The refs
in the bundle will be stored under the hidden `refs/bundle/*`
namespace. This option is incompatible with `--depth`,
`--shallow-since`, and `--shallow-exclude`.

:git-clone: 1
include::urls.txt[]

Expand Down
9 changes: 9 additions & 0 deletions Documentation/gitremote-helpers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ Supported commands: 'list', 'import'.
Can guarantee that when a clone is requested, the received
pack is self contained and is connected.

'get'::
Can use the 'get' command to download a file from a given URI.

If a helper advertises 'connect', Git will use it if possible and
fall back to another capability if the helper requests so when
connecting (see the 'connect' command under COMMANDS).
Expand Down Expand Up @@ -418,6 +421,12 @@ Supported if the helper has the "connect" capability.
+
Supported if the helper has the "stateless-connect" capability.

'get' <uri> <path>::
Downloads the file from the given `<uri>` to the given `<path>`. If
`<path>.temp` exists, then Git assumes that the `.temp` file is a
partial download from a previous attempt and will resume the
download from that position.

If a fatal error occurs, the program writes the error message to
stderr and exits. The caller should expect that a suitable error
message has been printed if the child closes the connection without
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,7 @@ LIB_OBJS += blob.o
LIB_OBJS += bloom.o
LIB_OBJS += branch.o
LIB_OBJS += bulk-checkin.o
LIB_OBJS += bundle-uri.o
LIB_OBJS += bundle.o
LIB_OBJS += cache-tree.o
LIB_OBJS += cbtree.o
Expand Down
18 changes: 18 additions & 0 deletions builtin/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "list-objects-filter-options.h"
#include "hook.h"
#include "bundle.h"
#include "bundle-uri.h"

/*
* Overall FIXMEs:
Expand Down Expand Up @@ -77,6 +78,7 @@ static int option_filter_submodules = -1; /* unspecified */
static int config_filter_submodules = -1; /* unspecified */
static struct string_list server_options = STRING_LIST_INIT_NODUP;
static int option_remote_submodules;
static const char *bundle_uri;

static int recurse_submodules_cb(const struct option *opt,
const char *arg, int unset)
Expand Down Expand Up @@ -160,6 +162,8 @@ static struct option builtin_clone_options[] = {
N_("any cloned submodules will use their remote-tracking branch")),
OPT_BOOL(0, "sparse", &option_sparse_checkout,
N_("initialize sparse-checkout file to include only files at root")),
OPT_STRING(0, "bundle-uri", &bundle_uri,
N_("uri"), N_("a URI for downloading bundles before fetching from origin remote")),
OPT_END()
};

Expand Down Expand Up @@ -933,6 +937,9 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
option_no_checkout = 1;
}

if (bundle_uri && deepen)
die(_("--bundle-uri is incompatible with --depth, --shallow-since, and --shallow-exclude"));

repo_name = argv[0];

path = get_repo_path(repo_name, &is_bundle);
Expand Down Expand Up @@ -1232,6 +1239,17 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
if (transport->smart_options && !deepen && !filter_options.choice)
transport->smart_options->check_self_contained_and_connected = 1;

/*
* Before fetching from the remote, download and install bundle
* data from the --bundle-uri option.
*/
if (bundle_uri) {
/* At this point, we need the_repository to match the cloned repo. */
repo_init(the_repository, git_dir, work_tree);
if (fetch_bundle_uri(the_repository, bundle_uri))
warning(_("failed to fetch objects from bundle URI '%s'"),
bundle_uri);
}

strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
refspec_ref_prefixes(&remote->fetch,
Expand Down
168 changes: 168 additions & 0 deletions bundle-uri.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#include "cache.h"
#include "bundle-uri.h"
#include "bundle.h"
#include "object-store.h"
#include "refs.h"
#include "run-command.h"

static int find_temp_filename(struct strbuf *name)
{
int fd;
/*
* Find a temporary filename that is available. This is briefly
* racy, but unlikely to collide.
*/
fd = odb_mkstemp(name, "bundles/tmp_uri_XXXXXX");
if (fd < 0) {
warning(_("failed to create temporary file"));
return -1;
}

close(fd);
unlink(name->buf);
return 0;
}

static int download_https_uri_to_file(const char *file, const char *uri)
{
int result = 0;
struct child_process cp = CHILD_PROCESS_INIT;
FILE *child_in = NULL, *child_out = NULL;
struct strbuf line = STRBUF_INIT;
int found_get = 0;

strvec_pushl(&cp.args, "git-remote-https", "origin", uri, NULL);
cp.in = -1;
cp.out = -1;

if (start_command(&cp))
return 1;

child_in = fdopen(cp.in, "w");
if (!child_in) {
result = 1;
goto cleanup;
}

child_out = fdopen(cp.out, "r");
if (!child_out) {
result = 1;
goto cleanup;
}

fprintf(child_in, "capabilities\n");
fflush(child_in);

while (!strbuf_getline(&line, child_out)) {
if (!line.len)
break;
if (!strcmp(line.buf, "get"))
found_get = 1;
}
strbuf_release(&line);

if (!found_get) {
result = error(_("insufficient capabilities"));
goto cleanup;
}

fprintf(child_in, "get %s %s\n\n", uri, file);

cleanup:
if (child_in)
fclose(child_in);
if (finish_command(&cp))
return 1;
if (child_out)
fclose(child_out);
return result;
}

static int copy_uri_to_file(const char *filename, const char *uri)
{
const char *out;

if (skip_prefix(uri, "https:", &out) ||
skip_prefix(uri, "http:", &out))
return download_https_uri_to_file(filename, uri);

if (!skip_prefix(uri, "file://", &out))
out = uri;

/* Copy as a file */
return copy_file(filename, out, 0);
}

static int unbundle_from_file(struct repository *r, const char *file)
{
int result = 0;
int bundle_fd;
struct bundle_header header = BUNDLE_HEADER_INIT;
struct string_list_item *refname;
struct strbuf bundle_ref = STRBUF_INIT;
size_t bundle_prefix_len;

if ((bundle_fd = read_bundle_header(file, &header)) < 0)
return 1;

if ((result = unbundle(r, &header, bundle_fd, NULL)))
return 1;

/*
* Convert all refs/heads/ from the bundle into refs/bundles/
* in the local repository.
*/
strbuf_addstr(&bundle_ref, "refs/bundles/");
bundle_prefix_len = bundle_ref.len;

for_each_string_list_item(refname, &header.references) {
struct object_id *oid = refname->util;
struct object_id old_oid;
const char *branch_name;
int has_old;

if (!skip_prefix(refname->string, "refs/heads/", &branch_name))
continue;

strbuf_setlen(&bundle_ref, bundle_prefix_len);
strbuf_addstr(&bundle_ref, branch_name);

has_old = !read_ref(bundle_ref.buf, &old_oid);
update_ref("fetched bundle", bundle_ref.buf, oid,
has_old ? &old_oid : NULL,
REF_SKIP_OID_VERIFICATION,
UPDATE_REFS_MSG_ON_ERR);
}

bundle_header_release(&header);
return result;
}

int fetch_bundle_uri(struct repository *r, const char *uri)
{
int result = 0;
struct strbuf filename = STRBUF_INIT;

if ((result = find_temp_filename(&filename)))
goto cleanup;

if ((result = copy_uri_to_file(filename.buf, uri))) {
warning(_("failed to download bundle from URI '%s'"), uri);
goto cleanup;
}

if ((result = !is_bundle(filename.buf, 0))) {
warning(_("file at URI '%s' is not a bundle"), uri);
goto cleanup;
}

if ((result = unbundle_from_file(r, filename.buf))) {
warning(_("failed to unbundle bundle from URI '%s'"), uri);
goto cleanup;
}

cleanup:
unlink(filename.buf);
strbuf_release(&filename);
return result;
}
14 changes: 14 additions & 0 deletions bundle-uri.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef BUNDLE_URI_H
#define BUNDLE_URI_H

struct repository;

/**
* Fetch data from the given 'uri' and unbundle the bundle data found
* based on that information.
*
* Returns non-zero if no bundle information is found at the given 'uri'.
*/
int fetch_bundle_uri(struct repository *r, const char *uri);

#endif
32 changes: 32 additions & 0 deletions remote-curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,33 @@ static void parse_fetch(struct strbuf *buf)
strbuf_reset(buf);
}

static void parse_get(struct strbuf *buf)
{
struct strbuf url = STRBUF_INIT;
struct strbuf path = STRBUF_INIT;
const char *p, *space;

if (!skip_prefix(buf->buf, "get ", &p))
die(_("http transport does not support %s"), buf->buf);

space = strchr(p, ' ');

if (!space)
die(_("protocol error: expected '<url> <path>', missing space"));

strbuf_add(&url, p, space - p);
strbuf_addstr(&path, space + 1);

if (http_get_file(url.buf, path.buf, NULL))
die(_("failed to download file at URL '%s'"), url.buf);

strbuf_release(&url);
strbuf_release(&path);
printf("\n");
fflush(stdout);
strbuf_reset(buf);
}

static int push_dav(int nr_spec, const char **specs)
{
struct child_process child = CHILD_PROCESS_INIT;
Expand Down Expand Up @@ -1564,9 +1591,14 @@ int cmd_main(int argc, const char **argv)
printf("unsupported\n");
fflush(stdout);

} else if (skip_prefix(buf.buf, "get ", &arg)) {
parse_get(&buf);
fflush(stdout);

} else if (!strcmp(buf.buf, "capabilities")) {
printf("stateless-connect\n");
printf("fetch\n");
printf("get\n");
printf("option\n");
printf("push\n");
printf("check-connectivity\n");
Expand Down
37 changes: 37 additions & 0 deletions t/t5557-http-get.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/sh

test_description='test downloading a file by URL'

. ./test-lib.sh

. "$TEST_DIRECTORY"/lib-httpd.sh
start_httpd

test_expect_success 'get by URL: 404' '
url="$HTTPD_URL/none.txt" &&
cat >input <<-EOF &&
capabilities
get $url file1
EOF
test_must_fail git remote-http $url <input 2>err &&
test_path_is_missing file1 &&
grep "failed to download file at URL" err &&
rm file1.temp
'

test_expect_success 'get by URL: 200' '
echo data >"$HTTPD_DOCUMENT_ROOT_PATH/exists.txt" &&
url="$HTTPD_URL/exists.txt" &&
cat >input <<-EOF &&
capabilities
get $url file2
EOF
GIT_TRACE2_PERF=1 git remote-http $url <input &&
test_cmp "$HTTPD_DOCUMENT_ROOT_PATH/exists.txt" file2
'

test_done
Loading

0 comments on commit b1dc090

Please sign in to comment.