1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "object-name.h"
7 #include "environment.h"
13 #include "tree-walk.h"
17 #include "oid-array.h"
21 #include "object-store-ll.h"
22 #include "read-cache-ll.h"
23 #include "repository.h"
26 #include "commit-reach.h"
28 #include "object-file-convert.h"
30 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
32 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
34 struct disambiguate_state
{
35 int len
; /* length of prefix in hex chars */
36 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
37 struct object_id bin_pfx
;
39 struct repository
*repo
;
40 disambiguate_hint_fn fn
;
42 struct object_id candidate
;
43 unsigned candidate_exists
:1;
44 unsigned candidate_checked
:1;
45 unsigned candidate_ok
:1;
46 unsigned disambiguate_fn_used
:1;
48 unsigned always_call_fn
:1;
51 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
53 /* The hash algorithm of current has already been filtered */
54 if (ds
->always_call_fn
) {
55 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
58 if (!ds
->candidate_exists
) {
59 /* this is the first candidate */
60 oidcpy(&ds
->candidate
, current
);
61 ds
->candidate_exists
= 1;
63 } else if (oideq(&ds
->candidate
, current
)) {
64 /* the same as what we already have seen */
69 /* cannot disambiguate between ds->candidate and current */
74 if (!ds
->candidate_checked
) {
75 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
76 ds
->disambiguate_fn_used
= 1;
77 ds
->candidate_checked
= 1;
80 if (!ds
->candidate_ok
) {
81 /* discard the candidate; we know it does not satisfy fn */
82 oidcpy(&ds
->candidate
, current
);
83 ds
->candidate_checked
= 0;
87 /* if we reach this point, we know ds->candidate satisfies fn */
88 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
90 * if both current and candidate satisfy fn, we cannot
97 /* otherwise, current can be discarded and candidate is still good */
100 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
102 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
104 struct disambiguate_state
*ds
= arg
;
105 /* no need to call match_hash, oidtree_each did prefix match */
106 update_candidates(ds
, oid
);
107 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
110 static void find_short_object_filename(struct disambiguate_state
*ds
)
112 struct object_directory
*odb
;
114 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
115 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
116 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
119 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
129 if ((*a
^ *b
) & 0xf0)
134 static void unique_in_midx(struct multi_pack_index
*m
,
135 struct disambiguate_state
*ds
)
137 uint32_t num
, i
, first
= 0;
138 const struct object_id
*current
= NULL
;
139 int len
= ds
->len
> ds
->repo
->hash_algo
->hexsz
?
140 ds
->repo
->hash_algo
->hexsz
: ds
->len
;
141 num
= m
->num_objects
;
146 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
149 * At this point, "first" is the location of the lowest object
150 * with an object name that could match "bin_pfx". See if we have
151 * 0, 1 or more objects that actually match(es).
153 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
154 struct object_id oid
;
155 current
= nth_midxed_object_oid(&oid
, m
, i
);
156 if (!match_hash(len
, ds
->bin_pfx
.hash
, current
->hash
))
158 update_candidates(ds
, current
);
162 static void unique_in_pack(struct packed_git
*p
,
163 struct disambiguate_state
*ds
)
165 uint32_t num
, i
, first
= 0;
166 int len
= ds
->len
> ds
->repo
->hash_algo
->hexsz
?
167 ds
->repo
->hash_algo
->hexsz
: ds
->len
;
169 if (p
->multi_pack_index
)
172 if (open_pack_index(p
) || !p
->num_objects
)
175 num
= p
->num_objects
;
176 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
179 * At this point, "first" is the location of the lowest object
180 * with an object name that could match "bin_pfx". See if we have
181 * 0, 1 or more objects that actually match(es).
183 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
184 struct object_id oid
;
185 nth_packed_object_id(&oid
, p
, i
);
186 if (!match_hash(len
, ds
->bin_pfx
.hash
, oid
.hash
))
188 update_candidates(ds
, &oid
);
192 static void find_short_packed_object(struct disambiguate_state
*ds
)
194 struct multi_pack_index
*m
;
195 struct packed_git
*p
;
197 /* Skip, unless oids from the storage hash algorithm are wanted */
198 if (ds
->bin_pfx
.algo
&& (&hash_algos
[ds
->bin_pfx
.algo
] != ds
->repo
->hash_algo
))
201 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
203 unique_in_midx(m
, ds
);
204 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
206 unique_in_pack(p
, ds
);
209 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
210 struct object_id
*oid
)
213 return SHORT_NAME_AMBIGUOUS
;
215 if (!ds
->candidate_exists
)
216 return MISSING_OBJECT
;
218 if (!ds
->candidate_checked
)
220 * If this is the only candidate, there is no point
221 * calling the disambiguation hint callback.
223 * On the other hand, if the current candidate
224 * replaced an earlier candidate that did _not_ pass
225 * the disambiguation hint callback, then we do have
226 * more than one objects that match the short name
227 * given, so we should make sure this one matches;
228 * otherwise, if we discovered this one and the one
229 * that we previously discarded in the reverse order,
230 * we would end up showing different results in the
233 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
234 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
236 if (!ds
->candidate_ok
)
237 return SHORT_NAME_AMBIGUOUS
;
239 oidcpy(oid
, &ds
->candidate
);
243 static int disambiguate_commit_only(struct repository
*r
,
244 const struct object_id
*oid
,
245 void *cb_data UNUSED
)
247 int kind
= oid_object_info(r
, oid
, NULL
);
248 return kind
== OBJ_COMMIT
;
251 static int disambiguate_committish_only(struct repository
*r
,
252 const struct object_id
*oid
,
253 void *cb_data UNUSED
)
258 kind
= oid_object_info(r
, oid
, NULL
);
259 if (kind
== OBJ_COMMIT
)
264 /* We need to do this the hard way... */
265 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
266 if (obj
&& obj
->type
== OBJ_COMMIT
)
271 static int disambiguate_tree_only(struct repository
*r
,
272 const struct object_id
*oid
,
273 void *cb_data UNUSED
)
275 int kind
= oid_object_info(r
, oid
, NULL
);
276 return kind
== OBJ_TREE
;
279 static int disambiguate_treeish_only(struct repository
*r
,
280 const struct object_id
*oid
,
281 void *cb_data UNUSED
)
286 kind
= oid_object_info(r
, oid
, NULL
);
287 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
292 /* We need to do this the hard way... */
293 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
294 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
299 static int disambiguate_blob_only(struct repository
*r
,
300 const struct object_id
*oid
,
301 void *cb_data UNUSED
)
303 int kind
= oid_object_info(r
, oid
, NULL
);
304 return kind
== OBJ_BLOB
;
307 static disambiguate_hint_fn default_disambiguate_hint
;
309 int set_disambiguate_hint_config(const char *var
, const char *value
)
311 static const struct {
313 disambiguate_hint_fn fn
;
316 { "commit", disambiguate_commit_only
},
317 { "committish", disambiguate_committish_only
},
318 { "tree", disambiguate_tree_only
},
319 { "treeish", disambiguate_treeish_only
},
320 { "blob", disambiguate_blob_only
}
325 return config_error_nonbool(var
);
327 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
328 if (!strcasecmp(value
, hints
[i
].name
)) {
329 default_disambiguate_hint
= hints
[i
].fn
;
334 return error("unknown hint type for '%s': %s", var
, value
);
337 static int init_object_disambiguation(struct repository
*r
,
338 const char *name
, int len
,
339 const struct git_hash_algo
*algo
,
340 struct disambiguate_state
*ds
)
344 if (len
< MINIMUM_ABBREV
|| len
> GIT_MAX_HEXSZ
)
347 memset(ds
, 0, sizeof(*ds
));
349 for (i
= 0; i
< len
;i
++) {
350 unsigned char c
= name
[i
];
352 if (c
>= '0' && c
<= '9')
354 else if (c
>= 'a' && c
<= 'f')
356 else if (c
>= 'A' && c
<='F') {
365 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
369 ds
->hex_pfx
[len
] = '\0';
371 ds
->bin_pfx
.algo
= algo
? hash_algo_by_ptr(algo
) : GIT_HASH_UNKNOWN
;
376 struct ambiguous_output
{
377 const struct disambiguate_state
*ds
;
378 struct strbuf advice
;
382 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
384 struct ambiguous_output
*state
= data
;
385 const struct disambiguate_state
*ds
= state
->ds
;
386 struct strbuf
*advice
= &state
->advice
;
387 struct strbuf
*sb
= &state
->sb
;
391 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
394 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
395 type
= oid_object_info(ds
->repo
, oid
, NULL
);
399 * TRANSLATORS: This is a line of ambiguous object
400 * output shown when we cannot look up or parse the
401 * object in question. E.g. "deadbeef [bad object]".
403 strbuf_addf(sb
, _("%s [bad object]"), hash
);
407 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
408 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
410 if (type
== OBJ_COMMIT
) {
411 struct strbuf date
= STRBUF_INIT
;
412 struct strbuf msg
= STRBUF_INIT
;
413 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
416 struct pretty_print_context pp
= {0};
417 pp
.date_mode
.type
= DATE_SHORT
;
418 repo_format_commit_message(the_repository
, commit
,
420 repo_format_commit_message(the_repository
, commit
,
425 * TRANSLATORS: This is a line of ambiguous commit
426 * object output. E.g.:
428 * "deadbeef commit 2021-01-01 - Some Commit Message"
430 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
433 strbuf_release(&date
);
434 strbuf_release(&msg
);
435 } else if (type
== OBJ_TAG
) {
436 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
438 if (!parse_tag(tag
) && tag
->tag
) {
440 * TRANSLATORS: This is a line of ambiguous
441 * tag object output. E.g.:
443 * "deadbeef tag 2022-01-01 - Some Tag Message"
445 * The second argument is the YYYY-MM-DD found
448 * The third argument is the "tag" string
451 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
452 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
456 * TRANSLATORS: This is a line of ambiguous
457 * tag object output where we couldn't parse
458 * the tag itself. E.g.:
460 * "deadbeef [bad tag, could not parse it]"
462 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
465 } else if (type
== OBJ_TREE
) {
467 * TRANSLATORS: This is a line of ambiguous <type>
468 * object output. E.g. "deadbeef tree".
470 strbuf_addf(sb
, _("%s tree"), hash
);
471 } else if (type
== OBJ_BLOB
) {
473 * TRANSLATORS: This is a line of ambiguous <type>
474 * object output. E.g. "deadbeef blob".
476 strbuf_addf(sb
, _("%s blob"), hash
);
482 * TRANSLATORS: This is line item of ambiguous object output
483 * from describe_ambiguous_object() above. For RTL languages
484 * you'll probably want to swap the "%s" and leading " " space
487 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
493 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
495 oid_array_append(data
, oid
);
499 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
500 const struct object_id
*oid
,
503 return collect_ambiguous(oid
, data
);
506 static int sort_ambiguous(const void *va
, const void *vb
, void *ctx
)
508 struct repository
*sort_ambiguous_repo
= ctx
;
509 const struct object_id
*a
= va
, *b
= vb
;
510 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
511 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
516 * Sorts by hash within the same object type, just as
517 * oid_array_for_each_unique() would do.
519 if (a_type
== b_type
) {
520 if (a
->algo
== b
->algo
)
523 return a
->algo
> b
->algo
? 1 : -1;
527 * Between object types show tags, then commits, and finally
530 * The object_type enum is commit, tree, blob, tag, but we
531 * want tag, commit, tree blob. Cleverly (perhaps too
532 * cleverly) do that with modulus, since the enum assigns 1 to
533 * commit, so tag becomes 0.
535 a_type_sort
= a_type
% 4;
536 b_type_sort
= b_type
% 4;
537 return a_type_sort
> b_type_sort
? 1 : -1;
540 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
542 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
545 static enum get_oid_result
get_short_oid(struct repository
*r
,
546 const char *name
, int len
,
547 struct object_id
*oid
,
551 struct disambiguate_state ds
;
552 int quietly
= !!(flags
& GET_OID_QUIETLY
);
553 const struct git_hash_algo
*algo
= r
->hash_algo
;
555 if (flags
& GET_OID_HASH_ANY
)
558 if (init_object_disambiguation(r
, name
, len
, algo
, &ds
) < 0)
561 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
562 BUG("multiple get_short_oid disambiguator flags");
564 if (flags
& GET_OID_COMMIT
)
565 ds
.fn
= disambiguate_commit_only
;
566 else if (flags
& GET_OID_COMMITTISH
)
567 ds
.fn
= disambiguate_committish_only
;
568 else if (flags
& GET_OID_TREE
)
569 ds
.fn
= disambiguate_tree_only
;
570 else if (flags
& GET_OID_TREEISH
)
571 ds
.fn
= disambiguate_treeish_only
;
572 else if (flags
& GET_OID_BLOB
)
573 ds
.fn
= disambiguate_blob_only
;
575 ds
.fn
= default_disambiguate_hint
;
577 find_short_object_filename(&ds
);
578 find_short_packed_object(&ds
);
579 status
= finish_object_disambiguation(&ds
, oid
);
582 * If we didn't find it, do the usual reprepare() slow-path,
583 * since the object may have recently been added to the repository
584 * or migrated from loose to packed.
586 if (status
== MISSING_OBJECT
) {
587 reprepare_packed_git(r
);
588 find_short_object_filename(&ds
);
589 find_short_packed_object(&ds
);
590 status
= finish_object_disambiguation(&ds
, oid
);
593 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
594 struct oid_array collect
= OID_ARRAY_INIT
;
595 struct ambiguous_output out
= {
598 .advice
= STRBUF_INIT
,
601 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
604 * We may still have ambiguity if we simply saw a series of
605 * candidates that did not satisfy our hint function. In
606 * that case, we still want to show them, so disable the hint
612 repo_for_each_abbrev(r
, ds
.hex_pfx
, algo
, collect_ambiguous
, &collect
);
613 sort_ambiguous_oid_array(r
, &collect
);
615 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
616 BUG("show_ambiguous_object shouldn't return non-zero");
619 * TRANSLATORS: The argument is the list of ambiguous
620 * objects composed in show_ambiguous_object(). See
621 * its "TRANSLATORS" comments for details.
623 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
625 oid_array_clear(&collect
);
626 strbuf_release(&out
.advice
);
627 strbuf_release(&out
.sb
);
633 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
634 const struct git_hash_algo
*algo
,
635 each_abbrev_fn fn
, void *cb_data
)
637 struct oid_array collect
= OID_ARRAY_INIT
;
638 struct disambiguate_state ds
;
641 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), algo
, &ds
) < 0)
644 ds
.always_call_fn
= 1;
645 ds
.fn
= repo_collect_ambiguous
;
646 ds
.cb_data
= &collect
;
647 find_short_object_filename(&ds
);
648 find_short_packed_object(&ds
);
650 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
651 oid_array_clear(&collect
);
656 * Return the slot of the most-significant bit set in "val". There are various
657 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
658 * probably not a big deal here.
660 static unsigned msb(unsigned long val
)
668 struct min_abbrev_data
{
669 unsigned int init_len
;
670 unsigned int cur_len
;
672 struct repository
*repo
;
673 const struct object_id
*oid
;
676 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
679 static const char hex
[] = "0123456789abcdef";
682 return hex
[oid
->hash
[pos
>> 1] >> 4];
684 return hex
[oid
->hash
[pos
>> 1] & 0xf];
687 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
689 struct min_abbrev_data
*mad
= cb_data
;
691 unsigned int i
= mad
->init_len
;
692 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
695 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
696 mad
->cur_len
= i
+ 1;
701 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
702 const struct object_id
*oid
,
705 return extend_abbrev_len(oid
, cb_data
);
708 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
709 struct min_abbrev_data
*mad
)
712 uint32_t num
, first
= 0;
713 struct object_id oid
;
714 const struct object_id
*mad_oid
;
719 num
= m
->num_objects
;
721 match
= bsearch_midx(mad_oid
, m
, &first
);
724 * first is now the position in the packfile where we would insert
725 * mad->hash if it does not exist (or the position of mad->hash if
726 * it does exist). Hence, we consider a maximum of two objects
727 * nearby for the abbreviation length.
731 if (nth_midxed_object_oid(&oid
, m
, first
))
732 extend_abbrev_len(&oid
, mad
);
733 } else if (first
< num
- 1) {
734 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
735 extend_abbrev_len(&oid
, mad
);
738 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
739 extend_abbrev_len(&oid
, mad
);
741 mad
->init_len
= mad
->cur_len
;
744 static void find_abbrev_len_for_pack(struct packed_git
*p
,
745 struct min_abbrev_data
*mad
)
748 uint32_t num
, first
= 0;
749 struct object_id oid
;
750 const struct object_id
*mad_oid
;
752 if (p
->multi_pack_index
)
755 if (open_pack_index(p
) || !p
->num_objects
)
758 num
= p
->num_objects
;
760 match
= bsearch_pack(mad_oid
, p
, &first
);
763 * first is now the position in the packfile where we would insert
764 * mad->hash if it does not exist (or the position of mad->hash if
765 * it does exist). Hence, we consider a maximum of two objects
766 * nearby for the abbreviation length.
770 if (!nth_packed_object_id(&oid
, p
, first
))
771 extend_abbrev_len(&oid
, mad
);
772 } else if (first
< num
- 1) {
773 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
774 extend_abbrev_len(&oid
, mad
);
777 if (!nth_packed_object_id(&oid
, p
, first
- 1))
778 extend_abbrev_len(&oid
, mad
);
780 mad
->init_len
= mad
->cur_len
;
783 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
785 struct multi_pack_index
*m
;
786 struct packed_git
*p
;
788 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
789 find_abbrev_len_for_midx(m
, mad
);
790 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
791 find_abbrev_len_for_pack(p
, mad
);
794 void strbuf_repo_add_unique_abbrev(struct strbuf
*sb
, struct repository
*repo
,
795 const struct object_id
*oid
, int abbrev_len
)
798 strbuf_grow(sb
, GIT_MAX_HEXSZ
+ 1);
799 r
= repo_find_unique_abbrev_r(repo
, sb
->buf
+ sb
->len
, oid
, abbrev_len
);
800 strbuf_setlen(sb
, sb
->len
+ r
);
803 void strbuf_add_unique_abbrev(struct strbuf
*sb
, const struct object_id
*oid
,
806 strbuf_repo_add_unique_abbrev(sb
, the_repository
, oid
, abbrev_len
);
809 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
810 const struct object_id
*oid
, int len
)
812 const struct git_hash_algo
*algo
=
813 oid
->algo
? &hash_algos
[oid
->algo
] : r
->hash_algo
;
814 struct disambiguate_state ds
;
815 struct min_abbrev_data mad
;
816 struct object_id oid_ret
;
817 const unsigned hexsz
= algo
->hexsz
;
820 unsigned long count
= repo_approximate_object_count(r
);
822 * Add one because the MSB only tells us the highest bit set,
823 * not including the value of all the _other_ bits (so "15"
824 * is only one off of 2^4, but the MSB is the 3rd bit.
826 len
= msb(count
) + 1;
828 * We now know we have on the order of 2^len objects, which
829 * expects a collision at 2^(len/2). But we also care about hex
830 * chars, not bits, and there are 4 bits per hex. So all
831 * together we need to divide by 2 and round up.
833 len
= DIV_ROUND_UP(len
, 2);
835 * For very small repos, we stick with our regular fallback.
837 if (len
< FALLBACK_DEFAULT_ABBREV
)
838 len
= FALLBACK_DEFAULT_ABBREV
;
841 oid_to_hex_r(hex
, oid
);
842 if (len
>= hexsz
|| !len
)
851 find_abbrev_len_packed(&mad
);
853 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, algo
, &ds
) < 0)
856 ds
.fn
= repo_extend_abbrev_len
;
857 ds
.always_call_fn
= 1;
858 ds
.cb_data
= (void *)&mad
;
860 find_short_object_filename(&ds
);
861 (void)finish_object_disambiguation(&ds
, &oid_ret
);
863 hex
[mad
.cur_len
] = 0;
867 const char *repo_find_unique_abbrev(struct repository
*r
,
868 const struct object_id
*oid
,
872 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
873 char *hex
= hexbuffer
[bufno
];
874 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
875 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
879 static int ambiguous_path(const char *path
, int len
)
884 for (cnt
= 0; cnt
< len
; cnt
++) {
904 static inline int at_mark(const char *string
, int len
,
905 const char **suffix
, int nr
)
909 for (i
= 0; i
< nr
; i
++) {
910 int suffix_len
= strlen(suffix
[i
]);
911 if (suffix_len
<= len
912 && !strncasecmp(string
, suffix
[i
], suffix_len
))
918 static inline int upstream_mark(const char *string
, int len
)
920 const char *suffix
[] = { "@{upstream}", "@{u}" };
921 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
924 static inline int push_mark(const char *string
, int len
)
926 const char *suffix
[] = { "@{push}" };
927 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
930 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
931 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
933 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
934 struct object_id
*oid
, unsigned int flags
)
936 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
937 static const char *object_name_msg
= N_(
938 "Git normally never creates a ref that ends with 40 hex characters\n"
939 "because it will be ignored when you just specify 40-hex. These refs\n"
940 "may be created by mistake. For example,\n"
942 " git switch -c $br $(git rev-parse ...)\n"
944 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
945 "examine these refs and maybe delete them. Turn this message off by\n"
946 "running \"git config advice.objectNameWarning false\"");
947 struct object_id tmp_oid
;
948 char *real_ref
= NULL
;
950 int at
, reflog_len
, nth_prior
= 0;
951 int fatal
= !(flags
& GET_OID_QUIETLY
);
953 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
954 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
955 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
956 if (refs_found
> 0) {
957 warning(warn_msg
, len
, str
);
958 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
959 fprintf(stderr
, "%s\n", _(object_name_msg
));
966 /* basic@{time or number or -number} format to query ref-log */
968 if (len
&& str
[len
-1] == '}') {
969 for (at
= len
-4; at
>= 0; at
--) {
970 if (str
[at
] == '@' && str
[at
+1] == '{') {
971 if (str
[at
+2] == '-') {
973 /* @{-N} not at start */
978 if (!upstream_mark(str
+ at
, len
- at
) &&
979 !push_mark(str
+ at
, len
- at
)) {
980 reflog_len
= (len
-1) - (at
+2);
988 /* Accept only unambiguous ref paths. */
989 if (len
&& ambiguous_path(str
, len
))
993 struct strbuf buf
= STRBUF_INIT
;
996 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
997 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
998 strbuf_release(&buf
);
1004 if (!len
&& reflog_len
)
1005 /* allow "@{...}" to mean the current branch reflog */
1006 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, !fatal
);
1007 else if (reflog_len
)
1008 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
1010 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, !fatal
);
1015 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
1017 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
1018 warning(warn_msg
, len
, str
);
1022 timestamp_t at_time
;
1023 timestamp_t co_time
;
1026 /* Is it asking for N-th entry, or approxidate? */
1027 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
1028 char ch
= str
[at
+2+i
];
1029 if ('0' <= ch
&& ch
<= '9')
1030 nth
= nth
* 10 + ch
- '0';
1034 if (100000000 <= nth
) {
1037 } else if (0 <= nth
)
1041 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
1042 at_time
= approxidate_careful(tmp
, &errors
);
1049 if (read_ref_at(get_main_ref_store(r
),
1050 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1051 &co_time
, &co_tz
, &co_cnt
)) {
1053 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1058 if (!(flags
& GET_OID_QUIETLY
)) {
1059 warning(_("log for '%.*s' only goes back to %s"),
1061 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1063 } else if (nth
== co_cnt
&& !is_null_oid(oid
)) {
1065 * We were asked for the Nth reflog (counting
1066 * from 0), but there were only N entries.
1067 * read_ref_at() will have returned "1" to tell
1068 * us it did not find an entry, but it did
1069 * still fill in the oid with the "old" value,
1073 if (flags
& GET_OID_QUIETLY
) {
1076 die(_("log for '%.*s' only has %d entries"),
1086 static enum get_oid_result
get_parent(struct repository
*r
,
1087 const char *name
, int len
,
1088 struct object_id
*result
, int idx
)
1090 struct object_id oid
;
1091 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1092 GET_OID_COMMITTISH
);
1093 struct commit
*commit
;
1094 struct commit_list
*p
;
1098 commit
= lookup_commit_reference(r
, &oid
);
1099 if (repo_parse_commit(r
, commit
))
1100 return MISSING_OBJECT
;
1102 oidcpy(result
, &commit
->object
.oid
);
1105 p
= commit
->parents
;
1108 oidcpy(result
, &p
->item
->object
.oid
);
1113 return MISSING_OBJECT
;
1116 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1117 const char *name
, int len
,
1118 struct object_id
*result
,
1121 struct object_id oid
;
1122 struct commit
*commit
;
1125 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1128 commit
= lookup_commit_reference(r
, &oid
);
1130 return MISSING_OBJECT
;
1132 while (generation
--) {
1133 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1134 return MISSING_OBJECT
;
1135 commit
= commit
->parents
->item
;
1137 oidcpy(result
, &commit
->object
.oid
);
1141 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1142 struct object
*o
, enum object_type expected_type
)
1144 if (name
&& !namelen
)
1145 namelen
= strlen(name
);
1147 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1149 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1151 if (o
->type
== OBJ_TAG
)
1152 o
= ((struct tag
*) o
)->tagged
;
1153 else if (o
->type
== OBJ_COMMIT
)
1154 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1157 error("%.*s: expected %s type, but the object "
1158 "dereferences to %s type",
1159 namelen
, name
, type_name(expected_type
),
1160 type_name(o
->type
));
1166 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1167 struct object_id
*oid
, unsigned lookup_flags
)
1169 struct object_id outer
;
1171 unsigned int expected_type
= 0;
1175 * "ref^{type}" dereferences ref repeatedly until you cannot
1176 * dereference anymore, or you get an object of given type,
1177 * whichever comes first. "ref^{}" means just dereference
1178 * tags until you get a non-tag. "ref^0" is a shorthand for
1179 * "ref^{commit}". "commit^{tree}" could be used to find the
1180 * top-level tree of the given commit.
1182 if (len
< 4 || name
[len
-1] != '}')
1185 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1187 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1193 sp
++; /* beginning of type name, or closing brace for empty */
1194 if (starts_with(sp
, "commit}"))
1195 expected_type
= OBJ_COMMIT
;
1196 else if (starts_with(sp
, "tag}"))
1197 expected_type
= OBJ_TAG
;
1198 else if (starts_with(sp
, "tree}"))
1199 expected_type
= OBJ_TREE
;
1200 else if (starts_with(sp
, "blob}"))
1201 expected_type
= OBJ_BLOB
;
1202 else if (starts_with(sp
, "object}"))
1203 expected_type
= OBJ_ANY
;
1204 else if (sp
[0] == '}')
1205 expected_type
= OBJ_NONE
;
1206 else if (sp
[0] == '/')
1207 expected_type
= OBJ_COMMIT
;
1211 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1212 if (expected_type
== OBJ_COMMIT
)
1213 lookup_flags
|= GET_OID_COMMITTISH
;
1214 else if (expected_type
== OBJ_TREE
)
1215 lookup_flags
|= GET_OID_TREEISH
;
1217 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1220 o
= parse_object(r
, &outer
);
1223 if (!expected_type
) {
1224 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1225 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1227 oidcpy(oid
, &o
->oid
);
1232 * At this point, the syntax look correct, so
1233 * if we do not get the needed object, we should
1236 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1240 oidcpy(oid
, &o
->oid
);
1242 /* "$commit^{/foo}" */
1245 struct commit_list
*list
= NULL
;
1248 * $commit^{/}. Some regex implementation may reject.
1249 * We don't need regex anyway. '' pattern always matches.
1254 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1255 commit_list_insert((struct commit
*)o
, &list
);
1256 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1263 static int get_describe_name(struct repository
*r
,
1264 const char *name
, int len
,
1265 struct object_id
*oid
)
1268 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1270 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1272 if (!isxdigit(ch
)) {
1273 /* We must be looking at g in "SOMETHING-g"
1274 * for it to be describe output.
1276 if (ch
== 'g' && cp
[-1] == '-') {
1279 return get_short_oid(r
,
1280 cp
, len
, oid
, flags
);
1287 static enum get_oid_result
get_oid_1(struct repository
*r
,
1288 const char *name
, int len
,
1289 struct object_id
*oid
,
1290 unsigned lookup_flags
)
1292 int ret
, has_suffix
;
1296 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1299 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1301 if ('0' <= ch
&& ch
<= '9')
1303 if (ch
== '~' || ch
== '^')
1309 unsigned int num
= 0;
1310 int len1
= cp
- name
;
1312 while (cp
< name
+ len
) {
1313 unsigned int digit
= *cp
++ - '0';
1314 if (unsigned_mult_overflows(num
, 10))
1315 return MISSING_OBJECT
;
1317 if (unsigned_add_overflows(num
, digit
))
1318 return MISSING_OBJECT
;
1321 if (!num
&& len1
== len
- 1)
1323 else if (num
> INT_MAX
)
1324 return MISSING_OBJECT
;
1325 if (has_suffix
== '^')
1326 return get_parent(r
, name
, len1
, oid
, num
);
1327 /* else if (has_suffix == '~') -- goes without saying */
1328 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1331 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1335 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1339 /* It could be describe output that is "SOMETHING-gXXXX" */
1340 ret
= get_describe_name(r
, name
, len
, oid
);
1344 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1348 * This interprets names like ':/Initial revision of "git"' by searching
1349 * through history and returning the first commit whose message starts
1350 * the given regular expression.
1352 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1354 * For a literal '!' character at the beginning of a pattern, you have to repeat
1355 * that, like: ':/!!foo'
1357 * For future extension, all other sequences beginning with ':/!' are reserved.
1360 /* Remember to update object flag allocation in object.h */
1361 #define ONELINE_SEEN (1u<<20)
1363 struct handle_one_ref_cb
{
1364 struct repository
*repo
;
1365 struct commit_list
**list
;
1368 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1372 struct handle_one_ref_cb
*cb
= cb_data
;
1373 struct commit_list
**list
= cb
->list
;
1374 struct object
*object
= parse_object(cb
->repo
, oid
);
1377 if (object
->type
== OBJ_TAG
) {
1378 object
= deref_tag(cb
->repo
, object
, path
,
1383 if (object
->type
!= OBJ_COMMIT
)
1385 commit_list_insert((struct commit
*)object
, list
);
1389 static int get_oid_oneline(struct repository
*r
,
1390 const char *prefix
, struct object_id
*oid
,
1391 struct commit_list
*list
)
1393 struct commit_list
*backup
= NULL
, *l
;
1398 if (prefix
[0] == '!') {
1401 if (prefix
[0] == '-') {
1404 } else if (prefix
[0] != '!') {
1409 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1412 for (l
= list
; l
; l
= l
->next
) {
1413 l
->item
->object
.flags
|= ONELINE_SEEN
;
1414 commit_list_insert(l
->item
, &backup
);
1417 const char *p
, *buf
;
1418 struct commit
*commit
;
1421 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1422 if (!parse_object(r
, &commit
->object
.oid
))
1424 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1425 p
= strstr(buf
, "\n\n");
1426 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1427 repo_unuse_commit_buffer(r
, commit
, buf
);
1430 oidcpy(oid
, &commit
->object
.oid
);
1436 free_commit_list(list
);
1437 for (l
= backup
; l
; l
= l
->next
)
1438 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1439 free_commit_list(backup
);
1440 return found
? 0 : -1;
1443 struct grab_nth_branch_switch_cbdata
{
1448 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1449 struct object_id
*noid UNUSED
,
1450 const char *email UNUSED
,
1451 timestamp_t timestamp UNUSED
,
1453 const char *message
, void *cb_data
)
1455 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1456 const char *match
= NULL
, *target
= NULL
;
1459 if (skip_prefix(message
, "checkout: moving from ", &match
))
1460 target
= strstr(match
, " to ");
1462 if (!match
|| !target
)
1464 if (--(cb
->remaining
) == 0) {
1465 len
= target
- match
;
1466 strbuf_reset(cb
->sb
);
1467 strbuf_add(cb
->sb
, match
, len
);
1468 return 1; /* we are done */
1474 * Parse @{-N} syntax, return the number of characters parsed
1475 * if successful; otherwise signal an error with negative value.
1477 static int interpret_nth_prior_checkout(struct repository
*r
,
1478 const char *name
, int namelen
,
1483 struct grab_nth_branch_switch_cbdata cb
;
1489 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1491 brace
= memchr(name
, '}', namelen
);
1494 nth
= strtol(name
+ 3, &num_end
, 10);
1495 if (num_end
!= brace
)
1502 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1503 "HEAD", grab_nth_branch_switch
, &cb
);
1505 retval
= brace
- name
+ 1;
1512 int repo_get_oid_mb(struct repository
*r
,
1514 struct object_id
*oid
)
1516 struct commit
*one
, *two
;
1517 struct commit_list
*mbs
= NULL
;
1518 struct object_id oid_tmp
;
1522 dots
= strstr(name
, "...");
1524 return repo_get_oid(r
, name
, oid
);
1526 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1529 strbuf_init(&sb
, dots
- name
);
1530 strbuf_add(&sb
, name
, dots
- name
);
1531 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1532 strbuf_release(&sb
);
1536 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1540 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1542 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1545 if (repo_get_merge_bases(r
, one
, two
, &mbs
) < 0) {
1546 free_commit_list(mbs
);
1549 if (!mbs
|| mbs
->next
)
1553 oidcpy(oid
, &mbs
->item
->object
.oid
);
1555 free_commit_list(mbs
);
1559 /* parse @something syntax, when 'something' is not {.*} */
1560 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1564 if (len
|| name
[1] == '{')
1567 /* make sure it's a single @, or @@{.*}, not @foo */
1568 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1569 if (next
&& next
[1] != '{')
1572 next
= name
+ namelen
;
1573 if (next
!= name
+ 1)
1577 strbuf_add(buf
, "HEAD", 4);
1581 static int reinterpret(struct repository
*r
,
1582 const char *name
, int namelen
, int len
,
1583 struct strbuf
*buf
, unsigned allowed
)
1585 /* we have extra data, which might need further processing */
1586 struct strbuf tmp
= STRBUF_INIT
;
1587 int used
= buf
->len
;
1589 struct interpret_branch_name_options options
= {
1593 strbuf_add(buf
, name
+ len
, namelen
- len
);
1594 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1595 /* that data was not interpreted, remove our cruft */
1597 strbuf_setlen(buf
, used
);
1601 strbuf_addbuf(buf
, &tmp
);
1602 strbuf_release(&tmp
);
1603 /* tweak for size of {-N} versus expanded ref name */
1604 return ret
- used
+ len
;
1607 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1609 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1611 strbuf_addstr(buf
, s
);
1615 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1620 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1621 starts_with(refname
, "refs/heads/"))
1623 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1624 starts_with(refname
, "refs/remotes/"))
1630 static int interpret_branch_mark(struct repository
*r
,
1631 const char *name
, int namelen
,
1632 int at
, struct strbuf
*buf
,
1633 int (*get_mark
)(const char *, int),
1634 const char *(*get_data
)(struct branch
*,
1636 const struct interpret_branch_name_options
*options
)
1639 struct branch
*branch
;
1640 struct strbuf err
= STRBUF_INIT
;
1643 len
= get_mark(name
+ at
, namelen
- at
);
1647 if (memchr(name
, ':', at
))
1651 char *name_str
= xmemdupz(name
, at
);
1652 branch
= branch_get(name_str
);
1655 branch
= branch_get(NULL
);
1657 value
= get_data(branch
, &err
);
1659 if (options
->nonfatal_dangling_mark
) {
1660 strbuf_release(&err
);
1667 if (!branch_interpret_allowed(value
, options
->allowed
))
1670 set_shortened_ref(r
, buf
, value
);
1674 int repo_interpret_branch_name(struct repository
*r
,
1675 const char *name
, int namelen
,
1677 const struct interpret_branch_name_options
*options
)
1684 namelen
= strlen(name
);
1686 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1687 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1689 return len
; /* syntax Ok, not enough switches */
1690 } else if (len
> 0) {
1692 return len
; /* consumed all */
1694 return reinterpret(r
, name
, namelen
, len
, buf
,
1700 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1703 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1704 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1706 return reinterpret(r
, name
, namelen
, len
, buf
,
1710 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1711 upstream_mark
, branch_get_upstream
,
1716 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1717 push_mark
, branch_get_push
,
1726 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1728 int len
= strlen(name
);
1729 struct interpret_branch_name_options options
= {
1732 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1737 strbuf_add(sb
, name
+ used
, len
- used
);
1740 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1742 if (startup_info
->have_repository
)
1743 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1745 strbuf_addstr(sb
, name
);
1748 * This splice must be done even if we end up rejecting the
1749 * name; builtin/branch.c::copy_or_rename_branch() still wants
1750 * to see what the name expanded to so that "branch -m" can be
1751 * used as a tool to correct earlier mistakes.
1753 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1756 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1759 return check_refname_format(sb
->buf
, 0);
1762 void object_context_release(struct object_context
*ctx
)
1768 * This is like "get_oid_basic()", except it allows "object ID expressions",
1769 * notably "xyz^" for "parent of xyz"
1771 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1773 struct object_context unused
;
1774 int ret
= get_oid_with_context(r
, name
, 0, oid
, &unused
);
1775 object_context_release(&unused
);
1780 * This returns a non-zero value if the string (built using printf
1781 * format and the given arguments) is not a valid object.
1783 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1787 struct strbuf sb
= STRBUF_INIT
;
1790 strbuf_vaddf(&sb
, fmt
, ap
);
1793 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1794 strbuf_release(&sb
);
1800 * Many callers know that the user meant to name a commit-ish by
1801 * syntactical positions where the object name appears. Calling this
1802 * function allows the machinery to disambiguate shorter-than-unique
1803 * abbreviated object names between commit-ish and others.
1805 * Note that this does NOT error out when the named object is not a
1806 * commit-ish. It is merely to give a hint to the disambiguation
1809 int repo_get_oid_committish(struct repository
*r
,
1811 struct object_id
*oid
)
1813 struct object_context unused
;
1814 int ret
= get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1816 object_context_release(&unused
);
1820 int repo_get_oid_treeish(struct repository
*r
,
1822 struct object_id
*oid
)
1824 struct object_context unused
;
1825 int ret
= get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1827 object_context_release(&unused
);
1831 int repo_get_oid_commit(struct repository
*r
,
1833 struct object_id
*oid
)
1835 struct object_context unused
;
1836 int ret
= get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1838 object_context_release(&unused
);
1842 int repo_get_oid_tree(struct repository
*r
,
1844 struct object_id
*oid
)
1846 struct object_context unused
;
1847 int ret
= get_oid_with_context(r
, name
, GET_OID_TREE
,
1849 object_context_release(&unused
);
1853 int repo_get_oid_blob(struct repository
*r
,
1855 struct object_id
*oid
)
1857 struct object_context unused
;
1858 int ret
= get_oid_with_context(r
, name
, GET_OID_BLOB
,
1860 object_context_release(&unused
);
1864 /* Must be called only when object_name:filename doesn't exist. */
1865 static void diagnose_invalid_oid_path(struct repository
*r
,
1867 const char *filename
,
1868 const struct object_id
*tree_oid
,
1869 const char *object_name
,
1870 int object_name_len
)
1872 struct object_id oid
;
1873 unsigned short mode
;
1878 if (file_exists(filename
))
1879 die(_("path '%s' exists on disk, but not in '%.*s'"),
1880 filename
, object_name_len
, object_name
);
1881 if (is_missing_file_error(errno
)) {
1882 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1884 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1885 die(_("path '%s' exists, but not '%s'\n"
1886 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1889 object_name_len
, object_name
,
1891 object_name_len
, object_name
,
1894 die(_("path '%s' does not exist in '%.*s'"),
1895 filename
, object_name_len
, object_name
);
1899 /* Must be called only when :stage:filename doesn't exist. */
1900 static void diagnose_invalid_index_path(struct repository
*r
,
1903 const char *filename
)
1905 struct index_state
*istate
= r
->index
;
1906 const struct cache_entry
*ce
;
1908 unsigned namelen
= strlen(filename
);
1909 struct strbuf fullname
= STRBUF_INIT
;
1914 /* Wrong stage number? */
1915 pos
= index_name_pos(istate
, filename
, namelen
);
1918 if (pos
< istate
->cache_nr
) {
1919 ce
= istate
->cache
[pos
];
1920 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1921 ce_namelen(ce
) == namelen
&&
1922 !memcmp(ce
->name
, filename
, namelen
))
1923 die(_("path '%s' is in the index, but not at stage %d\n"
1924 "hint: Did you mean ':%d:%s'?"),
1926 ce_stage(ce
), filename
);
1929 /* Confusion between relative and absolute filenames? */
1930 strbuf_addstr(&fullname
, prefix
);
1931 strbuf_addstr(&fullname
, filename
);
1932 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1935 if (pos
< istate
->cache_nr
) {
1936 ce
= istate
->cache
[pos
];
1937 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1938 ce_namelen(ce
) == fullname
.len
&&
1939 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1940 die(_("path '%s' is in the index, but not '%s'\n"
1941 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1942 fullname
.buf
, filename
,
1943 ce_stage(ce
), fullname
.buf
,
1944 ce_stage(ce
), filename
);
1947 if (repo_file_exists(r
, filename
))
1948 die(_("path '%s' exists on disk, but not in the index"), filename
);
1949 if (is_missing_file_error(errno
))
1950 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1953 strbuf_release(&fullname
);
1957 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1959 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1962 if (r
!= the_repository
|| !is_inside_work_tree())
1963 die(_("relative path syntax can't be used outside working tree"));
1965 /* die() inside prefix_path() if resolved path is outside worktree */
1966 return prefix_path(startup_info
->prefix
,
1967 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1971 static int reject_tree_in_index(struct repository
*repo
,
1973 const struct cache_entry
*ce
,
1978 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1981 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1985 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1989 struct object_id
*oid
,
1990 struct object_context
*oc
)
1992 int ret
, bracket_depth
;
1993 int namelen
= strlen(name
);
1995 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1997 memset(oc
, 0, sizeof(*oc
));
1998 oc
->mode
= S_IFINVALID
;
1999 strbuf_init(&oc
->symlink_path
, 0);
2000 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
2001 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
2002 die(_("<object>:<path> required, only <object> '%s' given"),
2007 * tree:path --> object name of path in tree
2008 * :path -> object name of absolute path in index
2009 * :./path -> object name of path relative to cwd in index
2010 * :[0-3]:path -> object name of path in index at stage
2011 * :/foo -> recent commit matching foo
2013 if (name
[0] == ':') {
2015 const struct cache_entry
*ce
;
2016 char *new_path
= NULL
;
2018 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
2019 struct handle_one_ref_cb cb
;
2020 struct commit_list
*list
= NULL
;
2024 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
2025 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
2026 commit_list_sort_by_date(&list
);
2027 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
2031 name
[1] < '0' || '3' < name
[1])
2034 stage
= name
[1] - '0';
2037 new_path
= resolve_relative_path(repo
, cp
);
2039 namelen
= namelen
- (cp
- name
);
2042 namelen
= strlen(cp
);
2045 if (flags
& GET_OID_RECORD_PATH
)
2046 oc
->path
= xstrdup(cp
);
2048 if (!repo
->index
|| !repo
->index
->cache
)
2049 repo_read_index(repo
);
2050 pos
= index_name_pos(repo
->index
, cp
, namelen
);
2053 while (pos
< repo
->index
->cache_nr
) {
2054 ce
= repo
->index
->cache
[pos
];
2055 if (ce_namelen(ce
) != namelen
||
2056 memcmp(ce
->name
, cp
, namelen
))
2058 if (ce_stage(ce
) == stage
) {
2060 if (reject_tree_in_index(repo
, only_to_die
, ce
,
2063 oidcpy(oid
, &ce
->oid
);
2064 oc
->mode
= ce
->ce_mode
;
2069 if (only_to_die
&& name
[1] && name
[1] != '/')
2070 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2074 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2077 else if (bracket_depth
&& *cp
== '}')
2079 else if (!bracket_depth
&& *cp
== ':')
2083 struct object_id tree_oid
;
2084 int len
= cp
- name
;
2085 unsigned sub_flags
= flags
;
2087 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2088 sub_flags
|= GET_OID_TREEISH
;
2090 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2091 const char *filename
= cp
+1;
2092 char *new_filename
= NULL
;
2094 new_filename
= resolve_relative_path(repo
, filename
);
2096 filename
= new_filename
;
2097 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2098 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2099 filename
, oid
, &oc
->symlink_path
,
2102 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2104 if (ret
&& only_to_die
) {
2105 diagnose_invalid_oid_path(repo
, prefix
,
2111 if (flags
& GET_OID_RECORD_PATH
)
2112 oc
->path
= xstrdup(filename
);
2118 die(_("invalid object name '%.*s'."), len
, name
);
2125 * Call this function when you know "name" given by the end user must
2126 * name an object but it doesn't; the function _may_ die with a better
2127 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2128 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2129 * you have a chance to diagnose the error further.
2131 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2135 struct object_context oc
;
2136 struct object_id oid
;
2137 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2139 object_context_release(&oc
);
2142 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2145 struct object_id
*oid
,
2146 struct object_context
*oc
)
2148 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2149 BUG("incompatible flags for get_oid_with_context");
2150 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);