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