1515#include "packfile.h"
1616#include "help.h"
1717#include "json-parser.h"
18+ #include "remote.h"
1819
1920static int is_unattended (void ) {
2021 return git_env_bool ("Scalar_UNATTENDED" , 0 );
@@ -340,6 +341,21 @@ static int set_config(const char *fmt, ...)
340341 return res ;
341342}
342343
344+ static int list_cache_server_urls (struct json_iterator * it )
345+ {
346+ const char * p ;
347+ char * q ;
348+ long l ;
349+
350+ if (it -> type == JSON_STRING &&
351+ skip_iprefix (it -> key .buf , ".CacheServers[" , & p ) &&
352+ (l = strtol (p , & q , 10 )) >= 0 && p != q &&
353+ !strcasecmp (q , "].Url" ))
354+ printf ("#%ld: %s\n" , l , it -> string_value .buf );
355+
356+ return 0 ;
357+ }
358+
343359/* Find N for which .CacheServers[N].GlobalDefault == true */
344360static int get_cache_server_index (struct json_iterator * it )
345361{
@@ -409,6 +425,16 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
409425 JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
410426 struct cache_server_url_data data = { .url = NULL };
411427
428+ if (!cache_server_url ) {
429+ it .fn = list_cache_server_urls ;
430+ if (iterate_json (& it ) < 0 ) {
431+ strbuf_release (& out );
432+ return error ("JSON parse error" );
433+ }
434+ strbuf_release (& out );
435+ return 0 ;
436+ }
437+
412438 if (iterate_json (& it ) < 0 ) {
413439 strbuf_release (& out );
414440 return error ("JSON parse error" );
@@ -425,7 +451,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
425451 return 1 ;
426452 }
427453 strbuf_release (& out );
428- return 0 ; /* error out quietly */
454+ /* error out quietly, unless we wanted to list URLs */
455+ return cache_server_url ?
456+ 0 : error (_ ("Could not access gvfs/config endpoint" ));
429457}
430458
431459static char * default_cache_root (const char * root )
@@ -1197,6 +1225,77 @@ static int cmd_version(int argc, const char **argv)
11971225 return 0 ;
11981226}
11991227
1228+ static int cmd_cache_server (int argc , const char * * argv )
1229+ {
1230+ int get = 0 ;
1231+ char * set = NULL , * list = NULL ;
1232+ const char * default_remote = "(default)" ;
1233+ struct option options [] = {
1234+ OPT_BOOL (0 , "get" , & get ,
1235+ N_ ("get the configured cache-server URL" )),
1236+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1237+ N_ ("configure the cache-server to use" )),
1238+ { OPTION_STRING , 0 , "list" , & list , N_ ("remote" ),
1239+ N_ ("list the possible cache-server URLs" ),
1240+ PARSE_OPT_OPTARG , NULL , (intptr_t ) default_remote },
1241+ OPT_END (),
1242+ };
1243+ const char * const usage [] = {
1244+ N_ ("scalar cache_server "
1245+ "[--get | --set <url> | --list [<remote>]] [<enlistment>]" ),
1246+ NULL
1247+ };
1248+ int res = 0 ;
1249+
1250+ argc = parse_options (argc , argv , NULL , options ,
1251+ usage , 0 );
1252+
1253+ if (get + !!set + !!list > 1 )
1254+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1255+ usage , options );
1256+
1257+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1258+
1259+ if (list ) {
1260+ const char * name = list , * url = list ;
1261+
1262+ if (list == default_remote )
1263+ list = NULL ;
1264+
1265+ if (!list || !strchr (list , '/' )) {
1266+ struct remote * remote ;
1267+
1268+ /* Look up remote */
1269+ remote = remote_get (list );
1270+ if (!remote ) {
1271+ error ("no such remote: '%s'" , name );
1272+ free (list );
1273+ return 1 ;
1274+ }
1275+ if (!remote -> url ) {
1276+ free (list );
1277+ return error (_ ("remote '%s' has no URLs" ),
1278+ name );
1279+ }
1280+ url = remote -> url [0 ];
1281+ }
1282+ res = supports_gvfs_protocol (url , NULL );
1283+ free (list );
1284+ } else if (set ) {
1285+ res = set_config ("gvfs.cache-server=%s" , set );
1286+ free (set );
1287+ } else {
1288+ char * url = NULL ;
1289+
1290+ printf ("Using cache server: %s\n" ,
1291+ git_config_get_string ("gvfs.cache-server" , & url ) ?
1292+ "(undefined)" : url );
1293+ free (url );
1294+ }
1295+
1296+ return !!res ;
1297+ }
1298+
12001299static struct {
12011300 const char * name ;
12021301 int (* fn )(int , const char * * );
@@ -1211,6 +1310,7 @@ static struct {
12111310 { "help" , cmd_help },
12121311 { "version" , cmd_version },
12131312 { "diagnose" , cmd_diagnose },
1313+ { "cache-server" , cmd_cache_server },
12141314 { NULL , NULL },
12151315};
12161316
0 commit comments