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 repo_format_commit_message(the_repository
, commit
,
399 repo_format_commit_message(the_repository
, commit
,
404 * TRANSLATORS: This is a line of ambiguous commit
405 * object output. E.g.:
407 * "deadbeef commit 2021-01-01 - Some Commit Message"
409 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
412 strbuf_release(&date
);
413 strbuf_release(&msg
);
414 } else if (type
== OBJ_TAG
) {
415 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
417 if (!parse_tag(tag
) && tag
->tag
) {
419 * TRANSLATORS: This is a line of ambiguous
420 * tag object output. E.g.:
422 * "deadbeef tag 2022-01-01 - Some Tag Message"
424 * The second argument is the YYYY-MM-DD found
427 * The third argument is the "tag" string
430 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
431 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
435 * TRANSLATORS: This is a line of ambiguous
436 * tag object output where we couldn't parse
437 * the tag itself. E.g.:
439 * "deadbeef [bad tag, could not parse it]"
441 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
444 } else if (type
== OBJ_TREE
) {
446 * TRANSLATORS: This is a line of ambiguous <type>
447 * object output. E.g. "deadbeef tree".
449 strbuf_addf(sb
, _("%s tree"), hash
);
450 } else if (type
== OBJ_BLOB
) {
452 * TRANSLATORS: This is a line of ambiguous <type>
453 * object output. E.g. "deadbeef blob".
455 strbuf_addf(sb
, _("%s blob"), hash
);
461 * TRANSLATORS: This is line item of ambiguous object output
462 * from describe_ambiguous_object() above. For RTL languages
463 * you'll probably want to swap the "%s" and leading " " space
466 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
472 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
474 oid_array_append(data
, oid
);
478 static int repo_collect_ambiguous(struct repository
*r
,
479 const struct object_id
*oid
,
482 return collect_ambiguous(oid
, data
);
485 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
487 struct repository
*sort_ambiguous_repo
= ctx
;
488 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
489 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
494 * Sorts by hash within the same object type, just as
495 * oid_array_for_each_unique() would do.
497 if (a_type
== b_type
)
501 * Between object types show tags, then commits, and finally
504 * The object_type enum is commit, tree, blob, tag, but we
505 * want tag, commit, tree blob. Cleverly (perhaps too
506 * cleverly) do that with modulus, since the enum assigns 1 to
507 * commit, so tag becomes 0.
509 a_type_sort
= a_type
% 4;
510 b_type_sort
= b_type
% 4;
511 return a_type_sort
> b_type_sort
? 1 : -1;
514 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
516 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
519 static enum get_oid_result
get_short_oid(struct repository
*r
,
520 const char *name
, int len
,
521 struct object_id
*oid
,
525 struct disambiguate_state ds
;
526 int quietly
= !!(flags
& GET_OID_QUIETLY
);
528 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
531 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
532 BUG("multiple get_short_oid disambiguator flags");
534 if (flags
& GET_OID_COMMIT
)
535 ds
.fn
= disambiguate_commit_only
;
536 else if (flags
& GET_OID_COMMITTISH
)
537 ds
.fn
= disambiguate_committish_only
;
538 else if (flags
& GET_OID_TREE
)
539 ds
.fn
= disambiguate_tree_only
;
540 else if (flags
& GET_OID_TREEISH
)
541 ds
.fn
= disambiguate_treeish_only
;
542 else if (flags
& GET_OID_BLOB
)
543 ds
.fn
= disambiguate_blob_only
;
545 ds
.fn
= default_disambiguate_hint
;
547 find_short_object_filename(&ds
);
548 find_short_packed_object(&ds
);
549 status
= finish_object_disambiguation(&ds
, oid
);
552 * If we didn't find it, do the usual reprepare() slow-path,
553 * since the object may have recently been added to the repository
554 * or migrated from loose to packed.
556 if (status
== MISSING_OBJECT
) {
557 reprepare_packed_git(r
);
558 find_short_object_filename(&ds
);
559 find_short_packed_object(&ds
);
560 status
= finish_object_disambiguation(&ds
, oid
);
563 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
564 struct oid_array collect
= OID_ARRAY_INIT
;
565 struct ambiguous_output out
= {
568 .advice
= STRBUF_INIT
,
571 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
574 * We may still have ambiguity if we simply saw a series of
575 * candidates that did not satisfy our hint function. In
576 * that case, we still want to show them, so disable the hint
582 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
583 sort_ambiguous_oid_array(r
, &collect
);
585 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
586 BUG("show_ambiguous_object shouldn't return non-zero");
589 * TRANSLATORS: The argument is the list of ambiguous
590 * objects composed in show_ambiguous_object(). See
591 * its "TRANSLATORS" comments for details.
593 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
595 oid_array_clear(&collect
);
596 strbuf_release(&out
.advice
);
597 strbuf_release(&out
.sb
);
603 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
604 each_abbrev_fn fn
, void *cb_data
)
606 struct oid_array collect
= OID_ARRAY_INIT
;
607 struct disambiguate_state ds
;
610 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
613 ds
.always_call_fn
= 1;
614 ds
.fn
= repo_collect_ambiguous
;
615 ds
.cb_data
= &collect
;
616 find_short_object_filename(&ds
);
617 find_short_packed_object(&ds
);
619 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
620 oid_array_clear(&collect
);
625 * Return the slot of the most-significant bit set in "val". There are various
626 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
627 * probably not a big deal here.
629 static unsigned msb(unsigned long val
)
637 struct min_abbrev_data
{
638 unsigned int init_len
;
639 unsigned int cur_len
;
641 struct repository
*repo
;
642 const struct object_id
*oid
;
645 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
648 static const char hex
[] = "0123456789abcdef";
651 return hex
[oid
->hash
[pos
>> 1] >> 4];
653 return hex
[oid
->hash
[pos
>> 1] & 0xf];
656 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
658 struct min_abbrev_data
*mad
= cb_data
;
660 unsigned int i
= mad
->init_len
;
661 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
664 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
665 mad
->cur_len
= i
+ 1;
670 static int repo_extend_abbrev_len(struct repository
*r
,
671 const struct object_id
*oid
,
674 return extend_abbrev_len(oid
, cb_data
);
677 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
678 struct min_abbrev_data
*mad
)
681 uint32_t num
, first
= 0;
682 struct object_id oid
;
683 const struct object_id
*mad_oid
;
688 num
= m
->num_objects
;
690 match
= bsearch_midx(mad_oid
, m
, &first
);
693 * first is now the position in the packfile where we would insert
694 * mad->hash if it does not exist (or the position of mad->hash if
695 * it does exist). Hence, we consider a maximum of two objects
696 * nearby for the abbreviation length.
700 if (nth_midxed_object_oid(&oid
, m
, first
))
701 extend_abbrev_len(&oid
, mad
);
702 } else if (first
< num
- 1) {
703 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
704 extend_abbrev_len(&oid
, mad
);
707 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
708 extend_abbrev_len(&oid
, mad
);
710 mad
->init_len
= mad
->cur_len
;
713 static void find_abbrev_len_for_pack(struct packed_git
*p
,
714 struct min_abbrev_data
*mad
)
717 uint32_t num
, first
= 0;
718 struct object_id oid
;
719 const struct object_id
*mad_oid
;
721 if (p
->multi_pack_index
)
724 if (open_pack_index(p
) || !p
->num_objects
)
727 num
= p
->num_objects
;
729 match
= bsearch_pack(mad_oid
, p
, &first
);
732 * first is now the position in the packfile where we would insert
733 * mad->hash if it does not exist (or the position of mad->hash if
734 * it does exist). Hence, we consider a maximum of two objects
735 * nearby for the abbreviation length.
739 if (!nth_packed_object_id(&oid
, p
, first
))
740 extend_abbrev_len(&oid
, mad
);
741 } else if (first
< num
- 1) {
742 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
743 extend_abbrev_len(&oid
, mad
);
746 if (!nth_packed_object_id(&oid
, p
, first
- 1))
747 extend_abbrev_len(&oid
, mad
);
749 mad
->init_len
= mad
->cur_len
;
752 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
754 struct multi_pack_index
*m
;
755 struct packed_git
*p
;
757 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
758 find_abbrev_len_for_midx(m
, mad
);
759 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
760 find_abbrev_len_for_pack(p
, mad
);
763 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
764 const struct object_id
*oid
, int len
)
766 struct disambiguate_state ds
;
767 struct min_abbrev_data mad
;
768 struct object_id oid_ret
;
769 const unsigned hexsz
= r
->hash_algo
->hexsz
;
772 unsigned long count
= repo_approximate_object_count(r
);
774 * Add one because the MSB only tells us the highest bit set,
775 * not including the value of all the _other_ bits (so "15"
776 * is only one off of 2^4, but the MSB is the 3rd bit.
778 len
= msb(count
) + 1;
780 * We now know we have on the order of 2^len objects, which
781 * expects a collision at 2^(len/2). But we also care about hex
782 * chars, not bits, and there are 4 bits per hex. So all
783 * together we need to divide by 2 and round up.
785 len
= DIV_ROUND_UP(len
, 2);
787 * For very small repos, we stick with our regular fallback.
789 if (len
< FALLBACK_DEFAULT_ABBREV
)
790 len
= FALLBACK_DEFAULT_ABBREV
;
793 oid_to_hex_r(hex
, oid
);
794 if (len
== hexsz
|| !len
)
803 find_abbrev_len_packed(&mad
);
805 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
808 ds
.fn
= repo_extend_abbrev_len
;
809 ds
.always_call_fn
= 1;
810 ds
.cb_data
= (void *)&mad
;
812 find_short_object_filename(&ds
);
813 (void)finish_object_disambiguation(&ds
, &oid_ret
);
815 hex
[mad
.cur_len
] = 0;
819 const char *repo_find_unique_abbrev(struct repository
*r
,
820 const struct object_id
*oid
,
824 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
825 char *hex
= hexbuffer
[bufno
];
826 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
827 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
831 static int ambiguous_path(const char *path
, int len
)
836 for (cnt
= 0; cnt
< len
; cnt
++) {
856 static inline int at_mark(const char *string
, int len
,
857 const char **suffix
, int nr
)
861 for (i
= 0; i
< nr
; i
++) {
862 int suffix_len
= strlen(suffix
[i
]);
863 if (suffix_len
<= len
864 && !strncasecmp(string
, suffix
[i
], suffix_len
))
870 static inline int upstream_mark(const char *string
, int len
)
872 const char *suffix
[] = { "@{upstream}", "@{u}" };
873 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
876 static inline int push_mark(const char *string
, int len
)
878 const char *suffix
[] = { "@{push}" };
879 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
882 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
883 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
885 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
886 struct object_id
*oid
, unsigned int flags
)
888 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
889 static const char *object_name_msg
= N_(
890 "Git normally never creates a ref that ends with 40 hex characters\n"
891 "because it will be ignored when you just specify 40-hex. These refs\n"
892 "may be created by mistake. For example,\n"
894 " git switch -c $br $(git rev-parse ...)\n"
896 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
897 "examine these refs and maybe delete them. Turn this message off by\n"
898 "running \"git config advice.objectNameWarning false\"");
899 struct object_id tmp_oid
;
900 char *real_ref
= NULL
;
902 int at
, reflog_len
, nth_prior
= 0;
904 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
905 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
906 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
907 if (refs_found
> 0) {
908 warning(warn_msg
, len
, str
);
909 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
910 fprintf(stderr
, "%s\n", _(object_name_msg
));
917 /* basic@{time or number or -number} format to query ref-log */
919 if (len
&& str
[len
-1] == '}') {
920 for (at
= len
-4; at
>= 0; at
--) {
921 if (str
[at
] == '@' && str
[at
+1] == '{') {
922 if (str
[at
+2] == '-') {
924 /* @{-N} not at start */
929 if (!upstream_mark(str
+ at
, len
- at
) &&
930 !push_mark(str
+ at
, len
- at
)) {
931 reflog_len
= (len
-1) - (at
+2);
939 /* Accept only unambiguous ref paths. */
940 if (len
&& ambiguous_path(str
, len
))
944 struct strbuf buf
= STRBUF_INIT
;
947 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
948 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
949 strbuf_release(&buf
);
955 if (!len
&& reflog_len
)
956 /* allow "@{...}" to mean the current branch reflog */
957 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, 0);
959 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
961 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, 0);
966 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
968 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
969 warning(warn_msg
, len
, str
);
977 /* Is it asking for N-th entry, or approxidate? */
978 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
979 char ch
= str
[at
+2+i
];
980 if ('0' <= ch
&& ch
<= '9')
981 nth
= nth
* 10 + ch
- '0';
985 if (100000000 <= nth
) {
992 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
993 at_time
= approxidate_careful(tmp
, &errors
);
1000 if (read_ref_at(get_main_ref_store(r
),
1001 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1002 &co_time
, &co_tz
, &co_cnt
)) {
1004 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1009 if (!(flags
& GET_OID_QUIETLY
)) {
1010 warning(_("log for '%.*s' only goes back to %s"),
1012 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1015 if (flags
& GET_OID_QUIETLY
) {
1018 die(_("log for '%.*s' only has %d entries"),
1028 static enum get_oid_result
get_parent(struct repository
*r
,
1029 const char *name
, int len
,
1030 struct object_id
*result
, int idx
)
1032 struct object_id oid
;
1033 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1034 GET_OID_COMMITTISH
);
1035 struct commit
*commit
;
1036 struct commit_list
*p
;
1040 commit
= lookup_commit_reference(r
, &oid
);
1041 if (repo_parse_commit(the_repository
, commit
))
1042 return MISSING_OBJECT
;
1044 oidcpy(result
, &commit
->object
.oid
);
1047 p
= commit
->parents
;
1050 oidcpy(result
, &p
->item
->object
.oid
);
1055 return MISSING_OBJECT
;
1058 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1059 const char *name
, int len
,
1060 struct object_id
*result
,
1063 struct object_id oid
;
1064 struct commit
*commit
;
1067 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1070 commit
= lookup_commit_reference(r
, &oid
);
1072 return MISSING_OBJECT
;
1074 while (generation
--) {
1075 if (repo_parse_commit(the_repository
, commit
) || !commit
->parents
)
1076 return MISSING_OBJECT
;
1077 commit
= commit
->parents
->item
;
1079 oidcpy(result
, &commit
->object
.oid
);
1083 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1084 struct object
*o
, enum object_type expected_type
)
1086 if (name
&& !namelen
)
1087 namelen
= strlen(name
);
1089 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1091 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1093 if (o
->type
== OBJ_TAG
)
1094 o
= ((struct tag
*) o
)->tagged
;
1095 else if (o
->type
== OBJ_COMMIT
)
1096 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1099 error("%.*s: expected %s type, but the object "
1100 "dereferences to %s type",
1101 namelen
, name
, type_name(expected_type
),
1102 type_name(o
->type
));
1108 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1109 struct object_id
*oid
, unsigned lookup_flags
)
1111 struct object_id outer
;
1113 unsigned int expected_type
= 0;
1117 * "ref^{type}" dereferences ref repeatedly until you cannot
1118 * dereference anymore, or you get an object of given type,
1119 * whichever comes first. "ref^{}" means just dereference
1120 * tags until you get a non-tag. "ref^0" is a shorthand for
1121 * "ref^{commit}". "commit^{tree}" could be used to find the
1122 * top-level tree of the given commit.
1124 if (len
< 4 || name
[len
-1] != '}')
1127 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1129 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1135 sp
++; /* beginning of type name, or closing brace for empty */
1136 if (starts_with(sp
, "commit}"))
1137 expected_type
= OBJ_COMMIT
;
1138 else if (starts_with(sp
, "tag}"))
1139 expected_type
= OBJ_TAG
;
1140 else if (starts_with(sp
, "tree}"))
1141 expected_type
= OBJ_TREE
;
1142 else if (starts_with(sp
, "blob}"))
1143 expected_type
= OBJ_BLOB
;
1144 else if (starts_with(sp
, "object}"))
1145 expected_type
= OBJ_ANY
;
1146 else if (sp
[0] == '}')
1147 expected_type
= OBJ_NONE
;
1148 else if (sp
[0] == '/')
1149 expected_type
= OBJ_COMMIT
;
1153 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1154 if (expected_type
== OBJ_COMMIT
)
1155 lookup_flags
|= GET_OID_COMMITTISH
;
1156 else if (expected_type
== OBJ_TREE
)
1157 lookup_flags
|= GET_OID_TREEISH
;
1159 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1162 o
= parse_object(r
, &outer
);
1165 if (!expected_type
) {
1166 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1167 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1169 oidcpy(oid
, &o
->oid
);
1174 * At this point, the syntax look correct, so
1175 * if we do not get the needed object, we should
1178 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1182 oidcpy(oid
, &o
->oid
);
1184 /* "$commit^{/foo}" */
1187 struct commit_list
*list
= NULL
;
1190 * $commit^{/}. Some regex implementation may reject.
1191 * We don't need regex anyway. '' pattern always matches.
1196 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1197 commit_list_insert((struct commit
*)o
, &list
);
1198 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1205 static int get_describe_name(struct repository
*r
,
1206 const char *name
, int len
,
1207 struct object_id
*oid
)
1210 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1212 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1214 if (!isxdigit(ch
)) {
1215 /* We must be looking at g in "SOMETHING-g"
1216 * for it to be describe output.
1218 if (ch
== 'g' && cp
[-1] == '-') {
1221 return get_short_oid(r
,
1222 cp
, len
, oid
, flags
);
1229 static enum get_oid_result
get_oid_1(struct repository
*r
,
1230 const char *name
, int len
,
1231 struct object_id
*oid
,
1232 unsigned lookup_flags
)
1234 int ret
, has_suffix
;
1238 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1241 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1243 if ('0' <= ch
&& ch
<= '9')
1245 if (ch
== '~' || ch
== '^')
1251 unsigned int num
= 0;
1252 int len1
= cp
- name
;
1254 while (cp
< name
+ len
) {
1255 unsigned int digit
= *cp
++ - '0';
1256 if (unsigned_mult_overflows(num
, 10))
1257 return MISSING_OBJECT
;
1259 if (unsigned_add_overflows(num
, digit
))
1260 return MISSING_OBJECT
;
1263 if (!num
&& len1
== len
- 1)
1265 else if (num
> INT_MAX
)
1266 return MISSING_OBJECT
;
1267 if (has_suffix
== '^')
1268 return get_parent(r
, name
, len1
, oid
, num
);
1269 /* else if (has_suffix == '~') -- goes without saying */
1270 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1273 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1277 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1281 /* It could be describe output that is "SOMETHING-gXXXX" */
1282 ret
= get_describe_name(r
, name
, len
, oid
);
1286 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1290 * This interprets names like ':/Initial revision of "git"' by searching
1291 * through history and returning the first commit whose message starts
1292 * the given regular expression.
1294 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1296 * For a literal '!' character at the beginning of a pattern, you have to repeat
1297 * that, like: ':/!!foo'
1299 * For future extension, all other sequences beginning with ':/!' are reserved.
1302 /* Remember to update object flag allocation in object.h */
1303 #define ONELINE_SEEN (1u<<20)
1305 struct handle_one_ref_cb
{
1306 struct repository
*repo
;
1307 struct commit_list
**list
;
1310 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1314 struct handle_one_ref_cb
*cb
= cb_data
;
1315 struct commit_list
**list
= cb
->list
;
1316 struct object
*object
= parse_object(cb
->repo
, oid
);
1319 if (object
->type
== OBJ_TAG
) {
1320 object
= deref_tag(cb
->repo
, object
, path
,
1325 if (object
->type
!= OBJ_COMMIT
)
1327 commit_list_insert((struct commit
*)object
, list
);
1331 static int get_oid_oneline(struct repository
*r
,
1332 const char *prefix
, struct object_id
*oid
,
1333 struct commit_list
*list
)
1335 struct commit_list
*backup
= NULL
, *l
;
1340 if (prefix
[0] == '!') {
1343 if (prefix
[0] == '-') {
1346 } else if (prefix
[0] != '!') {
1351 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1354 for (l
= list
; l
; l
= l
->next
) {
1355 l
->item
->object
.flags
|= ONELINE_SEEN
;
1356 commit_list_insert(l
->item
, &backup
);
1359 const char *p
, *buf
;
1360 struct commit
*commit
;
1363 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1364 if (!parse_object(r
, &commit
->object
.oid
))
1366 buf
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
1367 p
= strstr(buf
, "\n\n");
1368 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1369 repo_unuse_commit_buffer(the_repository
, commit
, buf
);
1372 oidcpy(oid
, &commit
->object
.oid
);
1378 free_commit_list(list
);
1379 for (l
= backup
; l
; l
= l
->next
)
1380 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1381 free_commit_list(backup
);
1382 return found
? 0 : -1;
1385 struct grab_nth_branch_switch_cbdata
{
1390 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1391 struct object_id
*noid UNUSED
,
1392 const char *email UNUSED
,
1393 timestamp_t timestamp UNUSED
,
1395 const char *message
, void *cb_data
)
1397 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1398 const char *match
= NULL
, *target
= NULL
;
1401 if (skip_prefix(message
, "checkout: moving from ", &match
))
1402 target
= strstr(match
, " to ");
1404 if (!match
|| !target
)
1406 if (--(cb
->remaining
) == 0) {
1407 len
= target
- match
;
1408 strbuf_reset(cb
->sb
);
1409 strbuf_add(cb
->sb
, match
, len
);
1410 return 1; /* we are done */
1416 * Parse @{-N} syntax, return the number of characters parsed
1417 * if successful; otherwise signal an error with negative value.
1419 static int interpret_nth_prior_checkout(struct repository
*r
,
1420 const char *name
, int namelen
,
1425 struct grab_nth_branch_switch_cbdata cb
;
1431 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1433 brace
= memchr(name
, '}', namelen
);
1436 nth
= strtol(name
+ 3, &num_end
, 10);
1437 if (num_end
!= brace
)
1444 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1445 "HEAD", grab_nth_branch_switch
, &cb
);
1447 retval
= brace
- name
+ 1;
1454 int repo_get_oid_mb(struct repository
*r
,
1456 struct object_id
*oid
)
1458 struct commit
*one
, *two
;
1459 struct commit_list
*mbs
;
1460 struct object_id oid_tmp
;
1464 dots
= strstr(name
, "...");
1466 return repo_get_oid(r
, name
, oid
);
1468 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1471 strbuf_init(&sb
, dots
- name
);
1472 strbuf_add(&sb
, name
, dots
- name
);
1473 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1474 strbuf_release(&sb
);
1478 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1482 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1484 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1487 mbs
= repo_get_merge_bases(r
, one
, two
);
1488 if (!mbs
|| mbs
->next
)
1492 oidcpy(oid
, &mbs
->item
->object
.oid
);
1494 free_commit_list(mbs
);
1498 /* parse @something syntax, when 'something' is not {.*} */
1499 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1503 if (len
|| name
[1] == '{')
1506 /* make sure it's a single @, or @@{.*}, not @foo */
1507 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1508 if (next
&& next
[1] != '{')
1511 next
= name
+ namelen
;
1512 if (next
!= name
+ 1)
1516 strbuf_add(buf
, "HEAD", 4);
1520 static int reinterpret(struct repository
*r
,
1521 const char *name
, int namelen
, int len
,
1522 struct strbuf
*buf
, unsigned allowed
)
1524 /* we have extra data, which might need further processing */
1525 struct strbuf tmp
= STRBUF_INIT
;
1526 int used
= buf
->len
;
1528 struct interpret_branch_name_options options
= {
1532 strbuf_add(buf
, name
+ len
, namelen
- len
);
1533 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1534 /* that data was not interpreted, remove our cruft */
1536 strbuf_setlen(buf
, used
);
1540 strbuf_addbuf(buf
, &tmp
);
1541 strbuf_release(&tmp
);
1542 /* tweak for size of {-N} versus expanded ref name */
1543 return ret
- used
+ len
;
1546 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1548 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1550 strbuf_addstr(buf
, s
);
1554 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1559 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1560 starts_with(refname
, "refs/heads/"))
1562 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1563 starts_with(refname
, "refs/remotes/"))
1569 static int interpret_branch_mark(struct repository
*r
,
1570 const char *name
, int namelen
,
1571 int at
, struct strbuf
*buf
,
1572 int (*get_mark
)(const char *, int),
1573 const char *(*get_data
)(struct branch
*,
1575 const struct interpret_branch_name_options
*options
)
1578 struct branch
*branch
;
1579 struct strbuf err
= STRBUF_INIT
;
1582 len
= get_mark(name
+ at
, namelen
- at
);
1586 if (memchr(name
, ':', at
))
1590 char *name_str
= xmemdupz(name
, at
);
1591 branch
= branch_get(name_str
);
1594 branch
= branch_get(NULL
);
1596 value
= get_data(branch
, &err
);
1598 if (options
->nonfatal_dangling_mark
) {
1599 strbuf_release(&err
);
1606 if (!branch_interpret_allowed(value
, options
->allowed
))
1609 set_shortened_ref(r
, buf
, value
);
1613 int repo_interpret_branch_name(struct repository
*r
,
1614 const char *name
, int namelen
,
1616 const struct interpret_branch_name_options
*options
)
1623 namelen
= strlen(name
);
1625 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1626 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1628 return len
; /* syntax Ok, not enough switches */
1629 } else if (len
> 0) {
1631 return len
; /* consumed all */
1633 return reinterpret(r
, name
, namelen
, len
, buf
,
1639 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1642 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1643 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1645 return reinterpret(r
, name
, namelen
, len
, buf
,
1649 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1650 upstream_mark
, branch_get_upstream
,
1655 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1656 push_mark
, branch_get_push
,
1665 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1667 int len
= strlen(name
);
1668 struct interpret_branch_name_options options
= {
1671 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1676 strbuf_add(sb
, name
+ used
, len
- used
);
1679 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1681 if (startup_info
->have_repository
)
1682 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1684 strbuf_addstr(sb
, name
);
1687 * This splice must be done even if we end up rejecting the
1688 * name; builtin/branch.c::copy_or_rename_branch() still wants
1689 * to see what the name expanded to so that "branch -m" can be
1690 * used as a tool to correct earlier mistakes.
1692 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1695 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1698 return check_refname_format(sb
->buf
, 0);
1702 * This is like "get_oid_basic()", except it allows "object ID expressions",
1703 * notably "xyz^" for "parent of xyz"
1705 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1707 struct object_context unused
;
1708 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1712 * This returns a non-zero value if the string (built using printf
1713 * format and the given arguments) is not a valid object.
1715 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1719 struct strbuf sb
= STRBUF_INIT
;
1722 strbuf_vaddf(&sb
, fmt
, ap
);
1725 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1726 strbuf_release(&sb
);
1732 * Many callers know that the user meant to name a commit-ish by
1733 * syntactical positions where the object name appears. Calling this
1734 * function allows the machinery to disambiguate shorter-than-unique
1735 * abbreviated object names between commit-ish and others.
1737 * Note that this does NOT error out when the named object is not a
1738 * commit-ish. It is merely to give a hint to the disambiguation
1741 int repo_get_oid_committish(struct repository
*r
,
1743 struct object_id
*oid
)
1745 struct object_context unused
;
1746 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1750 int repo_get_oid_treeish(struct repository
*r
,
1752 struct object_id
*oid
)
1754 struct object_context unused
;
1755 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1759 int repo_get_oid_commit(struct repository
*r
,
1761 struct object_id
*oid
)
1763 struct object_context unused
;
1764 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1768 int repo_get_oid_tree(struct repository
*r
,
1770 struct object_id
*oid
)
1772 struct object_context unused
;
1773 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1777 int repo_get_oid_blob(struct repository
*r
,
1779 struct object_id
*oid
)
1781 struct object_context unused
;
1782 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1786 /* Must be called only when object_name:filename doesn't exist. */
1787 static void diagnose_invalid_oid_path(struct repository
*r
,
1789 const char *filename
,
1790 const struct object_id
*tree_oid
,
1791 const char *object_name
,
1792 int object_name_len
)
1794 struct object_id oid
;
1795 unsigned short mode
;
1800 if (file_exists(filename
))
1801 die(_("path '%s' exists on disk, but not in '%.*s'"),
1802 filename
, object_name_len
, object_name
);
1803 if (is_missing_file_error(errno
)) {
1804 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1806 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1807 die(_("path '%s' exists, but not '%s'\n"
1808 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1811 object_name_len
, object_name
,
1813 object_name_len
, object_name
,
1816 die(_("path '%s' does not exist in '%.*s'"),
1817 filename
, object_name_len
, object_name
);
1821 /* Must be called only when :stage:filename doesn't exist. */
1822 static void diagnose_invalid_index_path(struct repository
*r
,
1825 const char *filename
)
1827 struct index_state
*istate
= r
->index
;
1828 const struct cache_entry
*ce
;
1830 unsigned namelen
= strlen(filename
);
1831 struct strbuf fullname
= STRBUF_INIT
;
1836 /* Wrong stage number? */
1837 pos
= index_name_pos(istate
, filename
, namelen
);
1840 if (pos
< istate
->cache_nr
) {
1841 ce
= istate
->cache
[pos
];
1842 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1843 ce_namelen(ce
) == namelen
&&
1844 !memcmp(ce
->name
, filename
, namelen
))
1845 die(_("path '%s' is in the index, but not at stage %d\n"
1846 "hint: Did you mean ':%d:%s'?"),
1848 ce_stage(ce
), filename
);
1851 /* Confusion between relative and absolute filenames? */
1852 strbuf_addstr(&fullname
, prefix
);
1853 strbuf_addstr(&fullname
, filename
);
1854 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1857 if (pos
< istate
->cache_nr
) {
1858 ce
= istate
->cache
[pos
];
1859 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1860 ce_namelen(ce
) == fullname
.len
&&
1861 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1862 die(_("path '%s' is in the index, but not '%s'\n"
1863 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1864 fullname
.buf
, filename
,
1865 ce_stage(ce
), fullname
.buf
,
1866 ce_stage(ce
), filename
);
1869 if (repo_file_exists(r
, filename
))
1870 die(_("path '%s' exists on disk, but not in the index"), filename
);
1871 if (is_missing_file_error(errno
))
1872 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1875 strbuf_release(&fullname
);
1879 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1881 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1884 if (r
!= the_repository
|| !is_inside_work_tree())
1885 die(_("relative path syntax can't be used outside working tree"));
1887 /* die() inside prefix_path() if resolved path is outside worktree */
1888 return prefix_path(startup_info
->prefix
,
1889 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1893 static int reject_tree_in_index(struct repository
*repo
,
1895 const struct cache_entry
*ce
,
1900 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1903 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1907 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1911 struct object_id
*oid
,
1912 struct object_context
*oc
)
1914 int ret
, bracket_depth
;
1915 int namelen
= strlen(name
);
1917 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1919 memset(oc
, 0, sizeof(*oc
));
1920 oc
->mode
= S_IFINVALID
;
1921 strbuf_init(&oc
->symlink_path
, 0);
1922 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1923 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1924 die(_("<object>:<path> required, only <object> '%s' given"),
1929 * tree:path --> object name of path in tree
1930 * :path -> object name of absolute path in index
1931 * :./path -> object name of path relative to cwd in index
1932 * :[0-3]:path -> object name of path in index at stage
1933 * :/foo -> recent commit matching foo
1935 if (name
[0] == ':') {
1937 const struct cache_entry
*ce
;
1938 char *new_path
= NULL
;
1940 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1941 struct handle_one_ref_cb cb
;
1942 struct commit_list
*list
= NULL
;
1946 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1947 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1948 commit_list_sort_by_date(&list
);
1949 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1953 name
[1] < '0' || '3' < name
[1])
1956 stage
= name
[1] - '0';
1959 new_path
= resolve_relative_path(repo
, cp
);
1961 namelen
= namelen
- (cp
- name
);
1964 namelen
= strlen(cp
);
1967 if (flags
& GET_OID_RECORD_PATH
)
1968 oc
->path
= xstrdup(cp
);
1970 if (!repo
->index
|| !repo
->index
->cache
)
1971 repo_read_index(repo
);
1972 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1975 while (pos
< repo
->index
->cache_nr
) {
1976 ce
= repo
->index
->cache
[pos
];
1977 if (ce_namelen(ce
) != namelen
||
1978 memcmp(ce
->name
, cp
, namelen
))
1980 if (ce_stage(ce
) == stage
) {
1982 if (reject_tree_in_index(repo
, only_to_die
, ce
,
1985 oidcpy(oid
, &ce
->oid
);
1986 oc
->mode
= ce
->ce_mode
;
1991 if (only_to_die
&& name
[1] && name
[1] != '/')
1992 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1996 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1999 else if (bracket_depth
&& *cp
== '}')
2001 else if (!bracket_depth
&& *cp
== ':')
2005 struct object_id tree_oid
;
2006 int len
= cp
- name
;
2007 unsigned sub_flags
= flags
;
2009 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2010 sub_flags
|= GET_OID_TREEISH
;
2012 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2013 const char *filename
= cp
+1;
2014 char *new_filename
= NULL
;
2016 new_filename
= resolve_relative_path(repo
, filename
);
2018 filename
= new_filename
;
2019 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2020 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2021 filename
, oid
, &oc
->symlink_path
,
2024 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2026 if (ret
&& only_to_die
) {
2027 diagnose_invalid_oid_path(repo
, prefix
,
2033 if (flags
& GET_OID_RECORD_PATH
)
2034 oc
->path
= xstrdup(filename
);
2040 die(_("invalid object name '%.*s'."), len
, name
);
2047 * Call this function when you know "name" given by the end user must
2048 * name an object but it doesn't; the function _may_ die with a better
2049 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2050 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2051 * you have a chance to diagnose the error further.
2053 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2057 struct object_context oc
;
2058 struct object_id oid
;
2059 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2063 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2066 struct object_id
*oid
,
2067 struct object_context
*oc
)
2069 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2070 BUG("incompatible flags for get_oid_with_context");
2071 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);