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