]>
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>", |
357af14f CR |
15 | "git remote show [-n] <name>", |
16 | "git remote prune [-n | --dry-run] <name>", | |
dbbd56f1 | 17 | "git remote [-v | --verbose] update [group]", |
211c8968 JS |
18 | NULL |
19 | }; | |
20 | ||
21 | static int verbose; | |
22 | ||
c4112bb6 JK |
23 | static int show_all(void); |
24 | ||
211c8968 JS |
25 | static inline int postfixcmp(const char *string, const char *postfix) |
26 | { | |
27 | int len1 = strlen(string), len2 = strlen(postfix); | |
28 | if (len1 < len2) | |
29 | return 1; | |
30 | return strcmp(string + len1 - len2, postfix); | |
31 | } | |
32 | ||
211c8968 JS |
33 | static int opt_parse_track(const struct option *opt, const char *arg, int not) |
34 | { | |
c455c87c | 35 | struct string_list *list = opt->value; |
211c8968 | 36 | if (not) |
c455c87c | 37 | string_list_clear(list, 0); |
211c8968 | 38 | else |
c455c87c | 39 | string_list_append(arg, list); |
211c8968 JS |
40 | return 0; |
41 | } | |
42 | ||
43 | static int fetch_remote(const char *name) | |
44 | { | |
dbbd56f1 CR |
45 | const char *argv[] = { "fetch", name, NULL, NULL }; |
46 | if (verbose) { | |
47 | argv[1] = "-v"; | |
48 | argv[2] = name; | |
49 | } | |
3000658f | 50 | printf("Updating %s\n", name); |
211c8968 JS |
51 | if (run_command_v_opt(argv, RUN_GIT_CMD)) |
52 | return error("Could not fetch %s", name); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | static int add(int argc, const char **argv) | |
57 | { | |
58 | int fetch = 0, mirror = 0; | |
c455c87c | 59 | struct string_list track = { NULL, 0, 0 }; |
211c8968 JS |
60 | const char *master = NULL; |
61 | struct remote *remote; | |
f285a2d7 | 62 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
211c8968 JS |
63 | const char *name, *url; |
64 | int i; | |
65 | ||
66 | struct option options[] = { | |
67 | OPT_GROUP("add specific options"), | |
68 | OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"), | |
69 | OPT_CALLBACK('t', "track", &track, "branch", | |
70 | "branch(es) to track", opt_parse_track), | |
71 | OPT_STRING('m', "master", &master, "branch", "master branch"), | |
72 | OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"), | |
73 | OPT_END() | |
74 | }; | |
75 | ||
76 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
77 | ||
78 | if (argc < 2) | |
79 | usage_with_options(builtin_remote_usage, options); | |
80 | ||
81 | name = argv[0]; | |
82 | url = argv[1]; | |
83 | ||
84 | remote = remote_get(name); | |
85 | if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) || | |
86 | remote->fetch_refspec_nr)) | |
87 | die("remote %s already exists.", name); | |
88 | ||
24b6177e JF |
89 | strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name); |
90 | if (!valid_fetch_refspec(buf2.buf)) | |
91 | die("'%s' is not a valid remote name", name); | |
92 | ||
211c8968 JS |
93 | strbuf_addf(&buf, "remote.%s.url", name); |
94 | if (git_config_set(buf.buf, url)) | |
95 | return 1; | |
96 | ||
24b6177e JF |
97 | strbuf_reset(&buf); |
98 | strbuf_addf(&buf, "remote.%s.fetch", name); | |
99 | ||
211c8968 | 100 | if (track.nr == 0) |
c455c87c | 101 | string_list_append("*", &track); |
211c8968 | 102 | for (i = 0; i < track.nr; i++) { |
c455c87c | 103 | struct string_list_item *item = track.items + i; |
211c8968 | 104 | |
211c8968 | 105 | strbuf_reset(&buf2); |
1ce89cc4 | 106 | strbuf_addch(&buf2, '+'); |
211c8968 JS |
107 | if (mirror) |
108 | strbuf_addf(&buf2, "refs/%s:refs/%s", | |
c455c87c | 109 | item->string, item->string); |
211c8968 JS |
110 | else |
111 | strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s", | |
c455c87c | 112 | item->string, name, item->string); |
211c8968 JS |
113 | if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) |
114 | return 1; | |
115 | } | |
116 | ||
84bb2dfd PB |
117 | if (mirror) { |
118 | strbuf_reset(&buf); | |
119 | strbuf_addf(&buf, "remote.%s.mirror", name); | |
bc699afc | 120 | if (git_config_set(buf.buf, "true")) |
84bb2dfd PB |
121 | return 1; |
122 | } | |
123 | ||
211c8968 JS |
124 | if (fetch && fetch_remote(name)) |
125 | return 1; | |
126 | ||
127 | if (master) { | |
128 | strbuf_reset(&buf); | |
129 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); | |
130 | ||
131 | strbuf_reset(&buf2); | |
132 | strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); | |
133 | ||
134 | if (create_symref(buf.buf, buf2.buf, "remote add")) | |
135 | return error("Could not setup master '%s'", master); | |
136 | } | |
137 | ||
138 | strbuf_release(&buf); | |
139 | strbuf_release(&buf2); | |
c455c87c | 140 | string_list_clear(&track, 0); |
211c8968 JS |
141 | |
142 | return 0; | |
143 | } | |
144 | ||
145 | struct branch_info { | |
e0cc81e6 | 146 | char *remote_name; |
c455c87c | 147 | struct string_list merge; |
211c8968 JS |
148 | }; |
149 | ||
c455c87c | 150 | static struct string_list branch_list; |
211c8968 | 151 | |
72972eb3 JH |
152 | static const char *abbrev_ref(const char *name, const char *prefix) |
153 | { | |
154 | const char *abbrev = skip_prefix(name, prefix); | |
155 | if (abbrev) | |
156 | return abbrev; | |
157 | return name; | |
158 | } | |
159 | #define abbrev_branch(name) abbrev_ref((name), "refs/heads/") | |
160 | ||
ef90d6d4 | 161 | static int config_read_branches(const char *key, const char *value, void *cb) |
211c8968 JS |
162 | { |
163 | if (!prefixcmp(key, "branch.")) { | |
164 | char *name; | |
c455c87c | 165 | struct string_list_item *item; |
211c8968 JS |
166 | struct branch_info *info; |
167 | enum { REMOTE, MERGE } type; | |
168 | ||
169 | key += 7; | |
170 | if (!postfixcmp(key, ".remote")) { | |
171 | name = xstrndup(key, strlen(key) - 7); | |
172 | type = REMOTE; | |
173 | } else if (!postfixcmp(key, ".merge")) { | |
174 | name = xstrndup(key, strlen(key) - 6); | |
175 | type = MERGE; | |
176 | } else | |
177 | return 0; | |
178 | ||
c455c87c | 179 | item = string_list_insert(name, &branch_list); |
211c8968 JS |
180 | |
181 | if (!item->util) | |
182 | item->util = xcalloc(sizeof(struct branch_info), 1); | |
183 | info = item->util; | |
184 | if (type == REMOTE) { | |
e0cc81e6 | 185 | if (info->remote_name) |
211c8968 | 186 | warning("more than one branch.%s", key); |
e0cc81e6 | 187 | info->remote_name = xstrdup(value); |
211c8968 JS |
188 | } else { |
189 | char *space = strchr(value, ' '); | |
72972eb3 | 190 | value = abbrev_branch(value); |
211c8968 JS |
191 | while (space) { |
192 | char *merge; | |
193 | merge = xstrndup(value, space - value); | |
c455c87c | 194 | string_list_append(merge, &info->merge); |
72972eb3 | 195 | value = abbrev_branch(space + 1); |
211c8968 JS |
196 | space = strchr(value, ' '); |
197 | } | |
c455c87c | 198 | string_list_append(xstrdup(value), &info->merge); |
211c8968 JS |
199 | } |
200 | } | |
201 | return 0; | |
202 | } | |
203 | ||
204 | static void read_branches(void) | |
205 | { | |
206 | if (branch_list.nr) | |
207 | return; | |
ef90d6d4 | 208 | git_config(config_read_branches, NULL); |
211c8968 JS |
209 | } |
210 | ||
211 | struct ref_states { | |
212 | struct remote *remote; | |
c455c87c | 213 | struct string_list new, stale, tracked; |
211c8968 JS |
214 | }; |
215 | ||
216 | static int handle_one_branch(const char *refname, | |
217 | const unsigned char *sha1, int flags, void *cb_data) | |
218 | { | |
219 | struct ref_states *states = cb_data; | |
220 | struct refspec refspec; | |
221 | ||
222 | memset(&refspec, 0, sizeof(refspec)); | |
223 | refspec.dst = (char *)refname; | |
224 | if (!remote_find_tracking(states->remote, &refspec)) { | |
c455c87c | 225 | struct string_list_item *item; |
72972eb3 | 226 | const char *name = abbrev_branch(refspec.src); |
740fdd27 JS |
227 | /* symbolic refs pointing nowhere were handled already */ |
228 | if ((flags & REF_ISSYMREF) || | |
c455c87c | 229 | unsorted_string_list_has_string(&states->tracked, |
740fdd27 | 230 | name) || |
c455c87c | 231 | unsorted_string_list_has_string(&states->new, |
211c8968 JS |
232 | name)) |
233 | return 0; | |
c455c87c | 234 | item = string_list_append(name, &states->stale); |
211c8968 JS |
235 | item->util = xstrdup(refname); |
236 | } | |
237 | return 0; | |
238 | } | |
239 | ||
e0cc81e6 | 240 | static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) |
211c8968 JS |
241 | { |
242 | struct ref *fetch_map = NULL, **tail = &fetch_map; | |
e0cc81e6 | 243 | struct ref *ref; |
211c8968 JS |
244 | int i; |
245 | ||
246 | for (i = 0; i < states->remote->fetch_refspec_nr; i++) | |
e0cc81e6 | 247 | if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1)) |
211c8968 JS |
248 | die("Could not get fetch map for refspec %s", |
249 | states->remote->fetch_refspec[i]); | |
250 | ||
c455c87c | 251 | states->new.strdup_strings = states->tracked.strdup_strings = 1; |
211c8968 | 252 | for (ref = fetch_map; ref; ref = ref->next) { |
211c8968 | 253 | unsigned char sha1[20]; |
211c8968 | 254 | if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1)) |
7b9a5e27 JS |
255 | string_list_append(abbrev_branch(ref->name), &states->new); |
256 | else | |
257 | string_list_append(abbrev_branch(ref->name), &states->tracked); | |
211c8968 JS |
258 | } |
259 | free_refs(fetch_map); | |
260 | ||
211c8968 | 261 | for_each_ref(handle_one_branch, states); |
c455c87c | 262 | sort_string_list(&states->stale); |
211c8968 JS |
263 | |
264 | return 0; | |
265 | } | |
266 | ||
7ad2458f SP |
267 | struct known_remote { |
268 | struct known_remote *next; | |
269 | struct remote *remote; | |
270 | }; | |
271 | ||
272 | struct known_remotes { | |
273 | struct remote *to_delete; | |
274 | struct known_remote *list; | |
275 | }; | |
276 | ||
277 | static int add_known_remote(struct remote *remote, void *cb_data) | |
278 | { | |
279 | struct known_remotes *all = cb_data; | |
280 | struct known_remote *r; | |
281 | ||
282 | if (!strcmp(all->to_delete->name, remote->name)) | |
283 | return 0; | |
284 | ||
285 | r = xmalloc(sizeof(*r)); | |
286 | r->remote = remote; | |
287 | r->next = all->list; | |
288 | all->list = r; | |
289 | return 0; | |
290 | } | |
291 | ||
211c8968 | 292 | struct branches_for_remote { |
7ad2458f | 293 | struct remote *remote; |
441adf0c | 294 | struct string_list *branches, *skipped; |
7ad2458f | 295 | struct known_remotes *keep; |
211c8968 JS |
296 | }; |
297 | ||
298 | static int add_branch_for_removal(const char *refname, | |
299 | const unsigned char *sha1, int flags, void *cb_data) | |
300 | { | |
301 | struct branches_for_remote *branches = cb_data; | |
7ad2458f | 302 | struct refspec refspec; |
c455c87c | 303 | struct string_list_item *item; |
7ad2458f | 304 | struct known_remote *kr; |
211c8968 | 305 | |
7ad2458f SP |
306 | memset(&refspec, 0, sizeof(refspec)); |
307 | refspec.dst = (char *)refname; | |
308 | if (remote_find_tracking(branches->remote, &refspec)) | |
309 | return 0; | |
310 | ||
311 | /* don't delete a branch if another remote also uses it */ | |
312 | for (kr = branches->keep->list; kr; kr = kr->next) { | |
313 | memset(&refspec, 0, sizeof(refspec)); | |
314 | refspec.dst = (char *)refname; | |
315 | if (!remote_find_tracking(kr->remote, &refspec)) | |
316 | return 0; | |
317 | } | |
3b9dcff5 | 318 | |
441adf0c JS |
319 | /* don't delete non-remote refs */ |
320 | if (prefixcmp(refname, "refs/remotes")) { | |
321 | /* advise user how to delete local branches */ | |
322 | if (!prefixcmp(refname, "refs/heads/")) | |
323 | string_list_append(abbrev_branch(refname), | |
324 | branches->skipped); | |
325 | /* silently skip over other non-remote refs */ | |
326 | return 0; | |
327 | } | |
328 | ||
7ad2458f SP |
329 | /* make sure that symrefs are deleted */ |
330 | if (flags & REF_ISSYMREF) | |
9db56f71 | 331 | return unlink(git_path("%s", refname)); |
3b9dcff5 | 332 | |
c455c87c | 333 | item = string_list_append(refname, branches->branches); |
7ad2458f SP |
334 | item->util = xmalloc(20); |
335 | hashcpy(item->util, sha1); | |
211c8968 JS |
336 | |
337 | return 0; | |
338 | } | |
339 | ||
bf98421a MV |
340 | struct rename_info { |
341 | const char *old; | |
342 | const char *new; | |
343 | struct string_list *remote_branches; | |
344 | }; | |
345 | ||
346 | static int read_remote_branches(const char *refname, | |
347 | const unsigned char *sha1, int flags, void *cb_data) | |
348 | { | |
349 | struct rename_info *rename = cb_data; | |
350 | struct strbuf buf = STRBUF_INIT; | |
351 | struct string_list_item *item; | |
352 | int flag; | |
353 | unsigned char orig_sha1[20]; | |
354 | const char *symref; | |
355 | ||
356 | strbuf_addf(&buf, "refs/remotes/%s", rename->old); | |
357 | if(!prefixcmp(refname, buf.buf)) { | |
358 | item = string_list_append(xstrdup(refname), rename->remote_branches); | |
359 | symref = resolve_ref(refname, orig_sha1, 1, &flag); | |
360 | if (flag & REF_ISSYMREF) | |
361 | item->util = xstrdup(symref); | |
362 | else | |
363 | item->util = NULL; | |
364 | } | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
1dd1239a MV |
369 | static int migrate_file(struct remote *remote) |
370 | { | |
371 | struct strbuf buf = STRBUF_INIT; | |
372 | int i; | |
373 | char *path = NULL; | |
374 | ||
375 | strbuf_addf(&buf, "remote.%s.url", remote->name); | |
376 | for (i = 0; i < remote->url_nr; i++) | |
377 | if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0)) | |
378 | return error("Could not append '%s' to '%s'", | |
379 | remote->url[i], buf.buf); | |
380 | strbuf_reset(&buf); | |
381 | strbuf_addf(&buf, "remote.%s.push", remote->name); | |
382 | for (i = 0; i < remote->push_refspec_nr; i++) | |
383 | if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0)) | |
384 | return error("Could not append '%s' to '%s'", | |
385 | remote->push_refspec[i], buf.buf); | |
386 | strbuf_reset(&buf); | |
387 | strbuf_addf(&buf, "remote.%s.fetch", remote->name); | |
388 | for (i = 0; i < remote->fetch_refspec_nr; i++) | |
389 | if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0)) | |
390 | return error("Could not append '%s' to '%s'", | |
391 | remote->fetch_refspec[i], buf.buf); | |
392 | if (remote->origin == REMOTE_REMOTES) | |
393 | path = git_path("remotes/%s", remote->name); | |
394 | else if (remote->origin == REMOTE_BRANCHES) | |
395 | path = git_path("branches/%s", remote->name); | |
396 | if (path && unlink(path)) | |
397 | warning("failed to remove '%s'", path); | |
398 | return 0; | |
399 | } | |
400 | ||
bf98421a MV |
401 | static int mv(int argc, const char **argv) |
402 | { | |
403 | struct option options[] = { | |
404 | OPT_END() | |
405 | }; | |
406 | struct remote *oldremote, *newremote; | |
407 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT; | |
408 | struct string_list remote_branches = { NULL, 0, 0, 0 }; | |
409 | struct rename_info rename; | |
410 | int i; | |
411 | ||
412 | if (argc != 3) | |
413 | usage_with_options(builtin_remote_usage, options); | |
414 | ||
415 | rename.old = argv[1]; | |
416 | rename.new = argv[2]; | |
417 | rename.remote_branches = &remote_branches; | |
418 | ||
419 | oldremote = remote_get(rename.old); | |
420 | if (!oldremote) | |
421 | die("No such remote: %s", rename.old); | |
422 | ||
1dd1239a MV |
423 | if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG) |
424 | return migrate_file(oldremote); | |
425 | ||
bf98421a MV |
426 | newremote = remote_get(rename.new); |
427 | if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr)) | |
428 | die("remote %s already exists.", rename.new); | |
429 | ||
430 | strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new); | |
431 | if (!valid_fetch_refspec(buf.buf)) | |
432 | die("'%s' is not a valid remote name", rename.new); | |
433 | ||
434 | strbuf_reset(&buf); | |
435 | strbuf_addf(&buf, "remote.%s", rename.old); | |
436 | strbuf_addf(&buf2, "remote.%s", rename.new); | |
437 | if (git_config_rename_section(buf.buf, buf2.buf) < 1) | |
438 | return error("Could not rename config section '%s' to '%s'", | |
439 | buf.buf, buf2.buf); | |
440 | ||
441 | strbuf_reset(&buf); | |
442 | strbuf_addf(&buf, "remote.%s.fetch", rename.new); | |
443 | if (git_config_set_multivar(buf.buf, NULL, NULL, 1)) | |
444 | return error("Could not remove config section '%s'", buf.buf); | |
445 | for (i = 0; i < oldremote->fetch_refspec_nr; i++) { | |
446 | char *ptr; | |
447 | ||
448 | strbuf_reset(&buf2); | |
449 | strbuf_addstr(&buf2, oldremote->fetch_refspec[i]); | |
450 | ptr = strstr(buf2.buf, rename.old); | |
451 | if (ptr) | |
452 | strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old), | |
453 | rename.new, strlen(rename.new)); | |
454 | if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) | |
455 | return error("Could not append '%s'", buf.buf); | |
456 | } | |
457 | ||
458 | read_branches(); | |
459 | for (i = 0; i < branch_list.nr; i++) { | |
460 | struct string_list_item *item = branch_list.items + i; | |
461 | struct branch_info *info = item->util; | |
e0cc81e6 | 462 | if (info->remote_name && !strcmp(info->remote_name, rename.old)) { |
bf98421a MV |
463 | strbuf_reset(&buf); |
464 | strbuf_addf(&buf, "branch.%s.remote", item->string); | |
465 | if (git_config_set(buf.buf, rename.new)) { | |
466 | return error("Could not set '%s'", buf.buf); | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | /* | |
472 | * First remove symrefs, then rename the rest, finally create | |
473 | * the new symrefs. | |
474 | */ | |
475 | for_each_ref(read_remote_branches, &rename); | |
476 | for (i = 0; i < remote_branches.nr; i++) { | |
477 | struct string_list_item *item = remote_branches.items + i; | |
478 | int flag = 0; | |
479 | unsigned char sha1[20]; | |
480 | const char *symref; | |
481 | ||
482 | symref = resolve_ref(item->string, sha1, 1, &flag); | |
483 | if (!(flag & REF_ISSYMREF)) | |
484 | continue; | |
485 | if (delete_ref(item->string, NULL, REF_NODEREF)) | |
486 | die("deleting '%s' failed", item->string); | |
487 | } | |
488 | for (i = 0; i < remote_branches.nr; i++) { | |
489 | struct string_list_item *item = remote_branches.items + i; | |
490 | ||
491 | if (item->util) | |
492 | continue; | |
493 | strbuf_reset(&buf); | |
494 | strbuf_addstr(&buf, item->string); | |
495 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), | |
496 | rename.new, strlen(rename.new)); | |
497 | strbuf_reset(&buf2); | |
498 | strbuf_addf(&buf2, "remote: renamed %s to %s", | |
499 | item->string, buf.buf); | |
500 | if (rename_ref(item->string, buf.buf, buf2.buf)) | |
501 | die("renaming '%s' failed", item->string); | |
502 | } | |
503 | for (i = 0; i < remote_branches.nr; i++) { | |
504 | struct string_list_item *item = remote_branches.items + i; | |
505 | ||
506 | if (!item->util) | |
507 | continue; | |
508 | strbuf_reset(&buf); | |
509 | strbuf_addstr(&buf, item->string); | |
510 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), | |
511 | rename.new, strlen(rename.new)); | |
512 | strbuf_reset(&buf2); | |
513 | strbuf_addstr(&buf2, item->util); | |
514 | strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old), | |
515 | rename.new, strlen(rename.new)); | |
516 | strbuf_reset(&buf3); | |
517 | strbuf_addf(&buf3, "remote: renamed %s to %s", | |
518 | item->string, buf.buf); | |
519 | if (create_symref(buf.buf, buf2.buf, buf3.buf)) | |
520 | die("creating '%s' failed", buf.buf); | |
521 | } | |
522 | return 0; | |
523 | } | |
524 | ||
c455c87c | 525 | static int remove_branches(struct string_list *branches) |
211c8968 JS |
526 | { |
527 | int i, result = 0; | |
528 | for (i = 0; i < branches->nr; i++) { | |
c455c87c JS |
529 | struct string_list_item *item = branches->items + i; |
530 | const char *refname = item->string; | |
211c8968 JS |
531 | unsigned char *sha1 = item->util; |
532 | ||
eca35a25 | 533 | if (delete_ref(refname, sha1, 0)) |
211c8968 JS |
534 | result |= error("Could not remove branch %s", refname); |
535 | } | |
536 | return result; | |
537 | } | |
538 | ||
539 | static int rm(int argc, const char **argv) | |
540 | { | |
541 | struct option options[] = { | |
542 | OPT_END() | |
543 | }; | |
544 | struct remote *remote; | |
f285a2d7 | 545 | struct strbuf buf = STRBUF_INIT; |
7ad2458f | 546 | struct known_remotes known_remotes = { NULL, NULL }; |
c455c87c | 547 | struct string_list branches = { NULL, 0, 0, 1 }; |
441adf0c JS |
548 | struct string_list skipped = { NULL, 0, 0, 1 }; |
549 | struct branches_for_remote cb_data = { | |
550 | NULL, &branches, &skipped, &known_remotes | |
551 | }; | |
e02f1762 | 552 | int i, result; |
211c8968 JS |
553 | |
554 | if (argc != 2) | |
555 | usage_with_options(builtin_remote_usage, options); | |
556 | ||
557 | remote = remote_get(argv[1]); | |
558 | if (!remote) | |
559 | die("No such remote: %s", argv[1]); | |
560 | ||
7ad2458f SP |
561 | known_remotes.to_delete = remote; |
562 | for_each_remote(add_known_remote, &known_remotes); | |
563 | ||
211c8968 JS |
564 | strbuf_addf(&buf, "remote.%s", remote->name); |
565 | if (git_config_rename_section(buf.buf, NULL) < 1) | |
566 | return error("Could not remove config section '%s'", buf.buf); | |
567 | ||
568 | read_branches(); | |
569 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 570 | struct string_list_item *item = branch_list.items + i; |
211c8968 | 571 | struct branch_info *info = item->util; |
e0cc81e6 | 572 | if (info->remote_name && !strcmp(info->remote_name, remote->name)) { |
211c8968 JS |
573 | const char *keys[] = { "remote", "merge", NULL }, **k; |
574 | for (k = keys; *k; k++) { | |
575 | strbuf_reset(&buf); | |
576 | strbuf_addf(&buf, "branch.%s.%s", | |
c455c87c | 577 | item->string, *k); |
211c8968 JS |
578 | if (git_config_set(buf.buf, NULL)) { |
579 | strbuf_release(&buf); | |
580 | return -1; | |
581 | } | |
582 | } | |
583 | } | |
584 | } | |
585 | ||
586 | /* | |
587 | * We cannot just pass a function to for_each_ref() which deletes | |
588 | * the branches one by one, since for_each_ref() relies on cached | |
589 | * refs, which are invalidated when deleting a branch. | |
590 | */ | |
7ad2458f | 591 | cb_data.remote = remote; |
e02f1762 | 592 | result = for_each_ref(add_branch_for_removal, &cb_data); |
211c8968 JS |
593 | strbuf_release(&buf); |
594 | ||
e02f1762 JS |
595 | if (!result) |
596 | result = remove_branches(&branches); | |
c455c87c | 597 | string_list_clear(&branches, 1); |
211c8968 | 598 | |
441adf0c JS |
599 | if (skipped.nr) { |
600 | fprintf(stderr, skipped.nr == 1 ? | |
601 | "Note: A non-remote branch was not removed; " | |
602 | "to delete it, use:\n" : | |
603 | "Note: Non-remote branches were not removed; " | |
604 | "to delete them, use:\n"); | |
605 | for (i = 0; i < skipped.nr; i++) | |
606 | fprintf(stderr, " git branch -d %s\n", | |
607 | skipped.items[i].string); | |
608 | } | |
609 | string_list_clear(&skipped, 0); | |
610 | ||
e02f1762 | 611 | return result; |
211c8968 JS |
612 | } |
613 | ||
79bbc7fb JS |
614 | static void show_list(const char *title, struct string_list *list, |
615 | const char *extra_arg) | |
211c8968 JS |
616 | { |
617 | int i; | |
618 | ||
619 | if (!list->nr) | |
620 | return; | |
621 | ||
79bbc7fb | 622 | printf(title, list->nr > 1 ? "es" : "", extra_arg); |
211c8968 | 623 | printf("\n"); |
20244ea2 JS |
624 | for (i = 0; i < list->nr; i++) |
625 | printf(" %s\n", list->items[i].string); | |
211c8968 JS |
626 | } |
627 | ||
88733235 JS |
628 | static void free_remote_ref_states(struct ref_states *states) |
629 | { | |
630 | string_list_clear(&states->new, 0); | |
631 | string_list_clear(&states->stale, 0); | |
632 | string_list_clear(&states->tracked, 0); | |
633 | } | |
634 | ||
67a7e2d0 OM |
635 | static int get_remote_ref_states(const char *name, |
636 | struct ref_states *states, | |
637 | int query) | |
638 | { | |
639 | struct transport *transport; | |
e0cc81e6 | 640 | const struct ref *remote_refs; |
67a7e2d0 OM |
641 | |
642 | states->remote = remote_get(name); | |
643 | if (!states->remote) | |
644 | return error("No such remote: %s", name); | |
645 | ||
646 | read_branches(); | |
647 | ||
648 | if (query) { | |
649 | transport = transport_get(NULL, states->remote->url_nr > 0 ? | |
650 | states->remote->url[0] : NULL); | |
e0cc81e6 | 651 | remote_refs = transport_get_remote_refs(transport); |
67a7e2d0 OM |
652 | transport_disconnect(transport); |
653 | ||
e0cc81e6 | 654 | get_ref_states(remote_refs, states); |
67a7e2d0 OM |
655 | } |
656 | ||
657 | return 0; | |
658 | } | |
659 | ||
e7d5a97d OM |
660 | static int append_ref_to_tracked_list(const char *refname, |
661 | const unsigned char *sha1, int flags, void *cb_data) | |
662 | { | |
663 | struct ref_states *states = cb_data; | |
664 | struct refspec refspec; | |
665 | ||
666 | memset(&refspec, 0, sizeof(refspec)); | |
667 | refspec.dst = (char *)refname; | |
72972eb3 | 668 | if (!remote_find_tracking(states->remote, &refspec)) |
c455c87c | 669 | string_list_append(abbrev_branch(refspec.src), &states->tracked); |
e7d5a97d OM |
670 | |
671 | return 0; | |
672 | } | |
673 | ||
67a7e2d0 | 674 | static int show(int argc, const char **argv) |
211c8968 | 675 | { |
0ecfcb3b | 676 | int no_query = 0, result = 0; |
211c8968 JS |
677 | struct option options[] = { |
678 | OPT_GROUP("show specific options"), | |
0ecfcb3b | 679 | OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"), |
211c8968 JS |
680 | OPT_END() |
681 | }; | |
682 | struct ref_states states; | |
683 | ||
684 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
685 | ||
67a7e2d0 OM |
686 | if (argc < 1) |
687 | return show_all(); | |
211c8968 JS |
688 | |
689 | memset(&states, 0, sizeof(states)); | |
690 | for (; argc; argc--, argv++) { | |
0ecfcb3b | 691 | int i; |
211c8968 | 692 | |
67a7e2d0 | 693 | get_remote_ref_states(*argv, &states, !no_query); |
211c8968 JS |
694 | |
695 | printf("* remote %s\n URL: %s\n", *argv, | |
696 | states.remote->url_nr > 0 ? | |
697 | states.remote->url[0] : "(no URL)"); | |
698 | ||
699 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 700 | struct string_list_item *branch = branch_list.items + i; |
211c8968 JS |
701 | struct branch_info *info = branch->util; |
702 | int j; | |
703 | ||
e0cc81e6 | 704 | if (!info->merge.nr || strcmp(*argv, info->remote_name)) |
211c8968 JS |
705 | continue; |
706 | printf(" Remote branch%s merged with 'git pull' " | |
707 | "while on branch %s\n ", | |
708 | info->merge.nr > 1 ? "es" : "", | |
c455c87c | 709 | branch->string); |
211c8968 | 710 | for (j = 0; j < info->merge.nr; j++) |
c455c87c | 711 | printf(" %s", info->merge.items[j].string); |
211c8968 JS |
712 | printf("\n"); |
713 | } | |
714 | ||
0ecfcb3b | 715 | if (!no_query) { |
79bbc7fb JS |
716 | show_list(" New remote branch%s (next fetch " |
717 | "will store in remotes/%s)", | |
718 | &states.new, states.remote->name); | |
0ecfcb3b | 719 | show_list(" Stale tracking branch%s (use 'git remote " |
79bbc7fb | 720 | "prune')", &states.stale, ""); |
0ecfcb3b | 721 | } |
211c8968 | 722 | |
e7d5a97d OM |
723 | if (no_query) |
724 | for_each_ref(append_ref_to_tracked_list, &states); | |
79bbc7fb | 725 | show_list(" Tracked remote branch%s", &states.tracked, ""); |
e7d5a97d | 726 | |
211c8968 | 727 | if (states.remote->push_refspec_nr) { |
20244ea2 | 728 | printf(" Local branch%s pushed with 'git push'\n", |
211c8968 JS |
729 | states.remote->push_refspec_nr > 1 ? |
730 | "es" : ""); | |
731 | for (i = 0; i < states.remote->push_refspec_nr; i++) { | |
732 | struct refspec *spec = states.remote->push + i; | |
20244ea2 JS |
733 | printf(" %s%s%s%s\n", |
734 | spec->force ? "+" : "", | |
72972eb3 JH |
735 | abbrev_branch(spec->src), |
736 | spec->dst ? ":" : "", | |
737 | spec->dst ? abbrev_branch(spec->dst) : ""); | |
211c8968 JS |
738 | } |
739 | } | |
67a7e2d0 | 740 | |
88733235 | 741 | free_remote_ref_states(&states); |
67a7e2d0 OM |
742 | } |
743 | ||
744 | return result; | |
745 | } | |
746 | ||
747 | static int prune(int argc, const char **argv) | |
748 | { | |
8d767927 | 749 | int dry_run = 0, result = 0; |
67a7e2d0 OM |
750 | struct option options[] = { |
751 | OPT_GROUP("prune specific options"), | |
8d767927 | 752 | OPT__DRY_RUN(&dry_run), |
67a7e2d0 OM |
753 | OPT_END() |
754 | }; | |
755 | struct ref_states states; | |
f8948e2f | 756 | const char *dangling_msg; |
67a7e2d0 OM |
757 | |
758 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
759 | ||
760 | if (argc < 1) | |
761 | usage_with_options(builtin_remote_usage, options); | |
762 | ||
f8948e2f JH |
763 | dangling_msg = (dry_run |
764 | ? " %s will become dangling!\n" | |
765 | : " %s has become dangling!\n"); | |
766 | ||
67a7e2d0 OM |
767 | memset(&states, 0, sizeof(states)); |
768 | for (; argc; argc--, argv++) { | |
769 | int i; | |
770 | ||
8d767927 OM |
771 | get_remote_ref_states(*argv, &states, 1); |
772 | ||
8b7d4e73 JH |
773 | if (states.stale.nr) { |
774 | printf("Pruning %s\n", *argv); | |
8d767927 OM |
775 | printf("URL: %s\n", |
776 | states.remote->url_nr | |
777 | ? states.remote->url[0] | |
778 | : "(no URL)"); | |
8b7d4e73 | 779 | } |
67a7e2d0 OM |
780 | |
781 | for (i = 0; i < states.stale.nr; i++) { | |
782 | const char *refname = states.stale.items[i].util; | |
8d767927 OM |
783 | |
784 | if (!dry_run) | |
eca35a25 | 785 | result |= delete_ref(refname, NULL, 0); |
8d767927 OM |
786 | |
787 | printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned", | |
72972eb3 | 788 | abbrev_ref(refname, "refs/remotes/")); |
f8948e2f | 789 | warn_dangling_symref(dangling_msg, refname); |
67a7e2d0 OM |
790 | } |
791 | ||
88733235 | 792 | free_remote_ref_states(&states); |
211c8968 JS |
793 | } |
794 | ||
795 | return result; | |
796 | } | |
797 | ||
84521ed6 | 798 | static int get_one_remote_for_update(struct remote *remote, void *priv) |
211c8968 | 799 | { |
c455c87c | 800 | struct string_list *list = priv; |
211c8968 | 801 | if (!remote->skip_default_update) |
83b76736 | 802 | string_list_append(remote->name, list); |
84521ed6 JS |
803 | return 0; |
804 | } | |
805 | ||
806 | struct remote_group { | |
807 | const char *name; | |
c455c87c | 808 | struct string_list *list; |
84521ed6 JS |
809 | } remote_group; |
810 | ||
ef90d6d4 | 811 | static int get_remote_group(const char *key, const char *value, void *cb) |
84521ed6 JS |
812 | { |
813 | if (!prefixcmp(key, "remotes.") && | |
814 | !strcmp(key + 8, remote_group.name)) { | |
815 | /* split list by white space */ | |
816 | int space = strcspn(value, " \t\n"); | |
817 | while (*value) { | |
818 | if (space > 1) | |
c455c87c | 819 | string_list_append(xstrndup(value, space), |
84521ed6 JS |
820 | remote_group.list); |
821 | value += space + (value[space] != '\0'); | |
822 | space = strcspn(value, " \t\n"); | |
823 | } | |
824 | } | |
825 | ||
211c8968 JS |
826 | return 0; |
827 | } | |
828 | ||
829 | static int update(int argc, const char **argv) | |
830 | { | |
84521ed6 | 831 | int i, result = 0; |
c455c87c | 832 | struct string_list list = { NULL, 0, 0, 0 }; |
84521ed6 | 833 | static const char *default_argv[] = { NULL, "default", NULL }; |
211c8968 | 834 | |
84521ed6 JS |
835 | if (argc < 2) { |
836 | argc = 2; | |
837 | argv = default_argv; | |
838 | } | |
211c8968 | 839 | |
84521ed6 JS |
840 | remote_group.list = &list; |
841 | for (i = 1; i < argc; i++) { | |
842 | remote_group.name = argv[i]; | |
ef90d6d4 | 843 | result = git_config(get_remote_group, NULL); |
84521ed6 JS |
844 | } |
845 | ||
846 | if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default")) | |
847 | result = for_each_remote(get_one_remote_for_update, &list); | |
848 | ||
849 | for (i = 0; i < list.nr; i++) | |
c455c87c | 850 | result |= fetch_remote(list.items[i].string); |
84521ed6 JS |
851 | |
852 | /* all names were strdup()ed or strndup()ed */ | |
c455c87c JS |
853 | list.strdup_strings = 1; |
854 | string_list_clear(&list, 0); | |
84521ed6 JS |
855 | |
856 | return result; | |
211c8968 JS |
857 | } |
858 | ||
859 | static int get_one_entry(struct remote *remote, void *priv) | |
860 | { | |
c455c87c | 861 | struct string_list *list = priv; |
211c8968 | 862 | |
7d20e218 MG |
863 | if (remote->url_nr > 0) { |
864 | int i; | |
865 | ||
866 | for (i = 0; i < remote->url_nr; i++) | |
867 | string_list_append(remote->name, list)->util = (void *)remote->url[i]; | |
868 | } else | |
869 | string_list_append(remote->name, list)->util = NULL; | |
211c8968 JS |
870 | |
871 | return 0; | |
872 | } | |
873 | ||
874 | static int show_all(void) | |
875 | { | |
c455c87c | 876 | struct string_list list = { NULL, 0, 0 }; |
211c8968 JS |
877 | int result = for_each_remote(get_one_entry, &list); |
878 | ||
879 | if (!result) { | |
880 | int i; | |
881 | ||
c455c87c | 882 | sort_string_list(&list); |
211c8968 | 883 | for (i = 0; i < list.nr; i++) { |
c455c87c | 884 | struct string_list_item *item = list.items + i; |
7d20e218 MG |
885 | if (verbose) |
886 | printf("%s\t%s\n", item->string, | |
887 | item->util ? (const char *)item->util : ""); | |
888 | else { | |
889 | if (i && !strcmp((item - 1)->string, item->string)) | |
890 | continue; | |
891 | printf("%s\n", item->string); | |
892 | } | |
211c8968 JS |
893 | } |
894 | } | |
895 | return result; | |
896 | } | |
897 | ||
898 | int cmd_remote(int argc, const char **argv, const char *prefix) | |
899 | { | |
900 | struct option options[] = { | |
901 | OPT__VERBOSE(&verbose), | |
902 | OPT_END() | |
903 | }; | |
904 | int result; | |
905 | ||
906 | argc = parse_options(argc, argv, options, builtin_remote_usage, | |
907 | PARSE_OPT_STOP_AT_NON_OPTION); | |
908 | ||
909 | if (argc < 1) | |
910 | result = show_all(); | |
911 | else if (!strcmp(argv[0], "add")) | |
912 | result = add(argc, argv); | |
bf98421a MV |
913 | else if (!strcmp(argv[0], "rename")) |
914 | result = mv(argc, argv); | |
211c8968 JS |
915 | else if (!strcmp(argv[0], "rm")) |
916 | result = rm(argc, argv); | |
917 | else if (!strcmp(argv[0], "show")) | |
67a7e2d0 | 918 | result = show(argc, argv); |
211c8968 | 919 | else if (!strcmp(argv[0], "prune")) |
67a7e2d0 | 920 | result = prune(argc, argv); |
211c8968 JS |
921 | else if (!strcmp(argv[0], "update")) |
922 | result = update(argc, argv); | |
923 | else { | |
924 | error("Unknown subcommand: %s", argv[0]); | |
925 | usage_with_options(builtin_remote_usage, options); | |
926 | } | |
927 | ||
928 | return result ? 1 : 0; | |
929 | } |