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