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