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