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