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