]>
Commit | Line | Data |
---|---|---|
211c8968 JS |
1 | #include "cache.h" |
2 | #include "parse-options.h" | |
3 | #include "transport.h" | |
4 | #include "remote.h" | |
c455c87c | 5 | #include "string-list.h" |
211c8968 JS |
6 | #include "strbuf.h" |
7 | #include "run-command.h" | |
8 | #include "refs.h" | |
9 | ||
10 | static const char * const builtin_remote_usage[] = { | |
357af14f CR |
11 | "git remote [-v | --verbose]", |
12 | "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>", | |
bf98421a | 13 | "git remote rename <old> <new>", |
211c8968 | 14 | "git remote rm <name>", |
4504107d TH |
15 | "git remote set-head <name> (-a | -d | <branch>)", |
16 | "git remote [-v | --verbose] show [-n] <name>", | |
357af14f | 17 | "git remote prune [-n | --dry-run] <name>", |
997c2a45 | 18 | "git remote [-v | --verbose] update [-p | --prune] [group | remote]", |
211c8968 JS |
19 | NULL |
20 | }; | |
21 | ||
4504107d TH |
22 | static const char * const builtin_remote_add_usage[] = { |
23 | "git remote add [<options>] <name> <url>", | |
24 | NULL | |
25 | }; | |
26 | ||
27 | static const char * const builtin_remote_rename_usage[] = { | |
28 | "git remote rename <old> <new>", | |
29 | NULL | |
30 | }; | |
31 | ||
32 | static const char * const builtin_remote_rm_usage[] = { | |
33 | "git remote rm <name>", | |
34 | NULL | |
35 | }; | |
36 | ||
37 | static const char * const builtin_remote_sethead_usage[] = { | |
38 | "git remote set-head <name> (-a | -d | <branch>])", | |
39 | NULL | |
40 | }; | |
41 | ||
42 | static const char * const builtin_remote_show_usage[] = { | |
43 | "git remote show [<options>] <name>", | |
44 | NULL | |
45 | }; | |
46 | ||
47 | static const char * const builtin_remote_prune_usage[] = { | |
48 | "git remote prune [<options>] <name>", | |
49 | NULL | |
50 | }; | |
51 | ||
52 | static const char * const builtin_remote_update_usage[] = { | |
53 | "git remote update [<options>] [<group> | <remote>]...", | |
54 | NULL | |
55 | }; | |
56 | ||
e61e0cc6 JS |
57 | #define GET_REF_STATES (1<<0) |
58 | #define GET_HEAD_NAMES (1<<1) | |
e5dcbfd9 | 59 | #define GET_PUSH_REF_STATES (1<<2) |
e61e0cc6 | 60 | |
211c8968 JS |
61 | static int verbose; |
62 | ||
c4112bb6 | 63 | static int show_all(void); |
b92c5f22 | 64 | static int prune_remote(const char *remote, int dry_run); |
c4112bb6 | 65 | |
211c8968 JS |
66 | static inline int postfixcmp(const char *string, const char *postfix) |
67 | { | |
68 | int len1 = strlen(string), len2 = strlen(postfix); | |
69 | if (len1 < len2) | |
70 | return 1; | |
71 | return strcmp(string + len1 - len2, postfix); | |
72 | } | |
73 | ||
211c8968 JS |
74 | static int opt_parse_track(const struct option *opt, const char *arg, int not) |
75 | { | |
c455c87c | 76 | struct string_list *list = opt->value; |
211c8968 | 77 | if (not) |
c455c87c | 78 | string_list_clear(list, 0); |
211c8968 | 79 | else |
c455c87c | 80 | string_list_append(arg, list); |
211c8968 JS |
81 | return 0; |
82 | } | |
83 | ||
84 | static int fetch_remote(const char *name) | |
85 | { | |
dbbd56f1 CR |
86 | const char *argv[] = { "fetch", name, NULL, NULL }; |
87 | if (verbose) { | |
88 | argv[1] = "-v"; | |
89 | argv[2] = name; | |
90 | } | |
3000658f | 91 | printf("Updating %s\n", name); |
211c8968 JS |
92 | if (run_command_v_opt(argv, RUN_GIT_CMD)) |
93 | return error("Could not fetch %s", name); | |
94 | return 0; | |
95 | } | |
96 | ||
97 | static int add(int argc, const char **argv) | |
98 | { | |
99 | int fetch = 0, mirror = 0; | |
c455c87c | 100 | struct string_list track = { NULL, 0, 0 }; |
211c8968 JS |
101 | const char *master = NULL; |
102 | struct remote *remote; | |
f285a2d7 | 103 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
211c8968 JS |
104 | const char *name, *url; |
105 | int i; | |
106 | ||
107 | struct option options[] = { | |
211c8968 JS |
108 | OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"), |
109 | OPT_CALLBACK('t', "track", &track, "branch", | |
110 | "branch(es) to track", opt_parse_track), | |
111 | OPT_STRING('m', "master", &master, "branch", "master branch"), | |
112 | OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"), | |
113 | OPT_END() | |
114 | }; | |
115 | ||
4504107d | 116 | argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage, |
37782920 | 117 | 0); |
211c8968 JS |
118 | |
119 | if (argc < 2) | |
4504107d | 120 | usage_with_options(builtin_remote_add_usage, options); |
211c8968 JS |
121 | |
122 | name = argv[0]; | |
123 | url = argv[1]; | |
124 | ||
125 | remote = remote_get(name); | |
126 | if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) || | |
127 | remote->fetch_refspec_nr)) | |
128 | die("remote %s already exists.", name); | |
129 | ||
24b6177e JF |
130 | strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name); |
131 | if (!valid_fetch_refspec(buf2.buf)) | |
132 | die("'%s' is not a valid remote name", name); | |
133 | ||
211c8968 JS |
134 | strbuf_addf(&buf, "remote.%s.url", name); |
135 | if (git_config_set(buf.buf, url)) | |
136 | return 1; | |
137 | ||
24b6177e JF |
138 | strbuf_reset(&buf); |
139 | strbuf_addf(&buf, "remote.%s.fetch", name); | |
140 | ||
211c8968 | 141 | if (track.nr == 0) |
c455c87c | 142 | string_list_append("*", &track); |
211c8968 | 143 | for (i = 0; i < track.nr; i++) { |
c455c87c | 144 | struct string_list_item *item = track.items + i; |
211c8968 | 145 | |
211c8968 | 146 | strbuf_reset(&buf2); |
1ce89cc4 | 147 | strbuf_addch(&buf2, '+'); |
211c8968 JS |
148 | if (mirror) |
149 | strbuf_addf(&buf2, "refs/%s:refs/%s", | |
c455c87c | 150 | item->string, item->string); |
211c8968 JS |
151 | else |
152 | strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s", | |
c455c87c | 153 | item->string, name, item->string); |
211c8968 JS |
154 | if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) |
155 | return 1; | |
156 | } | |
157 | ||
84bb2dfd PB |
158 | if (mirror) { |
159 | strbuf_reset(&buf); | |
160 | strbuf_addf(&buf, "remote.%s.mirror", name); | |
bc699afc | 161 | if (git_config_set(buf.buf, "true")) |
84bb2dfd PB |
162 | return 1; |
163 | } | |
164 | ||
211c8968 JS |
165 | if (fetch && fetch_remote(name)) |
166 | return 1; | |
167 | ||
168 | if (master) { | |
169 | strbuf_reset(&buf); | |
170 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); | |
171 | ||
172 | strbuf_reset(&buf2); | |
173 | strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); | |
174 | ||
175 | if (create_symref(buf.buf, buf2.buf, "remote add")) | |
176 | return error("Could not setup master '%s'", master); | |
177 | } | |
178 | ||
179 | strbuf_release(&buf); | |
180 | strbuf_release(&buf2); | |
c455c87c | 181 | string_list_clear(&track, 0); |
211c8968 JS |
182 | |
183 | return 0; | |
184 | } | |
185 | ||
186 | struct branch_info { | |
e0cc81e6 | 187 | char *remote_name; |
c455c87c | 188 | struct string_list merge; |
7ecbbf87 | 189 | int rebase; |
211c8968 JS |
190 | }; |
191 | ||
c455c87c | 192 | static struct string_list branch_list; |
211c8968 | 193 | |
72972eb3 JH |
194 | static const char *abbrev_ref(const char *name, const char *prefix) |
195 | { | |
196 | const char *abbrev = skip_prefix(name, prefix); | |
197 | if (abbrev) | |
198 | return abbrev; | |
199 | return name; | |
200 | } | |
201 | #define abbrev_branch(name) abbrev_ref((name), "refs/heads/") | |
202 | ||
ef90d6d4 | 203 | static int config_read_branches(const char *key, const char *value, void *cb) |
211c8968 JS |
204 | { |
205 | if (!prefixcmp(key, "branch.")) { | |
7ecbbf87 | 206 | const char *orig_key = key; |
211c8968 | 207 | char *name; |
c455c87c | 208 | struct string_list_item *item; |
211c8968 | 209 | struct branch_info *info; |
7ecbbf87 | 210 | enum { REMOTE, MERGE, REBASE } type; |
211c8968 JS |
211 | |
212 | key += 7; | |
213 | if (!postfixcmp(key, ".remote")) { | |
214 | name = xstrndup(key, strlen(key) - 7); | |
215 | type = REMOTE; | |
216 | } else if (!postfixcmp(key, ".merge")) { | |
217 | name = xstrndup(key, strlen(key) - 6); | |
218 | type = MERGE; | |
7ecbbf87 JS |
219 | } else if (!postfixcmp(key, ".rebase")) { |
220 | name = xstrndup(key, strlen(key) - 7); | |
221 | type = REBASE; | |
211c8968 JS |
222 | } else |
223 | return 0; | |
224 | ||
c455c87c | 225 | item = string_list_insert(name, &branch_list); |
211c8968 JS |
226 | |
227 | if (!item->util) | |
228 | item->util = xcalloc(sizeof(struct branch_info), 1); | |
229 | info = item->util; | |
230 | if (type == REMOTE) { | |
e0cc81e6 | 231 | if (info->remote_name) |
7ecbbf87 | 232 | warning("more than one %s", orig_key); |
e0cc81e6 | 233 | info->remote_name = xstrdup(value); |
7ecbbf87 | 234 | } else if (type == MERGE) { |
211c8968 | 235 | char *space = strchr(value, ' '); |
72972eb3 | 236 | value = abbrev_branch(value); |
211c8968 JS |
237 | while (space) { |
238 | char *merge; | |
239 | merge = xstrndup(value, space - value); | |
c455c87c | 240 | string_list_append(merge, &info->merge); |
72972eb3 | 241 | value = abbrev_branch(space + 1); |
211c8968 JS |
242 | space = strchr(value, ' '); |
243 | } | |
c455c87c | 244 | string_list_append(xstrdup(value), &info->merge); |
7ecbbf87 JS |
245 | } else |
246 | info->rebase = git_config_bool(orig_key, value); | |
211c8968 JS |
247 | } |
248 | return 0; | |
249 | } | |
250 | ||
251 | static void read_branches(void) | |
252 | { | |
253 | if (branch_list.nr) | |
254 | return; | |
ef90d6d4 | 255 | git_config(config_read_branches, NULL); |
211c8968 JS |
256 | } |
257 | ||
258 | struct ref_states { | |
259 | struct remote *remote; | |
e5dcbfd9 | 260 | struct string_list new, stale, tracked, heads, push; |
7ecbbf87 | 261 | int queried; |
211c8968 JS |
262 | }; |
263 | ||
264 | static int handle_one_branch(const char *refname, | |
265 | const unsigned char *sha1, int flags, void *cb_data) | |
266 | { | |
267 | struct ref_states *states = cb_data; | |
268 | struct refspec refspec; | |
269 | ||
270 | memset(&refspec, 0, sizeof(refspec)); | |
271 | refspec.dst = (char *)refname; | |
272 | if (!remote_find_tracking(states->remote, &refspec)) { | |
c455c87c | 273 | struct string_list_item *item; |
72972eb3 | 274 | const char *name = abbrev_branch(refspec.src); |
740fdd27 JS |
275 | /* symbolic refs pointing nowhere were handled already */ |
276 | if ((flags & REF_ISSYMREF) || | |
3bd92563 JS |
277 | string_list_has_string(&states->tracked, name) || |
278 | string_list_has_string(&states->new, name)) | |
211c8968 | 279 | return 0; |
c455c87c | 280 | item = string_list_append(name, &states->stale); |
211c8968 JS |
281 | item->util = xstrdup(refname); |
282 | } | |
283 | return 0; | |
284 | } | |
285 | ||
e0cc81e6 | 286 | static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) |
211c8968 JS |
287 | { |
288 | struct ref *fetch_map = NULL, **tail = &fetch_map; | |
e0cc81e6 | 289 | struct ref *ref; |
211c8968 JS |
290 | int i; |
291 | ||
292 | for (i = 0; i < states->remote->fetch_refspec_nr; i++) | |
e0cc81e6 | 293 | if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1)) |
211c8968 JS |
294 | die("Could not get fetch map for refspec %s", |
295 | states->remote->fetch_refspec[i]); | |
296 | ||
c455c87c | 297 | states->new.strdup_strings = states->tracked.strdup_strings = 1; |
211c8968 | 298 | for (ref = fetch_map; ref; ref = ref->next) { |
211c8968 | 299 | unsigned char sha1[20]; |
211c8968 | 300 | if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1)) |
7b9a5e27 JS |
301 | string_list_append(abbrev_branch(ref->name), &states->new); |
302 | else | |
303 | string_list_append(abbrev_branch(ref->name), &states->tracked); | |
211c8968 JS |
304 | } |
305 | free_refs(fetch_map); | |
306 | ||
3bd92563 JS |
307 | sort_string_list(&states->new); |
308 | sort_string_list(&states->tracked); | |
211c8968 | 309 | for_each_ref(handle_one_branch, states); |
c455c87c | 310 | sort_string_list(&states->stale); |
211c8968 JS |
311 | |
312 | return 0; | |
313 | } | |
314 | ||
e5dcbfd9 JS |
315 | struct push_info { |
316 | char *dest; | |
317 | int forced; | |
318 | enum { | |
319 | PUSH_STATUS_CREATE = 0, | |
320 | PUSH_STATUS_DELETE, | |
321 | PUSH_STATUS_UPTODATE, | |
322 | PUSH_STATUS_FASTFORWARD, | |
323 | PUSH_STATUS_OUTOFDATE, | |
324 | PUSH_STATUS_NOTQUERIED, | |
325 | } status; | |
326 | }; | |
327 | ||
328 | static int get_push_ref_states(const struct ref *remote_refs, | |
329 | struct ref_states *states) | |
330 | { | |
331 | struct remote *remote = states->remote; | |
6d2bf96e | 332 | struct ref *ref, *local_refs, *push_map; |
e5dcbfd9 JS |
333 | if (remote->mirror) |
334 | return 0; | |
335 | ||
336 | local_refs = get_local_heads(); | |
6a01554e | 337 | push_map = copy_ref_list(remote_refs); |
e5dcbfd9 | 338 | |
6d2bf96e | 339 | match_refs(local_refs, &push_map, remote->push_refspec_nr, |
e5dcbfd9 JS |
340 | remote->push_refspec, MATCH_REFS_NONE); |
341 | ||
342 | states->push.strdup_strings = 1; | |
343 | for (ref = push_map; ref; ref = ref->next) { | |
344 | struct string_list_item *item; | |
345 | struct push_info *info; | |
346 | ||
347 | if (!ref->peer_ref) | |
348 | continue; | |
349 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); | |
350 | ||
351 | item = string_list_append(abbrev_branch(ref->peer_ref->name), | |
352 | &states->push); | |
353 | item->util = xcalloc(sizeof(struct push_info), 1); | |
354 | info = item->util; | |
355 | info->forced = ref->force; | |
356 | info->dest = xstrdup(abbrev_branch(ref->name)); | |
357 | ||
358 | if (is_null_sha1(ref->new_sha1)) { | |
359 | info->status = PUSH_STATUS_DELETE; | |
360 | } else if (!hashcmp(ref->old_sha1, ref->new_sha1)) | |
361 | info->status = PUSH_STATUS_UPTODATE; | |
362 | else if (is_null_sha1(ref->old_sha1)) | |
363 | info->status = PUSH_STATUS_CREATE; | |
364 | else if (has_sha1_file(ref->old_sha1) && | |
365 | ref_newer(ref->new_sha1, ref->old_sha1)) | |
366 | info->status = PUSH_STATUS_FASTFORWARD; | |
367 | else | |
368 | info->status = PUSH_STATUS_OUTOFDATE; | |
e5dcbfd9 JS |
369 | } |
370 | free_refs(local_refs); | |
371 | free_refs(push_map); | |
372 | return 0; | |
373 | } | |
374 | ||
375 | static int get_push_ref_states_noquery(struct ref_states *states) | |
376 | { | |
377 | int i; | |
378 | struct remote *remote = states->remote; | |
379 | struct string_list_item *item; | |
380 | struct push_info *info; | |
381 | ||
382 | if (remote->mirror) | |
383 | return 0; | |
384 | ||
385 | states->push.strdup_strings = 1; | |
386 | if (!remote->push_refspec_nr) { | |
387 | item = string_list_append("(matching)", &states->push); | |
388 | info = item->util = xcalloc(sizeof(struct push_info), 1); | |
389 | info->status = PUSH_STATUS_NOTQUERIED; | |
390 | info->dest = xstrdup(item->string); | |
391 | } | |
392 | for (i = 0; i < remote->push_refspec_nr; i++) { | |
393 | struct refspec *spec = remote->push + i; | |
e5dcbfd9 JS |
394 | if (spec->matching) |
395 | item = string_list_append("(matching)", &states->push); | |
5ad6b025 | 396 | else if (strlen(spec->src)) |
e5dcbfd9 JS |
397 | item = string_list_append(spec->src, &states->push); |
398 | else | |
399 | item = string_list_append("(delete)", &states->push); | |
400 | ||
401 | info = item->util = xcalloc(sizeof(struct push_info), 1); | |
402 | info->forced = spec->force; | |
403 | info->status = PUSH_STATUS_NOTQUERIED; | |
5ad6b025 | 404 | info->dest = xstrdup(spec->dst ? spec->dst : item->string); |
e5dcbfd9 JS |
405 | } |
406 | return 0; | |
407 | } | |
408 | ||
e61e0cc6 JS |
409 | static int get_head_names(const struct ref *remote_refs, struct ref_states *states) |
410 | { | |
411 | struct ref *ref, *matches; | |
412 | struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; | |
413 | struct refspec refspec; | |
414 | ||
415 | refspec.force = 0; | |
416 | refspec.pattern = 1; | |
5ad6b025 | 417 | refspec.src = refspec.dst = "refs/heads/*"; |
e61e0cc6 JS |
418 | states->heads.strdup_strings = 1; |
419 | get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); | |
420 | matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), | |
421 | fetch_map, 1); | |
eeefa7c9 | 422 | for (ref = matches; ref; ref = ref->next) |
e61e0cc6 JS |
423 | string_list_append(abbrev_branch(ref->name), &states->heads); |
424 | ||
425 | free_refs(fetch_map); | |
426 | free_refs(matches); | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
7ad2458f SP |
431 | struct known_remote { |
432 | struct known_remote *next; | |
433 | struct remote *remote; | |
434 | }; | |
435 | ||
436 | struct known_remotes { | |
437 | struct remote *to_delete; | |
438 | struct known_remote *list; | |
439 | }; | |
440 | ||
441 | static int add_known_remote(struct remote *remote, void *cb_data) | |
442 | { | |
443 | struct known_remotes *all = cb_data; | |
444 | struct known_remote *r; | |
445 | ||
446 | if (!strcmp(all->to_delete->name, remote->name)) | |
447 | return 0; | |
448 | ||
449 | r = xmalloc(sizeof(*r)); | |
450 | r->remote = remote; | |
451 | r->next = all->list; | |
452 | all->list = r; | |
453 | return 0; | |
454 | } | |
455 | ||
211c8968 | 456 | struct branches_for_remote { |
7ad2458f | 457 | struct remote *remote; |
441adf0c | 458 | struct string_list *branches, *skipped; |
7ad2458f | 459 | struct known_remotes *keep; |
211c8968 JS |
460 | }; |
461 | ||
462 | static int add_branch_for_removal(const char *refname, | |
463 | const unsigned char *sha1, int flags, void *cb_data) | |
464 | { | |
465 | struct branches_for_remote *branches = cb_data; | |
7ad2458f | 466 | struct refspec refspec; |
c455c87c | 467 | struct string_list_item *item; |
7ad2458f | 468 | struct known_remote *kr; |
211c8968 | 469 | |
7ad2458f SP |
470 | memset(&refspec, 0, sizeof(refspec)); |
471 | refspec.dst = (char *)refname; | |
472 | if (remote_find_tracking(branches->remote, &refspec)) | |
473 | return 0; | |
474 | ||
475 | /* don't delete a branch if another remote also uses it */ | |
476 | for (kr = branches->keep->list; kr; kr = kr->next) { | |
477 | memset(&refspec, 0, sizeof(refspec)); | |
478 | refspec.dst = (char *)refname; | |
479 | if (!remote_find_tracking(kr->remote, &refspec)) | |
480 | return 0; | |
481 | } | |
3b9dcff5 | 482 | |
441adf0c JS |
483 | /* don't delete non-remote refs */ |
484 | if (prefixcmp(refname, "refs/remotes")) { | |
485 | /* advise user how to delete local branches */ | |
486 | if (!prefixcmp(refname, "refs/heads/")) | |
487 | string_list_append(abbrev_branch(refname), | |
488 | branches->skipped); | |
489 | /* silently skip over other non-remote refs */ | |
490 | return 0; | |
491 | } | |
492 | ||
7ad2458f SP |
493 | /* make sure that symrefs are deleted */ |
494 | if (flags & REF_ISSYMREF) | |
9db56f71 | 495 | return unlink(git_path("%s", refname)); |
3b9dcff5 | 496 | |
c455c87c | 497 | item = string_list_append(refname, branches->branches); |
7ad2458f SP |
498 | item->util = xmalloc(20); |
499 | hashcpy(item->util, sha1); | |
211c8968 JS |
500 | |
501 | return 0; | |
502 | } | |
503 | ||
bf98421a MV |
504 | struct rename_info { |
505 | const char *old; | |
506 | const char *new; | |
507 | struct string_list *remote_branches; | |
508 | }; | |
509 | ||
510 | static int read_remote_branches(const char *refname, | |
511 | const unsigned char *sha1, int flags, void *cb_data) | |
512 | { | |
513 | struct rename_info *rename = cb_data; | |
514 | struct strbuf buf = STRBUF_INIT; | |
515 | struct string_list_item *item; | |
516 | int flag; | |
517 | unsigned char orig_sha1[20]; | |
518 | const char *symref; | |
519 | ||
520 | strbuf_addf(&buf, "refs/remotes/%s", rename->old); | |
eeefa7c9 | 521 | if (!prefixcmp(refname, buf.buf)) { |
bf98421a MV |
522 | item = string_list_append(xstrdup(refname), rename->remote_branches); |
523 | symref = resolve_ref(refname, orig_sha1, 1, &flag); | |
524 | if (flag & REF_ISSYMREF) | |
525 | item->util = xstrdup(symref); | |
526 | else | |
527 | item->util = NULL; | |
528 | } | |
529 | ||
530 | return 0; | |
531 | } | |
532 | ||
1dd1239a MV |
533 | static int migrate_file(struct remote *remote) |
534 | { | |
535 | struct strbuf buf = STRBUF_INIT; | |
536 | int i; | |
537 | char *path = NULL; | |
538 | ||
539 | strbuf_addf(&buf, "remote.%s.url", remote->name); | |
540 | for (i = 0; i < remote->url_nr; i++) | |
541 | if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0)) | |
542 | return error("Could not append '%s' to '%s'", | |
543 | remote->url[i], buf.buf); | |
544 | strbuf_reset(&buf); | |
545 | strbuf_addf(&buf, "remote.%s.push", remote->name); | |
546 | for (i = 0; i < remote->push_refspec_nr; i++) | |
547 | if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0)) | |
548 | return error("Could not append '%s' to '%s'", | |
549 | remote->push_refspec[i], buf.buf); | |
550 | strbuf_reset(&buf); | |
551 | strbuf_addf(&buf, "remote.%s.fetch", remote->name); | |
552 | for (i = 0; i < remote->fetch_refspec_nr; i++) | |
553 | if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0)) | |
554 | return error("Could not append '%s' to '%s'", | |
555 | remote->fetch_refspec[i], buf.buf); | |
556 | if (remote->origin == REMOTE_REMOTES) | |
557 | path = git_path("remotes/%s", remote->name); | |
558 | else if (remote->origin == REMOTE_BRANCHES) | |
559 | path = git_path("branches/%s", remote->name); | |
691f1a28 AR |
560 | if (path) |
561 | unlink_or_warn(path); | |
1dd1239a MV |
562 | return 0; |
563 | } | |
564 | ||
bf98421a MV |
565 | static int mv(int argc, const char **argv) |
566 | { | |
567 | struct option options[] = { | |
568 | OPT_END() | |
569 | }; | |
570 | struct remote *oldremote, *newremote; | |
571 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT; | |
572 | struct string_list remote_branches = { NULL, 0, 0, 0 }; | |
573 | struct rename_info rename; | |
574 | int i; | |
575 | ||
576 | if (argc != 3) | |
4504107d | 577 | usage_with_options(builtin_remote_rename_usage, options); |
bf98421a MV |
578 | |
579 | rename.old = argv[1]; | |
580 | rename.new = argv[2]; | |
581 | rename.remote_branches = &remote_branches; | |
582 | ||
583 | oldremote = remote_get(rename.old); | |
584 | if (!oldremote) | |
585 | die("No such remote: %s", rename.old); | |
586 | ||
1dd1239a MV |
587 | if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG) |
588 | return migrate_file(oldremote); | |
589 | ||
bf98421a MV |
590 | newremote = remote_get(rename.new); |
591 | if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr)) | |
592 | die("remote %s already exists.", rename.new); | |
593 | ||
594 | strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new); | |
595 | if (!valid_fetch_refspec(buf.buf)) | |
596 | die("'%s' is not a valid remote name", rename.new); | |
597 | ||
598 | strbuf_reset(&buf); | |
599 | strbuf_addf(&buf, "remote.%s", rename.old); | |
600 | strbuf_addf(&buf2, "remote.%s", rename.new); | |
601 | if (git_config_rename_section(buf.buf, buf2.buf) < 1) | |
602 | return error("Could not rename config section '%s' to '%s'", | |
603 | buf.buf, buf2.buf); | |
604 | ||
605 | strbuf_reset(&buf); | |
606 | strbuf_addf(&buf, "remote.%s.fetch", rename.new); | |
607 | if (git_config_set_multivar(buf.buf, NULL, NULL, 1)) | |
608 | return error("Could not remove config section '%s'", buf.buf); | |
609 | for (i = 0; i < oldremote->fetch_refspec_nr; i++) { | |
610 | char *ptr; | |
611 | ||
612 | strbuf_reset(&buf2); | |
613 | strbuf_addstr(&buf2, oldremote->fetch_refspec[i]); | |
614 | ptr = strstr(buf2.buf, rename.old); | |
615 | if (ptr) | |
616 | strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old), | |
617 | rename.new, strlen(rename.new)); | |
618 | if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) | |
619 | return error("Could not append '%s'", buf.buf); | |
620 | } | |
621 | ||
622 | read_branches(); | |
623 | for (i = 0; i < branch_list.nr; i++) { | |
624 | struct string_list_item *item = branch_list.items + i; | |
625 | struct branch_info *info = item->util; | |
e0cc81e6 | 626 | if (info->remote_name && !strcmp(info->remote_name, rename.old)) { |
bf98421a MV |
627 | strbuf_reset(&buf); |
628 | strbuf_addf(&buf, "branch.%s.remote", item->string); | |
629 | if (git_config_set(buf.buf, rename.new)) { | |
630 | return error("Could not set '%s'", buf.buf); | |
631 | } | |
632 | } | |
633 | } | |
634 | ||
635 | /* | |
636 | * First remove symrefs, then rename the rest, finally create | |
637 | * the new symrefs. | |
638 | */ | |
639 | for_each_ref(read_remote_branches, &rename); | |
640 | for (i = 0; i < remote_branches.nr; i++) { | |
641 | struct string_list_item *item = remote_branches.items + i; | |
642 | int flag = 0; | |
643 | unsigned char sha1[20]; | |
bf98421a | 644 | |
eb3a9dd3 | 645 | resolve_ref(item->string, sha1, 1, &flag); |
bf98421a MV |
646 | if (!(flag & REF_ISSYMREF)) |
647 | continue; | |
648 | if (delete_ref(item->string, NULL, REF_NODEREF)) | |
649 | die("deleting '%s' failed", item->string); | |
650 | } | |
651 | for (i = 0; i < remote_branches.nr; i++) { | |
652 | struct string_list_item *item = remote_branches.items + i; | |
653 | ||
654 | if (item->util) | |
655 | continue; | |
656 | strbuf_reset(&buf); | |
657 | strbuf_addstr(&buf, item->string); | |
658 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), | |
659 | rename.new, strlen(rename.new)); | |
660 | strbuf_reset(&buf2); | |
661 | strbuf_addf(&buf2, "remote: renamed %s to %s", | |
662 | item->string, buf.buf); | |
663 | if (rename_ref(item->string, buf.buf, buf2.buf)) | |
664 | die("renaming '%s' failed", item->string); | |
665 | } | |
666 | for (i = 0; i < remote_branches.nr; i++) { | |
667 | struct string_list_item *item = remote_branches.items + i; | |
668 | ||
669 | if (!item->util) | |
670 | continue; | |
671 | strbuf_reset(&buf); | |
672 | strbuf_addstr(&buf, item->string); | |
673 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), | |
674 | rename.new, strlen(rename.new)); | |
675 | strbuf_reset(&buf2); | |
676 | strbuf_addstr(&buf2, item->util); | |
677 | strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old), | |
678 | rename.new, strlen(rename.new)); | |
679 | strbuf_reset(&buf3); | |
680 | strbuf_addf(&buf3, "remote: renamed %s to %s", | |
681 | item->string, buf.buf); | |
682 | if (create_symref(buf.buf, buf2.buf, buf3.buf)) | |
683 | die("creating '%s' failed", buf.buf); | |
684 | } | |
685 | return 0; | |
686 | } | |
687 | ||
c455c87c | 688 | static int remove_branches(struct string_list *branches) |
211c8968 JS |
689 | { |
690 | int i, result = 0; | |
691 | for (i = 0; i < branches->nr; i++) { | |
c455c87c JS |
692 | struct string_list_item *item = branches->items + i; |
693 | const char *refname = item->string; | |
211c8968 JS |
694 | unsigned char *sha1 = item->util; |
695 | ||
eca35a25 | 696 | if (delete_ref(refname, sha1, 0)) |
211c8968 JS |
697 | result |= error("Could not remove branch %s", refname); |
698 | } | |
699 | return result; | |
700 | } | |
701 | ||
702 | static int rm(int argc, const char **argv) | |
703 | { | |
704 | struct option options[] = { | |
705 | OPT_END() | |
706 | }; | |
707 | struct remote *remote; | |
f285a2d7 | 708 | struct strbuf buf = STRBUF_INIT; |
7ad2458f | 709 | struct known_remotes known_remotes = { NULL, NULL }; |
c455c87c | 710 | struct string_list branches = { NULL, 0, 0, 1 }; |
441adf0c JS |
711 | struct string_list skipped = { NULL, 0, 0, 1 }; |
712 | struct branches_for_remote cb_data = { | |
713 | NULL, &branches, &skipped, &known_remotes | |
714 | }; | |
e02f1762 | 715 | int i, result; |
211c8968 JS |
716 | |
717 | if (argc != 2) | |
4504107d | 718 | usage_with_options(builtin_remote_rm_usage, options); |
211c8968 JS |
719 | |
720 | remote = remote_get(argv[1]); | |
721 | if (!remote) | |
722 | die("No such remote: %s", argv[1]); | |
723 | ||
7ad2458f SP |
724 | known_remotes.to_delete = remote; |
725 | for_each_remote(add_known_remote, &known_remotes); | |
726 | ||
211c8968 JS |
727 | strbuf_addf(&buf, "remote.%s", remote->name); |
728 | if (git_config_rename_section(buf.buf, NULL) < 1) | |
729 | return error("Could not remove config section '%s'", buf.buf); | |
730 | ||
731 | read_branches(); | |
732 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 733 | struct string_list_item *item = branch_list.items + i; |
211c8968 | 734 | struct branch_info *info = item->util; |
e0cc81e6 | 735 | if (info->remote_name && !strcmp(info->remote_name, remote->name)) { |
211c8968 JS |
736 | const char *keys[] = { "remote", "merge", NULL }, **k; |
737 | for (k = keys; *k; k++) { | |
738 | strbuf_reset(&buf); | |
739 | strbuf_addf(&buf, "branch.%s.%s", | |
c455c87c | 740 | item->string, *k); |
211c8968 JS |
741 | if (git_config_set(buf.buf, NULL)) { |
742 | strbuf_release(&buf); | |
743 | return -1; | |
744 | } | |
745 | } | |
746 | } | |
747 | } | |
748 | ||
749 | /* | |
750 | * We cannot just pass a function to for_each_ref() which deletes | |
751 | * the branches one by one, since for_each_ref() relies on cached | |
752 | * refs, which are invalidated when deleting a branch. | |
753 | */ | |
7ad2458f | 754 | cb_data.remote = remote; |
e02f1762 | 755 | result = for_each_ref(add_branch_for_removal, &cb_data); |
211c8968 JS |
756 | strbuf_release(&buf); |
757 | ||
e02f1762 JS |
758 | if (!result) |
759 | result = remove_branches(&branches); | |
c455c87c | 760 | string_list_clear(&branches, 1); |
211c8968 | 761 | |
441adf0c JS |
762 | if (skipped.nr) { |
763 | fprintf(stderr, skipped.nr == 1 ? | |
764 | "Note: A non-remote branch was not removed; " | |
765 | "to delete it, use:\n" : | |
766 | "Note: Non-remote branches were not removed; " | |
767 | "to delete them, use:\n"); | |
768 | for (i = 0; i < skipped.nr; i++) | |
769 | fprintf(stderr, " git branch -d %s\n", | |
770 | skipped.items[i].string); | |
771 | } | |
772 | string_list_clear(&skipped, 0); | |
773 | ||
e02f1762 | 774 | return result; |
211c8968 JS |
775 | } |
776 | ||
2af202be | 777 | static void clear_push_info(void *util, const char *string) |
211c8968 | 778 | { |
e5dcbfd9 JS |
779 | struct push_info *info = util; |
780 | free(info->dest); | |
781 | free(info); | |
782 | } | |
211c8968 | 783 | |
88733235 JS |
784 | static void free_remote_ref_states(struct ref_states *states) |
785 | { | |
786 | string_list_clear(&states->new, 0); | |
787 | string_list_clear(&states->stale, 0); | |
788 | string_list_clear(&states->tracked, 0); | |
e61e0cc6 | 789 | string_list_clear(&states->heads, 0); |
e5dcbfd9 | 790 | string_list_clear_func(&states->push, clear_push_info); |
88733235 | 791 | } |
211c8968 | 792 | |
cca7c97e JS |
793 | static int append_ref_to_tracked_list(const char *refname, |
794 | const unsigned char *sha1, int flags, void *cb_data) | |
795 | { | |
796 | struct ref_states *states = cb_data; | |
797 | struct refspec refspec; | |
798 | ||
3bd92563 JS |
799 | if (flags & REF_ISSYMREF) |
800 | return 0; | |
801 | ||
cca7c97e JS |
802 | memset(&refspec, 0, sizeof(refspec)); |
803 | refspec.dst = (char *)refname; | |
804 | if (!remote_find_tracking(states->remote, &refspec)) | |
805 | string_list_append(abbrev_branch(refspec.src), &states->tracked); | |
806 | ||
807 | return 0; | |
211c8968 JS |
808 | } |
809 | ||
67a7e2d0 OM |
810 | static int get_remote_ref_states(const char *name, |
811 | struct ref_states *states, | |
812 | int query) | |
813 | { | |
814 | struct transport *transport; | |
e0cc81e6 | 815 | const struct ref *remote_refs; |
67a7e2d0 OM |
816 | |
817 | states->remote = remote_get(name); | |
818 | if (!states->remote) | |
819 | return error("No such remote: %s", name); | |
820 | ||
821 | read_branches(); | |
822 | ||
823 | if (query) { | |
345a3803 | 824 | transport = transport_get(states->remote, states->remote->url_nr > 0 ? |
67a7e2d0 | 825 | states->remote->url[0] : NULL); |
e0cc81e6 | 826 | remote_refs = transport_get_remote_refs(transport); |
67a7e2d0 OM |
827 | transport_disconnect(transport); |
828 | ||
7ecbbf87 | 829 | states->queried = 1; |
e61e0cc6 JS |
830 | if (query & GET_REF_STATES) |
831 | get_ref_states(remote_refs, states); | |
832 | if (query & GET_HEAD_NAMES) | |
833 | get_head_names(remote_refs, states); | |
e5dcbfd9 JS |
834 | if (query & GET_PUSH_REF_STATES) |
835 | get_push_ref_states(remote_refs, states); | |
3bd92563 | 836 | } else { |
cca7c97e | 837 | for_each_ref(append_ref_to_tracked_list, states); |
3bd92563 | 838 | sort_string_list(&states->tracked); |
e5dcbfd9 | 839 | get_push_ref_states_noquery(states); |
67a7e2d0 OM |
840 | } |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
7ecbbf87 JS |
845 | struct show_info { |
846 | struct string_list *list; | |
847 | struct ref_states *states; | |
e5dcbfd9 | 848 | int width, width2; |
7ecbbf87 JS |
849 | int any_rebase; |
850 | }; | |
851 | ||
2af202be | 852 | static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) |
e7d5a97d | 853 | { |
7ecbbf87 JS |
854 | struct show_info *info = cb_data; |
855 | int n = strlen(item->string); | |
856 | if (n > info->width) | |
857 | info->width = n; | |
858 | string_list_insert(item->string, info->list); | |
859 | return 0; | |
860 | } | |
e7d5a97d | 861 | |
2af202be | 862 | static int show_remote_info_item(struct string_list_item *item, void *cb_data) |
7ecbbf87 JS |
863 | { |
864 | struct show_info *info = cb_data; | |
865 | struct ref_states *states = info->states; | |
866 | const char *name = item->string; | |
867 | ||
868 | if (states->queried) { | |
869 | const char *fmt = "%s"; | |
870 | const char *arg = ""; | |
871 | if (string_list_has_string(&states->new, name)) { | |
872 | fmt = " new (next fetch will store in remotes/%s)"; | |
873 | arg = states->remote->name; | |
874 | } else if (string_list_has_string(&states->tracked, name)) | |
875 | arg = " tracked"; | |
876 | else if (string_list_has_string(&states->stale, name)) | |
877 | arg = " stale (use 'git remote prune' to remove)"; | |
878 | else | |
879 | arg = " ???"; | |
880 | printf(" %-*s", info->width, name); | |
881 | printf(fmt, arg); | |
882 | printf("\n"); | |
883 | } else | |
884 | printf(" %s\n", name); | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
2af202be | 889 | static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) |
7ecbbf87 JS |
890 | { |
891 | struct show_info *show_info = cb_data; | |
892 | struct ref_states *states = show_info->states; | |
893 | struct branch_info *branch_info = branch_item->util; | |
894 | struct string_list_item *item; | |
895 | int n; | |
896 | ||
897 | if (!branch_info->merge.nr || !branch_info->remote_name || | |
898 | strcmp(states->remote->name, branch_info->remote_name)) | |
899 | return 0; | |
900 | if ((n = strlen(branch_item->string)) > show_info->width) | |
901 | show_info->width = n; | |
902 | if (branch_info->rebase) | |
903 | show_info->any_rebase = 1; | |
904 | ||
905 | item = string_list_insert(branch_item->string, show_info->list); | |
906 | item->util = branch_info; | |
907 | ||
908 | return 0; | |
909 | } | |
910 | ||
2af202be | 911 | static int show_local_info_item(struct string_list_item *item, void *cb_data) |
7ecbbf87 JS |
912 | { |
913 | struct show_info *show_info = cb_data; | |
914 | struct branch_info *branch_info = item->util; | |
915 | struct string_list *merge = &branch_info->merge; | |
916 | const char *also; | |
917 | int i; | |
918 | ||
919 | if (branch_info->rebase && branch_info->merge.nr > 1) { | |
920 | error("invalid branch.%s.merge; cannot rebase onto > 1 branch", | |
921 | item->string); | |
922 | return 0; | |
923 | } | |
924 | ||
925 | printf(" %-*s ", show_info->width, item->string); | |
926 | if (branch_info->rebase) { | |
927 | printf("rebases onto remote %s\n", merge->items[0].string); | |
928 | return 0; | |
929 | } else if (show_info->any_rebase) { | |
930 | printf(" merges with remote %s\n", merge->items[0].string); | |
931 | also = " and with remote"; | |
932 | } else { | |
933 | printf("merges with remote %s\n", merge->items[0].string); | |
934 | also = " and with remote"; | |
935 | } | |
936 | for (i = 1; i < merge->nr; i++) | |
937 | printf(" %-*s %s %s\n", show_info->width, "", also, | |
938 | merge->items[i].string); | |
939 | ||
940 | return 0; | |
941 | } | |
e7d5a97d | 942 | |
2af202be | 943 | static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data) |
e5dcbfd9 JS |
944 | { |
945 | struct show_info *show_info = cb_data; | |
946 | struct push_info *push_info = push_item->util; | |
947 | struct string_list_item *item; | |
948 | int n; | |
949 | if ((n = strlen(push_item->string)) > show_info->width) | |
950 | show_info->width = n; | |
951 | if ((n = strlen(push_info->dest)) > show_info->width2) | |
952 | show_info->width2 = n; | |
953 | item = string_list_append(push_item->string, show_info->list); | |
954 | item->util = push_item->util; | |
955 | return 0; | |
956 | } | |
957 | ||
48ef5636 JK |
958 | /* |
959 | * Sorting comparison for a string list that has push_info | |
960 | * structs in its util field | |
961 | */ | |
962 | static int cmp_string_with_push(const void *va, const void *vb) | |
963 | { | |
964 | const struct string_list_item *a = va; | |
965 | const struct string_list_item *b = vb; | |
966 | const struct push_info *a_push = a->util; | |
967 | const struct push_info *b_push = b->util; | |
968 | int cmp = strcmp(a->string, b->string); | |
969 | return cmp ? cmp : strcmp(a_push->dest, b_push->dest); | |
970 | } | |
971 | ||
2af202be | 972 | static int show_push_info_item(struct string_list_item *item, void *cb_data) |
e5dcbfd9 JS |
973 | { |
974 | struct show_info *show_info = cb_data; | |
975 | struct push_info *push_info = item->util; | |
976 | char *src = item->string, *status = NULL; | |
977 | ||
978 | switch (push_info->status) { | |
979 | case PUSH_STATUS_CREATE: | |
980 | status = "create"; | |
981 | break; | |
982 | case PUSH_STATUS_DELETE: | |
983 | status = "delete"; | |
984 | src = "(none)"; | |
985 | break; | |
986 | case PUSH_STATUS_UPTODATE: | |
987 | status = "up to date"; | |
988 | break; | |
989 | case PUSH_STATUS_FASTFORWARD: | |
990 | status = "fast forwardable"; | |
991 | break; | |
992 | case PUSH_STATUS_OUTOFDATE: | |
993 | status = "local out of date"; | |
994 | break; | |
995 | case PUSH_STATUS_NOTQUERIED: | |
996 | break; | |
997 | } | |
998 | if (status) | |
999 | printf(" %-*s %s to %-*s (%s)\n", show_info->width, src, | |
1000 | push_info->forced ? "forces" : "pushes", | |
1001 | show_info->width2, push_info->dest, status); | |
1002 | else | |
1003 | printf(" %-*s %s to %s\n", show_info->width, src, | |
1004 | push_info->forced ? "forces" : "pushes", | |
1005 | push_info->dest); | |
e7d5a97d OM |
1006 | return 0; |
1007 | } | |
1008 | ||
67a7e2d0 | 1009 | static int show(int argc, const char **argv) |
211c8968 | 1010 | { |
e61e0cc6 | 1011 | int no_query = 0, result = 0, query_flag = 0; |
211c8968 | 1012 | struct option options[] = { |
0ecfcb3b | 1013 | OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"), |
211c8968 JS |
1014 | OPT_END() |
1015 | }; | |
1016 | struct ref_states states; | |
7ecbbf87 JS |
1017 | struct string_list info_list = { NULL, 0, 0, 0 }; |
1018 | struct show_info info; | |
211c8968 | 1019 | |
4504107d | 1020 | argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage, |
37782920 | 1021 | 0); |
211c8968 | 1022 | |
67a7e2d0 OM |
1023 | if (argc < 1) |
1024 | return show_all(); | |
211c8968 | 1025 | |
e61e0cc6 | 1026 | if (!no_query) |
e5dcbfd9 | 1027 | query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); |
e61e0cc6 | 1028 | |
211c8968 | 1029 | memset(&states, 0, sizeof(states)); |
7ecbbf87 JS |
1030 | memset(&info, 0, sizeof(info)); |
1031 | info.states = &states; | |
1032 | info.list = &info_list; | |
211c8968 | 1033 | for (; argc; argc--, argv++) { |
0ecfcb3b | 1034 | int i; |
857f8c30 MG |
1035 | const char **url; |
1036 | int url_nr; | |
211c8968 | 1037 | |
e61e0cc6 | 1038 | get_remote_ref_states(*argv, &states, query_flag); |
211c8968 | 1039 | |
956d27a8 | 1040 | printf("* remote %s\n", *argv); |
857f8c30 MG |
1041 | printf(" Fetch URL: %s\n", states.remote->url_nr > 0 ? |
1042 | states.remote->url[0] : "(no URL)"); | |
1043 | if (states.remote->pushurl_nr) { | |
1044 | url = states.remote->pushurl; | |
1045 | url_nr = states.remote->pushurl_nr; | |
1046 | } else { | |
1047 | url = states.remote->url; | |
1048 | url_nr = states.remote->url_nr; | |
1049 | } | |
1050 | for (i=0; i < url_nr; i++) | |
1051 | printf(" Push URL: %s\n", url[i]); | |
1052 | if (!i) | |
1053 | printf(" Push URL: %s\n", "(no URL)"); | |
e61e0cc6 JS |
1054 | if (no_query) |
1055 | printf(" HEAD branch: (not queried)\n"); | |
1056 | else if (!states.heads.nr) | |
1057 | printf(" HEAD branch: (unknown)\n"); | |
1058 | else if (states.heads.nr == 1) | |
1059 | printf(" HEAD branch: %s\n", states.heads.items[0].string); | |
1060 | else { | |
1061 | printf(" HEAD branch (remote HEAD is ambiguous," | |
1062 | " may be one of the following):\n"); | |
1063 | for (i = 0; i < states.heads.nr; i++) | |
1064 | printf(" %s\n", states.heads.items[i].string); | |
211c8968 JS |
1065 | } |
1066 | ||
7ecbbf87 JS |
1067 | /* remote branch info */ |
1068 | info.width = 0; | |
1069 | for_each_string_list(add_remote_to_show_info, &states.new, &info); | |
1070 | for_each_string_list(add_remote_to_show_info, &states.tracked, &info); | |
1071 | for_each_string_list(add_remote_to_show_info, &states.stale, &info); | |
1072 | if (info.list->nr) | |
1073 | printf(" Remote branch%s:%s\n", | |
1074 | info.list->nr > 1 ? "es" : "", | |
1075 | no_query ? " (status not queried)" : ""); | |
1076 | for_each_string_list(show_remote_info_item, info.list, &info); | |
1077 | string_list_clear(info.list, 0); | |
1078 | ||
1079 | /* git pull info */ | |
1080 | info.width = 0; | |
1081 | info.any_rebase = 0; | |
1082 | for_each_string_list(add_local_to_show_info, &branch_list, &info); | |
1083 | if (info.list->nr) | |
1084 | printf(" Local branch%s configured for 'git pull':\n", | |
1085 | info.list->nr > 1 ? "es" : ""); | |
1086 | for_each_string_list(show_local_info_item, info.list, &info); | |
1087 | string_list_clear(info.list, 0); | |
1088 | ||
1089 | /* git push info */ | |
e5dcbfd9 JS |
1090 | if (states.remote->mirror) |
1091 | printf(" Local refs will be mirrored by 'git push'\n"); | |
1092 | ||
1093 | info.width = info.width2 = 0; | |
1094 | for_each_string_list(add_push_to_show_info, &states.push, &info); | |
48ef5636 JK |
1095 | qsort(info.list->items, info.list->nr, |
1096 | sizeof(*info.list->items), cmp_string_with_push); | |
e5dcbfd9 JS |
1097 | if (info.list->nr) |
1098 | printf(" Local ref%s configured for 'git push'%s:\n", | |
1099 | info.list->nr > 1 ? "s" : "", | |
1100 | no_query ? " (status not queried)" : ""); | |
1101 | for_each_string_list(show_push_info_item, info.list, &info); | |
1102 | string_list_clear(info.list, 0); | |
67a7e2d0 | 1103 | |
88733235 | 1104 | free_remote_ref_states(&states); |
67a7e2d0 | 1105 | } |
211c8968 | 1106 | |
67a7e2d0 OM |
1107 | return result; |
1108 | } | |
67a7e2d0 | 1109 | |
bc14fac8 JS |
1110 | static int set_head(int argc, const char **argv) |
1111 | { | |
1112 | int i, opt_a = 0, opt_d = 0, result = 0; | |
1113 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; | |
1114 | char *head_name = NULL; | |
1115 | ||
1116 | struct option options[] = { | |
bc14fac8 JS |
1117 | OPT_BOOLEAN('a', "auto", &opt_a, |
1118 | "set refs/remotes/<name>/HEAD according to remote"), | |
1119 | OPT_BOOLEAN('d', "delete", &opt_d, | |
1120 | "delete refs/remotes/<name>/HEAD"), | |
1121 | OPT_END() | |
1122 | }; | |
4504107d | 1123 | argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage, |
37782920 | 1124 | 0); |
bc14fac8 JS |
1125 | if (argc) |
1126 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]); | |
1127 | ||
1128 | if (!opt_a && !opt_d && argc == 2) { | |
1129 | head_name = xstrdup(argv[1]); | |
1130 | } else if (opt_a && !opt_d && argc == 1) { | |
1131 | struct ref_states states; | |
1132 | memset(&states, 0, sizeof(states)); | |
1133 | get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); | |
1134 | if (!states.heads.nr) | |
1135 | result |= error("Cannot determine remote HEAD"); | |
1136 | else if (states.heads.nr > 1) { | |
1137 | result |= error("Multiple remote HEAD branches. " | |
1138 | "Please choose one explicitly with:"); | |
1139 | for (i = 0; i < states.heads.nr; i++) | |
1140 | fprintf(stderr, " git remote set-head %s %s\n", | |
1141 | argv[0], states.heads.items[i].string); | |
1142 | } else | |
1143 | head_name = xstrdup(states.heads.items[0].string); | |
1144 | free_remote_ref_states(&states); | |
1145 | } else if (opt_d && !opt_a && argc == 1) { | |
1146 | if (delete_ref(buf.buf, NULL, REF_NODEREF)) | |
1147 | result |= error("Could not delete %s", buf.buf); | |
1148 | } else | |
4504107d | 1149 | usage_with_options(builtin_remote_sethead_usage, options); |
bc14fac8 JS |
1150 | |
1151 | if (head_name) { | |
1152 | unsigned char sha1[20]; | |
1153 | strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name); | |
1154 | /* make sure it's valid */ | |
1155 | if (!resolve_ref(buf2.buf, sha1, 1, NULL)) | |
1156 | result |= error("Not a valid ref: %s", buf2.buf); | |
1157 | else if (create_symref(buf.buf, buf2.buf, "remote set-head")) | |
1158 | result |= error("Could not setup %s", buf.buf); | |
1159 | if (opt_a) | |
1160 | printf("%s/HEAD set to %s\n", argv[0], head_name); | |
1161 | free(head_name); | |
67a7e2d0 OM |
1162 | } |
1163 | ||
bc14fac8 JS |
1164 | strbuf_release(&buf); |
1165 | strbuf_release(&buf2); | |
67a7e2d0 OM |
1166 | return result; |
1167 | } | |
1168 | ||
1169 | static int prune(int argc, const char **argv) | |
1170 | { | |
8d767927 | 1171 | int dry_run = 0, result = 0; |
67a7e2d0 | 1172 | struct option options[] = { |
8d767927 | 1173 | OPT__DRY_RUN(&dry_run), |
67a7e2d0 OM |
1174 | OPT_END() |
1175 | }; | |
67a7e2d0 | 1176 | |
4504107d | 1177 | argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage, |
37782920 | 1178 | 0); |
67a7e2d0 OM |
1179 | |
1180 | if (argc < 1) | |
4504107d | 1181 | usage_with_options(builtin_remote_prune_usage, options); |
67a7e2d0 | 1182 | |
b92c5f22 FAG |
1183 | for (; argc; argc--, argv++) |
1184 | result |= prune_remote(*argv, dry_run); | |
f8948e2f | 1185 | |
b92c5f22 FAG |
1186 | return result; |
1187 | } | |
8d767927 | 1188 | |
b92c5f22 FAG |
1189 | static int prune_remote(const char *remote, int dry_run) |
1190 | { | |
1191 | int result = 0, i; | |
1192 | struct ref_states states; | |
1193 | const char *dangling_msg = dry_run | |
1194 | ? " %s will become dangling!\n" | |
1195 | : " %s has become dangling!\n"; | |
67a7e2d0 | 1196 | |
b92c5f22 FAG |
1197 | memset(&states, 0, sizeof(states)); |
1198 | get_remote_ref_states(remote, &states, GET_REF_STATES); | |
1199 | ||
1200 | if (states.stale.nr) { | |
1201 | printf("Pruning %s\n", remote); | |
1202 | printf("URL: %s\n", | |
1203 | states.remote->url_nr | |
1204 | ? states.remote->url[0] | |
1205 | : "(no URL)"); | |
1206 | } | |
8d767927 | 1207 | |
b92c5f22 FAG |
1208 | for (i = 0; i < states.stale.nr; i++) { |
1209 | const char *refname = states.stale.items[i].util; | |
8d767927 | 1210 | |
b92c5f22 FAG |
1211 | if (!dry_run) |
1212 | result |= delete_ref(refname, NULL, 0); | |
67a7e2d0 | 1213 | |
b92c5f22 FAG |
1214 | printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned", |
1215 | abbrev_ref(refname, "refs/remotes/")); | |
1216 | warn_dangling_symref(dangling_msg, refname); | |
211c8968 JS |
1217 | } |
1218 | ||
b92c5f22 | 1219 | free_remote_ref_states(&states); |
211c8968 JS |
1220 | return result; |
1221 | } | |
1222 | ||
84521ed6 | 1223 | static int get_one_remote_for_update(struct remote *remote, void *priv) |
211c8968 | 1224 | { |
c455c87c | 1225 | struct string_list *list = priv; |
211c8968 | 1226 | if (!remote->skip_default_update) |
83b76736 | 1227 | string_list_append(remote->name, list); |
84521ed6 JS |
1228 | return 0; |
1229 | } | |
1230 | ||
2af202be | 1231 | static struct remote_group { |
84521ed6 | 1232 | const char *name; |
c455c87c | 1233 | struct string_list *list; |
84521ed6 JS |
1234 | } remote_group; |
1235 | ||
bed5d421 | 1236 | static int get_remote_group(const char *key, const char *value, void *num_hits) |
84521ed6 JS |
1237 | { |
1238 | if (!prefixcmp(key, "remotes.") && | |
1239 | !strcmp(key + 8, remote_group.name)) { | |
1240 | /* split list by white space */ | |
1241 | int space = strcspn(value, " \t\n"); | |
1242 | while (*value) { | |
bed5d421 | 1243 | if (space > 1) { |
c455c87c | 1244 | string_list_append(xstrndup(value, space), |
84521ed6 | 1245 | remote_group.list); |
bed5d421 FAG |
1246 | ++*((int *)num_hits); |
1247 | } | |
84521ed6 JS |
1248 | value += space + (value[space] != '\0'); |
1249 | space = strcspn(value, " \t\n"); | |
1250 | } | |
1251 | } | |
1252 | ||
211c8968 JS |
1253 | return 0; |
1254 | } | |
1255 | ||
1256 | static int update(int argc, const char **argv) | |
1257 | { | |
efa54803 | 1258 | int i, result = 0, prune = 0; |
c455c87c | 1259 | struct string_list list = { NULL, 0, 0, 0 }; |
84521ed6 | 1260 | static const char *default_argv[] = { NULL, "default", NULL }; |
efa54803 | 1261 | struct option options[] = { |
efa54803 | 1262 | OPT_BOOLEAN('p', "prune", &prune, |
7c0282bf | 1263 | "prune remotes after fetching"), |
efa54803 FAG |
1264 | OPT_END() |
1265 | }; | |
211c8968 | 1266 | |
4504107d | 1267 | argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage, |
efa54803 | 1268 | PARSE_OPT_KEEP_ARGV0); |
84521ed6 JS |
1269 | if (argc < 2) { |
1270 | argc = 2; | |
1271 | argv = default_argv; | |
1272 | } | |
211c8968 | 1273 | |
84521ed6 JS |
1274 | remote_group.list = &list; |
1275 | for (i = 1; i < argc; i++) { | |
bed5d421 | 1276 | int groups_found = 0; |
84521ed6 | 1277 | remote_group.name = argv[i]; |
bed5d421 | 1278 | result = git_config(get_remote_group, &groups_found); |
b344e161 FAG |
1279 | if (!groups_found && (i != 1 || strcmp(argv[1], "default"))) { |
1280 | struct remote *remote; | |
1281 | if (!remote_is_configured(argv[i])) | |
1282 | die("No such remote or remote group: %s", | |
1283 | argv[i]); | |
1284 | remote = remote_get(argv[i]); | |
1285 | string_list_append(remote->name, remote_group.list); | |
1286 | } | |
84521ed6 JS |
1287 | } |
1288 | ||
1289 | if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default")) | |
1290 | result = for_each_remote(get_one_remote_for_update, &list); | |
1291 | ||
efa54803 FAG |
1292 | for (i = 0; i < list.nr; i++) { |
1293 | int err = fetch_remote(list.items[i].string); | |
1294 | result |= err; | |
1295 | if (!err && prune) | |
1296 | result |= prune_remote(list.items[i].string, 0); | |
1297 | } | |
84521ed6 JS |
1298 | |
1299 | /* all names were strdup()ed or strndup()ed */ | |
c455c87c JS |
1300 | list.strdup_strings = 1; |
1301 | string_list_clear(&list, 0); | |
84521ed6 JS |
1302 | |
1303 | return result; | |
211c8968 JS |
1304 | } |
1305 | ||
1306 | static int get_one_entry(struct remote *remote, void *priv) | |
1307 | { | |
c455c87c | 1308 | struct string_list *list = priv; |
3f721d1d | 1309 | struct strbuf url_buf = STRBUF_INIT; |
4a4b4cda MG |
1310 | const char **url; |
1311 | int i, url_nr; | |
211c8968 | 1312 | |
7d20e218 | 1313 | if (remote->url_nr > 0) { |
3f721d1d BW |
1314 | strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]); |
1315 | string_list_append(remote->name, list)->util = | |
1316 | strbuf_detach(&url_buf, NULL); | |
7d20e218 MG |
1317 | } else |
1318 | string_list_append(remote->name, list)->util = NULL; | |
4a4b4cda MG |
1319 | if (remote->pushurl_nr) { |
1320 | url = remote->pushurl; | |
1321 | url_nr = remote->pushurl_nr; | |
1322 | } else { | |
1323 | url = remote->url; | |
1324 | url_nr = remote->url_nr; | |
1325 | } | |
1326 | for (i = 0; i < url_nr; i++) | |
1327 | { | |
3f721d1d BW |
1328 | strbuf_addf(&url_buf, "%s (push)", url[i]); |
1329 | string_list_append(remote->name, list)->util = | |
1330 | strbuf_detach(&url_buf, NULL); | |
4a4b4cda | 1331 | } |
211c8968 JS |
1332 | |
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | static int show_all(void) | |
1337 | { | |
c455c87c | 1338 | struct string_list list = { NULL, 0, 0 }; |
4a4b4cda MG |
1339 | int result; |
1340 | ||
1341 | list.strdup_strings = 1; | |
1342 | result = for_each_remote(get_one_entry, &list); | |
211c8968 JS |
1343 | |
1344 | if (!result) { | |
1345 | int i; | |
1346 | ||
c455c87c | 1347 | sort_string_list(&list); |
211c8968 | 1348 | for (i = 0; i < list.nr; i++) { |
c455c87c | 1349 | struct string_list_item *item = list.items + i; |
7d20e218 MG |
1350 | if (verbose) |
1351 | printf("%s\t%s\n", item->string, | |
1352 | item->util ? (const char *)item->util : ""); | |
1353 | else { | |
1354 | if (i && !strcmp((item - 1)->string, item->string)) | |
1355 | continue; | |
1356 | printf("%s\n", item->string); | |
1357 | } | |
211c8968 JS |
1358 | } |
1359 | } | |
4a4b4cda | 1360 | string_list_clear(&list, 1); |
211c8968 JS |
1361 | return result; |
1362 | } | |
1363 | ||
1364 | int cmd_remote(int argc, const char **argv, const char *prefix) | |
1365 | { | |
1366 | struct option options[] = { | |
4504107d | 1367 | OPT_BOOLEAN('v', "verbose", &verbose, "be verbose; must be placed before a subcommand"), |
211c8968 JS |
1368 | OPT_END() |
1369 | }; | |
1370 | int result; | |
1371 | ||
37782920 | 1372 | argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, |
211c8968 JS |
1373 | PARSE_OPT_STOP_AT_NON_OPTION); |
1374 | ||
1375 | if (argc < 1) | |
1376 | result = show_all(); | |
1377 | else if (!strcmp(argv[0], "add")) | |
1378 | result = add(argc, argv); | |
bf98421a MV |
1379 | else if (!strcmp(argv[0], "rename")) |
1380 | result = mv(argc, argv); | |
211c8968 JS |
1381 | else if (!strcmp(argv[0], "rm")) |
1382 | result = rm(argc, argv); | |
bc14fac8 JS |
1383 | else if (!strcmp(argv[0], "set-head")) |
1384 | result = set_head(argc, argv); | |
211c8968 | 1385 | else if (!strcmp(argv[0], "show")) |
67a7e2d0 | 1386 | result = show(argc, argv); |
211c8968 | 1387 | else if (!strcmp(argv[0], "prune")) |
67a7e2d0 | 1388 | result = prune(argc, argv); |
211c8968 JS |
1389 | else if (!strcmp(argv[0], "update")) |
1390 | result = update(argc, argv); | |
1391 | else { | |
1392 | error("Unknown subcommand: %s", argv[0]); | |
1393 | usage_with_options(builtin_remote_usage, options); | |
1394 | } | |
1395 | ||
1396 | return result ? 1 : 0; | |
1397 | } |