Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "remote.h" |
| 3 | #include "refs.h" |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 4 | #include "commit.h" |
| 5 | #include "diff.h" |
| 6 | #include "revision.h" |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 7 | #include "dir.h" |
Jay Soffian | ec8452d | 2009-02-25 03:32:12 -0500 | [diff] [blame] | 8 | #include "tag.h" |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 9 | #include "string-list.h" |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 10 | |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 11 | enum map_direction { FROM_SRC, FROM_DST }; |
| 12 | |
Daniel Barkalow | e0aaa29 | 2008-04-17 19:32:35 -0400 | [diff] [blame] | 13 | static struct refspec s_tag_refspec = { |
| 14 | 0, |
| 15 | 1, |
Junio C Hamano | b84c343 | 2008-05-25 13:38:44 -0700 | [diff] [blame] | 16 | 0, |
Daniel Barkalow | 08fbdb3 | 2009-03-07 01:11:36 -0500 | [diff] [blame] | 17 | "refs/tags/*", |
| 18 | "refs/tags/*" |
Daniel Barkalow | e0aaa29 | 2008-04-17 19:32:35 -0400 | [diff] [blame] | 19 | }; |
| 20 | |
| 21 | const struct refspec *tag_refspec = &s_tag_refspec; |
| 22 | |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 23 | struct counted_string { |
| 24 | size_t len; |
| 25 | const char *s; |
| 26 | }; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 27 | struct rewrite { |
| 28 | const char *base; |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 29 | size_t baselen; |
| 30 | struct counted_string *instead_of; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 31 | int instead_of_nr; |
| 32 | int instead_of_alloc; |
| 33 | }; |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 34 | struct rewrites { |
| 35 | struct rewrite **rewrite; |
| 36 | int rewrite_alloc; |
| 37 | int rewrite_nr; |
| 38 | }; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 39 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 40 | static struct remote **remotes; |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 41 | static int remotes_alloc; |
| 42 | static int remotes_nr; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 43 | |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 44 | static struct branch **branches; |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 45 | static int branches_alloc; |
| 46 | static int branches_nr; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 47 | |
| 48 | static struct branch *current_branch; |
| 49 | static const char *default_remote_name; |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 50 | static int explicit_default_remote_name; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 51 | |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 52 | static struct rewrites rewrites; |
Josh Triplett | 1c2eafb | 2009-09-07 01:56:33 -0700 | [diff] [blame] | 53 | static struct rewrites rewrites_push; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 54 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 55 | #define BUF_SIZE (2048) |
| 56 | static char buffer[BUF_SIZE]; |
| 57 | |
Daniel Barkalow | 0a4da29 | 2009-11-18 02:42:23 +0100 | [diff] [blame] | 58 | static int valid_remote(const struct remote *remote) |
| 59 | { |
Daniel Barkalow | c578f51 | 2009-11-18 02:42:25 +0100 | [diff] [blame] | 60 | return (!!remote->url) || (!!remote->foreign_vcs); |
Daniel Barkalow | 0a4da29 | 2009-11-18 02:42:23 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 63 | static const char *alias_url(const char *url, struct rewrites *r) |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 64 | { |
| 65 | int i, j; |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 66 | char *ret; |
| 67 | struct counted_string *longest; |
| 68 | int longest_i; |
| 69 | |
| 70 | longest = NULL; |
| 71 | longest_i = -1; |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 72 | for (i = 0; i < r->rewrite_nr; i++) { |
| 73 | if (!r->rewrite[i]) |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 74 | continue; |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 75 | for (j = 0; j < r->rewrite[i]->instead_of_nr; j++) { |
| 76 | if (!prefixcmp(url, r->rewrite[i]->instead_of[j].s) && |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 77 | (!longest || |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 78 | longest->len < r->rewrite[i]->instead_of[j].len)) { |
| 79 | longest = &(r->rewrite[i]->instead_of[j]); |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 80 | longest_i = i; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 84 | if (!longest) |
| 85 | return url; |
| 86 | |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 87 | ret = xmalloc(r->rewrite[longest_i]->baselen + |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 88 | (strlen(url) - longest->len) + 1); |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 89 | strcpy(ret, r->rewrite[longest_i]->base); |
| 90 | strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len); |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 91 | return ret; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 94 | static void add_push_refspec(struct remote *remote, const char *ref) |
| 95 | { |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 96 | ALLOC_GROW(remote->push_refspec, |
| 97 | remote->push_refspec_nr + 1, |
| 98 | remote->push_refspec_alloc); |
| 99 | remote->push_refspec[remote->push_refspec_nr++] = ref; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 102 | static void add_fetch_refspec(struct remote *remote, const char *ref) |
| 103 | { |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 104 | ALLOC_GROW(remote->fetch_refspec, |
| 105 | remote->fetch_refspec_nr + 1, |
| 106 | remote->fetch_refspec_alloc); |
| 107 | remote->fetch_refspec[remote->fetch_refspec_nr++] = ref; |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Shawn O. Pearce | 28b91f8 | 2007-09-19 00:49:27 -0400 | [diff] [blame] | 110 | static void add_url(struct remote *remote, const char *url) |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 111 | { |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 112 | ALLOC_GROW(remote->url, remote->url_nr + 1, remote->url_alloc); |
| 113 | remote->url[remote->url_nr++] = url; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Michael J Gruber | 2034623 | 2009-06-09 18:01:34 +0200 | [diff] [blame] | 116 | static void add_pushurl(struct remote *remote, const char *pushurl) |
| 117 | { |
| 118 | ALLOC_GROW(remote->pushurl, remote->pushurl_nr + 1, remote->pushurl_alloc); |
| 119 | remote->pushurl[remote->pushurl_nr++] = pushurl; |
| 120 | } |
| 121 | |
Josh Triplett | 1c2eafb | 2009-09-07 01:56:33 -0700 | [diff] [blame] | 122 | static void add_pushurl_alias(struct remote *remote, const char *url) |
| 123 | { |
| 124 | const char *pushurl = alias_url(url, &rewrites_push); |
| 125 | if (pushurl != url) |
| 126 | add_pushurl(remote, pushurl); |
| 127 | } |
| 128 | |
| 129 | static void add_url_alias(struct remote *remote, const char *url) |
| 130 | { |
| 131 | add_url(remote, alias_url(url, &rewrites)); |
| 132 | add_pushurl_alias(remote, url); |
| 133 | } |
| 134 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 135 | static struct remote *make_remote(const char *name, int len) |
| 136 | { |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 137 | struct remote *ret; |
| 138 | int i; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 139 | |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 140 | for (i = 0; i < remotes_nr; i++) { |
| 141 | if (len ? (!strncmp(name, remotes[i]->name, len) && |
| 142 | !remotes[i]->name[len]) : |
| 143 | !strcmp(name, remotes[i]->name)) |
| 144 | return remotes[i]; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 145 | } |
| 146 | |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 147 | ret = xcalloc(1, sizeof(struct remote)); |
| 148 | ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc); |
| 149 | remotes[remotes_nr++] = ret; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 150 | if (len) |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 151 | ret->name = xstrndup(name, len); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 152 | else |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 153 | ret->name = xstrdup(name); |
| 154 | return ret; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 157 | static void add_merge(struct branch *branch, const char *name) |
| 158 | { |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 159 | ALLOC_GROW(branch->merge_name, branch->merge_nr + 1, |
| 160 | branch->merge_alloc); |
| 161 | branch->merge_name[branch->merge_nr++] = name; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static struct branch *make_branch(const char *name, int len) |
| 165 | { |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 166 | struct branch *ret; |
| 167 | int i; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 168 | char *refname; |
| 169 | |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 170 | for (i = 0; i < branches_nr; i++) { |
| 171 | if (len ? (!strncmp(name, branches[i]->name, len) && |
| 172 | !branches[i]->name[len]) : |
| 173 | !strcmp(name, branches[i]->name)) |
| 174 | return branches[i]; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 177 | ALLOC_GROW(branches, branches_nr + 1, branches_alloc); |
| 178 | ret = xcalloc(1, sizeof(struct branch)); |
| 179 | branches[branches_nr++] = ret; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 180 | if (len) |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 181 | ret->name = xstrndup(name, len); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 182 | else |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 183 | ret->name = xstrdup(name); |
Dotan Barak | e8eec71 | 2008-09-09 21:57:10 +0300 | [diff] [blame] | 184 | refname = xmalloc(strlen(name) + strlen("refs/heads/") + 1); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 185 | strcpy(refname, "refs/heads/"); |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 186 | strcpy(refname + strlen("refs/heads/"), ret->name); |
| 187 | ret->refname = refname; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 188 | |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 189 | return ret; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 190 | } |
| 191 | |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 192 | static struct rewrite *make_rewrite(struct rewrites *r, const char *base, int len) |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 193 | { |
| 194 | struct rewrite *ret; |
| 195 | int i; |
| 196 | |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 197 | for (i = 0; i < r->rewrite_nr; i++) { |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 198 | if (len |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 199 | ? (len == r->rewrite[i]->baselen && |
| 200 | !strncmp(base, r->rewrite[i]->base, len)) |
| 201 | : !strcmp(base, r->rewrite[i]->base)) |
| 202 | return r->rewrite[i]; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 203 | } |
| 204 | |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 205 | ALLOC_GROW(r->rewrite, r->rewrite_nr + 1, r->rewrite_alloc); |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 206 | ret = xcalloc(1, sizeof(struct rewrite)); |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 207 | r->rewrite[r->rewrite_nr++] = ret; |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 208 | if (len) { |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 209 | ret->base = xstrndup(base, len); |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 210 | ret->baselen = len; |
| 211 | } |
| 212 | else { |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 213 | ret->base = xstrdup(base); |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 214 | ret->baselen = strlen(base); |
| 215 | } |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static void add_instead_of(struct rewrite *rewrite, const char *instead_of) |
| 220 | { |
| 221 | ALLOC_GROW(rewrite->instead_of, rewrite->instead_of_nr + 1, rewrite->instead_of_alloc); |
Junio C Hamano | 844112c | 2008-02-24 22:25:04 -0800 | [diff] [blame] | 222 | rewrite->instead_of[rewrite->instead_of_nr].s = instead_of; |
| 223 | rewrite->instead_of[rewrite->instead_of_nr].len = strlen(instead_of); |
| 224 | rewrite->instead_of_nr++; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 227 | static void read_remotes_file(struct remote *remote) |
| 228 | { |
| 229 | FILE *f = fopen(git_path("remotes/%s", remote->name), "r"); |
| 230 | |
| 231 | if (!f) |
| 232 | return; |
Miklos Vajna | 89cf4c7 | 2008-11-10 21:43:00 +0100 | [diff] [blame] | 233 | remote->origin = REMOTE_REMOTES; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 234 | while (fgets(buffer, BUF_SIZE, f)) { |
| 235 | int value_list; |
| 236 | char *s, *p; |
| 237 | |
| 238 | if (!prefixcmp(buffer, "URL:")) { |
| 239 | value_list = 0; |
| 240 | s = buffer + 4; |
| 241 | } else if (!prefixcmp(buffer, "Push:")) { |
| 242 | value_list = 1; |
| 243 | s = buffer + 5; |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 244 | } else if (!prefixcmp(buffer, "Pull:")) { |
| 245 | value_list = 2; |
| 246 | s = buffer + 5; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 247 | } else |
| 248 | continue; |
| 249 | |
| 250 | while (isspace(*s)) |
| 251 | s++; |
| 252 | if (!*s) |
| 253 | continue; |
| 254 | |
| 255 | p = s + strlen(s); |
| 256 | while (isspace(p[-1])) |
| 257 | *--p = 0; |
| 258 | |
| 259 | switch (value_list) { |
| 260 | case 0: |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 261 | add_url_alias(remote, xstrdup(s)); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 262 | break; |
| 263 | case 1: |
| 264 | add_push_refspec(remote, xstrdup(s)); |
| 265 | break; |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 266 | case 2: |
| 267 | add_fetch_refspec(remote, xstrdup(s)); |
| 268 | break; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | fclose(f); |
| 272 | } |
| 273 | |
| 274 | static void read_branches_file(struct remote *remote) |
| 275 | { |
| 276 | const char *slash = strchr(remote->name, '/'); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 277 | char *frag; |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 278 | struct strbuf branch = STRBUF_INIT; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 279 | int n = slash ? slash - remote->name : 1000; |
| 280 | FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r"); |
| 281 | char *s, *p; |
| 282 | int len; |
| 283 | |
| 284 | if (!f) |
| 285 | return; |
| 286 | s = fgets(buffer, BUF_SIZE, f); |
| 287 | fclose(f); |
| 288 | if (!s) |
| 289 | return; |
| 290 | while (isspace(*s)) |
| 291 | s++; |
| 292 | if (!*s) |
| 293 | return; |
Miklos Vajna | 89cf4c7 | 2008-11-10 21:43:00 +0100 | [diff] [blame] | 294 | remote->origin = REMOTE_BRANCHES; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 295 | p = s + strlen(s); |
| 296 | while (isspace(p[-1])) |
| 297 | *--p = 0; |
| 298 | len = p - s; |
| 299 | if (slash) |
| 300 | len += strlen(slash); |
| 301 | p = xmalloc(len + 1); |
| 302 | strcpy(p, s); |
| 303 | if (slash) |
| 304 | strcat(p, slash); |
Daniel Barkalow | 472fa4c | 2008-03-25 19:35:28 -0400 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * With "slash", e.g. "git fetch jgarzik/netdev-2.6" when |
| 308 | * reading from $GIT_DIR/branches/jgarzik fetches "HEAD" from |
| 309 | * the partial URL obtained from the branches file plus |
| 310 | * "/netdev-2.6" and does not store it in any tracking ref. |
| 311 | * #branch specifier in the file is ignored. |
| 312 | * |
| 313 | * Otherwise, the branches file would have URL and optionally |
| 314 | * #branch specified. The "master" (or specified) branch is |
| 315 | * fetched and stored in the local branch of the same name. |
| 316 | */ |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 317 | frag = strchr(p, '#'); |
| 318 | if (frag) { |
| 319 | *(frag++) = '\0'; |
Daniel Barkalow | 472fa4c | 2008-03-25 19:35:28 -0400 | [diff] [blame] | 320 | strbuf_addf(&branch, "refs/heads/%s", frag); |
| 321 | } else |
| 322 | strbuf_addstr(&branch, "refs/heads/master"); |
| 323 | if (!slash) { |
| 324 | strbuf_addf(&branch, ":refs/heads/%s", remote->name); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 325 | } else { |
Daniel Barkalow | 472fa4c | 2008-03-25 19:35:28 -0400 | [diff] [blame] | 326 | strbuf_reset(&branch); |
| 327 | strbuf_addstr(&branch, "HEAD:"); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 328 | } |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 329 | add_url_alias(remote, p); |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 330 | add_fetch_refspec(remote, strbuf_detach(&branch, NULL)); |
Martin Koegler | 18afe10 | 2008-11-10 22:47:11 +0100 | [diff] [blame] | 331 | /* |
| 332 | * Cogito compatible push: push current HEAD to remote #branch |
| 333 | * (master if missing) |
| 334 | */ |
| 335 | strbuf_init(&branch, 0); |
| 336 | strbuf_addstr(&branch, "HEAD"); |
| 337 | if (frag) |
| 338 | strbuf_addf(&branch, ":refs/heads/%s", frag); |
| 339 | else |
| 340 | strbuf_addstr(&branch, ":refs/heads/master"); |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 341 | add_push_refspec(remote, strbuf_detach(&branch, NULL)); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 342 | remote->fetch_tags = 1; /* always auto-follow */ |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 343 | } |
| 344 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 345 | static int handle_config(const char *key, const char *value, void *cb) |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 346 | { |
| 347 | const char *name; |
| 348 | const char *subkey; |
| 349 | struct remote *remote; |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 350 | struct branch *branch; |
| 351 | if (!prefixcmp(key, "branch.")) { |
| 352 | name = key + 7; |
| 353 | subkey = strrchr(name, '.'); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 354 | if (!subkey) |
| 355 | return 0; |
Junio C Hamano | 896c053 | 2007-12-14 20:34:56 -0800 | [diff] [blame] | 356 | branch = make_branch(name, subkey - name); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 357 | if (!strcmp(subkey, ".remote")) { |
Junio C Hamano | d2370cc | 2008-02-11 11:00:10 -0800 | [diff] [blame] | 358 | if (!value) |
| 359 | return config_error_nonbool(key); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 360 | branch->remote_name = xstrdup(value); |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 361 | if (branch == current_branch) { |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 362 | default_remote_name = branch->remote_name; |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 363 | explicit_default_remote_name = 1; |
| 364 | } |
Junio C Hamano | d2370cc | 2008-02-11 11:00:10 -0800 | [diff] [blame] | 365 | } else if (!strcmp(subkey, ".merge")) { |
| 366 | if (!value) |
| 367 | return config_error_nonbool(key); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 368 | add_merge(branch, xstrdup(value)); |
Junio C Hamano | d2370cc | 2008-02-11 11:00:10 -0800 | [diff] [blame] | 369 | } |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 370 | return 0; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 371 | } |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 372 | if (!prefixcmp(key, "url.")) { |
| 373 | struct rewrite *rewrite; |
Daniel Barkalow | 60e3aba | 2008-04-12 15:32:00 -0400 | [diff] [blame] | 374 | name = key + 4; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 375 | subkey = strrchr(name, '.'); |
| 376 | if (!subkey) |
| 377 | return 0; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 378 | if (!strcmp(subkey, ".insteadof")) { |
Josh Triplett | 1c2eafb | 2009-09-07 01:56:33 -0700 | [diff] [blame] | 379 | rewrite = make_rewrite(&rewrites, name, subkey - name); |
| 380 | if (!value) |
| 381 | return config_error_nonbool(key); |
| 382 | add_instead_of(rewrite, xstrdup(value)); |
| 383 | } else if (!strcmp(subkey, ".pushinsteadof")) { |
| 384 | rewrite = make_rewrite(&rewrites_push, name, subkey - name); |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 385 | if (!value) |
| 386 | return config_error_nonbool(key); |
| 387 | add_instead_of(rewrite, xstrdup(value)); |
| 388 | } |
| 389 | } |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 390 | if (prefixcmp(key, "remote.")) |
| 391 | return 0; |
| 392 | name = key + 7; |
Brandon Casey | c82efaf | 2008-10-14 15:30:21 -0500 | [diff] [blame] | 393 | if (*name == '/') { |
| 394 | warning("Config remote shorthand cannot begin with '/': %s", |
| 395 | name); |
| 396 | return 0; |
| 397 | } |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 398 | subkey = strrchr(name, '.'); |
| 399 | if (!subkey) |
Johannes Sixt | cd294bc | 2009-04-23 15:49:05 +0200 | [diff] [blame] | 400 | return 0; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 401 | remote = make_remote(name, subkey - name); |
Miklos Vajna | 89cf4c7 | 2008-11-10 21:43:00 +0100 | [diff] [blame] | 402 | remote->origin = REMOTE_CONFIG; |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 403 | if (!strcmp(subkey, ".mirror")) |
| 404 | remote->mirror = git_config_bool(key, value); |
| 405 | else if (!strcmp(subkey, ".skipdefaultupdate")) |
| 406 | remote->skip_default_update = git_config_bool(key, value); |
Björn Gustavsson | 7cc91a2 | 2009-11-09 21:11:06 +0100 | [diff] [blame] | 407 | else if (!strcmp(subkey, ".skipfetchall")) |
| 408 | remote->skip_default_update = git_config_bool(key, value); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 409 | else if (!strcmp(subkey, ".url")) { |
| 410 | const char *v; |
| 411 | if (git_config_string(&v, key, value)) |
| 412 | return -1; |
| 413 | add_url(remote, v); |
Michael J Gruber | 2034623 | 2009-06-09 18:01:34 +0200 | [diff] [blame] | 414 | } else if (!strcmp(subkey, ".pushurl")) { |
| 415 | const char *v; |
| 416 | if (git_config_string(&v, key, value)) |
| 417 | return -1; |
| 418 | add_pushurl(remote, v); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 419 | } else if (!strcmp(subkey, ".push")) { |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 420 | const char *v; |
| 421 | if (git_config_string(&v, key, value)) |
| 422 | return -1; |
| 423 | add_push_refspec(remote, v); |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 424 | } else if (!strcmp(subkey, ".fetch")) { |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 425 | const char *v; |
| 426 | if (git_config_string(&v, key, value)) |
| 427 | return -1; |
| 428 | add_fetch_refspec(remote, v); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 429 | } else if (!strcmp(subkey, ".receivepack")) { |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 430 | const char *v; |
| 431 | if (git_config_string(&v, key, value)) |
| 432 | return -1; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 433 | if (!remote->receivepack) |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 434 | remote->receivepack = v; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 435 | else |
| 436 | error("more than one receivepack given, using the first"); |
Daniel Barkalow | 0012ba2 | 2007-09-10 23:02:51 -0400 | [diff] [blame] | 437 | } else if (!strcmp(subkey, ".uploadpack")) { |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 438 | const char *v; |
| 439 | if (git_config_string(&v, key, value)) |
| 440 | return -1; |
Daniel Barkalow | 0012ba2 | 2007-09-10 23:02:51 -0400 | [diff] [blame] | 441 | if (!remote->uploadpack) |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 442 | remote->uploadpack = v; |
Daniel Barkalow | 0012ba2 | 2007-09-10 23:02:51 -0400 | [diff] [blame] | 443 | else |
| 444 | error("more than one uploadpack given, using the first"); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 445 | } else if (!strcmp(subkey, ".tagopt")) { |
| 446 | if (!strcmp(value, "--no-tags")) |
| 447 | remote->fetch_tags = -1; |
Samuel Tardieu | 944163a | 2010-04-20 01:31:25 +0200 | [diff] [blame] | 448 | else if (!strcmp(value, "--tags")) |
| 449 | remote->fetch_tags = 2; |
Sam Vilain | 14c9821 | 2007-12-04 10:48:54 +1300 | [diff] [blame] | 450 | } else if (!strcmp(subkey, ".proxy")) { |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 451 | return git_config_string((const char **)&remote->http_proxy, |
| 452 | key, value); |
Daniel Barkalow | c578f51 | 2009-11-18 02:42:25 +0100 | [diff] [blame] | 453 | } else if (!strcmp(subkey, ".vcs")) { |
| 454 | return git_config_string(&remote->foreign_vcs, key, value); |
Paolo Bonzini | 84bb2df | 2008-04-17 13:17:20 +0200 | [diff] [blame] | 455 | } |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 459 | static void alias_all_urls(void) |
| 460 | { |
| 461 | int i, j; |
| 462 | for (i = 0; i < remotes_nr; i++) { |
Josh Triplett | 1c2eafb | 2009-09-07 01:56:33 -0700 | [diff] [blame] | 463 | int add_pushurl_aliases; |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 464 | if (!remotes[i]) |
| 465 | continue; |
Michael J Gruber | 2034623 | 2009-06-09 18:01:34 +0200 | [diff] [blame] | 466 | for (j = 0; j < remotes[i]->pushurl_nr; j++) { |
Josh Triplett | d071d94 | 2009-09-07 01:56:00 -0700 | [diff] [blame] | 467 | remotes[i]->pushurl[j] = alias_url(remotes[i]->pushurl[j], &rewrites); |
Michael J Gruber | 2034623 | 2009-06-09 18:01:34 +0200 | [diff] [blame] | 468 | } |
Josh Triplett | 1c2eafb | 2009-09-07 01:56:33 -0700 | [diff] [blame] | 469 | add_pushurl_aliases = remotes[i]->pushurl_nr == 0; |
| 470 | for (j = 0; j < remotes[i]->url_nr; j++) { |
| 471 | if (add_pushurl_aliases) |
| 472 | add_pushurl_alias(remotes[i], remotes[i]->url[j]); |
| 473 | remotes[i]->url[j] = alias_url(remotes[i]->url[j], &rewrites); |
| 474 | } |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 478 | static void read_config(void) |
| 479 | { |
| 480 | unsigned char sha1[20]; |
| 481 | const char *head_ref; |
| 482 | int flag; |
Tor Arntsen | 2543d9b | 2010-06-04 11:32:11 +0200 | [diff] [blame] | 483 | if (default_remote_name) /* did this already */ |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 484 | return; |
| 485 | default_remote_name = xstrdup("origin"); |
| 486 | current_branch = NULL; |
Nguyễn Thái Ngọc Duy | 8cad474 | 2011-12-12 18:20:32 +0700 | [diff] [blame] | 487 | head_ref = resolve_ref_unsafe("HEAD", sha1, 0, &flag); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 488 | if (head_ref && (flag & REF_ISSYMREF) && |
| 489 | !prefixcmp(head_ref, "refs/heads/")) { |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 490 | current_branch = |
| 491 | make_branch(head_ref + strlen("refs/heads/"), 0); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 492 | } |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 493 | git_config(handle_config, NULL); |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 494 | alias_all_urls(); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Junio C Hamano | 47c6ef1 | 2008-07-26 23:15:51 -0700 | [diff] [blame] | 497 | /* |
Brandon Casey | 2cb1f36 | 2008-08-21 19:16:30 -0500 | [diff] [blame] | 498 | * This function frees a refspec array. |
| 499 | * Warning: code paths should be checked to ensure that the src |
| 500 | * and dst pointers are always freeable pointers as well |
| 501 | * as the refspec pointer itself. |
| 502 | */ |
Nanako Shiraishi | 697d7f5 | 2008-09-25 18:41:00 +0900 | [diff] [blame] | 503 | static void free_refspecs(struct refspec *refspec, int nr_refspec) |
Brandon Casey | 2cb1f36 | 2008-08-21 19:16:30 -0500 | [diff] [blame] | 504 | { |
| 505 | int i; |
| 506 | |
| 507 | if (!refspec) |
| 508 | return; |
| 509 | |
| 510 | for (i = 0; i < nr_refspec; i++) { |
| 511 | free(refspec[i].src); |
| 512 | free(refspec[i].dst); |
| 513 | } |
| 514 | free(refspec); |
| 515 | } |
| 516 | |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 517 | static struct refspec *parse_refspec_internal(int nr_refspec, const char **refspec, int fetch, int verify) |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 518 | { |
| 519 | int i; |
| 520 | struct refspec *rs = xcalloc(sizeof(*rs), nr_refspec); |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 521 | |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 522 | for (i = 0; i < nr_refspec; i++) { |
Junio C Hamano | 47c6ef1 | 2008-07-26 23:15:51 -0700 | [diff] [blame] | 523 | size_t llen; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 524 | int is_glob; |
| 525 | const char *lhs, *rhs; |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 526 | int flags; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 527 | |
Benjamin Kramer | 8e76bf3 | 2009-03-13 13:51:33 +0100 | [diff] [blame] | 528 | is_glob = 0; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 529 | |
| 530 | lhs = refspec[i]; |
| 531 | if (*lhs == '+') { |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 532 | rs[i].force = 1; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 533 | lhs++; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 534 | } |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 535 | |
| 536 | rhs = strrchr(lhs, ':'); |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | * Before going on, special case ":" (or "+:") as a refspec |
| 540 | * for matching refs. |
| 541 | */ |
| 542 | if (!fetch && rhs == lhs && rhs[1] == '\0') { |
| 543 | rs[i].matching = 1; |
| 544 | continue; |
| 545 | } |
| 546 | |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 547 | if (rhs) { |
Junio C Hamano | 47c6ef1 | 2008-07-26 23:15:51 -0700 | [diff] [blame] | 548 | size_t rlen = strlen(++rhs); |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 549 | is_glob = (1 <= rlen && strchr(rhs, '*')); |
Daniel Barkalow | 08fbdb3 | 2009-03-07 01:11:36 -0500 | [diff] [blame] | 550 | rs[i].dst = xstrndup(rhs, rlen); |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | llen = (rhs ? (rhs - lhs - 1) : strlen(lhs)); |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 554 | if (1 <= llen && memchr(lhs, '*', llen)) { |
Junio C Hamano | 7d19da4 | 2008-03-25 21:15:52 -0700 | [diff] [blame] | 555 | if ((rhs && !is_glob) || (!rhs && fetch)) |
| 556 | goto invalid; |
| 557 | is_glob = 1; |
Junio C Hamano | 7d19da4 | 2008-03-25 21:15:52 -0700 | [diff] [blame] | 558 | } else if (rhs && is_glob) { |
| 559 | goto invalid; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 560 | } |
Junio C Hamano | 7d19da4 | 2008-03-25 21:15:52 -0700 | [diff] [blame] | 561 | |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 562 | rs[i].pattern = is_glob; |
| 563 | rs[i].src = xstrndup(lhs, llen); |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 564 | flags = REFNAME_ALLOW_ONELEVEL | (is_glob ? REFNAME_REFSPEC_PATTERN : 0); |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 565 | |
| 566 | if (fetch) { |
| 567 | /* |
| 568 | * LHS |
| 569 | * - empty is allowed; it means HEAD. |
| 570 | * - otherwise it must be a valid looking ref. |
| 571 | */ |
| 572 | if (!*rs[i].src) |
| 573 | ; /* empty is ok */ |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 574 | else if (check_refname_format(rs[i].src, flags)) |
| 575 | goto invalid; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 576 | /* |
| 577 | * RHS |
Junio C Hamano | 7d19da4 | 2008-03-25 21:15:52 -0700 | [diff] [blame] | 578 | * - missing is ok, and is same as empty. |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 579 | * - empty is ok; it means not to store. |
| 580 | * - otherwise it must be a valid looking ref. |
| 581 | */ |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 582 | if (!rs[i].dst) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 583 | ; /* ok */ |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 584 | else if (!*rs[i].dst) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 585 | ; /* ok */ |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 586 | else if (check_refname_format(rs[i].dst, flags)) |
| 587 | goto invalid; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 588 | } else { |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 589 | /* |
| 590 | * LHS |
| 591 | * - empty is allowed; it means delete. |
| 592 | * - when wildcarded, it must be a valid looking ref. |
| 593 | * - otherwise, it must be an extended SHA-1, but |
| 594 | * there is no existing way to validate this. |
| 595 | */ |
| 596 | if (!*rs[i].src) |
| 597 | ; /* empty is ok */ |
| 598 | else if (is_glob) { |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 599 | if (check_refname_format(rs[i].src, flags)) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 600 | goto invalid; |
| 601 | } |
| 602 | else |
| 603 | ; /* anything goes, for now */ |
| 604 | /* |
| 605 | * RHS |
| 606 | * - missing is allowed, but LHS then must be a |
| 607 | * valid looking ref. |
| 608 | * - empty is not allowed. |
| 609 | * - otherwise it must be a valid looking ref. |
| 610 | */ |
| 611 | if (!rs[i].dst) { |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 612 | if (check_refname_format(rs[i].src, flags)) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 613 | goto invalid; |
| 614 | } else if (!*rs[i].dst) { |
| 615 | goto invalid; |
| 616 | } else { |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 617 | if (check_refname_format(rs[i].dst, flags)) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 618 | goto invalid; |
| 619 | } |
Daniel Barkalow | ef00d15 | 2008-03-17 22:05:23 -0400 | [diff] [blame] | 620 | } |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 621 | } |
| 622 | return rs; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 623 | |
| 624 | invalid: |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 625 | if (verify) { |
Brandon Casey | 2cb1f36 | 2008-08-21 19:16:30 -0500 | [diff] [blame] | 626 | /* |
| 627 | * nr_refspec must be greater than zero and i must be valid |
| 628 | * since it is only possible to reach this point from within |
| 629 | * the for loop above. |
| 630 | */ |
| 631 | free_refspecs(rs, i+1); |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 632 | return NULL; |
| 633 | } |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 634 | die("Invalid refspec '%s'", refspec[i]); |
| 635 | } |
| 636 | |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 637 | int valid_fetch_refspec(const char *fetch_refspec_str) |
| 638 | { |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 639 | struct refspec *refspec; |
| 640 | |
Gary V. Vaughan | 66dbfd5 | 2010-05-14 09:31:33 +0000 | [diff] [blame] | 641 | refspec = parse_refspec_internal(1, &fetch_refspec_str, 1, 1); |
Brandon Casey | 2cb1f36 | 2008-08-21 19:16:30 -0500 | [diff] [blame] | 642 | free_refspecs(refspec, 1); |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 643 | return !!refspec; |
| 644 | } |
| 645 | |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 646 | struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec) |
| 647 | { |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 648 | return parse_refspec_internal(nr_refspec, refspec, 1, 0); |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 649 | } |
| 650 | |
Nanako Shiraishi | 697d7f5 | 2008-09-25 18:41:00 +0900 | [diff] [blame] | 651 | static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 652 | { |
Jonas Fonseca | 24b6177 | 2008-04-13 11:56:54 +0200 | [diff] [blame] | 653 | return parse_refspec_internal(nr_refspec, refspec, 0, 0); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 654 | } |
| 655 | |
Daniel Barkalow | 72ff894 | 2009-11-18 02:42:28 +0100 | [diff] [blame] | 656 | void free_refspec(int nr_refspec, struct refspec *refspec) |
| 657 | { |
| 658 | int i; |
| 659 | for (i = 0; i < nr_refspec; i++) { |
| 660 | free(refspec[i].src); |
| 661 | free(refspec[i].dst); |
| 662 | } |
| 663 | free(refspec); |
| 664 | } |
| 665 | |
Daniel Barkalow | df93e33 | 2008-02-15 14:14:18 -0500 | [diff] [blame] | 666 | static int valid_remote_nick(const char *name) |
| 667 | { |
Alexander Potashev | 8ca12c0 | 2009-01-10 15:07:50 +0300 | [diff] [blame] | 668 | if (!name[0] || is_dot_or_dotdot(name)) |
Daniel Barkalow | df93e33 | 2008-02-15 14:14:18 -0500 | [diff] [blame] | 669 | return 0; |
| 670 | return !strchr(name, '/'); /* no slash */ |
| 671 | } |
| 672 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 673 | struct remote *remote_get(const char *name) |
| 674 | { |
| 675 | struct remote *ret; |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 676 | int name_given = 0; |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 677 | |
| 678 | read_config(); |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 679 | if (name) |
| 680 | name_given = 1; |
| 681 | else { |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 682 | name = default_remote_name; |
Daniel Barkalow | fa685bd | 2009-03-11 01:47:20 -0400 | [diff] [blame] | 683 | name_given = explicit_default_remote_name; |
| 684 | } |
Junio C Hamano | 9326d49 | 2009-03-16 00:35:09 -0700 | [diff] [blame] | 685 | |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 686 | ret = make_remote(name, 0); |
Daniel Barkalow | df93e33 | 2008-02-15 14:14:18 -0500 | [diff] [blame] | 687 | if (valid_remote_nick(name)) { |
Daniel Barkalow | 0a4da29 | 2009-11-18 02:42:23 +0100 | [diff] [blame] | 688 | if (!valid_remote(ret)) |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 689 | read_remotes_file(ret); |
Daniel Barkalow | 0a4da29 | 2009-11-18 02:42:23 +0100 | [diff] [blame] | 690 | if (!valid_remote(ret)) |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 691 | read_branches_file(ret); |
| 692 | } |
Daniel Barkalow | 0a4da29 | 2009-11-18 02:42:23 +0100 | [diff] [blame] | 693 | if (name_given && !valid_remote(ret)) |
Daniel Barkalow | 55029ae | 2008-02-20 13:43:53 -0500 | [diff] [blame] | 694 | add_url_alias(ret, name); |
Daniel Barkalow | 0a4da29 | 2009-11-18 02:42:23 +0100 | [diff] [blame] | 695 | if (!valid_remote(ret)) |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 696 | return NULL; |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 697 | ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec); |
| 698 | ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec); |
Daniel Barkalow | 5751f49 | 2007-05-12 11:45:53 -0400 | [diff] [blame] | 699 | return ret; |
| 700 | } |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 701 | |
Finn Arne Gangstad | 9a23ba3 | 2009-04-06 15:41:01 +0200 | [diff] [blame] | 702 | int remote_is_configured(const char *name) |
| 703 | { |
| 704 | int i; |
| 705 | read_config(); |
| 706 | |
| 707 | for (i = 0; i < remotes_nr; i++) |
| 708 | if (!strcmp(name, remotes[i]->name)) |
| 709 | return 1; |
| 710 | return 0; |
| 711 | } |
| 712 | |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 713 | int for_each_remote(each_remote_fn fn, void *priv) |
| 714 | { |
| 715 | int i, result = 0; |
| 716 | read_config(); |
Daniel Barkalow | 2d31347 | 2008-02-18 23:41:41 -0500 | [diff] [blame] | 717 | for (i = 0; i < remotes_nr && !result; i++) { |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 718 | struct remote *r = remotes[i]; |
| 719 | if (!r) |
| 720 | continue; |
| 721 | if (!r->fetch) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 722 | r->fetch = parse_fetch_refspec(r->fetch_refspec_nr, |
| 723 | r->fetch_refspec); |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 724 | if (!r->push) |
Junio C Hamano | 46220ca | 2008-03-20 23:34:37 -0700 | [diff] [blame] | 725 | r->push = parse_push_refspec(r->push_refspec_nr, |
| 726 | r->push_refspec); |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 727 | result = fn(r, priv); |
| 728 | } |
| 729 | return result; |
| 730 | } |
| 731 | |
Daniel Barkalow | 2467a4f | 2007-10-08 00:25:07 -0400 | [diff] [blame] | 732 | void ref_remove_duplicates(struct ref *ref_map) |
| 733 | { |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 734 | struct string_list refs = STRING_LIST_INIT_NODUP; |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 735 | struct string_list_item *item = NULL; |
| 736 | struct ref *prev = NULL, *next = NULL; |
| 737 | for (; ref_map; prev = ref_map, ref_map = next) { |
| 738 | next = ref_map->next; |
Daniel Barkalow | 2467a4f | 2007-10-08 00:25:07 -0400 | [diff] [blame] | 739 | if (!ref_map->peer_ref) |
| 740 | continue; |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 741 | |
Julian Phillips | e8c8b71 | 2010-06-26 00:41:37 +0100 | [diff] [blame] | 742 | item = string_list_lookup(&refs, ref_map->peer_ref->name); |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 743 | if (item) { |
| 744 | if (strcmp(((struct ref *)item->util)->name, |
| 745 | ref_map->name)) |
| 746 | die("%s tracks both %s and %s", |
| 747 | ref_map->peer_ref->name, |
| 748 | ((struct ref *)item->util)->name, |
| 749 | ref_map->name); |
| 750 | prev->next = ref_map->next; |
| 751 | free(ref_map->peer_ref); |
| 752 | free(ref_map); |
Julian Phillips | 95c96d4 | 2009-11-13 21:25:56 +0000 | [diff] [blame] | 753 | ref_map = prev; /* skip this; we freed it */ |
| 754 | continue; |
Daniel Barkalow | 2467a4f | 2007-10-08 00:25:07 -0400 | [diff] [blame] | 755 | } |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 756 | |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 757 | item = string_list_insert(&refs, ref_map->peer_ref->name); |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 758 | item->util = ref_map; |
Daniel Barkalow | 2467a4f | 2007-10-08 00:25:07 -0400 | [diff] [blame] | 759 | } |
Julian Phillips | 73cf082 | 2009-10-25 21:28:11 +0000 | [diff] [blame] | 760 | string_list_clear(&refs, 0); |
Daniel Barkalow | 2467a4f | 2007-10-08 00:25:07 -0400 | [diff] [blame] | 761 | } |
| 762 | |
Shawn O. Pearce | 28b91f8 | 2007-09-19 00:49:27 -0400 | [diff] [blame] | 763 | int remote_has_url(struct remote *remote, const char *url) |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 764 | { |
| 765 | int i; |
Shawn O. Pearce | 28b91f8 | 2007-09-19 00:49:27 -0400 | [diff] [blame] | 766 | for (i = 0; i < remote->url_nr; i++) { |
| 767 | if (!strcmp(remote->url[i], url)) |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 768 | return 1; |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 773 | static int match_name_with_pattern(const char *key, const char *name, |
| 774 | const char *value, char **result) |
Daniel Barkalow | a3c8423 | 2009-03-07 01:11:29 -0500 | [diff] [blame] | 775 | { |
Daniel Barkalow | 08fbdb3 | 2009-03-07 01:11:36 -0500 | [diff] [blame] | 776 | const char *kstar = strchr(key, '*'); |
| 777 | size_t klen; |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 778 | size_t ksuffixlen; |
| 779 | size_t namelen; |
Daniel Barkalow | 08fbdb3 | 2009-03-07 01:11:36 -0500 | [diff] [blame] | 780 | int ret; |
| 781 | if (!kstar) |
| 782 | die("Key '%s' of pattern had no '*'", key); |
| 783 | klen = kstar - key; |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 784 | ksuffixlen = strlen(kstar + 1); |
| 785 | namelen = strlen(name); |
| 786 | ret = !strncmp(name, key, klen) && namelen >= klen + ksuffixlen && |
| 787 | !memcmp(name + namelen - ksuffixlen, kstar + 1, ksuffixlen); |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 788 | if (ret && value) { |
Daniel Barkalow | 08fbdb3 | 2009-03-07 01:11:36 -0500 | [diff] [blame] | 789 | const char *vstar = strchr(value, '*'); |
| 790 | size_t vlen; |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 791 | size_t vsuffixlen; |
Daniel Barkalow | 08fbdb3 | 2009-03-07 01:11:36 -0500 | [diff] [blame] | 792 | if (!vstar) |
| 793 | die("Value '%s' of pattern has no '*'", value); |
| 794 | vlen = vstar - value; |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 795 | vsuffixlen = strlen(vstar + 1); |
| 796 | *result = xmalloc(vlen + vsuffixlen + |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 797 | strlen(name) - |
Daniel Barkalow | abd2bde | 2009-03-07 01:11:39 -0500 | [diff] [blame] | 798 | klen - ksuffixlen + 1); |
| 799 | strncpy(*result, value, vlen); |
| 800 | strncpy(*result + vlen, |
| 801 | name + klen, namelen - klen - ksuffixlen); |
| 802 | strcpy(*result + vlen + namelen - klen - ksuffixlen, |
| 803 | vstar + 1); |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 804 | } |
Daniel Barkalow | a3c8423 | 2009-03-07 01:11:29 -0500 | [diff] [blame] | 805 | return ret; |
| 806 | } |
| 807 | |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 808 | static int query_refspecs(struct refspec *refs, int ref_count, struct refspec *query) |
Daniel Barkalow | 72ff894 | 2009-11-18 02:42:28 +0100 | [diff] [blame] | 809 | { |
| 810 | int i; |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 811 | int find_src = !query->src; |
Daniel Barkalow | 72ff894 | 2009-11-18 02:42:28 +0100 | [diff] [blame] | 812 | |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 813 | if (find_src && !query->dst) |
| 814 | return error("query_refspecs: need either src or dst"); |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 815 | |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 816 | for (i = 0; i < ref_count; i++) { |
| 817 | struct refspec *refspec = &refs[i]; |
| 818 | const char *key = find_src ? refspec->dst : refspec->src; |
| 819 | const char *value = find_src ? refspec->src : refspec->dst; |
| 820 | const char *needle = find_src ? query->dst : query->src; |
| 821 | char **result = find_src ? &query->src : &query->dst; |
| 822 | |
Shawn O. Pearce | 009c5bc | 2007-09-25 00:13:14 -0400 | [diff] [blame] | 823 | if (!refspec->dst) |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 824 | continue; |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 825 | if (refspec->pattern) { |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 826 | if (match_name_with_pattern(key, needle, value, result)) { |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 827 | query->force = refspec->force; |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 828 | return 0; |
| 829 | } |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 830 | } else if (!strcmp(needle, key)) { |
| 831 | *result = xstrdup(value); |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 832 | query->force = refspec->force; |
Johannes Schindelin | b42f692 | 2007-07-10 18:48:40 +0100 | [diff] [blame] | 833 | return 0; |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 834 | } |
| 835 | } |
Daniel Barkalow | 5d46c9d | 2007-05-12 11:46:03 -0400 | [diff] [blame] | 836 | return -1; |
| 837 | } |
| 838 | |
Carlos Martín Nieto | c500352 | 2011-10-15 07:04:24 +0200 | [diff] [blame] | 839 | char *apply_refspecs(struct refspec *refspecs, int nr_refspec, |
| 840 | const char *name) |
| 841 | { |
| 842 | struct refspec query; |
| 843 | |
| 844 | memset(&query, 0, sizeof(struct refspec)); |
| 845 | query.src = (char *)name; |
| 846 | |
| 847 | if (query_refspecs(refspecs, nr_refspec, &query)) |
| 848 | return NULL; |
| 849 | |
| 850 | return query.dst; |
| 851 | } |
| 852 | |
| 853 | int remote_find_tracking(struct remote *remote, struct refspec *refspec) |
| 854 | { |
| 855 | return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec); |
| 856 | } |
| 857 | |
René Scharfe | 8009768 | 2008-10-18 10:37:40 +0200 | [diff] [blame] | 858 | static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, |
| 859 | const char *name) |
| 860 | { |
| 861 | size_t len = strlen(name); |
| 862 | struct ref *ref = xcalloc(1, sizeof(struct ref) + prefixlen + len + 1); |
| 863 | memcpy(ref->name, prefix, prefixlen); |
| 864 | memcpy(ref->name + prefixlen, name, len); |
| 865 | return ref; |
| 866 | } |
| 867 | |
René Scharfe | 59c69c0 | 2008-10-18 10:44:18 +0200 | [diff] [blame] | 868 | struct ref *alloc_ref(const char *name) |
Daniel Barkalow | dfd255d | 2007-07-10 00:47:23 -0400 | [diff] [blame] | 869 | { |
René Scharfe | 59c69c0 | 2008-10-18 10:44:18 +0200 | [diff] [blame] | 870 | return alloc_ref_with_prefix("", 0, name); |
Krzysztof Kowalczyk | 737922a | 2008-05-10 16:26:58 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Jeff King | 59a5775 | 2011-06-07 19:03:03 -0400 | [diff] [blame] | 873 | struct ref *copy_ref(const struct ref *ref) |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 874 | { |
Jay Soffian | 7b3db09 | 2009-02-27 14:10:04 -0500 | [diff] [blame] | 875 | struct ref *cpy; |
| 876 | size_t len; |
| 877 | if (!ref) |
| 878 | return NULL; |
| 879 | len = strlen(ref->name); |
| 880 | cpy = xmalloc(sizeof(struct ref) + len + 1); |
| 881 | memcpy(cpy, ref, sizeof(struct ref) + len + 1); |
| 882 | cpy->next = NULL; |
| 883 | cpy->symref = ref->symref ? xstrdup(ref->symref) : NULL; |
| 884 | cpy->remote_status = ref->remote_status ? xstrdup(ref->remote_status) : NULL; |
| 885 | cpy->peer_ref = copy_ref(ref->peer_ref); |
| 886 | return cpy; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 889 | struct ref *copy_ref_list(const struct ref *ref) |
| 890 | { |
| 891 | struct ref *ret = NULL; |
| 892 | struct ref **tail = &ret; |
| 893 | while (ref) { |
| 894 | *tail = copy_ref(ref); |
| 895 | ref = ref->next; |
| 896 | tail = &((*tail)->next); |
| 897 | } |
| 898 | return ret; |
| 899 | } |
| 900 | |
Nanako Shiraishi | 697d7f5 | 2008-09-25 18:41:00 +0900 | [diff] [blame] | 901 | static void free_ref(struct ref *ref) |
Daniel Barkalow | be885d9 | 2008-04-26 15:53:12 -0400 | [diff] [blame] | 902 | { |
| 903 | if (!ref) |
| 904 | return; |
Jay Soffian | 7b3db09 | 2009-02-27 14:10:04 -0500 | [diff] [blame] | 905 | free_ref(ref->peer_ref); |
Daniel Barkalow | be885d9 | 2008-04-26 15:53:12 -0400 | [diff] [blame] | 906 | free(ref->remote_status); |
| 907 | free(ref->symref); |
| 908 | free(ref); |
| 909 | } |
| 910 | |
Daniel Barkalow | dfd255d | 2007-07-10 00:47:23 -0400 | [diff] [blame] | 911 | void free_refs(struct ref *ref) |
| 912 | { |
| 913 | struct ref *next; |
| 914 | while (ref) { |
| 915 | next = ref->next; |
Daniel Barkalow | be885d9 | 2008-04-26 15:53:12 -0400 | [diff] [blame] | 916 | free_ref(ref); |
Daniel Barkalow | dfd255d | 2007-07-10 00:47:23 -0400 | [diff] [blame] | 917 | ref = next; |
| 918 | } |
| 919 | } |
| 920 | |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 921 | static int count_refspec_match(const char *pattern, |
| 922 | struct ref *refs, |
| 923 | struct ref **matched_ref) |
| 924 | { |
| 925 | int patlen = strlen(pattern); |
| 926 | struct ref *matched_weak = NULL; |
| 927 | struct ref *matched = NULL; |
| 928 | int weak_match = 0; |
| 929 | int match = 0; |
| 930 | |
| 931 | for (weak_match = match = 0; refs; refs = refs->next) { |
| 932 | char *name = refs->name; |
| 933 | int namelen = strlen(name); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 934 | |
Steffen Prohaska | ae36bdc | 2007-11-11 15:01:47 +0100 | [diff] [blame] | 935 | if (!refname_match(pattern, name, ref_rev_parse_rules)) |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 936 | continue; |
| 937 | |
| 938 | /* A match is "weak" if it is with refs outside |
| 939 | * heads or tags, and did not specify the pattern |
| 940 | * in full (e.g. "refs/remotes/origin/master") or at |
| 941 | * least from the toplevel (e.g. "remotes/origin/master"); |
| 942 | * otherwise "git push $URL master" would result in |
| 943 | * ambiguity between remotes/origin/master and heads/master |
| 944 | * at the remote site. |
| 945 | */ |
| 946 | if (namelen != patlen && |
| 947 | patlen != namelen - 5 && |
| 948 | prefixcmp(name, "refs/heads/") && |
| 949 | prefixcmp(name, "refs/tags/")) { |
| 950 | /* We want to catch the case where only weak |
| 951 | * matches are found and there are multiple |
| 952 | * matches, and where more than one strong |
| 953 | * matches are found, as ambiguous. One |
| 954 | * strong match with zero or more weak matches |
| 955 | * are acceptable as a unique match. |
| 956 | */ |
| 957 | matched_weak = refs; |
| 958 | weak_match++; |
| 959 | } |
| 960 | else { |
| 961 | matched = refs; |
| 962 | match++; |
| 963 | } |
| 964 | } |
| 965 | if (!matched) { |
| 966 | *matched_ref = matched_weak; |
| 967 | return weak_match; |
| 968 | } |
| 969 | else { |
| 970 | *matched_ref = matched; |
| 971 | return match; |
| 972 | } |
| 973 | } |
| 974 | |
Daniel Barkalow | 1d73526 | 2007-07-10 00:47:26 -0400 | [diff] [blame] | 975 | static void tail_link_ref(struct ref *ref, struct ref ***tail) |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 976 | { |
| 977 | **tail = ref; |
Daniel Barkalow | 1d73526 | 2007-07-10 00:47:26 -0400 | [diff] [blame] | 978 | while (ref->next) |
| 979 | ref = ref->next; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 980 | *tail = &ref->next; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 981 | } |
| 982 | |
Felipe Contreras | 6765524 | 2012-02-23 00:43:40 +0200 | [diff] [blame] | 983 | static struct ref *alloc_delete_ref(void) |
| 984 | { |
| 985 | struct ref *ref = alloc_ref("(delete)"); |
| 986 | hashclr(ref->new_sha1); |
| 987 | return ref; |
| 988 | } |
| 989 | |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 990 | static struct ref *try_explicit_object_name(const char *name) |
| 991 | { |
| 992 | unsigned char sha1[20]; |
| 993 | struct ref *ref; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 994 | |
Felipe Contreras | 6765524 | 2012-02-23 00:43:40 +0200 | [diff] [blame] | 995 | if (!*name) |
| 996 | return alloc_delete_ref(); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 997 | if (get_sha1(name, sha1)) |
| 998 | return NULL; |
René Scharfe | 59c69c0 | 2008-10-18 10:44:18 +0200 | [diff] [blame] | 999 | ref = alloc_ref(name); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1000 | hashcpy(ref->new_sha1, sha1); |
| 1001 | return ref; |
| 1002 | } |
| 1003 | |
Daniel Barkalow | 1d73526 | 2007-07-10 00:47:26 -0400 | [diff] [blame] | 1004 | static struct ref *make_linked_ref(const char *name, struct ref ***tail) |
Junio C Hamano | 163f0ee | 2007-06-09 00:07:34 -0700 | [diff] [blame] | 1005 | { |
René Scharfe | 59c69c0 | 2008-10-18 10:44:18 +0200 | [diff] [blame] | 1006 | struct ref *ret = alloc_ref(name); |
Daniel Barkalow | 1d73526 | 2007-07-10 00:47:26 -0400 | [diff] [blame] | 1007 | tail_link_ref(ret, tail); |
| 1008 | return ret; |
Junio C Hamano | 163f0ee | 2007-06-09 00:07:34 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Jeff King | f8aae12 | 2008-04-23 05:16:06 -0400 | [diff] [blame] | 1011 | static char *guess_ref(const char *name, struct ref *peer) |
| 1012 | { |
| 1013 | struct strbuf buf = STRBUF_INIT; |
| 1014 | unsigned char sha1[20]; |
| 1015 | |
Nguyễn Thái Ngọc Duy | 8cad474 | 2011-12-12 18:20:32 +0700 | [diff] [blame] | 1016 | const char *r = resolve_ref_unsafe(peer->name, sha1, 1, NULL); |
Jeff King | f8aae12 | 2008-04-23 05:16:06 -0400 | [diff] [blame] | 1017 | if (!r) |
| 1018 | return NULL; |
| 1019 | |
| 1020 | if (!prefixcmp(r, "refs/heads/")) |
| 1021 | strbuf_addstr(&buf, "refs/heads/"); |
| 1022 | else if (!prefixcmp(r, "refs/tags/")) |
| 1023 | strbuf_addstr(&buf, "refs/tags/"); |
| 1024 | else |
| 1025 | return NULL; |
| 1026 | |
| 1027 | strbuf_addstr(&buf, name); |
| 1028 | return strbuf_detach(&buf, NULL); |
| 1029 | } |
| 1030 | |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1031 | static int match_explicit(struct ref *src, struct ref *dst, |
| 1032 | struct ref ***dst_tail, |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1033 | struct refspec *rs) |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1034 | { |
| 1035 | struct ref *matched_src, *matched_dst; |
Jay Soffian | cdf690e | 2009-02-25 03:32:16 -0500 | [diff] [blame] | 1036 | int copy_src; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1037 | |
| 1038 | const char *dst_value = rs->dst; |
Jeff King | f8aae12 | 2008-04-23 05:16:06 -0400 | [diff] [blame] | 1039 | char *dst_guess; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1040 | |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1041 | if (rs->pattern || rs->matching) |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1042 | return 0; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1043 | |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1044 | matched_src = matched_dst = NULL; |
| 1045 | switch (count_refspec_match(rs->src, src, &matched_src)) { |
| 1046 | case 1: |
Jay Soffian | cdf690e | 2009-02-25 03:32:16 -0500 | [diff] [blame] | 1047 | copy_src = 1; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1048 | break; |
| 1049 | case 0: |
| 1050 | /* The source could be in the get_sha1() format |
| 1051 | * not a reference name. :refs/other is a |
| 1052 | * way to delete 'other' ref at the remote end. |
| 1053 | */ |
| 1054 | matched_src = try_explicit_object_name(rs->src); |
Shawn O. Pearce | 7dfee37 | 2007-09-25 00:13:19 -0400 | [diff] [blame] | 1055 | if (!matched_src) |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1056 | return error("src refspec %s does not match any.", rs->src); |
Jay Soffian | cdf690e | 2009-02-25 03:32:16 -0500 | [diff] [blame] | 1057 | copy_src = 0; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1058 | break; |
| 1059 | default: |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1060 | return error("src refspec %s matches more than one.", rs->src); |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1061 | } |
Junio C Hamano | 3c8b7df | 2007-06-09 00:14:04 -0700 | [diff] [blame] | 1062 | |
Shawn O. Pearce | 4491e62 | 2007-09-25 00:13:25 -0400 | [diff] [blame] | 1063 | if (!dst_value) { |
Daniel Barkalow | 9f0ea7e | 2008-02-20 12:54:05 -0500 | [diff] [blame] | 1064 | unsigned char sha1[20]; |
| 1065 | int flag; |
| 1066 | |
Nguyễn Thái Ngọc Duy | 8cad474 | 2011-12-12 18:20:32 +0700 | [diff] [blame] | 1067 | dst_value = resolve_ref_unsafe(matched_src->name, sha1, 1, &flag); |
Daniel Barkalow | 9f0ea7e | 2008-02-20 12:54:05 -0500 | [diff] [blame] | 1068 | if (!dst_value || |
| 1069 | ((flag & REF_ISSYMREF) && |
| 1070 | prefixcmp(dst_value, "refs/heads/"))) |
| 1071 | die("%s cannot be resolved to branch.", |
| 1072 | matched_src->name); |
Shawn O. Pearce | 4491e62 | 2007-09-25 00:13:25 -0400 | [diff] [blame] | 1073 | } |
Junio C Hamano | 3c8b7df | 2007-06-09 00:14:04 -0700 | [diff] [blame] | 1074 | |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1075 | switch (count_refspec_match(dst_value, dst, &matched_dst)) { |
| 1076 | case 1: |
| 1077 | break; |
| 1078 | case 0: |
Junio C Hamano | 163f0ee | 2007-06-09 00:07:34 -0700 | [diff] [blame] | 1079 | if (!memcmp(dst_value, "refs/", 5)) |
Daniel Barkalow | 1d73526 | 2007-07-10 00:47:26 -0400 | [diff] [blame] | 1080 | matched_dst = make_linked_ref(dst_value, dst_tail); |
Brian Gianforcaro | eeefa7c | 2009-09-01 01:35:10 -0400 | [diff] [blame] | 1081 | else if ((dst_guess = guess_ref(dst_value, matched_src))) |
Jeff King | f8aae12 | 2008-04-23 05:16:06 -0400 | [diff] [blame] | 1082 | matched_dst = make_linked_ref(dst_guess, dst_tail); |
Junio C Hamano | 3c8b7df | 2007-06-09 00:14:04 -0700 | [diff] [blame] | 1083 | else |
Jeff King | f8aae12 | 2008-04-23 05:16:06 -0400 | [diff] [blame] | 1084 | error("unable to push to unqualified destination: %s\n" |
| 1085 | "The destination refspec neither matches an " |
| 1086 | "existing ref on the remote nor\n" |
| 1087 | "begins with refs/, and we are unable to " |
| 1088 | "guess a prefix based on the source ref.", |
| 1089 | dst_value); |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1090 | break; |
| 1091 | default: |
Junio C Hamano | 3c8b7df | 2007-06-09 00:14:04 -0700 | [diff] [blame] | 1092 | matched_dst = NULL; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1093 | error("dst refspec %s matches more than one.", |
| 1094 | dst_value); |
| 1095 | break; |
| 1096 | } |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1097 | if (!matched_dst) |
| 1098 | return -1; |
| 1099 | if (matched_dst->peer_ref) |
| 1100 | return error("dst ref %s receives from more than one src.", |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1101 | matched_dst->name); |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1102 | else { |
Jay Soffian | cdf690e | 2009-02-25 03:32:16 -0500 | [diff] [blame] | 1103 | matched_dst->peer_ref = copy_src ? copy_ref(matched_src) : matched_src; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1104 | matched_dst->force = rs->force; |
| 1105 | } |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1106 | return 0; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1109 | static int match_explicit_refs(struct ref *src, struct ref *dst, |
| 1110 | struct ref ***dst_tail, struct refspec *rs, |
| 1111 | int rs_nr) |
| 1112 | { |
| 1113 | int i, errs; |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1114 | for (i = errs = 0; i < rs_nr; i++) |
Jeff King | 9a7bbd1 | 2008-06-16 12:15:02 -0400 | [diff] [blame] | 1115 | errs += match_explicit(src, dst, dst_tail, &rs[i]); |
| 1116 | return errs; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1117 | } |
| 1118 | |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1119 | static char *get_ref_match(const struct refspec *rs, int rs_nr, const struct ref *ref, |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 1120 | int send_mirror, int direction, const struct refspec **ret_pat) |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 1121 | { |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1122 | const struct refspec *pat; |
| 1123 | char *name; |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 1124 | int i; |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1125 | int matching_refs = -1; |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 1126 | for (i = 0; i < rs_nr; i++) { |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1127 | if (rs[i].matching && |
| 1128 | (matching_refs == -1 || rs[i].force)) { |
| 1129 | matching_refs = i; |
| 1130 | continue; |
| 1131 | } |
| 1132 | |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1133 | if (rs[i].pattern) { |
| 1134 | const char *dst_side = rs[i].dst ? rs[i].dst : rs[i].src; |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 1135 | int match; |
| 1136 | if (direction == FROM_SRC) |
| 1137 | match = match_name_with_pattern(rs[i].src, ref->name, dst_side, &name); |
| 1138 | else |
| 1139 | match = match_name_with_pattern(dst_side, ref->name, rs[i].src, &name); |
| 1140 | if (match) { |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1141 | matching_refs = i; |
| 1142 | break; |
| 1143 | } |
| 1144 | } |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 1145 | } |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1146 | if (matching_refs == -1) |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1147 | return NULL; |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1148 | |
| 1149 | pat = rs + matching_refs; |
| 1150 | if (pat->matching) { |
| 1151 | /* |
| 1152 | * "matching refs"; traditionally we pushed everything |
| 1153 | * including refs outside refs/heads/ hierarchy, but |
| 1154 | * that does not make much sense these days. |
| 1155 | */ |
| 1156 | if (!send_mirror && prefixcmp(ref->name, "refs/heads/")) |
| 1157 | return NULL; |
| 1158 | name = xstrdup(ref->name); |
| 1159 | } |
| 1160 | if (ret_pat) |
| 1161 | *ret_pat = pat; |
| 1162 | return name; |
Daniel Barkalow | 8558fd9 | 2007-05-25 01:20:56 -0400 | [diff] [blame] | 1163 | } |
| 1164 | |
Clemens Buchacher | 6d2bf96 | 2009-05-31 16:26:48 +0200 | [diff] [blame] | 1165 | static struct ref **tail_ref(struct ref **head) |
| 1166 | { |
| 1167 | struct ref **tail = head; |
| 1168 | while (*tail) |
| 1169 | tail = &((*tail)->next); |
| 1170 | return tail; |
| 1171 | } |
| 1172 | |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1173 | /* |
Junio C Hamano | 29753cd | 2011-09-09 11:54:58 -0700 | [diff] [blame] | 1174 | * Given the set of refs the local repository has, the set of refs the |
| 1175 | * remote repository has, and the refspec used for push, determine |
| 1176 | * what remote refs we will update and with what value by setting |
| 1177 | * peer_ref (which object is being pushed) and force (if the push is |
| 1178 | * forced) in elements of "dst". The function may add new elements to |
| 1179 | * dst (e.g. pushing to a new branch, done in match_explicit_refs). |
Junio C Hamano | 54a8ad9 | 2007-06-08 23:22:58 -0700 | [diff] [blame] | 1180 | */ |
Junio C Hamano | 29753cd | 2011-09-09 11:54:58 -0700 | [diff] [blame] | 1181 | int match_push_refs(struct ref *src, struct ref **dst, |
| 1182 | int nr_refspec, const char **refspec, int flags) |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1183 | { |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1184 | struct refspec *rs; |
Andy Whitcroft | 28b9d6e | 2007-11-09 23:32:10 +0000 | [diff] [blame] | 1185 | int send_all = flags & MATCH_REFS_ALL; |
| 1186 | int send_mirror = flags & MATCH_REFS_MIRROR; |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 1187 | int send_prune = flags & MATCH_REFS_PRUNE; |
Jay Soffian | 5f48cb9 | 2009-02-25 03:32:17 -0500 | [diff] [blame] | 1188 | int errs; |
Linus Torvalds | 2af202b | 2009-06-18 10:28:43 -0700 | [diff] [blame] | 1189 | static const char *default_refspec[] = { ":", NULL }; |
Felipe Contreras | b1d8b1f | 2012-02-23 00:43:38 +0200 | [diff] [blame] | 1190 | struct ref *ref, **dst_tail = tail_ref(dst); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1191 | |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1192 | if (!nr_refspec) { |
| 1193 | nr_refspec = 1; |
| 1194 | refspec = default_refspec; |
| 1195 | } |
| 1196 | rs = parse_push_refspec(nr_refspec, (const char **) refspec); |
Clemens Buchacher | 6d2bf96 | 2009-05-31 16:26:48 +0200 | [diff] [blame] | 1197 | errs = match_explicit_refs(src, *dst, &dst_tail, rs, nr_refspec); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1198 | |
| 1199 | /* pick the remainder */ |
Felipe Contreras | b1d8b1f | 2012-02-23 00:43:38 +0200 | [diff] [blame] | 1200 | for (ref = src; ref; ref = ref->next) { |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1201 | struct ref *dst_peer; |
Alex Riesen | 6e66bf3 | 2007-06-08 01:43:05 +0200 | [diff] [blame] | 1202 | const struct refspec *pat = NULL; |
| 1203 | char *dst_name; |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1204 | |
Felipe Contreras | b1d8b1f | 2012-02-23 00:43:38 +0200 | [diff] [blame] | 1205 | if (ref->peer_ref) |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1206 | continue; |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1207 | |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 1208 | dst_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_SRC, &pat); |
Felipe Contreras | db70a04 | 2012-02-23 00:43:39 +0200 | [diff] [blame] | 1209 | if (!dst_name) |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1210 | continue; |
| 1211 | |
Clemens Buchacher | 6d2bf96 | 2009-05-31 16:26:48 +0200 | [diff] [blame] | 1212 | dst_peer = find_ref_by_name(*dst, dst_name); |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1213 | if (dst_peer) { |
| 1214 | if (dst_peer->peer_ref) |
| 1215 | /* We're already sending something to this ref. */ |
| 1216 | goto free_name; |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1217 | } else { |
| 1218 | if (pat->matching && !(send_all || send_mirror)) |
| 1219 | /* |
| 1220 | * Remote doesn't have it, and we have no |
| 1221 | * explicit pattern, and we don't have |
| 1222 | * --all nor --mirror. |
| 1223 | */ |
| 1224 | goto free_name; |
| 1225 | |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1226 | /* Create a new one and link it */ |
Clemens Buchacher | 6d2bf96 | 2009-05-31 16:26:48 +0200 | [diff] [blame] | 1227 | dst_peer = make_linked_ref(dst_name, &dst_tail); |
Felipe Contreras | b1d8b1f | 2012-02-23 00:43:38 +0200 | [diff] [blame] | 1228 | hashcpy(dst_peer->new_sha1, ref->new_sha1); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1229 | } |
Felipe Contreras | b1d8b1f | 2012-02-23 00:43:38 +0200 | [diff] [blame] | 1230 | dst_peer->peer_ref = copy_ref(ref); |
Paolo Bonzini | a83619d | 2008-04-28 11:32:12 -0400 | [diff] [blame] | 1231 | dst_peer->force = pat->force; |
Alex Riesen | 6e66bf3 | 2007-06-08 01:43:05 +0200 | [diff] [blame] | 1232 | free_name: |
| 1233 | free(dst_name); |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1234 | } |
Felipe Contreras | 6ddba5e | 2012-02-23 00:43:41 +0200 | [diff] [blame] | 1235 | if (send_prune) { |
| 1236 | /* check for missing refs on the remote */ |
| 1237 | for (ref = *dst; ref; ref = ref->next) { |
| 1238 | char *src_name; |
| 1239 | |
| 1240 | if (ref->peer_ref) |
| 1241 | /* We're already sending something to this ref. */ |
| 1242 | continue; |
| 1243 | |
| 1244 | src_name = get_ref_match(rs, nr_refspec, ref, send_mirror, FROM_DST, NULL); |
| 1245 | if (src_name) { |
| 1246 | if (!find_ref_by_name(src, src_name)) |
| 1247 | ref->peer_ref = alloc_delete_ref(); |
| 1248 | free(src_name); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
Jay Soffian | 5f48cb9 | 2009-02-25 03:32:17 -0500 | [diff] [blame] | 1252 | if (errs) |
| 1253 | return -1; |
Daniel Barkalow | 6b62816 | 2007-05-12 11:45:59 -0400 | [diff] [blame] | 1254 | return 0; |
| 1255 | } |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 1256 | |
Tay Ray Chuan | 20e8b46 | 2010-01-08 10:12:42 +0800 | [diff] [blame] | 1257 | void set_ref_status_for_push(struct ref *remote_refs, int send_mirror, |
| 1258 | int force_update) |
| 1259 | { |
| 1260 | struct ref *ref; |
| 1261 | |
| 1262 | for (ref = remote_refs; ref; ref = ref->next) { |
| 1263 | if (ref->peer_ref) |
| 1264 | hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); |
| 1265 | else if (!send_mirror) |
| 1266 | continue; |
| 1267 | |
| 1268 | ref->deletion = is_null_sha1(ref->new_sha1); |
| 1269 | if (!ref->deletion && |
| 1270 | !hashcmp(ref->old_sha1, ref->new_sha1)) { |
| 1271 | ref->status = REF_STATUS_UPTODATE; |
| 1272 | continue; |
| 1273 | } |
| 1274 | |
| 1275 | /* This part determines what can overwrite what. |
| 1276 | * The rules are: |
| 1277 | * |
| 1278 | * (0) you can always use --force or +A:B notation to |
| 1279 | * selectively force individual ref pairs. |
| 1280 | * |
| 1281 | * (1) if the old thing does not exist, it is OK. |
| 1282 | * |
| 1283 | * (2) if you do not have the old thing, you are not allowed |
| 1284 | * to overwrite it; you would not know what you are losing |
| 1285 | * otherwise. |
| 1286 | * |
| 1287 | * (3) if both new and old are commit-ish, and new is a |
| 1288 | * descendant of old, it is OK. |
| 1289 | * |
| 1290 | * (4) regardless of all of the above, removing :B is |
| 1291 | * always allowed. |
| 1292 | */ |
| 1293 | |
| 1294 | ref->nonfastforward = |
| 1295 | !ref->deletion && |
| 1296 | !is_null_sha1(ref->old_sha1) && |
| 1297 | (!has_sha1_file(ref->old_sha1) |
| 1298 | || !ref_newer(ref->new_sha1, ref->old_sha1)); |
| 1299 | |
| 1300 | if (ref->nonfastforward && !ref->force && !force_update) { |
| 1301 | ref->status = REF_STATUS_REJECT_NONFASTFORWARD; |
| 1302 | continue; |
| 1303 | } |
| 1304 | } |
| 1305 | } |
| 1306 | |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 1307 | struct branch *branch_get(const char *name) |
| 1308 | { |
| 1309 | struct branch *ret; |
| 1310 | |
| 1311 | read_config(); |
| 1312 | if (!name || !*name || !strcmp(name, "HEAD")) |
| 1313 | ret = current_branch; |
| 1314 | else |
| 1315 | ret = make_branch(name, 0); |
| 1316 | if (ret && ret->remote_name) { |
| 1317 | ret->remote = remote_get(ret->remote_name); |
| 1318 | if (ret->merge_nr) { |
| 1319 | int i; |
| 1320 | ret->merge = xcalloc(sizeof(*ret->merge), |
| 1321 | ret->merge_nr); |
| 1322 | for (i = 0; i < ret->merge_nr; i++) { |
| 1323 | ret->merge[i] = xcalloc(1, sizeof(**ret->merge)); |
| 1324 | ret->merge[i]->src = xstrdup(ret->merge_name[i]); |
Michael J Gruber | 5e6e2b4 | 2009-04-01 23:42:49 +0200 | [diff] [blame] | 1325 | if (remote_find_tracking(ret->remote, ret->merge[i]) |
| 1326 | && !strcmp(ret->remote_name, ".")) |
| 1327 | ret->merge[i]->dst = xstrdup(ret->merge_name[i]); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | return ret; |
| 1332 | } |
| 1333 | |
| 1334 | int branch_has_merge_config(struct branch *branch) |
| 1335 | { |
| 1336 | return branch && !!branch->merge; |
| 1337 | } |
| 1338 | |
Shawn O. Pearce | 85682c1 | 2007-09-18 04:54:53 -0400 | [diff] [blame] | 1339 | int branch_merge_matches(struct branch *branch, |
| 1340 | int i, |
| 1341 | const char *refname) |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 1342 | { |
Shawn O. Pearce | 85682c1 | 2007-09-18 04:54:53 -0400 | [diff] [blame] | 1343 | if (!branch || i < 0 || i >= branch->merge_nr) |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 1344 | return 0; |
Steffen Prohaska | 605b497 | 2007-11-11 15:01:48 +0100 | [diff] [blame] | 1345 | return refname_match(branch->merge[i]->src, refname, ref_fetch_rules); |
Daniel Barkalow | cf81834 | 2007-09-10 23:02:56 -0400 | [diff] [blame] | 1346 | } |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1347 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1348 | static struct ref *get_expanded_map(const struct ref *remote_refs, |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1349 | const struct refspec *refspec) |
| 1350 | { |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1351 | const struct ref *ref; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1352 | struct ref *ret = NULL; |
| 1353 | struct ref **tail = &ret; |
| 1354 | |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 1355 | char *expn_name; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1356 | |
| 1357 | for (ref = remote_refs; ref; ref = ref->next) { |
| 1358 | if (strchr(ref->name, '^')) |
| 1359 | continue; /* a dereference item */ |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 1360 | if (match_name_with_pattern(refspec->src, ref->name, |
| 1361 | refspec->dst, &expn_name)) { |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1362 | struct ref *cpy = copy_ref(ref); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1363 | |
Daniel Barkalow | e928213 | 2009-03-07 01:11:34 -0500 | [diff] [blame] | 1364 | cpy->peer_ref = alloc_ref(expn_name); |
| 1365 | free(expn_name); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1366 | if (refspec->force) |
| 1367 | cpy->peer_ref->force = 1; |
| 1368 | *tail = cpy; |
| 1369 | tail = &cpy->next; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | return ret; |
| 1374 | } |
| 1375 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1376 | static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1377 | { |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1378 | const struct ref *ref; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1379 | for (ref = refs; ref; ref = ref->next) { |
Steffen Prohaska | 605b497 | 2007-11-11 15:01:48 +0100 | [diff] [blame] | 1380 | if (refname_match(name, ref->name, ref_fetch_rules)) |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1381 | return ref; |
| 1382 | } |
| 1383 | return NULL; |
| 1384 | } |
| 1385 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1386 | struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1387 | { |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1388 | const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1389 | |
| 1390 | if (!ref) |
Junio C Hamano | 9ad7c5a | 2007-10-26 23:09:48 -0700 | [diff] [blame] | 1391 | return NULL; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1392 | |
| 1393 | return copy_ref(ref); |
| 1394 | } |
| 1395 | |
| 1396 | static struct ref *get_local_ref(const char *name) |
| 1397 | { |
Clemens Buchacher | 3eb9699 | 2009-06-17 15:38:36 +0200 | [diff] [blame] | 1398 | if (!name || name[0] == '\0') |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1399 | return NULL; |
| 1400 | |
René Scharfe | 59c69c0 | 2008-10-18 10:44:18 +0200 | [diff] [blame] | 1401 | if (!prefixcmp(name, "refs/")) |
| 1402 | return alloc_ref(name); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1403 | |
| 1404 | if (!prefixcmp(name, "heads/") || |
| 1405 | !prefixcmp(name, "tags/") || |
René Scharfe | 8009768 | 2008-10-18 10:37:40 +0200 | [diff] [blame] | 1406 | !prefixcmp(name, "remotes/")) |
| 1407 | return alloc_ref_with_prefix("refs/", 5, name); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1408 | |
René Scharfe | 8009768 | 2008-10-18 10:37:40 +0200 | [diff] [blame] | 1409 | return alloc_ref_with_prefix("refs/heads/", 11, name); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 1412 | int get_fetch_map(const struct ref *remote_refs, |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1413 | const struct refspec *refspec, |
Junio C Hamano | 9ad7c5a | 2007-10-26 23:09:48 -0700 | [diff] [blame] | 1414 | struct ref ***tail, |
| 1415 | int missing_ok) |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1416 | { |
Daniel Barkalow | ef00d15 | 2008-03-17 22:05:23 -0400 | [diff] [blame] | 1417 | struct ref *ref_map, **rmp; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1418 | |
| 1419 | if (refspec->pattern) { |
| 1420 | ref_map = get_expanded_map(remote_refs, refspec); |
| 1421 | } else { |
Junio C Hamano | 9ad7c5a | 2007-10-26 23:09:48 -0700 | [diff] [blame] | 1422 | const char *name = refspec->src[0] ? refspec->src : "HEAD"; |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1423 | |
Junio C Hamano | 9ad7c5a | 2007-10-26 23:09:48 -0700 | [diff] [blame] | 1424 | ref_map = get_remote_ref(remote_refs, name); |
| 1425 | if (!missing_ok && !ref_map) |
| 1426 | die("Couldn't find remote ref %s", name); |
| 1427 | if (ref_map) { |
| 1428 | ref_map->peer_ref = get_local_ref(refspec->dst); |
| 1429 | if (ref_map->peer_ref && refspec->force) |
| 1430 | ref_map->peer_ref->force = 1; |
| 1431 | } |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1432 | } |
| 1433 | |
Daniel Barkalow | ef00d15 | 2008-03-17 22:05:23 -0400 | [diff] [blame] | 1434 | for (rmp = &ref_map; *rmp; ) { |
| 1435 | if ((*rmp)->peer_ref) { |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 1436 | if (check_refname_format((*rmp)->peer_ref->name + 5, |
| 1437 | REFNAME_ALLOW_ONELEVEL)) { |
Daniel Barkalow | ef00d15 | 2008-03-17 22:05:23 -0400 | [diff] [blame] | 1438 | struct ref *ignore = *rmp; |
| 1439 | error("* Ignoring funny ref '%s' locally", |
| 1440 | (*rmp)->peer_ref->name); |
| 1441 | *rmp = (*rmp)->next; |
| 1442 | free(ignore->peer_ref); |
| 1443 | free(ignore); |
| 1444 | continue; |
| 1445 | } |
| 1446 | } |
| 1447 | rmp = &((*rmp)->next); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1448 | } |
| 1449 | |
Alex Riesen | 8f70a76 | 2007-10-12 22:40:04 +0200 | [diff] [blame] | 1450 | if (ref_map) |
| 1451 | tail_link_ref(ref_map, tail); |
Daniel Barkalow | d71ab17 | 2007-09-10 23:03:08 -0400 | [diff] [blame] | 1452 | |
| 1453 | return 0; |
| 1454 | } |
Daniel Barkalow | be885d9 | 2008-04-26 15:53:12 -0400 | [diff] [blame] | 1455 | |
| 1456 | int resolve_remote_symref(struct ref *ref, struct ref *list) |
| 1457 | { |
| 1458 | if (!ref->symref) |
| 1459 | return 0; |
| 1460 | for (; list; list = list->next) |
| 1461 | if (!strcmp(ref->symref, list->name)) { |
| 1462 | hashcpy(ref->old_sha1, list->old_sha1); |
| 1463 | return 0; |
| 1464 | } |
| 1465 | return 1; |
| 1466 | } |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1467 | |
Jay Soffian | ec8452d | 2009-02-25 03:32:12 -0500 | [diff] [blame] | 1468 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
| 1469 | { |
| 1470 | while (list) { |
| 1471 | struct commit_list *temp = list; |
| 1472 | temp->item->object.flags &= ~mark; |
| 1473 | list = temp->next; |
| 1474 | free(temp); |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1) |
| 1479 | { |
| 1480 | struct object *o; |
| 1481 | struct commit *old, *new; |
| 1482 | struct commit_list *list, *used; |
| 1483 | int found = 0; |
| 1484 | |
| 1485 | /* Both new and old must be commit-ish and new is descendant of |
| 1486 | * old. Otherwise we require --force. |
| 1487 | */ |
| 1488 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
| 1489 | if (!o || o->type != OBJ_COMMIT) |
| 1490 | return 0; |
| 1491 | old = (struct commit *) o; |
| 1492 | |
| 1493 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
| 1494 | if (!o || o->type != OBJ_COMMIT) |
| 1495 | return 0; |
| 1496 | new = (struct commit *) o; |
| 1497 | |
| 1498 | if (parse_commit(new) < 0) |
| 1499 | return 0; |
| 1500 | |
| 1501 | used = list = NULL; |
| 1502 | commit_list_insert(new, &list); |
| 1503 | while (list) { |
| 1504 | new = pop_most_recent_commit(&list, TMP_MARK); |
| 1505 | commit_list_insert(new, &used); |
| 1506 | if (new == old) { |
| 1507 | found = 1; |
| 1508 | break; |
| 1509 | } |
| 1510 | } |
| 1511 | unmark_and_free(list, TMP_MARK); |
| 1512 | unmark_and_free(used, TMP_MARK); |
| 1513 | return found; |
| 1514 | } |
| 1515 | |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1516 | /* |
| 1517 | * Return true if there is anything to report, otherwise false. |
| 1518 | */ |
| 1519 | int stat_tracking_info(struct branch *branch, int *num_ours, int *num_theirs) |
| 1520 | { |
| 1521 | unsigned char sha1[20]; |
| 1522 | struct commit *ours, *theirs; |
| 1523 | char symmetric[84]; |
| 1524 | struct rev_info revs; |
| 1525 | const char *rev_argv[10], *base; |
| 1526 | int rev_argc; |
| 1527 | |
| 1528 | /* |
| 1529 | * Nothing to report unless we are marked to build on top of |
| 1530 | * somebody else. |
| 1531 | */ |
| 1532 | if (!branch || |
| 1533 | !branch->merge || !branch->merge[0] || !branch->merge[0]->dst) |
| 1534 | return 0; |
| 1535 | |
| 1536 | /* |
| 1537 | * If what we used to build on no longer exists, there is |
| 1538 | * nothing to report. |
| 1539 | */ |
| 1540 | base = branch->merge[0]->dst; |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 1541 | if (read_ref(base, sha1)) |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1542 | return 0; |
Michael J Gruber | 57ffc5f | 2009-05-11 16:42:54 +0200 | [diff] [blame] | 1543 | theirs = lookup_commit_reference(sha1); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1544 | if (!theirs) |
| 1545 | return 0; |
| 1546 | |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 1547 | if (read_ref(branch->refname, sha1)) |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1548 | return 0; |
Michael J Gruber | 57ffc5f | 2009-05-11 16:42:54 +0200 | [diff] [blame] | 1549 | ours = lookup_commit_reference(sha1); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1550 | if (!ours) |
| 1551 | return 0; |
| 1552 | |
| 1553 | /* are we the same? */ |
| 1554 | if (theirs == ours) |
| 1555 | return 0; |
| 1556 | |
Junio C Hamano | 8fbf879 | 2009-04-21 16:32:18 -0700 | [diff] [blame] | 1557 | /* Run "rev-list --left-right ours...theirs" internally... */ |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1558 | rev_argc = 0; |
| 1559 | rev_argv[rev_argc++] = NULL; |
| 1560 | rev_argv[rev_argc++] = "--left-right"; |
| 1561 | rev_argv[rev_argc++] = symmetric; |
| 1562 | rev_argv[rev_argc++] = "--"; |
| 1563 | rev_argv[rev_argc] = NULL; |
| 1564 | |
| 1565 | strcpy(symmetric, sha1_to_hex(ours->object.sha1)); |
| 1566 | strcpy(symmetric + 40, "..."); |
| 1567 | strcpy(symmetric + 43, sha1_to_hex(theirs->object.sha1)); |
| 1568 | |
| 1569 | init_revisions(&revs, NULL); |
| 1570 | setup_revisions(rev_argc, rev_argv, &revs, NULL); |
| 1571 | prepare_revision_walk(&revs); |
| 1572 | |
| 1573 | /* ... and count the commits on each side. */ |
| 1574 | *num_ours = 0; |
| 1575 | *num_theirs = 0; |
| 1576 | while (1) { |
| 1577 | struct commit *c = get_revision(&revs); |
| 1578 | if (!c) |
| 1579 | break; |
| 1580 | if (c->object.flags & SYMMETRIC_LEFT) |
| 1581 | (*num_ours)++; |
| 1582 | else |
| 1583 | (*num_theirs)++; |
| 1584 | } |
Junio C Hamano | c0234b2 | 2008-07-03 12:09:48 -0700 | [diff] [blame] | 1585 | |
| 1586 | /* clear object flags smudged by the above traversal */ |
| 1587 | clear_commit_marks(ours, ALL_REV_FLAGS); |
| 1588 | clear_commit_marks(theirs, ALL_REV_FLAGS); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1589 | return 1; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * Return true when there is anything to report, otherwise false. |
| 1594 | */ |
| 1595 | int format_tracking_info(struct branch *branch, struct strbuf *sb) |
| 1596 | { |
| 1597 | int num_ours, num_theirs; |
Avery Pennarun | 4de53ce | 2008-07-16 15:19:27 -0400 | [diff] [blame] | 1598 | const char *base; |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1599 | |
| 1600 | if (!stat_tracking_info(branch, &num_ours, &num_theirs)) |
| 1601 | return 0; |
| 1602 | |
| 1603 | base = branch->merge[0]->dst; |
Michael J Gruber | 45972ff | 2009-04-16 10:20:44 +0200 | [diff] [blame] | 1604 | base = shorten_unambiguous_ref(base, 0); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1605 | if (!num_theirs) |
Jiang Xin | 8a5b749 | 2012-02-02 10:02:23 +0800 | [diff] [blame] | 1606 | strbuf_addf(sb, |
| 1607 | Q_("Your branch is ahead of '%s' by %d commit.\n", |
| 1608 | "Your branch is ahead of '%s' by %d commits.\n", |
| 1609 | num_ours), |
| 1610 | base, num_ours); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1611 | else if (!num_ours) |
Jiang Xin | 8a5b749 | 2012-02-02 10:02:23 +0800 | [diff] [blame] | 1612 | strbuf_addf(sb, |
| 1613 | Q_("Your branch is behind '%s' by %d commit, " |
| 1614 | "and can be fast-forwarded.\n", |
| 1615 | "Your branch is behind '%s' by %d commits, " |
| 1616 | "and can be fast-forwarded.\n", |
| 1617 | num_theirs), |
| 1618 | base, num_theirs); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1619 | else |
Jiang Xin | 8a5b749 | 2012-02-02 10:02:23 +0800 | [diff] [blame] | 1620 | strbuf_addf(sb, |
| 1621 | Q_("Your branch and '%s' have diverged,\n" |
| 1622 | "and have %d and %d different commit each, " |
| 1623 | "respectively.\n", |
| 1624 | "Your branch and '%s' have diverged,\n" |
| 1625 | "and have %d and %d different commits each, " |
| 1626 | "respectively.\n", |
| 1627 | num_theirs), |
| 1628 | base, num_ours, num_theirs); |
Junio C Hamano | 6d21bf9 | 2008-07-02 00:51:18 -0700 | [diff] [blame] | 1629 | return 1; |
| 1630 | } |
Jay Soffian | 454e202 | 2009-02-25 03:32:11 -0500 | [diff] [blame] | 1631 | |
| 1632 | static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
| 1633 | { |
| 1634 | struct ref ***local_tail = cb_data; |
| 1635 | struct ref *ref; |
| 1636 | int len; |
| 1637 | |
| 1638 | /* we already know it starts with refs/ to get here */ |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 1639 | if (check_refname_format(refname + 5, 0)) |
Jay Soffian | 454e202 | 2009-02-25 03:32:11 -0500 | [diff] [blame] | 1640 | return 0; |
| 1641 | |
| 1642 | len = strlen(refname) + 1; |
| 1643 | ref = xcalloc(1, sizeof(*ref) + len); |
| 1644 | hashcpy(ref->new_sha1, sha1); |
| 1645 | memcpy(ref->name, refname, len); |
| 1646 | **local_tail = ref; |
| 1647 | *local_tail = &ref->next; |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
| 1651 | struct ref *get_local_heads(void) |
| 1652 | { |
Nguyễn Thái Ngọc Duy | 55f0566 | 2009-04-17 08:16:23 +1000 | [diff] [blame] | 1653 | struct ref *local_refs = NULL, **local_tail = &local_refs; |
Jay Soffian | 454e202 | 2009-02-25 03:32:11 -0500 | [diff] [blame] | 1654 | for_each_ref(one_local_ref, &local_tail); |
| 1655 | return local_refs; |
| 1656 | } |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1657 | |
Jay Soffian | 4229f1f | 2009-02-27 14:10:05 -0500 | [diff] [blame] | 1658 | struct ref *guess_remote_head(const struct ref *head, |
| 1659 | const struct ref *refs, |
| 1660 | int all) |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1661 | { |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1662 | const struct ref *r; |
Jay Soffian | 4229f1f | 2009-02-27 14:10:05 -0500 | [diff] [blame] | 1663 | struct ref *list = NULL; |
| 1664 | struct ref **tail = &list; |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1665 | |
Jay Soffian | 6cb4e6c | 2009-02-25 03:32:14 -0500 | [diff] [blame] | 1666 | if (!head) |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1667 | return NULL; |
| 1668 | |
Jeff King | fbb074c | 2009-02-27 14:10:06 -0500 | [diff] [blame] | 1669 | /* |
| 1670 | * Some transports support directly peeking at |
| 1671 | * where HEAD points; if that is the case, then |
| 1672 | * we don't have to guess. |
| 1673 | */ |
| 1674 | if (head->symref) |
| 1675 | return copy_ref(find_ref_by_name(refs, head->symref)); |
| 1676 | |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1677 | /* If refs/heads/master could be right, it is. */ |
Jay Soffian | 4229f1f | 2009-02-27 14:10:05 -0500 | [diff] [blame] | 1678 | if (!all) { |
| 1679 | r = find_ref_by_name(refs, "refs/heads/master"); |
| 1680 | if (r && !hashcmp(r->old_sha1, head->old_sha1)) |
| 1681 | return copy_ref(r); |
| 1682 | } |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1683 | |
| 1684 | /* Look for another ref that points there */ |
Jay Soffian | 4229f1f | 2009-02-27 14:10:05 -0500 | [diff] [blame] | 1685 | for (r = refs; r; r = r->next) { |
Jeff King | 61adfd3 | 2011-06-03 01:11:13 -0400 | [diff] [blame] | 1686 | if (r != head && |
| 1687 | !prefixcmp(r->name, "refs/heads/") && |
| 1688 | !hashcmp(r->old_sha1, head->old_sha1)) { |
Jay Soffian | 4229f1f | 2009-02-27 14:10:05 -0500 | [diff] [blame] | 1689 | *tail = copy_ref(r); |
| 1690 | tail = &((*tail)->next); |
| 1691 | if (!all) |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1695 | |
Jay Soffian | 4229f1f | 2009-02-27 14:10:05 -0500 | [diff] [blame] | 1696 | return list; |
Jay Soffian | 8ef5173 | 2009-02-25 03:32:13 -0500 | [diff] [blame] | 1697 | } |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1698 | |
| 1699 | struct stale_heads_info { |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1700 | struct string_list *ref_names; |
| 1701 | struct ref **stale_refs_tail; |
Carlos Martín Nieto | ed43de6 | 2011-10-15 07:04:25 +0200 | [diff] [blame] | 1702 | struct refspec *refs; |
| 1703 | int ref_count; |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1704 | }; |
| 1705 | |
| 1706 | static int get_stale_heads_cb(const char *refname, |
| 1707 | const unsigned char *sha1, int flags, void *cb_data) |
| 1708 | { |
| 1709 | struct stale_heads_info *info = cb_data; |
Carlos Martín Nieto | ed43de6 | 2011-10-15 07:04:25 +0200 | [diff] [blame] | 1710 | struct refspec query; |
| 1711 | memset(&query, 0, sizeof(struct refspec)); |
| 1712 | query.dst = (char *)refname; |
| 1713 | |
| 1714 | if (query_refspecs(info->refs, info->ref_count, &query)) |
| 1715 | return 0; /* No matches */ |
| 1716 | |
| 1717 | /* |
| 1718 | * If we did find a suitable refspec and it's not a symref and |
| 1719 | * it's not in the list of refs that currently exist in that |
| 1720 | * remote we consider it to be stale. |
| 1721 | */ |
| 1722 | if (!((flags & REF_ISSYMREF) || |
| 1723 | string_list_has_string(info->ref_names, query.src))) { |
| 1724 | struct ref *ref = make_linked_ref(refname, &info->stale_refs_tail); |
| 1725 | hashcpy(ref->new_sha1, sha1); |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1726 | } |
Carlos Martín Nieto | ed43de6 | 2011-10-15 07:04:25 +0200 | [diff] [blame] | 1727 | |
| 1728 | free(query.src); |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
| 1731 | |
Carlos Martín Nieto | ed43de6 | 2011-10-15 07:04:25 +0200 | [diff] [blame] | 1732 | struct ref *get_stale_heads(struct refspec *refs, int ref_count, struct ref *fetch_map) |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1733 | { |
| 1734 | struct ref *ref, *stale_refs = NULL; |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 1735 | struct string_list ref_names = STRING_LIST_INIT_NODUP; |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1736 | struct stale_heads_info info; |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1737 | info.ref_names = &ref_names; |
| 1738 | info.stale_refs_tail = &stale_refs; |
Carlos Martín Nieto | ed43de6 | 2011-10-15 07:04:25 +0200 | [diff] [blame] | 1739 | info.refs = refs; |
| 1740 | info.ref_count = ref_count; |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1741 | for (ref = fetch_map; ref; ref = ref->next) |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 1742 | string_list_append(&ref_names, ref->name); |
Jay Soffian | f2ef607 | 2009-11-10 00:03:31 -0500 | [diff] [blame] | 1743 | sort_string_list(&ref_names); |
| 1744 | for_each_ref(get_stale_heads_cb, &info); |
| 1745 | string_list_clear(&ref_names, 0); |
| 1746 | return stale_refs; |
| 1747 | } |