Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 2 | #include "tag.h" |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 3 | #include "commit.h" |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 4 | #include "tree.h" |
| 5 | #include "blob.h" |
Junio C Hamano | f3ab49d | 2006-04-19 11:56:53 -0700 | [diff] [blame] | 6 | #include "tree-walk.h" |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 7 | #include "refs.h" |
Johannes Schindelin | 28fb843 | 2009-09-10 17:25:57 +0200 | [diff] [blame] | 8 | #include "remote.h" |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 9 | #include "dir.h" |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 10 | #include "sha1-array.h" |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 11 | |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 12 | static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *); |
| 13 | |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 14 | typedef int (*disambiguate_hint_fn)(const unsigned char *, void *); |
| 15 | |
| 16 | struct disambiguate_state { |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 17 | int len; /* length of prefix in hex chars */ |
Jeff King | 59e4e34 | 2016-09-26 08:00:07 -0400 | [diff] [blame] | 18 | char hex_pfx[GIT_SHA1_HEXSZ + 1]; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 19 | unsigned char bin_pfx[GIT_SHA1_RAWSZ]; |
| 20 | |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 21 | disambiguate_hint_fn fn; |
| 22 | void *cb_data; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 23 | unsigned char candidate[GIT_SHA1_RAWSZ]; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 24 | unsigned candidate_exists:1; |
| 25 | unsigned candidate_checked:1; |
| 26 | unsigned candidate_ok:1; |
| 27 | unsigned disambiguate_fn_used:1; |
| 28 | unsigned ambiguous:1; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 29 | unsigned always_call_fn:1; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static void update_candidates(struct disambiguate_state *ds, const unsigned char *current) |
| 33 | { |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 34 | if (ds->always_call_fn) { |
| 35 | ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0; |
| 36 | return; |
| 37 | } |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 38 | if (!ds->candidate_exists) { |
| 39 | /* this is the first candidate */ |
| 40 | hashcpy(ds->candidate, current); |
| 41 | ds->candidate_exists = 1; |
| 42 | return; |
| 43 | } else if (!hashcmp(ds->candidate, current)) { |
| 44 | /* the same as what we already have seen */ |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (!ds->fn) { |
| 49 | /* cannot disambiguate between ds->candidate and current */ |
| 50 | ds->ambiguous = 1; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (!ds->candidate_checked) { |
| 55 | ds->candidate_ok = ds->fn(ds->candidate, ds->cb_data); |
| 56 | ds->disambiguate_fn_used = 1; |
| 57 | ds->candidate_checked = 1; |
| 58 | } |
| 59 | |
| 60 | if (!ds->candidate_ok) { |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 61 | /* discard the candidate; we know it does not satisfy fn */ |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 62 | hashcpy(ds->candidate, current); |
| 63 | ds->candidate_checked = 0; |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | /* if we reach this point, we know ds->candidate satisfies fn */ |
| 68 | if (ds->fn(current, ds->cb_data)) { |
| 69 | /* |
| 70 | * if both current and candidate satisfy fn, we cannot |
| 71 | * disambiguate. |
| 72 | */ |
| 73 | ds->candidate_ok = 0; |
| 74 | ds->ambiguous = 1; |
| 75 | } |
| 76 | |
| 77 | /* otherwise, current can be discarded and candidate is still good */ |
| 78 | } |
| 79 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 80 | static void find_short_object_filename(struct disambiguate_state *ds) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 81 | { |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 82 | struct alternate_object_database *alt; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 83 | char hex[GIT_SHA1_HEXSZ]; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 84 | static struct alternate_object_database *fakeent; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 85 | |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 86 | if (!fakeent) { |
Junio C Hamano | 274ac00 | 2012-06-18 11:41:03 -0700 | [diff] [blame] | 87 | /* |
| 88 | * Create a "fake" alternate object database that |
| 89 | * points to our own object database, to make it |
| 90 | * easier to get a temporary working space in |
| 91 | * alt->name/alt->base while iterating over the |
| 92 | * object databases including our own. |
| 93 | */ |
Jeff King | 7f0fa2c | 2016-10-03 16:35:31 -0400 | [diff] [blame] | 94 | fakeent = alloc_alt_odb(get_object_directory()); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 95 | } |
| 96 | fakeent->next = alt_odb_list; |
| 97 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 98 | xsnprintf(hex, sizeof(hex), "%.2s", ds->hex_pfx); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 99 | for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) { |
Jeff King | 38dbe5f | 2016-10-03 16:36:04 -0400 | [diff] [blame] | 100 | struct strbuf *buf = alt_scratch_buf(alt); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 101 | struct dirent *de; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 102 | DIR *dir; |
Jeff King | 597f913 | 2016-10-03 16:35:51 -0400 | [diff] [blame] | 103 | |
Junio C Hamano | dec0401 | 2016-10-17 13:25:19 -0700 | [diff] [blame] | 104 | strbuf_addf(buf, "%.2s/", ds->hex_pfx); |
Jeff King | 38dbe5f | 2016-10-03 16:36:04 -0400 | [diff] [blame] | 105 | dir = opendir(buf->buf); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 106 | if (!dir) |
| 107 | continue; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 108 | |
| 109 | while (!ds->ambiguous && (de = readdir(dir)) != NULL) { |
| 110 | unsigned char sha1[20]; |
| 111 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 112 | if (strlen(de->d_name) != 38) |
| 113 | continue; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 114 | if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2)) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 115 | continue; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 116 | memcpy(hex + 2, de->d_name, 38); |
| 117 | if (!get_sha1_hex(hex, sha1)) |
| 118 | update_candidates(ds, sha1); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 119 | } |
| 120 | closedir(dir); |
| 121 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b) |
| 125 | { |
| 126 | do { |
| 127 | if (*a != *b) |
| 128 | return 0; |
| 129 | a++; |
| 130 | b++; |
| 131 | len -= 2; |
| 132 | } while (len > 1); |
| 133 | if (len) |
| 134 | if ((*a ^ *b) & 0xf0) |
| 135 | return 0; |
| 136 | return 1; |
| 137 | } |
| 138 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 139 | static void unique_in_pack(struct packed_git *p, |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 140 | struct disambiguate_state *ds) |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 141 | { |
| 142 | uint32_t num, last, i, first = 0; |
| 143 | const unsigned char *current = NULL; |
| 144 | |
| 145 | open_pack_index(p); |
| 146 | num = p->num_objects; |
| 147 | last = num; |
| 148 | while (first < last) { |
| 149 | uint32_t mid = (first + last) / 2; |
| 150 | const unsigned char *current; |
| 151 | int cmp; |
| 152 | |
| 153 | current = nth_packed_object_sha1(p, mid); |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 154 | cmp = hashcmp(ds->bin_pfx, current); |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 155 | if (!cmp) { |
| 156 | first = mid; |
| 157 | break; |
| 158 | } |
| 159 | if (cmp > 0) { |
| 160 | first = mid+1; |
| 161 | continue; |
| 162 | } |
| 163 | last = mid; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * At this point, "first" is the location of the lowest object |
Junio C Hamano | 1703f9a | 2012-06-20 22:35:43 -0700 | [diff] [blame] | 168 | * with an object name that could match "bin_pfx". See if we have |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 169 | * 0, 1 or more objects that actually match(es). |
| 170 | */ |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 171 | for (i = first; i < num && !ds->ambiguous; i++) { |
| 172 | current = nth_packed_object_sha1(p, i); |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 173 | if (!match_sha(ds->len, ds->bin_pfx, current)) |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 174 | break; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 175 | update_candidates(ds, current); |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 176 | } |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 179 | static void find_short_packed_object(struct disambiguate_state *ds) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 180 | { |
| 181 | struct packed_git *p; |
| 182 | |
| 183 | prepare_packed_git(); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 184 | for (p = packed_git; p && !ds->ambiguous; p = p->next) |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 185 | unique_in_pack(p, ds); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 188 | #define SHORT_NAME_NOT_FOUND (-1) |
| 189 | #define SHORT_NAME_AMBIGUOUS (-2) |
| 190 | |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 191 | static int finish_object_disambiguation(struct disambiguate_state *ds, |
| 192 | unsigned char *sha1) |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 193 | { |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 194 | if (ds->ambiguous) |
| 195 | return SHORT_NAME_AMBIGUOUS; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 196 | |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 197 | if (!ds->candidate_exists) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 198 | return SHORT_NAME_NOT_FOUND; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (!ds->candidate_checked) |
| 201 | /* |
| 202 | * If this is the only candidate, there is no point |
| 203 | * calling the disambiguation hint callback. |
| 204 | * |
| 205 | * On the other hand, if the current candidate |
| 206 | * replaced an earlier candidate that did _not_ pass |
| 207 | * the disambiguation hint callback, then we do have |
| 208 | * more than one objects that match the short name |
| 209 | * given, so we should make sure this one matches; |
| 210 | * otherwise, if we discovered this one and the one |
| 211 | * that we previously discarded in the reverse order, |
| 212 | * we would end up showing different results in the |
| 213 | * same repository! |
| 214 | */ |
| 215 | ds->candidate_ok = (!ds->disambiguate_fn_used || |
| 216 | ds->fn(ds->candidate, ds->cb_data)); |
| 217 | |
| 218 | if (!ds->candidate_ok) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 219 | return SHORT_NAME_AMBIGUOUS; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 220 | |
| 221 | hashcpy(sha1, ds->candidate); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 225 | static int disambiguate_commit_only(const unsigned char *sha1, void *cb_data_unused) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 226 | { |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 227 | int kind = sha1_object_info(sha1, NULL); |
| 228 | return kind == OBJ_COMMIT; |
| 229 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 230 | |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 231 | static int disambiguate_committish_only(const unsigned char *sha1, void *cb_data_unused) |
| 232 | { |
| 233 | struct object *obj; |
| 234 | int kind; |
| 235 | |
| 236 | kind = sha1_object_info(sha1, NULL); |
| 237 | if (kind == OBJ_COMMIT) |
| 238 | return 1; |
| 239 | if (kind != OBJ_TAG) |
| 240 | return 0; |
| 241 | |
| 242 | /* We need to do this the hard way... */ |
Junio C Hamano | 94d75d1 | 2013-07-01 21:54:45 -0700 | [diff] [blame] | 243 | obj = deref_tag(parse_object(sha1), NULL, 0); |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 244 | if (obj && obj->type == OBJ_COMMIT) |
| 245 | return 1; |
| 246 | return 0; |
| 247 | } |
| 248 | |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 249 | static int disambiguate_tree_only(const unsigned char *sha1, void *cb_data_unused) |
| 250 | { |
| 251 | int kind = sha1_object_info(sha1, NULL); |
| 252 | return kind == OBJ_TREE; |
| 253 | } |
| 254 | |
| 255 | static int disambiguate_treeish_only(const unsigned char *sha1, void *cb_data_unused) |
| 256 | { |
| 257 | struct object *obj; |
| 258 | int kind; |
| 259 | |
| 260 | kind = sha1_object_info(sha1, NULL); |
| 261 | if (kind == OBJ_TREE || kind == OBJ_COMMIT) |
| 262 | return 1; |
| 263 | if (kind != OBJ_TAG) |
| 264 | return 0; |
| 265 | |
| 266 | /* We need to do this the hard way... */ |
Jeff King | 5d5def2 | 2016-09-26 07:59:48 -0400 | [diff] [blame] | 267 | obj = deref_tag(parse_object(sha1), NULL, 0); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 268 | if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT)) |
| 269 | return 1; |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int disambiguate_blob_only(const unsigned char *sha1, void *cb_data_unused) |
| 274 | { |
| 275 | int kind = sha1_object_info(sha1, NULL); |
| 276 | return kind == OBJ_BLOB; |
| 277 | } |
| 278 | |
Jeff King | 5b33cb1 | 2016-09-27 08:38:01 -0400 | [diff] [blame] | 279 | static disambiguate_hint_fn default_disambiguate_hint; |
| 280 | |
| 281 | int set_disambiguate_hint_config(const char *var, const char *value) |
| 282 | { |
| 283 | static const struct { |
| 284 | const char *name; |
| 285 | disambiguate_hint_fn fn; |
| 286 | } hints[] = { |
| 287 | { "none", NULL }, |
| 288 | { "commit", disambiguate_commit_only }, |
| 289 | { "committish", disambiguate_committish_only }, |
| 290 | { "tree", disambiguate_tree_only }, |
| 291 | { "treeish", disambiguate_treeish_only }, |
| 292 | { "blob", disambiguate_blob_only } |
| 293 | }; |
| 294 | int i; |
| 295 | |
| 296 | if (!value) |
| 297 | return config_error_nonbool(var); |
| 298 | |
| 299 | for (i = 0; i < ARRAY_SIZE(hints); i++) { |
| 300 | if (!strcasecmp(value, hints[i].name)) { |
| 301 | default_disambiguate_hint = hints[i].fn; |
| 302 | return 0; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | return error("unknown hint type for '%s': %s", var, value); |
| 307 | } |
| 308 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 309 | static int init_object_disambiguation(const char *name, int len, |
| 310 | struct disambiguate_state *ds) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 311 | { |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 312 | int i; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 313 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 314 | if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ) |
| 315 | return -1; |
| 316 | |
| 317 | memset(ds, 0, sizeof(*ds)); |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 318 | |
Linus Torvalds | af61c6e | 2005-09-19 15:16:03 -0700 | [diff] [blame] | 319 | for (i = 0; i < len ;i++) { |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 320 | unsigned char c = name[i]; |
| 321 | unsigned char val; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 322 | if (c >= '0' && c <= '9') |
| 323 | val = c - '0'; |
| 324 | else if (c >= 'a' && c <= 'f') |
| 325 | val = c - 'a' + 10; |
| 326 | else if (c >= 'A' && c <='F') { |
| 327 | val = c - 'A' + 10; |
| 328 | c -= 'A' - 'a'; |
| 329 | } |
| 330 | else |
| 331 | return -1; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 332 | ds->hex_pfx[i] = c; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 333 | if (!(i & 1)) |
| 334 | val <<= 4; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 335 | ds->bin_pfx[i >> 1] |= val; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 336 | } |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 337 | |
| 338 | ds->len = len; |
Jeff King | 59e4e34 | 2016-09-26 08:00:07 -0400 | [diff] [blame] | 339 | ds->hex_pfx[len] = '\0'; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 340 | prepare_alt_odb(); |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 341 | return 0; |
| 342 | } |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 343 | |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 344 | static int show_ambiguous_object(const unsigned char *sha1, void *data) |
| 345 | { |
| 346 | const struct disambiguate_state *ds = data; |
| 347 | struct strbuf desc = STRBUF_INIT; |
| 348 | int type; |
| 349 | |
| 350 | if (ds->fn && !ds->fn(sha1, ds->cb_data)) |
| 351 | return 0; |
| 352 | |
| 353 | type = sha1_object_info(sha1, NULL); |
| 354 | if (type == OBJ_COMMIT) { |
| 355 | struct commit *commit = lookup_commit(sha1); |
| 356 | if (commit) { |
| 357 | struct pretty_print_context pp = {0}; |
| 358 | pp.date_mode.type = DATE_SHORT; |
| 359 | format_commit_message(commit, " %ad - %s", &desc, &pp); |
| 360 | } |
| 361 | } else if (type == OBJ_TAG) { |
| 362 | struct tag *tag = lookup_tag(sha1); |
| 363 | if (!parse_tag(tag) && tag->tag) |
| 364 | strbuf_addf(&desc, " %s", tag->tag); |
| 365 | } |
| 366 | |
| 367 | advise(" %s %s%s", |
| 368 | find_unique_abbrev(sha1, DEFAULT_ABBREV), |
| 369 | typename(type) ? typename(type) : "unknown type", |
| 370 | desc.buf); |
| 371 | |
| 372 | strbuf_release(&desc); |
| 373 | return 0; |
| 374 | } |
| 375 | |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 376 | static int get_short_sha1(const char *name, int len, unsigned char *sha1, |
| 377 | unsigned flags) |
| 378 | { |
| 379 | int status; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 380 | struct disambiguate_state ds; |
| 381 | int quietly = !!(flags & GET_SHA1_QUIETLY); |
| 382 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 383 | if (init_object_disambiguation(name, len, &ds) < 0) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 384 | return -1; |
Jeff King | 259942f | 2016-09-26 07:59:01 -0400 | [diff] [blame] | 385 | |
| 386 | if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS)) |
| 387 | die("BUG: multiple get_short_sha1 disambiguator flags"); |
| 388 | |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 389 | if (flags & GET_SHA1_COMMIT) |
| 390 | ds.fn = disambiguate_commit_only; |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 391 | else if (flags & GET_SHA1_COMMITTISH) |
| 392 | ds.fn = disambiguate_committish_only; |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 393 | else if (flags & GET_SHA1_TREE) |
| 394 | ds.fn = disambiguate_tree_only; |
| 395 | else if (flags & GET_SHA1_TREEISH) |
| 396 | ds.fn = disambiguate_treeish_only; |
| 397 | else if (flags & GET_SHA1_BLOB) |
| 398 | ds.fn = disambiguate_blob_only; |
Jeff King | 5b33cb1 | 2016-09-27 08:38:01 -0400 | [diff] [blame] | 399 | else |
| 400 | ds.fn = default_disambiguate_hint; |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 401 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 402 | find_short_object_filename(&ds); |
| 403 | find_short_packed_object(&ds); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 404 | status = finish_object_disambiguation(&ds, sha1); |
| 405 | |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 406 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) { |
| 407 | error(_("short SHA1 %s is ambiguous"), ds.hex_pfx); |
| 408 | |
| 409 | /* |
| 410 | * We may still have ambiguity if we simply saw a series of |
| 411 | * candidates that did not satisfy our hint function. In |
| 412 | * that case, we still want to show them, so disable the hint |
| 413 | * function entirely. |
| 414 | */ |
| 415 | if (!ds.ambiguous) |
| 416 | ds.fn = NULL; |
| 417 | |
| 418 | advise(_("The candidates are:")); |
| 419 | for_each_abbrev(ds.hex_pfx, show_ambiguous_object, &ds); |
| 420 | } |
| 421 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 422 | return status; |
| 423 | } |
| 424 | |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 425 | static int collect_ambiguous(const unsigned char *sha1, void *data) |
| 426 | { |
| 427 | sha1_array_append(data, sha1); |
| 428 | return 0; |
| 429 | } |
| 430 | |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 431 | int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data) |
| 432 | { |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 433 | struct sha1_array collect = SHA1_ARRAY_INIT; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 434 | struct disambiguate_state ds; |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 435 | int ret; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 436 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 437 | if (init_object_disambiguation(prefix, strlen(prefix), &ds) < 0) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 438 | return -1; |
| 439 | |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 440 | ds.always_call_fn = 1; |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 441 | ds.fn = collect_ambiguous; |
| 442 | ds.cb_data = &collect; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 443 | find_short_object_filename(&ds); |
| 444 | find_short_packed_object(&ds); |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 445 | |
| 446 | ret = sha1_array_for_each_unique(&collect, fn, cb_data); |
| 447 | sha1_array_clear(&collect); |
| 448 | return ret; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 451 | /* |
| 452 | * Return the slot of the most-significant bit set in "val". There are various |
| 453 | * ways to do this quickly with fls() or __builtin_clzl(), but speed is |
| 454 | * probably not a big deal here. |
| 455 | */ |
| 456 | static unsigned msb(unsigned long val) |
| 457 | { |
| 458 | unsigned r = 0; |
| 459 | while (val >>= 1) |
| 460 | r++; |
| 461 | return r; |
| 462 | } |
| 463 | |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 464 | int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 465 | { |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 466 | int status, exists; |
Junio C Hamano | 47dd0d5 | 2005-12-13 17:21:41 -0800 | [diff] [blame] | 467 | |
Linus Torvalds | e6c587c | 2016-09-30 17:19:37 -0700 | [diff] [blame] | 468 | if (len < 0) { |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 469 | unsigned long count = approximate_object_count(); |
| 470 | /* |
| 471 | * Add one because the MSB only tells us the highest bit set, |
| 472 | * not including the value of all the _other_ bits (so "15" |
| 473 | * is only one off of 2^4, but the MSB is the 3rd bit. |
| 474 | */ |
| 475 | len = msb(count) + 1; |
| 476 | /* |
| 477 | * We now know we have on the order of 2^len objects, which |
| 478 | * expects a collision at 2^(len/2). But we also care about hex |
| 479 | * chars, not bits, and there are 4 bits per hex. So all |
| 480 | * together we need to divide by 2; but we also want to round |
| 481 | * odd numbers up, hence adding one before dividing. |
| 482 | */ |
| 483 | len = (len + 1) / 2; |
| 484 | /* |
| 485 | * For very small repos, we stick with our regular fallback. |
| 486 | */ |
| 487 | if (len < FALLBACK_DEFAULT_ABBREV) |
| 488 | len = FALLBACK_DEFAULT_ABBREV; |
Linus Torvalds | e6c587c | 2016-09-30 17:19:37 -0700 | [diff] [blame] | 489 | } |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 490 | |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 491 | sha1_to_hex_r(hex, sha1); |
Junio C Hamano | 02c5cba | 2006-08-09 13:17:04 -0700 | [diff] [blame] | 492 | if (len == 40 || !len) |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 493 | return 40; |
Mike Hommey | 61e704e | 2014-11-26 19:12:47 +0900 | [diff] [blame] | 494 | exists = has_sha1_file(sha1); |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 495 | while (len < 40) { |
| 496 | unsigned char sha1_ret[20]; |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 497 | status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY); |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 498 | if (exists |
| 499 | ? !status |
| 500 | : status == SHORT_NAME_NOT_FOUND) { |
Junio C Hamano | ea2c69e | 2011-03-10 22:41:14 -0800 | [diff] [blame] | 501 | hex[len] = 0; |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 502 | return len; |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 503 | } |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 504 | len++; |
| 505 | } |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 506 | return len; |
| 507 | } |
| 508 | |
| 509 | const char *find_unique_abbrev(const unsigned char *sha1, int len) |
| 510 | { |
Jeff King | ef2ed50 | 2016-10-20 02:19:19 -0400 | [diff] [blame] | 511 | static int bufno; |
| 512 | static char hexbuffer[4][GIT_SHA1_HEXSZ + 1]; |
René Scharfe | 3e98919 | 2016-11-01 09:49:07 +0100 | [diff] [blame] | 513 | char *hex = hexbuffer[bufno]; |
| 514 | bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 515 | find_unique_abbrev_r(hex, sha1, len); |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 516 | return hex; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 519 | static int ambiguous_path(const char *path, int len) |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 520 | { |
| 521 | int slash = 1; |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 522 | int cnt; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 523 | |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 524 | for (cnt = 0; cnt < len; cnt++) { |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 525 | switch (*path++) { |
| 526 | case '\0': |
| 527 | break; |
| 528 | case '/': |
| 529 | if (slash) |
| 530 | break; |
| 531 | slash = 1; |
| 532 | continue; |
| 533 | case '.': |
| 534 | continue; |
| 535 | default: |
| 536 | slash = 0; |
| 537 | continue; |
| 538 | } |
Junio C Hamano | c054d64 | 2005-12-17 00:00:50 -0800 | [diff] [blame] | 539 | break; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 540 | } |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 541 | return slash; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Jeff King | a1ad0eb | 2015-05-21 00:45:39 -0400 | [diff] [blame] | 544 | static inline int at_mark(const char *string, int len, |
| 545 | const char **suffix, int nr) |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 546 | { |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 547 | int i; |
| 548 | |
Jeff King | a1ad0eb | 2015-05-21 00:45:39 -0400 | [diff] [blame] | 549 | for (i = 0; i < nr; i++) { |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 550 | int suffix_len = strlen(suffix[i]); |
| 551 | if (suffix_len <= len |
| 552 | && !memcmp(string, suffix[i], suffix_len)) |
| 553 | return suffix_len; |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 | |
Jeff King | a1ad0eb | 2015-05-21 00:45:39 -0400 | [diff] [blame] | 558 | static inline int upstream_mark(const char *string, int len) |
| 559 | { |
| 560 | const char *suffix[] = { "@{upstream}", "@{u}" }; |
| 561 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); |
| 562 | } |
| 563 | |
Jeff King | adfe5d0 | 2015-05-21 00:45:47 -0400 | [diff] [blame] | 564 | static inline int push_mark(const char *string, int len) |
| 565 | { |
| 566 | const char *suffix[] = { "@{push}" }; |
| 567 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); |
| 568 | } |
| 569 | |
Junio C Hamano | e48ba20 | 2012-07-02 09:46:50 -0700 | [diff] [blame] | 570 | static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags); |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 571 | static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf); |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 572 | |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 573 | static int get_sha1_basic(const char *str, int len, unsigned char *sha1, |
| 574 | unsigned int flags) |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 575 | { |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 576 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 577 | static const char *object_name_msg = N_( |
| 578 | "Git normally never creates a ref that ends with 40 hex characters\n" |
| 579 | "because it will be ignored when you just specify 40-hex. These refs\n" |
| 580 | "may be created by mistake. For example,\n" |
| 581 | "\n" |
| 582 | " git checkout -b $br $(git rev-parse ...)\n" |
| 583 | "\n" |
| 584 | "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n" |
| 585 | "examine these refs and maybe delete them. Turn this message off by\n" |
Thomas Rast | 8dc84fd | 2013-07-31 22:23:31 +0200 | [diff] [blame] | 586 | "running \"git config advice.objectNameWarning false\""); |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 587 | unsigned char tmp_sha1[20]; |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 588 | char *real_ref = NULL; |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 589 | int refs_found = 0; |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 590 | int at, reflog_len, nth_prior = 0; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 591 | |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 592 | if (len == 40 && !get_sha1_hex(str, sha1)) { |
Brodie Rao | 832cf74 | 2014-01-06 19:32:01 -0800 | [diff] [blame] | 593 | if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 594 | refs_found = dwim_ref(str, len, tmp_sha1, &real_ref); |
Brodie Rao | 832cf74 | 2014-01-06 19:32:01 -0800 | [diff] [blame] | 595 | if (refs_found > 0) { |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 596 | warning(warn_msg, len, str); |
| 597 | if (advice_object_name_warning) |
| 598 | fprintf(stderr, "%s\n", _(object_name_msg)); |
| 599 | } |
| 600 | free(real_ref); |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 601 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 602 | return 0; |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 603 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 604 | |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 605 | /* basic@{time or number or -number} format to query ref-log */ |
Junio C Hamano | 694500e | 2006-10-23 21:15:34 -0700 | [diff] [blame] | 606 | reflog_len = at = 0; |
Johannes Schindelin | f265458 | 2009-01-28 00:07:46 +0100 | [diff] [blame] | 607 | if (len && str[len-1] == '}') { |
Ramkumar Ramachandra | e883a05 | 2013-05-07 16:55:09 -0500 | [diff] [blame] | 608 | for (at = len-4; at >= 0; at--) { |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 609 | if (str[at] == '@' && str[at+1] == '{') { |
Ramkumar Ramachandra | 83d16bc | 2013-05-07 16:55:11 -0500 | [diff] [blame] | 610 | if (str[at+2] == '-') { |
| 611 | if (at != 0) |
| 612 | /* @{-N} not at start */ |
| 613 | return -1; |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 614 | nth_prior = 1; |
| 615 | continue; |
| 616 | } |
Jeff King | adfe5d0 | 2015-05-21 00:45:47 -0400 | [diff] [blame] | 617 | if (!upstream_mark(str + at, len - at) && |
| 618 | !push_mark(str + at, len - at)) { |
Johannes Schindelin | 28fb843 | 2009-09-10 17:25:57 +0200 | [diff] [blame] | 619 | reflog_len = (len-1) - (at+2); |
| 620 | len = at; |
| 621 | } |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 622 | break; |
| 623 | } |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 627 | /* Accept only unambiguous ref paths. */ |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 628 | if (len && ambiguous_path(str, len)) |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 629 | return -1; |
| 630 | |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 631 | if (nth_prior) { |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 632 | struct strbuf buf = STRBUF_INIT; |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 633 | int detached; |
| 634 | |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 635 | if (interpret_nth_prior_checkout(str, len, &buf) > 0) { |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 636 | detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1)); |
| 637 | strbuf_release(&buf); |
| 638 | if (detached) |
| 639 | return 0; |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 640 | } |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | if (!len && reflog_len) |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 644 | /* allow "@{...}" to mean the current branch reflog */ |
| 645 | refs_found = dwim_ref("HEAD", 4, sha1, &real_ref); |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 646 | else if (reflog_len) |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 647 | refs_found = dwim_log(str, len, sha1, &real_ref); |
| 648 | else |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 649 | refs_found = dwim_ref(str, len, sha1, &real_ref); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 650 | |
| 651 | if (!refs_found) |
| 652 | return -1; |
| 653 | |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 654 | if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) && |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 655 | (refs_found > 1 || |
| 656 | !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY))) |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 657 | warning(warn_msg, len, str); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 658 | |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 659 | if (reflog_len) { |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 660 | int nth, i; |
| 661 | unsigned long at_time; |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 662 | unsigned long co_time; |
| 663 | int co_tz, co_cnt; |
| 664 | |
Nicolas Pitre | fe55851 | 2007-02-01 12:33:23 -0500 | [diff] [blame] | 665 | /* Is it asking for N-th entry, or approxidate? */ |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 666 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
| 667 | char ch = str[at+2+i]; |
| 668 | if ('0' <= ch && ch <= '9') |
| 669 | nth = nth * 10 + ch - '0'; |
| 670 | else |
| 671 | nth = -1; |
| 672 | } |
Shawn O. Pearce | ea360dd | 2008-08-21 08:40:44 -0700 | [diff] [blame] | 673 | if (100000000 <= nth) { |
| 674 | at_time = nth; |
| 675 | nth = -1; |
| 676 | } else if (0 <= nth) |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 677 | at_time = 0; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 678 | else { |
Junio C Hamano | 93cfa7c | 2010-01-26 11:58:00 -0800 | [diff] [blame] | 679 | int errors = 0; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 680 | char *tmp = xstrndup(str + at + 2, reflog_len); |
Junio C Hamano | 93cfa7c | 2010-01-26 11:58:00 -0800 | [diff] [blame] | 681 | at_time = approxidate_careful(tmp, &errors); |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 682 | free(tmp); |
Jeff King | 28b3563 | 2014-07-24 00:41:11 -0400 | [diff] [blame] | 683 | if (errors) { |
| 684 | free(real_ref); |
Junio C Hamano | a5e10ac | 2010-01-27 10:53:09 -0800 | [diff] [blame] | 685 | return -1; |
Jeff King | 28b3563 | 2014-07-24 00:41:11 -0400 | [diff] [blame] | 686 | } |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 687 | } |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 688 | if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL, |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 689 | &co_time, &co_tz, &co_cnt)) { |
Ramkumar Ramachandra | 305ebea | 2013-05-22 16:09:55 +0530 | [diff] [blame] | 690 | if (!len) { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 691 | if (starts_with(real_ref, "refs/heads/")) { |
Ramkumar Ramachandra | 305ebea | 2013-05-22 16:09:55 +0530 | [diff] [blame] | 692 | str = real_ref + 11; |
| 693 | len = strlen(real_ref + 11); |
| 694 | } else { |
| 695 | /* detached HEAD */ |
| 696 | str = "HEAD"; |
| 697 | len = 4; |
| 698 | } |
| 699 | } |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 700 | if (at_time) { |
| 701 | if (!(flags & GET_SHA1_QUIETLY)) { |
| 702 | warning("Log for '%.*s' only goes " |
| 703 | "back to %s.", len, str, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 704 | show_date(co_time, co_tz, DATE_MODE(RFC2822))); |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 705 | } |
| 706 | } else { |
| 707 | if (flags & GET_SHA1_QUIETLY) { |
| 708 | exit(128); |
| 709 | } |
Jon Seymour | e6eedc3 | 2010-08-24 14:52:42 +1000 | [diff] [blame] | 710 | die("Log for '%.*s' only has %d entries.", |
| 711 | len, str, co_cnt); |
| 712 | } |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 713 | } |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 714 | } |
| 715 | |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 716 | free(real_ref); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 717 | return 0; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 720 | static int get_parent(const char *name, int len, |
| 721 | unsigned char *result, int idx) |
| 722 | { |
| 723 | unsigned char sha1[20]; |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 724 | int ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 725 | struct commit *commit; |
| 726 | struct commit_list *p; |
| 727 | |
| 728 | if (ret) |
| 729 | return ret; |
| 730 | commit = lookup_commit_reference(sha1); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 731 | if (parse_commit(commit)) |
| 732 | return -1; |
| 733 | if (!idx) { |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 734 | hashcpy(result, commit->object.oid.hash); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | p = commit->parents; |
| 738 | while (p) { |
| 739 | if (!--idx) { |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 740 | hashcpy(result, p->item->object.oid.hash); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 741 | return 0; |
| 742 | } |
| 743 | p = p->next; |
| 744 | } |
| 745 | return -1; |
| 746 | } |
| 747 | |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 748 | static int get_nth_ancestor(const char *name, int len, |
| 749 | unsigned char *result, int generation) |
| 750 | { |
| 751 | unsigned char sha1[20]; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 752 | struct commit *commit; |
| 753 | int ret; |
| 754 | |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 755 | ret = get_sha1_1(name, len, sha1, GET_SHA1_COMMITTISH); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 756 | if (ret) |
| 757 | return ret; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 758 | commit = lookup_commit_reference(sha1); |
| 759 | if (!commit) |
| 760 | return -1; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 761 | |
| 762 | while (generation--) { |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 763 | if (parse_commit(commit) || !commit->parents) |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 764 | return -1; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 765 | commit = commit->parents->item; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 766 | } |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 767 | hashcpy(result, commit->object.oid.hash); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 768 | return 0; |
| 769 | } |
| 770 | |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 771 | struct object *peel_to_type(const char *name, int namelen, |
| 772 | struct object *o, enum object_type expected_type) |
| 773 | { |
| 774 | if (name && !namelen) |
| 775 | namelen = strlen(name); |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 776 | while (1) { |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 777 | if (!o || (!o->parsed && !parse_object(o->oid.hash))) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 778 | return NULL; |
Junio C Hamano | a6a3f2c | 2013-03-31 15:24:12 -0700 | [diff] [blame] | 779 | if (expected_type == OBJ_ANY || o->type == expected_type) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 780 | return o; |
| 781 | if (o->type == OBJ_TAG) |
| 782 | o = ((struct tag*) o)->tagged; |
| 783 | else if (o->type == OBJ_COMMIT) |
| 784 | o = &(((struct commit *) o)->tree->object); |
| 785 | else { |
| 786 | if (name) |
| 787 | error("%.*s: expected %s type, but the object " |
| 788 | "dereferences to %s type", |
| 789 | namelen, name, typename(expected_type), |
| 790 | typename(o->type)); |
| 791 | return NULL; |
| 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 796 | static int peel_onion(const char *name, int len, unsigned char *sha1, |
| 797 | unsigned lookup_flags) |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 798 | { |
| 799 | unsigned char outer[20]; |
| 800 | const char *sp; |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 801 | unsigned int expected_type = 0; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 802 | struct object *o; |
| 803 | |
| 804 | /* |
| 805 | * "ref^{type}" dereferences ref repeatedly until you cannot |
| 806 | * dereference anymore, or you get an object of given type, |
| 807 | * whichever comes first. "ref^{}" means just dereference |
| 808 | * tags until you get a non-tag. "ref^0" is a shorthand for |
| 809 | * "ref^{commit}". "commit^{tree}" could be used to find the |
| 810 | * top-level tree of the given commit. |
| 811 | */ |
| 812 | if (len < 4 || name[len-1] != '}') |
| 813 | return -1; |
| 814 | |
| 815 | for (sp = name + len - 1; name <= sp; sp--) { |
| 816 | int ch = *sp; |
| 817 | if (ch == '{' && name < sp && sp[-1] == '^') |
| 818 | break; |
| 819 | } |
| 820 | if (sp <= name) |
| 821 | return -1; |
| 822 | |
| 823 | sp++; /* beginning of type name, or closing brace for empty */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 824 | if (starts_with(sp, "commit}")) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 825 | expected_type = OBJ_COMMIT; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 826 | else if (starts_with(sp, "tag}")) |
Richard Hansen | 75aa26d | 2013-09-03 15:50:16 -0400 | [diff] [blame] | 827 | expected_type = OBJ_TAG; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 828 | else if (starts_with(sp, "tree}")) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 829 | expected_type = OBJ_TREE; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 830 | else if (starts_with(sp, "blob}")) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 831 | expected_type = OBJ_BLOB; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 832 | else if (starts_with(sp, "object}")) |
Junio C Hamano | a6a3f2c | 2013-03-31 15:24:12 -0700 | [diff] [blame] | 833 | expected_type = OBJ_ANY; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 834 | else if (sp[0] == '}') |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 835 | expected_type = OBJ_NONE; |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 836 | else if (sp[0] == '/') |
| 837 | expected_type = OBJ_COMMIT; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 838 | else |
| 839 | return -1; |
| 840 | |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 841 | lookup_flags &= ~GET_SHA1_DISAMBIGUATORS; |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 842 | if (expected_type == OBJ_COMMIT) |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 843 | lookup_flags |= GET_SHA1_COMMITTISH; |
Junio C Hamano | ed1ca60 | 2013-03-31 15:19:52 -0700 | [diff] [blame] | 844 | else if (expected_type == OBJ_TREE) |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 845 | lookup_flags |= GET_SHA1_TREEISH; |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 846 | |
| 847 | if (get_sha1_1(name, sp - name - 2, outer, lookup_flags)) |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 848 | return -1; |
| 849 | |
| 850 | o = parse_object(outer); |
| 851 | if (!o) |
| 852 | return -1; |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 853 | if (!expected_type) { |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 854 | o = deref_tag(o, name, sp - name - 2); |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 855 | if (!o || (!o->parsed && !parse_object(o->oid.hash))) |
Junio C Hamano | 6e1c6c1 | 2005-10-19 22:48:16 -0700 | [diff] [blame] | 856 | return -1; |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 857 | hashcpy(sha1, o->oid.hash); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 858 | return 0; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 859 | } |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 860 | |
| 861 | /* |
| 862 | * At this point, the syntax look correct, so |
| 863 | * if we do not get the needed object, we should |
| 864 | * barf. |
| 865 | */ |
| 866 | o = peel_to_type(name, len, o, expected_type); |
| 867 | if (!o) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 868 | return -1; |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 869 | |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 870 | hashcpy(sha1, o->oid.hash); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 871 | if (sp[0] == '/') { |
| 872 | /* "$commit^{/foo}" */ |
| 873 | char *prefix; |
| 874 | int ret; |
| 875 | struct commit_list *list = NULL; |
| 876 | |
Nguyễn Thái Ngọc Duy | 4322842 | 2010-12-15 16:02:54 +0700 | [diff] [blame] | 877 | /* |
| 878 | * $commit^{/}. Some regex implementation may reject. |
| 879 | * We don't need regex anyway. '' pattern always matches. |
| 880 | */ |
| 881 | if (sp[1] == '}') |
| 882 | return 0; |
| 883 | |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 884 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
| 885 | commit_list_insert((struct commit *)o, &list); |
| 886 | ret = get_sha1_oneline(prefix, sha1, list); |
| 887 | free(prefix); |
| 888 | return ret; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 893 | static int get_describe_name(const char *name, int len, unsigned char *sha1) |
| 894 | { |
| 895 | const char *cp; |
Junio C Hamano | 6269b6b | 2012-06-18 13:45:56 -0700 | [diff] [blame] | 896 | unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT; |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 897 | |
| 898 | for (cp = name + len - 1; name + 2 <= cp; cp--) { |
| 899 | char ch = *cp; |
René Scharfe | 6f75d45 | 2015-03-09 23:46:54 +0100 | [diff] [blame] | 900 | if (!isxdigit(ch)) { |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 901 | /* We must be looking at g in "SOMETHING-g" |
| 902 | * for it to be describe output. |
| 903 | */ |
| 904 | if (ch == 'g' && cp[-1] == '-') { |
| 905 | cp++; |
| 906 | len -= cp - name; |
Junio C Hamano | 6269b6b | 2012-06-18 13:45:56 -0700 | [diff] [blame] | 907 | return get_short_sha1(cp, len, sha1, flags); |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | } |
| 911 | return -1; |
| 912 | } |
| 913 | |
Junio C Hamano | e48ba20 | 2012-07-02 09:46:50 -0700 | [diff] [blame] | 914 | static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 915 | { |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 916 | int ret, has_suffix; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 917 | const char *cp; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 918 | |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 919 | /* |
| 920 | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 921 | */ |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 922 | has_suffix = 0; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 923 | for (cp = name + len - 1; name <= cp; cp--) { |
| 924 | int ch = *cp; |
| 925 | if ('0' <= ch && ch <= '9') |
| 926 | continue; |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 927 | if (ch == '~' || ch == '^') |
| 928 | has_suffix = ch; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 929 | break; |
| 930 | } |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 931 | |
| 932 | if (has_suffix) { |
| 933 | int num = 0; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 934 | int len1 = cp - name; |
| 935 | cp++; |
| 936 | while (cp < name + len) |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 937 | num = num * 10 + *cp++ - '0'; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 938 | if (!num && len1 == len - 1) |
| 939 | num = 1; |
| 940 | if (has_suffix == '^') |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 941 | return get_parent(name, len1, sha1, num); |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 942 | /* else if (has_suffix == '~') -- goes without saying */ |
| 943 | return get_nth_ancestor(name, len1, sha1, num); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 946 | ret = peel_onion(name, len, sha1, lookup_flags); |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 947 | if (!ret) |
| 948 | return 0; |
| 949 | |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 950 | ret = get_sha1_basic(name, len, sha1, lookup_flags); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 951 | if (!ret) |
| 952 | return 0; |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 953 | |
| 954 | /* It could be describe output that is "SOMETHING-gXXXX" */ |
| 955 | ret = get_describe_name(name, len, sha1); |
| 956 | if (!ret) |
| 957 | return 0; |
| 958 | |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 959 | return get_short_sha1(name, len, sha1, lookup_flags); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 962 | /* |
| 963 | * This interprets names like ':/Initial revision of "git"' by searching |
| 964 | * through history and returning the first commit whose message starts |
Junio C Hamano | 3d04589 | 2010-08-12 18:32:49 -0700 | [diff] [blame] | 965 | * the given regular expression. |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 966 | * |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 967 | * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'. |
| 968 | * |
| 969 | * For a literal '!' character at the beginning of a pattern, you have to repeat |
| 970 | * that, like: ':/!!foo' |
| 971 | * |
| 972 | * For future extension, all other sequences beginning with ':/!' are reserved. |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 973 | */ |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 974 | |
| 975 | /* Remember to update object flag allocation in object.h */ |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 976 | #define ONELINE_SEEN (1u<<20) |
| 977 | |
Michael Haggerty | 9c5fe0b | 2015-05-25 18:39:05 +0000 | [diff] [blame] | 978 | static int handle_one_ref(const char *path, const struct object_id *oid, |
| 979 | int flag, void *cb_data) |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 980 | { |
| 981 | struct commit_list **list = cb_data; |
Michael Haggerty | 9c5fe0b | 2015-05-25 18:39:05 +0000 | [diff] [blame] | 982 | struct object *object = parse_object(oid->hash); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 983 | if (!object) |
| 984 | return 0; |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 985 | if (object->type == OBJ_TAG) { |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 986 | object = deref_tag(object, path, strlen(path)); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 987 | if (!object) |
| 988 | return 0; |
| 989 | } |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 990 | if (object->type != OBJ_COMMIT) |
| 991 | return 0; |
René Scharfe | e8d1dfe | 2014-08-21 20:30:29 +0200 | [diff] [blame] | 992 | commit_list_insert((struct commit *)object, list); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 993 | return 0; |
| 994 | } |
| 995 | |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 996 | static int get_sha1_oneline(const char *prefix, unsigned char *sha1, |
| 997 | struct commit_list *list) |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 998 | { |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 999 | struct commit_list *backup = NULL, *l; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1000 | int found = 0; |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1001 | int negative = 0; |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1002 | regex_t regex; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1003 | |
| 1004 | if (prefix[0] == '!') { |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1005 | prefix++; |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1006 | |
| 1007 | if (prefix[0] == '-') { |
| 1008 | prefix++; |
| 1009 | negative = 1; |
| 1010 | } else if (prefix[0] != '!') { |
Junio C Hamano | e6a6a76 | 2016-02-24 13:25:52 -0800 | [diff] [blame] | 1011 | return -1; |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1012 | } |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1013 | } |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1014 | |
| 1015 | if (regcomp(®ex, prefix, REG_EXTENDED)) |
Jeff King | aac4fac | 2016-02-10 16:19:25 -0500 | [diff] [blame] | 1016 | return -1; |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1017 | |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1018 | for (l = list; l; l = l->next) { |
| 1019 | l->item->object.flags |= ONELINE_SEEN; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1020 | commit_list_insert(l->item, &backup); |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1021 | } |
Jim Meyering | ed8ad7e | 2007-03-11 19:49:08 +0100 | [diff] [blame] | 1022 | while (list) { |
Jeff King | ba41c1c | 2014-06-10 17:41:02 -0400 | [diff] [blame] | 1023 | const char *p, *buf; |
Linus Torvalds | 1358e7d | 2007-03-12 11:30:38 -0700 | [diff] [blame] | 1024 | struct commit *commit; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1025 | int matches; |
Jim Meyering | ed8ad7e | 2007-03-11 19:49:08 +0100 | [diff] [blame] | 1026 | |
| 1027 | commit = pop_most_recent_commit(&list, ONELINE_SEEN); |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 1028 | if (!parse_object(commit->object.oid.hash)) |
Martin Koegler | 283cdbc | 2008-02-18 21:47:53 +0100 | [diff] [blame] | 1029 | continue; |
Jeff King | 8597ea3 | 2014-06-10 17:44:13 -0400 | [diff] [blame] | 1030 | buf = get_commit_buffer(commit, NULL); |
Jeff King | ba41c1c | 2014-06-10 17:41:02 -0400 | [diff] [blame] | 1031 | p = strstr(buf, "\n\n"); |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1032 | matches = negative ^ (p && !regexec(®ex, p + 2, 0, NULL, 0)); |
Jeff King | ba41c1c | 2014-06-10 17:41:02 -0400 | [diff] [blame] | 1033 | unuse_commit_buffer(commit, buf); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1034 | |
| 1035 | if (matches) { |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 1036 | hashcpy(sha1, commit->object.oid.hash); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1037 | found = 1; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | } |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1041 | regfree(®ex); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1042 | free_commit_list(list); |
| 1043 | for (l = backup; l; l = l->next) |
| 1044 | clear_commit_marks(l->item, ONELINE_SEEN); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1045 | free_commit_list(backup); |
| 1046 | return found ? 0 : -1; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1047 | } |
| 1048 | |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1049 | struct grab_nth_branch_switch_cbdata { |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1050 | int remaining; |
| 1051 | struct strbuf buf; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1052 | }; |
| 1053 | |
| 1054 | static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1, |
| 1055 | const char *email, unsigned long timestamp, int tz, |
| 1056 | const char *message, void *cb_data) |
| 1057 | { |
| 1058 | struct grab_nth_branch_switch_cbdata *cb = cb_data; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1059 | const char *match = NULL, *target = NULL; |
| 1060 | size_t len; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1061 | |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 1062 | if (skip_prefix(message, "checkout: moving from ", &match)) |
Junio C Hamano | d7c03c1 | 2009-01-21 00:37:38 -0800 | [diff] [blame] | 1063 | target = strstr(match, " to "); |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1064 | |
Junio C Hamano | c829774 | 2009-01-19 16:44:08 -0800 | [diff] [blame] | 1065 | if (!match || !target) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1066 | return 0; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1067 | if (--(cb->remaining) == 0) { |
| 1068 | len = target - match; |
| 1069 | strbuf_reset(&cb->buf); |
| 1070 | strbuf_add(&cb->buf, match, len); |
| 1071 | return 1; /* we are done */ |
| 1072 | } |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | /* |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1077 | * Parse @{-N} syntax, return the number of characters parsed |
| 1078 | * if successful; otherwise signal an error with negative value. |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1079 | */ |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1080 | static int interpret_nth_prior_checkout(const char *name, int namelen, |
| 1081 | struct strbuf *buf) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1082 | { |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 1083 | long nth; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1084 | int retval; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1085 | struct grab_nth_branch_switch_cbdata cb; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1086 | const char *brace; |
| 1087 | char *num_end; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1088 | |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1089 | if (namelen < 4) |
| 1090 | return -1; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1091 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') |
| 1092 | return -1; |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1093 | brace = memchr(name, '}', namelen); |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1094 | if (!brace) |
| 1095 | return -1; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1096 | nth = strtol(name + 3, &num_end, 10); |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1097 | if (num_end != brace) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1098 | return -1; |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 1099 | if (nth <= 0) |
| 1100 | return -1; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1101 | cb.remaining = nth; |
| 1102 | strbuf_init(&cb.buf, 20); |
| 1103 | |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 1104 | retval = 0; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1105 | if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) { |
| 1106 | strbuf_reset(buf); |
René Scharfe | e992d1e | 2014-07-10 10:52:21 +0200 | [diff] [blame] | 1107 | strbuf_addbuf(buf, &cb.buf); |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1108 | retval = brace - name + 1; |
Junio C Hamano | 101d15e | 2009-01-19 22:18:29 -0800 | [diff] [blame] | 1109 | } |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 1110 | |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1111 | strbuf_release(&cb.buf); |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 1112 | return retval; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1115 | int get_oid_mb(const char *name, struct object_id *oid) |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1116 | { |
| 1117 | struct commit *one, *two; |
| 1118 | struct commit_list *mbs; |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1119 | struct object_id oid_tmp; |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1120 | const char *dots; |
| 1121 | int st; |
| 1122 | |
| 1123 | dots = strstr(name, "..."); |
| 1124 | if (!dots) |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1125 | return get_oid(name, oid); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1126 | if (dots == name) |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1127 | st = get_oid("HEAD", &oid_tmp); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1128 | else { |
| 1129 | struct strbuf sb; |
| 1130 | strbuf_init(&sb, dots - name); |
| 1131 | strbuf_add(&sb, name, dots - name); |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1132 | st = get_sha1_committish(sb.buf, oid_tmp.hash); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1133 | strbuf_release(&sb); |
| 1134 | } |
| 1135 | if (st) |
| 1136 | return st; |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1137 | one = lookup_commit_reference_gently(oid_tmp.hash, 0); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1138 | if (!one) |
| 1139 | return -1; |
| 1140 | |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1141 | if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash)) |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1142 | return -1; |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1143 | two = lookup_commit_reference_gently(oid_tmp.hash, 0); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1144 | if (!two) |
| 1145 | return -1; |
Junio C Hamano | 2ce406c | 2014-10-30 12:20:44 -0700 | [diff] [blame] | 1146 | mbs = get_merge_bases(one, two); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1147 | if (!mbs || mbs->next) |
| 1148 | st = -1; |
| 1149 | else { |
| 1150 | st = 0; |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1151 | oidcpy(oid, &mbs->item->object.oid); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1152 | } |
| 1153 | free_commit_list(mbs); |
| 1154 | return st; |
| 1155 | } |
| 1156 | |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1157 | /* parse @something syntax, when 'something' is not {.*} */ |
| 1158 | static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf) |
| 1159 | { |
| 1160 | const char *next; |
| 1161 | |
| 1162 | if (len || name[1] == '{') |
| 1163 | return -1; |
| 1164 | |
| 1165 | /* make sure it's a single @, or @@{.*}, not @foo */ |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1166 | next = memchr(name + len + 1, '@', namelen - len - 1); |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1167 | if (next && next[1] != '{') |
| 1168 | return -1; |
| 1169 | if (!next) |
| 1170 | next = name + namelen; |
| 1171 | if (next != name + 1) |
| 1172 | return -1; |
| 1173 | |
| 1174 | strbuf_reset(buf); |
| 1175 | strbuf_add(buf, "HEAD", 4); |
| 1176 | return 1; |
| 1177 | } |
| 1178 | |
Felipe Contreras | 7a0a49a | 2013-05-07 17:04:30 -0500 | [diff] [blame] | 1179 | static int reinterpret(const char *name, int namelen, int len, struct strbuf *buf) |
| 1180 | { |
| 1181 | /* we have extra data, which might need further processing */ |
| 1182 | struct strbuf tmp = STRBUF_INIT; |
| 1183 | int used = buf->len; |
| 1184 | int ret; |
| 1185 | |
| 1186 | strbuf_add(buf, name + len, namelen - len); |
Felipe Contreras | cf99a76 | 2013-09-02 01:34:29 -0500 | [diff] [blame] | 1187 | ret = interpret_branch_name(buf->buf, buf->len, &tmp); |
Felipe Contreras | 7a0a49a | 2013-05-07 17:04:30 -0500 | [diff] [blame] | 1188 | /* that data was not interpreted, remove our cruft */ |
| 1189 | if (ret < 0) { |
| 1190 | strbuf_setlen(buf, used); |
| 1191 | return len; |
| 1192 | } |
| 1193 | strbuf_reset(buf); |
| 1194 | strbuf_addbuf(buf, &tmp); |
| 1195 | strbuf_release(&tmp); |
| 1196 | /* tweak for size of {-N} versus expanded ref name */ |
| 1197 | return ret - used + len; |
| 1198 | } |
| 1199 | |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1200 | static void set_shortened_ref(struct strbuf *buf, const char *ref) |
| 1201 | { |
| 1202 | char *s = shorten_unambiguous_ref(ref, 0); |
| 1203 | strbuf_reset(buf); |
| 1204 | strbuf_addstr(buf, s); |
| 1205 | free(s); |
| 1206 | } |
| 1207 | |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1208 | static int interpret_branch_mark(const char *name, int namelen, |
| 1209 | int at, struct strbuf *buf, |
| 1210 | int (*get_mark)(const char *, int), |
| 1211 | const char *(*get_data)(struct branch *, |
| 1212 | struct strbuf *)) |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1213 | { |
| 1214 | int len; |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1215 | struct branch *branch; |
| 1216 | struct strbuf err = STRBUF_INIT; |
| 1217 | const char *value; |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1218 | |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1219 | len = get_mark(name + at, namelen - at); |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1220 | if (!len) |
| 1221 | return -1; |
| 1222 | |
Jeff King | 3f6eb30 | 2014-01-15 03:37:23 -0500 | [diff] [blame] | 1223 | if (memchr(name, ':', at)) |
| 1224 | return -1; |
| 1225 | |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1226 | if (at) { |
| 1227 | char *name_str = xmemdupz(name, at); |
| 1228 | branch = branch_get(name_str); |
| 1229 | free(name_str); |
| 1230 | } else |
| 1231 | branch = branch_get(NULL); |
| 1232 | |
| 1233 | value = get_data(branch, &err); |
| 1234 | if (!value) |
| 1235 | die("%s", err.buf); |
| 1236 | |
| 1237 | set_shortened_ref(buf, value); |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1238 | return len + at; |
| 1239 | } |
| 1240 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1241 | /* |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1242 | * This reads short-hand syntax that not only evaluates to a commit |
| 1243 | * object name, but also can act as if the end user spelled the name |
| 1244 | * of the branch from the command line. |
| 1245 | * |
| 1246 | * - "@{-N}" finds the name of the Nth previous branch we were on, and |
| 1247 | * places the name of the branch in the given buf and returns the |
| 1248 | * number of characters parsed if successful. |
| 1249 | * |
| 1250 | * - "<branch>@{upstream}" finds the name of the other ref that |
| 1251 | * <branch> is configured to merge with (missing <branch> defaults |
| 1252 | * to the current branch), and places the name of the branch in the |
| 1253 | * given buf and returns the number of characters parsed if |
| 1254 | * successful. |
| 1255 | * |
| 1256 | * If the input is not of the accepted format, it returns a negative |
| 1257 | * number to signal an error. |
| 1258 | * |
| 1259 | * If the input was ok but there are not N branch switches in the |
| 1260 | * reflog, it returns 0. |
| 1261 | */ |
Felipe Contreras | cf99a76 | 2013-09-02 01:34:29 -0500 | [diff] [blame] | 1262 | int interpret_branch_name(const char *name, int namelen, struct strbuf *buf) |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1263 | { |
Jeff King | f278f40 | 2014-01-15 03:27:32 -0500 | [diff] [blame] | 1264 | char *at; |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1265 | const char *start; |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1266 | int len = interpret_nth_prior_checkout(name, namelen, buf); |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1267 | |
Felipe Contreras | cf99a76 | 2013-09-02 01:34:29 -0500 | [diff] [blame] | 1268 | if (!namelen) |
| 1269 | namelen = strlen(name); |
| 1270 | |
Felipe Contreras | 1f27e7d | 2013-04-30 16:49:11 -0500 | [diff] [blame] | 1271 | if (!len) { |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1272 | return len; /* syntax Ok, not enough switches */ |
Felipe Contreras | 1f27e7d | 2013-04-30 16:49:11 -0500 | [diff] [blame] | 1273 | } else if (len > 0) { |
| 1274 | if (len == namelen) |
| 1275 | return len; /* consumed all */ |
| 1276 | else |
| 1277 | return reinterpret(name, namelen, len, buf); |
Jeff King | d46a830 | 2010-01-28 04:52:22 -0500 | [diff] [blame] | 1278 | } |
| 1279 | |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1280 | for (start = name; |
| 1281 | (at = memchr(start, '@', namelen - (start - name))); |
| 1282 | start = at + 1) { |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1283 | |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1284 | len = interpret_empty_at(name, namelen, at - name, buf); |
| 1285 | if (len > 0) |
| 1286 | return reinterpret(name, namelen, len, buf); |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1287 | |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1288 | len = interpret_branch_mark(name, namelen, at - name, buf, |
| 1289 | upstream_mark, branch_get_upstream); |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1290 | if (len > 0) |
| 1291 | return len; |
Jeff King | adfe5d0 | 2015-05-21 00:45:47 -0400 | [diff] [blame] | 1292 | |
| 1293 | len = interpret_branch_mark(name, namelen, at - name, buf, |
| 1294 | push_mark, branch_get_push); |
Zbigniew Jędrzejewski-Szmek | 6472028 | 2012-04-14 09:54:35 +0200 | [diff] [blame] | 1295 | if (len > 0) |
Zbigniew Jędrzejewski-Szmek | 17c8221 | 2012-04-14 09:54:34 +0200 | [diff] [blame] | 1296 | return len; |
Zbigniew Jędrzejewski-Szmek | bb0dab5 | 2012-04-14 09:54:33 +0200 | [diff] [blame] | 1297 | } |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1298 | |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1299 | return -1; |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1302 | int strbuf_branchname(struct strbuf *sb, const char *name) |
| 1303 | { |
| 1304 | int len = strlen(name); |
Felipe Contreras | cf99a76 | 2013-09-02 01:34:29 -0500 | [diff] [blame] | 1305 | int used = interpret_branch_name(name, len, sb); |
Junio C Hamano | 84cf246 | 2013-05-15 14:32:30 -0700 | [diff] [blame] | 1306 | |
| 1307 | if (used == len) |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1308 | return 0; |
Junio C Hamano | 84cf246 | 2013-05-15 14:32:30 -0700 | [diff] [blame] | 1309 | if (used < 0) |
| 1310 | used = 0; |
| 1311 | strbuf_add(sb, name + used, len - used); |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1312 | return len; |
| 1313 | } |
| 1314 | |
| 1315 | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) |
| 1316 | { |
| 1317 | strbuf_branchname(sb, name); |
| 1318 | if (name[0] == '-') |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 1319 | return -1; |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1320 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 1321 | return check_refname_format(sb->buf, 0); |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1322 | } |
| 1323 | |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1324 | /* |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1325 | * This is like "get_sha1_basic()", except it allows "sha1 expressions", |
| 1326 | * notably "xyz^" for "parent of xyz" |
| 1327 | */ |
| 1328 | int get_sha1(const char *name, unsigned char *sha1) |
| 1329 | { |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1330 | struct object_context unused; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1331 | return get_sha1_with_context(name, 0, sha1, &unused); |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1334 | /* |
brian m. carlson | 2764fd9 | 2016-04-17 23:10:36 +0000 | [diff] [blame] | 1335 | * This is like "get_sha1()", but for struct object_id. |
| 1336 | */ |
| 1337 | int get_oid(const char *name, struct object_id *oid) |
| 1338 | { |
| 1339 | return get_sha1(name, oid->hash); |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | /* |
Richard Hansen | a8a5406 | 2013-09-04 15:04:31 -0400 | [diff] [blame] | 1344 | * Many callers know that the user meant to name a commit-ish by |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1345 | * syntactical positions where the object name appears. Calling this |
| 1346 | * function allows the machinery to disambiguate shorter-than-unique |
Richard Hansen | a8a5406 | 2013-09-04 15:04:31 -0400 | [diff] [blame] | 1347 | * abbreviated object names between commit-ish and others. |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1348 | * |
| 1349 | * Note that this does NOT error out when the named object is not a |
Richard Hansen | a8a5406 | 2013-09-04 15:04:31 -0400 | [diff] [blame] | 1350 | * commit-ish. It is merely to give a hint to the disambiguation |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1351 | * machinery. |
| 1352 | */ |
| 1353 | int get_sha1_committish(const char *name, unsigned char *sha1) |
| 1354 | { |
| 1355 | struct object_context unused; |
| 1356 | return get_sha1_with_context(name, GET_SHA1_COMMITTISH, |
| 1357 | sha1, &unused); |
| 1358 | } |
| 1359 | |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1360 | int get_sha1_treeish(const char *name, unsigned char *sha1) |
| 1361 | { |
| 1362 | struct object_context unused; |
| 1363 | return get_sha1_with_context(name, GET_SHA1_TREEISH, |
| 1364 | sha1, &unused); |
| 1365 | } |
| 1366 | |
| 1367 | int get_sha1_commit(const char *name, unsigned char *sha1) |
| 1368 | { |
| 1369 | struct object_context unused; |
| 1370 | return get_sha1_with_context(name, GET_SHA1_COMMIT, |
| 1371 | sha1, &unused); |
| 1372 | } |
| 1373 | |
| 1374 | int get_sha1_tree(const char *name, unsigned char *sha1) |
| 1375 | { |
| 1376 | struct object_context unused; |
| 1377 | return get_sha1_with_context(name, GET_SHA1_TREE, |
| 1378 | sha1, &unused); |
| 1379 | } |
| 1380 | |
| 1381 | int get_sha1_blob(const char *name, unsigned char *sha1) |
| 1382 | { |
| 1383 | struct object_context unused; |
| 1384 | return get_sha1_with_context(name, GET_SHA1_BLOB, |
| 1385 | sha1, &unused); |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1386 | } |
| 1387 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1388 | /* Must be called only when object_name:filename doesn't exist. */ |
| 1389 | static void diagnose_invalid_sha1_path(const char *prefix, |
| 1390 | const char *filename, |
| 1391 | const unsigned char *tree_sha1, |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1392 | const char *object_name, |
| 1393 | int object_name_len) |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1394 | { |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1395 | unsigned char sha1[20]; |
| 1396 | unsigned mode; |
| 1397 | |
| 1398 | if (!prefix) |
| 1399 | prefix = ""; |
| 1400 | |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 1401 | if (file_exists(filename)) |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1402 | die("Path '%s' exists on disk, but not in '%.*s'.", |
| 1403 | filename, object_name_len, object_name); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1404 | if (errno == ENOENT || errno == ENOTDIR) { |
Jeff King | b2724c8 | 2014-06-19 17:26:56 -0400 | [diff] [blame] | 1405 | char *fullname = xstrfmt("%s%s", prefix, filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1406 | |
| 1407 | if (!get_tree_entry(tree_sha1, fullname, |
| 1408 | sha1, &mode)) { |
| 1409 | die("Path '%s' exists, but not '%s'.\n" |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1410 | "Did you mean '%.*s:%s' aka '%.*s:./%s'?", |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1411 | fullname, |
| 1412 | filename, |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1413 | object_name_len, object_name, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1414 | fullname, |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1415 | object_name_len, object_name, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1416 | filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1417 | } |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1418 | die("Path '%s' does not exist in '%.*s'", |
| 1419 | filename, object_name_len, object_name); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | /* Must be called only when :stage:filename doesn't exist. */ |
| 1424 | static void diagnose_invalid_index_path(int stage, |
| 1425 | const char *prefix, |
| 1426 | const char *filename) |
| 1427 | { |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1428 | const struct cache_entry *ce; |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1429 | int pos; |
| 1430 | unsigned namelen = strlen(filename); |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1431 | struct strbuf fullname = STRBUF_INIT; |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1432 | |
| 1433 | if (!prefix) |
| 1434 | prefix = ""; |
| 1435 | |
| 1436 | /* Wrong stage number? */ |
| 1437 | pos = cache_name_pos(filename, namelen); |
| 1438 | if (pos < 0) |
| 1439 | pos = -pos - 1; |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1440 | if (pos < active_nr) { |
| 1441 | ce = active_cache[pos]; |
| 1442 | if (ce_namelen(ce) == namelen && |
| 1443 | !memcmp(ce->name, filename, namelen)) |
| 1444 | die("Path '%s' is in the index, but not at stage %d.\n" |
| 1445 | "Did you mean ':%d:%s'?", |
| 1446 | filename, stage, |
| 1447 | ce_stage(ce), filename); |
| 1448 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1449 | |
| 1450 | /* Confusion between relative and absolute filenames? */ |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1451 | strbuf_addstr(&fullname, prefix); |
| 1452 | strbuf_addstr(&fullname, filename); |
| 1453 | pos = cache_name_pos(fullname.buf, fullname.len); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1454 | if (pos < 0) |
| 1455 | pos = -pos - 1; |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1456 | if (pos < active_nr) { |
| 1457 | ce = active_cache[pos]; |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1458 | if (ce_namelen(ce) == fullname.len && |
| 1459 | !memcmp(ce->name, fullname.buf, fullname.len)) |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1460 | die("Path '%s' is in the index, but not '%s'.\n" |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1461 | "Did you mean ':%d:%s' aka ':%d:./%s'?", |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1462 | fullname.buf, filename, |
| 1463 | ce_stage(ce), fullname.buf, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1464 | ce_stage(ce), filename); |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1465 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1466 | |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 1467 | if (file_exists(filename)) |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1468 | die("Path '%s' exists on disk, but not in the index.", filename); |
| 1469 | if (errno == ENOENT || errno == ENOTDIR) |
| 1470 | die("Path '%s' does not exist (neither on disk nor in the index).", |
| 1471 | filename); |
| 1472 | |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1473 | strbuf_release(&fullname); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1477 | static char *resolve_relative_path(const char *rel) |
| 1478 | { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1479 | if (!starts_with(rel, "./") && !starts_with(rel, "../")) |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1480 | return NULL; |
| 1481 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1482 | if (!is_inside_work_tree()) |
| 1483 | die("relative path syntax can't be used outside working tree."); |
| 1484 | |
| 1485 | /* die() inside prefix_path() if resolved path is outside worktree */ |
| 1486 | return prefix_path(startup_info->prefix, |
| 1487 | startup_info->prefix ? strlen(startup_info->prefix) : 0, |
| 1488 | rel); |
| 1489 | } |
| 1490 | |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1491 | static int get_sha1_with_context_1(const char *name, |
| 1492 | unsigned flags, |
| 1493 | const char *prefix, |
| 1494 | unsigned char *sha1, |
| 1495 | struct object_context *oc) |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1496 | { |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1497 | int ret, bracket_depth; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1498 | int namelen = strlen(name); |
| 1499 | const char *cp; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1500 | int only_to_die = flags & GET_SHA1_ONLY_TO_DIE; |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1501 | |
Jeff King | 7243ffd | 2016-09-26 07:59:15 -0400 | [diff] [blame] | 1502 | if (only_to_die) |
| 1503 | flags |= GET_SHA1_QUIETLY; |
| 1504 | |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1505 | memset(oc, 0, sizeof(*oc)); |
| 1506 | oc->mode = S_IFINVALID; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1507 | ret = get_sha1_1(name, namelen, sha1, flags); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1508 | if (!ret) |
| 1509 | return ret; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1510 | /* |
| 1511 | * sha1:path --> object name of path in ent sha1 |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1512 | * :path -> object name of absolute path in index |
| 1513 | * :./path -> object name of path relative to cwd in index |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1514 | * :[0-3]:path -> object name of path in index at stage |
Matthieu Moy | 95ad6d2 | 2010-09-24 18:43:59 +0200 | [diff] [blame] | 1515 | * :/foo -> recent commit matching foo |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1516 | */ |
| 1517 | if (name[0] == ':') { |
| 1518 | int stage = 0; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1519 | const struct cache_entry *ce; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1520 | char *new_path = NULL; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1521 | int pos; |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1522 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1523 | struct commit_list *list = NULL; |
Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 +0000 | [diff] [blame] | 1524 | |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1525 | for_each_ref(handle_one_ref, &list); |
René Scharfe | e8d1dfe | 2014-08-21 20:30:29 +0200 | [diff] [blame] | 1526 | commit_list_sort_by_date(&list); |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1527 | return get_sha1_oneline(name + 2, sha1, list); |
| 1528 | } |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1529 | if (namelen < 3 || |
| 1530 | name[2] != ':' || |
| 1531 | name[1] < '0' || '3' < name[1]) |
| 1532 | cp = name + 1; |
| 1533 | else { |
| 1534 | stage = name[1] - '0'; |
| 1535 | cp = name + 3; |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1536 | } |
Junio C Hamano | 3d6e0f7 | 2010-12-09 13:38:05 -0800 | [diff] [blame] | 1537 | new_path = resolve_relative_path(cp); |
| 1538 | if (!new_path) { |
| 1539 | namelen = namelen - (cp - name); |
| 1540 | } else { |
| 1541 | cp = new_path; |
| 1542 | namelen = strlen(cp); |
| 1543 | } |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1544 | |
René Scharfe | 2ce63e9 | 2015-02-21 20:55:22 +0100 | [diff] [blame] | 1545 | strlcpy(oc->path, cp, sizeof(oc->path)); |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1546 | |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1547 | if (!active_cache) |
| 1548 | read_cache(); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1549 | pos = cache_name_pos(cp, namelen); |
| 1550 | if (pos < 0) |
| 1551 | pos = -pos - 1; |
| 1552 | while (pos < active_nr) { |
| 1553 | ce = active_cache[pos]; |
| 1554 | if (ce_namelen(ce) != namelen || |
| 1555 | memcmp(ce->name, cp, namelen)) |
| 1556 | break; |
| 1557 | if (ce_stage(ce) == stage) { |
brian m. carlson | 99d1a98 | 2016-09-05 20:07:52 +0000 | [diff] [blame] | 1558 | hashcpy(sha1, ce->oid.hash); |
Kirill Smelkov | 9006471 | 2010-09-29 15:35:24 +0400 | [diff] [blame] | 1559 | oc->mode = ce->ce_mode; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1560 | free(new_path); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1561 | return 0; |
| 1562 | } |
Junio C Hamano | e7cef45 | 2006-05-08 15:44:06 -0700 | [diff] [blame] | 1563 | pos++; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1564 | } |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1565 | if (only_to_die && name[1] && name[1] != '/') |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1566 | diagnose_invalid_index_path(stage, prefix, cp); |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1567 | free(new_path); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1568 | return -1; |
| 1569 | } |
Shawn Pearce | cce91a2 | 2006-05-19 03:29:43 -0400 | [diff] [blame] | 1570 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
| 1571 | if (*cp == '{') |
| 1572 | bracket_depth++; |
| 1573 | else if (bracket_depth && *cp == '}') |
| 1574 | bracket_depth--; |
| 1575 | else if (!bracket_depth && *cp == ':') |
| 1576 | break; |
| 1577 | } |
| 1578 | if (*cp == ':') { |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1579 | unsigned char tree_sha1[20]; |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1580 | int len = cp - name; |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 1581 | unsigned sub_flags = flags; |
| 1582 | |
| 1583 | sub_flags &= ~GET_SHA1_DISAMBIGUATORS; |
| 1584 | sub_flags |= GET_SHA1_TREEISH; |
| 1585 | |
| 1586 | if (!get_sha1_1(name, len, tree_sha1, sub_flags)) { |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1587 | const char *filename = cp+1; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1588 | char *new_filename = NULL; |
| 1589 | |
| 1590 | new_filename = resolve_relative_path(filename); |
| 1591 | if (new_filename) |
| 1592 | filename = new_filename; |
David Turner | c4ec967 | 2015-05-20 13:03:39 -0400 | [diff] [blame] | 1593 | if (flags & GET_SHA1_FOLLOW_SYMLINKS) { |
| 1594 | ret = get_tree_entry_follow_symlinks(tree_sha1, |
| 1595 | filename, sha1, &oc->symlink_path, |
| 1596 | &oc->mode); |
| 1597 | } else { |
| 1598 | ret = get_tree_entry(tree_sha1, filename, |
| 1599 | sha1, &oc->mode); |
| 1600 | if (ret && only_to_die) { |
| 1601 | diagnose_invalid_sha1_path(prefix, |
| 1602 | filename, |
| 1603 | tree_sha1, |
| 1604 | name, len); |
| 1605 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1606 | } |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1607 | hashcpy(oc->tree, tree_sha1); |
René Scharfe | 2ce63e9 | 2015-02-21 20:55:22 +0100 | [diff] [blame] | 1608 | strlcpy(oc->path, filename, sizeof(oc->path)); |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1609 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1610 | free(new_filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1611 | return ret; |
| 1612 | } else { |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1613 | if (only_to_die) |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1614 | die("Invalid object name '%.*s'.", len, name); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1615 | } |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1616 | } |
| 1617 | return ret; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1618 | } |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 1619 | |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 1620 | /* |
| 1621 | * Call this function when you know "name" given by the end user must |
| 1622 | * name an object but it doesn't; the function _may_ die with a better |
| 1623 | * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not |
| 1624 | * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case |
| 1625 | * you have a chance to diagnose the error further. |
| 1626 | */ |
| 1627 | void maybe_die_on_misspelt_object_name(const char *name, const char *prefix) |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 1628 | { |
| 1629 | struct object_context oc; |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 1630 | unsigned char sha1[20]; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1631 | get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc); |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1634 | int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc) |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 1635 | { |
David Turner | c4ec967 | 2015-05-20 13:03:39 -0400 | [diff] [blame] | 1636 | if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE) |
| 1637 | die("BUG: incompatible flags for get_sha1_with_context"); |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1638 | return get_sha1_with_context_1(str, flags, NULL, sha1, orc); |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 1639 | } |