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,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
452480static 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+
12911381static 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
0 commit comments