1 #include "git-compat-util.h"
2 #include "object-name.h"
5 #include "environment.h"
12 #include "tree-walk.h"
16 #include "oid-array.h"
20 #include "object-store-ll.h"
21 #include "read-cache-ll.h"
22 #include "repository.h"
24 #include "submodule.h"
26 #include "commit-reach.h"
29 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
31 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
33 struct disambiguate_state
{
34 int len
; /* length of prefix in hex chars */
35 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
36 struct object_id bin_pfx
;
38 struct repository
*repo
;
39 disambiguate_hint_fn fn
;
41 struct object_id candidate
;
42 unsigned candidate_exists
:1;
43 unsigned candidate_checked
:1;
44 unsigned candidate_ok
:1;
45 unsigned disambiguate_fn_used
:1;
47 unsigned always_call_fn
:1;
50 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
52 if (ds
->always_call_fn
) {
53 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
56 if (!ds
->candidate_exists
) {
57 /* this is the first candidate */
58 oidcpy(&ds
->candidate
, current
);
59 ds
->candidate_exists
= 1;
61 } else if (oideq(&ds
->candidate
, current
)) {
62 /* the same as what we already have seen */
67 /* cannot disambiguate between ds->candidate and current */
72 if (!ds
->candidate_checked
) {
73 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
74 ds
->disambiguate_fn_used
= 1;
75 ds
->candidate_checked
= 1;
78 if (!ds
->candidate_ok
) {
79 /* discard the candidate; we know it does not satisfy fn */
80 oidcpy(&ds
->candidate
, current
);
81 ds
->candidate_checked
= 0;
85 /* if we reach this point, we know ds->candidate satisfies fn */
86 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
88 * if both current and candidate satisfy fn, we cannot
95 /* otherwise, current can be discarded and candidate is still good */
98 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
100 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
102 struct disambiguate_state
*ds
= arg
;
103 /* no need to call match_hash, oidtree_each did prefix match */
104 update_candidates(ds
, oid
);
105 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
108 static void find_short_object_filename(struct disambiguate_state
*ds
)
110 struct object_directory
*odb
;
112 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
113 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
114 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
117 static int match_hash(unsigned len
, const unsigned char *a
, const unsigned char *b
)
127 if ((*a
^ *b
) & 0xf0)
132 static void unique_in_midx(struct multi_pack_index
*m
,
133 struct disambiguate_state
*ds
)
135 uint32_t num
, i
, first
= 0;
136 const struct object_id
*current
= NULL
;
137 num
= m
->num_objects
;
142 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
145 * At this point, "first" is the location of the lowest object
146 * with an object name that could match "bin_pfx". See if we have
147 * 0, 1 or more objects that actually match(es).
149 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
150 struct object_id oid
;
151 current
= nth_midxed_object_oid(&oid
, m
, i
);
152 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
154 update_candidates(ds
, current
);
158 static void unique_in_pack(struct packed_git
*p
,
159 struct disambiguate_state
*ds
)
161 uint32_t num
, i
, first
= 0;
163 if (p
->multi_pack_index
)
166 if (open_pack_index(p
) || !p
->num_objects
)
169 num
= p
->num_objects
;
170 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
173 * At this point, "first" is the location of the lowest object
174 * with an object name that could match "bin_pfx". See if we have
175 * 0, 1 or more objects that actually match(es).
177 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
178 struct object_id oid
;
179 nth_packed_object_id(&oid
, p
, i
);
180 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, oid
.hash
))
182 update_candidates(ds
, &oid
);
186 static void find_short_packed_object(struct disambiguate_state
*ds
)
188 struct multi_pack_index
*m
;
189 struct packed_git
*p
;
191 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
193 unique_in_midx(m
, ds
);
194 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
196 unique_in_pack(p
, ds
);
199 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
200 struct object_id
*oid
)
203 return SHORT_NAME_AMBIGUOUS
;
205 if (!ds
->candidate_exists
)
206 return MISSING_OBJECT
;
208 if (!ds
->candidate_checked
)
210 * If this is the only candidate, there is no point
211 * calling the disambiguation hint callback.
213 * On the other hand, if the current candidate
214 * replaced an earlier candidate that did _not_ pass
215 * the disambiguation hint callback, then we do have
216 * more than one objects that match the short name
217 * given, so we should make sure this one matches;
218 * otherwise, if we discovered this one and the one
219 * that we previously discarded in the reverse order,
220 * we would end up showing different results in the
223 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
224 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
226 if (!ds
->candidate_ok
)
227 return SHORT_NAME_AMBIGUOUS
;
229 oidcpy(oid
, &ds
->candidate
);
233 static int disambiguate_commit_only(struct repository
*r
,
234 const struct object_id
*oid
,
235 void *cb_data UNUSED
)
237 int kind
= oid_object_info(r
, oid
, NULL
);
238 return kind
== OBJ_COMMIT
;
241 static int disambiguate_committish_only(struct repository
*r
,
242 const struct object_id
*oid
,
243 void *cb_data UNUSED
)
248 kind
= oid_object_info(r
, oid
, NULL
);
249 if (kind
== OBJ_COMMIT
)
254 /* We need to do this the hard way... */
255 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
256 if (obj
&& obj
->type
== OBJ_COMMIT
)
261 static int disambiguate_tree_only(struct repository
*r
,
262 const struct object_id
*oid
,
263 void *cb_data UNUSED
)
265 int kind
= oid_object_info(r
, oid
, NULL
);
266 return kind
== OBJ_TREE
;
269 static int disambiguate_treeish_only(struct repository
*r
,
270 const struct object_id
*oid
,
271 void *cb_data UNUSED
)
276 kind
= oid_object_info(r
, oid
, NULL
);
277 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
282 /* We need to do this the hard way... */
283 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
284 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
289 static int disambiguate_blob_only(struct repository
*r
,
290 const struct object_id
*oid
,
291 void *cb_data UNUSED
)
293 int kind
= oid_object_info(r
, oid
, NULL
);
294 return kind
== OBJ_BLOB
;
297 static disambiguate_hint_fn default_disambiguate_hint
;
299 int set_disambiguate_hint_config(const char *var
, const char *value
)
301 static const struct {
303 disambiguate_hint_fn fn
;
306 { "commit", disambiguate_commit_only
},
307 { "committish", disambiguate_committish_only
},
308 { "tree", disambiguate_tree_only
},
309 { "treeish", disambiguate_treeish_only
},
310 { "blob", disambiguate_blob_only
}
315 return config_error_nonbool(var
);
317 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
318 if (!strcasecmp(value
, hints
[i
].name
)) {
319 default_disambiguate_hint
= hints
[i
].fn
;
324 return error("unknown hint type for '%s': %s", var
, value
);
327 static int init_object_disambiguation(struct repository
*r
,
328 const char *name
, int len
,
329 struct disambiguate_state
*ds
)
333 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
336 memset(ds
, 0, sizeof(*ds
));
338 for (i
= 0; i
< len
;i
++) {
339 unsigned char c
= name
[i
];
341 if (c
>= '0' && c
<= '9')
343 else if (c
>= 'a' && c
<= 'f')
345 else if (c
>= 'A' && c
<='F') {
354 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
358 ds
->hex_pfx
[len
] = '\0';
364 struct ambiguous_output
{
365 const struct disambiguate_state
*ds
;
366 struct strbuf advice
;
370 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
372 struct ambiguous_output
*state
= data
;
373 const struct disambiguate_state
*ds
= state
->ds
;
374 struct strbuf
*advice
= &state
->advice
;
375 struct strbuf
*sb
= &state
->sb
;
379 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
382 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
383 type
= oid_object_info(ds
->repo
, oid
, NULL
);
387 * TRANSLATORS: This is a line of ambiguous object
388 * output shown when we cannot look up or parse the
389 * object in question. E.g. "deadbeef [bad object]".
391 strbuf_addf(sb
, _("%s [bad object]"), hash
);
395 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
396 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
398 if (type
== OBJ_COMMIT
) {
399 struct strbuf date
= STRBUF_INIT
;
400 struct strbuf msg
= STRBUF_INIT
;
401 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
404 struct pretty_print_context pp
= {0};
405 pp
.date_mode
.type
= DATE_SHORT
;
406 repo_format_commit_message(the_repository
, commit
,
408 repo_format_commit_message(the_repository
, commit
,
413 * TRANSLATORS: This is a line of ambiguous commit
414 * object output. E.g.:
416 * "deadbeef commit 2021-01-01 - Some Commit Message"
418 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
421 strbuf_release(&date
);
422 strbuf_release(&msg
);
423 } else if (type
== OBJ_TAG
) {
424 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
426 if (!parse_tag(tag
) && tag
->tag
) {
428 * TRANSLATORS: This is a line of ambiguous
429 * tag object output. E.g.:
431 * "deadbeef tag 2022-01-01 - Some Tag Message"
433 * The second argument is the YYYY-MM-DD found
436 * The third argument is the "tag" string
439 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
440 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
444 * TRANSLATORS: This is a line of ambiguous
445 * tag object output where we couldn't parse
446 * the tag itself. E.g.:
448 * "deadbeef [bad tag, could not parse it]"
450 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
453 } else if (type
== OBJ_TREE
) {
455 * TRANSLATORS: This is a line of ambiguous <type>
456 * object output. E.g. "deadbeef tree".
458 strbuf_addf(sb
, _("%s tree"), hash
);
459 } else if (type
== OBJ_BLOB
) {
461 * TRANSLATORS: This is a line of ambiguous <type>
462 * object output. E.g. "deadbeef blob".
464 strbuf_addf(sb
, _("%s blob"), hash
);
470 * TRANSLATORS: This is line item of ambiguous object output
471 * from describe_ambiguous_object() above. For RTL languages
472 * you'll probably want to swap the "%s" and leading " " space
475 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
481 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
483 oid_array_append(data
, oid
);
487 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
488 const struct object_id
*oid
,
491 return collect_ambiguous(oid
, data
);
494 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
496 struct repository
*sort_ambiguous_repo
= ctx
;
497 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
498 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
503 * Sorts by hash within the same object type, just as
504 * oid_array_for_each_unique() would do.
506 if (a_type
== b_type
)
510 * Between object types show tags, then commits, and finally
513 * The object_type enum is commit, tree, blob, tag, but we
514 * want tag, commit, tree blob. Cleverly (perhaps too
515 * cleverly) do that with modulus, since the enum assigns 1 to
516 * commit, so tag becomes 0.
518 a_type_sort
= a_type
% 4;
519 b_type_sort
= b_type
% 4;
520 return a_type_sort
> b_type_sort
? 1 : -1;
523 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
525 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
528 static enum get_oid_result
get_short_oid(struct repository
*r
,
529 const char *name
, int len
,
530 struct object_id
*oid
,
534 struct disambiguate_state ds
;
535 int quietly
= !!(flags
& GET_OID_QUIETLY
);
537 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
540 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
541 BUG("multiple get_short_oid disambiguator flags");
543 if (flags
& GET_OID_COMMIT
)
544 ds
.fn
= disambiguate_commit_only
;
545 else if (flags
& GET_OID_COMMITTISH
)
546 ds
.fn
= disambiguate_committish_only
;
547 else if (flags
& GET_OID_TREE
)
548 ds
.fn
= disambiguate_tree_only
;
549 else if (flags
& GET_OID_TREEISH
)
550 ds
.fn
= disambiguate_treeish_only
;
551 else if (flags
& GET_OID_BLOB
)
552 ds
.fn
= disambiguate_blob_only
;
554 ds
.fn
= default_disambiguate_hint
;
556 find_short_object_filename(&ds
);
557 find_short_packed_object(&ds
);
558 status
= finish_object_disambiguation(&ds
, oid
);
561 * If we didn't find it, do the usual reprepare() slow-path,
562 * since the object may have recently been added to the repository
563 * or migrated from loose to packed.
565 if (status
== MISSING_OBJECT
) {
566 reprepare_packed_git(r
);
567 find_short_object_filename(&ds
);
568 find_short_packed_object(&ds
);
569 status
= finish_object_disambiguation(&ds
, oid
);
572 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
573 struct oid_array collect
= OID_ARRAY_INIT
;
574 struct ambiguous_output out
= {
577 .advice
= STRBUF_INIT
,
580 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
583 * We may still have ambiguity if we simply saw a series of
584 * candidates that did not satisfy our hint function. In
585 * that case, we still want to show them, so disable the hint
591 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
592 sort_ambiguous_oid_array(r
, &collect
);
594 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
595 BUG("show_ambiguous_object shouldn't return non-zero");
598 * TRANSLATORS: The argument is the list of ambiguous
599 * objects composed in show_ambiguous_object(). See
600 * its "TRANSLATORS" comments for details.
602 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
604 oid_array_clear(&collect
);
605 strbuf_release(&out
.advice
);
606 strbuf_release(&out
.sb
);
612 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
613 each_abbrev_fn fn
, void *cb_data
)
615 struct oid_array collect
= OID_ARRAY_INIT
;
616 struct disambiguate_state ds
;
619 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
622 ds
.always_call_fn
= 1;
623 ds
.fn
= repo_collect_ambiguous
;
624 ds
.cb_data
= &collect
;
625 find_short_object_filename(&ds
);
626 find_short_packed_object(&ds
);
628 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
629 oid_array_clear(&collect
);
634 * Return the slot of the most-significant bit set in "val". There are various
635 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
636 * probably not a big deal here.
638 static unsigned msb(unsigned long val
)
646 struct min_abbrev_data
{
647 unsigned int init_len
;
648 unsigned int cur_len
;
650 struct repository
*repo
;
651 const struct object_id
*oid
;
654 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
657 static const char hex
[] = "0123456789abcdef";
660 return hex
[oid
->hash
[pos
>> 1] >> 4];
662 return hex
[oid
->hash
[pos
>> 1] & 0xf];
665 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
667 struct min_abbrev_data
*mad
= cb_data
;
669 unsigned int i
= mad
->init_len
;
670 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
673 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
674 mad
->cur_len
= i
+ 1;
679 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
680 const struct object_id
*oid
,
683 return extend_abbrev_len(oid
, cb_data
);
686 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
687 struct min_abbrev_data
*mad
)
690 uint32_t num
, first
= 0;
691 struct object_id oid
;
692 const struct object_id
*mad_oid
;
697 num
= m
->num_objects
;
699 match
= bsearch_midx(mad_oid
, m
, &first
);
702 * first is now the position in the packfile where we would insert
703 * mad->hash if it does not exist (or the position of mad->hash if
704 * it does exist). Hence, we consider a maximum of two objects
705 * nearby for the abbreviation length.
709 if (nth_midxed_object_oid(&oid
, m
, first
))
710 extend_abbrev_len(&oid
, mad
);
711 } else if (first
< num
- 1) {
712 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
713 extend_abbrev_len(&oid
, mad
);
716 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
717 extend_abbrev_len(&oid
, mad
);
719 mad
->init_len
= mad
->cur_len
;
722 static void find_abbrev_len_for_pack(struct packed_git
*p
,
723 struct min_abbrev_data
*mad
)
726 uint32_t num
, first
= 0;
727 struct object_id oid
;
728 const struct object_id
*mad_oid
;
730 if (p
->multi_pack_index
)
733 if (open_pack_index(p
) || !p
->num_objects
)
736 num
= p
->num_objects
;
738 match
= bsearch_pack(mad_oid
, p
, &first
);
741 * first is now the position in the packfile where we would insert
742 * mad->hash if it does not exist (or the position of mad->hash if
743 * it does exist). Hence, we consider a maximum of two objects
744 * nearby for the abbreviation length.
748 if (!nth_packed_object_id(&oid
, p
, first
))
749 extend_abbrev_len(&oid
, mad
);
750 } else if (first
< num
- 1) {
751 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
752 extend_abbrev_len(&oid
, mad
);
755 if (!nth_packed_object_id(&oid
, p
, first
- 1))
756 extend_abbrev_len(&oid
, mad
);
758 mad
->init_len
= mad
->cur_len
;
761 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
763 struct multi_pack_index
*m
;
764 struct packed_git
*p
;
766 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
767 find_abbrev_len_for_midx(m
, mad
);
768 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
769 find_abbrev_len_for_pack(p
, mad
);
772 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
773 const struct object_id
*oid
, int len
)
775 struct disambiguate_state ds
;
776 struct min_abbrev_data mad
;
777 struct object_id oid_ret
;
778 const unsigned hexsz
= r
->hash_algo
->hexsz
;
781 unsigned long count
= repo_approximate_object_count(r
);
783 * Add one because the MSB only tells us the highest bit set,
784 * not including the value of all the _other_ bits (so "15"
785 * is only one off of 2^4, but the MSB is the 3rd bit.
787 len
= msb(count
) + 1;
789 * We now know we have on the order of 2^len objects, which
790 * expects a collision at 2^(len/2). But we also care about hex
791 * chars, not bits, and there are 4 bits per hex. So all
792 * together we need to divide by 2 and round up.
794 len
= DIV_ROUND_UP(len
, 2);
796 * For very small repos, we stick with our regular fallback.
798 if (len
< FALLBACK_DEFAULT_ABBREV
)
799 len
= FALLBACK_DEFAULT_ABBREV
;
802 oid_to_hex_r(hex
, oid
);
803 if (len
== hexsz
|| !len
)
812 find_abbrev_len_packed(&mad
);
814 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
817 ds
.fn
= repo_extend_abbrev_len
;
818 ds
.always_call_fn
= 1;
819 ds
.cb_data
= (void *)&mad
;
821 find_short_object_filename(&ds
);
822 (void)finish_object_disambiguation(&ds
, &oid_ret
);
824 hex
[mad
.cur_len
] = 0;
828 const char *repo_find_unique_abbrev(struct repository
*r
,
829 const struct object_id
*oid
,
833 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
834 char *hex
= hexbuffer
[bufno
];
835 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
836 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
840 static int ambiguous_path(const char *path
, int len
)
845 for (cnt
= 0; cnt
< len
; cnt
++) {
865 static inline int at_mark(const char *string
, int len
,
866 const char **suffix
, int nr
)
870 for (i
= 0; i
< nr
; i
++) {
871 int suffix_len
= strlen(suffix
[i
]);
872 if (suffix_len
<= len
873 && !strncasecmp(string
, suffix
[i
], suffix_len
))
879 static inline int upstream_mark(const char *string
, int len
)
881 const char *suffix
[] = { "@{upstream}", "@{u}" };
882 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
885 static inline int push_mark(const char *string
, int len
)
887 const char *suffix
[] = { "@{push}" };
888 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
891 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
892 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
894 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
895 struct object_id
*oid
, unsigned int flags
)
897 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
898 static const char *object_name_msg
= N_(
899 "Git normally never creates a ref that ends with 40 hex characters\n"
900 "because it will be ignored when you just specify 40-hex. These refs\n"
901 "may be created by mistake. For example,\n"
903 " git switch -c $br $(git rev-parse ...)\n"
905 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
906 "examine these refs and maybe delete them. Turn this message off by\n"
907 "running \"git config advice.objectNameWarning false\"");
908 struct object_id tmp_oid
;
909 char *real_ref
= NULL
;
911 int at
, reflog_len
, nth_prior
= 0;
912 int fatal
= !(flags
& GET_OID_QUIETLY
);
914 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
915 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
916 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
917 if (refs_found
> 0) {
918 warning(warn_msg
, len
, str
);
919 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
920 fprintf(stderr
, "%s\n", _(object_name_msg
));
927 /* basic@{time or number or -number} format to query ref-log */
929 if (len
&& str
[len
-1] == '}') {
930 for (at
= len
-4; at
>= 0; at
--) {
931 if (str
[at
] == '@' && str
[at
+1] == '{') {
932 if (str
[at
+2] == '-') {
934 /* @{-N} not at start */
939 if (!upstream_mark(str
+ at
, len
- at
) &&
940 !push_mark(str
+ at
, len
- at
)) {
941 reflog_len
= (len
-1) - (at
+2);
949 /* Accept only unambiguous ref paths. */
950 if (len
&& ambiguous_path(str
, len
))
954 struct strbuf buf
= STRBUF_INIT
;
957 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
958 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
959 strbuf_release(&buf
);
965 if (!len
&& reflog_len
)
966 /* allow "@{...}" to mean the current branch reflog */
967 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, !fatal
);
969 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
971 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, !fatal
);
976 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
978 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
979 warning(warn_msg
, len
, str
);
987 /* Is it asking for N-th entry, or approxidate? */
988 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
989 char ch
= str
[at
+2+i
];
990 if ('0' <= ch
&& ch
<= '9')
991 nth
= nth
* 10 + ch
- '0';
995 if (100000000 <= nth
) {
1002 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
1003 at_time
= approxidate_careful(tmp
, &errors
);
1010 if (read_ref_at(get_main_ref_store(r
),
1011 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1012 &co_time
, &co_tz
, &co_cnt
)) {
1014 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1019 if (!(flags
& GET_OID_QUIETLY
)) {
1020 warning(_("log for '%.*s' only goes back to %s"),
1022 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1025 if (flags
& GET_OID_QUIETLY
) {
1028 die(_("log for '%.*s' only has %d entries"),
1038 static enum get_oid_result
get_parent(struct repository
*r
,
1039 const char *name
, int len
,
1040 struct object_id
*result
, int idx
)
1042 struct object_id oid
;
1043 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1044 GET_OID_COMMITTISH
);
1045 struct commit
*commit
;
1046 struct commit_list
*p
;
1050 commit
= lookup_commit_reference(r
, &oid
);
1051 if (repo_parse_commit(r
, commit
))
1052 return MISSING_OBJECT
;
1054 oidcpy(result
, &commit
->object
.oid
);
1057 p
= commit
->parents
;
1060 oidcpy(result
, &p
->item
->object
.oid
);
1065 return MISSING_OBJECT
;
1068 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1069 const char *name
, int len
,
1070 struct object_id
*result
,
1073 struct object_id oid
;
1074 struct commit
*commit
;
1077 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1080 commit
= lookup_commit_reference(r
, &oid
);
1082 return MISSING_OBJECT
;
1084 while (generation
--) {
1085 if (repo_parse_commit(r
, commit
) || !commit
->parents
)
1086 return MISSING_OBJECT
;
1087 commit
= commit
->parents
->item
;
1089 oidcpy(result
, &commit
->object
.oid
);
1093 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1094 struct object
*o
, enum object_type expected_type
)
1096 if (name
&& !namelen
)
1097 namelen
= strlen(name
);
1099 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1101 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1103 if (o
->type
== OBJ_TAG
)
1104 o
= ((struct tag
*) o
)->tagged
;
1105 else if (o
->type
== OBJ_COMMIT
)
1106 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1109 error("%.*s: expected %s type, but the object "
1110 "dereferences to %s type",
1111 namelen
, name
, type_name(expected_type
),
1112 type_name(o
->type
));
1118 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1119 struct object_id
*oid
, unsigned lookup_flags
)
1121 struct object_id outer
;
1123 unsigned int expected_type
= 0;
1127 * "ref^{type}" dereferences ref repeatedly until you cannot
1128 * dereference anymore, or you get an object of given type,
1129 * whichever comes first. "ref^{}" means just dereference
1130 * tags until you get a non-tag. "ref^0" is a shorthand for
1131 * "ref^{commit}". "commit^{tree}" could be used to find the
1132 * top-level tree of the given commit.
1134 if (len
< 4 || name
[len
-1] != '}')
1137 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1139 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1145 sp
++; /* beginning of type name, or closing brace for empty */
1146 if (starts_with(sp
, "commit}"))
1147 expected_type
= OBJ_COMMIT
;
1148 else if (starts_with(sp
, "tag}"))
1149 expected_type
= OBJ_TAG
;
1150 else if (starts_with(sp
, "tree}"))
1151 expected_type
= OBJ_TREE
;
1152 else if (starts_with(sp
, "blob}"))
1153 expected_type
= OBJ_BLOB
;
1154 else if (starts_with(sp
, "object}"))
1155 expected_type
= OBJ_ANY
;
1156 else if (sp
[0] == '}')
1157 expected_type
= OBJ_NONE
;
1158 else if (sp
[0] == '/')
1159 expected_type
= OBJ_COMMIT
;
1163 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1164 if (expected_type
== OBJ_COMMIT
)
1165 lookup_flags
|= GET_OID_COMMITTISH
;
1166 else if (expected_type
== OBJ_TREE
)
1167 lookup_flags
|= GET_OID_TREEISH
;
1169 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1172 o
= parse_object(r
, &outer
);
1175 if (!expected_type
) {
1176 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1177 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1179 oidcpy(oid
, &o
->oid
);
1184 * At this point, the syntax look correct, so
1185 * if we do not get the needed object, we should
1188 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1192 oidcpy(oid
, &o
->oid
);
1194 /* "$commit^{/foo}" */
1197 struct commit_list
*list
= NULL
;
1200 * $commit^{/}. Some regex implementation may reject.
1201 * We don't need regex anyway. '' pattern always matches.
1206 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1207 commit_list_insert((struct commit
*)o
, &list
);
1208 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1215 static int get_describe_name(struct repository
*r
,
1216 const char *name
, int len
,
1217 struct object_id
*oid
)
1220 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1222 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1224 if (!isxdigit(ch
)) {
1225 /* We must be looking at g in "SOMETHING-g"
1226 * for it to be describe output.
1228 if (ch
== 'g' && cp
[-1] == '-') {
1231 return get_short_oid(r
,
1232 cp
, len
, oid
, flags
);
1239 static enum get_oid_result
get_oid_1(struct repository
*r
,
1240 const char *name
, int len
,
1241 struct object_id
*oid
,
1242 unsigned lookup_flags
)
1244 int ret
, has_suffix
;
1248 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1251 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1253 if ('0' <= ch
&& ch
<= '9')
1255 if (ch
== '~' || ch
== '^')
1261 unsigned int num
= 0;
1262 int len1
= cp
- name
;
1264 while (cp
< name
+ len
) {
1265 unsigned int digit
= *cp
++ - '0';
1266 if (unsigned_mult_overflows(num
, 10))
1267 return MISSING_OBJECT
;
1269 if (unsigned_add_overflows(num
, digit
))
1270 return MISSING_OBJECT
;
1273 if (!num
&& len1
== len
- 1)
1275 else if (num
> INT_MAX
)
1276 return MISSING_OBJECT
;
1277 if (has_suffix
== '^')
1278 return get_parent(r
, name
, len1
, oid
, num
);
1279 /* else if (has_suffix == '~') -- goes without saying */
1280 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1283 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1287 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1291 /* It could be describe output that is "SOMETHING-gXXXX" */
1292 ret
= get_describe_name(r
, name
, len
, oid
);
1296 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1300 * This interprets names like ':/Initial revision of "git"' by searching
1301 * through history and returning the first commit whose message starts
1302 * the given regular expression.
1304 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1306 * For a literal '!' character at the beginning of a pattern, you have to repeat
1307 * that, like: ':/!!foo'
1309 * For future extension, all other sequences beginning with ':/!' are reserved.
1312 /* Remember to update object flag allocation in object.h */
1313 #define ONELINE_SEEN (1u<<20)
1315 struct handle_one_ref_cb
{
1316 struct repository
*repo
;
1317 struct commit_list
**list
;
1320 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1324 struct handle_one_ref_cb
*cb
= cb_data
;
1325 struct commit_list
**list
= cb
->list
;
1326 struct object
*object
= parse_object(cb
->repo
, oid
);
1329 if (object
->type
== OBJ_TAG
) {
1330 object
= deref_tag(cb
->repo
, object
, path
,
1335 if (object
->type
!= OBJ_COMMIT
)
1337 commit_list_insert((struct commit
*)object
, list
);
1341 static int get_oid_oneline(struct repository
*r
,
1342 const char *prefix
, struct object_id
*oid
,
1343 struct commit_list
*list
)
1345 struct commit_list
*backup
= NULL
, *l
;
1350 if (prefix
[0] == '!') {
1353 if (prefix
[0] == '-') {
1356 } else if (prefix
[0] != '!') {
1361 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1364 for (l
= list
; l
; l
= l
->next
) {
1365 l
->item
->object
.flags
|= ONELINE_SEEN
;
1366 commit_list_insert(l
->item
, &backup
);
1369 const char *p
, *buf
;
1370 struct commit
*commit
;
1373 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1374 if (!parse_object(r
, &commit
->object
.oid
))
1376 buf
= repo_get_commit_buffer(r
, commit
, NULL
);
1377 p
= strstr(buf
, "\n\n");
1378 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1379 repo_unuse_commit_buffer(r
, commit
, buf
);
1382 oidcpy(oid
, &commit
->object
.oid
);
1388 free_commit_list(list
);
1389 for (l
= backup
; l
; l
= l
->next
)
1390 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1391 free_commit_list(backup
);
1392 return found
? 0 : -1;
1395 struct grab_nth_branch_switch_cbdata
{
1400 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1401 struct object_id
*noid UNUSED
,
1402 const char *email UNUSED
,
1403 timestamp_t timestamp UNUSED
,
1405 const char *message
, void *cb_data
)
1407 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1408 const char *match
= NULL
, *target
= NULL
;
1411 if (skip_prefix(message
, "checkout: moving from ", &match
))
1412 target
= strstr(match
, " to ");
1414 if (!match
|| !target
)
1416 if (--(cb
->remaining
) == 0) {
1417 len
= target
- match
;
1418 strbuf_reset(cb
->sb
);
1419 strbuf_add(cb
->sb
, match
, len
);
1420 return 1; /* we are done */
1426 * Parse @{-N} syntax, return the number of characters parsed
1427 * if successful; otherwise signal an error with negative value.
1429 static int interpret_nth_prior_checkout(struct repository
*r
,
1430 const char *name
, int namelen
,
1435 struct grab_nth_branch_switch_cbdata cb
;
1441 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1443 brace
= memchr(name
, '}', namelen
);
1446 nth
= strtol(name
+ 3, &num_end
, 10);
1447 if (num_end
!= brace
)
1454 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1455 "HEAD", grab_nth_branch_switch
, &cb
);
1457 retval
= brace
- name
+ 1;
1464 int repo_get_oid_mb(struct repository
*r
,
1466 struct object_id
*oid
)
1468 struct commit
*one
, *two
;
1469 struct commit_list
*mbs
;
1470 struct object_id oid_tmp
;
1474 dots
= strstr(name
, "...");
1476 return repo_get_oid(r
, name
, oid
);
1478 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1481 strbuf_init(&sb
, dots
- name
);
1482 strbuf_add(&sb
, name
, dots
- name
);
1483 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1484 strbuf_release(&sb
);
1488 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1492 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1494 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1497 mbs
= repo_get_merge_bases(r
, one
, two
);
1498 if (!mbs
|| mbs
->next
)
1502 oidcpy(oid
, &mbs
->item
->object
.oid
);
1504 free_commit_list(mbs
);
1508 /* parse @something syntax, when 'something' is not {.*} */
1509 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1513 if (len
|| name
[1] == '{')
1516 /* make sure it's a single @, or @@{.*}, not @foo */
1517 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1518 if (next
&& next
[1] != '{')
1521 next
= name
+ namelen
;
1522 if (next
!= name
+ 1)
1526 strbuf_add(buf
, "HEAD", 4);
1530 static int reinterpret(struct repository
*r
,
1531 const char *name
, int namelen
, int len
,
1532 struct strbuf
*buf
, unsigned allowed
)
1534 /* we have extra data, which might need further processing */
1535 struct strbuf tmp
= STRBUF_INIT
;
1536 int used
= buf
->len
;
1538 struct interpret_branch_name_options options
= {
1542 strbuf_add(buf
, name
+ len
, namelen
- len
);
1543 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1544 /* that data was not interpreted, remove our cruft */
1546 strbuf_setlen(buf
, used
);
1550 strbuf_addbuf(buf
, &tmp
);
1551 strbuf_release(&tmp
);
1552 /* tweak for size of {-N} versus expanded ref name */
1553 return ret
- used
+ len
;
1556 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1558 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1560 strbuf_addstr(buf
, s
);
1564 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1569 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1570 starts_with(refname
, "refs/heads/"))
1572 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1573 starts_with(refname
, "refs/remotes/"))
1579 static int interpret_branch_mark(struct repository
*r
,
1580 const char *name
, int namelen
,
1581 int at
, struct strbuf
*buf
,
1582 int (*get_mark
)(const char *, int),
1583 const char *(*get_data
)(struct branch
*,
1585 const struct interpret_branch_name_options
*options
)
1588 struct branch
*branch
;
1589 struct strbuf err
= STRBUF_INIT
;
1592 len
= get_mark(name
+ at
, namelen
- at
);
1596 if (memchr(name
, ':', at
))
1600 char *name_str
= xmemdupz(name
, at
);
1601 branch
= branch_get(name_str
);
1604 branch
= branch_get(NULL
);
1606 value
= get_data(branch
, &err
);
1608 if (options
->nonfatal_dangling_mark
) {
1609 strbuf_release(&err
);
1616 if (!branch_interpret_allowed(value
, options
->allowed
))
1619 set_shortened_ref(r
, buf
, value
);
1623 int repo_interpret_branch_name(struct repository
*r
,
1624 const char *name
, int namelen
,
1626 const struct interpret_branch_name_options
*options
)
1633 namelen
= strlen(name
);
1635 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1636 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1638 return len
; /* syntax Ok, not enough switches */
1639 } else if (len
> 0) {
1641 return len
; /* consumed all */
1643 return reinterpret(r
, name
, namelen
, len
, buf
,
1649 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1652 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1653 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1655 return reinterpret(r
, name
, namelen
, len
, buf
,
1659 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1660 upstream_mark
, branch_get_upstream
,
1665 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1666 push_mark
, branch_get_push
,
1675 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1677 int len
= strlen(name
);
1678 struct interpret_branch_name_options options
= {
1681 int used
= repo_interpret_branch_name(the_repository
, name
, len
, sb
,
1686 strbuf_add(sb
, name
+ used
, len
- used
);
1689 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1691 if (startup_info
->have_repository
)
1692 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1694 strbuf_addstr(sb
, name
);
1697 * This splice must be done even if we end up rejecting the
1698 * name; builtin/branch.c::copy_or_rename_branch() still wants
1699 * to see what the name expanded to so that "branch -m" can be
1700 * used as a tool to correct earlier mistakes.
1702 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1705 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1708 return check_refname_format(sb
->buf
, 0);
1712 * This is like "get_oid_basic()", except it allows "object ID expressions",
1713 * notably "xyz^" for "parent of xyz"
1715 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1717 struct object_context unused
;
1718 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1722 * This returns a non-zero value if the string (built using printf
1723 * format and the given arguments) is not a valid object.
1725 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1729 struct strbuf sb
= STRBUF_INIT
;
1732 strbuf_vaddf(&sb
, fmt
, ap
);
1735 ret
= repo_get_oid(the_repository
, sb
.buf
, oid
);
1736 strbuf_release(&sb
);
1742 * Many callers know that the user meant to name a commit-ish by
1743 * syntactical positions where the object name appears. Calling this
1744 * function allows the machinery to disambiguate shorter-than-unique
1745 * abbreviated object names between commit-ish and others.
1747 * Note that this does NOT error out when the named object is not a
1748 * commit-ish. It is merely to give a hint to the disambiguation
1751 int repo_get_oid_committish(struct repository
*r
,
1753 struct object_id
*oid
)
1755 struct object_context unused
;
1756 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1760 int repo_get_oid_treeish(struct repository
*r
,
1762 struct object_id
*oid
)
1764 struct object_context unused
;
1765 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1769 int repo_get_oid_commit(struct repository
*r
,
1771 struct object_id
*oid
)
1773 struct object_context unused
;
1774 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1778 int repo_get_oid_tree(struct repository
*r
,
1780 struct object_id
*oid
)
1782 struct object_context unused
;
1783 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1787 int repo_get_oid_blob(struct repository
*r
,
1789 struct object_id
*oid
)
1791 struct object_context unused
;
1792 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1796 /* Must be called only when object_name:filename doesn't exist. */
1797 static void diagnose_invalid_oid_path(struct repository
*r
,
1799 const char *filename
,
1800 const struct object_id
*tree_oid
,
1801 const char *object_name
,
1802 int object_name_len
)
1804 struct object_id oid
;
1805 unsigned short mode
;
1810 if (file_exists(filename
))
1811 die(_("path '%s' exists on disk, but not in '%.*s'"),
1812 filename
, object_name_len
, object_name
);
1813 if (is_missing_file_error(errno
)) {
1814 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1816 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1817 die(_("path '%s' exists, but not '%s'\n"
1818 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1821 object_name_len
, object_name
,
1823 object_name_len
, object_name
,
1826 die(_("path '%s' does not exist in '%.*s'"),
1827 filename
, object_name_len
, object_name
);
1831 /* Must be called only when :stage:filename doesn't exist. */
1832 static void diagnose_invalid_index_path(struct repository
*r
,
1835 const char *filename
)
1837 struct index_state
*istate
= r
->index
;
1838 const struct cache_entry
*ce
;
1840 unsigned namelen
= strlen(filename
);
1841 struct strbuf fullname
= STRBUF_INIT
;
1846 /* Wrong stage number? */
1847 pos
= index_name_pos(istate
, filename
, namelen
);
1850 if (pos
< istate
->cache_nr
) {
1851 ce
= istate
->cache
[pos
];
1852 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1853 ce_namelen(ce
) == namelen
&&
1854 !memcmp(ce
->name
, filename
, namelen
))
1855 die(_("path '%s' is in the index, but not at stage %d\n"
1856 "hint: Did you mean ':%d:%s'?"),
1858 ce_stage(ce
), filename
);
1861 /* Confusion between relative and absolute filenames? */
1862 strbuf_addstr(&fullname
, prefix
);
1863 strbuf_addstr(&fullname
, filename
);
1864 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1867 if (pos
< istate
->cache_nr
) {
1868 ce
= istate
->cache
[pos
];
1869 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1870 ce_namelen(ce
) == fullname
.len
&&
1871 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1872 die(_("path '%s' is in the index, but not '%s'\n"
1873 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1874 fullname
.buf
, filename
,
1875 ce_stage(ce
), fullname
.buf
,
1876 ce_stage(ce
), filename
);
1879 if (repo_file_exists(r
, filename
))
1880 die(_("path '%s' exists on disk, but not in the index"), filename
);
1881 if (is_missing_file_error(errno
))
1882 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1885 strbuf_release(&fullname
);
1889 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1891 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1894 if (r
!= the_repository
|| !is_inside_work_tree())
1895 die(_("relative path syntax can't be used outside working tree"));
1897 /* die() inside prefix_path() if resolved path is outside worktree */
1898 return prefix_path(startup_info
->prefix
,
1899 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1903 static int reject_tree_in_index(struct repository
*repo
,
1905 const struct cache_entry
*ce
,
1910 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1913 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1917 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1921 struct object_id
*oid
,
1922 struct object_context
*oc
)
1924 int ret
, bracket_depth
;
1925 int namelen
= strlen(name
);
1927 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1929 memset(oc
, 0, sizeof(*oc
));
1930 oc
->mode
= S_IFINVALID
;
1931 strbuf_init(&oc
->symlink_path
, 0);
1932 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1933 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1934 die(_("<object>:<path> required, only <object> '%s' given"),
1939 * tree:path --> object name of path in tree
1940 * :path -> object name of absolute path in index
1941 * :./path -> object name of path relative to cwd in index
1942 * :[0-3]:path -> object name of path in index at stage
1943 * :/foo -> recent commit matching foo
1945 if (name
[0] == ':') {
1947 const struct cache_entry
*ce
;
1948 char *new_path
= NULL
;
1950 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1951 struct handle_one_ref_cb cb
;
1952 struct commit_list
*list
= NULL
;
1956 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1957 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1958 commit_list_sort_by_date(&list
);
1959 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1963 name
[1] < '0' || '3' < name
[1])
1966 stage
= name
[1] - '0';
1969 new_path
= resolve_relative_path(repo
, cp
);
1971 namelen
= namelen
- (cp
- name
);
1974 namelen
= strlen(cp
);
1977 if (flags
& GET_OID_RECORD_PATH
)
1978 oc
->path
= xstrdup(cp
);
1980 if (!repo
->index
|| !repo
->index
->cache
)
1981 repo_read_index(repo
);
1982 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1985 while (pos
< repo
->index
->cache_nr
) {
1986 ce
= repo
->index
->cache
[pos
];
1987 if (ce_namelen(ce
) != namelen
||
1988 memcmp(ce
->name
, cp
, namelen
))
1990 if (ce_stage(ce
) == stage
) {
1992 if (reject_tree_in_index(repo
, only_to_die
, ce
,
1995 oidcpy(oid
, &ce
->oid
);
1996 oc
->mode
= ce
->ce_mode
;
2001 if (only_to_die
&& name
[1] && name
[1] != '/')
2002 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
2006 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2009 else if (bracket_depth
&& *cp
== '}')
2011 else if (!bracket_depth
&& *cp
== ':')
2015 struct object_id tree_oid
;
2016 int len
= cp
- name
;
2017 unsigned sub_flags
= flags
;
2019 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2020 sub_flags
|= GET_OID_TREEISH
;
2022 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2023 const char *filename
= cp
+1;
2024 char *new_filename
= NULL
;
2026 new_filename
= resolve_relative_path(repo
, filename
);
2028 filename
= new_filename
;
2029 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2030 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2031 filename
, oid
, &oc
->symlink_path
,
2034 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2036 if (ret
&& only_to_die
) {
2037 diagnose_invalid_oid_path(repo
, prefix
,
2043 if (flags
& GET_OID_RECORD_PATH
)
2044 oc
->path
= xstrdup(filename
);
2050 die(_("invalid object name '%.*s'."), len
, name
);
2057 * Call this function when you know "name" given by the end user must
2058 * name an object but it doesn't; the function _may_ die with a better
2059 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2060 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2061 * you have a chance to diagnose the error further.
2063 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2067 struct object_context oc
;
2068 struct object_id oid
;
2069 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2073 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2076 struct object_id
*oid
,
2077 struct object_context
*oc
)
2079 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2080 BUG("incompatible flags for get_oid_with_context");
2081 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);