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