]>
Commit | Line | Data |
---|---|---|
9938af6a | 1 | #include "cache.h" |
5385f52d | 2 | #include "tag.h" |
9938af6a | 3 | #include "commit.h" |
5385f52d JH |
4 | #include "tree.h" |
5 | #include "blob.h" | |
f3ab49db | 6 | #include "tree-walk.h" |
d556fae2 | 7 | #include "refs.h" |
28fb8438 | 8 | #include "remote.h" |
dbe44faa | 9 | #include "dir.h" |
fad6b9e5 | 10 | #include "sha1-array.h" |
9938af6a | 11 | |
32574b68 NTND |
12 | static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *); |
13 | ||
d2b7d9c7 | 14 | typedef int (*disambiguate_hint_fn)(const struct object_id *, void *); |
a78fafe7 JH |
15 | |
16 | struct disambiguate_state { | |
0016043b | 17 | int len; /* length of prefix in hex chars */ |
dc01505f | 18 | char hex_pfx[GIT_MAX_HEXSZ + 1]; |
d2ee1185 | 19 | struct object_id bin_pfx; |
0016043b | 20 | |
a78fafe7 JH |
21 | disambiguate_hint_fn fn; |
22 | void *cb_data; | |
d2ee1185 | 23 | struct object_id candidate; |
a78fafe7 JH |
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; | |
957d7406 | 29 | unsigned always_call_fn:1; |
a78fafe7 JH |
30 | }; |
31 | ||
d2b7d9c7 | 32 | static void update_candidates(struct disambiguate_state *ds, const struct object_id *current) |
a78fafe7 | 33 | { |
957d7406 JH |
34 | if (ds->always_call_fn) { |
35 | ds->ambiguous = ds->fn(current, ds->cb_data) ? 1 : 0; | |
36 | return; | |
37 | } | |
a78fafe7 JH |
38 | if (!ds->candidate_exists) { |
39 | /* this is the first candidate */ | |
d2b7d9c7 | 40 | oidcpy(&ds->candidate, current); |
a78fafe7 JH |
41 | ds->candidate_exists = 1; |
42 | return; | |
d2b7d9c7 | 43 | } else if (!oidcmp(&ds->candidate, current)) { |
a78fafe7 JH |
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) { | |
d2b7d9c7 | 55 | ds->candidate_ok = ds->fn(&ds->candidate, ds->cb_data); |
a78fafe7 JH |
56 | ds->disambiguate_fn_used = 1; |
57 | ds->candidate_checked = 1; | |
58 | } | |
59 | ||
60 | if (!ds->candidate_ok) { | |
749f763d | 61 | /* discard the candidate; we know it does not satisfy fn */ |
d2b7d9c7 | 62 | oidcpy(&ds->candidate, current); |
a78fafe7 JH |
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 | ||
0016043b | 80 | static void find_short_object_filename(struct disambiguate_state *ds) |
9938af6a | 81 | { |
99a19b43 | 82 | struct alternate_object_database *alt; |
dc01505f | 83 | char hex[GIT_MAX_HEXSZ]; |
99a19b43 JH |
84 | static struct alternate_object_database *fakeent; |
85 | ||
86 | if (!fakeent) { | |
274ac009 JH |
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 | */ | |
7f0fa2c0 | 94 | fakeent = alloc_alt_odb(get_object_directory()); |
99a19b43 JH |
95 | } |
96 | fakeent->next = alt_odb_list; | |
9938af6a | 97 | |
0016043b | 98 | xsnprintf(hex, sizeof(hex), "%.2s", ds->hex_pfx); |
a78fafe7 | 99 | for (alt = fakeent; alt && !ds->ambiguous; alt = alt->next) { |
38dbe5f0 | 100 | struct strbuf *buf = alt_scratch_buf(alt); |
9938af6a | 101 | struct dirent *de; |
99a19b43 | 102 | DIR *dir; |
597f9134 | 103 | |
dec04019 | 104 | strbuf_addf(buf, "%.2s/", ds->hex_pfx); |
38dbe5f0 | 105 | dir = opendir(buf->buf); |
99a19b43 JH |
106 | if (!dir) |
107 | continue; | |
a78fafe7 JH |
108 | |
109 | while (!ds->ambiguous && (de = readdir(dir)) != NULL) { | |
d2b7d9c7 | 110 | struct object_id oid; |
a78fafe7 | 111 | |
d2b7d9c7 | 112 | if (strlen(de->d_name) != GIT_SHA1_HEXSZ - 2) |
9938af6a | 113 | continue; |
0016043b | 114 | if (memcmp(de->d_name, ds->hex_pfx + 2, ds->len - 2)) |
9938af6a | 115 | continue; |
d2b7d9c7 | 116 | memcpy(hex + 2, de->d_name, GIT_SHA1_HEXSZ - 2); |
117 | if (!get_oid_hex(hex, &oid)) | |
118 | update_candidates(ds, &oid); | |
9938af6a JH |
119 | } |
120 | closedir(dir); | |
121 | } | |
9938af6a JH |
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 | ||
0016043b | 139 | static void unique_in_pack(struct packed_git *p, |
a78fafe7 | 140 | struct disambiguate_state *ds) |
9938af6a | 141 | { |
f703e6ea | 142 | uint32_t num, last, i, first = 0; |
d2b7d9c7 | 143 | const struct object_id *current = NULL; |
f703e6ea JH |
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); | |
d2ee1185 | 154 | cmp = hashcmp(ds->bin_pfx.hash, current); |
f703e6ea JH |
155 | if (!cmp) { |
156 | first = mid; | |
157 | break; | |
9938af6a | 158 | } |
f703e6ea JH |
159 | if (cmp > 0) { |
160 | first = mid+1; | |
161 | continue; | |
9938af6a | 162 | } |
f703e6ea JH |
163 | last = mid; |
164 | } | |
165 | ||
166 | /* | |
167 | * At this point, "first" is the location of the lowest object | |
1703f9aa | 168 | * with an object name that could match "bin_pfx". See if we have |
f703e6ea JH |
169 | * 0, 1 or more objects that actually match(es). |
170 | */ | |
a78fafe7 | 171 | for (i = first; i < num && !ds->ambiguous; i++) { |
d2b7d9c7 | 172 | struct object_id oid; |
173 | current = nth_packed_object_oid(&oid, p, i); | |
174 | if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash)) | |
f703e6ea | 175 | break; |
a78fafe7 | 176 | update_candidates(ds, current); |
9938af6a | 177 | } |
f703e6ea JH |
178 | } |
179 | ||
0016043b | 180 | static void find_short_packed_object(struct disambiguate_state *ds) |
9938af6a JH |
181 | { |
182 | struct packed_git *p; | |
183 | ||
184 | prepare_packed_git(); | |
a78fafe7 | 185 | for (p = packed_git; p && !ds->ambiguous; p = p->next) |
0016043b | 186 | unique_in_pack(p, ds); |
99a19b43 JH |
187 | } |
188 | ||
013f276e JH |
189 | #define SHORT_NAME_NOT_FOUND (-1) |
190 | #define SHORT_NAME_AMBIGUOUS (-2) | |
191 | ||
a78fafe7 JH |
192 | static int finish_object_disambiguation(struct disambiguate_state *ds, |
193 | unsigned char *sha1) | |
99a19b43 | 194 | { |
a78fafe7 JH |
195 | if (ds->ambiguous) |
196 | return SHORT_NAME_AMBIGUOUS; | |
99a19b43 | 197 | |
a78fafe7 | 198 | if (!ds->candidate_exists) |
013f276e | 199 | return SHORT_NAME_NOT_FOUND; |
a78fafe7 JH |
200 | |
201 | if (!ds->candidate_checked) | |
202 | /* | |
203 | * If this is the only candidate, there is no point | |
204 | * calling the disambiguation hint callback. | |
205 | * | |
206 | * On the other hand, if the current candidate | |
207 | * replaced an earlier candidate that did _not_ pass | |
208 | * the disambiguation hint callback, then we do have | |
209 | * more than one objects that match the short name | |
210 | * given, so we should make sure this one matches; | |
211 | * otherwise, if we discovered this one and the one | |
212 | * that we previously discarded in the reverse order, | |
213 | * we would end up showing different results in the | |
214 | * same repository! | |
215 | */ | |
216 | ds->candidate_ok = (!ds->disambiguate_fn_used || | |
d2b7d9c7 | 217 | ds->fn(&ds->candidate, ds->cb_data)); |
a78fafe7 JH |
218 | |
219 | if (!ds->candidate_ok) | |
013f276e | 220 | return SHORT_NAME_AMBIGUOUS; |
a78fafe7 | 221 | |
d2ee1185 | 222 | hashcpy(sha1, ds->candidate.hash); |
9938af6a JH |
223 | return 0; |
224 | } | |
225 | ||
d2b7d9c7 | 226 | static int disambiguate_commit_only(const struct object_id *oid, void *cb_data_unused) |
aa1dec9e | 227 | { |
d2b7d9c7 | 228 | int kind = sha1_object_info(oid->hash, NULL); |
aa1dec9e JH |
229 | return kind == OBJ_COMMIT; |
230 | } | |
231 | ||
d2b7d9c7 | 232 | static int disambiguate_committish_only(const struct object_id *oid, void *cb_data_unused) |
e2643617 JH |
233 | { |
234 | struct object *obj; | |
235 | int kind; | |
236 | ||
d2b7d9c7 | 237 | kind = sha1_object_info(oid->hash, NULL); |
e2643617 JH |
238 | if (kind == OBJ_COMMIT) |
239 | return 1; | |
240 | if (kind != OBJ_TAG) | |
99a19b43 | 241 | return 0; |
e2643617 JH |
242 | |
243 | /* We need to do this the hard way... */ | |
c251c83d | 244 | obj = deref_tag(parse_object(oid), NULL, 0); |
e2643617 JH |
245 | if (obj && obj->type == OBJ_COMMIT) |
246 | return 1; | |
9938af6a JH |
247 | return 0; |
248 | } | |
249 | ||
d2b7d9c7 | 250 | static int disambiguate_tree_only(const struct object_id *oid, void *cb_data_unused) |
9938af6a | 251 | { |
d2b7d9c7 | 252 | int kind = sha1_object_info(oid->hash, NULL); |
daba53ae JH |
253 | return kind == OBJ_TREE; |
254 | } | |
9938af6a | 255 | |
d2b7d9c7 | 256 | static int disambiguate_treeish_only(const struct object_id *oid, void *cb_data_unused) |
daba53ae JH |
257 | { |
258 | struct object *obj; | |
259 | int kind; | |
260 | ||
d2b7d9c7 | 261 | kind = sha1_object_info(oid->hash, NULL); |
daba53ae JH |
262 | if (kind == OBJ_TREE || kind == OBJ_COMMIT) |
263 | return 1; | |
264 | if (kind != OBJ_TAG) | |
265 | return 0; | |
266 | ||
267 | /* We need to do this the hard way... */ | |
c251c83d | 268 | obj = deref_tag(parse_object(oid), NULL, 0); |
daba53ae JH |
269 | if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT)) |
270 | return 1; | |
271 | return 0; | |
272 | } | |
273 | ||
d2b7d9c7 | 274 | static int disambiguate_blob_only(const struct object_id *oid, void *cb_data_unused) |
daba53ae | 275 | { |
d2b7d9c7 | 276 | int kind = sha1_object_info(oid->hash, NULL); |
daba53ae JH |
277 | return kind == OBJ_BLOB; |
278 | } | |
279 | ||
5b33cb1f JK |
280 | static disambiguate_hint_fn default_disambiguate_hint; |
281 | ||
282 | int set_disambiguate_hint_config(const char *var, const char *value) | |
283 | { | |
284 | static const struct { | |
285 | const char *name; | |
286 | disambiguate_hint_fn fn; | |
287 | } hints[] = { | |
288 | { "none", NULL }, | |
289 | { "commit", disambiguate_commit_only }, | |
290 | { "committish", disambiguate_committish_only }, | |
291 | { "tree", disambiguate_tree_only }, | |
292 | { "treeish", disambiguate_treeish_only }, | |
293 | { "blob", disambiguate_blob_only } | |
294 | }; | |
295 | int i; | |
296 | ||
297 | if (!value) | |
298 | return config_error_nonbool(var); | |
299 | ||
300 | for (i = 0; i < ARRAY_SIZE(hints); i++) { | |
301 | if (!strcasecmp(value, hints[i].name)) { | |
302 | default_disambiguate_hint = hints[i].fn; | |
303 | return 0; | |
304 | } | |
305 | } | |
306 | ||
307 | return error("unknown hint type for '%s': %s", var, value); | |
308 | } | |
309 | ||
0016043b JK |
310 | static int init_object_disambiguation(const char *name, int len, |
311 | struct disambiguate_state *ds) | |
9938af6a | 312 | { |
957d7406 | 313 | int i; |
9938af6a | 314 | |
0016043b JK |
315 | if (len < MINIMUM_ABBREV || len > GIT_SHA1_HEXSZ) |
316 | return -1; | |
317 | ||
318 | memset(ds, 0, sizeof(*ds)); | |
0016043b | 319 | |
af61c6e0 | 320 | for (i = 0; i < len ;i++) { |
9938af6a JH |
321 | unsigned char c = name[i]; |
322 | unsigned char val; | |
9938af6a JH |
323 | if (c >= '0' && c <= '9') |
324 | val = c - '0'; | |
325 | else if (c >= 'a' && c <= 'f') | |
326 | val = c - 'a' + 10; | |
327 | else if (c >= 'A' && c <='F') { | |
328 | val = c - 'A' + 10; | |
329 | c -= 'A' - 'a'; | |
330 | } | |
331 | else | |
332 | return -1; | |
0016043b | 333 | ds->hex_pfx[i] = c; |
9938af6a JH |
334 | if (!(i & 1)) |
335 | val <<= 4; | |
d2ee1185 | 336 | ds->bin_pfx.hash[i >> 1] |= val; |
9938af6a | 337 | } |
0016043b JK |
338 | |
339 | ds->len = len; | |
59e4e34f | 340 | ds->hex_pfx[len] = '\0'; |
0016043b | 341 | prepare_alt_odb(); |
957d7406 JH |
342 | return 0; |
343 | } | |
344 | ||
1b7ba794 | 345 | static int show_ambiguous_object(const struct object_id *oid, void *data) |
1ffa26c4 JK |
346 | { |
347 | const struct disambiguate_state *ds = data; | |
348 | struct strbuf desc = STRBUF_INIT; | |
349 | int type; | |
350 | ||
d2b7d9c7 | 351 | |
1b7ba794 | 352 | if (ds->fn && !ds->fn(oid, ds->cb_data)) |
1ffa26c4 JK |
353 | return 0; |
354 | ||
1b7ba794 | 355 | type = sha1_object_info(oid->hash, NULL); |
1ffa26c4 | 356 | if (type == OBJ_COMMIT) { |
bc83266a | 357 | struct commit *commit = lookup_commit(oid); |
1ffa26c4 JK |
358 | if (commit) { |
359 | struct pretty_print_context pp = {0}; | |
360 | pp.date_mode.type = DATE_SHORT; | |
361 | format_commit_message(commit, " %ad - %s", &desc, &pp); | |
362 | } | |
363 | } else if (type == OBJ_TAG) { | |
d3101b53 | 364 | struct tag *tag = lookup_tag(oid); |
1ffa26c4 JK |
365 | if (!parse_tag(tag) && tag->tag) |
366 | strbuf_addf(&desc, " %s", tag->tag); | |
367 | } | |
368 | ||
369 | advise(" %s %s%s", | |
1b7ba794 | 370 | find_unique_abbrev(oid->hash, DEFAULT_ABBREV), |
1ffa26c4 JK |
371 | typename(type) ? typename(type) : "unknown type", |
372 | desc.buf); | |
373 | ||
374 | strbuf_release(&desc); | |
375 | return 0; | |
376 | } | |
377 | ||
957d7406 JH |
378 | static int get_short_sha1(const char *name, int len, unsigned char *sha1, |
379 | unsigned flags) | |
380 | { | |
381 | int status; | |
957d7406 JH |
382 | struct disambiguate_state ds; |
383 | int quietly = !!(flags & GET_SHA1_QUIETLY); | |
384 | ||
0016043b | 385 | if (init_object_disambiguation(name, len, &ds) < 0) |
957d7406 | 386 | return -1; |
99a19b43 | 387 | |
259942f5 JK |
388 | if (HAS_MULTI_BITS(flags & GET_SHA1_DISAMBIGUATORS)) |
389 | die("BUG: multiple get_short_sha1 disambiguator flags"); | |
390 | ||
aa1dec9e JH |
391 | if (flags & GET_SHA1_COMMIT) |
392 | ds.fn = disambiguate_commit_only; | |
e2643617 JH |
393 | else if (flags & GET_SHA1_COMMITTISH) |
394 | ds.fn = disambiguate_committish_only; | |
daba53ae JH |
395 | else if (flags & GET_SHA1_TREE) |
396 | ds.fn = disambiguate_tree_only; | |
397 | else if (flags & GET_SHA1_TREEISH) | |
398 | ds.fn = disambiguate_treeish_only; | |
399 | else if (flags & GET_SHA1_BLOB) | |
400 | ds.fn = disambiguate_blob_only; | |
5b33cb1f JK |
401 | else |
402 | ds.fn = default_disambiguate_hint; | |
aa1dec9e | 403 | |
0016043b JK |
404 | find_short_object_filename(&ds); |
405 | find_short_packed_object(&ds); | |
a78fafe7 | 406 | status = finish_object_disambiguation(&ds, sha1); |
99a19b43 | 407 | |
1ffa26c4 JK |
408 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) { |
409 | error(_("short SHA1 %s is ambiguous"), ds.hex_pfx); | |
410 | ||
411 | /* | |
412 | * We may still have ambiguity if we simply saw a series of | |
413 | * candidates that did not satisfy our hint function. In | |
414 | * that case, we still want to show them, so disable the hint | |
415 | * function entirely. | |
416 | */ | |
417 | if (!ds.ambiguous) | |
418 | ds.fn = NULL; | |
419 | ||
420 | advise(_("The candidates are:")); | |
421 | for_each_abbrev(ds.hex_pfx, show_ambiguous_object, &ds); | |
422 | } | |
423 | ||
013f276e JH |
424 | return status; |
425 | } | |
426 | ||
d2b7d9c7 | 427 | static int collect_ambiguous(const struct object_id *oid, void *data) |
fad6b9e5 | 428 | { |
910650d2 | 429 | oid_array_append(data, oid); |
fad6b9e5 JK |
430 | return 0; |
431 | } | |
432 | ||
957d7406 JH |
433 | int for_each_abbrev(const char *prefix, each_abbrev_fn fn, void *cb_data) |
434 | { | |
910650d2 | 435 | struct oid_array collect = OID_ARRAY_INIT; |
957d7406 | 436 | struct disambiguate_state ds; |
fad6b9e5 | 437 | int ret; |
957d7406 | 438 | |
0016043b | 439 | if (init_object_disambiguation(prefix, strlen(prefix), &ds) < 0) |
957d7406 | 440 | return -1; |
957d7406 | 441 | |
957d7406 | 442 | ds.always_call_fn = 1; |
fad6b9e5 JK |
443 | ds.fn = collect_ambiguous; |
444 | ds.cb_data = &collect; | |
0016043b JK |
445 | find_short_object_filename(&ds); |
446 | find_short_packed_object(&ds); | |
fad6b9e5 | 447 | |
910650d2 | 448 | ret = oid_array_for_each_unique(&collect, fn, cb_data); |
449 | oid_array_clear(&collect); | |
fad6b9e5 | 450 | return ret; |
957d7406 JH |
451 | } |
452 | ||
8e3f52d7 JK |
453 | /* |
454 | * Return the slot of the most-significant bit set in "val". There are various | |
455 | * ways to do this quickly with fls() or __builtin_clzl(), but speed is | |
456 | * probably not a big deal here. | |
457 | */ | |
458 | static unsigned msb(unsigned long val) | |
459 | { | |
460 | unsigned r = 0; | |
461 | while (val >>= 1) | |
462 | r++; | |
463 | return r; | |
464 | } | |
465 | ||
af49c6d0 | 466 | int find_unique_abbrev_r(char *hex, const unsigned char *sha1, int len) |
013f276e | 467 | { |
b66fde9a | 468 | int status, exists; |
47dd0d59 | 469 | |
e6c587c7 | 470 | if (len < 0) { |
8e3f52d7 JK |
471 | unsigned long count = approximate_object_count(); |
472 | /* | |
473 | * Add one because the MSB only tells us the highest bit set, | |
474 | * not including the value of all the _other_ bits (so "15" | |
475 | * is only one off of 2^4, but the MSB is the 3rd bit. | |
476 | */ | |
477 | len = msb(count) + 1; | |
478 | /* | |
479 | * We now know we have on the order of 2^len objects, which | |
480 | * expects a collision at 2^(len/2). But we also care about hex | |
481 | * chars, not bits, and there are 4 bits per hex. So all | |
482 | * together we need to divide by 2; but we also want to round | |
483 | * odd numbers up, hence adding one before dividing. | |
484 | */ | |
485 | len = (len + 1) / 2; | |
486 | /* | |
487 | * For very small repos, we stick with our regular fallback. | |
488 | */ | |
489 | if (len < FALLBACK_DEFAULT_ABBREV) | |
490 | len = FALLBACK_DEFAULT_ABBREV; | |
e6c587c7 | 491 | } |
8e3f52d7 | 492 | |
af49c6d0 | 493 | sha1_to_hex_r(hex, sha1); |
02c5cba2 | 494 | if (len == 40 || !len) |
af49c6d0 | 495 | return 40; |
61e704e3 | 496 | exists = has_sha1_file(sha1); |
013f276e JH |
497 | while (len < 40) { |
498 | unsigned char sha1_ret[20]; | |
8e3f52d7 | 499 | status = get_short_sha1(hex, len, sha1_ret, GET_SHA1_QUIETLY); |
b66fde9a JH |
500 | if (exists |
501 | ? !status | |
502 | : status == SHORT_NAME_NOT_FOUND) { | |
ea2c69ed | 503 | hex[len] = 0; |
af49c6d0 | 504 | return len; |
013f276e | 505 | } |
013f276e JH |
506 | len++; |
507 | } | |
af49c6d0 JK |
508 | return len; |
509 | } | |
510 | ||
511 | const char *find_unique_abbrev(const unsigned char *sha1, int len) | |
512 | { | |
ef2ed501 | 513 | static int bufno; |
dc01505f | 514 | static char hexbuffer[4][GIT_MAX_HEXSZ + 1]; |
3e98919a RS |
515 | char *hex = hexbuffer[bufno]; |
516 | bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); | |
af49c6d0 | 517 | find_unique_abbrev_r(hex, sha1, len); |
b66fde9a | 518 | return hex; |
9938af6a JH |
519 | } |
520 | ||
6677c466 | 521 | static int ambiguous_path(const char *path, int len) |
af13cdf2 LT |
522 | { |
523 | int slash = 1; | |
6677c466 | 524 | int cnt; |
af13cdf2 | 525 | |
6677c466 | 526 | for (cnt = 0; cnt < len; cnt++) { |
af13cdf2 LT |
527 | switch (*path++) { |
528 | case '\0': | |
529 | break; | |
530 | case '/': | |
531 | if (slash) | |
532 | break; | |
533 | slash = 1; | |
534 | continue; | |
535 | case '.': | |
536 | continue; | |
537 | default: | |
538 | slash = 0; | |
539 | continue; | |
540 | } | |
c054d64e | 541 | break; |
af13cdf2 | 542 | } |
6677c466 | 543 | return slash; |
af13cdf2 LT |
544 | } |
545 | ||
a1ad0eb0 JK |
546 | static inline int at_mark(const char *string, int len, |
547 | const char **suffix, int nr) | |
ae0ba8e2 | 548 | { |
ae0ba8e2 JH |
549 | int i; |
550 | ||
a1ad0eb0 | 551 | for (i = 0; i < nr; i++) { |
ae0ba8e2 JH |
552 | int suffix_len = strlen(suffix[i]); |
553 | if (suffix_len <= len | |
244ea1b5 | 554 | && !strncasecmp(string, suffix[i], suffix_len)) |
ae0ba8e2 JH |
555 | return suffix_len; |
556 | } | |
557 | return 0; | |
558 | } | |
559 | ||
a1ad0eb0 JK |
560 | static inline int upstream_mark(const char *string, int len) |
561 | { | |
562 | const char *suffix[] = { "@{upstream}", "@{u}" }; | |
563 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); | |
564 | } | |
565 | ||
adfe5d04 JK |
566 | static inline int push_mark(const char *string, int len) |
567 | { | |
568 | const char *suffix[] = { "@{push}" }; | |
569 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); | |
570 | } | |
571 | ||
e48ba200 | 572 | static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags); |
8cd4249c | 573 | static int interpret_nth_prior_checkout(const char *name, int namelen, struct strbuf *buf); |
d18ba221 | 574 | |
c41a87dd DA |
575 | static int get_sha1_basic(const char *str, int len, unsigned char *sha1, |
576 | unsigned int flags) | |
e86eb666 | 577 | { |
eedce784 | 578 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
798c35fc NTND |
579 | static const char *object_name_msg = N_( |
580 | "Git normally never creates a ref that ends with 40 hex characters\n" | |
581 | "because it will be ignored when you just specify 40-hex. These refs\n" | |
582 | "may be created by mistake. For example,\n" | |
583 | "\n" | |
584 | " git checkout -b $br $(git rev-parse ...)\n" | |
585 | "\n" | |
586 | "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n" | |
587 | "examine these refs and maybe delete them. Turn this message off by\n" | |
8dc84fdc | 588 | "running \"git config advice.objectNameWarning false\""); |
798c35fc | 589 | unsigned char tmp_sha1[20]; |
ed378ec7 | 590 | char *real_ref = NULL; |
ab2a1a32 | 591 | int refs_found = 0; |
128fd54d | 592 | int at, reflog_len, nth_prior = 0; |
9938af6a | 593 | |
798c35fc | 594 | if (len == 40 && !get_sha1_hex(str, sha1)) { |
832cf74c | 595 | if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { |
25fba78d | 596 | refs_found = dwim_ref(str, len, tmp_sha1, &real_ref); |
832cf74c | 597 | if (refs_found > 0) { |
25fba78d JK |
598 | warning(warn_msg, len, str); |
599 | if (advice_object_name_warning) | |
600 | fprintf(stderr, "%s\n", _(object_name_msg)); | |
601 | } | |
602 | free(real_ref); | |
798c35fc | 603 | } |
9938af6a | 604 | return 0; |
798c35fc | 605 | } |
9938af6a | 606 | |
d18ba221 | 607 | /* basic@{time or number or -number} format to query ref-log */ |
694500ed | 608 | reflog_len = at = 0; |
f265458f | 609 | if (len && str[len-1] == '}') { |
e883a057 | 610 | for (at = len-4; at >= 0; at--) { |
ab2a1a32 | 611 | if (str[at] == '@' && str[at+1] == '{') { |
83d16bc7 RR |
612 | if (str[at+2] == '-') { |
613 | if (at != 0) | |
614 | /* @{-N} not at start */ | |
615 | return -1; | |
128fd54d FC |
616 | nth_prior = 1; |
617 | continue; | |
618 | } | |
adfe5d04 JK |
619 | if (!upstream_mark(str + at, len - at) && |
620 | !push_mark(str + at, len - at)) { | |
28fb8438 JS |
621 | reflog_len = (len-1) - (at+2); |
622 | len = at; | |
623 | } | |
ab2a1a32 JH |
624 | break; |
625 | } | |
d556fae2 SP |
626 | } |
627 | } | |
628 | ||
af13cdf2 | 629 | /* Accept only unambiguous ref paths. */ |
11cf8801 | 630 | if (len && ambiguous_path(str, len)) |
af13cdf2 LT |
631 | return -1; |
632 | ||
128fd54d | 633 | if (nth_prior) { |
d18ba221 | 634 | struct strbuf buf = STRBUF_INIT; |
128fd54d FC |
635 | int detached; |
636 | ||
8cd4249c | 637 | if (interpret_nth_prior_checkout(str, len, &buf) > 0) { |
128fd54d FC |
638 | detached = (buf.len == 40 && !get_sha1_hex(buf.buf, sha1)); |
639 | strbuf_release(&buf); | |
640 | if (detached) | |
641 | return 0; | |
d18ba221 | 642 | } |
128fd54d FC |
643 | } |
644 | ||
645 | if (!len && reflog_len) | |
11cf8801 NP |
646 | /* allow "@{...}" to mean the current branch reflog */ |
647 | refs_found = dwim_ref("HEAD", 4, sha1, &real_ref); | |
128fd54d | 648 | else if (reflog_len) |
f2eba66d NP |
649 | refs_found = dwim_log(str, len, sha1, &real_ref); |
650 | else | |
11cf8801 | 651 | refs_found = dwim_ref(str, len, sha1, &real_ref); |
d556fae2 SP |
652 | |
653 | if (!refs_found) | |
654 | return -1; | |
655 | ||
c41a87dd | 656 | if (warn_ambiguous_refs && !(flags & GET_SHA1_QUIETLY) && |
798c35fc NTND |
657 | (refs_found > 1 || |
658 | !get_short_sha1(str, len, tmp_sha1, GET_SHA1_QUIETLY))) | |
eedce784 | 659 | warning(warn_msg, len, str); |
d556fae2 | 660 | |
ab2a1a32 | 661 | if (reflog_len) { |
ab2a1a32 | 662 | int nth, i; |
dddbad72 JS |
663 | timestamp_t at_time; |
664 | timestamp_t co_time; | |
16d7cc90 JH |
665 | int co_tz, co_cnt; |
666 | ||
fe558516 | 667 | /* Is it asking for N-th entry, or approxidate? */ |
ab2a1a32 JH |
668 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
669 | char ch = str[at+2+i]; | |
670 | if ('0' <= ch && ch <= '9') | |
671 | nth = nth * 10 + ch - '0'; | |
672 | else | |
673 | nth = -1; | |
674 | } | |
ea360dd0 SP |
675 | if (100000000 <= nth) { |
676 | at_time = nth; | |
677 | nth = -1; | |
678 | } else if (0 <= nth) | |
ab2a1a32 | 679 | at_time = 0; |
861f00e3 | 680 | else { |
93cfa7c7 | 681 | int errors = 0; |
861f00e3 | 682 | char *tmp = xstrndup(str + at + 2, reflog_len); |
93cfa7c7 | 683 | at_time = approxidate_careful(tmp, &errors); |
861f00e3 | 684 | free(tmp); |
28b35632 JK |
685 | if (errors) { |
686 | free(real_ref); | |
a5e10acb | 687 | return -1; |
28b35632 | 688 | } |
861f00e3 | 689 | } |
c41a87dd | 690 | if (read_ref_at(real_ref, flags, at_time, nth, sha1, NULL, |
16d7cc90 | 691 | &co_time, &co_tz, &co_cnt)) { |
305ebea0 | 692 | if (!len) { |
59556548 | 693 | if (starts_with(real_ref, "refs/heads/")) { |
305ebea0 RR |
694 | str = real_ref + 11; |
695 | len = strlen(real_ref + 11); | |
696 | } else { | |
697 | /* detached HEAD */ | |
698 | str = "HEAD"; | |
699 | len = 4; | |
700 | } | |
701 | } | |
c41a87dd DA |
702 | if (at_time) { |
703 | if (!(flags & GET_SHA1_QUIETLY)) { | |
704 | warning("Log for '%.*s' only goes " | |
705 | "back to %s.", len, str, | |
a5481a6c | 706 | show_date(co_time, co_tz, DATE_MODE(RFC2822))); |
c41a87dd DA |
707 | } |
708 | } else { | |
709 | if (flags & GET_SHA1_QUIETLY) { | |
710 | exit(128); | |
711 | } | |
e6eedc31 JS |
712 | die("Log for '%.*s' only has %d entries.", |
713 | len, str, co_cnt); | |
714 | } | |
16d7cc90 | 715 | } |
d556fae2 SP |
716 | } |
717 | ||
ed378ec7 | 718 | free(real_ref); |
d556fae2 | 719 | return 0; |
9938af6a JH |
720 | } |
721 | ||
9938af6a JH |
722 | static int get_parent(const char *name, int len, |
723 | unsigned char *result, int idx) | |
724 | { | |
1e43ed98 | 725 | struct object_id oid; |
726 | int ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH); | |
9938af6a JH |
727 | struct commit *commit; |
728 | struct commit_list *p; | |
729 | ||
730 | if (ret) | |
731 | return ret; | |
bc83266a | 732 | commit = lookup_commit_reference(&oid); |
9938af6a JH |
733 | if (parse_commit(commit)) |
734 | return -1; | |
735 | if (!idx) { | |
ed1c9977 | 736 | hashcpy(result, commit->object.oid.hash); |
9938af6a JH |
737 | return 0; |
738 | } | |
739 | p = commit->parents; | |
740 | while (p) { | |
741 | if (!--idx) { | |
ed1c9977 | 742 | hashcpy(result, p->item->object.oid.hash); |
9938af6a JH |
743 | return 0; |
744 | } | |
745 | p = p->next; | |
746 | } | |
747 | return -1; | |
748 | } | |
749 | ||
4f7599ac JH |
750 | static int get_nth_ancestor(const char *name, int len, |
751 | unsigned char *result, int generation) | |
752 | { | |
1e43ed98 | 753 | struct object_id oid; |
621ff675 LT |
754 | struct commit *commit; |
755 | int ret; | |
756 | ||
1e43ed98 | 757 | ret = get_sha1_1(name, len, oid.hash, GET_SHA1_COMMITTISH); |
4f7599ac JH |
758 | if (ret) |
759 | return ret; | |
bc83266a | 760 | commit = lookup_commit_reference(&oid); |
621ff675 LT |
761 | if (!commit) |
762 | return -1; | |
4f7599ac JH |
763 | |
764 | while (generation--) { | |
621ff675 | 765 | if (parse_commit(commit) || !commit->parents) |
4f7599ac | 766 | return -1; |
621ff675 | 767 | commit = commit->parents->item; |
4f7599ac | 768 | } |
ed1c9977 | 769 | hashcpy(result, commit->object.oid.hash); |
4f7599ac JH |
770 | return 0; |
771 | } | |
772 | ||
81776315 JH |
773 | struct object *peel_to_type(const char *name, int namelen, |
774 | struct object *o, enum object_type expected_type) | |
775 | { | |
776 | if (name && !namelen) | |
777 | namelen = strlen(name); | |
81776315 | 778 | while (1) { |
c251c83d | 779 | if (!o || (!o->parsed && !parse_object(&o->oid))) |
81776315 | 780 | return NULL; |
a6a3f2cc | 781 | if (expected_type == OBJ_ANY || o->type == expected_type) |
81776315 JH |
782 | return o; |
783 | if (o->type == OBJ_TAG) | |
784 | o = ((struct tag*) o)->tagged; | |
785 | else if (o->type == OBJ_COMMIT) | |
786 | o = &(((struct commit *) o)->tree->object); | |
787 | else { | |
788 | if (name) | |
789 | error("%.*s: expected %s type, but the object " | |
790 | "dereferences to %s type", | |
791 | namelen, name, typename(expected_type), | |
792 | typename(o->type)); | |
793 | return NULL; | |
794 | } | |
795 | } | |
796 | } | |
797 | ||
8a10fea4 JK |
798 | static int peel_onion(const char *name, int len, unsigned char *sha1, |
799 | unsigned lookup_flags) | |
5385f52d | 800 | { |
de37d50d | 801 | struct object_id outer; |
5385f52d | 802 | const char *sp; |
885a86ab | 803 | unsigned int expected_type = 0; |
5385f52d JH |
804 | struct object *o; |
805 | ||
806 | /* | |
807 | * "ref^{type}" dereferences ref repeatedly until you cannot | |
808 | * dereference anymore, or you get an object of given type, | |
809 | * whichever comes first. "ref^{}" means just dereference | |
810 | * tags until you get a non-tag. "ref^0" is a shorthand for | |
811 | * "ref^{commit}". "commit^{tree}" could be used to find the | |
812 | * top-level tree of the given commit. | |
813 | */ | |
814 | if (len < 4 || name[len-1] != '}') | |
815 | return -1; | |
816 | ||
817 | for (sp = name + len - 1; name <= sp; sp--) { | |
818 | int ch = *sp; | |
819 | if (ch == '{' && name < sp && sp[-1] == '^') | |
820 | break; | |
821 | } | |
822 | if (sp <= name) | |
823 | return -1; | |
824 | ||
825 | sp++; /* beginning of type name, or closing brace for empty */ | |
59556548 | 826 | if (starts_with(sp, "commit}")) |
1974632c | 827 | expected_type = OBJ_COMMIT; |
59556548 | 828 | else if (starts_with(sp, "tag}")) |
75aa26d3 | 829 | expected_type = OBJ_TAG; |
59556548 | 830 | else if (starts_with(sp, "tree}")) |
1974632c | 831 | expected_type = OBJ_TREE; |
59556548 | 832 | else if (starts_with(sp, "blob}")) |
1974632c | 833 | expected_type = OBJ_BLOB; |
59556548 | 834 | else if (starts_with(sp, "object}")) |
a6a3f2cc | 835 | expected_type = OBJ_ANY; |
5385f52d | 836 | else if (sp[0] == '}') |
1974632c | 837 | expected_type = OBJ_NONE; |
32574b68 NTND |
838 | else if (sp[0] == '/') |
839 | expected_type = OBJ_COMMIT; | |
5385f52d JH |
840 | else |
841 | return -1; | |
842 | ||
8a10fea4 | 843 | lookup_flags &= ~GET_SHA1_DISAMBIGUATORS; |
e2643617 | 844 | if (expected_type == OBJ_COMMIT) |
8a10fea4 | 845 | lookup_flags |= GET_SHA1_COMMITTISH; |
ed1ca602 | 846 | else if (expected_type == OBJ_TREE) |
8a10fea4 | 847 | lookup_flags |= GET_SHA1_TREEISH; |
e2643617 | 848 | |
de37d50d | 849 | if (get_sha1_1(name, sp - name - 2, outer.hash, lookup_flags)) |
5385f52d JH |
850 | return -1; |
851 | ||
c251c83d | 852 | o = parse_object(&outer); |
5385f52d JH |
853 | if (!o) |
854 | return -1; | |
885a86ab | 855 | if (!expected_type) { |
9534f40b | 856 | o = deref_tag(o, name, sp - name - 2); |
c251c83d | 857 | if (!o || (!o->parsed && !parse_object(&o->oid))) |
6e1c6c10 | 858 | return -1; |
f2fd0760 | 859 | hashcpy(sha1, o->oid.hash); |
32574b68 | 860 | return 0; |
5385f52d | 861 | } |
32574b68 NTND |
862 | |
863 | /* | |
864 | * At this point, the syntax look correct, so | |
865 | * if we do not get the needed object, we should | |
866 | * barf. | |
867 | */ | |
868 | o = peel_to_type(name, len, o, expected_type); | |
869 | if (!o) | |
81776315 | 870 | return -1; |
32574b68 | 871 | |
ed1c9977 | 872 | hashcpy(sha1, o->oid.hash); |
32574b68 NTND |
873 | if (sp[0] == '/') { |
874 | /* "$commit^{/foo}" */ | |
875 | char *prefix; | |
876 | int ret; | |
877 | struct commit_list *list = NULL; | |
878 | ||
81776315 | 879 | /* |
4322842a NTND |
880 | * $commit^{/}. Some regex implementation may reject. |
881 | * We don't need regex anyway. '' pattern always matches. | |
5385f52d | 882 | */ |
4322842a | 883 | if (sp[1] == '}') |
81776315 | 884 | return 0; |
4322842a | 885 | |
32574b68 NTND |
886 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
887 | commit_list_insert((struct commit *)o, &list); | |
888 | ret = get_sha1_oneline(prefix, sha1, list); | |
889 | free(prefix); | |
890 | return ret; | |
5385f52d JH |
891 | } |
892 | return 0; | |
893 | } | |
894 | ||
7dd45e15 JH |
895 | static int get_describe_name(const char *name, int len, unsigned char *sha1) |
896 | { | |
897 | const char *cp; | |
6269b6b6 | 898 | unsigned flags = GET_SHA1_QUIETLY | GET_SHA1_COMMIT; |
7dd45e15 JH |
899 | |
900 | for (cp = name + len - 1; name + 2 <= cp; cp--) { | |
901 | char ch = *cp; | |
6f75d45b | 902 | if (!isxdigit(ch)) { |
7dd45e15 JH |
903 | /* We must be looking at g in "SOMETHING-g" |
904 | * for it to be describe output. | |
905 | */ | |
906 | if (ch == 'g' && cp[-1] == '-') { | |
907 | cp++; | |
908 | len -= cp - name; | |
6269b6b6 | 909 | return get_short_sha1(cp, len, sha1, flags); |
7dd45e15 JH |
910 | } |
911 | } | |
912 | } | |
913 | return -1; | |
914 | } | |
915 | ||
e48ba200 | 916 | static int get_sha1_1(const char *name, int len, unsigned char *sha1, unsigned lookup_flags) |
9938af6a | 917 | { |
0601dbe1 | 918 | int ret, has_suffix; |
4f7599ac | 919 | const char *cp; |
9938af6a | 920 | |
621ff675 LT |
921 | /* |
922 | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". | |
4f7599ac | 923 | */ |
0601dbe1 | 924 | has_suffix = 0; |
4f7599ac JH |
925 | for (cp = name + len - 1; name <= cp; cp--) { |
926 | int ch = *cp; | |
927 | if ('0' <= ch && ch <= '9') | |
928 | continue; | |
0601dbe1 JH |
929 | if (ch == '~' || ch == '^') |
930 | has_suffix = ch; | |
4f7599ac JH |
931 | break; |
932 | } | |
0601dbe1 JH |
933 | |
934 | if (has_suffix) { | |
935 | int num = 0; | |
4f7599ac JH |
936 | int len1 = cp - name; |
937 | cp++; | |
938 | while (cp < name + len) | |
0601dbe1 | 939 | num = num * 10 + *cp++ - '0'; |
621ff675 LT |
940 | if (!num && len1 == len - 1) |
941 | num = 1; | |
942 | if (has_suffix == '^') | |
0601dbe1 | 943 | return get_parent(name, len1, sha1, num); |
0601dbe1 JH |
944 | /* else if (has_suffix == '~') -- goes without saying */ |
945 | return get_nth_ancestor(name, len1, sha1, num); | |
4f7599ac JH |
946 | } |
947 | ||
8a10fea4 | 948 | ret = peel_onion(name, len, sha1, lookup_flags); |
5385f52d JH |
949 | if (!ret) |
950 | return 0; | |
951 | ||
c41a87dd | 952 | ret = get_sha1_basic(name, len, sha1, lookup_flags); |
9938af6a JH |
953 | if (!ret) |
954 | return 0; | |
7dd45e15 JH |
955 | |
956 | /* It could be describe output that is "SOMETHING-gXXXX" */ | |
957 | ret = get_describe_name(name, len, sha1); | |
958 | if (!ret) | |
959 | return 0; | |
960 | ||
e2643617 | 961 | return get_short_sha1(name, len, sha1, lookup_flags); |
9938af6a JH |
962 | } |
963 | ||
f7bff003 JH |
964 | /* |
965 | * This interprets names like ':/Initial revision of "git"' by searching | |
966 | * through history and returning the first commit whose message starts | |
3d045897 | 967 | * the given regular expression. |
f7bff003 | 968 | * |
0769854f WP |
969 | * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'. |
970 | * | |
971 | * For a literal '!' character at the beginning of a pattern, you have to repeat | |
972 | * that, like: ':/!!foo' | |
973 | * | |
974 | * For future extension, all other sequences beginning with ':/!' are reserved. | |
f7bff003 | 975 | */ |
208acbfb NTND |
976 | |
977 | /* Remember to update object flag allocation in object.h */ | |
f7bff003 JH |
978 | #define ONELINE_SEEN (1u<<20) |
979 | ||
9c5fe0b8 MH |
980 | static int handle_one_ref(const char *path, const struct object_id *oid, |
981 | int flag, void *cb_data) | |
28a4d940 JS |
982 | { |
983 | struct commit_list **list = cb_data; | |
c251c83d | 984 | struct object *object = parse_object(oid); |
28a4d940 JS |
985 | if (!object) |
986 | return 0; | |
affeef12 | 987 | if (object->type == OBJ_TAG) { |
28a4d940 | 988 | object = deref_tag(object, path, strlen(path)); |
affeef12 MK |
989 | if (!object) |
990 | return 0; | |
991 | } | |
28a4d940 JS |
992 | if (object->type != OBJ_COMMIT) |
993 | return 0; | |
e8d1dfe6 | 994 | commit_list_insert((struct commit *)object, list); |
28a4d940 JS |
995 | return 0; |
996 | } | |
997 | ||
84baa31b NTND |
998 | static int get_sha1_oneline(const char *prefix, unsigned char *sha1, |
999 | struct commit_list *list) | |
28a4d940 | 1000 | { |
84baa31b | 1001 | struct commit_list *backup = NULL, *l; |
28042dbc | 1002 | int found = 0; |
0769854f | 1003 | int negative = 0; |
57895105 | 1004 | regex_t regex; |
28a4d940 JS |
1005 | |
1006 | if (prefix[0] == '!') { | |
28a4d940 | 1007 | prefix++; |
0769854f WP |
1008 | |
1009 | if (prefix[0] == '-') { | |
1010 | prefix++; | |
1011 | negative = 1; | |
1012 | } else if (prefix[0] != '!') { | |
e6a6a768 | 1013 | return -1; |
0769854f | 1014 | } |
28a4d940 | 1015 | } |
57895105 LT |
1016 | |
1017 | if (regcomp(®ex, prefix, REG_EXTENDED)) | |
aac4fac1 | 1018 | return -1; |
57895105 | 1019 | |
84baa31b NTND |
1020 | for (l = list; l; l = l->next) { |
1021 | l->item->object.flags |= ONELINE_SEEN; | |
28a4d940 | 1022 | commit_list_insert(l->item, &backup); |
84baa31b | 1023 | } |
ed8ad7e2 | 1024 | while (list) { |
ba41c1c9 | 1025 | const char *p, *buf; |
1358e7d6 | 1026 | struct commit *commit; |
28042dbc | 1027 | int matches; |
ed8ad7e2 JM |
1028 | |
1029 | commit = pop_most_recent_commit(&list, ONELINE_SEEN); | |
c251c83d | 1030 | if (!parse_object(&commit->object.oid)) |
283cdbcf | 1031 | continue; |
8597ea3a | 1032 | buf = get_commit_buffer(commit, NULL); |
ba41c1c9 | 1033 | p = strstr(buf, "\n\n"); |
0769854f | 1034 | matches = negative ^ (p && !regexec(®ex, p + 2, 0, NULL, 0)); |
ba41c1c9 | 1035 | unuse_commit_buffer(commit, buf); |
28042dbc JH |
1036 | |
1037 | if (matches) { | |
ed1c9977 | 1038 | hashcpy(sha1, commit->object.oid.hash); |
28042dbc | 1039 | found = 1; |
28a4d940 JS |
1040 | break; |
1041 | } | |
1042 | } | |
57895105 | 1043 | regfree(®ex); |
28a4d940 JS |
1044 | free_commit_list(list); |
1045 | for (l = backup; l; l = l->next) | |
1046 | clear_commit_marks(l->item, ONELINE_SEEN); | |
28042dbc JH |
1047 | free_commit_list(backup); |
1048 | return found ? 0 : -1; | |
28a4d940 JS |
1049 | } |
1050 | ||
ae5a6c36 | 1051 | struct grab_nth_branch_switch_cbdata { |
98f85ff4 JH |
1052 | int remaining; |
1053 | struct strbuf buf; | |
ae5a6c36 JH |
1054 | }; |
1055 | ||
9461d272 | 1056 | static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 1057 | const char *email, timestamp_t timestamp, int tz, |
ae5a6c36 JH |
1058 | const char *message, void *cb_data) |
1059 | { | |
1060 | struct grab_nth_branch_switch_cbdata *cb = cb_data; | |
a884d0cb TR |
1061 | const char *match = NULL, *target = NULL; |
1062 | size_t len; | |
1063 | ||
95b567c7 | 1064 | if (skip_prefix(message, "checkout: moving from ", &match)) |
d7c03c1f | 1065 | target = strstr(match, " to "); |
ae5a6c36 | 1066 | |
c829774c | 1067 | if (!match || !target) |
ae5a6c36 | 1068 | return 0; |
98f85ff4 JH |
1069 | if (--(cb->remaining) == 0) { |
1070 | len = target - match; | |
1071 | strbuf_reset(&cb->buf); | |
1072 | strbuf_add(&cb->buf, match, len); | |
1073 | return 1; /* we are done */ | |
1074 | } | |
ae5a6c36 JH |
1075 | return 0; |
1076 | } | |
1077 | ||
1078 | /* | |
ae0ba8e2 JH |
1079 | * Parse @{-N} syntax, return the number of characters parsed |
1080 | * if successful; otherwise signal an error with negative value. | |
ae5a6c36 | 1081 | */ |
8cd4249c JK |
1082 | static int interpret_nth_prior_checkout(const char *name, int namelen, |
1083 | struct strbuf *buf) | |
ae5a6c36 | 1084 | { |
c2883e62 | 1085 | long nth; |
98f85ff4 | 1086 | int retval; |
ae5a6c36 | 1087 | struct grab_nth_branch_switch_cbdata cb; |
a884d0cb TR |
1088 | const char *brace; |
1089 | char *num_end; | |
ae5a6c36 | 1090 | |
8cd4249c JK |
1091 | if (namelen < 4) |
1092 | return -1; | |
ae5a6c36 JH |
1093 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') |
1094 | return -1; | |
8cd4249c | 1095 | brace = memchr(name, '}', namelen); |
a884d0cb TR |
1096 | if (!brace) |
1097 | return -1; | |
98f85ff4 | 1098 | nth = strtol(name + 3, &num_end, 10); |
a884d0cb | 1099 | if (num_end != brace) |
ae5a6c36 | 1100 | return -1; |
c2883e62 JH |
1101 | if (nth <= 0) |
1102 | return -1; | |
98f85ff4 JH |
1103 | cb.remaining = nth; |
1104 | strbuf_init(&cb.buf, 20); | |
1105 | ||
39765e59 | 1106 | retval = 0; |
98f85ff4 JH |
1107 | if (0 < for_each_reflog_ent_reverse("HEAD", grab_nth_branch_switch, &cb)) { |
1108 | strbuf_reset(buf); | |
e992d1eb | 1109 | strbuf_addbuf(buf, &cb.buf); |
98f85ff4 JH |
1110 | retval = brace - name + 1; |
1111 | } | |
a884d0cb | 1112 | |
98f85ff4 | 1113 | strbuf_release(&cb.buf); |
39765e59 | 1114 | return retval; |
ae5a6c36 JH |
1115 | } |
1116 | ||
151b2911 | 1117 | int get_oid_mb(const char *name, struct object_id *oid) |
619a644d JH |
1118 | { |
1119 | struct commit *one, *two; | |
1120 | struct commit_list *mbs; | |
151b2911 | 1121 | struct object_id oid_tmp; |
619a644d JH |
1122 | const char *dots; |
1123 | int st; | |
1124 | ||
1125 | dots = strstr(name, "..."); | |
1126 | if (!dots) | |
151b2911 | 1127 | return get_oid(name, oid); |
619a644d | 1128 | if (dots == name) |
151b2911 | 1129 | st = get_oid("HEAD", &oid_tmp); |
619a644d JH |
1130 | else { |
1131 | struct strbuf sb; | |
1132 | strbuf_init(&sb, dots - name); | |
1133 | strbuf_add(&sb, name, dots - name); | |
151b2911 | 1134 | st = get_sha1_committish(sb.buf, oid_tmp.hash); |
619a644d JH |
1135 | strbuf_release(&sb); |
1136 | } | |
1137 | if (st) | |
1138 | return st; | |
bc83266a | 1139 | one = lookup_commit_reference_gently(&oid_tmp, 0); |
619a644d JH |
1140 | if (!one) |
1141 | return -1; | |
1142 | ||
151b2911 | 1143 | if (get_sha1_committish(dots[3] ? (dots + 3) : "HEAD", oid_tmp.hash)) |
619a644d | 1144 | return -1; |
bc83266a | 1145 | two = lookup_commit_reference_gently(&oid_tmp, 0); |
619a644d JH |
1146 | if (!two) |
1147 | return -1; | |
2ce406cc | 1148 | mbs = get_merge_bases(one, two); |
619a644d JH |
1149 | if (!mbs || mbs->next) |
1150 | st = -1; | |
1151 | else { | |
1152 | st = 0; | |
151b2911 | 1153 | oidcpy(oid, &mbs->item->object.oid); |
619a644d JH |
1154 | } |
1155 | free_commit_list(mbs); | |
1156 | return st; | |
1157 | } | |
1158 | ||
9ba89f48 FC |
1159 | /* parse @something syntax, when 'something' is not {.*} */ |
1160 | static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf) | |
1161 | { | |
1162 | const char *next; | |
1163 | ||
1164 | if (len || name[1] == '{') | |
1165 | return -1; | |
1166 | ||
1167 | /* make sure it's a single @, or @@{.*}, not @foo */ | |
8cd4249c | 1168 | next = memchr(name + len + 1, '@', namelen - len - 1); |
9ba89f48 FC |
1169 | if (next && next[1] != '{') |
1170 | return -1; | |
1171 | if (!next) | |
1172 | next = name + namelen; | |
1173 | if (next != name + 1) | |
1174 | return -1; | |
1175 | ||
1176 | strbuf_reset(buf); | |
1177 | strbuf_add(buf, "HEAD", 4); | |
1178 | return 1; | |
1179 | } | |
1180 | ||
0e9f62da JK |
1181 | static int reinterpret(const char *name, int namelen, int len, |
1182 | struct strbuf *buf, unsigned allowed) | |
7a0a49a7 FC |
1183 | { |
1184 | /* we have extra data, which might need further processing */ | |
1185 | struct strbuf tmp = STRBUF_INIT; | |
1186 | int used = buf->len; | |
1187 | int ret; | |
1188 | ||
1189 | strbuf_add(buf, name + len, namelen - len); | |
0e9f62da | 1190 | ret = interpret_branch_name(buf->buf, buf->len, &tmp, allowed); |
7a0a49a7 FC |
1191 | /* that data was not interpreted, remove our cruft */ |
1192 | if (ret < 0) { | |
1193 | strbuf_setlen(buf, used); | |
1194 | return len; | |
1195 | } | |
1196 | strbuf_reset(buf); | |
1197 | strbuf_addbuf(buf, &tmp); | |
1198 | strbuf_release(&tmp); | |
1199 | /* tweak for size of {-N} versus expanded ref name */ | |
1200 | return ret - used + len; | |
1201 | } | |
1202 | ||
a39c14af JK |
1203 | static void set_shortened_ref(struct strbuf *buf, const char *ref) |
1204 | { | |
1205 | char *s = shorten_unambiguous_ref(ref, 0); | |
1206 | strbuf_reset(buf); | |
1207 | strbuf_addstr(buf, s); | |
1208 | free(s); | |
1209 | } | |
1210 | ||
0e9f62da JK |
1211 | static int branch_interpret_allowed(const char *refname, unsigned allowed) |
1212 | { | |
1213 | if (!allowed) | |
1214 | return 1; | |
1215 | ||
1216 | if ((allowed & INTERPRET_BRANCH_LOCAL) && | |
1217 | starts_with(refname, "refs/heads/")) | |
1218 | return 1; | |
1219 | if ((allowed & INTERPRET_BRANCH_REMOTE) && | |
1220 | starts_with(refname, "refs/remotes/")) | |
1221 | return 1; | |
1222 | ||
1223 | return 0; | |
1224 | } | |
1225 | ||
48c58471 JK |
1226 | static int interpret_branch_mark(const char *name, int namelen, |
1227 | int at, struct strbuf *buf, | |
1228 | int (*get_mark)(const char *, int), | |
1229 | const char *(*get_data)(struct branch *, | |
0e9f62da JK |
1230 | struct strbuf *), |
1231 | unsigned allowed) | |
a39c14af JK |
1232 | { |
1233 | int len; | |
48c58471 JK |
1234 | struct branch *branch; |
1235 | struct strbuf err = STRBUF_INIT; | |
1236 | const char *value; | |
a39c14af | 1237 | |
48c58471 | 1238 | len = get_mark(name + at, namelen - at); |
a39c14af JK |
1239 | if (!len) |
1240 | return -1; | |
1241 | ||
3f6eb30f JK |
1242 | if (memchr(name, ':', at)) |
1243 | return -1; | |
1244 | ||
48c58471 JK |
1245 | if (at) { |
1246 | char *name_str = xmemdupz(name, at); | |
1247 | branch = branch_get(name_str); | |
1248 | free(name_str); | |
1249 | } else | |
1250 | branch = branch_get(NULL); | |
1251 | ||
1252 | value = get_data(branch, &err); | |
1253 | if (!value) | |
1254 | die("%s", err.buf); | |
1255 | ||
0e9f62da JK |
1256 | if (!branch_interpret_allowed(value, allowed)) |
1257 | return -1; | |
1258 | ||
48c58471 | 1259 | set_shortened_ref(buf, value); |
a39c14af JK |
1260 | return len + at; |
1261 | } | |
1262 | ||
0e9f62da JK |
1263 | int interpret_branch_name(const char *name, int namelen, struct strbuf *buf, |
1264 | unsigned allowed) | |
ae0ba8e2 | 1265 | { |
f278f40f | 1266 | char *at; |
9892d5d4 | 1267 | const char *start; |
13228c30 | 1268 | int len; |
ae0ba8e2 | 1269 | |
cf99a761 FC |
1270 | if (!namelen) |
1271 | namelen = strlen(name); | |
1272 | ||
0e9f62da JK |
1273 | if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) { |
1274 | len = interpret_nth_prior_checkout(name, namelen, buf); | |
1275 | if (!len) { | |
1276 | return len; /* syntax Ok, not enough switches */ | |
1277 | } else if (len > 0) { | |
1278 | if (len == namelen) | |
1279 | return len; /* consumed all */ | |
1280 | else | |
1281 | return reinterpret(name, namelen, len, buf, allowed); | |
1282 | } | |
d46a8301 JK |
1283 | } |
1284 | ||
9892d5d4 JK |
1285 | for (start = name; |
1286 | (at = memchr(start, '@', namelen - (start - name))); | |
1287 | start = at + 1) { | |
9ba89f48 | 1288 | |
0e9f62da JK |
1289 | if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) { |
1290 | len = interpret_empty_at(name, namelen, at - name, buf); | |
1291 | if (len > 0) | |
1292 | return reinterpret(name, namelen, len, buf, | |
1293 | allowed); | |
1294 | } | |
9ba89f48 | 1295 | |
48c58471 | 1296 | len = interpret_branch_mark(name, namelen, at - name, buf, |
0e9f62da JK |
1297 | upstream_mark, branch_get_upstream, |
1298 | allowed); | |
9892d5d4 JK |
1299 | if (len > 0) |
1300 | return len; | |
adfe5d04 JK |
1301 | |
1302 | len = interpret_branch_mark(name, namelen, at - name, buf, | |
0e9f62da JK |
1303 | push_mark, branch_get_push, |
1304 | allowed); | |
9892d5d4 JK |
1305 | if (len > 0) |
1306 | return len; | |
bb0dab5d | 1307 | } |
9ba89f48 | 1308 | |
a39c14af | 1309 | return -1; |
ae0ba8e2 JH |
1310 | } |
1311 | ||
0e9f62da | 1312 | void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) |
6bab74e7 JN |
1313 | { |
1314 | int len = strlen(name); | |
0e9f62da | 1315 | int used = interpret_branch_name(name, len, sb, allowed); |
84cf2466 | 1316 | |
84cf2466 JH |
1317 | if (used < 0) |
1318 | used = 0; | |
1319 | strbuf_add(sb, name + used, len - used); | |
6bab74e7 JN |
1320 | } |
1321 | ||
1322 | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) | |
1323 | { | |
7d5c960b | 1324 | strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); |
6bab74e7 | 1325 | if (name[0] == '-') |
8d9c5010 | 1326 | return -1; |
6bab74e7 | 1327 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
8d9c5010 | 1328 | return check_refname_format(sb->buf, 0); |
6bab74e7 JN |
1329 | } |
1330 | ||
9938af6a JH |
1331 | /* |
1332 | * This is like "get_sha1_basic()", except it allows "sha1 expressions", | |
1333 | * notably "xyz^" for "parent of xyz" | |
1334 | */ | |
1335 | int get_sha1(const char *name, unsigned char *sha1) | |
1336 | { | |
573285e5 | 1337 | struct object_context unused; |
33bd598c | 1338 | return get_sha1_with_context(name, 0, sha1, &unused); |
a0cd87a5 MK |
1339 | } |
1340 | ||
2764fd93 | 1341 | /* |
1342 | * This is like "get_sha1()", but for struct object_id. | |
1343 | */ | |
1344 | int get_oid(const char *name, struct object_id *oid) | |
1345 | { | |
1346 | return get_sha1(name, oid->hash); | |
1347 | } | |
1348 | ||
1349 | ||
cd74e473 | 1350 | /* |
a8a5406a | 1351 | * Many callers know that the user meant to name a commit-ish by |
cd74e473 JH |
1352 | * syntactical positions where the object name appears. Calling this |
1353 | * function allows the machinery to disambiguate shorter-than-unique | |
a8a5406a | 1354 | * abbreviated object names between commit-ish and others. |
cd74e473 JH |
1355 | * |
1356 | * Note that this does NOT error out when the named object is not a | |
a8a5406a | 1357 | * commit-ish. It is merely to give a hint to the disambiguation |
cd74e473 JH |
1358 | * machinery. |
1359 | */ | |
1360 | int get_sha1_committish(const char *name, unsigned char *sha1) | |
1361 | { | |
1362 | struct object_context unused; | |
1363 | return get_sha1_with_context(name, GET_SHA1_COMMITTISH, | |
1364 | sha1, &unused); | |
1365 | } | |
1366 | ||
daba53ae JH |
1367 | int get_sha1_treeish(const char *name, unsigned char *sha1) |
1368 | { | |
1369 | struct object_context unused; | |
1370 | return get_sha1_with_context(name, GET_SHA1_TREEISH, | |
1371 | sha1, &unused); | |
1372 | } | |
1373 | ||
1374 | int get_sha1_commit(const char *name, unsigned char *sha1) | |
1375 | { | |
1376 | struct object_context unused; | |
1377 | return get_sha1_with_context(name, GET_SHA1_COMMIT, | |
1378 | sha1, &unused); | |
1379 | } | |
1380 | ||
1381 | int get_sha1_tree(const char *name, unsigned char *sha1) | |
1382 | { | |
1383 | struct object_context unused; | |
1384 | return get_sha1_with_context(name, GET_SHA1_TREE, | |
1385 | sha1, &unused); | |
1386 | } | |
1387 | ||
1388 | int get_sha1_blob(const char *name, unsigned char *sha1) | |
1389 | { | |
1390 | struct object_context unused; | |
1391 | return get_sha1_with_context(name, GET_SHA1_BLOB, | |
1392 | sha1, &unused); | |
a0cd87a5 MK |
1393 | } |
1394 | ||
009fee47 MM |
1395 | /* Must be called only when object_name:filename doesn't exist. */ |
1396 | static void diagnose_invalid_sha1_path(const char *prefix, | |
1397 | const char *filename, | |
1398 | const unsigned char *tree_sha1, | |
b2981d06 RS |
1399 | const char *object_name, |
1400 | int object_name_len) | |
009fee47 | 1401 | { |
009fee47 MM |
1402 | unsigned char sha1[20]; |
1403 | unsigned mode; | |
1404 | ||
1405 | if (!prefix) | |
1406 | prefix = ""; | |
1407 | ||
dbe44faa | 1408 | if (file_exists(filename)) |
b2981d06 RS |
1409 | die("Path '%s' exists on disk, but not in '%.*s'.", |
1410 | filename, object_name_len, object_name); | |
009fee47 | 1411 | if (errno == ENOENT || errno == ENOTDIR) { |
b2724c87 | 1412 | char *fullname = xstrfmt("%s%s", prefix, filename); |
009fee47 MM |
1413 | |
1414 | if (!get_tree_entry(tree_sha1, fullname, | |
1415 | sha1, &mode)) { | |
1416 | die("Path '%s' exists, but not '%s'.\n" | |
b2981d06 | 1417 | "Did you mean '%.*s:%s' aka '%.*s:./%s'?", |
009fee47 MM |
1418 | fullname, |
1419 | filename, | |
b2981d06 | 1420 | object_name_len, object_name, |
e41d718c | 1421 | fullname, |
b2981d06 | 1422 | object_name_len, object_name, |
e41d718c | 1423 | filename); |
009fee47 | 1424 | } |
b2981d06 RS |
1425 | die("Path '%s' does not exist in '%.*s'", |
1426 | filename, object_name_len, object_name); | |
009fee47 MM |
1427 | } |
1428 | } | |
1429 | ||
1430 | /* Must be called only when :stage:filename doesn't exist. */ | |
1431 | static void diagnose_invalid_index_path(int stage, | |
1432 | const char *prefix, | |
1433 | const char *filename) | |
1434 | { | |
9c5e6c80 | 1435 | const struct cache_entry *ce; |
009fee47 MM |
1436 | int pos; |
1437 | unsigned namelen = strlen(filename); | |
43bb66ae | 1438 | struct strbuf fullname = STRBUF_INIT; |
009fee47 MM |
1439 | |
1440 | if (!prefix) | |
1441 | prefix = ""; | |
1442 | ||
1443 | /* Wrong stage number? */ | |
1444 | pos = cache_name_pos(filename, namelen); | |
1445 | if (pos < 0) | |
1446 | pos = -pos - 1; | |
77e8466f MH |
1447 | if (pos < active_nr) { |
1448 | ce = active_cache[pos]; | |
1449 | if (ce_namelen(ce) == namelen && | |
1450 | !memcmp(ce->name, filename, namelen)) | |
1451 | die("Path '%s' is in the index, but not at stage %d.\n" | |
1452 | "Did you mean ':%d:%s'?", | |
1453 | filename, stage, | |
1454 | ce_stage(ce), filename); | |
1455 | } | |
009fee47 MM |
1456 | |
1457 | /* Confusion between relative and absolute filenames? */ | |
43bb66ae JK |
1458 | strbuf_addstr(&fullname, prefix); |
1459 | strbuf_addstr(&fullname, filename); | |
1460 | pos = cache_name_pos(fullname.buf, fullname.len); | |
009fee47 MM |
1461 | if (pos < 0) |
1462 | pos = -pos - 1; | |
77e8466f MH |
1463 | if (pos < active_nr) { |
1464 | ce = active_cache[pos]; | |
43bb66ae JK |
1465 | if (ce_namelen(ce) == fullname.len && |
1466 | !memcmp(ce->name, fullname.buf, fullname.len)) | |
77e8466f | 1467 | die("Path '%s' is in the index, but not '%s'.\n" |
e41d718c | 1468 | "Did you mean ':%d:%s' aka ':%d:./%s'?", |
43bb66ae JK |
1469 | fullname.buf, filename, |
1470 | ce_stage(ce), fullname.buf, | |
e41d718c | 1471 | ce_stage(ce), filename); |
77e8466f | 1472 | } |
009fee47 | 1473 | |
dbe44faa | 1474 | if (file_exists(filename)) |
009fee47 MM |
1475 | die("Path '%s' exists on disk, but not in the index.", filename); |
1476 | if (errno == ENOENT || errno == ENOTDIR) | |
1477 | die("Path '%s' does not exist (neither on disk nor in the index).", | |
1478 | filename); | |
1479 | ||
43bb66ae | 1480 | strbuf_release(&fullname); |
009fee47 MM |
1481 | } |
1482 | ||
1483 | ||
979f7929 NTND |
1484 | static char *resolve_relative_path(const char *rel) |
1485 | { | |
59556548 | 1486 | if (!starts_with(rel, "./") && !starts_with(rel, "../")) |
979f7929 NTND |
1487 | return NULL; |
1488 | ||
979f7929 NTND |
1489 | if (!is_inside_work_tree()) |
1490 | die("relative path syntax can't be used outside working tree."); | |
1491 | ||
1492 | /* die() inside prefix_path() if resolved path is outside worktree */ | |
1493 | return prefix_path(startup_info->prefix, | |
1494 | startup_info->prefix ? strlen(startup_info->prefix) : 0, | |
1495 | rel); | |
1496 | } | |
1497 | ||
33bd598c JH |
1498 | static int get_sha1_with_context_1(const char *name, |
1499 | unsigned flags, | |
1500 | const char *prefix, | |
1501 | unsigned char *sha1, | |
1502 | struct object_context *oc) | |
a0cd87a5 MK |
1503 | { |
1504 | int ret, bracket_depth; | |
73b0e5af JH |
1505 | int namelen = strlen(name); |
1506 | const char *cp; | |
33bd598c | 1507 | int only_to_die = flags & GET_SHA1_ONLY_TO_DIE; |
5119602a | 1508 | |
7243ffdd JK |
1509 | if (only_to_die) |
1510 | flags |= GET_SHA1_QUIETLY; | |
1511 | ||
573285e5 CP |
1512 | memset(oc, 0, sizeof(*oc)); |
1513 | oc->mode = S_IFINVALID; | |
33bd598c | 1514 | ret = get_sha1_1(name, namelen, sha1, flags); |
73b0e5af JH |
1515 | if (!ret) |
1516 | return ret; | |
33bd598c JH |
1517 | /* |
1518 | * sha1:path --> object name of path in ent sha1 | |
979f7929 NTND |
1519 | * :path -> object name of absolute path in index |
1520 | * :./path -> object name of path relative to cwd in index | |
73b0e5af | 1521 | * :[0-3]:path -> object name of path in index at stage |
95ad6d2d | 1522 | * :/foo -> recent commit matching foo |
73b0e5af JH |
1523 | */ |
1524 | if (name[0] == ':') { | |
1525 | int stage = 0; | |
9c5e6c80 | 1526 | const struct cache_entry *ce; |
979f7929 | 1527 | char *new_path = NULL; |
73b0e5af | 1528 | int pos; |
2e83b66c | 1529 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
84baa31b | 1530 | struct commit_list *list = NULL; |
2b2a5be3 | 1531 | |
84baa31b | 1532 | for_each_ref(handle_one_ref, &list); |
e8d1dfe6 | 1533 | commit_list_sort_by_date(&list); |
84baa31b NTND |
1534 | return get_sha1_oneline(name + 2, sha1, list); |
1535 | } | |
73b0e5af JH |
1536 | if (namelen < 3 || |
1537 | name[2] != ':' || | |
1538 | name[1] < '0' || '3' < name[1]) | |
1539 | cp = name + 1; | |
1540 | else { | |
1541 | stage = name[1] - '0'; | |
1542 | cp = name + 3; | |
5119602a | 1543 | } |
3d6e0f74 JH |
1544 | new_path = resolve_relative_path(cp); |
1545 | if (!new_path) { | |
1546 | namelen = namelen - (cp - name); | |
1547 | } else { | |
1548 | cp = new_path; | |
1549 | namelen = strlen(cp); | |
1550 | } | |
573285e5 | 1551 | |
2ce63e9f | 1552 | strlcpy(oc->path, cp, sizeof(oc->path)); |
573285e5 | 1553 | |
73b0e5af JH |
1554 | if (!active_cache) |
1555 | read_cache(); | |
73b0e5af JH |
1556 | pos = cache_name_pos(cp, namelen); |
1557 | if (pos < 0) | |
1558 | pos = -pos - 1; | |
1559 | while (pos < active_nr) { | |
1560 | ce = active_cache[pos]; | |
1561 | if (ce_namelen(ce) != namelen || | |
1562 | memcmp(ce->name, cp, namelen)) | |
1563 | break; | |
1564 | if (ce_stage(ce) == stage) { | |
99d1a986 | 1565 | hashcpy(sha1, ce->oid.hash); |
90064710 | 1566 | oc->mode = ce->ce_mode; |
979f7929 | 1567 | free(new_path); |
73b0e5af JH |
1568 | return 0; |
1569 | } | |
e7cef45f | 1570 | pos++; |
73b0e5af | 1571 | } |
2e83b66c | 1572 | if (only_to_die && name[1] && name[1] != '/') |
009fee47 | 1573 | diagnose_invalid_index_path(stage, prefix, cp); |
979f7929 | 1574 | free(new_path); |
73b0e5af JH |
1575 | return -1; |
1576 | } | |
cce91a2c SP |
1577 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
1578 | if (*cp == '{') | |
1579 | bracket_depth++; | |
1580 | else if (bracket_depth && *cp == '}') | |
1581 | bracket_depth--; | |
1582 | else if (!bracket_depth && *cp == ':') | |
1583 | break; | |
1584 | } | |
1585 | if (*cp == ':') { | |
73b0e5af | 1586 | unsigned char tree_sha1[20]; |
b2981d06 | 1587 | int len = cp - name; |
8a10fea4 JK |
1588 | unsigned sub_flags = flags; |
1589 | ||
1590 | sub_flags &= ~GET_SHA1_DISAMBIGUATORS; | |
1591 | sub_flags |= GET_SHA1_TREEISH; | |
1592 | ||
1593 | if (!get_sha1_1(name, len, tree_sha1, sub_flags)) { | |
009fee47 | 1594 | const char *filename = cp+1; |
979f7929 NTND |
1595 | char *new_filename = NULL; |
1596 | ||
1597 | new_filename = resolve_relative_path(filename); | |
1598 | if (new_filename) | |
1599 | filename = new_filename; | |
c4ec9677 DT |
1600 | if (flags & GET_SHA1_FOLLOW_SYMLINKS) { |
1601 | ret = get_tree_entry_follow_symlinks(tree_sha1, | |
1602 | filename, sha1, &oc->symlink_path, | |
1603 | &oc->mode); | |
1604 | } else { | |
1605 | ret = get_tree_entry(tree_sha1, filename, | |
1606 | sha1, &oc->mode); | |
1607 | if (ret && only_to_die) { | |
1608 | diagnose_invalid_sha1_path(prefix, | |
1609 | filename, | |
1610 | tree_sha1, | |
1611 | name, len); | |
1612 | } | |
009fee47 | 1613 | } |
573285e5 | 1614 | hashcpy(oc->tree, tree_sha1); |
2ce63e9f | 1615 | strlcpy(oc->path, filename, sizeof(oc->path)); |
573285e5 | 1616 | |
979f7929 | 1617 | free(new_filename); |
009fee47 MM |
1618 | return ret; |
1619 | } else { | |
2e83b66c | 1620 | if (only_to_die) |
b2981d06 | 1621 | die("Invalid object name '%.*s'.", len, name); |
009fee47 | 1622 | } |
5119602a LT |
1623 | } |
1624 | return ret; | |
9938af6a | 1625 | } |
f01cc14c | 1626 | |
8c135ea2 JH |
1627 | /* |
1628 | * Call this function when you know "name" given by the end user must | |
1629 | * name an object but it doesn't; the function _may_ die with a better | |
1630 | * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not | |
1631 | * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case | |
1632 | * you have a chance to diagnose the error further. | |
1633 | */ | |
1634 | void maybe_die_on_misspelt_object_name(const char *name, const char *prefix) | |
f01cc14c JH |
1635 | { |
1636 | struct object_context oc; | |
8c135ea2 | 1637 | unsigned char sha1[20]; |
33bd598c | 1638 | get_sha1_with_context_1(name, GET_SHA1_ONLY_TO_DIE, prefix, sha1, &oc); |
8c135ea2 JH |
1639 | } |
1640 | ||
33bd598c | 1641 | int get_sha1_with_context(const char *str, unsigned flags, unsigned char *sha1, struct object_context *orc) |
f01cc14c | 1642 | { |
c4ec9677 DT |
1643 | if (flags & GET_SHA1_FOLLOW_SYMLINKS && flags & GET_SHA1_ONLY_TO_DIE) |
1644 | die("BUG: incompatible flags for get_sha1_with_context"); | |
33bd598c | 1645 | return get_sha1_with_context_1(str, flags, NULL, sha1, orc); |
f01cc14c | 1646 | } |