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