23
23
#include "wrapper.h"
24
24
#include "trace2.h"
25
25
#include "json-parser.h"
26
+ #include "remote.h"
26
27
#include "path.h"
27
28
28
29
static int is_unattended (void ) {
@@ -376,6 +377,21 @@ static int set_config(const char *fmt, ...)
376
377
return res ;
377
378
}
378
379
380
+ static int list_cache_server_urls (struct json_iterator * it )
381
+ {
382
+ const char * p ;
383
+ char * q ;
384
+ long l ;
385
+
386
+ if (it -> type == JSON_STRING &&
387
+ skip_iprefix (it -> key .buf , ".CacheServers[" , & p ) &&
388
+ (l = strtol (p , & q , 10 )) >= 0 && p != q &&
389
+ !strcasecmp (q , "].Url" ))
390
+ printf ("#%ld: %s\n" , l , it -> string_value .buf );
391
+
392
+ return 0 ;
393
+ }
394
+
379
395
/* Find N for which .CacheServers[N].GlobalDefault == true */
380
396
static int get_cache_server_index (struct json_iterator * it )
381
397
{
@@ -446,6 +462,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
446
462
JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
447
463
struct cache_server_url_data data = { .url = NULL };
448
464
465
+ if (!cache_server_url ) {
466
+ it .fn = list_cache_server_urls ;
467
+ if (iterate_json (& it ) < 0 ) {
468
+ reset_iterator (& it );
469
+ strbuf_release (& out );
470
+ return error ("JSON parse error" );
471
+ }
472
+ reset_iterator (& it );
473
+ strbuf_release (& out );
474
+ return 0 ;
475
+ }
476
+
449
477
if (iterate_json (& it ) < 0 ) {
450
478
reset_iterator (& it );
451
479
strbuf_release (& out );
@@ -466,7 +494,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
466
494
return 1 ;
467
495
}
468
496
strbuf_release (& out );
469
- return 0 ; /* error out quietly */
497
+ /* error out quietly, unless we wanted to list URLs */
498
+ return cache_server_url ?
499
+ 0 : error (_ ("Could not access gvfs/config endpoint" ));
470
500
}
471
501
472
502
static char * default_cache_root (const char * root )
@@ -1338,6 +1368,68 @@ static int cmd_version(int argc, const char **argv)
1338
1368
return 0 ;
1339
1369
}
1340
1370
1371
+ static int cmd_cache_server (int argc , const char * * argv )
1372
+ {
1373
+ int get = 0 ;
1374
+ const char * set = NULL , * list = NULL ;
1375
+ struct option options [] = {
1376
+ OPT_CMDMODE (0 , "get" , & get ,
1377
+ N_ ("get the configured cache-server URL" ), 1 ),
1378
+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1379
+ N_ ("configure the cache-server to use" )),
1380
+ OPT_STRING (0 , "list" , & list , N_ ("remote" ),
1381
+ N_ ("list the possible cache-server URLs" )),
1382
+ OPT_END (),
1383
+ };
1384
+ const char * const usage [] = {
1385
+ N_ ("scalar cache-server "
1386
+ "[--get | --set <url> | --list <remote>] [<enlistment>]" ),
1387
+ NULL
1388
+ };
1389
+ int res = 0 ;
1390
+
1391
+ argc = parse_options (argc , argv , NULL , options ,
1392
+ usage , 0 );
1393
+
1394
+ if (get + !!set + !!list > 1 )
1395
+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1396
+ usage , options );
1397
+
1398
+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1399
+
1400
+ if (list ) {
1401
+ const char * name = list , * url = list ;
1402
+
1403
+ if (!strchr (list , '/' )) {
1404
+ struct remote * remote ;
1405
+
1406
+ /* Look up remote */
1407
+ remote = remote_get (list );
1408
+ if (!remote ) {
1409
+ error ("no such remote: '%s'" , name );
1410
+ return 1 ;
1411
+ }
1412
+ if (!remote -> url .nr ) {
1413
+ return error (_ ("remote '%s' has no URLs" ),
1414
+ name );
1415
+ }
1416
+ url = remote -> url .v [0 ];
1417
+ }
1418
+ res = supports_gvfs_protocol (url , NULL );
1419
+ } else if (set ) {
1420
+ res = set_config ("gvfs.cache-server=%s" , set );
1421
+ } else {
1422
+ char * url = NULL ;
1423
+
1424
+ printf ("Using cache server: %s\n" ,
1425
+ git_config_get_string ("gvfs.cache-server" , & url ) ?
1426
+ "(undefined)" : url );
1427
+ free (url );
1428
+ }
1429
+
1430
+ return !!res ;
1431
+ }
1432
+
1341
1433
static struct {
1342
1434
const char * name ;
1343
1435
int (* fn )(int , const char * * );
@@ -1352,6 +1444,7 @@ static struct {
1352
1444
{ "help" , cmd_help },
1353
1445
{ "version" , cmd_version },
1354
1446
{ "diagnose" , cmd_diagnose },
1447
+ { "cache-server" , cmd_cache_server },
1355
1448
{ NULL , NULL },
1356
1449
};
1357
1450
0 commit comments