]>
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 { | |
146 | char *remote; | |
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) { | |
185 | if (info->remote) | |
186 | warning("more than one branch.%s", key); | |
187 | info->remote = xstrdup(value); | |
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); |
c455c87c | 209 | sort_string_list(&branch_list); |
211c8968 JS |
210 | } |
211 | ||
212 | struct ref_states { | |
213 | struct remote *remote; | |
c455c87c | 214 | struct string_list new, stale, tracked; |
211c8968 JS |
215 | }; |
216 | ||
217 | static int handle_one_branch(const char *refname, | |
218 | const unsigned char *sha1, int flags, void *cb_data) | |
219 | { | |
220 | struct ref_states *states = cb_data; | |
221 | struct refspec refspec; | |
222 | ||
223 | memset(&refspec, 0, sizeof(refspec)); | |
224 | refspec.dst = (char *)refname; | |
225 | if (!remote_find_tracking(states->remote, &refspec)) { | |
c455c87c | 226 | struct string_list_item *item; |
72972eb3 | 227 | const char *name = abbrev_branch(refspec.src); |
740fdd27 JS |
228 | /* symbolic refs pointing nowhere were handled already */ |
229 | if ((flags & REF_ISSYMREF) || | |
c455c87c | 230 | unsorted_string_list_has_string(&states->tracked, |
740fdd27 | 231 | name) || |
c455c87c | 232 | unsorted_string_list_has_string(&states->new, |
211c8968 JS |
233 | name)) |
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 | ||
241 | static int get_ref_states(const struct ref *ref, struct ref_states *states) | |
242 | { | |
243 | struct ref *fetch_map = NULL, **tail = &fetch_map; | |
244 | int i; | |
245 | ||
246 | for (i = 0; i < states->remote->fetch_refspec_nr; i++) | |
247 | if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1)) | |
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) { |
c455c87c | 253 | struct string_list *target = &states->tracked; |
211c8968 JS |
254 | unsigned char sha1[20]; |
255 | void *util = NULL; | |
256 | ||
257 | if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1)) | |
258 | target = &states->new; | |
259 | else { | |
260 | target = &states->tracked; | |
261 | if (hashcmp(sha1, ref->new_sha1)) | |
262 | util = &states; | |
263 | } | |
c455c87c | 264 | string_list_append(abbrev_branch(ref->name), target)->util = util; |
211c8968 JS |
265 | } |
266 | free_refs(fetch_map); | |
267 | ||
211c8968 | 268 | for_each_ref(handle_one_branch, states); |
c455c87c | 269 | sort_string_list(&states->stale); |
211c8968 JS |
270 | |
271 | return 0; | |
272 | } | |
273 | ||
7ad2458f SP |
274 | struct known_remote { |
275 | struct known_remote *next; | |
276 | struct remote *remote; | |
277 | }; | |
278 | ||
279 | struct known_remotes { | |
280 | struct remote *to_delete; | |
281 | struct known_remote *list; | |
282 | }; | |
283 | ||
284 | static int add_known_remote(struct remote *remote, void *cb_data) | |
285 | { | |
286 | struct known_remotes *all = cb_data; | |
287 | struct known_remote *r; | |
288 | ||
289 | if (!strcmp(all->to_delete->name, remote->name)) | |
290 | return 0; | |
291 | ||
292 | r = xmalloc(sizeof(*r)); | |
293 | r->remote = remote; | |
294 | r->next = all->list; | |
295 | all->list = r; | |
296 | return 0; | |
297 | } | |
298 | ||
211c8968 | 299 | struct branches_for_remote { |
7ad2458f | 300 | struct remote *remote; |
c455c87c | 301 | struct string_list *branches; |
7ad2458f | 302 | struct known_remotes *keep; |
211c8968 JS |
303 | }; |
304 | ||
305 | static int add_branch_for_removal(const char *refname, | |
306 | const unsigned char *sha1, int flags, void *cb_data) | |
307 | { | |
308 | struct branches_for_remote *branches = cb_data; | |
7ad2458f | 309 | struct refspec refspec; |
c455c87c | 310 | struct string_list_item *item; |
7ad2458f | 311 | struct known_remote *kr; |
211c8968 | 312 | |
7ad2458f SP |
313 | memset(&refspec, 0, sizeof(refspec)); |
314 | refspec.dst = (char *)refname; | |
315 | if (remote_find_tracking(branches->remote, &refspec)) | |
316 | return 0; | |
317 | ||
318 | /* don't delete a branch if another remote also uses it */ | |
319 | for (kr = branches->keep->list; kr; kr = kr->next) { | |
320 | memset(&refspec, 0, sizeof(refspec)); | |
321 | refspec.dst = (char *)refname; | |
322 | if (!remote_find_tracking(kr->remote, &refspec)) | |
323 | return 0; | |
324 | } | |
3b9dcff5 | 325 | |
7ad2458f SP |
326 | /* make sure that symrefs are deleted */ |
327 | if (flags & REF_ISSYMREF) | |
9db56f71 | 328 | return unlink(git_path("%s", refname)); |
3b9dcff5 | 329 | |
c455c87c | 330 | item = string_list_append(refname, branches->branches); |
7ad2458f SP |
331 | item->util = xmalloc(20); |
332 | hashcpy(item->util, sha1); | |
211c8968 JS |
333 | |
334 | return 0; | |
335 | } | |
336 | ||
bf98421a MV |
337 | struct rename_info { |
338 | const char *old; | |
339 | const char *new; | |
340 | struct string_list *remote_branches; | |
341 | }; | |
342 | ||
343 | static int read_remote_branches(const char *refname, | |
344 | const unsigned char *sha1, int flags, void *cb_data) | |
345 | { | |
346 | struct rename_info *rename = cb_data; | |
347 | struct strbuf buf = STRBUF_INIT; | |
348 | struct string_list_item *item; | |
349 | int flag; | |
350 | unsigned char orig_sha1[20]; | |
351 | const char *symref; | |
352 | ||
353 | strbuf_addf(&buf, "refs/remotes/%s", rename->old); | |
354 | if(!prefixcmp(refname, buf.buf)) { | |
355 | item = string_list_append(xstrdup(refname), rename->remote_branches); | |
356 | symref = resolve_ref(refname, orig_sha1, 1, &flag); | |
357 | if (flag & REF_ISSYMREF) | |
358 | item->util = xstrdup(symref); | |
359 | else | |
360 | item->util = NULL; | |
361 | } | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
1dd1239a MV |
366 | static int migrate_file(struct remote *remote) |
367 | { | |
368 | struct strbuf buf = STRBUF_INIT; | |
369 | int i; | |
370 | char *path = NULL; | |
371 | ||
372 | strbuf_addf(&buf, "remote.%s.url", remote->name); | |
373 | for (i = 0; i < remote->url_nr; i++) | |
374 | if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0)) | |
375 | return error("Could not append '%s' to '%s'", | |
376 | remote->url[i], buf.buf); | |
377 | strbuf_reset(&buf); | |
378 | strbuf_addf(&buf, "remote.%s.push", remote->name); | |
379 | for (i = 0; i < remote->push_refspec_nr; i++) | |
380 | if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0)) | |
381 | return error("Could not append '%s' to '%s'", | |
382 | remote->push_refspec[i], buf.buf); | |
383 | strbuf_reset(&buf); | |
384 | strbuf_addf(&buf, "remote.%s.fetch", remote->name); | |
385 | for (i = 0; i < remote->fetch_refspec_nr; i++) | |
386 | if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0)) | |
387 | return error("Could not append '%s' to '%s'", | |
388 | remote->fetch_refspec[i], buf.buf); | |
389 | if (remote->origin == REMOTE_REMOTES) | |
390 | path = git_path("remotes/%s", remote->name); | |
391 | else if (remote->origin == REMOTE_BRANCHES) | |
392 | path = git_path("branches/%s", remote->name); | |
393 | if (path && unlink(path)) | |
394 | warning("failed to remove '%s'", path); | |
395 | return 0; | |
396 | } | |
397 | ||
bf98421a MV |
398 | static int mv(int argc, const char **argv) |
399 | { | |
400 | struct option options[] = { | |
401 | OPT_END() | |
402 | }; | |
403 | struct remote *oldremote, *newremote; | |
404 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT; | |
405 | struct string_list remote_branches = { NULL, 0, 0, 0 }; | |
406 | struct rename_info rename; | |
407 | int i; | |
408 | ||
409 | if (argc != 3) | |
410 | usage_with_options(builtin_remote_usage, options); | |
411 | ||
412 | rename.old = argv[1]; | |
413 | rename.new = argv[2]; | |
414 | rename.remote_branches = &remote_branches; | |
415 | ||
416 | oldremote = remote_get(rename.old); | |
417 | if (!oldremote) | |
418 | die("No such remote: %s", rename.old); | |
419 | ||
1dd1239a MV |
420 | if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG) |
421 | return migrate_file(oldremote); | |
422 | ||
bf98421a MV |
423 | newremote = remote_get(rename.new); |
424 | if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr)) | |
425 | die("remote %s already exists.", rename.new); | |
426 | ||
427 | strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new); | |
428 | if (!valid_fetch_refspec(buf.buf)) | |
429 | die("'%s' is not a valid remote name", rename.new); | |
430 | ||
431 | strbuf_reset(&buf); | |
432 | strbuf_addf(&buf, "remote.%s", rename.old); | |
433 | strbuf_addf(&buf2, "remote.%s", rename.new); | |
434 | if (git_config_rename_section(buf.buf, buf2.buf) < 1) | |
435 | return error("Could not rename config section '%s' to '%s'", | |
436 | buf.buf, buf2.buf); | |
437 | ||
438 | strbuf_reset(&buf); | |
439 | strbuf_addf(&buf, "remote.%s.fetch", rename.new); | |
440 | if (git_config_set_multivar(buf.buf, NULL, NULL, 1)) | |
441 | return error("Could not remove config section '%s'", buf.buf); | |
442 | for (i = 0; i < oldremote->fetch_refspec_nr; i++) { | |
443 | char *ptr; | |
444 | ||
445 | strbuf_reset(&buf2); | |
446 | strbuf_addstr(&buf2, oldremote->fetch_refspec[i]); | |
447 | ptr = strstr(buf2.buf, rename.old); | |
448 | if (ptr) | |
449 | strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old), | |
450 | rename.new, strlen(rename.new)); | |
451 | if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) | |
452 | return error("Could not append '%s'", buf.buf); | |
453 | } | |
454 | ||
455 | read_branches(); | |
456 | for (i = 0; i < branch_list.nr; i++) { | |
457 | struct string_list_item *item = branch_list.items + i; | |
458 | struct branch_info *info = item->util; | |
459 | if (info->remote && !strcmp(info->remote, rename.old)) { | |
460 | strbuf_reset(&buf); | |
461 | strbuf_addf(&buf, "branch.%s.remote", item->string); | |
462 | if (git_config_set(buf.buf, rename.new)) { | |
463 | return error("Could not set '%s'", buf.buf); | |
464 | } | |
465 | } | |
466 | } | |
467 | ||
468 | /* | |
469 | * First remove symrefs, then rename the rest, finally create | |
470 | * the new symrefs. | |
471 | */ | |
472 | for_each_ref(read_remote_branches, &rename); | |
473 | for (i = 0; i < remote_branches.nr; i++) { | |
474 | struct string_list_item *item = remote_branches.items + i; | |
475 | int flag = 0; | |
476 | unsigned char sha1[20]; | |
477 | const char *symref; | |
478 | ||
479 | symref = resolve_ref(item->string, sha1, 1, &flag); | |
480 | if (!(flag & REF_ISSYMREF)) | |
481 | continue; | |
482 | if (delete_ref(item->string, NULL, REF_NODEREF)) | |
483 | die("deleting '%s' failed", item->string); | |
484 | } | |
485 | for (i = 0; i < remote_branches.nr; i++) { | |
486 | struct string_list_item *item = remote_branches.items + i; | |
487 | ||
488 | if (item->util) | |
489 | continue; | |
490 | strbuf_reset(&buf); | |
491 | strbuf_addstr(&buf, item->string); | |
492 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), | |
493 | rename.new, strlen(rename.new)); | |
494 | strbuf_reset(&buf2); | |
495 | strbuf_addf(&buf2, "remote: renamed %s to %s", | |
496 | item->string, buf.buf); | |
497 | if (rename_ref(item->string, buf.buf, buf2.buf)) | |
498 | die("renaming '%s' failed", item->string); | |
499 | } | |
500 | for (i = 0; i < remote_branches.nr; i++) { | |
501 | struct string_list_item *item = remote_branches.items + i; | |
502 | ||
503 | if (!item->util) | |
504 | continue; | |
505 | strbuf_reset(&buf); | |
506 | strbuf_addstr(&buf, item->string); | |
507 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old), | |
508 | rename.new, strlen(rename.new)); | |
509 | strbuf_reset(&buf2); | |
510 | strbuf_addstr(&buf2, item->util); | |
511 | strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old), | |
512 | rename.new, strlen(rename.new)); | |
513 | strbuf_reset(&buf3); | |
514 | strbuf_addf(&buf3, "remote: renamed %s to %s", | |
515 | item->string, buf.buf); | |
516 | if (create_symref(buf.buf, buf2.buf, buf3.buf)) | |
517 | die("creating '%s' failed", buf.buf); | |
518 | } | |
519 | return 0; | |
520 | } | |
521 | ||
c455c87c | 522 | static int remove_branches(struct string_list *branches) |
211c8968 JS |
523 | { |
524 | int i, result = 0; | |
525 | for (i = 0; i < branches->nr; i++) { | |
c455c87c JS |
526 | struct string_list_item *item = branches->items + i; |
527 | const char *refname = item->string; | |
211c8968 JS |
528 | unsigned char *sha1 = item->util; |
529 | ||
eca35a25 | 530 | if (delete_ref(refname, sha1, 0)) |
211c8968 JS |
531 | result |= error("Could not remove branch %s", refname); |
532 | } | |
533 | return result; | |
534 | } | |
535 | ||
536 | static int rm(int argc, const char **argv) | |
537 | { | |
538 | struct option options[] = { | |
539 | OPT_END() | |
540 | }; | |
541 | struct remote *remote; | |
f285a2d7 | 542 | struct strbuf buf = STRBUF_INIT; |
7ad2458f | 543 | struct known_remotes known_remotes = { NULL, NULL }; |
c455c87c | 544 | struct string_list branches = { NULL, 0, 0, 1 }; |
7ad2458f | 545 | struct branches_for_remote cb_data = { NULL, &branches, &known_remotes }; |
211c8968 JS |
546 | int i; |
547 | ||
548 | if (argc != 2) | |
549 | usage_with_options(builtin_remote_usage, options); | |
550 | ||
551 | remote = remote_get(argv[1]); | |
552 | if (!remote) | |
553 | die("No such remote: %s", argv[1]); | |
554 | ||
7ad2458f SP |
555 | known_remotes.to_delete = remote; |
556 | for_each_remote(add_known_remote, &known_remotes); | |
557 | ||
211c8968 JS |
558 | strbuf_addf(&buf, "remote.%s", remote->name); |
559 | if (git_config_rename_section(buf.buf, NULL) < 1) | |
560 | return error("Could not remove config section '%s'", buf.buf); | |
561 | ||
562 | read_branches(); | |
563 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 564 | struct string_list_item *item = branch_list.items + i; |
211c8968 JS |
565 | struct branch_info *info = item->util; |
566 | if (info->remote && !strcmp(info->remote, remote->name)) { | |
567 | const char *keys[] = { "remote", "merge", NULL }, **k; | |
568 | for (k = keys; *k; k++) { | |
569 | strbuf_reset(&buf); | |
570 | strbuf_addf(&buf, "branch.%s.%s", | |
c455c87c | 571 | item->string, *k); |
211c8968 JS |
572 | if (git_config_set(buf.buf, NULL)) { |
573 | strbuf_release(&buf); | |
574 | return -1; | |
575 | } | |
576 | } | |
577 | } | |
578 | } | |
579 | ||
580 | /* | |
581 | * We cannot just pass a function to for_each_ref() which deletes | |
582 | * the branches one by one, since for_each_ref() relies on cached | |
583 | * refs, which are invalidated when deleting a branch. | |
584 | */ | |
7ad2458f | 585 | cb_data.remote = remote; |
211c8968 JS |
586 | i = for_each_ref(add_branch_for_removal, &cb_data); |
587 | strbuf_release(&buf); | |
588 | ||
589 | if (!i) | |
590 | i = remove_branches(&branches); | |
c455c87c | 591 | string_list_clear(&branches, 1); |
211c8968 JS |
592 | |
593 | return i; | |
594 | } | |
595 | ||
79bbc7fb JS |
596 | static void show_list(const char *title, struct string_list *list, |
597 | const char *extra_arg) | |
211c8968 JS |
598 | { |
599 | int i; | |
600 | ||
601 | if (!list->nr) | |
602 | return; | |
603 | ||
79bbc7fb | 604 | printf(title, list->nr > 1 ? "es" : "", extra_arg); |
211c8968 | 605 | printf("\n"); |
20244ea2 JS |
606 | for (i = 0; i < list->nr; i++) |
607 | printf(" %s\n", list->items[i].string); | |
211c8968 JS |
608 | } |
609 | ||
67a7e2d0 OM |
610 | static int get_remote_ref_states(const char *name, |
611 | struct ref_states *states, | |
612 | int query) | |
613 | { | |
614 | struct transport *transport; | |
615 | const struct ref *ref; | |
616 | ||
617 | states->remote = remote_get(name); | |
618 | if (!states->remote) | |
619 | return error("No such remote: %s", name); | |
620 | ||
621 | read_branches(); | |
622 | ||
623 | if (query) { | |
624 | transport = transport_get(NULL, states->remote->url_nr > 0 ? | |
625 | states->remote->url[0] : NULL); | |
626 | ref = transport_get_remote_refs(transport); | |
627 | transport_disconnect(transport); | |
628 | ||
629 | get_ref_states(ref, states); | |
630 | } | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
e7d5a97d OM |
635 | static int append_ref_to_tracked_list(const char *refname, |
636 | const unsigned char *sha1, int flags, void *cb_data) | |
637 | { | |
638 | struct ref_states *states = cb_data; | |
639 | struct refspec refspec; | |
640 | ||
641 | memset(&refspec, 0, sizeof(refspec)); | |
642 | refspec.dst = (char *)refname; | |
72972eb3 | 643 | if (!remote_find_tracking(states->remote, &refspec)) |
c455c87c | 644 | string_list_append(abbrev_branch(refspec.src), &states->tracked); |
e7d5a97d OM |
645 | |
646 | return 0; | |
647 | } | |
648 | ||
67a7e2d0 | 649 | static int show(int argc, const char **argv) |
211c8968 | 650 | { |
0ecfcb3b | 651 | int no_query = 0, result = 0; |
211c8968 JS |
652 | struct option options[] = { |
653 | OPT_GROUP("show specific options"), | |
0ecfcb3b | 654 | OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"), |
211c8968 JS |
655 | OPT_END() |
656 | }; | |
657 | struct ref_states states; | |
658 | ||
659 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
660 | ||
67a7e2d0 OM |
661 | if (argc < 1) |
662 | return show_all(); | |
211c8968 JS |
663 | |
664 | memset(&states, 0, sizeof(states)); | |
665 | for (; argc; argc--, argv++) { | |
0ecfcb3b | 666 | int i; |
211c8968 | 667 | |
67a7e2d0 | 668 | get_remote_ref_states(*argv, &states, !no_query); |
211c8968 JS |
669 | |
670 | printf("* remote %s\n URL: %s\n", *argv, | |
671 | states.remote->url_nr > 0 ? | |
672 | states.remote->url[0] : "(no URL)"); | |
673 | ||
674 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 675 | struct string_list_item *branch = branch_list.items + i; |
211c8968 JS |
676 | struct branch_info *info = branch->util; |
677 | int j; | |
678 | ||
679 | if (!info->merge.nr || strcmp(*argv, info->remote)) | |
680 | continue; | |
681 | printf(" Remote branch%s merged with 'git pull' " | |
682 | "while on branch %s\n ", | |
683 | info->merge.nr > 1 ? "es" : "", | |
c455c87c | 684 | branch->string); |
211c8968 | 685 | for (j = 0; j < info->merge.nr; j++) |
c455c87c | 686 | printf(" %s", info->merge.items[j].string); |
211c8968 JS |
687 | printf("\n"); |
688 | } | |
689 | ||
0ecfcb3b | 690 | if (!no_query) { |
79bbc7fb JS |
691 | show_list(" New remote branch%s (next fetch " |
692 | "will store in remotes/%s)", | |
693 | &states.new, states.remote->name); | |
0ecfcb3b | 694 | show_list(" Stale tracking branch%s (use 'git remote " |
79bbc7fb | 695 | "prune')", &states.stale, ""); |
0ecfcb3b | 696 | } |
211c8968 | 697 | |
e7d5a97d OM |
698 | if (no_query) |
699 | for_each_ref(append_ref_to_tracked_list, &states); | |
79bbc7fb | 700 | show_list(" Tracked remote branch%s", &states.tracked, ""); |
e7d5a97d | 701 | |
211c8968 | 702 | if (states.remote->push_refspec_nr) { |
20244ea2 | 703 | printf(" Local branch%s pushed with 'git push'\n", |
211c8968 JS |
704 | states.remote->push_refspec_nr > 1 ? |
705 | "es" : ""); | |
706 | for (i = 0; i < states.remote->push_refspec_nr; i++) { | |
707 | struct refspec *spec = states.remote->push + i; | |
20244ea2 JS |
708 | printf(" %s%s%s%s\n", |
709 | spec->force ? "+" : "", | |
72972eb3 JH |
710 | abbrev_branch(spec->src), |
711 | spec->dst ? ":" : "", | |
712 | spec->dst ? abbrev_branch(spec->dst) : ""); | |
211c8968 JS |
713 | } |
714 | } | |
67a7e2d0 OM |
715 | |
716 | /* NEEDSWORK: free remote */ | |
c455c87c JS |
717 | string_list_clear(&states.new, 0); |
718 | string_list_clear(&states.stale, 0); | |
719 | string_list_clear(&states.tracked, 0); | |
67a7e2d0 OM |
720 | } |
721 | ||
722 | return result; | |
723 | } | |
724 | ||
725 | static int prune(int argc, const char **argv) | |
726 | { | |
8d767927 | 727 | int dry_run = 0, result = 0; |
67a7e2d0 OM |
728 | struct option options[] = { |
729 | OPT_GROUP("prune specific options"), | |
8d767927 | 730 | OPT__DRY_RUN(&dry_run), |
67a7e2d0 OM |
731 | OPT_END() |
732 | }; | |
733 | struct ref_states states; | |
734 | ||
735 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
736 | ||
737 | if (argc < 1) | |
738 | usage_with_options(builtin_remote_usage, options); | |
739 | ||
740 | memset(&states, 0, sizeof(states)); | |
741 | for (; argc; argc--, argv++) { | |
742 | int i; | |
743 | ||
8d767927 OM |
744 | get_remote_ref_states(*argv, &states, 1); |
745 | ||
8b7d4e73 JH |
746 | if (states.stale.nr) { |
747 | printf("Pruning %s\n", *argv); | |
8d767927 OM |
748 | printf("URL: %s\n", |
749 | states.remote->url_nr | |
750 | ? states.remote->url[0] | |
751 | : "(no URL)"); | |
8b7d4e73 | 752 | } |
67a7e2d0 OM |
753 | |
754 | for (i = 0; i < states.stale.nr; i++) { | |
755 | const char *refname = states.stale.items[i].util; | |
8d767927 OM |
756 | |
757 | if (!dry_run) | |
eca35a25 | 758 | result |= delete_ref(refname, NULL, 0); |
8d767927 OM |
759 | |
760 | printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned", | |
72972eb3 | 761 | abbrev_ref(refname, "refs/remotes/")); |
67a7e2d0 OM |
762 | } |
763 | ||
211c8968 | 764 | /* NEEDSWORK: free remote */ |
c455c87c JS |
765 | string_list_clear(&states.new, 0); |
766 | string_list_clear(&states.stale, 0); | |
767 | string_list_clear(&states.tracked, 0); | |
211c8968 JS |
768 | } |
769 | ||
770 | return result; | |
771 | } | |
772 | ||
84521ed6 | 773 | static int get_one_remote_for_update(struct remote *remote, void *priv) |
211c8968 | 774 | { |
c455c87c | 775 | struct string_list *list = priv; |
211c8968 | 776 | if (!remote->skip_default_update) |
83b76736 | 777 | string_list_append(remote->name, list); |
84521ed6 JS |
778 | return 0; |
779 | } | |
780 | ||
781 | struct remote_group { | |
782 | const char *name; | |
c455c87c | 783 | struct string_list *list; |
84521ed6 JS |
784 | } remote_group; |
785 | ||
ef90d6d4 | 786 | static int get_remote_group(const char *key, const char *value, void *cb) |
84521ed6 JS |
787 | { |
788 | if (!prefixcmp(key, "remotes.") && | |
789 | !strcmp(key + 8, remote_group.name)) { | |
790 | /* split list by white space */ | |
791 | int space = strcspn(value, " \t\n"); | |
792 | while (*value) { | |
793 | if (space > 1) | |
c455c87c | 794 | string_list_append(xstrndup(value, space), |
84521ed6 JS |
795 | remote_group.list); |
796 | value += space + (value[space] != '\0'); | |
797 | space = strcspn(value, " \t\n"); | |
798 | } | |
799 | } | |
800 | ||
211c8968 JS |
801 | return 0; |
802 | } | |
803 | ||
804 | static int update(int argc, const char **argv) | |
805 | { | |
84521ed6 | 806 | int i, result = 0; |
c455c87c | 807 | struct string_list list = { NULL, 0, 0, 0 }; |
84521ed6 | 808 | static const char *default_argv[] = { NULL, "default", NULL }; |
211c8968 | 809 | |
84521ed6 JS |
810 | if (argc < 2) { |
811 | argc = 2; | |
812 | argv = default_argv; | |
813 | } | |
211c8968 | 814 | |
84521ed6 JS |
815 | remote_group.list = &list; |
816 | for (i = 1; i < argc; i++) { | |
817 | remote_group.name = argv[i]; | |
ef90d6d4 | 818 | result = git_config(get_remote_group, NULL); |
84521ed6 JS |
819 | } |
820 | ||
821 | if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default")) | |
822 | result = for_each_remote(get_one_remote_for_update, &list); | |
823 | ||
824 | for (i = 0; i < list.nr; i++) | |
c455c87c | 825 | result |= fetch_remote(list.items[i].string); |
84521ed6 JS |
826 | |
827 | /* all names were strdup()ed or strndup()ed */ | |
c455c87c JS |
828 | list.strdup_strings = 1; |
829 | string_list_clear(&list, 0); | |
84521ed6 JS |
830 | |
831 | return result; | |
211c8968 JS |
832 | } |
833 | ||
834 | static int get_one_entry(struct remote *remote, void *priv) | |
835 | { | |
c455c87c | 836 | struct string_list *list = priv; |
211c8968 | 837 | |
7d20e218 MG |
838 | if (remote->url_nr > 0) { |
839 | int i; | |
840 | ||
841 | for (i = 0; i < remote->url_nr; i++) | |
842 | string_list_append(remote->name, list)->util = (void *)remote->url[i]; | |
843 | } else | |
844 | string_list_append(remote->name, list)->util = NULL; | |
211c8968 JS |
845 | |
846 | return 0; | |
847 | } | |
848 | ||
849 | static int show_all(void) | |
850 | { | |
c455c87c | 851 | struct string_list list = { NULL, 0, 0 }; |
211c8968 JS |
852 | int result = for_each_remote(get_one_entry, &list); |
853 | ||
854 | if (!result) { | |
855 | int i; | |
856 | ||
c455c87c | 857 | sort_string_list(&list); |
211c8968 | 858 | for (i = 0; i < list.nr; i++) { |
c455c87c | 859 | struct string_list_item *item = list.items + i; |
7d20e218 MG |
860 | if (verbose) |
861 | printf("%s\t%s\n", item->string, | |
862 | item->util ? (const char *)item->util : ""); | |
863 | else { | |
864 | if (i && !strcmp((item - 1)->string, item->string)) | |
865 | continue; | |
866 | printf("%s\n", item->string); | |
867 | } | |
211c8968 JS |
868 | } |
869 | } | |
870 | return result; | |
871 | } | |
872 | ||
873 | int cmd_remote(int argc, const char **argv, const char *prefix) | |
874 | { | |
875 | struct option options[] = { | |
876 | OPT__VERBOSE(&verbose), | |
877 | OPT_END() | |
878 | }; | |
879 | int result; | |
880 | ||
881 | argc = parse_options(argc, argv, options, builtin_remote_usage, | |
882 | PARSE_OPT_STOP_AT_NON_OPTION); | |
883 | ||
884 | if (argc < 1) | |
885 | result = show_all(); | |
886 | else if (!strcmp(argv[0], "add")) | |
887 | result = add(argc, argv); | |
bf98421a MV |
888 | else if (!strcmp(argv[0], "rename")) |
889 | result = mv(argc, argv); | |
211c8968 JS |
890 | else if (!strcmp(argv[0], "rm")) |
891 | result = rm(argc, argv); | |
892 | else if (!strcmp(argv[0], "show")) | |
67a7e2d0 | 893 | result = show(argc, argv); |
211c8968 | 894 | else if (!strcmp(argv[0], "prune")) |
67a7e2d0 | 895 | result = prune(argc, argv); |
211c8968 JS |
896 | else if (!strcmp(argv[0], "update")) |
897 | result = update(argc, argv); | |
898 | else { | |
899 | error("Unknown subcommand: %s", argv[0]); | |
900 | usage_with_options(builtin_remote_usage, options); | |
901 | } | |
902 | ||
903 | return result ? 1 : 0; | |
904 | } |