1 #include "git-compat-util.h"
2 #include "object-name.h"
5 #include "environment.h"
11 #include "tree-walk.h"
15 #include "oid-array.h"
19 #include "object-store-ll.h"
20 #include "read-cache-ll.h"
21 #include "repository.h"
24 #include "commit-reach.h"
26 #include "object-file-convert.h"
28 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
30 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
32 struct disambiguate_state
{
33 int len
; /* length of prefix in hex chars */
34 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
35 struct object_id bin_pfx
;
37 struct repository
*repo
;
38 disambiguate_hint_fn fn
;
40 struct object_id candidate
;
41 unsigned candidate_exists
:1;
42 unsigned candidate_checked
:1;
43 unsigned candidate_ok
:1;
44 unsigned disambiguate_fn_used
:1;
46 unsigned always_call_fn
:1;
49 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
51 /* The hash algorithm of current has already been filtered */
52 if (ds
->always_call_fn
) {
53 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
56 if (!ds
->candidate_exists
) {
57 /* this is the first candidate */
58 oidcpy(&ds
->candidate
, current
);
59 ds
->candidate_exists
= 1;
61 } else if (oideq(&ds
->candidate
, current
)) {
62 /* the same as what we already have seen */
67 /* cannot disambiguate between ds->candidate and current */
72 if (!ds
->candidate_checked
) {
73 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
74 ds
->disambiguate_fn_used
= 1;
75 ds
->candidate_checked
= 1;
78 if (!ds
->candidate_ok
) {
79 /* discard the candidate; we know it does not satisfy fn */
80 oidcpy(&ds
->candidate
, current
);
81 ds
->candidate_checked
= 0;
85 /* if we reach this point, we know ds->candidate satisfies fn */
86 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
88 * if both current and candidate satisfy fn, we cannot
95 /* otherwise, current can be discarded and candidate is still good */
98 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
100 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
102 struct disambiguate_state
*ds
= arg
;
103 /* no need to call match_hash, oidtree_each did prefix match */
104 update_candidates(ds
, oid
);
105 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
108 static void find_short_object_filename(struct disambiguate_state
*ds
)
110 struct object_directory
*odb
;
112 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
113 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
114 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
117 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
127 if ((*a
^ *b
) & 0xf0)
132 static void unique_in_midx(struct multi_pack_index
*m
,
133 struct disambiguate_state
*ds
)
135 uint32_t num
, i
, first
= 0;
136 const struct object_id
*current
= NULL
;
137 int len
= ds
->len
> ds
->repo
->hash_algo
->hexsz
?
138 ds
->repo
->hash_algo
->hexsz
: ds
->len
;
139 num
= m
->num_objects
;
144 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
147 * At this point, "first" is the location of the lowest object
148 * with an object name that could match "bin_pfx". See if we have
149 * 0, 1 or more objects that actually match(es).
151 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
152 struct object_id oid
;
153 current
= nth_midxed_object_oid(&oid
, m
, i
);
154 if (!match_hash(len
, ds
->bin_pfx
.hash
, current
->hash
))
156 update_candidates(ds
, current
);
160 static void unique_in_pack(struct packed_git
*p
,
161 struct disambiguate_state
*ds
)
163 uint32_t num
, i
, first
= 0;
164 int len
= ds
->len
> ds
->repo
->hash_algo
->hexsz
?
165 ds
->repo
->hash_algo
->hexsz
: ds
->len
;
167 if (p
->multi_pack_index
)
170 if (open_pack_index(p
) || !p
->num_objects
)
173 num
= p
->num_objects
;
174 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
177 * At this point, "first" is the location of the lowest object
178 * with an object name that could match "bin_pfx". See if we have
179 * 0, 1 or more objects that actually match(es).
181 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
182 struct object_id oid
;
183 nth_packed_object_id(&oid
, p
, i
);
184 if (!match_hash(len
, ds
->bin_pfx
.hash
, oid
.hash
))
186 update_candidates(ds
, &oid
);
190 static void find_short_packed_object(struct disambiguate_state
*ds
)
192 struct multi_pack_index
*m
;
193 struct packed_git
*p
;
195 /* Skip, unless oids from the storage hash algorithm are wanted */
196 if (ds
->bin_pfx
.algo
&& (&hash_algos
[ds
->bin_pfx
.algo
] != ds
->repo
->hash_algo
))
199 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
201 unique_in_midx(m
, ds
);
202 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
204 unique_in_pack(p
, ds
);
207 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
208 struct object_id
*oid
)
211 return SHORT_NAME_AMBIGUOUS
;
213 if (!ds
->candidate_exists
)
214 return MISSING_OBJECT
;
216 if (!ds
->candidate_checked
)
218 * If this is the only candidate, there is no point
219 * calling the disambiguation hint callback.
221 * On the other hand, if the current candidate
222 * replaced an earlier candidate that did _not_ pass
223 * the disambiguation hint callback, then we do have
224 * more than one objects that match the short name
225 * given, so we should make sure this one matches;
226 * otherwise, if we discovered this one and the one
227 * that we previously discarded in the reverse order,
228 * we would end up showing different results in the
231 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
232 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
234 if (!ds
->candidate_ok
)
235 return SHORT_NAME_AMBIGUOUS
;
237 oidcpy(oid
, &ds
->candidate
);
241 static int disambiguate_commit_only(struct repository
*r
,
242 const struct object_id
*oid
,
243 void *cb_data UNUSED
)
245 int kind
= oid_object_info(r
, oid
, NULL
);
246 return kind
== OBJ_COMMIT
;
249 static int disambiguate_committish_only(struct repository
*r
,
250 const struct object_id
*oid
,
251 void *cb_data UNUSED
)
256 kind
= oid_object_info(r
, oid
, NULL
);
257 if (kind
== OBJ_COMMIT
)
262 /* We need to do this the hard way... */
263 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
264 if (obj
&& obj
->type
== OBJ_COMMIT
)
269 static int disambiguate_tree_only(struct repository
*r
,
270 const struct object_id
*oid
,
271 void *cb_data UNUSED
)
273 int kind
= oid_object_info(r
, oid
, NULL
);
274 return kind
== OBJ_TREE
;
277 static int disambiguate_treeish_only(struct repository
*r
,
278 const struct object_id
*oid
,
279 void *cb_data UNUSED
)
284 kind
= oid_object_info(r
, oid
, NULL
);
285 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
290 /* We need to do this the hard way... */
291 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
292 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
297 static int disambiguate_blob_only(struct repository
*r
,
298 const struct object_id
*oid
,
299 void *cb_data UNUSED
)
301 int kind
= oid_object_info(r
, oid
, NULL
);
302 return kind
== OBJ_BLOB
;
305 static disambiguate_hint_fn default_disambiguate_hint
;
307 int set_disambiguate_hint_config(const char *var
, const char *value
)
309 static const struct {
311 disambiguate_hint_fn fn
;
314 { "commit", disambiguate_commit_only
},
315 { "committish", disambiguate_committish_only
},
316 { "tree", disambiguate_tree_only
},
317 { "treeish", disambiguate_treeish_only
},
318 { "blob", disambiguate_blob_only
}
323 return config_error_nonbool(var
);
325 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
326 if (!strcasecmp(value
, hints
[i
].name
)) {
327 default_disambiguate_hint
= hints
[i
].fn
;
332 return error("unknown hint type for '%s': %s", var
, value
);
335 static int init_object_disambiguation(struct repository
*r
,
336 const char *name
, int len
,
337 const struct git_hash_algo
*algo
,
338 struct disambiguate_state
*ds
)
342 if (len
< MINIMUM_ABBREV
|| len
> GIT_MAX_HEXSZ
)
345 memset(ds
, 0, sizeof(*ds
));
347 for (i
= 0; i
< len
;i
++) {
348 unsigned char c
= name
[i
];
350 if (c
>= '0' && c
<= '9')
352 else if (c
>= 'a' && c
<= 'f')
354 else if (c
>= 'A' && c
<='F') {
363 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
367 ds
->hex_pfx
[len
] = '\0';
369 ds
->bin_pfx
.algo
= algo
? hash_algo_by_ptr(algo
) : GIT_HASH_UNKNOWN
;
374 struct ambiguous_output
{
375 const struct disambiguate_state
*ds
;
376 struct strbuf advice
;
380 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
382 struct ambiguous_output
*state
= data
;
383 const struct disambiguate_state
*ds
= state
->ds
;
384 struct strbuf
*advice
= &state
->advice
;
385 struct strbuf
*sb
= &state
->sb
;
389 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
392 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
393 type
= oid_object_info(ds
->repo
, oid
, NULL
);
397 * TRANSLATORS: This is a line of ambiguous object
398 * output shown when we cannot look up or parse the
399 * object in question. E.g. "deadbeef [bad object]".
401 strbuf_addf(sb
, _("%s [bad object]"), hash
);
405 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
406 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
408 if (type
== OBJ_COMMIT
) {
409 struct strbuf date
= STRBUF_INIT
;
410 struct strbuf msg
= STRBUF_INIT
;
411 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
414 struct pretty_print_context pp
= {0};
415 pp
.date_mode
.type
= DATE_SHORT
;
416 repo_format_commit_message(the_repository
, commit
,
418 repo_format_commit_message(the_repository
, commit
,
423 * TRANSLATORS: This is a line of ambiguous commit
424 * object output. E.g.:
426 * "deadbeef commit 2021-01-01 - Some Commit Message"
428 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
431 strbuf_release(&date
);
432 strbuf_release(&msg
);
433 } else if (type
== OBJ_TAG
) {
434 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
436 if (!parse_tag(tag
) && tag
->tag
) {
438 * TRANSLATORS: This is a line of ambiguous
439 * tag object output. E.g.:
441 * "deadbeef tag 2022-01-01 - Some Tag Message"
443 * The second argument is the YYYY-MM-DD found
446 * The third argument is the "tag" string
449 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
450 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
454 * TRANSLATORS: This is a line of ambiguous
455 * tag object output where we couldn't parse
456 * the tag itself. E.g.:
458 * "deadbeef [bad tag, could not parse it]"
460 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
463 } else if (type
== OBJ_TREE
) {
465 * TRANSLATORS: This is a line of ambiguous <type>
466 * object output. E.g. "deadbeef tree".
468 strbuf_addf(sb
, _("%s tree"), hash
);
469 } else if (type
== OBJ_BLOB
) {
471 * TRANSLATORS: This is a line of ambiguous <type>
472 * object output. E.g. "deadbeef blob".
474 strbuf_addf(sb
, _("%s blob"), hash
);
480 * TRANSLATORS: This is line item of ambiguous object output
481 * from describe_ambiguous_object() above. For RTL languages
482 * you'll probably want to swap the "%s" and leading " " space
485 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
491 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
493 oid_array_append(data
, oid
);
497 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
498 const struct object_id
*oid
,
501 return collect_ambiguous(oid
, data
);
504 static int sort_ambiguous(const void *va
, const void *vb
, void *ctx
)
506 struct repository
*sort_ambiguous_repo
= ctx
;
507 const struct object_id
*a
= va
, *b
= vb
;
508 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
509 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
514 * Sorts by hash within the same object type, just as
515 * oid_array_for_each_unique() would do.
517 if (a_type
== b_type
) {
518 if (a
->algo
== b
->algo
)
521 return a
->algo
> b
->algo
? 1 : -1;
525 * Between object types show tags, then commits, and finally
528 * The object_type enum is commit, tree, blob, tag, but we
529 * want tag, commit, tree blob. Cleverly (perhaps too
530 * cleverly) do that with modulus, since the enum assigns 1 to
531 * commit, so tag becomes 0.
533 a_type_sort
= a_type
% 4;
534 b_type_sort
= b_type
% 4;
535 return a_type_sort
> b_type_sort
? 1 : -1;
538 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
540 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
543 static enum get_oid_result
get_short_oid(struct repository
*r
,
544 const char *name
, int len
,
545 struct object_id
*oid
,
549 struct disambiguate_state ds
;
550 int quietly
= !!(flags
& GET_OID_QUIETLY
);
551 const struct git_hash_algo
*algo
= r
->hash_algo
;
553 if (flags
& GET_OID_HASH_ANY
)
556 if (init_object_disambiguation(r
, name
, len
, algo
, &ds
) < 0)
559 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
560 BUG("multiple get_short_oid disambiguator flags");
562 if (flags
& GET_OID_COMMIT
)
563 ds
.fn
= disambiguate_commit_only
;
564 else if (flags
& GET_OID_COMMITTISH
)
565 ds
.fn
= disambiguate_committish_only
;
566 else if (flags
& GET_OID_TREE
)
567 ds
.fn
= disambiguate_tree_only
;
568 else if (flags
& GET_OID_TREEISH
)
569 ds
.fn
= disambiguate_treeish_only
;
570 else if (flags
& GET_OID_BLOB
)
571 ds
.fn
= disambiguate_blob_only
;
573 ds
.fn
= default_disambiguate_hint
;
575 find_short_object_filename(&ds
);
576 find_short_packed_object(&ds
);
577 status
= finish_object_disambiguation(&ds
, oid
);
580 * If we didn't find it, do the usual reprepare() slow-path,
581 * since the object may have recently been added to the repository
582 * or migrated from loose to packed.
584 if (status
== MISSING_OBJECT
) {
585 reprepare_packed_git(r
);
586 find_short_object_filename(&ds
);
587 find_short_packed_object(&ds
);
588 status
= finish_object_disambiguation(&ds
, oid
);
591 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
592 struct oid_array collect
= OID_ARRAY_INIT
;
593 struct ambiguous_output out
= {
596 .advice
= STRBUF_INIT
,
599 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
602 * We may still have ambiguity if we simply saw a series of
603 * candidates that did not satisfy our hint function. In
604 * that case, we still want to show them, so disable the hint
610 repo_for_each_abbrev(r
, ds
.hex_pfx
, algo
, collect_ambiguous
, &collect
);
611 sort_ambiguous_oid_array(r
, &collect
);
613 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
614 BUG("show_ambiguous_object shouldn't return non-zero");
617 * TRANSLATORS: The argument is the list of ambiguous
618 * objects composed in show_ambiguous_object(). See
619 * its "TRANSLATORS" comments for details.
621 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
623 oid_array_clear(&collect
);
624 strbuf_release(&out
.advice
);
625 strbuf_release(&out
.sb
);
631 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
632 const struct git_hash_algo
*algo
,
633 each_abbrev_fn fn
, void *cb_data
)
635 struct oid_array collect
= OID_ARRAY_INIT
;
636 struct disambiguate_state ds
;
639 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), algo
, &ds
) < 0)
642 ds
.always_call_fn
= 1;
643 ds
.fn
= repo_collect_ambiguous
;
644 ds
.cb_data
= &collect
;
645 find_short_object_filename(&ds
);
646 find_short_packed_object(&ds
);
648 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
649 oid_array_clear(&collect
);
654 * Return the slot of the most-significant bit set in "val". There are various
655 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
656 * probably not a big deal here.
658 static unsigned msb(unsigned long val
)
666 struct min_abbrev_data
{
667 unsigned int init_len
;
668 unsigned int cur_len
;
670 struct repository
*repo
;
671 const struct object_id
*oid
;
674 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
677 static const char hex
[] = "0123456789abcdef";
680 return hex
[oid
->hash
[pos
>> 1] >> 4];
682 return hex
[oid
->hash
[pos
>> 1] & 0xf];
685 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
687 struct min_abbrev_data
*mad
= cb_data
;
689 unsigned int i
= mad
->init_len
;
690 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
693 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
694 mad
->cur_len
= i
+ 1;
699 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
700 const struct object_id
*oid
,
703 return extend_abbrev_len(oid
, cb_data
);
706 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
707 struct min_abbrev_data
*mad
)
710 uint32_t num
, first
= 0;
711 struct object_id oid
;
712 const struct object_id
*mad_oid
;
717 num
= m
->num_objects
;
719 match
= bsearch_midx(mad_oid
, m
, &first
);
722 * first is now the position in the packfile where we would insert
723 * mad->hash if it does not exist (or the position of mad->hash if
724 * it does exist). Hence, we consider a maximum of two objects
725 * nearby for the abbreviation length.
729 if (nth_midxed_object_oid(&oid
, m
, first
))
730 extend_abbrev_len(&oid
, mad
);
731 } else if (first
< num
- 1) {
732 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
733 extend_abbrev_len(&oid
, mad
);
736 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
737 extend_abbrev_len(&oid
, mad
);
739 mad
->init_len
= mad
->cur_len
;
742 static void find_abbrev_len_for_pack(struct packed_git
*p
,
743 struct min_abbrev_data
*mad
)
746 uint32_t num
, first
= 0;
747 struct object_id oid
;
748 const struct object_id
*mad_oid
;
750 if (p
->multi_pack_index
)
753 if (open_pack_index(p
) || !p
->num_objects
)
756 num
= p
->num_objects
;
758 match
= bsearch_pack(mad_oid
, p
, &first
);
761 * first is now the position in the packfile where we would insert
762 * mad->hash if it does not exist (or the position of mad->hash if
763 * it does exist). Hence, we consider a maximum of two objects
764 * nearby for the abbreviation length.
768 if (!nth_packed_object_id(&oid
, p
, first
))
769 extend_abbrev_len(&oid
, mad
);
770 } else if (first
< num
- 1) {
771 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
772 extend_abbrev_len(&oid
, mad
);
775 if (!nth_packed_object_id(&oid
, p
, first
- 1))
776 extend_abbrev_len(&oid
, mad
);
778 mad
->init_len
= mad
->cur_len
;
781 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
783 struct multi_pack_index
*m
;
784 struct packed_git
*p
;
786 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
787 find_abbrev_len_for_midx(m
, mad
);
788 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
789 find_abbrev_len_for_pack(p
, mad
);
792 void strbuf_repo_add_unique_abbrev(struct strbuf
*sb
, struct repository
*repo
,
793 const struct object_id
*oid
, int abbrev_len
)
796 strbuf_grow(sb
, GIT_MAX_HEXSZ
+ 1);
797 r
= repo_find_unique_abbrev_r(repo
, sb
->buf
+ sb
->len
, oid
, abbrev_len
);
798 strbuf_setlen(sb
, sb
->len
+ r
);
801 void strbuf_add_unique_abbrev(struct strbuf
*sb
, const struct object_id
*oid
,
804 strbuf_repo_add_unique_abbrev(sb
, the_repository
, oid
, abbrev_len
);
807 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
808 const struct object_id
*oid
, int len
)
810 const struct git_hash_algo
*algo
=
811 oid
->algo
? &hash_algos
[oid
->algo
] : r
->hash_algo
;
812 struct disambiguate_state ds
;
813 struct min_abbrev_data mad
;
814 struct object_id oid_ret
;
815 const unsigned hexsz
= algo
->hexsz
;
818 unsigned long count
= repo_approximate_object_count(r
);
820 * Add one because the MSB only tells us the highest bit set,
821 * not including the value of all the _other_ bits (so "15"
822 * is only one off of 2^4, but the MSB is the 3rd bit.
824 len
= msb(count
) + 1;
826 * We now know we have on the order of 2^len objects, which
827 * expects a collision at 2^(len/2). But we also care about hex
828 * chars, not bits, and there are 4 bits per hex. So all
829 * together we need to divide by 2 and round up.
831 len
= DIV_ROUND_UP(len
, 2);
833 * For very small repos, we stick with our regular fallback.
835 if (len
< FALLBACK_DEFAULT_ABBREV
)
836 len
= FALLBACK_DEFAULT_ABBREV
;
839 oid_to_hex_r(hex
, oid
);
840 if (len
>= hexsz
|| !len
)
849 find_abbrev_len_packed(&mad
);
851 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, algo
, &ds
) < 0)
854 ds
.fn
= repo_extend_abbrev_len
;
855 ds
.always_call_fn
= 1;
856 ds
.cb_data
= (void *)&mad
;
858 find_short_object_filename(&ds
);
859 (void)finish_object_disambiguation(&ds
, &oid_ret
);
861 hex
[mad
.cur_len
] = 0;
865 const char *repo_find_unique_abbrev(struct repository
*r
,
866 const struct object_id
*oid
,
870 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
871 char *hex
= hexbuffer
[bufno
];
872 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
873 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
877 static int ambiguous_path(const char *path
, int len
)
882 for (cnt
= 0; cnt
< len
; cnt
++) {
902 static inline int at_mark(const char *string
, int len
,
903 const char **suffix
, int nr
)
907 for (i
= 0; i
< nr
; i
++) {
908 int suffix_len
= strlen(suffix
[i
]);
909 if (suffix_len
<= len
910 && !strncasecmp(string
, suffix
[i
], suffix_len
))
916 static inline int upstream_mark(const char *string
, int len
)
918 const char *suffix
[] = { "@{upstream}", "@{u}" };
919 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
922 static inline int push_mark(const char *string
, int len
)
924 const char *suffix
[] = { "@{push}" };
925 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
928 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
929 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
931 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
932 struct object_id
*oid
, unsigned int flags
)
934 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
935 static const char *object_name_msg
= N_(
936 "Git normally never creates a ref that ends with 40 hex characters\n"
937 "because it will be ignored when you just specify 40-hex. These refs\n"
938 "may be created by mistake. For example,\n"
940 " git switch -c $br $(git rev-parse ...)\n"
942 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
943 "examine these refs and maybe delete them. Turn this message off by\n"
944 "running \"git config advice.objectNameWarning false\"");
945 struct object_id tmp_oid
;
946 char *real_ref
= NULL
;
948 int at
, reflog_len
, nth_prior
= 0;
949 int fatal
= !(flags
& GET_OID_QUIETLY
);
951 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
952 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
953 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
954 if (refs_found
> 0) {
955 warning(warn_msg
, len
, str
);
956 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
957 fprintf(stderr
, "%s\n", _(object_name_msg
));
964 /* basic@{time or number or -number} format to query ref-log */
966 if (len
&& str
[len
-1] == '}') {
967 for (at
= len
-4; at
>= 0; at
--) {
968 if (str
[at
] == '@' && str
[at
+1] == '{') {
969 if (str
[at
+2] == '-') {
971 /* @{-N} not at start */
976 if (!upstream_mark(str
+ at
, len
- at
) &&
977 !push_mark(str
+ at
, len
- at
)) {
978 reflog_len
= (len
-1) - (at
+2);
986 /* Accept only unambiguous ref paths. */
987 if (len
&& ambiguous_path(str
, len
))
991 struct strbuf buf
= STRBUF_INIT
;
994 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
995 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
996 strbuf_release(&buf
);
1002 if (!len
&& reflog_len
)
1003 /* allow "@{...}" to mean the current branch reflog */
1004 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, !fatal
);
1005 else if (reflog_len
)
1006 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
1008 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, !fatal
);
1013 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
1015 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
1016 warning(warn_msg
, len
, str
);
1020 timestamp_t at_time
;
1021 timestamp_t co_time
;
1024 /* Is it asking for N-th entry, or approxidate? */
1025 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
1026 char ch
= str
[at
+2+i
];
1027 if ('0' <= ch
&& ch
<= '9')
1028 nth
= nth
* 10 + ch
- '0';
1032 if (100000000 <= nth
) {
1035 } else if (0 <= nth
)
1039 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
1040 at_time
= approxidate_careful(tmp
, &errors
);
1047 if (read_ref_at(get_main_ref_store(r
),
1048 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1049 &co_time
, &co_tz
, &co_cnt
)) {
1051 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1056 if (!(flags
& GET_OID_QUIETLY
)) {
1057 warning(_("log for '%.*s' only goes back to %s"),
1059 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1061 } else if (nth
== co_cnt
&& !is_null_oid(oid
)) {
1063 * We were asked for the Nth reflog (counting
1064 * from 0), but there were only N entries.
1065 * read_ref_at() will have returned "1" to tell
1066 * us it did not find an entry, but it did
1067 * still fill in the oid with the "old" value,
1071 if (flags
& GET_OID_QUIETLY
) {
1074 die(_("log for '%.*s' only has %d entries"),
1084 static enum get_oid_result
get_parent(struct repository
*r
,
1085 const char *name
, int len
,
1086 struct object_id
*result
, int idx
)
1088 struct object_id oid
;
1089 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1090 GET_OID_COMMITTISH
);
1091 struct commit
*commit
;
1092 struct commit_list
*p
;
1096 commit
= lookup_commit_reference(r
, &oid
);
1097 if (repo_parse_commit(r
, commit
))
1098 return MISSING_OBJECT
;
1100 oidcpy(result
, &commit
->object
.oid
);
1103 p
= commit
->parents
;
1106 oidcpy(result
, &p
->item
->object
.oid
);
1111 return MISSING_OBJECT
;
1114 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1115 const char *name
, int len
,
1116 struct object_id
*result
,
1119 struct object_id oid
;
1120 struct commit
*commit
;
1123 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1126 commit
= lookup_commit_reference(r
, &oid
);
1128 return MISSING_OBJECT
;
1130 while (generation
--) {
1131 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1132 return MISSING_OBJECT
;
1133 commit
= commit
->parents
->item
;
1135 oidcpy(result
, &commit
->object
.oid
);
1139 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1140 struct object
*o
, enum object_type expected_type
)
1142 if (name
&& !namelen
)
1143 namelen
= strlen(name
);
1145 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1147 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1149 if (o
->type
== OBJ_TAG
)
1150 o
= ((struct tag
*) o
)->tagged
;
1151 else if (o
->type
== OBJ_COMMIT
)
1152 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1155 error("%.*s: expected %s type, but the object "
1156 "dereferences to %s type",
1157 namelen
, name
, type_name(expected_type
),
1158 type_name(o
->type
));
1164 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1165 struct object_id
*oid
, unsigned lookup_flags
)
1167 struct object_id outer
;
1169 unsigned int expected_type
= 0;
1173 * "ref^{type}" dereferences ref repeatedly until you cannot
1174 * dereference anymore, or you get an object of given type,
1175 * whichever comes first. "ref^{}" means just dereference
1176 * tags until you get a non-tag. "ref^0" is a shorthand for
1177 * "ref^{commit}". "commit^{tree}" could be used to find the
1178 * top-level tree of the given commit.
1180 if (len
< 4 || name
[len
-1] != '}')
1183 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1185 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1191 sp
++; /* beginning of type name, or closing brace for empty */
1192 if (starts_with(sp
, "commit}"))
1193 expected_type
= OBJ_COMMIT
;
1194 else if (starts_with(sp
, "tag}"))
1195 expected_type
= OBJ_TAG
;
1196 else if (starts_with(sp
, "tree}"))
1197 expected_type
= OBJ_TREE
;
1198 else if (starts_with(sp
, "blob}"))
1199 expected_type
= OBJ_BLOB
;
1200 else if (starts_with(sp
, "object}"))
1201 expected_type
= OBJ_ANY
;
1202 else if (sp
[0] == '}')
1203 expected_type
= OBJ_NONE
;
1204 else if (sp
[0] == '/')
1205 expected_type
= OBJ_COMMIT
;
1209 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1210 if (expected_type
== OBJ_COMMIT
)
1211 lookup_flags
|= GET_OID_COMMITTISH
;
1212 else if (expected_type
== OBJ_TREE
)
1213 lookup_flags
|= GET_OID_TREEISH
;
1215 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1218 o
= parse_object(r
, &outer
);
1221 if (!expected_type
) {
1222 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1223 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1225 oidcpy(oid
, &o
->oid
);
1230 * At this point, the syntax look correct, so
1231 * if we do not get the needed object, we should
1234 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1238 oidcpy(oid
, &o
->oid
);
1240 /* "$commit^{/foo}" */
1243 struct commit_list
*list
= NULL
;
1246 * $commit^{/}. Some regex implementation may reject.
1247 * We don't need regex anyway. '' pattern always matches.
1252 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1253 commit_list_insert((struct commit
*)o
, &list
);
1254 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1261 static int get_describe_name(struct repository
*r
,
1262 const char *name
, int len
,
1263 struct object_id
*oid
)
1266 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1268 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1270 if (!isxdigit(ch
)) {
1271 /* We must be looking at g in "SOMETHING-g"
1272 * for it to be describe output.
1274 if (ch
== 'g' && cp
[-1] == '-') {
1277 return get_short_oid(r
,
1278 cp
, len
, oid
, flags
);
1285 static enum get_oid_result
get_oid_1(struct repository
*r
,
1286 const char *name
, int len
,
1287 struct object_id
*oid
,
1288 unsigned lookup_flags
)
1290 int ret
, has_suffix
;
1294 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1297 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1299 if ('0' <= ch
&& ch
<= '9')
1301 if (ch
== '~' || ch
== '^')
1307 unsigned int num
= 0;
1308 int len1
= cp
- name
;
1310 while (cp
< name
+ len
) {
1311 unsigned int digit
= *cp
++ - '0';
1312 if (unsigned_mult_overflows(num
, 10))
1313 return MISSING_OBJECT
;
1315 if (unsigned_add_overflows(num
, digit
))
1316 return MISSING_OBJECT
;
1319 if (!num
&& len1
== len
- 1)
1321 else if (num
> INT_MAX
)
1322 return MISSING_OBJECT
;
1323 if (has_suffix
== '^')
1324 return get_parent(r
, name
, len1
, oid
, num
);
1325 /* else if (has_suffix == '~') -- goes without saying */
1326 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1329 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1333 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1337 /* It could be describe output that is "SOMETHING-gXXXX" */
1338 ret
= get_describe_name(r
, name
, len
, oid
);
1342 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1346 * This interprets names like ':/Initial revision of "git"' by searching
1347 * through history and returning the first commit whose message starts
1348 * the given regular expression.
1350 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1352 * For a literal '!' character at the beginning of a pattern, you have to repeat
1353 * that, like: ':/!!foo'
1355 * For future extension, all other sequences beginning with ':/!' are reserved.
1358 /* Remember to update object flag allocation in object.h */
1359 #define ONELINE_SEEN (1u<<20)
1361 struct handle_one_ref_cb
{
1362 struct repository
*repo
;
1363 struct commit_list
**list
;
1366 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1370 struct handle_one_ref_cb
*cb
= cb_data
;
1371 struct commit_list
**list
= cb
->list
;
1372 struct object
*object
= parse_object(cb
->repo
, oid
);
1375 if (object
->type
== OBJ_TAG
) {
1376 object
= deref_tag(cb
->repo
, object
, path
,
1381 if (object
->type
!= OBJ_COMMIT
)
1383 commit_list_insert((struct commit
*)object
, list
);
1387 static int get_oid_oneline(struct repository
*r
,
1388 const char *prefix
, struct object_id
*oid
,
1389 struct commit_list
*list
)
1391 struct commit_list
*backup
= NULL
, *l
;
1396 if (prefix
[0] == '!') {
1399 if (prefix
[0] == '-') {
1402 } else if (prefix
[0] != '!') {
1407 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1410 for (l
= list
; l
; l
= l
->next
) {
1411 l
->item
->object
.flags
|= ONELINE_SEEN
;
1412 commit_list_insert(l
->item
, &backup
);
1415 const char *p
, *buf
;
1416 struct commit
*commit
;
1419 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1420 if (!parse_object(r
, &commit
->object
.oid
))
1422 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1423 p
= strstr(buf
, "\n\n");
1424 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1425 repo_unuse_commit_buffer(r
, commit
, buf
);
1428 oidcpy(oid
, &commit
->object
.oid
);
1434 free_commit_list(list
);
1435 for (l
= backup
; l
; l
= l
->next
)
1436 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1437 free_commit_list(backup
);
1438 return found
? 0 : -1;
1441 struct grab_nth_branch_switch_cbdata
{
1446 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1447 struct object_id
*noid UNUSED
,
1448 const char *email UNUSED
,
1449 timestamp_t timestamp UNUSED
,
1451 const char *message
, void *cb_data
)
1453 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1454 const char *match
= NULL
, *target
= NULL
;
1457 if (skip_prefix(message
, "checkout: moving from ", &match
))
1458 target
= strstr(match
, " to ");
1460 if (!match
|| !target
)
1462 if (--(cb
->remaining
) == 0) {
1463 len
= target
- match
;
1464 strbuf_reset(cb
->sb
);
1465 strbuf_add(cb
->sb
, match
, len
);
1466 return 1; /* we are done */
1472 * Parse @{-N} syntax, return the number of characters parsed
1473 * if successful; otherwise signal an error with negative value.
1475 static int interpret_nth_prior_checkout(struct repository
*r
,
1476 const char *name
, int namelen
,
1481 struct grab_nth_branch_switch_cbdata cb
;
1487 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1489 brace
= memchr(name
, '}', namelen
);
1492 nth
= strtol(name
+ 3, &num_end
, 10);
1493 if (num_end
!= brace
)
1500 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1501 "HEAD", grab_nth_branch_switch
, &cb
);
1503 retval
= brace
- name
+ 1;
1510 int repo_get_oid_mb(struct repository
*r
,
1512 struct object_id
*oid
)
1514 struct commit
*one
, *two
;
1515 struct commit_list
*mbs
= NULL
;
1516 struct object_id oid_tmp
;
1520 dots
= strstr(name
, "...");
1522 return repo_get_oid(r
, name
, oid
);
1524 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1527 strbuf_init(&sb
, dots
- name
);
1528 strbuf_add(&sb
, name
, dots
- name
);
1529 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1530 strbuf_release(&sb
);
1534 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1538 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1540 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1543 if (repo_get_merge_bases(r
, one
, two
, &mbs
) < 0) {
1544 free_commit_list(mbs
);
1547 if (!mbs
|| mbs
->next
)
1551 oidcpy(oid
, &mbs
->item
->object
.oid
);
1553 free_commit_list(mbs
);
1557 /* parse @something syntax, when 'something' is not {.*} */
1558 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1562 if (len
|| name
[1] == '{')
1565 /* make sure it's a single @, or @@{.*}, not @foo */
1566 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1567 if (next
&& next
[1] != '{')
1570 next
= name
+ namelen
;
1571 if (next
!= name
+ 1)
1575 strbuf_add(buf
, "HEAD", 4);
1579 static int reinterpret(struct repository
*r
,
1580 const char *name
, int namelen
, int len
,
1581 struct strbuf
*buf
, unsigned allowed
)
1583 /* we have extra data, which might need further processing */
1584 struct strbuf tmp
= STRBUF_INIT
;
1585 int used
= buf
->len
;
1587 struct interpret_branch_name_options options
= {
1591 strbuf_add(buf
, name
+ len
, namelen
- len
);
1592 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1593 /* that data was not interpreted, remove our cruft */
1595 strbuf_setlen(buf
, used
);
1599 strbuf_addbuf(buf
, &tmp
);
1600 strbuf_release(&tmp
);
1601 /* tweak for size of {-N} versus expanded ref name */
1602 return ret
- used
+ len
;
1605 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1607 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1609 strbuf_addstr(buf
, s
);
1613 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1618 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1619 starts_with(refname
, "refs/heads/"))
1621 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1622 starts_with(refname
, "refs/remotes/"))
1628 static int interpret_branch_mark(struct repository
*r
,
1629 const char *name
, int namelen
,
1630 int at
, struct strbuf
*buf
,
1631 int (*get_mark
)(const char *, int),
1632 const char *(*get_data
)(struct branch
*,
1634 const struct interpret_branch_name_options
*options
)
1637 struct branch
*branch
;
1638 struct strbuf err
= STRBUF_INIT
;
1641 len
= get_mark(name
+ at
, namelen
- at
);
1645 if (memchr(name
, ':', at
))
1649 char *name_str
= xmemdupz(name
, at
);
1650 branch
= branch_get(name_str
);
1653 branch
= branch_get(NULL
);
1655 value
= get_data(branch
, &err
);
1657 if (options
->nonfatal_dangling_mark
) {
1658 strbuf_release(&err
);
1665 if (!branch_interpret_allowed(value
, options
->allowed
))
1668 set_shortened_ref(r
, buf
, value
);
1672 int repo_interpret_branch_name(struct repository
*r
,
1673 const char *name
, int namelen
,
1675 const struct interpret_branch_name_options
*options
)
1682 namelen
= strlen(name
);
1684 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1685 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1687 return len
; /* syntax Ok, not enough switches */
1688 } else if (len
> 0) {
1690 return len
; /* consumed all */
1692 return reinterpret(r
, name
, namelen
, len
, buf
,
1698 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1701 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1702 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1704 return reinterpret(r
, name
, namelen
, len
, buf
,
1708 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1709 upstream_mark
, branch_get_upstream
,
1714 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1715 push_mark
, branch_get_push
,
1724 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1726 int len
= strlen(name
);
1727 struct interpret_branch_name_options options
= {
1730 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1735 strbuf_add(sb
, name
+ used
, len
- used
);
1738 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1740 if (startup_info
->have_repository
)
1741 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1743 strbuf_addstr(sb
, name
);
1746 * This splice must be done even if we end up rejecting the
1747 * name; builtin/branch.c::copy_or_rename_branch() still wants
1748 * to see what the name expanded to so that "branch -m" can be
1749 * used as a tool to correct earlier mistakes.
1751 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1754 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1757 return check_refname_format(sb
->buf
, 0);
1761 * This is like "get_oid_basic()", except it allows "object ID expressions",
1762 * notably "xyz^" for "parent of xyz"
1764 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1766 struct object_context unused
;
1767 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1771 * This returns a non-zero value if the string (built using printf
1772 * format and the given arguments) is not a valid object.
1774 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1778 struct strbuf sb
= STRBUF_INIT
;
1781 strbuf_vaddf(&sb
, fmt
, ap
);
1784 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1785 strbuf_release(&sb
);
1791 * Many callers know that the user meant to name a commit-ish by
1792 * syntactical positions where the object name appears. Calling this
1793 * function allows the machinery to disambiguate shorter-than-unique
1794 * abbreviated object names between commit-ish and others.
1796 * Note that this does NOT error out when the named object is not a
1797 * commit-ish. It is merely to give a hint to the disambiguation
1800 int repo_get_oid_committish(struct repository
*r
,
1802 struct object_id
*oid
)
1804 struct object_context unused
;
1805 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1809 int repo_get_oid_treeish(struct repository
*r
,
1811 struct object_id
*oid
)
1813 struct object_context unused
;
1814 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1818 int repo_get_oid_commit(struct repository
*r
,
1820 struct object_id
*oid
)
1822 struct object_context unused
;
1823 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1827 int repo_get_oid_tree(struct repository
*r
,
1829 struct object_id
*oid
)
1831 struct object_context unused
;
1832 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1836 int repo_get_oid_blob(struct repository
*r
,
1838 struct object_id
*oid
)
1840 struct object_context unused
;
1841 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1845 /* Must be called only when object_name:filename doesn't exist. */
1846 static void diagnose_invalid_oid_path(struct repository
*r
,
1848 const char *filename
,
1849 const struct object_id
*tree_oid
,
1850 const char *object_name
,
1851 int object_name_len
)
1853 struct object_id oid
;
1854 unsigned short mode
;
1859 if (file_exists(filename
))
1860 die(_("path '%s' exists on disk, but not in '%.*s'"),
1861 filename
, object_name_len
, object_name
);
1862 if (is_missing_file_error(errno
)) {
1863 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1865 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1866 die(_("path '%s' exists, but not '%s'\n"
1867 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1870 object_name_len
, object_name
,
1872 object_name_len
, object_name
,
1875 die(_("path '%s' does not exist in '%.*s'"),
1876 filename
, object_name_len
, object_name
);
1880 /* Must be called only when :stage:filename doesn't exist. */
1881 static void diagnose_invalid_index_path(struct repository
*r
,
1884 const char *filename
)
1886 struct index_state
*istate
= r
->index
;
1887 const struct cache_entry
*ce
;
1889 unsigned namelen
= strlen(filename
);
1890 struct strbuf fullname
= STRBUF_INIT
;
1895 /* Wrong stage number? */
1896 pos
= index_name_pos(istate
, filename
, namelen
);
1899 if (pos
< istate
->cache_nr
) {
1900 ce
= istate
->cache
[pos
];
1901 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1902 ce_namelen(ce
) == namelen
&&
1903 !memcmp(ce
->name
, filename
, namelen
))
1904 die(_("path '%s' is in the index, but not at stage %d\n"
1905 "hint: Did you mean ':%d:%s'?"),
1907 ce_stage(ce
), filename
);
1910 /* Confusion between relative and absolute filenames? */
1911 strbuf_addstr(&fullname
, prefix
);
1912 strbuf_addstr(&fullname
, filename
);
1913 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1916 if (pos
< istate
->cache_nr
) {
1917 ce
= istate
->cache
[pos
];
1918 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1919 ce_namelen(ce
) == fullname
.len
&&
1920 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1921 die(_("path '%s' is in the index, but not '%s'\n"
1922 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1923 fullname
.buf
, filename
,
1924 ce_stage(ce
), fullname
.buf
,
1925 ce_stage(ce
), filename
);
1928 if (repo_file_exists(r
, filename
))
1929 die(_("path '%s' exists on disk, but not in the index"), filename
);
1930 if (is_missing_file_error(errno
))
1931 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1934 strbuf_release(&fullname
);
1938 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1940 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1943 if (r
!= the_repository
|| !is_inside_work_tree())
1944 die(_("relative path syntax can't be used outside working tree"));
1946 /* die() inside prefix_path() if resolved path is outside worktree */
1947 return prefix_path(startup_info
->prefix
,
1948 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1952 static int reject_tree_in_index(struct repository
*repo
,
1954 const struct cache_entry
*ce
,
1959 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1962 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1966 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1970 struct object_id
*oid
,
1971 struct object_context
*oc
)
1973 int ret
, bracket_depth
;
1974 int namelen
= strlen(name
);
1976 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1978 memset(oc
, 0, sizeof(*oc
));
1979 oc
->mode
= S_IFINVALID
;
1980 strbuf_init(&oc
->symlink_path
, 0);
1981 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1982 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1983 die(_("<object>:<path> required, only <object> '%s' given"),
1988 * tree:path --> object name of path in tree
1989 * :path -> object name of absolute path in index
1990 * :./path -> object name of path relative to cwd in index
1991 * :[0-3]:path -> object name of path in index at stage
1992 * :/foo -> recent commit matching foo
1994 if (name
[0] == ':') {
1996 const struct cache_entry
*ce
;
1997 char *new_path
= NULL
;
1999 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
2000 struct handle_one_ref_cb cb
;
2001 struct commit_list
*list
= NULL
;
2005 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
2006 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
2007 commit_list_sort_by_date(&list
);
2008 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
2012 name
[1] < '0' || '3' < name
[1])
2015 stage
= name
[1] - '0';
2018 new_path
= resolve_relative_path(repo
, cp
);
2020 namelen
= namelen
- (cp
- name
);
2023 namelen
= strlen(cp
);
2026 if (flags
& GET_OID_RECORD_PATH
)
2027 oc
->path
= xstrdup(cp
);
2029 if (!repo
->index
|| !repo
->index
->cache
)
2030 repo_read_index(repo
);
2031 pos
= index_name_pos(repo
->index
, cp
, namelen
);
2034 while (pos
< repo
->index
->cache_nr
) {
2035 ce
= repo
->index
->cache
[pos
];
2036 if (ce_namelen(ce
) != namelen
||
2037 memcmp(ce
->name
, cp
, namelen
))
2039 if (ce_stage(ce
) == stage
) {
2041 if (reject_tree_in_index(repo
, only_to_die
, ce
,
2044 oidcpy(oid
, &ce
->oid
);
2045 oc
->mode
= ce
->ce_mode
;
2050 if (only_to_die
&& name
[1] && name
[1] != '/')
2051 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2055 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2058 else if (bracket_depth
&& *cp
== '}')
2060 else if (!bracket_depth
&& *cp
== ':')
2064 struct object_id tree_oid
;
2065 int len
= cp
- name
;
2066 unsigned sub_flags
= flags
;
2068 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2069 sub_flags
|= GET_OID_TREEISH
;
2071 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2072 const char *filename
= cp
+1;
2073 char *new_filename
= NULL
;
2075 new_filename
= resolve_relative_path(repo
, filename
);
2077 filename
= new_filename
;
2078 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2079 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2080 filename
, oid
, &oc
->symlink_path
,
2083 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2085 if (ret
&& only_to_die
) {
2086 diagnose_invalid_oid_path(repo
, prefix
,
2092 if (flags
& GET_OID_RECORD_PATH
)
2093 oc
->path
= xstrdup(filename
);
2099 die(_("invalid object name '%.*s'."), len
, name
);
2106 * Call this function when you know "name" given by the end user must
2107 * name an object but it doesn't; the function _may_ die with a better
2108 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2109 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2110 * you have a chance to diagnose the error further.
2112 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2116 struct object_context oc
;
2117 struct object_id oid
;
2118 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2122 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2125 struct object_id
*oid
,
2126 struct object_context
*oc
)
2128 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2129 BUG("incompatible flags for get_oid_with_context");
2130 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);