]>
Commit | Line | Data |
---|---|---|
c2e86add | 1 | #include "builtin.h" |
b2141fc1 | 2 | #include "config.h" |
211c8968 JS |
3 | #include "parse-options.h" |
4 | #include "transport.h" | |
5 | #include "remote.h" | |
c455c87c | 6 | #include "string-list.h" |
211c8968 JS |
7 | #include "strbuf.h" |
8 | #include "run-command.h" | |
88f8576e | 9 | #include "rebase.h" |
211c8968 | 10 | #include "refs.h" |
ec0cb496 | 11 | #include "refspec.h" |
cbd53a21 | 12 | #include "object-store.h" |
8607590e | 13 | #include "argv-array.h" |
1d614d41 | 14 | #include "commit-reach.h" |
211c8968 JS |
15 | |
16 | static const char * const builtin_remote_usage[] = { | |
fbbae140 | 17 | N_("git remote [-v | --verbose]"), |
9c9b4f2f | 18 | N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"), |
fbbae140 | 19 | N_("git remote rename <old> <new>"), |
90585604 | 20 | N_("git remote remove <name>"), |
9c9b4f2f | 21 | N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), |
fbbae140 NTND |
22 | N_("git remote [-v | --verbose] show [-n] <name>"), |
23 | N_("git remote prune [-n | --dry-run] <name>"), | |
24 | N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"), | |
25 | N_("git remote set-branches [--add] <name> <branch>..."), | |
96f78d39 | 26 | N_("git remote get-url [--push] [--all] <name>"), |
fbbae140 NTND |
27 | N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), |
28 | N_("git remote set-url --add <name> <newurl>"), | |
29 | N_("git remote set-url --delete <name> <url>"), | |
211c8968 JS |
30 | NULL |
31 | }; | |
32 | ||
4504107d | 33 | static const char * const builtin_remote_add_usage[] = { |
fbbae140 | 34 | N_("git remote add [<options>] <name> <url>"), |
4504107d TH |
35 | NULL |
36 | }; | |
37 | ||
38 | static const char * const builtin_remote_rename_usage[] = { | |
fbbae140 | 39 | N_("git remote rename <old> <new>"), |
4504107d TH |
40 | NULL |
41 | }; | |
42 | ||
43 | static const char * const builtin_remote_rm_usage[] = { | |
90585604 | 44 | N_("git remote remove <name>"), |
4504107d TH |
45 | NULL |
46 | }; | |
47 | ||
48 | static const char * const builtin_remote_sethead_usage[] = { | |
e49c8f33 | 49 | N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), |
4504107d TH |
50 | NULL |
51 | }; | |
52 | ||
3d8b6949 | 53 | static const char * const builtin_remote_setbranches_usage[] = { |
fbbae140 NTND |
54 | N_("git remote set-branches <name> <branch>..."), |
55 | N_("git remote set-branches --add <name> <branch>..."), | |
3d8b6949 JN |
56 | NULL |
57 | }; | |
58 | ||
4504107d | 59 | static const char * const builtin_remote_show_usage[] = { |
fbbae140 | 60 | N_("git remote show [<options>] <name>"), |
4504107d TH |
61 | NULL |
62 | }; | |
63 | ||
64 | static const char * const builtin_remote_prune_usage[] = { | |
fbbae140 | 65 | N_("git remote prune [<options>] <name>"), |
4504107d TH |
66 | NULL |
67 | }; | |
68 | ||
69 | static const char * const builtin_remote_update_usage[] = { | |
fbbae140 | 70 | N_("git remote update [<options>] [<group> | <remote>]..."), |
4504107d TH |
71 | NULL |
72 | }; | |
73 | ||
96f78d39 BB |
74 | static const char * const builtin_remote_geturl_usage[] = { |
75 | N_("git remote get-url [--push] [--all] <name>"), | |
76 | NULL | |
77 | }; | |
78 | ||
433f2be1 | 79 | static const char * const builtin_remote_seturl_usage[] = { |
fbbae140 NTND |
80 | N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), |
81 | N_("git remote set-url --add <name> <newurl>"), | |
82 | N_("git remote set-url --delete <name> <url>"), | |
433f2be1 IL |
83 | NULL |
84 | }; | |
85 | ||
e61e0cc6 JS |
86 | #define GET_REF_STATES (1<<0) |
87 | #define GET_HEAD_NAMES (1<<1) | |
e5dcbfd9 | 88 | #define GET_PUSH_REF_STATES (1<<2) |
e61e0cc6 | 89 | |
211c8968 JS |
90 | static int verbose; |
91 | ||
211c8968 JS |
92 | static int fetch_remote(const char *name) |
93 | { | |
dbbd56f1 CR |
94 | const char *argv[] = { "fetch", name, NULL, NULL }; |
95 | if (verbose) { | |
96 | argv[1] = "-v"; | |
97 | argv[2] = name; | |
98 | } | |
bb16d5dd | 99 | printf_ln(_("Updating %s"), name); |
211c8968 | 100 | if (run_command_v_opt(argv, RUN_GIT_CMD)) |
bb16d5dd | 101 | return error(_("Could not fetch %s"), name); |
211c8968 JS |
102 | return 0; |
103 | } | |
104 | ||
111fb858 ST |
105 | enum { |
106 | TAGS_UNSET = 0, | |
107 | TAGS_DEFAULT = 1, | |
108 | TAGS_SET = 2 | |
109 | }; | |
110 | ||
a9f5a355 JK |
111 | #define MIRROR_NONE 0 |
112 | #define MIRROR_FETCH 1 | |
113 | #define MIRROR_PUSH 2 | |
114 | #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH) | |
115 | ||
ab5e4b67 PS |
116 | static void add_branch(const char *key, const char *branchname, |
117 | const char *remotename, int mirror, struct strbuf *tmp) | |
3d8b6949 JN |
118 | { |
119 | strbuf_reset(tmp); | |
120 | strbuf_addch(tmp, '+'); | |
121 | if (mirror) | |
122 | strbuf_addf(tmp, "refs/%s:refs/%s", | |
123 | branchname, branchname); | |
124 | else | |
125 | strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s", | |
126 | branchname, remotename, branchname); | |
3d180648 | 127 | git_config_set_multivar(key, tmp->buf, "^$", 0); |
3d8b6949 JN |
128 | } |
129 | ||
09902486 | 130 | static const char mirror_advice[] = |
bb16d5dd NTND |
131 | N_("--mirror is dangerous and deprecated; please\n" |
132 | "\t use --mirror=fetch or --mirror=push instead"); | |
09902486 | 133 | |
a9f5a355 JK |
134 | static int parse_mirror_opt(const struct option *opt, const char *arg, int not) |
135 | { | |
136 | unsigned *mirror = opt->value; | |
137 | if (not) | |
138 | *mirror = MIRROR_NONE; | |
09902486 | 139 | else if (!arg) { |
bb16d5dd | 140 | warning("%s", _(mirror_advice)); |
a9f5a355 | 141 | *mirror = MIRROR_BOTH; |
09902486 | 142 | } |
a9f5a355 JK |
143 | else if (!strcmp(arg, "fetch")) |
144 | *mirror = MIRROR_FETCH; | |
145 | else if (!strcmp(arg, "push")) | |
146 | *mirror = MIRROR_PUSH; | |
147 | else | |
bb16d5dd | 148 | return error(_("unknown mirror argument: %s"), arg); |
a9f5a355 JK |
149 | return 0; |
150 | } | |
151 | ||
211c8968 JS |
152 | static int add(int argc, const char **argv) |
153 | { | |
a9f5a355 JK |
154 | int fetch = 0, fetch_tags = TAGS_DEFAULT; |
155 | unsigned mirror = MIRROR_NONE; | |
183113a5 | 156 | struct string_list track = STRING_LIST_INIT_NODUP; |
211c8968 JS |
157 | const char *master = NULL; |
158 | struct remote *remote; | |
f285a2d7 | 159 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
211c8968 JS |
160 | const char *name, *url; |
161 | int i; | |
162 | ||
163 | struct option options[] = { | |
d5d09d47 | 164 | OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")), |
111fb858 | 165 | OPT_SET_INT(0, "tags", &fetch_tags, |
fbbae140 | 166 | N_("import all tags and associated objects when fetching"), |
111fb858 ST |
167 | TAGS_SET), |
168 | OPT_SET_INT(0, NULL, &fetch_tags, | |
fbbae140 NTND |
169 | N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET), |
170 | OPT_STRING_LIST('t', "track", &track, N_("branch"), | |
171 | N_("branch(es) to track")), | |
172 | OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")), | |
203c8533 | 173 | OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)", |
fbbae140 | 174 | N_("set up remote as a mirror to push to or fetch from"), |
203c8533 | 175 | PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt), |
211c8968 JS |
176 | OPT_END() |
177 | }; | |
178 | ||
4504107d | 179 | argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage, |
37782920 | 180 | 0); |
211c8968 | 181 | |
2d2e3d25 | 182 | if (argc != 2) |
4504107d | 183 | usage_with_options(builtin_remote_add_usage, options); |
211c8968 | 184 | |
13fc2c18 | 185 | if (mirror && master) |
bb16d5dd | 186 | die(_("specifying a master branch makes no sense with --mirror")); |
3eafdc96 | 187 | if (mirror && !(mirror & MIRROR_FETCH) && track.nr) |
bb16d5dd | 188 | die(_("specifying branches to track makes sense only with fetch mirrors")); |
13fc2c18 | 189 | |
211c8968 JS |
190 | name = argv[0]; |
191 | url = argv[1]; | |
192 | ||
193 | remote = remote_get(name); | |
e459b073 | 194 | if (remote_is_configured(remote, 1)) |
bb16d5dd | 195 | die(_("remote %s already exists."), name); |
211c8968 | 196 | |
24b6177e JF |
197 | strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name); |
198 | if (!valid_fetch_refspec(buf2.buf)) | |
bb16d5dd | 199 | die(_("'%s' is not a valid remote name"), name); |
24b6177e | 200 | |
211c8968 | 201 | strbuf_addf(&buf, "remote.%s.url", name); |
3d180648 | 202 | git_config_set(buf.buf, url); |
211c8968 | 203 | |
a9f5a355 JK |
204 | if (!mirror || mirror & MIRROR_FETCH) { |
205 | strbuf_reset(&buf); | |
206 | strbuf_addf(&buf, "remote.%s.fetch", name); | |
207 | if (track.nr == 0) | |
208 | string_list_append(&track, "*"); | |
209 | for (i = 0; i < track.nr; i++) { | |
ab5e4b67 PS |
210 | add_branch(buf.buf, track.items[i].string, |
211 | name, mirror, &buf2); | |
a9f5a355 | 212 | } |
211c8968 JS |
213 | } |
214 | ||
a9f5a355 | 215 | if (mirror & MIRROR_PUSH) { |
84bb2dfd PB |
216 | strbuf_reset(&buf); |
217 | strbuf_addf(&buf, "remote.%s.mirror", name); | |
3d180648 | 218 | git_config_set(buf.buf, "true"); |
84bb2dfd PB |
219 | } |
220 | ||
111fb858 ST |
221 | if (fetch_tags != TAGS_DEFAULT) { |
222 | strbuf_reset(&buf); | |
223 | strbuf_addf(&buf, "remote.%s.tagopt", name); | |
3d180648 PS |
224 | git_config_set(buf.buf, |
225 | fetch_tags == TAGS_SET ? "--tags" : "--no-tags"); | |
111fb858 ST |
226 | } |
227 | ||
211c8968 JS |
228 | if (fetch && fetch_remote(name)) |
229 | return 1; | |
230 | ||
231 | if (master) { | |
232 | strbuf_reset(&buf); | |
233 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); | |
234 | ||
235 | strbuf_reset(&buf2); | |
236 | strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); | |
237 | ||
238 | if (create_symref(buf.buf, buf2.buf, "remote add")) | |
bb16d5dd | 239 | return error(_("Could not setup master '%s'"), master); |
211c8968 JS |
240 | } |
241 | ||
242 | strbuf_release(&buf); | |
243 | strbuf_release(&buf2); | |
c455c87c | 244 | string_list_clear(&track, 0); |
211c8968 JS |
245 | |
246 | return 0; | |
247 | } | |
248 | ||
249 | struct branch_info { | |
e0cc81e6 | 250 | char *remote_name; |
c455c87c | 251 | struct string_list merge; |
88f8576e | 252 | enum rebase_type rebase; |
923d4a5c | 253 | char *push_remote_name; |
211c8968 JS |
254 | }; |
255 | ||
2721ce21 | 256 | static struct string_list branch_list = STRING_LIST_INIT_NODUP; |
211c8968 | 257 | |
72972eb3 JH |
258 | static const char *abbrev_ref(const char *name, const char *prefix) |
259 | { | |
cf4fff57 | 260 | skip_prefix(name, prefix, &name); |
72972eb3 JH |
261 | return name; |
262 | } | |
263 | #define abbrev_branch(name) abbrev_ref((name), "refs/heads/") | |
264 | ||
ef90d6d4 | 265 | static int config_read_branches(const char *key, const char *value, void *cb) |
211c8968 | 266 | { |
1a83068c BW |
267 | const char *orig_key = key; |
268 | char *name; | |
269 | struct string_list_item *item; | |
270 | struct branch_info *info; | |
923d4a5c | 271 | enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type; |
1a83068c BW |
272 | size_t key_len; |
273 | ||
274 | if (!starts_with(key, "branch.")) | |
275 | return 0; | |
276 | ||
ceff1a13 BW |
277 | key += strlen("branch."); |
278 | if (strip_suffix(key, ".remote", &key_len)) | |
1a83068c | 279 | type = REMOTE; |
ceff1a13 | 280 | else if (strip_suffix(key, ".merge", &key_len)) |
1a83068c | 281 | type = MERGE; |
ceff1a13 | 282 | else if (strip_suffix(key, ".rebase", &key_len)) |
1a83068c | 283 | type = REBASE; |
923d4a5c BW |
284 | else if (strip_suffix(key, ".pushremote", &key_len)) |
285 | type = PUSH_REMOTE; | |
ceff1a13 | 286 | else |
1a83068c | 287 | return 0; |
ceff1a13 | 288 | name = xmemdupz(key, key_len); |
1a83068c BW |
289 | |
290 | item = string_list_insert(&branch_list, name); | |
291 | ||
292 | if (!item->util) | |
293 | item->util = xcalloc(1, sizeof(struct branch_info)); | |
294 | info = item->util; | |
ceff1a13 BW |
295 | switch (type) { |
296 | case REMOTE: | |
1a83068c BW |
297 | if (info->remote_name) |
298 | warning(_("more than one %s"), orig_key); | |
299 | info->remote_name = xstrdup(value); | |
ceff1a13 BW |
300 | break; |
301 | case MERGE: { | |
1a83068c BW |
302 | char *space = strchr(value, ' '); |
303 | value = abbrev_branch(value); | |
304 | while (space) { | |
305 | char *merge; | |
306 | merge = xstrndup(value, space - value); | |
307 | string_list_append(&info->merge, merge); | |
308 | value = abbrev_branch(space + 1); | |
309 | space = strchr(value, ' '); | |
310 | } | |
311 | string_list_append(&info->merge, xstrdup(value)); | |
ceff1a13 BW |
312 | break; |
313 | } | |
314 | case REBASE: | |
1a83068c BW |
315 | /* |
316 | * Consider invalid values as false and check the | |
317 | * truth value with >= REBASE_TRUE. | |
318 | */ | |
319 | info->rebase = rebase_parse_value(value); | |
ceff1a13 | 320 | break; |
923d4a5c BW |
321 | case PUSH_REMOTE: |
322 | if (info->push_remote_name) | |
323 | warning(_("more than one %s"), orig_key); | |
324 | info->push_remote_name = xstrdup(value); | |
325 | break; | |
ceff1a13 BW |
326 | default: |
327 | BUG("unexpected type=%d", type); | |
328 | } | |
1a83068c | 329 | |
211c8968 JS |
330 | return 0; |
331 | } | |
332 | ||
333 | static void read_branches(void) | |
334 | { | |
335 | if (branch_list.nr) | |
336 | return; | |
ef90d6d4 | 337 | git_config(config_read_branches, NULL); |
211c8968 JS |
338 | } |
339 | ||
340 | struct ref_states { | |
341 | struct remote *remote; | |
b537e0b1 | 342 | struct string_list new_refs, stale, tracked, heads, push; |
7ecbbf87 | 343 | int queried; |
211c8968 JS |
344 | }; |
345 | ||
e0cc81e6 | 346 | static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) |
211c8968 JS |
347 | { |
348 | struct ref *fetch_map = NULL, **tail = &fetch_map; | |
f2ef6075 | 349 | struct ref *ref, *stale_refs; |
211c8968 JS |
350 | int i; |
351 | ||
e5349abf BW |
352 | for (i = 0; i < states->remote->fetch.nr; i++) |
353 | if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1)) | |
bb16d5dd | 354 | die(_("Could not get fetch map for refspec %s"), |
e5349abf | 355 | states->remote->fetch.raw[i]); |
211c8968 | 356 | |
b537e0b1 | 357 | states->new_refs.strdup_strings = 1; |
92f676fc BW |
358 | states->tracked.strdup_strings = 1; |
359 | states->stale.strdup_strings = 1; | |
211c8968 | 360 | for (ref = fetch_map; ref; ref = ref->next) { |
c6893323 | 361 | if (!ref->peer_ref || !ref_exists(ref->peer_ref->name)) |
b537e0b1 | 362 | string_list_append(&states->new_refs, abbrev_branch(ref->name)); |
7b9a5e27 | 363 | else |
1d2f80fa | 364 | string_list_append(&states->tracked, abbrev_branch(ref->name)); |
211c8968 | 365 | } |
a2ac50cb | 366 | stale_refs = get_stale_heads(&states->remote->fetch, fetch_map); |
f2ef6075 JS |
367 | for (ref = stale_refs; ref; ref = ref->next) { |
368 | struct string_list_item *item = | |
1d2f80fa | 369 | string_list_append(&states->stale, abbrev_branch(ref->name)); |
f2ef6075 JS |
370 | item->util = xstrdup(ref->name); |
371 | } | |
372 | free_refs(stale_refs); | |
211c8968 JS |
373 | free_refs(fetch_map); |
374 | ||
b537e0b1 | 375 | string_list_sort(&states->new_refs); |
3383e199 MH |
376 | string_list_sort(&states->tracked); |
377 | string_list_sort(&states->stale); | |
211c8968 JS |
378 | |
379 | return 0; | |
380 | } | |
381 | ||
e5dcbfd9 JS |
382 | struct push_info { |
383 | char *dest; | |
384 | int forced; | |
385 | enum { | |
386 | PUSH_STATUS_CREATE = 0, | |
387 | PUSH_STATUS_DELETE, | |
388 | PUSH_STATUS_UPTODATE, | |
389 | PUSH_STATUS_FASTFORWARD, | |
390 | PUSH_STATUS_OUTOFDATE, | |
4b05548f | 391 | PUSH_STATUS_NOTQUERIED |
e5dcbfd9 JS |
392 | } status; |
393 | }; | |
394 | ||
395 | static int get_push_ref_states(const struct ref *remote_refs, | |
396 | struct ref_states *states) | |
397 | { | |
398 | struct remote *remote = states->remote; | |
6d2bf96e | 399 | struct ref *ref, *local_refs, *push_map; |
e5dcbfd9 JS |
400 | if (remote->mirror) |
401 | return 0; | |
402 | ||
403 | local_refs = get_local_heads(); | |
6a01554e | 404 | push_map = copy_ref_list(remote_refs); |
e5dcbfd9 | 405 | |
5c7ec846 | 406 | match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE); |
e5dcbfd9 JS |
407 | |
408 | states->push.strdup_strings = 1; | |
409 | for (ref = push_map; ref; ref = ref->next) { | |
410 | struct string_list_item *item; | |
411 | struct push_info *info; | |
412 | ||
413 | if (!ref->peer_ref) | |
414 | continue; | |
f4e54d02 | 415 | oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); |
e5dcbfd9 | 416 | |
1d2f80fa JP |
417 | item = string_list_append(&states->push, |
418 | abbrev_branch(ref->peer_ref->name)); | |
38069454 | 419 | item->util = xcalloc(1, sizeof(struct push_info)); |
e5dcbfd9 JS |
420 | info = item->util; |
421 | info->forced = ref->force; | |
422 | info->dest = xstrdup(abbrev_branch(ref->name)); | |
423 | ||
f4e54d02 | 424 | if (is_null_oid(&ref->new_oid)) { |
e5dcbfd9 | 425 | info->status = PUSH_STATUS_DELETE; |
4a7e27e9 | 426 | } else if (oideq(&ref->old_oid, &ref->new_oid)) |
e5dcbfd9 | 427 | info->status = PUSH_STATUS_UPTODATE; |
f4e54d02 | 428 | else if (is_null_oid(&ref->old_oid)) |
e5dcbfd9 | 429 | info->status = PUSH_STATUS_CREATE; |
f4e54d02 | 430 | else if (has_object_file(&ref->old_oid) && |
6f3d57b6 | 431 | ref_newer(&ref->new_oid, &ref->old_oid)) |
e5dcbfd9 JS |
432 | info->status = PUSH_STATUS_FASTFORWARD; |
433 | else | |
434 | info->status = PUSH_STATUS_OUTOFDATE; | |
e5dcbfd9 JS |
435 | } |
436 | free_refs(local_refs); | |
437 | free_refs(push_map); | |
438 | return 0; | |
439 | } | |
440 | ||
441 | static int get_push_ref_states_noquery(struct ref_states *states) | |
442 | { | |
443 | int i; | |
444 | struct remote *remote = states->remote; | |
445 | struct string_list_item *item; | |
446 | struct push_info *info; | |
447 | ||
448 | if (remote->mirror) | |
449 | return 0; | |
450 | ||
451 | states->push.strdup_strings = 1; | |
6bdb304b | 452 | if (!remote->push.nr) { |
bb16d5dd | 453 | item = string_list_append(&states->push, _("(matching)")); |
38069454 | 454 | info = item->util = xcalloc(1, sizeof(struct push_info)); |
e5dcbfd9 JS |
455 | info->status = PUSH_STATUS_NOTQUERIED; |
456 | info->dest = xstrdup(item->string); | |
457 | } | |
6bdb304b BW |
458 | for (i = 0; i < remote->push.nr; i++) { |
459 | const struct refspec_item *spec = &remote->push.items[i]; | |
e5dcbfd9 | 460 | if (spec->matching) |
bb16d5dd | 461 | item = string_list_append(&states->push, _("(matching)")); |
5ad6b025 | 462 | else if (strlen(spec->src)) |
1d2f80fa | 463 | item = string_list_append(&states->push, spec->src); |
e5dcbfd9 | 464 | else |
bb16d5dd | 465 | item = string_list_append(&states->push, _("(delete)")); |
e5dcbfd9 | 466 | |
38069454 | 467 | info = item->util = xcalloc(1, sizeof(struct push_info)); |
e5dcbfd9 JS |
468 | info->forced = spec->force; |
469 | info->status = PUSH_STATUS_NOTQUERIED; | |
5ad6b025 | 470 | info->dest = xstrdup(spec->dst ? spec->dst : item->string); |
e5dcbfd9 JS |
471 | } |
472 | return 0; | |
473 | } | |
474 | ||
e61e0cc6 JS |
475 | static int get_head_names(const struct ref *remote_refs, struct ref_states *states) |
476 | { | |
477 | struct ref *ref, *matches; | |
478 | struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; | |
0ad4a5ff | 479 | struct refspec_item refspec; |
e61e0cc6 JS |
480 | |
481 | refspec.force = 0; | |
482 | refspec.pattern = 1; | |
5ad6b025 | 483 | refspec.src = refspec.dst = "refs/heads/*"; |
e61e0cc6 JS |
484 | states->heads.strdup_strings = 1; |
485 | get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); | |
486 | matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), | |
487 | fetch_map, 1); | |
eeefa7c9 | 488 | for (ref = matches; ref; ref = ref->next) |
1d2f80fa | 489 | string_list_append(&states->heads, abbrev_branch(ref->name)); |
e61e0cc6 JS |
490 | |
491 | free_refs(fetch_map); | |
492 | free_refs(matches); | |
493 | ||
494 | return 0; | |
495 | } | |
496 | ||
7ad2458f SP |
497 | struct known_remote { |
498 | struct known_remote *next; | |
499 | struct remote *remote; | |
500 | }; | |
501 | ||
502 | struct known_remotes { | |
503 | struct remote *to_delete; | |
504 | struct known_remote *list; | |
505 | }; | |
506 | ||
507 | static int add_known_remote(struct remote *remote, void *cb_data) | |
508 | { | |
509 | struct known_remotes *all = cb_data; | |
510 | struct known_remote *r; | |
511 | ||
512 | if (!strcmp(all->to_delete->name, remote->name)) | |
513 | return 0; | |
514 | ||
515 | r = xmalloc(sizeof(*r)); | |
516 | r->remote = remote; | |
517 | r->next = all->list; | |
518 | all->list = r; | |
519 | return 0; | |
520 | } | |
521 | ||
211c8968 | 522 | struct branches_for_remote { |
7ad2458f | 523 | struct remote *remote; |
441adf0c | 524 | struct string_list *branches, *skipped; |
7ad2458f | 525 | struct known_remotes *keep; |
211c8968 JS |
526 | }; |
527 | ||
528 | static int add_branch_for_removal(const char *refname, | |
45690a57 | 529 | const struct object_id *oid, int flags, void *cb_data) |
211c8968 JS |
530 | { |
531 | struct branches_for_remote *branches = cb_data; | |
0ad4a5ff | 532 | struct refspec_item refspec; |
7ad2458f | 533 | struct known_remote *kr; |
211c8968 | 534 | |
7ad2458f SP |
535 | memset(&refspec, 0, sizeof(refspec)); |
536 | refspec.dst = (char *)refname; | |
537 | if (remote_find_tracking(branches->remote, &refspec)) | |
538 | return 0; | |
539 | ||
540 | /* don't delete a branch if another remote also uses it */ | |
541 | for (kr = branches->keep->list; kr; kr = kr->next) { | |
542 | memset(&refspec, 0, sizeof(refspec)); | |
543 | refspec.dst = (char *)refname; | |
544 | if (!remote_find_tracking(kr->remote, &refspec)) | |
545 | return 0; | |
546 | } | |
3b9dcff5 | 547 | |
13931236 | 548 | /* don't delete non-remote-tracking refs */ |
59556548 | 549 | if (!starts_with(refname, "refs/remotes/")) { |
441adf0c | 550 | /* advise user how to delete local branches */ |
59556548 | 551 | if (starts_with(refname, "refs/heads/")) |
1d2f80fa JP |
552 | string_list_append(branches->skipped, |
553 | abbrev_branch(refname)); | |
441adf0c JS |
554 | /* silently skip over other non-remote refs */ |
555 | return 0; | |
556 | } | |
557 | ||
e26cdf91 | 558 | string_list_append(branches->branches, refname); |
211c8968 JS |
559 | |
560 | return 0; | |
561 | } | |
562 | ||
bf98421a | 563 | struct rename_info { |
b537e0b1 BW |
564 | const char *old_name; |
565 | const char *new_name; | |
bf98421a MV |
566 | struct string_list *remote_branches; |
567 | }; | |
568 | ||
569 | static int read_remote_branches(const char *refname, | |
53dc95b5 | 570 | const struct object_id *oid, int flags, void *cb_data) |
bf98421a MV |
571 | { |
572 | struct rename_info *rename = cb_data; | |
573 | struct strbuf buf = STRBUF_INIT; | |
574 | struct string_list_item *item; | |
575 | int flag; | |
bf98421a MV |
576 | const char *symref; |
577 | ||
b537e0b1 | 578 | strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name); |
59556548 | 579 | if (starts_with(refname, buf.buf)) { |
fe583c6c | 580 | item = string_list_append(rename->remote_branches, refname); |
7695d118 | 581 | symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING, |
744c040b | 582 | NULL, &flag); |
752848df | 583 | if (symref && (flag & REF_ISSYMREF)) |
bf98421a MV |
584 | item->util = xstrdup(symref); |
585 | else | |
586 | item->util = NULL; | |
587 | } | |
e2581b72 | 588 | strbuf_release(&buf); |
bf98421a MV |
589 | |
590 | return 0; | |
591 | } | |
592 | ||
1dd1239a MV |
593 | static int migrate_file(struct remote *remote) |
594 | { | |
595 | struct strbuf buf = STRBUF_INIT; | |
596 | int i; | |
1dd1239a MV |
597 | |
598 | strbuf_addf(&buf, "remote.%s.url", remote->name); | |
599 | for (i = 0; i < remote->url_nr; i++) | |
3d180648 | 600 | git_config_set_multivar(buf.buf, remote->url[i], "^$", 0); |
1dd1239a MV |
601 | strbuf_reset(&buf); |
602 | strbuf_addf(&buf, "remote.%s.push", remote->name); | |
6bdb304b BW |
603 | for (i = 0; i < remote->push.raw_nr; i++) |
604 | git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0); | |
1dd1239a MV |
605 | strbuf_reset(&buf); |
606 | strbuf_addf(&buf, "remote.%s.fetch", remote->name); | |
e5349abf BW |
607 | for (i = 0; i < remote->fetch.raw_nr; i++) |
608 | git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0); | |
1dd1239a | 609 | if (remote->origin == REMOTE_REMOTES) |
b21a5d66 | 610 | unlink_or_warn(git_path("remotes/%s", remote->name)); |
1dd1239a | 611 | else if (remote->origin == REMOTE_BRANCHES) |
b21a5d66 | 612 | unlink_or_warn(git_path("branches/%s", remote->name)); |
b95c8ce8 | 613 | strbuf_release(&buf); |
c397debf | 614 | |
1dd1239a MV |
615 | return 0; |
616 | } | |
617 | ||
b3fd6cbf BW |
618 | struct push_default_info |
619 | { | |
620 | const char *old_name; | |
621 | enum config_scope scope; | |
622 | struct strbuf origin; | |
623 | int linenr; | |
624 | }; | |
625 | ||
626 | static int config_read_push_default(const char *key, const char *value, | |
627 | void *cb) | |
628 | { | |
629 | struct push_default_info* info = cb; | |
630 | if (strcmp(key, "remote.pushdefault") || | |
631 | !value || strcmp(value, info->old_name)) | |
632 | return 0; | |
633 | ||
634 | info->scope = current_config_scope(); | |
635 | strbuf_reset(&info->origin); | |
636 | strbuf_addstr(&info->origin, current_config_name()); | |
637 | info->linenr = current_config_line(); | |
638 | ||
639 | return 0; | |
640 | } | |
641 | ||
642 | static void handle_push_default(const char* old_name, const char* new_name) | |
643 | { | |
644 | struct push_default_info push_default = { | |
645 | old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 }; | |
646 | git_config(config_read_push_default, &push_default); | |
647 | if (push_default.scope >= CONFIG_SCOPE_COMMAND) | |
648 | ; /* pass */ | |
649 | else if (push_default.scope >= CONFIG_SCOPE_LOCAL) { | |
650 | int result = git_config_set_gently("remote.pushDefault", | |
651 | new_name); | |
652 | if (new_name && result && result != CONFIG_NOTHING_SET) | |
653 | die(_("could not set '%s'"), "remote.pushDefault"); | |
654 | else if (!new_name && result && result != CONFIG_NOTHING_SET) | |
655 | die(_("could not unset '%s'"), "remote.pushDefault"); | |
656 | } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) { | |
657 | /* warn */ | |
658 | warning(_("The %s configuration remote.pushDefault in:\n" | |
659 | "\t%s:%d\n" | |
660 | "now names the non-existent remote '%s'"), | |
661 | config_scope_name(push_default.scope), | |
662 | push_default.origin.buf, push_default.linenr, | |
663 | old_name); | |
664 | } | |
665 | } | |
666 | ||
667 | ||
bf98421a MV |
668 | static int mv(int argc, const char **argv) |
669 | { | |
670 | struct option options[] = { | |
671 | OPT_END() | |
672 | }; | |
673 | struct remote *oldremote, *newremote; | |
28f555f6 MZ |
674 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT, |
675 | old_remote_context = STRBUF_INIT; | |
fe583c6c | 676 | struct string_list remote_branches = STRING_LIST_INIT_DUP; |
bf98421a | 677 | struct rename_info rename; |
b52d00ae | 678 | int i, refspec_updated = 0; |
bf98421a MV |
679 | |
680 | if (argc != 3) | |
4504107d | 681 | usage_with_options(builtin_remote_rename_usage, options); |
bf98421a | 682 | |
b537e0b1 BW |
683 | rename.old_name = argv[1]; |
684 | rename.new_name = argv[2]; | |
bf98421a MV |
685 | rename.remote_branches = &remote_branches; |
686 | ||
b537e0b1 | 687 | oldremote = remote_get(rename.old_name); |
e459b073 | 688 | if (!remote_is_configured(oldremote, 1)) |
5025425d | 689 | die(_("No such remote: '%s'"), rename.old_name); |
bf98421a | 690 | |
b537e0b1 | 691 | if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG) |
1dd1239a MV |
692 | return migrate_file(oldremote); |
693 | ||
b537e0b1 | 694 | newremote = remote_get(rename.new_name); |
e459b073 | 695 | if (remote_is_configured(newremote, 1)) |
b537e0b1 | 696 | die(_("remote %s already exists."), rename.new_name); |
bf98421a | 697 | |
b537e0b1 | 698 | strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name); |
bf98421a | 699 | if (!valid_fetch_refspec(buf.buf)) |
b537e0b1 | 700 | die(_("'%s' is not a valid remote name"), rename.new_name); |
bf98421a MV |
701 | |
702 | strbuf_reset(&buf); | |
b537e0b1 BW |
703 | strbuf_addf(&buf, "remote.%s", rename.old_name); |
704 | strbuf_addf(&buf2, "remote.%s", rename.new_name); | |
bf98421a | 705 | if (git_config_rename_section(buf.buf, buf2.buf) < 1) |
bb16d5dd | 706 | return error(_("Could not rename config section '%s' to '%s'"), |
bf98421a MV |
707 | buf.buf, buf2.buf); |
708 | ||
709 | strbuf_reset(&buf); | |
b537e0b1 | 710 | strbuf_addf(&buf, "remote.%s.fetch", rename.new_name); |
3d180648 | 711 | git_config_set_multivar(buf.buf, NULL, NULL, 1); |
b537e0b1 | 712 | strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name); |
e5349abf | 713 | for (i = 0; i < oldremote->fetch.raw_nr; i++) { |
bf98421a MV |
714 | char *ptr; |
715 | ||
716 | strbuf_reset(&buf2); | |
e5349abf | 717 | strbuf_addstr(&buf2, oldremote->fetch.raw[i]); |
28f555f6 | 718 | ptr = strstr(buf2.buf, old_remote_context.buf); |
b52d00ae MZ |
719 | if (ptr) { |
720 | refspec_updated = 1; | |
28f555f6 MZ |
721 | strbuf_splice(&buf2, |
722 | ptr-buf2.buf + strlen(":refs/remotes/"), | |
b537e0b1 BW |
723 | strlen(rename.old_name), rename.new_name, |
724 | strlen(rename.new_name)); | |
b52d00ae | 725 | } else |
aa3bb871 | 726 | warning(_("Not updating non-default fetch refspec\n" |
bb16d5dd NTND |
727 | "\t%s\n" |
728 | "\tPlease update the configuration manually if necessary."), | |
1822b86a MZ |
729 | buf2.buf); |
730 | ||
3d180648 | 731 | git_config_set_multivar(buf.buf, buf2.buf, "^$", 0); |
bf98421a MV |
732 | } |
733 | ||
734 | read_branches(); | |
735 | for (i = 0; i < branch_list.nr; i++) { | |
736 | struct string_list_item *item = branch_list.items + i; | |
737 | struct branch_info *info = item->util; | |
b537e0b1 | 738 | if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) { |
bf98421a MV |
739 | strbuf_reset(&buf); |
740 | strbuf_addf(&buf, "branch.%s.remote", item->string); | |
b537e0b1 | 741 | git_config_set(buf.buf, rename.new_name); |
bf98421a | 742 | } |
923d4a5c BW |
743 | if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) { |
744 | strbuf_reset(&buf); | |
745 | strbuf_addf(&buf, "branch.%s.pushremote", item->string); | |
746 | git_config_set(buf.buf, rename.new_name); | |
747 | } | |
bf98421a MV |
748 | } |
749 | ||
b52d00ae MZ |
750 | if (!refspec_updated) |
751 | return 0; | |
752 | ||
bf98421a MV |
753 | /* |
754 | * First remove symrefs, then rename the rest, finally create | |
755 | * the new symrefs. | |
756 | */ | |
53dc95b5 | 757 | for_each_ref(read_remote_branches, &rename); |
bf98421a MV |
758 | for (i = 0; i < remote_branches.nr; i++) { |
759 | struct string_list_item *item = remote_branches.items + i; | |
760 | int flag = 0; | |
bf98421a | 761 | |
99f86bde | 762 | read_ref_full(item->string, RESOLVE_REF_READING, NULL, &flag); |
bf98421a MV |
763 | if (!(flag & REF_ISSYMREF)) |
764 | continue; | |
91774afc | 765 | if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF)) |
bb16d5dd | 766 | die(_("deleting '%s' failed"), item->string); |
bf98421a MV |
767 | } |
768 | for (i = 0; i < remote_branches.nr; i++) { | |
769 | struct string_list_item *item = remote_branches.items + i; | |
770 | ||
771 | if (item->util) | |
772 | continue; | |
773 | strbuf_reset(&buf); | |
774 | strbuf_addstr(&buf, item->string); | |
b537e0b1 BW |
775 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name), |
776 | rename.new_name, strlen(rename.new_name)); | |
bf98421a MV |
777 | strbuf_reset(&buf2); |
778 | strbuf_addf(&buf2, "remote: renamed %s to %s", | |
779 | item->string, buf.buf); | |
780 | if (rename_ref(item->string, buf.buf, buf2.buf)) | |
bb16d5dd | 781 | die(_("renaming '%s' failed"), item->string); |
bf98421a MV |
782 | } |
783 | for (i = 0; i < remote_branches.nr; i++) { | |
784 | struct string_list_item *item = remote_branches.items + i; | |
785 | ||
786 | if (!item->util) | |
787 | continue; | |
788 | strbuf_reset(&buf); | |
789 | strbuf_addstr(&buf, item->string); | |
b537e0b1 BW |
790 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name), |
791 | rename.new_name, strlen(rename.new_name)); | |
bf98421a MV |
792 | strbuf_reset(&buf2); |
793 | strbuf_addstr(&buf2, item->util); | |
b537e0b1 BW |
794 | strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name), |
795 | rename.new_name, strlen(rename.new_name)); | |
bf98421a MV |
796 | strbuf_reset(&buf3); |
797 | strbuf_addf(&buf3, "remote: renamed %s to %s", | |
798 | item->string, buf.buf); | |
799 | if (create_symref(buf.buf, buf2.buf, buf3.buf)) | |
bb16d5dd | 800 | die(_("creating '%s' failed"), buf.buf); |
bf98421a | 801 | } |
fe583c6c | 802 | string_list_clear(&remote_branches, 1); |
b3fd6cbf BW |
803 | |
804 | handle_push_default(rename.old_name, rename.new_name); | |
805 | ||
bf98421a MV |
806 | return 0; |
807 | } | |
808 | ||
211c8968 JS |
809 | static int rm(int argc, const char **argv) |
810 | { | |
811 | struct option options[] = { | |
812 | OPT_END() | |
813 | }; | |
814 | struct remote *remote; | |
f285a2d7 | 815 | struct strbuf buf = STRBUF_INIT; |
7ad2458f | 816 | struct known_remotes known_remotes = { NULL, NULL }; |
183113a5 TF |
817 | struct string_list branches = STRING_LIST_INIT_DUP; |
818 | struct string_list skipped = STRING_LIST_INIT_DUP; | |
66dbfd55 | 819 | struct branches_for_remote cb_data; |
e02f1762 | 820 | int i, result; |
211c8968 | 821 | |
66dbfd55 GV |
822 | memset(&cb_data, 0, sizeof(cb_data)); |
823 | cb_data.branches = &branches; | |
824 | cb_data.skipped = &skipped; | |
825 | cb_data.keep = &known_remotes; | |
826 | ||
211c8968 | 827 | if (argc != 2) |
4504107d | 828 | usage_with_options(builtin_remote_rm_usage, options); |
211c8968 JS |
829 | |
830 | remote = remote_get(argv[1]); | |
e459b073 | 831 | if (!remote_is_configured(remote, 1)) |
5025425d | 832 | die(_("No such remote: '%s'"), argv[1]); |
211c8968 | 833 | |
7ad2458f SP |
834 | known_remotes.to_delete = remote; |
835 | for_each_remote(add_known_remote, &known_remotes); | |
836 | ||
211c8968 JS |
837 | read_branches(); |
838 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 839 | struct string_list_item *item = branch_list.items + i; |
211c8968 | 840 | struct branch_info *info = item->util; |
e0cc81e6 | 841 | if (info->remote_name && !strcmp(info->remote_name, remote->name)) { |
211c8968 JS |
842 | const char *keys[] = { "remote", "merge", NULL }, **k; |
843 | for (k = keys; *k; k++) { | |
844 | strbuf_reset(&buf); | |
845 | strbuf_addf(&buf, "branch.%s.%s", | |
c455c87c | 846 | item->string, *k); |
20690b21 RL |
847 | result = git_config_set_gently(buf.buf, NULL); |
848 | if (result && result != CONFIG_NOTHING_SET) | |
849 | die(_("could not unset '%s'"), buf.buf); | |
211c8968 JS |
850 | } |
851 | } | |
923d4a5c BW |
852 | if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) { |
853 | strbuf_reset(&buf); | |
854 | strbuf_addf(&buf, "branch.%s.pushremote", item->string); | |
855 | result = git_config_set_gently(buf.buf, NULL); | |
856 | if (result && result != CONFIG_NOTHING_SET) | |
857 | die(_("could not unset '%s'"), buf.buf); | |
858 | } | |
211c8968 JS |
859 | } |
860 | ||
861 | /* | |
862 | * We cannot just pass a function to for_each_ref() which deletes | |
863 | * the branches one by one, since for_each_ref() relies on cached | |
864 | * refs, which are invalidated when deleting a branch. | |
865 | */ | |
7ad2458f | 866 | cb_data.remote = remote; |
45690a57 | 867 | result = for_each_ref(add_branch_for_removal, &cb_data); |
211c8968 JS |
868 | strbuf_release(&buf); |
869 | ||
e02f1762 | 870 | if (!result) |
91774afc | 871 | result = delete_refs("remote: remove", &branches, REF_NO_DEREF); |
e26cdf91 | 872 | string_list_clear(&branches, 0); |
211c8968 | 873 | |
441adf0c | 874 | if (skipped.nr) { |
bb16d5dd NTND |
875 | fprintf_ln(stderr, |
876 | Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n" | |
877 | "to delete it, use:", | |
878 | "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n" | |
879 | "to delete them, use:", | |
880 | skipped.nr)); | |
441adf0c JS |
881 | for (i = 0; i < skipped.nr; i++) |
882 | fprintf(stderr, " git branch -d %s\n", | |
883 | skipped.items[i].string); | |
884 | } | |
885 | string_list_clear(&skipped, 0); | |
886 | ||
b07bdd34 JL |
887 | if (!result) { |
888 | strbuf_addf(&buf, "remote.%s", remote->name); | |
889 | if (git_config_rename_section(buf.buf, NULL) < 1) | |
890 | return error(_("Could not remove config section '%s'"), buf.buf); | |
b3fd6cbf BW |
891 | |
892 | handle_push_default(remote->name, NULL); | |
b07bdd34 JL |
893 | } |
894 | ||
e02f1762 | 895 | return result; |
211c8968 JS |
896 | } |
897 | ||
2af202be | 898 | static void clear_push_info(void *util, const char *string) |
211c8968 | 899 | { |
e5dcbfd9 JS |
900 | struct push_info *info = util; |
901 | free(info->dest); | |
902 | free(info); | |
903 | } | |
211c8968 | 904 | |
88733235 JS |
905 | static void free_remote_ref_states(struct ref_states *states) |
906 | { | |
b537e0b1 | 907 | string_list_clear(&states->new_refs, 0); |
92f676fc | 908 | string_list_clear(&states->stale, 1); |
88733235 | 909 | string_list_clear(&states->tracked, 0); |
e61e0cc6 | 910 | string_list_clear(&states->heads, 0); |
e5dcbfd9 | 911 | string_list_clear_func(&states->push, clear_push_info); |
88733235 | 912 | } |
211c8968 | 913 | |
cca7c97e | 914 | static int append_ref_to_tracked_list(const char *refname, |
53dc95b5 | 915 | const struct object_id *oid, int flags, void *cb_data) |
cca7c97e JS |
916 | { |
917 | struct ref_states *states = cb_data; | |
0ad4a5ff | 918 | struct refspec_item refspec; |
cca7c97e | 919 | |
3bd92563 JS |
920 | if (flags & REF_ISSYMREF) |
921 | return 0; | |
922 | ||
cca7c97e JS |
923 | memset(&refspec, 0, sizeof(refspec)); |
924 | refspec.dst = (char *)refname; | |
925 | if (!remote_find_tracking(states->remote, &refspec)) | |
1d2f80fa | 926 | string_list_append(&states->tracked, abbrev_branch(refspec.src)); |
cca7c97e JS |
927 | |
928 | return 0; | |
211c8968 JS |
929 | } |
930 | ||
67a7e2d0 OM |
931 | static int get_remote_ref_states(const char *name, |
932 | struct ref_states *states, | |
933 | int query) | |
934 | { | |
935 | struct transport *transport; | |
e0cc81e6 | 936 | const struct ref *remote_refs; |
67a7e2d0 OM |
937 | |
938 | states->remote = remote_get(name); | |
939 | if (!states->remote) | |
5025425d | 940 | return error(_("No such remote: '%s'"), name); |
67a7e2d0 OM |
941 | |
942 | read_branches(); | |
943 | ||
944 | if (query) { | |
345a3803 | 945 | transport = transport_get(states->remote, states->remote->url_nr > 0 ? |
67a7e2d0 | 946 | states->remote->url[0] : NULL); |
1af8ae1c | 947 | remote_refs = transport_get_remote_refs(transport, NULL); |
67a7e2d0 OM |
948 | transport_disconnect(transport); |
949 | ||
7ecbbf87 | 950 | states->queried = 1; |
e61e0cc6 JS |
951 | if (query & GET_REF_STATES) |
952 | get_ref_states(remote_refs, states); | |
953 | if (query & GET_HEAD_NAMES) | |
954 | get_head_names(remote_refs, states); | |
e5dcbfd9 JS |
955 | if (query & GET_PUSH_REF_STATES) |
956 | get_push_ref_states(remote_refs, states); | |
3bd92563 | 957 | } else { |
53dc95b5 | 958 | for_each_ref(append_ref_to_tracked_list, states); |
3383e199 | 959 | string_list_sort(&states->tracked); |
e5dcbfd9 | 960 | get_push_ref_states_noquery(states); |
67a7e2d0 OM |
961 | } |
962 | ||
963 | return 0; | |
964 | } | |
965 | ||
7ecbbf87 JS |
966 | struct show_info { |
967 | struct string_list *list; | |
968 | struct ref_states *states; | |
e5dcbfd9 | 969 | int width, width2; |
7ecbbf87 JS |
970 | int any_rebase; |
971 | }; | |
972 | ||
2af202be | 973 | static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) |
e7d5a97d | 974 | { |
7ecbbf87 JS |
975 | struct show_info *info = cb_data; |
976 | int n = strlen(item->string); | |
977 | if (n > info->width) | |
978 | info->width = n; | |
78a395d3 | 979 | string_list_insert(info->list, item->string); |
7ecbbf87 JS |
980 | return 0; |
981 | } | |
e7d5a97d | 982 | |
2af202be | 983 | static int show_remote_info_item(struct string_list_item *item, void *cb_data) |
7ecbbf87 JS |
984 | { |
985 | struct show_info *info = cb_data; | |
986 | struct ref_states *states = info->states; | |
987 | const char *name = item->string; | |
988 | ||
989 | if (states->queried) { | |
990 | const char *fmt = "%s"; | |
991 | const char *arg = ""; | |
b537e0b1 | 992 | if (string_list_has_string(&states->new_refs, name)) { |
bb16d5dd | 993 | fmt = _(" new (next fetch will store in remotes/%s)"); |
7ecbbf87 JS |
994 | arg = states->remote->name; |
995 | } else if (string_list_has_string(&states->tracked, name)) | |
bb16d5dd | 996 | arg = _(" tracked"); |
7ecbbf87 | 997 | else if (string_list_has_string(&states->stale, name)) |
bb16d5dd | 998 | arg = _(" stale (use 'git remote prune' to remove)"); |
7ecbbf87 | 999 | else |
bb16d5dd | 1000 | arg = _(" ???"); |
7ecbbf87 JS |
1001 | printf(" %-*s", info->width, name); |
1002 | printf(fmt, arg); | |
1003 | printf("\n"); | |
1004 | } else | |
1005 | printf(" %s\n", name); | |
1006 | ||
1007 | return 0; | |
1008 | } | |
1009 | ||
2af202be | 1010 | static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) |
7ecbbf87 JS |
1011 | { |
1012 | struct show_info *show_info = cb_data; | |
1013 | struct ref_states *states = show_info->states; | |
1014 | struct branch_info *branch_info = branch_item->util; | |
1015 | struct string_list_item *item; | |
1016 | int n; | |
1017 | ||
1018 | if (!branch_info->merge.nr || !branch_info->remote_name || | |
1019 | strcmp(states->remote->name, branch_info->remote_name)) | |
1020 | return 0; | |
1021 | if ((n = strlen(branch_item->string)) > show_info->width) | |
1022 | show_info->width = n; | |
88f8576e | 1023 | if (branch_info->rebase >= REBASE_TRUE) |
7ecbbf87 JS |
1024 | show_info->any_rebase = 1; |
1025 | ||
78a395d3 | 1026 | item = string_list_insert(show_info->list, branch_item->string); |
7ecbbf87 JS |
1027 | item->util = branch_info; |
1028 | ||
1029 | return 0; | |
1030 | } | |
1031 | ||
2af202be | 1032 | static int show_local_info_item(struct string_list_item *item, void *cb_data) |
7ecbbf87 JS |
1033 | { |
1034 | struct show_info *show_info = cb_data; | |
1035 | struct branch_info *branch_info = item->util; | |
1036 | struct string_list *merge = &branch_info->merge; | |
a1b467a4 | 1037 | int width = show_info->width + 4; |
7ecbbf87 JS |
1038 | int i; |
1039 | ||
88f8576e | 1040 | if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) { |
bb16d5dd | 1041 | error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"), |
7ecbbf87 JS |
1042 | item->string); |
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | printf(" %-*s ", show_info->width, item->string); | |
88f8576e | 1047 | if (branch_info->rebase >= REBASE_TRUE) { |
1131ec98 | 1048 | const char *msg; |
88f8576e | 1049 | if (branch_info->rebase == REBASE_INTERACTIVE) |
1131ec98 JS |
1050 | msg = _("rebases interactively onto remote %s"); |
1051 | else if (branch_info->rebase == REBASE_MERGES) | |
1052 | msg = _("rebases interactively (with merges) onto " | |
1053 | "remote %s"); | |
1054 | else | |
1055 | msg = _("rebases onto remote %s"); | |
1056 | printf_ln(msg, merge->items[0].string); | |
7ecbbf87 JS |
1057 | return 0; |
1058 | } else if (show_info->any_rebase) { | |
bb16d5dd | 1059 | printf_ln(_(" merges with remote %s"), merge->items[0].string); |
a1b467a4 | 1060 | width++; |
7ecbbf87 | 1061 | } else { |
bb16d5dd | 1062 | printf_ln(_("merges with remote %s"), merge->items[0].string); |
7ecbbf87 JS |
1063 | } |
1064 | for (i = 1; i < merge->nr; i++) | |
a1b467a4 | 1065 | printf(_("%-*s and with remote %s\n"), width, "", |
7ecbbf87 JS |
1066 | merge->items[i].string); |
1067 | ||
1068 | return 0; | |
1069 | } | |
e7d5a97d | 1070 | |
2af202be | 1071 | static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data) |
e5dcbfd9 JS |
1072 | { |
1073 | struct show_info *show_info = cb_data; | |
1074 | struct push_info *push_info = push_item->util; | |
1075 | struct string_list_item *item; | |
1076 | int n; | |
1077 | if ((n = strlen(push_item->string)) > show_info->width) | |
1078 | show_info->width = n; | |
1079 | if ((n = strlen(push_info->dest)) > show_info->width2) | |
1080 | show_info->width2 = n; | |
1d2f80fa | 1081 | item = string_list_append(show_info->list, push_item->string); |
e5dcbfd9 JS |
1082 | item->util = push_item->util; |
1083 | return 0; | |
1084 | } | |
1085 | ||
48ef5636 JK |
1086 | /* |
1087 | * Sorting comparison for a string list that has push_info | |
1088 | * structs in its util field | |
1089 | */ | |
1090 | static int cmp_string_with_push(const void *va, const void *vb) | |
1091 | { | |
1092 | const struct string_list_item *a = va; | |
1093 | const struct string_list_item *b = vb; | |
1094 | const struct push_info *a_push = a->util; | |
1095 | const struct push_info *b_push = b->util; | |
1096 | int cmp = strcmp(a->string, b->string); | |
1097 | return cmp ? cmp : strcmp(a_push->dest, b_push->dest); | |
1098 | } | |
1099 | ||
2af202be | 1100 | static int show_push_info_item(struct string_list_item *item, void *cb_data) |
e5dcbfd9 JS |
1101 | { |
1102 | struct show_info *show_info = cb_data; | |
1103 | struct push_info *push_info = item->util; | |
bb16d5dd | 1104 | const char *src = item->string, *status = NULL; |
e5dcbfd9 JS |
1105 | |
1106 | switch (push_info->status) { | |
1107 | case PUSH_STATUS_CREATE: | |
bb16d5dd | 1108 | status = _("create"); |
e5dcbfd9 JS |
1109 | break; |
1110 | case PUSH_STATUS_DELETE: | |
bb16d5dd NTND |
1111 | status = _("delete"); |
1112 | src = _("(none)"); | |
e5dcbfd9 JS |
1113 | break; |
1114 | case PUSH_STATUS_UPTODATE: | |
bb16d5dd | 1115 | status = _("up to date"); |
e5dcbfd9 JS |
1116 | break; |
1117 | case PUSH_STATUS_FASTFORWARD: | |
bb16d5dd | 1118 | status = _("fast-forwardable"); |
e5dcbfd9 JS |
1119 | break; |
1120 | case PUSH_STATUS_OUTOFDATE: | |
bb16d5dd | 1121 | status = _("local out of date"); |
e5dcbfd9 JS |
1122 | break; |
1123 | case PUSH_STATUS_NOTQUERIED: | |
1124 | break; | |
1125 | } | |
bb16d5dd NTND |
1126 | if (status) { |
1127 | if (push_info->forced) | |
1128 | printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src, | |
1129 | show_info->width2, push_info->dest, status); | |
1130 | else | |
1131 | printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src, | |
1132 | show_info->width2, push_info->dest, status); | |
1133 | } else { | |
1134 | if (push_info->forced) | |
1135 | printf_ln(_(" %-*s forces to %s"), show_info->width, src, | |
1136 | push_info->dest); | |
1137 | else | |
1138 | printf_ln(_(" %-*s pushes to %s"), show_info->width, src, | |
1139 | push_info->dest); | |
1140 | } | |
e7d5a97d OM |
1141 | return 0; |
1142 | } | |
1143 | ||
ce2223fd MH |
1144 | static int get_one_entry(struct remote *remote, void *priv) |
1145 | { | |
1146 | struct string_list *list = priv; | |
1147 | struct strbuf url_buf = STRBUF_INIT; | |
1148 | const char **url; | |
1149 | int i, url_nr; | |
1150 | ||
1151 | if (remote->url_nr > 0) { | |
1152 | strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]); | |
1153 | string_list_append(list, remote->name)->util = | |
1154 | strbuf_detach(&url_buf, NULL); | |
1155 | } else | |
1156 | string_list_append(list, remote->name)->util = NULL; | |
1157 | if (remote->pushurl_nr) { | |
1158 | url = remote->pushurl; | |
1159 | url_nr = remote->pushurl_nr; | |
1160 | } else { | |
1161 | url = remote->url; | |
1162 | url_nr = remote->url_nr; | |
1163 | } | |
1164 | for (i = 0; i < url_nr; i++) | |
1165 | { | |
1166 | strbuf_addf(&url_buf, "%s (push)", url[i]); | |
1167 | string_list_append(list, remote->name)->util = | |
1168 | strbuf_detach(&url_buf, NULL); | |
1169 | } | |
1170 | ||
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | static int show_all(void) | |
1175 | { | |
1176 | struct string_list list = STRING_LIST_INIT_NODUP; | |
1177 | int result; | |
1178 | ||
1179 | list.strdup_strings = 1; | |
1180 | result = for_each_remote(get_one_entry, &list); | |
1181 | ||
1182 | if (!result) { | |
1183 | int i; | |
1184 | ||
3383e199 | 1185 | string_list_sort(&list); |
ce2223fd MH |
1186 | for (i = 0; i < list.nr; i++) { |
1187 | struct string_list_item *item = list.items + i; | |
1188 | if (verbose) | |
1189 | printf("%s\t%s\n", item->string, | |
1190 | item->util ? (const char *)item->util : ""); | |
1191 | else { | |
1192 | if (i && !strcmp((item - 1)->string, item->string)) | |
1193 | continue; | |
1194 | printf("%s\n", item->string); | |
1195 | } | |
1196 | } | |
1197 | } | |
1198 | string_list_clear(&list, 1); | |
1199 | return result; | |
1200 | } | |
1201 | ||
67a7e2d0 | 1202 | static int show(int argc, const char **argv) |
211c8968 | 1203 | { |
e61e0cc6 | 1204 | int no_query = 0, result = 0, query_flag = 0; |
211c8968 | 1205 | struct option options[] = { |
d5d09d47 | 1206 | OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")), |
211c8968 JS |
1207 | OPT_END() |
1208 | }; | |
1209 | struct ref_states states; | |
183113a5 | 1210 | struct string_list info_list = STRING_LIST_INIT_NODUP; |
7ecbbf87 | 1211 | struct show_info info; |
211c8968 | 1212 | |
4504107d | 1213 | argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage, |
37782920 | 1214 | 0); |
211c8968 | 1215 | |
67a7e2d0 OM |
1216 | if (argc < 1) |
1217 | return show_all(); | |
211c8968 | 1218 | |
e61e0cc6 | 1219 | if (!no_query) |
e5dcbfd9 | 1220 | query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); |
e61e0cc6 | 1221 | |
211c8968 | 1222 | memset(&states, 0, sizeof(states)); |
7ecbbf87 JS |
1223 | memset(&info, 0, sizeof(info)); |
1224 | info.states = &states; | |
1225 | info.list = &info_list; | |
211c8968 | 1226 | for (; argc; argc--, argv++) { |
0ecfcb3b | 1227 | int i; |
857f8c30 MG |
1228 | const char **url; |
1229 | int url_nr; | |
211c8968 | 1230 | |
e61e0cc6 | 1231 | get_remote_ref_states(*argv, &states, query_flag); |
211c8968 | 1232 | |
bb16d5dd NTND |
1233 | printf_ln(_("* remote %s"), *argv); |
1234 | printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ? | |
1235 | states.remote->url[0] : _("(no URL)")); | |
857f8c30 MG |
1236 | if (states.remote->pushurl_nr) { |
1237 | url = states.remote->pushurl; | |
1238 | url_nr = states.remote->pushurl_nr; | |
1239 | } else { | |
1240 | url = states.remote->url; | |
1241 | url_nr = states.remote->url_nr; | |
1242 | } | |
cd2b8ae9 | 1243 | for (i = 0; i < url_nr; i++) |
66f5f6dc ÆAB |
1244 | /* |
1245 | * TRANSLATORS: the colon ':' should align | |
1246 | * with the one in " Fetch URL: %s" | |
1247 | * translation. | |
1248 | */ | |
bb16d5dd | 1249 | printf_ln(_(" Push URL: %s"), url[i]); |
857f8c30 | 1250 | if (!i) |
7ba7b9ab | 1251 | printf_ln(_(" Push URL: %s"), _("(no URL)")); |
e61e0cc6 | 1252 | if (no_query) |
7ba7b9ab | 1253 | printf_ln(_(" HEAD branch: %s"), _("(not queried)")); |
e61e0cc6 | 1254 | else if (!states.heads.nr) |
7ba7b9ab | 1255 | printf_ln(_(" HEAD branch: %s"), _("(unknown)")); |
e61e0cc6 | 1256 | else if (states.heads.nr == 1) |
bb16d5dd | 1257 | printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string); |
e61e0cc6 | 1258 | else { |
bb16d5dd NTND |
1259 | printf(_(" HEAD branch (remote HEAD is ambiguous," |
1260 | " may be one of the following):\n")); | |
e61e0cc6 JS |
1261 | for (i = 0; i < states.heads.nr; i++) |
1262 | printf(" %s\n", states.heads.items[i].string); | |
211c8968 JS |
1263 | } |
1264 | ||
7ecbbf87 JS |
1265 | /* remote branch info */ |
1266 | info.width = 0; | |
b537e0b1 | 1267 | for_each_string_list(&states.new_refs, add_remote_to_show_info, &info); |
b684e977 JP |
1268 | for_each_string_list(&states.tracked, add_remote_to_show_info, &info); |
1269 | for_each_string_list(&states.stale, add_remote_to_show_info, &info); | |
7ecbbf87 | 1270 | if (info.list->nr) |
bb16d5dd NTND |
1271 | printf_ln(Q_(" Remote branch:%s", |
1272 | " Remote branches:%s", | |
1273 | info.list->nr), | |
1274 | no_query ? _(" (status not queried)") : ""); | |
b684e977 | 1275 | for_each_string_list(info.list, show_remote_info_item, &info); |
7ecbbf87 JS |
1276 | string_list_clear(info.list, 0); |
1277 | ||
1278 | /* git pull info */ | |
1279 | info.width = 0; | |
1280 | info.any_rebase = 0; | |
b684e977 | 1281 | for_each_string_list(&branch_list, add_local_to_show_info, &info); |
7ecbbf87 | 1282 | if (info.list->nr) |
bb16d5dd NTND |
1283 | printf_ln(Q_(" Local branch configured for 'git pull':", |
1284 | " Local branches configured for 'git pull':", | |
1285 | info.list->nr)); | |
b684e977 | 1286 | for_each_string_list(info.list, show_local_info_item, &info); |
7ecbbf87 JS |
1287 | string_list_clear(info.list, 0); |
1288 | ||
1289 | /* git push info */ | |
e5dcbfd9 | 1290 | if (states.remote->mirror) |
bb16d5dd | 1291 | printf_ln(_(" Local refs will be mirrored by 'git push'")); |
e5dcbfd9 JS |
1292 | |
1293 | info.width = info.width2 = 0; | |
b684e977 | 1294 | for_each_string_list(&states.push, add_push_to_show_info, &info); |
9ed0d8d6 | 1295 | QSORT(info.list->items, info.list->nr, cmp_string_with_push); |
e5dcbfd9 | 1296 | if (info.list->nr) |
bb16d5dd NTND |
1297 | printf_ln(Q_(" Local ref configured for 'git push'%s:", |
1298 | " Local refs configured for 'git push'%s:", | |
1299 | info.list->nr), | |
1300 | no_query ? _(" (status not queried)") : ""); | |
b684e977 | 1301 | for_each_string_list(info.list, show_push_info_item, &info); |
e5dcbfd9 | 1302 | string_list_clear(info.list, 0); |
67a7e2d0 | 1303 | |
88733235 | 1304 | free_remote_ref_states(&states); |
67a7e2d0 | 1305 | } |
211c8968 | 1306 | |
67a7e2d0 OM |
1307 | return result; |
1308 | } | |
67a7e2d0 | 1309 | |
bc14fac8 JS |
1310 | static int set_head(int argc, const char **argv) |
1311 | { | |
1312 | int i, opt_a = 0, opt_d = 0, result = 0; | |
1313 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; | |
1314 | char *head_name = NULL; | |
1315 | ||
1316 | struct option options[] = { | |
d5d09d47 SB |
1317 | OPT_BOOL('a', "auto", &opt_a, |
1318 | N_("set refs/remotes/<name>/HEAD according to remote")), | |
1319 | OPT_BOOL('d', "delete", &opt_d, | |
1320 | N_("delete refs/remotes/<name>/HEAD")), | |
bc14fac8 JS |
1321 | OPT_END() |
1322 | }; | |
4504107d | 1323 | argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage, |
37782920 | 1324 | 0); |
bc14fac8 JS |
1325 | if (argc) |
1326 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]); | |
1327 | ||
1328 | if (!opt_a && !opt_d && argc == 2) { | |
1329 | head_name = xstrdup(argv[1]); | |
1330 | } else if (opt_a && !opt_d && argc == 1) { | |
1331 | struct ref_states states; | |
1332 | memset(&states, 0, sizeof(states)); | |
1333 | get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); | |
1334 | if (!states.heads.nr) | |
bb16d5dd | 1335 | result |= error(_("Cannot determine remote HEAD")); |
bc14fac8 | 1336 | else if (states.heads.nr > 1) { |
bb16d5dd NTND |
1337 | result |= error(_("Multiple remote HEAD branches. " |
1338 | "Please choose one explicitly with:")); | |
bc14fac8 JS |
1339 | for (i = 0; i < states.heads.nr; i++) |
1340 | fprintf(stderr, " git remote set-head %s %s\n", | |
1341 | argv[0], states.heads.items[i].string); | |
1342 | } else | |
1343 | head_name = xstrdup(states.heads.items[0].string); | |
1344 | free_remote_ref_states(&states); | |
1345 | } else if (opt_d && !opt_a && argc == 1) { | |
91774afc | 1346 | if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF)) |
bb16d5dd | 1347 | result |= error(_("Could not delete %s"), buf.buf); |
bc14fac8 | 1348 | } else |
4504107d | 1349 | usage_with_options(builtin_remote_sethead_usage, options); |
bc14fac8 JS |
1350 | |
1351 | if (head_name) { | |
bc14fac8 JS |
1352 | strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name); |
1353 | /* make sure it's valid */ | |
c6893323 | 1354 | if (!ref_exists(buf2.buf)) |
bb16d5dd | 1355 | result |= error(_("Not a valid ref: %s"), buf2.buf); |
bc14fac8 | 1356 | else if (create_symref(buf.buf, buf2.buf, "remote set-head")) |
bb16d5dd | 1357 | result |= error(_("Could not setup %s"), buf.buf); |
bc14fac8 JS |
1358 | if (opt_a) |
1359 | printf("%s/HEAD set to %s\n", argv[0], head_name); | |
1360 | free(head_name); | |
67a7e2d0 OM |
1361 | } |
1362 | ||
bc14fac8 JS |
1363 | strbuf_release(&buf); |
1364 | strbuf_release(&buf2); | |
67a7e2d0 OM |
1365 | return result; |
1366 | } | |
1367 | ||
b92c5f22 FAG |
1368 | static int prune_remote(const char *remote, int dry_run) |
1369 | { | |
8552943f | 1370 | int result = 0; |
b92c5f22 | 1371 | struct ref_states states; |
fcce0da9 | 1372 | struct string_list refs_to_prune = STRING_LIST_INIT_NODUP; |
8552943f | 1373 | struct string_list_item *item; |
b92c5f22 | 1374 | const char *dangling_msg = dry_run |
bb16d5dd NTND |
1375 | ? _(" %s will become dangling!") |
1376 | : _(" %s has become dangling!"); | |
67a7e2d0 | 1377 | |
b92c5f22 FAG |
1378 | memset(&states, 0, sizeof(states)); |
1379 | get_remote_ref_states(remote, &states, GET_REF_STATES); | |
1380 | ||
16d4fa3d MH |
1381 | if (!states.stale.nr) { |
1382 | free_remote_ref_states(&states); | |
1383 | return 0; | |
1384 | } | |
1385 | ||
1386 | printf_ln(_("Pruning %s"), remote); | |
1387 | printf_ln(_("URL: %s"), | |
1388 | states.remote->url_nr | |
1389 | ? states.remote->url[0] | |
1390 | : _("(no URL)")); | |
1391 | ||
8552943f MH |
1392 | for_each_string_list_item(item, &states.stale) |
1393 | string_list_append(&refs_to_prune, item->util); | |
3383e199 | 1394 | string_list_sort(&refs_to_prune); |
28d3f214 | 1395 | |
a122366d | 1396 | if (!dry_run) |
64da4199 | 1397 | result |= delete_refs("remote: prune", &refs_to_prune, 0); |
8d767927 | 1398 | |
8552943f MH |
1399 | for_each_string_list_item(item, &states.stale) { |
1400 | const char *refname = item->util; | |
8d767927 | 1401 | |
bb16d5dd NTND |
1402 | if (dry_run) |
1403 | printf_ln(_(" * [would prune] %s"), | |
1404 | abbrev_ref(refname, "refs/remotes/")); | |
1405 | else | |
1406 | printf_ln(_(" * [pruned] %s"), | |
1407 | abbrev_ref(refname, "refs/remotes/")); | |
211c8968 JS |
1408 | } |
1409 | ||
fcce0da9 | 1410 | warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune); |
e6bea66d | 1411 | |
fcce0da9 | 1412 | string_list_clear(&refs_to_prune, 0); |
b92c5f22 | 1413 | free_remote_ref_states(&states); |
211c8968 JS |
1414 | return result; |
1415 | } | |
1416 | ||
ce2223fd MH |
1417 | static int prune(int argc, const char **argv) |
1418 | { | |
1419 | int dry_run = 0, result = 0; | |
1420 | struct option options[] = { | |
1421 | OPT__DRY_RUN(&dry_run, N_("dry run")), | |
1422 | OPT_END() | |
1423 | }; | |
1424 | ||
1425 | argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage, | |
1426 | 0); | |
1427 | ||
1428 | if (argc < 1) | |
1429 | usage_with_options(builtin_remote_prune_usage, options); | |
1430 | ||
1431 | for (; argc; argc--, argv++) | |
1432 | result |= prune_remote(*argv, dry_run); | |
1433 | ||
1434 | return result; | |
1435 | } | |
1436 | ||
8db35596 | 1437 | static int get_remote_default(const char *key, const char *value, void *priv) |
211c8968 | 1438 | { |
8db35596 BG |
1439 | if (strcmp(key, "remotes.default") == 0) { |
1440 | int *found = priv; | |
1441 | *found = 1; | |
84521ed6 | 1442 | } |
211c8968 JS |
1443 | return 0; |
1444 | } | |
1445 | ||
1446 | static int update(int argc, const char **argv) | |
1447 | { | |
90765fa3 | 1448 | int i, prune = -1; |
efa54803 | 1449 | struct option options[] = { |
d5d09d47 SB |
1450 | OPT_BOOL('p', "prune", &prune, |
1451 | N_("prune remotes after fetching")), | |
efa54803 FAG |
1452 | OPT_END() |
1453 | }; | |
8607590e | 1454 | struct argv_array fetch_argv = ARGV_ARRAY_INIT; |
8db35596 | 1455 | int default_defined = 0; |
8607590e | 1456 | int retval; |
211c8968 | 1457 | |
4504107d | 1458 | argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage, |
efa54803 | 1459 | PARSE_OPT_KEEP_ARGV0); |
211c8968 | 1460 | |
8607590e | 1461 | argv_array_push(&fetch_argv, "fetch"); |
84521ed6 | 1462 | |
90765fa3 MH |
1463 | if (prune != -1) |
1464 | argv_array_push(&fetch_argv, prune ? "--prune" : "--no-prune"); | |
8db35596 | 1465 | if (verbose) |
8607590e MH |
1466 | argv_array_push(&fetch_argv, "-v"); |
1467 | argv_array_push(&fetch_argv, "--multiple"); | |
4f2e842d | 1468 | if (argc < 2) |
8607590e | 1469 | argv_array_push(&fetch_argv, "default"); |
4f2e842d | 1470 | for (i = 1; i < argc; i++) |
8607590e | 1471 | argv_array_push(&fetch_argv, argv[i]); |
84521ed6 | 1472 | |
8607590e | 1473 | if (strcmp(fetch_argv.argv[fetch_argv.argc-1], "default") == 0) { |
8db35596 | 1474 | git_config(get_remote_default, &default_defined); |
8607590e MH |
1475 | if (!default_defined) { |
1476 | argv_array_pop(&fetch_argv); | |
1477 | argv_array_push(&fetch_argv, "--all"); | |
1478 | } | |
efa54803 | 1479 | } |
84521ed6 | 1480 | |
8607590e MH |
1481 | retval = run_command_v_opt(fetch_argv.argv, RUN_GIT_CMD); |
1482 | argv_array_clear(&fetch_argv); | |
1483 | return retval; | |
211c8968 JS |
1484 | } |
1485 | ||
113c29ad | 1486 | static int remove_all_fetch_refspecs(const char *key) |
3d8b6949 | 1487 | { |
30598ad0 | 1488 | return git_config_set_multivar_gently(key, NULL, NULL, 1); |
3d8b6949 JN |
1489 | } |
1490 | ||
ab5e4b67 PS |
1491 | static void add_branches(struct remote *remote, const char **branches, |
1492 | const char *key) | |
3d8b6949 JN |
1493 | { |
1494 | const char *remotename = remote->name; | |
1495 | int mirror = remote->mirror; | |
1496 | struct strbuf refspec = STRBUF_INIT; | |
1497 | ||
1498 | for (; *branches; branches++) | |
ab5e4b67 | 1499 | add_branch(key, *branches, remotename, mirror, &refspec); |
3d8b6949 JN |
1500 | |
1501 | strbuf_release(&refspec); | |
3d8b6949 JN |
1502 | } |
1503 | ||
1504 | static int set_remote_branches(const char *remotename, const char **branches, | |
1505 | int add_mode) | |
1506 | { | |
1507 | struct strbuf key = STRBUF_INIT; | |
1508 | struct remote *remote; | |
1509 | ||
1510 | strbuf_addf(&key, "remote.%s.fetch", remotename); | |
1511 | ||
3d8b6949 | 1512 | remote = remote_get(remotename); |
e459b073 | 1513 | if (!remote_is_configured(remote, 1)) |
674468b3 | 1514 | die(_("No such remote '%s'"), remotename); |
3d8b6949 | 1515 | |
113c29ad | 1516 | if (!add_mode && remove_all_fetch_refspecs(key.buf)) { |
3d8b6949 JN |
1517 | strbuf_release(&key); |
1518 | return 1; | |
1519 | } | |
ab5e4b67 | 1520 | add_branches(remote, branches, key.buf); |
3d8b6949 JN |
1521 | |
1522 | strbuf_release(&key); | |
1523 | return 0; | |
1524 | } | |
1525 | ||
1526 | static int set_branches(int argc, const char **argv) | |
1527 | { | |
1528 | int add_mode = 0; | |
1529 | struct option options[] = { | |
d5d09d47 | 1530 | OPT_BOOL('\0', "add", &add_mode, N_("add branch")), |
3d8b6949 JN |
1531 | OPT_END() |
1532 | }; | |
1533 | ||
1534 | argc = parse_options(argc, argv, NULL, options, | |
1535 | builtin_remote_setbranches_usage, 0); | |
1536 | if (argc == 0) { | |
bb16d5dd | 1537 | error(_("no remote specified")); |
656cdf0c | 1538 | usage_with_options(builtin_remote_setbranches_usage, options); |
3d8b6949 JN |
1539 | } |
1540 | argv[argc] = NULL; | |
1541 | ||
1542 | return set_remote_branches(argv[0], argv + 1, add_mode); | |
1543 | } | |
1544 | ||
96f78d39 BB |
1545 | static int get_url(int argc, const char **argv) |
1546 | { | |
1547 | int i, push_mode = 0, all_mode = 0; | |
1548 | const char *remotename = NULL; | |
1549 | struct remote *remote; | |
1550 | const char **url; | |
1551 | int url_nr; | |
1552 | struct option options[] = { | |
1553 | OPT_BOOL('\0', "push", &push_mode, | |
1554 | N_("query push URLs rather than fetch URLs")), | |
1555 | OPT_BOOL('\0', "all", &all_mode, | |
1556 | N_("return all URLs")), | |
1557 | OPT_END() | |
1558 | }; | |
1559 | argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0); | |
1560 | ||
1561 | if (argc != 1) | |
1562 | usage_with_options(builtin_remote_geturl_usage, options); | |
1563 | ||
1564 | remotename = argv[0]; | |
1565 | ||
96f78d39 | 1566 | remote = remote_get(remotename); |
e459b073 | 1567 | if (!remote_is_configured(remote, 1)) |
674468b3 | 1568 | die(_("No such remote '%s'"), remotename); |
96f78d39 BB |
1569 | |
1570 | url_nr = 0; | |
1571 | if (push_mode) { | |
1572 | url = remote->pushurl; | |
1573 | url_nr = remote->pushurl_nr; | |
1574 | } | |
1575 | /* else fetch mode */ | |
1576 | ||
1577 | /* Use the fetch URL when no push URLs were found or requested. */ | |
1578 | if (!url_nr) { | |
1579 | url = remote->url; | |
1580 | url_nr = remote->url_nr; | |
1581 | } | |
1582 | ||
1583 | if (!url_nr) | |
1584 | die(_("no URLs configured for remote '%s'"), remotename); | |
1585 | ||
1586 | if (all_mode) { | |
1587 | for (i = 0; i < url_nr; i++) | |
1588 | printf_ln("%s", url[i]); | |
1589 | } else { | |
1590 | printf_ln("%s", *url); | |
1591 | } | |
1592 | ||
1593 | return 0; | |
1594 | } | |
1595 | ||
433f2be1 IL |
1596 | static int set_url(int argc, const char **argv) |
1597 | { | |
1598 | int i, push_mode = 0, add_mode = 0, delete_mode = 0; | |
1599 | int matches = 0, negative_matches = 0; | |
1600 | const char *remotename = NULL; | |
1601 | const char *newurl = NULL; | |
1602 | const char *oldurl = NULL; | |
1603 | struct remote *remote; | |
1604 | regex_t old_regex; | |
1605 | const char **urlset; | |
1606 | int urlset_nr; | |
1607 | struct strbuf name_buf = STRBUF_INIT; | |
1608 | struct option options[] = { | |
d5d09d47 SB |
1609 | OPT_BOOL('\0', "push", &push_mode, |
1610 | N_("manipulate push URLs")), | |
1611 | OPT_BOOL('\0', "add", &add_mode, | |
1612 | N_("add URL")), | |
1613 | OPT_BOOL('\0', "delete", &delete_mode, | |
fbbae140 | 1614 | N_("delete URLs")), |
433f2be1 IL |
1615 | OPT_END() |
1616 | }; | |
c49904ef | 1617 | argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage, |
433f2be1 IL |
1618 | PARSE_OPT_KEEP_ARGV0); |
1619 | ||
1620 | if (add_mode && delete_mode) | |
bb16d5dd | 1621 | die(_("--add --delete doesn't make sense")); |
433f2be1 IL |
1622 | |
1623 | if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3)) | |
1624 | usage_with_options(builtin_remote_seturl_usage, options); | |
1625 | ||
1626 | remotename = argv[1]; | |
1627 | newurl = argv[2]; | |
1628 | if (argc > 3) | |
1629 | oldurl = argv[3]; | |
1630 | ||
1631 | if (delete_mode) | |
1632 | oldurl = newurl; | |
1633 | ||
433f2be1 | 1634 | remote = remote_get(remotename); |
e459b073 | 1635 | if (!remote_is_configured(remote, 1)) |
674468b3 | 1636 | die(_("No such remote '%s'"), remotename); |
433f2be1 IL |
1637 | |
1638 | if (push_mode) { | |
1639 | strbuf_addf(&name_buf, "remote.%s.pushurl", remotename); | |
1640 | urlset = remote->pushurl; | |
1641 | urlset_nr = remote->pushurl_nr; | |
1642 | } else { | |
1643 | strbuf_addf(&name_buf, "remote.%s.url", remotename); | |
1644 | urlset = remote->url; | |
1645 | urlset_nr = remote->url_nr; | |
1646 | } | |
1647 | ||
1648 | /* Special cases that add new entry. */ | |
1649 | if ((!oldurl && !delete_mode) || add_mode) { | |
1650 | if (add_mode) | |
1651 | git_config_set_multivar(name_buf.buf, newurl, | |
45ebdcc9 | 1652 | "^$", 0); |
433f2be1 IL |
1653 | else |
1654 | git_config_set(name_buf.buf, newurl); | |
85af9f7a | 1655 | goto out; |
433f2be1 IL |
1656 | } |
1657 | ||
1658 | /* Old URL specified. Demand that one matches. */ | |
1659 | if (regcomp(&old_regex, oldurl, REG_EXTENDED)) | |
bb16d5dd | 1660 | die(_("Invalid old URL pattern: %s"), oldurl); |
433f2be1 IL |
1661 | |
1662 | for (i = 0; i < urlset_nr; i++) | |
1663 | if (!regexec(&old_regex, urlset[i], 0, NULL, 0)) | |
1664 | matches++; | |
1665 | else | |
1666 | negative_matches++; | |
1667 | if (!delete_mode && !matches) | |
bb16d5dd | 1668 | die(_("No such URL found: %s"), oldurl); |
433f2be1 | 1669 | if (delete_mode && !negative_matches && !push_mode) |
bb16d5dd | 1670 | die(_("Will not delete all non-push URLs")); |
433f2be1 IL |
1671 | |
1672 | regfree(&old_regex); | |
1673 | ||
1674 | if (!delete_mode) | |
1675 | git_config_set_multivar(name_buf.buf, newurl, oldurl, 0); | |
1676 | else | |
1677 | git_config_set_multivar(name_buf.buf, NULL, oldurl, 1); | |
85af9f7a RS |
1678 | out: |
1679 | strbuf_release(&name_buf); | |
433f2be1 IL |
1680 | return 0; |
1681 | } | |
1682 | ||
211c8968 JS |
1683 | int cmd_remote(int argc, const char **argv, const char *prefix) |
1684 | { | |
1685 | struct option options[] = { | |
fbbae140 | 1686 | OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")), |
211c8968 JS |
1687 | OPT_END() |
1688 | }; | |
1689 | int result; | |
1690 | ||
37782920 | 1691 | argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, |
211c8968 JS |
1692 | PARSE_OPT_STOP_AT_NON_OPTION); |
1693 | ||
1694 | if (argc < 1) | |
1695 | result = show_all(); | |
1696 | else if (!strcmp(argv[0], "add")) | |
1697 | result = add(argc, argv); | |
bf98421a MV |
1698 | else if (!strcmp(argv[0], "rename")) |
1699 | result = mv(argc, argv); | |
e17dba8f | 1700 | else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove")) |
211c8968 | 1701 | result = rm(argc, argv); |
bc14fac8 JS |
1702 | else if (!strcmp(argv[0], "set-head")) |
1703 | result = set_head(argc, argv); | |
3d8b6949 JN |
1704 | else if (!strcmp(argv[0], "set-branches")) |
1705 | result = set_branches(argc, argv); | |
96f78d39 BB |
1706 | else if (!strcmp(argv[0], "get-url")) |
1707 | result = get_url(argc, argv); | |
433f2be1 IL |
1708 | else if (!strcmp(argv[0], "set-url")) |
1709 | result = set_url(argc, argv); | |
211c8968 | 1710 | else if (!strcmp(argv[0], "show")) |
67a7e2d0 | 1711 | result = show(argc, argv); |
211c8968 | 1712 | else if (!strcmp(argv[0], "prune")) |
67a7e2d0 | 1713 | result = prune(argc, argv); |
211c8968 JS |
1714 | else if (!strcmp(argv[0], "update")) |
1715 | result = update(argc, argv); | |
1716 | else { | |
bb16d5dd | 1717 | error(_("Unknown subcommand: %s"), argv[0]); |
211c8968 JS |
1718 | usage_with_options(builtin_remote_usage, options); |
1719 | } | |
1720 | ||
1721 | return result ? 1 : 0; | |
1722 | } |