Skip to content

Commit 1822022

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. The way the `gvfs-helper` command operates is apparently incompatible with HTTP/2, that's why we need to enforce HTTP/1.1 in Scalar clones accessing Azure Repos. Co-authored-by: Derrick Stolee <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 3fab670 commit 1822022

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

diagnose.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "parse-options.h"
1313
#include "repository.h"
1414
#include "write-or-die.h"
15+
#include "config.h"
1516

1617
struct archive_dir {
1718
const char *path;
@@ -185,6 +186,7 @@ int create_diagnostics_archive(struct repository *r,
185186
struct strvec archiver_args = STRVEC_INIT;
186187
char **argv_copy = NULL;
187188
int stdout_fd = -1, archiver_fd = -1;
189+
char *cache_server_url = NULL;
188190
struct strbuf buf = STRBUF_INIT;
189191
int res;
190192
struct archive_dir archive_dirs[] = {
@@ -220,6 +222,11 @@ int create_diagnostics_archive(struct repository *r,
220222
get_version_info(&buf, 1);
221223

222224
strbuf_addf(&buf, "Repository root: %s\n", r->worktree);
225+
226+
repo_config_get_string(r, "gvfs.cache-server", &cache_server_url);
227+
strbuf_addf(&buf, "Cache Server: %s\n\n",
228+
cache_server_url ? cache_server_url : "None");
229+
223230
get_disk_info(&buf);
224231
write_or_die(stdout_fd, buf.buf, buf.len);
225232
strvec_pushf(&archiver_args,
@@ -277,6 +284,7 @@ int create_diagnostics_archive(struct repository *r,
277284
free(argv_copy);
278285
strvec_clear(&archiver_args);
279286
strbuf_release(&buf);
287+
free(cache_server_url);
280288

281289
return res;
282290
}

scalar.c

Lines changed: 128 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "help.h"
2020
#include "setup.h"
2121
#include "trace2.h"
22+
#include "json-parser.h"
2223

2324
static void setup_enlistment_directory(int argc, const char **argv,
2425
const char * const *usagestr,
@@ -354,6 +355,85 @@ static int set_config(const char *fmt, ...)
354355
return res;
355356
}
356357

358+
/* Find N for which .CacheServers[N].GlobalDefault == true */
359+
static int get_cache_server_index(struct json_iterator *it)
360+
{
361+
const char *p;
362+
char *q;
363+
long l;
364+
365+
if (it->type == JSON_TRUE &&
366+
skip_iprefix(it->key.buf, ".CacheServers[", &p) &&
367+
(l = strtol(p, &q, 10)) >= 0 && p != q &&
368+
!strcasecmp(q, "].GlobalDefault")) {
369+
*(long *)it->fn_data = l;
370+
return 1;
371+
}
372+
373+
return 0;
374+
}
375+
376+
struct cache_server_url_data {
377+
char *key, *url;
378+
};
379+
380+
/* Get .CacheServers[N].Url */
381+
static int get_cache_server_url(struct json_iterator *it)
382+
{
383+
struct cache_server_url_data *data = it->fn_data;
384+
385+
if (it->type == JSON_STRING &&
386+
!strcasecmp(data->key, it->key.buf)) {
387+
data->url = strbuf_detach(&it->string_value, NULL);
388+
return 1;
389+
}
390+
391+
return 0;
392+
}
393+
394+
/*
395+
* If `cache_server_url` is `NULL`, print the list to `stdout`.
396+
*
397+
* Since `gvfs-helper` requires a Git directory, this _must_ be run in
398+
* a worktree.
399+
*/
400+
static int supports_gvfs_protocol(const char *url, char **cache_server_url)
401+
{
402+
struct child_process cp = CHILD_PROCESS_INIT;
403+
struct strbuf out = STRBUF_INIT;
404+
405+
cp.git_cmd = 1;
406+
strvec_pushl(&cp.args, "-c", "http.version=HTTP/1.1",
407+
"gvfs-helper", "--remote", url, "config", NULL);
408+
if (!pipe_command(&cp, NULL, 0, &out, 512, NULL, 0)) {
409+
long l = 0;
410+
struct json_iterator it =
411+
JSON_ITERATOR_INIT(out.buf, get_cache_server_index, &l);
412+
struct cache_server_url_data data = { .url = NULL };
413+
414+
if (iterate_json(&it) < 0) {
415+
reset_iterator(&it);
416+
strbuf_release(&out);
417+
return error("JSON parse error");
418+
}
419+
data.key = xstrfmt(".CacheServers[%ld].Url", l);
420+
it.fn = get_cache_server_url;
421+
it.fn_data = &data;
422+
if (iterate_json(&it) < 0) {
423+
reset_iterator(&it);
424+
strbuf_release(&out);
425+
return error("JSON parse error");
426+
}
427+
*cache_server_url = data.url;
428+
free(data.key);
429+
reset_iterator(&it);
430+
strbuf_release(&out);
431+
return 1;
432+
}
433+
strbuf_release(&out);
434+
return 0; /* error out quietly */
435+
}
436+
357437
static char *remote_default_branch(const char *url)
358438
{
359439
struct child_process cp = CHILD_PROCESS_INIT;
@@ -454,6 +534,8 @@ static int cmd_clone(int argc, const char **argv)
454534
char *branch_to_free = NULL;
455535
int full_clone = 0, single_branch = 0, show_progress = isatty(2);
456536
int src = 1, tags = 1, maintenance = 1;
537+
const char *cache_server_url = NULL;
538+
char *default_cache_server_url = NULL;
457539
struct option clone_options[] = {
458540
OPT_STRING('b', "branch", &branch, N_("<branch>"),
459541
N_("branch to checkout after clone")),
@@ -468,6 +550,9 @@ static int cmd_clone(int argc, const char **argv)
468550
N_("specify if tags should be fetched during clone")),
469551
OPT_BOOL(0, "maintenance", &maintenance,
470552
N_("specify if background maintenance should be enabled")),
553+
OPT_STRING(0, "cache-server-url", &cache_server_url,
554+
N_("<url>"),
555+
N_("the url or friendly name of the cache server")),
471556
OPT_END(),
472557
};
473558
const char * const clone_usage[] = {
@@ -479,6 +564,7 @@ static int cmd_clone(int argc, const char **argv)
479564
char *enlistment = NULL, *dir = NULL;
480565
struct strbuf buf = STRBUF_INIT;
481566
int res;
567+
int gvfs_protocol;
482568

483569
argc = parse_options(argc, argv, NULL, clone_options, clone_usage, 0);
484570

@@ -544,9 +630,7 @@ static int cmd_clone(int argc, const char **argv)
544630
set_config("remote.origin.fetch="
545631
"+refs/heads/%s:refs/remotes/origin/%s",
546632
single_branch ? branch : "*",
547-
single_branch ? branch : "*") ||
548-
set_config("remote.origin.promisor=true") ||
549-
set_config("remote.origin.partialCloneFilter=blob:none")) {
633+
single_branch ? branch : "*")) {
550634
res = error(_("could not configure remote in '%s'"), dir);
551635
goto cleanup;
552636
}
@@ -556,6 +640,41 @@ static int cmd_clone(int argc, const char **argv)
556640
goto cleanup;
557641
}
558642

643+
if (set_config("credential.https://dev.azure.com.useHttpPath=true")) {
644+
res = error(_("could not configure credential.useHttpPath"));
645+
goto cleanup;
646+
}
647+
648+
gvfs_protocol = cache_server_url ||
649+
supports_gvfs_protocol(url, &default_cache_server_url);
650+
651+
if (gvfs_protocol) {
652+
if (!cache_server_url)
653+
cache_server_url = default_cache_server_url;
654+
if (set_config("core.useGVFSHelper=true") ||
655+
set_config("core.gvfs=150") ||
656+
set_config("http.version=HTTP/1.1")) {
657+
res = error(_("could not turn on GVFS helper"));
658+
goto cleanup;
659+
}
660+
if (cache_server_url &&
661+
set_config("gvfs.cache-server=%s", cache_server_url)) {
662+
res = error(_("could not configure cache server"));
663+
goto cleanup;
664+
}
665+
if (cache_server_url)
666+
fprintf(stderr, "Cache server URL: %s\n",
667+
cache_server_url);
668+
} else {
669+
if (set_config("core.useGVFSHelper=false") ||
670+
set_config("remote.origin.promisor=true") ||
671+
set_config("remote.origin.partialCloneFilter=blob:none")) {
672+
res = error(_("could not configure partial clone in "
673+
"'%s'"), dir);
674+
goto cleanup;
675+
}
676+
}
677+
559678
if (!full_clone &&
560679
(res = run_git("sparse-checkout", "init", "--cone", NULL)))
561680
goto cleanup;
@@ -568,6 +687,11 @@ static int cmd_clone(int argc, const char **argv)
568687
"origin",
569688
(tags ? NULL : "--no-tags"),
570689
NULL))) {
690+
if (gvfs_protocol) {
691+
res = error(_("failed to prefetch commits and trees"));
692+
goto cleanup;
693+
}
694+
571695
warning(_("partial clone failed; attempting full clone"));
572696

573697
if (set_config("remote.origin.promisor") ||
@@ -602,6 +726,7 @@ static int cmd_clone(int argc, const char **argv)
602726
free(enlistment);
603727
free(dir);
604728
strbuf_release(&buf);
729+
free(default_cache_server_url);
605730
return res;
606731
}
607732

0 commit comments

Comments
 (0)