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