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