Skip to content

Commit 1db87ec

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 df4f0a6 commit 1db87ec

File tree

3 files changed

+150
-1
lines changed

3 files changed

+150
-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: 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
{
@@ -430,6 +446,18 @@ 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+
reset_iterator(&it);
453+
strbuf_release(&out);
454+
return error("JSON parse error");
455+
}
456+
reset_iterator(&it);
457+
strbuf_release(&out);
458+
return 0;
459+
}
460+
433461
if (iterate_json(&it) < 0) {
434462
reset_iterator(&it);
435463
strbuf_release(&out);
@@ -450,7 +478,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
450478
return 1;
451479
}
452480
strbuf_release(&out);
453-
return 0; /* error out quietly */
481+
/* error out quietly, unless we wanted to list URLs */
482+
return cache_server_url ?
483+
0 : error(_("Could not access gvfs/config endpoint"));
454484
}
455485

456486
static char *default_cache_root(const char *root)
@@ -1292,6 +1322,68 @@ static int cmd_version(int argc, const char **argv)
12921322
return 0;
12931323
}
12941324

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

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)