]>
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" |
9938af6a | 9 | |
32574b68 NTND |
10 | static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *); |
11 | ||
9938af6a JH |
12 | static int find_short_object_filename(int len, const char *name, unsigned char *sha1) |
13 | { | |
99a19b43 | 14 | struct alternate_object_database *alt; |
9938af6a | 15 | char hex[40]; |
99a19b43 JH |
16 | int found = 0; |
17 | static struct alternate_object_database *fakeent; | |
18 | ||
19 | if (!fakeent) { | |
20 | const char *objdir = get_object_directory(); | |
21 | int objdir_len = strlen(objdir); | |
22 | int entlen = objdir_len + 43; | |
23 | fakeent = xmalloc(sizeof(*fakeent) + entlen); | |
24 | memcpy(fakeent->base, objdir, objdir_len); | |
25 | fakeent->name = fakeent->base + objdir_len + 1; | |
26 | fakeent->name[-1] = '/'; | |
27 | } | |
28 | fakeent->next = alt_odb_list; | |
9938af6a | 29 | |
9938af6a | 30 | sprintf(hex, "%.2s", name); |
99a19b43 | 31 | for (alt = fakeent; alt && found < 2; alt = alt->next) { |
9938af6a | 32 | struct dirent *de; |
99a19b43 JH |
33 | DIR *dir; |
34 | sprintf(alt->name, "%.2s/", name); | |
35 | dir = opendir(alt->base); | |
36 | if (!dir) | |
37 | continue; | |
9938af6a JH |
38 | while ((de = readdir(dir)) != NULL) { |
39 | if (strlen(de->d_name) != 38) | |
40 | continue; | |
99a19b43 | 41 | if (memcmp(de->d_name, name + 2, len - 2)) |
9938af6a | 42 | continue; |
99a19b43 JH |
43 | if (!found) { |
44 | memcpy(hex + 2, de->d_name, 38); | |
45 | found++; | |
46 | } | |
47 | else if (memcmp(hex + 2, de->d_name, 38)) { | |
48 | found = 2; | |
9938af6a | 49 | break; |
99a19b43 | 50 | } |
9938af6a JH |
51 | } |
52 | closedir(dir); | |
53 | } | |
54 | if (found == 1) | |
55 | return get_sha1_hex(hex, sha1) == 0; | |
99a19b43 | 56 | return found; |
9938af6a JH |
57 | } |
58 | ||
59 | static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b) | |
60 | { | |
61 | do { | |
62 | if (*a != *b) | |
63 | return 0; | |
64 | a++; | |
65 | b++; | |
66 | len -= 2; | |
67 | } while (len > 1); | |
68 | if (len) | |
69 | if ((*a ^ *b) & 0xf0) | |
70 | return 0; | |
71 | return 1; | |
72 | } | |
73 | ||
74 | static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1) | |
75 | { | |
76 | struct packed_git *p; | |
d72308e0 | 77 | const unsigned char *found_sha1 = NULL; |
99a19b43 | 78 | int found = 0; |
9938af6a JH |
79 | |
80 | prepare_packed_git(); | |
99a19b43 | 81 | for (p = packed_git; p && found < 2; p = p->next) { |
1055880e JB |
82 | uint32_t num, last; |
83 | uint32_t first = 0; | |
84 | open_pack_index(p); | |
85 | num = p->num_objects; | |
86 | last = num; | |
9938af6a | 87 | while (first < last) { |
326bf396 | 88 | uint32_t mid = (first + last) / 2; |
d72308e0 | 89 | const unsigned char *now; |
9938af6a JH |
90 | int cmp; |
91 | ||
d72308e0 | 92 | now = nth_packed_object_sha1(p, mid); |
a89fccd2 | 93 | cmp = hashcmp(match, now); |
9938af6a JH |
94 | if (!cmp) { |
95 | first = mid; | |
96 | break; | |
97 | } | |
98 | if (cmp > 0) { | |
99 | first = mid+1; | |
100 | continue; | |
101 | } | |
102 | last = mid; | |
103 | } | |
104 | if (first < num) { | |
d72308e0 NP |
105 | const unsigned char *now, *next; |
106 | now = nth_packed_object_sha1(p, first); | |
9938af6a | 107 | if (match_sha(len, match, now)) { |
d72308e0 NP |
108 | next = nth_packed_object_sha1(p, first+1); |
109 | if (!next|| !match_sha(len, match, next)) { | |
0bc45890 JH |
110 | /* unique within this pack */ |
111 | if (!found) { | |
d72308e0 | 112 | found_sha1 = now; |
0bc45890 JH |
113 | found++; |
114 | } | |
a89fccd2 | 115 | else if (hashcmp(found_sha1, now)) { |
0bc45890 JH |
116 | found = 2; |
117 | break; | |
118 | } | |
99a19b43 | 119 | } |
0bc45890 JH |
120 | else { |
121 | /* not even unique within this pack */ | |
99a19b43 JH |
122 | found = 2; |
123 | break; | |
9938af6a JH |
124 | } |
125 | } | |
126 | } | |
127 | } | |
99a19b43 | 128 | if (found == 1) |
e702496e | 129 | hashcpy(sha1, found_sha1); |
99a19b43 JH |
130 | return found; |
131 | } | |
132 | ||
013f276e JH |
133 | #define SHORT_NAME_NOT_FOUND (-1) |
134 | #define SHORT_NAME_AMBIGUOUS (-2) | |
135 | ||
99a19b43 JH |
136 | static int find_unique_short_object(int len, char *canonical, |
137 | unsigned char *res, unsigned char *sha1) | |
138 | { | |
139 | int has_unpacked, has_packed; | |
140 | unsigned char unpacked_sha1[20], packed_sha1[20]; | |
141 | ||
693d2bc6 | 142 | prepare_alt_odb(); |
99a19b43 JH |
143 | has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1); |
144 | has_packed = find_short_packed_object(len, res, packed_sha1); | |
145 | if (!has_unpacked && !has_packed) | |
013f276e | 146 | return SHORT_NAME_NOT_FOUND; |
99a19b43 | 147 | if (1 < has_unpacked || 1 < has_packed) |
013f276e | 148 | return SHORT_NAME_AMBIGUOUS; |
99a19b43 | 149 | if (has_unpacked != has_packed) { |
e702496e | 150 | hashcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1)); |
99a19b43 JH |
151 | return 0; |
152 | } | |
153 | /* Both have unique ones -- do they match? */ | |
a89fccd2 | 154 | if (hashcmp(packed_sha1, unpacked_sha1)) |
e974c9ab | 155 | return SHORT_NAME_AMBIGUOUS; |
e702496e | 156 | hashcpy(sha1, packed_sha1); |
9938af6a JH |
157 | return 0; |
158 | } | |
159 | ||
013f276e JH |
160 | static int get_short_sha1(const char *name, int len, unsigned char *sha1, |
161 | int quietly) | |
9938af6a | 162 | { |
013f276e | 163 | int i, status; |
9938af6a JH |
164 | char canonical[40]; |
165 | unsigned char res[20]; | |
166 | ||
8a83157e | 167 | if (len < MINIMUM_ABBREV || len > 40) |
af61c6e0 | 168 | return -1; |
a8e0d16d | 169 | hashclr(res); |
9938af6a | 170 | memset(canonical, 'x', 40); |
af61c6e0 | 171 | for (i = 0; i < len ;i++) { |
9938af6a JH |
172 | unsigned char c = name[i]; |
173 | unsigned char val; | |
9938af6a JH |
174 | if (c >= '0' && c <= '9') |
175 | val = c - '0'; | |
176 | else if (c >= 'a' && c <= 'f') | |
177 | val = c - 'a' + 10; | |
178 | else if (c >= 'A' && c <='F') { | |
179 | val = c - 'A' + 10; | |
180 | c -= 'A' - 'a'; | |
181 | } | |
182 | else | |
183 | return -1; | |
184 | canonical[i] = c; | |
185 | if (!(i & 1)) | |
186 | val <<= 4; | |
187 | res[i >> 1] |= val; | |
188 | } | |
99a19b43 | 189 | |
013f276e JH |
190 | status = find_unique_short_object(i, canonical, res, sha1); |
191 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) | |
192 | return error("short SHA1 %.*s is ambiguous.", len, canonical); | |
193 | return status; | |
194 | } | |
195 | ||
196 | const char *find_unique_abbrev(const unsigned char *sha1, int len) | |
197 | { | |
b66fde9a | 198 | int status, exists; |
013f276e | 199 | static char hex[41]; |
47dd0d59 | 200 | |
b66fde9a | 201 | exists = has_sha1_file(sha1); |
013f276e | 202 | memcpy(hex, sha1_to_hex(sha1), 40); |
02c5cba2 | 203 | if (len == 40 || !len) |
47dd0d59 | 204 | return hex; |
013f276e JH |
205 | while (len < 40) { |
206 | unsigned char sha1_ret[20]; | |
207 | status = get_short_sha1(hex, len, sha1_ret, 1); | |
b66fde9a JH |
208 | if (exists |
209 | ? !status | |
210 | : status == SHORT_NAME_NOT_FOUND) { | |
ea2c69ed | 211 | hex[len] = 0; |
013f276e JH |
212 | return hex; |
213 | } | |
013f276e JH |
214 | len++; |
215 | } | |
b66fde9a | 216 | return hex; |
9938af6a JH |
217 | } |
218 | ||
6677c466 | 219 | static int ambiguous_path(const char *path, int len) |
af13cdf2 LT |
220 | { |
221 | int slash = 1; | |
6677c466 | 222 | int cnt; |
af13cdf2 | 223 | |
6677c466 | 224 | for (cnt = 0; cnt < len; cnt++) { |
af13cdf2 LT |
225 | switch (*path++) { |
226 | case '\0': | |
227 | break; | |
228 | case '/': | |
229 | if (slash) | |
230 | break; | |
231 | slash = 1; | |
232 | continue; | |
233 | case '.': | |
234 | continue; | |
235 | default: | |
236 | slash = 0; | |
237 | continue; | |
238 | } | |
c054d64e | 239 | break; |
af13cdf2 | 240 | } |
6677c466 | 241 | return slash; |
af13cdf2 LT |
242 | } |
243 | ||
ae0ba8e2 JH |
244 | static inline int upstream_mark(const char *string, int len) |
245 | { | |
246 | const char *suffix[] = { "@{upstream}", "@{u}" }; | |
247 | int i; | |
248 | ||
249 | for (i = 0; i < ARRAY_SIZE(suffix); i++) { | |
250 | int suffix_len = strlen(suffix[i]); | |
251 | if (suffix_len <= len | |
252 | && !memcmp(string, suffix[i], suffix_len)) | |
253 | return suffix_len; | |
254 | } | |
255 | return 0; | |
256 | } | |
257 | ||
d18ba221 TR |
258 | static int get_sha1_1(const char *name, int len, unsigned char *sha1); |
259 | ||
e86eb666 JH |
260 | static int get_sha1_basic(const char *str, int len, unsigned char *sha1) |
261 | { | |
eedce784 | 262 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
ed378ec7 | 263 | char *real_ref = NULL; |
ab2a1a32 JH |
264 | int refs_found = 0; |
265 | int at, reflog_len; | |
9938af6a | 266 | |
3c3852e3 | 267 | if (len == 40 && !get_sha1_hex(str, sha1)) |
9938af6a JH |
268 | return 0; |
269 | ||
d18ba221 | 270 | /* basic@{time or number or -number} format to query ref-log */ |
694500ed | 271 | reflog_len = at = 0; |
f265458f | 272 | if (len && str[len-1] == '}') { |
aa9c55b6 | 273 | for (at = len-2; at >= 0; at--) { |
ab2a1a32 | 274 | if (str[at] == '@' && str[at+1] == '{') { |
ae0ba8e2 | 275 | if (!upstream_mark(str + at, len - at)) { |
28fb8438 JS |
276 | reflog_len = (len-1) - (at+2); |
277 | len = at; | |
278 | } | |
ab2a1a32 JH |
279 | break; |
280 | } | |
d556fae2 SP |
281 | } |
282 | } | |
283 | ||
af13cdf2 | 284 | /* Accept only unambiguous ref paths. */ |
11cf8801 | 285 | if (len && ambiguous_path(str, len)) |
af13cdf2 LT |
286 | return -1; |
287 | ||
11cf8801 | 288 | if (!len && reflog_len) { |
d18ba221 TR |
289 | struct strbuf buf = STRBUF_INIT; |
290 | int ret; | |
291 | /* try the @{-N} syntax for n-th checkout */ | |
431b1969 | 292 | ret = interpret_branch_name(str+at, &buf); |
d18ba221 TR |
293 | if (ret > 0) { |
294 | /* substitute this branch name and restart */ | |
295 | return get_sha1_1(buf.buf, buf.len, sha1); | |
296 | } else if (ret == 0) { | |
297 | return -1; | |
298 | } | |
11cf8801 NP |
299 | /* allow "@{...}" to mean the current branch reflog */ |
300 | refs_found = dwim_ref("HEAD", 4, sha1, &real_ref); | |
f2eba66d NP |
301 | } else if (reflog_len) |
302 | refs_found = dwim_log(str, len, sha1, &real_ref); | |
303 | else | |
11cf8801 | 304 | refs_found = dwim_ref(str, len, sha1, &real_ref); |
d556fae2 SP |
305 | |
306 | if (!refs_found) | |
307 | return -1; | |
308 | ||
309 | if (warn_ambiguous_refs && refs_found > 1) | |
eedce784 | 310 | warning(warn_msg, len, str); |
d556fae2 | 311 | |
ab2a1a32 | 312 | if (reflog_len) { |
ab2a1a32 JH |
313 | int nth, i; |
314 | unsigned long at_time; | |
16d7cc90 JH |
315 | unsigned long co_time; |
316 | int co_tz, co_cnt; | |
317 | ||
12a258c0 JK |
318 | /* a @{-N} placed anywhere except the start is an error */ |
319 | if (str[at+2] == '-') | |
320 | return -1; | |
321 | ||
fe558516 | 322 | /* Is it asking for N-th entry, or approxidate? */ |
ab2a1a32 JH |
323 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
324 | char ch = str[at+2+i]; | |
325 | if ('0' <= ch && ch <= '9') | |
326 | nth = nth * 10 + ch - '0'; | |
327 | else | |
328 | nth = -1; | |
329 | } | |
ea360dd0 SP |
330 | if (100000000 <= nth) { |
331 | at_time = nth; | |
332 | nth = -1; | |
333 | } else if (0 <= nth) | |
ab2a1a32 | 334 | at_time = 0; |
861f00e3 | 335 | else { |
93cfa7c7 | 336 | int errors = 0; |
861f00e3 | 337 | char *tmp = xstrndup(str + at + 2, reflog_len); |
93cfa7c7 | 338 | at_time = approxidate_careful(tmp, &errors); |
861f00e3 | 339 | free(tmp); |
a5e10acb JH |
340 | if (errors) |
341 | return -1; | |
861f00e3 | 342 | } |
16d7cc90 JH |
343 | if (read_ref_at(real_ref, at_time, nth, sha1, NULL, |
344 | &co_time, &co_tz, &co_cnt)) { | |
345 | if (at_time) | |
eedce784 JS |
346 | warning("Log for '%.*s' only goes " |
347 | "back to %s.", len, str, | |
73013afd | 348 | show_date(co_time, co_tz, DATE_RFC2822)); |
e6eedc31 JS |
349 | else { |
350 | free(real_ref); | |
351 | die("Log for '%.*s' only has %d entries.", | |
352 | len, str, co_cnt); | |
353 | } | |
16d7cc90 | 354 | } |
d556fae2 SP |
355 | } |
356 | ||
ed378ec7 | 357 | free(real_ref); |
d556fae2 | 358 | return 0; |
9938af6a JH |
359 | } |
360 | ||
9938af6a JH |
361 | static int get_parent(const char *name, int len, |
362 | unsigned char *result, int idx) | |
363 | { | |
364 | unsigned char sha1[20]; | |
365 | int ret = get_sha1_1(name, len, sha1); | |
366 | struct commit *commit; | |
367 | struct commit_list *p; | |
368 | ||
369 | if (ret) | |
370 | return ret; | |
371 | commit = lookup_commit_reference(sha1); | |
372 | if (!commit) | |
373 | return -1; | |
374 | if (parse_commit(commit)) | |
375 | return -1; | |
376 | if (!idx) { | |
e702496e | 377 | hashcpy(result, commit->object.sha1); |
9938af6a JH |
378 | return 0; |
379 | } | |
380 | p = commit->parents; | |
381 | while (p) { | |
382 | if (!--idx) { | |
e702496e | 383 | hashcpy(result, p->item->object.sha1); |
9938af6a JH |
384 | return 0; |
385 | } | |
386 | p = p->next; | |
387 | } | |
388 | return -1; | |
389 | } | |
390 | ||
4f7599ac JH |
391 | static int get_nth_ancestor(const char *name, int len, |
392 | unsigned char *result, int generation) | |
393 | { | |
394 | unsigned char sha1[20]; | |
621ff675 LT |
395 | struct commit *commit; |
396 | int ret; | |
397 | ||
398 | ret = get_sha1_1(name, len, sha1); | |
4f7599ac JH |
399 | if (ret) |
400 | return ret; | |
621ff675 LT |
401 | commit = lookup_commit_reference(sha1); |
402 | if (!commit) | |
403 | return -1; | |
4f7599ac JH |
404 | |
405 | while (generation--) { | |
621ff675 | 406 | if (parse_commit(commit) || !commit->parents) |
4f7599ac | 407 | return -1; |
621ff675 | 408 | commit = commit->parents->item; |
4f7599ac | 409 | } |
621ff675 | 410 | hashcpy(result, commit->object.sha1); |
4f7599ac JH |
411 | return 0; |
412 | } | |
413 | ||
81776315 JH |
414 | struct object *peel_to_type(const char *name, int namelen, |
415 | struct object *o, enum object_type expected_type) | |
416 | { | |
417 | if (name && !namelen) | |
418 | namelen = strlen(name); | |
81776315 JH |
419 | while (1) { |
420 | if (!o || (!o->parsed && !parse_object(o->sha1))) | |
421 | return NULL; | |
422 | if (o->type == expected_type) | |
423 | return o; | |
424 | if (o->type == OBJ_TAG) | |
425 | o = ((struct tag*) o)->tagged; | |
426 | else if (o->type == OBJ_COMMIT) | |
427 | o = &(((struct commit *) o)->tree->object); | |
428 | else { | |
429 | if (name) | |
430 | error("%.*s: expected %s type, but the object " | |
431 | "dereferences to %s type", | |
432 | namelen, name, typename(expected_type), | |
433 | typename(o->type)); | |
434 | return NULL; | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
5385f52d JH |
439 | static int peel_onion(const char *name, int len, unsigned char *sha1) |
440 | { | |
441 | unsigned char outer[20]; | |
442 | const char *sp; | |
885a86ab | 443 | unsigned int expected_type = 0; |
5385f52d JH |
444 | struct object *o; |
445 | ||
446 | /* | |
447 | * "ref^{type}" dereferences ref repeatedly until you cannot | |
448 | * dereference anymore, or you get an object of given type, | |
449 | * whichever comes first. "ref^{}" means just dereference | |
450 | * tags until you get a non-tag. "ref^0" is a shorthand for | |
451 | * "ref^{commit}". "commit^{tree}" could be used to find the | |
452 | * top-level tree of the given commit. | |
453 | */ | |
454 | if (len < 4 || name[len-1] != '}') | |
455 | return -1; | |
456 | ||
457 | for (sp = name + len - 1; name <= sp; sp--) { | |
458 | int ch = *sp; | |
459 | if (ch == '{' && name < sp && sp[-1] == '^') | |
460 | break; | |
461 | } | |
462 | if (sp <= name) | |
463 | return -1; | |
464 | ||
465 | sp++; /* beginning of type name, or closing brace for empty */ | |
466 | if (!strncmp(commit_type, sp, 6) && sp[6] == '}') | |
1974632c | 467 | expected_type = OBJ_COMMIT; |
5385f52d | 468 | else if (!strncmp(tree_type, sp, 4) && sp[4] == '}') |
1974632c | 469 | expected_type = OBJ_TREE; |
5385f52d | 470 | else if (!strncmp(blob_type, sp, 4) && sp[4] == '}') |
1974632c | 471 | expected_type = OBJ_BLOB; |
5385f52d | 472 | else if (sp[0] == '}') |
1974632c | 473 | expected_type = OBJ_NONE; |
32574b68 NTND |
474 | else if (sp[0] == '/') |
475 | expected_type = OBJ_COMMIT; | |
5385f52d JH |
476 | else |
477 | return -1; | |
478 | ||
479 | if (get_sha1_1(name, sp - name - 2, outer)) | |
480 | return -1; | |
481 | ||
482 | o = parse_object(outer); | |
483 | if (!o) | |
484 | return -1; | |
885a86ab | 485 | if (!expected_type) { |
9534f40b | 486 | o = deref_tag(o, name, sp - name - 2); |
6e1c6c10 JH |
487 | if (!o || (!o->parsed && !parse_object(o->sha1))) |
488 | return -1; | |
e702496e | 489 | hashcpy(sha1, o->sha1); |
32574b68 | 490 | return 0; |
5385f52d | 491 | } |
32574b68 NTND |
492 | |
493 | /* | |
494 | * At this point, the syntax look correct, so | |
495 | * if we do not get the needed object, we should | |
496 | * barf. | |
497 | */ | |
498 | o = peel_to_type(name, len, o, expected_type); | |
499 | if (!o) | |
81776315 | 500 | return -1; |
32574b68 NTND |
501 | |
502 | hashcpy(sha1, o->sha1); | |
503 | if (sp[0] == '/') { | |
504 | /* "$commit^{/foo}" */ | |
505 | char *prefix; | |
506 | int ret; | |
507 | struct commit_list *list = NULL; | |
508 | ||
81776315 | 509 | /* |
4322842a NTND |
510 | * $commit^{/}. Some regex implementation may reject. |
511 | * We don't need regex anyway. '' pattern always matches. | |
5385f52d | 512 | */ |
4322842a | 513 | if (sp[1] == '}') |
81776315 | 514 | return 0; |
4322842a | 515 | |
32574b68 NTND |
516 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
517 | commit_list_insert((struct commit *)o, &list); | |
518 | ret = get_sha1_oneline(prefix, sha1, list); | |
519 | free(prefix); | |
520 | return ret; | |
5385f52d JH |
521 | } |
522 | return 0; | |
523 | } | |
524 | ||
7dd45e15 JH |
525 | static int get_describe_name(const char *name, int len, unsigned char *sha1) |
526 | { | |
527 | const char *cp; | |
528 | ||
529 | for (cp = name + len - 1; name + 2 <= cp; cp--) { | |
530 | char ch = *cp; | |
531 | if (hexval(ch) & ~0377) { | |
532 | /* We must be looking at g in "SOMETHING-g" | |
533 | * for it to be describe output. | |
534 | */ | |
535 | if (ch == 'g' && cp[-1] == '-') { | |
536 | cp++; | |
537 | len -= cp - name; | |
538 | return get_short_sha1(cp, len, sha1, 1); | |
539 | } | |
540 | } | |
541 | } | |
542 | return -1; | |
543 | } | |
544 | ||
9938af6a JH |
545 | static int get_sha1_1(const char *name, int len, unsigned char *sha1) |
546 | { | |
0601dbe1 | 547 | int ret, has_suffix; |
4f7599ac | 548 | const char *cp; |
9938af6a | 549 | |
621ff675 LT |
550 | /* |
551 | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". | |
4f7599ac | 552 | */ |
0601dbe1 | 553 | has_suffix = 0; |
4f7599ac JH |
554 | for (cp = name + len - 1; name <= cp; cp--) { |
555 | int ch = *cp; | |
556 | if ('0' <= ch && ch <= '9') | |
557 | continue; | |
0601dbe1 JH |
558 | if (ch == '~' || ch == '^') |
559 | has_suffix = ch; | |
4f7599ac JH |
560 | break; |
561 | } | |
0601dbe1 JH |
562 | |
563 | if (has_suffix) { | |
564 | int num = 0; | |
4f7599ac JH |
565 | int len1 = cp - name; |
566 | cp++; | |
567 | while (cp < name + len) | |
0601dbe1 | 568 | num = num * 10 + *cp++ - '0'; |
621ff675 LT |
569 | if (!num && len1 == len - 1) |
570 | num = 1; | |
571 | if (has_suffix == '^') | |
0601dbe1 | 572 | return get_parent(name, len1, sha1, num); |
0601dbe1 JH |
573 | /* else if (has_suffix == '~') -- goes without saying */ |
574 | return get_nth_ancestor(name, len1, sha1, num); | |
4f7599ac JH |
575 | } |
576 | ||
5385f52d JH |
577 | ret = peel_onion(name, len, sha1); |
578 | if (!ret) | |
579 | return 0; | |
580 | ||
9938af6a JH |
581 | ret = get_sha1_basic(name, len, sha1); |
582 | if (!ret) | |
583 | return 0; | |
7dd45e15 JH |
584 | |
585 | /* It could be describe output that is "SOMETHING-gXXXX" */ | |
586 | ret = get_describe_name(name, len, sha1); | |
587 | if (!ret) | |
588 | return 0; | |
589 | ||
013f276e | 590 | return get_short_sha1(name, len, sha1, 0); |
9938af6a JH |
591 | } |
592 | ||
f7bff003 JH |
593 | /* |
594 | * This interprets names like ':/Initial revision of "git"' by searching | |
595 | * through history and returning the first commit whose message starts | |
3d045897 | 596 | * the given regular expression. |
f7bff003 JH |
597 | * |
598 | * For future extension, ':/!' is reserved. If you want to match a message | |
599 | * beginning with a '!', you have to repeat the exclamation mark. | |
600 | */ | |
601 | #define ONELINE_SEEN (1u<<20) | |
602 | ||
28a4d940 JS |
603 | static int handle_one_ref(const char *path, |
604 | const unsigned char *sha1, int flag, void *cb_data) | |
605 | { | |
606 | struct commit_list **list = cb_data; | |
607 | struct object *object = parse_object(sha1); | |
608 | if (!object) | |
609 | return 0; | |
affeef12 | 610 | if (object->type == OBJ_TAG) { |
28a4d940 | 611 | object = deref_tag(object, path, strlen(path)); |
affeef12 MK |
612 | if (!object) |
613 | return 0; | |
614 | } | |
28a4d940 JS |
615 | if (object->type != OBJ_COMMIT) |
616 | return 0; | |
47e44ed1 | 617 | commit_list_insert_by_date((struct commit *)object, list); |
28a4d940 JS |
618 | return 0; |
619 | } | |
620 | ||
84baa31b NTND |
621 | static int get_sha1_oneline(const char *prefix, unsigned char *sha1, |
622 | struct commit_list *list) | |
28a4d940 | 623 | { |
84baa31b | 624 | struct commit_list *backup = NULL, *l; |
28042dbc | 625 | int found = 0; |
57895105 | 626 | regex_t regex; |
28a4d940 JS |
627 | |
628 | if (prefix[0] == '!') { | |
629 | if (prefix[1] != '!') | |
630 | die ("Invalid search pattern: %s", prefix); | |
631 | prefix++; | |
632 | } | |
57895105 LT |
633 | |
634 | if (regcomp(®ex, prefix, REG_EXTENDED)) | |
635 | die("Invalid search pattern: %s", prefix); | |
636 | ||
84baa31b NTND |
637 | for (l = list; l; l = l->next) { |
638 | l->item->object.flags |= ONELINE_SEEN; | |
28a4d940 | 639 | commit_list_insert(l->item, &backup); |
84baa31b | 640 | } |
ed8ad7e2 | 641 | while (list) { |
28042dbc | 642 | char *p, *to_free = NULL; |
1358e7d6 | 643 | struct commit *commit; |
364d3e65 JS |
644 | enum object_type type; |
645 | unsigned long size; | |
28042dbc | 646 | int matches; |
ed8ad7e2 JM |
647 | |
648 | commit = pop_most_recent_commit(&list, ONELINE_SEEN); | |
283cdbcf MK |
649 | if (!parse_object(commit->object.sha1)) |
650 | continue; | |
364d3e65 JS |
651 | if (commit->buffer) |
652 | p = commit->buffer; | |
653 | else { | |
654 | p = read_sha1_file(commit->object.sha1, &type, &size); | |
655 | if (!p) | |
656 | continue; | |
28042dbc | 657 | to_free = p; |
364d3e65 | 658 | } |
28042dbc JH |
659 | |
660 | p = strstr(p, "\n\n"); | |
661 | matches = p && !regexec(®ex, p + 2, 0, NULL, 0); | |
662 | free(to_free); | |
663 | ||
664 | if (matches) { | |
28a4d940 | 665 | hashcpy(sha1, commit->object.sha1); |
28042dbc | 666 | found = 1; |
28a4d940 JS |
667 | break; |
668 | } | |
669 | } | |
57895105 | 670 | regfree(®ex); |
28a4d940 JS |
671 | free_commit_list(list); |
672 | for (l = backup; l; l = l->next) | |
673 | clear_commit_marks(l->item, ONELINE_SEEN); | |
28042dbc JH |
674 | free_commit_list(backup); |
675 | return found ? 0 : -1; | |
28a4d940 JS |
676 | } |
677 | ||
ae5a6c36 | 678 | struct grab_nth_branch_switch_cbdata { |
c2883e62 | 679 | long cnt, alloc; |
ae5a6c36 JH |
680 | struct strbuf *buf; |
681 | }; | |
682 | ||
683 | static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1, | |
684 | const char *email, unsigned long timestamp, int tz, | |
685 | const char *message, void *cb_data) | |
686 | { | |
687 | struct grab_nth_branch_switch_cbdata *cb = cb_data; | |
a884d0cb TR |
688 | const char *match = NULL, *target = NULL; |
689 | size_t len; | |
c2883e62 | 690 | int nth; |
a884d0cb TR |
691 | |
692 | if (!prefixcmp(message, "checkout: moving from ")) { | |
693 | match = message + strlen("checkout: moving from "); | |
d7c03c1f | 694 | target = strstr(match, " to "); |
ae5a6c36 JH |
695 | } |
696 | ||
c829774c | 697 | if (!match || !target) |
ae5a6c36 JH |
698 | return 0; |
699 | ||
d7c03c1f | 700 | len = target - match; |
c2883e62 JH |
701 | nth = cb->cnt++ % cb->alloc; |
702 | strbuf_reset(&cb->buf[nth]); | |
703 | strbuf_add(&cb->buf[nth], match, len); | |
ae5a6c36 JH |
704 | return 0; |
705 | } | |
706 | ||
707 | /* | |
ae0ba8e2 JH |
708 | * Parse @{-N} syntax, return the number of characters parsed |
709 | * if successful; otherwise signal an error with negative value. | |
ae5a6c36 | 710 | */ |
ae0ba8e2 | 711 | static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf) |
ae5a6c36 | 712 | { |
c2883e62 | 713 | long nth; |
39765e59 | 714 | int i, retval; |
ae5a6c36 | 715 | struct grab_nth_branch_switch_cbdata cb; |
a884d0cb TR |
716 | const char *brace; |
717 | char *num_end; | |
ae5a6c36 JH |
718 | |
719 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') | |
720 | return -1; | |
a884d0cb TR |
721 | brace = strchr(name, '}'); |
722 | if (!brace) | |
723 | return -1; | |
724 | nth = strtol(name+3, &num_end, 10); | |
725 | if (num_end != brace) | |
ae5a6c36 | 726 | return -1; |
c2883e62 JH |
727 | if (nth <= 0) |
728 | return -1; | |
729 | cb.alloc = nth; | |
730 | cb.buf = xmalloc(nth * sizeof(struct strbuf)); | |
731 | for (i = 0; i < nth; i++) | |
732 | strbuf_init(&cb.buf[i], 20); | |
733 | cb.cnt = 0; | |
39765e59 | 734 | retval = 0; |
101d15e0 JH |
735 | for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb); |
736 | if (cb.cnt < nth) { | |
737 | cb.cnt = 0; | |
101d15e0 JH |
738 | for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb); |
739 | } | |
c2883e62 | 740 | if (cb.cnt < nth) |
39765e59 | 741 | goto release_return; |
c2883e62 JH |
742 | i = cb.cnt % nth; |
743 | strbuf_reset(buf); | |
744 | strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len); | |
39765e59 JH |
745 | retval = brace-name+1; |
746 | ||
747 | release_return: | |
c2883e62 JH |
748 | for (i = 0; i < nth; i++) |
749 | strbuf_release(&cb.buf[i]); | |
750 | free(cb.buf); | |
a884d0cb | 751 | |
39765e59 | 752 | return retval; |
ae5a6c36 JH |
753 | } |
754 | ||
619a644d JH |
755 | int get_sha1_mb(const char *name, unsigned char *sha1) |
756 | { | |
757 | struct commit *one, *two; | |
758 | struct commit_list *mbs; | |
759 | unsigned char sha1_tmp[20]; | |
760 | const char *dots; | |
761 | int st; | |
762 | ||
763 | dots = strstr(name, "..."); | |
764 | if (!dots) | |
765 | return get_sha1(name, sha1); | |
766 | if (dots == name) | |
767 | st = get_sha1("HEAD", sha1_tmp); | |
768 | else { | |
769 | struct strbuf sb; | |
770 | strbuf_init(&sb, dots - name); | |
771 | strbuf_add(&sb, name, dots - name); | |
772 | st = get_sha1(sb.buf, sha1_tmp); | |
773 | strbuf_release(&sb); | |
774 | } | |
775 | if (st) | |
776 | return st; | |
777 | one = lookup_commit_reference_gently(sha1_tmp, 0); | |
778 | if (!one) | |
779 | return -1; | |
780 | ||
781 | if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp)) | |
782 | return -1; | |
783 | two = lookup_commit_reference_gently(sha1_tmp, 0); | |
784 | if (!two) | |
785 | return -1; | |
786 | mbs = get_merge_bases(one, two, 1); | |
787 | if (!mbs || mbs->next) | |
788 | st = -1; | |
789 | else { | |
790 | st = 0; | |
791 | hashcpy(sha1, mbs->item->object.sha1); | |
792 | } | |
793 | free_commit_list(mbs); | |
794 | return st; | |
795 | } | |
796 | ||
ae0ba8e2 JH |
797 | /* |
798 | * This reads short-hand syntax that not only evaluates to a commit | |
799 | * object name, but also can act as if the end user spelled the name | |
800 | * of the branch from the command line. | |
801 | * | |
802 | * - "@{-N}" finds the name of the Nth previous branch we were on, and | |
803 | * places the name of the branch in the given buf and returns the | |
804 | * number of characters parsed if successful. | |
805 | * | |
806 | * - "<branch>@{upstream}" finds the name of the other ref that | |
807 | * <branch> is configured to merge with (missing <branch> defaults | |
808 | * to the current branch), and places the name of the branch in the | |
809 | * given buf and returns the number of characters parsed if | |
810 | * successful. | |
811 | * | |
812 | * If the input is not of the accepted format, it returns a negative | |
813 | * number to signal an error. | |
814 | * | |
815 | * If the input was ok but there are not N branch switches in the | |
816 | * reflog, it returns 0. | |
817 | */ | |
818 | int interpret_branch_name(const char *name, struct strbuf *buf) | |
819 | { | |
820 | char *cp; | |
821 | struct branch *upstream; | |
822 | int namelen = strlen(name); | |
823 | int len = interpret_nth_prior_checkout(name, buf); | |
824 | int tmp_len; | |
825 | ||
826 | if (!len) | |
827 | return len; /* syntax Ok, not enough switches */ | |
d46a8301 JK |
828 | if (0 < len && len == namelen) |
829 | return len; /* consumed all */ | |
830 | else if (0 < len) { | |
831 | /* we have extra data, which might need further processing */ | |
832 | struct strbuf tmp = STRBUF_INIT; | |
833 | int used = buf->len; | |
834 | int ret; | |
835 | ||
836 | strbuf_add(buf, name + len, namelen - len); | |
837 | ret = interpret_branch_name(buf->buf, &tmp); | |
838 | /* that data was not interpreted, remove our cruft */ | |
839 | if (ret < 0) { | |
840 | strbuf_setlen(buf, used); | |
841 | return len; | |
842 | } | |
843 | strbuf_reset(buf); | |
844 | strbuf_addbuf(buf, &tmp); | |
845 | strbuf_release(&tmp); | |
846 | /* tweak for size of {-N} versus expanded ref name */ | |
847 | return ret - used + len; | |
848 | } | |
849 | ||
ae0ba8e2 JH |
850 | cp = strchr(name, '@'); |
851 | if (!cp) | |
852 | return -1; | |
853 | tmp_len = upstream_mark(cp, namelen - (cp - name)); | |
854 | if (!tmp_len) | |
855 | return -1; | |
856 | len = cp + tmp_len - name; | |
857 | cp = xstrndup(name, cp - name); | |
858 | upstream = branch_get(*cp ? cp : NULL); | |
9884e67f ZJS |
859 | /* |
860 | * Upstream can be NULL only if cp refers to HEAD and HEAD | |
861 | * points to something different than a branch. | |
862 | */ | |
863 | if (!upstream) | |
64720288 | 864 | return error(_("HEAD does not point to a branch")); |
bb0dab5d ZJS |
865 | if (!upstream->merge || !upstream->merge[0]->dst) { |
866 | if (!ref_exists(upstream->refname)) | |
64720288 | 867 | return error(_("No such branch: '%s'"), cp); |
17c82211 | 868 | if (!upstream->merge) |
64720288 | 869 | return error(_("No upstream configured for branch '%s'"), |
17c82211 ZJS |
870 | upstream->name); |
871 | return error( | |
64720288 | 872 | _("Upstream branch '%s' not stored as a remote-tracking branch"), |
17c82211 | 873 | upstream->merge[0]->src); |
bb0dab5d | 874 | } |
ae0ba8e2 JH |
875 | free(cp); |
876 | cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0); | |
877 | strbuf_reset(buf); | |
878 | strbuf_addstr(buf, cp); | |
879 | free(cp); | |
880 | return len; | |
881 | } | |
882 | ||
6bab74e7 JN |
883 | int strbuf_branchname(struct strbuf *sb, const char *name) |
884 | { | |
885 | int len = strlen(name); | |
886 | if (interpret_branch_name(name, sb) == len) | |
887 | return 0; | |
888 | strbuf_add(sb, name, len); | |
889 | return len; | |
890 | } | |
891 | ||
892 | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) | |
893 | { | |
894 | strbuf_branchname(sb, name); | |
895 | if (name[0] == '-') | |
8d9c5010 | 896 | return -1; |
6bab74e7 | 897 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
8d9c5010 | 898 | return check_refname_format(sb->buf, 0); |
6bab74e7 JN |
899 | } |
900 | ||
9938af6a JH |
901 | /* |
902 | * This is like "get_sha1_basic()", except it allows "sha1 expressions", | |
903 | * notably "xyz^" for "parent of xyz" | |
904 | */ | |
905 | int get_sha1(const char *name, unsigned char *sha1) | |
906 | { | |
573285e5 CP |
907 | struct object_context unused; |
908 | return get_sha1_with_context(name, sha1, &unused); | |
a0cd87a5 MK |
909 | } |
910 | ||
009fee47 MM |
911 | /* Must be called only when object_name:filename doesn't exist. */ |
912 | static void diagnose_invalid_sha1_path(const char *prefix, | |
913 | const char *filename, | |
914 | const unsigned char *tree_sha1, | |
915 | const char *object_name) | |
916 | { | |
917 | struct stat st; | |
918 | unsigned char sha1[20]; | |
919 | unsigned mode; | |
920 | ||
921 | if (!prefix) | |
922 | prefix = ""; | |
923 | ||
924 | if (!lstat(filename, &st)) | |
925 | die("Path '%s' exists on disk, but not in '%s'.", | |
926 | filename, object_name); | |
927 | if (errno == ENOENT || errno == ENOTDIR) { | |
928 | char *fullname = xmalloc(strlen(filename) | |
929 | + strlen(prefix) + 1); | |
930 | strcpy(fullname, prefix); | |
931 | strcat(fullname, filename); | |
932 | ||
933 | if (!get_tree_entry(tree_sha1, fullname, | |
934 | sha1, &mode)) { | |
935 | die("Path '%s' exists, but not '%s'.\n" | |
e41d718c | 936 | "Did you mean '%s:%s' aka '%s:./%s'?", |
009fee47 MM |
937 | fullname, |
938 | filename, | |
939 | object_name, | |
e41d718c MG |
940 | fullname, |
941 | object_name, | |
942 | filename); | |
009fee47 MM |
943 | } |
944 | die("Path '%s' does not exist in '%s'", | |
945 | filename, object_name); | |
946 | } | |
947 | } | |
948 | ||
949 | /* Must be called only when :stage:filename doesn't exist. */ | |
950 | static void diagnose_invalid_index_path(int stage, | |
951 | const char *prefix, | |
952 | const char *filename) | |
953 | { | |
954 | struct stat st; | |
955 | struct cache_entry *ce; | |
956 | int pos; | |
957 | unsigned namelen = strlen(filename); | |
958 | unsigned fullnamelen; | |
959 | char *fullname; | |
960 | ||
961 | if (!prefix) | |
962 | prefix = ""; | |
963 | ||
964 | /* Wrong stage number? */ | |
965 | pos = cache_name_pos(filename, namelen); | |
966 | if (pos < 0) | |
967 | pos = -pos - 1; | |
77e8466f MH |
968 | if (pos < active_nr) { |
969 | ce = active_cache[pos]; | |
970 | if (ce_namelen(ce) == namelen && | |
971 | !memcmp(ce->name, filename, namelen)) | |
972 | die("Path '%s' is in the index, but not at stage %d.\n" | |
973 | "Did you mean ':%d:%s'?", | |
974 | filename, stage, | |
975 | ce_stage(ce), filename); | |
976 | } | |
009fee47 MM |
977 | |
978 | /* Confusion between relative and absolute filenames? */ | |
979 | fullnamelen = namelen + strlen(prefix); | |
980 | fullname = xmalloc(fullnamelen + 1); | |
981 | strcpy(fullname, prefix); | |
982 | strcat(fullname, filename); | |
983 | pos = cache_name_pos(fullname, fullnamelen); | |
984 | if (pos < 0) | |
985 | pos = -pos - 1; | |
77e8466f MH |
986 | if (pos < active_nr) { |
987 | ce = active_cache[pos]; | |
988 | if (ce_namelen(ce) == fullnamelen && | |
989 | !memcmp(ce->name, fullname, fullnamelen)) | |
990 | die("Path '%s' is in the index, but not '%s'.\n" | |
e41d718c | 991 | "Did you mean ':%d:%s' aka ':%d:./%s'?", |
77e8466f | 992 | fullname, filename, |
e41d718c MG |
993 | ce_stage(ce), fullname, |
994 | ce_stage(ce), filename); | |
77e8466f | 995 | } |
009fee47 MM |
996 | |
997 | if (!lstat(filename, &st)) | |
998 | die("Path '%s' exists on disk, but not in the index.", filename); | |
999 | if (errno == ENOENT || errno == ENOTDIR) | |
1000 | die("Path '%s' does not exist (neither on disk nor in the index).", | |
1001 | filename); | |
1002 | ||
1003 | free(fullname); | |
1004 | } | |
1005 | ||
1006 | ||
2e83b66c JH |
1007 | int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode, |
1008 | int only_to_die, const char *prefix) | |
573285e5 CP |
1009 | { |
1010 | struct object_context oc; | |
1011 | int ret; | |
2e83b66c | 1012 | ret = get_sha1_with_context_1(name, sha1, &oc, only_to_die, prefix); |
573285e5 CP |
1013 | *mode = oc.mode; |
1014 | return ret; | |
1015 | } | |
1016 | ||
979f7929 NTND |
1017 | static char *resolve_relative_path(const char *rel) |
1018 | { | |
1019 | if (prefixcmp(rel, "./") && prefixcmp(rel, "../")) | |
1020 | return NULL; | |
1021 | ||
1022 | if (!startup_info) | |
1023 | die("BUG: startup_info struct is not initialized."); | |
1024 | ||
1025 | if (!is_inside_work_tree()) | |
1026 | die("relative path syntax can't be used outside working tree."); | |
1027 | ||
1028 | /* die() inside prefix_path() if resolved path is outside worktree */ | |
1029 | return prefix_path(startup_info->prefix, | |
1030 | startup_info->prefix ? strlen(startup_info->prefix) : 0, | |
1031 | rel); | |
1032 | } | |
1033 | ||
573285e5 CP |
1034 | int get_sha1_with_context_1(const char *name, unsigned char *sha1, |
1035 | struct object_context *oc, | |
2e83b66c | 1036 | int only_to_die, const char *prefix) |
a0cd87a5 MK |
1037 | { |
1038 | int ret, bracket_depth; | |
73b0e5af JH |
1039 | int namelen = strlen(name); |
1040 | const char *cp; | |
5119602a | 1041 | |
573285e5 CP |
1042 | memset(oc, 0, sizeof(*oc)); |
1043 | oc->mode = S_IFINVALID; | |
73b0e5af JH |
1044 | ret = get_sha1_1(name, namelen, sha1); |
1045 | if (!ret) | |
1046 | return ret; | |
1047 | /* sha1:path --> object name of path in ent sha1 | |
979f7929 NTND |
1048 | * :path -> object name of absolute path in index |
1049 | * :./path -> object name of path relative to cwd in index | |
73b0e5af | 1050 | * :[0-3]:path -> object name of path in index at stage |
95ad6d2d | 1051 | * :/foo -> recent commit matching foo |
73b0e5af JH |
1052 | */ |
1053 | if (name[0] == ':') { | |
1054 | int stage = 0; | |
1055 | struct cache_entry *ce; | |
979f7929 | 1056 | char *new_path = NULL; |
73b0e5af | 1057 | int pos; |
2e83b66c | 1058 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
84baa31b NTND |
1059 | struct commit_list *list = NULL; |
1060 | for_each_ref(handle_one_ref, &list); | |
1061 | return get_sha1_oneline(name + 2, sha1, list); | |
1062 | } | |
73b0e5af JH |
1063 | if (namelen < 3 || |
1064 | name[2] != ':' || | |
1065 | name[1] < '0' || '3' < name[1]) | |
1066 | cp = name + 1; | |
1067 | else { | |
1068 | stage = name[1] - '0'; | |
1069 | cp = name + 3; | |
5119602a | 1070 | } |
3d6e0f74 JH |
1071 | new_path = resolve_relative_path(cp); |
1072 | if (!new_path) { | |
1073 | namelen = namelen - (cp - name); | |
1074 | } else { | |
1075 | cp = new_path; | |
1076 | namelen = strlen(cp); | |
1077 | } | |
573285e5 CP |
1078 | |
1079 | strncpy(oc->path, cp, | |
1080 | sizeof(oc->path)); | |
1081 | oc->path[sizeof(oc->path)-1] = '\0'; | |
1082 | ||
73b0e5af JH |
1083 | if (!active_cache) |
1084 | read_cache(); | |
73b0e5af JH |
1085 | pos = cache_name_pos(cp, namelen); |
1086 | if (pos < 0) | |
1087 | pos = -pos - 1; | |
1088 | while (pos < active_nr) { | |
1089 | ce = active_cache[pos]; | |
1090 | if (ce_namelen(ce) != namelen || | |
1091 | memcmp(ce->name, cp, namelen)) | |
1092 | break; | |
1093 | if (ce_stage(ce) == stage) { | |
e702496e | 1094 | hashcpy(sha1, ce->sha1); |
90064710 | 1095 | oc->mode = ce->ce_mode; |
979f7929 | 1096 | free(new_path); |
73b0e5af JH |
1097 | return 0; |
1098 | } | |
e7cef45f | 1099 | pos++; |
73b0e5af | 1100 | } |
2e83b66c | 1101 | if (only_to_die && name[1] && name[1] != '/') |
009fee47 | 1102 | diagnose_invalid_index_path(stage, prefix, cp); |
979f7929 | 1103 | free(new_path); |
73b0e5af JH |
1104 | return -1; |
1105 | } | |
cce91a2c SP |
1106 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
1107 | if (*cp == '{') | |
1108 | bracket_depth++; | |
1109 | else if (bracket_depth && *cp == '}') | |
1110 | bracket_depth--; | |
1111 | else if (!bracket_depth && *cp == ':') | |
1112 | break; | |
1113 | } | |
1114 | if (*cp == ':') { | |
73b0e5af | 1115 | unsigned char tree_sha1[20]; |
009fee47 | 1116 | char *object_name = NULL; |
2e83b66c | 1117 | if (only_to_die) { |
009fee47 MM |
1118 | object_name = xmalloc(cp-name+1); |
1119 | strncpy(object_name, name, cp-name); | |
1120 | object_name[cp-name] = '\0'; | |
1121 | } | |
1122 | if (!get_sha1_1(name, cp-name, tree_sha1)) { | |
1123 | const char *filename = cp+1; | |
979f7929 NTND |
1124 | char *new_filename = NULL; |
1125 | ||
1126 | new_filename = resolve_relative_path(filename); | |
1127 | if (new_filename) | |
1128 | filename = new_filename; | |
573285e5 | 1129 | ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode); |
2e83b66c | 1130 | if (only_to_die) { |
009fee47 MM |
1131 | diagnose_invalid_sha1_path(prefix, filename, |
1132 | tree_sha1, object_name); | |
1133 | free(object_name); | |
1134 | } | |
573285e5 CP |
1135 | hashcpy(oc->tree, tree_sha1); |
1136 | strncpy(oc->path, filename, | |
1137 | sizeof(oc->path)); | |
1138 | oc->path[sizeof(oc->path)-1] = '\0'; | |
1139 | ||
979f7929 | 1140 | free(new_filename); |
009fee47 MM |
1141 | return ret; |
1142 | } else { | |
2e83b66c | 1143 | if (only_to_die) |
009fee47 MM |
1144 | die("Invalid object name '%s'.", object_name); |
1145 | } | |
5119602a LT |
1146 | } |
1147 | return ret; | |
9938af6a | 1148 | } |