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