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