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