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