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