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