]>
Commit | Line | Data |
---|---|---|
9938af6a | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
5385f52d | 3 | #include "tag.h" |
9938af6a | 4 | #include "commit.h" |
5385f52d JH |
5 | #include "tree.h" |
6 | #include "blob.h" | |
f3ab49db | 7 | #include "tree-walk.h" |
d556fae2 | 8 | #include "refs.h" |
28fb8438 | 9 | #include "remote.h" |
dbe44faa | 10 | #include "dir.h" |
fad6b9e5 | 11 | #include "sha1-array.h" |
0317f455 | 12 | #include "packfile.h" |
0d4a1321 | 13 | #include "object-store.h" |
031dc927 | 14 | #include "repository.h" |
efe461b0 | 15 | #include "submodule.h" |
8aac67a1 | 16 | #include "midx.h" |
64043556 | 17 | #include "commit-reach.h" |
9938af6a | 18 | |
0bb41a19 | 19 | static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *); |
32574b68 | 20 | |
ef9b0370 | 21 | typedef int (*disambiguate_hint_fn)(struct repository *, const struct object_id *, void *); |
a78fafe7 JH |
22 | |
23 | struct disambiguate_state { | |
0016043b | 24 | int len; /* length of prefix in hex chars */ |
dc01505f | 25 | char hex_pfx[GIT_MAX_HEXSZ + 1]; |
d2ee1185 | 26 | struct object_id bin_pfx; |
0016043b | 27 | |
ef9b0370 | 28 | struct repository *repo; |
a78fafe7 JH |
29 | disambiguate_hint_fn fn; |
30 | void *cb_data; | |
d2ee1185 | 31 | struct object_id candidate; |
a78fafe7 JH |
32 | unsigned candidate_exists:1; |
33 | unsigned candidate_checked:1; | |
34 | unsigned candidate_ok:1; | |
35 | unsigned disambiguate_fn_used:1; | |
36 | unsigned ambiguous:1; | |
957d7406 | 37 | unsigned always_call_fn:1; |
a78fafe7 JH |
38 | }; |
39 | ||
d2b7d9c7 | 40 | static void update_candidates(struct disambiguate_state *ds, const struct object_id *current) |
a78fafe7 | 41 | { |
957d7406 | 42 | if (ds->always_call_fn) { |
ef9b0370 | 43 | ds->ambiguous = ds->fn(ds->repo, current, ds->cb_data) ? 1 : 0; |
957d7406 JH |
44 | return; |
45 | } | |
a78fafe7 JH |
46 | if (!ds->candidate_exists) { |
47 | /* this is the first candidate */ | |
d2b7d9c7 | 48 | oidcpy(&ds->candidate, current); |
a78fafe7 JH |
49 | ds->candidate_exists = 1; |
50 | return; | |
4a7e27e9 | 51 | } else if (oideq(&ds->candidate, current)) { |
a78fafe7 JH |
52 | /* the same as what we already have seen */ |
53 | return; | |
54 | } | |
55 | ||
56 | if (!ds->fn) { | |
57 | /* cannot disambiguate between ds->candidate and current */ | |
58 | ds->ambiguous = 1; | |
59 | return; | |
60 | } | |
61 | ||
62 | if (!ds->candidate_checked) { | |
ef9b0370 | 63 | ds->candidate_ok = ds->fn(ds->repo, &ds->candidate, ds->cb_data); |
a78fafe7 JH |
64 | ds->disambiguate_fn_used = 1; |
65 | ds->candidate_checked = 1; | |
66 | } | |
67 | ||
68 | if (!ds->candidate_ok) { | |
749f763d | 69 | /* discard the candidate; we know it does not satisfy fn */ |
d2b7d9c7 | 70 | oidcpy(&ds->candidate, current); |
a78fafe7 JH |
71 | ds->candidate_checked = 0; |
72 | return; | |
73 | } | |
74 | ||
75 | /* if we reach this point, we know ds->candidate satisfies fn */ | |
ef9b0370 | 76 | if (ds->fn(ds->repo, current, ds->cb_data)) { |
a78fafe7 JH |
77 | /* |
78 | * if both current and candidate satisfy fn, we cannot | |
79 | * disambiguate. | |
80 | */ | |
81 | ds->candidate_ok = 0; | |
82 | ds->ambiguous = 1; | |
83 | } | |
84 | ||
85 | /* otherwise, current can be discarded and candidate is still good */ | |
86 | } | |
87 | ||
cc817ca3 RS |
88 | static int match_sha(unsigned, const unsigned char *, const unsigned char *); |
89 | ||
0016043b | 90 | static void find_short_object_filename(struct disambiguate_state *ds) |
9938af6a | 91 | { |
263db403 | 92 | struct object_directory *odb; |
99a19b43 | 93 | |
ef9b0370 | 94 | for (odb = ds->repo->objects->odb; odb && !ds->ambiguous; odb = odb->next) { |
cc817ca3 | 95 | int pos; |
0000d654 | 96 | struct oid_array *loose_objects; |
cc817ca3 | 97 | |
0000d654 RS |
98 | loose_objects = odb_loose_cache(odb, &ds->bin_pfx); |
99 | pos = oid_array_lookup(loose_objects, &ds->bin_pfx); | |
cc817ca3 RS |
100 | if (pos < 0) |
101 | pos = -1 - pos; | |
0000d654 | 102 | while (!ds->ambiguous && pos < loose_objects->nr) { |
cc817ca3 | 103 | const struct object_id *oid; |
0000d654 | 104 | oid = loose_objects->oid + pos; |
cc817ca3 RS |
105 | if (!match_sha(ds->len, ds->bin_pfx.hash, oid->hash)) |
106 | break; | |
107 | update_candidates(ds, oid); | |
108 | pos++; | |
9938af6a | 109 | } |
9938af6a | 110 | } |
9938af6a JH |
111 | } |
112 | ||
113 | static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b) | |
114 | { | |
115 | do { | |
116 | if (*a != *b) | |
117 | return 0; | |
118 | a++; | |
119 | b++; | |
120 | len -= 2; | |
121 | } while (len > 1); | |
122 | if (len) | |
123 | if ((*a ^ *b) & 0xf0) | |
124 | return 0; | |
125 | return 1; | |
126 | } | |
127 | ||
8aac67a1 DS |
128 | static void unique_in_midx(struct multi_pack_index *m, |
129 | struct disambiguate_state *ds) | |
130 | { | |
131 | uint32_t num, i, first = 0; | |
132 | const struct object_id *current = NULL; | |
133 | num = m->num_objects; | |
134 | ||
135 | if (!num) | |
136 | return; | |
137 | ||
138 | bsearch_midx(&ds->bin_pfx, m, &first); | |
139 | ||
140 | /* | |
141 | * At this point, "first" is the location of the lowest object | |
142 | * with an object name that could match "bin_pfx". See if we have | |
143 | * 0, 1 or more objects that actually match(es). | |
144 | */ | |
145 | for (i = first; i < num && !ds->ambiguous; i++) { | |
146 | struct object_id oid; | |
147 | current = nth_midxed_object_oid(&oid, m, i); | |
148 | if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash)) | |
149 | break; | |
150 | update_candidates(ds, current); | |
151 | } | |
152 | } | |
153 | ||
0016043b | 154 | static void unique_in_pack(struct packed_git *p, |
a78fafe7 | 155 | struct disambiguate_state *ds) |
9938af6a | 156 | { |
902f5a21 | 157 | uint32_t num, i, first = 0; |
d2b7d9c7 | 158 | const struct object_id *current = NULL; |
f703e6ea | 159 | |
af96fe33 DS |
160 | if (p->multi_pack_index) |
161 | return; | |
162 | ||
0e87b856 DS |
163 | if (open_pack_index(p) || !p->num_objects) |
164 | return; | |
165 | ||
f703e6ea | 166 | num = p->num_objects; |
902f5a21 | 167 | bsearch_pack(&ds->bin_pfx, p, &first); |
f703e6ea JH |
168 | |
169 | /* | |
170 | * At this point, "first" is the location of the lowest object | |
1703f9aa | 171 | * with an object name that could match "bin_pfx". See if we have |
f703e6ea JH |
172 | * 0, 1 or more objects that actually match(es). |
173 | */ | |
a78fafe7 | 174 | for (i = first; i < num && !ds->ambiguous; i++) { |
d2b7d9c7 | 175 | struct object_id oid; |
176 | current = nth_packed_object_oid(&oid, p, i); | |
177 | if (!match_sha(ds->len, ds->bin_pfx.hash, current->hash)) | |
f703e6ea | 178 | break; |
a78fafe7 | 179 | update_candidates(ds, current); |
9938af6a | 180 | } |
f703e6ea JH |
181 | } |
182 | ||
0016043b | 183 | static void find_short_packed_object(struct disambiguate_state *ds) |
9938af6a | 184 | { |
8aac67a1 | 185 | struct multi_pack_index *m; |
9938af6a JH |
186 | struct packed_git *p; |
187 | ||
ef9b0370 | 188 | for (m = get_multi_pack_index(ds->repo); m && !ds->ambiguous; |
8aac67a1 DS |
189 | m = m->next) |
190 | unique_in_midx(m, ds); | |
ef9b0370 | 191 | for (p = get_packed_git(ds->repo); p && !ds->ambiguous; |
a80d72db | 192 | p = p->next) |
0016043b | 193 | unique_in_pack(p, ds); |
99a19b43 JH |
194 | } |
195 | ||
a78fafe7 | 196 | static int finish_object_disambiguation(struct disambiguate_state *ds, |
e82caf38 | 197 | struct object_id *oid) |
99a19b43 | 198 | { |
a78fafe7 JH |
199 | if (ds->ambiguous) |
200 | return SHORT_NAME_AMBIGUOUS; | |
99a19b43 | 201 | |
a78fafe7 | 202 | if (!ds->candidate_exists) |
d1dd94b3 | 203 | return MISSING_OBJECT; |
a78fafe7 JH |
204 | |
205 | if (!ds->candidate_checked) | |
206 | /* | |
207 | * If this is the only candidate, there is no point | |
208 | * calling the disambiguation hint callback. | |
209 | * | |
210 | * On the other hand, if the current candidate | |
211 | * replaced an earlier candidate that did _not_ pass | |
212 | * the disambiguation hint callback, then we do have | |
213 | * more than one objects that match the short name | |
214 | * given, so we should make sure this one matches; | |
215 | * otherwise, if we discovered this one and the one | |
216 | * that we previously discarded in the reverse order, | |
217 | * we would end up showing different results in the | |
218 | * same repository! | |
219 | */ | |
220 | ds->candidate_ok = (!ds->disambiguate_fn_used || | |
ef9b0370 | 221 | ds->fn(ds->repo, &ds->candidate, ds->cb_data)); |
a78fafe7 JH |
222 | |
223 | if (!ds->candidate_ok) | |
013f276e | 224 | return SHORT_NAME_AMBIGUOUS; |
a78fafe7 | 225 | |
e82caf38 | 226 | oidcpy(oid, &ds->candidate); |
9938af6a JH |
227 | return 0; |
228 | } | |
229 | ||
ef9b0370 NTND |
230 | static int disambiguate_commit_only(struct repository *r, |
231 | const struct object_id *oid, | |
232 | void *cb_data_unused) | |
aa1dec9e | 233 | { |
ef9b0370 | 234 | int kind = oid_object_info(r, oid, NULL); |
aa1dec9e JH |
235 | return kind == OBJ_COMMIT; |
236 | } | |
237 | ||
ef9b0370 NTND |
238 | static int disambiguate_committish_only(struct repository *r, |
239 | const struct object_id *oid, | |
240 | void *cb_data_unused) | |
e2643617 JH |
241 | { |
242 | struct object *obj; | |
243 | int kind; | |
244 | ||
ef9b0370 | 245 | kind = oid_object_info(r, oid, NULL); |
e2643617 JH |
246 | if (kind == OBJ_COMMIT) |
247 | return 1; | |
248 | if (kind != OBJ_TAG) | |
99a19b43 | 249 | return 0; |
e2643617 JH |
250 | |
251 | /* We need to do this the hard way... */ | |
ef9b0370 | 252 | obj = deref_tag(r, parse_object(r, oid), NULL, 0); |
e2643617 JH |
253 | if (obj && obj->type == OBJ_COMMIT) |
254 | return 1; | |
9938af6a JH |
255 | return 0; |
256 | } | |
257 | ||
ef9b0370 NTND |
258 | static int disambiguate_tree_only(struct repository *r, |
259 | const struct object_id *oid, | |
260 | void *cb_data_unused) | |
9938af6a | 261 | { |
ef9b0370 | 262 | int kind = oid_object_info(r, oid, NULL); |
daba53ae JH |
263 | return kind == OBJ_TREE; |
264 | } | |
9938af6a | 265 | |
ef9b0370 NTND |
266 | static int disambiguate_treeish_only(struct repository *r, |
267 | const struct object_id *oid, | |
268 | void *cb_data_unused) | |
daba53ae JH |
269 | { |
270 | struct object *obj; | |
271 | int kind; | |
272 | ||
ef9b0370 | 273 | kind = oid_object_info(r, oid, NULL); |
daba53ae JH |
274 | if (kind == OBJ_TREE || kind == OBJ_COMMIT) |
275 | return 1; | |
276 | if (kind != OBJ_TAG) | |
277 | return 0; | |
278 | ||
279 | /* We need to do this the hard way... */ | |
ef9b0370 | 280 | obj = deref_tag(r, parse_object(r, oid), NULL, 0); |
daba53ae JH |
281 | if (obj && (obj->type == OBJ_TREE || obj->type == OBJ_COMMIT)) |
282 | return 1; | |
283 | return 0; | |
284 | } | |
285 | ||
ef9b0370 NTND |
286 | static int disambiguate_blob_only(struct repository *r, |
287 | const struct object_id *oid, | |
288 | void *cb_data_unused) | |
daba53ae | 289 | { |
ef9b0370 | 290 | int kind = oid_object_info(r, oid, NULL); |
daba53ae JH |
291 | return kind == OBJ_BLOB; |
292 | } | |
293 | ||
5b33cb1f JK |
294 | static disambiguate_hint_fn default_disambiguate_hint; |
295 | ||
296 | int set_disambiguate_hint_config(const char *var, const char *value) | |
297 | { | |
298 | static const struct { | |
299 | const char *name; | |
300 | disambiguate_hint_fn fn; | |
301 | } hints[] = { | |
302 | { "none", NULL }, | |
303 | { "commit", disambiguate_commit_only }, | |
304 | { "committish", disambiguate_committish_only }, | |
305 | { "tree", disambiguate_tree_only }, | |
306 | { "treeish", disambiguate_treeish_only }, | |
307 | { "blob", disambiguate_blob_only } | |
308 | }; | |
309 | int i; | |
310 | ||
311 | if (!value) | |
312 | return config_error_nonbool(var); | |
313 | ||
314 | for (i = 0; i < ARRAY_SIZE(hints); i++) { | |
315 | if (!strcasecmp(value, hints[i].name)) { | |
316 | default_disambiguate_hint = hints[i].fn; | |
317 | return 0; | |
318 | } | |
319 | } | |
320 | ||
321 | return error("unknown hint type for '%s': %s", var, value); | |
322 | } | |
323 | ||
ef9b0370 NTND |
324 | static int init_object_disambiguation(struct repository *r, |
325 | const char *name, int len, | |
0016043b | 326 | struct disambiguate_state *ds) |
9938af6a | 327 | { |
957d7406 | 328 | int i; |
9938af6a | 329 | |
7b38efad | 330 | if (len < MINIMUM_ABBREV || len > the_hash_algo->hexsz) |
0016043b JK |
331 | return -1; |
332 | ||
333 | memset(ds, 0, sizeof(*ds)); | |
0016043b | 334 | |
af61c6e0 | 335 | for (i = 0; i < len ;i++) { |
9938af6a JH |
336 | unsigned char c = name[i]; |
337 | unsigned char val; | |
9938af6a JH |
338 | if (c >= '0' && c <= '9') |
339 | val = c - '0'; | |
340 | else if (c >= 'a' && c <= 'f') | |
341 | val = c - 'a' + 10; | |
342 | else if (c >= 'A' && c <='F') { | |
343 | val = c - 'A' + 10; | |
344 | c -= 'A' - 'a'; | |
345 | } | |
346 | else | |
347 | return -1; | |
0016043b | 348 | ds->hex_pfx[i] = c; |
9938af6a JH |
349 | if (!(i & 1)) |
350 | val <<= 4; | |
d2ee1185 | 351 | ds->bin_pfx.hash[i >> 1] |= val; |
9938af6a | 352 | } |
0016043b JK |
353 | |
354 | ds->len = len; | |
59e4e34f | 355 | ds->hex_pfx[len] = '\0'; |
ef9b0370 NTND |
356 | ds->repo = r; |
357 | prepare_alt_odb(r); | |
957d7406 JH |
358 | return 0; |
359 | } | |
360 | ||
1b7ba794 | 361 | static int show_ambiguous_object(const struct object_id *oid, void *data) |
1ffa26c4 JK |
362 | { |
363 | const struct disambiguate_state *ds = data; | |
364 | struct strbuf desc = STRBUF_INIT; | |
365 | int type; | |
366 | ||
ef9b0370 | 367 | if (ds->fn && !ds->fn(ds->repo, oid, ds->cb_data)) |
1ffa26c4 JK |
368 | return 0; |
369 | ||
ef9b0370 | 370 | type = oid_object_info(ds->repo, oid, NULL); |
1ffa26c4 | 371 | if (type == OBJ_COMMIT) { |
ef9b0370 | 372 | struct commit *commit = lookup_commit(ds->repo, oid); |
1ffa26c4 JK |
373 | if (commit) { |
374 | struct pretty_print_context pp = {0}; | |
375 | pp.date_mode.type = DATE_SHORT; | |
376 | format_commit_message(commit, " %ad - %s", &desc, &pp); | |
377 | } | |
378 | } else if (type == OBJ_TAG) { | |
ef9b0370 | 379 | struct tag *tag = lookup_tag(ds->repo, oid); |
1ffa26c4 JK |
380 | if (!parse_tag(tag) && tag->tag) |
381 | strbuf_addf(&desc, " %s", tag->tag); | |
382 | } | |
383 | ||
384 | advise(" %s %s%s", | |
ef9b0370 | 385 | repo_find_unique_abbrev(ds->repo, oid, DEFAULT_ABBREV), |
debca9d2 | 386 | type_name(type) ? type_name(type) : "unknown type", |
1ffa26c4 JK |
387 | desc.buf); |
388 | ||
389 | strbuf_release(&desc); | |
390 | return 0; | |
391 | } | |
392 | ||
a885c93b ÆAB |
393 | static int collect_ambiguous(const struct object_id *oid, void *data) |
394 | { | |
395 | oid_array_append(data, oid); | |
396 | return 0; | |
397 | } | |
398 | ||
ef9b0370 NTND |
399 | static int repo_collect_ambiguous(struct repository *r, |
400 | const struct object_id *oid, | |
401 | void *data) | |
402 | { | |
403 | return collect_ambiguous(oid, data); | |
404 | } | |
405 | ||
7cfcb16b | 406 | static int sort_ambiguous(const void *a, const void *b, void *ctx) |
5cc044e0 | 407 | { |
7cfcb16b | 408 | struct repository *sort_ambiguous_repo = ctx; |
fae2ae45 NTND |
409 | int a_type = oid_object_info(sort_ambiguous_repo, a, NULL); |
410 | int b_type = oid_object_info(sort_ambiguous_repo, b, NULL); | |
5cc044e0 ÆAB |
411 | int a_type_sort; |
412 | int b_type_sort; | |
413 | ||
414 | /* | |
415 | * Sorts by hash within the same object type, just as | |
416 | * oid_array_for_each_unique() would do. | |
417 | */ | |
418 | if (a_type == b_type) | |
419 | return oidcmp(a, b); | |
420 | ||
421 | /* | |
422 | * Between object types show tags, then commits, and finally | |
423 | * trees and blobs. | |
424 | * | |
425 | * The object_type enum is commit, tree, blob, tag, but we | |
426 | * want tag, commit, tree blob. Cleverly (perhaps too | |
427 | * cleverly) do that with modulus, since the enum assigns 1 to | |
428 | * commit, so tag becomes 0. | |
429 | */ | |
430 | a_type_sort = a_type % 4; | |
431 | b_type_sort = b_type % 4; | |
432 | return a_type_sort > b_type_sort ? 1 : -1; | |
433 | } | |
434 | ||
fae2ae45 NTND |
435 | static void sort_ambiguous_oid_array(struct repository *r, struct oid_array *a) |
436 | { | |
7cfcb16b | 437 | QSORT_S(a->oid, a->nr, sort_ambiguous, r); |
fae2ae45 NTND |
438 | } |
439 | ||
c6c0235b NTND |
440 | static enum get_oid_result get_short_oid(struct repository *r, |
441 | const char *name, int len, | |
d1dd94b3 DT |
442 | struct object_id *oid, |
443 | unsigned flags) | |
957d7406 JH |
444 | { |
445 | int status; | |
957d7406 | 446 | struct disambiguate_state ds; |
321c89bf | 447 | int quietly = !!(flags & GET_OID_QUIETLY); |
957d7406 | 448 | |
c6c0235b | 449 | if (init_object_disambiguation(r, name, len, &ds) < 0) |
957d7406 | 450 | return -1; |
99a19b43 | 451 | |
321c89bf | 452 | if (HAS_MULTI_BITS(flags & GET_OID_DISAMBIGUATORS)) |
033abf97 | 453 | BUG("multiple get_short_oid disambiguator flags"); |
259942f5 | 454 | |
321c89bf | 455 | if (flags & GET_OID_COMMIT) |
aa1dec9e | 456 | ds.fn = disambiguate_commit_only; |
321c89bf | 457 | else if (flags & GET_OID_COMMITTISH) |
e2643617 | 458 | ds.fn = disambiguate_committish_only; |
321c89bf | 459 | else if (flags & GET_OID_TREE) |
daba53ae | 460 | ds.fn = disambiguate_tree_only; |
321c89bf | 461 | else if (flags & GET_OID_TREEISH) |
daba53ae | 462 | ds.fn = disambiguate_treeish_only; |
321c89bf | 463 | else if (flags & GET_OID_BLOB) |
daba53ae | 464 | ds.fn = disambiguate_blob_only; |
5b33cb1f JK |
465 | else |
466 | ds.fn = default_disambiguate_hint; | |
aa1dec9e | 467 | |
0016043b JK |
468 | find_short_object_filename(&ds); |
469 | find_short_packed_object(&ds); | |
e82caf38 | 470 | status = finish_object_disambiguation(&ds, oid); |
99a19b43 | 471 | |
6d67a993 JS |
472 | /* |
473 | * If we didn't find it, do the usual reprepare() slow-path, | |
474 | * since the object may have recently been added to the repository | |
475 | * or migrated from loose to packed. | |
476 | */ | |
477 | if (status == MISSING_OBJECT) { | |
34e7771b | 478 | reprepare_packed_git(r); |
6d67a993 JS |
479 | find_short_object_filename(&ds); |
480 | find_short_packed_object(&ds); | |
481 | status = finish_object_disambiguation(&ds, oid); | |
482 | } | |
483 | ||
1ffa26c4 | 484 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) { |
5cc044e0 ÆAB |
485 | struct oid_array collect = OID_ARRAY_INIT; |
486 | ||
1ffa26c4 JK |
487 | error(_("short SHA1 %s is ambiguous"), ds.hex_pfx); |
488 | ||
489 | /* | |
490 | * We may still have ambiguity if we simply saw a series of | |
491 | * candidates that did not satisfy our hint function. In | |
492 | * that case, we still want to show them, so disable the hint | |
493 | * function entirely. | |
494 | */ | |
495 | if (!ds.ambiguous) | |
496 | ds.fn = NULL; | |
497 | ||
498 | advise(_("The candidates are:")); | |
c6c0235b NTND |
499 | repo_for_each_abbrev(r, ds.hex_pfx, collect_ambiguous, &collect); |
500 | sort_ambiguous_oid_array(r, &collect); | |
5cc044e0 ÆAB |
501 | |
502 | if (oid_array_for_each(&collect, show_ambiguous_object, &ds)) | |
503 | BUG("show_ambiguous_object shouldn't return non-zero"); | |
504 | oid_array_clear(&collect); | |
1ffa26c4 JK |
505 | } |
506 | ||
013f276e JH |
507 | return status; |
508 | } | |
509 | ||
4e99f2db NTND |
510 | int repo_for_each_abbrev(struct repository *r, const char *prefix, |
511 | each_abbrev_fn fn, void *cb_data) | |
957d7406 | 512 | { |
910650d2 | 513 | struct oid_array collect = OID_ARRAY_INIT; |
957d7406 | 514 | struct disambiguate_state ds; |
fad6b9e5 | 515 | int ret; |
957d7406 | 516 | |
4e99f2db | 517 | if (init_object_disambiguation(r, prefix, strlen(prefix), &ds) < 0) |
957d7406 | 518 | return -1; |
957d7406 | 519 | |
957d7406 | 520 | ds.always_call_fn = 1; |
ef9b0370 | 521 | ds.fn = repo_collect_ambiguous; |
fad6b9e5 | 522 | ds.cb_data = &collect; |
0016043b JK |
523 | find_short_object_filename(&ds); |
524 | find_short_packed_object(&ds); | |
fad6b9e5 | 525 | |
910650d2 | 526 | ret = oid_array_for_each_unique(&collect, fn, cb_data); |
527 | oid_array_clear(&collect); | |
fad6b9e5 | 528 | return ret; |
957d7406 JH |
529 | } |
530 | ||
8e3f52d7 JK |
531 | /* |
532 | * Return the slot of the most-significant bit set in "val". There are various | |
533 | * ways to do this quickly with fls() or __builtin_clzl(), but speed is | |
534 | * probably not a big deal here. | |
535 | */ | |
536 | static unsigned msb(unsigned long val) | |
537 | { | |
538 | unsigned r = 0; | |
539 | while (val >>= 1) | |
540 | r++; | |
541 | return r; | |
542 | } | |
543 | ||
5b20ace6 DS |
544 | struct min_abbrev_data { |
545 | unsigned int init_len; | |
546 | unsigned int cur_len; | |
547 | char *hex; | |
7f07c033 | 548 | struct repository *repo; |
626fd982 | 549 | const struct object_id *oid; |
5b20ace6 DS |
550 | }; |
551 | ||
a42d6fd2 DS |
552 | static inline char get_hex_char_from_oid(const struct object_id *oid, |
553 | unsigned int pos) | |
554 | { | |
555 | static const char hex[] = "0123456789abcdef"; | |
556 | ||
557 | if ((pos & 1) == 0) | |
558 | return hex[oid->hash[pos >> 1] >> 4]; | |
559 | else | |
560 | return hex[oid->hash[pos >> 1] & 0xf]; | |
561 | } | |
562 | ||
5b20ace6 | 563 | static int extend_abbrev_len(const struct object_id *oid, void *cb_data) |
013f276e | 564 | { |
5b20ace6 | 565 | struct min_abbrev_data *mad = cb_data; |
47dd0d59 | 566 | |
5b20ace6 | 567 | unsigned int i = mad->init_len; |
a42d6fd2 | 568 | while (mad->hex[i] && mad->hex[i] == get_hex_char_from_oid(oid, i)) |
5b20ace6 DS |
569 | i++; |
570 | ||
571 | if (i < GIT_MAX_RAWSZ && i >= mad->cur_len) | |
572 | mad->cur_len = i + 1; | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
ef9b0370 NTND |
577 | static int repo_extend_abbrev_len(struct repository *r, |
578 | const struct object_id *oid, | |
579 | void *cb_data) | |
580 | { | |
581 | return extend_abbrev_len(oid, cb_data); | |
582 | } | |
583 | ||
8aac67a1 DS |
584 | static void find_abbrev_len_for_midx(struct multi_pack_index *m, |
585 | struct min_abbrev_data *mad) | |
586 | { | |
587 | int match = 0; | |
588 | uint32_t num, first = 0; | |
589 | struct object_id oid; | |
590 | const struct object_id *mad_oid; | |
591 | ||
592 | if (!m->num_objects) | |
593 | return; | |
594 | ||
595 | num = m->num_objects; | |
596 | mad_oid = mad->oid; | |
597 | match = bsearch_midx(mad_oid, m, &first); | |
598 | ||
599 | /* | |
600 | * first is now the position in the packfile where we would insert | |
601 | * mad->hash if it does not exist (or the position of mad->hash if | |
602 | * it does exist). Hence, we consider a maximum of two objects | |
603 | * nearby for the abbreviation length. | |
604 | */ | |
605 | mad->init_len = 0; | |
606 | if (!match) { | |
607 | if (nth_midxed_object_oid(&oid, m, first)) | |
608 | extend_abbrev_len(&oid, mad); | |
609 | } else if (first < num - 1) { | |
610 | if (nth_midxed_object_oid(&oid, m, first + 1)) | |
611 | extend_abbrev_len(&oid, mad); | |
612 | } | |
613 | if (first > 0) { | |
614 | if (nth_midxed_object_oid(&oid, m, first - 1)) | |
615 | extend_abbrev_len(&oid, mad); | |
616 | } | |
617 | mad->init_len = mad->cur_len; | |
618 | } | |
619 | ||
0e87b856 DS |
620 | static void find_abbrev_len_for_pack(struct packed_git *p, |
621 | struct min_abbrev_data *mad) | |
013f276e | 622 | { |
0e87b856 | 623 | int match = 0; |
0aaf05b3 | 624 | uint32_t num, first = 0; |
0e87b856 | 625 | struct object_id oid; |
0aaf05b3 | 626 | const struct object_id *mad_oid; |
0e87b856 | 627 | |
af96fe33 DS |
628 | if (p->multi_pack_index) |
629 | return; | |
630 | ||
0e87b856 DS |
631 | if (open_pack_index(p) || !p->num_objects) |
632 | return; | |
633 | ||
634 | num = p->num_objects; | |
0aaf05b3 DS |
635 | mad_oid = mad->oid; |
636 | match = bsearch_pack(mad_oid, p, &first); | |
0e87b856 DS |
637 | |
638 | /* | |
639 | * first is now the position in the packfile where we would insert | |
640 | * mad->hash if it does not exist (or the position of mad->hash if | |
21abed50 | 641 | * it does exist). Hence, we consider a maximum of two objects |
0e87b856 DS |
642 | * nearby for the abbreviation length. |
643 | */ | |
644 | mad->init_len = 0; | |
645 | if (!match) { | |
21abed50 DS |
646 | if (nth_packed_object_oid(&oid, p, first)) |
647 | extend_abbrev_len(&oid, mad); | |
0e87b856 | 648 | } else if (first < num - 1) { |
21abed50 DS |
649 | if (nth_packed_object_oid(&oid, p, first + 1)) |
650 | extend_abbrev_len(&oid, mad); | |
0e87b856 DS |
651 | } |
652 | if (first > 0) { | |
21abed50 DS |
653 | if (nth_packed_object_oid(&oid, p, first - 1)) |
654 | extend_abbrev_len(&oid, mad); | |
0e87b856 DS |
655 | } |
656 | mad->init_len = mad->cur_len; | |
657 | } | |
658 | ||
659 | static void find_abbrev_len_packed(struct min_abbrev_data *mad) | |
660 | { | |
8aac67a1 | 661 | struct multi_pack_index *m; |
0e87b856 DS |
662 | struct packed_git *p; |
663 | ||
7f07c033 | 664 | for (m = get_multi_pack_index(mad->repo); m; m = m->next) |
8aac67a1 | 665 | find_abbrev_len_for_midx(m, mad); |
7f07c033 | 666 | for (p = get_packed_git(mad->repo); p; p = p->next) |
0e87b856 DS |
667 | find_abbrev_len_for_pack(p, mad); |
668 | } | |
669 | ||
8bb95572 NTND |
670 | int repo_find_unique_abbrev_r(struct repository *r, char *hex, |
671 | const struct object_id *oid, int len) | |
5b20ace6 DS |
672 | { |
673 | struct disambiguate_state ds; | |
674 | struct min_abbrev_data mad; | |
675 | struct object_id oid_ret; | |
8bb95572 | 676 | const unsigned hexsz = r->hash_algo->hexsz; |
7b38efad | 677 | |
e6c587c7 | 678 | if (len < 0) { |
8bb95572 | 679 | unsigned long count = repo_approximate_object_count(r); |
8e3f52d7 JK |
680 | /* |
681 | * Add one because the MSB only tells us the highest bit set, | |
682 | * not including the value of all the _other_ bits (so "15" | |
683 | * is only one off of 2^4, but the MSB is the 3rd bit. | |
684 | */ | |
685 | len = msb(count) + 1; | |
686 | /* | |
687 | * We now know we have on the order of 2^len objects, which | |
688 | * expects a collision at 2^(len/2). But we also care about hex | |
689 | * chars, not bits, and there are 4 bits per hex. So all | |
42c78a21 | 690 | * together we need to divide by 2 and round up. |
8e3f52d7 | 691 | */ |
42c78a21 | 692 | len = DIV_ROUND_UP(len, 2); |
8e3f52d7 JK |
693 | /* |
694 | * For very small repos, we stick with our regular fallback. | |
695 | */ | |
696 | if (len < FALLBACK_DEFAULT_ABBREV) | |
697 | len = FALLBACK_DEFAULT_ABBREV; | |
e6c587c7 | 698 | } |
8e3f52d7 | 699 | |
aab9583f | 700 | oid_to_hex_r(hex, oid); |
7b38efad | 701 | if (len == hexsz || !len) |
702 | return hexsz; | |
5b20ace6 | 703 | |
8bb95572 | 704 | mad.repo = r; |
5b20ace6 DS |
705 | mad.init_len = len; |
706 | mad.cur_len = len; | |
707 | mad.hex = hex; | |
626fd982 | 708 | mad.oid = oid; |
0e87b856 DS |
709 | |
710 | find_abbrev_len_packed(&mad); | |
711 | ||
ef9b0370 | 712 | if (init_object_disambiguation(r, hex, mad.cur_len, &ds) < 0) |
0e87b856 | 713 | return -1; |
5b20ace6 | 714 | |
ef9b0370 | 715 | ds.fn = repo_extend_abbrev_len; |
5b20ace6 DS |
716 | ds.always_call_fn = 1; |
717 | ds.cb_data = (void *)&mad; | |
718 | ||
719 | find_short_object_filename(&ds); | |
5b20ace6 DS |
720 | (void)finish_object_disambiguation(&ds, &oid_ret); |
721 | ||
722 | hex[mad.cur_len] = 0; | |
723 | return mad.cur_len; | |
af49c6d0 JK |
724 | } |
725 | ||
8bb95572 NTND |
726 | const char *repo_find_unique_abbrev(struct repository *r, |
727 | const struct object_id *oid, | |
728 | int len) | |
af49c6d0 | 729 | { |
ef2ed501 | 730 | static int bufno; |
dc01505f | 731 | static char hexbuffer[4][GIT_MAX_HEXSZ + 1]; |
3e98919a RS |
732 | char *hex = hexbuffer[bufno]; |
733 | bufno = (bufno + 1) % ARRAY_SIZE(hexbuffer); | |
8bb95572 | 734 | repo_find_unique_abbrev_r(r, hex, oid, len); |
b66fde9a | 735 | return hex; |
9938af6a JH |
736 | } |
737 | ||
6677c466 | 738 | static int ambiguous_path(const char *path, int len) |
af13cdf2 LT |
739 | { |
740 | int slash = 1; | |
6677c466 | 741 | int cnt; |
af13cdf2 | 742 | |
6677c466 | 743 | for (cnt = 0; cnt < len; cnt++) { |
af13cdf2 LT |
744 | switch (*path++) { |
745 | case '\0': | |
746 | break; | |
747 | case '/': | |
748 | if (slash) | |
749 | break; | |
750 | slash = 1; | |
751 | continue; | |
752 | case '.': | |
753 | continue; | |
754 | default: | |
755 | slash = 0; | |
756 | continue; | |
757 | } | |
c054d64e | 758 | break; |
af13cdf2 | 759 | } |
6677c466 | 760 | return slash; |
af13cdf2 LT |
761 | } |
762 | ||
a1ad0eb0 JK |
763 | static inline int at_mark(const char *string, int len, |
764 | const char **suffix, int nr) | |
ae0ba8e2 | 765 | { |
ae0ba8e2 JH |
766 | int i; |
767 | ||
a1ad0eb0 | 768 | for (i = 0; i < nr; i++) { |
ae0ba8e2 JH |
769 | int suffix_len = strlen(suffix[i]); |
770 | if (suffix_len <= len | |
244ea1b5 | 771 | && !strncasecmp(string, suffix[i], suffix_len)) |
ae0ba8e2 JH |
772 | return suffix_len; |
773 | } | |
774 | return 0; | |
775 | } | |
776 | ||
a1ad0eb0 JK |
777 | static inline int upstream_mark(const char *string, int len) |
778 | { | |
779 | const char *suffix[] = { "@{upstream}", "@{u}" }; | |
780 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); | |
781 | } | |
782 | ||
adfe5d04 JK |
783 | static inline int push_mark(const char *string, int len) |
784 | { | |
785 | const char *suffix[] = { "@{push}" }; | |
786 | return at_mark(string, len, suffix, ARRAY_SIZE(suffix)); | |
787 | } | |
788 | ||
2b1790f5 | 789 | static enum get_oid_result get_oid_1(struct repository *r, const char *name, int len, struct object_id *oid, unsigned lookup_flags); |
23a57129 | 790 | static int interpret_nth_prior_checkout(struct repository *r, const char *name, int namelen, struct strbuf *buf); |
d18ba221 | 791 | |
49281cf5 NTND |
792 | static int get_oid_basic(struct repository *r, const char *str, int len, |
793 | struct object_id *oid, unsigned int flags) | |
e86eb666 | 794 | { |
eedce784 | 795 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
798c35fc NTND |
796 | static const char *object_name_msg = N_( |
797 | "Git normally never creates a ref that ends with 40 hex characters\n" | |
798 | "because it will be ignored when you just specify 40-hex. These refs\n" | |
799 | "may be created by mistake. For example,\n" | |
800 | "\n" | |
328c6cb8 | 801 | " git switch -c $br $(git rev-parse ...)\n" |
798c35fc NTND |
802 | "\n" |
803 | "where \"$br\" is somehow empty and a 40-hex ref is created. Please\n" | |
804 | "examine these refs and maybe delete them. Turn this message off by\n" | |
8dc84fdc | 805 | "running \"git config advice.objectNameWarning false\""); |
e82caf38 | 806 | struct object_id tmp_oid; |
ed378ec7 | 807 | char *real_ref = NULL; |
ab2a1a32 | 808 | int refs_found = 0; |
128fd54d | 809 | int at, reflog_len, nth_prior = 0; |
9938af6a | 810 | |
49281cf5 | 811 | if (len == r->hash_algo->hexsz && !get_oid_hex(str, oid)) { |
832cf74c | 812 | if (warn_ambiguous_refs && warn_on_object_refname_ambiguity) { |
49281cf5 | 813 | refs_found = repo_dwim_ref(r, str, len, &tmp_oid, &real_ref); |
832cf74c | 814 | if (refs_found > 0) { |
25fba78d JK |
815 | warning(warn_msg, len, str); |
816 | if (advice_object_name_warning) | |
817 | fprintf(stderr, "%s\n", _(object_name_msg)); | |
818 | } | |
819 | free(real_ref); | |
798c35fc | 820 | } |
9938af6a | 821 | return 0; |
798c35fc | 822 | } |
9938af6a | 823 | |
d18ba221 | 824 | /* basic@{time or number or -number} format to query ref-log */ |
694500ed | 825 | reflog_len = at = 0; |
f265458f | 826 | if (len && str[len-1] == '}') { |
e883a057 | 827 | for (at = len-4; at >= 0; at--) { |
ab2a1a32 | 828 | if (str[at] == '@' && str[at+1] == '{') { |
83d16bc7 RR |
829 | if (str[at+2] == '-') { |
830 | if (at != 0) | |
831 | /* @{-N} not at start */ | |
832 | return -1; | |
128fd54d FC |
833 | nth_prior = 1; |
834 | continue; | |
835 | } | |
adfe5d04 JK |
836 | if (!upstream_mark(str + at, len - at) && |
837 | !push_mark(str + at, len - at)) { | |
28fb8438 JS |
838 | reflog_len = (len-1) - (at+2); |
839 | len = at; | |
840 | } | |
ab2a1a32 JH |
841 | break; |
842 | } | |
d556fae2 SP |
843 | } |
844 | } | |
845 | ||
af13cdf2 | 846 | /* Accept only unambiguous ref paths. */ |
11cf8801 | 847 | if (len && ambiguous_path(str, len)) |
af13cdf2 LT |
848 | return -1; |
849 | ||
128fd54d | 850 | if (nth_prior) { |
d18ba221 | 851 | struct strbuf buf = STRBUF_INIT; |
128fd54d FC |
852 | int detached; |
853 | ||
49281cf5 NTND |
854 | if (interpret_nth_prior_checkout(r, str, len, &buf) > 0) { |
855 | detached = (buf.len == r->hash_algo->hexsz && !get_oid_hex(buf.buf, oid)); | |
128fd54d FC |
856 | strbuf_release(&buf); |
857 | if (detached) | |
858 | return 0; | |
d18ba221 | 859 | } |
128fd54d FC |
860 | } |
861 | ||
862 | if (!len && reflog_len) | |
11cf8801 | 863 | /* allow "@{...}" to mean the current branch reflog */ |
49281cf5 | 864 | refs_found = repo_dwim_ref(r, "HEAD", 4, oid, &real_ref); |
128fd54d | 865 | else if (reflog_len) |
49281cf5 | 866 | refs_found = repo_dwim_log(r, str, len, oid, &real_ref); |
f2eba66d | 867 | else |
49281cf5 | 868 | refs_found = repo_dwim_ref(r, str, len, oid, &real_ref); |
d556fae2 SP |
869 | |
870 | if (!refs_found) | |
871 | return -1; | |
872 | ||
321c89bf | 873 | if (warn_ambiguous_refs && !(flags & GET_OID_QUIETLY) && |
798c35fc | 874 | (refs_found > 1 || |
49281cf5 | 875 | !get_short_oid(r, str, len, &tmp_oid, GET_OID_QUIETLY))) |
eedce784 | 876 | warning(warn_msg, len, str); |
d556fae2 | 877 | |
ab2a1a32 | 878 | if (reflog_len) { |
ab2a1a32 | 879 | int nth, i; |
dddbad72 JS |
880 | timestamp_t at_time; |
881 | timestamp_t co_time; | |
16d7cc90 JH |
882 | int co_tz, co_cnt; |
883 | ||
fe558516 | 884 | /* Is it asking for N-th entry, or approxidate? */ |
ab2a1a32 JH |
885 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
886 | char ch = str[at+2+i]; | |
887 | if ('0' <= ch && ch <= '9') | |
888 | nth = nth * 10 + ch - '0'; | |
889 | else | |
890 | nth = -1; | |
891 | } | |
ea360dd0 SP |
892 | if (100000000 <= nth) { |
893 | at_time = nth; | |
894 | nth = -1; | |
895 | } else if (0 <= nth) | |
ab2a1a32 | 896 | at_time = 0; |
861f00e3 | 897 | else { |
93cfa7c7 | 898 | int errors = 0; |
861f00e3 | 899 | char *tmp = xstrndup(str + at + 2, reflog_len); |
93cfa7c7 | 900 | at_time = approxidate_careful(tmp, &errors); |
861f00e3 | 901 | free(tmp); |
28b35632 JK |
902 | if (errors) { |
903 | free(real_ref); | |
a5e10acb | 904 | return -1; |
28b35632 | 905 | } |
861f00e3 | 906 | } |
49281cf5 | 907 | if (read_ref_at(get_main_ref_store(r), |
7fdff474 | 908 | real_ref, flags, at_time, nth, oid, NULL, |
16d7cc90 | 909 | &co_time, &co_tz, &co_cnt)) { |
305ebea0 | 910 | if (!len) { |
59556548 | 911 | if (starts_with(real_ref, "refs/heads/")) { |
305ebea0 RR |
912 | str = real_ref + 11; |
913 | len = strlen(real_ref + 11); | |
914 | } else { | |
915 | /* detached HEAD */ | |
916 | str = "HEAD"; | |
917 | len = 4; | |
918 | } | |
919 | } | |
c41a87dd | 920 | if (at_time) { |
321c89bf | 921 | if (!(flags & GET_OID_QUIETLY)) { |
c41a87dd DA |
922 | warning("Log for '%.*s' only goes " |
923 | "back to %s.", len, str, | |
a5481a6c | 924 | show_date(co_time, co_tz, DATE_MODE(RFC2822))); |
c41a87dd DA |
925 | } |
926 | } else { | |
321c89bf | 927 | if (flags & GET_OID_QUIETLY) { |
c41a87dd DA |
928 | exit(128); |
929 | } | |
e6eedc31 JS |
930 | die("Log for '%.*s' only has %d entries.", |
931 | len, str, co_cnt); | |
932 | } | |
16d7cc90 | 933 | } |
d556fae2 SP |
934 | } |
935 | ||
ed378ec7 | 936 | free(real_ref); |
d556fae2 | 937 | return 0; |
9938af6a JH |
938 | } |
939 | ||
2b1790f5 NTND |
940 | static enum get_oid_result get_parent(struct repository *r, |
941 | const char *name, int len, | |
d1dd94b3 | 942 | struct object_id *result, int idx) |
9938af6a | 943 | { |
1e43ed98 | 944 | struct object_id oid; |
2b1790f5 | 945 | enum get_oid_result ret = get_oid_1(r, name, len, &oid, |
d1dd94b3 | 946 | GET_OID_COMMITTISH); |
9938af6a JH |
947 | struct commit *commit; |
948 | struct commit_list *p; | |
949 | ||
950 | if (ret) | |
951 | return ret; | |
2b1790f5 | 952 | commit = lookup_commit_reference(r, &oid); |
9938af6a | 953 | if (parse_commit(commit)) |
d1dd94b3 | 954 | return MISSING_OBJECT; |
9938af6a | 955 | if (!idx) { |
e82caf38 | 956 | oidcpy(result, &commit->object.oid); |
d1dd94b3 | 957 | return FOUND; |
9938af6a JH |
958 | } |
959 | p = commit->parents; | |
960 | while (p) { | |
961 | if (!--idx) { | |
e82caf38 | 962 | oidcpy(result, &p->item->object.oid); |
d1dd94b3 | 963 | return FOUND; |
9938af6a JH |
964 | } |
965 | p = p->next; | |
966 | } | |
d1dd94b3 | 967 | return MISSING_OBJECT; |
9938af6a JH |
968 | } |
969 | ||
2b1790f5 NTND |
970 | static enum get_oid_result get_nth_ancestor(struct repository *r, |
971 | const char *name, int len, | |
d1dd94b3 DT |
972 | struct object_id *result, |
973 | int generation) | |
4f7599ac | 974 | { |
1e43ed98 | 975 | struct object_id oid; |
621ff675 LT |
976 | struct commit *commit; |
977 | int ret; | |
978 | ||
2b1790f5 | 979 | ret = get_oid_1(r, name, len, &oid, GET_OID_COMMITTISH); |
4f7599ac JH |
980 | if (ret) |
981 | return ret; | |
2b1790f5 | 982 | commit = lookup_commit_reference(r, &oid); |
621ff675 | 983 | if (!commit) |
d1dd94b3 | 984 | return MISSING_OBJECT; |
4f7599ac JH |
985 | |
986 | while (generation--) { | |
621ff675 | 987 | if (parse_commit(commit) || !commit->parents) |
d1dd94b3 | 988 | return MISSING_OBJECT; |
621ff675 | 989 | commit = commit->parents->item; |
4f7599ac | 990 | } |
e82caf38 | 991 | oidcpy(result, &commit->object.oid); |
d1dd94b3 | 992 | return FOUND; |
4f7599ac JH |
993 | } |
994 | ||
2b1790f5 NTND |
995 | struct object *repo_peel_to_type(struct repository *r, const char *name, int namelen, |
996 | struct object *o, enum object_type expected_type) | |
81776315 JH |
997 | { |
998 | if (name && !namelen) | |
999 | namelen = strlen(name); | |
81776315 | 1000 | while (1) { |
2b1790f5 | 1001 | if (!o || (!o->parsed && !parse_object(r, &o->oid))) |
81776315 | 1002 | return NULL; |
a6a3f2cc | 1003 | if (expected_type == OBJ_ANY || o->type == expected_type) |
81776315 JH |
1004 | return o; |
1005 | if (o->type == OBJ_TAG) | |
1006 | o = ((struct tag*) o)->tagged; | |
1007 | else if (o->type == OBJ_COMMIT) | |
2b1790f5 | 1008 | o = &(repo_get_commit_tree(r, ((struct commit *)o))->object); |
81776315 JH |
1009 | else { |
1010 | if (name) | |
1011 | error("%.*s: expected %s type, but the object " | |
1012 | "dereferences to %s type", | |
debca9d2 BW |
1013 | namelen, name, type_name(expected_type), |
1014 | type_name(o->type)); | |
81776315 JH |
1015 | return NULL; |
1016 | } | |
1017 | } | |
1018 | } | |
1019 | ||
2b1790f5 NTND |
1020 | static int peel_onion(struct repository *r, const char *name, int len, |
1021 | struct object_id *oid, unsigned lookup_flags) | |
5385f52d | 1022 | { |
de37d50d | 1023 | struct object_id outer; |
5385f52d | 1024 | const char *sp; |
885a86ab | 1025 | unsigned int expected_type = 0; |
5385f52d JH |
1026 | struct object *o; |
1027 | ||
1028 | /* | |
1029 | * "ref^{type}" dereferences ref repeatedly until you cannot | |
1030 | * dereference anymore, or you get an object of given type, | |
1031 | * whichever comes first. "ref^{}" means just dereference | |
1032 | * tags until you get a non-tag. "ref^0" is a shorthand for | |
1033 | * "ref^{commit}". "commit^{tree}" could be used to find the | |
1034 | * top-level tree of the given commit. | |
1035 | */ | |
1036 | if (len < 4 || name[len-1] != '}') | |
1037 | return -1; | |
1038 | ||
1039 | for (sp = name + len - 1; name <= sp; sp--) { | |
1040 | int ch = *sp; | |
1041 | if (ch == '{' && name < sp && sp[-1] == '^') | |
1042 | break; | |
1043 | } | |
1044 | if (sp <= name) | |
1045 | return -1; | |
1046 | ||
1047 | sp++; /* beginning of type name, or closing brace for empty */ | |
59556548 | 1048 | if (starts_with(sp, "commit}")) |
1974632c | 1049 | expected_type = OBJ_COMMIT; |
59556548 | 1050 | else if (starts_with(sp, "tag}")) |
75aa26d3 | 1051 | expected_type = OBJ_TAG; |
59556548 | 1052 | else if (starts_with(sp, "tree}")) |
1974632c | 1053 | expected_type = OBJ_TREE; |
59556548 | 1054 | else if (starts_with(sp, "blob}")) |
1974632c | 1055 | expected_type = OBJ_BLOB; |
59556548 | 1056 | else if (starts_with(sp, "object}")) |
a6a3f2cc | 1057 | expected_type = OBJ_ANY; |
5385f52d | 1058 | else if (sp[0] == '}') |
1974632c | 1059 | expected_type = OBJ_NONE; |
32574b68 NTND |
1060 | else if (sp[0] == '/') |
1061 | expected_type = OBJ_COMMIT; | |
5385f52d JH |
1062 | else |
1063 | return -1; | |
1064 | ||
321c89bf | 1065 | lookup_flags &= ~GET_OID_DISAMBIGUATORS; |
e2643617 | 1066 | if (expected_type == OBJ_COMMIT) |
321c89bf | 1067 | lookup_flags |= GET_OID_COMMITTISH; |
ed1ca602 | 1068 | else if (expected_type == OBJ_TREE) |
321c89bf | 1069 | lookup_flags |= GET_OID_TREEISH; |
e2643617 | 1070 | |
2b1790f5 | 1071 | if (get_oid_1(r, name, sp - name - 2, &outer, lookup_flags)) |
5385f52d JH |
1072 | return -1; |
1073 | ||
2b1790f5 | 1074 | o = parse_object(r, &outer); |
5385f52d JH |
1075 | if (!o) |
1076 | return -1; | |
885a86ab | 1077 | if (!expected_type) { |
2b1790f5 NTND |
1078 | o = deref_tag(r, o, name, sp - name - 2); |
1079 | if (!o || (!o->parsed && !parse_object(r, &o->oid))) | |
6e1c6c10 | 1080 | return -1; |
e82caf38 | 1081 | oidcpy(oid, &o->oid); |
32574b68 | 1082 | return 0; |
5385f52d | 1083 | } |
32574b68 NTND |
1084 | |
1085 | /* | |
1086 | * At this point, the syntax look correct, so | |
1087 | * if we do not get the needed object, we should | |
1088 | * barf. | |
1089 | */ | |
2b1790f5 | 1090 | o = repo_peel_to_type(r, name, len, o, expected_type); |
32574b68 | 1091 | if (!o) |
81776315 | 1092 | return -1; |
32574b68 | 1093 | |
e82caf38 | 1094 | oidcpy(oid, &o->oid); |
32574b68 NTND |
1095 | if (sp[0] == '/') { |
1096 | /* "$commit^{/foo}" */ | |
1097 | char *prefix; | |
1098 | int ret; | |
1099 | struct commit_list *list = NULL; | |
1100 | ||
81776315 | 1101 | /* |
4322842a NTND |
1102 | * $commit^{/}. Some regex implementation may reject. |
1103 | * We don't need regex anyway. '' pattern always matches. | |
5385f52d | 1104 | */ |
4322842a | 1105 | if (sp[1] == '}') |
81776315 | 1106 | return 0; |
4322842a | 1107 | |
32574b68 NTND |
1108 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
1109 | commit_list_insert((struct commit *)o, &list); | |
2b1790f5 | 1110 | ret = get_oid_oneline(r, prefix, oid, list); |
32574b68 NTND |
1111 | free(prefix); |
1112 | return ret; | |
5385f52d JH |
1113 | } |
1114 | return 0; | |
1115 | } | |
1116 | ||
0c6b5ba1 NTND |
1117 | static int get_describe_name(struct repository *r, |
1118 | const char *name, int len, | |
1119 | struct object_id *oid) | |
7dd45e15 JH |
1120 | { |
1121 | const char *cp; | |
321c89bf | 1122 | unsigned flags = GET_OID_QUIETLY | GET_OID_COMMIT; |
7dd45e15 JH |
1123 | |
1124 | for (cp = name + len - 1; name + 2 <= cp; cp--) { | |
1125 | char ch = *cp; | |
6f75d45b | 1126 | if (!isxdigit(ch)) { |
7dd45e15 JH |
1127 | /* We must be looking at g in "SOMETHING-g" |
1128 | * for it to be describe output. | |
1129 | */ | |
1130 | if (ch == 'g' && cp[-1] == '-') { | |
1131 | cp++; | |
1132 | len -= cp - name; | |
0c6b5ba1 | 1133 | return get_short_oid(r, |
c6c0235b | 1134 | cp, len, oid, flags); |
7dd45e15 JH |
1135 | } |
1136 | } | |
1137 | } | |
1138 | return -1; | |
1139 | } | |
1140 | ||
2b1790f5 NTND |
1141 | static enum get_oid_result get_oid_1(struct repository *r, |
1142 | const char *name, int len, | |
d1dd94b3 DT |
1143 | struct object_id *oid, |
1144 | unsigned lookup_flags) | |
9938af6a | 1145 | { |
0601dbe1 | 1146 | int ret, has_suffix; |
4f7599ac | 1147 | const char *cp; |
9938af6a | 1148 | |
621ff675 LT |
1149 | /* |
1150 | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". | |
4f7599ac | 1151 | */ |
0601dbe1 | 1152 | has_suffix = 0; |
4f7599ac JH |
1153 | for (cp = name + len - 1; name <= cp; cp--) { |
1154 | int ch = *cp; | |
1155 | if ('0' <= ch && ch <= '9') | |
1156 | continue; | |
0601dbe1 JH |
1157 | if (ch == '~' || ch == '^') |
1158 | has_suffix = ch; | |
4f7599ac JH |
1159 | break; |
1160 | } | |
0601dbe1 JH |
1161 | |
1162 | if (has_suffix) { | |
59fa5f5a | 1163 | unsigned int num = 0; |
4f7599ac JH |
1164 | int len1 = cp - name; |
1165 | cp++; | |
59fa5f5a RS |
1166 | while (cp < name + len) { |
1167 | unsigned int digit = *cp++ - '0'; | |
1168 | if (unsigned_mult_overflows(num, 10)) | |
1169 | return MISSING_OBJECT; | |
1170 | num *= 10; | |
1171 | if (unsigned_add_overflows(num, digit)) | |
1172 | return MISSING_OBJECT; | |
1173 | num += digit; | |
1174 | } | |
621ff675 LT |
1175 | if (!num && len1 == len - 1) |
1176 | num = 1; | |
59fa5f5a RS |
1177 | else if (num > INT_MAX) |
1178 | return MISSING_OBJECT; | |
621ff675 | 1179 | if (has_suffix == '^') |
2b1790f5 | 1180 | return get_parent(r, name, len1, oid, num); |
0601dbe1 | 1181 | /* else if (has_suffix == '~') -- goes without saying */ |
2b1790f5 | 1182 | return get_nth_ancestor(r, name, len1, oid, num); |
4f7599ac JH |
1183 | } |
1184 | ||
2b1790f5 | 1185 | ret = peel_onion(r, name, len, oid, lookup_flags); |
5385f52d | 1186 | if (!ret) |
d1dd94b3 | 1187 | return FOUND; |
5385f52d | 1188 | |
2b1790f5 | 1189 | ret = get_oid_basic(r, name, len, oid, lookup_flags); |
9938af6a | 1190 | if (!ret) |
d1dd94b3 | 1191 | return FOUND; |
7dd45e15 JH |
1192 | |
1193 | /* It could be describe output that is "SOMETHING-gXXXX" */ | |
2b1790f5 | 1194 | ret = get_describe_name(r, name, len, oid); |
7dd45e15 | 1195 | if (!ret) |
d1dd94b3 | 1196 | return FOUND; |
7dd45e15 | 1197 | |
2b1790f5 | 1198 | return get_short_oid(r, name, len, oid, lookup_flags); |
9938af6a JH |
1199 | } |
1200 | ||
f7bff003 JH |
1201 | /* |
1202 | * This interprets names like ':/Initial revision of "git"' by searching | |
1203 | * through history and returning the first commit whose message starts | |
3d045897 | 1204 | * the given regular expression. |
f7bff003 | 1205 | * |
0769854f WP |
1206 | * For negative-matching, prefix the pattern-part with '!-', like: ':/!-WIP'. |
1207 | * | |
1208 | * For a literal '!' character at the beginning of a pattern, you have to repeat | |
1209 | * that, like: ':/!!foo' | |
1210 | * | |
1211 | * For future extension, all other sequences beginning with ':/!' are reserved. | |
f7bff003 | 1212 | */ |
208acbfb NTND |
1213 | |
1214 | /* Remember to update object flag allocation in object.h */ | |
f7bff003 JH |
1215 | #define ONELINE_SEEN (1u<<20) |
1216 | ||
c931ba4e NTND |
1217 | struct handle_one_ref_cb { |
1218 | struct repository *repo; | |
1219 | struct commit_list **list; | |
1220 | }; | |
1221 | ||
9c5fe0b8 MH |
1222 | static int handle_one_ref(const char *path, const struct object_id *oid, |
1223 | int flag, void *cb_data) | |
28a4d940 | 1224 | { |
c931ba4e NTND |
1225 | struct handle_one_ref_cb *cb = cb_data; |
1226 | struct commit_list **list = cb->list; | |
1227 | struct object *object = parse_object(cb->repo, oid); | |
28a4d940 JS |
1228 | if (!object) |
1229 | return 0; | |
affeef12 | 1230 | if (object->type == OBJ_TAG) { |
c931ba4e | 1231 | object = deref_tag(cb->repo, object, path, |
a74093da | 1232 | strlen(path)); |
affeef12 MK |
1233 | if (!object) |
1234 | return 0; | |
1235 | } | |
28a4d940 JS |
1236 | if (object->type != OBJ_COMMIT) |
1237 | return 0; | |
e8d1dfe6 | 1238 | commit_list_insert((struct commit *)object, list); |
28a4d940 JS |
1239 | return 0; |
1240 | } | |
1241 | ||
0bb41a19 NTND |
1242 | static int get_oid_oneline(struct repository *r, |
1243 | const char *prefix, struct object_id *oid, | |
1244 | struct commit_list *list) | |
28a4d940 | 1245 | { |
84baa31b | 1246 | struct commit_list *backup = NULL, *l; |
28042dbc | 1247 | int found = 0; |
0769854f | 1248 | int negative = 0; |
57895105 | 1249 | regex_t regex; |
28a4d940 JS |
1250 | |
1251 | if (prefix[0] == '!') { | |
28a4d940 | 1252 | prefix++; |
0769854f WP |
1253 | |
1254 | if (prefix[0] == '-') { | |
1255 | prefix++; | |
1256 | negative = 1; | |
1257 | } else if (prefix[0] != '!') { | |
e6a6a768 | 1258 | return -1; |
0769854f | 1259 | } |
28a4d940 | 1260 | } |
57895105 LT |
1261 | |
1262 | if (regcomp(®ex, prefix, REG_EXTENDED)) | |
aac4fac1 | 1263 | return -1; |
57895105 | 1264 | |
84baa31b NTND |
1265 | for (l = list; l; l = l->next) { |
1266 | l->item->object.flags |= ONELINE_SEEN; | |
28a4d940 | 1267 | commit_list_insert(l->item, &backup); |
84baa31b | 1268 | } |
ed8ad7e2 | 1269 | while (list) { |
ba41c1c9 | 1270 | const char *p, *buf; |
1358e7d6 | 1271 | struct commit *commit; |
28042dbc | 1272 | int matches; |
ed8ad7e2 JM |
1273 | |
1274 | commit = pop_most_recent_commit(&list, ONELINE_SEEN); | |
0bb41a19 | 1275 | if (!parse_object(r, &commit->object.oid)) |
283cdbcf | 1276 | continue; |
8597ea3a | 1277 | buf = get_commit_buffer(commit, NULL); |
ba41c1c9 | 1278 | p = strstr(buf, "\n\n"); |
0769854f | 1279 | matches = negative ^ (p && !regexec(®ex, p + 2, 0, NULL, 0)); |
ba41c1c9 | 1280 | unuse_commit_buffer(commit, buf); |
28042dbc JH |
1281 | |
1282 | if (matches) { | |
e82caf38 | 1283 | oidcpy(oid, &commit->object.oid); |
28042dbc | 1284 | found = 1; |
28a4d940 JS |
1285 | break; |
1286 | } | |
1287 | } | |
57895105 | 1288 | regfree(®ex); |
28a4d940 JS |
1289 | free_commit_list(list); |
1290 | for (l = backup; l; l = l->next) | |
1291 | clear_commit_marks(l->item, ONELINE_SEEN); | |
28042dbc JH |
1292 | free_commit_list(backup); |
1293 | return found ? 0 : -1; | |
28a4d940 JS |
1294 | } |
1295 | ||
ae5a6c36 | 1296 | struct grab_nth_branch_switch_cbdata { |
98f85ff4 | 1297 | int remaining; |
4b3aa170 | 1298 | struct strbuf *sb; |
ae5a6c36 JH |
1299 | }; |
1300 | ||
9461d272 | 1301 | static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid, |
dddbad72 | 1302 | const char *email, timestamp_t timestamp, int tz, |
ae5a6c36 JH |
1303 | const char *message, void *cb_data) |
1304 | { | |
1305 | struct grab_nth_branch_switch_cbdata *cb = cb_data; | |
a884d0cb TR |
1306 | const char *match = NULL, *target = NULL; |
1307 | size_t len; | |
1308 | ||
95b567c7 | 1309 | if (skip_prefix(message, "checkout: moving from ", &match)) |
d7c03c1f | 1310 | target = strstr(match, " to "); |
ae5a6c36 | 1311 | |
c829774c | 1312 | if (!match || !target) |
ae5a6c36 | 1313 | return 0; |
98f85ff4 JH |
1314 | if (--(cb->remaining) == 0) { |
1315 | len = target - match; | |
4b3aa170 RS |
1316 | strbuf_reset(cb->sb); |
1317 | strbuf_add(cb->sb, match, len); | |
98f85ff4 JH |
1318 | return 1; /* we are done */ |
1319 | } | |
ae5a6c36 JH |
1320 | return 0; |
1321 | } | |
1322 | ||
1323 | /* | |
ae0ba8e2 JH |
1324 | * Parse @{-N} syntax, return the number of characters parsed |
1325 | * if successful; otherwise signal an error with negative value. | |
ae5a6c36 | 1326 | */ |
23a57129 NTND |
1327 | static int interpret_nth_prior_checkout(struct repository *r, |
1328 | const char *name, int namelen, | |
8cd4249c | 1329 | struct strbuf *buf) |
ae5a6c36 | 1330 | { |
c2883e62 | 1331 | long nth; |
98f85ff4 | 1332 | int retval; |
ae5a6c36 | 1333 | struct grab_nth_branch_switch_cbdata cb; |
a884d0cb TR |
1334 | const char *brace; |
1335 | char *num_end; | |
ae5a6c36 | 1336 | |
8cd4249c JK |
1337 | if (namelen < 4) |
1338 | return -1; | |
ae5a6c36 JH |
1339 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') |
1340 | return -1; | |
8cd4249c | 1341 | brace = memchr(name, '}', namelen); |
a884d0cb TR |
1342 | if (!brace) |
1343 | return -1; | |
98f85ff4 | 1344 | nth = strtol(name + 3, &num_end, 10); |
a884d0cb | 1345 | if (num_end != brace) |
ae5a6c36 | 1346 | return -1; |
c2883e62 JH |
1347 | if (nth <= 0) |
1348 | return -1; | |
98f85ff4 | 1349 | cb.remaining = nth; |
4b3aa170 | 1350 | cb.sb = buf; |
98f85ff4 | 1351 | |
23a57129 NTND |
1352 | retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r), |
1353 | "HEAD", grab_nth_branch_switch, &cb); | |
1354 | if (0 < retval) { | |
98f85ff4 | 1355 | retval = brace - name + 1; |
23a57129 NTND |
1356 | } else |
1357 | retval = 0; | |
a884d0cb | 1358 | |
39765e59 | 1359 | return retval; |
ae5a6c36 JH |
1360 | } |
1361 | ||
0daf7ff6 NTND |
1362 | int repo_get_oid_mb(struct repository *r, |
1363 | const char *name, | |
1364 | struct object_id *oid) | |
619a644d JH |
1365 | { |
1366 | struct commit *one, *two; | |
1367 | struct commit_list *mbs; | |
151b2911 | 1368 | struct object_id oid_tmp; |
619a644d JH |
1369 | const char *dots; |
1370 | int st; | |
1371 | ||
1372 | dots = strstr(name, "..."); | |
1373 | if (!dots) | |
0daf7ff6 | 1374 | return repo_get_oid(r, name, oid); |
619a644d | 1375 | if (dots == name) |
0daf7ff6 | 1376 | st = repo_get_oid(r, "HEAD", &oid_tmp); |
619a644d JH |
1377 | else { |
1378 | struct strbuf sb; | |
1379 | strbuf_init(&sb, dots - name); | |
1380 | strbuf_add(&sb, name, dots - name); | |
0daf7ff6 | 1381 | st = repo_get_oid_committish(r, sb.buf, &oid_tmp); |
619a644d JH |
1382 | strbuf_release(&sb); |
1383 | } | |
1384 | if (st) | |
1385 | return st; | |
0daf7ff6 | 1386 | one = lookup_commit_reference_gently(r, &oid_tmp, 0); |
619a644d JH |
1387 | if (!one) |
1388 | return -1; | |
1389 | ||
0daf7ff6 | 1390 | if (repo_get_oid_committish(r, dots[3] ? (dots + 3) : "HEAD", &oid_tmp)) |
619a644d | 1391 | return -1; |
0daf7ff6 | 1392 | two = lookup_commit_reference_gently(r, &oid_tmp, 0); |
619a644d JH |
1393 | if (!two) |
1394 | return -1; | |
34e7771b | 1395 | mbs = repo_get_merge_bases(r, one, two); |
619a644d JH |
1396 | if (!mbs || mbs->next) |
1397 | st = -1; | |
1398 | else { | |
1399 | st = 0; | |
151b2911 | 1400 | oidcpy(oid, &mbs->item->object.oid); |
619a644d JH |
1401 | } |
1402 | free_commit_list(mbs); | |
1403 | return st; | |
1404 | } | |
1405 | ||
9ba89f48 FC |
1406 | /* parse @something syntax, when 'something' is not {.*} */ |
1407 | static int interpret_empty_at(const char *name, int namelen, int len, struct strbuf *buf) | |
1408 | { | |
1409 | const char *next; | |
1410 | ||
1411 | if (len || name[1] == '{') | |
1412 | return -1; | |
1413 | ||
1414 | /* make sure it's a single @, or @@{.*}, not @foo */ | |
8cd4249c | 1415 | next = memchr(name + len + 1, '@', namelen - len - 1); |
9ba89f48 FC |
1416 | if (next && next[1] != '{') |
1417 | return -1; | |
1418 | if (!next) | |
1419 | next = name + namelen; | |
1420 | if (next != name + 1) | |
1421 | return -1; | |
1422 | ||
1423 | strbuf_reset(buf); | |
1424 | strbuf_add(buf, "HEAD", 4); | |
1425 | return 1; | |
1426 | } | |
1427 | ||
71588ed2 NTND |
1428 | static int reinterpret(struct repository *r, |
1429 | const char *name, int namelen, int len, | |
0e9f62da | 1430 | struct strbuf *buf, unsigned allowed) |
7a0a49a7 FC |
1431 | { |
1432 | /* we have extra data, which might need further processing */ | |
1433 | struct strbuf tmp = STRBUF_INIT; | |
1434 | int used = buf->len; | |
1435 | int ret; | |
1436 | ||
1437 | strbuf_add(buf, name + len, namelen - len); | |
71588ed2 | 1438 | ret = repo_interpret_branch_name(r, buf->buf, buf->len, &tmp, allowed); |
7a0a49a7 FC |
1439 | /* that data was not interpreted, remove our cruft */ |
1440 | if (ret < 0) { | |
1441 | strbuf_setlen(buf, used); | |
1442 | return len; | |
1443 | } | |
1444 | strbuf_reset(buf); | |
1445 | strbuf_addbuf(buf, &tmp); | |
1446 | strbuf_release(&tmp); | |
1447 | /* tweak for size of {-N} versus expanded ref name */ | |
1448 | return ret - used + len; | |
1449 | } | |
1450 | ||
ea1c873c | 1451 | static void set_shortened_ref(struct repository *r, struct strbuf *buf, const char *ref) |
a39c14af | 1452 | { |
ea1c873c | 1453 | char *s = refs_shorten_unambiguous_ref(get_main_ref_store(r), ref, 0); |
a39c14af JK |
1454 | strbuf_reset(buf); |
1455 | strbuf_addstr(buf, s); | |
1456 | free(s); | |
1457 | } | |
1458 | ||
0e9f62da JK |
1459 | static int branch_interpret_allowed(const char *refname, unsigned allowed) |
1460 | { | |
1461 | if (!allowed) | |
1462 | return 1; | |
1463 | ||
1464 | if ((allowed & INTERPRET_BRANCH_LOCAL) && | |
1465 | starts_with(refname, "refs/heads/")) | |
1466 | return 1; | |
1467 | if ((allowed & INTERPRET_BRANCH_REMOTE) && | |
1468 | starts_with(refname, "refs/remotes/")) | |
1469 | return 1; | |
1470 | ||
1471 | return 0; | |
1472 | } | |
1473 | ||
ea1c873c NTND |
1474 | static int interpret_branch_mark(struct repository *r, |
1475 | const char *name, int namelen, | |
48c58471 JK |
1476 | int at, struct strbuf *buf, |
1477 | int (*get_mark)(const char *, int), | |
1478 | const char *(*get_data)(struct branch *, | |
0e9f62da JK |
1479 | struct strbuf *), |
1480 | unsigned allowed) | |
a39c14af JK |
1481 | { |
1482 | int len; | |
48c58471 JK |
1483 | struct branch *branch; |
1484 | struct strbuf err = STRBUF_INIT; | |
1485 | const char *value; | |
a39c14af | 1486 | |
48c58471 | 1487 | len = get_mark(name + at, namelen - at); |
a39c14af JK |
1488 | if (!len) |
1489 | return -1; | |
1490 | ||
3f6eb30f JK |
1491 | if (memchr(name, ':', at)) |
1492 | return -1; | |
1493 | ||
48c58471 JK |
1494 | if (at) { |
1495 | char *name_str = xmemdupz(name, at); | |
1496 | branch = branch_get(name_str); | |
1497 | free(name_str); | |
1498 | } else | |
1499 | branch = branch_get(NULL); | |
1500 | ||
1501 | value = get_data(branch, &err); | |
1502 | if (!value) | |
1503 | die("%s", err.buf); | |
1504 | ||
0e9f62da JK |
1505 | if (!branch_interpret_allowed(value, allowed)) |
1506 | return -1; | |
1507 | ||
ea1c873c | 1508 | set_shortened_ref(r, buf, value); |
a39c14af JK |
1509 | return len + at; |
1510 | } | |
1511 | ||
8f56e9d4 NTND |
1512 | int repo_interpret_branch_name(struct repository *r, |
1513 | const char *name, int namelen, | |
1514 | struct strbuf *buf, | |
1515 | unsigned allowed) | |
ae0ba8e2 | 1516 | { |
f278f40f | 1517 | char *at; |
9892d5d4 | 1518 | const char *start; |
13228c30 | 1519 | int len; |
ae0ba8e2 | 1520 | |
cf99a761 FC |
1521 | if (!namelen) |
1522 | namelen = strlen(name); | |
1523 | ||
0e9f62da | 1524 | if (!allowed || (allowed & INTERPRET_BRANCH_LOCAL)) { |
71588ed2 | 1525 | len = interpret_nth_prior_checkout(r, name, namelen, buf); |
0e9f62da JK |
1526 | if (!len) { |
1527 | return len; /* syntax Ok, not enough switches */ | |
1528 | } else if (len > 0) { | |
1529 | if (len == namelen) | |
1530 | return len; /* consumed all */ | |
1531 | else | |
71588ed2 | 1532 | return reinterpret(r, name, namelen, len, buf, allowed); |
0e9f62da | 1533 | } |
d46a8301 JK |
1534 | } |
1535 | ||
9892d5d4 JK |
1536 | for (start = name; |
1537 | (at = memchr(start, '@', namelen - (start - name))); | |
1538 | start = at + 1) { | |
9ba89f48 | 1539 | |
0e9f62da JK |
1540 | if (!allowed || (allowed & INTERPRET_BRANCH_HEAD)) { |
1541 | len = interpret_empty_at(name, namelen, at - name, buf); | |
1542 | if (len > 0) | |
71588ed2 | 1543 | return reinterpret(r, name, namelen, len, buf, |
0e9f62da JK |
1544 | allowed); |
1545 | } | |
9ba89f48 | 1546 | |
71588ed2 | 1547 | len = interpret_branch_mark(r, name, namelen, at - name, buf, |
0e9f62da JK |
1548 | upstream_mark, branch_get_upstream, |
1549 | allowed); | |
9892d5d4 JK |
1550 | if (len > 0) |
1551 | return len; | |
adfe5d04 | 1552 | |
71588ed2 | 1553 | len = interpret_branch_mark(r, name, namelen, at - name, buf, |
0e9f62da JK |
1554 | push_mark, branch_get_push, |
1555 | allowed); | |
9892d5d4 JK |
1556 | if (len > 0) |
1557 | return len; | |
bb0dab5d | 1558 | } |
9ba89f48 | 1559 | |
a39c14af | 1560 | return -1; |
ae0ba8e2 JH |
1561 | } |
1562 | ||
0e9f62da | 1563 | void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) |
6bab74e7 JN |
1564 | { |
1565 | int len = strlen(name); | |
0e9f62da | 1566 | int used = interpret_branch_name(name, len, sb, allowed); |
84cf2466 | 1567 | |
84cf2466 JH |
1568 | if (used < 0) |
1569 | used = 0; | |
1570 | strbuf_add(sb, name + used, len - used); | |
6bab74e7 JN |
1571 | } |
1572 | ||
1573 | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) | |
1574 | { | |
7c3f847a JH |
1575 | if (startup_info->have_repository) |
1576 | strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); | |
1577 | else | |
1578 | strbuf_addstr(sb, name); | |
a625b092 JH |
1579 | |
1580 | /* | |
1581 | * This splice must be done even if we end up rejecting the | |
1582 | * name; builtin/branch.c::copy_or_rename_branch() still wants | |
1583 | * to see what the name expanded to so that "branch -m" can be | |
1584 | * used as a tool to correct earlier mistakes. | |
1585 | */ | |
6bab74e7 | 1586 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
a625b092 JH |
1587 | |
1588 | if (*name == '-' || | |
1589 | !strcmp(sb->buf, "refs/heads/HEAD")) | |
1590 | return -1; | |
1591 | ||
8d9c5010 | 1592 | return check_refname_format(sb->buf, 0); |
6bab74e7 JN |
1593 | } |
1594 | ||
9938af6a | 1595 | /* |
e82caf38 | 1596 | * This is like "get_oid_basic()", except it allows "object ID expressions", |
9938af6a JH |
1597 | * notably "xyz^" for "parent of xyz" |
1598 | */ | |
ec580eaa | 1599 | int repo_get_oid(struct repository *r, const char *name, struct object_id *oid) |
2764fd93 | 1600 | { |
e82caf38 | 1601 | struct object_context unused; |
ec580eaa | 1602 | return get_oid_with_context(r, name, 0, oid, &unused); |
2764fd93 | 1603 | } |
1604 | ||
f5116f43 PSU |
1605 | /* |
1606 | * This returns a non-zero value if the string (built using printf | |
1607 | * format and the given arguments) is not a valid object. | |
1608 | */ | |
1609 | int get_oidf(struct object_id *oid, const char *fmt, ...) | |
1610 | { | |
1611 | va_list ap; | |
1612 | int ret; | |
1613 | struct strbuf sb = STRBUF_INIT; | |
1614 | ||
1615 | va_start(ap, fmt); | |
1616 | strbuf_vaddf(&sb, fmt, ap); | |
1617 | va_end(ap); | |
1618 | ||
1619 | ret = get_oid(sb.buf, oid); | |
1620 | strbuf_release(&sb); | |
1621 | ||
1622 | return ret; | |
1623 | } | |
2764fd93 | 1624 | |
cd74e473 | 1625 | /* |
a8a5406a | 1626 | * Many callers know that the user meant to name a commit-ish by |
cd74e473 JH |
1627 | * syntactical positions where the object name appears. Calling this |
1628 | * function allows the machinery to disambiguate shorter-than-unique | |
a8a5406a | 1629 | * abbreviated object names between commit-ish and others. |
cd74e473 JH |
1630 | * |
1631 | * Note that this does NOT error out when the named object is not a | |
a8a5406a | 1632 | * commit-ish. It is merely to give a hint to the disambiguation |
cd74e473 JH |
1633 | * machinery. |
1634 | */ | |
65e50464 NTND |
1635 | int repo_get_oid_committish(struct repository *r, |
1636 | const char *name, | |
1637 | struct object_id *oid) | |
cd74e473 JH |
1638 | { |
1639 | struct object_context unused; | |
65e50464 | 1640 | return get_oid_with_context(r, name, GET_OID_COMMITTISH, |
e82caf38 | 1641 | oid, &unused); |
cd74e473 JH |
1642 | } |
1643 | ||
65e50464 NTND |
1644 | int repo_get_oid_treeish(struct repository *r, |
1645 | const char *name, | |
1646 | struct object_id *oid) | |
daba53ae JH |
1647 | { |
1648 | struct object_context unused; | |
65e50464 | 1649 | return get_oid_with_context(r, name, GET_OID_TREEISH, |
e82caf38 | 1650 | oid, &unused); |
daba53ae JH |
1651 | } |
1652 | ||
65e50464 NTND |
1653 | int repo_get_oid_commit(struct repository *r, |
1654 | const char *name, | |
1655 | struct object_id *oid) | |
daba53ae JH |
1656 | { |
1657 | struct object_context unused; | |
65e50464 | 1658 | return get_oid_with_context(r, name, GET_OID_COMMIT, |
e82caf38 | 1659 | oid, &unused); |
daba53ae JH |
1660 | } |
1661 | ||
65e50464 NTND |
1662 | int repo_get_oid_tree(struct repository *r, |
1663 | const char *name, | |
1664 | struct object_id *oid) | |
daba53ae JH |
1665 | { |
1666 | struct object_context unused; | |
65e50464 | 1667 | return get_oid_with_context(r, name, GET_OID_TREE, |
e82caf38 | 1668 | oid, &unused); |
daba53ae JH |
1669 | } |
1670 | ||
65e50464 NTND |
1671 | int repo_get_oid_blob(struct repository *r, |
1672 | const char *name, | |
1673 | struct object_id *oid) | |
daba53ae JH |
1674 | { |
1675 | struct object_context unused; | |
65e50464 | 1676 | return get_oid_with_context(r, name, GET_OID_BLOB, |
e82caf38 | 1677 | oid, &unused); |
a0cd87a5 MK |
1678 | } |
1679 | ||
009fee47 | 1680 | /* Must be called only when object_name:filename doesn't exist. */ |
50ddb089 NTND |
1681 | static void diagnose_invalid_oid_path(struct repository *r, |
1682 | const char *prefix, | |
e82caf38 | 1683 | const char *filename, |
1684 | const struct object_id *tree_oid, | |
1685 | const char *object_name, | |
1686 | int object_name_len) | |
009fee47 | 1687 | { |
e82caf38 | 1688 | struct object_id oid; |
5ec1e728 | 1689 | unsigned short mode; |
009fee47 MM |
1690 | |
1691 | if (!prefix) | |
1692 | prefix = ""; | |
1693 | ||
dbe44faa | 1694 | if (file_exists(filename)) |
b2981d06 RS |
1695 | die("Path '%s' exists on disk, but not in '%.*s'.", |
1696 | filename, object_name_len, object_name); | |
c7054209 | 1697 | if (is_missing_file_error(errno)) { |
b2724c87 | 1698 | char *fullname = xstrfmt("%s%s", prefix, filename); |
009fee47 | 1699 | |
50ddb089 | 1700 | if (!get_tree_entry(r, tree_oid, fullname, &oid, &mode)) { |
009fee47 | 1701 | die("Path '%s' exists, but not '%s'.\n" |
b2981d06 | 1702 | "Did you mean '%.*s:%s' aka '%.*s:./%s'?", |
009fee47 MM |
1703 | fullname, |
1704 | filename, | |
b2981d06 | 1705 | object_name_len, object_name, |
e41d718c | 1706 | fullname, |
b2981d06 | 1707 | object_name_len, object_name, |
e41d718c | 1708 | filename); |
009fee47 | 1709 | } |
b2981d06 RS |
1710 | die("Path '%s' does not exist in '%.*s'", |
1711 | filename, object_name_len, object_name); | |
009fee47 MM |
1712 | } |
1713 | } | |
1714 | ||
1715 | /* Must be called only when :stage:filename doesn't exist. */ | |
0488481e | 1716 | static void diagnose_invalid_index_path(struct repository *r, |
3a7a698e | 1717 | int stage, |
009fee47 MM |
1718 | const char *prefix, |
1719 | const char *filename) | |
1720 | { | |
0488481e | 1721 | struct index_state *istate = r->index; |
9c5e6c80 | 1722 | const struct cache_entry *ce; |
009fee47 MM |
1723 | int pos; |
1724 | unsigned namelen = strlen(filename); | |
43bb66ae | 1725 | struct strbuf fullname = STRBUF_INIT; |
009fee47 MM |
1726 | |
1727 | if (!prefix) | |
1728 | prefix = ""; | |
1729 | ||
1730 | /* Wrong stage number? */ | |
3a7a698e | 1731 | pos = index_name_pos(istate, filename, namelen); |
009fee47 MM |
1732 | if (pos < 0) |
1733 | pos = -pos - 1; | |
3a7a698e NTND |
1734 | if (pos < istate->cache_nr) { |
1735 | ce = istate->cache[pos]; | |
77e8466f MH |
1736 | if (ce_namelen(ce) == namelen && |
1737 | !memcmp(ce->name, filename, namelen)) | |
1738 | die("Path '%s' is in the index, but not at stage %d.\n" | |
1739 | "Did you mean ':%d:%s'?", | |
1740 | filename, stage, | |
1741 | ce_stage(ce), filename); | |
1742 | } | |
009fee47 MM |
1743 | |
1744 | /* Confusion between relative and absolute filenames? */ | |
43bb66ae JK |
1745 | strbuf_addstr(&fullname, prefix); |
1746 | strbuf_addstr(&fullname, filename); | |
3a7a698e | 1747 | pos = index_name_pos(istate, fullname.buf, fullname.len); |
009fee47 MM |
1748 | if (pos < 0) |
1749 | pos = -pos - 1; | |
3a7a698e NTND |
1750 | if (pos < istate->cache_nr) { |
1751 | ce = istate->cache[pos]; | |
43bb66ae JK |
1752 | if (ce_namelen(ce) == fullname.len && |
1753 | !memcmp(ce->name, fullname.buf, fullname.len)) | |
77e8466f | 1754 | die("Path '%s' is in the index, but not '%s'.\n" |
e41d718c | 1755 | "Did you mean ':%d:%s' aka ':%d:./%s'?", |
43bb66ae JK |
1756 | fullname.buf, filename, |
1757 | ce_stage(ce), fullname.buf, | |
e41d718c | 1758 | ce_stage(ce), filename); |
77e8466f | 1759 | } |
009fee47 | 1760 | |
0488481e | 1761 | if (repo_file_exists(r, filename)) |
009fee47 | 1762 | die("Path '%s' exists on disk, but not in the index.", filename); |
c7054209 | 1763 | if (is_missing_file_error(errno)) |
009fee47 MM |
1764 | die("Path '%s' does not exist (neither on disk nor in the index).", |
1765 | filename); | |
1766 | ||
43bb66ae | 1767 | strbuf_release(&fullname); |
009fee47 MM |
1768 | } |
1769 | ||
1770 | ||
0381f7f3 | 1771 | static char *resolve_relative_path(struct repository *r, const char *rel) |
979f7929 | 1772 | { |
59556548 | 1773 | if (!starts_with(rel, "./") && !starts_with(rel, "../")) |
979f7929 NTND |
1774 | return NULL; |
1775 | ||
0381f7f3 | 1776 | if (r != the_repository || !is_inside_work_tree()) |
979f7929 NTND |
1777 | die("relative path syntax can't be used outside working tree."); |
1778 | ||
1779 | /* die() inside prefix_path() if resolved path is outside worktree */ | |
1780 | return prefix_path(startup_info->prefix, | |
1781 | startup_info->prefix ? strlen(startup_info->prefix) : 0, | |
1782 | rel); | |
1783 | } | |
1784 | ||
7589e636 | 1785 | static enum get_oid_result get_oid_with_context_1(struct repository *repo, |
3a7a698e | 1786 | const char *name, |
e82caf38 | 1787 | unsigned flags, |
1788 | const char *prefix, | |
1789 | struct object_id *oid, | |
1790 | struct object_context *oc) | |
a0cd87a5 MK |
1791 | { |
1792 | int ret, bracket_depth; | |
73b0e5af JH |
1793 | int namelen = strlen(name); |
1794 | const char *cp; | |
321c89bf | 1795 | int only_to_die = flags & GET_OID_ONLY_TO_DIE; |
5119602a | 1796 | |
7243ffdd | 1797 | if (only_to_die) |
321c89bf | 1798 | flags |= GET_OID_QUIETLY; |
7243ffdd | 1799 | |
573285e5 CP |
1800 | memset(oc, 0, sizeof(*oc)); |
1801 | oc->mode = S_IFINVALID; | |
d72cae12 | 1802 | strbuf_init(&oc->symlink_path, 0); |
2b1790f5 | 1803 | ret = get_oid_1(repo, name, namelen, oid, flags); |
73b0e5af JH |
1804 | if (!ret) |
1805 | return ret; | |
33bd598c JH |
1806 | /* |
1807 | * sha1:path --> object name of path in ent sha1 | |
979f7929 NTND |
1808 | * :path -> object name of absolute path in index |
1809 | * :./path -> object name of path relative to cwd in index | |
73b0e5af | 1810 | * :[0-3]:path -> object name of path in index at stage |
95ad6d2d | 1811 | * :/foo -> recent commit matching foo |
73b0e5af JH |
1812 | */ |
1813 | if (name[0] == ':') { | |
1814 | int stage = 0; | |
9c5e6c80 | 1815 | const struct cache_entry *ce; |
979f7929 | 1816 | char *new_path = NULL; |
73b0e5af | 1817 | int pos; |
2e83b66c | 1818 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
c931ba4e | 1819 | struct handle_one_ref_cb cb; |
84baa31b | 1820 | struct commit_list *list = NULL; |
2b2a5be3 | 1821 | |
c931ba4e NTND |
1822 | cb.repo = repo; |
1823 | cb.list = &list; | |
1824 | refs_for_each_ref(repo->refs, handle_one_ref, &cb); | |
1825 | refs_head_ref(repo->refs, handle_one_ref, &cb); | |
e8d1dfe6 | 1826 | commit_list_sort_by_date(&list); |
0bb41a19 | 1827 | return get_oid_oneline(repo, name + 2, oid, list); |
84baa31b | 1828 | } |
73b0e5af JH |
1829 | if (namelen < 3 || |
1830 | name[2] != ':' || | |
1831 | name[1] < '0' || '3' < name[1]) | |
1832 | cp = name + 1; | |
1833 | else { | |
1834 | stage = name[1] - '0'; | |
1835 | cp = name + 3; | |
5119602a | 1836 | } |
0381f7f3 | 1837 | new_path = resolve_relative_path(repo, cp); |
3d6e0f74 JH |
1838 | if (!new_path) { |
1839 | namelen = namelen - (cp - name); | |
1840 | } else { | |
1841 | cp = new_path; | |
1842 | namelen = strlen(cp); | |
1843 | } | |
573285e5 | 1844 | |
321c89bf | 1845 | if (flags & GET_OID_RECORD_PATH) |
dc944b65 | 1846 | oc->path = xstrdup(cp); |
573285e5 | 1847 | |
581d2fd9 | 1848 | if (!repo->index || !repo->index->cache) |
efe461b0 | 1849 | repo_read_index(repo); |
3a7a698e | 1850 | pos = index_name_pos(repo->index, cp, namelen); |
73b0e5af JH |
1851 | if (pos < 0) |
1852 | pos = -pos - 1; | |
3a7a698e NTND |
1853 | while (pos < repo->index->cache_nr) { |
1854 | ce = repo->index->cache[pos]; | |
73b0e5af JH |
1855 | if (ce_namelen(ce) != namelen || |
1856 | memcmp(ce->name, cp, namelen)) | |
1857 | break; | |
1858 | if (ce_stage(ce) == stage) { | |
e82caf38 | 1859 | oidcpy(oid, &ce->oid); |
90064710 | 1860 | oc->mode = ce->ce_mode; |
979f7929 | 1861 | free(new_path); |
73b0e5af JH |
1862 | return 0; |
1863 | } | |
e7cef45f | 1864 | pos++; |
73b0e5af | 1865 | } |
2e83b66c | 1866 | if (only_to_die && name[1] && name[1] != '/') |
0488481e | 1867 | diagnose_invalid_index_path(repo, stage, prefix, cp); |
979f7929 | 1868 | free(new_path); |
73b0e5af JH |
1869 | return -1; |
1870 | } | |
cce91a2c SP |
1871 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
1872 | if (*cp == '{') | |
1873 | bracket_depth++; | |
1874 | else if (bracket_depth && *cp == '}') | |
1875 | bracket_depth--; | |
1876 | else if (!bracket_depth && *cp == ':') | |
1877 | break; | |
1878 | } | |
1879 | if (*cp == ':') { | |
e82caf38 | 1880 | struct object_id tree_oid; |
b2981d06 | 1881 | int len = cp - name; |
8a10fea4 JK |
1882 | unsigned sub_flags = flags; |
1883 | ||
321c89bf | 1884 | sub_flags &= ~GET_OID_DISAMBIGUATORS; |
1885 | sub_flags |= GET_OID_TREEISH; | |
8a10fea4 | 1886 | |
2b1790f5 | 1887 | if (!get_oid_1(repo, name, len, &tree_oid, sub_flags)) { |
009fee47 | 1888 | const char *filename = cp+1; |
979f7929 NTND |
1889 | char *new_filename = NULL; |
1890 | ||
0381f7f3 | 1891 | new_filename = resolve_relative_path(repo, filename); |
979f7929 NTND |
1892 | if (new_filename) |
1893 | filename = new_filename; | |
321c89bf | 1894 | if (flags & GET_OID_FOLLOW_SYMLINKS) { |
0dd1f0c3 | 1895 | ret = get_tree_entry_follow_symlinks(repo, &tree_oid, |
3b683bcf | 1896 | filename, oid, &oc->symlink_path, |
c4ec9677 DT |
1897 | &oc->mode); |
1898 | } else { | |
50ddb089 | 1899 | ret = get_tree_entry(repo, &tree_oid, filename, oid, |
916bc35b | 1900 | &oc->mode); |
c4ec9677 | 1901 | if (ret && only_to_die) { |
50ddb089 | 1902 | diagnose_invalid_oid_path(repo, prefix, |
c4ec9677 | 1903 | filename, |
e82caf38 | 1904 | &tree_oid, |
c4ec9677 DT |
1905 | name, len); |
1906 | } | |
009fee47 | 1907 | } |
321c89bf | 1908 | if (flags & GET_OID_RECORD_PATH) |
dc944b65 | 1909 | oc->path = xstrdup(filename); |
573285e5 | 1910 | |
979f7929 | 1911 | free(new_filename); |
009fee47 MM |
1912 | return ret; |
1913 | } else { | |
2e83b66c | 1914 | if (only_to_die) |
b2981d06 | 1915 | die("Invalid object name '%.*s'.", len, name); |
009fee47 | 1916 | } |
5119602a LT |
1917 | } |
1918 | return ret; | |
9938af6a | 1919 | } |
f01cc14c | 1920 | |
8c135ea2 JH |
1921 | /* |
1922 | * Call this function when you know "name" given by the end user must | |
1923 | * name an object but it doesn't; the function _may_ die with a better | |
1924 | * diagnostic message than "no such object 'name'", e.g. "Path 'doc' does not | |
1925 | * exist in 'HEAD'" when given "HEAD:doc", or it may return in which case | |
1926 | * you have a chance to diagnose the error further. | |
1927 | */ | |
e270f42c NTND |
1928 | void maybe_die_on_misspelt_object_name(struct repository *r, |
1929 | const char *name, | |
1930 | const char *prefix) | |
f01cc14c JH |
1931 | { |
1932 | struct object_context oc; | |
e82caf38 | 1933 | struct object_id oid; |
e270f42c | 1934 | get_oid_with_context_1(r, name, GET_OID_ONLY_TO_DIE, |
3a7a698e | 1935 | prefix, &oid, &oc); |
8c135ea2 JH |
1936 | } |
1937 | ||
127b48f9 NTND |
1938 | enum get_oid_result get_oid_with_context(struct repository *repo, |
1939 | const char *str, | |
1940 | unsigned flags, | |
1941 | struct object_id *oid, | |
1942 | struct object_context *oc) | |
f01cc14c | 1943 | { |
321c89bf | 1944 | if (flags & GET_OID_FOLLOW_SYMLINKS && flags & GET_OID_ONLY_TO_DIE) |
033abf97 | 1945 | BUG("incompatible flags for get_sha1_with_context"); |
3a7a698e | 1946 | return get_oid_with_context_1(repo, str, flags, NULL, oid, oc); |
f01cc14c | 1947 | } |