Skip to content

Commit a97f3d5

Browse files
dschoderrickstolee
andcommitted
scalar clone: support GVFS-enabled remote repositories
With this change, we come a big step closer to feature parity with Scalar: this allows cloning from Azure Repos (which do not support partial clones at time of writing). We use the just-implemented JSON parser to parse the response we got from the `gvfs/config` endpoint; Please note that this response might, or might not, contain information about a cache server. The presence or absence of said cache server, however, has nothing to do with the ability to speak the GVFS protocol (but the presence of the `gvfs/config` endpoint does that). An alternative considered during the development of this patch was to perform simple string matching instead of parsing the JSON-formatted data; However, this would have been fragile, as the response contains free-form text (e.g. the repository's description) which might contain parts that would confuse a simple string matcher (but not a proper JSON parser). Note: we need to limit the re-try logic in `git clone` to handle only the non-GVFS case: the call to `set_config()` to un-set the partial clone settings would otherwise fail because those settings would not exist in the GVFS protocol case. This will at least give us a clearer reason why such a fetch fails. Co-authored-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
1 parent c56e6ac commit a97f3d5

File tree

2 files changed

+131
-3
lines changed

2 files changed

+131
-3
lines changed

diagnose.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "packfile.h"
1212
#include "parse-options.h"
1313
#include "write-or-die.h"
14+
#include "config.h"
1415

1516
struct archive_dir {
1617
const char *path;
@@ -219,6 +220,7 @@ int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
219220
struct strvec archiver_args = STRVEC_INIT;
220221
char **argv_copy = NULL;
221222
int stdout_fd = -1, archiver_fd = -1;
223+
char *cache_server_url = NULL;
222224
struct strbuf buf = STRBUF_INIT;
223225
int res, i;
224226
struct archive_dir archive_dirs[] = {
@@ -254,6 +256,11 @@ int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
254256
get_version_info(&buf, 1);
255257

256258
strbuf_addf(&buf, "Repository root: %s\n", the_repository->worktree);
259+
260+
git_config_get_string("gvfs.cache-server", &cache_server_url);
261+
strbuf_addf(&buf, "Cache Server: %s\n\n",
262+
cache_server_url ? cache_server_url : "None");
263+
257264
get_disk_info(&buf);
258265
write_or_die(stdout_fd, buf.buf, buf.len);
259266
strvec_pushf(&archiver_args,
@@ -311,6 +318,7 @@ int create_diagnostics_archive(struct strbuf *zip_path, enum diagnose_mode mode)
311318
free(argv_copy);
312319
strvec_clear(&archiver_args);
313320
strbuf_release(&buf);
321+
free(cache_server_url);
314322

315323
return res;
316324
}

scalar.c

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "help.h"
1818
#include "setup.h"
1919
#include "trace2.h"
20+
#include "json-parser.h"
2021

2122
static void setup_enlistment_directory(int argc, const char **argv,
2223
const char * const *usagestr,
@@ -334,6 +335,80 @@ static int set_config(const char *fmt, ...)
334335
return res;
335336
}
336337

338+
/* Find N for which .CacheServers[N].GlobalDefault == true */
339+
static int get_cache_server_index(struct json_iterator *it)
340+
{
341+
const char *p;
342+
char *q;
343+
long l;
344+
345+
if (it->type == JSON_TRUE &&
346+
skip_iprefix(it->key.buf, ".CacheServers[", &p) &&
347+
(l = strtol(p, &q, 10)) >= 0 && p != q &&
348+
!strcasecmp(q, "].GlobalDefault")) {
349+
*(long *)it->fn_data = l;
350+
return 1;
351+
}
352+
353+
return 0;
354+
}
355+
356+
struct cache_server_url_data {
357+
char *key, *url;
358+
};
359+
360+
/* Get .CacheServers[N].Url */
361+
static int get_cache_server_url(struct json_iterator *it)
362+
{
363+
struct cache_server_url_data *data = it->fn_data;
364+
365+
if (it->type == JSON_STRING &&
366+
!strcasecmp(data->key, it->key.buf)) {
367+
data->url = strbuf_detach(&it->string_value, NULL);
368+
return 1;
369+
}
370+
371+
return 0;
372+
}
373+
374+
/*
375+
* If `cache_server_url` is `NULL`, print the list to `stdout`.
376+
*
377+
* Since `gvfs-helper` requires a Git directory, this _must_ be run in
378+
* a worktree.
379+
*/
380+
static int supports_gvfs_protocol(const char *url, char **cache_server_url)
381+
{
382+
struct child_process cp = CHILD_PROCESS_INIT;
383+
struct strbuf out = STRBUF_INIT;
384+
385+
cp.git_cmd = 1;
386+
strvec_pushl(&cp.args, "gvfs-helper", "--remote", url, "config", NULL);
387+
if (!pipe_command(&cp, NULL, 0, &out, 512, NULL, 0)) {
388+
long l = 0;
389+
struct json_iterator it =
390+
JSON_ITERATOR_INIT(out.buf, get_cache_server_index, &l);
391+
struct cache_server_url_data data = { .url = NULL };
392+
393+
if (iterate_json(&it) < 0) {
394+
strbuf_release(&out);
395+
return error("JSON parse error");
396+
}
397+
data.key = xstrfmt(".CacheServers[%ld].Url", l);
398+
it.fn = get_cache_server_url;
399+
it.fn_data = &data;
400+
if (iterate_json(&it) < 0) {
401+
strbuf_release(&out);
402+
return error("JSON parse error");
403+
}
404+
*cache_server_url = data.url;
405+
free(data.key);
406+
return 1;
407+
}
408+
strbuf_release(&out);
409+
return 0; /* error out quietly */
410+
}
411+
337412
static char *remote_default_branch(const char *url)
338413
{
339414
struct child_process cp = CHILD_PROCESS_INIT;
@@ -435,6 +510,8 @@ static int cmd_clone(int argc, const char **argv)
435510
{
436511
const char *branch = NULL;
437512
int full_clone = 0, single_branch = 0, show_progress = isatty(2);
513+
const char *cache_server_url = NULL;
514+
char *default_cache_server_url = NULL;
438515
struct option clone_options[] = {
439516
OPT_STRING('b', "branch", &branch, N_("<branch>"),
440517
N_("branch to checkout after clone")),
@@ -443,6 +520,9 @@ static int cmd_clone(int argc, const char **argv)
443520
OPT_BOOL(0, "single-branch", &single_branch,
444521
N_("only download metadata for the branch that will "
445522
"be checked out")),
523+
OPT_STRING(0, "cache-server-url", &cache_server_url,
524+
N_("<url>"),
525+
N_("the url or friendly name of the cache server")),
446526
OPT_END(),
447527
};
448528
const char * const clone_usage[] = {
@@ -453,6 +533,7 @@ static int cmd_clone(int argc, const char **argv)
453533
char *enlistment = NULL, *dir = NULL;
454534
struct strbuf buf = STRBUF_INIT;
455535
int res;
536+
int gvfs_protocol;
456537

457538
argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
458539

@@ -515,13 +596,46 @@ static int cmd_clone(int argc, const char **argv)
515596
set_config("remote.origin.fetch="
516597
"+refs/heads/%s:refs/remotes/origin/%s",
517598
single_branch ? branch : "*",
518-
single_branch ? branch : "*") ||
519-
set_config("remote.origin.promisor=true") ||
520-
set_config("remote.origin.partialCloneFilter=blob:none")) {
599+
single_branch ? branch : "*")) {
521600
res = error(_("could not configure remote in '%s'"), dir);
522601
goto cleanup;
523602
}
524603

604+
if (set_config("credential.https://dev.azure.com.useHttpPath=true")) {
605+
res = error(_("could not configure credential.useHttpPath"));
606+
goto cleanup;
607+
}
608+
609+
gvfs_protocol = cache_server_url ||
610+
supports_gvfs_protocol(url, &default_cache_server_url);
611+
612+
if (gvfs_protocol) {
613+
if (!cache_server_url)
614+
cache_server_url = default_cache_server_url;
615+
if (set_config("core.useGVFSHelper=true") ||
616+
set_config("core.gvfs=150") ||
617+
set_config("http.version=HTTP/1.1")) {
618+
res = error(_("could not turn on GVFS helper"));
619+
goto cleanup;
620+
}
621+
if (cache_server_url &&
622+
set_config("gvfs.cache-server=%s", cache_server_url)) {
623+
res = error(_("could not configure cache server"));
624+
goto cleanup;
625+
}
626+
if (cache_server_url)
627+
fprintf(stderr, "Cache server URL: %s\n",
628+
cache_server_url);
629+
} else {
630+
if (set_config("core.useGVFSHelper=false") ||
631+
set_config("remote.origin.promisor=true") ||
632+
set_config("remote.origin.partialCloneFilter=blob:none")) {
633+
res = error(_("could not configure partial clone in "
634+
"'%s'"), dir);
635+
goto cleanup;
636+
}
637+
}
638+
525639
if (!full_clone &&
526640
(res = run_git("sparse-checkout", "init", "--cone", NULL)))
527641
goto cleanup;
@@ -532,6 +646,11 @@ static int cmd_clone(int argc, const char **argv)
532646
if ((res = run_git("fetch", "--quiet",
533647
show_progress ? "--progress" : "--no-progress",
534648
"origin", NULL))) {
649+
if (gvfs_protocol) {
650+
res = error(_("failed to prefetch commits and trees"));
651+
goto cleanup;
652+
}
653+
535654
warning(_("partial clone failed; attempting full clone"));
536655

537656
if (set_config("remote.origin.promisor") ||
@@ -564,6 +683,7 @@ static int cmd_clone(int argc, const char **argv)
564683
free(enlistment);
565684
free(dir);
566685
strbuf_release(&buf);
686+
free(default_cache_server_url);
567687
return res;
568688
}
569689

0 commit comments

Comments
 (0)