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