Skip to content

Commit 46afbd6

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 2eb9d69 commit 46afbd6

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
@@ -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: 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) {
@@ -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
{
@@ -431,6 +447,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
431447
JSON_ITERATOR_INIT(out.buf, get_cache_server_index, &l);
432448
struct cache_server_url_data data = { .url = NULL };
433449

450+
if (!cache_server_url) {
451+
it.fn = list_cache_server_urls;
452+
if (iterate_json(&it) < 0) {
453+
reset_iterator(&it);
454+
strbuf_release(&out);
455+
return error("JSON parse error");
456+
}
457+
reset_iterator(&it);
458+
strbuf_release(&out);
459+
return 0;
460+
}
461+
434462
if (iterate_json(&it) < 0) {
435463
reset_iterator(&it);
436464
strbuf_release(&out);
@@ -451,7 +479,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
451479
return 1;
452480
}
453481
strbuf_release(&out);
454-
return 0; /* error out quietly */
482+
/* error out quietly, unless we wanted to list URLs */
483+
return cache_server_url ?
484+
0 : error(_("Could not access gvfs/config endpoint"));
455485
}
456486

457487
static char *default_cache_root(const char *root)
@@ -1297,6 +1327,68 @@ static int cmd_version(int argc, const char **argv)
12971327
return 0;
12981328
}
12991329

1330+
static int cmd_cache_server(int argc, const char **argv)
1331+
{
1332+
int get = 0;
1333+
const char *set = NULL, *list = NULL;
1334+
struct option options[] = {
1335+
OPT_CMDMODE(0, "get", &get,
1336+
N_("get the configured cache-server URL"), 1),
1337+
OPT_STRING(0, "set", &set, N_("URL"),
1338+
N_("configure the cache-server to use")),
1339+
OPT_STRING(0, "list", &list, N_("remote"),
1340+
N_("list the possible cache-server URLs")),
1341+
OPT_END(),
1342+
};
1343+
const char * const usage[] = {
1344+
N_("scalar cache-server "
1345+
"[--get | --set <url> | --list <remote>] [<enlistment>]"),
1346+
NULL
1347+
};
1348+
int res = 0;
1349+
1350+
argc = parse_options(argc, argv, NULL, options,
1351+
usage, 0);
1352+
1353+
if (get + !!set + !!list > 1)
1354+
usage_msg_opt(_("--get/--set/--list are mutually exclusive"),
1355+
usage, options);
1356+
1357+
setup_enlistment_directory(argc, argv, usage, options, NULL);
1358+
1359+
if (list) {
1360+
const char *name = list, *url = list;
1361+
1362+
if (!strchr(list, '/')) {
1363+
struct remote *remote;
1364+
1365+
/* Look up remote */
1366+
remote = remote_get(list);
1367+
if (!remote) {
1368+
error("no such remote: '%s'", name);
1369+
return 1;
1370+
}
1371+
if (!remote->url.nr) {
1372+
return error(_("remote '%s' has no URLs"),
1373+
name);
1374+
}
1375+
url = remote->url.v[0];
1376+
}
1377+
res = supports_gvfs_protocol(url, NULL);
1378+
} else if (set) {
1379+
res = set_config("gvfs.cache-server=%s", set);
1380+
} else {
1381+
char *url = NULL;
1382+
1383+
printf("Using cache server: %s\n",
1384+
git_config_get_string("gvfs.cache-server", &url) ?
1385+
"(undefined)" : url);
1386+
free(url);
1387+
}
1388+
1389+
return !!res;
1390+
}
1391+
13001392
static struct {
13011393
const char *name;
13021394
int (*fn)(int, const char **);
@@ -1311,6 +1403,7 @@ static struct {
13111403
{ "help", cmd_help },
13121404
{ "version", cmd_version },
13131405
{ "diagnose", cmd_diagnose },
1406+
{ "cache-server", cmd_cache_server },
13141407
{ NULL, NULL},
13151408
};
13161409

t/t9210-scalar.sh

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

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

0 commit comments

Comments
 (0)