Elijah Newren | bc5c5ec | 2023-05-16 06:33:57 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 2 | #include "object-name.h" |
Elijah Newren | 6c6ddf9 | 2023-04-11 03:00:39 +0000 | [diff] [blame] | 3 | #include "advice.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 4 | #include "config.h" |
Elijah Newren | 32a8f51 | 2023-03-21 06:26:03 +0000 | [diff] [blame] | 5 | #include "environment.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 6 | #include "gettext.h" |
Elijah Newren | 41771fa | 2023-02-24 00:09:27 +0000 | [diff] [blame] | 7 | #include "hex.h" |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 8 | #include "tag.h" |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 9 | #include "commit.h" |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 10 | #include "tree.h" |
Junio C Hamano | f3ab49d | 2006-04-19 11:56:53 -0700 | [diff] [blame] | 11 | #include "tree-walk.h" |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 12 | #include "refs.h" |
Johannes Schindelin | 28fb843 | 2009-09-10 17:25:57 +0200 | [diff] [blame] | 13 | #include "remote.h" |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 14 | #include "dir.h" |
Jeff King | fe299ec | 2020-03-30 10:03:46 -0400 | [diff] [blame] | 15 | #include "oid-array.h" |
Elijah Newren | e3d2f20 | 2023-04-22 20:17:27 +0000 | [diff] [blame] | 16 | #include "oidtree.h" |
Jonathan Tan | 0317f45 | 2017-08-18 15:20:19 -0700 | [diff] [blame] | 17 | #include "packfile.h" |
Elijah Newren | d4a4f92 | 2023-04-22 20:17:26 +0000 | [diff] [blame] | 18 | #include "pretty.h" |
Elijah Newren | a034e91 | 2023-05-16 06:34:06 +0000 | [diff] [blame] | 19 | #include "object-store-ll.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 20 | #include "read-cache-ll.h" |
Stefan Beller | 031dc92 | 2018-03-23 18:20:57 +0100 | [diff] [blame] | 21 | #include "repository.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 22 | #include "setup.h" |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 23 | #include "midx.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 24 | #include "commit-reach.h" |
Ævar Arnfjörð Bjarmason | 88c7b4c | 2022-02-16 09:14:02 +0100 | [diff] [blame] | 25 | #include "date.h" |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 26 | |
Nguyễn Thái Ngọc Duy | 0bb41a1 | 2019-04-16 16:33:29 +0700 | [diff] [blame] | 27 | static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 28 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 29 | typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 30 | |
| 31 | struct disambiguate_state { |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 32 | int len; /* length of prefix in hex chars */ |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 33 | char hex_pfx[GIT_MAX_HEXSZ + 1]; |
brian m. carlson | d2ee118 | 2017-03-26 16:01:33 +0000 | [diff] [blame] | 34 | struct object_id bin_pfx; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 35 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 36 | struct repository *repo; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 37 | disambiguate_hint_fn fn; |
| 38 | void *cb_data; |
brian m. carlson | d2ee118 | 2017-03-26 16:01:33 +0000 | [diff] [blame] | 39 | struct object_id candidate; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 40 | unsigned candidate_exists:1; |
| 41 | unsigned candidate_checked:1; |
| 42 | unsigned candidate_ok:1; |
| 43 | unsigned disambiguate_fn_used:1; |
| 44 | unsigned ambiguous:1; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 45 | unsigned always_call_fn:1; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
brian m. carlson | d2b7d9c | 2017-03-26 16:01:34 +0000 | [diff] [blame] | 48 | static void update_candidates(struct disambiguate_state *ds, const struct object_id *current) |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 49 | { |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 50 | if (ds->always_call_fn) { |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 51 | ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 54 | if (!ds->candidate_exists) { |
| 55 | /* this is the first candidate */ |
brian m. carlson | d2b7d9c | 2017-03-26 16:01:34 +0000 | [diff] [blame] | 56 | oidcpy(&ds->candidate, current); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 57 | ds->candidate_exists = 1; |
| 58 | return; |
Jeff King | 4a7e27e | 2018-08-28 17:22:40 -0400 | [diff] [blame] | 59 | } else if (oideq(&ds->candidate, current)) { |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 60 | /* the same as what we already have seen */ |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (!ds->fn) { |
| 65 | /* cannot disambiguate between ds->candidate and current */ |
| 66 | ds->ambiguous = 1; |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (!ds->candidate_checked) { |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 71 | ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 72 | ds->disambiguate_fn_used = 1; |
| 73 | ds->candidate_checked = 1; |
| 74 | } |
| 75 | |
| 76 | if (!ds->candidate_ok) { |
Ondřej Bílka | 749f763 | 2013-07-22 23:02:23 +0200 | [diff] [blame] | 77 | /* discard the candidate; we know it does not satisfy fn */ |
brian m. carlson | d2b7d9c | 2017-03-26 16:01:34 +0000 | [diff] [blame] | 78 | oidcpy(&ds->candidate, current); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 79 | ds->candidate_checked = 0; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | /* if we reach this point, we know ds->candidate satisfies fn */ |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 84 | if (ds->fn(ds->repo, current, ds->cb_data)) { |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 85 | /* |
| 86 | * if both current and candidate satisfy fn, we cannot |
| 87 | * disambiguate. |
| 88 | */ |
| 89 | ds->candidate_ok = 0; |
| 90 | ds->ambiguous = 1; |
| 91 | } |
| 92 | |
| 93 | /* otherwise, current can be discarded and candidate is still good */ |
| 94 | } |
| 95 | |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 96 | static int match_hash(unsigned, const unsigned char *, const unsigned char *); |
René Scharfe | cc817ca | 2017-06-22 20:19:48 +0200 | [diff] [blame] | 97 | |
Eric Wong | 92d8ed8 | 2021-07-07 23:10:19 +0000 | [diff] [blame] | 98 | static enum cb_next match_prefix(const struct object_id *oid, void *arg) |
| 99 | { |
| 100 | struct disambiguate_state *ds = arg; |
| 101 | /* no need to call match_hash, oidtree_each did prefix match */ |
| 102 | update_candidates(ds, oid); |
| 103 | return ds->ambiguous ? CB_BREAK : CB_CONTINUE; |
| 104 | } |
| 105 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 106 | static void find_short_object_filename(struct disambiguate_state *ds) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 107 | { |
Jeff King | 263db40 | 2018-11-12 09:48:47 -0500 | [diff] [blame] | 108 | struct object_directory *odb; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 109 | |
Eric Wong | 92d8ed8 | 2021-07-07 23:10:19 +0000 | [diff] [blame] | 110 | for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) |
| 111 | oidtree_each(odb_loose_cache(odb, &ds->bin_pfx), |
| 112 | &ds->bin_pfx, ds->len, match_prefix, ds); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 115 | static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 116 | { |
| 117 | do { |
| 118 | if (*a != *b) |
| 119 | return 0; |
| 120 | a++; |
| 121 | b++; |
| 122 | len -= 2; |
| 123 | } while (len > 1); |
| 124 | if (len) |
| 125 | if ((*a ^ *b) & 0xf0) |
| 126 | return 0; |
| 127 | return 1; |
| 128 | } |
| 129 | |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 130 | static void unique_in_midx(struct multi_pack_index *m, |
| 131 | struct disambiguate_state *ds) |
| 132 | { |
| 133 | uint32_t num, i, first = 0; |
| 134 | const struct object_id *current = NULL; |
| 135 | num = m->num_objects; |
| 136 | |
| 137 | if (!num) |
| 138 | return; |
| 139 | |
| 140 | bsearch_midx(&ds->bin_pfx, m, &first); |
| 141 | |
| 142 | /* |
| 143 | * At this point, "first" is the location of the lowest object |
| 144 | * with an object name that could match "bin_pfx". See if we have |
| 145 | * 0, 1 or more objects that actually match(es). |
| 146 | */ |
| 147 | for (i = first; i < num && !ds->ambiguous; i++) { |
| 148 | struct object_id oid; |
| 149 | current = nth_midxed_object_oid(&oid, m, i); |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 150 | if (!match_hash(ds->len, ds->bin_pfx.hash, current->hash)) |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 151 | break; |
| 152 | update_candidates(ds, current); |
| 153 | } |
| 154 | } |
| 155 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 156 | static void unique_in_pack(struct packed_git *p, |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 157 | struct disambiguate_state *ds) |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 158 | { |
René Scharfe | 902f5a2 | 2018-03-24 17:41:08 +0100 | [diff] [blame] | 159 | uint32_t num, i, first = 0; |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 160 | |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 161 | if (p->multi_pack_index) |
| 162 | return; |
| 163 | |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 164 | if (open_pack_index(p) || !p->num_objects) |
| 165 | return; |
| 166 | |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 167 | num = p->num_objects; |
René Scharfe | 902f5a2 | 2018-03-24 17:41:08 +0100 | [diff] [blame] | 168 | bsearch_pack(&ds->bin_pfx, p, &first); |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * At this point, "first" is the location of the lowest object |
Junio C Hamano | 1703f9a | 2012-06-20 22:35:43 -0700 | [diff] [blame] | 172 | * with an object name that could match "bin_pfx". See if we have |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 173 | * 0, 1 or more objects that actually match(es). |
| 174 | */ |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 175 | for (i = first; i < num && !ds->ambiguous; i++) { |
brian m. carlson | d2b7d9c | 2017-03-26 16:01:34 +0000 | [diff] [blame] | 176 | struct object_id oid; |
Jeff King | 0763671 | 2020-02-23 23:27:36 -0500 | [diff] [blame] | 177 | nth_packed_object_id(&oid, p, i); |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 178 | if (!match_hash(ds->len, ds->bin_pfx.hash, oid.hash)) |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 179 | break; |
Jeff King | 0763671 | 2020-02-23 23:27:36 -0500 | [diff] [blame] | 180 | update_candidates(ds, &oid); |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 181 | } |
Junio C Hamano | f703e6e | 2012-06-18 13:10:38 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 184 | static void find_short_packed_object(struct disambiguate_state *ds) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 185 | { |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 186 | struct multi_pack_index *m; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 187 | struct packed_git *p; |
| 188 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 189 | for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous; |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 190 | m = m->next) |
| 191 | unique_in_midx(m, ds); |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 192 | for (p = get_packed_git(ds->repo); p && !ds->ambiguous; |
Stefan Beller | a80d72d | 2018-03-23 18:20:59 +0100 | [diff] [blame] | 193 | p = p->next) |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 194 | unique_in_pack(p, ds); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 197 | static int finish_object_disambiguation(struct disambiguate_state *ds, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 198 | struct object_id *oid) |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 199 | { |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 200 | if (ds->ambiguous) |
| 201 | return SHORT_NAME_AMBIGUOUS; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 202 | |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 203 | if (!ds->candidate_exists) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 204 | return MISSING_OBJECT; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 205 | |
| 206 | if (!ds->candidate_checked) |
| 207 | /* |
| 208 | * If this is the only candidate, there is no point |
| 209 | * calling the disambiguation hint callback. |
| 210 | * |
| 211 | * On the other hand, if the current candidate |
| 212 | * replaced an earlier candidate that did _not_ pass |
| 213 | * the disambiguation hint callback, then we do have |
| 214 | * more than one objects that match the short name |
| 215 | * given, so we should make sure this one matches; |
| 216 | * otherwise, if we discovered this one and the one |
| 217 | * that we previously discarded in the reverse order, |
| 218 | * we would end up showing different results in the |
| 219 | * same repository! |
| 220 | */ |
| 221 | ds->candidate_ok = (!ds->disambiguate_fn_used || |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 222 | ds->fn(ds->repo, &ds->candidate, ds->cb_data)); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 223 | |
| 224 | if (!ds->candidate_ok) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 225 | return SHORT_NAME_AMBIGUOUS; |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 226 | |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 227 | oidcpy(oid, &ds->candidate); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 231 | static int disambiguate_commit_only(struct repository *r, |
| 232 | const struct object_id *oid, |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 233 | void *cb_data UNUSED) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 234 | { |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 235 | int kind = oid_object_info(r, oid, NULL); |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 236 | return kind == OBJ_COMMIT; |
| 237 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 238 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 239 | static int disambiguate_committish_only(struct repository *r, |
| 240 | const struct object_id *oid, |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 241 | void *cb_data UNUSED) |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 242 | { |
| 243 | struct object *obj; |
| 244 | int kind; |
| 245 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 246 | kind = oid_object_info(r, oid, NULL); |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 247 | if (kind == OBJ_COMMIT) |
| 248 | return 1; |
| 249 | if (kind != OBJ_TAG) |
| 250 | return 0; |
| 251 | |
| 252 | /* We need to do this the hard way... */ |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 253 | obj = deref_tag(r, parse_object(r, oid), NULL, 0); |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 254 | if (obj && obj->type == OBJ_COMMIT) |
| 255 | return 1; |
| 256 | return 0; |
| 257 | } |
| 258 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 259 | static int disambiguate_tree_only(struct repository *r, |
| 260 | const struct object_id *oid, |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 261 | void *cb_data UNUSED) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 262 | { |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 263 | int kind = oid_object_info(r, oid, NULL); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 264 | return kind == OBJ_TREE; |
| 265 | } |
| 266 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 267 | static int disambiguate_treeish_only(struct repository *r, |
| 268 | const struct object_id *oid, |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 269 | void *cb_data UNUSED) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 270 | { |
| 271 | struct object *obj; |
| 272 | int kind; |
| 273 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 274 | kind = oid_object_info(r, oid, NULL); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 275 | if (kind == OBJ_TREE || kind == OBJ_COMMIT) |
| 276 | return 1; |
| 277 | if (kind != OBJ_TAG) |
| 278 | return 0; |
| 279 | |
| 280 | /* We need to do this the hard way... */ |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 281 | obj = deref_tag(r, parse_object(r, oid), NULL, 0); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 282 | if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT)) |
| 283 | return 1; |
| 284 | return 0; |
| 285 | } |
| 286 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 287 | static int disambiguate_blob_only(struct repository *r, |
| 288 | const struct object_id *oid, |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 289 | void *cb_data UNUSED) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 290 | { |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 291 | int kind = oid_object_info(r, oid, NULL); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 292 | return kind == OBJ_BLOB; |
| 293 | } |
| 294 | |
Jeff King | 5b33cb1 | 2016-09-27 08:38:01 -0400 | [diff] [blame] | 295 | static disambiguate_hint_fn default_disambiguate_hint; |
| 296 | |
| 297 | int set_disambiguate_hint_config(const char *var, const char *value) |
| 298 | { |
| 299 | static const struct { |
| 300 | const char *name; |
| 301 | disambiguate_hint_fn fn; |
| 302 | } hints[] = { |
| 303 | { "none", NULL }, |
| 304 | { "commit", disambiguate_commit_only }, |
| 305 | { "committish", disambiguate_committish_only }, |
| 306 | { "tree", disambiguate_tree_only }, |
| 307 | { "treeish", disambiguate_treeish_only }, |
| 308 | { "blob", disambiguate_blob_only } |
| 309 | }; |
| 310 | int i; |
| 311 | |
| 312 | if (!value) |
| 313 | return config_error_nonbool(var); |
| 314 | |
| 315 | for (i = 0; i < ARRAY_SIZE(hints); i++) { |
| 316 | if (!strcasecmp(value, hints[i].name)) { |
| 317 | default_disambiguate_hint = hints[i].fn; |
| 318 | return 0; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | return error("unknown hint type for '%s': %s", var, value); |
| 323 | } |
| 324 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 325 | static int init_object_disambiguation(struct repository *r, |
| 326 | const char *name, int len, |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 327 | struct disambiguate_state *ds) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 328 | { |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 329 | int i; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 330 | |
brian m. carlson | 7b38efa | 2018-07-16 01:27:58 +0000 | [diff] [blame] | 331 | if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz) |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 332 | return -1; |
| 333 | |
| 334 | memset(ds, 0, sizeof(*ds)); |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 335 | |
Linus Torvalds | af61c6e | 2005-09-19 15:16:03 -0700 | [diff] [blame] | 336 | for (i = 0; i < len ;i++) { |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 337 | unsigned char c = name[i]; |
| 338 | unsigned char val; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 339 | if (c >= '0' && c <= '9') |
| 340 | val = c - '0'; |
| 341 | else if (c >= 'a' && c <= 'f') |
| 342 | val = c - 'a' + 10; |
| 343 | else if (c >= 'A' && c <='F') { |
| 344 | val = c - 'A' + 10; |
| 345 | c -= 'A' - 'a'; |
| 346 | } |
| 347 | else |
| 348 | return -1; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 349 | ds->hex_pfx[i] = c; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 350 | if (!(i & 1)) |
| 351 | val <<= 4; |
brian m. carlson | d2ee118 | 2017-03-26 16:01:33 +0000 | [diff] [blame] | 352 | ds->bin_pfx.hash[i >> 1] |= val; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 353 | } |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 354 | |
| 355 | ds->len = len; |
Jeff King | 59e4e34 | 2016-09-26 08:00:07 -0400 | [diff] [blame] | 356 | ds->hex_pfx[len] = '\0'; |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 357 | ds->repo = r; |
| 358 | prepare_alt_odb(r); |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 359 | return 0; |
| 360 | } |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 361 | |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 362 | struct ambiguous_output { |
| 363 | const struct disambiguate_state *ds; |
| 364 | struct strbuf advice; |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 365 | struct strbuf sb; |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 366 | }; |
| 367 | |
brian m. carlson | 1b7ba79 | 2017-03-31 01:39:59 +0000 | [diff] [blame] | 368 | static int show_ambiguous_object(const struct object_id *oid, void *data) |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 369 | { |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 370 | struct ambiguous_output *state = data; |
| 371 | const struct disambiguate_state *ds = state->ds; |
| 372 | struct strbuf *advice = &state->advice; |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 373 | struct strbuf *sb = &state->sb; |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 374 | int type; |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 375 | const char *hash; |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 376 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 377 | if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data)) |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 378 | return 0; |
| 379 | |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 380 | hash = repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV); |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 381 | type = oid_object_info(ds->repo, oid, NULL); |
Ævar Arnfjörð Bjarmason | 6780e68 | 2022-01-27 06:26:44 +0100 | [diff] [blame] | 382 | |
| 383 | if (type < 0) { |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 384 | /* |
| 385 | * TRANSLATORS: This is a line of ambiguous object |
| 386 | * output shown when we cannot look up or parse the |
| 387 | * object in question. E.g. "deadbeef [bad object]". |
| 388 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 389 | strbuf_addf(sb, _("%s [bad object]"), hash); |
Ævar Arnfjörð Bjarmason | 6780e68 | 2022-01-27 06:26:44 +0100 | [diff] [blame] | 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | assert(type == OBJ_TREE || type == OBJ_COMMIT || |
| 394 | type == OBJ_BLOB || type == OBJ_TAG); |
Ævar Arnfjörð Bjarmason | 6780e68 | 2022-01-27 06:26:44 +0100 | [diff] [blame] | 395 | |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 396 | if (type == OBJ_COMMIT) { |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 397 | struct strbuf date = STRBUF_INIT; |
| 398 | struct strbuf msg = STRBUF_INIT; |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 399 | struct commit *commit = lookup_commit(ds->repo, oid); |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 400 | |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 401 | if (commit) { |
| 402 | struct pretty_print_context pp = {0}; |
| 403 | pp.date_mode.type = DATE_SHORT; |
Ævar Arnfjörð Bjarmason | bab8216 | 2023-03-28 15:58:51 +0200 | [diff] [blame] | 404 | repo_format_commit_message(the_repository, commit, |
| 405 | "%ad", &date, &pp); |
| 406 | repo_format_commit_message(the_repository, commit, |
| 407 | "%s", &msg, &pp); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 408 | } |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | * TRANSLATORS: This is a line of ambiguous commit |
| 412 | * object output. E.g.: |
| 413 | * |
| 414 | * "deadbeef commit 2021-01-01 - Some Commit Message" |
| 415 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 416 | strbuf_addf(sb, _("%s commit %s - %s"), hash, date.buf, |
| 417 | msg.buf); |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 418 | |
| 419 | strbuf_release(&date); |
| 420 | strbuf_release(&msg); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 421 | } else if (type == OBJ_TAG) { |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 422 | struct tag *tag = lookup_tag(ds->repo, oid); |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 423 | |
| 424 | if (!parse_tag(tag) && tag->tag) { |
| 425 | /* |
| 426 | * TRANSLATORS: This is a line of ambiguous |
| 427 | * tag object output. E.g.: |
| 428 | * |
Ævar Arnfjörð Bjarmason | 851b3d7 | 2022-01-27 06:26:47 +0100 | [diff] [blame] | 429 | * "deadbeef tag 2022-01-01 - Some Tag Message" |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 430 | * |
Ævar Arnfjörð Bjarmason | 851b3d7 | 2022-01-27 06:26:47 +0100 | [diff] [blame] | 431 | * The second argument is the YYYY-MM-DD found |
| 432 | * in the tag. |
| 433 | * |
| 434 | * The third argument is the "tag" string |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 435 | * from object.c. |
| 436 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 437 | strbuf_addf(sb, _("%s tag %s - %s"), hash, |
Ævar Arnfjörð Bjarmason | 851b3d7 | 2022-01-27 06:26:47 +0100 | [diff] [blame] | 438 | show_date(tag->date, 0, DATE_MODE(SHORT)), |
| 439 | tag->tag); |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 440 | } else { |
| 441 | /* |
| 442 | * TRANSLATORS: This is a line of ambiguous |
| 443 | * tag object output where we couldn't parse |
| 444 | * the tag itself. E.g.: |
| 445 | * |
Ævar Arnfjörð Bjarmason | 851b3d7 | 2022-01-27 06:26:47 +0100 | [diff] [blame] | 446 | * "deadbeef [bad tag, could not parse it]" |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 447 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 448 | strbuf_addf(sb, _("%s [bad tag, could not parse it]"), |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 449 | hash); |
| 450 | } |
| 451 | } else if (type == OBJ_TREE) { |
| 452 | /* |
| 453 | * TRANSLATORS: This is a line of ambiguous <type> |
| 454 | * object output. E.g. "deadbeef tree". |
| 455 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 456 | strbuf_addf(sb, _("%s tree"), hash); |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 457 | } else if (type == OBJ_BLOB) { |
| 458 | /* |
| 459 | * TRANSLATORS: This is a line of ambiguous <type> |
| 460 | * object output. E.g. "deadbeef blob". |
| 461 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 462 | strbuf_addf(sb, _("%s blob"), hash); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 463 | } |
| 464 | |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 465 | |
Ævar Arnfjörð Bjarmason | 6780e68 | 2022-01-27 06:26:44 +0100 | [diff] [blame] | 466 | out: |
Ævar Arnfjörð Bjarmason | ba5e8a0 | 2022-01-27 06:26:46 +0100 | [diff] [blame] | 467 | /* |
| 468 | * TRANSLATORS: This is line item of ambiguous object output |
| 469 | * from describe_ambiguous_object() above. For RTL languages |
| 470 | * you'll probably want to swap the "%s" and leading " " space |
| 471 | * around. |
| 472 | */ |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 473 | strbuf_addf(advice, _(" %s\n"), sb->buf); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 474 | |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 475 | strbuf_reset(sb); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Ævar Arnfjörð Bjarmason | a885c93 | 2018-05-10 12:43:01 +0000 | [diff] [blame] | 479 | static int collect_ambiguous(const struct object_id *oid, void *data) |
| 480 | { |
| 481 | oid_array_append(data, oid); |
| 482 | return 0; |
| 483 | } |
| 484 | |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 485 | static int repo_collect_ambiguous(struct repository *r UNUSED, |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 486 | const struct object_id *oid, |
| 487 | void *data) |
| 488 | { |
| 489 | return collect_ambiguous(oid, data); |
| 490 | } |
| 491 | |
René Scharfe | 7cfcb16 | 2019-08-20 20:49:12 +0200 | [diff] [blame] | 492 | static int sort_ambiguous(const void *a, const void *b, void *ctx) |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 493 | { |
René Scharfe | 7cfcb16 | 2019-08-20 20:49:12 +0200 | [diff] [blame] | 494 | struct repository *sort_ambiguous_repo = ctx; |
Nguyễn Thái Ngọc Duy | fae2ae4 | 2019-04-16 16:33:20 +0700 | [diff] [blame] | 495 | int a_type = oid_object_info(sort_ambiguous_repo, a, NULL); |
| 496 | int b_type = oid_object_info(sort_ambiguous_repo, b, NULL); |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 497 | int a_type_sort; |
| 498 | int b_type_sort; |
| 499 | |
| 500 | /* |
| 501 | * Sorts by hash within the same object type, just as |
| 502 | * oid_array_for_each_unique() would do. |
| 503 | */ |
| 504 | if (a_type == b_type) |
| 505 | return oidcmp(a, b); |
| 506 | |
| 507 | /* |
| 508 | * Between object types show tags, then commits, and finally |
| 509 | * trees and blobs. |
| 510 | * |
| 511 | * The object_type enum is commit, tree, blob, tag, but we |
| 512 | * want tag, commit, tree blob. Cleverly (perhaps too |
| 513 | * cleverly) do that with modulus, since the enum assigns 1 to |
| 514 | * commit, so tag becomes 0. |
| 515 | */ |
| 516 | a_type_sort = a_type % 4; |
| 517 | b_type_sort = b_type % 4; |
| 518 | return a_type_sort > b_type_sort ? 1 : -1; |
| 519 | } |
| 520 | |
Nguyễn Thái Ngọc Duy | fae2ae4 | 2019-04-16 16:33:20 +0700 | [diff] [blame] | 521 | static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a) |
| 522 | { |
René Scharfe | 7cfcb16 | 2019-08-20 20:49:12 +0200 | [diff] [blame] | 523 | QSORT_S(a->oid, a->nr, sort_ambiguous, r); |
Nguyễn Thái Ngọc Duy | fae2ae4 | 2019-04-16 16:33:20 +0700 | [diff] [blame] | 524 | } |
| 525 | |
Nguyễn Thái Ngọc Duy | c6c0235 | 2019-04-16 16:33:25 +0700 | [diff] [blame] | 526 | static enum get_oid_result get_short_oid(struct repository *r, |
| 527 | const char *name, int len, |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 528 | struct object_id *oid, |
| 529 | unsigned flags) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 530 | { |
| 531 | int status; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 532 | struct disambiguate_state ds; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 533 | int quietly = !!(flags & GET_OID_QUIETLY); |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 534 | |
Nguyễn Thái Ngọc Duy | c6c0235 | 2019-04-16 16:33:25 +0700 | [diff] [blame] | 535 | if (init_object_disambiguation(r, name, len, &ds) < 0) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 536 | return -1; |
Jeff King | 259942f | 2016-09-26 07:59:01 -0400 | [diff] [blame] | 537 | |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 538 | if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS)) |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 539 | BUG("multiple get_short_oid disambiguator flags"); |
Jeff King | 259942f | 2016-09-26 07:59:01 -0400 | [diff] [blame] | 540 | |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 541 | if (flags & GET_OID_COMMIT) |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 542 | ds.fn = disambiguate_commit_only; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 543 | else if (flags & GET_OID_COMMITTISH) |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 544 | ds.fn = disambiguate_committish_only; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 545 | else if (flags & GET_OID_TREE) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 546 | ds.fn = disambiguate_tree_only; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 547 | else if (flags & GET_OID_TREEISH) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 548 | ds.fn = disambiguate_treeish_only; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 549 | else if (flags & GET_OID_BLOB) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 550 | ds.fn = disambiguate_blob_only; |
Jeff King | 5b33cb1 | 2016-09-27 08:38:01 -0400 | [diff] [blame] | 551 | else |
| 552 | ds.fn = default_disambiguate_hint; |
Junio C Hamano | aa1dec9 | 2012-06-20 23:03:09 -0700 | [diff] [blame] | 553 | |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 554 | find_short_object_filename(&ds); |
| 555 | find_short_packed_object(&ds); |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 556 | status = finish_object_disambiguation(&ds, oid); |
Junio C Hamano | a78fafe | 2012-06-20 22:07:36 -0700 | [diff] [blame] | 557 | |
Johannes Schindelin | 6d67a99 | 2019-03-13 03:16:34 -0700 | [diff] [blame] | 558 | /* |
| 559 | * If we didn't find it, do the usual reprepare() slow-path, |
| 560 | * since the object may have recently been added to the repository |
| 561 | * or migrated from loose to packed. |
| 562 | */ |
| 563 | if (status == MISSING_OBJECT) { |
Nguyễn Thái Ngọc Duy | 34e7771 | 2019-06-27 16:28:52 +0700 | [diff] [blame] | 564 | reprepare_packed_git(r); |
Johannes Schindelin | 6d67a99 | 2019-03-13 03:16:34 -0700 | [diff] [blame] | 565 | find_short_object_filename(&ds); |
| 566 | find_short_packed_object(&ds); |
| 567 | status = finish_object_disambiguation(&ds, oid); |
| 568 | } |
| 569 | |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 570 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) { |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 571 | struct oid_array collect = OID_ARRAY_INIT; |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 572 | struct ambiguous_output out = { |
| 573 | .ds = &ds, |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 574 | .sb = STRBUF_INIT, |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 575 | .advice = STRBUF_INIT, |
| 576 | }; |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 577 | |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 578 | error(_("short object ID %s is ambiguous"), ds.hex_pfx); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * We may still have ambiguity if we simply saw a series of |
| 582 | * candidates that did not satisfy our hint function. In |
| 583 | * that case, we still want to show them, so disable the hint |
| 584 | * function entirely. |
| 585 | */ |
| 586 | if (!ds.ambiguous) |
| 587 | ds.fn = NULL; |
| 588 | |
Nguyễn Thái Ngọc Duy | c6c0235 | 2019-04-16 16:33:25 +0700 | [diff] [blame] | 589 | repo_for_each_abbrev(r, ds.hex_pfx, collect_ambiguous, &collect); |
| 590 | sort_ambiguous_oid_array(r, &collect); |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 591 | |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 592 | if (oid_array_for_each(&collect, show_ambiguous_object, &out)) |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 593 | BUG("show_ambiguous_object shouldn't return non-zero"); |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 594 | |
| 595 | /* |
| 596 | * TRANSLATORS: The argument is the list of ambiguous |
| 597 | * objects composed in show_ambiguous_object(). See |
| 598 | * its "TRANSLATORS" comments for details. |
| 599 | */ |
| 600 | advise(_("The candidates are:\n%s"), out.advice.buf); |
| 601 | |
Ævar Arnfjörð Bjarmason | 5cc044e | 2018-05-10 12:43:02 +0000 | [diff] [blame] | 602 | oid_array_clear(&collect); |
Ævar Arnfjörð Bjarmason | d2ef3cb | 2022-01-27 06:26:48 +0100 | [diff] [blame] | 603 | strbuf_release(&out.advice); |
Ævar Arnfjörð Bjarmason | 3a73c1d | 2022-01-27 06:26:49 +0100 | [diff] [blame] | 604 | strbuf_release(&out.sb); |
Jeff King | 1ffa26c | 2016-09-26 08:00:36 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 607 | return status; |
| 608 | } |
| 609 | |
Nguyễn Thái Ngọc Duy | 4e99f2d | 2019-04-16 16:33:24 +0700 | [diff] [blame] | 610 | int repo_for_each_abbrev(struct repository *r, const char *prefix, |
| 611 | each_abbrev_fn fn, void *cb_data) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 612 | { |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 613 | struct oid_array collect = OID_ARRAY_INIT; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 614 | struct disambiguate_state ds; |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 615 | int ret; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 616 | |
Nguyễn Thái Ngọc Duy | 4e99f2d | 2019-04-16 16:33:24 +0700 | [diff] [blame] | 617 | if (init_object_disambiguation(r, prefix, strlen(prefix), &ds) < 0) |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 618 | return -1; |
| 619 | |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 620 | ds.always_call_fn = 1; |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 621 | ds.fn = repo_collect_ambiguous; |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 622 | ds.cb_data = &collect; |
Jeff King | 0016043 | 2016-09-26 08:00:04 -0400 | [diff] [blame] | 623 | find_short_object_filename(&ds); |
| 624 | find_short_packed_object(&ds); |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 625 | |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 626 | ret = oid_array_for_each_unique(&collect, fn, cb_data); |
| 627 | oid_array_clear(&collect); |
Jeff King | fad6b9e | 2016-09-26 08:00:33 -0400 | [diff] [blame] | 628 | return ret; |
Junio C Hamano | 957d740 | 2012-07-03 14:21:59 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 631 | /* |
| 632 | * Return the slot of the most-significant bit set in "val". There are various |
| 633 | * ways to do this quickly with fls() or __builtin_clzl(), but speed is |
| 634 | * probably not a big deal here. |
| 635 | */ |
| 636 | static unsigned msb(unsigned long val) |
| 637 | { |
| 638 | unsigned r = 0; |
| 639 | while (val >>= 1) |
| 640 | r++; |
| 641 | return r; |
| 642 | } |
| 643 | |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 644 | struct min_abbrev_data { |
| 645 | unsigned int init_len; |
| 646 | unsigned int cur_len; |
| 647 | char *hex; |
Nguyễn Thái Ngọc Duy | 7f07c03 | 2019-04-16 16:33:21 +0700 | [diff] [blame] | 648 | struct repository *repo; |
brian m. carlson | 626fd98 | 2018-03-22 13:40:08 -0400 | [diff] [blame] | 649 | const struct object_id *oid; |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 650 | }; |
| 651 | |
Derrick Stolee | a42d6fd | 2017-10-12 08:02:19 -0400 | [diff] [blame] | 652 | static inline char get_hex_char_from_oid(const struct object_id *oid, |
| 653 | unsigned int pos) |
| 654 | { |
| 655 | static const char hex[] = "0123456789abcdef"; |
| 656 | |
| 657 | if ((pos & 1) == 0) |
| 658 | return hex[oid->hash[pos >> 1] >> 4]; |
| 659 | else |
| 660 | return hex[oid->hash[pos >> 1] & 0xf]; |
| 661 | } |
| 662 | |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 663 | static int extend_abbrev_len(const struct object_id *oid, void *cb_data) |
| 664 | { |
| 665 | struct min_abbrev_data *mad = cb_data; |
| 666 | |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 667 | unsigned int i = mad->init_len; |
Derrick Stolee | a42d6fd | 2017-10-12 08:02:19 -0400 | [diff] [blame] | 668 | while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i)) |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 669 | i++; |
| 670 | |
| 671 | if (i < GIT_MAX_RAWSZ && i >= mad->cur_len) |
| 672 | mad->cur_len = i + 1; |
| 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Jeff King | 07ffb95 | 2023-02-24 01:38:30 -0500 | [diff] [blame] | 677 | static int repo_extend_abbrev_len(struct repository *r UNUSED, |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 678 | const struct object_id *oid, |
| 679 | void *cb_data) |
| 680 | { |
| 681 | return extend_abbrev_len(oid, cb_data); |
| 682 | } |
| 683 | |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 684 | static void find_abbrev_len_for_midx(struct multi_pack_index *m, |
| 685 | struct min_abbrev_data *mad) |
| 686 | { |
| 687 | int match = 0; |
| 688 | uint32_t num, first = 0; |
| 689 | struct object_id oid; |
| 690 | const struct object_id *mad_oid; |
| 691 | |
| 692 | if (!m->num_objects) |
| 693 | return; |
| 694 | |
| 695 | num = m->num_objects; |
| 696 | mad_oid = mad->oid; |
| 697 | match = bsearch_midx(mad_oid, m, &first); |
| 698 | |
| 699 | /* |
| 700 | * first is now the position in the packfile where we would insert |
| 701 | * mad->hash if it does not exist (or the position of mad->hash if |
| 702 | * it does exist). Hence, we consider a maximum of two objects |
| 703 | * nearby for the abbreviation length. |
| 704 | */ |
| 705 | mad->init_len = 0; |
| 706 | if (!match) { |
| 707 | if (nth_midxed_object_oid(&oid, m, first)) |
| 708 | extend_abbrev_len(&oid, mad); |
| 709 | } else if (first < num - 1) { |
| 710 | if (nth_midxed_object_oid(&oid, m, first + 1)) |
| 711 | extend_abbrev_len(&oid, mad); |
| 712 | } |
| 713 | if (first > 0) { |
| 714 | if (nth_midxed_object_oid(&oid, m, first - 1)) |
| 715 | extend_abbrev_len(&oid, mad); |
| 716 | } |
| 717 | mad->init_len = mad->cur_len; |
| 718 | } |
| 719 | |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 720 | static void find_abbrev_len_for_pack(struct packed_git *p, |
| 721 | struct min_abbrev_data *mad) |
| 722 | { |
| 723 | int match = 0; |
Derrick Stolee | 0aaf05b | 2018-03-22 13:40:10 -0400 | [diff] [blame] | 724 | uint32_t num, first = 0; |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 725 | struct object_id oid; |
Derrick Stolee | 0aaf05b | 2018-03-22 13:40:10 -0400 | [diff] [blame] | 726 | const struct object_id *mad_oid; |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 727 | |
Derrick Stolee | af96fe3 | 2019-04-29 09:18:56 -0700 | [diff] [blame] | 728 | if (p->multi_pack_index) |
| 729 | return; |
| 730 | |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 731 | if (open_pack_index(p) || !p->num_objects) |
| 732 | return; |
| 733 | |
| 734 | num = p->num_objects; |
Derrick Stolee | 0aaf05b | 2018-03-22 13:40:10 -0400 | [diff] [blame] | 735 | mad_oid = mad->oid; |
| 736 | match = bsearch_pack(mad_oid, p, &first); |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 737 | |
| 738 | /* |
| 739 | * first is now the position in the packfile where we would insert |
| 740 | * mad->hash if it does not exist (or the position of mad->hash if |
Derrick Stolee | 21abed5 | 2018-02-27 06:47:04 -0500 | [diff] [blame] | 741 | * it does exist). Hence, we consider a maximum of two objects |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 742 | * nearby for the abbreviation length. |
| 743 | */ |
| 744 | mad->init_len = 0; |
| 745 | if (!match) { |
Jeff King | 0763671 | 2020-02-23 23:27:36 -0500 | [diff] [blame] | 746 | if (!nth_packed_object_id(&oid, p, first)) |
Derrick Stolee | 21abed5 | 2018-02-27 06:47:04 -0500 | [diff] [blame] | 747 | extend_abbrev_len(&oid, mad); |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 748 | } else if (first < num - 1) { |
Jeff King | 0763671 | 2020-02-23 23:27:36 -0500 | [diff] [blame] | 749 | if (!nth_packed_object_id(&oid, p, first + 1)) |
Derrick Stolee | 21abed5 | 2018-02-27 06:47:04 -0500 | [diff] [blame] | 750 | extend_abbrev_len(&oid, mad); |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 751 | } |
| 752 | if (first > 0) { |
Jeff King | 0763671 | 2020-02-23 23:27:36 -0500 | [diff] [blame] | 753 | if (!nth_packed_object_id(&oid, p, first - 1)) |
Derrick Stolee | 21abed5 | 2018-02-27 06:47:04 -0500 | [diff] [blame] | 754 | extend_abbrev_len(&oid, mad); |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 755 | } |
| 756 | mad->init_len = mad->cur_len; |
| 757 | } |
| 758 | |
| 759 | static void find_abbrev_len_packed(struct min_abbrev_data *mad) |
| 760 | { |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 761 | struct multi_pack_index *m; |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 762 | struct packed_git *p; |
| 763 | |
Nguyễn Thái Ngọc Duy | 7f07c03 | 2019-04-16 16:33:21 +0700 | [diff] [blame] | 764 | for (m = get_multi_pack_index(mad->repo); m; m = m->next) |
Derrick Stolee | 8aac67a | 2018-07-12 15:39:35 -0400 | [diff] [blame] | 765 | find_abbrev_len_for_midx(m, mad); |
Nguyễn Thái Ngọc Duy | 7f07c03 | 2019-04-16 16:33:21 +0700 | [diff] [blame] | 766 | for (p = get_packed_git(mad->repo); p; p = p->next) |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 767 | find_abbrev_len_for_pack(p, mad); |
| 768 | } |
| 769 | |
Calvin Wan | f940185 | 2023-06-06 19:48:41 +0000 | [diff] [blame] | 770 | void strbuf_repo_add_unique_abbrev(struct strbuf *sb, struct repository *repo, |
| 771 | const struct object_id *oid, int abbrev_len) |
| 772 | { |
| 773 | int r; |
| 774 | strbuf_grow(sb, GIT_MAX_HEXSZ + 1); |
| 775 | r = repo_find_unique_abbrev_r(repo, sb->buf + sb->len, oid, abbrev_len); |
| 776 | strbuf_setlen(sb, sb->len + r); |
| 777 | } |
| 778 | |
| 779 | void strbuf_add_unique_abbrev(struct strbuf *sb, const struct object_id *oid, |
| 780 | int abbrev_len) |
| 781 | { |
| 782 | strbuf_repo_add_unique_abbrev(sb, the_repository, oid, abbrev_len); |
| 783 | } |
| 784 | |
Nguyễn Thái Ngọc Duy | 8bb9557 | 2019-04-16 16:33:22 +0700 | [diff] [blame] | 785 | int repo_find_unique_abbrev_r(struct repository *r, char *hex, |
| 786 | const struct object_id *oid, int len) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 787 | { |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 788 | struct disambiguate_state ds; |
| 789 | struct min_abbrev_data mad; |
| 790 | struct object_id oid_ret; |
Nguyễn Thái Ngọc Duy | 8bb9557 | 2019-04-16 16:33:22 +0700 | [diff] [blame] | 791 | const unsigned hexsz = r->hash_algo->hexsz; |
brian m. carlson | 7b38efa | 2018-07-16 01:27:58 +0000 | [diff] [blame] | 792 | |
Linus Torvalds | e6c587c | 2016-09-30 17:19:37 -0700 | [diff] [blame] | 793 | if (len < 0) { |
Nguyễn Thái Ngọc Duy | 8bb9557 | 2019-04-16 16:33:22 +0700 | [diff] [blame] | 794 | unsigned long count = repo_approximate_object_count(r); |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 795 | /* |
| 796 | * Add one because the MSB only tells us the highest bit set, |
| 797 | * not including the value of all the _other_ bits (so "15" |
| 798 | * is only one off of 2^4, but the MSB is the 3rd bit. |
| 799 | */ |
| 800 | len = msb(count) + 1; |
| 801 | /* |
| 802 | * We now know we have on the order of 2^len objects, which |
| 803 | * expects a collision at 2^(len/2). But we also care about hex |
| 804 | * chars, not bits, and there are 4 bits per hex. So all |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 805 | * together we need to divide by 2 and round up. |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 806 | */ |
René Scharfe | 42c78a2 | 2017-07-08 12:35:35 +0200 | [diff] [blame] | 807 | len = DIV_ROUND_UP(len, 2); |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 808 | /* |
| 809 | * For very small repos, we stick with our regular fallback. |
| 810 | */ |
| 811 | if (len < FALLBACK_DEFAULT_ABBREV) |
| 812 | len = FALLBACK_DEFAULT_ABBREV; |
Linus Torvalds | e6c587c | 2016-09-30 17:19:37 -0700 | [diff] [blame] | 813 | } |
Jeff King | 8e3f52d | 2016-10-03 19:47:28 -0400 | [diff] [blame] | 814 | |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 815 | oid_to_hex_r(hex, oid); |
brian m. carlson | 7b38efa | 2018-07-16 01:27:58 +0000 | [diff] [blame] | 816 | if (len == hexsz || !len) |
| 817 | return hexsz; |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 818 | |
Nguyễn Thái Ngọc Duy | 8bb9557 | 2019-04-16 16:33:22 +0700 | [diff] [blame] | 819 | mad.repo = r; |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 820 | mad.init_len = len; |
| 821 | mad.cur_len = len; |
| 822 | mad.hex = hex; |
brian m. carlson | 626fd98 | 2018-03-22 13:40:08 -0400 | [diff] [blame] | 823 | mad.oid = oid; |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 824 | |
| 825 | find_abbrev_len_packed(&mad); |
| 826 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 827 | if (init_object_disambiguation(r, hex, mad.cur_len, &ds) < 0) |
Derrick Stolee | 0e87b85 | 2017-10-12 08:02:20 -0400 | [diff] [blame] | 828 | return -1; |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 829 | |
Nguyễn Thái Ngọc Duy | ef9b037 | 2019-04-16 16:33:23 +0700 | [diff] [blame] | 830 | ds.fn = repo_extend_abbrev_len; |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 831 | ds.always_call_fn = 1; |
| 832 | ds.cb_data = (void *)&mad; |
| 833 | |
| 834 | find_short_object_filename(&ds); |
Derrick Stolee | 5b20ace | 2017-10-08 14:49:40 -0400 | [diff] [blame] | 835 | (void)finish_object_disambiguation(&ds, &oid_ret); |
| 836 | |
| 837 | hex[mad.cur_len] = 0; |
| 838 | return mad.cur_len; |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 839 | } |
| 840 | |
Nguyễn Thái Ngọc Duy | 8bb9557 | 2019-04-16 16:33:22 +0700 | [diff] [blame] | 841 | const char *repo_find_unique_abbrev(struct repository *r, |
| 842 | const struct object_id *oid, |
| 843 | int len) |
Jeff King | af49c6d | 2015-09-24 17:05:45 -0400 | [diff] [blame] | 844 | { |
Jeff King | ef2ed50 | 2016-10-20 02:19:19 -0400 | [diff] [blame] | 845 | static int bufno; |
brian m. carlson | dc01505 | 2017-03-26 16:01:24 +0000 | [diff] [blame] | 846 | static char hexbuffer[4][GIT_MAX_HEXSZ + 1]; |
René Scharfe | 3e98919 | 2016-11-01 09:49:07 +0100 | [diff] [blame] | 847 | char *hex = hexbuffer[bufno]; |
| 848 | bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); |
Nguyễn Thái Ngọc Duy | 8bb9557 | 2019-04-16 16:33:22 +0700 | [diff] [blame] | 849 | repo_find_unique_abbrev_r(r, hex, oid, len); |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 850 | return hex; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 853 | static int ambiguous_path(const char *path, int len) |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 854 | { |
| 855 | int slash = 1; |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 856 | int cnt; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 857 | |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 858 | for (cnt = 0; cnt < len; cnt++) { |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 859 | switch (*path++) { |
| 860 | case '\0': |
| 861 | break; |
| 862 | case '/': |
| 863 | if (slash) |
| 864 | break; |
| 865 | slash = 1; |
| 866 | continue; |
| 867 | case '.': |
| 868 | continue; |
| 869 | default: |
| 870 | slash = 0; |
| 871 | continue; |
| 872 | } |
Junio C Hamano | c054d64 | 2005-12-17 00:00:50 -0800 | [diff] [blame] | 873 | break; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 874 | } |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 875 | return slash; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 876 | } |
| 877 | |
Jeff King | a1ad0eb | 2015-05-21 00:45:39 -0400 | [diff] [blame] | 878 | static inline int at_mark(const char *string, int len, |
| 879 | const char **suffix, int nr) |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 880 | { |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 881 | int i; |
| 882 | |
Jeff King | a1ad0eb | 2015-05-21 00:45:39 -0400 | [diff] [blame] | 883 | for (i = 0; i < nr; i++) { |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 884 | int suffix_len = strlen(suffix[i]); |
| 885 | if (suffix_len <= len |
Ævar Arnfjörð Bjarmason | 244ea1b | 2017-03-27 11:16:55 +0000 | [diff] [blame] | 886 | && !strncasecmp(string, suffix[i], suffix_len)) |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 887 | return suffix_len; |
| 888 | } |
| 889 | return 0; |
| 890 | } |
| 891 | |
Jeff King | a1ad0eb | 2015-05-21 00:45:39 -0400 | [diff] [blame] | 892 | static inline int upstream_mark(const char *string, int len) |
| 893 | { |
| 894 | const char *suffix[] = { "@{upstream}", "@{u}" }; |
| 895 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); |
| 896 | } |
| 897 | |
Jeff King | adfe5d0 | 2015-05-21 00:45:47 -0400 | [diff] [blame] | 898 | static inline int push_mark(const char *string, int len) |
| 899 | { |
| 900 | const char *suffix[] = { "@{push}" }; |
| 901 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); |
| 902 | } |
| 903 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 904 | static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags); |
Nguyễn Thái Ngọc Duy | 23a5712 | 2019-04-16 16:33:26 +0700 | [diff] [blame] | 905 | static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf); |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 906 | |
Nguyễn Thái Ngọc Duy | 49281cf | 2019-04-16 16:33:31 +0700 | [diff] [blame] | 907 | static int get_oid_basic(struct repository *r, const char *str, int len, |
| 908 | struct object_id *oid, unsigned int flags) |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 909 | { |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 910 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 911 | static const char *object_name_msg = N_( |
| 912 | "Git normally never creates a ref that ends with 40 hex characters\n" |
| 913 | "because it will be ignored when you just specify 40-hex. These refs\n" |
| 914 | "may be created by mistake. For example,\n" |
| 915 | "\n" |
Nguyễn Thái Ngọc Duy | 328c6cb | 2019-03-29 17:39:19 +0700 | [diff] [blame] | 916 | " git switch -c $br $(git rev-parse ...)\n" |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 917 | "\n" |
| 918 | "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n" |
| 919 | "examine these refs and maybe delete them. Turn this message off by\n" |
Thomas Rast | 8dc84fd | 2013-07-31 22:23:31 +0200 | [diff] [blame] | 920 | "running \"git config advice.objectNameWarning false\""); |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 921 | struct object_id tmp_oid; |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 922 | char *real_ref = NULL; |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 923 | int refs_found = 0; |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 924 | int at, reflog_len, nth_prior = 0; |
Felipe Contreras | dfbfdc5 | 2023-03-16 11:15:14 -0600 | [diff] [blame] | 925 | int fatal = !(flags & GET_OID_QUIETLY); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 926 | |
Nguyễn Thái Ngọc Duy | 49281cf | 2019-04-16 16:33:31 +0700 | [diff] [blame] | 927 | if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) { |
Brodie Rao | 832cf74 | 2014-01-06 19:32:01 -0800 | [diff] [blame] | 928 | if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { |
Jonathan Tan | f24c30e | 2020-09-01 15:28:09 -0700 | [diff] [blame] | 929 | refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref, 0); |
Brodie Rao | 832cf74 | 2014-01-06 19:32:01 -0800 | [diff] [blame] | 930 | if (refs_found > 0) { |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 931 | warning(warn_msg, len, str); |
Ben Boeckel | ed9bff0 | 2021-08-23 12:44:00 +0200 | [diff] [blame] | 932 | if (advice_enabled(ADVICE_OBJECT_NAME_WARNING)) |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 933 | fprintf(stderr, "%s\n", _(object_name_msg)); |
| 934 | } |
| 935 | free(real_ref); |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 936 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 937 | return 0; |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 938 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 939 | |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 940 | /* basic@{time or number or -number} format to query ref-log */ |
Junio C Hamano | 694500e | 2006-10-23 21:15:34 -0700 | [diff] [blame] | 941 | reflog_len = at = 0; |
Johannes Schindelin | f265458 | 2009-01-28 00:07:46 +0100 | [diff] [blame] | 942 | if (len && str[len-1] == '}') { |
Ramkumar Ramachandra | e883a05 | 2013-05-07 16:55:09 -0500 | [diff] [blame] | 943 | for (at = len-4; at >= 0; at--) { |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 944 | if (str[at] == '@' && str[at+1] == '{') { |
Ramkumar Ramachandra | 83d16bc | 2013-05-07 16:55:11 -0500 | [diff] [blame] | 945 | if (str[at+2] == '-') { |
| 946 | if (at != 0) |
| 947 | /* @{-N} not at start */ |
| 948 | return -1; |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 949 | nth_prior = 1; |
| 950 | continue; |
| 951 | } |
Jeff King | adfe5d0 | 2015-05-21 00:45:47 -0400 | [diff] [blame] | 952 | if (!upstream_mark(str + at, len - at) && |
| 953 | !push_mark(str + at, len - at)) { |
Johannes Schindelin | 28fb843 | 2009-09-10 17:25:57 +0200 | [diff] [blame] | 954 | reflog_len = (len-1) - (at+2); |
| 955 | len = at; |
| 956 | } |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 957 | break; |
| 958 | } |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 962 | /* Accept only unambiguous ref paths. */ |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 963 | if (len && ambiguous_path(str, len)) |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 964 | return -1; |
| 965 | |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 966 | if (nth_prior) { |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 967 | struct strbuf buf = STRBUF_INIT; |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 968 | int detached; |
| 969 | |
Nguyễn Thái Ngọc Duy | 49281cf | 2019-04-16 16:33:31 +0700 | [diff] [blame] | 970 | if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) { |
| 971 | detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid)); |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 972 | strbuf_release(&buf); |
| 973 | if (detached) |
| 974 | return 0; |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 975 | } |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | if (!len && reflog_len) |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 979 | /* allow "@{...}" to mean the current branch reflog */ |
Felipe Contreras | dfbfdc5 | 2023-03-16 11:15:14 -0600 | [diff] [blame] | 980 | refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref, !fatal); |
Felipe Contreras | 128fd54 | 2013-05-07 16:55:10 -0500 | [diff] [blame] | 981 | else if (reflog_len) |
Nguyễn Thái Ngọc Duy | 49281cf | 2019-04-16 16:33:31 +0700 | [diff] [blame] | 982 | refs_found = repo_dwim_log(r, str, len, oid, &real_ref); |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 983 | else |
Felipe Contreras | dfbfdc5 | 2023-03-16 11:15:14 -0600 | [diff] [blame] | 984 | refs_found = repo_dwim_ref(r, str, len, oid, &real_ref, !fatal); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 985 | |
| 986 | if (!refs_found) |
| 987 | return -1; |
| 988 | |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 989 | if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) && |
Nguyễn Thái Ngọc Duy | 798c35f | 2013-05-29 19:12:42 +0700 | [diff] [blame] | 990 | (refs_found > 1 || |
Nguyễn Thái Ngọc Duy | 49281cf | 2019-04-16 16:33:31 +0700 | [diff] [blame] | 991 | !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY))) |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 992 | warning(warn_msg, len, str); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 993 | |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 994 | if (reflog_len) { |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 995 | int nth, i; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 996 | timestamp_t at_time; |
| 997 | timestamp_t co_time; |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 998 | int co_tz, co_cnt; |
| 999 | |
Nicolas Pitre | fe55851 | 2007-02-01 12:33:23 -0500 | [diff] [blame] | 1000 | /* Is it asking for N-th entry, or approxidate? */ |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 1001 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
| 1002 | char ch = str[at+2+i]; |
| 1003 | if ('0' <= ch && ch <= '9') |
| 1004 | nth = nth * 10 + ch - '0'; |
| 1005 | else |
| 1006 | nth = -1; |
| 1007 | } |
Shawn O. Pearce | ea360dd | 2008-08-21 08:40:44 -0700 | [diff] [blame] | 1008 | if (100000000 <= nth) { |
| 1009 | at_time = nth; |
| 1010 | nth = -1; |
| 1011 | } else if (0 <= nth) |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 1012 | at_time = 0; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 1013 | else { |
Junio C Hamano | 93cfa7c | 2010-01-26 11:58:00 -0800 | [diff] [blame] | 1014 | int errors = 0; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 1015 | char *tmp = xstrndup(str + at + 2, reflog_len); |
Junio C Hamano | 93cfa7c | 2010-01-26 11:58:00 -0800 | [diff] [blame] | 1016 | at_time = approxidate_careful(tmp, &errors); |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 1017 | free(tmp); |
Jeff King | 28b3563 | 2014-07-24 00:41:11 -0400 | [diff] [blame] | 1018 | if (errors) { |
| 1019 | free(real_ref); |
Junio C Hamano | a5e10ac | 2010-01-27 10:53:09 -0800 | [diff] [blame] | 1020 | return -1; |
Jeff King | 28b3563 | 2014-07-24 00:41:11 -0400 | [diff] [blame] | 1021 | } |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 1022 | } |
Nguyễn Thái Ngọc Duy | 49281cf | 2019-04-16 16:33:31 +0700 | [diff] [blame] | 1023 | if (read_ref_at(get_main_ref_store(r), |
Nguyễn Thái Ngọc Duy | 7fdff47 | 2019-04-06 18:34:30 +0700 | [diff] [blame] | 1024 | real_ref, flags, at_time, nth, oid, NULL, |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 1025 | &co_time, &co_tz, &co_cnt)) { |
Ramkumar Ramachandra | 305ebea | 2013-05-22 16:09:55 +0530 | [diff] [blame] | 1026 | if (!len) { |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 1027 | if (!skip_prefix(real_ref, "refs/heads/", &str)) |
Ramkumar Ramachandra | 305ebea | 2013-05-22 16:09:55 +0530 | [diff] [blame] | 1028 | str = "HEAD"; |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 1029 | len = strlen(str); |
Ramkumar Ramachandra | 305ebea | 2013-05-22 16:09:55 +0530 | [diff] [blame] | 1030 | } |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 1031 | if (at_time) { |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1032 | if (!(flags & GET_OID_QUIETLY)) { |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1033 | warning(_("log for '%.*s' only goes back to %s"), |
| 1034 | len, str, |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 1035 | show_date(co_time, co_tz, DATE_MODE(RFC2822))); |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 1036 | } |
Jeff King | 755e746 | 2024-02-26 05:04:07 -0500 | [diff] [blame] | 1037 | } else if (nth == co_cnt && !is_null_oid(oid)) { |
| 1038 | /* |
| 1039 | * We were asked for the Nth reflog (counting |
| 1040 | * from 0), but there were only N entries. |
| 1041 | * read_ref_at() will have returned "1" to tell |
| 1042 | * us it did not find an entry, but it did |
| 1043 | * still fill in the oid with the "old" value, |
| 1044 | * which we can use. |
| 1045 | */ |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 1046 | } else { |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1047 | if (flags & GET_OID_QUIETLY) { |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 1048 | exit(128); |
| 1049 | } |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1050 | die(_("log for '%.*s' only has %d entries"), |
Jon Seymour | e6eedc3 | 2010-08-24 14:52:42 +1000 | [diff] [blame] | 1051 | len, str, co_cnt); |
| 1052 | } |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 1053 | } |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 1056 | free(real_ref); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 1057 | return 0; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1060 | static enum get_oid_result get_parent(struct repository *r, |
| 1061 | const char *name, int len, |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1062 | struct object_id *result, int idx) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1063 | { |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 1064 | struct object_id oid; |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1065 | enum get_oid_result ret = get_oid_1(r, name, len, &oid, |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1066 | GET_OID_COMMITTISH); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1067 | struct commit *commit; |
| 1068 | struct commit_list *p; |
| 1069 | |
| 1070 | if (ret) |
| 1071 | return ret; |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1072 | commit = lookup_commit_reference(r, &oid); |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 1073 | if (repo_parse_commit(r, commit)) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1074 | return MISSING_OBJECT; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1075 | if (!idx) { |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1076 | oidcpy(result, &commit->object.oid); |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1077 | return FOUND; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1078 | } |
| 1079 | p = commit->parents; |
| 1080 | while (p) { |
| 1081 | if (!--idx) { |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1082 | oidcpy(result, &p->item->object.oid); |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1083 | return FOUND; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1084 | } |
| 1085 | p = p->next; |
| 1086 | } |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1087 | return MISSING_OBJECT; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1090 | static enum get_oid_result get_nth_ancestor(struct repository *r, |
| 1091 | const char *name, int len, |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1092 | struct object_id *result, |
| 1093 | int generation) |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1094 | { |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 1095 | struct object_id oid; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 1096 | struct commit *commit; |
| 1097 | int ret; |
| 1098 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1099 | ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1100 | if (ret) |
| 1101 | return ret; |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1102 | commit = lookup_commit_reference(r, &oid); |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 1103 | if (!commit) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1104 | return MISSING_OBJECT; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1105 | |
| 1106 | while (generation--) { |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 1107 | if (repo_parse_commit(r, commit) || !commit->parents) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1108 | return MISSING_OBJECT; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 1109 | commit = commit->parents->item; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1110 | } |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1111 | oidcpy(result, &commit->object.oid); |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1112 | return FOUND; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1115 | struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen, |
| 1116 | struct object *o, enum object_type expected_type) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1117 | { |
| 1118 | if (name && !namelen) |
| 1119 | namelen = strlen(name); |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1120 | while (1) { |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1121 | if (!o || (!o->parsed && !parse_object(r, &o->oid))) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1122 | return NULL; |
Junio C Hamano | a6a3f2c | 2013-03-31 15:24:12 -0700 | [diff] [blame] | 1123 | if (expected_type == OBJ_ANY || o->type == expected_type) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1124 | return o; |
| 1125 | if (o->type == OBJ_TAG) |
| 1126 | o = ((struct tag*) o)->tagged; |
| 1127 | else if (o->type == OBJ_COMMIT) |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1128 | o = &(repo_get_commit_tree(r, ((struct commit *)o))->object); |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1129 | else { |
| 1130 | if (name) |
| 1131 | error("%.*s: expected %s type, but the object " |
| 1132 | "dereferences to %s type", |
Brandon Williams | debca9d | 2018-02-14 10:59:24 -0800 | [diff] [blame] | 1133 | namelen, name, type_name(expected_type), |
| 1134 | type_name(o->type)); |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1135 | return NULL; |
| 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1140 | static int peel_onion(struct repository *r, const char *name, int len, |
| 1141 | struct object_id *oid, unsigned lookup_flags) |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1142 | { |
brian m. carlson | de37d50 | 2017-05-06 22:10:29 +0000 | [diff] [blame] | 1143 | struct object_id outer; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1144 | const char *sp; |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 1145 | unsigned int expected_type = 0; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1146 | struct object *o; |
| 1147 | |
| 1148 | /* |
| 1149 | * "ref^{type}" dereferences ref repeatedly until you cannot |
| 1150 | * dereference anymore, or you get an object of given type, |
| 1151 | * whichever comes first. "ref^{}" means just dereference |
| 1152 | * tags until you get a non-tag. "ref^0" is a shorthand for |
| 1153 | * "ref^{commit}". "commit^{tree}" could be used to find the |
| 1154 | * top-level tree of the given commit. |
| 1155 | */ |
| 1156 | if (len < 4 || name[len-1] != '}') |
| 1157 | return -1; |
| 1158 | |
| 1159 | for (sp = name + len - 1; name <= sp; sp--) { |
| 1160 | int ch = *sp; |
| 1161 | if (ch == '{' && name < sp && sp[-1] == '^') |
| 1162 | break; |
| 1163 | } |
| 1164 | if (sp <= name) |
| 1165 | return -1; |
| 1166 | |
| 1167 | sp++; /* beginning of type name, or closing brace for empty */ |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1168 | if (starts_with(sp, "commit}")) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1169 | expected_type = OBJ_COMMIT; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1170 | else if (starts_with(sp, "tag}")) |
Richard Hansen | 75aa26d | 2013-09-03 15:50:16 -0400 | [diff] [blame] | 1171 | expected_type = OBJ_TAG; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1172 | else if (starts_with(sp, "tree}")) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1173 | expected_type = OBJ_TREE; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1174 | else if (starts_with(sp, "blob}")) |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1175 | expected_type = OBJ_BLOB; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1176 | else if (starts_with(sp, "object}")) |
Junio C Hamano | a6a3f2c | 2013-03-31 15:24:12 -0700 | [diff] [blame] | 1177 | expected_type = OBJ_ANY; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1178 | else if (sp[0] == '}') |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 1179 | expected_type = OBJ_NONE; |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1180 | else if (sp[0] == '/') |
| 1181 | expected_type = OBJ_COMMIT; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1182 | else |
| 1183 | return -1; |
| 1184 | |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1185 | lookup_flags &= ~GET_OID_DISAMBIGUATORS; |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 1186 | if (expected_type == OBJ_COMMIT) |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1187 | lookup_flags |= GET_OID_COMMITTISH; |
Junio C Hamano | ed1ca60 | 2013-03-31 15:19:52 -0700 | [diff] [blame] | 1188 | else if (expected_type == OBJ_TREE) |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1189 | lookup_flags |= GET_OID_TREEISH; |
Junio C Hamano | e264361 | 2012-07-02 10:00:40 -0700 | [diff] [blame] | 1190 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1191 | if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags)) |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1192 | return -1; |
| 1193 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1194 | o = parse_object(r, &outer); |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1195 | if (!o) |
| 1196 | return -1; |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 1197 | if (!expected_type) { |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1198 | o = deref_tag(r, o, name, sp - name - 2); |
| 1199 | if (!o || (!o->parsed && !parse_object(r, &o->oid))) |
Junio C Hamano | 6e1c6c1 | 2005-10-19 22:48:16 -0700 | [diff] [blame] | 1200 | return -1; |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1201 | oidcpy(oid, &o->oid); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1202 | return 0; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1203 | } |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1204 | |
| 1205 | /* |
| 1206 | * At this point, the syntax look correct, so |
| 1207 | * if we do not get the needed object, we should |
| 1208 | * barf. |
| 1209 | */ |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1210 | o = repo_peel_to_type(r, name, len, o, expected_type); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1211 | if (!o) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 1212 | return -1; |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1213 | |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1214 | oidcpy(oid, &o->oid); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1215 | if (sp[0] == '/') { |
| 1216 | /* "$commit^{/foo}" */ |
| 1217 | char *prefix; |
| 1218 | int ret; |
| 1219 | struct commit_list *list = NULL; |
| 1220 | |
Nguyễn Thái Ngọc Duy | 4322842 | 2010-12-15 16:02:54 +0700 | [diff] [blame] | 1221 | /* |
| 1222 | * $commit^{/}. Some regex implementation may reject. |
| 1223 | * We don't need regex anyway. '' pattern always matches. |
| 1224 | */ |
| 1225 | if (sp[1] == '}') |
| 1226 | return 0; |
| 1227 | |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1228 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
| 1229 | commit_list_insert((struct commit *)o, &list); |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1230 | ret = get_oid_oneline(r, prefix, oid, list); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 1231 | free(prefix); |
| 1232 | return ret; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1233 | } |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Nguyễn Thái Ngọc Duy | 0c6b5ba | 2019-04-16 16:33:30 +0700 | [diff] [blame] | 1237 | static int get_describe_name(struct repository *r, |
| 1238 | const char *name, int len, |
| 1239 | struct object_id *oid) |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1240 | { |
| 1241 | const char *cp; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1242 | unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT; |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1243 | |
| 1244 | for (cp = name + len - 1; name + 2 <= cp; cp--) { |
| 1245 | char ch = *cp; |
René Scharfe | 6f75d45 | 2015-03-09 23:46:54 +0100 | [diff] [blame] | 1246 | if (!isxdigit(ch)) { |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1247 | /* We must be looking at g in "SOMETHING-g" |
| 1248 | * for it to be describe output. |
| 1249 | */ |
| 1250 | if (ch == 'g' && cp[-1] == '-') { |
| 1251 | cp++; |
| 1252 | len -= cp - name; |
Nguyễn Thái Ngọc Duy | 0c6b5ba | 2019-04-16 16:33:30 +0700 | [diff] [blame] | 1253 | return get_short_oid(r, |
Nguyễn Thái Ngọc Duy | c6c0235 | 2019-04-16 16:33:25 +0700 | [diff] [blame] | 1254 | cp, len, oid, flags); |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | return -1; |
| 1259 | } |
| 1260 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1261 | static enum get_oid_result get_oid_1(struct repository *r, |
| 1262 | const char *name, int len, |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1263 | struct object_id *oid, |
| 1264 | unsigned lookup_flags) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1265 | { |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 1266 | int ret, has_suffix; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1267 | const char *cp; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1268 | |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 1269 | /* |
| 1270 | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1271 | */ |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 1272 | has_suffix = 0; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1273 | for (cp = name + len - 1; name <= cp; cp--) { |
| 1274 | int ch = *cp; |
| 1275 | if ('0' <= ch && ch <= '9') |
| 1276 | continue; |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 1277 | if (ch == '~' || ch == '^') |
| 1278 | has_suffix = ch; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1279 | break; |
| 1280 | } |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 1281 | |
| 1282 | if (has_suffix) { |
René Scharfe | 59fa5f5 | 2019-09-15 14:10:28 +0200 | [diff] [blame] | 1283 | unsigned int num = 0; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1284 | int len1 = cp - name; |
| 1285 | cp++; |
René Scharfe | 59fa5f5 | 2019-09-15 14:10:28 +0200 | [diff] [blame] | 1286 | while (cp < name + len) { |
| 1287 | unsigned int digit = *cp++ - '0'; |
| 1288 | if (unsigned_mult_overflows(num, 10)) |
| 1289 | return MISSING_OBJECT; |
| 1290 | num *= 10; |
| 1291 | if (unsigned_add_overflows(num, digit)) |
| 1292 | return MISSING_OBJECT; |
| 1293 | num += digit; |
| 1294 | } |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 1295 | if (!num && len1 == len - 1) |
| 1296 | num = 1; |
René Scharfe | 59fa5f5 | 2019-09-15 14:10:28 +0200 | [diff] [blame] | 1297 | else if (num > INT_MAX) |
| 1298 | return MISSING_OBJECT; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 1299 | if (has_suffix == '^') |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1300 | return get_parent(r, name, len1, oid, num); |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 1301 | /* else if (has_suffix == '~') -- goes without saying */ |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1302 | return get_nth_ancestor(r, name, len1, oid, num); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1305 | ret = peel_onion(r, name, len, oid, lookup_flags); |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1306 | if (!ret) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1307 | return FOUND; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 1308 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1309 | ret = get_oid_basic(r, name, len, oid, lookup_flags); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1310 | if (!ret) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1311 | return FOUND; |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1312 | |
| 1313 | /* It could be describe output that is "SOMETHING-gXXXX" */ |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1314 | ret = get_describe_name(r, name, len, oid); |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1315 | if (!ret) |
David Turner | d1dd94b | 2019-01-17 23:19:43 -0500 | [diff] [blame] | 1316 | return FOUND; |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 1317 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1318 | return get_short_oid(r, name, len, oid, lookup_flags); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 1321 | /* |
| 1322 | * This interprets names like ':/Initial revision of "git"' by searching |
| 1323 | * through history and returning the first commit whose message starts |
Junio C Hamano | 3d04589 | 2010-08-12 18:32:49 -0700 | [diff] [blame] | 1324 | * the given regular expression. |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 1325 | * |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1326 | * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'. |
| 1327 | * |
| 1328 | * For a literal '!' character at the beginning of a pattern, you have to repeat |
| 1329 | * that, like: ':/!!foo' |
| 1330 | * |
| 1331 | * For future extension, all other sequences beginning with ':/!' are reserved. |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 1332 | */ |
Nguyễn Thái Ngọc Duy | 208acbf | 2014-03-25 20:23:26 +0700 | [diff] [blame] | 1333 | |
| 1334 | /* Remember to update object flag allocation in object.h */ |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 1335 | #define ONELINE_SEEN (1u<<20) |
| 1336 | |
Nguyễn Thái Ngọc Duy | c931ba4 | 2019-04-16 16:33:33 +0700 | [diff] [blame] | 1337 | struct handle_one_ref_cb { |
| 1338 | struct repository *repo; |
| 1339 | struct commit_list **list; |
| 1340 | }; |
| 1341 | |
Michael Haggerty | 9c5fe0b | 2015-05-25 18:39:05 +0000 | [diff] [blame] | 1342 | static int handle_one_ref(const char *path, const struct object_id *oid, |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 1343 | int flag UNUSED, |
Jeff King | 63e14ee | 2022-08-19 06:08:32 -0400 | [diff] [blame] | 1344 | void *cb_data) |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1345 | { |
Nguyễn Thái Ngọc Duy | c931ba4 | 2019-04-16 16:33:33 +0700 | [diff] [blame] | 1346 | struct handle_one_ref_cb *cb = cb_data; |
| 1347 | struct commit_list **list = cb->list; |
| 1348 | struct object *object = parse_object(cb->repo, oid); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1349 | if (!object) |
| 1350 | return 0; |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 1351 | if (object->type == OBJ_TAG) { |
Nguyễn Thái Ngọc Duy | c931ba4 | 2019-04-16 16:33:33 +0700 | [diff] [blame] | 1352 | object = deref_tag(cb->repo, object, path, |
Stefan Beller | a74093d | 2018-06-28 18:22:05 -0700 | [diff] [blame] | 1353 | strlen(path)); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 1354 | if (!object) |
| 1355 | return 0; |
| 1356 | } |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1357 | if (object->type != OBJ_COMMIT) |
| 1358 | return 0; |
René Scharfe | e8d1dfe | 2014-08-21 20:30:29 +0200 | [diff] [blame] | 1359 | commit_list_insert((struct commit *)object, list); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1360 | return 0; |
| 1361 | } |
| 1362 | |
Nguyễn Thái Ngọc Duy | 0bb41a1 | 2019-04-16 16:33:29 +0700 | [diff] [blame] | 1363 | static int get_oid_oneline(struct repository *r, |
| 1364 | const char *prefix, struct object_id *oid, |
| 1365 | struct commit_list *list) |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1366 | { |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1367 | struct commit_list *backup = NULL, *l; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1368 | int found = 0; |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1369 | int negative = 0; |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1370 | regex_t regex; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1371 | |
| 1372 | if (prefix[0] == '!') { |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1373 | prefix++; |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1374 | |
| 1375 | if (prefix[0] == '-') { |
| 1376 | prefix++; |
| 1377 | negative = 1; |
| 1378 | } else if (prefix[0] != '!') { |
Junio C Hamano | e6a6a76 | 2016-02-24 13:25:52 -0800 | [diff] [blame] | 1379 | return -1; |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1380 | } |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1381 | } |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1382 | |
| 1383 | if (regcomp(®ex, prefix, REG_EXTENDED)) |
Jeff King | aac4fac | 2016-02-10 16:19:25 -0500 | [diff] [blame] | 1384 | return -1; |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1385 | |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1386 | for (l = list; l; l = l->next) { |
| 1387 | l->item->object.flags |= ONELINE_SEEN; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1388 | commit_list_insert(l->item, &backup); |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1389 | } |
Jim Meyering | ed8ad7e | 2007-03-11 19:49:08 +0100 | [diff] [blame] | 1390 | while (list) { |
Jeff King | ba41c1c | 2014-06-10 17:41:02 -0400 | [diff] [blame] | 1391 | const char *p, *buf; |
Linus Torvalds | 1358e7d | 2007-03-12 11:30:38 -0700 | [diff] [blame] | 1392 | struct commit *commit; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1393 | int matches; |
Jim Meyering | ed8ad7e | 2007-03-11 19:49:08 +0100 | [diff] [blame] | 1394 | |
| 1395 | commit = pop_most_recent_commit(&list, ONELINE_SEEN); |
Nguyễn Thái Ngọc Duy | 0bb41a1 | 2019-04-16 16:33:29 +0700 | [diff] [blame] | 1396 | if (!parse_object(r, &commit->object.oid)) |
Martin Koegler | 283cdbc | 2008-02-18 21:47:53 +0100 | [diff] [blame] | 1397 | continue; |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 1398 | buf = repo_get_commit_buffer(r, commit, NULL); |
Jeff King | ba41c1c | 2014-06-10 17:41:02 -0400 | [diff] [blame] | 1399 | p = strstr(buf, "\n\n"); |
Will Palmer | 0769854 | 2016-01-30 17:06:01 -0700 | [diff] [blame] | 1400 | matches = negative ^ (p && !regexec(®ex, p + 2, 0, NULL, 0)); |
Ævar Arnfjörð Bjarmason | 4a93b89 | 2023-03-28 15:58:58 +0200 | [diff] [blame] | 1401 | repo_unuse_commit_buffer(r, commit, buf); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1402 | |
| 1403 | if (matches) { |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1404 | oidcpy(oid, &commit->object.oid); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1405 | found = 1; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1406 | break; |
| 1407 | } |
| 1408 | } |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 1409 | regfree(®ex); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1410 | free_commit_list(list); |
| 1411 | for (l = backup; l; l = l->next) |
| 1412 | clear_commit_marks(l->item, ONELINE_SEEN); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 1413 | free_commit_list(backup); |
| 1414 | return found ? 0 : -1; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 1415 | } |
| 1416 | |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1417 | struct grab_nth_branch_switch_cbdata { |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1418 | int remaining; |
René Scharfe | 4b3aa17 | 2019-09-18 18:35:38 +0200 | [diff] [blame] | 1419 | struct strbuf *sb; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1420 | }; |
| 1421 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 1422 | static int grab_nth_branch_switch(struct object_id *ooid UNUSED, |
| 1423 | struct object_id *noid UNUSED, |
| 1424 | const char *email UNUSED, |
| 1425 | timestamp_t timestamp UNUSED, |
| 1426 | int tz UNUSED, |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1427 | const char *message, void *cb_data) |
| 1428 | { |
| 1429 | struct grab_nth_branch_switch_cbdata *cb = cb_data; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1430 | const char *match = NULL, *target = NULL; |
| 1431 | size_t len; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1432 | |
Jeff King | 95b567c | 2014-06-18 15:48:29 -0400 | [diff] [blame] | 1433 | if (skip_prefix(message, "checkout: moving from ", &match)) |
Junio C Hamano | d7c03c1 | 2009-01-21 00:37:38 -0800 | [diff] [blame] | 1434 | target = strstr(match, " to "); |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1435 | |
Junio C Hamano | c829774 | 2009-01-19 16:44:08 -0800 | [diff] [blame] | 1436 | if (!match || !target) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1437 | return 0; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1438 | if (--(cb->remaining) == 0) { |
| 1439 | len = target - match; |
René Scharfe | 4b3aa17 | 2019-09-18 18:35:38 +0200 | [diff] [blame] | 1440 | strbuf_reset(cb->sb); |
| 1441 | strbuf_add(cb->sb, match, len); |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1442 | return 1; /* we are done */ |
| 1443 | } |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | /* |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1448 | * Parse @{-N} syntax, return the number of characters parsed |
| 1449 | * if successful; otherwise signal an error with negative value. |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1450 | */ |
Nguyễn Thái Ngọc Duy | 23a5712 | 2019-04-16 16:33:26 +0700 | [diff] [blame] | 1451 | static int interpret_nth_prior_checkout(struct repository *r, |
| 1452 | const char *name, int namelen, |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1453 | struct strbuf *buf) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1454 | { |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 1455 | long nth; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1456 | int retval; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1457 | struct grab_nth_branch_switch_cbdata cb; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1458 | const char *brace; |
| 1459 | char *num_end; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1460 | |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1461 | if (namelen < 4) |
| 1462 | return -1; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1463 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') |
| 1464 | return -1; |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1465 | brace = memchr(name, '}', namelen); |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1466 | if (!brace) |
| 1467 | return -1; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1468 | nth = strtol(name + 3, &num_end, 10); |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 1469 | if (num_end != brace) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1470 | return -1; |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 1471 | if (nth <= 0) |
| 1472 | return -1; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1473 | cb.remaining = nth; |
René Scharfe | 4b3aa17 | 2019-09-18 18:35:38 +0200 | [diff] [blame] | 1474 | cb.sb = buf; |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1475 | |
Nguyễn Thái Ngọc Duy | 23a5712 | 2019-04-16 16:33:26 +0700 | [diff] [blame] | 1476 | retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r), |
| 1477 | "HEAD", grab_nth_branch_switch, &cb); |
| 1478 | if (0 < retval) { |
Junio C Hamano | 98f85ff | 2013-03-08 13:27:37 -0800 | [diff] [blame] | 1479 | retval = brace - name + 1; |
Nguyễn Thái Ngọc Duy | 23a5712 | 2019-04-16 16:33:26 +0700 | [diff] [blame] | 1480 | } else |
| 1481 | retval = 0; |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 1482 | |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 1483 | return retval; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 1484 | } |
| 1485 | |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1486 | int repo_get_oid_mb(struct repository *r, |
| 1487 | const char *name, |
| 1488 | struct object_id *oid) |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1489 | { |
| 1490 | struct commit *one, *two; |
Johannes Schindelin | 76e2a09 | 2024-02-28 09:44:14 +0000 | [diff] [blame] | 1491 | struct commit_list *mbs = NULL; |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1492 | struct object_id oid_tmp; |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1493 | const char *dots; |
| 1494 | int st; |
| 1495 | |
| 1496 | dots = strstr(name, "..."); |
| 1497 | if (!dots) |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1498 | return repo_get_oid(r, name, oid); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1499 | if (dots == name) |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1500 | st = repo_get_oid(r, "HEAD", &oid_tmp); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1501 | else { |
| 1502 | struct strbuf sb; |
| 1503 | strbuf_init(&sb, dots - name); |
| 1504 | strbuf_add(&sb, name, dots - name); |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1505 | st = repo_get_oid_committish(r, sb.buf, &oid_tmp); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1506 | strbuf_release(&sb); |
| 1507 | } |
| 1508 | if (st) |
| 1509 | return st; |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1510 | one = lookup_commit_reference_gently(r, &oid_tmp, 0); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1511 | if (!one) |
| 1512 | return -1; |
| 1513 | |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1514 | if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp)) |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1515 | return -1; |
Nguyễn Thái Ngọc Duy | 0daf7ff | 2019-04-16 16:33:41 +0700 | [diff] [blame] | 1516 | two = lookup_commit_reference_gently(r, &oid_tmp, 0); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1517 | if (!two) |
| 1518 | return -1; |
Johannes Schindelin | 76e2a09 | 2024-02-28 09:44:14 +0000 | [diff] [blame] | 1519 | if (repo_get_merge_bases(r, one, two, &mbs) < 0) { |
| 1520 | free_commit_list(mbs); |
| 1521 | return -1; |
| 1522 | } |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1523 | if (!mbs || mbs->next) |
| 1524 | st = -1; |
| 1525 | else { |
| 1526 | st = 0; |
brian m. carlson | 151b291 | 2016-09-05 20:08:07 +0000 | [diff] [blame] | 1527 | oidcpy(oid, &mbs->item->object.oid); |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 1528 | } |
| 1529 | free_commit_list(mbs); |
| 1530 | return st; |
| 1531 | } |
| 1532 | |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1533 | /* parse @something syntax, when 'something' is not {.*} */ |
| 1534 | static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf) |
| 1535 | { |
| 1536 | const char *next; |
| 1537 | |
| 1538 | if (len || name[1] == '{') |
| 1539 | return -1; |
| 1540 | |
| 1541 | /* make sure it's a single @, or @@{.*}, not @foo */ |
Jeff King | 8cd4249 | 2014-01-15 03:31:57 -0500 | [diff] [blame] | 1542 | next = memchr(name + len + 1, '@', namelen - len - 1); |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1543 | if (next && next[1] != '{') |
| 1544 | return -1; |
| 1545 | if (!next) |
| 1546 | next = name + namelen; |
| 1547 | if (next != name + 1) |
| 1548 | return -1; |
| 1549 | |
| 1550 | strbuf_reset(buf); |
| 1551 | strbuf_add(buf, "HEAD", 4); |
| 1552 | return 1; |
| 1553 | } |
| 1554 | |
Nguyễn Thái Ngọc Duy | 71588ed | 2019-04-16 16:33:28 +0700 | [diff] [blame] | 1555 | static int reinterpret(struct repository *r, |
| 1556 | const char *name, int namelen, int len, |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1557 | struct strbuf *buf, unsigned allowed) |
Felipe Contreras | 7a0a49a | 2013-05-07 17:04:30 -0500 | [diff] [blame] | 1558 | { |
| 1559 | /* we have extra data, which might need further processing */ |
| 1560 | struct strbuf tmp = STRBUF_INIT; |
| 1561 | int used = buf->len; |
| 1562 | int ret; |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1563 | struct interpret_branch_name_options options = { |
| 1564 | .allowed = allowed |
| 1565 | }; |
Felipe Contreras | 7a0a49a | 2013-05-07 17:04:30 -0500 | [diff] [blame] | 1566 | |
| 1567 | strbuf_add(buf, name + len, namelen - len); |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1568 | ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, &options); |
Felipe Contreras | 7a0a49a | 2013-05-07 17:04:30 -0500 | [diff] [blame] | 1569 | /* that data was not interpreted, remove our cruft */ |
| 1570 | if (ret < 0) { |
| 1571 | strbuf_setlen(buf, used); |
| 1572 | return len; |
| 1573 | } |
| 1574 | strbuf_reset(buf); |
| 1575 | strbuf_addbuf(buf, &tmp); |
| 1576 | strbuf_release(&tmp); |
| 1577 | /* tweak for size of {-N} versus expanded ref name */ |
| 1578 | return ret - used + len; |
| 1579 | } |
| 1580 | |
Nguyễn Thái Ngọc Duy | ea1c873 | 2019-04-16 16:33:27 +0700 | [diff] [blame] | 1581 | static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref) |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1582 | { |
Nguyễn Thái Ngọc Duy | ea1c873 | 2019-04-16 16:33:27 +0700 | [diff] [blame] | 1583 | char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0); |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1584 | strbuf_reset(buf); |
| 1585 | strbuf_addstr(buf, s); |
| 1586 | free(s); |
| 1587 | } |
| 1588 | |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1589 | static int branch_interpret_allowed(const char *refname, unsigned allowed) |
| 1590 | { |
| 1591 | if (!allowed) |
| 1592 | return 1; |
| 1593 | |
| 1594 | if ((allowed & INTERPRET_BRANCH_LOCAL) && |
| 1595 | starts_with(refname, "refs/heads/")) |
| 1596 | return 1; |
| 1597 | if ((allowed & INTERPRET_BRANCH_REMOTE) && |
| 1598 | starts_with(refname, "refs/remotes/")) |
| 1599 | return 1; |
| 1600 | |
| 1601 | return 0; |
| 1602 | } |
| 1603 | |
Nguyễn Thái Ngọc Duy | ea1c873 | 2019-04-16 16:33:27 +0700 | [diff] [blame] | 1604 | static int interpret_branch_mark(struct repository *r, |
| 1605 | const char *name, int namelen, |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1606 | int at, struct strbuf *buf, |
| 1607 | int (*get_mark)(const char *, int), |
| 1608 | const char *(*get_data)(struct branch *, |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1609 | struct strbuf *), |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1610 | const struct interpret_branch_name_options *options) |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1611 | { |
| 1612 | int len; |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1613 | struct branch *branch; |
| 1614 | struct strbuf err = STRBUF_INIT; |
| 1615 | const char *value; |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1616 | |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1617 | len = get_mark(name + at, namelen - at); |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1618 | if (!len) |
| 1619 | return -1; |
| 1620 | |
Jeff King | 3f6eb30 | 2014-01-15 03:37:23 -0500 | [diff] [blame] | 1621 | if (memchr(name, ':', at)) |
| 1622 | return -1; |
| 1623 | |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1624 | if (at) { |
| 1625 | char *name_str = xmemdupz(name, at); |
| 1626 | branch = branch_get(name_str); |
| 1627 | free(name_str); |
| 1628 | } else |
| 1629 | branch = branch_get(NULL); |
| 1630 | |
| 1631 | value = get_data(branch, &err); |
Jonathan Tan | f24c30e | 2020-09-01 15:28:09 -0700 | [diff] [blame] | 1632 | if (!value) { |
| 1633 | if (options->nonfatal_dangling_mark) { |
| 1634 | strbuf_release(&err); |
| 1635 | return -1; |
| 1636 | } else { |
| 1637 | die("%s", err.buf); |
| 1638 | } |
| 1639 | } |
Jeff King | 48c5847 | 2015-05-21 00:45:43 -0400 | [diff] [blame] | 1640 | |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1641 | if (!branch_interpret_allowed(value, options->allowed)) |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1642 | return -1; |
| 1643 | |
Nguyễn Thái Ngọc Duy | ea1c873 | 2019-04-16 16:33:27 +0700 | [diff] [blame] | 1644 | set_shortened_ref(r, buf, value); |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1645 | return len + at; |
| 1646 | } |
| 1647 | |
Nguyễn Thái Ngọc Duy | 8f56e9d | 2019-04-06 18:34:26 +0700 | [diff] [blame] | 1648 | int repo_interpret_branch_name(struct repository *r, |
| 1649 | const char *name, int namelen, |
| 1650 | struct strbuf *buf, |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1651 | const struct interpret_branch_name_options *options) |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1652 | { |
Jeff King | f278f40 | 2014-01-15 03:27:32 -0500 | [diff] [blame] | 1653 | char *at; |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1654 | const char *start; |
Jeff King | 13228c3 | 2017-02-27 04:25:40 -0500 | [diff] [blame] | 1655 | int len; |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1656 | |
Felipe Contreras | cf99a76 | 2013-09-02 01:34:29 -0500 | [diff] [blame] | 1657 | if (!namelen) |
| 1658 | namelen = strlen(name); |
| 1659 | |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1660 | if (!options->allowed || (options->allowed & INTERPRET_BRANCH_LOCAL)) { |
Nguyễn Thái Ngọc Duy | 71588ed | 2019-04-16 16:33:28 +0700 | [diff] [blame] | 1661 | len = interpret_nth_prior_checkout(r, name, namelen, buf); |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1662 | if (!len) { |
| 1663 | return len; /* syntax Ok, not enough switches */ |
| 1664 | } else if (len > 0) { |
| 1665 | if (len == namelen) |
| 1666 | return len; /* consumed all */ |
| 1667 | else |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1668 | return reinterpret(r, name, namelen, len, buf, |
| 1669 | options->allowed); |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1670 | } |
Jeff King | d46a830 | 2010-01-28 04:52:22 -0500 | [diff] [blame] | 1671 | } |
| 1672 | |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1673 | for (start = name; |
| 1674 | (at = memchr(start, '@', namelen - (start - name))); |
| 1675 | start = at + 1) { |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1676 | |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1677 | if (!options->allowed || (options->allowed & INTERPRET_BRANCH_HEAD)) { |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1678 | len = interpret_empty_at(name, namelen, at - name, buf); |
| 1679 | if (len > 0) |
Nguyễn Thái Ngọc Duy | 71588ed | 2019-04-16 16:33:28 +0700 | [diff] [blame] | 1680 | return reinterpret(r, name, namelen, len, buf, |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1681 | options->allowed); |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1682 | } |
Felipe Contreras | 9ba89f4 | 2013-09-02 01:34:30 -0500 | [diff] [blame] | 1683 | |
Nguyễn Thái Ngọc Duy | 71588ed | 2019-04-16 16:33:28 +0700 | [diff] [blame] | 1684 | len = interpret_branch_mark(r, name, namelen, at - name, buf, |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1685 | upstream_mark, branch_get_upstream, |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1686 | options); |
Jeff King | 9892d5d | 2014-01-15 03:40:46 -0500 | [diff] [blame] | 1687 | if (len > 0) |
| 1688 | return len; |
Jeff King | adfe5d0 | 2015-05-21 00:45:47 -0400 | [diff] [blame] | 1689 | |
Nguyễn Thái Ngọc Duy | 71588ed | 2019-04-16 16:33:28 +0700 | [diff] [blame] | 1690 | len = interpret_branch_mark(r, name, namelen, at - name, buf, |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1691 | push_mark, branch_get_push, |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1692 | options); |
Zbigniew Jędrzejewski-Szmek | 6472028 | 2012-04-14 09:54:35 +0200 | [diff] [blame] | 1693 | if (len > 0) |
Zbigniew Jędrzejewski-Szmek | 17c8221 | 2012-04-14 09:54:34 +0200 | [diff] [blame] | 1694 | return len; |
Zbigniew Jędrzejewski-Szmek | bb0dab5 | 2012-04-14 09:54:33 +0200 | [diff] [blame] | 1695 | } |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1696 | |
Jeff King | a39c14a | 2014-01-15 03:26:33 -0500 | [diff] [blame] | 1697 | return -1; |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1698 | } |
| 1699 | |
Jeff King | 0e9f62d | 2017-03-02 03:23:01 -0500 | [diff] [blame] | 1700 | void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1701 | { |
| 1702 | int len = strlen(name); |
Jonathan Tan | a4f66a7 | 2020-09-01 15:28:07 -0700 | [diff] [blame] | 1703 | struct interpret_branch_name_options options = { |
| 1704 | .allowed = allowed |
| 1705 | }; |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1706 | int used = repo_interpret_branch_name(the_repository, name, len, sb, |
| 1707 | &options); |
Junio C Hamano | 84cf246 | 2013-05-15 14:32:30 -0700 | [diff] [blame] | 1708 | |
Junio C Hamano | 84cf246 | 2013-05-15 14:32:30 -0700 | [diff] [blame] | 1709 | if (used < 0) |
| 1710 | used = 0; |
| 1711 | strbuf_add(sb, name + used, len - used); |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) |
| 1715 | { |
Junio C Hamano | 7c3f847 | 2017-10-17 00:08:08 -0700 | [diff] [blame] | 1716 | if (startup_info->have_repository) |
| 1717 | strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); |
| 1718 | else |
| 1719 | strbuf_addstr(sb, name); |
Junio C Hamano | a625b09 | 2017-11-14 17:12:58 +0530 | [diff] [blame] | 1720 | |
| 1721 | /* |
| 1722 | * This splice must be done even if we end up rejecting the |
| 1723 | * name; builtin/branch.c::copy_or_rename_branch() still wants |
| 1724 | * to see what the name expanded to so that "branch -m" can be |
| 1725 | * used as a tool to correct earlier mistakes. |
| 1726 | */ |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1727 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
Junio C Hamano | a625b09 | 2017-11-14 17:12:58 +0530 | [diff] [blame] | 1728 | |
| 1729 | if (*name == '-' || |
| 1730 | !strcmp(sb->buf, "refs/heads/HEAD")) |
| 1731 | return -1; |
| 1732 | |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 1733 | return check_refname_format(sb->buf, 0); |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 1734 | } |
| 1735 | |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 1736 | /* |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1737 | * This is like "get_oid_basic()", except it allows "object ID expressions", |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1738 | * notably "xyz^" for "parent of xyz" |
| 1739 | */ |
Nguyễn Thái Ngọc Duy | ec580ea | 2019-04-16 16:33:37 +0700 | [diff] [blame] | 1740 | int repo_get_oid(struct repository *r, const char *name, struct object_id *oid) |
brian m. carlson | 2764fd9 | 2016-04-17 23:10:36 +0000 | [diff] [blame] | 1741 | { |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1742 | struct object_context unused; |
Nguyễn Thái Ngọc Duy | ec580ea | 2019-04-16 16:33:37 +0700 | [diff] [blame] | 1743 | return get_oid_with_context(r, name, 0, oid, &unused); |
brian m. carlson | 2764fd9 | 2016-04-17 23:10:36 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Paul-Sebastian Ungureanu | f5116f4 | 2019-02-25 23:16:05 +0000 | [diff] [blame] | 1746 | /* |
| 1747 | * This returns a non-zero value if the string (built using printf |
| 1748 | * format and the given arguments) is not a valid object. |
| 1749 | */ |
| 1750 | int get_oidf(struct object_id *oid, const char *fmt, ...) |
| 1751 | { |
| 1752 | va_list ap; |
| 1753 | int ret; |
| 1754 | struct strbuf sb = STRBUF_INIT; |
| 1755 | |
| 1756 | va_start(ap, fmt); |
| 1757 | strbuf_vaddf(&sb, fmt, ap); |
| 1758 | va_end(ap); |
| 1759 | |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 1760 | ret = repo_get_oid(the_repository, sb.buf, oid); |
Paul-Sebastian Ungureanu | f5116f4 | 2019-02-25 23:16:05 +0000 | [diff] [blame] | 1761 | strbuf_release(&sb); |
| 1762 | |
| 1763 | return ret; |
| 1764 | } |
brian m. carlson | 2764fd9 | 2016-04-17 23:10:36 +0000 | [diff] [blame] | 1765 | |
| 1766 | /* |
Richard Hansen | a8a5406 | 2013-09-04 15:04:31 -0400 | [diff] [blame] | 1767 | * Many callers know that the user meant to name a commit-ish by |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1768 | * syntactical positions where the object name appears. Calling this |
| 1769 | * function allows the machinery to disambiguate shorter-than-unique |
Richard Hansen | a8a5406 | 2013-09-04 15:04:31 -0400 | [diff] [blame] | 1770 | * abbreviated object names between commit-ish and others. |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1771 | * |
| 1772 | * Note that this does NOT error out when the named object is not a |
Richard Hansen | a8a5406 | 2013-09-04 15:04:31 -0400 | [diff] [blame] | 1773 | * commit-ish. It is merely to give a hint to the disambiguation |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1774 | * machinery. |
| 1775 | */ |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1776 | int repo_get_oid_committish(struct repository *r, |
| 1777 | const char *name, |
| 1778 | struct object_id *oid) |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1779 | { |
| 1780 | struct object_context unused; |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1781 | return get_oid_with_context(r, name, GET_OID_COMMITTISH, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1782 | oid, &unused); |
Junio C Hamano | cd74e47 | 2012-07-02 12:04:52 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1785 | int repo_get_oid_treeish(struct repository *r, |
| 1786 | const char *name, |
| 1787 | struct object_id *oid) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1788 | { |
| 1789 | struct object_context unused; |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1790 | return get_oid_with_context(r, name, GET_OID_TREEISH, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1791 | oid, &unused); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1792 | } |
| 1793 | |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1794 | int repo_get_oid_commit(struct repository *r, |
| 1795 | const char *name, |
| 1796 | struct object_id *oid) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1797 | { |
| 1798 | struct object_context unused; |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1799 | return get_oid_with_context(r, name, GET_OID_COMMIT, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1800 | oid, &unused); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1803 | int repo_get_oid_tree(struct repository *r, |
| 1804 | const char *name, |
| 1805 | struct object_id *oid) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1806 | { |
| 1807 | struct object_context unused; |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1808 | return get_oid_with_context(r, name, GET_OID_TREE, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1809 | oid, &unused); |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1812 | int repo_get_oid_blob(struct repository *r, |
| 1813 | const char *name, |
| 1814 | struct object_id *oid) |
Junio C Hamano | daba53a | 2012-07-02 23:35:05 -0700 | [diff] [blame] | 1815 | { |
| 1816 | struct object_context unused; |
Nguyễn Thái Ngọc Duy | 65e5046 | 2019-04-16 16:33:40 +0700 | [diff] [blame] | 1817 | return get_oid_with_context(r, name, GET_OID_BLOB, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1818 | oid, &unused); |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1819 | } |
| 1820 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1821 | /* Must be called only when object_name:filename doesn't exist. */ |
Nguyễn Thái Ngọc Duy | 50ddb08 | 2019-06-27 16:28:49 +0700 | [diff] [blame] | 1822 | static void diagnose_invalid_oid_path(struct repository *r, |
| 1823 | const char *prefix, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1824 | const char *filename, |
| 1825 | const struct object_id *tree_oid, |
| 1826 | const char *object_name, |
| 1827 | int object_name_len) |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1828 | { |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1829 | struct object_id oid; |
Elijah Newren | 5ec1e72 | 2019-04-05 08:00:12 -0700 | [diff] [blame] | 1830 | unsigned short mode; |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1831 | |
| 1832 | if (!prefix) |
| 1833 | prefix = ""; |
| 1834 | |
René Scharfe | dbe44fa | 2015-05-19 23:44:23 +0200 | [diff] [blame] | 1835 | if (file_exists(filename)) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1836 | die(_("path '%s' exists on disk, but not in '%.*s'"), |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1837 | filename, object_name_len, object_name); |
Junio C Hamano | c705420 | 2017-05-30 09:23:33 +0900 | [diff] [blame] | 1838 | if (is_missing_file_error(errno)) { |
Jeff King | b2724c8 | 2014-06-19 17:26:56 -0400 | [diff] [blame] | 1839 | char *fullname = xstrfmt("%s%s", prefix, filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1840 | |
Nguyễn Thái Ngọc Duy | 50ddb08 | 2019-06-27 16:28:49 +0700 | [diff] [blame] | 1841 | if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) { |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1842 | die(_("path '%s' exists, but not '%s'\n" |
| 1843 | "hint: Did you mean '%.*s:%s' aka '%.*s:./%s'?"), |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1844 | fullname, |
| 1845 | filename, |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1846 | object_name_len, object_name, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1847 | fullname, |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1848 | object_name_len, object_name, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1849 | filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1850 | } |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1851 | die(_("path '%s' does not exist in '%.*s'"), |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 1852 | filename, object_name_len, object_name); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | /* Must be called only when :stage:filename doesn't exist. */ |
Nguyễn Thái Ngọc Duy | 0488481 | 2019-04-16 16:33:34 +0700 | [diff] [blame] | 1857 | static void diagnose_invalid_index_path(struct repository *r, |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 1858 | int stage, |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1859 | const char *prefix, |
| 1860 | const char *filename) |
| 1861 | { |
Nguyễn Thái Ngọc Duy | 0488481 | 2019-04-16 16:33:34 +0700 | [diff] [blame] | 1862 | struct index_state *istate = r->index; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1863 | const struct cache_entry *ce; |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1864 | int pos; |
| 1865 | unsigned namelen = strlen(filename); |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1866 | struct strbuf fullname = STRBUF_INIT; |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1867 | |
| 1868 | if (!prefix) |
| 1869 | prefix = ""; |
| 1870 | |
| 1871 | /* Wrong stage number? */ |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 1872 | pos = index_name_pos(istate, filename, namelen); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1873 | if (pos < 0) |
| 1874 | pos = -pos - 1; |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 1875 | if (pos < istate->cache_nr) { |
| 1876 | ce = istate->cache[pos]; |
Derrick Stolee | 4925adb | 2022-04-26 20:43:19 +0000 | [diff] [blame] | 1877 | if (!S_ISSPARSEDIR(ce->ce_mode) && |
| 1878 | ce_namelen(ce) == namelen && |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1879 | !memcmp(ce->name, filename, namelen)) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1880 | die(_("path '%s' is in the index, but not at stage %d\n" |
| 1881 | "hint: Did you mean ':%d:%s'?"), |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1882 | filename, stage, |
| 1883 | ce_stage(ce), filename); |
| 1884 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1885 | |
| 1886 | /* Confusion between relative and absolute filenames? */ |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1887 | strbuf_addstr(&fullname, prefix); |
| 1888 | strbuf_addstr(&fullname, filename); |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 1889 | pos = index_name_pos(istate, fullname.buf, fullname.len); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1890 | if (pos < 0) |
| 1891 | pos = -pos - 1; |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 1892 | if (pos < istate->cache_nr) { |
| 1893 | ce = istate->cache[pos]; |
Derrick Stolee | 4925adb | 2022-04-26 20:43:19 +0000 | [diff] [blame] | 1894 | if (!S_ISSPARSEDIR(ce->ce_mode) && |
| 1895 | ce_namelen(ce) == fullname.len && |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1896 | !memcmp(ce->name, fullname.buf, fullname.len)) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1897 | die(_("path '%s' is in the index, but not '%s'\n" |
| 1898 | "hint: Did you mean ':%d:%s' aka ':%d:./%s'?"), |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1899 | fullname.buf, filename, |
| 1900 | ce_stage(ce), fullname.buf, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1901 | ce_stage(ce), filename); |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1902 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1903 | |
Nguyễn Thái Ngọc Duy | 0488481 | 2019-04-16 16:33:34 +0700 | [diff] [blame] | 1904 | if (repo_file_exists(r, filename)) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1905 | die(_("path '%s' exists on disk, but not in the index"), filename); |
Junio C Hamano | c705420 | 2017-05-30 09:23:33 +0900 | [diff] [blame] | 1906 | if (is_missing_file_error(errno)) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1907 | die(_("path '%s' does not exist (neither on disk nor in the index)"), |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1908 | filename); |
| 1909 | |
Jeff King | 43bb66a | 2015-09-24 17:07:52 -0400 | [diff] [blame] | 1910 | strbuf_release(&fullname); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
Nguyễn Thái Ngọc Duy | 0381f7f | 2019-04-16 16:33:35 +0700 | [diff] [blame] | 1914 | static char *resolve_relative_path(struct repository *r, const char *rel) |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1915 | { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 1916 | if (!starts_with(rel, "./") && !starts_with(rel, "../")) |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1917 | return NULL; |
| 1918 | |
Nguyễn Thái Ngọc Duy | 0381f7f | 2019-04-16 16:33:35 +0700 | [diff] [blame] | 1919 | if (r != the_repository || !is_inside_work_tree()) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 1920 | die(_("relative path syntax can't be used outside working tree")); |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1921 | |
| 1922 | /* die() inside prefix_path() if resolved path is outside worktree */ |
| 1923 | return prefix_path(startup_info->prefix, |
| 1924 | startup_info->prefix ? strlen(startup_info->prefix) : 0, |
| 1925 | rel); |
| 1926 | } |
| 1927 | |
Derrick Stolee | 561287d | 2022-04-26 20:43:18 +0000 | [diff] [blame] | 1928 | static int reject_tree_in_index(struct repository *repo, |
| 1929 | int only_to_die, |
| 1930 | const struct cache_entry *ce, |
| 1931 | int stage, |
| 1932 | const char *prefix, |
| 1933 | const char *cp) |
| 1934 | { |
| 1935 | if (!S_ISSPARSEDIR(ce->ce_mode)) |
| 1936 | return 0; |
| 1937 | if (only_to_die) |
| 1938 | diagnose_invalid_index_path(repo, stage, prefix, cp); |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
Junio C Hamano | 7589e63 | 2019-02-06 22:05:22 -0800 | [diff] [blame] | 1942 | static enum get_oid_result get_oid_with_context_1(struct repository *repo, |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 1943 | const char *name, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 1944 | unsigned flags, |
| 1945 | const char *prefix, |
| 1946 | struct object_id *oid, |
| 1947 | struct object_context *oc) |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1948 | { |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1949 | int ret, bracket_depth; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1950 | int namelen = strlen(name); |
| 1951 | const char *cp; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 1952 | int only_to_die = flags & GET_OID_ONLY_TO_DIE; |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1953 | |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1954 | memset(oc, 0, sizeof(*oc)); |
| 1955 | oc->mode = S_IFINVALID; |
Jeff King | d72cae1 | 2017-05-19 08:52:25 -0400 | [diff] [blame] | 1956 | strbuf_init(&oc->symlink_path, 0); |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 1957 | ret = get_oid_1(repo, name, namelen, oid, flags); |
Ævar Arnfjörð Bjarmason | 245b948 | 2021-12-28 14:28:50 +0100 | [diff] [blame] | 1958 | if (!ret && flags & GET_OID_REQUIRE_PATH) |
| 1959 | die(_("<object>:<path> required, only <object> '%s' given"), |
| 1960 | name); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1961 | if (!ret) |
| 1962 | return ret; |
Junio C Hamano | 33bd598 | 2012-07-02 10:32:11 -0700 | [diff] [blame] | 1963 | /* |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 1964 | * tree:path --> object name of path in tree |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1965 | * :path -> object name of absolute path in index |
| 1966 | * :./path -> object name of path relative to cwd in index |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1967 | * :[0-3]:path -> object name of path in index at stage |
Matthieu Moy | 95ad6d2 | 2010-09-24 18:43:59 +0200 | [diff] [blame] | 1968 | * :/foo -> recent commit matching foo |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1969 | */ |
| 1970 | if (name[0] == ':') { |
| 1971 | int stage = 0; |
Nguyễn Thái Ngọc Duy | 9c5e6c8 | 2013-07-09 22:29:00 +0700 | [diff] [blame] | 1972 | const struct cache_entry *ce; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1973 | char *new_path = NULL; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1974 | int pos; |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1975 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
Nguyễn Thái Ngọc Duy | c931ba4 | 2019-04-16 16:33:33 +0700 | [diff] [blame] | 1976 | struct handle_one_ref_cb cb; |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1977 | struct commit_list *list = NULL; |
Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 +0000 | [diff] [blame] | 1978 | |
Nguyễn Thái Ngọc Duy | c931ba4 | 2019-04-16 16:33:33 +0700 | [diff] [blame] | 1979 | cb.repo = repo; |
| 1980 | cb.list = &list; |
Junio C Hamano | 5ff4b92 | 2020-04-09 17:03:45 -0700 | [diff] [blame] | 1981 | refs_for_each_ref(get_main_ref_store(repo), handle_one_ref, &cb); |
Jeff King | 0220461 | 2020-04-09 23:04:11 -0400 | [diff] [blame] | 1982 | refs_head_ref(get_main_ref_store(repo), handle_one_ref, &cb); |
René Scharfe | e8d1dfe | 2014-08-21 20:30:29 +0200 | [diff] [blame] | 1983 | commit_list_sort_by_date(&list); |
Nguyễn Thái Ngọc Duy | 0bb41a1 | 2019-04-16 16:33:29 +0700 | [diff] [blame] | 1984 | return get_oid_oneline(repo, name + 2, oid, list); |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1985 | } |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1986 | if (namelen < 3 || |
| 1987 | name[2] != ':' || |
| 1988 | name[1] < '0' || '3' < name[1]) |
| 1989 | cp = name + 1; |
| 1990 | else { |
| 1991 | stage = name[1] - '0'; |
| 1992 | cp = name + 3; |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1993 | } |
Nguyễn Thái Ngọc Duy | 0381f7f | 2019-04-16 16:33:35 +0700 | [diff] [blame] | 1994 | new_path = resolve_relative_path(repo, cp); |
Junio C Hamano | 3d6e0f7 | 2010-12-09 13:38:05 -0800 | [diff] [blame] | 1995 | if (!new_path) { |
| 1996 | namelen = namelen - (cp - name); |
| 1997 | } else { |
| 1998 | cp = new_path; |
| 1999 | namelen = strlen(cp); |
| 2000 | } |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 2001 | |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 2002 | if (flags & GET_OID_RECORD_PATH) |
Jeff King | dc944b6 | 2017-05-19 08:54:43 -0400 | [diff] [blame] | 2003 | oc->path = xstrdup(cp); |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 2004 | |
Jeff King | 581d2fd | 2019-05-14 09:54:55 -0400 | [diff] [blame] | 2005 | if (!repo->index || !repo->index->cache) |
Nguyễn Thái Ngọc Duy | efe461b | 2019-04-16 16:33:36 +0700 | [diff] [blame] | 2006 | repo_read_index(repo); |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 2007 | pos = index_name_pos(repo->index, cp, namelen); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 2008 | if (pos < 0) |
| 2009 | pos = -pos - 1; |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 2010 | while (pos < repo->index->cache_nr) { |
| 2011 | ce = repo->index->cache[pos]; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 2012 | if (ce_namelen(ce) != namelen || |
| 2013 | memcmp(ce->name, cp, namelen)) |
| 2014 | break; |
| 2015 | if (ce_stage(ce) == stage) { |
Derrick Stolee | 561287d | 2022-04-26 20:43:18 +0000 | [diff] [blame] | 2016 | free(new_path); |
| 2017 | if (reject_tree_in_index(repo, only_to_die, ce, |
| 2018 | stage, prefix, cp)) |
| 2019 | return -1; |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 2020 | oidcpy(oid, &ce->oid); |
Kirill Smelkov | 9006471 | 2010-09-29 15:35:24 +0400 | [diff] [blame] | 2021 | oc->mode = ce->ce_mode; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 2022 | return 0; |
| 2023 | } |
Junio C Hamano | e7cef45 | 2006-05-08 15:44:06 -0700 | [diff] [blame] | 2024 | pos++; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 2025 | } |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 2026 | if (only_to_die && name[1] && name[1] != '/') |
Nguyễn Thái Ngọc Duy | 0488481 | 2019-04-16 16:33:34 +0700 | [diff] [blame] | 2027 | diagnose_invalid_index_path(repo, stage, prefix, cp); |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 2028 | free(new_path); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 2029 | return -1; |
| 2030 | } |
Shawn Pearce | cce91a2 | 2006-05-19 03:29:43 -0400 | [diff] [blame] | 2031 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
| 2032 | if (*cp == '{') |
| 2033 | bracket_depth++; |
| 2034 | else if (bracket_depth && *cp == '}') |
| 2035 | bracket_depth--; |
| 2036 | else if (!bracket_depth && *cp == ':') |
| 2037 | break; |
| 2038 | } |
| 2039 | if (*cp == ':') { |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 2040 | struct object_id tree_oid; |
René Scharfe | b2981d0 | 2013-03-16 19:29:31 +0100 | [diff] [blame] | 2041 | int len = cp - name; |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 2042 | unsigned sub_flags = flags; |
| 2043 | |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 2044 | sub_flags &= ~GET_OID_DISAMBIGUATORS; |
| 2045 | sub_flags |= GET_OID_TREEISH; |
Jeff King | 8a10fea | 2016-09-26 07:59:41 -0400 | [diff] [blame] | 2046 | |
Nguyễn Thái Ngọc Duy | 2b1790f | 2019-04-16 16:33:32 +0700 | [diff] [blame] | 2047 | if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) { |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 2048 | const char *filename = cp+1; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 2049 | char *new_filename = NULL; |
| 2050 | |
Nguyễn Thái Ngọc Duy | 0381f7f | 2019-04-16 16:33:35 +0700 | [diff] [blame] | 2051 | new_filename = resolve_relative_path(repo, filename); |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 2052 | if (new_filename) |
| 2053 | filename = new_filename; |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 2054 | if (flags & GET_OID_FOLLOW_SYMLINKS) { |
Nguyễn Thái Ngọc Duy | 0dd1f0c | 2019-06-27 16:28:50 +0700 | [diff] [blame] | 2055 | ret = get_tree_entry_follow_symlinks(repo, &tree_oid, |
brian m. carlson | 3b683bc | 2018-05-02 00:25:40 +0000 | [diff] [blame] | 2056 | filename, oid, &oc->symlink_path, |
David Turner | c4ec967 | 2015-05-20 13:03:39 -0400 | [diff] [blame] | 2057 | &oc->mode); |
| 2058 | } else { |
Nguyễn Thái Ngọc Duy | 50ddb08 | 2019-06-27 16:28:49 +0700 | [diff] [blame] | 2059 | ret = get_tree_entry(repo, &tree_oid, filename, oid, |
brian m. carlson | 916bc35 | 2018-03-12 02:27:51 +0000 | [diff] [blame] | 2060 | &oc->mode); |
David Turner | c4ec967 | 2015-05-20 13:03:39 -0400 | [diff] [blame] | 2061 | if (ret && only_to_die) { |
Nguyễn Thái Ngọc Duy | 50ddb08 | 2019-06-27 16:28:49 +0700 | [diff] [blame] | 2062 | diagnose_invalid_oid_path(repo, prefix, |
David Turner | c4ec967 | 2015-05-20 13:03:39 -0400 | [diff] [blame] | 2063 | filename, |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 2064 | &tree_oid, |
David Turner | c4ec967 | 2015-05-20 13:03:39 -0400 | [diff] [blame] | 2065 | name, len); |
| 2066 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 2067 | } |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 2068 | if (flags & GET_OID_RECORD_PATH) |
Jeff King | dc944b6 | 2017-05-19 08:54:43 -0400 | [diff] [blame] | 2069 | oc->path = xstrdup(filename); |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 2070 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 2071 | free(new_filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 2072 | return ret; |
| 2073 | } else { |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 2074 | if (only_to_die) |
Jeff King | b041830 | 2020-01-24 19:13:01 -0500 | [diff] [blame] | 2075 | die(_("invalid object name '%.*s'."), len, name); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 2076 | } |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 2077 | } |
| 2078 | return ret; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 2079 | } |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 2080 | |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 2081 | /* |
| 2082 | * Call this function when you know "name" given by the end user must |
| 2083 | * name an object but it doesn't; the function _may_ die with a better |
| 2084 | * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not |
| 2085 | * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case |
| 2086 | * you have a chance to diagnose the error further. |
| 2087 | */ |
Nguyễn Thái Ngọc Duy | e270f42 | 2019-04-16 16:33:39 +0700 | [diff] [blame] | 2088 | void maybe_die_on_misspelt_object_name(struct repository *r, |
| 2089 | const char *name, |
| 2090 | const char *prefix) |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 2091 | { |
| 2092 | struct object_context oc; |
brian m. carlson | e82caf3 | 2017-07-13 23:49:28 +0000 | [diff] [blame] | 2093 | struct object_id oid; |
Ævar Arnfjörð Bjarmason | 9ce6000 | 2021-12-28 14:28:49 +0100 | [diff] [blame] | 2094 | get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE | GET_OID_QUIETLY, |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 2095 | prefix, &oid, &oc); |
Junio C Hamano | 8c135ea | 2012-07-02 11:01:25 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
Duy Nguyen | 127b48f | 2019-02-12 19:43:23 +0700 | [diff] [blame] | 2098 | enum get_oid_result get_oid_with_context(struct repository *repo, |
| 2099 | const char *str, |
| 2100 | unsigned flags, |
| 2101 | struct object_id *oid, |
| 2102 | struct object_context *oc) |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 2103 | { |
brian m. carlson | 321c89b | 2017-07-13 23:49:29 +0000 | [diff] [blame] | 2104 | if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE) |
Martin Ågren | 1e6771e | 2020-12-31 12:56:20 +0100 | [diff] [blame] | 2105 | BUG("incompatible flags for get_oid_with_context"); |
Nguyễn Thái Ngọc Duy | 3a7a698 | 2019-01-12 09:13:28 +0700 | [diff] [blame] | 2106 | return get_oid_with_context_1(repo, str, flags, NULL, oid, oc); |
Junio C Hamano | f01cc14 | 2012-07-02 10:19:35 -0700 | [diff] [blame] | 2107 | } |