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