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