Skip to content

Commit bd50de7

Browse files
committed
scalar: add the cache-server command
This allows setting the GVFS-enabled cache server, or listing the one(s) associated with the remote repository. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ef89882 commit bd50de7

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-1
lines changed

Documentation/scalar.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ scalar run ( all | config | commit-graph | fetch | loose-objects | pack-files )
1919
scalar reconfigure [--maintenance=(enable|disable|keep)] [ --all | <enlistment> ]
2020
scalar diagnose [<enlistment>]
2121
scalar delete <enlistment>
22+
scalar cache-server ( --get | --set <url> | --list [<remote>] ) [<enlistment>]
2223

2324
DESCRIPTION
2425
-----------
@@ -204,6 +205,27 @@ delete <enlistment>::
204205
This subcommand lets you delete an existing Scalar enlistment from your
205206
local file system, unregistering the repository.
206207

208+
Cache-server
209+
~~~~~~~~~~~~
210+
211+
cache-server ( --get | --set <url> | --list [<remote>] ) [<enlistment>]::
212+
This command lets you query or set the GVFS-enabled cache server used
213+
to fetch missing objects.
214+
215+
--get::
216+
This is the default command mode: query the currently-configured cache
217+
server URL, if any.
218+
219+
--list::
220+
Access the `gvfs/info` endpoint of the specified remote (default:
221+
`origin`) to figure out which cache servers are available, if any.
222+
+
223+
In contrast to the `--get` command mode (which only accesses the local
224+
repository), this command mode triggers a request via the network that
225+
potentially requires authentication. If authentication is required, the
226+
configured credential helper is employed (see linkgit:git-credential[1]
227+
for details).
228+
207229
SEE ALSO
208230
--------
209231
linkgit:git-clone[1], linkgit:git-maintenance[1].

scalar.c

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "wrapper.h"
2424
#include "trace2.h"
2525
#include "json-parser.h"
26+
#include "remote.h"
2627
#include "path.h"
2728

2829
static int is_unattended(void) {
@@ -376,6 +377,21 @@ static int set_config(const char *fmt, ...)
376377
return res;
377378
}
378379

380+
static int list_cache_server_urls(struct json_iterator *it)
381+
{
382+
const char *p;
383+
char *q;
384+
long l;
385+
386+
if (it->type == JSON_STRING &&
387+
skip_iprefix(it->key.buf, ".CacheServers[", &p) &&
388+
(l = strtol(p, &q, 10)) >= 0 && p != q &&
389+
!strcasecmp(q, "].Url"))
390+
printf("#%ld: %s\n", l, it->string_value.buf);
391+
392+
return 0;
393+
}
394+
379395
/* Find N for which .CacheServers[N].GlobalDefault == true */
380396
static int get_cache_server_index(struct json_iterator *it)
381397
{
@@ -446,6 +462,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
446462
JSON_ITERATOR_INIT(out.buf, get_cache_server_index, &l);
447463
struct cache_server_url_data data = { .url = NULL };
448464

465+
if (!cache_server_url) {
466+
it.fn = list_cache_server_urls;
467+
if (iterate_json(&it) < 0) {
468+
reset_iterator(&it);
469+
strbuf_release(&out);
470+
return error("JSON parse error");
471+
}
472+
reset_iterator(&it);
473+
strbuf_release(&out);
474+
return 0;
475+
}
476+
449477
if (iterate_json(&it) < 0) {
450478
reset_iterator(&it);
451479
strbuf_release(&out);
@@ -466,7 +494,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
466494
return 1;
467495
}
468496
strbuf_release(&out);
469-
return 0; /* error out quietly */
497+
/* error out quietly, unless we wanted to list URLs */
498+
return cache_server_url ?
499+
0 : error(_("Could not access gvfs/config endpoint"));
470500
}
471501

472502
static char *default_cache_root(const char *root)
@@ -1338,6 +1368,68 @@ static int cmd_version(int argc, const char **argv)
13381368
return 0;
13391369
}
13401370

1371+
static int cmd_cache_server(int argc, const char **argv)
1372+
{
1373+
int get = 0;
1374+
const char *set = NULL, *list = NULL;
1375+
struct option options[] = {
1376+
OPT_CMDMODE(0, "get", &get,
1377+
N_("get the configured cache-server URL"), 1),
1378+
OPT_STRING(0, "set", &set, N_("URL"),
1379+
N_("configure the cache-server to use")),
1380+
OPT_STRING(0, "list", &list, N_("remote"),
1381+
N_("list the possible cache-server URLs")),
1382+
OPT_END(),
1383+
};
1384+
const char * const usage[] = {
1385+
N_("scalar cache-server "
1386+
"[--get | --set <url> | --list <remote>] [<enlistment>]"),
1387+
NULL
1388+
};
1389+
int res = 0;
1390+
1391+
argc = parse_options(argc, argv, NULL, options,
1392+
usage, 0);
1393+
1394+
if (get + !!set + !!list > 1)
1395+
usage_msg_opt(_("--get/--set/--list are mutually exclusive"),
1396+
usage, options);
1397+
1398+
setup_enlistment_directory(argc, argv, usage, options, NULL);
1399+
1400+
if (list) {
1401+
const char *name = list, *url = list;
1402+
1403+
if (!strchr(list, '/')) {
1404+
struct remote *remote;
1405+
1406+
/* Look up remote */
1407+
remote = remote_get(list);
1408+
if (!remote) {
1409+
error("no such remote: '%s'", name);
1410+
return 1;
1411+
}
1412+
if (!remote->url.nr) {
1413+
return error(_("remote '%s' has no URLs"),
1414+
name);
1415+
}
1416+
url = remote->url.v[0];
1417+
}
1418+
res = supports_gvfs_protocol(url, NULL);
1419+
} else if (set) {
1420+
res = set_config("gvfs.cache-server=%s", set);
1421+
} else {
1422+
char *url = NULL;
1423+
1424+
printf("Using cache server: %s\n",
1425+
git_config_get_string("gvfs.cache-server", &url) ?
1426+
"(undefined)" : url);
1427+
free(url);
1428+
}
1429+
1430+
return !!res;
1431+
}
1432+
13411433
static struct {
13421434
const char *name;
13431435
int (*fn)(int, const char **);
@@ -1352,6 +1444,7 @@ static struct {
13521444
{ "help", cmd_help },
13531445
{ "version", cmd_version },
13541446
{ "diagnose", cmd_diagnose },
1447+
{ "cache-server", cmd_cache_server },
13551448
{ NULL, NULL},
13561449
};
13571450

t/t9210-scalar.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,4 +484,38 @@ test_expect_success '`scalar delete` with existing repo' '
484484
test_path_is_missing existing
485485
'
486486

487+
test_expect_success 'scalar cache-server basics' '
488+
repo=with-cache-server &&
489+
git init $repo &&
490+
scalar cache-server --get $repo >out &&
491+
cat >expect <<-EOF &&
492+
Using cache server: (undefined)
493+
EOF
494+
test_cmp expect out &&
495+
496+
scalar cache-server --set http://fake-server/url $repo &&
497+
test_cmp_config -C $repo http://fake-server/url gvfs.cache-server &&
498+
scalar delete $repo &&
499+
test_path_is_missing $repo
500+
'
501+
502+
test_expect_success 'scalar cache-server list URL' '
503+
repo=with-real-gvfs &&
504+
git init $repo &&
505+
git -C $repo remote add origin http://$HOST_PORT/ &&
506+
scalar cache-server --list origin $repo >out &&
507+
508+
cat >expect <<-EOF &&
509+
#0: http://$HOST_PORT/servertype/cache
510+
EOF
511+
512+
test_cmp expect out &&
513+
514+
test_must_fail scalar -C $repo cache-server --list 2>err &&
515+
grep "requires a value" err &&
516+
517+
scalar delete $repo &&
518+
test_path_is_missing $repo
519+
'
520+
487521
test_done

0 commit comments

Comments
 (0)