11 #include "sha1-array.h"
13 #include "object-store.h"
14 #include "repository.h"
16 #include "commit-reach.h"
18 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
20 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
22 struct disambiguate_state
{
23 int len
; /* length of prefix in hex chars */
24 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
25 struct object_id bin_pfx
;
27 struct repository
*repo
;
28 disambiguate_hint_fn fn
;
30 struct object_id candidate
;
31 unsigned candidate_exists
:1;
32 unsigned candidate_checked
:1;
33 unsigned candidate_ok
:1;
34 unsigned disambiguate_fn_used
:1;
36 unsigned always_call_fn
:1;
39 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
41 if (ds
->always_call_fn
) {
42 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
45 if (!ds
->candidate_exists
) {
46 /* this is the first candidate */
47 oidcpy(&ds
->candidate
, current
);
48 ds
->candidate_exists
= 1;
50 } else if (oideq(&ds
->candidate
, current
)) {
51 /* the same as what we already have seen */
56 /* cannot disambiguate between ds->candidate and current */
61 if (!ds
->candidate_checked
) {
62 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
63 ds
->disambiguate_fn_used
= 1;
64 ds
->candidate_checked
= 1;
67 if (!ds
->candidate_ok
) {
68 /* discard the candidate; we know it does not satisfy fn */
69 oidcpy(&ds
->candidate
, current
);
70 ds
->candidate_checked
= 0;
74 /* if we reach this point, we know ds->candidate satisfies fn */
75 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
77 * if both current and candidate satisfy fn, we cannot
84 /* otherwise, current can be discarded and candidate is still good */
87 static int match_sha(unsigned, const unsigned char *, const unsigned char *);
89 static void find_short_object_filename(struct disambiguate_state
*ds
)
91 struct object_directory
*odb
;
93 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
) {
95 struct oid_array
*loose_objects
;
97 loose_objects
= odb_loose_cache(odb
, &ds
->bin_pfx
);
98 pos
= oid_array_lookup(loose_objects
, &ds
->bin_pfx
);
101 while (!ds
->ambiguous
&& pos
< loose_objects
->nr
) {
102 const struct object_id
*oid
;
103 oid
= loose_objects
->oid
+ pos
;
104 if (!match_sha(ds
->len
, ds
->bin_pfx
.hash
, oid
->hash
))
106 update_candidates(ds
, oid
);
112 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
122 if ((*a
^ *b
) & 0xf0)
127 static void unique_in_midx(struct multi_pack_index
*m
,
128 struct disambiguate_state
*ds
)
130 uint32_t num
, i
, first
= 0;
131 const struct object_id
*current
= NULL
;
132 num
= m
->num_objects
;
137 bsearch_midx(&ds
->bin_pfx
, m
, &first
);
140 * At this point, "first" is the location of the lowest object
141 * with an object name that could match "bin_pfx". See if we have
142 * 0, 1 or more objects that actually match(es).
144 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
145 struct object_id oid
;
146 current
= nth_midxed_object_oid(&oid
, m
, i
);
147 if (!match_sha(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
149 update_candidates(ds
, current
);
153 static void unique_in_pack(struct packed_git
*p
,
154 struct disambiguate_state
*ds
)
156 uint32_t num
, i
, first
= 0;
157 const struct object_id
*current
= NULL
;
159 if (open_pack_index(p
) || !p
->num_objects
)
162 num
= p
->num_objects
;
163 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
166 * At this point, "first" is the location of the lowest object
167 * with an object name that could match "bin_pfx". See if we have
168 * 0, 1 or more objects that actually match(es).
170 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
171 struct object_id oid
;
172 current
= nth_packed_object_oid(&oid
, p
, i
);
173 if (!match_sha(ds
->len
, ds
->bin_pfx
.hash
, current
->hash
))
175 update_candidates(ds
, current
);
179 static void find_short_packed_object(struct disambiguate_state
*ds
)
181 struct multi_pack_index
*m
;
182 struct packed_git
*p
;
184 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
186 unique_in_midx(m
, ds
);
187 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
189 unique_in_pack(p
, ds
);
192 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
193 struct object_id
*oid
)
196 return SHORT_NAME_AMBIGUOUS
;
198 if (!ds
->candidate_exists
)
199 return MISSING_OBJECT
;
201 if (!ds
->candidate_checked
)
203 * If this is the only candidate, there is no point
204 * calling the disambiguation hint callback.
206 * On the other hand, if the current candidate
207 * replaced an earlier candidate that did _not_ pass
208 * the disambiguation hint callback, then we do have
209 * more than one objects that match the short name
210 * given, so we should make sure this one matches;
211 * otherwise, if we discovered this one and the one
212 * that we previously discarded in the reverse order,
213 * we would end up showing different results in the
216 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
217 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
219 if (!ds
->candidate_ok
)
220 return SHORT_NAME_AMBIGUOUS
;
222 oidcpy(oid
, &ds
->candidate
);
226 static int disambiguate_commit_only(struct repository
*r
,
227 const struct object_id
*oid
,
228 void *cb_data_unused
)
230 int kind
= oid_object_info(r
, oid
, NULL
);
231 return kind
== OBJ_COMMIT
;
234 static int disambiguate_committish_only(struct repository
*r
,
235 const struct object_id
*oid
,
236 void *cb_data_unused
)
241 kind
= oid_object_info(r
, oid
, NULL
);
242 if (kind
== OBJ_COMMIT
)
247 /* We need to do this the hard way... */
248 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
249 if (obj
&& obj
->type
== OBJ_COMMIT
)
254 static int disambiguate_tree_only(struct repository
*r
,
255 const struct object_id
*oid
,
256 void *cb_data_unused
)
258 int kind
= oid_object_info(r
, oid
, NULL
);
259 return kind
== OBJ_TREE
;
262 static int disambiguate_treeish_only(struct repository
*r
,
263 const struct object_id
*oid
,
264 void *cb_data_unused
)
269 kind
= oid_object_info(r
, oid
, NULL
);
270 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
275 /* We need to do this the hard way... */
276 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
277 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
282 static int disambiguate_blob_only(struct repository
*r
,
283 const struct object_id
*oid
,
284 void *cb_data_unused
)
286 int kind
= oid_object_info(r
, oid
, NULL
);
287 return kind
== OBJ_BLOB
;
290 static disambiguate_hint_fn default_disambiguate_hint
;
292 int set_disambiguate_hint_config(const char *var
, const char *value
)
294 static const struct {
296 disambiguate_hint_fn fn
;
299 { "commit", disambiguate_commit_only
},
300 { "committish", disambiguate_committish_only
},
301 { "tree", disambiguate_tree_only
},
302 { "treeish", disambiguate_treeish_only
},
303 { "blob", disambiguate_blob_only
}
308 return config_error_nonbool(var
);
310 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
311 if (!strcasecmp(value
, hints
[i
].name
)) {
312 default_disambiguate_hint
= hints
[i
].fn
;
317 return error("unknown hint type for '%s': %s", var
, value
);
320 static int init_object_disambiguation(struct repository
*r
,
321 const char *name
, int len
,
322 struct disambiguate_state
*ds
)
326 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
329 memset(ds
, 0, sizeof(*ds
));
331 for (i
= 0; i
< len
;i
++) {
332 unsigned char c
= name
[i
];
334 if (c
>= '0' && c
<= '9')
336 else if (c
>= 'a' && c
<= 'f')
338 else if (c
>= 'A' && c
<='F') {
347 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
351 ds
->hex_pfx
[len
] = '\0';
357 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
359 const struct disambiguate_state
*ds
= data
;
360 struct strbuf desc
= STRBUF_INIT
;
363 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
366 type
= oid_object_info(ds
->repo
, oid
, NULL
);
367 if (type
== OBJ_COMMIT
) {
368 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
370 struct pretty_print_context pp
= {0};
371 pp
.date_mode
.type
= DATE_SHORT
;
372 format_commit_message(commit
, " %ad - %s", &desc
, &pp
);
374 } else if (type
== OBJ_TAG
) {
375 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
376 if (!parse_tag(tag
) && tag
->tag
)
377 strbuf_addf(&desc
, " %s", tag
->tag
);
381 repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
),
382 type_name(type
) ? type_name(type
) : "unknown type",
385 strbuf_release(&desc
);
389 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
391 oid_array_append(data
, oid
);
395 static int repo_collect_ambiguous(struct repository
*r
,
396 const struct object_id
*oid
,
399 return collect_ambiguous(oid
, data
);
402 static struct repository
*sort_ambiguous_repo
;
403 static int sort_ambiguous(const void *a
, const void *b
)
405 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
406 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
411 * Sorts by hash within the same object type, just as
412 * oid_array_for_each_unique() would do.
414 if (a_type
== b_type
)
418 * Between object types show tags, then commits, and finally
421 * The object_type enum is commit, tree, blob, tag, but we
422 * want tag, commit, tree blob. Cleverly (perhaps too
423 * cleverly) do that with modulus, since the enum assigns 1 to
424 * commit, so tag becomes 0.
426 a_type_sort
= a_type
% 4;
427 b_type_sort
= b_type
% 4;
428 return a_type_sort
> b_type_sort
? 1 : -1;
431 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
433 /* mutex will be needed if this code is to be made thread safe */
434 sort_ambiguous_repo
= r
;
435 QSORT(a
->oid
, a
->nr
, sort_ambiguous
);
436 sort_ambiguous_repo
= NULL
;
439 static enum get_oid_result
get_short_oid(struct repository
*r
,
440 const char *name
, int len
,
441 struct object_id
*oid
,
445 struct disambiguate_state ds
;
446 int quietly
= !!(flags
& GET_OID_QUIETLY
);
448 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
451 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
452 BUG("multiple get_short_oid disambiguator flags");
454 if (flags
& GET_OID_COMMIT
)
455 ds
.fn
= disambiguate_commit_only
;
456 else if (flags
& GET_OID_COMMITTISH
)
457 ds
.fn
= disambiguate_committish_only
;
458 else if (flags
& GET_OID_TREE
)
459 ds
.fn
= disambiguate_tree_only
;
460 else if (flags
& GET_OID_TREEISH
)
461 ds
.fn
= disambiguate_treeish_only
;
462 else if (flags
& GET_OID_BLOB
)
463 ds
.fn
= disambiguate_blob_only
;
465 ds
.fn
= default_disambiguate_hint
;
467 find_short_object_filename(&ds
);
468 find_short_packed_object(&ds
);
469 status
= finish_object_disambiguation(&ds
, oid
);
471 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
472 struct oid_array collect
= OID_ARRAY_INIT
;
474 error(_("short SHA1 %s is ambiguous"), ds
.hex_pfx
);
477 * We may still have ambiguity if we simply saw a series of
478 * candidates that did not satisfy our hint function. In
479 * that case, we still want to show them, so disable the hint
485 advise(_("The candidates are:"));
486 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
487 sort_ambiguous_oid_array(r
, &collect
);
489 if (oid_array_for_each(&collect
, show_ambiguous_object
, &ds
))
490 BUG("show_ambiguous_object shouldn't return non-zero");
491 oid_array_clear(&collect
);
497 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
498 each_abbrev_fn fn
, void *cb_data
)
500 struct oid_array collect
= OID_ARRAY_INIT
;
501 struct disambiguate_state ds
;
504 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
507 ds
.always_call_fn
= 1;
508 ds
.fn
= repo_collect_ambiguous
;
509 ds
.cb_data
= &collect
;
510 find_short_object_filename(&ds
);
511 find_short_packed_object(&ds
);
513 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
514 oid_array_clear(&collect
);
519 * Return the slot of the most-significant bit set in "val". There are various
520 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
521 * probably not a big deal here.
523 static unsigned msb(unsigned long val
)
531 struct min_abbrev_data
{
532 unsigned int init_len
;
533 unsigned int cur_len
;
535 struct repository
*repo
;
536 const struct object_id
*oid
;
539 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
542 static const char hex
[] = "0123456789abcdef";
545 return hex
[oid
->hash
[pos
>> 1] >> 4];
547 return hex
[oid
->hash
[pos
>> 1] & 0xf];
550 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
552 struct min_abbrev_data
*mad
= cb_data
;
554 unsigned int i
= mad
->init_len
;
555 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
558 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
559 mad
->cur_len
= i
+ 1;
564 static int repo_extend_abbrev_len(struct repository
*r
,
565 const struct object_id
*oid
,
568 return extend_abbrev_len(oid
, cb_data
);
571 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
572 struct min_abbrev_data
*mad
)
575 uint32_t num
, first
= 0;
576 struct object_id oid
;
577 const struct object_id
*mad_oid
;
582 num
= m
->num_objects
;
584 match
= bsearch_midx(mad_oid
, m
, &first
);
587 * first is now the position in the packfile where we would insert
588 * mad->hash if it does not exist (or the position of mad->hash if
589 * it does exist). Hence, we consider a maximum of two objects
590 * nearby for the abbreviation length.
594 if (nth_midxed_object_oid(&oid
, m
, first
))
595 extend_abbrev_len(&oid
, mad
);
596 } else if (first
< num
- 1) {
597 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
598 extend_abbrev_len(&oid
, mad
);
601 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
602 extend_abbrev_len(&oid
, mad
);
604 mad
->init_len
= mad
->cur_len
;
607 static void find_abbrev_len_for_pack(struct packed_git
*p
,
608 struct min_abbrev_data
*mad
)
611 uint32_t num
, first
= 0;
612 struct object_id oid
;
613 const struct object_id
*mad_oid
;
615 if (open_pack_index(p
) || !p
->num_objects
)
618 num
= p
->num_objects
;
620 match
= bsearch_pack(mad_oid
, p
, &first
);
623 * first is now the position in the packfile where we would insert
624 * mad->hash if it does not exist (or the position of mad->hash if
625 * it does exist). Hence, we consider a maximum of two objects
626 * nearby for the abbreviation length.
630 if (nth_packed_object_oid(&oid
, p
, first
))
631 extend_abbrev_len(&oid
, mad
);
632 } else if (first
< num
- 1) {
633 if (nth_packed_object_oid(&oid
, p
, first
+ 1))
634 extend_abbrev_len(&oid
, mad
);
637 if (nth_packed_object_oid(&oid
, p
, first
- 1))
638 extend_abbrev_len(&oid
, mad
);
640 mad
->init_len
= mad
->cur_len
;
643 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
645 struct multi_pack_index
*m
;
646 struct packed_git
*p
;
648 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
649 find_abbrev_len_for_midx(m
, mad
);
650 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
651 find_abbrev_len_for_pack(p
, mad
);
654 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
655 const struct object_id
*oid
, int len
)
657 struct disambiguate_state ds
;
658 struct min_abbrev_data mad
;
659 struct object_id oid_ret
;
660 const unsigned hexsz
= r
->hash_algo
->hexsz
;
663 unsigned long count
= repo_approximate_object_count(r
);
665 * Add one because the MSB only tells us the highest bit set,
666 * not including the value of all the _other_ bits (so "15"
667 * is only one off of 2^4, but the MSB is the 3rd bit.
669 len
= msb(count
) + 1;
671 * We now know we have on the order of 2^len objects, which
672 * expects a collision at 2^(len/2). But we also care about hex
673 * chars, not bits, and there are 4 bits per hex. So all
674 * together we need to divide by 2 and round up.
676 len
= DIV_ROUND_UP(len
, 2);
678 * For very small repos, we stick with our regular fallback.
680 if (len
< FALLBACK_DEFAULT_ABBREV
)
681 len
= FALLBACK_DEFAULT_ABBREV
;
684 oid_to_hex_r(hex
, oid
);
685 if (len
== hexsz
|| !len
)
694 find_abbrev_len_packed(&mad
);
696 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
699 ds
.fn
= repo_extend_abbrev_len
;
700 ds
.always_call_fn
= 1;
701 ds
.cb_data
= (void *)&mad
;
703 find_short_object_filename(&ds
);
704 (void)finish_object_disambiguation(&ds
, &oid_ret
);
706 hex
[mad
.cur_len
] = 0;
710 const char *repo_find_unique_abbrev(struct repository
*r
,
711 const struct object_id
*oid
,
715 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
716 char *hex
= hexbuffer
[bufno
];
717 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
718 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
722 static int ambiguous_path(const char *path
, int len
)
727 for (cnt
= 0; cnt
< len
; cnt
++) {
747 static inline int at_mark(const char *string
, int len
,
748 const char **suffix
, int nr
)
752 for (i
= 0; i
< nr
; i
++) {
753 int suffix_len
= strlen(suffix
[i
]);
754 if (suffix_len
<= len
755 && !strncasecmp(string
, suffix
[i
], suffix_len
))
761 static inline int upstream_mark(const char *string
, int len
)
763 const char *suffix
[] = { "@{upstream}", "@{u}" };
764 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
767 static inline int push_mark(const char *string
, int len
)
769 const char *suffix
[] = { "@{push}" };
770 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
773 static enum get_oid_result
get_oid_1(const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
774 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
776 static int get_oid_basic(const char *str
, int len
, struct object_id
*oid
,
779 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
780 static const char *object_name_msg
= N_(
781 "Git normally never creates a ref that ends with 40 hex characters\n"
782 "because it will be ignored when you just specify 40-hex. These refs\n"
783 "may be created by mistake. For example,\n"
785 " git checkout -b $br $(git rev-parse ...)\n"
787 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
788 "examine these refs and maybe delete them. Turn this message off by\n"
789 "running \"git config advice.objectNameWarning false\"");
790 struct object_id tmp_oid
;
791 char *real_ref
= NULL
;
793 int at
, reflog_len
, nth_prior
= 0;
795 if (len
== the_hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
796 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
797 refs_found
= dwim_ref(str
, len
, &tmp_oid
, &real_ref
);
798 if (refs_found
> 0) {
799 warning(warn_msg
, len
, str
);
800 if (advice_object_name_warning
)
801 fprintf(stderr
, "%s\n", _(object_name_msg
));
808 /* basic@{time or number or -number} format to query ref-log */
810 if (len
&& str
[len
-1] == '}') {
811 for (at
= len
-4; at
>= 0; at
--) {
812 if (str
[at
] == '@' && str
[at
+1] == '{') {
813 if (str
[at
+2] == '-') {
815 /* @{-N} not at start */
820 if (!upstream_mark(str
+ at
, len
- at
) &&
821 !push_mark(str
+ at
, len
- at
)) {
822 reflog_len
= (len
-1) - (at
+2);
830 /* Accept only unambiguous ref paths. */
831 if (len
&& ambiguous_path(str
, len
))
835 struct strbuf buf
= STRBUF_INIT
;
838 if (interpret_nth_prior_checkout(the_repository
, str
, len
, &buf
) > 0) {
839 detached
= (buf
.len
== the_hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
840 strbuf_release(&buf
);
846 if (!len
&& reflog_len
)
847 /* allow "@{...}" to mean the current branch reflog */
848 refs_found
= dwim_ref("HEAD", 4, oid
, &real_ref
);
850 refs_found
= dwim_log(str
, len
, oid
, &real_ref
);
852 refs_found
= dwim_ref(str
, len
, oid
, &real_ref
);
857 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
859 !get_short_oid(the_repository
,
860 str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
861 warning(warn_msg
, len
, str
);
869 /* Is it asking for N-th entry, or approxidate? */
870 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
871 char ch
= str
[at
+2+i
];
872 if ('0' <= ch
&& ch
<= '9')
873 nth
= nth
* 10 + ch
- '0';
877 if (100000000 <= nth
) {
884 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
885 at_time
= approxidate_careful(tmp
, &errors
);
892 if (read_ref_at(get_main_ref_store(the_repository
),
893 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
894 &co_time
, &co_tz
, &co_cnt
)) {
896 if (starts_with(real_ref
, "refs/heads/")) {
898 len
= strlen(real_ref
+ 11);
906 if (!(flags
& GET_OID_QUIETLY
)) {
907 warning("Log for '%.*s' only goes "
908 "back to %s.", len
, str
,
909 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
912 if (flags
& GET_OID_QUIETLY
) {
915 die("Log for '%.*s' only has %d entries.",
925 static enum get_oid_result
get_parent(const char *name
, int len
,
926 struct object_id
*result
, int idx
)
928 struct object_id oid
;
929 enum get_oid_result ret
= get_oid_1(name
, len
, &oid
,
931 struct commit
*commit
;
932 struct commit_list
*p
;
936 commit
= lookup_commit_reference(the_repository
, &oid
);
937 if (parse_commit(commit
))
938 return MISSING_OBJECT
;
940 oidcpy(result
, &commit
->object
.oid
);
946 oidcpy(result
, &p
->item
->object
.oid
);
951 return MISSING_OBJECT
;
954 static enum get_oid_result
get_nth_ancestor(const char *name
, int len
,
955 struct object_id
*result
,
958 struct object_id oid
;
959 struct commit
*commit
;
962 ret
= get_oid_1(name
, len
, &oid
, GET_OID_COMMITTISH
);
965 commit
= lookup_commit_reference(the_repository
, &oid
);
967 return MISSING_OBJECT
;
969 while (generation
--) {
970 if (parse_commit(commit
) || !commit
->parents
)
971 return MISSING_OBJECT
;
972 commit
= commit
->parents
->item
;
974 oidcpy(result
, &commit
->object
.oid
);
978 struct object
*peel_to_type(const char *name
, int namelen
,
979 struct object
*o
, enum object_type expected_type
)
981 if (name
&& !namelen
)
982 namelen
= strlen(name
);
984 if (!o
|| (!o
->parsed
&& !parse_object(the_repository
, &o
->oid
)))
986 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
988 if (o
->type
== OBJ_TAG
)
989 o
= ((struct tag
*) o
)->tagged
;
990 else if (o
->type
== OBJ_COMMIT
)
991 o
= &(get_commit_tree(((struct commit
*)o
))->object
);
994 error("%.*s: expected %s type, but the object "
995 "dereferences to %s type",
996 namelen
, name
, type_name(expected_type
),
1003 static int peel_onion(const char *name
, int len
, struct object_id
*oid
,
1004 unsigned lookup_flags
)
1006 struct object_id outer
;
1008 unsigned int expected_type
= 0;
1012 * "ref^{type}" dereferences ref repeatedly until you cannot
1013 * dereference anymore, or you get an object of given type,
1014 * whichever comes first. "ref^{}" means just dereference
1015 * tags until you get a non-tag. "ref^0" is a shorthand for
1016 * "ref^{commit}". "commit^{tree}" could be used to find the
1017 * top-level tree of the given commit.
1019 if (len
< 4 || name
[len
-1] != '}')
1022 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1024 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1030 sp
++; /* beginning of type name, or closing brace for empty */
1031 if (starts_with(sp
, "commit}"))
1032 expected_type
= OBJ_COMMIT
;
1033 else if (starts_with(sp
, "tag}"))
1034 expected_type
= OBJ_TAG
;
1035 else if (starts_with(sp
, "tree}"))
1036 expected_type
= OBJ_TREE
;
1037 else if (starts_with(sp
, "blob}"))
1038 expected_type
= OBJ_BLOB
;
1039 else if (starts_with(sp
, "object}"))
1040 expected_type
= OBJ_ANY
;
1041 else if (sp
[0] == '}')
1042 expected_type
= OBJ_NONE
;
1043 else if (sp
[0] == '/')
1044 expected_type
= OBJ_COMMIT
;
1048 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1049 if (expected_type
== OBJ_COMMIT
)
1050 lookup_flags
|= GET_OID_COMMITTISH
;
1051 else if (expected_type
== OBJ_TREE
)
1052 lookup_flags
|= GET_OID_TREEISH
;
1054 if (get_oid_1(name
, sp
- name
- 2, &outer
, lookup_flags
))
1057 o
= parse_object(the_repository
, &outer
);
1060 if (!expected_type
) {
1061 o
= deref_tag(the_repository
, o
, name
, sp
- name
- 2);
1062 if (!o
|| (!o
->parsed
&& !parse_object(the_repository
, &o
->oid
)))
1064 oidcpy(oid
, &o
->oid
);
1069 * At this point, the syntax look correct, so
1070 * if we do not get the needed object, we should
1073 o
= peel_to_type(name
, len
, o
, expected_type
);
1077 oidcpy(oid
, &o
->oid
);
1079 /* "$commit^{/foo}" */
1082 struct commit_list
*list
= NULL
;
1085 * $commit^{/}. Some regex implementation may reject.
1086 * We don't need regex anyway. '' pattern always matches.
1091 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1092 commit_list_insert((struct commit
*)o
, &list
);
1093 ret
= get_oid_oneline(the_repository
, prefix
, oid
, list
);
1100 static int get_describe_name(struct repository
*r
,
1101 const char *name
, int len
,
1102 struct object_id
*oid
)
1105 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1107 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1109 if (!isxdigit(ch
)) {
1110 /* We must be looking at g in "SOMETHING-g"
1111 * for it to be describe output.
1113 if (ch
== 'g' && cp
[-1] == '-') {
1116 return get_short_oid(r
,
1117 cp
, len
, oid
, flags
);
1124 static enum get_oid_result
get_oid_1(const char *name
, int len
,
1125 struct object_id
*oid
,
1126 unsigned lookup_flags
)
1128 int ret
, has_suffix
;
1132 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1135 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1137 if ('0' <= ch
&& ch
<= '9')
1139 if (ch
== '~' || ch
== '^')
1146 int len1
= cp
- name
;
1148 while (cp
< name
+ len
)
1149 num
= num
* 10 + *cp
++ - '0';
1150 if (!num
&& len1
== len
- 1)
1152 if (has_suffix
== '^')
1153 return get_parent(name
, len1
, oid
, num
);
1154 /* else if (has_suffix == '~') -- goes without saying */
1155 return get_nth_ancestor(name
, len1
, oid
, num
);
1158 ret
= peel_onion(name
, len
, oid
, lookup_flags
);
1162 ret
= get_oid_basic(name
, len
, oid
, lookup_flags
);
1166 /* It could be describe output that is "SOMETHING-gXXXX" */
1167 ret
= get_describe_name(the_repository
, name
, len
, oid
);
1171 return get_short_oid(the_repository
, name
, len
, oid
, lookup_flags
);
1175 * This interprets names like ':/Initial revision of "git"' by searching
1176 * through history and returning the first commit whose message starts
1177 * the given regular expression.
1179 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1181 * For a literal '!' character at the beginning of a pattern, you have to repeat
1182 * that, like: ':/!!foo'
1184 * For future extension, all other sequences beginning with ':/!' are reserved.
1187 /* Remember to update object flag allocation in object.h */
1188 #define ONELINE_SEEN (1u<<20)
1190 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1191 int flag
, void *cb_data
)
1193 struct commit_list
**list
= cb_data
;
1194 struct object
*object
= parse_object(the_repository
, oid
);
1197 if (object
->type
== OBJ_TAG
) {
1198 object
= deref_tag(the_repository
, object
, path
,
1203 if (object
->type
!= OBJ_COMMIT
)
1205 commit_list_insert((struct commit
*)object
, list
);
1209 static int get_oid_oneline(struct repository
*r
,
1210 const char *prefix
, struct object_id
*oid
,
1211 struct commit_list
*list
)
1213 struct commit_list
*backup
= NULL
, *l
;
1218 if (prefix
[0] == '!') {
1221 if (prefix
[0] == '-') {
1224 } else if (prefix
[0] != '!') {
1229 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1232 for (l
= list
; l
; l
= l
->next
) {
1233 l
->item
->object
.flags
|= ONELINE_SEEN
;
1234 commit_list_insert(l
->item
, &backup
);
1237 const char *p
, *buf
;
1238 struct commit
*commit
;
1241 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1242 if (!parse_object(r
, &commit
->object
.oid
))
1244 buf
= get_commit_buffer(commit
, NULL
);
1245 p
= strstr(buf
, "\n\n");
1246 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1247 unuse_commit_buffer(commit
, buf
);
1250 oidcpy(oid
, &commit
->object
.oid
);
1256 free_commit_list(list
);
1257 for (l
= backup
; l
; l
= l
->next
)
1258 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1259 free_commit_list(backup
);
1260 return found
? 0 : -1;
1263 struct grab_nth_branch_switch_cbdata
{
1268 static int grab_nth_branch_switch(struct object_id
*ooid
, struct object_id
*noid
,
1269 const char *email
, timestamp_t timestamp
, int tz
,
1270 const char *message
, void *cb_data
)
1272 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1273 const char *match
= NULL
, *target
= NULL
;
1276 if (skip_prefix(message
, "checkout: moving from ", &match
))
1277 target
= strstr(match
, " to ");
1279 if (!match
|| !target
)
1281 if (--(cb
->remaining
) == 0) {
1282 len
= target
- match
;
1283 strbuf_reset(&cb
->buf
);
1284 strbuf_add(&cb
->buf
, match
, len
);
1285 return 1; /* we are done */
1291 * Parse @{-N} syntax, return the number of characters parsed
1292 * if successful; otherwise signal an error with negative value.
1294 static int interpret_nth_prior_checkout(struct repository
*r
,
1295 const char *name
, int namelen
,
1300 struct grab_nth_branch_switch_cbdata cb
;
1306 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1308 brace
= memchr(name
, '}', namelen
);
1311 nth
= strtol(name
+ 3, &num_end
, 10);
1312 if (num_end
!= brace
)
1317 strbuf_init(&cb
.buf
, 20);
1319 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1320 "HEAD", grab_nth_branch_switch
, &cb
);
1323 strbuf_addbuf(buf
, &cb
.buf
);
1324 retval
= brace
- name
+ 1;
1328 strbuf_release(&cb
.buf
);
1332 int get_oid_mb(const char *name
, struct object_id
*oid
)
1334 struct commit
*one
, *two
;
1335 struct commit_list
*mbs
;
1336 struct object_id oid_tmp
;
1340 dots
= strstr(name
, "...");
1342 return get_oid(name
, oid
);
1344 st
= get_oid("HEAD", &oid_tmp
);
1347 strbuf_init(&sb
, dots
- name
);
1348 strbuf_add(&sb
, name
, dots
- name
);
1349 st
= get_oid_committish(sb
.buf
, &oid_tmp
);
1350 strbuf_release(&sb
);
1354 one
= lookup_commit_reference_gently(the_repository
, &oid_tmp
, 0);
1358 if (get_oid_committish(dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1360 two
= lookup_commit_reference_gently(the_repository
, &oid_tmp
, 0);
1363 mbs
= get_merge_bases(one
, two
);
1364 if (!mbs
|| mbs
->next
)
1368 oidcpy(oid
, &mbs
->item
->object
.oid
);
1370 free_commit_list(mbs
);
1374 /* parse @something syntax, when 'something' is not {.*} */
1375 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1379 if (len
|| name
[1] == '{')
1382 /* make sure it's a single @, or @@{.*}, not @foo */
1383 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1384 if (next
&& next
[1] != '{')
1387 next
= name
+ namelen
;
1388 if (next
!= name
+ 1)
1392 strbuf_add(buf
, "HEAD", 4);
1396 static int reinterpret(struct repository
*r
,
1397 const char *name
, int namelen
, int len
,
1398 struct strbuf
*buf
, unsigned allowed
)
1400 /* we have extra data, which might need further processing */
1401 struct strbuf tmp
= STRBUF_INIT
;
1402 int used
= buf
->len
;
1405 strbuf_add(buf
, name
+ len
, namelen
- len
);
1406 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, allowed
);
1407 /* that data was not interpreted, remove our cruft */
1409 strbuf_setlen(buf
, used
);
1413 strbuf_addbuf(buf
, &tmp
);
1414 strbuf_release(&tmp
);
1415 /* tweak for size of {-N} versus expanded ref name */
1416 return ret
- used
+ len
;
1419 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1421 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1423 strbuf_addstr(buf
, s
);
1427 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1432 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1433 starts_with(refname
, "refs/heads/"))
1435 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1436 starts_with(refname
, "refs/remotes/"))
1442 static int interpret_branch_mark(struct repository
*r
,
1443 const char *name
, int namelen
,
1444 int at
, struct strbuf
*buf
,
1445 int (*get_mark
)(const char *, int),
1446 const char *(*get_data
)(struct branch
*,
1451 struct branch
*branch
;
1452 struct strbuf err
= STRBUF_INIT
;
1455 len
= get_mark(name
+ at
, namelen
- at
);
1459 if (memchr(name
, ':', at
))
1463 char *name_str
= xmemdupz(name
, at
);
1464 branch
= branch_get(name_str
);
1467 branch
= branch_get(NULL
);
1469 value
= get_data(branch
, &err
);
1473 if (!branch_interpret_allowed(value
, allowed
))
1476 set_shortened_ref(r
, buf
, value
);
1480 int repo_interpret_branch_name(struct repository
*r
,
1481 const char *name
, int namelen
,
1490 namelen
= strlen(name
);
1492 if (!allowed
|| (allowed
& INTERPRET_BRANCH_LOCAL
)) {
1493 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1495 return len
; /* syntax Ok, not enough switches */
1496 } else if (len
> 0) {
1498 return len
; /* consumed all */
1500 return reinterpret(r
, name
, namelen
, len
, buf
, allowed
);
1505 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1508 if (!allowed
|| (allowed
& INTERPRET_BRANCH_HEAD
)) {
1509 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1511 return reinterpret(r
, name
, namelen
, len
, buf
,
1515 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1516 upstream_mark
, branch_get_upstream
,
1521 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1522 push_mark
, branch_get_push
,
1531 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1533 int len
= strlen(name
);
1534 int used
= interpret_branch_name(name
, len
, sb
, allowed
);
1538 strbuf_add(sb
, name
+ used
, len
- used
);
1541 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1543 if (startup_info
->have_repository
)
1544 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1546 strbuf_addstr(sb
, name
);
1549 * This splice must be done even if we end up rejecting the
1550 * name; builtin/branch.c::copy_or_rename_branch() still wants
1551 * to see what the name expanded to so that "branch -m" can be
1552 * used as a tool to correct earlier mistakes.
1554 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1557 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1560 return check_refname_format(sb
->buf
, 0);
1564 * This is like "get_oid_basic()", except it allows "object ID expressions",
1565 * notably "xyz^" for "parent of xyz"
1567 int get_oid(const char *name
, struct object_id
*oid
)
1569 struct object_context unused
;
1570 return get_oid_with_context(the_repository
, name
, 0, oid
, &unused
);
1575 * Many callers know that the user meant to name a commit-ish by
1576 * syntactical positions where the object name appears. Calling this
1577 * function allows the machinery to disambiguate shorter-than-unique
1578 * abbreviated object names between commit-ish and others.
1580 * Note that this does NOT error out when the named object is not a
1581 * commit-ish. It is merely to give a hint to the disambiguation
1584 int get_oid_committish(const char *name
, struct object_id
*oid
)
1586 struct object_context unused
;
1587 return get_oid_with_context(the_repository
,
1588 name
, GET_OID_COMMITTISH
,
1592 int get_oid_treeish(const char *name
, struct object_id
*oid
)
1594 struct object_context unused
;
1595 return get_oid_with_context(the_repository
,
1596 name
, GET_OID_TREEISH
,
1600 int get_oid_commit(const char *name
, struct object_id
*oid
)
1602 struct object_context unused
;
1603 return get_oid_with_context(the_repository
,
1604 name
, GET_OID_COMMIT
,
1608 int get_oid_tree(const char *name
, struct object_id
*oid
)
1610 struct object_context unused
;
1611 return get_oid_with_context(the_repository
,
1616 int get_oid_blob(const char *name
, struct object_id
*oid
)
1618 struct object_context unused
;
1619 return get_oid_with_context(the_repository
,
1624 /* Must be called only when object_name:filename doesn't exist. */
1625 static void diagnose_invalid_oid_path(const char *prefix
,
1626 const char *filename
,
1627 const struct object_id
*tree_oid
,
1628 const char *object_name
,
1629 int object_name_len
)
1631 struct object_id oid
;
1637 if (file_exists(filename
))
1638 die("Path '%s' exists on disk, but not in '%.*s'.",
1639 filename
, object_name_len
, object_name
);
1640 if (is_missing_file_error(errno
)) {
1641 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1643 if (!get_tree_entry(tree_oid
, fullname
, &oid
, &mode
)) {
1644 die("Path '%s' exists, but not '%s'.\n"
1645 "Did you mean '%.*s:%s' aka '%.*s:./%s'?",
1648 object_name_len
, object_name
,
1650 object_name_len
, object_name
,
1653 die("Path '%s' does not exist in '%.*s'",
1654 filename
, object_name_len
, object_name
);
1658 /* Must be called only when :stage:filename doesn't exist. */
1659 static void diagnose_invalid_index_path(struct index_state
*istate
,
1662 const char *filename
)
1664 const struct cache_entry
*ce
;
1666 unsigned namelen
= strlen(filename
);
1667 struct strbuf fullname
= STRBUF_INIT
;
1672 /* Wrong stage number? */
1673 pos
= index_name_pos(istate
, filename
, namelen
);
1676 if (pos
< istate
->cache_nr
) {
1677 ce
= istate
->cache
[pos
];
1678 if (ce_namelen(ce
) == namelen
&&
1679 !memcmp(ce
->name
, filename
, namelen
))
1680 die("Path '%s' is in the index, but not at stage %d.\n"
1681 "Did you mean ':%d:%s'?",
1683 ce_stage(ce
), filename
);
1686 /* Confusion between relative and absolute filenames? */
1687 strbuf_addstr(&fullname
, prefix
);
1688 strbuf_addstr(&fullname
, filename
);
1689 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1692 if (pos
< istate
->cache_nr
) {
1693 ce
= istate
->cache
[pos
];
1694 if (ce_namelen(ce
) == fullname
.len
&&
1695 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1696 die("Path '%s' is in the index, but not '%s'.\n"
1697 "Did you mean ':%d:%s' aka ':%d:./%s'?",
1698 fullname
.buf
, filename
,
1699 ce_stage(ce
), fullname
.buf
,
1700 ce_stage(ce
), filename
);
1703 if (file_exists(filename
))
1704 die("Path '%s' exists on disk, but not in the index.", filename
);
1705 if (is_missing_file_error(errno
))
1706 die("Path '%s' does not exist (neither on disk nor in the index).",
1709 strbuf_release(&fullname
);
1713 static char *resolve_relative_path(const char *rel
)
1715 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1718 if (!is_inside_work_tree())
1719 die("relative path syntax can't be used outside working tree.");
1721 /* die() inside prefix_path() if resolved path is outside worktree */
1722 return prefix_path(startup_info
->prefix
,
1723 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1727 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1731 struct object_id
*oid
,
1732 struct object_context
*oc
)
1734 int ret
, bracket_depth
;
1735 int namelen
= strlen(name
);
1737 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1740 flags
|= GET_OID_QUIETLY
;
1742 memset(oc
, 0, sizeof(*oc
));
1743 oc
->mode
= S_IFINVALID
;
1744 strbuf_init(&oc
->symlink_path
, 0);
1745 ret
= get_oid_1(name
, namelen
, oid
, flags
);
1749 * sha1:path --> object name of path in ent sha1
1750 * :path -> object name of absolute path in index
1751 * :./path -> object name of path relative to cwd in index
1752 * :[0-3]:path -> object name of path in index at stage
1753 * :/foo -> recent commit matching foo
1755 if (name
[0] == ':') {
1757 const struct cache_entry
*ce
;
1758 char *new_path
= NULL
;
1760 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1761 struct commit_list
*list
= NULL
;
1763 for_each_ref(handle_one_ref
, &list
);
1764 head_ref(handle_one_ref
, &list
);
1765 commit_list_sort_by_date(&list
);
1766 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1770 name
[1] < '0' || '3' < name
[1])
1773 stage
= name
[1] - '0';
1776 new_path
= resolve_relative_path(cp
);
1778 namelen
= namelen
- (cp
- name
);
1781 namelen
= strlen(cp
);
1784 if (flags
& GET_OID_RECORD_PATH
)
1785 oc
->path
= xstrdup(cp
);
1787 if (!repo
->index
->cache
)
1788 repo_read_index(the_repository
);
1789 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1792 while (pos
< repo
->index
->cache_nr
) {
1793 ce
= repo
->index
->cache
[pos
];
1794 if (ce_namelen(ce
) != namelen
||
1795 memcmp(ce
->name
, cp
, namelen
))
1797 if (ce_stage(ce
) == stage
) {
1798 oidcpy(oid
, &ce
->oid
);
1799 oc
->mode
= ce
->ce_mode
;
1805 if (only_to_die
&& name
[1] && name
[1] != '/')
1806 diagnose_invalid_index_path(repo
->index
, stage
, prefix
, cp
);
1810 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
1813 else if (bracket_depth
&& *cp
== '}')
1815 else if (!bracket_depth
&& *cp
== ':')
1819 struct object_id tree_oid
;
1820 int len
= cp
- name
;
1821 unsigned sub_flags
= flags
;
1823 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
1824 sub_flags
|= GET_OID_TREEISH
;
1826 if (!get_oid_1(name
, len
, &tree_oid
, sub_flags
)) {
1827 const char *filename
= cp
+1;
1828 char *new_filename
= NULL
;
1830 new_filename
= resolve_relative_path(filename
);
1832 filename
= new_filename
;
1833 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
1834 ret
= get_tree_entry_follow_symlinks(&tree_oid
,
1835 filename
, oid
, &oc
->symlink_path
,
1838 ret
= get_tree_entry(&tree_oid
, filename
, oid
,
1840 if (ret
&& only_to_die
) {
1841 diagnose_invalid_oid_path(prefix
,
1847 if (flags
& GET_OID_RECORD_PATH
)
1848 oc
->path
= xstrdup(filename
);
1854 die("Invalid object name '%.*s'.", len
, name
);
1861 * Call this function when you know "name" given by the end user must
1862 * name an object but it doesn't; the function _may_ die with a better
1863 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
1864 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
1865 * you have a chance to diagnose the error further.
1867 void maybe_die_on_misspelt_object_name(const char *name
, const char *prefix
)
1869 struct object_context oc
;
1870 struct object_id oid
;
1871 get_oid_with_context_1(the_repository
, name
, GET_OID_ONLY_TO_DIE
,
1875 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
1878 struct object_id
*oid
,
1879 struct object_context
*oc
)
1881 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
1882 BUG("incompatible flags for get_sha1_with_context");
1883 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);