@@ -16,6 +16,7 @@ static const char * const builtin_remote_usage[] = {
16
16
"git remote [-v | --verbose] show [-n] <name>" ,
17
17
"git remote prune [-n | --dry-run] <name>" ,
18
18
"git remote [-v | --verbose] update [-p | --prune] [group | remote]" ,
19
+ "git remote set-branches <name> [--add] <branch>..." ,
19
20
"git remote set-url <name> <newurl> [<oldurl>]" ,
20
21
"git remote set-url --add <name> <newurl>" ,
21
22
"git remote set-url --delete <name> <url>" ,
@@ -42,6 +43,12 @@ static const char * const builtin_remote_sethead_usage[] = {
42
43
NULL
43
44
};
44
45
46
+ static const char * const builtin_remote_setbranches_usage [] = {
47
+ "git remote set-branches <name> <branch>..." ,
48
+ "git remote set-branches --add <name> <branch>..." ,
49
+ NULL
50
+ };
51
+
45
52
static const char * const builtin_remote_show_usage [] = {
46
53
"git remote show [<options>] <name>" ,
47
54
NULL
@@ -104,6 +111,20 @@ static int fetch_remote(const char *name)
104
111
return 0 ;
105
112
}
106
113
114
+ static int add_branch (const char * key , const char * branchname ,
115
+ const char * remotename , int mirror , struct strbuf * tmp )
116
+ {
117
+ strbuf_reset (tmp );
118
+ strbuf_addch (tmp , '+' );
119
+ if (mirror )
120
+ strbuf_addf (tmp , "refs/%s:refs/%s" ,
121
+ branchname , branchname );
122
+ else
123
+ strbuf_addf (tmp , "refs/heads/%s:refs/remotes/%s/%s" ,
124
+ branchname , remotename , branchname );
125
+ return git_config_set_multivar (key , tmp -> buf , "^$" , 0 );
126
+ }
127
+
107
128
static int add (int argc , const char * * argv )
108
129
{
109
130
int fetch = 0 , mirror = 0 ;
@@ -151,17 +172,8 @@ static int add(int argc, const char **argv)
151
172
if (track .nr == 0 )
152
173
string_list_append ("*" , & track );
153
174
for (i = 0 ; i < track .nr ; i ++ ) {
154
- struct string_list_item * item = track .items + i ;
155
-
156
- strbuf_reset (& buf2 );
157
- strbuf_addch (& buf2 , '+' );
158
- if (mirror )
159
- strbuf_addf (& buf2 , "refs/%s:refs/%s" ,
160
- item -> string , item -> string );
161
- else
162
- strbuf_addf (& buf2 , "refs/heads/%s:refs/remotes/%s/%s" ,
163
- item -> string , name , item -> string );
164
- if (git_config_set_multivar (buf .buf , buf2 .buf , "^$" , 0 ))
175
+ if (add_branch (buf .buf , track .items [i ].string ,
176
+ name , mirror , & buf2 ))
165
177
return 1 ;
166
178
}
167
179
@@ -1265,6 +1277,72 @@ static int update(int argc, const char **argv)
1265
1277
return run_command_v_opt (fetch_argv , RUN_GIT_CMD );
1266
1278
}
1267
1279
1280
+ static int remove_all_fetch_refspecs (const char * remote , const char * key )
1281
+ {
1282
+ return git_config_set_multivar (key , NULL , NULL , 1 );
1283
+ }
1284
+
1285
+ static int add_branches (struct remote * remote , const char * * branches ,
1286
+ const char * key )
1287
+ {
1288
+ const char * remotename = remote -> name ;
1289
+ int mirror = remote -> mirror ;
1290
+ struct strbuf refspec = STRBUF_INIT ;
1291
+
1292
+ for (; * branches ; branches ++ )
1293
+ if (add_branch (key , * branches , remotename , mirror , & refspec )) {
1294
+ strbuf_release (& refspec );
1295
+ return 1 ;
1296
+ }
1297
+
1298
+ strbuf_release (& refspec );
1299
+ return 0 ;
1300
+ }
1301
+
1302
+ static int set_remote_branches (const char * remotename , const char * * branches ,
1303
+ int add_mode )
1304
+ {
1305
+ struct strbuf key = STRBUF_INIT ;
1306
+ struct remote * remote ;
1307
+
1308
+ strbuf_addf (& key , "remote.%s.fetch" , remotename );
1309
+
1310
+ if (!remote_is_configured (remotename ))
1311
+ die ("No such remote '%s'" , remotename );
1312
+ remote = remote_get (remotename );
1313
+
1314
+ if (!add_mode && remove_all_fetch_refspecs (remotename , key .buf )) {
1315
+ strbuf_release (& key );
1316
+ return 1 ;
1317
+ }
1318
+ if (add_branches (remote , branches , key .buf )) {
1319
+ strbuf_release (& key );
1320
+ return 1 ;
1321
+ }
1322
+
1323
+ strbuf_release (& key );
1324
+ return 0 ;
1325
+ }
1326
+
1327
+ static int set_branches (int argc , const char * * argv )
1328
+ {
1329
+ int add_mode = 0 ;
1330
+ struct option options [] = {
1331
+ OPT_BOOLEAN ('\0' , "add" , & add_mode , "add branch" ),
1332
+ OPT_END ()
1333
+ };
1334
+
1335
+ argc = parse_options (argc , argv , NULL , options ,
1336
+ builtin_remote_setbranches_usage , 0 );
1337
+ if (argc == 0 ) {
1338
+ error ("no remote specified" );
1339
+ usage_with_options (builtin_remote_seturl_usage , options );
1340
+ }
1341
+ argv [argc ] = NULL ;
1342
+
1343
+ return set_remote_branches (argv [0 ], argv + 1 , add_mode );
1344
+ }
1345
+
1268
1346
static int set_url (int argc , const char * * argv )
1269
1347
{
1270
1348
int i , push_mode = 0 , add_mode = 0 , delete_mode = 0 ;
@@ -1430,6 +1508,8 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
1430
1508
result = rm (argc , argv );
1431
1509
else if (!strcmp (argv [0 ], "set-head" ))
1432
1510
result = set_head (argc , argv );
1511
+ else if (!strcmp (argv [0 ], "set-branches" ))
1512
+ result = set_branches (argc , argv );
1433
1513
else if (!strcmp (argv [0 ], "set-url" ))
1434
1514
result = set_url (argc , argv );
1435
1515
else if (!strcmp (argv [0 ], "show" ))
0 commit comments