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