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