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