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