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