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