Stephen Boyd | c2e86ad | 2011-03-22 00:51:05 -0700 | [diff] [blame] | 1 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 3 | #include "parse-options.h" |
| 4 | #include "transport.h" |
| 5 | #include "remote.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 6 | #include "string-list.h" |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 7 | #include "strbuf.h" |
| 8 | #include "run-command.h" |
Bert Wesarg | 88f8576 | 2020-01-27 08:04:27 +0100 | [diff] [blame] | 9 | #include "rebase.h" |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 10 | #include "refs.h" |
Brandon Williams | ec0cb49 | 2018-05-16 15:57:48 -0700 | [diff] [blame] | 11 | #include "refspec.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 12 | #include "object-store.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 13 | #include "strvec.h" |
Derrick Stolee | 1d614d4 | 2018-07-20 16:33:06 +0000 | [diff] [blame] | 14 | #include "commit-reach.h" |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 15 | |
| 16 | static const char * const builtin_remote_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 17 | N_("git remote [-v | --verbose]"), |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 18 | N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"), |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 19 | N_("git remote rename <old> <new>"), |
Junio C Hamano | 9058560 | 2012-09-12 14:21:58 -0700 | [diff] [blame] | 20 | N_("git remote remove <name>"), |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 21 | N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 22 | N_("git remote [-v | --verbose] show [-n] <name>"), |
| 23 | N_("git remote prune [-n | --dry-run] <name>"), |
| 24 | N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"), |
| 25 | N_("git remote set-branches [--add] <name> <branch>..."), |
Ben Boeckel | 96f78d3 | 2015-09-15 21:53:47 -0400 | [diff] [blame] | 26 | N_("git remote get-url [--push] [--all] <name>"), |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 27 | N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), |
| 28 | N_("git remote set-url --add <name> <newurl>"), |
| 29 | N_("git remote set-url --delete <name> <url>"), |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 30 | NULL |
| 31 | }; |
| 32 | |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 33 | static const char * const builtin_remote_add_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 34 | N_("git remote add [<options>] <name> <url>"), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 35 | NULL |
| 36 | }; |
| 37 | |
| 38 | static const char * const builtin_remote_rename_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 39 | N_("git remote rename <old> <new>"), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 40 | NULL |
| 41 | }; |
| 42 | |
| 43 | static const char * const builtin_remote_rm_usage[] = { |
Junio C Hamano | 9058560 | 2012-09-12 14:21:58 -0700 | [diff] [blame] | 44 | N_("git remote remove <name>"), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 45 | NULL |
| 46 | }; |
| 47 | |
| 48 | static const char * const builtin_remote_sethead_usage[] = { |
Philip Oakley | e49c8f3 | 2013-09-21 16:51:46 +0100 | [diff] [blame] | 49 | N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 50 | NULL |
| 51 | }; |
| 52 | |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 53 | static const char * const builtin_remote_setbranches_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 54 | N_("git remote set-branches <name> <branch>..."), |
| 55 | N_("git remote set-branches --add <name> <branch>..."), |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 56 | NULL |
| 57 | }; |
| 58 | |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 59 | static const char * const builtin_remote_show_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 60 | N_("git remote show [<options>] <name>"), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 61 | NULL |
| 62 | }; |
| 63 | |
| 64 | static const char * const builtin_remote_prune_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 65 | N_("git remote prune [<options>] <name>"), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 66 | NULL |
| 67 | }; |
| 68 | |
| 69 | static const char * const builtin_remote_update_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 70 | N_("git remote update [<options>] [<group> | <remote>]..."), |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 71 | NULL |
| 72 | }; |
| 73 | |
Ben Boeckel | 96f78d3 | 2015-09-15 21:53:47 -0400 | [diff] [blame] | 74 | static const char * const builtin_remote_geturl_usage[] = { |
| 75 | N_("git remote get-url [--push] [--all] <name>"), |
| 76 | NULL |
| 77 | }; |
| 78 | |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 79 | static const char * const builtin_remote_seturl_usage[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 80 | N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"), |
| 81 | N_("git remote set-url --add <name> <newurl>"), |
| 82 | N_("git remote set-url --delete <name> <url>"), |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 83 | NULL |
| 84 | }; |
| 85 | |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 86 | #define GET_REF_STATES (1<<0) |
| 87 | #define GET_HEAD_NAMES (1<<1) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 88 | #define GET_PUSH_REF_STATES (1<<2) |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 89 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 90 | static int verbose; |
| 91 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 92 | static int fetch_remote(const char *name) |
| 93 | { |
Cheng Renquan | dbbd56f | 2008-11-18 19:04:02 +0800 | [diff] [blame] | 94 | const char *argv[] = { "fetch", name, NULL, NULL }; |
| 95 | if (verbose) { |
| 96 | argv[1] = "-v"; |
| 97 | argv[2] = name; |
| 98 | } |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 99 | printf_ln(_("Updating %s"), name); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 100 | if (run_command_v_opt(argv, RUN_GIT_CMD)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 101 | return error(_("Could not fetch %s"), name); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Samuel Tardieu | 111fb85 | 2010-04-20 01:31:31 +0200 | [diff] [blame] | 105 | enum { |
| 106 | TAGS_UNSET = 0, |
| 107 | TAGS_DEFAULT = 1, |
| 108 | TAGS_SET = 2 |
| 109 | }; |
| 110 | |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 111 | #define MIRROR_NONE 0 |
| 112 | #define MIRROR_FETCH 1 |
| 113 | #define MIRROR_PUSH 2 |
| 114 | #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH) |
| 115 | |
Patrick Steinhardt | ab5e4b6 | 2016-02-22 12:23:29 +0100 | [diff] [blame] | 116 | static void add_branch(const char *key, const char *branchname, |
| 117 | const char *remotename, int mirror, struct strbuf *tmp) |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 118 | { |
| 119 | strbuf_reset(tmp); |
| 120 | strbuf_addch(tmp, '+'); |
| 121 | if (mirror) |
| 122 | strbuf_addf(tmp, "refs/%s:refs/%s", |
| 123 | branchname, branchname); |
| 124 | else |
| 125 | strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s", |
| 126 | branchname, remotename, branchname); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 127 | git_config_set_multivar(key, tmp->buf, "^$", 0); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Jeff King | 0990248 | 2011-03-30 15:53:39 -0400 | [diff] [blame] | 130 | static const char mirror_advice[] = |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 131 | N_("--mirror is dangerous and deprecated; please\n" |
| 132 | "\t use --mirror=fetch or --mirror=push instead"); |
Jeff King | 0990248 | 2011-03-30 15:53:39 -0400 | [diff] [blame] | 133 | |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 134 | static int parse_mirror_opt(const struct option *opt, const char *arg, int not) |
| 135 | { |
| 136 | unsigned *mirror = opt->value; |
| 137 | if (not) |
| 138 | *mirror = MIRROR_NONE; |
Jeff King | 0990248 | 2011-03-30 15:53:39 -0400 | [diff] [blame] | 139 | else if (!arg) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 140 | warning("%s", _(mirror_advice)); |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 141 | *mirror = MIRROR_BOTH; |
Jeff King | 0990248 | 2011-03-30 15:53:39 -0400 | [diff] [blame] | 142 | } |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 143 | else if (!strcmp(arg, "fetch")) |
| 144 | *mirror = MIRROR_FETCH; |
| 145 | else if (!strcmp(arg, "push")) |
| 146 | *mirror = MIRROR_PUSH; |
| 147 | else |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 148 | return error(_("unknown mirror argument: %s"), arg); |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 152 | static int add(int argc, const char **argv) |
| 153 | { |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 154 | int fetch = 0, fetch_tags = TAGS_DEFAULT; |
| 155 | unsigned mirror = MIRROR_NONE; |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 156 | struct string_list track = STRING_LIST_INIT_NODUP; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 157 | const char *master = NULL; |
| 158 | struct remote *remote; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 159 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 160 | const char *name, *url; |
| 161 | int i; |
| 162 | |
| 163 | struct option options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 164 | OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")), |
Samuel Tardieu | 111fb85 | 2010-04-20 01:31:31 +0200 | [diff] [blame] | 165 | OPT_SET_INT(0, "tags", &fetch_tags, |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 166 | N_("import all tags and associated objects when fetching"), |
Samuel Tardieu | 111fb85 | 2010-04-20 01:31:31 +0200 | [diff] [blame] | 167 | TAGS_SET), |
| 168 | OPT_SET_INT(0, NULL, &fetch_tags, |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 169 | N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET), |
| 170 | OPT_STRING_LIST('t', "track", &track, N_("branch"), |
| 171 | N_("branch(es) to track")), |
| 172 | OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 173 | OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)", |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 174 | N_("set up remote as a mirror to push to or fetch from"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 175 | PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt), |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 176 | OPT_END() |
| 177 | }; |
| 178 | |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 179 | argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage, |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 180 | 0); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 181 | |
Thomas Rast | 2d2e3d2 | 2013-04-24 15:54:36 +0200 | [diff] [blame] | 182 | if (argc != 2) |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 183 | usage_with_options(builtin_remote_add_usage, options); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 184 | |
Jeff King | 13fc2c1 | 2011-03-30 15:52:52 -0400 | [diff] [blame] | 185 | if (mirror && master) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 186 | die(_("specifying a master branch makes no sense with --mirror")); |
Jeff King | 3eafdc9 | 2011-05-26 11:11:00 -0400 | [diff] [blame] | 187 | if (mirror && !(mirror & MIRROR_FETCH) && track.nr) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 188 | die(_("specifying branches to track makes sense only with fetch mirrors")); |
Jeff King | 13fc2c1 | 2011-03-30 15:52:52 -0400 | [diff] [blame] | 189 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 190 | name = argv[0]; |
| 191 | url = argv[1]; |
| 192 | |
| 193 | remote = remote_get(name); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 194 | if (remote_is_configured(remote, 1)) { |
| 195 | error(_("remote %s already exists."), name); |
| 196 | exit(3); |
| 197 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 198 | |
Sean Barag | f2c6fda | 2020-10-01 03:46:13 +0000 | [diff] [blame] | 199 | if (!valid_remote_name(name)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 200 | die(_("'%s' is not a valid remote name"), name); |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 201 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 202 | strbuf_addf(&buf, "remote.%s.url", name); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 203 | git_config_set(buf.buf, url); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 204 | |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 205 | if (!mirror || mirror & MIRROR_FETCH) { |
| 206 | strbuf_reset(&buf); |
| 207 | strbuf_addf(&buf, "remote.%s.fetch", name); |
| 208 | if (track.nr == 0) |
| 209 | string_list_append(&track, "*"); |
| 210 | for (i = 0; i < track.nr; i++) { |
Patrick Steinhardt | ab5e4b6 | 2016-02-22 12:23:29 +0100 | [diff] [blame] | 211 | add_branch(buf.buf, track.items[i].string, |
| 212 | name, mirror, &buf2); |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 213 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Jeff King | a9f5a35 | 2011-03-30 15:53:19 -0400 | [diff] [blame] | 216 | if (mirror & MIRROR_PUSH) { |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 217 | strbuf_reset(&buf); |
| 218 | strbuf_addf(&buf, "remote.%s.mirror", name); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 219 | git_config_set(buf.buf, "true"); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Samuel Tardieu | 111fb85 | 2010-04-20 01:31:31 +0200 | [diff] [blame] | 222 | if (fetch_tags != TAGS_DEFAULT) { |
| 223 | strbuf_reset(&buf); |
Ævar Arnfjörð Bjarmason | bfa9148 | 2021-02-25 02:21:16 +0100 | [diff] [blame] | 224 | strbuf_addf(&buf, "remote.%s.tagOpt", name); |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 225 | git_config_set(buf.buf, |
| 226 | fetch_tags == TAGS_SET ? "--tags" : "--no-tags"); |
Samuel Tardieu | 111fb85 | 2010-04-20 01:31:31 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 229 | if (fetch && fetch_remote(name)) |
| 230 | return 1; |
| 231 | |
| 232 | if (master) { |
| 233 | strbuf_reset(&buf); |
| 234 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); |
| 235 | |
| 236 | strbuf_reset(&buf2); |
| 237 | strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); |
| 238 | |
| 239 | if (create_symref(buf.buf, buf2.buf, "remote add")) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 240 | return error(_("Could not setup master '%s'"), master); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | strbuf_release(&buf); |
| 244 | strbuf_release(&buf2); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 245 | string_list_clear(&track, 0); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | struct branch_info { |
Jay Soffian | e0cc81e | 2009-02-25 03:32:21 -0500 | [diff] [blame] | 251 | char *remote_name; |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 252 | struct string_list merge; |
Bert Wesarg | 88f8576 | 2020-01-27 08:04:27 +0100 | [diff] [blame] | 253 | enum rebase_type rebase; |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 254 | char *push_remote_name; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
Jeff King | 2721ce2 | 2016-06-13 06:04:20 -0400 | [diff] [blame] | 257 | static struct string_list branch_list = STRING_LIST_INIT_NODUP; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 258 | |
Junio C Hamano | 72972eb | 2008-07-17 21:30:33 -0700 | [diff] [blame] | 259 | static const char *abbrev_ref(const char *name, const char *prefix) |
| 260 | { |
Jeff King | cf4fff5 | 2014-06-18 15:44:19 -0400 | [diff] [blame] | 261 | skip_prefix(name, prefix, &name); |
Junio C Hamano | 72972eb | 2008-07-17 21:30:33 -0700 | [diff] [blame] | 262 | return name; |
| 263 | } |
| 264 | #define abbrev_branch(name) abbrev_ref((name), "refs/heads/") |
| 265 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 266 | static int config_read_branches(const char *key, const char *value, void *cb) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 267 | { |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 268 | const char *orig_key = key; |
| 269 | char *name; |
| 270 | struct string_list_item *item; |
| 271 | struct branch_info *info; |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 272 | enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type; |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 273 | size_t key_len; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 274 | |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 275 | if (!starts_with(key, "branch.")) |
| 276 | return 0; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 277 | |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 278 | key += strlen("branch."); |
| 279 | if (strip_suffix(key, ".remote", &key_len)) |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 280 | type = REMOTE; |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 281 | else if (strip_suffix(key, ".merge", &key_len)) |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 282 | type = MERGE; |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 283 | else if (strip_suffix(key, ".rebase", &key_len)) |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 284 | type = REBASE; |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 285 | else if (strip_suffix(key, ".pushremote", &key_len)) |
| 286 | type = PUSH_REMOTE; |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 287 | else |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 288 | return 0; |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 289 | name = xmemdupz(key, key_len); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 290 | |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 291 | item = string_list_insert(&branch_list, name); |
| 292 | |
| 293 | if (!item->util) |
| 294 | item->util = xcalloc(1, sizeof(struct branch_info)); |
| 295 | info = item->util; |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 296 | switch (type) { |
| 297 | case REMOTE: |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 298 | if (info->remote_name) |
| 299 | warning(_("more than one %s"), orig_key); |
| 300 | info->remote_name = xstrdup(value); |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 301 | break; |
| 302 | case MERGE: { |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 303 | char *space = strchr(value, ' '); |
| 304 | value = abbrev_branch(value); |
| 305 | while (space) { |
| 306 | char *merge; |
| 307 | merge = xstrndup(value, space - value); |
| 308 | string_list_append(&info->merge, merge); |
| 309 | value = abbrev_branch(space + 1); |
| 310 | space = strchr(value, ' '); |
| 311 | } |
| 312 | string_list_append(&info->merge, xstrdup(value)); |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 313 | break; |
| 314 | } |
| 315 | case REBASE: |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 316 | /* |
| 317 | * Consider invalid values as false and check the |
| 318 | * truth value with >= REBASE_TRUE. |
| 319 | */ |
| 320 | info->rebase = rebase_parse_value(value); |
Johannes Schindelin | ab7c7c2 | 2021-09-07 21:05:03 +0000 | [diff] [blame] | 321 | if (info->rebase == REBASE_INVALID) |
| 322 | warning(_("unhandled branch.%s.rebase=%s; assuming " |
| 323 | "'true'"), name, value); |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 324 | break; |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 325 | case PUSH_REMOTE: |
| 326 | if (info->push_remote_name) |
| 327 | warning(_("more than one %s"), orig_key); |
| 328 | info->push_remote_name = xstrdup(value); |
| 329 | break; |
Bert Wesarg | ceff1a1 | 2020-01-27 08:04:29 +0100 | [diff] [blame] | 330 | default: |
| 331 | BUG("unexpected type=%d", type); |
| 332 | } |
Bert Wesarg | 1a83068 | 2020-01-27 08:04:28 +0100 | [diff] [blame] | 333 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static void read_branches(void) |
| 338 | { |
| 339 | if (branch_list.nr) |
| 340 | return; |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 341 | git_config(config_read_branches, NULL); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | struct ref_states { |
| 345 | struct remote *remote; |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 346 | struct string_list new_refs, stale, tracked, heads, push; |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 347 | int queried; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 348 | }; |
| 349 | |
Ævar Arnfjörð Bjarmason | 0bc7787 | 2021-10-01 12:27:34 +0200 | [diff] [blame] | 350 | #define REF_STATES_INIT { \ |
| 351 | .new_refs = STRING_LIST_INIT_DUP, \ |
| 352 | .stale = STRING_LIST_INIT_DUP, \ |
| 353 | .tracked = STRING_LIST_INIT_DUP, \ |
| 354 | .heads = STRING_LIST_INIT_DUP, \ |
| 355 | .push = STRING_LIST_INIT_DUP, \ |
| 356 | } |
| 357 | |
Jay Soffian | e0cc81e | 2009-02-25 03:32:21 -0500 | [diff] [blame] | 358 | static int get_ref_states(const struct ref *remote_refs, struct ref_states *states) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 359 | { |
| 360 | struct ref *fetch_map = NULL, **tail = &fetch_map; |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 361 | struct ref *ref, *stale_refs; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 362 | int i; |
| 363 | |
Brandon Williams | e5349ab | 2018-05-16 15:58:01 -0700 | [diff] [blame] | 364 | for (i = 0; i < states->remote->fetch.nr; i++) |
| 365 | if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 366 | die(_("Could not get fetch map for refspec %s"), |
Brandon Williams | e5349ab | 2018-05-16 15:58:01 -0700 | [diff] [blame] | 367 | states->remote->fetch.raw[i]); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 368 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 369 | for (ref = fetch_map; ref; ref = ref->next) { |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 370 | if (!ref->peer_ref || !ref_exists(ref->peer_ref->name)) |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 371 | string_list_append(&states->new_refs, abbrev_branch(ref->name)); |
Jay Soffian | 7b9a5e2 | 2009-02-25 03:32:20 -0500 | [diff] [blame] | 372 | else |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 373 | string_list_append(&states->tracked, abbrev_branch(ref->name)); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 374 | } |
Brandon Williams | a2ac50c | 2018-05-16 15:58:10 -0700 | [diff] [blame] | 375 | stale_refs = get_stale_heads(&states->remote->fetch, fetch_map); |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 376 | for (ref = stale_refs; ref; ref = ref->next) { |
| 377 | struct string_list_item *item = |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 378 | string_list_append(&states->stale, abbrev_branch(ref->name)); |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 379 | item->util = xstrdup(ref->name); |
| 380 | } |
| 381 | free_refs(stale_refs); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 382 | free_refs(fetch_map); |
| 383 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 384 | string_list_sort(&states->new_refs); |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 385 | string_list_sort(&states->tracked); |
| 386 | string_list_sort(&states->stale); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 387 | |
| 388 | return 0; |
| 389 | } |
| 390 | |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 391 | struct push_info { |
| 392 | char *dest; |
| 393 | int forced; |
| 394 | enum { |
| 395 | PUSH_STATUS_CREATE = 0, |
| 396 | PUSH_STATUS_DELETE, |
| 397 | PUSH_STATUS_UPTODATE, |
| 398 | PUSH_STATUS_FASTFORWARD, |
| 399 | PUSH_STATUS_OUTOFDATE, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 400 | PUSH_STATUS_NOTQUERIED |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 401 | } status; |
| 402 | }; |
| 403 | |
| 404 | static int get_push_ref_states(const struct ref *remote_refs, |
| 405 | struct ref_states *states) |
| 406 | { |
| 407 | struct remote *remote = states->remote; |
Clemens Buchacher | 6d2bf96 | 2009-05-31 16:26:48 +0200 | [diff] [blame] | 408 | struct ref *ref, *local_refs, *push_map; |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 409 | if (remote->mirror) |
| 410 | return 0; |
| 411 | |
| 412 | local_refs = get_local_heads(); |
Clemens Buchacher | 6a01554 | 2009-05-27 22:13:43 +0200 | [diff] [blame] | 413 | push_map = copy_ref_list(remote_refs); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 414 | |
Brandon Williams | 5c7ec84 | 2018-05-16 15:58:21 -0700 | [diff] [blame] | 415 | match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 416 | |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 417 | for (ref = push_map; ref; ref = ref->next) { |
| 418 | struct string_list_item *item; |
| 419 | struct push_info *info; |
| 420 | |
| 421 | if (!ref->peer_ref) |
| 422 | continue; |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 423 | oidcpy(&ref->new_oid, &ref->peer_ref->new_oid); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 424 | |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 425 | item = string_list_append(&states->push, |
| 426 | abbrev_branch(ref->peer_ref->name)); |
Brian Gesiak | 3806945 | 2014-05-27 00:33:44 +0900 | [diff] [blame] | 427 | item->util = xcalloc(1, sizeof(struct push_info)); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 428 | info = item->util; |
| 429 | info->forced = ref->force; |
| 430 | info->dest = xstrdup(abbrev_branch(ref->name)); |
| 431 | |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 432 | if (is_null_oid(&ref->new_oid)) { |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 433 | info->status = PUSH_STATUS_DELETE; |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 434 | } else if (oideq(&ref->old_oid, &ref->new_oid)) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 435 | info->status = PUSH_STATUS_UPTODATE; |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 436 | else if (is_null_oid(&ref->old_oid)) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 437 | info->status = PUSH_STATUS_CREATE; |
brian m. carlson | f4e54d0 | 2015-11-10 02:22:20 +0000 | [diff] [blame] | 438 | else if (has_object_file(&ref->old_oid) && |
brian m. carlson | 6f3d57b | 2015-11-10 02:22:25 +0000 | [diff] [blame] | 439 | ref_newer(&ref->new_oid, &ref->old_oid)) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 440 | info->status = PUSH_STATUS_FASTFORWARD; |
| 441 | else |
| 442 | info->status = PUSH_STATUS_OUTOFDATE; |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 443 | } |
| 444 | free_refs(local_refs); |
| 445 | free_refs(push_map); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static int get_push_ref_states_noquery(struct ref_states *states) |
| 450 | { |
| 451 | int i; |
| 452 | struct remote *remote = states->remote; |
| 453 | struct string_list_item *item; |
| 454 | struct push_info *info; |
| 455 | |
| 456 | if (remote->mirror) |
| 457 | return 0; |
| 458 | |
Brandon Williams | 6bdb304 | 2018-05-16 15:58:00 -0700 | [diff] [blame] | 459 | if (!remote->push.nr) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 460 | item = string_list_append(&states->push, _("(matching)")); |
Brian Gesiak | 3806945 | 2014-05-27 00:33:44 +0900 | [diff] [blame] | 461 | info = item->util = xcalloc(1, sizeof(struct push_info)); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 462 | info->status = PUSH_STATUS_NOTQUERIED; |
| 463 | info->dest = xstrdup(item->string); |
| 464 | } |
Brandon Williams | 6bdb304 | 2018-05-16 15:58:00 -0700 | [diff] [blame] | 465 | for (i = 0; i < remote->push.nr; i++) { |
| 466 | const struct refspec_item *spec = &remote->push.items[i]; |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 467 | if (spec->matching) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 468 | item = string_list_append(&states->push, _("(matching)")); |
Junio C Hamano | 5ad6b02 | 2009-03-08 00:12:33 -0800 | [diff] [blame] | 469 | else if (strlen(spec->src)) |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 470 | item = string_list_append(&states->push, spec->src); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 471 | else |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 472 | item = string_list_append(&states->push, _("(delete)")); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 473 | |
Brian Gesiak | 3806945 | 2014-05-27 00:33:44 +0900 | [diff] [blame] | 474 | info = item->util = xcalloc(1, sizeof(struct push_info)); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 475 | info->forced = spec->force; |
| 476 | info->status = PUSH_STATUS_NOTQUERIED; |
Junio C Hamano | 5ad6b02 | 2009-03-08 00:12:33 -0800 | [diff] [blame] | 477 | info->dest = xstrdup(spec->dst ? spec->dst : item->string); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 478 | } |
| 479 | return 0; |
| 480 | } |
| 481 | |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 482 | static int get_head_names(const struct ref *remote_refs, struct ref_states *states) |
| 483 | { |
| 484 | struct ref *ref, *matches; |
| 485 | struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map; |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 486 | struct refspec_item refspec; |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 487 | |
Jacob Keller | 95e7c38 | 2020-08-14 17:25:08 -0700 | [diff] [blame] | 488 | memset(&refspec, 0, sizeof(refspec)); |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 489 | refspec.force = 0; |
| 490 | refspec.pattern = 1; |
Junio C Hamano | 5ad6b02 | 2009-03-08 00:12:33 -0800 | [diff] [blame] | 491 | refspec.src = refspec.dst = "refs/heads/*"; |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 492 | get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0); |
| 493 | matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"), |
| 494 | fetch_map, 1); |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 495 | for (ref = matches; ref; ref = ref->next) |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 496 | string_list_append(&states->heads, abbrev_branch(ref->name)); |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 497 | |
| 498 | free_refs(fetch_map); |
| 499 | free_refs(matches); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 504 | struct known_remote { |
| 505 | struct known_remote *next; |
| 506 | struct remote *remote; |
| 507 | }; |
| 508 | |
| 509 | struct known_remotes { |
| 510 | struct remote *to_delete; |
| 511 | struct known_remote *list; |
| 512 | }; |
| 513 | |
| 514 | static int add_known_remote(struct remote *remote, void *cb_data) |
| 515 | { |
| 516 | struct known_remotes *all = cb_data; |
| 517 | struct known_remote *r; |
| 518 | |
| 519 | if (!strcmp(all->to_delete->name, remote->name)) |
| 520 | return 0; |
| 521 | |
| 522 | r = xmalloc(sizeof(*r)); |
| 523 | r->remote = remote; |
| 524 | r->next = all->list; |
| 525 | all->list = r; |
| 526 | return 0; |
| 527 | } |
| 528 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 529 | struct branches_for_remote { |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 530 | struct remote *remote; |
Jay Soffian | 441adf0 | 2009-02-04 11:06:07 -0500 | [diff] [blame] | 531 | struct string_list *branches, *skipped; |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 532 | struct known_remotes *keep; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | static int add_branch_for_removal(const char *refname, |
Michael Haggerty | 45690a5 | 2015-05-25 18:38:41 +0000 | [diff] [blame] | 536 | const struct object_id *oid, int flags, void *cb_data) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 537 | { |
| 538 | struct branches_for_remote *branches = cb_data; |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 539 | struct refspec_item refspec; |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 540 | struct known_remote *kr; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 541 | |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 542 | memset(&refspec, 0, sizeof(refspec)); |
| 543 | refspec.dst = (char *)refname; |
| 544 | if (remote_find_tracking(branches->remote, &refspec)) |
| 545 | return 0; |
Johannes Schindelin | 3b9dcff | 2008-03-08 23:40:42 +0100 | [diff] [blame] | 546 | |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 547 | /* don't delete a branch if another remote also uses it */ |
| 548 | for (kr = branches->keep->list; kr; kr = kr->next) { |
| 549 | memset(&refspec, 0, sizeof(refspec)); |
| 550 | refspec.dst = (char *)refname; |
| 551 | if (!remote_find_tracking(kr->remote, &refspec)) |
| 552 | return 0; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Matthieu Moy | 1393123 | 2010-11-02 16:31:25 +0100 | [diff] [blame] | 555 | /* don't delete non-remote-tracking refs */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 556 | if (!starts_with(refname, "refs/remotes/")) { |
Jay Soffian | 441adf0 | 2009-02-04 11:06:07 -0500 | [diff] [blame] | 557 | /* advise user how to delete local branches */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 558 | if (starts_with(refname, "refs/heads/")) |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 559 | string_list_append(branches->skipped, |
| 560 | abbrev_branch(refname)); |
Jay Soffian | 441adf0 | 2009-02-04 11:06:07 -0500 | [diff] [blame] | 561 | /* silently skip over other non-remote refs */ |
| 562 | return 0; |
| 563 | } |
| 564 | |
Michael Haggerty | e26cdf9 | 2015-05-25 18:38:42 +0000 | [diff] [blame] | 565 | string_list_append(branches->branches, refname); |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 566 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 570 | struct rename_info { |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 571 | const char *old_name; |
| 572 | const char *new_name; |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 573 | struct string_list *remote_branches; |
| 574 | }; |
| 575 | |
| 576 | static int read_remote_branches(const char *refname, |
Michael Haggerty | 53dc95b | 2015-05-25 18:38:43 +0000 | [diff] [blame] | 577 | const struct object_id *oid, int flags, void *cb_data) |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 578 | { |
| 579 | struct rename_info *rename = cb_data; |
| 580 | struct strbuf buf = STRBUF_INIT; |
| 581 | struct string_list_item *item; |
| 582 | int flag; |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 583 | const char *symref; |
| 584 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 585 | strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 586 | if (starts_with(refname, buf.buf)) { |
René Scharfe | fe583c6 | 2018-08-01 12:19:07 +0200 | [diff] [blame] | 587 | item = string_list_append(rename->remote_branches, refname); |
Ronnie Sahlberg | 7695d11 | 2014-07-15 12:59:36 -0700 | [diff] [blame] | 588 | symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING, |
René Scharfe | 744c040 | 2017-09-23 11:45:04 +0200 | [diff] [blame] | 589 | NULL, &flag); |
Jeff King | 752848d | 2017-10-19 13:47:30 -0400 | [diff] [blame] | 590 | if (symref && (flag & REF_ISSYMREF)) |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 591 | item->util = xstrdup(symref); |
| 592 | else |
| 593 | item->util = NULL; |
| 594 | } |
Rene Scharfe | e2581b7 | 2017-08-30 20:00:25 +0200 | [diff] [blame] | 595 | strbuf_release(&buf); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 600 | static int migrate_file(struct remote *remote) |
| 601 | { |
| 602 | struct strbuf buf = STRBUF_INIT; |
| 603 | int i; |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 604 | |
| 605 | strbuf_addf(&buf, "remote.%s.url", remote->name); |
| 606 | for (i = 0; i < remote->url_nr; i++) |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 607 | git_config_set_multivar(buf.buf, remote->url[i], "^$", 0); |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 608 | strbuf_reset(&buf); |
| 609 | strbuf_addf(&buf, "remote.%s.push", remote->name); |
Brandon Williams | 6bdb304 | 2018-05-16 15:58:00 -0700 | [diff] [blame] | 610 | for (i = 0; i < remote->push.raw_nr; i++) |
| 611 | git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0); |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 612 | strbuf_reset(&buf); |
| 613 | strbuf_addf(&buf, "remote.%s.fetch", remote->name); |
Brandon Williams | e5349ab | 2018-05-16 15:58:01 -0700 | [diff] [blame] | 614 | for (i = 0; i < remote->fetch.raw_nr; i++) |
| 615 | git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0); |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 616 | if (remote->origin == REMOTE_REMOTES) |
Jeff King | b21a5d6 | 2015-08-10 05:35:49 -0400 | [diff] [blame] | 617 | unlink_or_warn(git_path("remotes/%s", remote->name)); |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 618 | else if (remote->origin == REMOTE_BRANCHES) |
Jeff King | b21a5d6 | 2015-08-10 05:35:49 -0400 | [diff] [blame] | 619 | unlink_or_warn(git_path("branches/%s", remote->name)); |
Rene Scharfe | b95c8ce | 2017-08-30 20:00:26 +0200 | [diff] [blame] | 620 | strbuf_release(&buf); |
Patrick Steinhardt | c397deb | 2016-02-22 12:23:30 +0100 | [diff] [blame] | 621 | |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Bert Wesarg | b3fd6cb | 2020-02-01 10:34:09 +0100 | [diff] [blame] | 625 | struct push_default_info |
| 626 | { |
| 627 | const char *old_name; |
| 628 | enum config_scope scope; |
| 629 | struct strbuf origin; |
| 630 | int linenr; |
| 631 | }; |
| 632 | |
| 633 | static int config_read_push_default(const char *key, const char *value, |
| 634 | void *cb) |
| 635 | { |
| 636 | struct push_default_info* info = cb; |
| 637 | if (strcmp(key, "remote.pushdefault") || |
| 638 | !value || strcmp(value, info->old_name)) |
| 639 | return 0; |
| 640 | |
| 641 | info->scope = current_config_scope(); |
| 642 | strbuf_reset(&info->origin); |
| 643 | strbuf_addstr(&info->origin, current_config_name()); |
| 644 | info->linenr = current_config_line(); |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static void handle_push_default(const char* old_name, const char* new_name) |
| 650 | { |
| 651 | struct push_default_info push_default = { |
| 652 | old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 }; |
| 653 | git_config(config_read_push_default, &push_default); |
| 654 | if (push_default.scope >= CONFIG_SCOPE_COMMAND) |
| 655 | ; /* pass */ |
| 656 | else if (push_default.scope >= CONFIG_SCOPE_LOCAL) { |
| 657 | int result = git_config_set_gently("remote.pushDefault", |
| 658 | new_name); |
| 659 | if (new_name && result && result != CONFIG_NOTHING_SET) |
| 660 | die(_("could not set '%s'"), "remote.pushDefault"); |
| 661 | else if (!new_name && result && result != CONFIG_NOTHING_SET) |
| 662 | die(_("could not unset '%s'"), "remote.pushDefault"); |
| 663 | } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) { |
| 664 | /* warn */ |
| 665 | warning(_("The %s configuration remote.pushDefault in:\n" |
| 666 | "\t%s:%d\n" |
| 667 | "now names the non-existent remote '%s'"), |
| 668 | config_scope_name(push_default.scope), |
| 669 | push_default.origin.buf, push_default.linenr, |
| 670 | old_name); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 675 | static int mv(int argc, const char **argv) |
| 676 | { |
| 677 | struct option options[] = { |
| 678 | OPT_END() |
| 679 | }; |
| 680 | struct remote *oldremote, *newremote; |
Martin von Zweigbergk | 28f555f | 2011-09-01 20:50:33 -0400 | [diff] [blame] | 681 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT, |
| 682 | old_remote_context = STRBUF_INIT; |
René Scharfe | fe583c6 | 2018-08-01 12:19:07 +0200 | [diff] [blame] | 683 | struct string_list remote_branches = STRING_LIST_INIT_DUP; |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 684 | struct rename_info rename; |
Martin von Zweigbergk | b52d00a | 2011-09-10 15:39:23 -0400 | [diff] [blame] | 685 | int i, refspec_updated = 0; |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 686 | |
| 687 | if (argc != 3) |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 688 | usage_with_options(builtin_remote_rename_usage, options); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 689 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 690 | rename.old_name = argv[1]; |
| 691 | rename.new_name = argv[2]; |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 692 | rename.remote_branches = &remote_branches; |
| 693 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 694 | oldremote = remote_get(rename.old_name); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 695 | if (!remote_is_configured(oldremote, 1)) { |
| 696 | error(_("No such remote: '%s'"), rename.old_name); |
| 697 | exit(2); |
| 698 | } |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 699 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 700 | if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG) |
Miklos Vajna | 1dd1239 | 2008-11-10 21:43:01 +0100 | [diff] [blame] | 701 | return migrate_file(oldremote); |
| 702 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 703 | newremote = remote_get(rename.new_name); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 704 | if (remote_is_configured(newremote, 1)) { |
| 705 | error(_("remote %s already exists."), rename.new_name); |
| 706 | exit(3); |
| 707 | } |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 708 | |
Sean Barag | f2c6fda | 2020-10-01 03:46:13 +0000 | [diff] [blame] | 709 | if (!valid_remote_name(rename.new_name)) |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 710 | die(_("'%s' is not a valid remote name"), rename.new_name); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 711 | |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 712 | strbuf_addf(&buf, "remote.%s", rename.old_name); |
| 713 | strbuf_addf(&buf2, "remote.%s", rename.new_name); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 714 | if (git_config_rename_section(buf.buf, buf2.buf) < 1) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 715 | return error(_("Could not rename config section '%s' to '%s'"), |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 716 | buf.buf, buf2.buf); |
| 717 | |
| 718 | strbuf_reset(&buf); |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 719 | strbuf_addf(&buf, "remote.%s.fetch", rename.new_name); |
Derrick Stolee | 504ee12 | 2020-11-25 22:12:49 +0000 | [diff] [blame] | 720 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 721 | strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name); |
Brandon Williams | e5349ab | 2018-05-16 15:58:01 -0700 | [diff] [blame] | 722 | for (i = 0; i < oldremote->fetch.raw_nr; i++) { |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 723 | char *ptr; |
| 724 | |
| 725 | strbuf_reset(&buf2); |
Brandon Williams | e5349ab | 2018-05-16 15:58:01 -0700 | [diff] [blame] | 726 | strbuf_addstr(&buf2, oldremote->fetch.raw[i]); |
Martin von Zweigbergk | 28f555f | 2011-09-01 20:50:33 -0400 | [diff] [blame] | 727 | ptr = strstr(buf2.buf, old_remote_context.buf); |
Martin von Zweigbergk | b52d00a | 2011-09-10 15:39:23 -0400 | [diff] [blame] | 728 | if (ptr) { |
| 729 | refspec_updated = 1; |
Martin von Zweigbergk | 28f555f | 2011-09-01 20:50:33 -0400 | [diff] [blame] | 730 | strbuf_splice(&buf2, |
| 731 | ptr-buf2.buf + strlen(":refs/remotes/"), |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 732 | strlen(rename.old_name), rename.new_name, |
| 733 | strlen(rename.new_name)); |
Martin von Zweigbergk | b52d00a | 2011-09-10 15:39:23 -0400 | [diff] [blame] | 734 | } else |
Ralf Thielow | aa3bb87 | 2012-05-18 18:46:01 +0200 | [diff] [blame] | 735 | warning(_("Not updating non-default fetch refspec\n" |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 736 | "\t%s\n" |
| 737 | "\tPlease update the configuration manually if necessary."), |
Martin von Zweigbergk | 1822b86 | 2011-09-03 11:26:59 -0400 | [diff] [blame] | 738 | buf2.buf); |
| 739 | |
Patrick Steinhardt | 3d18064 | 2016-02-22 12:23:36 +0100 | [diff] [blame] | 740 | git_config_set_multivar(buf.buf, buf2.buf, "^$", 0); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | read_branches(); |
| 744 | for (i = 0; i < branch_list.nr; i++) { |
| 745 | struct string_list_item *item = branch_list.items + i; |
| 746 | struct branch_info *info = item->util; |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 747 | if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) { |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 748 | strbuf_reset(&buf); |
| 749 | strbuf_addf(&buf, "branch.%s.remote", item->string); |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 750 | git_config_set(buf.buf, rename.new_name); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 751 | } |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 752 | if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) { |
| 753 | strbuf_reset(&buf); |
Ævar Arnfjörð Bjarmason | 0f1da60 | 2021-02-25 02:21:17 +0100 | [diff] [blame] | 754 | strbuf_addf(&buf, "branch.%s.pushRemote", item->string); |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 755 | git_config_set(buf.buf, rename.new_name); |
| 756 | } |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 757 | } |
| 758 | |
Martin von Zweigbergk | b52d00a | 2011-09-10 15:39:23 -0400 | [diff] [blame] | 759 | if (!refspec_updated) |
| 760 | return 0; |
| 761 | |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 762 | /* |
| 763 | * First remove symrefs, then rename the rest, finally create |
| 764 | * the new symrefs. |
| 765 | */ |
Michael Haggerty | 53dc95b | 2015-05-25 18:38:43 +0000 | [diff] [blame] | 766 | for_each_ref(read_remote_branches, &rename); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 767 | for (i = 0; i < remote_branches.nr; i++) { |
| 768 | struct string_list_item *item = remote_branches.items + i; |
Patrick Steinhardt | 1553f5e | 2022-03-01 10:33:50 +0100 | [diff] [blame^] | 769 | struct strbuf referent = STRBUF_INIT; |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 770 | |
Patrick Steinhardt | 1553f5e | 2022-03-01 10:33:50 +0100 | [diff] [blame^] | 771 | if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string, |
| 772 | &referent)) |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 773 | continue; |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 774 | if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 775 | die(_("deleting '%s' failed"), item->string); |
Patrick Steinhardt | 1553f5e | 2022-03-01 10:33:50 +0100 | [diff] [blame^] | 776 | |
| 777 | strbuf_release(&referent); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 778 | } |
| 779 | for (i = 0; i < remote_branches.nr; i++) { |
| 780 | struct string_list_item *item = remote_branches.items + i; |
| 781 | |
| 782 | if (item->util) |
| 783 | continue; |
| 784 | strbuf_reset(&buf); |
| 785 | strbuf_addstr(&buf, item->string); |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 786 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name), |
| 787 | rename.new_name, strlen(rename.new_name)); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 788 | strbuf_reset(&buf2); |
| 789 | strbuf_addf(&buf2, "remote: renamed %s to %s", |
| 790 | item->string, buf.buf); |
| 791 | if (rename_ref(item->string, buf.buf, buf2.buf)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 792 | die(_("renaming '%s' failed"), item->string); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 793 | } |
| 794 | for (i = 0; i < remote_branches.nr; i++) { |
| 795 | struct string_list_item *item = remote_branches.items + i; |
| 796 | |
| 797 | if (!item->util) |
| 798 | continue; |
| 799 | strbuf_reset(&buf); |
| 800 | strbuf_addstr(&buf, item->string); |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 801 | strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name), |
| 802 | rename.new_name, strlen(rename.new_name)); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 803 | strbuf_reset(&buf2); |
| 804 | strbuf_addstr(&buf2, item->util); |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 805 | strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name), |
| 806 | rename.new_name, strlen(rename.new_name)); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 807 | strbuf_reset(&buf3); |
| 808 | strbuf_addf(&buf3, "remote: renamed %s to %s", |
| 809 | item->string, buf.buf); |
| 810 | if (create_symref(buf.buf, buf2.buf, buf3.buf)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 811 | die(_("creating '%s' failed"), buf.buf); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 812 | } |
René Scharfe | fe583c6 | 2018-08-01 12:19:07 +0200 | [diff] [blame] | 813 | string_list_clear(&remote_branches, 1); |
Bert Wesarg | b3fd6cb | 2020-02-01 10:34:09 +0100 | [diff] [blame] | 814 | |
| 815 | handle_push_default(rename.old_name, rename.new_name); |
| 816 | |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 817 | return 0; |
| 818 | } |
| 819 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 820 | static int rm(int argc, const char **argv) |
| 821 | { |
| 822 | struct option options[] = { |
| 823 | OPT_END() |
| 824 | }; |
| 825 | struct remote *remote; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 826 | struct strbuf buf = STRBUF_INIT; |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 827 | struct known_remotes known_remotes = { NULL, NULL }; |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 828 | struct string_list branches = STRING_LIST_INIT_DUP; |
| 829 | struct string_list skipped = STRING_LIST_INIT_DUP; |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 830 | struct branches_for_remote cb_data; |
Jay Soffian | e02f176 | 2009-02-03 12:51:12 -0500 | [diff] [blame] | 831 | int i, result; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 832 | |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 833 | memset(&cb_data, 0, sizeof(cb_data)); |
| 834 | cb_data.branches = &branches; |
| 835 | cb_data.skipped = &skipped; |
| 836 | cb_data.keep = &known_remotes; |
| 837 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 838 | if (argc != 2) |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 839 | usage_with_options(builtin_remote_rm_usage, options); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 840 | |
| 841 | remote = remote_get(argv[1]); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 842 | if (!remote_is_configured(remote, 1)) { |
| 843 | error(_("No such remote: '%s'"), argv[1]); |
| 844 | exit(2); |
| 845 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 846 | |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 847 | known_remotes.to_delete = remote; |
| 848 | for_each_remote(add_known_remote, &known_remotes); |
| 849 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 850 | read_branches(); |
| 851 | for (i = 0; i < branch_list.nr; i++) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 852 | struct string_list_item *item = branch_list.items + i; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 853 | struct branch_info *info = item->util; |
Jay Soffian | e0cc81e | 2009-02-25 03:32:21 -0500 | [diff] [blame] | 854 | if (info->remote_name && !strcmp(info->remote_name, remote->name)) { |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 855 | const char *keys[] = { "remote", "merge", NULL }, **k; |
| 856 | for (k = keys; *k; k++) { |
| 857 | strbuf_reset(&buf); |
| 858 | strbuf_addf(&buf, "branch.%s.%s", |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 859 | item->string, *k); |
Ross Lagerwall | 20690b2 | 2017-02-18 00:23:41 +0000 | [diff] [blame] | 860 | result = git_config_set_gently(buf.buf, NULL); |
| 861 | if (result && result != CONFIG_NOTHING_SET) |
| 862 | die(_("could not unset '%s'"), buf.buf); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 863 | } |
| 864 | } |
Bert Wesarg | 923d4a5 | 2020-01-27 08:04:30 +0100 | [diff] [blame] | 865 | if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) { |
| 866 | strbuf_reset(&buf); |
| 867 | strbuf_addf(&buf, "branch.%s.pushremote", item->string); |
| 868 | result = git_config_set_gently(buf.buf, NULL); |
| 869 | if (result && result != CONFIG_NOTHING_SET) |
| 870 | die(_("could not unset '%s'"), buf.buf); |
| 871 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* |
| 875 | * We cannot just pass a function to for_each_ref() which deletes |
| 876 | * the branches one by one, since for_each_ref() relies on cached |
| 877 | * refs, which are invalidated when deleting a branch. |
| 878 | */ |
Shawn O. Pearce | 7ad2458 | 2008-06-01 00:28:04 -0400 | [diff] [blame] | 879 | cb_data.remote = remote; |
Michael Haggerty | 45690a5 | 2015-05-25 18:38:41 +0000 | [diff] [blame] | 880 | result = for_each_ref(add_branch_for_removal, &cb_data); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 881 | strbuf_release(&buf); |
| 882 | |
Jay Soffian | e02f176 | 2009-02-03 12:51:12 -0500 | [diff] [blame] | 883 | if (!result) |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 884 | result = delete_refs("remote: remove", &branches, REF_NO_DEREF); |
Michael Haggerty | e26cdf9 | 2015-05-25 18:38:42 +0000 | [diff] [blame] | 885 | string_list_clear(&branches, 0); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 886 | |
Jay Soffian | 441adf0 | 2009-02-04 11:06:07 -0500 | [diff] [blame] | 887 | if (skipped.nr) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 888 | fprintf_ln(stderr, |
| 889 | Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n" |
| 890 | "to delete it, use:", |
| 891 | "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n" |
| 892 | "to delete them, use:", |
| 893 | skipped.nr)); |
Jay Soffian | 441adf0 | 2009-02-04 11:06:07 -0500 | [diff] [blame] | 894 | for (i = 0; i < skipped.nr; i++) |
| 895 | fprintf(stderr, " git branch -d %s\n", |
| 896 | skipped.items[i].string); |
| 897 | } |
| 898 | string_list_clear(&skipped, 0); |
| 899 | |
Jens Lindström | b07bdd3 | 2014-05-23 12:28:43 +0200 | [diff] [blame] | 900 | if (!result) { |
| 901 | strbuf_addf(&buf, "remote.%s", remote->name); |
| 902 | if (git_config_rename_section(buf.buf, NULL) < 1) |
| 903 | return error(_("Could not remove config section '%s'"), buf.buf); |
Bert Wesarg | b3fd6cb | 2020-02-01 10:34:09 +0100 | [diff] [blame] | 904 | |
| 905 | handle_push_default(remote->name, NULL); |
Jens Lindström | b07bdd3 | 2014-05-23 12:28:43 +0200 | [diff] [blame] | 906 | } |
| 907 | |
Jay Soffian | e02f176 | 2009-02-03 12:51:12 -0500 | [diff] [blame] | 908 | return result; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 911 | static void clear_push_info(void *util, const char *string) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 912 | { |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 913 | struct push_info *info = util; |
| 914 | free(info->dest); |
| 915 | free(info); |
| 916 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 917 | |
Jay Soffian | 8873323 | 2009-02-25 03:32:19 -0500 | [diff] [blame] | 918 | static void free_remote_ref_states(struct ref_states *states) |
| 919 | { |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 920 | string_list_clear(&states->new_refs, 0); |
Bert Wesarg | 92f676f | 2009-12-01 00:57:27 +0100 | [diff] [blame] | 921 | string_list_clear(&states->stale, 1); |
Jay Soffian | 8873323 | 2009-02-25 03:32:19 -0500 | [diff] [blame] | 922 | string_list_clear(&states->tracked, 0); |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 923 | string_list_clear(&states->heads, 0); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 924 | string_list_clear_func(&states->push, clear_push_info); |
Jay Soffian | 8873323 | 2009-02-25 03:32:19 -0500 | [diff] [blame] | 925 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 926 | |
Jay Soffian | cca7c97 | 2009-02-25 03:32:22 -0500 | [diff] [blame] | 927 | static int append_ref_to_tracked_list(const char *refname, |
Michael Haggerty | 53dc95b | 2015-05-25 18:38:43 +0000 | [diff] [blame] | 928 | const struct object_id *oid, int flags, void *cb_data) |
Jay Soffian | cca7c97 | 2009-02-25 03:32:22 -0500 | [diff] [blame] | 929 | { |
| 930 | struct ref_states *states = cb_data; |
Brandon Williams | 0ad4a5f | 2018-05-16 15:57:49 -0700 | [diff] [blame] | 931 | struct refspec_item refspec; |
Jay Soffian | cca7c97 | 2009-02-25 03:32:22 -0500 | [diff] [blame] | 932 | |
Jay Soffian | 3bd9256 | 2009-02-25 03:32:23 -0500 | [diff] [blame] | 933 | if (flags & REF_ISSYMREF) |
| 934 | return 0; |
| 935 | |
Jay Soffian | cca7c97 | 2009-02-25 03:32:22 -0500 | [diff] [blame] | 936 | memset(&refspec, 0, sizeof(refspec)); |
| 937 | refspec.dst = (char *)refname; |
| 938 | if (!remote_find_tracking(states->remote, &refspec)) |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 939 | string_list_append(&states->tracked, abbrev_branch(refspec.src)); |
Jay Soffian | cca7c97 | 2009-02-25 03:32:22 -0500 | [diff] [blame] | 940 | |
| 941 | return 0; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 944 | static int get_remote_ref_states(const char *name, |
| 945 | struct ref_states *states, |
| 946 | int query) |
| 947 | { |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 948 | states->remote = remote_get(name); |
| 949 | if (!states->remote) |
Shulhan | 5025425 | 2018-09-13 20:18:33 +0700 | [diff] [blame] | 950 | return error(_("No such remote: '%s'"), name); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 951 | |
| 952 | read_branches(); |
| 953 | |
| 954 | if (query) { |
Andrzej Hunt | 68ffe09 | 2021-03-21 16:58:37 +0000 | [diff] [blame] | 955 | struct transport *transport; |
| 956 | const struct ref *remote_refs; |
| 957 | |
Chris Frey | 345a380 | 2009-06-25 17:21:35 -0400 | [diff] [blame] | 958 | transport = transport_get(states->remote, states->remote->url_nr > 0 ? |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 959 | states->remote->url[0] : NULL); |
Brandon Williams | 1af8ae1 | 2018-03-15 10:31:23 -0700 | [diff] [blame] | 960 | remote_refs = transport_get_remote_refs(transport, NULL); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 961 | |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 962 | states->queried = 1; |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 963 | if (query & GET_REF_STATES) |
| 964 | get_ref_states(remote_refs, states); |
| 965 | if (query & GET_HEAD_NAMES) |
| 966 | get_head_names(remote_refs, states); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 967 | if (query & GET_PUSH_REF_STATES) |
| 968 | get_push_ref_states(remote_refs, states); |
Andrzej Hunt | 68ffe09 | 2021-03-21 16:58:37 +0000 | [diff] [blame] | 969 | transport_disconnect(transport); |
Jay Soffian | 3bd9256 | 2009-02-25 03:32:23 -0500 | [diff] [blame] | 970 | } else { |
Michael Haggerty | 53dc95b | 2015-05-25 18:38:43 +0000 | [diff] [blame] | 971 | for_each_ref(append_ref_to_tracked_list, states); |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 972 | string_list_sort(&states->tracked); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 973 | get_push_ref_states_noquery(states); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | return 0; |
| 977 | } |
| 978 | |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 979 | struct show_info { |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 980 | struct string_list list; |
| 981 | struct ref_states states; |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 982 | int width, width2; |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 983 | int any_rebase; |
| 984 | }; |
| 985 | |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 986 | #define SHOW_INFO_INIT { \ |
| 987 | .list = STRING_LIST_INIT_DUP, \ |
| 988 | .states = REF_STATES_INIT, \ |
| 989 | } |
| 990 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 991 | static int add_remote_to_show_info(struct string_list_item *item, void *cb_data) |
Olivier Marin | e7d5a97 | 2008-06-11 00:54:49 +0200 | [diff] [blame] | 992 | { |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 993 | struct show_info *info = cb_data; |
| 994 | int n = strlen(item->string); |
| 995 | if (n > info->width) |
| 996 | info->width = n; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 997 | string_list_insert(&info->list, item->string); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 998 | return 0; |
| 999 | } |
Olivier Marin | e7d5a97 | 2008-06-11 00:54:49 +0200 | [diff] [blame] | 1000 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1001 | static int show_remote_info_item(struct string_list_item *item, void *cb_data) |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1002 | { |
| 1003 | struct show_info *info = cb_data; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1004 | struct ref_states *states = &info->states; |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1005 | const char *name = item->string; |
Olivier Marin | e7d5a97 | 2008-06-11 00:54:49 +0200 | [diff] [blame] | 1006 | |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1007 | if (states->queried) { |
| 1008 | const char *fmt = "%s"; |
| 1009 | const char *arg = ""; |
Brandon Williams | b537e0b | 2018-02-14 10:59:35 -0800 | [diff] [blame] | 1010 | if (string_list_has_string(&states->new_refs, name)) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1011 | fmt = _(" new (next fetch will store in remotes/%s)"); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1012 | arg = states->remote->name; |
| 1013 | } else if (string_list_has_string(&states->tracked, name)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1014 | arg = _(" tracked"); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1015 | else if (string_list_has_string(&states->stale, name)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1016 | arg = _(" stale (use 'git remote prune' to remove)"); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1017 | else |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1018 | arg = _(" ???"); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1019 | printf(" %-*s", info->width, name); |
| 1020 | printf(fmt, arg); |
| 1021 | printf("\n"); |
| 1022 | } else |
| 1023 | printf(" %s\n", name); |
| 1024 | |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1028 | static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data) |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1029 | { |
| 1030 | struct show_info *show_info = cb_data; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1031 | struct ref_states *states = &show_info->states; |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1032 | struct branch_info *branch_info = branch_item->util; |
| 1033 | struct string_list_item *item; |
| 1034 | int n; |
| 1035 | |
| 1036 | if (!branch_info->merge.nr || !branch_info->remote_name || |
| 1037 | strcmp(states->remote->name, branch_info->remote_name)) |
| 1038 | return 0; |
| 1039 | if ((n = strlen(branch_item->string)) > show_info->width) |
| 1040 | show_info->width = n; |
Bert Wesarg | 88f8576 | 2020-01-27 08:04:27 +0100 | [diff] [blame] | 1041 | if (branch_info->rebase >= REBASE_TRUE) |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1042 | show_info->any_rebase = 1; |
| 1043 | |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1044 | item = string_list_insert(&show_info->list, branch_item->string); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1045 | item->util = branch_info; |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1050 | static int show_local_info_item(struct string_list_item *item, void *cb_data) |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1051 | { |
| 1052 | struct show_info *show_info = cb_data; |
| 1053 | struct branch_info *branch_info = item->util; |
| 1054 | struct string_list *merge = &branch_info->merge; |
Vasco Almeida | a1b467a | 2016-06-17 20:21:22 +0000 | [diff] [blame] | 1055 | int width = show_info->width + 4; |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1056 | int i; |
| 1057 | |
Bert Wesarg | 88f8576 | 2020-01-27 08:04:27 +0100 | [diff] [blame] | 1058 | if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1059 | error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"), |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1060 | item->string); |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | printf(" %-*s ", show_info->width, item->string); |
Bert Wesarg | 88f8576 | 2020-01-27 08:04:27 +0100 | [diff] [blame] | 1065 | if (branch_info->rebase >= REBASE_TRUE) { |
Johannes Schindelin | 1131ec9 | 2018-04-25 14:29:38 +0200 | [diff] [blame] | 1066 | const char *msg; |
Bert Wesarg | 88f8576 | 2020-01-27 08:04:27 +0100 | [diff] [blame] | 1067 | if (branch_info->rebase == REBASE_INTERACTIVE) |
Johannes Schindelin | 1131ec9 | 2018-04-25 14:29:38 +0200 | [diff] [blame] | 1068 | msg = _("rebases interactively onto remote %s"); |
| 1069 | else if (branch_info->rebase == REBASE_MERGES) |
| 1070 | msg = _("rebases interactively (with merges) onto " |
| 1071 | "remote %s"); |
| 1072 | else |
| 1073 | msg = _("rebases onto remote %s"); |
| 1074 | printf_ln(msg, merge->items[0].string); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1075 | return 0; |
| 1076 | } else if (show_info->any_rebase) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1077 | printf_ln(_(" merges with remote %s"), merge->items[0].string); |
Vasco Almeida | a1b467a | 2016-06-17 20:21:22 +0000 | [diff] [blame] | 1078 | width++; |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1079 | } else { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1080 | printf_ln(_("merges with remote %s"), merge->items[0].string); |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1081 | } |
| 1082 | for (i = 1; i < merge->nr; i++) |
Vasco Almeida | a1b467a | 2016-06-17 20:21:22 +0000 | [diff] [blame] | 1083 | printf(_("%-*s and with remote %s\n"), width, "", |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1084 | merge->items[i].string); |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1089 | static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1090 | { |
| 1091 | struct show_info *show_info = cb_data; |
| 1092 | struct push_info *push_info = push_item->util; |
| 1093 | struct string_list_item *item; |
| 1094 | int n; |
| 1095 | if ((n = strlen(push_item->string)) > show_info->width) |
| 1096 | show_info->width = n; |
| 1097 | if ((n = strlen(push_info->dest)) > show_info->width2) |
| 1098 | show_info->width2 = n; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1099 | item = string_list_append(&show_info->list, push_item->string); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1100 | item->util = push_item->util; |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Jeff King | 48ef563 | 2009-03-22 04:59:20 -0400 | [diff] [blame] | 1104 | /* |
| 1105 | * Sorting comparison for a string list that has push_info |
| 1106 | * structs in its util field |
| 1107 | */ |
| 1108 | static int cmp_string_with_push(const void *va, const void *vb) |
| 1109 | { |
| 1110 | const struct string_list_item *a = va; |
| 1111 | const struct string_list_item *b = vb; |
| 1112 | const struct push_info *a_push = a->util; |
| 1113 | const struct push_info *b_push = b->util; |
| 1114 | int cmp = strcmp(a->string, b->string); |
| 1115 | return cmp ? cmp : strcmp(a_push->dest, b_push->dest); |
| 1116 | } |
| 1117 | |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1118 | static int show_push_info_item(struct string_list_item *item, void *cb_data) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1119 | { |
| 1120 | struct show_info *show_info = cb_data; |
| 1121 | struct push_info *push_info = item->util; |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1122 | const char *src = item->string, *status = NULL; |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1123 | |
| 1124 | switch (push_info->status) { |
| 1125 | case PUSH_STATUS_CREATE: |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1126 | status = _("create"); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1127 | break; |
| 1128 | case PUSH_STATUS_DELETE: |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1129 | status = _("delete"); |
| 1130 | src = _("(none)"); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1131 | break; |
| 1132 | case PUSH_STATUS_UPTODATE: |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1133 | status = _("up to date"); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1134 | break; |
| 1135 | case PUSH_STATUS_FASTFORWARD: |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1136 | status = _("fast-forwardable"); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1137 | break; |
| 1138 | case PUSH_STATUS_OUTOFDATE: |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1139 | status = _("local out of date"); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1140 | break; |
| 1141 | case PUSH_STATUS_NOTQUERIED: |
| 1142 | break; |
| 1143 | } |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1144 | if (status) { |
| 1145 | if (push_info->forced) |
| 1146 | printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src, |
| 1147 | show_info->width2, push_info->dest, status); |
| 1148 | else |
| 1149 | printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src, |
| 1150 | show_info->width2, push_info->dest, status); |
| 1151 | } else { |
| 1152 | if (push_info->forced) |
| 1153 | printf_ln(_(" %-*s forces to %s"), show_info->width, src, |
| 1154 | push_info->dest); |
| 1155 | else |
| 1156 | printf_ln(_(" %-*s pushes to %s"), show_info->width, src, |
| 1157 | push_info->dest); |
| 1158 | } |
Olivier Marin | e7d5a97 | 2008-06-11 00:54:49 +0200 | [diff] [blame] | 1159 | return 0; |
| 1160 | } |
| 1161 | |
Michael Haggerty | ce2223f | 2013-10-30 06:33:02 +0100 | [diff] [blame] | 1162 | static int get_one_entry(struct remote *remote, void *priv) |
| 1163 | { |
| 1164 | struct string_list *list = priv; |
| 1165 | struct strbuf url_buf = STRBUF_INIT; |
| 1166 | const char **url; |
| 1167 | int i, url_nr; |
| 1168 | |
| 1169 | if (remote->url_nr > 0) { |
| 1170 | strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]); |
| 1171 | string_list_append(list, remote->name)->util = |
| 1172 | strbuf_detach(&url_buf, NULL); |
| 1173 | } else |
| 1174 | string_list_append(list, remote->name)->util = NULL; |
| 1175 | if (remote->pushurl_nr) { |
| 1176 | url = remote->pushurl; |
| 1177 | url_nr = remote->pushurl_nr; |
| 1178 | } else { |
| 1179 | url = remote->url; |
| 1180 | url_nr = remote->url_nr; |
| 1181 | } |
| 1182 | for (i = 0; i < url_nr; i++) |
| 1183 | { |
| 1184 | strbuf_addf(&url_buf, "%s (push)", url[i]); |
| 1185 | string_list_append(list, remote->name)->util = |
| 1186 | strbuf_detach(&url_buf, NULL); |
| 1187 | } |
| 1188 | |
| 1189 | return 0; |
| 1190 | } |
| 1191 | |
| 1192 | static int show_all(void) |
| 1193 | { |
| 1194 | struct string_list list = STRING_LIST_INIT_NODUP; |
| 1195 | int result; |
| 1196 | |
| 1197 | list.strdup_strings = 1; |
| 1198 | result = for_each_remote(get_one_entry, &list); |
| 1199 | |
| 1200 | if (!result) { |
| 1201 | int i; |
| 1202 | |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 1203 | string_list_sort(&list); |
Michael Haggerty | ce2223f | 2013-10-30 06:33:02 +0100 | [diff] [blame] | 1204 | for (i = 0; i < list.nr; i++) { |
| 1205 | struct string_list_item *item = list.items + i; |
| 1206 | if (verbose) |
| 1207 | printf("%s\t%s\n", item->string, |
| 1208 | item->util ? (const char *)item->util : ""); |
| 1209 | else { |
| 1210 | if (i && !strcmp((item - 1)->string, item->string)) |
| 1211 | continue; |
| 1212 | printf("%s\n", item->string); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | string_list_clear(&list, 1); |
| 1217 | return result; |
| 1218 | } |
| 1219 | |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1220 | static int show(int argc, const char **argv) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1221 | { |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 1222 | int no_query = 0, result = 0, query_flag = 0; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1223 | struct option options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1224 | OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")), |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1225 | OPT_END() |
| 1226 | }; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1227 | struct show_info info = SHOW_INFO_INIT; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1228 | |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 1229 | argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage, |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 1230 | 0); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1231 | |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1232 | if (argc < 1) |
| 1233 | return show_all(); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1234 | |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 1235 | if (!no_query) |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1236 | query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES); |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 1237 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1238 | for (; argc; argc--, argv++) { |
Olivier Marin | 0ecfcb3 | 2008-06-10 16:51:08 +0200 | [diff] [blame] | 1239 | int i; |
Michael J Gruber | 857f8c3 | 2009-06-13 18:29:10 +0200 | [diff] [blame] | 1240 | const char **url; |
| 1241 | int url_nr; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1242 | |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1243 | get_remote_ref_states(*argv, &info.states, query_flag); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1244 | |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1245 | printf_ln(_("* remote %s"), *argv); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1246 | printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ? |
| 1247 | info.states.remote->url[0] : _("(no URL)")); |
| 1248 | if (info.states.remote->pushurl_nr) { |
| 1249 | url = info.states.remote->pushurl; |
| 1250 | url_nr = info.states.remote->pushurl_nr; |
Michael J Gruber | 857f8c3 | 2009-06-13 18:29:10 +0200 | [diff] [blame] | 1251 | } else { |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1252 | url = info.states.remote->url; |
| 1253 | url_nr = info.states.remote->url_nr; |
Michael J Gruber | 857f8c3 | 2009-06-13 18:29:10 +0200 | [diff] [blame] | 1254 | } |
Junio C Hamano | cd2b8ae | 2011-08-25 14:46:52 -0700 | [diff] [blame] | 1255 | for (i = 0; i < url_nr; i++) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 1256 | /* |
| 1257 | * TRANSLATORS: the colon ':' should align |
| 1258 | * with the one in " Fetch URL: %s" |
| 1259 | * translation. |
| 1260 | */ |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1261 | printf_ln(_(" Push URL: %s"), url[i]); |
Michael J Gruber | 857f8c3 | 2009-06-13 18:29:10 +0200 | [diff] [blame] | 1262 | if (!i) |
Vasco Almeida | 7ba7b9a | 2016-06-17 20:21:21 +0000 | [diff] [blame] | 1263 | printf_ln(_(" Push URL: %s"), _("(no URL)")); |
Olivier Marin | e7d5a97 | 2008-06-11 00:54:49 +0200 | [diff] [blame] | 1264 | if (no_query) |
Vasco Almeida | 7ba7b9a | 2016-06-17 20:21:21 +0000 | [diff] [blame] | 1265 | printf_ln(_(" HEAD branch: %s"), _("(not queried)")); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1266 | else if (!info.states.heads.nr) |
Vasco Almeida | 7ba7b9a | 2016-06-17 20:21:21 +0000 | [diff] [blame] | 1267 | printf_ln(_(" HEAD branch: %s"), _("(unknown)")); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1268 | else if (info.states.heads.nr == 1) |
| 1269 | printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string); |
Jay Soffian | e61e0cc | 2009-02-25 03:32:24 -0500 | [diff] [blame] | 1270 | else { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1271 | printf(_(" HEAD branch (remote HEAD is ambiguous," |
| 1272 | " may be one of the following):\n")); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1273 | for (i = 0; i < info.states.heads.nr; i++) |
| 1274 | printf(" %s\n", info.states.heads.items[i].string); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1275 | } |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1276 | |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1277 | /* remote branch info */ |
| 1278 | info.width = 0; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1279 | for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info); |
| 1280 | for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info); |
| 1281 | for_each_string_list(&info.states.stale, add_remote_to_show_info, &info); |
| 1282 | if (info.list.nr) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1283 | printf_ln(Q_(" Remote branch:%s", |
| 1284 | " Remote branches:%s", |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1285 | info.list.nr), |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1286 | no_query ? _(" (status not queried)") : ""); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1287 | for_each_string_list(&info.list, show_remote_info_item, &info); |
| 1288 | string_list_clear(&info.list, 0); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1289 | |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1290 | /* git pull info */ |
| 1291 | info.width = 0; |
| 1292 | info.any_rebase = 0; |
Julian Phillips | b684e97 | 2010-06-26 00:41:34 +0100 | [diff] [blame] | 1293 | for_each_string_list(&branch_list, add_local_to_show_info, &info); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1294 | if (info.list.nr) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1295 | printf_ln(Q_(" Local branch configured for 'git pull':", |
| 1296 | " Local branches configured for 'git pull':", |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1297 | info.list.nr)); |
| 1298 | for_each_string_list(&info.list, show_local_info_item, &info); |
| 1299 | string_list_clear(&info.list, 0); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1300 | |
Jay Soffian | 7ecbbf8 | 2009-02-25 03:32:27 -0500 | [diff] [blame] | 1301 | /* git push info */ |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1302 | if (info.states.remote->mirror) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1303 | printf_ln(_(" Local refs will be mirrored by 'git push'")); |
Jay Soffian | e5dcbfd | 2009-02-25 03:32:28 -0500 | [diff] [blame] | 1304 | |
| 1305 | info.width = info.width2 = 0; |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1306 | for_each_string_list(&info.states.push, add_push_to_show_info, &info); |
| 1307 | QSORT(info.list.items, info.list.nr, cmp_string_with_push); |
| 1308 | if (info.list.nr) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1309 | printf_ln(Q_(" Local ref configured for 'git push'%s:", |
| 1310 | " Local refs configured for 'git push'%s:", |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1311 | info.list.nr), |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1312 | no_query ? _(" (status not queried)") : ""); |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1313 | for_each_string_list(&info.list, show_push_info_item, &info); |
| 1314 | string_list_clear(&info.list, 0); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1315 | |
Ævar Arnfjörð Bjarmason | 0000e81 | 2021-10-01 12:27:35 +0200 | [diff] [blame] | 1316 | free_remote_ref_states(&info.states); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | return result; |
| 1320 | } |
| 1321 | |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1322 | static int set_head(int argc, const char **argv) |
| 1323 | { |
| 1324 | int i, opt_a = 0, opt_d = 0, result = 0; |
| 1325 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
| 1326 | char *head_name = NULL; |
| 1327 | |
| 1328 | struct option options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1329 | OPT_BOOL('a', "auto", &opt_a, |
| 1330 | N_("set refs/remotes/<name>/HEAD according to remote")), |
| 1331 | OPT_BOOL('d', "delete", &opt_d, |
| 1332 | N_("delete refs/remotes/<name>/HEAD")), |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1333 | OPT_END() |
| 1334 | }; |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 1335 | argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage, |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 1336 | 0); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1337 | if (argc) |
| 1338 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]); |
| 1339 | |
| 1340 | if (!opt_a && !opt_d && argc == 2) { |
| 1341 | head_name = xstrdup(argv[1]); |
| 1342 | } else if (opt_a && !opt_d && argc == 1) { |
Ævar Arnfjörð Bjarmason | 0bc7787 | 2021-10-01 12:27:34 +0200 | [diff] [blame] | 1343 | struct ref_states states = REF_STATES_INIT; |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1344 | get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES); |
| 1345 | if (!states.heads.nr) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1346 | result |= error(_("Cannot determine remote HEAD")); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1347 | else if (states.heads.nr > 1) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1348 | result |= error(_("Multiple remote HEAD branches. " |
| 1349 | "Please choose one explicitly with:")); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1350 | for (i = 0; i < states.heads.nr; i++) |
| 1351 | fprintf(stderr, " git remote set-head %s %s\n", |
| 1352 | argv[0], states.heads.items[i].string); |
| 1353 | } else |
| 1354 | head_name = xstrdup(states.heads.items[0].string); |
| 1355 | free_remote_ref_states(&states); |
| 1356 | } else if (opt_d && !opt_a && argc == 1) { |
Michael Haggerty | 91774af | 2017-11-05 09:42:06 +0100 | [diff] [blame] | 1357 | if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1358 | result |= error(_("Could not delete %s"), buf.buf); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1359 | } else |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 1360 | usage_with_options(builtin_remote_sethead_usage, options); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1361 | |
| 1362 | if (head_name) { |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1363 | strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name); |
| 1364 | /* make sure it's valid */ |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 1365 | if (!ref_exists(buf2.buf)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1366 | result |= error(_("Not a valid ref: %s"), buf2.buf); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1367 | else if (create_symref(buf.buf, buf2.buf, "remote set-head")) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1368 | result |= error(_("Could not setup %s"), buf.buf); |
Christian Schlack | 5a07c6c | 2020-09-17 15:27:38 +0000 | [diff] [blame] | 1369 | else if (opt_a) |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1370 | printf("%s/HEAD set to %s\n", argv[0], head_name); |
| 1371 | free(head_name); |
| 1372 | } |
| 1373 | |
| 1374 | strbuf_release(&buf); |
| 1375 | strbuf_release(&buf2); |
| 1376 | return result; |
| 1377 | } |
| 1378 | |
Finn Arne Gangstad | b92c5f2 | 2009-04-03 11:02:37 +0200 | [diff] [blame] | 1379 | static int prune_remote(const char *remote, int dry_run) |
| 1380 | { |
Michael Haggerty | 8552943 | 2014-11-25 09:02:34 +0100 | [diff] [blame] | 1381 | int result = 0; |
Ævar Arnfjörð Bjarmason | 0bc7787 | 2021-10-01 12:27:34 +0200 | [diff] [blame] | 1382 | struct ref_states states = REF_STATES_INIT; |
Michael Haggerty | fcce0da | 2014-11-25 09:02:33 +0100 | [diff] [blame] | 1383 | struct string_list refs_to_prune = STRING_LIST_INIT_NODUP; |
Michael Haggerty | 8552943 | 2014-11-25 09:02:34 +0100 | [diff] [blame] | 1384 | struct string_list_item *item; |
Finn Arne Gangstad | b92c5f2 | 2009-04-03 11:02:37 +0200 | [diff] [blame] | 1385 | const char *dangling_msg = dry_run |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1386 | ? _(" %s will become dangling!") |
| 1387 | : _(" %s has become dangling!"); |
Junio C Hamano | f8948e2 | 2009-02-08 23:27:10 -0800 | [diff] [blame] | 1388 | |
Finn Arne Gangstad | b92c5f2 | 2009-04-03 11:02:37 +0200 | [diff] [blame] | 1389 | get_remote_ref_states(remote, &states, GET_REF_STATES); |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1390 | |
Michael Haggerty | 16d4fa3 | 2014-11-25 09:02:29 +0100 | [diff] [blame] | 1391 | if (!states.stale.nr) { |
| 1392 | free_remote_ref_states(&states); |
| 1393 | return 0; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Michael Haggerty | 16d4fa3 | 2014-11-25 09:02:29 +0100 | [diff] [blame] | 1396 | printf_ln(_("Pruning %s"), remote); |
| 1397 | printf_ln(_("URL: %s"), |
| 1398 | states.remote->url_nr |
| 1399 | ? states.remote->url[0] |
| 1400 | : _("(no URL)")); |
| 1401 | |
Michael Haggerty | 8552943 | 2014-11-25 09:02:34 +0100 | [diff] [blame] | 1402 | for_each_string_list_item(item, &states.stale) |
| 1403 | string_list_append(&refs_to_prune, item->util); |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 1404 | string_list_sort(&refs_to_prune); |
Michael Haggerty | 28d3f21 | 2014-11-25 09:02:30 +0100 | [diff] [blame] | 1405 | |
Michael Haggerty | a122366 | 2015-06-22 16:02:58 +0200 | [diff] [blame] | 1406 | if (!dry_run) |
Michael Haggerty | 64da419 | 2017-05-22 16:17:38 +0200 | [diff] [blame] | 1407 | result |= delete_refs("remote: prune", &refs_to_prune, 0); |
Michael Haggerty | 16d4fa3 | 2014-11-25 09:02:29 +0100 | [diff] [blame] | 1408 | |
Michael Haggerty | 8552943 | 2014-11-25 09:02:34 +0100 | [diff] [blame] | 1409 | for_each_string_list_item(item, &states.stale) { |
| 1410 | const char *refname = item->util; |
Finn Arne Gangstad | b92c5f2 | 2009-04-03 11:02:37 +0200 | [diff] [blame] | 1411 | |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1412 | if (dry_run) |
| 1413 | printf_ln(_(" * [would prune] %s"), |
| 1414 | abbrev_ref(refname, "refs/remotes/")); |
| 1415 | else |
| 1416 | printf_ln(_(" * [pruned] %s"), |
| 1417 | abbrev_ref(refname, "refs/remotes/")); |
Finn Arne Gangstad | b92c5f2 | 2009-04-03 11:02:37 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Michael Haggerty | fcce0da | 2014-11-25 09:02:33 +0100 | [diff] [blame] | 1420 | warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune); |
Jens Lindström | e6bea66 | 2014-05-23 12:30:25 +0200 | [diff] [blame] | 1421 | |
Michael Haggerty | fcce0da | 2014-11-25 09:02:33 +0100 | [diff] [blame] | 1422 | string_list_clear(&refs_to_prune, 0); |
Finn Arne Gangstad | b92c5f2 | 2009-04-03 11:02:37 +0200 | [diff] [blame] | 1423 | free_remote_ref_states(&states); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1424 | return result; |
| 1425 | } |
| 1426 | |
Michael Haggerty | ce2223f | 2013-10-30 06:33:02 +0100 | [diff] [blame] | 1427 | static int prune(int argc, const char **argv) |
| 1428 | { |
| 1429 | int dry_run = 0, result = 0; |
| 1430 | struct option options[] = { |
| 1431 | OPT__DRY_RUN(&dry_run, N_("dry run")), |
| 1432 | OPT_END() |
| 1433 | }; |
| 1434 | |
| 1435 | argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage, |
| 1436 | 0); |
| 1437 | |
| 1438 | if (argc < 1) |
| 1439 | usage_with_options(builtin_remote_prune_usage, options); |
| 1440 | |
| 1441 | for (; argc; argc--, argv++) |
| 1442 | result |= prune_remote(*argv, dry_run); |
| 1443 | |
| 1444 | return result; |
| 1445 | } |
| 1446 | |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1447 | static int get_remote_default(const char *key, const char *value, void *priv) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1448 | { |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1449 | if (strcmp(key, "remotes.default") == 0) { |
| 1450 | int *found = priv; |
| 1451 | *found = 1; |
Johannes Schindelin | 84521ed | 2008-03-04 11:23:53 +0000 | [diff] [blame] | 1452 | } |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | static int update(int argc, const char **argv) |
| 1457 | { |
Michael Haggerty | 90765fa | 2013-10-30 06:33:04 +0100 | [diff] [blame] | 1458 | int i, prune = -1; |
Finn Arne Gangstad | efa5480 | 2009-04-03 11:03:44 +0200 | [diff] [blame] | 1459 | struct option options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1460 | OPT_BOOL('p', "prune", &prune, |
| 1461 | N_("prune remotes after fetching")), |
Finn Arne Gangstad | efa5480 | 2009-04-03 11:03:44 +0200 | [diff] [blame] | 1462 | OPT_END() |
| 1463 | }; |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1464 | struct strvec fetch_argv = STRVEC_INIT; |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1465 | int default_defined = 0; |
Michael Haggerty | 8607590 | 2013-10-30 06:33:03 +0100 | [diff] [blame] | 1466 | int retval; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1467 | |
Tim Henigan | 4504107 | 2009-11-20 18:43:13 -0500 | [diff] [blame] | 1468 | argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage, |
Finn Arne Gangstad | efa5480 | 2009-04-03 11:03:44 +0200 | [diff] [blame] | 1469 | PARSE_OPT_KEEP_ARGV0); |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1470 | |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1471 | strvec_push(&fetch_argv, "fetch"); |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1472 | |
Michael Haggerty | 90765fa | 2013-10-30 06:33:04 +0100 | [diff] [blame] | 1473 | if (prune != -1) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1474 | strvec_push(&fetch_argv, prune ? "--prune" : "--no-prune"); |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1475 | if (verbose) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1476 | strvec_push(&fetch_argv, "-v"); |
| 1477 | strvec_push(&fetch_argv, "--multiple"); |
Björn Gustavsson | 4f2e842 | 2009-12-31 10:43:17 +0100 | [diff] [blame] | 1478 | if (argc < 2) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1479 | strvec_push(&fetch_argv, "default"); |
Björn Gustavsson | 4f2e842 | 2009-12-31 10:43:17 +0100 | [diff] [blame] | 1480 | for (i = 1; i < argc; i++) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1481 | strvec_push(&fetch_argv, argv[i]); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1482 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1483 | if (strcmp(fetch_argv.v[fetch_argv.nr-1], "default") == 0) { |
Björn Gustavsson | 8db3559 | 2009-11-10 09:21:32 +0100 | [diff] [blame] | 1484 | git_config(get_remote_default, &default_defined); |
Michael Haggerty | 8607590 | 2013-10-30 06:33:03 +0100 | [diff] [blame] | 1485 | if (!default_defined) { |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1486 | strvec_pop(&fetch_argv); |
| 1487 | strvec_push(&fetch_argv, "--all"); |
Michael Haggerty | 8607590 | 2013-10-30 06:33:03 +0100 | [diff] [blame] | 1488 | } |
Johannes Schindelin | 84521ed | 2008-03-04 11:23:53 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1491 | retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1492 | strvec_clear(&fetch_argv); |
Michael Haggerty | 8607590 | 2013-10-30 06:33:03 +0100 | [diff] [blame] | 1493 | return retval; |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Jeff King | 113c29a | 2019-05-09 17:31:46 -0400 | [diff] [blame] | 1496 | static int remove_all_fetch_refspecs(const char *key) |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1497 | { |
Derrick Stolee | 504ee12 | 2020-11-25 22:12:49 +0000 | [diff] [blame] | 1498 | return git_config_set_multivar_gently(key, NULL, NULL, |
| 1499 | CONFIG_FLAGS_MULTI_REPLACE); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1500 | } |
| 1501 | |
Patrick Steinhardt | ab5e4b6 | 2016-02-22 12:23:29 +0100 | [diff] [blame] | 1502 | static void add_branches(struct remote *remote, const char **branches, |
| 1503 | const char *key) |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1504 | { |
| 1505 | const char *remotename = remote->name; |
| 1506 | int mirror = remote->mirror; |
| 1507 | struct strbuf refspec = STRBUF_INIT; |
| 1508 | |
| 1509 | for (; *branches; branches++) |
Patrick Steinhardt | ab5e4b6 | 2016-02-22 12:23:29 +0100 | [diff] [blame] | 1510 | add_branch(key, *branches, remotename, mirror, &refspec); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1511 | |
| 1512 | strbuf_release(&refspec); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | static int set_remote_branches(const char *remotename, const char **branches, |
| 1516 | int add_mode) |
| 1517 | { |
| 1518 | struct strbuf key = STRBUF_INIT; |
| 1519 | struct remote *remote; |
| 1520 | |
| 1521 | strbuf_addf(&key, "remote.%s.fetch", remotename); |
| 1522 | |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1523 | remote = remote_get(remotename); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 1524 | if (!remote_is_configured(remote, 1)) { |
| 1525 | error(_("No such remote '%s'"), remotename); |
| 1526 | exit(2); |
| 1527 | } |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1528 | |
Jeff King | 113c29a | 2019-05-09 17:31:46 -0400 | [diff] [blame] | 1529 | if (!add_mode && remove_all_fetch_refspecs(key.buf)) { |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1530 | strbuf_release(&key); |
| 1531 | return 1; |
| 1532 | } |
Patrick Steinhardt | ab5e4b6 | 2016-02-22 12:23:29 +0100 | [diff] [blame] | 1533 | add_branches(remote, branches, key.buf); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1534 | |
| 1535 | strbuf_release(&key); |
| 1536 | return 0; |
| 1537 | } |
| 1538 | |
| 1539 | static int set_branches(int argc, const char **argv) |
| 1540 | { |
| 1541 | int add_mode = 0; |
| 1542 | struct option options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1543 | OPT_BOOL('\0', "add", &add_mode, N_("add branch")), |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1544 | OPT_END() |
| 1545 | }; |
| 1546 | |
| 1547 | argc = parse_options(argc, argv, NULL, options, |
| 1548 | builtin_remote_setbranches_usage, 0); |
| 1549 | if (argc == 0) { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1550 | error(_("no remote specified")); |
Junio C Hamano | 656cdf0 | 2011-11-06 21:12:59 -0800 | [diff] [blame] | 1551 | usage_with_options(builtin_remote_setbranches_usage, options); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1552 | } |
| 1553 | argv[argc] = NULL; |
| 1554 | |
| 1555 | return set_remote_branches(argv[0], argv + 1, add_mode); |
| 1556 | } |
| 1557 | |
Ben Boeckel | 96f78d3 | 2015-09-15 21:53:47 -0400 | [diff] [blame] | 1558 | static int get_url(int argc, const char **argv) |
| 1559 | { |
| 1560 | int i, push_mode = 0, all_mode = 0; |
| 1561 | const char *remotename = NULL; |
| 1562 | struct remote *remote; |
| 1563 | const char **url; |
| 1564 | int url_nr; |
| 1565 | struct option options[] = { |
| 1566 | OPT_BOOL('\0', "push", &push_mode, |
| 1567 | N_("query push URLs rather than fetch URLs")), |
| 1568 | OPT_BOOL('\0', "all", &all_mode, |
| 1569 | N_("return all URLs")), |
| 1570 | OPT_END() |
| 1571 | }; |
| 1572 | argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0); |
| 1573 | |
| 1574 | if (argc != 1) |
| 1575 | usage_with_options(builtin_remote_geturl_usage, options); |
| 1576 | |
| 1577 | remotename = argv[0]; |
| 1578 | |
Ben Boeckel | 96f78d3 | 2015-09-15 21:53:47 -0400 | [diff] [blame] | 1579 | remote = remote_get(remotename); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 1580 | if (!remote_is_configured(remote, 1)) { |
| 1581 | error(_("No such remote '%s'"), remotename); |
| 1582 | exit(2); |
| 1583 | } |
Ben Boeckel | 96f78d3 | 2015-09-15 21:53:47 -0400 | [diff] [blame] | 1584 | |
| 1585 | url_nr = 0; |
| 1586 | if (push_mode) { |
| 1587 | url = remote->pushurl; |
| 1588 | url_nr = remote->pushurl_nr; |
| 1589 | } |
| 1590 | /* else fetch mode */ |
| 1591 | |
| 1592 | /* Use the fetch URL when no push URLs were found or requested. */ |
| 1593 | if (!url_nr) { |
| 1594 | url = remote->url; |
| 1595 | url_nr = remote->url_nr; |
| 1596 | } |
| 1597 | |
| 1598 | if (!url_nr) |
| 1599 | die(_("no URLs configured for remote '%s'"), remotename); |
| 1600 | |
| 1601 | if (all_mode) { |
| 1602 | for (i = 0; i < url_nr; i++) |
| 1603 | printf_ln("%s", url[i]); |
| 1604 | } else { |
| 1605 | printf_ln("%s", *url); |
| 1606 | } |
| 1607 | |
| 1608 | return 0; |
| 1609 | } |
| 1610 | |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1611 | static int set_url(int argc, const char **argv) |
| 1612 | { |
| 1613 | int i, push_mode = 0, add_mode = 0, delete_mode = 0; |
| 1614 | int matches = 0, negative_matches = 0; |
| 1615 | const char *remotename = NULL; |
| 1616 | const char *newurl = NULL; |
| 1617 | const char *oldurl = NULL; |
| 1618 | struct remote *remote; |
| 1619 | regex_t old_regex; |
| 1620 | const char **urlset; |
| 1621 | int urlset_nr; |
| 1622 | struct strbuf name_buf = STRBUF_INIT; |
| 1623 | struct option options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 1624 | OPT_BOOL('\0', "push", &push_mode, |
| 1625 | N_("manipulate push URLs")), |
| 1626 | OPT_BOOL('\0', "add", &add_mode, |
| 1627 | N_("add URL")), |
| 1628 | OPT_BOOL('\0', "delete", &delete_mode, |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 1629 | N_("delete URLs")), |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1630 | OPT_END() |
| 1631 | }; |
Felipe Contreras | c49904e | 2011-11-07 05:36:57 +0200 | [diff] [blame] | 1632 | argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage, |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1633 | PARSE_OPT_KEEP_ARGV0); |
| 1634 | |
| 1635 | if (add_mode && delete_mode) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1636 | die(_("--add --delete doesn't make sense")); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1637 | |
| 1638 | if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3)) |
| 1639 | usage_with_options(builtin_remote_seturl_usage, options); |
| 1640 | |
| 1641 | remotename = argv[1]; |
| 1642 | newurl = argv[2]; |
| 1643 | if (argc > 3) |
| 1644 | oldurl = argv[3]; |
| 1645 | |
| 1646 | if (delete_mode) |
| 1647 | oldurl = newurl; |
| 1648 | |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1649 | remote = remote_get(remotename); |
Ævar Arnfjörð Bjarmason | 9144ba4 | 2020-10-27 10:41:36 +0100 | [diff] [blame] | 1650 | if (!remote_is_configured(remote, 1)) { |
| 1651 | error(_("No such remote '%s'"), remotename); |
| 1652 | exit(2); |
| 1653 | } |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1654 | |
| 1655 | if (push_mode) { |
| 1656 | strbuf_addf(&name_buf, "remote.%s.pushurl", remotename); |
| 1657 | urlset = remote->pushurl; |
| 1658 | urlset_nr = remote->pushurl_nr; |
| 1659 | } else { |
| 1660 | strbuf_addf(&name_buf, "remote.%s.url", remotename); |
| 1661 | urlset = remote->url; |
| 1662 | urlset_nr = remote->url_nr; |
| 1663 | } |
| 1664 | |
| 1665 | /* Special cases that add new entry. */ |
| 1666 | if ((!oldurl && !delete_mode) || add_mode) { |
| 1667 | if (add_mode) |
| 1668 | git_config_set_multivar(name_buf.buf, newurl, |
Patrick Steinhardt | 45ebdcc | 2016-02-22 12:23:28 +0100 | [diff] [blame] | 1669 | "^$", 0); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1670 | else |
| 1671 | git_config_set(name_buf.buf, newurl); |
Rene Scharfe | 85af9f7 | 2017-08-30 20:00:27 +0200 | [diff] [blame] | 1672 | goto out; |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /* Old URL specified. Demand that one matches. */ |
| 1676 | if (regcomp(&old_regex, oldurl, REG_EXTENDED)) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1677 | die(_("Invalid old URL pattern: %s"), oldurl); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1678 | |
| 1679 | for (i = 0; i < urlset_nr; i++) |
| 1680 | if (!regexec(&old_regex, urlset[i], 0, NULL, 0)) |
| 1681 | matches++; |
| 1682 | else |
| 1683 | negative_matches++; |
| 1684 | if (!delete_mode && !matches) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1685 | die(_("No such URL found: %s"), oldurl); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1686 | if (delete_mode && !negative_matches && !push_mode) |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1687 | die(_("Will not delete all non-push URLs")); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1688 | |
| 1689 | regfree(&old_regex); |
| 1690 | |
| 1691 | if (!delete_mode) |
| 1692 | git_config_set_multivar(name_buf.buf, newurl, oldurl, 0); |
| 1693 | else |
Derrick Stolee | 504ee12 | 2020-11-25 22:12:49 +0000 | [diff] [blame] | 1694 | git_config_set_multivar(name_buf.buf, NULL, oldurl, |
| 1695 | CONFIG_FLAGS_MULTI_REPLACE); |
Rene Scharfe | 85af9f7 | 2017-08-30 20:00:27 +0200 | [diff] [blame] | 1696 | out: |
| 1697 | strbuf_release(&name_buf); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1698 | return 0; |
| 1699 | } |
| 1700 | |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1701 | int cmd_remote(int argc, const char **argv, const char *prefix) |
| 1702 | { |
| 1703 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | fbbae14 | 2012-08-20 19:32:35 +0700 | [diff] [blame] | 1704 | OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")), |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1705 | OPT_END() |
| 1706 | }; |
| 1707 | int result; |
| 1708 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 1709 | argc = parse_options(argc, argv, prefix, options, builtin_remote_usage, |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1710 | PARSE_OPT_STOP_AT_NON_OPTION); |
| 1711 | |
| 1712 | if (argc < 1) |
| 1713 | result = show_all(); |
| 1714 | else if (!strcmp(argv[0], "add")) |
| 1715 | result = add(argc, argv); |
Miklos Vajna | bf98421 | 2008-11-03 19:26:18 +0100 | [diff] [blame] | 1716 | else if (!strcmp(argv[0], "rename")) |
| 1717 | result = mv(argc, argv); |
Nguyễn Thái Ngọc Duy | e17dba8 | 2012-09-06 19:25:23 +0700 | [diff] [blame] | 1718 | else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove")) |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1719 | result = rm(argc, argv); |
Jay Soffian | bc14fac | 2009-02-25 03:32:25 -0500 | [diff] [blame] | 1720 | else if (!strcmp(argv[0], "set-head")) |
| 1721 | result = set_head(argc, argv); |
Jonathan Nieder | 3d8b694 | 2010-05-19 13:38:50 -0500 | [diff] [blame] | 1722 | else if (!strcmp(argv[0], "set-branches")) |
| 1723 | result = set_branches(argc, argv); |
Ben Boeckel | 96f78d3 | 2015-09-15 21:53:47 -0400 | [diff] [blame] | 1724 | else if (!strcmp(argv[0], "get-url")) |
| 1725 | result = get_url(argc, argv); |
Ilari Liusvaara | 433f2be | 2010-01-18 19:18:02 +0200 | [diff] [blame] | 1726 | else if (!strcmp(argv[0], "set-url")) |
| 1727 | result = set_url(argc, argv); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1728 | else if (!strcmp(argv[0], "show")) |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1729 | result = show(argc, argv); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1730 | else if (!strcmp(argv[0], "prune")) |
Olivier Marin | 67a7e2d | 2008-06-10 16:51:21 +0200 | [diff] [blame] | 1731 | result = prune(argc, argv); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1732 | else if (!strcmp(argv[0], "update")) |
| 1733 | result = update(argc, argv); |
| 1734 | else { |
Nguyễn Thái Ngọc Duy | bb16d5d | 2012-04-23 19:30:26 +0700 | [diff] [blame] | 1735 | error(_("Unknown subcommand: %s"), argv[0]); |
Johannes Schindelin | 211c896 | 2008-02-29 01:45:45 +0000 | [diff] [blame] | 1736 | usage_with_options(builtin_remote_usage, options); |
| 1737 | } |
| 1738 | |
| 1739 | return result ? 1 : 0; |
| 1740 | } |