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