| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "builtin.h" |
| 4 | #include "advice.h" |
| 5 | #include "config.h" |
| 6 | #include "date.h" |
| 7 | #include "gettext.h" |
| 8 | #include "ident.h" |
| 9 | #include "parse-options.h" |
| 10 | #include "path.h" |
| 11 | #include "transport.h" |
| 12 | #include "remote.h" |
| 13 | #include "string-list.h" |
| 14 | #include "strbuf.h" |
| 15 | #include "run-command.h" |
| 16 | #include "rebase.h" |
| 17 | #include "refs.h" |
| 18 | #include "refspec.h" |
| 19 | #include "odb.h" |
| 20 | #include "strvec.h" |
| 21 | #include "commit-reach.h" |
| 22 | #include "progress.h" |
| 23 | |
| 24 | static const char * const builtin_remote_usage[] = { |
| 25 | "git remote [-v | --verbose]", |
| 26 | N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"), |
| 27 | N_("git remote rename [--[no-]progress] <old> <new>"), |
| 28 | N_("git remote remove <name>"), |
| 29 | N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), |
| 30 | N_("git remote [-v | --verbose] show [-n] <name>"), |
| 31 | N_("git remote prune [-n | --dry-run] <name>"), |
| 32 | N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"), |
| 33 | N_("git remote set-branches [--add] <name> <branch>..."), |
| 34 | N_("git remote get-url [--push] [--all] <name>"), |
| 35 | N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), |
| 36 | N_("git remote set-url --add <name> <newurl>"), |
| 37 | N_("git remote set-url --delete <name> <url>"), |
| 38 | NULL |
| 39 | }; |
| 40 | |
| 41 | static const char * const builtin_remote_add_usage[] = { |
| 42 | N_("git remote add [<options>] <name> <url>"), |
| 43 | NULL |
| 44 | }; |
| 45 | |
| 46 | static const char * const builtin_remote_rename_usage[] = { |
| 47 | N_("git remote rename [--[no-]progress] <old> <new>"), |
| 48 | NULL |
| 49 | }; |
| 50 | |
| 51 | static const char * const builtin_remote_rm_usage[] = { |
| 52 | N_("git remote remove <name>"), |
| 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | static const char * const builtin_remote_sethead_usage[] = { |
| 57 | N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), |
| 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | static const char * const builtin_remote_setbranches_usage[] = { |
| 62 | N_("git remote set-branches <name> <branch>..."), |
| 63 | N_("git remote set-branches --add <name> <branch>..."), |
| 64 | NULL |
| 65 | }; |
| 66 | |
| 67 | static const char * const builtin_remote_show_usage[] = { |
| 68 | N_("git remote show [<options>] <name>"), |
| 69 | NULL |
| 70 | }; |
| 71 | |
| 72 | static const char * const builtin_remote_prune_usage[] = { |
| 73 | N_("git remote prune [<options>] <name>"), |
| 74 | NULL |
| 75 | }; |
| 76 | |
| 77 | static const char * const builtin_remote_update_usage[] = { |
| 78 | N_("git remote update [<options>] [<group> | <remote>]..."), |
| 79 | NULL |
| 80 | }; |
| 81 | |
| 82 | static const char * const builtin_remote_geturl_usage[] = { |
| 83 | N_("git remote get-url [--push] [--all] <name>"), |
| 84 | NULL |
| 85 | }; |
| 86 | |
| 87 | static const char * const builtin_remote_seturl_usage[] = { |
| 88 | N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), |
| 89 | N_("git remote set-url --add <name> <newurl>"), |
| 90 | N_("git remote set-url --delete <name> <url>"), |
| 91 | NULL |
| 92 | }; |
| 93 | |
| 94 | #define GET_REF_STATES (1<<0) |
| 95 | #define GET_HEAD_NAMES (1<<1) |
| 96 | #define GET_PUSH_REF_STATES (1<<2) |
| 97 | |
| 98 | static int verbose; |
| 99 | |
| 100 | static int fetch_remote(const char *name) |
| 101 | { |
| 102 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 103 | |
| 104 | strvec_push(&cmd.args, "fetch"); |
| 105 | if (verbose) |
| 106 | strvec_push(&cmd.args, "-v"); |
| 107 | strvec_push(&cmd.args, name); |
| 108 | cmd.git_cmd = 1; |
| 109 | printf_ln(_("Updating %s"), name); |
| 110 | if (run_command(&cmd)) |
| 111 | return error(_("Could not fetch %s"), name); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | enum { |
| 116 | TAGS_UNSET = 0, |
| 117 | TAGS_DEFAULT = 1, |
| 118 | TAGS_SET = 2 |
| 119 | }; |
| 120 | |
| 121 | #define MIRROR_NONE 0 |
| 122 | #define MIRROR_FETCH 1 |
| 123 | #define MIRROR_PUSH 2 |
| 124 | #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH) |
| 125 | |
| 126 | static void add_branch(const char *key, const char *branchname, |
| 127 | const char *remotename, int mirror, struct strbuf *tmp) |
| 128 | { |
| 129 | strbuf_reset(tmp); |
| 130 | strbuf_addch(tmp, '+'); |
| 131 | if (mirror) |
| 132 | strbuf_addf(tmp, "refs/%s:refs/%s", |
| 133 | branchname, branchname); |
| 134 | else |
| 135 | strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s", |
| 136 | branchname, remotename, branchname); |
| 137 | repo_config_set_multivar(the_repository, key, tmp->buf, "^$", 0); |
| 138 | } |
| 139 | |
| 140 | static const char mirror_advice[] = |
| 141 | N_("--mirror is dangerous and deprecated; please\n" |
| 142 | "\t use --mirror=fetch or --mirror=push instead"); |
| 143 | |
| 144 | static int parse_mirror_opt(const struct option *opt, const char *arg, int not) |
| 145 | { |
| 146 | unsigned *mirror = opt->value; |
| 147 | if (not) |
| 148 | *mirror = MIRROR_NONE; |
| 149 | else if (!arg) { |
| 150 | warning("%s", _(mirror_advice)); |
| 151 | *mirror = MIRROR_BOTH; |
| 152 | } |
| 153 | else if (!strcmp(arg, "fetch")) |
| 154 | *mirror = MIRROR_FETCH; |
| 155 | else if (!strcmp(arg, "push")) |
| 156 | *mirror = MIRROR_PUSH; |
| 157 | else |
| 158 | return error(_("unknown --mirror argument: %s"), arg); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int check_remote_collision(struct remote *remote, void *data) |
| 163 | { |
| 164 | const char *name = data; |
| 165 | const char *p; |
| 166 | |
| 167 | if (skip_prefix(name, remote->name, &p) && *p == '/') |
| 168 | die(_("remote name '%s' is a subset of existing remote '%s'"), |
| 169 | name, remote->name); |
| 170 | if (skip_prefix(remote->name, name, &p) && *p == '/') |
| 171 | die(_("remote name '%s' is a superset of existing remote '%s'"), |
| 172 | name, remote->name); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int add(int argc, const char **argv, const char *prefix, |
| 178 | struct repository *repo UNUSED) |
| 179 | { |
| 180 | int fetch = 0, fetch_tags = TAGS_DEFAULT; |
| 181 | unsigned mirror = MIRROR_NONE; |
| 182 | struct string_list track = STRING_LIST_INIT_NODUP; |
| 183 | const char *master = NULL; |
| 184 | struct remote *remote; |
| 185 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
| 186 | const char *name, *url; |
| 187 | int result = 0; |
| 188 | |
| 189 | struct option options[] = { |
| 190 | OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")), |
| 191 | OPT_SET_INT(0, "tags", &fetch_tags, |
| 192 | N_("import all tags and associated objects when fetching\n" |
| 193 | "or do not fetch any tag at all (--no-tags)"), |
| 194 | TAGS_SET), |
| 195 | OPT_STRING_LIST('t', "track", &track, N_("branch"), |
| 196 | N_("branch(es) to track")), |
| 197 | OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")), |
| 198 | OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)", |
| 199 | N_("set up remote as a mirror to push to or fetch from"), |
| 200 | PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt), |
| 201 | OPT_END() |
| 202 | }; |
| 203 | |
| 204 | argc = parse_options(argc, argv, prefix, options, |
| 205 | builtin_remote_add_usage, 0); |
| 206 | |
| 207 | if (argc != 2) |
| 208 | usage_with_options(builtin_remote_add_usage, options); |
| 209 | |
| 210 | if (mirror && master) |
| 211 | die(_("specifying a master branch makes no sense with --mirror")); |
| 212 | if (mirror && !(mirror & MIRROR_FETCH) && track.nr) |
| 213 | die(_("specifying branches to track makes sense only with fetch mirrors")); |
| 214 | |
| 215 | name = argv[0]; |
| 216 | url = argv[1]; |
| 217 | |
| 218 | remote = remote_get(name); |
| 219 | if (remote_is_configured(remote, 1)) { |
| 220 | error(_("remote %s already exists."), name); |
| 221 | exit(3); |
| 222 | } |
| 223 | |
| 224 | if (!valid_remote_name(name)) |
| 225 | die(_("'%s' is not a valid remote name"), name); |
| 226 | |
| 227 | for_each_remote(check_remote_collision, (void *)name); |
| 228 | |
| 229 | strbuf_addf(&buf, "remote.%s.url", name); |
| 230 | repo_config_set(the_repository, buf.buf, url); |
| 231 | |
| 232 | if (!mirror || mirror & MIRROR_FETCH) { |
| 233 | strbuf_reset(&buf); |
| 234 | strbuf_addf(&buf, "remote.%s.fetch", name); |
| 235 | if (track.nr == 0) |
| 236 | string_list_append(&track, "*"); |
| 237 | for (size_t i = 0; i < track.nr; i++) { |
| 238 | add_branch(buf.buf, track.items[i].string, |
| 239 | name, mirror, &buf2); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | if (mirror & MIRROR_PUSH) { |
| 244 | strbuf_reset(&buf); |
| 245 | strbuf_addf(&buf, "remote.%s.mirror", name); |
| 246 | repo_config_set(the_repository, buf.buf, "true"); |
| 247 | } |
| 248 | |
| 249 | if (fetch_tags != TAGS_DEFAULT) { |
| 250 | strbuf_reset(&buf); |
| 251 | strbuf_addf(&buf, "remote.%s.tagOpt", name); |
| 252 | repo_config_set(the_repository, buf.buf, |
| 253 | fetch_tags == TAGS_SET ? "--tags" : "--no-tags"); |
| 254 | } |
| 255 | |
| 256 | if (fetch && fetch_remote(name)) { |
| 257 | result = 1; |
| 258 | goto out; |
| 259 | } |
| 260 | |
| 261 | if (master) { |
| 262 | strbuf_reset(&buf); |
| 263 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); |
| 264 | |
| 265 | strbuf_reset(&buf2); |
| 266 | strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); |
| 267 | |
| 268 | if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote add")) |
| 269 | result = error(_("Could not setup master '%s'"), master); |
| 270 | } |
| 271 | |
| 272 | out: |
| 273 | strbuf_release(&buf); |
| 274 | strbuf_release(&buf2); |
| 275 | string_list_clear(&track, 0); |
| 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | struct branch_info { |
| 281 | char *remote_name; |
| 282 | struct string_list merge; |
| 283 | enum rebase_type rebase; |
| 284 | char *push_remote_name; |
| 285 | }; |
| 286 | |
| 287 | static struct string_list branch_list = STRING_LIST_INIT_DUP; |
| 288 | |
| 289 | static const char *abbrev_ref(const char *name, const char *prefix) |
| 290 | { |
| 291 | skip_prefix(name, prefix, &name); |
| 292 | return name; |
| 293 | } |
| 294 | #define abbrev_branch(name) abbrev_ref((name), "refs/heads/") |
| 295 | |
| 296 | static int config_read_branches(const char *key, const char *value, |
| 297 | const struct config_context *ctx UNUSED, |
| 298 | void *data UNUSED) |
| 299 | { |
| 300 | const char *orig_key = key; |
| 301 | char *name; |
| 302 | struct string_list_item *item; |
| 303 | struct branch_info *info; |
| 304 | enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type; |
| 305 | size_t key_len; |
| 306 | |
| 307 | if (!starts_with(key, "branch.")) |
| 308 | return 0; |
| 309 | |
| 310 | key += strlen("branch."); |
| 311 | if (strip_suffix(key, ".remote", &key_len)) |
| 312 | type = REMOTE; |
| 313 | else if (strip_suffix(key, ".merge", &key_len)) |
| 314 | type = MERGE; |
| 315 | else if (strip_suffix(key, ".rebase", &key_len)) |
| 316 | type = REBASE; |
| 317 | else if (strip_suffix(key, ".pushremote", &key_len)) |
| 318 | type = PUSH_REMOTE; |
| 319 | else |
| 320 | return 0; |
| 321 | |
| 322 | name = xmemdupz(key, key_len); |
| 323 | item = string_list_insert(&branch_list, name); |
| 324 | |
| 325 | if (!item->util) |
| 326 | item->util = xcalloc(1, sizeof(struct branch_info)); |
| 327 | info = item->util; |
| 328 | switch (type) { |
| 329 | case REMOTE: |
| 330 | if (info->remote_name) |
| 331 | warning(_("more than one %s"), orig_key); |
| 332 | info->remote_name = xstrdup(value); |
| 333 | break; |
| 334 | case MERGE: { |
| 335 | const char *space = strchr(value, ' '); |
| 336 | value = abbrev_branch(value); |
| 337 | while (space) { |
| 338 | char *merge; |
| 339 | merge = xstrndup(value, space - value); |
| 340 | string_list_append(&info->merge, merge); |
| 341 | value = abbrev_branch(space + 1); |
| 342 | space = strchr(value, ' '); |
| 343 | } |
| 344 | string_list_append(&info->merge, xstrdup(value)); |
| 345 | break; |
| 346 | } |
| 347 | case REBASE: |
| 348 | /* |
| 349 | * Consider invalid values as false and check the |
| 350 | * truth value with >= REBASE_TRUE. |
| 351 | */ |
| 352 | info->rebase = rebase_parse_value(value); |
| 353 | if (info->rebase == REBASE_INVALID) |
| 354 | warning(_("unhandled branch.%s.rebase=%s; assuming " |
| 355 | "'true'"), name, value); |
| 356 | break; |
| 357 | case PUSH_REMOTE: |
| 358 | if (info->push_remote_name) |
| 359 | warning(_("more than one %s"), orig_key); |
| 360 | info->push_remote_name = xstrdup(value); |
| 361 | break; |
| 362 | default: |
| 363 | BUG("unexpected type=%d", type); |
| 364 | } |
| 365 | |
| 366 | free(name); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static void read_branches(void) |
| 371 | { |
| 372 | if (branch_list.nr) |
| 373 | return; |
| 374 | repo_config(the_repository, config_read_branches, NULL); |
| 375 | } |
| 376 | |
| 377 | struct ref_states { |
| 378 | struct remote *remote; |
| 379 | struct string_list new_refs, skipped, stale, tracked, heads, push; |
| 380 | int queried; |
| 381 | }; |
| 382 | |
| 383 | #define REF_STATES_INIT { \ |
| 384 | .new_refs = STRING_LIST_INIT_DUP, \ |
| 385 | .skipped = STRING_LIST_INIT_DUP, \ |
| 386 | .stale = STRING_LIST_INIT_DUP, \ |
| 387 | .tracked = STRING_LIST_INIT_DUP, \ |
| 388 | .heads = STRING_LIST_INIT_DUP, \ |
| 389 | .push = STRING_LIST_INIT_DUP, \ |
| 390 | } |
| 391 | |
| 392 | static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) |
| 393 | { |
| 394 | struct ref *fetch_map = NULL, **tail = &fetch_map; |
| 395 | struct ref *ref, *stale_refs; |
| 396 | int i; |
| 397 | |
| 398 | for (i = 0; i < states->remote->fetch.nr; i++) |
| 399 | if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1)) |
| 400 | die(_("Could not get fetch map for refspec %s"), |
| 401 | states->remote->fetch.items[i].raw); |
| 402 | |
| 403 | for (ref = fetch_map; ref; ref = ref->next) { |
| 404 | if (refname_matches_negative_refspec_item(ref->name, &states->remote->fetch)) |
| 405 | string_list_append(&states->skipped, abbrev_branch(ref->name)); |
| 406 | else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name)) |
| 407 | string_list_append(&states->new_refs, abbrev_branch(ref->name)); |
| 408 | else |
| 409 | string_list_append(&states->tracked, abbrev_branch(ref->name)); |
| 410 | } |
| 411 | stale_refs = get_stale_heads(&states->remote->fetch, fetch_map); |
| 412 | for (ref = stale_refs; ref; ref = ref->next) { |
| 413 | struct string_list_item *item = |
| 414 | string_list_append(&states->stale, abbrev_branch(ref->name)); |
| 415 | item->util = xstrdup(ref->name); |
| 416 | } |
| 417 | free_refs(stale_refs); |
| 418 | free_refs(fetch_map); |
| 419 | |
| 420 | string_list_sort(&states->new_refs); |
| 421 | string_list_sort(&states->skipped); |
| 422 | string_list_sort(&states->tracked); |
| 423 | string_list_sort(&states->stale); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | struct push_info { |
| 429 | char *dest; |
| 430 | int forced; |
| 431 | enum { |
| 432 | PUSH_STATUS_CREATE = 0, |
| 433 | PUSH_STATUS_DELETE, |
| 434 | PUSH_STATUS_UPTODATE, |
| 435 | PUSH_STATUS_FASTFORWARD, |
| 436 | PUSH_STATUS_OUTOFDATE, |
| 437 | PUSH_STATUS_NOTQUERIED |
| 438 | } status; |
| 439 | }; |
| 440 | |
| 441 | static int get_push_ref_states(const struct ref *remote_refs, |
| 442 | struct ref_states *states) |
| 443 | { |
| 444 | struct remote *remote = states->remote; |
| 445 | struct ref *ref, *local_refs, *push_map; |
| 446 | if (remote->mirror) |
| 447 | return 0; |
| 448 | |
| 449 | local_refs = get_local_heads(); |
| 450 | push_map = copy_ref_list(remote_refs); |
| 451 | |
| 452 | match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE); |
| 453 | |
| 454 | for (ref = push_map; ref; ref = ref->next) { |
| 455 | struct string_list_item *item; |
| 456 | struct push_info *info; |
| 457 | |
| 458 | if (!ref->peer_ref) |
| 459 | continue; |
| 460 | oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); |
| 461 | |
| 462 | item = string_list_append(&states->push, |
| 463 | abbrev_branch(ref->peer_ref->name)); |
| 464 | item->util = xcalloc(1, sizeof(struct push_info)); |
| 465 | info = item->util; |
| 466 | info->forced = ref->force; |
| 467 | info->dest = xstrdup(abbrev_branch(ref->name)); |
| 468 | |
| 469 | if (is_null_oid(&ref->new_oid)) { |
| 470 | info->status = PUSH_STATUS_DELETE; |
| 471 | } else if (oideq(&ref->old_oid, &ref->new_oid)) |
| 472 | info->status = PUSH_STATUS_UPTODATE; |
| 473 | else if (is_null_oid(&ref->old_oid)) |
| 474 | info->status = PUSH_STATUS_CREATE; |
| 475 | else if (odb_has_object(the_repository->objects, &ref->old_oid, |
| 476 | ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR) && |
| 477 | ref_newer(&ref->new_oid, &ref->old_oid)) |
| 478 | info->status = PUSH_STATUS_FASTFORWARD; |
| 479 | else |
| 480 | info->status = PUSH_STATUS_OUTOFDATE; |
| 481 | } |
| 482 | free_refs(local_refs); |
| 483 | free_refs(push_map); |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static int get_push_ref_states_noquery(struct ref_states *states) |
| 488 | { |
| 489 | int i; |
| 490 | struct remote *remote = states->remote; |
| 491 | struct string_list_item *item; |
| 492 | struct push_info *info; |
| 493 | |
| 494 | if (remote->mirror) |
| 495 | return 0; |
| 496 | |
| 497 | if (!remote->push.nr) { |
| 498 | item = string_list_append(&states->push, _("(matching)")); |
| 499 | info = item->util = xcalloc(1, sizeof(struct push_info)); |
| 500 | info->status = PUSH_STATUS_NOTQUERIED; |
| 501 | info->dest = xstrdup(item->string); |
| 502 | } |
| 503 | for (i = 0; i < remote->push.nr; i++) { |
| 504 | const struct refspec_item *spec = &remote->push.items[i]; |
| 505 | if (spec->matching) |
| 506 | item = string_list_append(&states->push, _("(matching)")); |
| 507 | else if (strlen(spec->src)) |
| 508 | item = string_list_append(&states->push, spec->src); |
| 509 | else |
| 510 | item = string_list_append(&states->push, _("(delete)")); |
| 511 | |
| 512 | info = item->util = xcalloc(1, sizeof(struct push_info)); |
| 513 | info->forced = spec->force; |
| 514 | info->status = PUSH_STATUS_NOTQUERIED; |
| 515 | info->dest = xstrdup(spec->dst ? spec->dst : item->string); |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int get_head_names(const struct ref *remote_refs, struct ref_states *states) |
| 521 | { |
| 522 | struct ref *ref, *matches; |
| 523 | struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; |
| 524 | struct refspec_item refspec = { |
| 525 | .force = 0, |
| 526 | .pattern = 1, |
| 527 | .src = (char *) "refs/heads/*", |
| 528 | .dst = (char *) "refs/heads/*", |
| 529 | }; |
| 530 | |
| 531 | get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); |
| 532 | matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), |
| 533 | fetch_map, REMOTE_GUESS_HEAD_ALL); |
| 534 | for (ref = matches; ref; ref = ref->next) |
| 535 | string_list_append(&states->heads, abbrev_branch(ref->name)); |
| 536 | |
| 537 | free_refs(fetch_map); |
| 538 | free_refs(matches); |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | struct known_remote { |
| 543 | struct known_remote *next; |
| 544 | struct remote *remote; |
| 545 | }; |
| 546 | |
| 547 | struct known_remotes { |
| 548 | struct remote *to_delete; |
| 549 | struct known_remote *list; |
| 550 | }; |
| 551 | |
| 552 | static int add_known_remote(struct remote *remote, void *cb_data) |
| 553 | { |
| 554 | struct known_remotes *all = cb_data; |
| 555 | struct known_remote *r; |
| 556 | |
| 557 | if (!strcmp(all->to_delete->name, remote->name)) |
| 558 | return 0; |
| 559 | |
| 560 | r = xmalloc(sizeof(*r)); |
| 561 | r->remote = remote; |
| 562 | r->next = all->list; |
| 563 | all->list = r; |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | struct branches_for_remote { |
| 568 | struct remote *remote; |
| 569 | struct string_list *branches, *skipped; |
| 570 | struct known_remotes *keep; |
| 571 | }; |
| 572 | |
| 573 | static int add_branch_for_removal(const struct reference *ref, void *cb_data) |
| 574 | { |
| 575 | struct branches_for_remote *branches = cb_data; |
| 576 | struct refspec_item refspec; |
| 577 | struct known_remote *kr; |
| 578 | |
| 579 | memset(&refspec, 0, sizeof(refspec)); |
| 580 | refspec.dst = (char *)ref->name; |
| 581 | if (remote_find_tracking(branches->remote, &refspec)) |
| 582 | return 0; |
| 583 | free(refspec.src); |
| 584 | |
| 585 | /* don't delete a branch if another remote also uses it */ |
| 586 | for (kr = branches->keep->list; kr; kr = kr->next) { |
| 587 | memset(&refspec, 0, sizeof(refspec)); |
| 588 | refspec.dst = (char *)ref->name; |
| 589 | if (!remote_find_tracking(kr->remote, &refspec)) { |
| 590 | free(refspec.src); |
| 591 | return 0; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* don't delete non-remote-tracking refs */ |
| 596 | if (!starts_with(ref->name, "refs/remotes/")) { |
| 597 | /* advise user how to delete local branches */ |
| 598 | if (starts_with(ref->name, "refs/heads/")) |
| 599 | string_list_append(branches->skipped, |
| 600 | abbrev_branch(ref->name)); |
| 601 | /* silently skip over other non-remote refs */ |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | string_list_append(branches->branches, ref->name); |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | struct rename_info { |
| 611 | const char *old_name; |
| 612 | const char *new_name; |
| 613 | struct ref_transaction *transaction; |
| 614 | struct progress *progress; |
| 615 | struct strbuf *err; |
| 616 | uint32_t progress_nr; |
| 617 | uint64_t index; |
| 618 | }; |
| 619 | |
| 620 | static void compute_renamed_ref(struct rename_info *rename, |
| 621 | const char *refname, |
| 622 | struct strbuf *out) |
| 623 | { |
| 624 | strbuf_reset(out); |
| 625 | strbuf_addstr(out, refname); |
| 626 | strbuf_splice(out, strlen("refs/remotes/"), strlen(rename->old_name), |
| 627 | rename->new_name, strlen(rename->new_name)); |
| 628 | } |
| 629 | |
| 630 | static int rename_one_reflog_entry(const char *old_refname, |
| 631 | struct object_id *old_oid, |
| 632 | struct object_id *new_oid, |
| 633 | const char *committer, |
| 634 | timestamp_t timestamp, int tz, |
| 635 | const char *msg, void *cb_data) |
| 636 | { |
| 637 | struct rename_info *rename = cb_data; |
| 638 | struct strbuf new_refname = STRBUF_INIT; |
| 639 | struct strbuf identity = STRBUF_INIT; |
| 640 | struct strbuf name = STRBUF_INIT; |
| 641 | struct strbuf mail = STRBUF_INIT; |
| 642 | struct ident_split ident; |
| 643 | const char *date; |
| 644 | int error; |
| 645 | |
| 646 | compute_renamed_ref(rename, old_refname, &new_refname); |
| 647 | |
| 648 | if (split_ident_line(&ident, committer, strlen(committer)) < 0) { |
| 649 | error = -1; |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | strbuf_add(&name, ident.name_begin, ident.name_end - ident.name_begin); |
| 654 | strbuf_add(&mail, ident.mail_begin, ident.mail_end - ident.mail_begin); |
| 655 | |
| 656 | date = show_date(timestamp, tz, DATE_MODE(NORMAL)); |
| 657 | strbuf_addstr(&identity, fmt_ident(name.buf, mail.buf, |
| 658 | WANT_BLANK_IDENT, date, 0)); |
| 659 | |
| 660 | error = ref_transaction_update_reflog(rename->transaction, new_refname.buf, |
| 661 | new_oid, old_oid, identity.buf, msg, |
| 662 | rename->index++, rename->err); |
| 663 | |
| 664 | out: |
| 665 | strbuf_release(&new_refname); |
| 666 | strbuf_release(&identity); |
| 667 | strbuf_release(&name); |
| 668 | strbuf_release(&mail); |
| 669 | return error; |
| 670 | } |
| 671 | |
| 672 | static int rename_one_reflog(const char *old_refname, |
| 673 | const struct object_id *old_oid, |
| 674 | struct rename_info *rename) |
| 675 | { |
| 676 | struct strbuf new_refname = STRBUF_INIT; |
| 677 | struct strbuf message = STRBUF_INIT; |
| 678 | int error; |
| 679 | |
| 680 | if (!refs_reflog_exists(get_main_ref_store(the_repository), old_refname)) |
| 681 | return 0; |
| 682 | |
| 683 | error = refs_for_each_reflog_ent(get_main_ref_store(the_repository), |
| 684 | old_refname, rename_one_reflog_entry, rename); |
| 685 | if (error < 0) |
| 686 | goto out; |
| 687 | |
| 688 | compute_renamed_ref(rename, old_refname, &new_refname); |
| 689 | |
| 690 | /* |
| 691 | * Manually write the reflog entry for the now-renamed ref. We cannot |
| 692 | * rely on `rename_one_ref()` to do this for us as that would screw |
| 693 | * over order in which reflog entries are being written. |
| 694 | * |
| 695 | * Furthermore, we only append the entry in case the reference |
| 696 | * resolves. Missing references shouldn't have reflogs anyway. |
| 697 | */ |
| 698 | strbuf_addf(&message, "remote: renamed %s to %s", old_refname, |
| 699 | new_refname.buf); |
| 700 | |
| 701 | error = ref_transaction_update_reflog(rename->transaction, new_refname.buf, |
| 702 | old_oid, old_oid, git_committer_info(0), |
| 703 | message.buf, rename->index++, rename->err); |
| 704 | if (error < 0) |
| 705 | return error; |
| 706 | |
| 707 | out: |
| 708 | strbuf_release(&new_refname); |
| 709 | strbuf_release(&message); |
| 710 | return error; |
| 711 | } |
| 712 | |
| 713 | static int rename_one_ref(const struct reference *ref, void *cb_data) |
| 714 | { |
| 715 | struct strbuf new_referent = STRBUF_INIT; |
| 716 | struct strbuf new_refname = STRBUF_INIT; |
| 717 | struct rename_info *rename = cb_data; |
| 718 | const struct object_id *oid = ref->oid; |
| 719 | const char *referent = ref->target; |
| 720 | int error; |
| 721 | |
| 722 | compute_renamed_ref(rename, ref->name, &new_refname); |
| 723 | |
| 724 | if (ref->flags & REF_ISSYMREF) { |
| 725 | /* |
| 726 | * Stupidly enough `referent` is not pointing to the immediate |
| 727 | * target of a symref, but it's the recursively resolved value. |
| 728 | * So symrefs pointing to symrefs would be misresolved, and |
| 729 | * unborn symrefs don't have any value for the `referent` at all. |
| 730 | */ |
| 731 | referent = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), |
| 732 | ref->name, RESOLVE_REF_NO_RECURSE, |
| 733 | NULL, NULL); |
| 734 | compute_renamed_ref(rename, referent, &new_referent); |
| 735 | oid = NULL; |
| 736 | } |
| 737 | |
| 738 | error = ref_transaction_delete(rename->transaction, ref->name, |
| 739 | oid, referent, REF_NO_DEREF, NULL, rename->err); |
| 740 | if (error < 0) |
| 741 | goto out; |
| 742 | |
| 743 | error = ref_transaction_update(rename->transaction, new_refname.buf, oid, null_oid(the_hash_algo), |
| 744 | (ref->flags & REF_ISSYMREF) ? new_referent.buf : NULL, NULL, |
| 745 | REF_SKIP_CREATE_REFLOG | REF_NO_DEREF | REF_SKIP_OID_VERIFICATION, |
| 746 | NULL, rename->err); |
| 747 | if (error < 0) |
| 748 | goto out; |
| 749 | |
| 750 | error = rename_one_reflog(ref->name, oid, rename); |
| 751 | if (error < 0) |
| 752 | goto out; |
| 753 | |
| 754 | display_progress(rename->progress, ++rename->progress_nr); |
| 755 | |
| 756 | out: |
| 757 | strbuf_release(&new_referent); |
| 758 | strbuf_release(&new_refname); |
| 759 | return error; |
| 760 | } |
| 761 | |
| 762 | static int migrate_file(struct remote *remote) |
| 763 | { |
| 764 | struct strbuf buf = STRBUF_INIT; |
| 765 | |
| 766 | strbuf_addf(&buf, "remote.%s.url", remote->name); |
| 767 | for (size_t i = 0; i < remote->url.nr; i++) |
| 768 | repo_config_set_multivar(the_repository, buf.buf, remote->url.v[i], "^$", 0); |
| 769 | strbuf_reset(&buf); |
| 770 | strbuf_addf(&buf, "remote.%s.push", remote->name); |
| 771 | for (int i = 0; i < remote->push.nr; i++) |
| 772 | repo_config_set_multivar(the_repository, buf.buf, remote->push.items[i].raw, "^$", 0); |
| 773 | strbuf_reset(&buf); |
| 774 | strbuf_addf(&buf, "remote.%s.fetch", remote->name); |
| 775 | for (int i = 0; i < remote->fetch.nr; i++) |
| 776 | repo_config_set_multivar(the_repository, buf.buf, remote->fetch.items[i].raw, "^$", 0); |
| 777 | #ifndef WITH_BREAKING_CHANGES |
| 778 | if (remote->origin == REMOTE_REMOTES) |
| 779 | unlink_or_warn(repo_git_path_replace(the_repository, &buf, |
| 780 | "remotes/%s", remote->name)); |
| 781 | else if (remote->origin == REMOTE_BRANCHES) |
| 782 | unlink_or_warn(repo_git_path_replace(the_repository, &buf, |
| 783 | "branches/%s", remote->name)); |
| 784 | #endif /* WITH_BREAKING_CHANGES */ |
| 785 | strbuf_release(&buf); |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | struct push_default_info |
| 791 | { |
| 792 | const char *old_name; |
| 793 | enum config_scope scope; |
| 794 | struct strbuf origin; |
| 795 | int linenr; |
| 796 | }; |
| 797 | |
| 798 | static int config_read_push_default(const char *key, const char *value, |
| 799 | const struct config_context *ctx, void *cb) |
| 800 | { |
| 801 | const struct key_value_info *kvi = ctx->kvi; |
| 802 | |
| 803 | struct push_default_info* info = cb; |
| 804 | if (strcmp(key, "remote.pushdefault") || |
| 805 | !value || strcmp(value, info->old_name)) |
| 806 | return 0; |
| 807 | |
| 808 | info->scope = kvi->scope; |
| 809 | strbuf_reset(&info->origin); |
| 810 | strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type)); |
| 811 | info->linenr = kvi->linenr; |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static void handle_push_default(const char* old_name, const char* new_name) |
| 817 | { |
| 818 | struct push_default_info push_default = { |
| 819 | .old_name = old_name, |
| 820 | .scope = CONFIG_SCOPE_UNKNOWN, |
| 821 | .origin = STRBUF_INIT, |
| 822 | .linenr = -1, |
| 823 | }; |
| 824 | repo_config(the_repository, config_read_push_default, &push_default); |
| 825 | if (push_default.scope >= CONFIG_SCOPE_COMMAND) |
| 826 | ; /* pass */ |
| 827 | else if (push_default.scope >= CONFIG_SCOPE_LOCAL) { |
| 828 | int result = repo_config_set_gently(the_repository, "remote.pushDefault", |
| 829 | new_name); |
| 830 | if (new_name && result && result != CONFIG_NOTHING_SET) |
| 831 | die(_("could not set '%s'"), "remote.pushDefault"); |
| 832 | else if (!new_name && result && result != CONFIG_NOTHING_SET) |
| 833 | die(_("could not unset '%s'"), "remote.pushDefault"); |
| 834 | } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) { |
| 835 | /* warn */ |
| 836 | warning(_("The %s configuration remote.pushDefault in:\n" |
| 837 | "\t%s:%d\n" |
| 838 | "now names the non-existent remote '%s'"), |
| 839 | config_scope_name(push_default.scope), |
| 840 | push_default.origin.buf, push_default.linenr, |
| 841 | old_name); |
| 842 | } |
| 843 | |
| 844 | strbuf_release(&push_default.origin); |
| 845 | } |
| 846 | |
| 847 | static const char conflicting_remote_refs_advice[] = N_( |
| 848 | "The remote you are trying to rename has conflicting references in the\n" |
| 849 | "new target refspec. This is most likely caused by you trying to nest\n" |
| 850 | "a remote into itself, e.g. by renaming 'parent' into 'parent/child'\n" |
| 851 | "or by unnesting a remote, e.g. the other way round.\n" |
| 852 | "\n" |
| 853 | "If that is the case, you can address this by first renaming the\n" |
| 854 | "remote to a different name.\n"); |
| 855 | |
| 856 | static int mv(int argc, const char **argv, const char *prefix, |
| 857 | struct repository *repo UNUSED) |
| 858 | { |
| 859 | int show_progress = isatty(2); |
| 860 | struct option options[] = { |
| 861 | OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")), |
| 862 | OPT_END() |
| 863 | }; |
| 864 | struct remote *oldremote, *newremote; |
| 865 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT, |
| 866 | old_remote_context = STRBUF_INIT, err = STRBUF_INIT; |
| 867 | struct rename_info rename = { |
| 868 | .err = &err, |
| 869 | }; |
| 870 | int refspecs_need_update = 0; |
| 871 | int result = 0; |
| 872 | |
| 873 | argc = parse_options(argc, argv, prefix, options, |
| 874 | builtin_remote_rename_usage, 0); |
| 875 | |
| 876 | if (argc != 2) |
| 877 | usage_with_options(builtin_remote_rename_usage, options); |
| 878 | |
| 879 | rename.old_name = argv[0]; |
| 880 | rename.new_name = argv[1]; |
| 881 | |
| 882 | oldremote = remote_get(rename.old_name); |
| 883 | if (!remote_is_configured(oldremote, 1)) { |
| 884 | error(_("No such remote: '%s'"), rename.old_name); |
| 885 | exit(2); |
| 886 | } |
| 887 | |
| 888 | if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG) |
| 889 | return migrate_file(oldremote); |
| 890 | |
| 891 | newremote = remote_get(rename.new_name); |
| 892 | if (remote_is_configured(newremote, 1)) { |
| 893 | error(_("remote %s already exists."), rename.new_name); |
| 894 | exit(3); |
| 895 | } |
| 896 | |
| 897 | if (!valid_remote_name(rename.new_name)) |
| 898 | die(_("'%s' is not a valid remote name"), rename.new_name); |
| 899 | |
| 900 | strbuf_addf(&buf, "remote.%s", rename.old_name); |
| 901 | strbuf_addf(&buf2, "remote.%s", rename.new_name); |
| 902 | if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1) { |
| 903 | result = error(_("Could not rename config section '%s' to '%s'"), |
| 904 | buf.buf, buf2.buf); |
| 905 | goto out; |
| 906 | } |
| 907 | |
| 908 | strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name); |
| 909 | |
| 910 | for (int i = 0; i < oldremote->fetch.nr && !refspecs_need_update; i++) |
| 911 | refspecs_need_update = !!strstr(oldremote->fetch.items[i].raw, |
| 912 | old_remote_context.buf); |
| 913 | |
| 914 | if (refspecs_need_update) { |
| 915 | struct refs_for_each_ref_options opts = { |
| 916 | .flags = REFS_FOR_EACH_INCLUDE_BROKEN, |
| 917 | }; |
| 918 | rename.transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
| 919 | 0, &err); |
| 920 | if (!rename.transaction) |
| 921 | goto out; |
| 922 | |
| 923 | if (show_progress) |
| 924 | rename.progress = start_delayed_progress(the_repository, |
| 925 | _("Renaming remote references"), 0); |
| 926 | |
| 927 | strbuf_reset(&buf); |
| 928 | strbuf_addf(&buf, "refs/remotes/%s/", rename.old_name); |
| 929 | opts.prefix = buf.buf; |
| 930 | |
| 931 | result = refs_for_each_ref_ext(get_main_ref_store(the_repository), |
| 932 | rename_one_ref, &rename, &opts); |
| 933 | if (result < 0) |
| 934 | die(_("queueing remote ref renames failed: %s"), rename.err->buf); |
| 935 | |
| 936 | result = ref_transaction_prepare(rename.transaction, &err); |
| 937 | if (result < 0) { |
| 938 | error("renaming remote references failed: %s", err.buf); |
| 939 | if (result == REF_TRANSACTION_ERROR_NAME_CONFLICT) |
| 940 | advise(conflicting_remote_refs_advice); |
| 941 | die(NULL); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if (oldremote->fetch.nr) { |
| 946 | strbuf_reset(&buf); |
| 947 | strbuf_addf(&buf, "remote.%s.fetch", rename.new_name); |
| 948 | repo_config_set_multivar(the_repository, buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); |
| 949 | for (int i = 0; i < oldremote->fetch.nr; i++) { |
| 950 | char *ptr; |
| 951 | |
| 952 | strbuf_reset(&buf2); |
| 953 | strbuf_addstr(&buf2, oldremote->fetch.items[i].raw); |
| 954 | ptr = strstr(buf2.buf, old_remote_context.buf); |
| 955 | if (ptr) { |
| 956 | strbuf_splice(&buf2, |
| 957 | ptr-buf2.buf + strlen(":refs/remotes/"), |
| 958 | strlen(rename.old_name), rename.new_name, |
| 959 | strlen(rename.new_name)); |
| 960 | } else |
| 961 | warning(_("Not updating non-default fetch refspec\n" |
| 962 | "\t%s\n" |
| 963 | "\tPlease update the configuration manually if necessary."), |
| 964 | buf2.buf); |
| 965 | |
| 966 | repo_config_set_multivar(the_repository, buf.buf, buf2.buf, "^$", 0); |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | read_branches(); |
| 971 | for (size_t i = 0; i < branch_list.nr; i++) { |
| 972 | struct string_list_item *item = branch_list.items + i; |
| 973 | struct branch_info *info = item->util; |
| 974 | if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) { |
| 975 | strbuf_reset(&buf); |
| 976 | strbuf_addf(&buf, "branch.%s.remote", item->string); |
| 977 | repo_config_set(the_repository, buf.buf, rename.new_name); |
| 978 | } |
| 979 | if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) { |
| 980 | strbuf_reset(&buf); |
| 981 | strbuf_addf(&buf, "branch.%s.pushRemote", item->string); |
| 982 | repo_config_set(the_repository, buf.buf, rename.new_name); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | if (refspecs_need_update) { |
| 987 | result = ref_transaction_commit(rename.transaction, &err); |
| 988 | if (result < 0) |
| 989 | die(_("renaming remote refs failed: %s"), rename.err->buf); |
| 990 | |
| 991 | stop_progress(&rename.progress); |
| 992 | |
| 993 | handle_push_default(rename.old_name, rename.new_name); |
| 994 | } |
| 995 | |
| 996 | out: |
| 997 | ref_transaction_free(rename.transaction); |
| 998 | strbuf_release(&old_remote_context); |
| 999 | strbuf_release(&buf); |
| 1000 | strbuf_release(&buf2); |
| 1001 | strbuf_release(&buf3); |
| 1002 | strbuf_release(&err); |
| 1003 | return result; |
| 1004 | } |
| 1005 | |
| 1006 | static int rm(int argc, const char **argv, const char *prefix, |
| 1007 | struct repository *repo UNUSED) |
| 1008 | { |
| 1009 | struct option options[] = { |
| 1010 | OPT_END() |
| 1011 | }; |
| 1012 | struct remote *remote; |
| 1013 | struct strbuf buf = STRBUF_INIT; |
| 1014 | struct known_remotes known_remotes = { NULL, NULL }; |
| 1015 | struct string_list branches = STRING_LIST_INIT_DUP; |
| 1016 | struct string_list skipped = STRING_LIST_INIT_DUP; |
| 1017 | struct branches_for_remote cb_data; |
| 1018 | int result; |
| 1019 | |
| 1020 | memset(&cb_data, 0, sizeof(cb_data)); |
| 1021 | cb_data.branches = &branches; |
| 1022 | cb_data.skipped = &skipped; |
| 1023 | cb_data.keep = &known_remotes; |
| 1024 | |
| 1025 | argc = parse_options(argc, argv, prefix, options, |
| 1026 | builtin_remote_rm_usage, 0); |
| 1027 | if (argc != 1) |
| 1028 | usage_with_options(builtin_remote_rm_usage, options); |
| 1029 | |
| 1030 | remote = remote_get(argv[0]); |
| 1031 | if (!remote_is_configured(remote, 1)) { |
| 1032 | error(_("No such remote: '%s'"), argv[0]); |
| 1033 | exit(2); |
| 1034 | } |
| 1035 | |
| 1036 | known_remotes.to_delete = remote; |
| 1037 | for_each_remote(add_known_remote, &known_remotes); |
| 1038 | |
| 1039 | read_branches(); |
| 1040 | for (size_t i = 0; i < branch_list.nr; i++) { |
| 1041 | struct string_list_item *item = branch_list.items + i; |
| 1042 | struct branch_info *info = item->util; |
| 1043 | if (info->remote_name && !strcmp(info->remote_name, remote->name)) { |
| 1044 | const char *keys[] = { "remote", "merge", NULL }, **k; |
| 1045 | for (k = keys; *k; k++) { |
| 1046 | strbuf_reset(&buf); |
| 1047 | strbuf_addf(&buf, "branch.%s.%s", |
| 1048 | item->string, *k); |
| 1049 | result = repo_config_set_gently(the_repository, buf.buf, NULL); |
| 1050 | if (result && result != CONFIG_NOTHING_SET) |
| 1051 | die(_("could not unset '%s'"), buf.buf); |
| 1052 | } |
| 1053 | } |
| 1054 | if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) { |
| 1055 | strbuf_reset(&buf); |
| 1056 | strbuf_addf(&buf, "branch.%s.pushremote", item->string); |
| 1057 | result = repo_config_set_gently(the_repository, buf.buf, NULL); |
| 1058 | if (result && result != CONFIG_NOTHING_SET) |
| 1059 | die(_("could not unset '%s'"), buf.buf); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * We cannot just pass a function to for_each_ref() which deletes |
| 1065 | * the branches one by one, since for_each_ref() relies on cached |
| 1066 | * refs, which are invalidated when deleting a branch. |
| 1067 | */ |
| 1068 | cb_data.remote = remote; |
| 1069 | result = refs_for_each_ref(get_main_ref_store(the_repository), |
| 1070 | add_branch_for_removal, &cb_data); |
| 1071 | strbuf_release(&buf); |
| 1072 | |
| 1073 | if (!result) |
| 1074 | result = refs_delete_refs(get_main_ref_store(the_repository), |
| 1075 | "remote: remove", &branches, |
| 1076 | REF_NO_DEREF); |
| 1077 | string_list_clear(&branches, 0); |
| 1078 | |
| 1079 | if (skipped.nr) { |
| 1080 | fprintf_ln(stderr, |
| 1081 | Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n" |
| 1082 | "to delete it, use:", |
| 1083 | "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n" |
| 1084 | "to delete them, use:", |
| 1085 | skipped.nr)); |
| 1086 | for (size_t i = 0; i < skipped.nr; i++) |
| 1087 | fprintf(stderr, " git branch -d %s\n", |
| 1088 | skipped.items[i].string); |
| 1089 | } |
| 1090 | string_list_clear(&skipped, 0); |
| 1091 | |
| 1092 | if (!result) { |
| 1093 | strbuf_addf(&buf, "remote.%s", remote->name); |
| 1094 | if (repo_config_rename_section(the_repository, buf.buf, NULL) < 1) { |
| 1095 | result = error(_("Could not remove config section '%s'"), buf.buf); |
| 1096 | goto out; |
| 1097 | } |
| 1098 | |
| 1099 | handle_push_default(remote->name, NULL); |
| 1100 | } |
| 1101 | |
| 1102 | out: |
| 1103 | for (struct known_remote *r = known_remotes.list; r;) { |
| 1104 | struct known_remote *next = r->next; |
| 1105 | free(r); |
| 1106 | r = next; |
| 1107 | } |
| 1108 | strbuf_release(&buf); |
| 1109 | return result; |
| 1110 | } |
| 1111 | |
| 1112 | static void clear_push_info(void *util, const char *string UNUSED) |
| 1113 | { |
| 1114 | struct push_info *info = util; |
| 1115 | free(info->dest); |
| 1116 | free(info); |
| 1117 | } |
| 1118 | |
| 1119 | static void free_remote_ref_states(struct ref_states *states) |
| 1120 | { |
| 1121 | string_list_clear(&states->new_refs, 0); |
| 1122 | string_list_clear(&states->skipped, 0); |
| 1123 | string_list_clear(&states->stale, 1); |
| 1124 | string_list_clear(&states->tracked, 0); |
| 1125 | string_list_clear(&states->heads, 0); |
| 1126 | string_list_clear_func(&states->push, clear_push_info); |
| 1127 | } |
| 1128 | |
| 1129 | static int append_ref_to_tracked_list(const struct reference *ref, void *cb_data) |
| 1130 | { |
| 1131 | struct ref_states *states = cb_data; |
| 1132 | struct refspec_item refspec; |
| 1133 | |
| 1134 | if (ref->flags & REF_ISSYMREF) |
| 1135 | return 0; |
| 1136 | |
| 1137 | memset(&refspec, 0, sizeof(refspec)); |
| 1138 | refspec.dst = (char *)ref->name; |
| 1139 | if (!remote_find_tracking(states->remote, &refspec)) { |
| 1140 | string_list_append(&states->tracked, abbrev_branch(refspec.src)); |
| 1141 | free(refspec.src); |
| 1142 | } |
| 1143 | |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static int get_remote_ref_states(const char *name, |
| 1148 | struct ref_states *states, |
| 1149 | int query) |
| 1150 | { |
| 1151 | states->remote = remote_get(name); |
| 1152 | if (!states->remote) |
| 1153 | return error(_("No such remote: '%s'"), name); |
| 1154 | |
| 1155 | read_branches(); |
| 1156 | |
| 1157 | if (query) { |
| 1158 | struct transport *transport; |
| 1159 | const struct ref *remote_refs; |
| 1160 | |
| 1161 | transport = transport_get(states->remote, states->remote->url.v[0]); |
| 1162 | remote_refs = transport_get_remote_refs(transport, NULL); |
| 1163 | |
| 1164 | states->queried = 1; |
| 1165 | if (query & GET_REF_STATES) |
| 1166 | get_ref_states(remote_refs, states); |
| 1167 | if (query & GET_HEAD_NAMES) |
| 1168 | get_head_names(remote_refs, states); |
| 1169 | if (query & GET_PUSH_REF_STATES) |
| 1170 | get_push_ref_states(remote_refs, states); |
| 1171 | transport_disconnect(transport); |
| 1172 | } else { |
| 1173 | refs_for_each_ref(get_main_ref_store(the_repository), |
| 1174 | append_ref_to_tracked_list, states); |
| 1175 | string_list_sort(&states->tracked); |
| 1176 | get_push_ref_states_noquery(states); |
| 1177 | } |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | struct show_info { |
| 1183 | struct string_list list; |
| 1184 | struct ref_states states; |
| 1185 | int width, width2; |
| 1186 | int any_rebase; |
| 1187 | }; |
| 1188 | |
| 1189 | #define SHOW_INFO_INIT { \ |
| 1190 | .list = STRING_LIST_INIT_DUP, \ |
| 1191 | .states = REF_STATES_INIT, \ |
| 1192 | } |
| 1193 | |
| 1194 | static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) |
| 1195 | { |
| 1196 | struct show_info *info = cb_data; |
| 1197 | int n = strlen(item->string); |
| 1198 | if (n > info->width) |
| 1199 | info->width = n; |
| 1200 | string_list_insert(&info->list, item->string); |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static int show_remote_info_item(struct string_list_item *item, void *cb_data) |
| 1205 | { |
| 1206 | struct show_info *info = cb_data; |
| 1207 | struct ref_states *states = &info->states; |
| 1208 | const char *name = item->string; |
| 1209 | |
| 1210 | if (states->queried) { |
| 1211 | const char *fmt = "%s"; |
| 1212 | const char *arg = ""; |
| 1213 | if (string_list_has_string(&states->new_refs, name)) { |
| 1214 | fmt = _(" new (next fetch will store in remotes/%s)"); |
| 1215 | arg = states->remote->name; |
| 1216 | } else if (string_list_has_string(&states->tracked, name)) |
| 1217 | arg = _(" tracked"); |
| 1218 | else if (string_list_has_string(&states->skipped, name)) |
| 1219 | arg = _(" skipped"); |
| 1220 | else if (string_list_has_string(&states->stale, name)) |
| 1221 | arg = _(" stale (use 'git remote prune' to remove)"); |
| 1222 | else |
| 1223 | arg = _(" ???"); |
| 1224 | printf(" %-*s", info->width, name); |
| 1225 | printf(fmt, arg); |
| 1226 | printf("\n"); |
| 1227 | } else |
| 1228 | printf(" %s\n", name); |
| 1229 | |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) |
| 1234 | { |
| 1235 | struct show_info *show_info = cb_data; |
| 1236 | struct ref_states *states = &show_info->states; |
| 1237 | struct branch_info *branch_info = branch_item->util; |
| 1238 | struct string_list_item *item; |
| 1239 | int n; |
| 1240 | |
| 1241 | if (!branch_info->merge.nr || !branch_info->remote_name || |
| 1242 | strcmp(states->remote->name, branch_info->remote_name)) |
| 1243 | return 0; |
| 1244 | if ((n = strlen(branch_item->string)) > show_info->width) |
| 1245 | show_info->width = n; |
| 1246 | if (branch_info->rebase >= REBASE_TRUE) |
| 1247 | show_info->any_rebase = 1; |
| 1248 | |
| 1249 | item = string_list_insert(&show_info->list, branch_item->string); |
| 1250 | item->util = branch_info; |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | static int show_local_info_item(struct string_list_item *item, void *cb_data) |
| 1256 | { |
| 1257 | struct show_info *show_info = cb_data; |
| 1258 | struct branch_info *branch_info = item->util; |
| 1259 | struct string_list *merge = &branch_info->merge; |
| 1260 | int width = show_info->width + 4; |
| 1261 | |
| 1262 | if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) { |
| 1263 | error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"), |
| 1264 | item->string); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | printf(" %-*s ", show_info->width, item->string); |
| 1269 | if (branch_info->rebase >= REBASE_TRUE) { |
| 1270 | const char *msg; |
| 1271 | if (branch_info->rebase == REBASE_INTERACTIVE) |
| 1272 | msg = _("rebases interactively onto remote %s"); |
| 1273 | else if (branch_info->rebase == REBASE_MERGES) |
| 1274 | msg = _("rebases interactively (with merges) onto " |
| 1275 | "remote %s"); |
| 1276 | else |
| 1277 | msg = _("rebases onto remote %s"); |
| 1278 | printf_ln(msg, merge->items[0].string); |
| 1279 | return 0; |
| 1280 | } else if (show_info->any_rebase) { |
| 1281 | printf_ln(_(" merges with remote %s"), merge->items[0].string); |
| 1282 | width++; |
| 1283 | } else { |
| 1284 | printf_ln(_("merges with remote %s"), merge->items[0].string); |
| 1285 | } |
| 1286 | for (size_t i = 1; i < merge->nr; i++) |
| 1287 | printf(_("%-*s and with remote %s\n"), width, "", |
| 1288 | merge->items[i].string); |
| 1289 | |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data) |
| 1294 | { |
| 1295 | struct show_info *show_info = cb_data; |
| 1296 | struct push_info *push_info = push_item->util; |
| 1297 | struct string_list_item *item; |
| 1298 | int n; |
| 1299 | if ((n = strlen(push_item->string)) > show_info->width) |
| 1300 | show_info->width = n; |
| 1301 | if ((n = strlen(push_info->dest)) > show_info->width2) |
| 1302 | show_info->width2 = n; |
| 1303 | item = string_list_append(&show_info->list, push_item->string); |
| 1304 | item->util = push_item->util; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * Sorting comparison for a string list that has push_info |
| 1310 | * structs in its util field |
| 1311 | */ |
| 1312 | static int cmp_string_with_push(const void *va, const void *vb) |
| 1313 | { |
| 1314 | const struct string_list_item *a = va; |
| 1315 | const struct string_list_item *b = vb; |
| 1316 | const struct push_info *a_push = a->util; |
| 1317 | const struct push_info *b_push = b->util; |
| 1318 | int cmp = strcmp(a->string, b->string); |
| 1319 | return cmp ? cmp : strcmp(a_push->dest, b_push->dest); |
| 1320 | } |
| 1321 | |
| 1322 | static int show_push_info_item(struct string_list_item *item, void *cb_data) |
| 1323 | { |
| 1324 | struct show_info *show_info = cb_data; |
| 1325 | struct push_info *push_info = item->util; |
| 1326 | const char *src = item->string, *status = NULL; |
| 1327 | |
| 1328 | switch (push_info->status) { |
| 1329 | case PUSH_STATUS_CREATE: |
| 1330 | status = _("create"); |
| 1331 | break; |
| 1332 | case PUSH_STATUS_DELETE: |
| 1333 | status = _("delete"); |
| 1334 | src = _("(none)"); |
| 1335 | break; |
| 1336 | case PUSH_STATUS_UPTODATE: |
| 1337 | status = _("up to date"); |
| 1338 | break; |
| 1339 | case PUSH_STATUS_FASTFORWARD: |
| 1340 | status = _("fast-forwardable"); |
| 1341 | break; |
| 1342 | case PUSH_STATUS_OUTOFDATE: |
| 1343 | status = _("local out of date"); |
| 1344 | break; |
| 1345 | case PUSH_STATUS_NOTQUERIED: |
| 1346 | break; |
| 1347 | } |
| 1348 | if (status) { |
| 1349 | if (push_info->forced) |
| 1350 | printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src, |
| 1351 | show_info->width2, push_info->dest, status); |
| 1352 | else |
| 1353 | printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src, |
| 1354 | show_info->width2, push_info->dest, status); |
| 1355 | } else { |
| 1356 | if (push_info->forced) |
| 1357 | printf_ln(_(" %-*s forces to %s"), show_info->width, src, |
| 1358 | push_info->dest); |
| 1359 | else |
| 1360 | printf_ln(_(" %-*s pushes to %s"), show_info->width, src, |
| 1361 | push_info->dest); |
| 1362 | } |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
| 1366 | static int get_one_entry(struct remote *remote, void *priv) |
| 1367 | { |
| 1368 | struct string_list *list = priv; |
| 1369 | struct strbuf remote_info_buf = STRBUF_INIT; |
| 1370 | struct strvec *url; |
| 1371 | |
| 1372 | if (remote->url.nr > 0) { |
| 1373 | struct strbuf promisor_config = STRBUF_INIT; |
| 1374 | const char *partial_clone_filter = NULL; |
| 1375 | |
| 1376 | strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name); |
| 1377 | strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url.v[0]); |
| 1378 | if (!repo_config_get_string_tmp(the_repository, promisor_config.buf, &partial_clone_filter)) |
| 1379 | strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter); |
| 1380 | |
| 1381 | strbuf_release(&promisor_config); |
| 1382 | string_list_append(list, remote->name)->util = |
| 1383 | strbuf_detach(&remote_info_buf, NULL); |
| 1384 | } else |
| 1385 | string_list_append(list, remote->name)->util = NULL; |
| 1386 | url = push_url_of_remote(remote); |
| 1387 | for (size_t i = 0; i < url->nr; i++) { |
| 1388 | strbuf_addf(&remote_info_buf, "%s (push)", url->v[i]); |
| 1389 | string_list_append(list, remote->name)->util = |
| 1390 | strbuf_detach(&remote_info_buf, NULL); |
| 1391 | } |
| 1392 | |
| 1393 | return 0; |
| 1394 | } |
| 1395 | |
| 1396 | static int show_all(void) |
| 1397 | { |
| 1398 | struct string_list list = STRING_LIST_INIT_DUP; |
| 1399 | int result; |
| 1400 | |
| 1401 | result = for_each_remote(get_one_entry, &list); |
| 1402 | |
| 1403 | if (!result) { |
| 1404 | string_list_sort(&list); |
| 1405 | for (size_t i = 0; i < list.nr; i++) { |
| 1406 | struct string_list_item *item = list.items + i; |
| 1407 | if (verbose) |
| 1408 | printf("%s\t%s\n", item->string, |
| 1409 | item->util ? (const char *)item->util : ""); |
| 1410 | else { |
| 1411 | if (i && !strcmp((item - 1)->string, item->string)) |
| 1412 | continue; |
| 1413 | printf("%s\n", item->string); |
| 1414 | } |
| 1415 | } |
| 1416 | } |
| 1417 | string_list_clear(&list, 1); |
| 1418 | return result; |
| 1419 | } |
| 1420 | |
| 1421 | static int show(int argc, const char **argv, const char *prefix, |
| 1422 | struct repository *repo UNUSED) |
| 1423 | { |
| 1424 | int no_query = 0, result = 0, query_flag = 0; |
| 1425 | struct option options[] = { |
| 1426 | OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")), |
| 1427 | OPT_END() |
| 1428 | }; |
| 1429 | struct show_info info = SHOW_INFO_INIT; |
| 1430 | |
| 1431 | argc = parse_options(argc, argv, prefix, options, |
| 1432 | builtin_remote_show_usage, |
| 1433 | 0); |
| 1434 | |
| 1435 | if (argc < 1) |
| 1436 | return show_all(); |
| 1437 | |
| 1438 | if (!no_query) |
| 1439 | query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); |
| 1440 | |
| 1441 | for (; argc; argc--, argv++) { |
| 1442 | size_t i; |
| 1443 | struct strvec *url; |
| 1444 | |
| 1445 | get_remote_ref_states(*argv, &info.states, query_flag); |
| 1446 | |
| 1447 | printf_ln(_("* remote %s"), *argv); |
| 1448 | printf_ln(_(" Fetch URL: %s"), info.states.remote->url.v[0]); |
| 1449 | url = push_url_of_remote(info.states.remote); |
| 1450 | for (i = 0; i < url->nr; i++) |
| 1451 | /* |
| 1452 | * TRANSLATORS: the colon ':' should align |
| 1453 | * with the one in " Fetch URL: %s" |
| 1454 | * translation. |
| 1455 | */ |
| 1456 | printf_ln(_(" Push URL: %s"), url->v[i]); |
| 1457 | if (!i) |
| 1458 | printf_ln(_(" Push URL: %s"), _("(no URL)")); |
| 1459 | if (no_query) |
| 1460 | printf_ln(_(" HEAD branch: %s"), _("(not queried)")); |
| 1461 | else if (!info.states.heads.nr) |
| 1462 | printf_ln(_(" HEAD branch: %s"), _("(unknown)")); |
| 1463 | else if (info.states.heads.nr == 1) |
| 1464 | printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string); |
| 1465 | else { |
| 1466 | printf(_(" HEAD branch (remote HEAD is ambiguous," |
| 1467 | " may be one of the following):\n")); |
| 1468 | for (i = 0; i < info.states.heads.nr; i++) |
| 1469 | printf(" %s\n", info.states.heads.items[i].string); |
| 1470 | } |
| 1471 | |
| 1472 | /* remote branch info */ |
| 1473 | info.width = 0; |
| 1474 | for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info); |
| 1475 | for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info); |
| 1476 | for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info); |
| 1477 | for_each_string_list(&info.states.stale, add_remote_to_show_info, &info); |
| 1478 | if (info.list.nr) |
| 1479 | printf_ln(Q_(" Remote branch:%s", |
| 1480 | " Remote branches:%s", |
| 1481 | info.list.nr), |
| 1482 | no_query ? _(" (status not queried)") : ""); |
| 1483 | for_each_string_list(&info.list, show_remote_info_item, &info); |
| 1484 | string_list_clear(&info.list, 0); |
| 1485 | |
| 1486 | /* git pull info */ |
| 1487 | info.width = 0; |
| 1488 | info.any_rebase = 0; |
| 1489 | for_each_string_list(&branch_list, add_local_to_show_info, &info); |
| 1490 | if (info.list.nr) |
| 1491 | printf_ln(Q_(" Local branch configured for 'git pull':", |
| 1492 | " Local branches configured for 'git pull':", |
| 1493 | info.list.nr)); |
| 1494 | for_each_string_list(&info.list, show_local_info_item, &info); |
| 1495 | string_list_clear(&info.list, 0); |
| 1496 | |
| 1497 | /* git push info */ |
| 1498 | if (info.states.remote->mirror) |
| 1499 | printf_ln(_(" Local refs will be mirrored by 'git push'")); |
| 1500 | |
| 1501 | info.width = info.width2 = 0; |
| 1502 | for_each_string_list(&info.states.push, add_push_to_show_info, &info); |
| 1503 | QSORT(info.list.items, info.list.nr, cmp_string_with_push); |
| 1504 | if (info.list.nr) |
| 1505 | printf_ln(Q_(" Local ref configured for 'git push'%s:", |
| 1506 | " Local refs configured for 'git push'%s:", |
| 1507 | info.list.nr), |
| 1508 | no_query ? _(" (status not queried)") : ""); |
| 1509 | for_each_string_list(&info.list, show_push_info_item, &info); |
| 1510 | string_list_clear(&info.list, 0); |
| 1511 | |
| 1512 | free_remote_ref_states(&info.states); |
| 1513 | } |
| 1514 | |
| 1515 | return result; |
| 1516 | } |
| 1517 | |
| 1518 | static void report_set_head_auto(const char *remote, const char *head_name, |
| 1519 | struct strbuf *b_local_head, int was_detached) { |
| 1520 | struct strbuf buf_prefix = STRBUF_INIT; |
| 1521 | const char *prev_head = NULL; |
| 1522 | |
| 1523 | strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote); |
| 1524 | skip_prefix(b_local_head->buf, buf_prefix.buf, &prev_head); |
| 1525 | |
| 1526 | if (prev_head && !strcmp(prev_head, head_name)) |
| 1527 | printf(_("'%s/HEAD' is unchanged and points to '%s'\n"), |
| 1528 | remote, head_name); |
| 1529 | else if (prev_head) |
| 1530 | printf(_("'%s/HEAD' has changed from '%s' and now points to '%s'\n"), |
| 1531 | remote, prev_head, head_name); |
| 1532 | else if (!b_local_head->len) |
| 1533 | printf(_("'%s/HEAD' is now created and points to '%s'\n"), |
| 1534 | remote, head_name); |
| 1535 | else if (was_detached && b_local_head->len) |
| 1536 | printf(_("'%s/HEAD' was detached at '%s' and now points to '%s'\n"), |
| 1537 | remote, b_local_head->buf, head_name); |
| 1538 | else |
| 1539 | printf(_("'%s/HEAD' used to point to '%s' " |
| 1540 | "(which is not a remote branch), but now points to '%s'\n"), |
| 1541 | remote, b_local_head->buf, head_name); |
| 1542 | strbuf_release(&buf_prefix); |
| 1543 | } |
| 1544 | |
| 1545 | static int set_head(int argc, const char **argv, const char *prefix, |
| 1546 | struct repository *repo UNUSED) |
| 1547 | { |
| 1548 | int opt_a = 0, opt_d = 0, result = 0, was_detached; |
| 1549 | struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT, |
| 1550 | b_local_head = STRBUF_INIT; |
| 1551 | char *head_name = NULL; |
| 1552 | struct ref_store *refs = get_main_ref_store(the_repository); |
| 1553 | struct remote *remote; |
| 1554 | |
| 1555 | struct option options[] = { |
| 1556 | OPT_BOOL('a', "auto", &opt_a, |
| 1557 | N_("set refs/remotes/<name>/HEAD according to remote")), |
| 1558 | OPT_BOOL('d', "delete", &opt_d, |
| 1559 | N_("delete refs/remotes/<name>/HEAD")), |
| 1560 | OPT_END() |
| 1561 | }; |
| 1562 | argc = parse_options(argc, argv, prefix, options, |
| 1563 | builtin_remote_sethead_usage, 0); |
| 1564 | |
| 1565 | /* All modes require at least a remote name. */ |
| 1566 | if (!argc) |
| 1567 | usage_with_options(builtin_remote_sethead_usage, options); |
| 1568 | |
| 1569 | strbuf_addf(&b_head, "refs/remotes/%s/HEAD", argv[0]); |
| 1570 | remote = remote_get(argv[0]); |
| 1571 | |
| 1572 | if (!opt_a && !opt_d && argc == 2) { |
| 1573 | head_name = xstrdup(argv[1]); |
| 1574 | } else if (opt_a && !opt_d && argc == 1) { |
| 1575 | struct ref_states states = REF_STATES_INIT; |
| 1576 | get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); |
| 1577 | if (!states.heads.nr) |
| 1578 | result |= error(_("Cannot determine remote HEAD")); |
| 1579 | else if (states.heads.nr > 1) { |
| 1580 | result |= error(_("Multiple remote HEAD branches. " |
| 1581 | "Please choose one explicitly with:")); |
| 1582 | for (size_t i = 0; i < states.heads.nr; i++) |
| 1583 | fprintf(stderr, " git remote set-head %s %s\n", |
| 1584 | argv[0], states.heads.items[i].string); |
| 1585 | } else |
| 1586 | head_name = xstrdup(states.heads.items[0].string); |
| 1587 | free_remote_ref_states(&states); |
| 1588 | } else if (opt_d && !opt_a && argc == 1) { |
| 1589 | if (refs_delete_ref(refs, NULL, b_head.buf, NULL, REF_NO_DEREF)) |
| 1590 | result |= error(_("Could not delete %s"), b_head.buf); |
| 1591 | } else |
| 1592 | usage_with_options(builtin_remote_sethead_usage, options); |
| 1593 | |
| 1594 | if (!head_name) |
| 1595 | goto cleanup; |
| 1596 | strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", argv[0], head_name); |
| 1597 | if (!refs_ref_exists(refs, b_remote_head.buf)) { |
| 1598 | result |= error(_("Not a valid ref: %s"), b_remote_head.buf); |
| 1599 | goto cleanup; |
| 1600 | } |
| 1601 | was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf, |
| 1602 | "remote set-head", &b_local_head, 0); |
| 1603 | if (was_detached == -1) { |
| 1604 | result |= error(_("Could not set up %s"), b_head.buf); |
| 1605 | goto cleanup; |
| 1606 | } |
| 1607 | if (opt_a) |
| 1608 | report_set_head_auto(argv[0], head_name, &b_local_head, was_detached); |
| 1609 | if (remote->follow_remote_head == FOLLOW_REMOTE_ALWAYS) { |
| 1610 | struct strbuf config_name = STRBUF_INIT; |
| 1611 | strbuf_addf(&config_name, |
| 1612 | "remote.%s.followremotehead", remote->name); |
| 1613 | repo_config_set(the_repository, config_name.buf, "warn"); |
| 1614 | strbuf_release(&config_name); |
| 1615 | } |
| 1616 | |
| 1617 | cleanup: |
| 1618 | free(head_name); |
| 1619 | strbuf_release(&b_head); |
| 1620 | strbuf_release(&b_remote_head); |
| 1621 | strbuf_release(&b_local_head); |
| 1622 | return result; |
| 1623 | } |
| 1624 | |
| 1625 | static int prune_remote(const char *remote, int dry_run) |
| 1626 | { |
| 1627 | int result = 0; |
| 1628 | struct ref_states states = REF_STATES_INIT; |
| 1629 | struct string_list refs_to_prune = STRING_LIST_INIT_NODUP; |
| 1630 | struct string_list_item *item; |
| 1631 | |
| 1632 | get_remote_ref_states(remote, &states, GET_REF_STATES); |
| 1633 | |
| 1634 | if (!states.stale.nr) { |
| 1635 | free_remote_ref_states(&states); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | |
| 1639 | printf_ln(_("Pruning %s"), remote); |
| 1640 | printf_ln(_("URL: %s"), states.remote->url.v[0]); |
| 1641 | |
| 1642 | for_each_string_list_item(item, &states.stale) |
| 1643 | string_list_append(&refs_to_prune, item->util); |
| 1644 | string_list_sort(&refs_to_prune); |
| 1645 | |
| 1646 | if (!dry_run) |
| 1647 | result |= refs_delete_refs(get_main_ref_store(the_repository), |
| 1648 | "remote: prune", &refs_to_prune, 0); |
| 1649 | |
| 1650 | for_each_string_list_item(item, &states.stale) { |
| 1651 | const char *refname = item->util; |
| 1652 | |
| 1653 | if (dry_run) |
| 1654 | printf_ln(_(" * [would prune] %s"), |
| 1655 | abbrev_ref(refname, "refs/remotes/")); |
| 1656 | else |
| 1657 | printf_ln(_(" * [pruned] %s"), |
| 1658 | abbrev_ref(refname, "refs/remotes/")); |
| 1659 | } |
| 1660 | |
| 1661 | refs_warn_dangling_symrefs(get_main_ref_store(the_repository), |
| 1662 | stdout, " ", dry_run, &refs_to_prune); |
| 1663 | |
| 1664 | string_list_clear(&refs_to_prune, 0); |
| 1665 | free_remote_ref_states(&states); |
| 1666 | return result; |
| 1667 | } |
| 1668 | |
| 1669 | static int prune(int argc, const char **argv, const char *prefix, |
| 1670 | struct repository *repo UNUSED) |
| 1671 | { |
| 1672 | int dry_run = 0, result = 0; |
| 1673 | struct option options[] = { |
| 1674 | OPT__DRY_RUN(&dry_run, N_("dry run")), |
| 1675 | OPT_END() |
| 1676 | }; |
| 1677 | |
| 1678 | argc = parse_options(argc, argv, prefix, options, |
| 1679 | builtin_remote_prune_usage, 0); |
| 1680 | |
| 1681 | if (argc < 1) |
| 1682 | usage_with_options(builtin_remote_prune_usage, options); |
| 1683 | |
| 1684 | for (; argc; argc--, argv++) |
| 1685 | result |= prune_remote(*argv, dry_run); |
| 1686 | |
| 1687 | return result; |
| 1688 | } |
| 1689 | |
| 1690 | static int get_remote_default(const char *key, const char *value UNUSED, |
| 1691 | const struct config_context *ctx UNUSED, |
| 1692 | void *priv) |
| 1693 | { |
| 1694 | if (strcmp(key, "remotes.default") == 0) { |
| 1695 | int *found = priv; |
| 1696 | *found = 1; |
| 1697 | } |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | static int update(int argc, const char **argv, const char *prefix, |
| 1702 | struct repository *repo UNUSED) |
| 1703 | { |
| 1704 | int i, prune = -1; |
| 1705 | struct option options[] = { |
| 1706 | OPT_BOOL('p', "prune", &prune, |
| 1707 | N_("prune remotes after fetching")), |
| 1708 | OPT_END() |
| 1709 | }; |
| 1710 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 1711 | int default_defined = 0; |
| 1712 | |
| 1713 | argc = parse_options(argc, argv, prefix, options, |
| 1714 | builtin_remote_update_usage, |
| 1715 | PARSE_OPT_KEEP_ARGV0); |
| 1716 | |
| 1717 | strvec_push(&cmd.args, "fetch"); |
| 1718 | |
| 1719 | if (prune != -1) |
| 1720 | strvec_push(&cmd.args, prune ? "--prune" : "--no-prune"); |
| 1721 | if (verbose) |
| 1722 | strvec_push(&cmd.args, "-v"); |
| 1723 | strvec_push(&cmd.args, "--multiple"); |
| 1724 | if (argc < 2) |
| 1725 | strvec_push(&cmd.args, "default"); |
| 1726 | for (i = 1; i < argc; i++) |
| 1727 | strvec_push(&cmd.args, argv[i]); |
| 1728 | |
| 1729 | if (strcmp(cmd.args.v[cmd.args.nr-1], "default") == 0) { |
| 1730 | repo_config(the_repository, get_remote_default, &default_defined); |
| 1731 | if (!default_defined) { |
| 1732 | strvec_pop(&cmd.args); |
| 1733 | strvec_push(&cmd.args, "--all"); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | cmd.git_cmd = 1; |
| 1738 | return run_command(&cmd); |
| 1739 | } |
| 1740 | |
| 1741 | static int remove_all_fetch_refspecs(const char *key) |
| 1742 | { |
| 1743 | return repo_config_set_multivar_gently(the_repository, key, NULL, NULL, |
| 1744 | CONFIG_FLAGS_MULTI_REPLACE); |
| 1745 | } |
| 1746 | |
| 1747 | static void add_branches(struct remote *remote, const char **branches, |
| 1748 | const char *key) |
| 1749 | { |
| 1750 | const char *remotename = remote->name; |
| 1751 | int mirror = remote->mirror; |
| 1752 | struct strbuf refspec = STRBUF_INIT; |
| 1753 | |
| 1754 | for (; *branches; branches++) |
| 1755 | add_branch(key, *branches, remotename, mirror, &refspec); |
| 1756 | |
| 1757 | strbuf_release(&refspec); |
| 1758 | } |
| 1759 | |
| 1760 | static int set_remote_branches(const char *remotename, const char **branches, |
| 1761 | int add_mode) |
| 1762 | { |
| 1763 | struct strbuf key = STRBUF_INIT; |
| 1764 | struct remote *remote; |
| 1765 | |
| 1766 | strbuf_addf(&key, "remote.%s.fetch", remotename); |
| 1767 | |
| 1768 | remote = remote_get(remotename); |
| 1769 | if (!remote_is_configured(remote, 1)) { |
| 1770 | error(_("No such remote '%s'"), remotename); |
| 1771 | exit(2); |
| 1772 | } |
| 1773 | |
| 1774 | if (!add_mode && remove_all_fetch_refspecs(key.buf)) { |
| 1775 | strbuf_release(&key); |
| 1776 | return 1; |
| 1777 | } |
| 1778 | add_branches(remote, branches, key.buf); |
| 1779 | |
| 1780 | strbuf_release(&key); |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | static int set_branches(int argc, const char **argv, const char *prefix, |
| 1785 | struct repository *repo UNUSED) |
| 1786 | { |
| 1787 | int add_mode = 0; |
| 1788 | struct option options[] = { |
| 1789 | OPT_BOOL('\0', "add", &add_mode, N_("add branch")), |
| 1790 | OPT_END() |
| 1791 | }; |
| 1792 | |
| 1793 | argc = parse_options(argc, argv, prefix, options, |
| 1794 | builtin_remote_setbranches_usage, 0); |
| 1795 | if (argc == 0) { |
| 1796 | error(_("no remote specified")); |
| 1797 | usage_with_options(builtin_remote_setbranches_usage, options); |
| 1798 | } |
| 1799 | argv[argc] = NULL; |
| 1800 | |
| 1801 | return set_remote_branches(argv[0], argv + 1, add_mode); |
| 1802 | } |
| 1803 | |
| 1804 | static int get_url(int argc, const char **argv, const char *prefix, |
| 1805 | struct repository *repo UNUSED) |
| 1806 | { |
| 1807 | int push_mode = 0, all_mode = 0; |
| 1808 | const char *remotename = NULL; |
| 1809 | struct remote *remote; |
| 1810 | struct strvec *url; |
| 1811 | struct option options[] = { |
| 1812 | OPT_BOOL('\0', "push", &push_mode, |
| 1813 | N_("query push URLs rather than fetch URLs")), |
| 1814 | OPT_BOOL('\0', "all", &all_mode, |
| 1815 | N_("return all URLs")), |
| 1816 | OPT_END() |
| 1817 | }; |
| 1818 | argc = parse_options(argc, argv, prefix, options, |
| 1819 | builtin_remote_geturl_usage, 0); |
| 1820 | |
| 1821 | if (argc != 1) |
| 1822 | usage_with_options(builtin_remote_geturl_usage, options); |
| 1823 | |
| 1824 | remotename = argv[0]; |
| 1825 | |
| 1826 | remote = remote_get(remotename); |
| 1827 | if (!remote_is_configured(remote, 1)) { |
| 1828 | error(_("No such remote '%s'"), remotename); |
| 1829 | exit(2); |
| 1830 | } |
| 1831 | |
| 1832 | url = push_mode ? push_url_of_remote(remote) : &remote->url; |
| 1833 | |
| 1834 | if (all_mode) { |
| 1835 | for (size_t i = 0; i < url->nr; i++) |
| 1836 | printf_ln("%s", url->v[i]); |
| 1837 | } else { |
| 1838 | printf_ln("%s", url->v[0]); |
| 1839 | } |
| 1840 | |
| 1841 | return 0; |
| 1842 | } |
| 1843 | |
| 1844 | static int set_url(int argc, const char **argv, const char *prefix, |
| 1845 | struct repository *repo UNUSED) |
| 1846 | { |
| 1847 | int push_mode = 0, add_mode = 0, delete_mode = 0; |
| 1848 | int matches = 0, negative_matches = 0; |
| 1849 | const char *remotename = NULL; |
| 1850 | const char *newurl = NULL; |
| 1851 | const char *oldurl = NULL; |
| 1852 | struct remote *remote; |
| 1853 | regex_t old_regex; |
| 1854 | struct strvec *urlset; |
| 1855 | struct strbuf name_buf = STRBUF_INIT; |
| 1856 | struct option options[] = { |
| 1857 | OPT_BOOL('\0', "push", &push_mode, |
| 1858 | N_("manipulate push URLs")), |
| 1859 | OPT_BOOL('\0', "add", &add_mode, |
| 1860 | N_("add URL")), |
| 1861 | OPT_BOOL('\0', "delete", &delete_mode, |
| 1862 | N_("delete URLs")), |
| 1863 | OPT_END() |
| 1864 | }; |
| 1865 | argc = parse_options(argc, argv, prefix, options, |
| 1866 | builtin_remote_seturl_usage, |
| 1867 | PARSE_OPT_KEEP_ARGV0); |
| 1868 | |
| 1869 | if (add_mode && delete_mode) |
| 1870 | die(_("--add --delete doesn't make sense")); |
| 1871 | |
| 1872 | if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3)) |
| 1873 | usage_with_options(builtin_remote_seturl_usage, options); |
| 1874 | |
| 1875 | remotename = argv[1]; |
| 1876 | newurl = argv[2]; |
| 1877 | if (argc > 3) |
| 1878 | oldurl = argv[3]; |
| 1879 | |
| 1880 | if (delete_mode) |
| 1881 | oldurl = newurl; |
| 1882 | |
| 1883 | remote = remote_get(remotename); |
| 1884 | if (!remote_is_configured(remote, 1)) { |
| 1885 | error(_("No such remote '%s'"), remotename); |
| 1886 | exit(2); |
| 1887 | } |
| 1888 | |
| 1889 | if (push_mode) { |
| 1890 | strbuf_addf(&name_buf, "remote.%s.pushurl", remotename); |
| 1891 | urlset = &remote->pushurl; |
| 1892 | } else { |
| 1893 | strbuf_addf(&name_buf, "remote.%s.url", remotename); |
| 1894 | urlset = &remote->url; |
| 1895 | } |
| 1896 | |
| 1897 | /* Special cases that add new entry. */ |
| 1898 | if ((!oldurl && !delete_mode) || add_mode) { |
| 1899 | if (add_mode) |
| 1900 | repo_config_set_multivar(the_repository, name_buf.buf, newurl, |
| 1901 | "^$", 0); |
| 1902 | else |
| 1903 | repo_config_set(the_repository, name_buf.buf, newurl); |
| 1904 | goto out; |
| 1905 | } |
| 1906 | |
| 1907 | /* Old URL specified. Demand that one matches. */ |
| 1908 | if (regcomp(&old_regex, oldurl, REG_EXTENDED)) |
| 1909 | die(_("Invalid old URL pattern: %s"), oldurl); |
| 1910 | |
| 1911 | for (size_t i = 0; i < urlset->nr; i++) |
| 1912 | if (!regexec(&old_regex, urlset->v[i], 0, NULL, 0)) |
| 1913 | matches++; |
| 1914 | else |
| 1915 | negative_matches++; |
| 1916 | if (!delete_mode && !matches) |
| 1917 | die(_("No such URL found: %s"), oldurl); |
| 1918 | if (delete_mode && !negative_matches && !push_mode) |
| 1919 | die(_("Will not delete all non-push URLs")); |
| 1920 | |
| 1921 | regfree(&old_regex); |
| 1922 | |
| 1923 | if (!delete_mode) |
| 1924 | repo_config_set_multivar(the_repository, name_buf.buf, newurl, oldurl, 0); |
| 1925 | else |
| 1926 | repo_config_set_multivar(the_repository, name_buf.buf, NULL, oldurl, |
| 1927 | CONFIG_FLAGS_MULTI_REPLACE); |
| 1928 | out: |
| 1929 | strbuf_release(&name_buf); |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | int cmd_remote(int argc, |
| 1934 | const char **argv, |
| 1935 | const char *prefix, |
| 1936 | struct repository *repo) |
| 1937 | { |
| 1938 | parse_opt_subcommand_fn *fn = NULL; |
| 1939 | struct option options[] = { |
| 1940 | OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")), |
| 1941 | OPT_SUBCOMMAND("add", &fn, add), |
| 1942 | OPT_SUBCOMMAND("rename", &fn, mv), |
| 1943 | OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE), |
| 1944 | OPT_SUBCOMMAND("remove", &fn, rm), |
| 1945 | OPT_SUBCOMMAND("set-head", &fn, set_head), |
| 1946 | OPT_SUBCOMMAND("set-branches", &fn, set_branches), |
| 1947 | OPT_SUBCOMMAND("get-url", &fn, get_url), |
| 1948 | OPT_SUBCOMMAND("set-url", &fn, set_url), |
| 1949 | OPT_SUBCOMMAND("show", &fn, show), |
| 1950 | OPT_SUBCOMMAND("prune", &fn, prune), |
| 1951 | OPT_SUBCOMMAND("update", &fn, update), |
| 1952 | OPT_END() |
| 1953 | }; |
| 1954 | |
| 1955 | argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, |
| 1956 | PARSE_OPT_SUBCOMMAND_OPTIONAL); |
| 1957 | |
| 1958 | if (fn) { |
| 1959 | return !!fn(argc, argv, prefix, repo); |
| 1960 | } else { |
| 1961 | if (argc) { |
| 1962 | error(_("unknown subcommand: `%s'"), argv[0]); |
| 1963 | usage_with_options(builtin_remote_usage, options); |
| 1964 | } |
| 1965 | return !!show_all(); |
| 1966 | } |
| 1967 | } |