blob: 484081de82928108a23a714a76ea88693e56bdd1 [file] [log] [blame]
Junio C Hamano9938af62005-08-03 22:15:49 -07001#include "cache.h"
Junio C Hamano5385f522005-10-13 18:57:40 -07002#include "tag.h"
Junio C Hamano9938af62005-08-03 22:15:49 -07003#include "commit.h"
Junio C Hamano5385f522005-10-13 18:57:40 -07004#include "tree.h"
5#include "blob.h"
Junio C Hamanof3ab49d2006-04-19 11:56:53 -07006#include "tree-walk.h"
Shawn Pearced556fae2006-05-17 05:56:09 -04007#include "refs.h"
Johannes Schindelin28fb8432009-09-10 17:25:57 +02008#include "remote.h"
Junio C Hamano9938af62005-08-03 22:15:49 -07009
10static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
11{
Junio C Hamano99a19b42005-10-02 21:40:51 -070012 struct alternate_object_database *alt;
Junio C Hamano9938af62005-08-03 22:15:49 -070013 char hex[40];
Junio C Hamano99a19b42005-10-02 21:40:51 -070014 int found = 0;
15 static struct alternate_object_database *fakeent;
Junio C Hamano9938af62005-08-03 22:15:49 -070016
Junio C Hamano99a19b42005-10-02 21:40:51 -070017 if (!fakeent) {
18 const char *objdir = get_object_directory();
19 int objdir_len = strlen(objdir);
20 int entlen = objdir_len + 43;
21 fakeent = xmalloc(sizeof(*fakeent) + entlen);
22 memcpy(fakeent->base, objdir, objdir_len);
23 fakeent->name = fakeent->base + objdir_len + 1;
24 fakeent->name[-1] = '/';
25 }
26 fakeent->next = alt_odb_list;
27
Junio C Hamano9938af62005-08-03 22:15:49 -070028 sprintf(hex, "%.2s", name);
Junio C Hamano99a19b42005-10-02 21:40:51 -070029 for (alt = fakeent; alt && found < 2; alt = alt->next) {
Junio C Hamano9938af62005-08-03 22:15:49 -070030 struct dirent *de;
Junio C Hamano99a19b42005-10-02 21:40:51 -070031 DIR *dir;
32 sprintf(alt->name, "%.2s/", name);
33 dir = opendir(alt->base);
34 if (!dir)
35 continue;
Junio C Hamano9938af62005-08-03 22:15:49 -070036 while ((de = readdir(dir)) != NULL) {
37 if (strlen(de->d_name) != 38)
38 continue;
Junio C Hamano99a19b42005-10-02 21:40:51 -070039 if (memcmp(de->d_name, name + 2, len - 2))
Junio C Hamano9938af62005-08-03 22:15:49 -070040 continue;
Junio C Hamano99a19b42005-10-02 21:40:51 -070041 if (!found) {
42 memcpy(hex + 2, de->d_name, 38);
43 found++;
44 }
45 else if (memcmp(hex + 2, de->d_name, 38)) {
46 found = 2;
Junio C Hamano9938af62005-08-03 22:15:49 -070047 break;
Junio C Hamano99a19b42005-10-02 21:40:51 -070048 }
Junio C Hamano9938af62005-08-03 22:15:49 -070049 }
50 closedir(dir);
51 }
52 if (found == 1)
53 return get_sha1_hex(hex, sha1) == 0;
Junio C Hamano99a19b42005-10-02 21:40:51 -070054 return found;
Junio C Hamano9938af62005-08-03 22:15:49 -070055}
56
57static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
58{
59 do {
60 if (*a != *b)
61 return 0;
62 a++;
63 b++;
64 len -= 2;
65 } while (len > 1);
66 if (len)
67 if ((*a ^ *b) & 0xf0)
68 return 0;
69 return 1;
70}
71
72static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
73{
74 struct packed_git *p;
Nicolas Pitred72308e2007-04-04 16:49:04 -040075 const unsigned char *found_sha1 = NULL;
Junio C Hamano99a19b42005-10-02 21:40:51 -070076 int found = 0;
Junio C Hamano9938af62005-08-03 22:15:49 -070077
78 prepare_packed_git();
Junio C Hamano99a19b42005-10-02 21:40:51 -070079 for (p = packed_git; p && found < 2; p = p->next) {
James Bowes10558802007-05-29 19:29:51 -040080 uint32_t num, last;
81 uint32_t first = 0;
82 open_pack_index(p);
83 num = p->num_objects;
84 last = num;
Junio C Hamano9938af62005-08-03 22:15:49 -070085 while (first < last) {
Shawn O. Pearce326bf392007-03-06 20:44:19 -050086 uint32_t mid = (first + last) / 2;
Nicolas Pitred72308e2007-04-04 16:49:04 -040087 const unsigned char *now;
Junio C Hamano9938af62005-08-03 22:15:49 -070088 int cmp;
89
Nicolas Pitred72308e2007-04-04 16:49:04 -040090 now = nth_packed_object_sha1(p, mid);
David Rientjesa89fccd2006-08-17 11:54:57 -070091 cmp = hashcmp(match, now);
Junio C Hamano9938af62005-08-03 22:15:49 -070092 if (!cmp) {
93 first = mid;
94 break;
95 }
96 if (cmp > 0) {
97 first = mid+1;
98 continue;
99 }
100 last = mid;
101 }
102 if (first < num) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400103 const unsigned char *now, *next;
104 now = nth_packed_object_sha1(p, first);
Junio C Hamano9938af62005-08-03 22:15:49 -0700105 if (match_sha(len, match, now)) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400106 next = nth_packed_object_sha1(p, first+1);
107 if (!next|| !match_sha(len, match, next)) {
Junio C Hamano0bc45892005-10-02 21:40:51 -0700108 /* unique within this pack */
109 if (!found) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400110 found_sha1 = now;
Junio C Hamano0bc45892005-10-02 21:40:51 -0700111 found++;
112 }
David Rientjesa89fccd2006-08-17 11:54:57 -0700113 else if (hashcmp(found_sha1, now)) {
Junio C Hamano0bc45892005-10-02 21:40:51 -0700114 found = 2;
115 break;
116 }
Junio C Hamano99a19b42005-10-02 21:40:51 -0700117 }
Junio C Hamano0bc45892005-10-02 21:40:51 -0700118 else {
119 /* not even unique within this pack */
Junio C Hamano99a19b42005-10-02 21:40:51 -0700120 found = 2;
121 break;
Junio C Hamano9938af62005-08-03 22:15:49 -0700122 }
123 }
124 }
125 }
Junio C Hamano99a19b42005-10-02 21:40:51 -0700126 if (found == 1)
Shawn Pearcee7024962006-08-23 02:49:00 -0400127 hashcpy(sha1, found_sha1);
Junio C Hamano99a19b42005-10-02 21:40:51 -0700128 return found;
129}
130
Junio C Hamano013f2762005-10-11 15:22:48 -0700131#define SHORT_NAME_NOT_FOUND (-1)
132#define SHORT_NAME_AMBIGUOUS (-2)
133
Junio C Hamano99a19b42005-10-02 21:40:51 -0700134static int find_unique_short_object(int len, char *canonical,
135 unsigned char *res, unsigned char *sha1)
136{
137 int has_unpacked, has_packed;
138 unsigned char unpacked_sha1[20], packed_sha1[20];
139
Shawn O. Pearce693d2bc2007-05-26 01:25:11 -0400140 prepare_alt_odb();
Junio C Hamano99a19b42005-10-02 21:40:51 -0700141 has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1);
142 has_packed = find_short_packed_object(len, res, packed_sha1);
143 if (!has_unpacked && !has_packed)
Junio C Hamano013f2762005-10-11 15:22:48 -0700144 return SHORT_NAME_NOT_FOUND;
Junio C Hamano99a19b42005-10-02 21:40:51 -0700145 if (1 < has_unpacked || 1 < has_packed)
Junio C Hamano013f2762005-10-11 15:22:48 -0700146 return SHORT_NAME_AMBIGUOUS;
Junio C Hamano99a19b42005-10-02 21:40:51 -0700147 if (has_unpacked != has_packed) {
Shawn Pearcee7024962006-08-23 02:49:00 -0400148 hashcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1));
Junio C Hamano99a19b42005-10-02 21:40:51 -0700149 return 0;
150 }
151 /* Both have unique ones -- do they match? */
David Rientjesa89fccd2006-08-17 11:54:57 -0700152 if (hashcmp(packed_sha1, unpacked_sha1))
Uwe Zeisbergere974c9a2006-01-26 12:26:15 +0100153 return SHORT_NAME_AMBIGUOUS;
Shawn Pearcee7024962006-08-23 02:49:00 -0400154 hashcpy(sha1, packed_sha1);
Junio C Hamano9938af62005-08-03 22:15:49 -0700155 return 0;
156}
157
Junio C Hamano013f2762005-10-11 15:22:48 -0700158static int get_short_sha1(const char *name, int len, unsigned char *sha1,
159 int quietly)
Junio C Hamano9938af62005-08-03 22:15:49 -0700160{
Junio C Hamano013f2762005-10-11 15:22:48 -0700161 int i, status;
Junio C Hamano9938af62005-08-03 22:15:49 -0700162 char canonical[40];
163 unsigned char res[20];
164
pclouds@gmail.com8a831572006-10-19 08:34:41 +0700165 if (len < MINIMUM_ABBREV || len > 40)
Linus Torvaldsaf61c6e2005-09-19 15:16:03 -0700166 return -1;
Junio C Hamanoa8e0d162006-08-23 13:57:23 -0700167 hashclr(res);
Junio C Hamano9938af62005-08-03 22:15:49 -0700168 memset(canonical, 'x', 40);
Linus Torvaldsaf61c6e2005-09-19 15:16:03 -0700169 for (i = 0; i < len ;i++) {
Junio C Hamano9938af62005-08-03 22:15:49 -0700170 unsigned char c = name[i];
171 unsigned char val;
Junio C Hamano9938af62005-08-03 22:15:49 -0700172 if (c >= '0' && c <= '9')
173 val = c - '0';
174 else if (c >= 'a' && c <= 'f')
175 val = c - 'a' + 10;
176 else if (c >= 'A' && c <='F') {
177 val = c - 'A' + 10;
178 c -= 'A' - 'a';
179 }
180 else
181 return -1;
182 canonical[i] = c;
183 if (!(i & 1))
184 val <<= 4;
185 res[i >> 1] |= val;
186 }
Junio C Hamano99a19b42005-10-02 21:40:51 -0700187
Junio C Hamano013f2762005-10-11 15:22:48 -0700188 status = find_unique_short_object(i, canonical, res, sha1);
189 if (!quietly && (status == SHORT_NAME_AMBIGUOUS))
190 return error("short SHA1 %.*s is ambiguous.", len, canonical);
191 return status;
192}
193
194const char *find_unique_abbrev(const unsigned char *sha1, int len)
195{
Junio C Hamanob66fde92008-03-01 23:35:32 -0800196 int status, exists;
Junio C Hamano013f2762005-10-11 15:22:48 -0700197 static char hex[41];
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800198
Junio C Hamanob66fde92008-03-01 23:35:32 -0800199 exists = has_sha1_file(sha1);
Junio C Hamano013f2762005-10-11 15:22:48 -0700200 memcpy(hex, sha1_to_hex(sha1), 40);
Junio C Hamano02c5cba2006-08-09 13:17:04 -0700201 if (len == 40 || !len)
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800202 return hex;
Junio C Hamano013f2762005-10-11 15:22:48 -0700203 while (len < 40) {
204 unsigned char sha1_ret[20];
205 status = get_short_sha1(hex, len, sha1_ret, 1);
Junio C Hamanob66fde92008-03-01 23:35:32 -0800206 if (exists
207 ? !status
208 : status == SHORT_NAME_NOT_FOUND) {
Junio C Hamano013f2762005-10-11 15:22:48 -0700209 hex[len] = 0;
210 return hex;
211 }
Junio C Hamano013f2762005-10-11 15:22:48 -0700212 len++;
213 }
Junio C Hamanob66fde92008-03-01 23:35:32 -0800214 return hex;
Junio C Hamano9938af62005-08-03 22:15:49 -0700215}
216
Junio C Hamano6677c462005-12-15 12:54:00 -0800217static int ambiguous_path(const char *path, int len)
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700218{
219 int slash = 1;
Junio C Hamano6677c462005-12-15 12:54:00 -0800220 int cnt;
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700221
Junio C Hamano6677c462005-12-15 12:54:00 -0800222 for (cnt = 0; cnt < len; cnt++) {
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700223 switch (*path++) {
224 case '\0':
225 break;
226 case '/':
227 if (slash)
228 break;
229 slash = 1;
230 continue;
231 case '.':
232 continue;
233 default:
234 slash = 0;
235 continue;
236 }
Junio C Hamanoc054d642005-12-17 00:00:50 -0800237 break;
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700238 }
Junio C Hamano6677c462005-12-15 12:54:00 -0800239 return slash;
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700240}
241
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100242/*
243 * *string and *len will only be substituted, and *string returned (for
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800244 * later free()ing) if the string passed in is a magic short-hand form
245 * to name a branch.
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100246 */
Junio C Hamano431b1962009-03-21 12:51:34 -0700247static char *substitute_branch_name(const char **string, int *len)
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100248{
249 struct strbuf buf = STRBUF_INIT;
Junio C Hamano431b1962009-03-21 12:51:34 -0700250 int ret = interpret_branch_name(*string, &buf);
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100251
252 if (ret == *len) {
253 size_t size;
254 *string = strbuf_detach(&buf, &size);
255 *len = size;
256 return (char *)*string;
257 }
258
259 return NULL;
260}
261
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800262int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
Junio C Hamano9938af62005-08-03 22:15:49 -0700263{
Junio C Hamano431b1962009-03-21 12:51:34 -0700264 char *last_branch = substitute_branch_name(&str, &len);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800265 const char **p, *r;
266 int refs_found = 0;
267
268 *ref = NULL;
Steffen Prohaska79803322007-11-11 15:01:46 +0100269 for (p = ref_rev_parse_rules; *p; p++) {
Alex Riesen94cc3552008-10-26 23:07:24 +0100270 char fullref[PATH_MAX];
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800271 unsigned char sha1_from_ref[20];
272 unsigned char *this_result;
Junio C Hamano057e7132009-02-08 23:52:01 -0800273 int flag;
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800274
275 this_result = refs_found ? sha1_from_ref : sha1;
Alex Riesen94cc3552008-10-26 23:07:24 +0100276 mksnpath(fullref, sizeof(fullref), *p, len, str);
Junio C Hamano057e7132009-02-08 23:52:01 -0800277 r = resolve_ref(fullref, this_result, 1, &flag);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800278 if (r) {
279 if (!refs_found++)
280 *ref = xstrdup(r);
281 if (!warn_ambiguous_refs)
282 break;
Jeff King003c6ab2010-02-16 02:03:16 -0500283 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD"))
Junio C Hamano057e7132009-02-08 23:52:01 -0800284 warning("ignoring dangling symref %s.", fullref);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800285 }
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100286 free(last_branch);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800287 return refs_found;
288}
289
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100290int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
Nicolas Pitref2eba662007-02-03 21:49:16 -0500291{
Junio C Hamano431b1962009-03-21 12:51:34 -0700292 char *last_branch = substitute_branch_name(&str, &len);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500293 const char **p;
294 int logs_found = 0;
295
296 *log = NULL;
Steffen Prohaska79803322007-11-11 15:01:46 +0100297 for (p = ref_rev_parse_rules; *p; p++) {
Nicolas Pitref2eba662007-02-03 21:49:16 -0500298 struct stat st;
Junio C Hamano40facde2007-02-08 23:24:51 -0800299 unsigned char hash[20];
300 char path[PATH_MAX];
301 const char *ref, *it;
302
Alex Riesen94cc3552008-10-26 23:07:24 +0100303 mksnpath(path, sizeof(path), *p, len, str);
Junio C Hamano0c4cd7f2008-07-23 17:22:58 -0700304 ref = resolve_ref(path, hash, 1, NULL);
Junio C Hamano40facde2007-02-08 23:24:51 -0800305 if (!ref)
306 continue;
Nicolas Pitref2eba662007-02-03 21:49:16 -0500307 if (!stat(git_path("logs/%s", path), &st) &&
Junio C Hamano40facde2007-02-08 23:24:51 -0800308 S_ISREG(st.st_mode))
309 it = path;
310 else if (strcmp(ref, path) &&
311 !stat(git_path("logs/%s", ref), &st) &&
312 S_ISREG(st.st_mode))
313 it = ref;
314 else
315 continue;
316 if (!logs_found++) {
317 *log = xstrdup(it);
318 hashcpy(sha1, hash);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500319 }
Junio C Hamano40facde2007-02-08 23:24:51 -0800320 if (!warn_ambiguous_refs)
321 break;
Nicolas Pitref2eba662007-02-03 21:49:16 -0500322 }
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100323 free(last_branch);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500324 return logs_found;
325}
326
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800327static inline int upstream_mark(const char *string, int len)
328{
329 const char *suffix[] = { "@{upstream}", "@{u}" };
330 int i;
331
332 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
333 int suffix_len = strlen(suffix[i]);
334 if (suffix_len <= len
335 && !memcmp(string, suffix[i], suffix_len))
336 return suffix_len;
337 }
338 return 0;
339}
340
Thomas Rastd18ba222009-01-17 17:09:55 +0100341static int get_sha1_1(const char *name, int len, unsigned char *sha1);
342
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800343static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
344{
Jon Seymoureedce782010-08-24 14:52:43 +1000345 static const char *warn_msg = "refname '%.*s' is ambiguous.";
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700346 char *real_ref = NULL;
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700347 int refs_found = 0;
348 int at, reflog_len;
Junio C Hamano9938af62005-08-03 22:15:49 -0700349
Linus Torvalds3c3852e2005-08-13 11:05:25 -0700350 if (len == 40 && !get_sha1_hex(str, sha1))
Junio C Hamano9938af62005-08-03 22:15:49 -0700351 return 0;
352
Thomas Rastd18ba222009-01-17 17:09:55 +0100353 /* basic@{time or number or -number} format to query ref-log */
Junio C Hamano694500e2006-10-23 21:15:34 -0700354 reflog_len = at = 0;
Johannes Schindelinf2654582009-01-28 00:07:46 +0100355 if (len && str[len-1] == '}') {
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100356 for (at = len-2; at >= 0; at--) {
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700357 if (str[at] == '@' && str[at+1] == '{') {
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800358 if (!upstream_mark(str + at, len - at)) {
Johannes Schindelin28fb8432009-09-10 17:25:57 +0200359 reflog_len = (len-1) - (at+2);
360 len = at;
361 }
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700362 break;
363 }
Shawn Pearced556fae2006-05-17 05:56:09 -0400364 }
365 }
366
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700367 /* Accept only unambiguous ref paths. */
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500368 if (len && ambiguous_path(str, len))
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700369 return -1;
370
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500371 if (!len && reflog_len) {
Thomas Rastd18ba222009-01-17 17:09:55 +0100372 struct strbuf buf = STRBUF_INIT;
373 int ret;
374 /* try the @{-N} syntax for n-th checkout */
Junio C Hamano431b1962009-03-21 12:51:34 -0700375 ret = interpret_branch_name(str+at, &buf);
Thomas Rastd18ba222009-01-17 17:09:55 +0100376 if (ret > 0) {
377 /* substitute this branch name and restart */
378 return get_sha1_1(buf.buf, buf.len, sha1);
379 } else if (ret == 0) {
380 return -1;
381 }
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500382 /* allow "@{...}" to mean the current branch reflog */
383 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500384 } else if (reflog_len)
385 refs_found = dwim_log(str, len, sha1, &real_ref);
386 else
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500387 refs_found = dwim_ref(str, len, sha1, &real_ref);
Shawn Pearced556fae2006-05-17 05:56:09 -0400388
389 if (!refs_found)
390 return -1;
391
392 if (warn_ambiguous_refs && refs_found > 1)
Jon Seymoureedce782010-08-24 14:52:43 +1000393 warning(warn_msg, len, str);
Shawn Pearced556fae2006-05-17 05:56:09 -0400394
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700395 if (reflog_len) {
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700396 int nth, i;
397 unsigned long at_time;
Junio C Hamano16d7cc92007-01-19 01:19:05 -0800398 unsigned long co_time;
399 int co_tz, co_cnt;
400
Jeff King12a258c2010-01-28 04:56:43 -0500401 /* a @{-N} placed anywhere except the start is an error */
402 if (str[at+2] == '-')
403 return -1;
404
Nicolas Pitrefe558512007-02-01 12:33:23 -0500405 /* Is it asking for N-th entry, or approxidate? */
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700406 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
407 char ch = str[at+2+i];
408 if ('0' <= ch && ch <= '9')
409 nth = nth * 10 + ch - '0';
410 else
411 nth = -1;
412 }
Shawn O. Pearceea360dd2008-08-21 08:40:44 -0700413 if (100000000 <= nth) {
414 at_time = nth;
415 nth = -1;
416 } else if (0 <= nth)
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700417 at_time = 0;
Jeff King861f00e2008-04-30 00:13:58 -0400418 else {
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800419 int errors = 0;
Jeff King861f00e2008-04-30 00:13:58 -0400420 char *tmp = xstrndup(str + at + 2, reflog_len);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800421 at_time = approxidate_careful(tmp, &errors);
Jeff King861f00e2008-04-30 00:13:58 -0400422 free(tmp);
Junio C Hamanoa5e10ac2010-01-27 10:53:09 -0800423 if (errors)
424 return -1;
Jeff King861f00e2008-04-30 00:13:58 -0400425 }
Junio C Hamano16d7cc92007-01-19 01:19:05 -0800426 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
427 &co_time, &co_tz, &co_cnt)) {
428 if (at_time)
Jon Seymoureedce782010-08-24 14:52:43 +1000429 warning("Log for '%.*s' only goes "
430 "back to %s.", len, str,
Junio C Hamano73013af2007-07-13 23:14:52 -0700431 show_date(co_time, co_tz, DATE_RFC2822));
Jon Seymoure6eedc32010-08-24 14:52:42 +1000432 else {
433 free(real_ref);
434 die("Log for '%.*s' only has %d entries.",
435 len, str, co_cnt);
436 }
Junio C Hamano16d7cc92007-01-19 01:19:05 -0800437 }
Shawn Pearced556fae2006-05-17 05:56:09 -0400438 }
439
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700440 free(real_ref);
Shawn Pearced556fae2006-05-17 05:56:09 -0400441 return 0;
Junio C Hamano9938af62005-08-03 22:15:49 -0700442}
443
Junio C Hamano9938af62005-08-03 22:15:49 -0700444static int get_parent(const char *name, int len,
445 unsigned char *result, int idx)
446{
447 unsigned char sha1[20];
448 int ret = get_sha1_1(name, len, sha1);
449 struct commit *commit;
450 struct commit_list *p;
451
452 if (ret)
453 return ret;
454 commit = lookup_commit_reference(sha1);
455 if (!commit)
456 return -1;
457 if (parse_commit(commit))
458 return -1;
459 if (!idx) {
Shawn Pearcee7024962006-08-23 02:49:00 -0400460 hashcpy(result, commit->object.sha1);
Junio C Hamano9938af62005-08-03 22:15:49 -0700461 return 0;
462 }
463 p = commit->parents;
464 while (p) {
465 if (!--idx) {
Shawn Pearcee7024962006-08-23 02:49:00 -0400466 hashcpy(result, p->item->object.sha1);
Junio C Hamano9938af62005-08-03 22:15:49 -0700467 return 0;
468 }
469 p = p->next;
470 }
471 return -1;
472}
473
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700474static int get_nth_ancestor(const char *name, int len,
475 unsigned char *result, int generation)
476{
477 unsigned char sha1[20];
Linus Torvalds621ff672008-03-14 11:49:40 -0700478 struct commit *commit;
479 int ret;
480
481 ret = get_sha1_1(name, len, sha1);
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700482 if (ret)
483 return ret;
Linus Torvalds621ff672008-03-14 11:49:40 -0700484 commit = lookup_commit_reference(sha1);
485 if (!commit)
486 return -1;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700487
488 while (generation--) {
Linus Torvalds621ff672008-03-14 11:49:40 -0700489 if (parse_commit(commit) || !commit->parents)
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700490 return -1;
Linus Torvalds621ff672008-03-14 11:49:40 -0700491 commit = commit->parents->item;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700492 }
Linus Torvalds621ff672008-03-14 11:49:40 -0700493 hashcpy(result, commit->object.sha1);
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700494 return 0;
495}
496
Junio C Hamano81776312007-12-24 00:51:01 -0800497struct object *peel_to_type(const char *name, int namelen,
498 struct object *o, enum object_type expected_type)
499{
500 if (name && !namelen)
501 namelen = strlen(name);
502 if (!o) {
503 unsigned char sha1[20];
504 if (get_sha1_1(name, namelen, sha1))
505 return NULL;
506 o = parse_object(sha1);
507 }
508 while (1) {
509 if (!o || (!o->parsed && !parse_object(o->sha1)))
510 return NULL;
511 if (o->type == expected_type)
512 return o;
513 if (o->type == OBJ_TAG)
514 o = ((struct tag*) o)->tagged;
515 else if (o->type == OBJ_COMMIT)
516 o = &(((struct commit *) o)->tree->object);
517 else {
518 if (name)
519 error("%.*s: expected %s type, but the object "
520 "dereferences to %s type",
521 namelen, name, typename(expected_type),
522 typename(o->type));
523 return NULL;
524 }
525 }
526}
527
Junio C Hamano5385f522005-10-13 18:57:40 -0700528static int peel_onion(const char *name, int len, unsigned char *sha1)
529{
530 unsigned char outer[20];
531 const char *sp;
Linus Torvalds885a86a2006-06-14 16:45:13 -0700532 unsigned int expected_type = 0;
Junio C Hamano5385f522005-10-13 18:57:40 -0700533 struct object *o;
534
535 /*
536 * "ref^{type}" dereferences ref repeatedly until you cannot
537 * dereference anymore, or you get an object of given type,
538 * whichever comes first. "ref^{}" means just dereference
539 * tags until you get a non-tag. "ref^0" is a shorthand for
540 * "ref^{commit}". "commit^{tree}" could be used to find the
541 * top-level tree of the given commit.
542 */
543 if (len < 4 || name[len-1] != '}')
544 return -1;
545
546 for (sp = name + len - 1; name <= sp; sp--) {
547 int ch = *sp;
548 if (ch == '{' && name < sp && sp[-1] == '^')
549 break;
550 }
551 if (sp <= name)
552 return -1;
553
554 sp++; /* beginning of type name, or closing brace for empty */
555 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700556 expected_type = OBJ_COMMIT;
Junio C Hamano5385f522005-10-13 18:57:40 -0700557 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700558 expected_type = OBJ_TREE;
Junio C Hamano5385f522005-10-13 18:57:40 -0700559 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700560 expected_type = OBJ_BLOB;
Junio C Hamano5385f522005-10-13 18:57:40 -0700561 else if (sp[0] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700562 expected_type = OBJ_NONE;
Junio C Hamano5385f522005-10-13 18:57:40 -0700563 else
564 return -1;
565
566 if (get_sha1_1(name, sp - name - 2, outer))
567 return -1;
568
569 o = parse_object(outer);
570 if (!o)
571 return -1;
Linus Torvalds885a86a2006-06-14 16:45:13 -0700572 if (!expected_type) {
Junio C Hamano9534f402005-11-02 15:19:13 -0800573 o = deref_tag(o, name, sp - name - 2);
Junio C Hamano6e1c6c12005-10-19 22:48:16 -0700574 if (!o || (!o->parsed && !parse_object(o->sha1)))
575 return -1;
Shawn Pearcee7024962006-08-23 02:49:00 -0400576 hashcpy(sha1, o->sha1);
Junio C Hamano5385f522005-10-13 18:57:40 -0700577 }
578 else {
Junio C Hamano81776312007-12-24 00:51:01 -0800579 /*
580 * At this point, the syntax look correct, so
Junio C Hamano5385f522005-10-13 18:57:40 -0700581 * if we do not get the needed object, we should
582 * barf.
583 */
Junio C Hamano81776312007-12-24 00:51:01 -0800584 o = peel_to_type(name, len, o, expected_type);
585 if (o) {
586 hashcpy(sha1, o->sha1);
587 return 0;
Junio C Hamano5385f522005-10-13 18:57:40 -0700588 }
Junio C Hamano81776312007-12-24 00:51:01 -0800589 return -1;
Junio C Hamano5385f522005-10-13 18:57:40 -0700590 }
591 return 0;
592}
593
Junio C Hamano7dd45e12006-09-20 16:11:08 -0700594static int get_describe_name(const char *name, int len, unsigned char *sha1)
595{
596 const char *cp;
597
598 for (cp = name + len - 1; name + 2 <= cp; cp--) {
599 char ch = *cp;
600 if (hexval(ch) & ~0377) {
601 /* We must be looking at g in "SOMETHING-g"
602 * for it to be describe output.
603 */
604 if (ch == 'g' && cp[-1] == '-') {
605 cp++;
606 len -= cp - name;
607 return get_short_sha1(cp, len, sha1, 1);
608 }
609 }
610 }
611 return -1;
612}
613
Junio C Hamano9938af62005-08-03 22:15:49 -0700614static int get_sha1_1(const char *name, int len, unsigned char *sha1)
615{
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800616 int ret, has_suffix;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700617 const char *cp;
Junio C Hamano9938af62005-08-03 22:15:49 -0700618
Linus Torvalds621ff672008-03-14 11:49:40 -0700619 /*
620 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700621 */
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800622 has_suffix = 0;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700623 for (cp = name + len - 1; name <= cp; cp--) {
624 int ch = *cp;
625 if ('0' <= ch && ch <= '9')
626 continue;
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800627 if (ch == '~' || ch == '^')
628 has_suffix = ch;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700629 break;
630 }
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800631
632 if (has_suffix) {
633 int num = 0;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700634 int len1 = cp - name;
635 cp++;
636 while (cp < name + len)
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800637 num = num * 10 + *cp++ - '0';
Linus Torvalds621ff672008-03-14 11:49:40 -0700638 if (!num && len1 == len - 1)
639 num = 1;
640 if (has_suffix == '^')
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800641 return get_parent(name, len1, sha1, num);
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800642 /* else if (has_suffix == '~') -- goes without saying */
643 return get_nth_ancestor(name, len1, sha1, num);
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700644 }
645
Junio C Hamano5385f522005-10-13 18:57:40 -0700646 ret = peel_onion(name, len, sha1);
647 if (!ret)
648 return 0;
649
Junio C Hamano9938af62005-08-03 22:15:49 -0700650 ret = get_sha1_basic(name, len, sha1);
651 if (!ret)
652 return 0;
Junio C Hamano7dd45e12006-09-20 16:11:08 -0700653
654 /* It could be describe output that is "SOMETHING-gXXXX" */
655 ret = get_describe_name(name, len, sha1);
656 if (!ret)
657 return 0;
658
Junio C Hamano013f2762005-10-11 15:22:48 -0700659 return get_short_sha1(name, len, sha1, 0);
Junio C Hamano9938af62005-08-03 22:15:49 -0700660}
661
Junio C Hamanof7bff002010-08-02 14:37:06 -0700662/*
663 * This interprets names like ':/Initial revision of "git"' by searching
664 * through history and returning the first commit whose message starts
Junio C Hamano3d045892010-08-12 18:32:49 -0700665 * the given regular expression.
Junio C Hamanof7bff002010-08-02 14:37:06 -0700666 *
667 * For future extension, ':/!' is reserved. If you want to match a message
668 * beginning with a '!', you have to repeat the exclamation mark.
669 */
670#define ONELINE_SEEN (1u<<20)
671
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100672static int handle_one_ref(const char *path,
673 const unsigned char *sha1, int flag, void *cb_data)
674{
675 struct commit_list **list = cb_data;
676 struct object *object = parse_object(sha1);
677 if (!object)
678 return 0;
Martin Koegleraffeef12008-02-18 08:31:54 +0100679 if (object->type == OBJ_TAG) {
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100680 object = deref_tag(object, path, strlen(path));
Martin Koegleraffeef12008-02-18 08:31:54 +0100681 if (!object)
682 return 0;
683 }
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100684 if (object->type != OBJ_COMMIT)
685 return 0;
686 insert_by_date((struct commit *)object, list);
Junio C Hamanof7bff002010-08-02 14:37:06 -0700687 object->flags |= ONELINE_SEEN;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100688 return 0;
689}
690
Linus Torvalds1358e7d2007-03-12 11:30:38 -0700691static int get_sha1_oneline(const char *prefix, unsigned char *sha1)
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100692{
693 struct commit_list *list = NULL, *backup = NULL, *l;
Linus Torvalds1358e7d2007-03-12 11:30:38 -0700694 int retval = -1;
Johannes Schindelin364d3e62007-12-03 18:42:39 +0000695 char *temp_commit_buffer = NULL;
Linus Torvalds57895102010-04-23 08:20:20 -0700696 regex_t regex;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100697
698 if (prefix[0] == '!') {
699 if (prefix[1] != '!')
700 die ("Invalid search pattern: %s", prefix);
701 prefix++;
702 }
Linus Torvalds57895102010-04-23 08:20:20 -0700703
704 if (regcomp(&regex, prefix, REG_EXTENDED))
705 die("Invalid search pattern: %s", prefix);
706
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100707 for_each_ref(handle_one_ref, &list);
708 for (l = list; l; l = l->next)
709 commit_list_insert(l->item, &backup);
Jim Meyeringed8ad7e2007-03-11 19:49:08 +0100710 while (list) {
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100711 char *p;
Linus Torvalds1358e7d2007-03-12 11:30:38 -0700712 struct commit *commit;
Johannes Schindelin364d3e62007-12-03 18:42:39 +0000713 enum object_type type;
714 unsigned long size;
Jim Meyeringed8ad7e2007-03-11 19:49:08 +0100715
716 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
Martin Koegler283cdbc2008-02-18 21:47:53 +0100717 if (!parse_object(commit->object.sha1))
718 continue;
Jim Meyering8e0f7002008-01-31 18:26:32 +0100719 free(temp_commit_buffer);
Johannes Schindelin364d3e62007-12-03 18:42:39 +0000720 if (commit->buffer)
721 p = commit->buffer;
722 else {
723 p = read_sha1_file(commit->object.sha1, &type, &size);
724 if (!p)
725 continue;
726 temp_commit_buffer = p;
727 }
728 if (!(p = strstr(p, "\n\n")))
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100729 continue;
Linus Torvalds57895102010-04-23 08:20:20 -0700730 if (!regexec(&regex, p + 2, 0, NULL, 0)) {
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100731 hashcpy(sha1, commit->object.sha1);
Linus Torvalds1358e7d2007-03-12 11:30:38 -0700732 retval = 0;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100733 break;
734 }
735 }
Linus Torvalds57895102010-04-23 08:20:20 -0700736 regfree(&regex);
Jim Meyering8e0f7002008-01-31 18:26:32 +0100737 free(temp_commit_buffer);
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100738 free_commit_list(list);
739 for (l = backup; l; l = l->next)
740 clear_commit_marks(l->item, ONELINE_SEEN);
Linus Torvalds1358e7d2007-03-12 11:30:38 -0700741 return retval;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100742}
743
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100744struct grab_nth_branch_switch_cbdata {
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800745 long cnt, alloc;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100746 struct strbuf *buf;
747};
748
749static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
750 const char *email, unsigned long timestamp, int tz,
751 const char *message, void *cb_data)
752{
753 struct grab_nth_branch_switch_cbdata *cb = cb_data;
Thomas Rasta884d0c2009-01-17 17:09:54 +0100754 const char *match = NULL, *target = NULL;
755 size_t len;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800756 int nth;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100757
Thomas Rasta884d0c2009-01-17 17:09:54 +0100758 if (!prefixcmp(message, "checkout: moving from ")) {
759 match = message + strlen("checkout: moving from ");
Junio C Hamanod7c03c12009-01-21 00:37:38 -0800760 target = strstr(match, " to ");
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100761 }
762
Junio C Hamanoc8297742009-01-19 16:44:08 -0800763 if (!match || !target)
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100764 return 0;
765
Junio C Hamanod7c03c12009-01-21 00:37:38 -0800766 len = target - match;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800767 nth = cb->cnt++ % cb->alloc;
768 strbuf_reset(&cb->buf[nth]);
769 strbuf_add(&cb->buf[nth], match, len);
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100770 return 0;
771}
772
773/*
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800774 * Parse @{-N} syntax, return the number of characters parsed
775 * if successful; otherwise signal an error with negative value.
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100776 */
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800777static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf)
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100778{
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800779 long nth;
Junio C Hamano39765e52009-01-19 21:58:31 -0800780 int i, retval;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100781 struct grab_nth_branch_switch_cbdata cb;
Thomas Rasta884d0c2009-01-17 17:09:54 +0100782 const char *brace;
783 char *num_end;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100784
785 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
786 return -1;
Thomas Rasta884d0c2009-01-17 17:09:54 +0100787 brace = strchr(name, '}');
788 if (!brace)
789 return -1;
790 nth = strtol(name+3, &num_end, 10);
791 if (num_end != brace)
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100792 return -1;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800793 if (nth <= 0)
794 return -1;
795 cb.alloc = nth;
796 cb.buf = xmalloc(nth * sizeof(struct strbuf));
797 for (i = 0; i < nth; i++)
798 strbuf_init(&cb.buf[i], 20);
799 cb.cnt = 0;
Junio C Hamano39765e52009-01-19 21:58:31 -0800800 retval = 0;
Junio C Hamano101d15e2009-01-19 22:18:29 -0800801 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
802 if (cb.cnt < nth) {
803 cb.cnt = 0;
Junio C Hamano101d15e2009-01-19 22:18:29 -0800804 for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
805 }
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800806 if (cb.cnt < nth)
Junio C Hamano39765e52009-01-19 21:58:31 -0800807 goto release_return;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800808 i = cb.cnt % nth;
809 strbuf_reset(buf);
810 strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len);
Junio C Hamano39765e52009-01-19 21:58:31 -0800811 retval = brace-name+1;
812
813release_return:
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800814 for (i = 0; i < nth; i++)
815 strbuf_release(&cb.buf[i]);
816 free(cb.buf);
Thomas Rasta884d0c2009-01-17 17:09:54 +0100817
Junio C Hamano39765e52009-01-19 21:58:31 -0800818 return retval;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100819}
820
Junio C Hamano619a6442009-10-18 12:34:56 -0700821int get_sha1_mb(const char *name, unsigned char *sha1)
822{
823 struct commit *one, *two;
824 struct commit_list *mbs;
825 unsigned char sha1_tmp[20];
826 const char *dots;
827 int st;
828
829 dots = strstr(name, "...");
830 if (!dots)
831 return get_sha1(name, sha1);
832 if (dots == name)
833 st = get_sha1("HEAD", sha1_tmp);
834 else {
835 struct strbuf sb;
836 strbuf_init(&sb, dots - name);
837 strbuf_add(&sb, name, dots - name);
838 st = get_sha1(sb.buf, sha1_tmp);
839 strbuf_release(&sb);
840 }
841 if (st)
842 return st;
843 one = lookup_commit_reference_gently(sha1_tmp, 0);
844 if (!one)
845 return -1;
846
847 if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
848 return -1;
849 two = lookup_commit_reference_gently(sha1_tmp, 0);
850 if (!two)
851 return -1;
852 mbs = get_merge_bases(one, two, 1);
853 if (!mbs || mbs->next)
854 st = -1;
855 else {
856 st = 0;
857 hashcpy(sha1, mbs->item->object.sha1);
858 }
859 free_commit_list(mbs);
860 return st;
861}
862
Junio C Hamano9938af62005-08-03 22:15:49 -0700863/*
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800864 * This reads short-hand syntax that not only evaluates to a commit
865 * object name, but also can act as if the end user spelled the name
866 * of the branch from the command line.
867 *
868 * - "@{-N}" finds the name of the Nth previous branch we were on, and
869 * places the name of the branch in the given buf and returns the
870 * number of characters parsed if successful.
871 *
872 * - "<branch>@{upstream}" finds the name of the other ref that
873 * <branch> is configured to merge with (missing <branch> defaults
874 * to the current branch), and places the name of the branch in the
875 * given buf and returns the number of characters parsed if
876 * successful.
877 *
878 * If the input is not of the accepted format, it returns a negative
879 * number to signal an error.
880 *
881 * If the input was ok but there are not N branch switches in the
882 * reflog, it returns 0.
883 */
884int interpret_branch_name(const char *name, struct strbuf *buf)
885{
886 char *cp;
887 struct branch *upstream;
888 int namelen = strlen(name);
889 int len = interpret_nth_prior_checkout(name, buf);
890 int tmp_len;
891
892 if (!len)
893 return len; /* syntax Ok, not enough switches */
Jeff Kingd46a8302010-01-28 04:52:22 -0500894 if (0 < len && len == namelen)
895 return len; /* consumed all */
896 else if (0 < len) {
897 /* we have extra data, which might need further processing */
898 struct strbuf tmp = STRBUF_INIT;
899 int used = buf->len;
900 int ret;
901
902 strbuf_add(buf, name + len, namelen - len);
903 ret = interpret_branch_name(buf->buf, &tmp);
904 /* that data was not interpreted, remove our cruft */
905 if (ret < 0) {
906 strbuf_setlen(buf, used);
907 return len;
908 }
909 strbuf_reset(buf);
910 strbuf_addbuf(buf, &tmp);
911 strbuf_release(&tmp);
912 /* tweak for size of {-N} versus expanded ref name */
913 return ret - used + len;
914 }
915
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800916 cp = strchr(name, '@');
917 if (!cp)
918 return -1;
919 tmp_len = upstream_mark(cp, namelen - (cp - name));
920 if (!tmp_len)
921 return -1;
922 len = cp + tmp_len - name;
923 cp = xstrndup(name, cp - name);
924 upstream = branch_get(*cp ? cp : NULL);
925 if (!upstream
926 || !upstream->merge
927 || !upstream->merge[0]->dst)
928 return error("No upstream branch found for '%s'", cp);
929 free(cp);
930 cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
931 strbuf_reset(buf);
932 strbuf_addstr(buf, cp);
933 free(cp);
934 return len;
935}
936
937/*
Junio C Hamano9938af62005-08-03 22:15:49 -0700938 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
939 * notably "xyz^" for "parent of xyz"
940 */
941int get_sha1(const char *name, unsigned char *sha1)
942{
Clément Poulain573285e2010-06-09 19:02:06 +0200943 struct object_context unused;
944 return get_sha1_with_context(name, sha1, &unused);
Martin Koeglera0cd87a2007-04-23 22:55:05 +0200945}
946
Matthieu Moy009fee42009-12-07 11:10:50 +0100947/* Must be called only when object_name:filename doesn't exist. */
948static void diagnose_invalid_sha1_path(const char *prefix,
949 const char *filename,
950 const unsigned char *tree_sha1,
951 const char *object_name)
952{
953 struct stat st;
954 unsigned char sha1[20];
955 unsigned mode;
956
957 if (!prefix)
958 prefix = "";
959
960 if (!lstat(filename, &st))
961 die("Path '%s' exists on disk, but not in '%s'.",
962 filename, object_name);
963 if (errno == ENOENT || errno == ENOTDIR) {
964 char *fullname = xmalloc(strlen(filename)
965 + strlen(prefix) + 1);
966 strcpy(fullname, prefix);
967 strcat(fullname, filename);
968
969 if (!get_tree_entry(tree_sha1, fullname,
970 sha1, &mode)) {
971 die("Path '%s' exists, but not '%s'.\n"
972 "Did you mean '%s:%s'?",
973 fullname,
974 filename,
975 object_name,
976 fullname);
977 }
978 die("Path '%s' does not exist in '%s'",
979 filename, object_name);
980 }
981}
982
983/* Must be called only when :stage:filename doesn't exist. */
984static void diagnose_invalid_index_path(int stage,
985 const char *prefix,
986 const char *filename)
987{
988 struct stat st;
989 struct cache_entry *ce;
990 int pos;
991 unsigned namelen = strlen(filename);
992 unsigned fullnamelen;
993 char *fullname;
994
995 if (!prefix)
996 prefix = "";
997
998 /* Wrong stage number? */
999 pos = cache_name_pos(filename, namelen);
1000 if (pos < 0)
1001 pos = -pos - 1;
Markus Heidelberg77e84662010-02-28 16:49:15 +01001002 if (pos < active_nr) {
1003 ce = active_cache[pos];
1004 if (ce_namelen(ce) == namelen &&
1005 !memcmp(ce->name, filename, namelen))
1006 die("Path '%s' is in the index, but not at stage %d.\n"
1007 "Did you mean ':%d:%s'?",
1008 filename, stage,
1009 ce_stage(ce), filename);
1010 }
Matthieu Moy009fee42009-12-07 11:10:50 +01001011
1012 /* Confusion between relative and absolute filenames? */
1013 fullnamelen = namelen + strlen(prefix);
1014 fullname = xmalloc(fullnamelen + 1);
1015 strcpy(fullname, prefix);
1016 strcat(fullname, filename);
1017 pos = cache_name_pos(fullname, fullnamelen);
1018 if (pos < 0)
1019 pos = -pos - 1;
Markus Heidelberg77e84662010-02-28 16:49:15 +01001020 if (pos < active_nr) {
1021 ce = active_cache[pos];
1022 if (ce_namelen(ce) == fullnamelen &&
1023 !memcmp(ce->name, fullname, fullnamelen))
1024 die("Path '%s' is in the index, but not '%s'.\n"
1025 "Did you mean ':%d:%s'?",
1026 fullname, filename,
1027 ce_stage(ce), fullname);
1028 }
Matthieu Moy009fee42009-12-07 11:10:50 +01001029
1030 if (!lstat(filename, &st))
1031 die("Path '%s' exists on disk, but not in the index.", filename);
1032 if (errno == ENOENT || errno == ENOTDIR)
1033 die("Path '%s' does not exist (neither on disk nor in the index).",
1034 filename);
1035
1036 free(fullname);
1037}
1038
1039
1040int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode, int gently, const char *prefix)
Martin Koeglera0cd87a2007-04-23 22:55:05 +02001041{
Clément Poulain573285e2010-06-09 19:02:06 +02001042 struct object_context oc;
1043 int ret;
1044 ret = get_sha1_with_context_1(name, sha1, &oc, gently, prefix);
1045 *mode = oc.mode;
1046 return ret;
1047}
1048
1049int get_sha1_with_context_1(const char *name, unsigned char *sha1,
1050 struct object_context *oc,
1051 int gently, const char *prefix)
1052{
Martin Koeglera0cd87a2007-04-23 22:55:05 +02001053 int ret, bracket_depth;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001054 int namelen = strlen(name);
1055 const char *cp;
Linus Torvalds51196022006-04-18 16:45:16 -07001056
Clément Poulain573285e2010-06-09 19:02:06 +02001057 memset(oc, 0, sizeof(*oc));
1058 oc->mode = S_IFINVALID;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001059 ret = get_sha1_1(name, namelen, sha1);
1060 if (!ret)
1061 return ret;
1062 /* sha1:path --> object name of path in ent sha1
1063 * :path -> object name of path in index
1064 * :[0-3]:path -> object name of path in index at stage
Matthieu Moy95ad6d22010-09-24 18:43:59 +02001065 * :/foo -> recent commit matching foo
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001066 */
1067 if (name[0] == ':') {
1068 int stage = 0;
1069 struct cache_entry *ce;
1070 int pos;
Johannes Schindelin28a4d942007-02-24 03:08:20 +01001071 if (namelen > 2 && name[1] == '/')
1072 return get_sha1_oneline(name + 2, sha1);
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001073 if (namelen < 3 ||
1074 name[2] != ':' ||
1075 name[1] < '0' || '3' < name[1])
1076 cp = name + 1;
1077 else {
1078 stage = name[1] - '0';
1079 cp = name + 3;
Linus Torvalds51196022006-04-18 16:45:16 -07001080 }
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001081 namelen = namelen - (cp - name);
Clément Poulain573285e2010-06-09 19:02:06 +02001082
1083 strncpy(oc->path, cp,
1084 sizeof(oc->path));
1085 oc->path[sizeof(oc->path)-1] = '\0';
1086
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001087 if (!active_cache)
1088 read_cache();
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001089 pos = cache_name_pos(cp, namelen);
1090 if (pos < 0)
1091 pos = -pos - 1;
1092 while (pos < active_nr) {
1093 ce = active_cache[pos];
1094 if (ce_namelen(ce) != namelen ||
1095 memcmp(ce->name, cp, namelen))
1096 break;
1097 if (ce_stage(ce) == stage) {
Shawn Pearcee7024962006-08-23 02:49:00 -04001098 hashcpy(sha1, ce->sha1);
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001099 return 0;
1100 }
Junio C Hamanoe7cef452006-05-08 15:44:06 -07001101 pos++;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001102 }
Matthieu Moy009fee42009-12-07 11:10:50 +01001103 if (!gently)
1104 diagnose_invalid_index_path(stage, prefix, cp);
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001105 return -1;
1106 }
Shawn Pearcecce91a22006-05-19 03:29:43 -04001107 for (cp = name, bracket_depth = 0; *cp; cp++) {
1108 if (*cp == '{')
1109 bracket_depth++;
1110 else if (bracket_depth && *cp == '}')
1111 bracket_depth--;
1112 else if (!bracket_depth && *cp == ':')
1113 break;
1114 }
1115 if (*cp == ':') {
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001116 unsigned char tree_sha1[20];
Matthieu Moy009fee42009-12-07 11:10:50 +01001117 char *object_name = NULL;
1118 if (!gently) {
1119 object_name = xmalloc(cp-name+1);
1120 strncpy(object_name, name, cp-name);
1121 object_name[cp-name] = '\0';
1122 }
1123 if (!get_sha1_1(name, cp-name, tree_sha1)) {
1124 const char *filename = cp+1;
Clément Poulain573285e2010-06-09 19:02:06 +02001125 ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
Matthieu Moy009fee42009-12-07 11:10:50 +01001126 if (!gently) {
1127 diagnose_invalid_sha1_path(prefix, filename,
1128 tree_sha1, object_name);
1129 free(object_name);
1130 }
Clément Poulain573285e2010-06-09 19:02:06 +02001131 hashcpy(oc->tree, tree_sha1);
1132 strncpy(oc->path, filename,
1133 sizeof(oc->path));
1134 oc->path[sizeof(oc->path)-1] = '\0';
1135
Matthieu Moy009fee42009-12-07 11:10:50 +01001136 return ret;
1137 } else {
1138 if (!gently)
1139 die("Invalid object name '%s'.", object_name);
1140 }
Linus Torvalds51196022006-04-18 16:45:16 -07001141 }
1142 return ret;
Junio C Hamano9938af62005-08-03 22:15:49 -07001143}