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 )
@@ -1292,6 +1322,68 @@ static int cmd_version(int argc, const char **argv)
1292
1322
return 0 ;
1293
1323
}
1294
1324
1325
+ static int cmd_cache_server (int argc , const char * * argv )
1326
+ {
1327
+ int get = 0 ;
1328
+ const char * set = NULL , * list = NULL ;
1329
+ struct option options [] = {
1330
+ OPT_CMDMODE (0 , "get" , & get ,
1331
+ N_ ("get the configured cache-server URL" ), 1 ),
1332
+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1333
+ N_ ("configure the cache-server to use" )),
1334
+ OPT_STRING (0 , "list" , & list , N_ ("remote" ),
1335
+ N_ ("list the possible cache-server URLs" )),
1336
+ OPT_END (),
1337
+ };
1338
+ const char * const usage [] = {
1339
+ N_ ("scalar cache-server "
1340
+ "[--get | --set <url> | --list <remote>] [<enlistment>]" ),
1341
+ NULL
1342
+ };
1343
+ int res = 0 ;
1344
+
1345
+ argc = parse_options (argc , argv , NULL , options ,
1346
+ usage , 0 );
1347
+
1348
+ if (get + !!set + !!list > 1 )
1349
+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1350
+ usage , options );
1351
+
1352
+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1353
+
1354
+ if (list ) {
1355
+ const char * name = list , * url = list ;
1356
+
1357
+ if (!strchr (list , '/' )) {
1358
+ struct remote * remote ;
1359
+
1360
+ /* Look up remote */
1361
+ remote = remote_get (list );
1362
+ if (!remote ) {
1363
+ error ("no such remote: '%s'" , name );
1364
+ return 1 ;
1365
+ }
1366
+ if (!remote -> url .nr ) {
1367
+ return error (_ ("remote '%s' has no URLs" ),
1368
+ name );
1369
+ }
1370
+ url = remote -> url .v [0 ];
1371
+ }
1372
+ res = supports_gvfs_protocol (url , NULL );
1373
+ } else if (set ) {
1374
+ res = set_config ("gvfs.cache-server=%s" , set );
1375
+ } else {
1376
+ char * url = NULL ;
1377
+
1378
+ printf ("Using cache server: %s\n" ,
1379
+ git_config_get_string ("gvfs.cache-server" , & url ) ?
1380
+ "(undefined)" : url );
1381
+ free (url );
1382
+ }
1383
+
1384
+ return !!res ;
1385
+ }
1386
+
1295
1387
static struct {
1296
1388
const char * name ;
1297
1389
int (* fn )(int , const char * * );
@@ -1306,6 +1398,7 @@ static struct {
1306
1398
{ "help" , cmd_help },
1307
1399
{ "version" , cmd_version },
1308
1400
{ "diagnose" , cmd_diagnose },
1401
+ { "cache-server" , cmd_cache_server },
1309
1402
{ NULL , NULL },
1310
1403
};
1311
1404
0 commit comments