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
*,
31 const struct commit_list
*);
33 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
35 struct disambiguate_state
{
36 int len
; /* length of prefix in hex chars */
37 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
38 struct object_id bin_pfx
;
40 struct repository
*repo
;
41 disambiguate_hint_fn fn
;
43 struct object_id candidate
;
44 unsigned candidate_exists
:1;
45 unsigned candidate_checked
:1;
46 unsigned candidate_ok
:1;
47 unsigned disambiguate_fn_used
:1;
49 unsigned always_call_fn
:1;
52 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
54 /* The hash algorithm of current has already been filtered */
55 if (ds
->always_call_fn
) {
56 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
59 if (!ds
->candidate_exists
) {
60 /* this is the first candidate */
61 oidcpy(&ds
->candidate
, current
);
62 ds
->candidate_exists
= 1;
64 } else if (oideq(&ds
->candidate
, current
)) {
65 /* the same as what we already have seen */
70 /* cannot disambiguate between ds->candidate and current */
75 if (!ds
->candidate_checked
) {
76 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
77 ds
->disambiguate_fn_used
= 1;
78 ds
->candidate_checked
= 1;
81 if (!ds
->candidate_ok
) {
82 /* discard the candidate; we know it does not satisfy fn */
83 oidcpy(&ds
->candidate
, current
);
84 ds
->candidate_checked
= 0;
88 /* if we reach this point, we know ds->candidate satisfies fn */
89 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
91 * if both current and candidate satisfy fn, we cannot
98 /* otherwise, current can be discarded and candidate is still good */
101 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
103 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
105 struct disambiguate_state
*ds
= arg
;
106 /* no need to call match_hash, oidtree_each did prefix match */
107 update_candidates(ds
, oid
);
108 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
111 static void find_short_object_filename(struct disambiguate_state
*ds
)
113 struct object_directory
*odb
;
115 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
116 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
117 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
120 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
130 if ((*a
^ *b
) & 0xf0)
135 static void unique_in_midx(struct multi_pack_index
*m
,
136 struct disambiguate_state
*ds
)
138 for (; m
; m
= m
->base_midx
) {
139 uint32_t num
, i
, first
= 0;
140 const struct object_id
*current
= NULL
;
141 int len
= ds
->len
> ds
->repo
->hash_algo
->hexsz
?
142 ds
->repo
->hash_algo
->hexsz
: ds
->len
;
147 num
= m
->num_objects
+ m
->num_objects_in_base
;
149 bsearch_one_midx(&ds
->bin_pfx
, m
, &first
);
152 * At this point, "first" is the location of the lowest
153 * object with an object name that could match
154 * "bin_pfx". See if we have 0, 1 or more objects that
155 * actually match(es).
157 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
158 struct object_id oid
;
159 current
= nth_midxed_object_oid(&oid
, m
, i
);
160 if (!match_hash(len
, ds
->bin_pfx
.hash
, current
->hash
))
162 update_candidates(ds
, current
);
167 static void unique_in_pack(struct packed_git
*p
,
168 struct disambiguate_state
*ds
)
170 uint32_t num
, i
, first
= 0;
171 int len
= ds
->len
> ds
->repo
->hash_algo
->hexsz
?
172 ds
->repo
->hash_algo
->hexsz
: ds
->len
;
174 if (p
->multi_pack_index
)
177 if (open_pack_index(p
) || !p
->num_objects
)
180 num
= p
->num_objects
;
181 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
184 * At this point, "first" is the location of the lowest object
185 * with an object name that could match "bin_pfx". See if we have
186 * 0, 1 or more objects that actually match(es).
188 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
189 struct object_id oid
;
190 nth_packed_object_id(&oid
, p
, i
);
191 if (!match_hash(len
, ds
->bin_pfx
.hash
, oid
.hash
))
193 update_candidates(ds
, &oid
);
197 static void find_short_packed_object(struct disambiguate_state
*ds
)
199 struct multi_pack_index
*m
;
200 struct packed_git
*p
;
202 /* Skip, unless oids from the storage hash algorithm are wanted */
203 if (ds
->bin_pfx
.algo
&& (&hash_algos
[ds
->bin_pfx
.algo
] != ds
->repo
->hash_algo
))
206 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
208 unique_in_midx(m
, ds
);
209 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
211 unique_in_pack(p
, ds
);
214 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
215 struct object_id
*oid
)
218 return SHORT_NAME_AMBIGUOUS
;
220 if (!ds
->candidate_exists
)
221 return MISSING_OBJECT
;
223 if (!ds
->candidate_checked
)
225 * If this is the only candidate, there is no point
226 * calling the disambiguation hint callback.
228 * On the other hand, if the current candidate
229 * replaced an earlier candidate that did _not_ pass
230 * the disambiguation hint callback, then we do have
231 * more than one objects that match the short name
232 * given, so we should make sure this one matches;
233 * otherwise, if we discovered this one and the one
234 * that we previously discarded in the reverse order,
235 * we would end up showing different results in the
238 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
239 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
241 if (!ds
->candidate_ok
)
242 return SHORT_NAME_AMBIGUOUS
;
244 oidcpy(oid
, &ds
->candidate
);
248 static int disambiguate_commit_only(struct repository
*r
,
249 const struct object_id
*oid
,
250 void *cb_data UNUSED
)
252 int kind
= oid_object_info(r
, oid
, NULL
);
253 return kind
== OBJ_COMMIT
;
256 static int disambiguate_committish_only(struct repository
*r
,
257 const struct object_id
*oid
,
258 void *cb_data UNUSED
)
263 kind
= oid_object_info(r
, oid
, NULL
);
264 if (kind
== OBJ_COMMIT
)
269 /* We need to do this the hard way... */
270 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
271 if (obj
&& obj
->type
== OBJ_COMMIT
)
276 static int disambiguate_tree_only(struct repository
*r
,
277 const struct object_id
*oid
,
278 void *cb_data UNUSED
)
280 int kind
= oid_object_info(r
, oid
, NULL
);
281 return kind
== OBJ_TREE
;
284 static int disambiguate_treeish_only(struct repository
*r
,
285 const struct object_id
*oid
,
286 void *cb_data UNUSED
)
291 kind
= oid_object_info(r
, oid
, NULL
);
292 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
297 /* We need to do this the hard way... */
298 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
299 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
304 static int disambiguate_blob_only(struct repository
*r
,
305 const struct object_id
*oid
,
306 void *cb_data UNUSED
)
308 int kind
= oid_object_info(r
, oid
, NULL
);
309 return kind
== OBJ_BLOB
;
312 static disambiguate_hint_fn default_disambiguate_hint
;
314 int set_disambiguate_hint_config(const char *var
, const char *value
)
316 static const struct {
318 disambiguate_hint_fn fn
;
321 { "commit", disambiguate_commit_only
},
322 { "committish", disambiguate_committish_only
},
323 { "tree", disambiguate_tree_only
},
324 { "treeish", disambiguate_treeish_only
},
325 { "blob", disambiguate_blob_only
}
330 return config_error_nonbool(var
);
332 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
333 if (!strcasecmp(value
, hints
[i
].name
)) {
334 default_disambiguate_hint
= hints
[i
].fn
;
339 return error("unknown hint type for '%s': %s", var
, value
);
342 static int init_object_disambiguation(struct repository
*r
,
343 const char *name
, int len
,
344 const struct git_hash_algo
*algo
,
345 struct disambiguate_state
*ds
)
349 if (len
< MINIMUM_ABBREV
|| len
> GIT_MAX_HEXSZ
)
352 memset(ds
, 0, sizeof(*ds
));
354 for (i
= 0; i
< len
;i
++) {
355 unsigned char c
= name
[i
];
357 if (c
>= '0' && c
<= '9')
359 else if (c
>= 'a' && c
<= 'f')
361 else if (c
>= 'A' && c
<='F') {
370 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
374 ds
->hex_pfx
[len
] = '\0';
376 ds
->bin_pfx
.algo
= algo
? hash_algo_by_ptr(algo
) : GIT_HASH_UNKNOWN
;
381 struct ambiguous_output
{
382 const struct disambiguate_state
*ds
;
383 struct strbuf advice
;
387 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
389 struct ambiguous_output
*state
= data
;
390 const struct disambiguate_state
*ds
= state
->ds
;
391 struct strbuf
*advice
= &state
->advice
;
392 struct strbuf
*sb
= &state
->sb
;
396 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
399 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
400 type
= oid_object_info(ds
->repo
, oid
, NULL
);
404 * TRANSLATORS: This is a line of ambiguous object
405 * output shown when we cannot look up or parse the
406 * object in question. E.g. "deadbeef [bad object]".
408 strbuf_addf(sb
, _("%s [bad object]"), hash
);
412 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
413 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
415 if (type
== OBJ_COMMIT
) {
416 struct strbuf date
= STRBUF_INIT
;
417 struct strbuf msg
= STRBUF_INIT
;
418 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
421 struct pretty_print_context pp
= {0};
422 pp
.date_mode
.type
= DATE_SHORT
;
423 repo_format_commit_message(the_repository
, commit
,
425 repo_format_commit_message(the_repository
, commit
,
430 * TRANSLATORS: This is a line of ambiguous commit
431 * object output. E.g.:
433 * "deadbeef commit 2021-01-01 - Some Commit Message"
435 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
438 strbuf_release(&date
);
439 strbuf_release(&msg
);
440 } else if (type
== OBJ_TAG
) {
441 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
443 if (!parse_tag(tag
) && tag
->tag
) {
445 * TRANSLATORS: This is a line of ambiguous
446 * tag object output. E.g.:
448 * "deadbeef tag 2022-01-01 - Some Tag Message"
450 * The second argument is the YYYY-MM-DD found
453 * The third argument is the "tag" string
456 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
457 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
461 * TRANSLATORS: This is a line of ambiguous
462 * tag object output where we couldn't parse
463 * the tag itself. E.g.:
465 * "deadbeef [bad tag, could not parse it]"
467 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
470 } else if (type
== OBJ_TREE
) {
472 * TRANSLATORS: This is a line of ambiguous <type>
473 * object output. E.g. "deadbeef tree".
475 strbuf_addf(sb
, _("%s tree"), hash
);
476 } else if (type
== OBJ_BLOB
) {
478 * TRANSLATORS: This is a line of ambiguous <type>
479 * object output. E.g. "deadbeef blob".
481 strbuf_addf(sb
, _("%s blob"), hash
);
487 * TRANSLATORS: This is line item of ambiguous object output
488 * from describe_ambiguous_object() above. For RTL languages
489 * you'll probably want to swap the "%s" and leading " " space
492 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
498 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
500 oid_array_append(data
, oid
);
504 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
505 const struct object_id
*oid
,
508 return collect_ambiguous(oid
, data
);
511 static int sort_ambiguous(const void *va
, const void *vb
, void *ctx
)
513 struct repository
*sort_ambiguous_repo
= ctx
;
514 const struct object_id
*a
= va
, *b
= vb
;
515 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
516 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
521 * Sorts by hash within the same object type, just as
522 * oid_array_for_each_unique() would do.
524 if (a_type
== b_type
) {
525 if (a
->algo
== b
->algo
)
528 return a
->algo
> b
->algo
? 1 : -1;
532 * Between object types show tags, then commits, and finally
535 * The object_type enum is commit, tree, blob, tag, but we
536 * want tag, commit, tree blob. Cleverly (perhaps too
537 * cleverly) do that with modulus, since the enum assigns 1 to
538 * commit, so tag becomes 0.
540 a_type_sort
= a_type
% 4;
541 b_type_sort
= b_type
% 4;
542 return a_type_sort
> b_type_sort
? 1 : -1;
545 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
547 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
550 static enum get_oid_result
get_short_oid(struct repository
*r
,
551 const char *name
, int len
,
552 struct object_id
*oid
,
556 struct disambiguate_state ds
;
557 int quietly
= !!(flags
& GET_OID_QUIETLY
);
558 const struct git_hash_algo
*algo
= r
->hash_algo
;
560 if (flags
& GET_OID_HASH_ANY
)
563 if (init_object_disambiguation(r
, name
, len
, algo
, &ds
) < 0)
566 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
567 BUG("multiple get_short_oid disambiguator flags");
569 if (flags
& GET_OID_COMMIT
)
570 ds
.fn
= disambiguate_commit_only
;
571 else if (flags
& GET_OID_COMMITTISH
)
572 ds
.fn
= disambiguate_committish_only
;
573 else if (flags
& GET_OID_TREE
)
574 ds
.fn
= disambiguate_tree_only
;
575 else if (flags
& GET_OID_TREEISH
)
576 ds
.fn
= disambiguate_treeish_only
;
577 else if (flags
& GET_OID_BLOB
)
578 ds
.fn
= disambiguate_blob_only
;
580 ds
.fn
= default_disambiguate_hint
;
582 find_short_object_filename(&ds
);
583 find_short_packed_object(&ds
);
584 status
= finish_object_disambiguation(&ds
, oid
);
587 * If we didn't find it, do the usual reprepare() slow-path,
588 * since the object may have recently been added to the repository
589 * or migrated from loose to packed.
591 if (status
== MISSING_OBJECT
) {
592 reprepare_packed_git(r
);
593 find_short_object_filename(&ds
);
594 find_short_packed_object(&ds
);
595 status
= finish_object_disambiguation(&ds
, oid
);
598 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
599 struct oid_array collect
= OID_ARRAY_INIT
;
600 struct ambiguous_output out
= {
603 .advice
= STRBUF_INIT
,
606 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
609 * We may still have ambiguity if we simply saw a series of
610 * candidates that did not satisfy our hint function. In
611 * that case, we still want to show them, so disable the hint
617 repo_for_each_abbrev(r
, ds
.hex_pfx
, algo
, collect_ambiguous
, &collect
);
618 sort_ambiguous_oid_array(r
, &collect
);
620 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
621 BUG("show_ambiguous_object shouldn't return non-zero");
624 * TRANSLATORS: The argument is the list of ambiguous
625 * objects composed in show_ambiguous_object(). See
626 * its "TRANSLATORS" comments for details.
628 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
630 oid_array_clear(&collect
);
631 strbuf_release(&out
.advice
);
632 strbuf_release(&out
.sb
);
638 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
639 const struct git_hash_algo
*algo
,
640 each_abbrev_fn fn
, void *cb_data
)
642 struct oid_array collect
= OID_ARRAY_INIT
;
643 struct disambiguate_state ds
;
646 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), algo
, &ds
) < 0)
649 ds
.always_call_fn
= 1;
650 ds
.fn
= repo_collect_ambiguous
;
651 ds
.cb_data
= &collect
;
652 find_short_object_filename(&ds
);
653 find_short_packed_object(&ds
);
655 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
656 oid_array_clear(&collect
);
661 * Return the slot of the most-significant bit set in "val". There are various
662 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
663 * probably not a big deal here.
665 static unsigned msb(unsigned long val
)
673 struct min_abbrev_data
{
674 unsigned int init_len
;
675 unsigned int cur_len
;
677 struct repository
*repo
;
678 const struct object_id
*oid
;
681 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
684 static const char hex
[] = "0123456789abcdef";
687 return hex
[oid
->hash
[pos
>> 1] >> 4];
689 return hex
[oid
->hash
[pos
>> 1] & 0xf];
692 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
694 struct min_abbrev_data
*mad
= cb_data
;
696 unsigned int i
= mad
->init_len
;
697 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
700 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
701 mad
->cur_len
= i
+ 1;
706 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
707 const struct object_id
*oid
,
710 return extend_abbrev_len(oid
, cb_data
);
713 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
714 struct min_abbrev_data
*mad
)
716 for (; m
; m
= m
->base_midx
) {
718 uint32_t num
, first
= 0;
719 struct object_id oid
;
720 const struct object_id
*mad_oid
;
725 num
= m
->num_objects
+ m
->num_objects_in_base
;
727 match
= bsearch_one_midx(mad_oid
, m
, &first
);
730 * first is now the position in the packfile where we
731 * would insert mad->hash if it does not exist (or the
732 * position of mad->hash if it does exist). Hence, we
733 * consider a maximum of two objects nearby for the
734 * abbreviation length.
738 if (nth_midxed_object_oid(&oid
, m
, first
))
739 extend_abbrev_len(&oid
, mad
);
740 } else if (first
< num
- 1) {
741 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
742 extend_abbrev_len(&oid
, mad
);
745 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
746 extend_abbrev_len(&oid
, mad
);
748 mad
->init_len
= mad
->cur_len
;
752 static void find_abbrev_len_for_pack(struct packed_git
*p
,
753 struct min_abbrev_data
*mad
)
756 uint32_t num
, first
= 0;
757 struct object_id oid
;
758 const struct object_id
*mad_oid
;
760 if (p
->multi_pack_index
)
763 if (open_pack_index(p
) || !p
->num_objects
)
766 num
= p
->num_objects
;
768 match
= bsearch_pack(mad_oid
, p
, &first
);
771 * first is now the position in the packfile where we would insert
772 * mad->hash if it does not exist (or the position of mad->hash if
773 * it does exist). Hence, we consider a maximum of two objects
774 * nearby for the abbreviation length.
778 if (!nth_packed_object_id(&oid
, p
, first
))
779 extend_abbrev_len(&oid
, mad
);
780 } else if (first
< num
- 1) {
781 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
782 extend_abbrev_len(&oid
, mad
);
785 if (!nth_packed_object_id(&oid
, p
, first
- 1))
786 extend_abbrev_len(&oid
, mad
);
788 mad
->init_len
= mad
->cur_len
;
791 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
793 struct multi_pack_index
*m
;
794 struct packed_git
*p
;
796 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
797 find_abbrev_len_for_midx(m
, mad
);
798 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
799 find_abbrev_len_for_pack(p
, mad
);
802 void strbuf_repo_add_unique_abbrev(struct strbuf
*sb
, struct repository
*repo
,
803 const struct object_id
*oid
, int abbrev_len
)
806 strbuf_grow(sb
, GIT_MAX_HEXSZ
+ 1);
807 r
= repo_find_unique_abbrev_r(repo
, sb
->buf
+ sb
->len
, oid
, abbrev_len
);
808 strbuf_setlen(sb
, sb
->len
+ r
);
811 void strbuf_add_unique_abbrev(struct strbuf
*sb
, const struct object_id
*oid
,
814 strbuf_repo_add_unique_abbrev(sb
, the_repository
, oid
, abbrev_len
);
817 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
818 const struct object_id
*oid
, int len
)
820 const struct git_hash_algo
*algo
=
821 oid
->algo
? &hash_algos
[oid
->algo
] : r
->hash_algo
;
822 struct disambiguate_state ds
;
823 struct min_abbrev_data mad
;
824 struct object_id oid_ret
;
825 const unsigned hexsz
= algo
->hexsz
;
828 unsigned long count
= repo_approximate_object_count(r
);
830 * Add one because the MSB only tells us the highest bit set,
831 * not including the value of all the _other_ bits (so "15"
832 * is only one off of 2^4, but the MSB is the 3rd bit.
834 len
= msb(count
) + 1;
836 * We now know we have on the order of 2^len objects, which
837 * expects a collision at 2^(len/2). But we also care about hex
838 * chars, not bits, and there are 4 bits per hex. So all
839 * together we need to divide by 2 and round up.
841 len
= DIV_ROUND_UP(len
, 2);
843 * For very small repos, we stick with our regular fallback.
845 if (len
< FALLBACK_DEFAULT_ABBREV
)
846 len
= FALLBACK_DEFAULT_ABBREV
;
849 oid_to_hex_r(hex
, oid
);
850 if (len
>= hexsz
|| !len
)
859 find_abbrev_len_packed(&mad
);
861 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, algo
, &ds
) < 0)
864 ds
.fn
= repo_extend_abbrev_len
;
865 ds
.always_call_fn
= 1;
866 ds
.cb_data
= (void *)&mad
;
868 find_short_object_filename(&ds
);
869 (void)finish_object_disambiguation(&ds
, &oid_ret
);
871 hex
[mad
.cur_len
] = 0;
875 const char *repo_find_unique_abbrev(struct repository
*r
,
876 const struct object_id
*oid
,
880 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
881 char *hex
= hexbuffer
[bufno
];
882 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
883 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
887 static int ambiguous_path(const char *path
, int len
)
892 for (cnt
= 0; cnt
< len
; cnt
++) {
912 static inline int at_mark(const char *string
, int len
,
913 const char **suffix
, int nr
)
917 for (i
= 0; i
< nr
; i
++) {
918 int suffix_len
= strlen(suffix
[i
]);
919 if (suffix_len
<= len
920 && !strncasecmp(string
, suffix
[i
], suffix_len
))
926 static inline int upstream_mark(const char *string
, int len
)
928 const char *suffix
[] = { "@{upstream}", "@{u}" };
929 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
932 static inline int push_mark(const char *string
, int len
)
934 const char *suffix
[] = { "@{push}" };
935 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
938 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
939 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
941 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
942 struct object_id
*oid
, unsigned int flags
)
944 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
945 static const char *object_name_msg
= N_(
946 "Git normally never creates a ref that ends with 40 hex characters\n"
947 "because it will be ignored when you just specify 40-hex. These refs\n"
948 "may be created by mistake. For example,\n"
950 " git switch -c $br $(git rev-parse ...)\n"
952 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
953 "examine these refs and maybe delete them. Turn this message off by\n"
954 "running \"git config advice.objectNameWarning false\"");
955 struct object_id tmp_oid
;
956 char *real_ref
= NULL
;
958 int at
, reflog_len
, nth_prior
= 0;
959 int fatal
= !(flags
& GET_OID_QUIETLY
);
961 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
962 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
963 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
964 if (refs_found
> 0) {
965 warning(warn_msg
, len
, str
);
966 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
967 fprintf(stderr
, "%s\n", _(object_name_msg
));
974 /* basic@{time or number or -number} format to query ref-log */
976 if (len
&& str
[len
-1] == '}') {
977 for (at
= len
-4; at
>= 0; at
--) {
978 if (str
[at
] == '@' && str
[at
+1] == '{') {
979 if (str
[at
+2] == '-') {
981 /* @{-N} not at start */
986 if (!upstream_mark(str
+ at
, len
- at
) &&
987 !push_mark(str
+ at
, len
- at
)) {
988 reflog_len
= (len
-1) - (at
+2);
996 /* Accept only unambiguous ref paths. */
997 if (len
&& ambiguous_path(str
, len
))
1001 struct strbuf buf
= STRBUF_INIT
;
1004 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
1005 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
1006 strbuf_release(&buf
);
1012 if (!len
&& reflog_len
)
1013 /* allow "@{...}" to mean the current branch reflog */
1014 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, !fatal
);
1015 else if (reflog_len
)
1016 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
1018 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, !fatal
);
1023 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
1025 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
1026 warning(warn_msg
, len
, str
);
1030 timestamp_t at_time
;
1031 timestamp_t co_time
;
1034 /* Is it asking for N-th entry, or approxidate? */
1035 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
1036 char ch
= str
[at
+2+i
];
1037 if ('0' <= ch
&& ch
<= '9')
1038 nth
= nth
* 10 + ch
- '0';
1042 if (100000000 <= nth
) {
1045 } else if (0 <= nth
)
1049 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
1050 at_time
= approxidate_careful(tmp
, &errors
);
1057 if (read_ref_at(get_main_ref_store(r
),
1058 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1059 &co_time
, &co_tz
, &co_cnt
)) {
1061 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1066 if (!(flags
& GET_OID_QUIETLY
)) {
1067 warning(_("log for '%.*s' only goes back to %s"),
1069 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1071 } else if (nth
== co_cnt
&& !is_null_oid(oid
)) {
1073 * We were asked for the Nth reflog (counting
1074 * from 0), but there were only N entries.
1075 * read_ref_at() will have returned "1" to tell
1076 * us it did not find an entry, but it did
1077 * still fill in the oid with the "old" value,
1081 if (flags
& GET_OID_QUIETLY
) {
1084 die(_("log for '%.*s' only has %d entries"),
1094 static enum get_oid_result
get_parent(struct repository
*r
,
1095 const char *name
, int len
,
1096 struct object_id
*result
, int idx
)
1098 struct object_id oid
;
1099 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1100 GET_OID_COMMITTISH
);
1101 struct commit
*commit
;
1102 struct commit_list
*p
;
1106 commit
= lookup_commit_reference(r
, &oid
);
1107 if (repo_parse_commit(r
, commit
))
1108 return MISSING_OBJECT
;
1110 oidcpy(result
, &commit
->object
.oid
);
1113 p
= commit
->parents
;
1116 oidcpy(result
, &p
->item
->object
.oid
);
1121 return MISSING_OBJECT
;
1124 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1125 const char *name
, int len
,
1126 struct object_id
*result
,
1129 struct object_id oid
;
1130 struct commit
*commit
;
1133 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1136 commit
= lookup_commit_reference(r
, &oid
);
1138 return MISSING_OBJECT
;
1140 while (generation
--) {
1141 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1142 return MISSING_OBJECT
;
1143 commit
= commit
->parents
->item
;
1145 oidcpy(result
, &commit
->object
.oid
);
1149 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1150 struct object
*o
, enum object_type expected_type
)
1152 if (name
&& !namelen
)
1153 namelen
= strlen(name
);
1155 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1157 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1159 if (o
->type
== OBJ_TAG
)
1160 o
= ((struct tag
*) o
)->tagged
;
1161 else if (o
->type
== OBJ_COMMIT
)
1162 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1165 error("%.*s: expected %s type, but the object "
1166 "dereferences to %s type",
1167 namelen
, name
, type_name(expected_type
),
1168 type_name(o
->type
));
1174 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1175 struct object_id
*oid
, unsigned lookup_flags
)
1177 struct object_id outer
;
1179 unsigned int expected_type
= 0;
1183 * "ref^{type}" dereferences ref repeatedly until you cannot
1184 * dereference anymore, or you get an object of given type,
1185 * whichever comes first. "ref^{}" means just dereference
1186 * tags until you get a non-tag. "ref^0" is a shorthand for
1187 * "ref^{commit}". "commit^{tree}" could be used to find the
1188 * top-level tree of the given commit.
1190 if (len
< 4 || name
[len
-1] != '}')
1193 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1195 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1201 sp
++; /* beginning of type name, or closing brace for empty */
1202 if (starts_with(sp
, "commit}"))
1203 expected_type
= OBJ_COMMIT
;
1204 else if (starts_with(sp
, "tag}"))
1205 expected_type
= OBJ_TAG
;
1206 else if (starts_with(sp
, "tree}"))
1207 expected_type
= OBJ_TREE
;
1208 else if (starts_with(sp
, "blob}"))
1209 expected_type
= OBJ_BLOB
;
1210 else if (starts_with(sp
, "object}"))
1211 expected_type
= OBJ_ANY
;
1212 else if (sp
[0] == '}')
1213 expected_type
= OBJ_NONE
;
1214 else if (sp
[0] == '/')
1215 expected_type
= OBJ_COMMIT
;
1219 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1220 if (expected_type
== OBJ_COMMIT
)
1221 lookup_flags
|= GET_OID_COMMITTISH
;
1222 else if (expected_type
== OBJ_TREE
)
1223 lookup_flags
|= GET_OID_TREEISH
;
1225 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1228 o
= parse_object(r
, &outer
);
1231 if (!expected_type
) {
1232 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1233 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1235 oidcpy(oid
, &o
->oid
);
1240 * At this point, the syntax look correct, so
1241 * if we do not get the needed object, we should
1244 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1248 oidcpy(oid
, &o
->oid
);
1250 /* "$commit^{/foo}" */
1253 struct commit_list
*list
= NULL
;
1256 * $commit^{/}. Some regex implementation may reject.
1257 * We don't need regex anyway. '' pattern always matches.
1262 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1263 commit_list_insert((struct commit
*)o
, &list
);
1264 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1266 free_commit_list(list
);
1273 static int get_describe_name(struct repository
*r
,
1274 const char *name
, int len
,
1275 struct object_id
*oid
)
1278 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1280 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1282 if (!isxdigit(ch
)) {
1283 /* We must be looking at g in "SOMETHING-g"
1284 * for it to be describe output.
1286 if (ch
== 'g' && cp
[-1] == '-') {
1289 return get_short_oid(r
,
1290 cp
, len
, oid
, flags
);
1297 static enum get_oid_result
get_oid_1(struct repository
*r
,
1298 const char *name
, int len
,
1299 struct object_id
*oid
,
1300 unsigned lookup_flags
)
1302 int ret
, has_suffix
;
1306 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1309 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1311 if ('0' <= ch
&& ch
<= '9')
1313 if (ch
== '~' || ch
== '^')
1319 unsigned int num
= 0;
1320 int len1
= cp
- name
;
1322 while (cp
< name
+ len
) {
1323 unsigned int digit
= *cp
++ - '0';
1324 if (unsigned_mult_overflows(num
, 10))
1325 return MISSING_OBJECT
;
1327 if (unsigned_add_overflows(num
, digit
))
1328 return MISSING_OBJECT
;
1331 if (!num
&& len1
== len
- 1)
1333 else if (num
> INT_MAX
)
1334 return MISSING_OBJECT
;
1335 if (has_suffix
== '^')
1336 return get_parent(r
, name
, len1
, oid
, num
);
1337 /* else if (has_suffix == '~') -- goes without saying */
1338 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1341 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1345 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1349 /* It could be describe output that is "SOMETHING-gXXXX" */
1350 ret
= get_describe_name(r
, name
, len
, oid
);
1354 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1358 * This interprets names like ':/Initial revision of "git"' by searching
1359 * through history and returning the first commit whose message starts
1360 * the given regular expression.
1362 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1364 * For a literal '!' character at the beginning of a pattern, you have to repeat
1365 * that, like: ':/!!foo'
1367 * For future extension, all other sequences beginning with ':/!' are reserved.
1370 /* Remember to update object flag allocation in object.h */
1371 #define ONELINE_SEEN (1u<<20)
1373 struct handle_one_ref_cb
{
1374 struct repository
*repo
;
1375 struct commit_list
**list
;
1378 static int handle_one_ref(const char *path
, const char *referent UNUSED
, const struct object_id
*oid
,
1382 struct handle_one_ref_cb
*cb
= cb_data
;
1383 struct commit_list
**list
= cb
->list
;
1384 struct object
*object
= parse_object(cb
->repo
, oid
);
1387 if (object
->type
== OBJ_TAG
) {
1388 object
= deref_tag(cb
->repo
, object
, path
,
1393 if (object
->type
!= OBJ_COMMIT
)
1395 commit_list_insert((struct commit
*)object
, list
);
1399 static int get_oid_oneline(struct repository
*r
,
1400 const char *prefix
, struct object_id
*oid
,
1401 const struct commit_list
*list
)
1403 struct commit_list
*copy
= NULL
;
1404 const struct commit_list
*l
;
1409 if (prefix
[0] == '!') {
1412 if (prefix
[0] == '-') {
1415 } else if (prefix
[0] != '!') {
1420 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1423 for (l
= list
; l
; l
= l
->next
) {
1424 l
->item
->object
.flags
|= ONELINE_SEEN
;
1425 commit_list_insert(l
->item
, ©
);
1428 const char *p
, *buf
;
1429 struct commit
*commit
;
1432 commit
= pop_most_recent_commit(©
, ONELINE_SEEN
);
1433 if (!parse_object(r
, &commit
->object
.oid
))
1435 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1436 p
= strstr(buf
, "\n\n");
1437 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1438 repo_unuse_commit_buffer(r
, commit
, buf
);
1441 oidcpy(oid
, &commit
->object
.oid
);
1447 for (l
= list
; l
; l
= l
->next
)
1448 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1449 free_commit_list(copy
);
1450 return found
? 0 : -1;
1453 struct grab_nth_branch_switch_cbdata
{
1458 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1459 struct object_id
*noid UNUSED
,
1460 const char *email UNUSED
,
1461 timestamp_t timestamp UNUSED
,
1463 const char *message
, void *cb_data
)
1465 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1466 const char *match
= NULL
, *target
= NULL
;
1469 if (skip_prefix(message
, "checkout: moving from ", &match
))
1470 target
= strstr(match
, " to ");
1472 if (!match
|| !target
)
1474 if (--(cb
->remaining
) == 0) {
1475 len
= target
- match
;
1476 strbuf_reset(cb
->sb
);
1477 strbuf_add(cb
->sb
, match
, len
);
1478 return 1; /* we are done */
1484 * Parse @{-N} syntax, return the number of characters parsed
1485 * if successful; otherwise signal an error with negative value.
1487 static int interpret_nth_prior_checkout(struct repository
*r
,
1488 const char *name
, int namelen
,
1493 struct grab_nth_branch_switch_cbdata cb
;
1499 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1501 brace
= memchr(name
, '}', namelen
);
1504 nth
= strtol(name
+ 3, &num_end
, 10);
1505 if (num_end
!= brace
)
1512 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1513 "HEAD", grab_nth_branch_switch
, &cb
);
1515 retval
= brace
- name
+ 1;
1522 int repo_get_oid_mb(struct repository
*r
,
1524 struct object_id
*oid
)
1526 struct commit
*one
, *two
;
1527 struct commit_list
*mbs
= NULL
;
1528 struct object_id oid_tmp
;
1532 dots
= strstr(name
, "...");
1534 return repo_get_oid(r
, name
, oid
);
1536 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1539 strbuf_init(&sb
, dots
- name
);
1540 strbuf_add(&sb
, name
, dots
- name
);
1541 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1542 strbuf_release(&sb
);
1546 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1550 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1552 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1555 if (repo_get_merge_bases(r
, one
, two
, &mbs
) < 0) {
1556 free_commit_list(mbs
);
1559 if (!mbs
|| mbs
->next
)
1563 oidcpy(oid
, &mbs
->item
->object
.oid
);
1565 free_commit_list(mbs
);
1569 /* parse @something syntax, when 'something' is not {.*} */
1570 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1574 if (len
|| name
[1] == '{')
1577 /* make sure it's a single @, or @@{.*}, not @foo */
1578 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1579 if (next
&& next
[1] != '{')
1582 next
= name
+ namelen
;
1583 if (next
!= name
+ 1)
1587 strbuf_add(buf
, "HEAD", 4);
1591 static int reinterpret(struct repository
*r
,
1592 const char *name
, int namelen
, int len
,
1593 struct strbuf
*buf
, unsigned allowed
)
1595 /* we have extra data, which might need further processing */
1596 struct strbuf tmp
= STRBUF_INIT
;
1597 int used
= buf
->len
;
1599 struct interpret_branch_name_options options
= {
1603 strbuf_add(buf
, name
+ len
, namelen
- len
);
1604 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1605 /* that data was not interpreted, remove our cruft */
1607 strbuf_setlen(buf
, used
);
1611 strbuf_addbuf(buf
, &tmp
);
1612 strbuf_release(&tmp
);
1613 /* tweak for size of {-N} versus expanded ref name */
1614 return ret
- used
+ len
;
1617 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1619 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1621 strbuf_addstr(buf
, s
);
1625 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1630 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1631 starts_with(refname
, "refs/heads/"))
1633 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1634 starts_with(refname
, "refs/remotes/"))
1640 static int interpret_branch_mark(struct repository
*r
,
1641 const char *name
, int namelen
,
1642 int at
, struct strbuf
*buf
,
1643 int (*get_mark
)(const char *, int),
1644 const char *(*get_data
)(struct branch
*,
1646 const struct interpret_branch_name_options
*options
)
1649 struct branch
*branch
;
1650 struct strbuf err
= STRBUF_INIT
;
1653 len
= get_mark(name
+ at
, namelen
- at
);
1657 if (memchr(name
, ':', at
))
1661 char *name_str
= xmemdupz(name
, at
);
1662 branch
= branch_get(name_str
);
1665 branch
= branch_get(NULL
);
1667 value
= get_data(branch
, &err
);
1669 if (options
->nonfatal_dangling_mark
) {
1670 strbuf_release(&err
);
1677 if (!branch_interpret_allowed(value
, options
->allowed
))
1680 set_shortened_ref(r
, buf
, value
);
1684 int repo_interpret_branch_name(struct repository
*r
,
1685 const char *name
, int namelen
,
1687 const struct interpret_branch_name_options
*options
)
1694 namelen
= strlen(name
);
1696 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1697 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1699 return len
; /* syntax Ok, not enough switches */
1700 } else if (len
> 0) {
1702 return len
; /* consumed all */
1704 return reinterpret(r
, name
, namelen
, len
, buf
,
1710 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1713 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1714 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1716 return reinterpret(r
, name
, namelen
, len
, buf
,
1720 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1721 upstream_mark
, branch_get_upstream
,
1726 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1727 push_mark
, branch_get_push
,
1736 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1738 int len
= strlen(name
);
1739 struct interpret_branch_name_options options
= {
1742 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1747 strbuf_add(sb
, name
+ used
, len
- used
);
1750 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1752 if (startup_info
->have_repository
)
1753 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1755 strbuf_addstr(sb
, name
);
1758 * This splice must be done even if we end up rejecting the
1759 * name; builtin/branch.c::copy_or_rename_branch() still wants
1760 * to see what the name expanded to so that "branch -m" can be
1761 * used as a tool to correct earlier mistakes.
1763 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1766 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1769 return check_refname_format(sb
->buf
, 0);
1772 void object_context_release(struct object_context
*ctx
)
1775 strbuf_release(&ctx
->symlink_path
);
1779 * This is like "get_oid_basic()", except it allows "object ID expressions",
1780 * notably "xyz^" for "parent of xyz"
1782 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1784 struct object_context unused
;
1785 int ret
= get_oid_with_context(r
, name
, 0, oid
, &unused
);
1786 object_context_release(&unused
);
1791 * This returns a non-zero value if the string (built using printf
1792 * format and the given arguments) is not a valid object.
1794 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1798 struct strbuf sb
= STRBUF_INIT
;
1801 strbuf_vaddf(&sb
, fmt
, ap
);
1804 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1805 strbuf_release(&sb
);
1811 * Many callers know that the user meant to name a commit-ish by
1812 * syntactical positions where the object name appears. Calling this
1813 * function allows the machinery to disambiguate shorter-than-unique
1814 * abbreviated object names between commit-ish and others.
1816 * Note that this does NOT error out when the named object is not a
1817 * commit-ish. It is merely to give a hint to the disambiguation
1820 int repo_get_oid_committish(struct repository
*r
,
1822 struct object_id
*oid
)
1824 struct object_context unused
;
1825 int ret
= get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1827 object_context_release(&unused
);
1831 int repo_get_oid_treeish(struct repository
*r
,
1833 struct object_id
*oid
)
1835 struct object_context unused
;
1836 int ret
= get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1838 object_context_release(&unused
);
1842 int repo_get_oid_commit(struct repository
*r
,
1844 struct object_id
*oid
)
1846 struct object_context unused
;
1847 int ret
= get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1849 object_context_release(&unused
);
1853 int repo_get_oid_tree(struct repository
*r
,
1855 struct object_id
*oid
)
1857 struct object_context unused
;
1858 int ret
= get_oid_with_context(r
, name
, GET_OID_TREE
,
1860 object_context_release(&unused
);
1864 int repo_get_oid_blob(struct repository
*r
,
1866 struct object_id
*oid
)
1868 struct object_context unused
;
1869 int ret
= get_oid_with_context(r
, name
, GET_OID_BLOB
,
1871 object_context_release(&unused
);
1875 /* Must be called only when object_name:filename doesn't exist. */
1876 static void diagnose_invalid_oid_path(struct repository
*r
,
1878 const char *filename
,
1879 const struct object_id
*tree_oid
,
1880 const char *object_name
,
1881 int object_name_len
)
1883 struct object_id oid
;
1884 unsigned short mode
;
1889 if (file_exists(filename
))
1890 die(_("path '%s' exists on disk, but not in '%.*s'"),
1891 filename
, object_name_len
, object_name
);
1892 if (is_missing_file_error(errno
)) {
1893 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1895 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1896 die(_("path '%s' exists, but not '%s'\n"
1897 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1900 object_name_len
, object_name
,
1902 object_name_len
, object_name
,
1905 die(_("path '%s' does not exist in '%.*s'"),
1906 filename
, object_name_len
, object_name
);
1910 /* Must be called only when :stage:filename doesn't exist. */
1911 static void diagnose_invalid_index_path(struct repository
*r
,
1914 const char *filename
)
1916 struct index_state
*istate
= r
->index
;
1917 const struct cache_entry
*ce
;
1919 unsigned namelen
= strlen(filename
);
1920 struct strbuf fullname
= STRBUF_INIT
;
1925 /* Wrong stage number? */
1926 pos
= index_name_pos(istate
, filename
, namelen
);
1929 if (pos
< istate
->cache_nr
) {
1930 ce
= istate
->cache
[pos
];
1931 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1932 ce_namelen(ce
) == namelen
&&
1933 !memcmp(ce
->name
, filename
, namelen
))
1934 die(_("path '%s' is in the index, but not at stage %d\n"
1935 "hint: Did you mean ':%d:%s'?"),
1937 ce_stage(ce
), filename
);
1940 /* Confusion between relative and absolute filenames? */
1941 strbuf_addstr(&fullname
, prefix
);
1942 strbuf_addstr(&fullname
, filename
);
1943 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1946 if (pos
< istate
->cache_nr
) {
1947 ce
= istate
->cache
[pos
];
1948 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1949 ce_namelen(ce
) == fullname
.len
&&
1950 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1951 die(_("path '%s' is in the index, but not '%s'\n"
1952 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1953 fullname
.buf
, filename
,
1954 ce_stage(ce
), fullname
.buf
,
1955 ce_stage(ce
), filename
);
1958 if (repo_file_exists(r
, filename
))
1959 die(_("path '%s' exists on disk, but not in the index"), filename
);
1960 if (is_missing_file_error(errno
))
1961 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1964 strbuf_release(&fullname
);
1968 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1970 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1973 if (r
!= the_repository
|| !is_inside_work_tree())
1974 die(_("relative path syntax can't be used outside working tree"));
1976 /* die() inside prefix_path() if resolved path is outside worktree */
1977 return prefix_path(startup_info
->prefix
,
1978 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1982 static int reject_tree_in_index(struct repository
*repo
,
1984 const struct cache_entry
*ce
,
1989 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1992 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1996 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
2000 struct object_id
*oid
,
2001 struct object_context
*oc
)
2003 int ret
, bracket_depth
;
2004 int namelen
= strlen(name
);
2006 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
2008 memset(oc
, 0, sizeof(*oc
));
2009 oc
->mode
= S_IFINVALID
;
2010 strbuf_init(&oc
->symlink_path
, 0);
2011 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
2012 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
2013 die(_("<object>:<path> required, only <object> '%s' given"),
2018 * tree:path --> object name of path in tree
2019 * :path -> object name of absolute path in index
2020 * :./path -> object name of path relative to cwd in index
2021 * :[0-3]:path -> object name of path in index at stage
2022 * :/foo -> recent commit matching foo
2024 if (name
[0] == ':') {
2026 const struct cache_entry
*ce
;
2027 char *new_path
= NULL
;
2029 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
2030 struct handle_one_ref_cb cb
;
2031 struct commit_list
*list
= NULL
;
2035 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
2036 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
2037 commit_list_sort_by_date(&list
);
2038 ret
= get_oid_oneline(repo
, name
+ 2, oid
, list
);
2040 free_commit_list(list
);
2045 name
[1] < '0' || '3' < name
[1])
2048 stage
= name
[1] - '0';
2051 new_path
= resolve_relative_path(repo
, cp
);
2053 namelen
= namelen
- (cp
- name
);
2056 namelen
= strlen(cp
);
2059 if (flags
& GET_OID_RECORD_PATH
)
2060 oc
->path
= xstrdup(cp
);
2062 if (!repo
->index
|| !repo
->index
->cache
)
2063 repo_read_index(repo
);
2064 pos
= index_name_pos(repo
->index
, cp
, namelen
);
2067 while (pos
< repo
->index
->cache_nr
) {
2068 ce
= repo
->index
->cache
[pos
];
2069 if (ce_namelen(ce
) != namelen
||
2070 memcmp(ce
->name
, cp
, namelen
))
2072 if (ce_stage(ce
) == stage
) {
2074 if (reject_tree_in_index(repo
, only_to_die
, ce
,
2077 oidcpy(oid
, &ce
->oid
);
2078 oc
->mode
= ce
->ce_mode
;
2083 if (only_to_die
&& name
[1] && name
[1] != '/')
2084 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2088 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2091 else if (bracket_depth
&& *cp
== '}')
2093 else if (!bracket_depth
&& *cp
== ':')
2097 struct object_id tree_oid
;
2098 int len
= cp
- name
;
2099 unsigned sub_flags
= flags
;
2101 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2102 sub_flags
|= GET_OID_TREEISH
;
2104 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2105 const char *filename
= cp
+1;
2106 char *new_filename
= NULL
;
2108 new_filename
= resolve_relative_path(repo
, filename
);
2110 filename
= new_filename
;
2111 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2112 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2113 filename
, oid
, &oc
->symlink_path
,
2116 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2118 if (ret
&& only_to_die
) {
2119 diagnose_invalid_oid_path(repo
, prefix
,
2125 if (flags
& GET_OID_RECORD_PATH
)
2126 oc
->path
= xstrdup(filename
);
2132 die(_("invalid object name '%.*s'."), len
, name
);
2139 * Call this function when you know "name" given by the end user must
2140 * name an object but it doesn't; the function _may_ die with a better
2141 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2142 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2143 * you have a chance to diagnose the error further.
2145 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2149 struct object_context oc
;
2150 struct object_id oid
;
2151 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2153 object_context_release(&oc
);
2156 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2159 struct object_id
*oid
,
2160 struct object_context
*oc
)
2162 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2163 BUG("incompatible flags for get_oid_with_context");
2164 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);