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