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 ) {
@@ -361,6 +362,21 @@ static int set_config(const char *fmt, ...)
361
362
return res ;
362
363
}
363
364
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
+
364
380
/* Find N for which .CacheServers[N].GlobalDefault == true */
365
381
static int get_cache_server_index (struct json_iterator * it )
366
382
{
@@ -430,6 +446,18 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
430
446
JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
431
447
struct cache_server_url_data data = { .url = NULL };
432
448
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
+
433
461
if (iterate_json (& it ) < 0 ) {
434
462
reset_iterator (& it );
435
463
strbuf_release (& out );
@@ -450,7 +478,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
450
478
return 1 ;
451
479
}
452
480
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" ));
454
484
}
455
485
456
486
static char * default_cache_root (const char * root )
@@ -1296,6 +1326,68 @@ static int cmd_version(int argc, const char **argv)
1296
1326
return 0 ;
1297
1327
}
1298
1328
1329
+ static int cmd_cache_server (int argc , const char * * argv )
1330
+ {
1331
+ int get = 0 ;
1332
+ const char * set = NULL , * list = NULL ;
1333
+ struct option options [] = {
1334
+ OPT_CMDMODE (0 , "get" , & get ,
1335
+ N_ ("get the configured cache-server URL" ), 1 ),
1336
+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1337
+ N_ ("configure the cache-server to use" )),
1338
+ OPT_STRING (0 , "list" , & list , N_ ("remote" ),
1339
+ N_ ("list the possible cache-server URLs" )),
1340
+ OPT_END (),
1341
+ };
1342
+ const char * const usage [] = {
1343
+ N_ ("scalar cache-server "
1344
+ "[--get | --set <url> | --list <remote>] [<enlistment>]" ),
1345
+ NULL
1346
+ };
1347
+ int res = 0 ;
1348
+
1349
+ argc = parse_options (argc , argv , NULL , options ,
1350
+ usage , 0 );
1351
+
1352
+ if (get + !!set + !!list > 1 )
1353
+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1354
+ usage , options );
1355
+
1356
+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1357
+
1358
+ if (list ) {
1359
+ const char * name = list , * url = list ;
1360
+
1361
+ if (!strchr (list , '/' )) {
1362
+ struct remote * remote ;
1363
+
1364
+ /* Look up remote */
1365
+ remote = remote_get (list );
1366
+ if (!remote ) {
1367
+ error ("no such remote: '%s'" , name );
1368
+ return 1 ;
1369
+ }
1370
+ if (!remote -> url .nr ) {
1371
+ return error (_ ("remote '%s' has no URLs" ),
1372
+ name );
1373
+ }
1374
+ url = remote -> url .v [0 ];
1375
+ }
1376
+ res = supports_gvfs_protocol (url , NULL );
1377
+ } else if (set ) {
1378
+ res = set_config ("gvfs.cache-server=%s" , set );
1379
+ } else {
1380
+ char * url = NULL ;
1381
+
1382
+ printf ("Using cache server: %s\n" ,
1383
+ git_config_get_string ("gvfs.cache-server" , & url ) ?
1384
+ "(undefined)" : url );
1385
+ free (url );
1386
+ }
1387
+
1388
+ return !!res ;
1389
+ }
1390
+
1299
1391
static struct {
1300
1392
const char * name ;
1301
1393
int (* fn )(int , const char * * );
@@ -1310,6 +1402,7 @@ static struct {
1310
1402
{ "help" , cmd_help },
1311
1403
{ "version" , cmd_version },
1312
1404
{ "diagnose" , cmd_diagnose },
1405
+ { "cache-server" , cmd_cache_server },
1313
1406
{ NULL , NULL },
1314
1407
};
1315
1408
0 commit comments