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