2 #include "object-name.h"
5 #include "environment.h"
12 #include "tree-walk.h"
16 #include "oid-array.h"
18 #include "object-store.h"
19 #include "repository.h"
21 #include "submodule.h"
23 #include "commit-reach.h"
26 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
28 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
30 struct disambiguate_state
{
31 int len
; /* length of prefix in hex chars */
32 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
33 struct object_id bin_pfx
;
35 struct repository
*repo
;
36 disambiguate_hint_fn fn
;
38 struct object_id candidate
;
39 unsigned candidate_exists
:1;
40 unsigned candidate_checked
:1;
41 unsigned candidate_ok
:1;
42 unsigned disambiguate_fn_used
:1;
44 unsigned always_call_fn
:1;
47 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
49 if (ds
->always_call_fn
) {
50 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
53 if (!ds
->candidate_exists
) {
54 /* this is the first candidate */
55 oidcpy(&ds
->candidate
, current
);
56 ds
->candidate_exists
= 1;
58 } else if (oideq(&ds
->candidate
, current
)) {
59 /* the same as what we already have seen */
64 /* cannot disambiguate between ds->candidate and current */
69 if (!ds
->candidate_checked
) {
70 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
71 ds
->disambiguate_fn_used
= 1;
72 ds
->candidate_checked
= 1;
75 if (!ds
->candidate_ok
) {
76 /* discard the candidate; we know it does not satisfy fn */
77 oidcpy(&ds
->candidate
, current
);
78 ds
->candidate_checked
= 0;
82 /* if we reach this point, we know ds->candidate satisfies fn */
83 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
85 * if both current and candidate satisfy fn, we cannot
92 /* otherwise, current can be discarded and candidate is still good */
95 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
97 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
99 struct disambiguate_state
*ds
= arg
;
100 /* no need to call match_hash, oidtree_each did prefix match */
101 update_candidates(ds
, oid
);
102 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
105 static void find_short_object_filename(struct disambiguate_state
*ds
)
107 struct object_directory
*odb
;
109 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
110 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
111 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
114 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
124 if ((*a
^ *b
) & 0xf0)
129 static void unique_in_midx(struct multi_pack_index
*m
,
130 struct disambiguate_state
*ds
)
132 uint32_t num
, i
, first
= 0;
133 const struct object_id
*current
= NULL
;
134 num
= m
->num_objects
;
139 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
142 * At this point, "first" is the location of the lowest object
143 * with an object name that could match "bin_pfx". See if we have
144 * 0, 1 or more objects that actually match(es).
146 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
147 struct object_id oid
;
148 current
= nth_midxed_object_oid(&oid
, m
, i
);
149 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
151 update_candidates(ds
, current
);
155 static void unique_in_pack(struct packed_git
*p
,
156 struct disambiguate_state
*ds
)
158 uint32_t num
, i
, first
= 0;
160 if (p
->multi_pack_index
)
163 if (open_pack_index(p
) || !p
->num_objects
)
166 num
= p
->num_objects
;
167 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
170 * At this point, "first" is the location of the lowest object
171 * with an object name that could match "bin_pfx". See if we have
172 * 0, 1 or more objects that actually match(es).
174 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
175 struct object_id oid
;
176 nth_packed_object_id(&oid
, p
, i
);
177 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, oid
.hash
))
179 update_candidates(ds
, &oid
);
183 static void find_short_packed_object(struct disambiguate_state
*ds
)
185 struct multi_pack_index
*m
;
186 struct packed_git
*p
;
188 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
190 unique_in_midx(m
, ds
);
191 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
193 unique_in_pack(p
, ds
);
196 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
197 struct object_id
*oid
)
200 return SHORT_NAME_AMBIGUOUS
;
202 if (!ds
->candidate_exists
)
203 return MISSING_OBJECT
;
205 if (!ds
->candidate_checked
)
207 * If this is the only candidate, there is no point
208 * calling the disambiguation hint callback.
210 * On the other hand, if the current candidate
211 * replaced an earlier candidate that did _not_ pass
212 * the disambiguation hint callback, then we do have
213 * more than one objects that match the short name
214 * given, so we should make sure this one matches;
215 * otherwise, if we discovered this one and the one
216 * that we previously discarded in the reverse order,
217 * we would end up showing different results in the
220 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
221 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
223 if (!ds
->candidate_ok
)
224 return SHORT_NAME_AMBIGUOUS
;
226 oidcpy(oid
, &ds
->candidate
);
230 static int disambiguate_commit_only(struct repository
*r
,
231 const struct object_id
*oid
,
232 void *cb_data UNUSED
)
234 int kind
= oid_object_info(r
, oid
, NULL
);
235 return kind
== OBJ_COMMIT
;
238 static int disambiguate_committish_only(struct repository
*r
,
239 const struct object_id
*oid
,
240 void *cb_data UNUSED
)
245 kind
= oid_object_info(r
, oid
, NULL
);
246 if (kind
== OBJ_COMMIT
)
251 /* We need to do this the hard way... */
252 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
253 if (obj
&& obj
->type
== OBJ_COMMIT
)
258 static int disambiguate_tree_only(struct repository
*r
,
259 const struct object_id
*oid
,
260 void *cb_data UNUSED
)
262 int kind
= oid_object_info(r
, oid
, NULL
);
263 return kind
== OBJ_TREE
;
266 static int disambiguate_treeish_only(struct repository
*r
,
267 const struct object_id
*oid
,
268 void *cb_data UNUSED
)
273 kind
= oid_object_info(r
, oid
, NULL
);
274 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
279 /* We need to do this the hard way... */
280 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
281 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
286 static int disambiguate_blob_only(struct repository
*r
,
287 const struct object_id
*oid
,
288 void *cb_data UNUSED
)
290 int kind
= oid_object_info(r
, oid
, NULL
);
291 return kind
== OBJ_BLOB
;
294 static disambiguate_hint_fn default_disambiguate_hint
;
296 int set_disambiguate_hint_config(const char *var
, const char *value
)
298 static const struct {
300 disambiguate_hint_fn fn
;
303 { "commit", disambiguate_commit_only
},
304 { "committish", disambiguate_committish_only
},
305 { "tree", disambiguate_tree_only
},
306 { "treeish", disambiguate_treeish_only
},
307 { "blob", disambiguate_blob_only
}
312 return config_error_nonbool(var
);
314 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
315 if (!strcasecmp(value
, hints
[i
].name
)) {
316 default_disambiguate_hint
= hints
[i
].fn
;
321 return error("unknown hint type for '%s': %s", var
, value
);
324 static int init_object_disambiguation(struct repository
*r
,
325 const char *name
, int len
,
326 struct disambiguate_state
*ds
)
330 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
333 memset(ds
, 0, sizeof(*ds
));
335 for (i
= 0; i
< len
;i
++) {
336 unsigned char c
= name
[i
];
338 if (c
>= '0' && c
<= '9')
340 else if (c
>= 'a' && c
<= 'f')
342 else if (c
>= 'A' && c
<='F') {
351 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
355 ds
->hex_pfx
[len
] = '\0';
361 struct ambiguous_output
{
362 const struct disambiguate_state
*ds
;
363 struct strbuf advice
;
367 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
369 struct ambiguous_output
*state
= data
;
370 const struct disambiguate_state
*ds
= state
->ds
;
371 struct strbuf
*advice
= &state
->advice
;
372 struct strbuf
*sb
= &state
->sb
;
376 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
379 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
380 type
= oid_object_info(ds
->repo
, oid
, NULL
);
384 * TRANSLATORS: This is a line of ambiguous object
385 * output shown when we cannot look up or parse the
386 * object in question. E.g. "deadbeef [bad object]".
388 strbuf_addf(sb
, _("%s [bad object]"), hash
);
392 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
393 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
395 if (type
== OBJ_COMMIT
) {
396 struct strbuf date
= STRBUF_INIT
;
397 struct strbuf msg
= STRBUF_INIT
;
398 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
401 struct pretty_print_context pp
= {0};
402 pp
.date_mode
.type
= DATE_SHORT
;
403 repo_format_commit_message(the_repository
, commit
,
405 repo_format_commit_message(the_repository
, commit
,
410 * TRANSLATORS: This is a line of ambiguous commit
411 * object output. E.g.:
413 * "deadbeef commit 2021-01-01 - Some Commit Message"
415 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
418 strbuf_release(&date
);
419 strbuf_release(&msg
);
420 } else if (type
== OBJ_TAG
) {
421 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
423 if (!parse_tag(tag
) && tag
->tag
) {
425 * TRANSLATORS: This is a line of ambiguous
426 * tag object output. E.g.:
428 * "deadbeef tag 2022-01-01 - Some Tag Message"
430 * The second argument is the YYYY-MM-DD found
433 * The third argument is the "tag" string
436 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
437 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
441 * TRANSLATORS: This is a line of ambiguous
442 * tag object output where we couldn't parse
443 * the tag itself. E.g.:
445 * "deadbeef [bad tag, could not parse it]"
447 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
450 } else if (type
== OBJ_TREE
) {
452 * TRANSLATORS: This is a line of ambiguous <type>
453 * object output. E.g. "deadbeef tree".
455 strbuf_addf(sb
, _("%s tree"), hash
);
456 } else if (type
== OBJ_BLOB
) {
458 * TRANSLATORS: This is a line of ambiguous <type>
459 * object output. E.g. "deadbeef blob".
461 strbuf_addf(sb
, _("%s blob"), hash
);
467 * TRANSLATORS: This is line item of ambiguous object output
468 * from describe_ambiguous_object() above. For RTL languages
469 * you'll probably want to swap the "%s" and leading " " space
472 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
478 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
480 oid_array_append(data
, oid
);
484 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
485 const struct object_id
*oid
,
488 return collect_ambiguous(oid
, data
);
491 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
493 struct repository
*sort_ambiguous_repo
= ctx
;
494 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
495 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
500 * Sorts by hash within the same object type, just as
501 * oid_array_for_each_unique() would do.
503 if (a_type
== b_type
)
507 * Between object types show tags, then commits, and finally
510 * The object_type enum is commit, tree, blob, tag, but we
511 * want tag, commit, tree blob. Cleverly (perhaps too
512 * cleverly) do that with modulus, since the enum assigns 1 to
513 * commit, so tag becomes 0.
515 a_type_sort
= a_type
% 4;
516 b_type_sort
= b_type
% 4;
517 return a_type_sort
> b_type_sort
? 1 : -1;
520 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
522 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
525 static enum get_oid_result
get_short_oid(struct repository
*r
,
526 const char *name
, int len
,
527 struct object_id
*oid
,
531 struct disambiguate_state ds
;
532 int quietly
= !!(flags
& GET_OID_QUIETLY
);
534 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
537 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
538 BUG("multiple get_short_oid disambiguator flags");
540 if (flags
& GET_OID_COMMIT
)
541 ds
.fn
= disambiguate_commit_only
;
542 else if (flags
& GET_OID_COMMITTISH
)
543 ds
.fn
= disambiguate_committish_only
;
544 else if (flags
& GET_OID_TREE
)
545 ds
.fn
= disambiguate_tree_only
;
546 else if (flags
& GET_OID_TREEISH
)
547 ds
.fn
= disambiguate_treeish_only
;
548 else if (flags
& GET_OID_BLOB
)
549 ds
.fn
= disambiguate_blob_only
;
551 ds
.fn
= default_disambiguate_hint
;
553 find_short_object_filename(&ds
);
554 find_short_packed_object(&ds
);
555 status
= finish_object_disambiguation(&ds
, oid
);
558 * If we didn't find it, do the usual reprepare() slow-path,
559 * since the object may have recently been added to the repository
560 * or migrated from loose to packed.
562 if (status
== MISSING_OBJECT
) {
563 reprepare_packed_git(r
);
564 find_short_object_filename(&ds
);
565 find_short_packed_object(&ds
);
566 status
= finish_object_disambiguation(&ds
, oid
);
569 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
570 struct oid_array collect
= OID_ARRAY_INIT
;
571 struct ambiguous_output out
= {
574 .advice
= STRBUF_INIT
,
577 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
580 * We may still have ambiguity if we simply saw a series of
581 * candidates that did not satisfy our hint function. In
582 * that case, we still want to show them, so disable the hint
588 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
589 sort_ambiguous_oid_array(r
, &collect
);
591 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
592 BUG("show_ambiguous_object shouldn't return non-zero");
595 * TRANSLATORS: The argument is the list of ambiguous
596 * objects composed in show_ambiguous_object(). See
597 * its "TRANSLATORS" comments for details.
599 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
601 oid_array_clear(&collect
);
602 strbuf_release(&out
.advice
);
603 strbuf_release(&out
.sb
);
609 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
610 each_abbrev_fn fn
, void *cb_data
)
612 struct oid_array collect
= OID_ARRAY_INIT
;
613 struct disambiguate_state ds
;
616 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
619 ds
.always_call_fn
= 1;
620 ds
.fn
= repo_collect_ambiguous
;
621 ds
.cb_data
= &collect
;
622 find_short_object_filename(&ds
);
623 find_short_packed_object(&ds
);
625 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
626 oid_array_clear(&collect
);
631 * Return the slot of the most-significant bit set in "val". There are various
632 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
633 * probably not a big deal here.
635 static unsigned msb(unsigned long val
)
643 struct min_abbrev_data
{
644 unsigned int init_len
;
645 unsigned int cur_len
;
647 struct repository
*repo
;
648 const struct object_id
*oid
;
651 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
654 static const char hex
[] = "0123456789abcdef";
657 return hex
[oid
->hash
[pos
>> 1] >> 4];
659 return hex
[oid
->hash
[pos
>> 1] & 0xf];
662 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
664 struct min_abbrev_data
*mad
= cb_data
;
666 unsigned int i
= mad
->init_len
;
667 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
670 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
671 mad
->cur_len
= i
+ 1;
676 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
677 const struct object_id
*oid
,
680 return extend_abbrev_len(oid
, cb_data
);
683 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
684 struct min_abbrev_data
*mad
)
687 uint32_t num
, first
= 0;
688 struct object_id oid
;
689 const struct object_id
*mad_oid
;
694 num
= m
->num_objects
;
696 match
= bsearch_midx(mad_oid
, m
, &first
);
699 * first is now the position in the packfile where we would insert
700 * mad->hash if it does not exist (or the position of mad->hash if
701 * it does exist). Hence, we consider a maximum of two objects
702 * nearby for the abbreviation length.
706 if (nth_midxed_object_oid(&oid
, m
, first
))
707 extend_abbrev_len(&oid
, mad
);
708 } else if (first
< num
- 1) {
709 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
710 extend_abbrev_len(&oid
, mad
);
713 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
714 extend_abbrev_len(&oid
, mad
);
716 mad
->init_len
= mad
->cur_len
;
719 static void find_abbrev_len_for_pack(struct packed_git
*p
,
720 struct min_abbrev_data
*mad
)
723 uint32_t num
, first
= 0;
724 struct object_id oid
;
725 const struct object_id
*mad_oid
;
727 if (p
->multi_pack_index
)
730 if (open_pack_index(p
) || !p
->num_objects
)
733 num
= p
->num_objects
;
735 match
= bsearch_pack(mad_oid
, p
, &first
);
738 * first is now the position in the packfile where we would insert
739 * mad->hash if it does not exist (or the position of mad->hash if
740 * it does exist). Hence, we consider a maximum of two objects
741 * nearby for the abbreviation length.
745 if (!nth_packed_object_id(&oid
, p
, first
))
746 extend_abbrev_len(&oid
, mad
);
747 } else if (first
< num
- 1) {
748 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
749 extend_abbrev_len(&oid
, mad
);
752 if (!nth_packed_object_id(&oid
, p
, first
- 1))
753 extend_abbrev_len(&oid
, mad
);
755 mad
->init_len
= mad
->cur_len
;
758 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
760 struct multi_pack_index
*m
;
761 struct packed_git
*p
;
763 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
764 find_abbrev_len_for_midx(m
, mad
);
765 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
766 find_abbrev_len_for_pack(p
, mad
);
769 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
770 const struct object_id
*oid
, int len
)
772 struct disambiguate_state ds
;
773 struct min_abbrev_data mad
;
774 struct object_id oid_ret
;
775 const unsigned hexsz
= r
->hash_algo
->hexsz
;
778 unsigned long count
= repo_approximate_object_count(r
);
780 * Add one because the MSB only tells us the highest bit set,
781 * not including the value of all the _other_ bits (so "15"
782 * is only one off of 2^4, but the MSB is the 3rd bit.
784 len
= msb(count
) + 1;
786 * We now know we have on the order of 2^len objects, which
787 * expects a collision at 2^(len/2). But we also care about hex
788 * chars, not bits, and there are 4 bits per hex. So all
789 * together we need to divide by 2 and round up.
791 len
= DIV_ROUND_UP(len
, 2);
793 * For very small repos, we stick with our regular fallback.
795 if (len
< FALLBACK_DEFAULT_ABBREV
)
796 len
= FALLBACK_DEFAULT_ABBREV
;
799 oid_to_hex_r(hex
, oid
);
800 if (len
== hexsz
|| !len
)
809 find_abbrev_len_packed(&mad
);
811 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
814 ds
.fn
= repo_extend_abbrev_len
;
815 ds
.always_call_fn
= 1;
816 ds
.cb_data
= (void *)&mad
;
818 find_short_object_filename(&ds
);
819 (void)finish_object_disambiguation(&ds
, &oid_ret
);
821 hex
[mad
.cur_len
] = 0;
825 const char *repo_find_unique_abbrev(struct repository
*r
,
826 const struct object_id
*oid
,
830 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
831 char *hex
= hexbuffer
[bufno
];
832 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
833 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
837 static int ambiguous_path(const char *path
, int len
)
842 for (cnt
= 0; cnt
< len
; cnt
++) {
862 static inline int at_mark(const char *string
, int len
,
863 const char **suffix
, int nr
)
867 for (i
= 0; i
< nr
; i
++) {
868 int suffix_len
= strlen(suffix
[i
]);
869 if (suffix_len
<= len
870 && !strncasecmp(string
, suffix
[i
], suffix_len
))
876 static inline int upstream_mark(const char *string
, int len
)
878 const char *suffix
[] = { "@{upstream}", "@{u}" };
879 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
882 static inline int push_mark(const char *string
, int len
)
884 const char *suffix
[] = { "@{push}" };
885 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
888 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
889 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
891 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
892 struct object_id
*oid
, unsigned int flags
)
894 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
895 static const char *object_name_msg
= N_(
896 "Git normally never creates a ref that ends with 40 hex characters\n"
897 "because it will be ignored when you just specify 40-hex. These refs\n"
898 "may be created by mistake. For example,\n"
900 " git switch -c $br $(git rev-parse ...)\n"
902 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
903 "examine these refs and maybe delete them. Turn this message off by\n"
904 "running \"git config advice.objectNameWarning false\"");
905 struct object_id tmp_oid
;
906 char *real_ref
= NULL
;
908 int at
, reflog_len
, nth_prior
= 0;
910 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
911 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
912 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
913 if (refs_found
> 0) {
914 warning(warn_msg
, len
, str
);
915 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
916 fprintf(stderr
, "%s\n", _(object_name_msg
));
923 /* basic@{time or number or -number} format to query ref-log */
925 if (len
&& str
[len
-1] == '}') {
926 for (at
= len
-4; at
>= 0; at
--) {
927 if (str
[at
] == '@' && str
[at
+1] == '{') {
928 if (str
[at
+2] == '-') {
930 /* @{-N} not at start */
935 if (!upstream_mark(str
+ at
, len
- at
) &&
936 !push_mark(str
+ at
, len
- at
)) {
937 reflog_len
= (len
-1) - (at
+2);
945 /* Accept only unambiguous ref paths. */
946 if (len
&& ambiguous_path(str
, len
))
950 struct strbuf buf
= STRBUF_INIT
;
953 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
954 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
955 strbuf_release(&buf
);
961 if (!len
&& reflog_len
)
962 /* allow "@{...}" to mean the current branch reflog */
963 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, 0);
965 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
967 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, 0);
972 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
974 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
975 warning(warn_msg
, len
, str
);
983 /* Is it asking for N-th entry, or approxidate? */
984 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
985 char ch
= str
[at
+2+i
];
986 if ('0' <= ch
&& ch
<= '9')
987 nth
= nth
* 10 + ch
- '0';
991 if (100000000 <= nth
) {
998 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
999 at_time
= approxidate_careful(tmp
, &errors
);
1006 if (read_ref_at(get_main_ref_store(r
),
1007 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1008 &co_time
, &co_tz
, &co_cnt
)) {
1010 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1015 if (!(flags
& GET_OID_QUIETLY
)) {
1016 warning(_("log for '%.*s' only goes back to %s"),
1018 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1021 if (flags
& GET_OID_QUIETLY
) {
1024 die(_("log for '%.*s' only has %d entries"),
1034 static enum get_oid_result
get_parent(struct repository
*r
,
1035 const char *name
, int len
,
1036 struct object_id
*result
, int idx
)
1038 struct object_id oid
;
1039 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1040 GET_OID_COMMITTISH
);
1041 struct commit
*commit
;
1042 struct commit_list
*p
;
1046 commit
= lookup_commit_reference(r
, &oid
);
1047 if (repo_parse_commit(r
, commit
))
1048 return MISSING_OBJECT
;
1050 oidcpy(result
, &commit
->object
.oid
);
1053 p
= commit
->parents
;
1056 oidcpy(result
, &p
->item
->object
.oid
);
1061 return MISSING_OBJECT
;
1064 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1065 const char *name
, int len
,
1066 struct object_id
*result
,
1069 struct object_id oid
;
1070 struct commit
*commit
;
1073 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1076 commit
= lookup_commit_reference(r
, &oid
);
1078 return MISSING_OBJECT
;
1080 while (generation
--) {
1081 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1082 return MISSING_OBJECT
;
1083 commit
= commit
->parents
->item
;
1085 oidcpy(result
, &commit
->object
.oid
);
1089 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1090 struct object
*o
, enum object_type expected_type
)
1092 if (name
&& !namelen
)
1093 namelen
= strlen(name
);
1095 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1097 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1099 if (o
->type
== OBJ_TAG
)
1100 o
= ((struct tag
*) o
)->tagged
;
1101 else if (o
->type
== OBJ_COMMIT
)
1102 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1105 error("%.*s: expected %s type, but the object "
1106 "dereferences to %s type",
1107 namelen
, name
, type_name(expected_type
),
1108 type_name(o
->type
));
1114 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1115 struct object_id
*oid
, unsigned lookup_flags
)
1117 struct object_id outer
;
1119 unsigned int expected_type
= 0;
1123 * "ref^{type}" dereferences ref repeatedly until you cannot
1124 * dereference anymore, or you get an object of given type,
1125 * whichever comes first. "ref^{}" means just dereference
1126 * tags until you get a non-tag. "ref^0" is a shorthand for
1127 * "ref^{commit}". "commit^{tree}" could be used to find the
1128 * top-level tree of the given commit.
1130 if (len
< 4 || name
[len
-1] != '}')
1133 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1135 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1141 sp
++; /* beginning of type name, or closing brace for empty */
1142 if (starts_with(sp
, "commit}"))
1143 expected_type
= OBJ_COMMIT
;
1144 else if (starts_with(sp
, "tag}"))
1145 expected_type
= OBJ_TAG
;
1146 else if (starts_with(sp
, "tree}"))
1147 expected_type
= OBJ_TREE
;
1148 else if (starts_with(sp
, "blob}"))
1149 expected_type
= OBJ_BLOB
;
1150 else if (starts_with(sp
, "object}"))
1151 expected_type
= OBJ_ANY
;
1152 else if (sp
[0] == '}')
1153 expected_type
= OBJ_NONE
;
1154 else if (sp
[0] == '/')
1155 expected_type
= OBJ_COMMIT
;
1159 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1160 if (expected_type
== OBJ_COMMIT
)
1161 lookup_flags
|= GET_OID_COMMITTISH
;
1162 else if (expected_type
== OBJ_TREE
)
1163 lookup_flags
|= GET_OID_TREEISH
;
1165 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1168 o
= parse_object(r
, &outer
);
1171 if (!expected_type
) {
1172 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1173 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1175 oidcpy(oid
, &o
->oid
);
1180 * At this point, the syntax look correct, so
1181 * if we do not get the needed object, we should
1184 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1188 oidcpy(oid
, &o
->oid
);
1190 /* "$commit^{/foo}" */
1193 struct commit_list
*list
= NULL
;
1196 * $commit^{/}. Some regex implementation may reject.
1197 * We don't need regex anyway. '' pattern always matches.
1202 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1203 commit_list_insert((struct commit
*)o
, &list
);
1204 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1211 static int get_describe_name(struct repository
*r
,
1212 const char *name
, int len
,
1213 struct object_id
*oid
)
1216 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1218 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1220 if (!isxdigit(ch
)) {
1221 /* We must be looking at g in "SOMETHING-g"
1222 * for it to be describe output.
1224 if (ch
== 'g' && cp
[-1] == '-') {
1227 return get_short_oid(r
,
1228 cp
, len
, oid
, flags
);
1235 static enum get_oid_result
get_oid_1(struct repository
*r
,
1236 const char *name
, int len
,
1237 struct object_id
*oid
,
1238 unsigned lookup_flags
)
1240 int ret
, has_suffix
;
1244 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1247 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1249 if ('0' <= ch
&& ch
<= '9')
1251 if (ch
== '~' || ch
== '^')
1257 unsigned int num
= 0;
1258 int len1
= cp
- name
;
1260 while (cp
< name
+ len
) {
1261 unsigned int digit
= *cp
++ - '0';
1262 if (unsigned_mult_overflows(num
, 10))
1263 return MISSING_OBJECT
;
1265 if (unsigned_add_overflows(num
, digit
))
1266 return MISSING_OBJECT
;
1269 if (!num
&& len1
== len
- 1)
1271 else if (num
> INT_MAX
)
1272 return MISSING_OBJECT
;
1273 if (has_suffix
== '^')
1274 return get_parent(r
, name
, len1
, oid
, num
);
1275 /* else if (has_suffix == '~') -- goes without saying */
1276 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1279 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1283 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1287 /* It could be describe output that is "SOMETHING-gXXXX" */
1288 ret
= get_describe_name(r
, name
, len
, oid
);
1292 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1296 * This interprets names like ':/Initial revision of "git"' by searching
1297 * through history and returning the first commit whose message starts
1298 * the given regular expression.
1300 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1302 * For a literal '!' character at the beginning of a pattern, you have to repeat
1303 * that, like: ':/!!foo'
1305 * For future extension, all other sequences beginning with ':/!' are reserved.
1308 /* Remember to update object flag allocation in object.h */
1309 #define ONELINE_SEEN (1u<<20)
1311 struct handle_one_ref_cb
{
1312 struct repository
*repo
;
1313 struct commit_list
**list
;
1316 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1320 struct handle_one_ref_cb
*cb
= cb_data
;
1321 struct commit_list
**list
= cb
->list
;
1322 struct object
*object
= parse_object(cb
->repo
, oid
);
1325 if (object
->type
== OBJ_TAG
) {
1326 object
= deref_tag(cb
->repo
, object
, path
,
1331 if (object
->type
!= OBJ_COMMIT
)
1333 commit_list_insert((struct commit
*)object
, list
);
1337 static int get_oid_oneline(struct repository
*r
,
1338 const char *prefix
, struct object_id
*oid
,
1339 struct commit_list
*list
)
1341 struct commit_list
*backup
= NULL
, *l
;
1346 if (prefix
[0] == '!') {
1349 if (prefix
[0] == '-') {
1352 } else if (prefix
[0] != '!') {
1357 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1360 for (l
= list
; l
; l
= l
->next
) {
1361 l
->item
->object
.flags
|= ONELINE_SEEN
;
1362 commit_list_insert(l
->item
, &backup
);
1365 const char *p
, *buf
;
1366 struct commit
*commit
;
1369 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1370 if (!parse_object(r
, &commit
->object
.oid
))
1372 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1373 p
= strstr(buf
, "\n\n");
1374 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1375 repo_unuse_commit_buffer(r
, commit
, buf
);
1378 oidcpy(oid
, &commit
->object
.oid
);
1384 free_commit_list(list
);
1385 for (l
= backup
; l
; l
= l
->next
)
1386 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1387 free_commit_list(backup
);
1388 return found
? 0 : -1;
1391 struct grab_nth_branch_switch_cbdata
{
1396 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1397 struct object_id
*noid UNUSED
,
1398 const char *email UNUSED
,
1399 timestamp_t timestamp UNUSED
,
1401 const char *message
, void *cb_data
)
1403 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1404 const char *match
= NULL
, *target
= NULL
;
1407 if (skip_prefix(message
, "checkout: moving from ", &match
))
1408 target
= strstr(match
, " to ");
1410 if (!match
|| !target
)
1412 if (--(cb
->remaining
) == 0) {
1413 len
= target
- match
;
1414 strbuf_reset(cb
->sb
);
1415 strbuf_add(cb
->sb
, match
, len
);
1416 return 1; /* we are done */
1422 * Parse @{-N} syntax, return the number of characters parsed
1423 * if successful; otherwise signal an error with negative value.
1425 static int interpret_nth_prior_checkout(struct repository
*r
,
1426 const char *name
, int namelen
,
1431 struct grab_nth_branch_switch_cbdata cb
;
1437 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1439 brace
= memchr(name
, '}', namelen
);
1442 nth
= strtol(name
+ 3, &num_end
, 10);
1443 if (num_end
!= brace
)
1450 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1451 "HEAD", grab_nth_branch_switch
, &cb
);
1453 retval
= brace
- name
+ 1;
1460 int repo_get_oid_mb(struct repository
*r
,
1462 struct object_id
*oid
)
1464 struct commit
*one
, *two
;
1465 struct commit_list
*mbs
;
1466 struct object_id oid_tmp
;
1470 dots
= strstr(name
, "...");
1472 return repo_get_oid(r
, name
, oid
);
1474 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1477 strbuf_init(&sb
, dots
- name
);
1478 strbuf_add(&sb
, name
, dots
- name
);
1479 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1480 strbuf_release(&sb
);
1484 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1488 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1490 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1493 mbs
= repo_get_merge_bases(r
, one
, two
);
1494 if (!mbs
|| mbs
->next
)
1498 oidcpy(oid
, &mbs
->item
->object
.oid
);
1500 free_commit_list(mbs
);
1504 /* parse @something syntax, when 'something' is not {.*} */
1505 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1509 if (len
|| name
[1] == '{')
1512 /* make sure it's a single @, or @@{.*}, not @foo */
1513 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1514 if (next
&& next
[1] != '{')
1517 next
= name
+ namelen
;
1518 if (next
!= name
+ 1)
1522 strbuf_add(buf
, "HEAD", 4);
1526 static int reinterpret(struct repository
*r
,
1527 const char *name
, int namelen
, int len
,
1528 struct strbuf
*buf
, unsigned allowed
)
1530 /* we have extra data, which might need further processing */
1531 struct strbuf tmp
= STRBUF_INIT
;
1532 int used
= buf
->len
;
1534 struct interpret_branch_name_options options
= {
1538 strbuf_add(buf
, name
+ len
, namelen
- len
);
1539 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1540 /* that data was not interpreted, remove our cruft */
1542 strbuf_setlen(buf
, used
);
1546 strbuf_addbuf(buf
, &tmp
);
1547 strbuf_release(&tmp
);
1548 /* tweak for size of {-N} versus expanded ref name */
1549 return ret
- used
+ len
;
1552 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1554 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1556 strbuf_addstr(buf
, s
);
1560 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1565 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1566 starts_with(refname
, "refs/heads/"))
1568 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1569 starts_with(refname
, "refs/remotes/"))
1575 static int interpret_branch_mark(struct repository
*r
,
1576 const char *name
, int namelen
,
1577 int at
, struct strbuf
*buf
,
1578 int (*get_mark
)(const char *, int),
1579 const char *(*get_data
)(struct branch
*,
1581 const struct interpret_branch_name_options
*options
)
1584 struct branch
*branch
;
1585 struct strbuf err
= STRBUF_INIT
;
1588 len
= get_mark(name
+ at
, namelen
- at
);
1592 if (memchr(name
, ':', at
))
1596 char *name_str
= xmemdupz(name
, at
);
1597 branch
= branch_get(name_str
);
1600 branch
= branch_get(NULL
);
1602 value
= get_data(branch
, &err
);
1604 if (options
->nonfatal_dangling_mark
) {
1605 strbuf_release(&err
);
1612 if (!branch_interpret_allowed(value
, options
->allowed
))
1615 set_shortened_ref(r
, buf
, value
);
1619 int repo_interpret_branch_name(struct repository
*r
,
1620 const char *name
, int namelen
,
1622 const struct interpret_branch_name_options
*options
)
1629 namelen
= strlen(name
);
1631 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1632 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1634 return len
; /* syntax Ok, not enough switches */
1635 } else if (len
> 0) {
1637 return len
; /* consumed all */
1639 return reinterpret(r
, name
, namelen
, len
, buf
,
1645 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1648 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1649 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1651 return reinterpret(r
, name
, namelen
, len
, buf
,
1655 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1656 upstream_mark
, branch_get_upstream
,
1661 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1662 push_mark
, branch_get_push
,
1671 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1673 int len
= strlen(name
);
1674 struct interpret_branch_name_options options
= {
1677 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1682 strbuf_add(sb
, name
+ used
, len
- used
);
1685 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1687 if (startup_info
->have_repository
)
1688 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1690 strbuf_addstr(sb
, name
);
1693 * This splice must be done even if we end up rejecting the
1694 * name; builtin/branch.c::copy_or_rename_branch() still wants
1695 * to see what the name expanded to so that "branch -m" can be
1696 * used as a tool to correct earlier mistakes.
1698 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1701 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1704 return check_refname_format(sb
->buf
, 0);
1708 * This is like "get_oid_basic()", except it allows "object ID expressions",
1709 * notably "xyz^" for "parent of xyz"
1711 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1713 struct object_context unused
;
1714 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1718 * This returns a non-zero value if the string (built using printf
1719 * format and the given arguments) is not a valid object.
1721 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1725 struct strbuf sb
= STRBUF_INIT
;
1728 strbuf_vaddf(&sb
, fmt
, ap
);
1731 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1732 strbuf_release(&sb
);
1738 * Many callers know that the user meant to name a commit-ish by
1739 * syntactical positions where the object name appears. Calling this
1740 * function allows the machinery to disambiguate shorter-than-unique
1741 * abbreviated object names between commit-ish and others.
1743 * Note that this does NOT error out when the named object is not a
1744 * commit-ish. It is merely to give a hint to the disambiguation
1747 int repo_get_oid_committish(struct repository
*r
,
1749 struct object_id
*oid
)
1751 struct object_context unused
;
1752 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1756 int repo_get_oid_treeish(struct repository
*r
,
1758 struct object_id
*oid
)
1760 struct object_context unused
;
1761 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1765 int repo_get_oid_commit(struct repository
*r
,
1767 struct object_id
*oid
)
1769 struct object_context unused
;
1770 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1774 int repo_get_oid_tree(struct repository
*r
,
1776 struct object_id
*oid
)
1778 struct object_context unused
;
1779 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1783 int repo_get_oid_blob(struct repository
*r
,
1785 struct object_id
*oid
)
1787 struct object_context unused
;
1788 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1792 /* Must be called only when object_name:filename doesn't exist. */
1793 static void diagnose_invalid_oid_path(struct repository
*r
,
1795 const char *filename
,
1796 const struct object_id
*tree_oid
,
1797 const char *object_name
,
1798 int object_name_len
)
1800 struct object_id oid
;
1801 unsigned short mode
;
1806 if (file_exists(filename
))
1807 die(_("path '%s' exists on disk, but not in '%.*s'"),
1808 filename
, object_name_len
, object_name
);
1809 if (is_missing_file_error(errno
)) {
1810 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1812 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1813 die(_("path '%s' exists, but not '%s'\n"
1814 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1817 object_name_len
, object_name
,
1819 object_name_len
, object_name
,
1822 die(_("path '%s' does not exist in '%.*s'"),
1823 filename
, object_name_len
, object_name
);
1827 /* Must be called only when :stage:filename doesn't exist. */
1828 static void diagnose_invalid_index_path(struct repository
*r
,
1831 const char *filename
)
1833 struct index_state
*istate
= r
->index
;
1834 const struct cache_entry
*ce
;
1836 unsigned namelen
= strlen(filename
);
1837 struct strbuf fullname
= STRBUF_INIT
;
1842 /* Wrong stage number? */
1843 pos
= index_name_pos(istate
, filename
, namelen
);
1846 if (pos
< istate
->cache_nr
) {
1847 ce
= istate
->cache
[pos
];
1848 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1849 ce_namelen(ce
) == namelen
&&
1850 !memcmp(ce
->name
, filename
, namelen
))
1851 die(_("path '%s' is in the index, but not at stage %d\n"
1852 "hint: Did you mean ':%d:%s'?"),
1854 ce_stage(ce
), filename
);
1857 /* Confusion between relative and absolute filenames? */
1858 strbuf_addstr(&fullname
, prefix
);
1859 strbuf_addstr(&fullname
, filename
);
1860 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1863 if (pos
< istate
->cache_nr
) {
1864 ce
= istate
->cache
[pos
];
1865 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1866 ce_namelen(ce
) == fullname
.len
&&
1867 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1868 die(_("path '%s' is in the index, but not '%s'\n"
1869 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1870 fullname
.buf
, filename
,
1871 ce_stage(ce
), fullname
.buf
,
1872 ce_stage(ce
), filename
);
1875 if (repo_file_exists(r
, filename
))
1876 die(_("path '%s' exists on disk, but not in the index"), filename
);
1877 if (is_missing_file_error(errno
))
1878 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1881 strbuf_release(&fullname
);
1885 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1887 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1890 if (r
!= the_repository
|| !is_inside_work_tree())
1891 die(_("relative path syntax can't be used outside working tree"));
1893 /* die() inside prefix_path() if resolved path is outside worktree */
1894 return prefix_path(startup_info
->prefix
,
1895 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1899 static int reject_tree_in_index(struct repository
*repo
,
1901 const struct cache_entry
*ce
,
1906 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1909 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1913 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1917 struct object_id
*oid
,
1918 struct object_context
*oc
)
1920 int ret
, bracket_depth
;
1921 int namelen
= strlen(name
);
1923 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1925 memset(oc
, 0, sizeof(*oc
));
1926 oc
->mode
= S_IFINVALID
;
1927 strbuf_init(&oc
->symlink_path
, 0);
1928 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1929 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1930 die(_("<object>:<path> required, only <object> '%s' given"),
1935 * tree:path --> object name of path in tree
1936 * :path -> object name of absolute path in index
1937 * :./path -> object name of path relative to cwd in index
1938 * :[0-3]:path -> object name of path in index at stage
1939 * :/foo -> recent commit matching foo
1941 if (name
[0] == ':') {
1943 const struct cache_entry
*ce
;
1944 char *new_path
= NULL
;
1946 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1947 struct handle_one_ref_cb cb
;
1948 struct commit_list
*list
= NULL
;
1952 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1953 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1954 commit_list_sort_by_date(&list
);
1955 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1959 name
[1] < '0' || '3' < name
[1])
1962 stage
= name
[1] - '0';
1965 new_path
= resolve_relative_path(repo
, cp
);
1967 namelen
= namelen
- (cp
- name
);
1970 namelen
= strlen(cp
);
1973 if (flags
& GET_OID_RECORD_PATH
)
1974 oc
->path
= xstrdup(cp
);
1976 if (!repo
->index
|| !repo
->index
->cache
)
1977 repo_read_index(repo
);
1978 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1981 while (pos
< repo
->index
->cache_nr
) {
1982 ce
= repo
->index
->cache
[pos
];
1983 if (ce_namelen(ce
) != namelen
||
1984 memcmp(ce
->name
, cp
, namelen
))
1986 if (ce_stage(ce
) == stage
) {
1988 if (reject_tree_in_index(repo
, only_to_die
, ce
,
1991 oidcpy(oid
, &ce
->oid
);
1992 oc
->mode
= ce
->ce_mode
;
1997 if (only_to_die
&& name
[1] && name
[1] != '/')
1998 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2002 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2005 else if (bracket_depth
&& *cp
== '}')
2007 else if (!bracket_depth
&& *cp
== ':')
2011 struct object_id tree_oid
;
2012 int len
= cp
- name
;
2013 unsigned sub_flags
= flags
;
2015 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2016 sub_flags
|= GET_OID_TREEISH
;
2018 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2019 const char *filename
= cp
+1;
2020 char *new_filename
= NULL
;
2022 new_filename
= resolve_relative_path(repo
, filename
);
2024 filename
= new_filename
;
2025 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2026 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2027 filename
, oid
, &oc
->symlink_path
,
2030 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2032 if (ret
&& only_to_die
) {
2033 diagnose_invalid_oid_path(repo
, prefix
,
2039 if (flags
& GET_OID_RECORD_PATH
)
2040 oc
->path
= xstrdup(filename
);
2046 die(_("invalid object name '%.*s'."), len
, name
);
2053 * Call this function when you know "name" given by the end user must
2054 * name an object but it doesn't; the function _may_ die with a better
2055 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2056 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2057 * you have a chance to diagnose the error further.
2059 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2063 struct object_context oc
;
2064 struct object_id oid
;
2065 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2069 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2072 struct object_id
*oid
,
2073 struct object_context
*oc
)
2075 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2076 BUG("incompatible flags for get_oid_with_context");
2077 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);