@@ -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
@@ -110,6 +117,20 @@ enum {
110
117
TAGS_SET = 2
111
118
};
112
119
120
+ static int add_branch (const char * key , const char * branchname ,
121
+ const char * remotename , int mirror , struct strbuf * tmp )
122
+ {
123
+ strbuf_reset (tmp );
124
+ strbuf_addch (tmp , '+' );
125
+ if (mirror )
126
+ strbuf_addf (tmp , "refs/%s:refs/%s" ,
127
+ branchname , branchname );
128
+ else
129
+ strbuf_addf (tmp , "refs/heads/%s:refs/remotes/%s/%s" ,
130
+ branchname , remotename , branchname );
131
+ return git_config_set_multivar (key , tmp -> buf , "^$" , 0 );
132
+ }
133
+
113
134
static int add (int argc , const char * * argv )
114
135
{
115
136
int fetch = 0 , mirror = 0 , fetch_tags = TAGS_DEFAULT ;
@@ -162,17 +183,8 @@ static int add(int argc, const char **argv)
162
183
if (track .nr == 0 )
163
184
string_list_append ("*" , & track );
164
185
for (i = 0 ; i < track .nr ; i ++ ) {
165
- struct string_list_item * item = track .items + i ;
166
-
167
- strbuf_reset (& buf2 );
168
- strbuf_addch (& buf2 , '+' );
169
- if (mirror )
170
- strbuf_addf (& buf2 , "refs/%s:refs/%s" ,
171
- item -> string , item -> string );
172
- else
173
- strbuf_addf (& buf2 , "refs/heads/%s:refs/remotes/%s/%s" ,
174
- item -> string , name , item -> string );
175
- if (git_config_set_multivar (buf .buf , buf2 .buf , "^$" , 0 ))
186
+ if (add_branch (buf .buf , track .items [i ].string ,
187
+ name , mirror , & buf2 ))
176
188
return 1 ;
177
189
}
178
190
@@ -1284,6 +1296,72 @@ static int update(int argc, const char **argv)
1284
1296
return run_command_v_opt (fetch_argv , RUN_GIT_CMD );
1285
1297
}
1286
1298
1299
+ static int remove_all_fetch_refspecs (const char * remote , const char * key )
1300
+ {
1301
+ return git_config_set_multivar (key , NULL , NULL , 1 );
1302
+ }
1303
+
1304
+ static int add_branches (struct remote * remote , const char * * branches ,
1305
+ const char * key )
1306
+ {
1307
+ const char * remotename = remote -> name ;
1308
+ int mirror = remote -> mirror ;
1309
+ struct strbuf refspec = STRBUF_INIT ;
1310
+
1311
+ for (; * branches ; branches ++ )
1312
+ if (add_branch (key , * branches , remotename , mirror , & refspec )) {
1313
+ strbuf_release (& refspec );
1314
+ return 1 ;
1315
+ }
1316
+
1317
+ strbuf_release (& refspec );
1318
+ return 0 ;
1319
+ }
1320
+
1321
+ static int set_remote_branches (const char * remotename , const char * * branches ,
1322
+ int add_mode )
1323
+ {
1324
+ struct strbuf key = STRBUF_INIT ;
1325
+ struct remote * remote ;
1326
+
1327
+ strbuf_addf (& key , "remote.%s.fetch" , remotename );
1328
+
1329
+ if (!remote_is_configured (remotename ))
1330
+ die ("No such remote '%s'" , remotename );
1331
+ remote = remote_get (remotename );
1332
+
1333
+ if (!add_mode && remove_all_fetch_refspecs (remotename , key .buf )) {
1334
+ strbuf_release (& key );
1335
+ return 1 ;
1336
+ }
1337
+ if (add_branches (remote , branches , key .buf )) {
1338
+ strbuf_release (& key );
1339
+ return 1 ;
1340
+ }
1341
+
1342
+ strbuf_release (& key );
1343
+ return 0 ;
1344
+ }
1345
+
1346
+ static int set_branches (int argc , const char * * argv )
1347
+ {
1348
+ int add_mode = 0 ;
1349
+ struct option options [] = {
1350
+ OPT_BOOLEAN ('\0' , "add" , & add_mode , "add branch" ),
1351
+ OPT_END ()
1352
+ };
1353
+
1354
+ argc = parse_options (argc , argv , NULL , options ,
1355
+ builtin_remote_setbranches_usage , 0 );
1356
+ if (argc == 0 ) {
1357
+ error ("no remote specified" );
1358
+ usage_with_options (builtin_remote_seturl_usage , options );
1359
+ }
1360
+ argv [argc ] = NULL ;
1361
+
1362
+ return set_remote_branches (argv [0 ], argv + 1 , add_mode );
1363
+ }
1364
+
1287
1365
static int set_url (int argc , const char * * argv )
1288
1366
{
1289
1367
int i , push_mode = 0 , add_mode = 0 , delete_mode = 0 ;
@@ -1449,6 +1527,8 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
1449
1527
result = rm (argc , argv );
1450
1528
else if (!strcmp (argv [0 ], "set-head" ))
1451
1529
result = set_head (argc , argv );
1530
+ else if (!strcmp (argv [0 ], "set-branches" ))
1531
+ result = set_branches (argc , argv );
1452
1532
else if (!strcmp (argv [0 ], "set-url" ))
1453
1533
result = set_url (argc , argv );
1454
1534
else if (!strcmp (argv [0 ], "show" ))
0 commit comments