11 #include "oid-array.h"
13 #include "object-store.h"
14 #include "repository.h"
15 #include "submodule.h"
17 #include "commit-reach.h"
20 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
22 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
24 struct disambiguate_state
{
25 int len
; /* length of prefix in hex chars */
26 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
27 struct object_id bin_pfx
;
29 struct repository
*repo
;
30 disambiguate_hint_fn fn
;
32 struct object_id candidate
;
33 unsigned candidate_exists
:1;
34 unsigned candidate_checked
:1;
35 unsigned candidate_ok
:1;
36 unsigned disambiguate_fn_used
:1;
38 unsigned always_call_fn
:1;
41 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
43 if (ds
->always_call_fn
) {
44 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
47 if (!ds
->candidate_exists
) {
48 /* this is the first candidate */
49 oidcpy(&ds
->candidate
, current
);
50 ds
->candidate_exists
= 1;
52 } else if (oideq(&ds
->candidate
, current
)) {
53 /* the same as what we already have seen */
58 /* cannot disambiguate between ds->candidate and current */
63 if (!ds
->candidate_checked
) {
64 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
65 ds
->disambiguate_fn_used
= 1;
66 ds
->candidate_checked
= 1;
69 if (!ds
->candidate_ok
) {
70 /* discard the candidate; we know it does not satisfy fn */
71 oidcpy(&ds
->candidate
, current
);
72 ds
->candidate_checked
= 0;
76 /* if we reach this point, we know ds->candidate satisfies fn */
77 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
79 * if both current and candidate satisfy fn, we cannot
86 /* otherwise, current can be discarded and candidate is still good */
89 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
91 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
93 struct disambiguate_state
*ds
= arg
;
94 /* no need to call match_hash, oidtree_each did prefix match */
95 update_candidates(ds
, oid
);
96 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
99 static void find_short_object_filename(struct disambiguate_state
*ds
)
101 struct object_directory
*odb
;
103 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
104 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
105 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
108 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
118 if ((*a
^ *b
) & 0xf0)
123 static void unique_in_midx(struct multi_pack_index
*m
,
124 struct disambiguate_state
*ds
)
126 uint32_t num
, i
, first
= 0;
127 const struct object_id
*current
= NULL
;
128 num
= m
->num_objects
;
133 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
136 * At this point, "first" is the location of the lowest object
137 * with an object name that could match "bin_pfx". See if we have
138 * 0, 1 or more objects that actually match(es).
140 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
141 struct object_id oid
;
142 current
= nth_midxed_object_oid(&oid
, m
, i
);
143 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
145 update_candidates(ds
, current
);
149 static void unique_in_pack(struct packed_git
*p
,
150 struct disambiguate_state
*ds
)
152 uint32_t num
, i
, first
= 0;
154 if (p
->multi_pack_index
)
157 if (open_pack_index(p
) || !p
->num_objects
)
160 num
= p
->num_objects
;
161 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
164 * At this point, "first" is the location of the lowest object
165 * with an object name that could match "bin_pfx". See if we have
166 * 0, 1 or more objects that actually match(es).
168 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
169 struct object_id oid
;
170 nth_packed_object_id(&oid
, p
, i
);
171 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, oid
.hash
))
173 update_candidates(ds
, &oid
);
177 static void find_short_packed_object(struct disambiguate_state
*ds
)
179 struct multi_pack_index
*m
;
180 struct packed_git
*p
;
182 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
184 unique_in_midx(m
, ds
);
185 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
187 unique_in_pack(p
, ds
);
190 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
191 struct object_id
*oid
)
194 return SHORT_NAME_AMBIGUOUS
;
196 if (!ds
->candidate_exists
)
197 return MISSING_OBJECT
;
199 if (!ds
->candidate_checked
)
201 * If this is the only candidate, there is no point
202 * calling the disambiguation hint callback.
204 * On the other hand, if the current candidate
205 * replaced an earlier candidate that did _not_ pass
206 * the disambiguation hint callback, then we do have
207 * more than one objects that match the short name
208 * given, so we should make sure this one matches;
209 * otherwise, if we discovered this one and the one
210 * that we previously discarded in the reverse order,
211 * we would end up showing different results in the
214 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
215 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
217 if (!ds
->candidate_ok
)
218 return SHORT_NAME_AMBIGUOUS
;
220 oidcpy(oid
, &ds
->candidate
);
224 static int disambiguate_commit_only(struct repository
*r
,
225 const struct object_id
*oid
,
226 void *cb_data_unused
)
228 int kind
= oid_object_info(r
, oid
, NULL
);
229 return kind
== OBJ_COMMIT
;
232 static int disambiguate_committish_only(struct repository
*r
,
233 const struct object_id
*oid
,
234 void *cb_data_unused
)
239 kind
= oid_object_info(r
, oid
, NULL
);
240 if (kind
== OBJ_COMMIT
)
245 /* We need to do this the hard way... */
246 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
247 if (obj
&& obj
->type
== OBJ_COMMIT
)
252 static int disambiguate_tree_only(struct repository
*r
,
253 const struct object_id
*oid
,
254 void *cb_data_unused
)
256 int kind
= oid_object_info(r
, oid
, NULL
);
257 return kind
== OBJ_TREE
;
260 static int disambiguate_treeish_only(struct repository
*r
,
261 const struct object_id
*oid
,
262 void *cb_data_unused
)
267 kind
= oid_object_info(r
, oid
, NULL
);
268 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
273 /* We need to do this the hard way... */
274 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
275 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
280 static int disambiguate_blob_only(struct repository
*r
,
281 const struct object_id
*oid
,
282 void *cb_data_unused
)
284 int kind
= oid_object_info(r
, oid
, NULL
);
285 return kind
== OBJ_BLOB
;
288 static disambiguate_hint_fn default_disambiguate_hint
;
290 int set_disambiguate_hint_config(const char *var
, const char *value
)
292 static const struct {
294 disambiguate_hint_fn fn
;
297 { "commit", disambiguate_commit_only
},
298 { "committish", disambiguate_committish_only
},
299 { "tree", disambiguate_tree_only
},
300 { "treeish", disambiguate_treeish_only
},
301 { "blob", disambiguate_blob_only
}
306 return config_error_nonbool(var
);
308 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
309 if (!strcasecmp(value
, hints
[i
].name
)) {
310 default_disambiguate_hint
= hints
[i
].fn
;
315 return error("unknown hint type for '%s': %s", var
, value
);
318 static int init_object_disambiguation(struct repository
*r
,
319 const char *name
, int len
,
320 struct disambiguate_state
*ds
)
324 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
327 memset(ds
, 0, sizeof(*ds
));
329 for (i
= 0; i
< len
;i
++) {
330 unsigned char c
= name
[i
];
332 if (c
>= '0' && c
<= '9')
334 else if (c
>= 'a' && c
<= 'f')
336 else if (c
>= 'A' && c
<='F') {
345 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
349 ds
->hex_pfx
[len
] = '\0';
355 struct ambiguous_output
{
356 const struct disambiguate_state
*ds
;
357 struct strbuf advice
;
361 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
363 struct ambiguous_output
*state
= data
;
364 const struct disambiguate_state
*ds
= state
->ds
;
365 struct strbuf
*advice
= &state
->advice
;
366 struct strbuf
*sb
= &state
->sb
;
370 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
373 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
374 type
= oid_object_info(ds
->repo
, oid
, NULL
);
378 * TRANSLATORS: This is a line of ambiguous object
379 * output shown when we cannot look up or parse the
380 * object in question. E.g. "deadbeef [bad object]".
382 strbuf_addf(sb
, _("%s [bad object]"), hash
);
386 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
387 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
389 if (type
== OBJ_COMMIT
) {
390 struct strbuf date
= STRBUF_INIT
;
391 struct strbuf msg
= STRBUF_INIT
;
392 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
395 struct pretty_print_context pp
= {0};
396 pp
.date_mode
.type
= DATE_SHORT
;
397 format_commit_message(commit
, "%ad", &date
, &pp
);
398 format_commit_message(commit
, "%s", &msg
, &pp
);
402 * TRANSLATORS: This is a line of ambiguous commit
403 * object output. E.g.:
405 * "deadbeef commit 2021-01-01 - Some Commit Message"
407 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
410 strbuf_release(&date
);
411 strbuf_release(&msg
);
412 } else if (type
== OBJ_TAG
) {
413 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
415 if (!parse_tag(tag
) && tag
->tag
) {
417 * TRANSLATORS: This is a line of ambiguous
418 * tag object output. E.g.:
420 * "deadbeef tag 2022-01-01 - Some Tag Message"
422 * The second argument is the YYYY-MM-DD found
425 * The third argument is the "tag" string
428 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
429 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
433 * TRANSLATORS: This is a line of ambiguous
434 * tag object output where we couldn't parse
435 * the tag itself. E.g.:
437 * "deadbeef [bad tag, could not parse it]"
439 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
442 } else if (type
== OBJ_TREE
) {
444 * TRANSLATORS: This is a line of ambiguous <type>
445 * object output. E.g. "deadbeef tree".
447 strbuf_addf(sb
, _("%s tree"), hash
);
448 } else if (type
== OBJ_BLOB
) {
450 * TRANSLATORS: This is a line of ambiguous <type>
451 * object output. E.g. "deadbeef blob".
453 strbuf_addf(sb
, _("%s blob"), hash
);
459 * TRANSLATORS: This is line item of ambiguous object output
460 * from describe_ambiguous_object() above. For RTL languages
461 * you'll probably want to swap the "%s" and leading " " space
464 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
470 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
472 oid_array_append(data
, oid
);
476 static int repo_collect_ambiguous(struct repository
*r
,
477 const struct object_id
*oid
,
480 return collect_ambiguous(oid
, data
);
483 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
485 struct repository
*sort_ambiguous_repo
= ctx
;
486 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
487 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
492 * Sorts by hash within the same object type, just as
493 * oid_array_for_each_unique() would do.
495 if (a_type
== b_type
)
499 * Between object types show tags, then commits, and finally
502 * The object_type enum is commit, tree, blob, tag, but we
503 * want tag, commit, tree blob. Cleverly (perhaps too
504 * cleverly) do that with modulus, since the enum assigns 1 to
505 * commit, so tag becomes 0.
507 a_type_sort
= a_type
% 4;
508 b_type_sort
= b_type
% 4;
509 return a_type_sort
> b_type_sort
? 1 : -1;
512 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
514 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
517 static enum get_oid_result
get_short_oid(struct repository
*r
,
518 const char *name
, int len
,
519 struct object_id
*oid
,
523 struct disambiguate_state ds
;
524 int quietly
= !!(flags
& GET_OID_QUIETLY
);
526 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
529 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
530 BUG("multiple get_short_oid disambiguator flags");
532 if (flags
& GET_OID_COMMIT
)
533 ds
.fn
= disambiguate_commit_only
;
534 else if (flags
& GET_OID_COMMITTISH
)
535 ds
.fn
= disambiguate_committish_only
;
536 else if (flags
& GET_OID_TREE
)
537 ds
.fn
= disambiguate_tree_only
;
538 else if (flags
& GET_OID_TREEISH
)
539 ds
.fn
= disambiguate_treeish_only
;
540 else if (flags
& GET_OID_BLOB
)
541 ds
.fn
= disambiguate_blob_only
;
543 ds
.fn
= default_disambiguate_hint
;
545 find_short_object_filename(&ds
);
546 find_short_packed_object(&ds
);
547 status
= finish_object_disambiguation(&ds
, oid
);
550 * If we didn't find it, do the usual reprepare() slow-path,
551 * since the object may have recently been added to the repository
552 * or migrated from loose to packed.
554 if (status
== MISSING_OBJECT
) {
555 reprepare_packed_git(r
);
556 find_short_object_filename(&ds
);
557 find_short_packed_object(&ds
);
558 status
= finish_object_disambiguation(&ds
, oid
);
561 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
562 struct oid_array collect
= OID_ARRAY_INIT
;
563 struct ambiguous_output out
= {
566 .advice
= STRBUF_INIT
,
569 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
572 * We may still have ambiguity if we simply saw a series of
573 * candidates that did not satisfy our hint function. In
574 * that case, we still want to show them, so disable the hint
580 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
581 sort_ambiguous_oid_array(r
, &collect
);
583 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
584 BUG("show_ambiguous_object shouldn't return non-zero");
587 * TRANSLATORS: The argument is the list of ambiguous
588 * objects composed in show_ambiguous_object(). See
589 * its "TRANSLATORS" comments for details.
591 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
593 oid_array_clear(&collect
);
594 strbuf_release(&out
.advice
);
595 strbuf_release(&out
.sb
);
601 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
602 each_abbrev_fn fn
, void *cb_data
)
604 struct oid_array collect
= OID_ARRAY_INIT
;
605 struct disambiguate_state ds
;
608 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
611 ds
.always_call_fn
= 1;
612 ds
.fn
= repo_collect_ambiguous
;
613 ds
.cb_data
= &collect
;
614 find_short_object_filename(&ds
);
615 find_short_packed_object(&ds
);
617 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
618 oid_array_clear(&collect
);
623 * Return the slot of the most-significant bit set in "val". There are various
624 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
625 * probably not a big deal here.
627 static unsigned msb(unsigned long val
)
635 struct min_abbrev_data
{
636 unsigned int init_len
;
637 unsigned int cur_len
;
639 struct repository
*repo
;
640 const struct object_id
*oid
;
643 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
646 static const char hex
[] = "0123456789abcdef";
649 return hex
[oid
->hash
[pos
>> 1] >> 4];
651 return hex
[oid
->hash
[pos
>> 1] & 0xf];
654 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
656 struct min_abbrev_data
*mad
= cb_data
;
658 unsigned int i
= mad
->init_len
;
659 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
662 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
663 mad
->cur_len
= i
+ 1;
668 static int repo_extend_abbrev_len(struct repository
*r
,
669 const struct object_id
*oid
,
672 return extend_abbrev_len(oid
, cb_data
);
675 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
676 struct min_abbrev_data
*mad
)
679 uint32_t num
, first
= 0;
680 struct object_id oid
;
681 const struct object_id
*mad_oid
;
686 num
= m
->num_objects
;
688 match
= bsearch_midx(mad_oid
, m
, &first
);
691 * first is now the position in the packfile where we would insert
692 * mad->hash if it does not exist (or the position of mad->hash if
693 * it does exist). Hence, we consider a maximum of two objects
694 * nearby for the abbreviation length.
698 if (nth_midxed_object_oid(&oid
, m
, first
))
699 extend_abbrev_len(&oid
, mad
);
700 } else if (first
< num
- 1) {
701 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
702 extend_abbrev_len(&oid
, mad
);
705 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
706 extend_abbrev_len(&oid
, mad
);
708 mad
->init_len
= mad
->cur_len
;
711 static void find_abbrev_len_for_pack(struct packed_git
*p
,
712 struct min_abbrev_data
*mad
)
715 uint32_t num
, first
= 0;
716 struct object_id oid
;
717 const struct object_id
*mad_oid
;
719 if (p
->multi_pack_index
)
722 if (open_pack_index(p
) || !p
->num_objects
)
725 num
= p
->num_objects
;
727 match
= bsearch_pack(mad_oid
, p
, &first
);
730 * first is now the position in the packfile where we would insert
731 * mad->hash if it does not exist (or the position of mad->hash if
732 * it does exist). Hence, we consider a maximum of two objects
733 * nearby for the abbreviation length.
737 if (!nth_packed_object_id(&oid
, p
, first
))
738 extend_abbrev_len(&oid
, mad
);
739 } else if (first
< num
- 1) {
740 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
741 extend_abbrev_len(&oid
, mad
);
744 if (!nth_packed_object_id(&oid
, p
, first
- 1))
745 extend_abbrev_len(&oid
, mad
);
747 mad
->init_len
= mad
->cur_len
;
750 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
752 struct multi_pack_index
*m
;
753 struct packed_git
*p
;
755 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
756 find_abbrev_len_for_midx(m
, mad
);
757 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
758 find_abbrev_len_for_pack(p
, mad
);
761 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
762 const struct object_id
*oid
, int len
)
764 struct disambiguate_state ds
;
765 struct min_abbrev_data mad
;
766 struct object_id oid_ret
;
767 const unsigned hexsz
= r
->hash_algo
->hexsz
;
770 unsigned long count
= repo_approximate_object_count(r
);
772 * Add one because the MSB only tells us the highest bit set,
773 * not including the value of all the _other_ bits (so "15"
774 * is only one off of 2^4, but the MSB is the 3rd bit.
776 len
= msb(count
) + 1;
778 * We now know we have on the order of 2^len objects, which
779 * expects a collision at 2^(len/2). But we also care about hex
780 * chars, not bits, and there are 4 bits per hex. So all
781 * together we need to divide by 2 and round up.
783 len
= DIV_ROUND_UP(len
, 2);
785 * For very small repos, we stick with our regular fallback.
787 if (len
< FALLBACK_DEFAULT_ABBREV
)
788 len
= FALLBACK_DEFAULT_ABBREV
;
791 oid_to_hex_r(hex
, oid
);
792 if (len
== hexsz
|| !len
)
801 find_abbrev_len_packed(&mad
);
803 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
806 ds
.fn
= repo_extend_abbrev_len
;
807 ds
.always_call_fn
= 1;
808 ds
.cb_data
= (void *)&mad
;
810 find_short_object_filename(&ds
);
811 (void)finish_object_disambiguation(&ds
, &oid_ret
);
813 hex
[mad
.cur_len
] = 0;
817 const char *repo_find_unique_abbrev(struct repository
*r
,
818 const struct object_id
*oid
,
822 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
823 char *hex
= hexbuffer
[bufno
];
824 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
825 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
829 static int ambiguous_path(const char *path
, int len
)
834 for (cnt
= 0; cnt
< len
; cnt
++) {
854 static inline int at_mark(const char *string
, int len
,
855 const char **suffix
, int nr
)
859 for (i
= 0; i
< nr
; i
++) {
860 int suffix_len
= strlen(suffix
[i
]);
861 if (suffix_len
<= len
862 && !strncasecmp(string
, suffix
[i
], suffix_len
))
868 static inline int upstream_mark(const char *string
, int len
)
870 const char *suffix
[] = { "@{upstream}", "@{u}" };
871 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
874 static inline int push_mark(const char *string
, int len
)
876 const char *suffix
[] = { "@{push}" };
877 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
880 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
881 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
883 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
884 struct object_id
*oid
, unsigned int flags
)
886 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
887 static const char *object_name_msg
= N_(
888 "Git normally never creates a ref that ends with 40 hex characters\n"
889 "because it will be ignored when you just specify 40-hex. These refs\n"
890 "may be created by mistake. For example,\n"
892 " git switch -c $br $(git rev-parse ...)\n"
894 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
895 "examine these refs and maybe delete them. Turn this message off by\n"
896 "running \"git config advice.objectNameWarning false\"");
897 struct object_id tmp_oid
;
898 char *real_ref
= NULL
;
900 int at
, reflog_len
, nth_prior
= 0;
902 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
903 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
904 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
905 if (refs_found
> 0) {
906 warning(warn_msg
, len
, str
);
907 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
908 fprintf(stderr
, "%s\n", _(object_name_msg
));
915 /* basic@{time or number or -number} format to query ref-log */
917 if (len
&& str
[len
-1] == '}') {
918 for (at
= len
-4; at
>= 0; at
--) {
919 if (str
[at
] == '@' && str
[at
+1] == '{') {
920 if (str
[at
+2] == '-') {
922 /* @{-N} not at start */
927 if (!upstream_mark(str
+ at
, len
- at
) &&
928 !push_mark(str
+ at
, len
- at
)) {
929 reflog_len
= (len
-1) - (at
+2);
937 /* Accept only unambiguous ref paths. */
938 if (len
&& ambiguous_path(str
, len
))
942 struct strbuf buf
= STRBUF_INIT
;
945 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
946 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
947 strbuf_release(&buf
);
953 if (!len
&& reflog_len
)
954 /* allow "@{...}" to mean the current branch reflog */
955 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, 0);
957 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
959 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, 0);
964 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
966 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
967 warning(warn_msg
, len
, str
);
975 /* Is it asking for N-th entry, or approxidate? */
976 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
977 char ch
= str
[at
+2+i
];
978 if ('0' <= ch
&& ch
<= '9')
979 nth
= nth
* 10 + ch
- '0';
983 if (100000000 <= nth
) {
990 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
991 at_time
= approxidate_careful(tmp
, &errors
);
998 if (read_ref_at(get_main_ref_store(r
),
999 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1000 &co_time
, &co_tz
, &co_cnt
)) {
1002 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1007 if (!(flags
& GET_OID_QUIETLY
)) {
1008 warning(_("log for '%.*s' only goes back to %s"),
1010 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1013 if (flags
& GET_OID_QUIETLY
) {
1016 die(_("log for '%.*s' only has %d entries"),
1026 static enum get_oid_result
get_parent(struct repository
*r
,
1027 const char *name
, int len
,
1028 struct object_id
*result
, int idx
)
1030 struct object_id oid
;
1031 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1032 GET_OID_COMMITTISH
);
1033 struct commit
*commit
;
1034 struct commit_list
*p
;
1038 commit
= lookup_commit_reference(r
, &oid
);
1039 if (parse_commit(commit
))
1040 return MISSING_OBJECT
;
1042 oidcpy(result
, &commit
->object
.oid
);
1045 p
= commit
->parents
;
1048 oidcpy(result
, &p
->item
->object
.oid
);
1053 return MISSING_OBJECT
;
1056 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1057 const char *name
, int len
,
1058 struct object_id
*result
,
1061 struct object_id oid
;
1062 struct commit
*commit
;
1065 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1068 commit
= lookup_commit_reference(r
, &oid
);
1070 return MISSING_OBJECT
;
1072 while (generation
--) {
1073 if (parse_commit(commit
) || !commit
->parents
)
1074 return MISSING_OBJECT
;
1075 commit
= commit
->parents
->item
;
1077 oidcpy(result
, &commit
->object
.oid
);
1081 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1082 struct object
*o
, enum object_type expected_type
)
1084 if (name
&& !namelen
)
1085 namelen
= strlen(name
);
1087 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1089 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1091 if (o
->type
== OBJ_TAG
)
1092 o
= ((struct tag
*) o
)->tagged
;
1093 else if (o
->type
== OBJ_COMMIT
)
1094 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1097 error("%.*s: expected %s type, but the object "
1098 "dereferences to %s type",
1099 namelen
, name
, type_name(expected_type
),
1100 type_name(o
->type
));
1106 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1107 struct object_id
*oid
, unsigned lookup_flags
)
1109 struct object_id outer
;
1111 unsigned int expected_type
= 0;
1115 * "ref^{type}" dereferences ref repeatedly until you cannot
1116 * dereference anymore, or you get an object of given type,
1117 * whichever comes first. "ref^{}" means just dereference
1118 * tags until you get a non-tag. "ref^0" is a shorthand for
1119 * "ref^{commit}". "commit^{tree}" could be used to find the
1120 * top-level tree of the given commit.
1122 if (len
< 4 || name
[len
-1] != '}')
1125 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1127 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1133 sp
++; /* beginning of type name, or closing brace for empty */
1134 if (starts_with(sp
, "commit}"))
1135 expected_type
= OBJ_COMMIT
;
1136 else if (starts_with(sp
, "tag}"))
1137 expected_type
= OBJ_TAG
;
1138 else if (starts_with(sp
, "tree}"))
1139 expected_type
= OBJ_TREE
;
1140 else if (starts_with(sp
, "blob}"))
1141 expected_type
= OBJ_BLOB
;
1142 else if (starts_with(sp
, "object}"))
1143 expected_type
= OBJ_ANY
;
1144 else if (sp
[0] == '}')
1145 expected_type
= OBJ_NONE
;
1146 else if (sp
[0] == '/')
1147 expected_type
= OBJ_COMMIT
;
1151 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1152 if (expected_type
== OBJ_COMMIT
)
1153 lookup_flags
|= GET_OID_COMMITTISH
;
1154 else if (expected_type
== OBJ_TREE
)
1155 lookup_flags
|= GET_OID_TREEISH
;
1157 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1160 o
= parse_object(r
, &outer
);
1163 if (!expected_type
) {
1164 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1165 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1167 oidcpy(oid
, &o
->oid
);
1172 * At this point, the syntax look correct, so
1173 * if we do not get the needed object, we should
1176 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1180 oidcpy(oid
, &o
->oid
);
1182 /* "$commit^{/foo}" */
1185 struct commit_list
*list
= NULL
;
1188 * $commit^{/}. Some regex implementation may reject.
1189 * We don't need regex anyway. '' pattern always matches.
1194 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1195 commit_list_insert((struct commit
*)o
, &list
);
1196 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1203 static int get_describe_name(struct repository
*r
,
1204 const char *name
, int len
,
1205 struct object_id
*oid
)
1208 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1210 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1212 if (!isxdigit(ch
)) {
1213 /* We must be looking at g in "SOMETHING-g"
1214 * for it to be describe output.
1216 if (ch
== 'g' && cp
[-1] == '-') {
1219 return get_short_oid(r
,
1220 cp
, len
, oid
, flags
);
1227 static enum get_oid_result
get_oid_1(struct repository
*r
,
1228 const char *name
, int len
,
1229 struct object_id
*oid
,
1230 unsigned lookup_flags
)
1232 int ret
, has_suffix
;
1236 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1239 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1241 if ('0' <= ch
&& ch
<= '9')
1243 if (ch
== '~' || ch
== '^')
1249 unsigned int num
= 0;
1250 int len1
= cp
- name
;
1252 while (cp
< name
+ len
) {
1253 unsigned int digit
= *cp
++ - '0';
1254 if (unsigned_mult_overflows(num
, 10))
1255 return MISSING_OBJECT
;
1257 if (unsigned_add_overflows(num
, digit
))
1258 return MISSING_OBJECT
;
1261 if (!num
&& len1
== len
- 1)
1263 else if (num
> INT_MAX
)
1264 return MISSING_OBJECT
;
1265 if (has_suffix
== '^')
1266 return get_parent(r
, name
, len1
, oid
, num
);
1267 /* else if (has_suffix == '~') -- goes without saying */
1268 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1271 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1275 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1279 /* It could be describe output that is "SOMETHING-gXXXX" */
1280 ret
= get_describe_name(r
, name
, len
, oid
);
1284 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1288 * This interprets names like ':/Initial revision of "git"' by searching
1289 * through history and returning the first commit whose message starts
1290 * the given regular expression.
1292 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1294 * For a literal '!' character at the beginning of a pattern, you have to repeat
1295 * that, like: ':/!!foo'
1297 * For future extension, all other sequences beginning with ':/!' are reserved.
1300 /* Remember to update object flag allocation in object.h */
1301 #define ONELINE_SEEN (1u<<20)
1303 struct handle_one_ref_cb
{
1304 struct repository
*repo
;
1305 struct commit_list
**list
;
1308 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1309 int flag
, void *cb_data
)
1311 struct handle_one_ref_cb
*cb
= cb_data
;
1312 struct commit_list
**list
= cb
->list
;
1313 struct object
*object
= parse_object(cb
->repo
, oid
);
1316 if (object
->type
== OBJ_TAG
) {
1317 object
= deref_tag(cb
->repo
, object
, path
,
1322 if (object
->type
!= OBJ_COMMIT
)
1324 commit_list_insert((struct commit
*)object
, list
);
1328 static int get_oid_oneline(struct repository
*r
,
1329 const char *prefix
, struct object_id
*oid
,
1330 struct commit_list
*list
)
1332 struct commit_list
*backup
= NULL
, *l
;
1337 if (prefix
[0] == '!') {
1340 if (prefix
[0] == '-') {
1343 } else if (prefix
[0] != '!') {
1348 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1351 for (l
= list
; l
; l
= l
->next
) {
1352 l
->item
->object
.flags
|= ONELINE_SEEN
;
1353 commit_list_insert(l
->item
, &backup
);
1356 const char *p
, *buf
;
1357 struct commit
*commit
;
1360 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1361 if (!parse_object(r
, &commit
->object
.oid
))
1363 buf
= get_commit_buffer(commit
, NULL
);
1364 p
= strstr(buf
, "\n\n");
1365 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1366 unuse_commit_buffer(commit
, buf
);
1369 oidcpy(oid
, &commit
->object
.oid
);
1375 free_commit_list(list
);
1376 for (l
= backup
; l
; l
= l
->next
)
1377 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1378 free_commit_list(backup
);
1379 return found
? 0 : -1;
1382 struct grab_nth_branch_switch_cbdata
{
1387 static int grab_nth_branch_switch(struct object_id
*ooid
, struct object_id
*noid
,
1388 const char *email
, timestamp_t timestamp
, int tz
,
1389 const char *message
, void *cb_data
)
1391 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1392 const char *match
= NULL
, *target
= NULL
;
1395 if (skip_prefix(message
, "checkout: moving from ", &match
))
1396 target
= strstr(match
, " to ");
1398 if (!match
|| !target
)
1400 if (--(cb
->remaining
) == 0) {
1401 len
= target
- match
;
1402 strbuf_reset(cb
->sb
);
1403 strbuf_add(cb
->sb
, match
, len
);
1404 return 1; /* we are done */
1410 * Parse @{-N} syntax, return the number of characters parsed
1411 * if successful; otherwise signal an error with negative value.
1413 static int interpret_nth_prior_checkout(struct repository
*r
,
1414 const char *name
, int namelen
,
1419 struct grab_nth_branch_switch_cbdata cb
;
1425 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1427 brace
= memchr(name
, '}', namelen
);
1430 nth
= strtol(name
+ 3, &num_end
, 10);
1431 if (num_end
!= brace
)
1438 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1439 "HEAD", grab_nth_branch_switch
, &cb
);
1441 retval
= brace
- name
+ 1;
1448 int repo_get_oid_mb(struct repository
*r
,
1450 struct object_id
*oid
)
1452 struct commit
*one
, *two
;
1453 struct commit_list
*mbs
;
1454 struct object_id oid_tmp
;
1458 dots
= strstr(name
, "...");
1460 return repo_get_oid(r
, name
, oid
);
1462 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1465 strbuf_init(&sb
, dots
- name
);
1466 strbuf_add(&sb
, name
, dots
- name
);
1467 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1468 strbuf_release(&sb
);
1472 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1476 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1478 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1481 mbs
= repo_get_merge_bases(r
, one
, two
);
1482 if (!mbs
|| mbs
->next
)
1486 oidcpy(oid
, &mbs
->item
->object
.oid
);
1488 free_commit_list(mbs
);
1492 /* parse @something syntax, when 'something' is not {.*} */
1493 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1497 if (len
|| name
[1] == '{')
1500 /* make sure it's a single @, or @@{.*}, not @foo */
1501 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1502 if (next
&& next
[1] != '{')
1505 next
= name
+ namelen
;
1506 if (next
!= name
+ 1)
1510 strbuf_add(buf
, "HEAD", 4);
1514 static int reinterpret(struct repository
*r
,
1515 const char *name
, int namelen
, int len
,
1516 struct strbuf
*buf
, unsigned allowed
)
1518 /* we have extra data, which might need further processing */
1519 struct strbuf tmp
= STRBUF_INIT
;
1520 int used
= buf
->len
;
1522 struct interpret_branch_name_options options
= {
1526 strbuf_add(buf
, name
+ len
, namelen
- len
);
1527 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1528 /* that data was not interpreted, remove our cruft */
1530 strbuf_setlen(buf
, used
);
1534 strbuf_addbuf(buf
, &tmp
);
1535 strbuf_release(&tmp
);
1536 /* tweak for size of {-N} versus expanded ref name */
1537 return ret
- used
+ len
;
1540 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1542 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1544 strbuf_addstr(buf
, s
);
1548 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1553 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1554 starts_with(refname
, "refs/heads/"))
1556 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1557 starts_with(refname
, "refs/remotes/"))
1563 static int interpret_branch_mark(struct repository
*r
,
1564 const char *name
, int namelen
,
1565 int at
, struct strbuf
*buf
,
1566 int (*get_mark
)(const char *, int),
1567 const char *(*get_data
)(struct branch
*,
1569 const struct interpret_branch_name_options
*options
)
1572 struct branch
*branch
;
1573 struct strbuf err
= STRBUF_INIT
;
1576 len
= get_mark(name
+ at
, namelen
- at
);
1580 if (memchr(name
, ':', at
))
1584 char *name_str
= xmemdupz(name
, at
);
1585 branch
= branch_get(name_str
);
1588 branch
= branch_get(NULL
);
1590 value
= get_data(branch
, &err
);
1592 if (options
->nonfatal_dangling_mark
) {
1593 strbuf_release(&err
);
1600 if (!branch_interpret_allowed(value
, options
->allowed
))
1603 set_shortened_ref(r
, buf
, value
);
1607 int repo_interpret_branch_name(struct repository
*r
,
1608 const char *name
, int namelen
,
1610 const struct interpret_branch_name_options
*options
)
1617 namelen
= strlen(name
);
1619 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1620 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1622 return len
; /* syntax Ok, not enough switches */
1623 } else if (len
> 0) {
1625 return len
; /* consumed all */
1627 return reinterpret(r
, name
, namelen
, len
, buf
,
1633 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1636 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1637 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1639 return reinterpret(r
, name
, namelen
, len
, buf
,
1643 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1644 upstream_mark
, branch_get_upstream
,
1649 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1650 push_mark
, branch_get_push
,
1659 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1661 int len
= strlen(name
);
1662 struct interpret_branch_name_options options
= {
1665 int used
= interpret_branch_name(name
, len
, sb
, &options
);
1669 strbuf_add(sb
, name
+ used
, len
- used
);
1672 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1674 if (startup_info
->have_repository
)
1675 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1677 strbuf_addstr(sb
, name
);
1680 * This splice must be done even if we end up rejecting the
1681 * name; builtin/branch.c::copy_or_rename_branch() still wants
1682 * to see what the name expanded to so that "branch -m" can be
1683 * used as a tool to correct earlier mistakes.
1685 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1688 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1691 return check_refname_format(sb
->buf
, 0);
1695 * This is like "get_oid_basic()", except it allows "object ID expressions",
1696 * notably "xyz^" for "parent of xyz"
1698 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1700 struct object_context unused
;
1701 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1705 * This returns a non-zero value if the string (built using printf
1706 * format and the given arguments) is not a valid object.
1708 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1712 struct strbuf sb
= STRBUF_INIT
;
1715 strbuf_vaddf(&sb
, fmt
, ap
);
1718 ret
= get_oid(sb
.buf
, oid
);
1719 strbuf_release(&sb
);
1725 * Many callers know that the user meant to name a commit-ish by
1726 * syntactical positions where the object name appears. Calling this
1727 * function allows the machinery to disambiguate shorter-than-unique
1728 * abbreviated object names between commit-ish and others.
1730 * Note that this does NOT error out when the named object is not a
1731 * commit-ish. It is merely to give a hint to the disambiguation
1734 int repo_get_oid_committish(struct repository
*r
,
1736 struct object_id
*oid
)
1738 struct object_context unused
;
1739 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1743 int repo_get_oid_treeish(struct repository
*r
,
1745 struct object_id
*oid
)
1747 struct object_context unused
;
1748 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1752 int repo_get_oid_commit(struct repository
*r
,
1754 struct object_id
*oid
)
1756 struct object_context unused
;
1757 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1761 int repo_get_oid_tree(struct repository
*r
,
1763 struct object_id
*oid
)
1765 struct object_context unused
;
1766 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1770 int repo_get_oid_blob(struct repository
*r
,
1772 struct object_id
*oid
)
1774 struct object_context unused
;
1775 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1779 /* Must be called only when object_name:filename doesn't exist. */
1780 static void diagnose_invalid_oid_path(struct repository
*r
,
1782 const char *filename
,
1783 const struct object_id
*tree_oid
,
1784 const char *object_name
,
1785 int object_name_len
)
1787 struct object_id oid
;
1788 unsigned short mode
;
1793 if (file_exists(filename
))
1794 die(_("path '%s' exists on disk, but not in '%.*s'"),
1795 filename
, object_name_len
, object_name
);
1796 if (is_missing_file_error(errno
)) {
1797 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1799 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1800 die(_("path '%s' exists, but not '%s'\n"
1801 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1804 object_name_len
, object_name
,
1806 object_name_len
, object_name
,
1809 die(_("path '%s' does not exist in '%.*s'"),
1810 filename
, object_name_len
, object_name
);
1814 /* Must be called only when :stage:filename doesn't exist. */
1815 static void diagnose_invalid_index_path(struct repository
*r
,
1818 const char *filename
)
1820 struct index_state
*istate
= r
->index
;
1821 const struct cache_entry
*ce
;
1823 unsigned namelen
= strlen(filename
);
1824 struct strbuf fullname
= STRBUF_INIT
;
1829 /* Wrong stage number? */
1830 pos
= index_name_pos(istate
, filename
, namelen
);
1833 if (pos
< istate
->cache_nr
) {
1834 ce
= istate
->cache
[pos
];
1835 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1836 ce_namelen(ce
) == namelen
&&
1837 !memcmp(ce
->name
, filename
, namelen
))
1838 die(_("path '%s' is in the index, but not at stage %d\n"
1839 "hint: Did you mean ':%d:%s'?"),
1841 ce_stage(ce
), filename
);
1844 /* Confusion between relative and absolute filenames? */
1845 strbuf_addstr(&fullname
, prefix
);
1846 strbuf_addstr(&fullname
, filename
);
1847 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1850 if (pos
< istate
->cache_nr
) {
1851 ce
= istate
->cache
[pos
];
1852 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1853 ce_namelen(ce
) == fullname
.len
&&
1854 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1855 die(_("path '%s' is in the index, but not '%s'\n"
1856 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1857 fullname
.buf
, filename
,
1858 ce_stage(ce
), fullname
.buf
,
1859 ce_stage(ce
), filename
);
1862 if (repo_file_exists(r
, filename
))
1863 die(_("path '%s' exists on disk, but not in the index"), filename
);
1864 if (is_missing_file_error(errno
))
1865 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1868 strbuf_release(&fullname
);
1872 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1874 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1877 if (r
!= the_repository
|| !is_inside_work_tree())
1878 die(_("relative path syntax can't be used outside working tree"));
1880 /* die() inside prefix_path() if resolved path is outside worktree */
1881 return prefix_path(startup_info
->prefix
,
1882 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1886 static int reject_tree_in_index(struct repository
*repo
,
1888 const struct cache_entry
*ce
,
1893 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1896 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1900 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1904 struct object_id
*oid
,
1905 struct object_context
*oc
)
1907 int ret
, bracket_depth
;
1908 int namelen
= strlen(name
);
1910 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1912 memset(oc
, 0, sizeof(*oc
));
1913 oc
->mode
= S_IFINVALID
;
1914 strbuf_init(&oc
->symlink_path
, 0);
1915 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1916 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1917 die(_("<object>:<path> required, only <object> '%s' given"),
1922 * tree:path --> object name of path in tree
1923 * :path -> object name of absolute path in index
1924 * :./path -> object name of path relative to cwd in index
1925 * :[0-3]:path -> object name of path in index at stage
1926 * :/foo -> recent commit matching foo
1928 if (name
[0] == ':') {
1930 const struct cache_entry
*ce
;
1931 char *new_path
= NULL
;
1933 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1934 struct handle_one_ref_cb cb
;
1935 struct commit_list
*list
= NULL
;
1939 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1940 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1941 commit_list_sort_by_date(&list
);
1942 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1946 name
[1] < '0' || '3' < name
[1])
1949 stage
= name
[1] - '0';
1952 new_path
= resolve_relative_path(repo
, cp
);
1954 namelen
= namelen
- (cp
- name
);
1957 namelen
= strlen(cp
);
1960 if (flags
& GET_OID_RECORD_PATH
)
1961 oc
->path
= xstrdup(cp
);
1963 if (!repo
->index
|| !repo
->index
->cache
)
1964 repo_read_index(repo
);
1965 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1968 while (pos
< repo
->index
->cache_nr
) {
1969 ce
= repo
->index
->cache
[pos
];
1970 if (ce_namelen(ce
) != namelen
||
1971 memcmp(ce
->name
, cp
, namelen
))
1973 if (ce_stage(ce
) == stage
) {
1975 if (reject_tree_in_index(repo
, only_to_die
, ce
,
1978 oidcpy(oid
, &ce
->oid
);
1979 oc
->mode
= ce
->ce_mode
;
1984 if (only_to_die
&& name
[1] && name
[1] != '/')
1985 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1989 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1992 else if (bracket_depth
&& *cp
== '}')
1994 else if (!bracket_depth
&& *cp
== ':')
1998 struct object_id tree_oid
;
1999 int len
= cp
- name
;
2000 unsigned sub_flags
= flags
;
2002 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2003 sub_flags
|= GET_OID_TREEISH
;
2005 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2006 const char *filename
= cp
+1;
2007 char *new_filename
= NULL
;
2009 new_filename
= resolve_relative_path(repo
, filename
);
2011 filename
= new_filename
;
2012 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2013 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2014 filename
, oid
, &oc
->symlink_path
,
2017 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2019 if (ret
&& only_to_die
) {
2020 diagnose_invalid_oid_path(repo
, prefix
,
2026 if (flags
& GET_OID_RECORD_PATH
)
2027 oc
->path
= xstrdup(filename
);
2033 die(_("invalid object name '%.*s'."), len
, name
);
2040 * Call this function when you know "name" given by the end user must
2041 * name an object but it doesn't; the function _may_ die with a better
2042 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2043 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2044 * you have a chance to diagnose the error further.
2046 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2050 struct object_context oc
;
2051 struct object_id oid
;
2052 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2056 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2059 struct object_id
*oid
,
2060 struct object_context
*oc
)
2062 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2063 BUG("incompatible flags for get_oid_with_context");
2064 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);