Skip to content

Commit aeea129

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 1145f87 commit aeea129

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-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
-----------
@@ -170,6 +171,27 @@ delete <enlistment>::
170171
This subcommand lets you delete an existing Scalar enlistment from your
171172
local file system, unregistering the repository.
172173

174+
Cache-server
175+
~~~~~~~~~~~~
176+
177+
cache-server ( --get | --set <url> | --list [<remote>] ) [<enlistment>]::
178+
This command lets you query or set the GVFS-enabled cache server used
179+
to fetch missing objects.
180+
181+
--get::
182+
This is the default command mode: query the currently-configured cache
183+
server URL, if any.
184+
185+
--list::
186+
Access the `gvfs/info` endpoint of the specified remote (default:
187+
`origin`) to figure out which cache servers are available, if any.
188+
+
189+
In contrast to the `--get` command mode (which only accesses the local
190+
repository), this command mode triggers a request via the network that
191+
potentially requires authentication. If authentication is required, the
192+
configured credential helper is employed (see linkgit:git-credential[1]
193+
for details).
194+
173195
SEE ALSO
174196
--------
175197
linkgit:git-clone[1], linkgit:git-maintenance[1].

scalar.c

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "wrapper.h"
2222
#include "trace2.h"
2323
#include "json-parser.h"
24+
#include "remote.h"
2425

2526
static int is_unattended(void) {
2627
return git_env_bool("Scalar_UNATTENDED", 0);
@@ -355,6 +356,21 @@ static int set_config(const char *fmt, ...)
355356
return res;
356357
}
357358

359+
static int list_cache_server_urls(struct json_iterator *it)
360+
{
361+
const char *p;
362+
char *q;
363+
long l;
364+
365+
if (it->type == JSON_STRING &&
366+
skip_iprefix(it->key.buf, ".CacheServers[", &p) &&
367+
(l = strtol(p, &q, 10)) >= 0 && p != q &&
368+
!strcasecmp(q, "].Url"))
369+
printf("#%ld: %s\n", l, it->string_value.buf);
370+
371+
return 0;
372+
}
373+
358374
/* Find N for which .CacheServers[N].GlobalDefault == true */
359375
static int get_cache_server_index(struct json_iterator *it)
360376
{
@@ -424,6 +440,16 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
424440
JSON_ITERATOR_INIT(out.buf, get_cache_server_index, &l);
425441
struct cache_server_url_data data = { .url = NULL };
426442

443+
if (!cache_server_url) {
444+
it.fn = list_cache_server_urls;
445+
if (iterate_json(&it) < 0) {
446+
strbuf_release(&out);
447+
return error("JSON parse error");
448+
}
449+
strbuf_release(&out);
450+
return 0;
451+
}
452+
427453
if (iterate_json(&it) < 0) {
428454
strbuf_release(&out);
429455
return error("JSON parse error");
@@ -440,7 +466,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
440466
return 1;
441467
}
442468
strbuf_release(&out);
443-
return 0; /* error out quietly */
469+
/* error out quietly, unless we wanted to list URLs */
470+
return cache_server_url ?
471+
0 : error(_("Could not access gvfs/config endpoint"));
444472
}
445473

446474
static char *default_cache_root(const char *root)
@@ -1234,6 +1262,77 @@ static int cmd_version(int argc, const char **argv)
12341262
return 0;
12351263
}
12361264

1265+
static int cmd_cache_server(int argc, const char **argv)
1266+
{
1267+
int get = 0;
1268+
char *set = NULL, *list = NULL;
1269+
const char *default_remote = "(default)";
1270+
struct option options[] = {
1271+
OPT_BOOL(0, "get", &get,
1272+
N_("get the configured cache-server URL")),
1273+
OPT_STRING(0, "set", &set, N_("URL"),
1274+
N_("configure the cache-server to use")),
1275+
{ OPTION_STRING, 0, "list", &list, N_("remote"),
1276+
N_("list the possible cache-server URLs"),
1277+
PARSE_OPT_OPTARG, NULL, (intptr_t) default_remote },
1278+
OPT_END(),
1279+
};
1280+
const char * const usage[] = {
1281+
N_("scalar cache_server "
1282+
"[--get | --set <url> | --list [<remote>]] [<enlistment>]"),
1283+
NULL
1284+
};
1285+
int res = 0;
1286+
1287+
argc = parse_options(argc, argv, NULL, options,
1288+
usage, 0);
1289+
1290+
if (get + !!set + !!list > 1)
1291+
usage_msg_opt(_("--get/--set/--list are mutually exclusive"),
1292+
usage, options);
1293+
1294+
setup_enlistment_directory(argc, argv, usage, options, NULL);
1295+
1296+
if (list) {
1297+
const char *name = list, *url = list;
1298+
1299+
if (list == default_remote)
1300+
list = NULL;
1301+
1302+
if (!list || !strchr(list, '/')) {
1303+
struct remote *remote;
1304+
1305+
/* Look up remote */
1306+
remote = remote_get(list);
1307+
if (!remote) {
1308+
error("no such remote: '%s'", name);
1309+
free(list);
1310+
return 1;
1311+
}
1312+
if (!remote->url) {
1313+
free(list);
1314+
return error(_("remote '%s' has no URLs"),
1315+
name);
1316+
}
1317+
url = remote->url[0];
1318+
}
1319+
res = supports_gvfs_protocol(url, NULL);
1320+
free(list);
1321+
} else if (set) {
1322+
res = set_config("gvfs.cache-server=%s", set);
1323+
free(set);
1324+
} else {
1325+
char *url = NULL;
1326+
1327+
printf("Using cache server: %s\n",
1328+
git_config_get_string("gvfs.cache-server", &url) ?
1329+
"(undefined)" : url);
1330+
free(url);
1331+
}
1332+
1333+
return !!res;
1334+
}
1335+
12371336
static struct {
12381337
const char *name;
12391338
int (*fn)(int, const char **);
@@ -1248,6 +1347,7 @@ static struct {
12481347
{ "help", cmd_help },
12491348
{ "version", cmd_version },
12501349
{ "diagnose", cmd_diagnose },
1350+
{ "cache-server", cmd_cache_server },
12511351
{ NULL, NULL},
12521352
};
12531353

0 commit comments

Comments
 (0)