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