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