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