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