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