Skip to content

Commit e9ee3ed

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 <johannes.schindelin@gmx.de>
1 parent bab0aa5 commit e9ee3ed

File tree

3 files changed

+148
-1
lines changed

3 files changed

+148
-1
lines changed

Documentation/scalar.txt

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

2223
DESCRIPTION
2324
-----------
@@ -182,6 +183,27 @@ delete <enlistment>::
182183
This subcommand lets you delete an existing Scalar enlistment from your
183184
local file system, unregistering the repository.
184185

186+
Cache-server
187+
~~~~~~~~~~~~
188+
189+
cache-server ( --get | --set <url> | --list [<remote>] ) [<enlistment>]::
190+
This command lets you query or set the GVFS-enabled cache server used
191+
to fetch missing objects.
192+
193+
--get::
194+
This is the default command mode: query the currently-configured cache
195+
server URL, if any.
196+
197+
--list::
198+
Access the `gvfs/info` endpoint of the specified remote (default:
199+
`origin`) to figure out which cache servers are available, if any.
200+
+
201+
In contrast to the `--get` command mode (which only accesses the local
202+
repository), this command mode triggers a request via the network that
203+
potentially requires authentication. If authentication is required, the
204+
configured credential helper is employed (see linkgit:git-credential[1]
205+
for details).
206+
185207
SEE ALSO
186208
--------
187209
linkgit:git-clone[1], linkgit:git-maintenance[1].

scalar.c

Lines changed: 92 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) {
@@ -361,6 +362,21 @@ static int set_config(const char *fmt, ...)
361362
return res;
362363
}
363364

365+
static int list_cache_server_urls(struct json_iterator *it)
366+
{
367+
const char *p;
368+
char *q;
369+
long l;
370+
371+
if (it->type == JSON_STRING &&
372+
skip_iprefix(it->key.buf, ".CacheServers[", &p) &&
373+
(l = strtol(p, &q, 10)) >= 0 && p != q &&
374+
!strcasecmp(q, "].Url"))
375+
printf("#%ld: %s\n", l, it->string_value.buf);
376+
377+
return 0;
378+
}
379+
364380
/* Find N for which .CacheServers[N].GlobalDefault == true */
365381
static int get_cache_server_index(struct json_iterator *it)
366382
{
@@ -430,6 +446,16 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
430446
JSON_ITERATOR_INIT(out.buf, get_cache_server_index, &l);
431447
struct cache_server_url_data data = { .url = NULL };
432448

449+
if (!cache_server_url) {
450+
it.fn = list_cache_server_urls;
451+
if (iterate_json(&it) < 0) {
452+
strbuf_release(&out);
453+
return error("JSON parse error");
454+
}
455+
strbuf_release(&out);
456+
return 0;
457+
}
458+
433459
if (iterate_json(&it) < 0) {
434460
strbuf_release(&out);
435461
return error("JSON parse error");
@@ -446,7 +472,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
446472
return 1;
447473
}
448474
strbuf_release(&out);
449-
return 0; /* error out quietly */
475+
/* error out quietly, unless we wanted to list URLs */
476+
return cache_server_url ?
477+
0 : error(_("Could not access gvfs/config endpoint"));
450478
}
451479

452480
static char *default_cache_root(const char *root)
@@ -1288,6 +1316,68 @@ static int cmd_version(int argc, const char **argv)
12881316
return 0;
12891317
}
12901318

1319+
static int cmd_cache_server(int argc, const char **argv)
1320+
{
1321+
int get = 0;
1322+
const char *set = NULL, *list = NULL;
1323+
struct option options[] = {
1324+
OPT_CMDMODE(0, "get", &get,
1325+
N_("get the configured cache-server URL"), 1),
1326+
OPT_STRING(0, "set", &set, N_("URL"),
1327+
N_("configure the cache-server to use")),
1328+
OPT_STRING(0, "list", &list, N_("remote"),
1329+
N_("list the possible cache-server URLs")),
1330+
OPT_END(),
1331+
};
1332+
const char * const usage[] = {
1333+
N_("scalar cache-server "
1334+
"[--get | --set <url> | --list <remote>] [<enlistment>]"),
1335+
NULL
1336+
};
1337+
int res = 0;
1338+
1339+
argc = parse_options(argc, argv, NULL, options,
1340+
usage, 0);
1341+
1342+
if (get + !!set + !!list > 1)
1343+
usage_msg_opt(_("--get/--set/--list are mutually exclusive"),
1344+
usage, options);
1345+
1346+
setup_enlistment_directory(argc, argv, usage, options, NULL);
1347+
1348+
if (list) {
1349+
const char *name = list, *url = list;
1350+
1351+
if (!strchr(list, '/')) {
1352+
struct remote *remote;
1353+
1354+
/* Look up remote */
1355+
remote = remote_get(list);
1356+
if (!remote) {
1357+
error("no such remote: '%s'", name);
1358+
return 1;
1359+
}
1360+
if (!remote->url.nr) {
1361+
return error(_("remote '%s' has no URLs"),
1362+
name);
1363+
}
1364+
url = remote->url.v[0];
1365+
}
1366+
res = supports_gvfs_protocol(url, NULL);
1367+
} else if (set) {
1368+
res = set_config("gvfs.cache-server=%s", set);
1369+
} else {
1370+
char *url = NULL;
1371+
1372+
printf("Using cache server: %s\n",
1373+
git_config_get_string("gvfs.cache-server", &url) ?
1374+
"(undefined)" : url);
1375+
free(url);
1376+
}
1377+
1378+
return !!res;
1379+
}
1380+
12911381
static struct {
12921382
const char *name;
12931383
int (*fn)(int, const char **);
@@ -1302,6 +1392,7 @@ static struct {
13021392
{ "help", cmd_help },
13031393
{ "version", cmd_version },
13041394
{ "diagnose", cmd_diagnose },
1395+
{ "cache-server", cmd_cache_server },
13051396
{ NULL, NULL},
13061397
};
13071398

t/t9210-scalar.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,38 @@ test_expect_success '`scalar delete` with existing repo' '
461461
test_path_is_missing existing
462462
'
463463

464+
test_expect_success 'scalar cache-server basics' '
465+
repo=with-cache-server &&
466+
git init $repo &&
467+
scalar cache-server --get $repo >out &&
468+
cat >expect <<-EOF &&
469+
Using cache server: (undefined)
470+
EOF
471+
test_cmp expect out &&
472+
473+
scalar cache-server --set http://fake-server/url $repo &&
474+
test_cmp_config -C $repo http://fake-server/url gvfs.cache-server &&
475+
scalar delete $repo &&
476+
test_path_is_missing $repo
477+
'
478+
479+
test_expect_success 'scalar cache-server list URL' '
480+
repo=with-real-gvfs &&
481+
git init $repo &&
482+
git -C $repo remote add origin http://$HOST_PORT/ &&
483+
scalar cache-server --list origin $repo >out &&
484+
485+
cat >expect <<-EOF &&
486+
#0: http://$HOST_PORT/servertype/cache
487+
EOF
488+
489+
test_cmp expect out &&
490+
491+
test_must_fail scalar -C $repo cache-server --list 2>err &&
492+
grep "requires a value" err &&
493+
494+
scalar delete $repo &&
495+
test_path_is_missing $repo
496+
'
497+
464498
test_done

0 commit comments

Comments
 (0)