21
21
#include "wrapper.h"
22
22
#include "trace2.h"
23
23
#include "json-parser.h"
24
+ #include "remote.h"
24
25
25
26
static int is_unattended (void ) {
26
27
return git_env_bool ("Scalar_UNATTENDED" , 0 );
@@ -355,6 +356,21 @@ static int set_config(const char *fmt, ...)
355
356
return res ;
356
357
}
357
358
359
+ static int list_cache_server_urls (struct json_iterator * it )
360
+ {
361
+ const char * p ;
362
+ char * q ;
363
+ long l ;
364
+
365
+ if (it -> type == JSON_STRING &&
366
+ skip_iprefix (it -> key .buf , ".CacheServers[" , & p ) &&
367
+ (l = strtol (p , & q , 10 )) >= 0 && p != q &&
368
+ !strcasecmp (q , "].Url" ))
369
+ printf ("#%ld: %s\n" , l , it -> string_value .buf );
370
+
371
+ return 0 ;
372
+ }
373
+
358
374
/* Find N for which .CacheServers[N].GlobalDefault == true */
359
375
static int get_cache_server_index (struct json_iterator * it )
360
376
{
@@ -424,6 +440,16 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
424
440
JSON_ITERATOR_INIT (out .buf , get_cache_server_index , & l );
425
441
struct cache_server_url_data data = { .url = NULL };
426
442
443
+ if (!cache_server_url ) {
444
+ it .fn = list_cache_server_urls ;
445
+ if (iterate_json (& it ) < 0 ) {
446
+ strbuf_release (& out );
447
+ return error ("JSON parse error" );
448
+ }
449
+ strbuf_release (& out );
450
+ return 0 ;
451
+ }
452
+
427
453
if (iterate_json (& it ) < 0 ) {
428
454
strbuf_release (& out );
429
455
return error ("JSON parse error" );
@@ -440,7 +466,9 @@ static int supports_gvfs_protocol(const char *url, char **cache_server_url)
440
466
return 1 ;
441
467
}
442
468
strbuf_release (& out );
443
- return 0 ; /* error out quietly */
469
+ /* error out quietly, unless we wanted to list URLs */
470
+ return cache_server_url ?
471
+ 0 : error (_ ("Could not access gvfs/config endpoint" ));
444
472
}
445
473
446
474
static char * default_cache_root (const char * root )
@@ -1234,6 +1262,77 @@ static int cmd_version(int argc, const char **argv)
1234
1262
return 0 ;
1235
1263
}
1236
1264
1265
+ static int cmd_cache_server (int argc , const char * * argv )
1266
+ {
1267
+ int get = 0 ;
1268
+ char * set = NULL , * list = NULL ;
1269
+ const char * default_remote = "(default)" ;
1270
+ struct option options [] = {
1271
+ OPT_BOOL (0 , "get" , & get ,
1272
+ N_ ("get the configured cache-server URL" )),
1273
+ OPT_STRING (0 , "set" , & set , N_ ("URL" ),
1274
+ N_ ("configure the cache-server to use" )),
1275
+ { OPTION_STRING , 0 , "list" , & list , N_ ("remote" ),
1276
+ N_ ("list the possible cache-server URLs" ),
1277
+ PARSE_OPT_OPTARG , NULL , (intptr_t ) default_remote },
1278
+ OPT_END (),
1279
+ };
1280
+ const char * const usage [] = {
1281
+ N_ ("scalar cache_server "
1282
+ "[--get | --set <url> | --list [<remote>]] [<enlistment>]" ),
1283
+ NULL
1284
+ };
1285
+ int res = 0 ;
1286
+
1287
+ argc = parse_options (argc , argv , NULL , options ,
1288
+ usage , 0 );
1289
+
1290
+ if (get + !!set + !!list > 1 )
1291
+ usage_msg_opt (_ ("--get/--set/--list are mutually exclusive" ),
1292
+ usage , options );
1293
+
1294
+ setup_enlistment_directory (argc , argv , usage , options , NULL );
1295
+
1296
+ if (list ) {
1297
+ const char * name = list , * url = list ;
1298
+
1299
+ if (list == default_remote )
1300
+ list = NULL ;
1301
+
1302
+ if (!list || !strchr (list , '/' )) {
1303
+ struct remote * remote ;
1304
+
1305
+ /* Look up remote */
1306
+ remote = remote_get (list );
1307
+ if (!remote ) {
1308
+ error ("no such remote: '%s'" , name );
1309
+ free (list );
1310
+ return 1 ;
1311
+ }
1312
+ if (!remote -> url ) {
1313
+ free (list );
1314
+ return error (_ ("remote '%s' has no URLs" ),
1315
+ name );
1316
+ }
1317
+ url = remote -> url [0 ];
1318
+ }
1319
+ res = supports_gvfs_protocol (url , NULL );
1320
+ free (list );
1321
+ } else if (set ) {
1322
+ res = set_config ("gvfs.cache-server=%s" , set );
1323
+ free (set );
1324
+ } else {
1325
+ char * url = NULL ;
1326
+
1327
+ printf ("Using cache server: %s\n" ,
1328
+ git_config_get_string ("gvfs.cache-server" , & url ) ?
1329
+ "(undefined)" : url );
1330
+ free (url );
1331
+ }
1332
+
1333
+ return !!res ;
1334
+ }
1335
+
1237
1336
static struct {
1238
1337
const char * name ;
1239
1338
int (* fn )(int , const char * * );
@@ -1248,6 +1347,7 @@ static struct {
1248
1347
{ "help" , cmd_help },
1249
1348
{ "version" , cmd_version },
1250
1349
{ "diagnose" , cmd_diagnose },
1350
+ { "cache-server" , cmd_cache_server },
1251
1351
{ NULL , NULL },
1252
1352
};
1253
1353
0 commit comments