9
9
10
10
static const char * const builtin_remote_usage [] = {
11
11
"git remote [-v | --verbose]" ,
12
- "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>" ,
12
+ "git remote add [-t <branch>] [-m <master>] [-f] [--mirror=<fetch|push> ] <name> <url>" ,
13
13
"git remote rename <old> <new>" ,
14
14
"git remote rm <name>" ,
15
15
"git remote set-head <name> (-a | -d | <branch>)" ,
@@ -117,6 +117,11 @@ enum {
117
117
TAGS_SET = 2
118
118
};
119
119
120
+ #define MIRROR_NONE 0
121
+ #define MIRROR_FETCH 1
122
+ #define MIRROR_PUSH 2
123
+ #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
124
+
120
125
static int add_branch (const char * key , const char * branchname ,
121
126
const char * remotename , int mirror , struct strbuf * tmp )
122
127
{
@@ -131,9 +136,32 @@ static int add_branch(const char *key, const char *branchname,
131
136
return git_config_set_multivar (key , tmp -> buf , "^$" , 0 );
132
137
}
133
138
139
+ static const char mirror_advice [] =
140
+ "--mirror is dangerous and deprecated; please\n"
141
+ "\t use --mirror=fetch or --mirror=push instead" ;
142
+
143
+ static int parse_mirror_opt (const struct option * opt , const char * arg , int not )
144
+ {
145
+ unsigned * mirror = opt -> value ;
146
+ if (not )
147
+ * mirror = MIRROR_NONE ;
148
+ else if (!arg ) {
149
+ warning ("%s" , mirror_advice );
150
+ * mirror = MIRROR_BOTH ;
151
+ }
152
+ else if (!strcmp (arg , "fetch" ))
153
+ * mirror = MIRROR_FETCH ;
154
+ else if (!strcmp (arg , "push" ))
155
+ * mirror = MIRROR_PUSH ;
156
+ else
157
+ return error ("unknown mirror argument: %s" , arg );
158
+ return 0 ;
159
+ }
160
+
134
161
static int add (int argc , const char * * argv )
135
162
{
136
- int fetch = 0 , mirror = 0 , fetch_tags = TAGS_DEFAULT ;
163
+ int fetch = 0 , fetch_tags = TAGS_DEFAULT ;
164
+ unsigned mirror = MIRROR_NONE ;
137
165
struct string_list track = STRING_LIST_INIT_NODUP ;
138
166
const char * master = NULL ;
139
167
struct remote * remote ;
@@ -151,7 +179,9 @@ static int add(int argc, const char **argv)
151
179
OPT_CALLBACK ('t' , "track" , & track , "branch" ,
152
180
"branch(es) to track" , opt_parse_track ),
153
181
OPT_STRING ('m' , "master" , & master , "branch" , "master branch" ),
154
- OPT_BOOLEAN (0 , "mirror" , & mirror , "no separate remotes" ),
182
+ { OPTION_CALLBACK , 0 , "mirror" , & mirror , "push|fetch" ,
183
+ "set up remote as a mirror to push to or fetch from" ,
184
+ PARSE_OPT_OPTARG , parse_mirror_opt },
155
185
OPT_END ()
156
186
};
157
187
@@ -161,6 +191,11 @@ static int add(int argc, const char **argv)
161
191
if (argc < 2 )
162
192
usage_with_options (builtin_remote_add_usage , options );
163
193
194
+ if (mirror && master )
195
+ die ("specifying a master branch makes no sense with --mirror" );
196
+ if (mirror && track .nr )
197
+ die ("specifying branches to track makes no sense with --mirror" );
198
+
164
199
name = argv [0 ];
165
200
url = argv [1 ];
166
201
@@ -177,18 +212,19 @@ static int add(int argc, const char **argv)
177
212
if (git_config_set (buf .buf , url ))
178
213
return 1 ;
179
214
180
- strbuf_reset (& buf );
181
- strbuf_addf (& buf , "remote.%s.fetch" , name );
182
-
183
- if (track .nr == 0 )
184
- string_list_append (& track , "*" );
185
- for (i = 0 ; i < track .nr ; i ++ ) {
186
- if (add_branch (buf .buf , track .items [i ].string ,
187
- name , mirror , & buf2 ))
188
- return 1 ;
215
+ if (!mirror || mirror & MIRROR_FETCH ) {
216
+ strbuf_reset (& buf );
217
+ strbuf_addf (& buf , "remote.%s.fetch" , name );
218
+ if (track .nr == 0 )
219
+ string_list_append (& track , "*" );
220
+ for (i = 0 ; i < track .nr ; i ++ ) {
221
+ if (add_branch (buf .buf , track .items [i ].string ,
222
+ name , mirror , & buf2 ))
223
+ return 1 ;
224
+ }
189
225
}
190
226
191
- if (mirror ) {
227
+ if (mirror & MIRROR_PUSH ) {
192
228
strbuf_reset (& buf );
193
229
strbuf_addf (& buf , "remote.%s.mirror" , name );
194
230
if (git_config_set (buf .buf , "true" ))
0 commit comments