2323#include "wrapper.h"
2424#include "trace2.h"
2525#include "json-parser.h"
26+ #include "remote.h"
2627#include "path.h"
2728
2829static 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 */
365381static 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
456486static 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+
12951387static 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
0 commit comments