1 #include "git-compat-util.h"
2 #include "object-name.h"
5 #include "environment.h"
11 #include "tree-walk.h"
15 #include "oid-array.h"
19 #include "object-store-ll.h"
20 #include "read-cache-ll.h"
21 #include "repository.h"
24 #include "commit-reach.h"
27 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
29 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
31 struct disambiguate_state
{
32 int len
; /* length of prefix in hex chars */
33 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
34 struct object_id bin_pfx
;
36 struct repository
*repo
;
37 disambiguate_hint_fn fn
;
39 struct object_id candidate
;
40 unsigned candidate_exists
:1;
41 unsigned candidate_checked
:1;
42 unsigned candidate_ok
:1;
43 unsigned disambiguate_fn_used
:1;
45 unsigned always_call_fn
:1;
48 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
50 if (ds
->always_call_fn
) {
51 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
54 if (!ds
->candidate_exists
) {
55 /* this is the first candidate */
56 oidcpy(&ds
->candidate
, current
);
57 ds
->candidate_exists
= 1;
59 } else if (oideq(&ds
->candidate
, current
)) {
60 /* the same as what we already have seen */
65 /* cannot disambiguate between ds->candidate and current */
70 if (!ds
->candidate_checked
) {
71 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
72 ds
->disambiguate_fn_used
= 1;
73 ds
->candidate_checked
= 1;
76 if (!ds
->candidate_ok
) {
77 /* discard the candidate; we know it does not satisfy fn */
78 oidcpy(&ds
->candidate
, current
);
79 ds
->candidate_checked
= 0;
83 /* if we reach this point, we know ds->candidate satisfies fn */
84 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
86 * if both current and candidate satisfy fn, we cannot
93 /* otherwise, current can be discarded and candidate is still good */
96 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
98 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
100 struct disambiguate_state
*ds
= arg
;
101 /* no need to call match_hash, oidtree_each did prefix match */
102 update_candidates(ds
, oid
);
103 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
106 static void find_short_object_filename(struct disambiguate_state
*ds
)
108 struct object_directory
*odb
;
110 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
111 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
112 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
115 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
125 if ((*a
^ *b
) & 0xf0)
130 static void unique_in_midx(struct multi_pack_index
*m
,
131 struct disambiguate_state
*ds
)
133 uint32_t num
, i
, first
= 0;
134 const struct object_id
*current
= NULL
;
135 num
= m
->num_objects
;
140 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
143 * At this point, "first" is the location of the lowest object
144 * with an object name that could match "bin_pfx". See if we have
145 * 0, 1 or more objects that actually match(es).
147 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
148 struct object_id oid
;
149 current
= nth_midxed_object_oid(&oid
, m
, i
);
150 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
152 update_candidates(ds
, current
);
156 static void unique_in_pack(struct packed_git
*p
,
157 struct disambiguate_state
*ds
)
159 uint32_t num
, i
, first
= 0;
161 if (p
->multi_pack_index
)
164 if (open_pack_index(p
) || !p
->num_objects
)
167 num
= p
->num_objects
;
168 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
171 * At this point, "first" is the location of the lowest object
172 * with an object name that could match "bin_pfx". See if we have
173 * 0, 1 or more objects that actually match(es).
175 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
176 struct object_id oid
;
177 nth_packed_object_id(&oid
, p
, i
);
178 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, oid
.hash
))
180 update_candidates(ds
, &oid
);
184 static void find_short_packed_object(struct disambiguate_state
*ds
)
186 struct multi_pack_index
*m
;
187 struct packed_git
*p
;
189 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
191 unique_in_midx(m
, ds
);
192 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
194 unique_in_pack(p
, ds
);
197 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
198 struct object_id
*oid
)
201 return SHORT_NAME_AMBIGUOUS
;
203 if (!ds
->candidate_exists
)
204 return MISSING_OBJECT
;
206 if (!ds
->candidate_checked
)
208 * If this is the only candidate, there is no point
209 * calling the disambiguation hint callback.
211 * On the other hand, if the current candidate
212 * replaced an earlier candidate that did _not_ pass
213 * the disambiguation hint callback, then we do have
214 * more than one objects that match the short name
215 * given, so we should make sure this one matches;
216 * otherwise, if we discovered this one and the one
217 * that we previously discarded in the reverse order,
218 * we would end up showing different results in the
221 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
222 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
224 if (!ds
->candidate_ok
)
225 return SHORT_NAME_AMBIGUOUS
;
227 oidcpy(oid
, &ds
->candidate
);
231 static int disambiguate_commit_only(struct repository
*r
,
232 const struct object_id
*oid
,
233 void *cb_data UNUSED
)
235 int kind
= oid_object_info(r
, oid
, NULL
);
236 return kind
== OBJ_COMMIT
;
239 static int disambiguate_committish_only(struct repository
*r
,
240 const struct object_id
*oid
,
241 void *cb_data UNUSED
)
246 kind
= oid_object_info(r
, oid
, NULL
);
247 if (kind
== OBJ_COMMIT
)
252 /* We need to do this the hard way... */
253 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
254 if (obj
&& obj
->type
== OBJ_COMMIT
)
259 static int disambiguate_tree_only(struct repository
*r
,
260 const struct object_id
*oid
,
261 void *cb_data UNUSED
)
263 int kind
= oid_object_info(r
, oid
, NULL
);
264 return kind
== OBJ_TREE
;
267 static int disambiguate_treeish_only(struct repository
*r
,
268 const struct object_id
*oid
,
269 void *cb_data UNUSED
)
274 kind
= oid_object_info(r
, oid
, NULL
);
275 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
280 /* We need to do this the hard way... */
281 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
282 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
287 static int disambiguate_blob_only(struct repository
*r
,
288 const struct object_id
*oid
,
289 void *cb_data UNUSED
)
291 int kind
= oid_object_info(r
, oid
, NULL
);
292 return kind
== OBJ_BLOB
;
295 static disambiguate_hint_fn default_disambiguate_hint
;
297 int set_disambiguate_hint_config(const char *var
, const char *value
)
299 static const struct {
301 disambiguate_hint_fn fn
;
304 { "commit", disambiguate_commit_only
},
305 { "committish", disambiguate_committish_only
},
306 { "tree", disambiguate_tree_only
},
307 { "treeish", disambiguate_treeish_only
},
308 { "blob", disambiguate_blob_only
}
313 return config_error_nonbool(var
);
315 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
316 if (!strcasecmp(value
, hints
[i
].name
)) {
317 default_disambiguate_hint
= hints
[i
].fn
;
322 return error("unknown hint type for '%s': %s", var
, value
);
325 static int init_object_disambiguation(struct repository
*r
,
326 const char *name
, int len
,
327 struct disambiguate_state
*ds
)
331 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
334 memset(ds
, 0, sizeof(*ds
));
336 for (i
= 0; i
< len
;i
++) {
337 unsigned char c
= name
[i
];
339 if (c
>= '0' && c
<= '9')
341 else if (c
>= 'a' && c
<= 'f')
343 else if (c
>= 'A' && c
<='F') {
352 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
356 ds
->hex_pfx
[len
] = '\0';
362 struct ambiguous_output
{
363 const struct disambiguate_state
*ds
;
364 struct strbuf advice
;
368 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
370 struct ambiguous_output
*state
= data
;
371 const struct disambiguate_state
*ds
= state
->ds
;
372 struct strbuf
*advice
= &state
->advice
;
373 struct strbuf
*sb
= &state
->sb
;
377 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
380 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
381 type
= oid_object_info(ds
->repo
, oid
, NULL
);
385 * TRANSLATORS: This is a line of ambiguous object
386 * output shown when we cannot look up or parse the
387 * object in question. E.g. "deadbeef [bad object]".
389 strbuf_addf(sb
, _("%s [bad object]"), hash
);
393 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
394 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
396 if (type
== OBJ_COMMIT
) {
397 struct strbuf date
= STRBUF_INIT
;
398 struct strbuf msg
= STRBUF_INIT
;
399 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
402 struct pretty_print_context pp
= {0};
403 pp
.date_mode
.type
= DATE_SHORT
;
404 repo_format_commit_message(the_repository
, commit
,
406 repo_format_commit_message(the_repository
, commit
,
411 * TRANSLATORS: This is a line of ambiguous commit
412 * object output. E.g.:
414 * "deadbeef commit 2021-01-01 - Some Commit Message"
416 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
419 strbuf_release(&date
);
420 strbuf_release(&msg
);
421 } else if (type
== OBJ_TAG
) {
422 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
424 if (!parse_tag(tag
) && tag
->tag
) {
426 * TRANSLATORS: This is a line of ambiguous
427 * tag object output. E.g.:
429 * "deadbeef tag 2022-01-01 - Some Tag Message"
431 * The second argument is the YYYY-MM-DD found
434 * The third argument is the "tag" string
437 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
438 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
442 * TRANSLATORS: This is a line of ambiguous
443 * tag object output where we couldn't parse
444 * the tag itself. E.g.:
446 * "deadbeef [bad tag, could not parse it]"
448 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
451 } else if (type
== OBJ_TREE
) {
453 * TRANSLATORS: This is a line of ambiguous <type>
454 * object output. E.g. "deadbeef tree".
456 strbuf_addf(sb
, _("%s tree"), hash
);
457 } else if (type
== OBJ_BLOB
) {
459 * TRANSLATORS: This is a line of ambiguous <type>
460 * object output. E.g. "deadbeef blob".
462 strbuf_addf(sb
, _("%s blob"), hash
);
468 * TRANSLATORS: This is line item of ambiguous object output
469 * from describe_ambiguous_object() above. For RTL languages
470 * you'll probably want to swap the "%s" and leading " " space
473 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
479 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
481 oid_array_append(data
, oid
);
485 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
486 const struct object_id
*oid
,
489 return collect_ambiguous(oid
, data
);
492 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
494 struct repository
*sort_ambiguous_repo
= ctx
;
495 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
496 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
501 * Sorts by hash within the same object type, just as
502 * oid_array_for_each_unique() would do.
504 if (a_type
== b_type
)
508 * Between object types show tags, then commits, and finally
511 * The object_type enum is commit, tree, blob, tag, but we
512 * want tag, commit, tree blob. Cleverly (perhaps too
513 * cleverly) do that with modulus, since the enum assigns 1 to
514 * commit, so tag becomes 0.
516 a_type_sort
= a_type
% 4;
517 b_type_sort
= b_type
% 4;
518 return a_type_sort
> b_type_sort
? 1 : -1;
521 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
523 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
526 static enum get_oid_result
get_short_oid(struct repository
*r
,
527 const char *name
, int len
,
528 struct object_id
*oid
,
532 struct disambiguate_state ds
;
533 int quietly
= !!(flags
& GET_OID_QUIETLY
);
535 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
538 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
539 BUG("multiple get_short_oid disambiguator flags");
541 if (flags
& GET_OID_COMMIT
)
542 ds
.fn
= disambiguate_commit_only
;
543 else if (flags
& GET_OID_COMMITTISH
)
544 ds
.fn
= disambiguate_committish_only
;
545 else if (flags
& GET_OID_TREE
)
546 ds
.fn
= disambiguate_tree_only
;
547 else if (flags
& GET_OID_TREEISH
)
548 ds
.fn
= disambiguate_treeish_only
;
549 else if (flags
& GET_OID_BLOB
)
550 ds
.fn
= disambiguate_blob_only
;
552 ds
.fn
= default_disambiguate_hint
;
554 find_short_object_filename(&ds
);
555 find_short_packed_object(&ds
);
556 status
= finish_object_disambiguation(&ds
, oid
);
559 * If we didn't find it, do the usual reprepare() slow-path,
560 * since the object may have recently been added to the repository
561 * or migrated from loose to packed.
563 if (status
== MISSING_OBJECT
) {
564 reprepare_packed_git(r
);
565 find_short_object_filename(&ds
);
566 find_short_packed_object(&ds
);
567 status
= finish_object_disambiguation(&ds
, oid
);
570 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
571 struct oid_array collect
= OID_ARRAY_INIT
;
572 struct ambiguous_output out
= {
575 .advice
= STRBUF_INIT
,
578 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
581 * We may still have ambiguity if we simply saw a series of
582 * candidates that did not satisfy our hint function. In
583 * that case, we still want to show them, so disable the hint
589 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
590 sort_ambiguous_oid_array(r
, &collect
);
592 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
593 BUG("show_ambiguous_object shouldn't return non-zero");
596 * TRANSLATORS: The argument is the list of ambiguous
597 * objects composed in show_ambiguous_object(). See
598 * its "TRANSLATORS" comments for details.
600 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
602 oid_array_clear(&collect
);
603 strbuf_release(&out
.advice
);
604 strbuf_release(&out
.sb
);
610 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
611 each_abbrev_fn fn
, void *cb_data
)
613 struct oid_array collect
= OID_ARRAY_INIT
;
614 struct disambiguate_state ds
;
617 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
620 ds
.always_call_fn
= 1;
621 ds
.fn
= repo_collect_ambiguous
;
622 ds
.cb_data
= &collect
;
623 find_short_object_filename(&ds
);
624 find_short_packed_object(&ds
);
626 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
627 oid_array_clear(&collect
);
632 * Return the slot of the most-significant bit set in "val". There are various
633 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
634 * probably not a big deal here.
636 static unsigned msb(unsigned long val
)
644 struct min_abbrev_data
{
645 unsigned int init_len
;
646 unsigned int cur_len
;
648 struct repository
*repo
;
649 const struct object_id
*oid
;
652 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
655 static const char hex
[] = "0123456789abcdef";
658 return hex
[oid
->hash
[pos
>> 1] >> 4];
660 return hex
[oid
->hash
[pos
>> 1] & 0xf];
663 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
665 struct min_abbrev_data
*mad
= cb_data
;
667 unsigned int i
= mad
->init_len
;
668 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
671 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
672 mad
->cur_len
= i
+ 1;
677 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
678 const struct object_id
*oid
,
681 return extend_abbrev_len(oid
, cb_data
);
684 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
685 struct min_abbrev_data
*mad
)
688 uint32_t num
, first
= 0;
689 struct object_id oid
;
690 const struct object_id
*mad_oid
;
695 num
= m
->num_objects
;
697 match
= bsearch_midx(mad_oid
, m
, &first
);
700 * first is now the position in the packfile where we would insert
701 * mad->hash if it does not exist (or the position of mad->hash if
702 * it does exist). Hence, we consider a maximum of two objects
703 * nearby for the abbreviation length.
707 if (nth_midxed_object_oid(&oid
, m
, first
))
708 extend_abbrev_len(&oid
, mad
);
709 } else if (first
< num
- 1) {
710 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
711 extend_abbrev_len(&oid
, mad
);
714 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
715 extend_abbrev_len(&oid
, mad
);
717 mad
->init_len
= mad
->cur_len
;
720 static void find_abbrev_len_for_pack(struct packed_git
*p
,
721 struct min_abbrev_data
*mad
)
724 uint32_t num
, first
= 0;
725 struct object_id oid
;
726 const struct object_id
*mad_oid
;
728 if (p
->multi_pack_index
)
731 if (open_pack_index(p
) || !p
->num_objects
)
734 num
= p
->num_objects
;
736 match
= bsearch_pack(mad_oid
, p
, &first
);
739 * first is now the position in the packfile where we would insert
740 * mad->hash if it does not exist (or the position of mad->hash if
741 * it does exist). Hence, we consider a maximum of two objects
742 * nearby for the abbreviation length.
746 if (!nth_packed_object_id(&oid
, p
, first
))
747 extend_abbrev_len(&oid
, mad
);
748 } else if (first
< num
- 1) {
749 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
750 extend_abbrev_len(&oid
, mad
);
753 if (!nth_packed_object_id(&oid
, p
, first
- 1))
754 extend_abbrev_len(&oid
, mad
);
756 mad
->init_len
= mad
->cur_len
;
759 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
761 struct multi_pack_index
*m
;
762 struct packed_git
*p
;
764 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
765 find_abbrev_len_for_midx(m
, mad
);
766 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
767 find_abbrev_len_for_pack(p
, mad
);
770 void strbuf_repo_add_unique_abbrev(struct strbuf
*sb
, struct repository
*repo
,
771 const struct object_id
*oid
, int abbrev_len
)
774 strbuf_grow(sb
, GIT_MAX_HEXSZ
+ 1);
775 r
= repo_find_unique_abbrev_r(repo
, sb
->buf
+ sb
->len
, oid
, abbrev_len
);
776 strbuf_setlen(sb
, sb
->len
+ r
);
779 void strbuf_add_unique_abbrev(struct strbuf
*sb
, const struct object_id
*oid
,
782 strbuf_repo_add_unique_abbrev(sb
, the_repository
, oid
, abbrev_len
);
785 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
786 const struct object_id
*oid
, int len
)
788 struct disambiguate_state ds
;
789 struct min_abbrev_data mad
;
790 struct object_id oid_ret
;
791 const unsigned hexsz
= r
->hash_algo
->hexsz
;
794 unsigned long count
= repo_approximate_object_count(r
);
796 * Add one because the MSB only tells us the highest bit set,
797 * not including the value of all the _other_ bits (so "15"
798 * is only one off of 2^4, but the MSB is the 3rd bit.
800 len
= msb(count
) + 1;
802 * We now know we have on the order of 2^len objects, which
803 * expects a collision at 2^(len/2). But we also care about hex
804 * chars, not bits, and there are 4 bits per hex. So all
805 * together we need to divide by 2 and round up.
807 len
= DIV_ROUND_UP(len
, 2);
809 * For very small repos, we stick with our regular fallback.
811 if (len
< FALLBACK_DEFAULT_ABBREV
)
812 len
= FALLBACK_DEFAULT_ABBREV
;
815 oid_to_hex_r(hex
, oid
);
816 if (len
== hexsz
|| !len
)
825 find_abbrev_len_packed(&mad
);
827 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
830 ds
.fn
= repo_extend_abbrev_len
;
831 ds
.always_call_fn
= 1;
832 ds
.cb_data
= (void *)&mad
;
834 find_short_object_filename(&ds
);
835 (void)finish_object_disambiguation(&ds
, &oid_ret
);
837 hex
[mad
.cur_len
] = 0;
841 const char *repo_find_unique_abbrev(struct repository
*r
,
842 const struct object_id
*oid
,
846 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
847 char *hex
= hexbuffer
[bufno
];
848 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
849 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
853 static int ambiguous_path(const char *path
, int len
)
858 for (cnt
= 0; cnt
< len
; cnt
++) {
878 static inline int at_mark(const char *string
, int len
,
879 const char **suffix
, int nr
)
883 for (i
= 0; i
< nr
; i
++) {
884 int suffix_len
= strlen(suffix
[i
]);
885 if (suffix_len
<= len
886 && !strncasecmp(string
, suffix
[i
], suffix_len
))
892 static inline int upstream_mark(const char *string
, int len
)
894 const char *suffix
[] = { "@{upstream}", "@{u}" };
895 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
898 static inline int push_mark(const char *string
, int len
)
900 const char *suffix
[] = { "@{push}" };
901 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
904 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
905 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
907 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
908 struct object_id
*oid
, unsigned int flags
)
910 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
911 static const char *object_name_msg
= N_(
912 "Git normally never creates a ref that ends with 40 hex characters\n"
913 "because it will be ignored when you just specify 40-hex. These refs\n"
914 "may be created by mistake. For example,\n"
916 " git switch -c $br $(git rev-parse ...)\n"
918 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
919 "examine these refs and maybe delete them. Turn this message off by\n"
920 "running \"git config advice.objectNameWarning false\"");
921 struct object_id tmp_oid
;
922 char *real_ref
= NULL
;
924 int at
, reflog_len
, nth_prior
= 0;
925 int fatal
= !(flags
& GET_OID_QUIETLY
);
927 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
928 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
929 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
930 if (refs_found
> 0) {
931 warning(warn_msg
, len
, str
);
932 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
933 fprintf(stderr
, "%s\n", _(object_name_msg
));
940 /* basic@{time or number or -number} format to query ref-log */
942 if (len
&& str
[len
-1] == '}') {
943 for (at
= len
-4; at
>= 0; at
--) {
944 if (str
[at
] == '@' && str
[at
+1] == '{') {
945 if (str
[at
+2] == '-') {
947 /* @{-N} not at start */
952 if (!upstream_mark(str
+ at
, len
- at
) &&
953 !push_mark(str
+ at
, len
- at
)) {
954 reflog_len
= (len
-1) - (at
+2);
962 /* Accept only unambiguous ref paths. */
963 if (len
&& ambiguous_path(str
, len
))
967 struct strbuf buf
= STRBUF_INIT
;
970 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
971 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
972 strbuf_release(&buf
);
978 if (!len
&& reflog_len
)
979 /* allow "@{...}" to mean the current branch reflog */
980 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, !fatal
);
982 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
984 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, !fatal
);
989 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
991 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
992 warning(warn_msg
, len
, str
);
1000 /* Is it asking for N-th entry, or approxidate? */
1001 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
1002 char ch
= str
[at
+2+i
];
1003 if ('0' <= ch
&& ch
<= '9')
1004 nth
= nth
* 10 + ch
- '0';
1008 if (100000000 <= nth
) {
1011 } else if (0 <= nth
)
1015 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
1016 at_time
= approxidate_careful(tmp
, &errors
);
1023 if (read_ref_at(get_main_ref_store(r
),
1024 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1025 &co_time
, &co_tz
, &co_cnt
)) {
1027 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1032 if (!(flags
& GET_OID_QUIETLY
)) {
1033 warning(_("log for '%.*s' only goes back to %s"),
1035 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1037 } else if (nth
== co_cnt
&& !is_null_oid(oid
)) {
1039 * We were asked for the Nth reflog (counting
1040 * from 0), but there were only N entries.
1041 * read_ref_at() will have returned "1" to tell
1042 * us it did not find an entry, but it did
1043 * still fill in the oid with the "old" value,
1047 if (flags
& GET_OID_QUIETLY
) {
1050 die(_("log for '%.*s' only has %d entries"),
1060 static enum get_oid_result
get_parent(struct repository
*r
,
1061 const char *name
, int len
,
1062 struct object_id
*result
, int idx
)
1064 struct object_id oid
;
1065 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1066 GET_OID_COMMITTISH
);
1067 struct commit
*commit
;
1068 struct commit_list
*p
;
1072 commit
= lookup_commit_reference(r
, &oid
);
1073 if (repo_parse_commit(r
, commit
))
1074 return MISSING_OBJECT
;
1076 oidcpy(result
, &commit
->object
.oid
);
1079 p
= commit
->parents
;
1082 oidcpy(result
, &p
->item
->object
.oid
);
1087 return MISSING_OBJECT
;
1090 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1091 const char *name
, int len
,
1092 struct object_id
*result
,
1095 struct object_id oid
;
1096 struct commit
*commit
;
1099 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1102 commit
= lookup_commit_reference(r
, &oid
);
1104 return MISSING_OBJECT
;
1106 while (generation
--) {
1107 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1108 return MISSING_OBJECT
;
1109 commit
= commit
->parents
->item
;
1111 oidcpy(result
, &commit
->object
.oid
);
1115 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1116 struct object
*o
, enum object_type expected_type
)
1118 if (name
&& !namelen
)
1119 namelen
= strlen(name
);
1121 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1123 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1125 if (o
->type
== OBJ_TAG
)
1126 o
= ((struct tag
*) o
)->tagged
;
1127 else if (o
->type
== OBJ_COMMIT
)
1128 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1131 error("%.*s: expected %s type, but the object "
1132 "dereferences to %s type",
1133 namelen
, name
, type_name(expected_type
),
1134 type_name(o
->type
));
1140 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1141 struct object_id
*oid
, unsigned lookup_flags
)
1143 struct object_id outer
;
1145 unsigned int expected_type
= 0;
1149 * "ref^{type}" dereferences ref repeatedly until you cannot
1150 * dereference anymore, or you get an object of given type,
1151 * whichever comes first. "ref^{}" means just dereference
1152 * tags until you get a non-tag. "ref^0" is a shorthand for
1153 * "ref^{commit}". "commit^{tree}" could be used to find the
1154 * top-level tree of the given commit.
1156 if (len
< 4 || name
[len
-1] != '}')
1159 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1161 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1167 sp
++; /* beginning of type name, or closing brace for empty */
1168 if (starts_with(sp
, "commit}"))
1169 expected_type
= OBJ_COMMIT
;
1170 else if (starts_with(sp
, "tag}"))
1171 expected_type
= OBJ_TAG
;
1172 else if (starts_with(sp
, "tree}"))
1173 expected_type
= OBJ_TREE
;
1174 else if (starts_with(sp
, "blob}"))
1175 expected_type
= OBJ_BLOB
;
1176 else if (starts_with(sp
, "object}"))
1177 expected_type
= OBJ_ANY
;
1178 else if (sp
[0] == '}')
1179 expected_type
= OBJ_NONE
;
1180 else if (sp
[0] == '/')
1181 expected_type
= OBJ_COMMIT
;
1185 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1186 if (expected_type
== OBJ_COMMIT
)
1187 lookup_flags
|= GET_OID_COMMITTISH
;
1188 else if (expected_type
== OBJ_TREE
)
1189 lookup_flags
|= GET_OID_TREEISH
;
1191 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1194 o
= parse_object(r
, &outer
);
1197 if (!expected_type
) {
1198 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1199 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1201 oidcpy(oid
, &o
->oid
);
1206 * At this point, the syntax look correct, so
1207 * if we do not get the needed object, we should
1210 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1214 oidcpy(oid
, &o
->oid
);
1216 /* "$commit^{/foo}" */
1219 struct commit_list
*list
= NULL
;
1222 * $commit^{/}. Some regex implementation may reject.
1223 * We don't need regex anyway. '' pattern always matches.
1228 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1229 commit_list_insert((struct commit
*)o
, &list
);
1230 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1237 static int get_describe_name(struct repository
*r
,
1238 const char *name
, int len
,
1239 struct object_id
*oid
)
1242 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1244 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1246 if (!isxdigit(ch
)) {
1247 /* We must be looking at g in "SOMETHING-g"
1248 * for it to be describe output.
1250 if (ch
== 'g' && cp
[-1] == '-') {
1253 return get_short_oid(r
,
1254 cp
, len
, oid
, flags
);
1261 static enum get_oid_result
get_oid_1(struct repository
*r
,
1262 const char *name
, int len
,
1263 struct object_id
*oid
,
1264 unsigned lookup_flags
)
1266 int ret
, has_suffix
;
1270 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1273 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1275 if ('0' <= ch
&& ch
<= '9')
1277 if (ch
== '~' || ch
== '^')
1283 unsigned int num
= 0;
1284 int len1
= cp
- name
;
1286 while (cp
< name
+ len
) {
1287 unsigned int digit
= *cp
++ - '0';
1288 if (unsigned_mult_overflows(num
, 10))
1289 return MISSING_OBJECT
;
1291 if (unsigned_add_overflows(num
, digit
))
1292 return MISSING_OBJECT
;
1295 if (!num
&& len1
== len
- 1)
1297 else if (num
> INT_MAX
)
1298 return MISSING_OBJECT
;
1299 if (has_suffix
== '^')
1300 return get_parent(r
, name
, len1
, oid
, num
);
1301 /* else if (has_suffix == '~') -- goes without saying */
1302 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1305 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1309 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1313 /* It could be describe output that is "SOMETHING-gXXXX" */
1314 ret
= get_describe_name(r
, name
, len
, oid
);
1318 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1322 * This interprets names like ':/Initial revision of "git"' by searching
1323 * through history and returning the first commit whose message starts
1324 * the given regular expression.
1326 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1328 * For a literal '!' character at the beginning of a pattern, you have to repeat
1329 * that, like: ':/!!foo'
1331 * For future extension, all other sequences beginning with ':/!' are reserved.
1334 /* Remember to update object flag allocation in object.h */
1335 #define ONELINE_SEEN (1u<<20)
1337 struct handle_one_ref_cb
{
1338 struct repository
*repo
;
1339 struct commit_list
**list
;
1342 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1346 struct handle_one_ref_cb
*cb
= cb_data
;
1347 struct commit_list
**list
= cb
->list
;
1348 struct object
*object
= parse_object(cb
->repo
, oid
);
1351 if (object
->type
== OBJ_TAG
) {
1352 object
= deref_tag(cb
->repo
, object
, path
,
1357 if (object
->type
!= OBJ_COMMIT
)
1359 commit_list_insert((struct commit
*)object
, list
);
1363 static int get_oid_oneline(struct repository
*r
,
1364 const char *prefix
, struct object_id
*oid
,
1365 struct commit_list
*list
)
1367 struct commit_list
*backup
= NULL
, *l
;
1372 if (prefix
[0] == '!') {
1375 if (prefix
[0] == '-') {
1378 } else if (prefix
[0] != '!') {
1383 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1386 for (l
= list
; l
; l
= l
->next
) {
1387 l
->item
->object
.flags
|= ONELINE_SEEN
;
1388 commit_list_insert(l
->item
, &backup
);
1391 const char *p
, *buf
;
1392 struct commit
*commit
;
1395 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1396 if (!parse_object(r
, &commit
->object
.oid
))
1398 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1399 p
= strstr(buf
, "\n\n");
1400 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1401 repo_unuse_commit_buffer(r
, commit
, buf
);
1404 oidcpy(oid
, &commit
->object
.oid
);
1410 free_commit_list(list
);
1411 for (l
= backup
; l
; l
= l
->next
)
1412 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1413 free_commit_list(backup
);
1414 return found
? 0 : -1;
1417 struct grab_nth_branch_switch_cbdata
{
1422 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1423 struct object_id
*noid UNUSED
,
1424 const char *email UNUSED
,
1425 timestamp_t timestamp UNUSED
,
1427 const char *message
, void *cb_data
)
1429 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1430 const char *match
= NULL
, *target
= NULL
;
1433 if (skip_prefix(message
, "checkout: moving from ", &match
))
1434 target
= strstr(match
, " to ");
1436 if (!match
|| !target
)
1438 if (--(cb
->remaining
) == 0) {
1439 len
= target
- match
;
1440 strbuf_reset(cb
->sb
);
1441 strbuf_add(cb
->sb
, match
, len
);
1442 return 1; /* we are done */
1448 * Parse @{-N} syntax, return the number of characters parsed
1449 * if successful; otherwise signal an error with negative value.
1451 static int interpret_nth_prior_checkout(struct repository
*r
,
1452 const char *name
, int namelen
,
1457 struct grab_nth_branch_switch_cbdata cb
;
1463 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1465 brace
= memchr(name
, '}', namelen
);
1468 nth
= strtol(name
+ 3, &num_end
, 10);
1469 if (num_end
!= brace
)
1476 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1477 "HEAD", grab_nth_branch_switch
, &cb
);
1479 retval
= brace
- name
+ 1;
1486 int repo_get_oid_mb(struct repository
*r
,
1488 struct object_id
*oid
)
1490 struct commit
*one
, *two
;
1491 struct commit_list
*mbs
;
1492 struct object_id oid_tmp
;
1496 dots
= strstr(name
, "...");
1498 return repo_get_oid(r
, name
, oid
);
1500 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1503 strbuf_init(&sb
, dots
- name
);
1504 strbuf_add(&sb
, name
, dots
- name
);
1505 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1506 strbuf_release(&sb
);
1510 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1514 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1516 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1519 mbs
= repo_get_merge_bases(r
, one
, two
);
1520 if (!mbs
|| mbs
->next
)
1524 oidcpy(oid
, &mbs
->item
->object
.oid
);
1526 free_commit_list(mbs
);
1530 /* parse @something syntax, when 'something' is not {.*} */
1531 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1535 if (len
|| name
[1] == '{')
1538 /* make sure it's a single @, or @@{.*}, not @foo */
1539 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1540 if (next
&& next
[1] != '{')
1543 next
= name
+ namelen
;
1544 if (next
!= name
+ 1)
1548 strbuf_add(buf
, "HEAD", 4);
1552 static int reinterpret(struct repository
*r
,
1553 const char *name
, int namelen
, int len
,
1554 struct strbuf
*buf
, unsigned allowed
)
1556 /* we have extra data, which might need further processing */
1557 struct strbuf tmp
= STRBUF_INIT
;
1558 int used
= buf
->len
;
1560 struct interpret_branch_name_options options
= {
1564 strbuf_add(buf
, name
+ len
, namelen
- len
);
1565 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1566 /* that data was not interpreted, remove our cruft */
1568 strbuf_setlen(buf
, used
);
1572 strbuf_addbuf(buf
, &tmp
);
1573 strbuf_release(&tmp
);
1574 /* tweak for size of {-N} versus expanded ref name */
1575 return ret
- used
+ len
;
1578 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1580 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1582 strbuf_addstr(buf
, s
);
1586 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1591 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1592 starts_with(refname
, "refs/heads/"))
1594 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1595 starts_with(refname
, "refs/remotes/"))
1601 static int interpret_branch_mark(struct repository
*r
,
1602 const char *name
, int namelen
,
1603 int at
, struct strbuf
*buf
,
1604 int (*get_mark
)(const char *, int),
1605 const char *(*get_data
)(struct branch
*,
1607 const struct interpret_branch_name_options
*options
)
1610 struct branch
*branch
;
1611 struct strbuf err
= STRBUF_INIT
;
1614 len
= get_mark(name
+ at
, namelen
- at
);
1618 if (memchr(name
, ':', at
))
1622 char *name_str
= xmemdupz(name
, at
);
1623 branch
= branch_get(name_str
);
1626 branch
= branch_get(NULL
);
1628 value
= get_data(branch
, &err
);
1630 if (options
->nonfatal_dangling_mark
) {
1631 strbuf_release(&err
);
1638 if (!branch_interpret_allowed(value
, options
->allowed
))
1641 set_shortened_ref(r
, buf
, value
);
1645 int repo_interpret_branch_name(struct repository
*r
,
1646 const char *name
, int namelen
,
1648 const struct interpret_branch_name_options
*options
)
1655 namelen
= strlen(name
);
1657 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1658 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1660 return len
; /* syntax Ok, not enough switches */
1661 } else if (len
> 0) {
1663 return len
; /* consumed all */
1665 return reinterpret(r
, name
, namelen
, len
, buf
,
1671 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1674 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1675 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1677 return reinterpret(r
, name
, namelen
, len
, buf
,
1681 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1682 upstream_mark
, branch_get_upstream
,
1687 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1688 push_mark
, branch_get_push
,
1697 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1699 int len
= strlen(name
);
1700 struct interpret_branch_name_options options
= {
1703 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1708 strbuf_add(sb
, name
+ used
, len
- used
);
1711 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1713 if (startup_info
->have_repository
)
1714 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1716 strbuf_addstr(sb
, name
);
1719 * This splice must be done even if we end up rejecting the
1720 * name; builtin/branch.c::copy_or_rename_branch() still wants
1721 * to see what the name expanded to so that "branch -m" can be
1722 * used as a tool to correct earlier mistakes.
1724 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1727 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1730 return check_refname_format(sb
->buf
, 0);
1734 * This is like "get_oid_basic()", except it allows "object ID expressions",
1735 * notably "xyz^" for "parent of xyz"
1737 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1739 struct object_context unused
;
1740 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1744 * This returns a non-zero value if the string (built using printf
1745 * format and the given arguments) is not a valid object.
1747 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1751 struct strbuf sb
= STRBUF_INIT
;
1754 strbuf_vaddf(&sb
, fmt
, ap
);
1757 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1758 strbuf_release(&sb
);
1764 * Many callers know that the user meant to name a commit-ish by
1765 * syntactical positions where the object name appears. Calling this
1766 * function allows the machinery to disambiguate shorter-than-unique
1767 * abbreviated object names between commit-ish and others.
1769 * Note that this does NOT error out when the named object is not a
1770 * commit-ish. It is merely to give a hint to the disambiguation
1773 int repo_get_oid_committish(struct repository
*r
,
1775 struct object_id
*oid
)
1777 struct object_context unused
;
1778 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1782 int repo_get_oid_treeish(struct repository
*r
,
1784 struct object_id
*oid
)
1786 struct object_context unused
;
1787 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1791 int repo_get_oid_commit(struct repository
*r
,
1793 struct object_id
*oid
)
1795 struct object_context unused
;
1796 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1800 int repo_get_oid_tree(struct repository
*r
,
1802 struct object_id
*oid
)
1804 struct object_context unused
;
1805 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1809 int repo_get_oid_blob(struct repository
*r
,
1811 struct object_id
*oid
)
1813 struct object_context unused
;
1814 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1818 /* Must be called only when object_name:filename doesn't exist. */
1819 static void diagnose_invalid_oid_path(struct repository
*r
,
1821 const char *filename
,
1822 const struct object_id
*tree_oid
,
1823 const char *object_name
,
1824 int object_name_len
)
1826 struct object_id oid
;
1827 unsigned short mode
;
1832 if (file_exists(filename
))
1833 die(_("path '%s' exists on disk, but not in '%.*s'"),
1834 filename
, object_name_len
, object_name
);
1835 if (is_missing_file_error(errno
)) {
1836 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1838 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1839 die(_("path '%s' exists, but not '%s'\n"
1840 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1843 object_name_len
, object_name
,
1845 object_name_len
, object_name
,
1848 die(_("path '%s' does not exist in '%.*s'"),
1849 filename
, object_name_len
, object_name
);
1853 /* Must be called only when :stage:filename doesn't exist. */
1854 static void diagnose_invalid_index_path(struct repository
*r
,
1857 const char *filename
)
1859 struct index_state
*istate
= r
->index
;
1860 const struct cache_entry
*ce
;
1862 unsigned namelen
= strlen(filename
);
1863 struct strbuf fullname
= STRBUF_INIT
;
1868 /* Wrong stage number? */
1869 pos
= index_name_pos(istate
, filename
, namelen
);
1872 if (pos
< istate
->cache_nr
) {
1873 ce
= istate
->cache
[pos
];
1874 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1875 ce_namelen(ce
) == namelen
&&
1876 !memcmp(ce
->name
, filename
, namelen
))
1877 die(_("path '%s' is in the index, but not at stage %d\n"
1878 "hint: Did you mean ':%d:%s'?"),
1880 ce_stage(ce
), filename
);
1883 /* Confusion between relative and absolute filenames? */
1884 strbuf_addstr(&fullname
, prefix
);
1885 strbuf_addstr(&fullname
, filename
);
1886 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1889 if (pos
< istate
->cache_nr
) {
1890 ce
= istate
->cache
[pos
];
1891 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1892 ce_namelen(ce
) == fullname
.len
&&
1893 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1894 die(_("path '%s' is in the index, but not '%s'\n"
1895 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1896 fullname
.buf
, filename
,
1897 ce_stage(ce
), fullname
.buf
,
1898 ce_stage(ce
), filename
);
1901 if (repo_file_exists(r
, filename
))
1902 die(_("path '%s' exists on disk, but not in the index"), filename
);
1903 if (is_missing_file_error(errno
))
1904 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1907 strbuf_release(&fullname
);
1911 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1913 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1916 if (r
!= the_repository
|| !is_inside_work_tree())
1917 die(_("relative path syntax can't be used outside working tree"));
1919 /* die() inside prefix_path() if resolved path is outside worktree */
1920 return prefix_path(startup_info
->prefix
,
1921 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1925 static int reject_tree_in_index(struct repository
*repo
,
1927 const struct cache_entry
*ce
,
1932 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1935 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1939 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1943 struct object_id
*oid
,
1944 struct object_context
*oc
)
1946 int ret
, bracket_depth
;
1947 int namelen
= strlen(name
);
1949 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1951 memset(oc
, 0, sizeof(*oc
));
1952 oc
->mode
= S_IFINVALID
;
1953 strbuf_init(&oc
->symlink_path
, 0);
1954 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1955 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1956 die(_("<object>:<path> required, only <object> '%s' given"),
1961 * tree:path --> object name of path in tree
1962 * :path -> object name of absolute path in index
1963 * :./path -> object name of path relative to cwd in index
1964 * :[0-3]:path -> object name of path in index at stage
1965 * :/foo -> recent commit matching foo
1967 if (name
[0] == ':') {
1969 const struct cache_entry
*ce
;
1970 char *new_path
= NULL
;
1972 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1973 struct handle_one_ref_cb cb
;
1974 struct commit_list
*list
= NULL
;
1978 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1979 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1980 commit_list_sort_by_date(&list
);
1981 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1985 name
[1] < '0' || '3' < name
[1])
1988 stage
= name
[1] - '0';
1991 new_path
= resolve_relative_path(repo
, cp
);
1993 namelen
= namelen
- (cp
- name
);
1996 namelen
= strlen(cp
);
1999 if (flags
& GET_OID_RECORD_PATH
)
2000 oc
->path
= xstrdup(cp
);
2002 if (!repo
->index
|| !repo
->index
->cache
)
2003 repo_read_index(repo
);
2004 pos
= index_name_pos(repo
->index
, cp
, namelen
);
2007 while (pos
< repo
->index
->cache_nr
) {
2008 ce
= repo
->index
->cache
[pos
];
2009 if (ce_namelen(ce
) != namelen
||
2010 memcmp(ce
->name
, cp
, namelen
))
2012 if (ce_stage(ce
) == stage
) {
2014 if (reject_tree_in_index(repo
, only_to_die
, ce
,
2017 oidcpy(oid
, &ce
->oid
);
2018 oc
->mode
= ce
->ce_mode
;
2023 if (only_to_die
&& name
[1] && name
[1] != '/')
2024 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2028 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2031 else if (bracket_depth
&& *cp
== '}')
2033 else if (!bracket_depth
&& *cp
== ':')
2037 struct object_id tree_oid
;
2038 int len
= cp
- name
;
2039 unsigned sub_flags
= flags
;
2041 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2042 sub_flags
|= GET_OID_TREEISH
;
2044 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2045 const char *filename
= cp
+1;
2046 char *new_filename
= NULL
;
2048 new_filename
= resolve_relative_path(repo
, filename
);
2050 filename
= new_filename
;
2051 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2052 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2053 filename
, oid
, &oc
->symlink_path
,
2056 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2058 if (ret
&& only_to_die
) {
2059 diagnose_invalid_oid_path(repo
, prefix
,
2065 if (flags
& GET_OID_RECORD_PATH
)
2066 oc
->path
= xstrdup(filename
);
2072 die(_("invalid object name '%.*s'."), len
, name
);
2079 * Call this function when you know "name" given by the end user must
2080 * name an object but it doesn't; the function _may_ die with a better
2081 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2082 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2083 * you have a chance to diagnose the error further.
2085 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2089 struct object_context oc
;
2090 struct object_id oid
;
2091 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2095 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2098 struct object_id
*oid
,
2099 struct object_context
*oc
)
2101 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2102 BUG("incompatible flags for get_oid_with_context");
2103 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);