2 #include "object-name.h"
5 #include "environment.h"
12 #include "tree-walk.h"
16 #include "oid-array.h"
20 #include "object-store.h"
21 #include "repository.h"
23 #include "submodule.h"
25 #include "commit-reach.h"
28 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
30 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
32 struct disambiguate_state
{
33 int len
; /* length of prefix in hex chars */
34 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
35 struct object_id bin_pfx
;
37 struct repository
*repo
;
38 disambiguate_hint_fn fn
;
40 struct object_id candidate
;
41 unsigned candidate_exists
:1;
42 unsigned candidate_checked
:1;
43 unsigned candidate_ok
:1;
44 unsigned disambiguate_fn_used
:1;
46 unsigned always_call_fn
:1;
49 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
51 if (ds
->always_call_fn
) {
52 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
55 if (!ds
->candidate_exists
) {
56 /* this is the first candidate */
57 oidcpy(&ds
->candidate
, current
);
58 ds
->candidate_exists
= 1;
60 } else if (oideq(&ds
->candidate
, current
)) {
61 /* the same as what we already have seen */
66 /* cannot disambiguate between ds->candidate and current */
71 if (!ds
->candidate_checked
) {
72 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
73 ds
->disambiguate_fn_used
= 1;
74 ds
->candidate_checked
= 1;
77 if (!ds
->candidate_ok
) {
78 /* discard the candidate; we know it does not satisfy fn */
79 oidcpy(&ds
->candidate
, current
);
80 ds
->candidate_checked
= 0;
84 /* if we reach this point, we know ds->candidate satisfies fn */
85 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
87 * if both current and candidate satisfy fn, we cannot
94 /* otherwise, current can be discarded and candidate is still good */
97 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
99 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
101 struct disambiguate_state
*ds
= arg
;
102 /* no need to call match_hash, oidtree_each did prefix match */
103 update_candidates(ds
, oid
);
104 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
107 static void find_short_object_filename(struct disambiguate_state
*ds
)
109 struct object_directory
*odb
;
111 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
112 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
113 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
116 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
126 if ((*a
^ *b
) & 0xf0)
131 static void unique_in_midx(struct multi_pack_index
*m
,
132 struct disambiguate_state
*ds
)
134 uint32_t num
, i
, first
= 0;
135 const struct object_id
*current
= NULL
;
136 num
= m
->num_objects
;
141 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
144 * At this point, "first" is the location of the lowest object
145 * with an object name that could match "bin_pfx". See if we have
146 * 0, 1 or more objects that actually match(es).
148 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
149 struct object_id oid
;
150 current
= nth_midxed_object_oid(&oid
, m
, i
);
151 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
153 update_candidates(ds
, current
);
157 static void unique_in_pack(struct packed_git
*p
,
158 struct disambiguate_state
*ds
)
160 uint32_t num
, i
, first
= 0;
162 if (p
->multi_pack_index
)
165 if (open_pack_index(p
) || !p
->num_objects
)
168 num
= p
->num_objects
;
169 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
172 * At this point, "first" is the location of the lowest object
173 * with an object name that could match "bin_pfx". See if we have
174 * 0, 1 or more objects that actually match(es).
176 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
177 struct object_id oid
;
178 nth_packed_object_id(&oid
, p
, i
);
179 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, oid
.hash
))
181 update_candidates(ds
, &oid
);
185 static void find_short_packed_object(struct disambiguate_state
*ds
)
187 struct multi_pack_index
*m
;
188 struct packed_git
*p
;
190 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
192 unique_in_midx(m
, ds
);
193 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
195 unique_in_pack(p
, ds
);
198 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
199 struct object_id
*oid
)
202 return SHORT_NAME_AMBIGUOUS
;
204 if (!ds
->candidate_exists
)
205 return MISSING_OBJECT
;
207 if (!ds
->candidate_checked
)
209 * If this is the only candidate, there is no point
210 * calling the disambiguation hint callback.
212 * On the other hand, if the current candidate
213 * replaced an earlier candidate that did _not_ pass
214 * the disambiguation hint callback, then we do have
215 * more than one objects that match the short name
216 * given, so we should make sure this one matches;
217 * otherwise, if we discovered this one and the one
218 * that we previously discarded in the reverse order,
219 * we would end up showing different results in the
222 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
223 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
225 if (!ds
->candidate_ok
)
226 return SHORT_NAME_AMBIGUOUS
;
228 oidcpy(oid
, &ds
->candidate
);
232 static int disambiguate_commit_only(struct repository
*r
,
233 const struct object_id
*oid
,
234 void *cb_data UNUSED
)
236 int kind
= oid_object_info(r
, oid
, NULL
);
237 return kind
== OBJ_COMMIT
;
240 static int disambiguate_committish_only(struct repository
*r
,
241 const struct object_id
*oid
,
242 void *cb_data UNUSED
)
247 kind
= oid_object_info(r
, oid
, NULL
);
248 if (kind
== OBJ_COMMIT
)
253 /* We need to do this the hard way... */
254 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
255 if (obj
&& obj
->type
== OBJ_COMMIT
)
260 static int disambiguate_tree_only(struct repository
*r
,
261 const struct object_id
*oid
,
262 void *cb_data UNUSED
)
264 int kind
= oid_object_info(r
, oid
, NULL
);
265 return kind
== OBJ_TREE
;
268 static int disambiguate_treeish_only(struct repository
*r
,
269 const struct object_id
*oid
,
270 void *cb_data UNUSED
)
275 kind
= oid_object_info(r
, oid
, NULL
);
276 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
281 /* We need to do this the hard way... */
282 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
283 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
288 static int disambiguate_blob_only(struct repository
*r
,
289 const struct object_id
*oid
,
290 void *cb_data UNUSED
)
292 int kind
= oid_object_info(r
, oid
, NULL
);
293 return kind
== OBJ_BLOB
;
296 static disambiguate_hint_fn default_disambiguate_hint
;
298 int set_disambiguate_hint_config(const char *var
, const char *value
)
300 static const struct {
302 disambiguate_hint_fn fn
;
305 { "commit", disambiguate_commit_only
},
306 { "committish", disambiguate_committish_only
},
307 { "tree", disambiguate_tree_only
},
308 { "treeish", disambiguate_treeish_only
},
309 { "blob", disambiguate_blob_only
}
314 return config_error_nonbool(var
);
316 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
317 if (!strcasecmp(value
, hints
[i
].name
)) {
318 default_disambiguate_hint
= hints
[i
].fn
;
323 return error("unknown hint type for '%s': %s", var
, value
);
326 static int init_object_disambiguation(struct repository
*r
,
327 const char *name
, int len
,
328 struct disambiguate_state
*ds
)
332 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
335 memset(ds
, 0, sizeof(*ds
));
337 for (i
= 0; i
< len
;i
++) {
338 unsigned char c
= name
[i
];
340 if (c
>= '0' && c
<= '9')
342 else if (c
>= 'a' && c
<= 'f')
344 else if (c
>= 'A' && c
<='F') {
353 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
357 ds
->hex_pfx
[len
] = '\0';
363 struct ambiguous_output
{
364 const struct disambiguate_state
*ds
;
365 struct strbuf advice
;
369 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
371 struct ambiguous_output
*state
= data
;
372 const struct disambiguate_state
*ds
= state
->ds
;
373 struct strbuf
*advice
= &state
->advice
;
374 struct strbuf
*sb
= &state
->sb
;
378 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
381 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
382 type
= oid_object_info(ds
->repo
, oid
, NULL
);
386 * TRANSLATORS: This is a line of ambiguous object
387 * output shown when we cannot look up or parse the
388 * object in question. E.g. "deadbeef [bad object]".
390 strbuf_addf(sb
, _("%s [bad object]"), hash
);
394 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
395 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
397 if (type
== OBJ_COMMIT
) {
398 struct strbuf date
= STRBUF_INIT
;
399 struct strbuf msg
= STRBUF_INIT
;
400 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
403 struct pretty_print_context pp
= {0};
404 pp
.date_mode
.type
= DATE_SHORT
;
405 repo_format_commit_message(the_repository
, commit
,
407 repo_format_commit_message(the_repository
, commit
,
412 * TRANSLATORS: This is a line of ambiguous commit
413 * object output. E.g.:
415 * "deadbeef commit 2021-01-01 - Some Commit Message"
417 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
420 strbuf_release(&date
);
421 strbuf_release(&msg
);
422 } else if (type
== OBJ_TAG
) {
423 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
425 if (!parse_tag(tag
) && tag
->tag
) {
427 * TRANSLATORS: This is a line of ambiguous
428 * tag object output. E.g.:
430 * "deadbeef tag 2022-01-01 - Some Tag Message"
432 * The second argument is the YYYY-MM-DD found
435 * The third argument is the "tag" string
438 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
439 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
443 * TRANSLATORS: This is a line of ambiguous
444 * tag object output where we couldn't parse
445 * the tag itself. E.g.:
447 * "deadbeef [bad tag, could not parse it]"
449 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
452 } else if (type
== OBJ_TREE
) {
454 * TRANSLATORS: This is a line of ambiguous <type>
455 * object output. E.g. "deadbeef tree".
457 strbuf_addf(sb
, _("%s tree"), hash
);
458 } else if (type
== OBJ_BLOB
) {
460 * TRANSLATORS: This is a line of ambiguous <type>
461 * object output. E.g. "deadbeef blob".
463 strbuf_addf(sb
, _("%s blob"), hash
);
469 * TRANSLATORS: This is line item of ambiguous object output
470 * from describe_ambiguous_object() above. For RTL languages
471 * you'll probably want to swap the "%s" and leading " " space
474 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
480 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
482 oid_array_append(data
, oid
);
486 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
487 const struct object_id
*oid
,
490 return collect_ambiguous(oid
, data
);
493 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
495 struct repository
*sort_ambiguous_repo
= ctx
;
496 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
497 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
502 * Sorts by hash within the same object type, just as
503 * oid_array_for_each_unique() would do.
505 if (a_type
== b_type
)
509 * Between object types show tags, then commits, and finally
512 * The object_type enum is commit, tree, blob, tag, but we
513 * want tag, commit, tree blob. Cleverly (perhaps too
514 * cleverly) do that with modulus, since the enum assigns 1 to
515 * commit, so tag becomes 0.
517 a_type_sort
= a_type
% 4;
518 b_type_sort
= b_type
% 4;
519 return a_type_sort
> b_type_sort
? 1 : -1;
522 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
524 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
527 static enum get_oid_result
get_short_oid(struct repository
*r
,
528 const char *name
, int len
,
529 struct object_id
*oid
,
533 struct disambiguate_state ds
;
534 int quietly
= !!(flags
& GET_OID_QUIETLY
);
536 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
539 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
540 BUG("multiple get_short_oid disambiguator flags");
542 if (flags
& GET_OID_COMMIT
)
543 ds
.fn
= disambiguate_commit_only
;
544 else if (flags
& GET_OID_COMMITTISH
)
545 ds
.fn
= disambiguate_committish_only
;
546 else if (flags
& GET_OID_TREE
)
547 ds
.fn
= disambiguate_tree_only
;
548 else if (flags
& GET_OID_TREEISH
)
549 ds
.fn
= disambiguate_treeish_only
;
550 else if (flags
& GET_OID_BLOB
)
551 ds
.fn
= disambiguate_blob_only
;
553 ds
.fn
= default_disambiguate_hint
;
555 find_short_object_filename(&ds
);
556 find_short_packed_object(&ds
);
557 status
= finish_object_disambiguation(&ds
, oid
);
560 * If we didn't find it, do the usual reprepare() slow-path,
561 * since the object may have recently been added to the repository
562 * or migrated from loose to packed.
564 if (status
== MISSING_OBJECT
) {
565 reprepare_packed_git(r
);
566 find_short_object_filename(&ds
);
567 find_short_packed_object(&ds
);
568 status
= finish_object_disambiguation(&ds
, oid
);
571 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
572 struct oid_array collect
= OID_ARRAY_INIT
;
573 struct ambiguous_output out
= {
576 .advice
= STRBUF_INIT
,
579 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
582 * We may still have ambiguity if we simply saw a series of
583 * candidates that did not satisfy our hint function. In
584 * that case, we still want to show them, so disable the hint
590 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
591 sort_ambiguous_oid_array(r
, &collect
);
593 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
594 BUG("show_ambiguous_object shouldn't return non-zero");
597 * TRANSLATORS: The argument is the list of ambiguous
598 * objects composed in show_ambiguous_object(). See
599 * its "TRANSLATORS" comments for details.
601 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
603 oid_array_clear(&collect
);
604 strbuf_release(&out
.advice
);
605 strbuf_release(&out
.sb
);
611 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
612 each_abbrev_fn fn
, void *cb_data
)
614 struct oid_array collect
= OID_ARRAY_INIT
;
615 struct disambiguate_state ds
;
618 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
621 ds
.always_call_fn
= 1;
622 ds
.fn
= repo_collect_ambiguous
;
623 ds
.cb_data
= &collect
;
624 find_short_object_filename(&ds
);
625 find_short_packed_object(&ds
);
627 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
628 oid_array_clear(&collect
);
633 * Return the slot of the most-significant bit set in "val". There are various
634 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
635 * probably not a big deal here.
637 static unsigned msb(unsigned long val
)
645 struct min_abbrev_data
{
646 unsigned int init_len
;
647 unsigned int cur_len
;
649 struct repository
*repo
;
650 const struct object_id
*oid
;
653 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
656 static const char hex
[] = "0123456789abcdef";
659 return hex
[oid
->hash
[pos
>> 1] >> 4];
661 return hex
[oid
->hash
[pos
>> 1] & 0xf];
664 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
666 struct min_abbrev_data
*mad
= cb_data
;
668 unsigned int i
= mad
->init_len
;
669 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
672 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
673 mad
->cur_len
= i
+ 1;
678 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
679 const struct object_id
*oid
,
682 return extend_abbrev_len(oid
, cb_data
);
685 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
686 struct min_abbrev_data
*mad
)
689 uint32_t num
, first
= 0;
690 struct object_id oid
;
691 const struct object_id
*mad_oid
;
696 num
= m
->num_objects
;
698 match
= bsearch_midx(mad_oid
, m
, &first
);
701 * first is now the position in the packfile where we would insert
702 * mad->hash if it does not exist (or the position of mad->hash if
703 * it does exist). Hence, we consider a maximum of two objects
704 * nearby for the abbreviation length.
708 if (nth_midxed_object_oid(&oid
, m
, first
))
709 extend_abbrev_len(&oid
, mad
);
710 } else if (first
< num
- 1) {
711 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
712 extend_abbrev_len(&oid
, mad
);
715 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
716 extend_abbrev_len(&oid
, mad
);
718 mad
->init_len
= mad
->cur_len
;
721 static void find_abbrev_len_for_pack(struct packed_git
*p
,
722 struct min_abbrev_data
*mad
)
725 uint32_t num
, first
= 0;
726 struct object_id oid
;
727 const struct object_id
*mad_oid
;
729 if (p
->multi_pack_index
)
732 if (open_pack_index(p
) || !p
->num_objects
)
735 num
= p
->num_objects
;
737 match
= bsearch_pack(mad_oid
, p
, &first
);
740 * first is now the position in the packfile where we would insert
741 * mad->hash if it does not exist (or the position of mad->hash if
742 * it does exist). Hence, we consider a maximum of two objects
743 * nearby for the abbreviation length.
747 if (!nth_packed_object_id(&oid
, p
, first
))
748 extend_abbrev_len(&oid
, mad
);
749 } else if (first
< num
- 1) {
750 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
751 extend_abbrev_len(&oid
, mad
);
754 if (!nth_packed_object_id(&oid
, p
, first
- 1))
755 extend_abbrev_len(&oid
, mad
);
757 mad
->init_len
= mad
->cur_len
;
760 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
762 struct multi_pack_index
*m
;
763 struct packed_git
*p
;
765 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
766 find_abbrev_len_for_midx(m
, mad
);
767 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
768 find_abbrev_len_for_pack(p
, mad
);
771 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
772 const struct object_id
*oid
, int len
)
774 struct disambiguate_state ds
;
775 struct min_abbrev_data mad
;
776 struct object_id oid_ret
;
777 const unsigned hexsz
= r
->hash_algo
->hexsz
;
780 unsigned long count
= repo_approximate_object_count(r
);
782 * Add one because the MSB only tells us the highest bit set,
783 * not including the value of all the _other_ bits (so "15"
784 * is only one off of 2^4, but the MSB is the 3rd bit.
786 len
= msb(count
) + 1;
788 * We now know we have on the order of 2^len objects, which
789 * expects a collision at 2^(len/2). But we also care about hex
790 * chars, not bits, and there are 4 bits per hex. So all
791 * together we need to divide by 2 and round up.
793 len
= DIV_ROUND_UP(len
, 2);
795 * For very small repos, we stick with our regular fallback.
797 if (len
< FALLBACK_DEFAULT_ABBREV
)
798 len
= FALLBACK_DEFAULT_ABBREV
;
801 oid_to_hex_r(hex
, oid
);
802 if (len
== hexsz
|| !len
)
811 find_abbrev_len_packed(&mad
);
813 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
816 ds
.fn
= repo_extend_abbrev_len
;
817 ds
.always_call_fn
= 1;
818 ds
.cb_data
= (void *)&mad
;
820 find_short_object_filename(&ds
);
821 (void)finish_object_disambiguation(&ds
, &oid_ret
);
823 hex
[mad
.cur_len
] = 0;
827 const char *repo_find_unique_abbrev(struct repository
*r
,
828 const struct object_id
*oid
,
832 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
833 char *hex
= hexbuffer
[bufno
];
834 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
835 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
839 static int ambiguous_path(const char *path
, int len
)
844 for (cnt
= 0; cnt
< len
; cnt
++) {
864 static inline int at_mark(const char *string
, int len
,
865 const char **suffix
, int nr
)
869 for (i
= 0; i
< nr
; i
++) {
870 int suffix_len
= strlen(suffix
[i
]);
871 if (suffix_len
<= len
872 && !strncasecmp(string
, suffix
[i
], suffix_len
))
878 static inline int upstream_mark(const char *string
, int len
)
880 const char *suffix
[] = { "@{upstream}", "@{u}" };
881 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
884 static inline int push_mark(const char *string
, int len
)
886 const char *suffix
[] = { "@{push}" };
887 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
890 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
891 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
893 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
894 struct object_id
*oid
, unsigned int flags
)
896 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
897 static const char *object_name_msg
= N_(
898 "Git normally never creates a ref that ends with 40 hex characters\n"
899 "because it will be ignored when you just specify 40-hex. These refs\n"
900 "may be created by mistake. For example,\n"
902 " git switch -c $br $(git rev-parse ...)\n"
904 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
905 "examine these refs and maybe delete them. Turn this message off by\n"
906 "running \"git config advice.objectNameWarning false\"");
907 struct object_id tmp_oid
;
908 char *real_ref
= NULL
;
910 int at
, reflog_len
, nth_prior
= 0;
911 int fatal
= !(flags
& GET_OID_QUIETLY
);
913 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
914 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
915 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
916 if (refs_found
> 0) {
917 warning(warn_msg
, len
, str
);
918 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
919 fprintf(stderr
, "%s\n", _(object_name_msg
));
926 /* basic@{time or number or -number} format to query ref-log */
928 if (len
&& str
[len
-1] == '}') {
929 for (at
= len
-4; at
>= 0; at
--) {
930 if (str
[at
] == '@' && str
[at
+1] == '{') {
931 if (str
[at
+2] == '-') {
933 /* @{-N} not at start */
938 if (!upstream_mark(str
+ at
, len
- at
) &&
939 !push_mark(str
+ at
, len
- at
)) {
940 reflog_len
= (len
-1) - (at
+2);
948 /* Accept only unambiguous ref paths. */
949 if (len
&& ambiguous_path(str
, len
))
953 struct strbuf buf
= STRBUF_INIT
;
956 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
957 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
958 strbuf_release(&buf
);
964 if (!len
&& reflog_len
)
965 /* allow "@{...}" to mean the current branch reflog */
966 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, !fatal
);
968 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
970 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, !fatal
);
975 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
977 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
978 warning(warn_msg
, len
, str
);
986 /* Is it asking for N-th entry, or approxidate? */
987 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
988 char ch
= str
[at
+2+i
];
989 if ('0' <= ch
&& ch
<= '9')
990 nth
= nth
* 10 + ch
- '0';
994 if (100000000 <= nth
) {
1001 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
1002 at_time
= approxidate_careful(tmp
, &errors
);
1009 if (read_ref_at(get_main_ref_store(r
),
1010 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1011 &co_time
, &co_tz
, &co_cnt
)) {
1013 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1018 if (!(flags
& GET_OID_QUIETLY
)) {
1019 warning(_("log for '%.*s' only goes back to %s"),
1021 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1024 if (flags
& GET_OID_QUIETLY
) {
1027 die(_("log for '%.*s' only has %d entries"),
1037 static enum get_oid_result
get_parent(struct repository
*r
,
1038 const char *name
, int len
,
1039 struct object_id
*result
, int idx
)
1041 struct object_id oid
;
1042 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1043 GET_OID_COMMITTISH
);
1044 struct commit
*commit
;
1045 struct commit_list
*p
;
1049 commit
= lookup_commit_reference(r
, &oid
);
1050 if (repo_parse_commit(r
, commit
))
1051 return MISSING_OBJECT
;
1053 oidcpy(result
, &commit
->object
.oid
);
1056 p
= commit
->parents
;
1059 oidcpy(result
, &p
->item
->object
.oid
);
1064 return MISSING_OBJECT
;
1067 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1068 const char *name
, int len
,
1069 struct object_id
*result
,
1072 struct object_id oid
;
1073 struct commit
*commit
;
1076 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1079 commit
= lookup_commit_reference(r
, &oid
);
1081 return MISSING_OBJECT
;
1083 while (generation
--) {
1084 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1085 return MISSING_OBJECT
;
1086 commit
= commit
->parents
->item
;
1088 oidcpy(result
, &commit
->object
.oid
);
1092 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1093 struct object
*o
, enum object_type expected_type
)
1095 if (name
&& !namelen
)
1096 namelen
= strlen(name
);
1098 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1100 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1102 if (o
->type
== OBJ_TAG
)
1103 o
= ((struct tag
*) o
)->tagged
;
1104 else if (o
->type
== OBJ_COMMIT
)
1105 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1108 error("%.*s: expected %s type, but the object "
1109 "dereferences to %s type",
1110 namelen
, name
, type_name(expected_type
),
1111 type_name(o
->type
));
1117 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1118 struct object_id
*oid
, unsigned lookup_flags
)
1120 struct object_id outer
;
1122 unsigned int expected_type
= 0;
1126 * "ref^{type}" dereferences ref repeatedly until you cannot
1127 * dereference anymore, or you get an object of given type,
1128 * whichever comes first. "ref^{}" means just dereference
1129 * tags until you get a non-tag. "ref^0" is a shorthand for
1130 * "ref^{commit}". "commit^{tree}" could be used to find the
1131 * top-level tree of the given commit.
1133 if (len
< 4 || name
[len
-1] != '}')
1136 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1138 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1144 sp
++; /* beginning of type name, or closing brace for empty */
1145 if (starts_with(sp
, "commit}"))
1146 expected_type
= OBJ_COMMIT
;
1147 else if (starts_with(sp
, "tag}"))
1148 expected_type
= OBJ_TAG
;
1149 else if (starts_with(sp
, "tree}"))
1150 expected_type
= OBJ_TREE
;
1151 else if (starts_with(sp
, "blob}"))
1152 expected_type
= OBJ_BLOB
;
1153 else if (starts_with(sp
, "object}"))
1154 expected_type
= OBJ_ANY
;
1155 else if (sp
[0] == '}')
1156 expected_type
= OBJ_NONE
;
1157 else if (sp
[0] == '/')
1158 expected_type
= OBJ_COMMIT
;
1162 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1163 if (expected_type
== OBJ_COMMIT
)
1164 lookup_flags
|= GET_OID_COMMITTISH
;
1165 else if (expected_type
== OBJ_TREE
)
1166 lookup_flags
|= GET_OID_TREEISH
;
1168 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1171 o
= parse_object(r
, &outer
);
1174 if (!expected_type
) {
1175 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1176 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1178 oidcpy(oid
, &o
->oid
);
1183 * At this point, the syntax look correct, so
1184 * if we do not get the needed object, we should
1187 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1191 oidcpy(oid
, &o
->oid
);
1193 /* "$commit^{/foo}" */
1196 struct commit_list
*list
= NULL
;
1199 * $commit^{/}. Some regex implementation may reject.
1200 * We don't need regex anyway. '' pattern always matches.
1205 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1206 commit_list_insert((struct commit
*)o
, &list
);
1207 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1214 static int get_describe_name(struct repository
*r
,
1215 const char *name
, int len
,
1216 struct object_id
*oid
)
1219 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1221 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1223 if (!isxdigit(ch
)) {
1224 /* We must be looking at g in "SOMETHING-g"
1225 * for it to be describe output.
1227 if (ch
== 'g' && cp
[-1] == '-') {
1230 return get_short_oid(r
,
1231 cp
, len
, oid
, flags
);
1238 static enum get_oid_result
get_oid_1(struct repository
*r
,
1239 const char *name
, int len
,
1240 struct object_id
*oid
,
1241 unsigned lookup_flags
)
1243 int ret
, has_suffix
;
1247 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1250 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1252 if ('0' <= ch
&& ch
<= '9')
1254 if (ch
== '~' || ch
== '^')
1260 unsigned int num
= 0;
1261 int len1
= cp
- name
;
1263 while (cp
< name
+ len
) {
1264 unsigned int digit
= *cp
++ - '0';
1265 if (unsigned_mult_overflows(num
, 10))
1266 return MISSING_OBJECT
;
1268 if (unsigned_add_overflows(num
, digit
))
1269 return MISSING_OBJECT
;
1272 if (!num
&& len1
== len
- 1)
1274 else if (num
> INT_MAX
)
1275 return MISSING_OBJECT
;
1276 if (has_suffix
== '^')
1277 return get_parent(r
, name
, len1
, oid
, num
);
1278 /* else if (has_suffix == '~') -- goes without saying */
1279 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1282 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1286 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1290 /* It could be describe output that is "SOMETHING-gXXXX" */
1291 ret
= get_describe_name(r
, name
, len
, oid
);
1295 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1299 * This interprets names like ':/Initial revision of "git"' by searching
1300 * through history and returning the first commit whose message starts
1301 * the given regular expression.
1303 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1305 * For a literal '!' character at the beginning of a pattern, you have to repeat
1306 * that, like: ':/!!foo'
1308 * For future extension, all other sequences beginning with ':/!' are reserved.
1311 /* Remember to update object flag allocation in object.h */
1312 #define ONELINE_SEEN (1u<<20)
1314 struct handle_one_ref_cb
{
1315 struct repository
*repo
;
1316 struct commit_list
**list
;
1319 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1323 struct handle_one_ref_cb
*cb
= cb_data
;
1324 struct commit_list
**list
= cb
->list
;
1325 struct object
*object
= parse_object(cb
->repo
, oid
);
1328 if (object
->type
== OBJ_TAG
) {
1329 object
= deref_tag(cb
->repo
, object
, path
,
1334 if (object
->type
!= OBJ_COMMIT
)
1336 commit_list_insert((struct commit
*)object
, list
);
1340 static int get_oid_oneline(struct repository
*r
,
1341 const char *prefix
, struct object_id
*oid
,
1342 struct commit_list
*list
)
1344 struct commit_list
*backup
= NULL
, *l
;
1349 if (prefix
[0] == '!') {
1352 if (prefix
[0] == '-') {
1355 } else if (prefix
[0] != '!') {
1360 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1363 for (l
= list
; l
; l
= l
->next
) {
1364 l
->item
->object
.flags
|= ONELINE_SEEN
;
1365 commit_list_insert(l
->item
, &backup
);
1368 const char *p
, *buf
;
1369 struct commit
*commit
;
1372 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1373 if (!parse_object(r
, &commit
->object
.oid
))
1375 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1376 p
= strstr(buf
, "\n\n");
1377 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1378 repo_unuse_commit_buffer(r
, commit
, buf
);
1381 oidcpy(oid
, &commit
->object
.oid
);
1387 free_commit_list(list
);
1388 for (l
= backup
; l
; l
= l
->next
)
1389 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1390 free_commit_list(backup
);
1391 return found
? 0 : -1;
1394 struct grab_nth_branch_switch_cbdata
{
1399 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1400 struct object_id
*noid UNUSED
,
1401 const char *email UNUSED
,
1402 timestamp_t timestamp UNUSED
,
1404 const char *message
, void *cb_data
)
1406 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1407 const char *match
= NULL
, *target
= NULL
;
1410 if (skip_prefix(message
, "checkout: moving from ", &match
))
1411 target
= strstr(match
, " to ");
1413 if (!match
|| !target
)
1415 if (--(cb
->remaining
) == 0) {
1416 len
= target
- match
;
1417 strbuf_reset(cb
->sb
);
1418 strbuf_add(cb
->sb
, match
, len
);
1419 return 1; /* we are done */
1425 * Parse @{-N} syntax, return the number of characters parsed
1426 * if successful; otherwise signal an error with negative value.
1428 static int interpret_nth_prior_checkout(struct repository
*r
,
1429 const char *name
, int namelen
,
1434 struct grab_nth_branch_switch_cbdata cb
;
1440 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1442 brace
= memchr(name
, '}', namelen
);
1445 nth
= strtol(name
+ 3, &num_end
, 10);
1446 if (num_end
!= brace
)
1453 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1454 "HEAD", grab_nth_branch_switch
, &cb
);
1456 retval
= brace
- name
+ 1;
1463 int repo_get_oid_mb(struct repository
*r
,
1465 struct object_id
*oid
)
1467 struct commit
*one
, *two
;
1468 struct commit_list
*mbs
;
1469 struct object_id oid_tmp
;
1473 dots
= strstr(name
, "...");
1475 return repo_get_oid(r
, name
, oid
);
1477 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1480 strbuf_init(&sb
, dots
- name
);
1481 strbuf_add(&sb
, name
, dots
- name
);
1482 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1483 strbuf_release(&sb
);
1487 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1491 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1493 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1496 mbs
= repo_get_merge_bases(r
, one
, two
);
1497 if (!mbs
|| mbs
->next
)
1501 oidcpy(oid
, &mbs
->item
->object
.oid
);
1503 free_commit_list(mbs
);
1507 /* parse @something syntax, when 'something' is not {.*} */
1508 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1512 if (len
|| name
[1] == '{')
1515 /* make sure it's a single @, or @@{.*}, not @foo */
1516 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1517 if (next
&& next
[1] != '{')
1520 next
= name
+ namelen
;
1521 if (next
!= name
+ 1)
1525 strbuf_add(buf
, "HEAD", 4);
1529 static int reinterpret(struct repository
*r
,
1530 const char *name
, int namelen
, int len
,
1531 struct strbuf
*buf
, unsigned allowed
)
1533 /* we have extra data, which might need further processing */
1534 struct strbuf tmp
= STRBUF_INIT
;
1535 int used
= buf
->len
;
1537 struct interpret_branch_name_options options
= {
1541 strbuf_add(buf
, name
+ len
, namelen
- len
);
1542 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1543 /* that data was not interpreted, remove our cruft */
1545 strbuf_setlen(buf
, used
);
1549 strbuf_addbuf(buf
, &tmp
);
1550 strbuf_release(&tmp
);
1551 /* tweak for size of {-N} versus expanded ref name */
1552 return ret
- used
+ len
;
1555 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1557 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1559 strbuf_addstr(buf
, s
);
1563 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1568 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1569 starts_with(refname
, "refs/heads/"))
1571 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1572 starts_with(refname
, "refs/remotes/"))
1578 static int interpret_branch_mark(struct repository
*r
,
1579 const char *name
, int namelen
,
1580 int at
, struct strbuf
*buf
,
1581 int (*get_mark
)(const char *, int),
1582 const char *(*get_data
)(struct branch
*,
1584 const struct interpret_branch_name_options
*options
)
1587 struct branch
*branch
;
1588 struct strbuf err
= STRBUF_INIT
;
1591 len
= get_mark(name
+ at
, namelen
- at
);
1595 if (memchr(name
, ':', at
))
1599 char *name_str
= xmemdupz(name
, at
);
1600 branch
= branch_get(name_str
);
1603 branch
= branch_get(NULL
);
1605 value
= get_data(branch
, &err
);
1607 if (options
->nonfatal_dangling_mark
) {
1608 strbuf_release(&err
);
1615 if (!branch_interpret_allowed(value
, options
->allowed
))
1618 set_shortened_ref(r
, buf
, value
);
1622 int repo_interpret_branch_name(struct repository
*r
,
1623 const char *name
, int namelen
,
1625 const struct interpret_branch_name_options
*options
)
1632 namelen
= strlen(name
);
1634 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1635 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1637 return len
; /* syntax Ok, not enough switches */
1638 } else if (len
> 0) {
1640 return len
; /* consumed all */
1642 return reinterpret(r
, name
, namelen
, len
, buf
,
1648 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1651 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1652 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1654 return reinterpret(r
, name
, namelen
, len
, buf
,
1658 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1659 upstream_mark
, branch_get_upstream
,
1664 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1665 push_mark
, branch_get_push
,
1674 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1676 int len
= strlen(name
);
1677 struct interpret_branch_name_options options
= {
1680 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1685 strbuf_add(sb
, name
+ used
, len
- used
);
1688 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1690 if (startup_info
->have_repository
)
1691 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1693 strbuf_addstr(sb
, name
);
1696 * This splice must be done even if we end up rejecting the
1697 * name; builtin/branch.c::copy_or_rename_branch() still wants
1698 * to see what the name expanded to so that "branch -m" can be
1699 * used as a tool to correct earlier mistakes.
1701 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1704 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1707 return check_refname_format(sb
->buf
, 0);
1711 * This is like "get_oid_basic()", except it allows "object ID expressions",
1712 * notably "xyz^" for "parent of xyz"
1714 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1716 struct object_context unused
;
1717 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1721 * This returns a non-zero value if the string (built using printf
1722 * format and the given arguments) is not a valid object.
1724 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1728 struct strbuf sb
= STRBUF_INIT
;
1731 strbuf_vaddf(&sb
, fmt
, ap
);
1734 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1735 strbuf_release(&sb
);
1741 * Many callers know that the user meant to name a commit-ish by
1742 * syntactical positions where the object name appears. Calling this
1743 * function allows the machinery to disambiguate shorter-than-unique
1744 * abbreviated object names between commit-ish and others.
1746 * Note that this does NOT error out when the named object is not a
1747 * commit-ish. It is merely to give a hint to the disambiguation
1750 int repo_get_oid_committish(struct repository
*r
,
1752 struct object_id
*oid
)
1754 struct object_context unused
;
1755 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1759 int repo_get_oid_treeish(struct repository
*r
,
1761 struct object_id
*oid
)
1763 struct object_context unused
;
1764 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1768 int repo_get_oid_commit(struct repository
*r
,
1770 struct object_id
*oid
)
1772 struct object_context unused
;
1773 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1777 int repo_get_oid_tree(struct repository
*r
,
1779 struct object_id
*oid
)
1781 struct object_context unused
;
1782 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1786 int repo_get_oid_blob(struct repository
*r
,
1788 struct object_id
*oid
)
1790 struct object_context unused
;
1791 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1795 /* Must be called only when object_name:filename doesn't exist. */
1796 static void diagnose_invalid_oid_path(struct repository
*r
,
1798 const char *filename
,
1799 const struct object_id
*tree_oid
,
1800 const char *object_name
,
1801 int object_name_len
)
1803 struct object_id oid
;
1804 unsigned short mode
;
1809 if (file_exists(filename
))
1810 die(_("path '%s' exists on disk, but not in '%.*s'"),
1811 filename
, object_name_len
, object_name
);
1812 if (is_missing_file_error(errno
)) {
1813 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1815 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1816 die(_("path '%s' exists, but not '%s'\n"
1817 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1820 object_name_len
, object_name
,
1822 object_name_len
, object_name
,
1825 die(_("path '%s' does not exist in '%.*s'"),
1826 filename
, object_name_len
, object_name
);
1830 /* Must be called only when :stage:filename doesn't exist. */
1831 static void diagnose_invalid_index_path(struct repository
*r
,
1834 const char *filename
)
1836 struct index_state
*istate
= r
->index
;
1837 const struct cache_entry
*ce
;
1839 unsigned namelen
= strlen(filename
);
1840 struct strbuf fullname
= STRBUF_INIT
;
1845 /* Wrong stage number? */
1846 pos
= index_name_pos(istate
, filename
, namelen
);
1849 if (pos
< istate
->cache_nr
) {
1850 ce
= istate
->cache
[pos
];
1851 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1852 ce_namelen(ce
) == namelen
&&
1853 !memcmp(ce
->name
, filename
, namelen
))
1854 die(_("path '%s' is in the index, but not at stage %d\n"
1855 "hint: Did you mean ':%d:%s'?"),
1857 ce_stage(ce
), filename
);
1860 /* Confusion between relative and absolute filenames? */
1861 strbuf_addstr(&fullname
, prefix
);
1862 strbuf_addstr(&fullname
, filename
);
1863 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1866 if (pos
< istate
->cache_nr
) {
1867 ce
= istate
->cache
[pos
];
1868 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1869 ce_namelen(ce
) == fullname
.len
&&
1870 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1871 die(_("path '%s' is in the index, but not '%s'\n"
1872 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1873 fullname
.buf
, filename
,
1874 ce_stage(ce
), fullname
.buf
,
1875 ce_stage(ce
), filename
);
1878 if (repo_file_exists(r
, filename
))
1879 die(_("path '%s' exists on disk, but not in the index"), filename
);
1880 if (is_missing_file_error(errno
))
1881 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1884 strbuf_release(&fullname
);
1888 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1890 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1893 if (r
!= the_repository
|| !is_inside_work_tree())
1894 die(_("relative path syntax can't be used outside working tree"));
1896 /* die() inside prefix_path() if resolved path is outside worktree */
1897 return prefix_path(startup_info
->prefix
,
1898 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1902 static int reject_tree_in_index(struct repository
*repo
,
1904 const struct cache_entry
*ce
,
1909 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1912 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1916 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1920 struct object_id
*oid
,
1921 struct object_context
*oc
)
1923 int ret
, bracket_depth
;
1924 int namelen
= strlen(name
);
1926 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1928 memset(oc
, 0, sizeof(*oc
));
1929 oc
->mode
= S_IFINVALID
;
1930 strbuf_init(&oc
->symlink_path
, 0);
1931 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1932 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1933 die(_("<object>:<path> required, only <object> '%s' given"),
1938 * tree:path --> object name of path in tree
1939 * :path -> object name of absolute path in index
1940 * :./path -> object name of path relative to cwd in index
1941 * :[0-3]:path -> object name of path in index at stage
1942 * :/foo -> recent commit matching foo
1944 if (name
[0] == ':') {
1946 const struct cache_entry
*ce
;
1947 char *new_path
= NULL
;
1949 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1950 struct handle_one_ref_cb cb
;
1951 struct commit_list
*list
= NULL
;
1955 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1956 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1957 commit_list_sort_by_date(&list
);
1958 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1962 name
[1] < '0' || '3' < name
[1])
1965 stage
= name
[1] - '0';
1968 new_path
= resolve_relative_path(repo
, cp
);
1970 namelen
= namelen
- (cp
- name
);
1973 namelen
= strlen(cp
);
1976 if (flags
& GET_OID_RECORD_PATH
)
1977 oc
->path
= xstrdup(cp
);
1979 if (!repo
->index
|| !repo
->index
->cache
)
1980 repo_read_index(repo
);
1981 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1984 while (pos
< repo
->index
->cache_nr
) {
1985 ce
= repo
->index
->cache
[pos
];
1986 if (ce_namelen(ce
) != namelen
||
1987 memcmp(ce
->name
, cp
, namelen
))
1989 if (ce_stage(ce
) == stage
) {
1991 if (reject_tree_in_index(repo
, only_to_die
, ce
,
1994 oidcpy(oid
, &ce
->oid
);
1995 oc
->mode
= ce
->ce_mode
;
2000 if (only_to_die
&& name
[1] && name
[1] != '/')
2001 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2005 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2008 else if (bracket_depth
&& *cp
== '}')
2010 else if (!bracket_depth
&& *cp
== ':')
2014 struct object_id tree_oid
;
2015 int len
= cp
- name
;
2016 unsigned sub_flags
= flags
;
2018 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2019 sub_flags
|= GET_OID_TREEISH
;
2021 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2022 const char *filename
= cp
+1;
2023 char *new_filename
= NULL
;
2025 new_filename
= resolve_relative_path(repo
, filename
);
2027 filename
= new_filename
;
2028 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2029 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2030 filename
, oid
, &oc
->symlink_path
,
2033 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2035 if (ret
&& only_to_die
) {
2036 diagnose_invalid_oid_path(repo
, prefix
,
2042 if (flags
& GET_OID_RECORD_PATH
)
2043 oc
->path
= xstrdup(filename
);
2049 die(_("invalid object name '%.*s'."), len
, name
);
2056 * Call this function when you know "name" given by the end user must
2057 * name an object but it doesn't; the function _may_ die with a better
2058 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2059 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2060 * you have a chance to diagnose the error further.
2062 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2066 struct object_context oc
;
2067 struct object_id oid
;
2068 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2072 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2075 struct object_id
*oid
,
2076 struct object_context
*oc
)
2078 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2079 BUG("incompatible flags for get_oid_with_context");
2080 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);