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