Skip to content

Commit 4b25bc2

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 dffce17 commit 4b25bc2

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
@@ -24,6 +24,7 @@
2424
#include "wrapper.h"
2525
#include "trace2.h"
2626
#include "json-parser.h"
27+
#include "remote.h"
2728
#include "path.h"
2829

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

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

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

473503
static char *default_cache_root(const char *root)
@@ -1339,6 +1369,68 @@ static int cmd_version(int argc, const char **argv)
13391369
return 0;
13401370
}
13411371

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

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)