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