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