3 #include "environment.h"
10 #include "tree-walk.h"
14 #include "oid-array.h"
16 #include "object-store.h"
17 #include "repository.h"
19 #include "submodule.h"
21 #include "commit-reach.h"
24 static int get_oid_oneline(struct repository
*r
, const char *, struct object_id
*, struct commit_list
*);
26 typedef int (*disambiguate_hint_fn
)(struct repository
*, const struct object_id
*, void *);
28 struct disambiguate_state
{
29 int len
; /* length of prefix in hex chars */
30 char hex_pfx
[GIT_MAX_HEXSZ
+ 1];
31 struct object_id bin_pfx
;
33 struct repository
*repo
;
34 disambiguate_hint_fn fn
;
36 struct object_id candidate
;
37 unsigned candidate_exists
:1;
38 unsigned candidate_checked
:1;
39 unsigned candidate_ok
:1;
40 unsigned disambiguate_fn_used
:1;
42 unsigned always_call_fn
:1;
45 static void update_candidates(struct disambiguate_state
*ds
, const struct object_id
*current
)
47 if (ds
->always_call_fn
) {
48 ds
->ambiguous
= ds
->fn(ds
->repo
, current
, ds
->cb_data
) ? 1 : 0;
51 if (!ds
->candidate_exists
) {
52 /* this is the first candidate */
53 oidcpy(&ds
->candidate
, current
);
54 ds
->candidate_exists
= 1;
56 } else if (oideq(&ds
->candidate
, current
)) {
57 /* the same as what we already have seen */
62 /* cannot disambiguate between ds->candidate and current */
67 if (!ds
->candidate_checked
) {
68 ds
->candidate_ok
= ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
);
69 ds
->disambiguate_fn_used
= 1;
70 ds
->candidate_checked
= 1;
73 if (!ds
->candidate_ok
) {
74 /* discard the candidate; we know it does not satisfy fn */
75 oidcpy(&ds
->candidate
, current
);
76 ds
->candidate_checked
= 0;
80 /* if we reach this point, we know ds->candidate satisfies fn */
81 if (ds
->fn(ds
->repo
, current
, ds
->cb_data
)) {
83 * if both current and candidate satisfy fn, we cannot
90 /* otherwise, current can be discarded and candidate is still good */
93 static int match_hash(unsigned, const unsigned char *, const unsigned char *);
95 static enum cb_next
match_prefix(const struct object_id
*oid
, void *arg
)
97 struct disambiguate_state
*ds
= arg
;
98 /* no need to call match_hash, oidtree_each did prefix match */
99 update_candidates(ds
, oid
);
100 return ds
->ambiguous
? CB_BREAK
: CB_CONTINUE
;
103 static void find_short_object_filename(struct disambiguate_state
*ds
)
105 struct object_directory
*odb
;
107 for (odb
= ds
->repo
->objects
->odb
; odb
&& !ds
->ambiguous
; odb
= odb
->next
)
108 oidtree_each(odb_loose_cache(odb
, &ds
->bin_pfx
),
109 &ds
->bin_pfx
, ds
->len
, match_prefix
, ds
);
112 static int match_hash(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_hash(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;
158 if (p
->multi_pack_index
)
161 if (open_pack_index(p
) || !p
->num_objects
)
164 num
= p
->num_objects
;
165 bsearch_pack(&ds
->bin_pfx
, p
, &first
);
168 * At this point, "first" is the location of the lowest object
169 * with an object name that could match "bin_pfx". See if we have
170 * 0, 1 or more objects that actually match(es).
172 for (i
= first
; i
< num
&& !ds
->ambiguous
; i
++) {
173 struct object_id oid
;
174 nth_packed_object_id(&oid
, p
, i
);
175 if (!match_hash(ds
->len
, ds
->bin_pfx
.hash
, oid
.hash
))
177 update_candidates(ds
, &oid
);
181 static void find_short_packed_object(struct disambiguate_state
*ds
)
183 struct multi_pack_index
*m
;
184 struct packed_git
*p
;
186 for (m
= get_multi_pack_index(ds
->repo
); m
&& !ds
->ambiguous
;
188 unique_in_midx(m
, ds
);
189 for (p
= get_packed_git(ds
->repo
); p
&& !ds
->ambiguous
;
191 unique_in_pack(p
, ds
);
194 static int finish_object_disambiguation(struct disambiguate_state
*ds
,
195 struct object_id
*oid
)
198 return SHORT_NAME_AMBIGUOUS
;
200 if (!ds
->candidate_exists
)
201 return MISSING_OBJECT
;
203 if (!ds
->candidate_checked
)
205 * If this is the only candidate, there is no point
206 * calling the disambiguation hint callback.
208 * On the other hand, if the current candidate
209 * replaced an earlier candidate that did _not_ pass
210 * the disambiguation hint callback, then we do have
211 * more than one objects that match the short name
212 * given, so we should make sure this one matches;
213 * otherwise, if we discovered this one and the one
214 * that we previously discarded in the reverse order,
215 * we would end up showing different results in the
218 ds
->candidate_ok
= (!ds
->disambiguate_fn_used
||
219 ds
->fn(ds
->repo
, &ds
->candidate
, ds
->cb_data
));
221 if (!ds
->candidate_ok
)
222 return SHORT_NAME_AMBIGUOUS
;
224 oidcpy(oid
, &ds
->candidate
);
228 static int disambiguate_commit_only(struct repository
*r
,
229 const struct object_id
*oid
,
230 void *cb_data UNUSED
)
232 int kind
= oid_object_info(r
, oid
, NULL
);
233 return kind
== OBJ_COMMIT
;
236 static int disambiguate_committish_only(struct repository
*r
,
237 const struct object_id
*oid
,
238 void *cb_data UNUSED
)
243 kind
= oid_object_info(r
, oid
, NULL
);
244 if (kind
== OBJ_COMMIT
)
249 /* We need to do this the hard way... */
250 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
251 if (obj
&& obj
->type
== OBJ_COMMIT
)
256 static int disambiguate_tree_only(struct repository
*r
,
257 const struct object_id
*oid
,
258 void *cb_data UNUSED
)
260 int kind
= oid_object_info(r
, oid
, NULL
);
261 return kind
== OBJ_TREE
;
264 static int disambiguate_treeish_only(struct repository
*r
,
265 const struct object_id
*oid
,
266 void *cb_data UNUSED
)
271 kind
= oid_object_info(r
, oid
, NULL
);
272 if (kind
== OBJ_TREE
|| kind
== OBJ_COMMIT
)
277 /* We need to do this the hard way... */
278 obj
= deref_tag(r
, parse_object(r
, oid
), NULL
, 0);
279 if (obj
&& (obj
->type
== OBJ_TREE
|| obj
->type
== OBJ_COMMIT
))
284 static int disambiguate_blob_only(struct repository
*r
,
285 const struct object_id
*oid
,
286 void *cb_data UNUSED
)
288 int kind
= oid_object_info(r
, oid
, NULL
);
289 return kind
== OBJ_BLOB
;
292 static disambiguate_hint_fn default_disambiguate_hint
;
294 int set_disambiguate_hint_config(const char *var
, const char *value
)
296 static const struct {
298 disambiguate_hint_fn fn
;
301 { "commit", disambiguate_commit_only
},
302 { "committish", disambiguate_committish_only
},
303 { "tree", disambiguate_tree_only
},
304 { "treeish", disambiguate_treeish_only
},
305 { "blob", disambiguate_blob_only
}
310 return config_error_nonbool(var
);
312 for (i
= 0; i
< ARRAY_SIZE(hints
); i
++) {
313 if (!strcasecmp(value
, hints
[i
].name
)) {
314 default_disambiguate_hint
= hints
[i
].fn
;
319 return error("unknown hint type for '%s': %s", var
, value
);
322 static int init_object_disambiguation(struct repository
*r
,
323 const char *name
, int len
,
324 struct disambiguate_state
*ds
)
328 if (len
< MINIMUM_ABBREV
|| len
> the_hash_algo
->hexsz
)
331 memset(ds
, 0, sizeof(*ds
));
333 for (i
= 0; i
< len
;i
++) {
334 unsigned char c
= name
[i
];
336 if (c
>= '0' && c
<= '9')
338 else if (c
>= 'a' && c
<= 'f')
340 else if (c
>= 'A' && c
<='F') {
349 ds
->bin_pfx
.hash
[i
>> 1] |= val
;
353 ds
->hex_pfx
[len
] = '\0';
359 struct ambiguous_output
{
360 const struct disambiguate_state
*ds
;
361 struct strbuf advice
;
365 static int show_ambiguous_object(const struct object_id
*oid
, void *data
)
367 struct ambiguous_output
*state
= data
;
368 const struct disambiguate_state
*ds
= state
->ds
;
369 struct strbuf
*advice
= &state
->advice
;
370 struct strbuf
*sb
= &state
->sb
;
374 if (ds
->fn
&& !ds
->fn(ds
->repo
, oid
, ds
->cb_data
))
377 hash
= repo_find_unique_abbrev(ds
->repo
, oid
, DEFAULT_ABBREV
);
378 type
= oid_object_info(ds
->repo
, oid
, NULL
);
382 * TRANSLATORS: This is a line of ambiguous object
383 * output shown when we cannot look up or parse the
384 * object in question. E.g. "deadbeef [bad object]".
386 strbuf_addf(sb
, _("%s [bad object]"), hash
);
390 assert(type
== OBJ_TREE
|| type
== OBJ_COMMIT
||
391 type
== OBJ_BLOB
|| type
== OBJ_TAG
);
393 if (type
== OBJ_COMMIT
) {
394 struct strbuf date
= STRBUF_INIT
;
395 struct strbuf msg
= STRBUF_INIT
;
396 struct commit
*commit
= lookup_commit(ds
->repo
, oid
);
399 struct pretty_print_context pp
= {0};
400 pp
.date_mode
.type
= DATE_SHORT
;
401 format_commit_message(commit
, "%ad", &date
, &pp
);
402 format_commit_message(commit
, "%s", &msg
, &pp
);
406 * TRANSLATORS: This is a line of ambiguous commit
407 * object output. E.g.:
409 * "deadbeef commit 2021-01-01 - Some Commit Message"
411 strbuf_addf(sb
, _("%s commit %s - %s"), hash
, date
.buf
,
414 strbuf_release(&date
);
415 strbuf_release(&msg
);
416 } else if (type
== OBJ_TAG
) {
417 struct tag
*tag
= lookup_tag(ds
->repo
, oid
);
419 if (!parse_tag(tag
) && tag
->tag
) {
421 * TRANSLATORS: This is a line of ambiguous
422 * tag object output. E.g.:
424 * "deadbeef tag 2022-01-01 - Some Tag Message"
426 * The second argument is the YYYY-MM-DD found
429 * The third argument is the "tag" string
432 strbuf_addf(sb
, _("%s tag %s - %s"), hash
,
433 show_date(tag
->date
, 0, DATE_MODE(SHORT
)),
437 * TRANSLATORS: This is a line of ambiguous
438 * tag object output where we couldn't parse
439 * the tag itself. E.g.:
441 * "deadbeef [bad tag, could not parse it]"
443 strbuf_addf(sb
, _("%s [bad tag, could not parse it]"),
446 } else if (type
== OBJ_TREE
) {
448 * TRANSLATORS: This is a line of ambiguous <type>
449 * object output. E.g. "deadbeef tree".
451 strbuf_addf(sb
, _("%s tree"), hash
);
452 } else if (type
== OBJ_BLOB
) {
454 * TRANSLATORS: This is a line of ambiguous <type>
455 * object output. E.g. "deadbeef blob".
457 strbuf_addf(sb
, _("%s blob"), hash
);
463 * TRANSLATORS: This is line item of ambiguous object output
464 * from describe_ambiguous_object() above. For RTL languages
465 * you'll probably want to swap the "%s" and leading " " space
468 strbuf_addf(advice
, _(" %s\n"), sb
->buf
);
474 static int collect_ambiguous(const struct object_id
*oid
, void *data
)
476 oid_array_append(data
, oid
);
480 static int repo_collect_ambiguous(struct repository
*r UNUSED
,
481 const struct object_id
*oid
,
484 return collect_ambiguous(oid
, data
);
487 static int sort_ambiguous(const void *a
, const void *b
, void *ctx
)
489 struct repository
*sort_ambiguous_repo
= ctx
;
490 int a_type
= oid_object_info(sort_ambiguous_repo
, a
, NULL
);
491 int b_type
= oid_object_info(sort_ambiguous_repo
, b
, NULL
);
496 * Sorts by hash within the same object type, just as
497 * oid_array_for_each_unique() would do.
499 if (a_type
== b_type
)
503 * Between object types show tags, then commits, and finally
506 * The object_type enum is commit, tree, blob, tag, but we
507 * want tag, commit, tree blob. Cleverly (perhaps too
508 * cleverly) do that with modulus, since the enum assigns 1 to
509 * commit, so tag becomes 0.
511 a_type_sort
= a_type
% 4;
512 b_type_sort
= b_type
% 4;
513 return a_type_sort
> b_type_sort
? 1 : -1;
516 static void sort_ambiguous_oid_array(struct repository
*r
, struct oid_array
*a
)
518 QSORT_S(a
->oid
, a
->nr
, sort_ambiguous
, r
);
521 static enum get_oid_result
get_short_oid(struct repository
*r
,
522 const char *name
, int len
,
523 struct object_id
*oid
,
527 struct disambiguate_state ds
;
528 int quietly
= !!(flags
& GET_OID_QUIETLY
);
530 if (init_object_disambiguation(r
, name
, len
, &ds
) < 0)
533 if (HAS_MULTI_BITS(flags
& GET_OID_DISAMBIGUATORS
))
534 BUG("multiple get_short_oid disambiguator flags");
536 if (flags
& GET_OID_COMMIT
)
537 ds
.fn
= disambiguate_commit_only
;
538 else if (flags
& GET_OID_COMMITTISH
)
539 ds
.fn
= disambiguate_committish_only
;
540 else if (flags
& GET_OID_TREE
)
541 ds
.fn
= disambiguate_tree_only
;
542 else if (flags
& GET_OID_TREEISH
)
543 ds
.fn
= disambiguate_treeish_only
;
544 else if (flags
& GET_OID_BLOB
)
545 ds
.fn
= disambiguate_blob_only
;
547 ds
.fn
= default_disambiguate_hint
;
549 find_short_object_filename(&ds
);
550 find_short_packed_object(&ds
);
551 status
= finish_object_disambiguation(&ds
, oid
);
554 * If we didn't find it, do the usual reprepare() slow-path,
555 * since the object may have recently been added to the repository
556 * or migrated from loose to packed.
558 if (status
== MISSING_OBJECT
) {
559 reprepare_packed_git(r
);
560 find_short_object_filename(&ds
);
561 find_short_packed_object(&ds
);
562 status
= finish_object_disambiguation(&ds
, oid
);
565 if (!quietly
&& (status
== SHORT_NAME_AMBIGUOUS
)) {
566 struct oid_array collect
= OID_ARRAY_INIT
;
567 struct ambiguous_output out
= {
570 .advice
= STRBUF_INIT
,
573 error(_("short object ID %s is ambiguous"), ds
.hex_pfx
);
576 * We may still have ambiguity if we simply saw a series of
577 * candidates that did not satisfy our hint function. In
578 * that case, we still want to show them, so disable the hint
584 repo_for_each_abbrev(r
, ds
.hex_pfx
, collect_ambiguous
, &collect
);
585 sort_ambiguous_oid_array(r
, &collect
);
587 if (oid_array_for_each(&collect
, show_ambiguous_object
, &out
))
588 BUG("show_ambiguous_object shouldn't return non-zero");
591 * TRANSLATORS: The argument is the list of ambiguous
592 * objects composed in show_ambiguous_object(). See
593 * its "TRANSLATORS" comments for details.
595 advise(_("The candidates are:\n%s"), out
.advice
.buf
);
597 oid_array_clear(&collect
);
598 strbuf_release(&out
.advice
);
599 strbuf_release(&out
.sb
);
605 int repo_for_each_abbrev(struct repository
*r
, const char *prefix
,
606 each_abbrev_fn fn
, void *cb_data
)
608 struct oid_array collect
= OID_ARRAY_INIT
;
609 struct disambiguate_state ds
;
612 if (init_object_disambiguation(r
, prefix
, strlen(prefix
), &ds
) < 0)
615 ds
.always_call_fn
= 1;
616 ds
.fn
= repo_collect_ambiguous
;
617 ds
.cb_data
= &collect
;
618 find_short_object_filename(&ds
);
619 find_short_packed_object(&ds
);
621 ret
= oid_array_for_each_unique(&collect
, fn
, cb_data
);
622 oid_array_clear(&collect
);
627 * Return the slot of the most-significant bit set in "val". There are various
628 * ways to do this quickly with fls() or __builtin_clzl(), but speed is
629 * probably not a big deal here.
631 static unsigned msb(unsigned long val
)
639 struct min_abbrev_data
{
640 unsigned int init_len
;
641 unsigned int cur_len
;
643 struct repository
*repo
;
644 const struct object_id
*oid
;
647 static inline char get_hex_char_from_oid(const struct object_id
*oid
,
650 static const char hex
[] = "0123456789abcdef";
653 return hex
[oid
->hash
[pos
>> 1] >> 4];
655 return hex
[oid
->hash
[pos
>> 1] & 0xf];
658 static int extend_abbrev_len(const struct object_id
*oid
, void *cb_data
)
660 struct min_abbrev_data
*mad
= cb_data
;
662 unsigned int i
= mad
->init_len
;
663 while (mad
->hex
[i
] && mad
->hex
[i
] == get_hex_char_from_oid(oid
, i
))
666 if (i
< GIT_MAX_RAWSZ
&& i
>= mad
->cur_len
)
667 mad
->cur_len
= i
+ 1;
672 static int repo_extend_abbrev_len(struct repository
*r UNUSED
,
673 const struct object_id
*oid
,
676 return extend_abbrev_len(oid
, cb_data
);
679 static void find_abbrev_len_for_midx(struct multi_pack_index
*m
,
680 struct min_abbrev_data
*mad
)
683 uint32_t num
, first
= 0;
684 struct object_id oid
;
685 const struct object_id
*mad_oid
;
690 num
= m
->num_objects
;
692 match
= bsearch_midx(mad_oid
, m
, &first
);
695 * first is now the position in the packfile where we would insert
696 * mad->hash if it does not exist (or the position of mad->hash if
697 * it does exist). Hence, we consider a maximum of two objects
698 * nearby for the abbreviation length.
702 if (nth_midxed_object_oid(&oid
, m
, first
))
703 extend_abbrev_len(&oid
, mad
);
704 } else if (first
< num
- 1) {
705 if (nth_midxed_object_oid(&oid
, m
, first
+ 1))
706 extend_abbrev_len(&oid
, mad
);
709 if (nth_midxed_object_oid(&oid
, m
, first
- 1))
710 extend_abbrev_len(&oid
, mad
);
712 mad
->init_len
= mad
->cur_len
;
715 static void find_abbrev_len_for_pack(struct packed_git
*p
,
716 struct min_abbrev_data
*mad
)
719 uint32_t num
, first
= 0;
720 struct object_id oid
;
721 const struct object_id
*mad_oid
;
723 if (p
->multi_pack_index
)
726 if (open_pack_index(p
) || !p
->num_objects
)
729 num
= p
->num_objects
;
731 match
= bsearch_pack(mad_oid
, p
, &first
);
734 * first is now the position in the packfile where we would insert
735 * mad->hash if it does not exist (or the position of mad->hash if
736 * it does exist). Hence, we consider a maximum of two objects
737 * nearby for the abbreviation length.
741 if (!nth_packed_object_id(&oid
, p
, first
))
742 extend_abbrev_len(&oid
, mad
);
743 } else if (first
< num
- 1) {
744 if (!nth_packed_object_id(&oid
, p
, first
+ 1))
745 extend_abbrev_len(&oid
, mad
);
748 if (!nth_packed_object_id(&oid
, p
, first
- 1))
749 extend_abbrev_len(&oid
, mad
);
751 mad
->init_len
= mad
->cur_len
;
754 static void find_abbrev_len_packed(struct min_abbrev_data
*mad
)
756 struct multi_pack_index
*m
;
757 struct packed_git
*p
;
759 for (m
= get_multi_pack_index(mad
->repo
); m
; m
= m
->next
)
760 find_abbrev_len_for_midx(m
, mad
);
761 for (p
= get_packed_git(mad
->repo
); p
; p
= p
->next
)
762 find_abbrev_len_for_pack(p
, mad
);
765 int repo_find_unique_abbrev_r(struct repository
*r
, char *hex
,
766 const struct object_id
*oid
, int len
)
768 struct disambiguate_state ds
;
769 struct min_abbrev_data mad
;
770 struct object_id oid_ret
;
771 const unsigned hexsz
= r
->hash_algo
->hexsz
;
774 unsigned long count
= repo_approximate_object_count(r
);
776 * Add one because the MSB only tells us the highest bit set,
777 * not including the value of all the _other_ bits (so "15"
778 * is only one off of 2^4, but the MSB is the 3rd bit.
780 len
= msb(count
) + 1;
782 * We now know we have on the order of 2^len objects, which
783 * expects a collision at 2^(len/2). But we also care about hex
784 * chars, not bits, and there are 4 bits per hex. So all
785 * together we need to divide by 2 and round up.
787 len
= DIV_ROUND_UP(len
, 2);
789 * For very small repos, we stick with our regular fallback.
791 if (len
< FALLBACK_DEFAULT_ABBREV
)
792 len
= FALLBACK_DEFAULT_ABBREV
;
795 oid_to_hex_r(hex
, oid
);
796 if (len
== hexsz
|| !len
)
805 find_abbrev_len_packed(&mad
);
807 if (init_object_disambiguation(r
, hex
, mad
.cur_len
, &ds
) < 0)
810 ds
.fn
= repo_extend_abbrev_len
;
811 ds
.always_call_fn
= 1;
812 ds
.cb_data
= (void *)&mad
;
814 find_short_object_filename(&ds
);
815 (void)finish_object_disambiguation(&ds
, &oid_ret
);
817 hex
[mad
.cur_len
] = 0;
821 const char *repo_find_unique_abbrev(struct repository
*r
,
822 const struct object_id
*oid
,
826 static char hexbuffer
[4][GIT_MAX_HEXSZ
+ 1];
827 char *hex
= hexbuffer
[bufno
];
828 bufno
= (bufno
+ 1) % ARRAY_SIZE(hexbuffer
);
829 repo_find_unique_abbrev_r(r
, hex
, oid
, len
);
833 static int ambiguous_path(const char *path
, int len
)
838 for (cnt
= 0; cnt
< len
; cnt
++) {
858 static inline int at_mark(const char *string
, int len
,
859 const char **suffix
, int nr
)
863 for (i
= 0; i
< nr
; i
++) {
864 int suffix_len
= strlen(suffix
[i
]);
865 if (suffix_len
<= len
866 && !strncasecmp(string
, suffix
[i
], suffix_len
))
872 static inline int upstream_mark(const char *string
, int len
)
874 const char *suffix
[] = { "@{upstream}", "@{u}" };
875 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
878 static inline int push_mark(const char *string
, int len
)
880 const char *suffix
[] = { "@{push}" };
881 return at_mark(string
, len
, suffix
, ARRAY_SIZE(suffix
));
884 static enum get_oid_result
get_oid_1(struct repository
*r
, const char *name
, int len
, struct object_id
*oid
, unsigned lookup_flags
);
885 static int interpret_nth_prior_checkout(struct repository
*r
, const char *name
, int namelen
, struct strbuf
*buf
);
887 static int get_oid_basic(struct repository
*r
, const char *str
, int len
,
888 struct object_id
*oid
, unsigned int flags
)
890 static const char *warn_msg
= "refname '%.*s' is ambiguous.";
891 static const char *object_name_msg
= N_(
892 "Git normally never creates a ref that ends with 40 hex characters\n"
893 "because it will be ignored when you just specify 40-hex. These refs\n"
894 "may be created by mistake. For example,\n"
896 " git switch -c $br $(git rev-parse ...)\n"
898 "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n"
899 "examine these refs and maybe delete them. Turn this message off by\n"
900 "running \"git config advice.objectNameWarning false\"");
901 struct object_id tmp_oid
;
902 char *real_ref
= NULL
;
904 int at
, reflog_len
, nth_prior
= 0;
906 if (len
== r
->hash_algo
->hexsz
&& !get_oid_hex(str
, oid
)) {
907 if (warn_ambiguous_refs
&& warn_on_object_refname_ambiguity
) {
908 refs_found
= repo_dwim_ref(r
, str
, len
, &tmp_oid
, &real_ref
, 0);
909 if (refs_found
> 0) {
910 warning(warn_msg
, len
, str
);
911 if (advice_enabled(ADVICE_OBJECT_NAME_WARNING
))
912 fprintf(stderr
, "%s\n", _(object_name_msg
));
919 /* basic@{time or number or -number} format to query ref-log */
921 if (len
&& str
[len
-1] == '}') {
922 for (at
= len
-4; at
>= 0; at
--) {
923 if (str
[at
] == '@' && str
[at
+1] == '{') {
924 if (str
[at
+2] == '-') {
926 /* @{-N} not at start */
931 if (!upstream_mark(str
+ at
, len
- at
) &&
932 !push_mark(str
+ at
, len
- at
)) {
933 reflog_len
= (len
-1) - (at
+2);
941 /* Accept only unambiguous ref paths. */
942 if (len
&& ambiguous_path(str
, len
))
946 struct strbuf buf
= STRBUF_INIT
;
949 if (interpret_nth_prior_checkout(r
, str
, len
, &buf
) > 0) {
950 detached
= (buf
.len
== r
->hash_algo
->hexsz
&& !get_oid_hex(buf
.buf
, oid
));
951 strbuf_release(&buf
);
957 if (!len
&& reflog_len
)
958 /* allow "@{...}" to mean the current branch reflog */
959 refs_found
= repo_dwim_ref(r
, "HEAD", 4, oid
, &real_ref
, 0);
961 refs_found
= repo_dwim_log(r
, str
, len
, oid
, &real_ref
);
963 refs_found
= repo_dwim_ref(r
, str
, len
, oid
, &real_ref
, 0);
968 if (warn_ambiguous_refs
&& !(flags
& GET_OID_QUIETLY
) &&
970 !get_short_oid(r
, str
, len
, &tmp_oid
, GET_OID_QUIETLY
)))
971 warning(warn_msg
, len
, str
);
979 /* Is it asking for N-th entry, or approxidate? */
980 for (i
= nth
= 0; 0 <= nth
&& i
< reflog_len
; i
++) {
981 char ch
= str
[at
+2+i
];
982 if ('0' <= ch
&& ch
<= '9')
983 nth
= nth
* 10 + ch
- '0';
987 if (100000000 <= nth
) {
994 char *tmp
= xstrndup(str
+ at
+ 2, reflog_len
);
995 at_time
= approxidate_careful(tmp
, &errors
);
1002 if (read_ref_at(get_main_ref_store(r
),
1003 real_ref
, flags
, at_time
, nth
, oid
, NULL
,
1004 &co_time
, &co_tz
, &co_cnt
)) {
1006 if (!skip_prefix(real_ref
, "refs/heads/", &str
))
1011 if (!(flags
& GET_OID_QUIETLY
)) {
1012 warning(_("log for '%.*s' only goes back to %s"),
1014 show_date(co_time
, co_tz
, DATE_MODE(RFC2822
)));
1017 if (flags
& GET_OID_QUIETLY
) {
1020 die(_("log for '%.*s' only has %d entries"),
1030 static enum get_oid_result
get_parent(struct repository
*r
,
1031 const char *name
, int len
,
1032 struct object_id
*result
, int idx
)
1034 struct object_id oid
;
1035 enum get_oid_result ret
= get_oid_1(r
, name
, len
, &oid
,
1036 GET_OID_COMMITTISH
);
1037 struct commit
*commit
;
1038 struct commit_list
*p
;
1042 commit
= lookup_commit_reference(r
, &oid
);
1043 if (parse_commit(commit
))
1044 return MISSING_OBJECT
;
1046 oidcpy(result
, &commit
->object
.oid
);
1049 p
= commit
->parents
;
1052 oidcpy(result
, &p
->item
->object
.oid
);
1057 return MISSING_OBJECT
;
1060 static enum get_oid_result
get_nth_ancestor(struct repository
*r
,
1061 const char *name
, int len
,
1062 struct object_id
*result
,
1065 struct object_id oid
;
1066 struct commit
*commit
;
1069 ret
= get_oid_1(r
, name
, len
, &oid
, GET_OID_COMMITTISH
);
1072 commit
= lookup_commit_reference(r
, &oid
);
1074 return MISSING_OBJECT
;
1076 while (generation
--) {
1077 if (parse_commit(commit
) || !commit
->parents
)
1078 return MISSING_OBJECT
;
1079 commit
= commit
->parents
->item
;
1081 oidcpy(result
, &commit
->object
.oid
);
1085 struct object
*repo_peel_to_type(struct repository
*r
, const char *name
, int namelen
,
1086 struct object
*o
, enum object_type expected_type
)
1088 if (name
&& !namelen
)
1089 namelen
= strlen(name
);
1091 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1093 if (expected_type
== OBJ_ANY
|| o
->type
== expected_type
)
1095 if (o
->type
== OBJ_TAG
)
1096 o
= ((struct tag
*) o
)->tagged
;
1097 else if (o
->type
== OBJ_COMMIT
)
1098 o
= &(repo_get_commit_tree(r
, ((struct commit
*)o
))->object
);
1101 error("%.*s: expected %s type, but the object "
1102 "dereferences to %s type",
1103 namelen
, name
, type_name(expected_type
),
1104 type_name(o
->type
));
1110 static int peel_onion(struct repository
*r
, const char *name
, int len
,
1111 struct object_id
*oid
, unsigned lookup_flags
)
1113 struct object_id outer
;
1115 unsigned int expected_type
= 0;
1119 * "ref^{type}" dereferences ref repeatedly until you cannot
1120 * dereference anymore, or you get an object of given type,
1121 * whichever comes first. "ref^{}" means just dereference
1122 * tags until you get a non-tag. "ref^0" is a shorthand for
1123 * "ref^{commit}". "commit^{tree}" could be used to find the
1124 * top-level tree of the given commit.
1126 if (len
< 4 || name
[len
-1] != '}')
1129 for (sp
= name
+ len
- 1; name
<= sp
; sp
--) {
1131 if (ch
== '{' && name
< sp
&& sp
[-1] == '^')
1137 sp
++; /* beginning of type name, or closing brace for empty */
1138 if (starts_with(sp
, "commit}"))
1139 expected_type
= OBJ_COMMIT
;
1140 else if (starts_with(sp
, "tag}"))
1141 expected_type
= OBJ_TAG
;
1142 else if (starts_with(sp
, "tree}"))
1143 expected_type
= OBJ_TREE
;
1144 else if (starts_with(sp
, "blob}"))
1145 expected_type
= OBJ_BLOB
;
1146 else if (starts_with(sp
, "object}"))
1147 expected_type
= OBJ_ANY
;
1148 else if (sp
[0] == '}')
1149 expected_type
= OBJ_NONE
;
1150 else if (sp
[0] == '/')
1151 expected_type
= OBJ_COMMIT
;
1155 lookup_flags
&= ~GET_OID_DISAMBIGUATORS
;
1156 if (expected_type
== OBJ_COMMIT
)
1157 lookup_flags
|= GET_OID_COMMITTISH
;
1158 else if (expected_type
== OBJ_TREE
)
1159 lookup_flags
|= GET_OID_TREEISH
;
1161 if (get_oid_1(r
, name
, sp
- name
- 2, &outer
, lookup_flags
))
1164 o
= parse_object(r
, &outer
);
1167 if (!expected_type
) {
1168 o
= deref_tag(r
, o
, name
, sp
- name
- 2);
1169 if (!o
|| (!o
->parsed
&& !parse_object(r
, &o
->oid
)))
1171 oidcpy(oid
, &o
->oid
);
1176 * At this point, the syntax look correct, so
1177 * if we do not get the needed object, we should
1180 o
= repo_peel_to_type(r
, name
, len
, o
, expected_type
);
1184 oidcpy(oid
, &o
->oid
);
1186 /* "$commit^{/foo}" */
1189 struct commit_list
*list
= NULL
;
1192 * $commit^{/}. Some regex implementation may reject.
1193 * We don't need regex anyway. '' pattern always matches.
1198 prefix
= xstrndup(sp
+ 1, name
+ len
- 1 - (sp
+ 1));
1199 commit_list_insert((struct commit
*)o
, &list
);
1200 ret
= get_oid_oneline(r
, prefix
, oid
, list
);
1207 static int get_describe_name(struct repository
*r
,
1208 const char *name
, int len
,
1209 struct object_id
*oid
)
1212 unsigned flags
= GET_OID_QUIETLY
| GET_OID_COMMIT
;
1214 for (cp
= name
+ len
- 1; name
+ 2 <= cp
; cp
--) {
1216 if (!isxdigit(ch
)) {
1217 /* We must be looking at g in "SOMETHING-g"
1218 * for it to be describe output.
1220 if (ch
== 'g' && cp
[-1] == '-') {
1223 return get_short_oid(r
,
1224 cp
, len
, oid
, flags
);
1231 static enum get_oid_result
get_oid_1(struct repository
*r
,
1232 const char *name
, int len
,
1233 struct object_id
*oid
,
1234 unsigned lookup_flags
)
1236 int ret
, has_suffix
;
1240 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
1243 for (cp
= name
+ len
- 1; name
<= cp
; cp
--) {
1245 if ('0' <= ch
&& ch
<= '9')
1247 if (ch
== '~' || ch
== '^')
1253 unsigned int num
= 0;
1254 int len1
= cp
- name
;
1256 while (cp
< name
+ len
) {
1257 unsigned int digit
= *cp
++ - '0';
1258 if (unsigned_mult_overflows(num
, 10))
1259 return MISSING_OBJECT
;
1261 if (unsigned_add_overflows(num
, digit
))
1262 return MISSING_OBJECT
;
1265 if (!num
&& len1
== len
- 1)
1267 else if (num
> INT_MAX
)
1268 return MISSING_OBJECT
;
1269 if (has_suffix
== '^')
1270 return get_parent(r
, name
, len1
, oid
, num
);
1271 /* else if (has_suffix == '~') -- goes without saying */
1272 return get_nth_ancestor(r
, name
, len1
, oid
, num
);
1275 ret
= peel_onion(r
, name
, len
, oid
, lookup_flags
);
1279 ret
= get_oid_basic(r
, name
, len
, oid
, lookup_flags
);
1283 /* It could be describe output that is "SOMETHING-gXXXX" */
1284 ret
= get_describe_name(r
, name
, len
, oid
);
1288 return get_short_oid(r
, name
, len
, oid
, lookup_flags
);
1292 * This interprets names like ':/Initial revision of "git"' by searching
1293 * through history and returning the first commit whose message starts
1294 * the given regular expression.
1296 * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'.
1298 * For a literal '!' character at the beginning of a pattern, you have to repeat
1299 * that, like: ':/!!foo'
1301 * For future extension, all other sequences beginning with ':/!' are reserved.
1304 /* Remember to update object flag allocation in object.h */
1305 #define ONELINE_SEEN (1u<<20)
1307 struct handle_one_ref_cb
{
1308 struct repository
*repo
;
1309 struct commit_list
**list
;
1312 static int handle_one_ref(const char *path
, const struct object_id
*oid
,
1316 struct handle_one_ref_cb
*cb
= cb_data
;
1317 struct commit_list
**list
= cb
->list
;
1318 struct object
*object
= parse_object(cb
->repo
, oid
);
1321 if (object
->type
== OBJ_TAG
) {
1322 object
= deref_tag(cb
->repo
, object
, path
,
1327 if (object
->type
!= OBJ_COMMIT
)
1329 commit_list_insert((struct commit
*)object
, list
);
1333 static int get_oid_oneline(struct repository
*r
,
1334 const char *prefix
, struct object_id
*oid
,
1335 struct commit_list
*list
)
1337 struct commit_list
*backup
= NULL
, *l
;
1342 if (prefix
[0] == '!') {
1345 if (prefix
[0] == '-') {
1348 } else if (prefix
[0] != '!') {
1353 if (regcomp(®ex
, prefix
, REG_EXTENDED
))
1356 for (l
= list
; l
; l
= l
->next
) {
1357 l
->item
->object
.flags
|= ONELINE_SEEN
;
1358 commit_list_insert(l
->item
, &backup
);
1361 const char *p
, *buf
;
1362 struct commit
*commit
;
1365 commit
= pop_most_recent_commit(&list
, ONELINE_SEEN
);
1366 if (!parse_object(r
, &commit
->object
.oid
))
1368 buf
= get_commit_buffer(commit
, NULL
);
1369 p
= strstr(buf
, "\n\n");
1370 matches
= negative
^ (p
&& !regexec(®ex
, p
+ 2, 0, NULL
, 0));
1371 unuse_commit_buffer(commit
, buf
);
1374 oidcpy(oid
, &commit
->object
.oid
);
1380 free_commit_list(list
);
1381 for (l
= backup
; l
; l
= l
->next
)
1382 clear_commit_marks(l
->item
, ONELINE_SEEN
);
1383 free_commit_list(backup
);
1384 return found
? 0 : -1;
1387 struct grab_nth_branch_switch_cbdata
{
1392 static int grab_nth_branch_switch(struct object_id
*ooid UNUSED
,
1393 struct object_id
*noid UNUSED
,
1394 const char *email UNUSED
,
1395 timestamp_t timestamp UNUSED
,
1397 const char *message
, void *cb_data
)
1399 struct grab_nth_branch_switch_cbdata
*cb
= cb_data
;
1400 const char *match
= NULL
, *target
= NULL
;
1403 if (skip_prefix(message
, "checkout: moving from ", &match
))
1404 target
= strstr(match
, " to ");
1406 if (!match
|| !target
)
1408 if (--(cb
->remaining
) == 0) {
1409 len
= target
- match
;
1410 strbuf_reset(cb
->sb
);
1411 strbuf_add(cb
->sb
, match
, len
);
1412 return 1; /* we are done */
1418 * Parse @{-N} syntax, return the number of characters parsed
1419 * if successful; otherwise signal an error with negative value.
1421 static int interpret_nth_prior_checkout(struct repository
*r
,
1422 const char *name
, int namelen
,
1427 struct grab_nth_branch_switch_cbdata cb
;
1433 if (name
[0] != '@' || name
[1] != '{' || name
[2] != '-')
1435 brace
= memchr(name
, '}', namelen
);
1438 nth
= strtol(name
+ 3, &num_end
, 10);
1439 if (num_end
!= brace
)
1446 retval
= refs_for_each_reflog_ent_reverse(get_main_ref_store(r
),
1447 "HEAD", grab_nth_branch_switch
, &cb
);
1449 retval
= brace
- name
+ 1;
1456 int repo_get_oid_mb(struct repository
*r
,
1458 struct object_id
*oid
)
1460 struct commit
*one
, *two
;
1461 struct commit_list
*mbs
;
1462 struct object_id oid_tmp
;
1466 dots
= strstr(name
, "...");
1468 return repo_get_oid(r
, name
, oid
);
1470 st
= repo_get_oid(r
, "HEAD", &oid_tmp
);
1473 strbuf_init(&sb
, dots
- name
);
1474 strbuf_add(&sb
, name
, dots
- name
);
1475 st
= repo_get_oid_committish(r
, sb
.buf
, &oid_tmp
);
1476 strbuf_release(&sb
);
1480 one
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1484 if (repo_get_oid_committish(r
, dots
[3] ? (dots
+ 3) : "HEAD", &oid_tmp
))
1486 two
= lookup_commit_reference_gently(r
, &oid_tmp
, 0);
1489 mbs
= repo_get_merge_bases(r
, one
, two
);
1490 if (!mbs
|| mbs
->next
)
1494 oidcpy(oid
, &mbs
->item
->object
.oid
);
1496 free_commit_list(mbs
);
1500 /* parse @something syntax, when 'something' is not {.*} */
1501 static int interpret_empty_at(const char *name
, int namelen
, int len
, struct strbuf
*buf
)
1505 if (len
|| name
[1] == '{')
1508 /* make sure it's a single @, or @@{.*}, not @foo */
1509 next
= memchr(name
+ len
+ 1, '@', namelen
- len
- 1);
1510 if (next
&& next
[1] != '{')
1513 next
= name
+ namelen
;
1514 if (next
!= name
+ 1)
1518 strbuf_add(buf
, "HEAD", 4);
1522 static int reinterpret(struct repository
*r
,
1523 const char *name
, int namelen
, int len
,
1524 struct strbuf
*buf
, unsigned allowed
)
1526 /* we have extra data, which might need further processing */
1527 struct strbuf tmp
= STRBUF_INIT
;
1528 int used
= buf
->len
;
1530 struct interpret_branch_name_options options
= {
1534 strbuf_add(buf
, name
+ len
, namelen
- len
);
1535 ret
= repo_interpret_branch_name(r
, buf
->buf
, buf
->len
, &tmp
, &options
);
1536 /* that data was not interpreted, remove our cruft */
1538 strbuf_setlen(buf
, used
);
1542 strbuf_addbuf(buf
, &tmp
);
1543 strbuf_release(&tmp
);
1544 /* tweak for size of {-N} versus expanded ref name */
1545 return ret
- used
+ len
;
1548 static void set_shortened_ref(struct repository
*r
, struct strbuf
*buf
, const char *ref
)
1550 char *s
= refs_shorten_unambiguous_ref(get_main_ref_store(r
), ref
, 0);
1552 strbuf_addstr(buf
, s
);
1556 static int branch_interpret_allowed(const char *refname
, unsigned allowed
)
1561 if ((allowed
& INTERPRET_BRANCH_LOCAL
) &&
1562 starts_with(refname
, "refs/heads/"))
1564 if ((allowed
& INTERPRET_BRANCH_REMOTE
) &&
1565 starts_with(refname
, "refs/remotes/"))
1571 static int interpret_branch_mark(struct repository
*r
,
1572 const char *name
, int namelen
,
1573 int at
, struct strbuf
*buf
,
1574 int (*get_mark
)(const char *, int),
1575 const char *(*get_data
)(struct branch
*,
1577 const struct interpret_branch_name_options
*options
)
1580 struct branch
*branch
;
1581 struct strbuf err
= STRBUF_INIT
;
1584 len
= get_mark(name
+ at
, namelen
- at
);
1588 if (memchr(name
, ':', at
))
1592 char *name_str
= xmemdupz(name
, at
);
1593 branch
= branch_get(name_str
);
1596 branch
= branch_get(NULL
);
1598 value
= get_data(branch
, &err
);
1600 if (options
->nonfatal_dangling_mark
) {
1601 strbuf_release(&err
);
1608 if (!branch_interpret_allowed(value
, options
->allowed
))
1611 set_shortened_ref(r
, buf
, value
);
1615 int repo_interpret_branch_name(struct repository
*r
,
1616 const char *name
, int namelen
,
1618 const struct interpret_branch_name_options
*options
)
1625 namelen
= strlen(name
);
1627 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_LOCAL
)) {
1628 len
= interpret_nth_prior_checkout(r
, name
, namelen
, buf
);
1630 return len
; /* syntax Ok, not enough switches */
1631 } else if (len
> 0) {
1633 return len
; /* consumed all */
1635 return reinterpret(r
, name
, namelen
, len
, buf
,
1641 (at
= memchr(start
, '@', namelen
- (start
- name
)));
1644 if (!options
->allowed
|| (options
->allowed
& INTERPRET_BRANCH_HEAD
)) {
1645 len
= interpret_empty_at(name
, namelen
, at
- name
, buf
);
1647 return reinterpret(r
, name
, namelen
, len
, buf
,
1651 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1652 upstream_mark
, branch_get_upstream
,
1657 len
= interpret_branch_mark(r
, name
, namelen
, at
- name
, buf
,
1658 push_mark
, branch_get_push
,
1667 void strbuf_branchname(struct strbuf
*sb
, const char *name
, unsigned allowed
)
1669 int len
= strlen(name
);
1670 struct interpret_branch_name_options options
= {
1673 int used
= interpret_branch_name(name
, len
, sb
, &options
);
1677 strbuf_add(sb
, name
+ used
, len
- used
);
1680 int strbuf_check_branch_ref(struct strbuf
*sb
, const char *name
)
1682 if (startup_info
->have_repository
)
1683 strbuf_branchname(sb
, name
, INTERPRET_BRANCH_LOCAL
);
1685 strbuf_addstr(sb
, name
);
1688 * This splice must be done even if we end up rejecting the
1689 * name; builtin/branch.c::copy_or_rename_branch() still wants
1690 * to see what the name expanded to so that "branch -m" can be
1691 * used as a tool to correct earlier mistakes.
1693 strbuf_splice(sb
, 0, 0, "refs/heads/", 11);
1696 !strcmp(sb
->buf
, "refs/heads/HEAD"))
1699 return check_refname_format(sb
->buf
, 0);
1703 * This is like "get_oid_basic()", except it allows "object ID expressions",
1704 * notably "xyz^" for "parent of xyz"
1706 int repo_get_oid(struct repository
*r
, const char *name
, struct object_id
*oid
)
1708 struct object_context unused
;
1709 return get_oid_with_context(r
, name
, 0, oid
, &unused
);
1713 * This returns a non-zero value if the string (built using printf
1714 * format and the given arguments) is not a valid object.
1716 int get_oidf(struct object_id
*oid
, const char *fmt
, ...)
1720 struct strbuf sb
= STRBUF_INIT
;
1723 strbuf_vaddf(&sb
, fmt
, ap
);
1726 ret
= get_oid(sb
.buf
, oid
);
1727 strbuf_release(&sb
);
1733 * Many callers know that the user meant to name a commit-ish by
1734 * syntactical positions where the object name appears. Calling this
1735 * function allows the machinery to disambiguate shorter-than-unique
1736 * abbreviated object names between commit-ish and others.
1738 * Note that this does NOT error out when the named object is not a
1739 * commit-ish. It is merely to give a hint to the disambiguation
1742 int repo_get_oid_committish(struct repository
*r
,
1744 struct object_id
*oid
)
1746 struct object_context unused
;
1747 return get_oid_with_context(r
, name
, GET_OID_COMMITTISH
,
1751 int repo_get_oid_treeish(struct repository
*r
,
1753 struct object_id
*oid
)
1755 struct object_context unused
;
1756 return get_oid_with_context(r
, name
, GET_OID_TREEISH
,
1760 int repo_get_oid_commit(struct repository
*r
,
1762 struct object_id
*oid
)
1764 struct object_context unused
;
1765 return get_oid_with_context(r
, name
, GET_OID_COMMIT
,
1769 int repo_get_oid_tree(struct repository
*r
,
1771 struct object_id
*oid
)
1773 struct object_context unused
;
1774 return get_oid_with_context(r
, name
, GET_OID_TREE
,
1778 int repo_get_oid_blob(struct repository
*r
,
1780 struct object_id
*oid
)
1782 struct object_context unused
;
1783 return get_oid_with_context(r
, name
, GET_OID_BLOB
,
1787 /* Must be called only when object_name:filename doesn't exist. */
1788 static void diagnose_invalid_oid_path(struct repository
*r
,
1790 const char *filename
,
1791 const struct object_id
*tree_oid
,
1792 const char *object_name
,
1793 int object_name_len
)
1795 struct object_id oid
;
1796 unsigned short mode
;
1801 if (file_exists(filename
))
1802 die(_("path '%s' exists on disk, but not in '%.*s'"),
1803 filename
, object_name_len
, object_name
);
1804 if (is_missing_file_error(errno
)) {
1805 char *fullname
= xstrfmt("%s%s", prefix
, filename
);
1807 if (!get_tree_entry(r
, tree_oid
, fullname
, &oid
, &mode
)) {
1808 die(_("path '%s' exists, but not '%s'\n"
1809 "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"),
1812 object_name_len
, object_name
,
1814 object_name_len
, object_name
,
1817 die(_("path '%s' does not exist in '%.*s'"),
1818 filename
, object_name_len
, object_name
);
1822 /* Must be called only when :stage:filename doesn't exist. */
1823 static void diagnose_invalid_index_path(struct repository
*r
,
1826 const char *filename
)
1828 struct index_state
*istate
= r
->index
;
1829 const struct cache_entry
*ce
;
1831 unsigned namelen
= strlen(filename
);
1832 struct strbuf fullname
= STRBUF_INIT
;
1837 /* Wrong stage number? */
1838 pos
= index_name_pos(istate
, filename
, namelen
);
1841 if (pos
< istate
->cache_nr
) {
1842 ce
= istate
->cache
[pos
];
1843 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1844 ce_namelen(ce
) == namelen
&&
1845 !memcmp(ce
->name
, filename
, namelen
))
1846 die(_("path '%s' is in the index, but not at stage %d\n"
1847 "hint: Did you mean ':%d:%s'?"),
1849 ce_stage(ce
), filename
);
1852 /* Confusion between relative and absolute filenames? */
1853 strbuf_addstr(&fullname
, prefix
);
1854 strbuf_addstr(&fullname
, filename
);
1855 pos
= index_name_pos(istate
, fullname
.buf
, fullname
.len
);
1858 if (pos
< istate
->cache_nr
) {
1859 ce
= istate
->cache
[pos
];
1860 if (!S_ISSPARSEDIR(ce
->ce_mode
) &&
1861 ce_namelen(ce
) == fullname
.len
&&
1862 !memcmp(ce
->name
, fullname
.buf
, fullname
.len
))
1863 die(_("path '%s' is in the index, but not '%s'\n"
1864 "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"),
1865 fullname
.buf
, filename
,
1866 ce_stage(ce
), fullname
.buf
,
1867 ce_stage(ce
), filename
);
1870 if (repo_file_exists(r
, filename
))
1871 die(_("path '%s' exists on disk, but not in the index"), filename
);
1872 if (is_missing_file_error(errno
))
1873 die(_("path '%s' does not exist (neither on disk nor in the index)"),
1876 strbuf_release(&fullname
);
1880 static char *resolve_relative_path(struct repository
*r
, const char *rel
)
1882 if (!starts_with(rel
, "./") && !starts_with(rel
, "../"))
1885 if (r
!= the_repository
|| !is_inside_work_tree())
1886 die(_("relative path syntax can't be used outside working tree"));
1888 /* die() inside prefix_path() if resolved path is outside worktree */
1889 return prefix_path(startup_info
->prefix
,
1890 startup_info
->prefix
? strlen(startup_info
->prefix
) : 0,
1894 static int reject_tree_in_index(struct repository
*repo
,
1896 const struct cache_entry
*ce
,
1901 if (!S_ISSPARSEDIR(ce
->ce_mode
))
1904 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1908 static enum get_oid_result
get_oid_with_context_1(struct repository
*repo
,
1912 struct object_id
*oid
,
1913 struct object_context
*oc
)
1915 int ret
, bracket_depth
;
1916 int namelen
= strlen(name
);
1918 int only_to_die
= flags
& GET_OID_ONLY_TO_DIE
;
1920 memset(oc
, 0, sizeof(*oc
));
1921 oc
->mode
= S_IFINVALID
;
1922 strbuf_init(&oc
->symlink_path
, 0);
1923 ret
= get_oid_1(repo
, name
, namelen
, oid
, flags
);
1924 if (!ret
&& flags
& GET_OID_REQUIRE_PATH
)
1925 die(_("<object>:<path> required, only <object> '%s' given"),
1930 * tree:path --> object name of path in tree
1931 * :path -> object name of absolute path in index
1932 * :./path -> object name of path relative to cwd in index
1933 * :[0-3]:path -> object name of path in index at stage
1934 * :/foo -> recent commit matching foo
1936 if (name
[0] == ':') {
1938 const struct cache_entry
*ce
;
1939 char *new_path
= NULL
;
1941 if (!only_to_die
&& namelen
> 2 && name
[1] == '/') {
1942 struct handle_one_ref_cb cb
;
1943 struct commit_list
*list
= NULL
;
1947 refs_for_each_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1948 refs_head_ref(get_main_ref_store(repo
), handle_one_ref
, &cb
);
1949 commit_list_sort_by_date(&list
);
1950 return get_oid_oneline(repo
, name
+ 2, oid
, list
);
1954 name
[1] < '0' || '3' < name
[1])
1957 stage
= name
[1] - '0';
1960 new_path
= resolve_relative_path(repo
, cp
);
1962 namelen
= namelen
- (cp
- name
);
1965 namelen
= strlen(cp
);
1968 if (flags
& GET_OID_RECORD_PATH
)
1969 oc
->path
= xstrdup(cp
);
1971 if (!repo
->index
|| !repo
->index
->cache
)
1972 repo_read_index(repo
);
1973 pos
= index_name_pos(repo
->index
, cp
, namelen
);
1976 while (pos
< repo
->index
->cache_nr
) {
1977 ce
= repo
->index
->cache
[pos
];
1978 if (ce_namelen(ce
) != namelen
||
1979 memcmp(ce
->name
, cp
, namelen
))
1981 if (ce_stage(ce
) == stage
) {
1983 if (reject_tree_in_index(repo
, only_to_die
, ce
,
1986 oidcpy(oid
, &ce
->oid
);
1987 oc
->mode
= ce
->ce_mode
;
1992 if (only_to_die
&& name
[1] && name
[1] != '/')
1993 diagnose_invalid_index_path(repo
, stage
, prefix
, cp
);
1997 for (cp
= name
, bracket_depth
= 0; *cp
; cp
++) {
2000 else if (bracket_depth
&& *cp
== '}')
2002 else if (!bracket_depth
&& *cp
== ':')
2006 struct object_id tree_oid
;
2007 int len
= cp
- name
;
2008 unsigned sub_flags
= flags
;
2010 sub_flags
&= ~GET_OID_DISAMBIGUATORS
;
2011 sub_flags
|= GET_OID_TREEISH
;
2013 if (!get_oid_1(repo
, name
, len
, &tree_oid
, sub_flags
)) {
2014 const char *filename
= cp
+1;
2015 char *new_filename
= NULL
;
2017 new_filename
= resolve_relative_path(repo
, filename
);
2019 filename
= new_filename
;
2020 if (flags
& GET_OID_FOLLOW_SYMLINKS
) {
2021 ret
= get_tree_entry_follow_symlinks(repo
, &tree_oid
,
2022 filename
, oid
, &oc
->symlink_path
,
2025 ret
= get_tree_entry(repo
, &tree_oid
, filename
, oid
,
2027 if (ret
&& only_to_die
) {
2028 diagnose_invalid_oid_path(repo
, prefix
,
2034 if (flags
& GET_OID_RECORD_PATH
)
2035 oc
->path
= xstrdup(filename
);
2041 die(_("invalid object name '%.*s'."), len
, name
);
2048 * Call this function when you know "name" given by the end user must
2049 * name an object but it doesn't; the function _may_ die with a better
2050 * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not
2051 * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case
2052 * you have a chance to diagnose the error further.
2054 void maybe_die_on_misspelt_object_name(struct repository
*r
,
2058 struct object_context oc
;
2059 struct object_id oid
;
2060 get_oid_with_context_1(r
, name
, GET_OID_ONLY_TO_DIE
| GET_OID_QUIETLY
,
2064 enum get_oid_result
get_oid_with_context(struct repository
*repo
,
2067 struct object_id
*oid
,
2068 struct object_context
*oc
)
2070 if (flags
& GET_OID_FOLLOW_SYMLINKS
&& flags
& GET_OID_ONLY_TO_DIE
)
2071 BUG("incompatible flags for get_oid_with_context");
2072 return get_oid_with_context_1(repo
, str
, flags
, NULL
, oid
, oc
);