blob: 709ff2eee64cf106191ad274bede82a95d00e2a3 [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
Nguyễn Thái Ngọc Duy32574b62010-12-13 10:01:15 +070010static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *);
11
Junio C Hamano9938af62005-08-03 22:15:49 -070012static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
13{
Junio C Hamano99a19b42005-10-02 21:40:51 -070014 struct alternate_object_database *alt;
Junio C Hamano9938af62005-08-03 22:15:49 -070015 char hex[40];
Junio C Hamano99a19b42005-10-02 21:40:51 -070016 int found = 0;
17 static struct alternate_object_database *fakeent;
Junio C Hamano9938af62005-08-03 22:15:49 -070018
Junio C Hamano99a19b42005-10-02 21:40:51 -070019 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;
29
Junio C Hamano9938af62005-08-03 22:15:49 -070030 sprintf(hex, "%.2s", name);
Junio C Hamano99a19b42005-10-02 21:40:51 -070031 for (alt = fakeent; alt && found < 2; alt = alt->next) {
Junio C Hamano9938af62005-08-03 22:15:49 -070032 struct dirent *de;
Junio C Hamano99a19b42005-10-02 21:40:51 -070033 DIR *dir;
34 sprintf(alt->name, "%.2s/", name);
35 dir = opendir(alt->base);
36 if (!dir)
37 continue;
Junio C Hamano9938af62005-08-03 22:15:49 -070038 while ((de = readdir(dir)) != NULL) {
39 if (strlen(de->d_name) != 38)
40 continue;
Junio C Hamano99a19b42005-10-02 21:40:51 -070041 if (memcmp(de->d_name, name + 2, len - 2))
Junio C Hamano9938af62005-08-03 22:15:49 -070042 continue;
Junio C Hamano99a19b42005-10-02 21:40:51 -070043 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;
Junio C Hamano9938af62005-08-03 22:15:49 -070049 break;
Junio C Hamano99a19b42005-10-02 21:40:51 -070050 }
Junio C Hamano9938af62005-08-03 22:15:49 -070051 }
52 closedir(dir);
53 }
54 if (found == 1)
55 return get_sha1_hex(hex, sha1) == 0;
Junio C Hamano99a19b42005-10-02 21:40:51 -070056 return found;
Junio C Hamano9938af62005-08-03 22:15:49 -070057}
58
59static 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
74static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
75{
76 struct packed_git *p;
Nicolas Pitred72308e2007-04-04 16:49:04 -040077 const unsigned char *found_sha1 = NULL;
Junio C Hamano99a19b42005-10-02 21:40:51 -070078 int found = 0;
Junio C Hamano9938af62005-08-03 22:15:49 -070079
80 prepare_packed_git();
Junio C Hamano99a19b42005-10-02 21:40:51 -070081 for (p = packed_git; p && found < 2; p = p->next) {
James Bowes10558802007-05-29 19:29:51 -040082 uint32_t num, last;
83 uint32_t first = 0;
84 open_pack_index(p);
85 num = p->num_objects;
86 last = num;
Junio C Hamano9938af62005-08-03 22:15:49 -070087 while (first < last) {
Shawn O. Pearce326bf392007-03-06 20:44:19 -050088 uint32_t mid = (first + last) / 2;
Nicolas Pitred72308e2007-04-04 16:49:04 -040089 const unsigned char *now;
Junio C Hamano9938af62005-08-03 22:15:49 -070090 int cmp;
91
Nicolas Pitred72308e2007-04-04 16:49:04 -040092 now = nth_packed_object_sha1(p, mid);
David Rientjesa89fccd2006-08-17 11:54:57 -070093 cmp = hashcmp(match, now);
Junio C Hamano9938af62005-08-03 22:15:49 -070094 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) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400105 const unsigned char *now, *next;
106 now = nth_packed_object_sha1(p, first);
Junio C Hamano9938af62005-08-03 22:15:49 -0700107 if (match_sha(len, match, now)) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400108 next = nth_packed_object_sha1(p, first+1);
109 if (!next|| !match_sha(len, match, next)) {
Junio C Hamano0bc45892005-10-02 21:40:51 -0700110 /* unique within this pack */
111 if (!found) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400112 found_sha1 = now;
Junio C Hamano0bc45892005-10-02 21:40:51 -0700113 found++;
114 }
David Rientjesa89fccd2006-08-17 11:54:57 -0700115 else if (hashcmp(found_sha1, now)) {
Junio C Hamano0bc45892005-10-02 21:40:51 -0700116 found = 2;
117 break;
118 }
Junio C Hamano99a19b42005-10-02 21:40:51 -0700119 }
Junio C Hamano0bc45892005-10-02 21:40:51 -0700120 else {
121 /* not even unique within this pack */
Junio C Hamano99a19b42005-10-02 21:40:51 -0700122 found = 2;
123 break;
Junio C Hamano9938af62005-08-03 22:15:49 -0700124 }
125 }
126 }
127 }
Junio C Hamano99a19b42005-10-02 21:40:51 -0700128 if (found == 1)
Shawn Pearcee7024962006-08-23 02:49:00 -0400129 hashcpy(sha1, found_sha1);
Junio C Hamano99a19b42005-10-02 21:40:51 -0700130 return found;
131}
132
Junio C Hamano013f2762005-10-11 15:22:48 -0700133#define SHORT_NAME_NOT_FOUND (-1)
134#define SHORT_NAME_AMBIGUOUS (-2)
135
Junio C Hamano99a19b42005-10-02 21:40:51 -0700136static 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
Shawn O. Pearce693d2bc2007-05-26 01:25:11 -0400142 prepare_alt_odb();
Junio C Hamano99a19b42005-10-02 21:40:51 -0700143 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)
Junio C Hamano013f2762005-10-11 15:22:48 -0700146 return SHORT_NAME_NOT_FOUND;
Junio C Hamano99a19b42005-10-02 21:40:51 -0700147 if (1 < has_unpacked || 1 < has_packed)
Junio C Hamano013f2762005-10-11 15:22:48 -0700148 return SHORT_NAME_AMBIGUOUS;
Junio C Hamano99a19b42005-10-02 21:40:51 -0700149 if (has_unpacked != has_packed) {
Shawn Pearcee7024962006-08-23 02:49:00 -0400150 hashcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1));
Junio C Hamano99a19b42005-10-02 21:40:51 -0700151 return 0;
152 }
153 /* Both have unique ones -- do they match? */
David Rientjesa89fccd2006-08-17 11:54:57 -0700154 if (hashcmp(packed_sha1, unpacked_sha1))
Uwe Zeisbergere974c9a2006-01-26 12:26:15 +0100155 return SHORT_NAME_AMBIGUOUS;
Shawn Pearcee7024962006-08-23 02:49:00 -0400156 hashcpy(sha1, packed_sha1);
Junio C Hamano9938af62005-08-03 22:15:49 -0700157 return 0;
158}
159
Junio C Hamano013f2762005-10-11 15:22:48 -0700160static int get_short_sha1(const char *name, int len, unsigned char *sha1,
161 int quietly)
Junio C Hamano9938af62005-08-03 22:15:49 -0700162{
Junio C Hamano013f2762005-10-11 15:22:48 -0700163 int i, status;
Junio C Hamano9938af62005-08-03 22:15:49 -0700164 char canonical[40];
165 unsigned char res[20];
166
pclouds@gmail.com8a831572006-10-19 08:34:41 +0700167 if (len < MINIMUM_ABBREV || len > 40)
Linus Torvaldsaf61c6e2005-09-19 15:16:03 -0700168 return -1;
Junio C Hamanoa8e0d162006-08-23 13:57:23 -0700169 hashclr(res);
Junio C Hamano9938af62005-08-03 22:15:49 -0700170 memset(canonical, 'x', 40);
Linus Torvaldsaf61c6e2005-09-19 15:16:03 -0700171 for (i = 0; i < len ;i++) {
Junio C Hamano9938af62005-08-03 22:15:49 -0700172 unsigned char c = name[i];
173 unsigned char val;
Junio C Hamano9938af62005-08-03 22:15:49 -0700174 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 }
Junio C Hamano99a19b42005-10-02 21:40:51 -0700189
Junio C Hamano013f2762005-10-11 15:22:48 -0700190 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
196const char *find_unique_abbrev(const unsigned char *sha1, int len)
197{
Junio C Hamanob66fde92008-03-01 23:35:32 -0800198 int status, exists;
Junio C Hamano013f2762005-10-11 15:22:48 -0700199 static char hex[41];
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800200
Junio C Hamanob66fde92008-03-01 23:35:32 -0800201 exists = has_sha1_file(sha1);
Junio C Hamano013f2762005-10-11 15:22:48 -0700202 memcpy(hex, sha1_to_hex(sha1), 40);
Junio C Hamano02c5cba2006-08-09 13:17:04 -0700203 if (len == 40 || !len)
Junio C Hamano47dd0d52005-12-13 17:21:41 -0800204 return hex;
Junio C Hamano013f2762005-10-11 15:22:48 -0700205 while (len < 40) {
206 unsigned char sha1_ret[20];
207 status = get_short_sha1(hex, len, sha1_ret, 1);
Junio C Hamanob66fde92008-03-01 23:35:32 -0800208 if (exists
209 ? !status
210 : status == SHORT_NAME_NOT_FOUND) {
Junio C Hamano72a5b562010-10-28 14:39:13 -0700211 int cut_at = len + unique_abbrev_extra_length;
212 cut_at = (cut_at < 40) ? cut_at : 40;
213 hex[cut_at] = 0;
Junio C Hamano013f2762005-10-11 15:22:48 -0700214 return hex;
215 }
Junio C Hamano013f2762005-10-11 15:22:48 -0700216 len++;
217 }
Junio C Hamanob66fde92008-03-01 23:35:32 -0800218 return hex;
Junio C Hamano9938af62005-08-03 22:15:49 -0700219}
220
Junio C Hamano6677c462005-12-15 12:54:00 -0800221static int ambiguous_path(const char *path, int len)
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700222{
223 int slash = 1;
Junio C Hamano6677c462005-12-15 12:54:00 -0800224 int cnt;
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700225
Junio C Hamano6677c462005-12-15 12:54:00 -0800226 for (cnt = 0; cnt < len; cnt++) {
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700227 switch (*path++) {
228 case '\0':
229 break;
230 case '/':
231 if (slash)
232 break;
233 slash = 1;
234 continue;
235 case '.':
236 continue;
237 default:
238 slash = 0;
239 continue;
240 }
Junio C Hamanoc054d642005-12-17 00:00:50 -0800241 break;
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700242 }
Junio C Hamano6677c462005-12-15 12:54:00 -0800243 return slash;
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700244}
245
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100246/*
247 * *string and *len will only be substituted, and *string returned (for
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800248 * later free()ing) if the string passed in is a magic short-hand form
249 * to name a branch.
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100250 */
Junio C Hamano431b1962009-03-21 12:51:34 -0700251static char *substitute_branch_name(const char **string, int *len)
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100252{
253 struct strbuf buf = STRBUF_INIT;
Junio C Hamano431b1962009-03-21 12:51:34 -0700254 int ret = interpret_branch_name(*string, &buf);
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100255
256 if (ret == *len) {
257 size_t size;
258 *string = strbuf_detach(&buf, &size);
259 *len = size;
260 return (char *)*string;
261 }
262
263 return NULL;
264}
265
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800266int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
Junio C Hamano9938af62005-08-03 22:15:49 -0700267{
Junio C Hamano431b1962009-03-21 12:51:34 -0700268 char *last_branch = substitute_branch_name(&str, &len);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800269 const char **p, *r;
270 int refs_found = 0;
271
272 *ref = NULL;
Steffen Prohaska79803322007-11-11 15:01:46 +0100273 for (p = ref_rev_parse_rules; *p; p++) {
Alex Riesen94cc3552008-10-26 23:07:24 +0100274 char fullref[PATH_MAX];
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800275 unsigned char sha1_from_ref[20];
276 unsigned char *this_result;
Junio C Hamano057e7132009-02-08 23:52:01 -0800277 int flag;
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800278
279 this_result = refs_found ? sha1_from_ref : sha1;
Alex Riesen94cc3552008-10-26 23:07:24 +0100280 mksnpath(fullref, sizeof(fullref), *p, len, str);
Junio C Hamano057e7132009-02-08 23:52:01 -0800281 r = resolve_ref(fullref, this_result, 1, &flag);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800282 if (r) {
283 if (!refs_found++)
284 *ref = xstrdup(r);
285 if (!warn_ambiguous_refs)
286 break;
Jeff King003c6ab2010-02-16 02:03:16 -0500287 } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD"))
Junio C Hamano057e7132009-02-08 23:52:01 -0800288 warning("ignoring dangling symref %s.", fullref);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800289 }
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100290 free(last_branch);
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800291 return refs_found;
292}
293
Johannes Schindelineb3a4822007-02-09 01:28:23 +0100294int dwim_log(const char *str, int len, unsigned char *sha1, char **log)
Nicolas Pitref2eba662007-02-03 21:49:16 -0500295{
Junio C Hamano431b1962009-03-21 12:51:34 -0700296 char *last_branch = substitute_branch_name(&str, &len);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500297 const char **p;
298 int logs_found = 0;
299
300 *log = NULL;
Steffen Prohaska79803322007-11-11 15:01:46 +0100301 for (p = ref_rev_parse_rules; *p; p++) {
Nicolas Pitref2eba662007-02-03 21:49:16 -0500302 struct stat st;
Junio C Hamano40facde2007-02-08 23:24:51 -0800303 unsigned char hash[20];
304 char path[PATH_MAX];
305 const char *ref, *it;
306
Alex Riesen94cc3552008-10-26 23:07:24 +0100307 mksnpath(path, sizeof(path), *p, len, str);
Junio C Hamano0c4cd7f2008-07-23 17:22:58 -0700308 ref = resolve_ref(path, hash, 1, NULL);
Junio C Hamano40facde2007-02-08 23:24:51 -0800309 if (!ref)
310 continue;
Nicolas Pitref2eba662007-02-03 21:49:16 -0500311 if (!stat(git_path("logs/%s", path), &st) &&
Junio C Hamano40facde2007-02-08 23:24:51 -0800312 S_ISREG(st.st_mode))
313 it = path;
314 else if (strcmp(ref, path) &&
315 !stat(git_path("logs/%s", ref), &st) &&
316 S_ISREG(st.st_mode))
317 it = ref;
318 else
319 continue;
320 if (!logs_found++) {
321 *log = xstrdup(it);
322 hashcpy(sha1, hash);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500323 }
Junio C Hamano40facde2007-02-08 23:24:51 -0800324 if (!warn_ambiguous_refs)
325 break;
Nicolas Pitref2eba662007-02-03 21:49:16 -0500326 }
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100327 free(last_branch);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500328 return logs_found;
329}
330
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800331static inline int upstream_mark(const char *string, int len)
332{
333 const char *suffix[] = { "@{upstream}", "@{u}" };
334 int i;
335
336 for (i = 0; i < ARRAY_SIZE(suffix); i++) {
337 int suffix_len = strlen(suffix[i]);
338 if (suffix_len <= len
339 && !memcmp(string, suffix[i], suffix_len))
340 return suffix_len;
341 }
342 return 0;
343}
344
Thomas Rastd18ba222009-01-17 17:09:55 +0100345static int get_sha1_1(const char *name, int len, unsigned char *sha1);
346
Junio C Hamanoe86eb662007-01-19 01:15:15 -0800347static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
348{
Jon Seymoureedce782010-08-24 14:52:43 +1000349 static const char *warn_msg = "refname '%.*s' is ambiguous.";
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700350 char *real_ref = NULL;
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700351 int refs_found = 0;
352 int at, reflog_len;
Junio C Hamano9938af62005-08-03 22:15:49 -0700353
Linus Torvalds3c3852e2005-08-13 11:05:25 -0700354 if (len == 40 && !get_sha1_hex(str, sha1))
Junio C Hamano9938af62005-08-03 22:15:49 -0700355 return 0;
356
Thomas Rastd18ba222009-01-17 17:09:55 +0100357 /* basic@{time or number or -number} format to query ref-log */
Junio C Hamano694500e2006-10-23 21:15:34 -0700358 reflog_len = at = 0;
Johannes Schindelinf2654582009-01-28 00:07:46 +0100359 if (len && str[len-1] == '}') {
Johannes Schindelinaa9c55b2009-01-17 19:08:12 +0100360 for (at = len-2; at >= 0; at--) {
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700361 if (str[at] == '@' && str[at+1] == '{') {
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800362 if (!upstream_mark(str + at, len - at)) {
Johannes Schindelin28fb8432009-09-10 17:25:57 +0200363 reflog_len = (len-1) - (at+2);
364 len = at;
365 }
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700366 break;
367 }
Shawn Pearced556fae2006-05-17 05:56:09 -0400368 }
369 }
370
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700371 /* Accept only unambiguous ref paths. */
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500372 if (len && ambiguous_path(str, len))
Linus Torvaldsaf13cdf2005-10-28 12:41:49 -0700373 return -1;
374
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500375 if (!len && reflog_len) {
Thomas Rastd18ba222009-01-17 17:09:55 +0100376 struct strbuf buf = STRBUF_INIT;
377 int ret;
378 /* try the @{-N} syntax for n-th checkout */
Junio C Hamano431b1962009-03-21 12:51:34 -0700379 ret = interpret_branch_name(str+at, &buf);
Thomas Rastd18ba222009-01-17 17:09:55 +0100380 if (ret > 0) {
381 /* substitute this branch name and restart */
382 return get_sha1_1(buf.buf, buf.len, sha1);
383 } else if (ret == 0) {
384 return -1;
385 }
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500386 /* allow "@{...}" to mean the current branch reflog */
387 refs_found = dwim_ref("HEAD", 4, sha1, &real_ref);
Nicolas Pitref2eba662007-02-03 21:49:16 -0500388 } else if (reflog_len)
389 refs_found = dwim_log(str, len, sha1, &real_ref);
390 else
Nicolas Pitre11cf8802007-02-01 17:29:33 -0500391 refs_found = dwim_ref(str, len, sha1, &real_ref);
Shawn Pearced556fae2006-05-17 05:56:09 -0400392
393 if (!refs_found)
394 return -1;
395
396 if (warn_ambiguous_refs && refs_found > 1)
Jon Seymoureedce782010-08-24 14:52:43 +1000397 warning(warn_msg, len, str);
Shawn Pearced556fae2006-05-17 05:56:09 -0400398
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700399 if (reflog_len) {
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700400 int nth, i;
401 unsigned long at_time;
Junio C Hamano16d7cc92007-01-19 01:19:05 -0800402 unsigned long co_time;
403 int co_tz, co_cnt;
404
Jeff King12a258c2010-01-28 04:56:43 -0500405 /* a @{-N} placed anywhere except the start is an error */
406 if (str[at+2] == '-')
407 return -1;
408
Nicolas Pitrefe558512007-02-01 12:33:23 -0500409 /* Is it asking for N-th entry, or approxidate? */
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700410 for (i = nth = 0; 0 <= nth && i < reflog_len; i++) {
411 char ch = str[at+2+i];
412 if ('0' <= ch && ch <= '9')
413 nth = nth * 10 + ch - '0';
414 else
415 nth = -1;
416 }
Shawn O. Pearceea360dd2008-08-21 08:40:44 -0700417 if (100000000 <= nth) {
418 at_time = nth;
419 nth = -1;
420 } else if (0 <= nth)
Junio C Hamanoab2a1a32006-10-05 23:16:15 -0700421 at_time = 0;
Jeff King861f00e2008-04-30 00:13:58 -0400422 else {
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800423 int errors = 0;
Jeff King861f00e2008-04-30 00:13:58 -0400424 char *tmp = xstrndup(str + at + 2, reflog_len);
Junio C Hamano93cfa7c2010-01-26 11:58:00 -0800425 at_time = approxidate_careful(tmp, &errors);
Jeff King861f00e2008-04-30 00:13:58 -0400426 free(tmp);
Junio C Hamanoa5e10ac2010-01-27 10:53:09 -0800427 if (errors)
428 return -1;
Jeff King861f00e2008-04-30 00:13:58 -0400429 }
Junio C Hamano16d7cc92007-01-19 01:19:05 -0800430 if (read_ref_at(real_ref, at_time, nth, sha1, NULL,
431 &co_time, &co_tz, &co_cnt)) {
432 if (at_time)
Jon Seymoureedce782010-08-24 14:52:43 +1000433 warning("Log for '%.*s' only goes "
434 "back to %s.", len, str,
Junio C Hamano73013af2007-07-13 23:14:52 -0700435 show_date(co_time, co_tz, DATE_RFC2822));
Jon Seymoure6eedc32010-08-24 14:52:42 +1000436 else {
437 free(real_ref);
438 die("Log for '%.*s' only has %d entries.",
439 len, str, co_cnt);
440 }
Junio C Hamano16d7cc92007-01-19 01:19:05 -0800441 }
Shawn Pearced556fae2006-05-17 05:56:09 -0400442 }
443
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700444 free(real_ref);
Shawn Pearced556fae2006-05-17 05:56:09 -0400445 return 0;
Junio C Hamano9938af62005-08-03 22:15:49 -0700446}
447
Junio C Hamano9938af62005-08-03 22:15:49 -0700448static int get_parent(const char *name, int len,
449 unsigned char *result, int idx)
450{
451 unsigned char sha1[20];
452 int ret = get_sha1_1(name, len, sha1);
453 struct commit *commit;
454 struct commit_list *p;
455
456 if (ret)
457 return ret;
458 commit = lookup_commit_reference(sha1);
459 if (!commit)
460 return -1;
461 if (parse_commit(commit))
462 return -1;
463 if (!idx) {
Shawn Pearcee7024962006-08-23 02:49:00 -0400464 hashcpy(result, commit->object.sha1);
Junio C Hamano9938af62005-08-03 22:15:49 -0700465 return 0;
466 }
467 p = commit->parents;
468 while (p) {
469 if (!--idx) {
Shawn Pearcee7024962006-08-23 02:49:00 -0400470 hashcpy(result, p->item->object.sha1);
Junio C Hamano9938af62005-08-03 22:15:49 -0700471 return 0;
472 }
473 p = p->next;
474 }
475 return -1;
476}
477
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700478static int get_nth_ancestor(const char *name, int len,
479 unsigned char *result, int generation)
480{
481 unsigned char sha1[20];
Linus Torvalds621ff672008-03-14 11:49:40 -0700482 struct commit *commit;
483 int ret;
484
485 ret = get_sha1_1(name, len, sha1);
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700486 if (ret)
487 return ret;
Linus Torvalds621ff672008-03-14 11:49:40 -0700488 commit = lookup_commit_reference(sha1);
489 if (!commit)
490 return -1;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700491
492 while (generation--) {
Linus Torvalds621ff672008-03-14 11:49:40 -0700493 if (parse_commit(commit) || !commit->parents)
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700494 return -1;
Linus Torvalds621ff672008-03-14 11:49:40 -0700495 commit = commit->parents->item;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700496 }
Linus Torvalds621ff672008-03-14 11:49:40 -0700497 hashcpy(result, commit->object.sha1);
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700498 return 0;
499}
500
Junio C Hamano81776312007-12-24 00:51:01 -0800501struct object *peel_to_type(const char *name, int namelen,
502 struct object *o, enum object_type expected_type)
503{
504 if (name && !namelen)
505 namelen = strlen(name);
506 if (!o) {
507 unsigned char sha1[20];
508 if (get_sha1_1(name, namelen, sha1))
509 return NULL;
510 o = parse_object(sha1);
511 }
512 while (1) {
513 if (!o || (!o->parsed && !parse_object(o->sha1)))
514 return NULL;
515 if (o->type == expected_type)
516 return o;
517 if (o->type == OBJ_TAG)
518 o = ((struct tag*) o)->tagged;
519 else if (o->type == OBJ_COMMIT)
520 o = &(((struct commit *) o)->tree->object);
521 else {
522 if (name)
523 error("%.*s: expected %s type, but the object "
524 "dereferences to %s type",
525 namelen, name, typename(expected_type),
526 typename(o->type));
527 return NULL;
528 }
529 }
530}
531
Junio C Hamano5385f522005-10-13 18:57:40 -0700532static int peel_onion(const char *name, int len, unsigned char *sha1)
533{
534 unsigned char outer[20];
535 const char *sp;
Linus Torvalds885a86a2006-06-14 16:45:13 -0700536 unsigned int expected_type = 0;
Junio C Hamano5385f522005-10-13 18:57:40 -0700537 struct object *o;
538
539 /*
540 * "ref^{type}" dereferences ref repeatedly until you cannot
541 * dereference anymore, or you get an object of given type,
542 * whichever comes first. "ref^{}" means just dereference
543 * tags until you get a non-tag. "ref^0" is a shorthand for
544 * "ref^{commit}". "commit^{tree}" could be used to find the
545 * top-level tree of the given commit.
546 */
547 if (len < 4 || name[len-1] != '}')
548 return -1;
549
550 for (sp = name + len - 1; name <= sp; sp--) {
551 int ch = *sp;
552 if (ch == '{' && name < sp && sp[-1] == '^')
553 break;
554 }
555 if (sp <= name)
556 return -1;
557
558 sp++; /* beginning of type name, or closing brace for empty */
559 if (!strncmp(commit_type, sp, 6) && sp[6] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700560 expected_type = OBJ_COMMIT;
Junio C Hamano5385f522005-10-13 18:57:40 -0700561 else if (!strncmp(tree_type, sp, 4) && sp[4] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700562 expected_type = OBJ_TREE;
Junio C Hamano5385f522005-10-13 18:57:40 -0700563 else if (!strncmp(blob_type, sp, 4) && sp[4] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700564 expected_type = OBJ_BLOB;
Junio C Hamano5385f522005-10-13 18:57:40 -0700565 else if (sp[0] == '}')
Linus Torvalds19746322006-07-11 20:45:31 -0700566 expected_type = OBJ_NONE;
Nguyễn Thái Ngọc Duy32574b62010-12-13 10:01:15 +0700567 else if (sp[0] == '/')
568 expected_type = OBJ_COMMIT;
Junio C Hamano5385f522005-10-13 18:57:40 -0700569 else
570 return -1;
571
572 if (get_sha1_1(name, sp - name - 2, outer))
573 return -1;
574
575 o = parse_object(outer);
576 if (!o)
577 return -1;
Linus Torvalds885a86a2006-06-14 16:45:13 -0700578 if (!expected_type) {
Junio C Hamano9534f402005-11-02 15:19:13 -0800579 o = deref_tag(o, name, sp - name - 2);
Junio C Hamano6e1c6c12005-10-19 22:48:16 -0700580 if (!o || (!o->parsed && !parse_object(o->sha1)))
581 return -1;
Shawn Pearcee7024962006-08-23 02:49:00 -0400582 hashcpy(sha1, o->sha1);
Nguyễn Thái Ngọc Duy32574b62010-12-13 10:01:15 +0700583 return 0;
Junio C Hamano5385f522005-10-13 18:57:40 -0700584 }
Nguyễn Thái Ngọc Duy32574b62010-12-13 10:01:15 +0700585
586 /*
587 * At this point, the syntax look correct, so
588 * if we do not get the needed object, we should
589 * barf.
590 */
591 o = peel_to_type(name, len, o, expected_type);
592 if (!o)
Junio C Hamano81776312007-12-24 00:51:01 -0800593 return -1;
Nguyễn Thái Ngọc Duy32574b62010-12-13 10:01:15 +0700594
595 hashcpy(sha1, o->sha1);
596 if (sp[0] == '/') {
597 /* "$commit^{/foo}" */
598 char *prefix;
599 int ret;
600 struct commit_list *list = NULL;
601
Nguyễn Thái Ngọc Duy43228422010-12-15 16:02:54 +0700602 /*
603 * $commit^{/}. Some regex implementation may reject.
604 * We don't need regex anyway. '' pattern always matches.
605 */
606 if (sp[1] == '}')
607 return 0;
608
Nguyễn Thái Ngọc Duy32574b62010-12-13 10:01:15 +0700609 prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1));
610 commit_list_insert((struct commit *)o, &list);
611 ret = get_sha1_oneline(prefix, sha1, list);
612 free(prefix);
613 return ret;
Junio C Hamano5385f522005-10-13 18:57:40 -0700614 }
615 return 0;
616}
617
Junio C Hamano7dd45e12006-09-20 16:11:08 -0700618static int get_describe_name(const char *name, int len, unsigned char *sha1)
619{
620 const char *cp;
621
622 for (cp = name + len - 1; name + 2 <= cp; cp--) {
623 char ch = *cp;
624 if (hexval(ch) & ~0377) {
625 /* We must be looking at g in "SOMETHING-g"
626 * for it to be describe output.
627 */
628 if (ch == 'g' && cp[-1] == '-') {
629 cp++;
630 len -= cp - name;
631 return get_short_sha1(cp, len, sha1, 1);
632 }
633 }
634 }
635 return -1;
636}
637
Junio C Hamano9938af62005-08-03 22:15:49 -0700638static int get_sha1_1(const char *name, int len, unsigned char *sha1)
639{
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800640 int ret, has_suffix;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700641 const char *cp;
Junio C Hamano9938af62005-08-03 22:15:49 -0700642
Linus Torvalds621ff672008-03-14 11:49:40 -0700643 /*
644 * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1".
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700645 */
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800646 has_suffix = 0;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700647 for (cp = name + len - 1; name <= cp; cp--) {
648 int ch = *cp;
649 if ('0' <= ch && ch <= '9')
650 continue;
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800651 if (ch == '~' || ch == '^')
652 has_suffix = ch;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700653 break;
654 }
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800655
656 if (has_suffix) {
657 int num = 0;
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700658 int len1 = cp - name;
659 cp++;
660 while (cp < name + len)
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800661 num = num * 10 + *cp++ - '0';
Linus Torvalds621ff672008-03-14 11:49:40 -0700662 if (!num && len1 == len - 1)
663 num = 1;
664 if (has_suffix == '^')
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800665 return get_parent(name, len1, sha1, num);
Junio C Hamano0601dbe2006-02-02 23:48:36 -0800666 /* else if (has_suffix == '~') -- goes without saying */
667 return get_nth_ancestor(name, len1, sha1, num);
Junio C Hamano4f7599a2005-08-21 02:43:54 -0700668 }
669
Junio C Hamano5385f522005-10-13 18:57:40 -0700670 ret = peel_onion(name, len, sha1);
671 if (!ret)
672 return 0;
673
Junio C Hamano9938af62005-08-03 22:15:49 -0700674 ret = get_sha1_basic(name, len, sha1);
675 if (!ret)
676 return 0;
Junio C Hamano7dd45e12006-09-20 16:11:08 -0700677
678 /* It could be describe output that is "SOMETHING-gXXXX" */
679 ret = get_describe_name(name, len, sha1);
680 if (!ret)
681 return 0;
682
Junio C Hamano013f2762005-10-11 15:22:48 -0700683 return get_short_sha1(name, len, sha1, 0);
Junio C Hamano9938af62005-08-03 22:15:49 -0700684}
685
Junio C Hamanof7bff002010-08-02 14:37:06 -0700686/*
687 * This interprets names like ':/Initial revision of "git"' by searching
688 * through history and returning the first commit whose message starts
Junio C Hamano3d045892010-08-12 18:32:49 -0700689 * the given regular expression.
Junio C Hamanof7bff002010-08-02 14:37:06 -0700690 *
691 * For future extension, ':/!' is reserved. If you want to match a message
692 * beginning with a '!', you have to repeat the exclamation mark.
693 */
694#define ONELINE_SEEN (1u<<20)
695
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100696static int handle_one_ref(const char *path,
697 const unsigned char *sha1, int flag, void *cb_data)
698{
699 struct commit_list **list = cb_data;
700 struct object *object = parse_object(sha1);
701 if (!object)
702 return 0;
Martin Koegleraffeef12008-02-18 08:31:54 +0100703 if (object->type == OBJ_TAG) {
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100704 object = deref_tag(object, path, strlen(path));
Martin Koegleraffeef12008-02-18 08:31:54 +0100705 if (!object)
706 return 0;
707 }
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100708 if (object->type != OBJ_COMMIT)
709 return 0;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200710 commit_list_insert_by_date((struct commit *)object, list);
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100711 return 0;
712}
713
Nguyễn Thái Ngọc Duy84baa312010-12-13 10:01:14 +0700714static int get_sha1_oneline(const char *prefix, unsigned char *sha1,
715 struct commit_list *list)
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100716{
Nguyễn Thái Ngọc Duy84baa312010-12-13 10:01:14 +0700717 struct commit_list *backup = NULL, *l;
Junio C Hamano28042db2010-12-12 22:19:00 -0800718 int found = 0;
Linus Torvalds57895102010-04-23 08:20:20 -0700719 regex_t regex;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100720
721 if (prefix[0] == '!') {
722 if (prefix[1] != '!')
723 die ("Invalid search pattern: %s", prefix);
724 prefix++;
725 }
Linus Torvalds57895102010-04-23 08:20:20 -0700726
727 if (regcomp(&regex, prefix, REG_EXTENDED))
728 die("Invalid search pattern: %s", prefix);
729
Nguyễn Thái Ngọc Duy84baa312010-12-13 10:01:14 +0700730 for (l = list; l; l = l->next) {
731 l->item->object.flags |= ONELINE_SEEN;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100732 commit_list_insert(l->item, &backup);
Nguyễn Thái Ngọc Duy84baa312010-12-13 10:01:14 +0700733 }
Jim Meyeringed8ad7e2007-03-11 19:49:08 +0100734 while (list) {
Junio C Hamano28042db2010-12-12 22:19:00 -0800735 char *p, *to_free = NULL;
Linus Torvalds1358e7d2007-03-12 11:30:38 -0700736 struct commit *commit;
Johannes Schindelin364d3e62007-12-03 18:42:39 +0000737 enum object_type type;
738 unsigned long size;
Junio C Hamano28042db2010-12-12 22:19:00 -0800739 int matches;
Jim Meyeringed8ad7e2007-03-11 19:49:08 +0100740
741 commit = pop_most_recent_commit(&list, ONELINE_SEEN);
Martin Koegler283cdbc2008-02-18 21:47:53 +0100742 if (!parse_object(commit->object.sha1))
743 continue;
Johannes Schindelin364d3e62007-12-03 18:42:39 +0000744 if (commit->buffer)
745 p = commit->buffer;
746 else {
747 p = read_sha1_file(commit->object.sha1, &type, &size);
748 if (!p)
749 continue;
Junio C Hamano28042db2010-12-12 22:19:00 -0800750 to_free = p;
Johannes Schindelin364d3e62007-12-03 18:42:39 +0000751 }
Junio C Hamano28042db2010-12-12 22:19:00 -0800752
753 p = strstr(p, "\n\n");
754 matches = p && !regexec(&regex, p + 2, 0, NULL, 0);
755 free(to_free);
756
757 if (matches) {
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100758 hashcpy(sha1, commit->object.sha1);
Junio C Hamano28042db2010-12-12 22:19:00 -0800759 found = 1;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100760 break;
761 }
762 }
Linus Torvalds57895102010-04-23 08:20:20 -0700763 regfree(&regex);
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100764 free_commit_list(list);
765 for (l = backup; l; l = l->next)
766 clear_commit_marks(l->item, ONELINE_SEEN);
Junio C Hamano28042db2010-12-12 22:19:00 -0800767 free_commit_list(backup);
768 return found ? 0 : -1;
Johannes Schindelin28a4d942007-02-24 03:08:20 +0100769}
770
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100771struct grab_nth_branch_switch_cbdata {
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800772 long cnt, alloc;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100773 struct strbuf *buf;
774};
775
776static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1,
777 const char *email, unsigned long timestamp, int tz,
778 const char *message, void *cb_data)
779{
780 struct grab_nth_branch_switch_cbdata *cb = cb_data;
Thomas Rasta884d0c2009-01-17 17:09:54 +0100781 const char *match = NULL, *target = NULL;
782 size_t len;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800783 int nth;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100784
Thomas Rasta884d0c2009-01-17 17:09:54 +0100785 if (!prefixcmp(message, "checkout: moving from ")) {
786 match = message + strlen("checkout: moving from ");
Junio C Hamanod7c03c12009-01-21 00:37:38 -0800787 target = strstr(match, " to ");
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100788 }
789
Junio C Hamanoc8297742009-01-19 16:44:08 -0800790 if (!match || !target)
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100791 return 0;
792
Junio C Hamanod7c03c12009-01-21 00:37:38 -0800793 len = target - match;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800794 nth = cb->cnt++ % cb->alloc;
795 strbuf_reset(&cb->buf[nth]);
796 strbuf_add(&cb->buf[nth], match, len);
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100797 return 0;
798}
799
800/*
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800801 * Parse @{-N} syntax, return the number of characters parsed
802 * if successful; otherwise signal an error with negative value.
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100803 */
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800804static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf)
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100805{
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800806 long nth;
Junio C Hamano39765e52009-01-19 21:58:31 -0800807 int i, retval;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100808 struct grab_nth_branch_switch_cbdata cb;
Thomas Rasta884d0c2009-01-17 17:09:54 +0100809 const char *brace;
810 char *num_end;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100811
812 if (name[0] != '@' || name[1] != '{' || name[2] != '-')
813 return -1;
Thomas Rasta884d0c2009-01-17 17:09:54 +0100814 brace = strchr(name, '}');
815 if (!brace)
816 return -1;
817 nth = strtol(name+3, &num_end, 10);
818 if (num_end != brace)
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100819 return -1;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800820 if (nth <= 0)
821 return -1;
822 cb.alloc = nth;
823 cb.buf = xmalloc(nth * sizeof(struct strbuf));
824 for (i = 0; i < nth; i++)
825 strbuf_init(&cb.buf[i], 20);
826 cb.cnt = 0;
Junio C Hamano39765e52009-01-19 21:58:31 -0800827 retval = 0;
Junio C Hamano101d15e2009-01-19 22:18:29 -0800828 for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb);
829 if (cb.cnt < nth) {
830 cb.cnt = 0;
Junio C Hamano101d15e2009-01-19 22:18:29 -0800831 for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb);
832 }
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800833 if (cb.cnt < nth)
Junio C Hamano39765e52009-01-19 21:58:31 -0800834 goto release_return;
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800835 i = cb.cnt % nth;
836 strbuf_reset(buf);
837 strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len);
Junio C Hamano39765e52009-01-19 21:58:31 -0800838 retval = brace-name+1;
839
840release_return:
Junio C Hamanoc2883e62009-01-19 00:04:25 -0800841 for (i = 0; i < nth; i++)
842 strbuf_release(&cb.buf[i]);
843 free(cb.buf);
Thomas Rasta884d0c2009-01-17 17:09:54 +0100844
Junio C Hamano39765e52009-01-19 21:58:31 -0800845 return retval;
Junio C Hamanoae5a6c32009-01-17 17:09:53 +0100846}
847
Junio C Hamano619a6442009-10-18 12:34:56 -0700848int get_sha1_mb(const char *name, unsigned char *sha1)
849{
850 struct commit *one, *two;
851 struct commit_list *mbs;
852 unsigned char sha1_tmp[20];
853 const char *dots;
854 int st;
855
856 dots = strstr(name, "...");
857 if (!dots)
858 return get_sha1(name, sha1);
859 if (dots == name)
860 st = get_sha1("HEAD", sha1_tmp);
861 else {
862 struct strbuf sb;
863 strbuf_init(&sb, dots - name);
864 strbuf_add(&sb, name, dots - name);
865 st = get_sha1(sb.buf, sha1_tmp);
866 strbuf_release(&sb);
867 }
868 if (st)
869 return st;
870 one = lookup_commit_reference_gently(sha1_tmp, 0);
871 if (!one)
872 return -1;
873
874 if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp))
875 return -1;
876 two = lookup_commit_reference_gently(sha1_tmp, 0);
877 if (!two)
878 return -1;
879 mbs = get_merge_bases(one, two, 1);
880 if (!mbs || mbs->next)
881 st = -1;
882 else {
883 st = 0;
884 hashcpy(sha1, mbs->item->object.sha1);
885 }
886 free_commit_list(mbs);
887 return st;
888}
889
Junio C Hamano9938af62005-08-03 22:15:49 -0700890/*
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800891 * This reads short-hand syntax that not only evaluates to a commit
892 * object name, but also can act as if the end user spelled the name
893 * of the branch from the command line.
894 *
895 * - "@{-N}" finds the name of the Nth previous branch we were on, and
896 * places the name of the branch in the given buf and returns the
897 * number of characters parsed if successful.
898 *
899 * - "<branch>@{upstream}" finds the name of the other ref that
900 * <branch> is configured to merge with (missing <branch> defaults
901 * to the current branch), and places the name of the branch in the
902 * given buf and returns the number of characters parsed if
903 * successful.
904 *
905 * If the input is not of the accepted format, it returns a negative
906 * number to signal an error.
907 *
908 * If the input was ok but there are not N branch switches in the
909 * reflog, it returns 0.
910 */
911int interpret_branch_name(const char *name, struct strbuf *buf)
912{
913 char *cp;
914 struct branch *upstream;
915 int namelen = strlen(name);
916 int len = interpret_nth_prior_checkout(name, buf);
917 int tmp_len;
918
919 if (!len)
920 return len; /* syntax Ok, not enough switches */
Jeff Kingd46a8302010-01-28 04:52:22 -0500921 if (0 < len && len == namelen)
922 return len; /* consumed all */
923 else if (0 < len) {
924 /* we have extra data, which might need further processing */
925 struct strbuf tmp = STRBUF_INIT;
926 int used = buf->len;
927 int ret;
928
929 strbuf_add(buf, name + len, namelen - len);
930 ret = interpret_branch_name(buf->buf, &tmp);
931 /* that data was not interpreted, remove our cruft */
932 if (ret < 0) {
933 strbuf_setlen(buf, used);
934 return len;
935 }
936 strbuf_reset(buf);
937 strbuf_addbuf(buf, &tmp);
938 strbuf_release(&tmp);
939 /* tweak for size of {-N} versus expanded ref name */
940 return ret - used + len;
941 }
942
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800943 cp = strchr(name, '@');
944 if (!cp)
945 return -1;
946 tmp_len = upstream_mark(cp, namelen - (cp - name));
947 if (!tmp_len)
948 return -1;
949 len = cp + tmp_len - name;
950 cp = xstrndup(name, cp - name);
951 upstream = branch_get(*cp ? cp : NULL);
952 if (!upstream
953 || !upstream->merge
954 || !upstream->merge[0]->dst)
955 return error("No upstream branch found for '%s'", cp);
956 free(cp);
957 cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0);
958 strbuf_reset(buf);
959 strbuf_addstr(buf, cp);
960 free(cp);
961 return len;
962}
963
Jonathan Nieder6bab74e2010-11-06 06:46:52 -0500964int strbuf_branchname(struct strbuf *sb, const char *name)
965{
966 int len = strlen(name);
967 if (interpret_branch_name(name, sb) == len)
968 return 0;
969 strbuf_add(sb, name, len);
970 return len;
971}
972
973int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
974{
975 strbuf_branchname(sb, name);
976 if (name[0] == '-')
977 return CHECK_REF_FORMAT_ERROR;
978 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
979 return check_ref_format(sb->buf);
980}
981
Junio C Hamanoae0ba8e2010-01-19 23:17:11 -0800982/*
Junio C Hamano9938af62005-08-03 22:15:49 -0700983 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
984 * notably "xyz^" for "parent of xyz"
985 */
986int get_sha1(const char *name, unsigned char *sha1)
987{
Clément Poulain573285e2010-06-09 19:02:06 +0200988 struct object_context unused;
989 return get_sha1_with_context(name, sha1, &unused);
Martin Koeglera0cd87a2007-04-23 22:55:05 +0200990}
991
Matthieu Moy009fee42009-12-07 11:10:50 +0100992/* Must be called only when object_name:filename doesn't exist. */
993static void diagnose_invalid_sha1_path(const char *prefix,
994 const char *filename,
995 const unsigned char *tree_sha1,
996 const char *object_name)
997{
998 struct stat st;
999 unsigned char sha1[20];
1000 unsigned mode;
1001
1002 if (!prefix)
1003 prefix = "";
1004
1005 if (!lstat(filename, &st))
1006 die("Path '%s' exists on disk, but not in '%s'.",
1007 filename, object_name);
1008 if (errno == ENOENT || errno == ENOTDIR) {
1009 char *fullname = xmalloc(strlen(filename)
1010 + strlen(prefix) + 1);
1011 strcpy(fullname, prefix);
1012 strcat(fullname, filename);
1013
1014 if (!get_tree_entry(tree_sha1, fullname,
1015 sha1, &mode)) {
1016 die("Path '%s' exists, but not '%s'.\n"
1017 "Did you mean '%s:%s'?",
1018 fullname,
1019 filename,
1020 object_name,
1021 fullname);
1022 }
1023 die("Path '%s' does not exist in '%s'",
1024 filename, object_name);
1025 }
1026}
1027
1028/* Must be called only when :stage:filename doesn't exist. */
1029static void diagnose_invalid_index_path(int stage,
1030 const char *prefix,
1031 const char *filename)
1032{
1033 struct stat st;
1034 struct cache_entry *ce;
1035 int pos;
1036 unsigned namelen = strlen(filename);
1037 unsigned fullnamelen;
1038 char *fullname;
1039
1040 if (!prefix)
1041 prefix = "";
1042
1043 /* Wrong stage number? */
1044 pos = cache_name_pos(filename, namelen);
1045 if (pos < 0)
1046 pos = -pos - 1;
Markus Heidelberg77e84662010-02-28 16:49:15 +01001047 if (pos < active_nr) {
1048 ce = active_cache[pos];
1049 if (ce_namelen(ce) == namelen &&
1050 !memcmp(ce->name, filename, namelen))
1051 die("Path '%s' is in the index, but not at stage %d.\n"
1052 "Did you mean ':%d:%s'?",
1053 filename, stage,
1054 ce_stage(ce), filename);
1055 }
Matthieu Moy009fee42009-12-07 11:10:50 +01001056
1057 /* Confusion between relative and absolute filenames? */
1058 fullnamelen = namelen + strlen(prefix);
1059 fullname = xmalloc(fullnamelen + 1);
1060 strcpy(fullname, prefix);
1061 strcat(fullname, filename);
1062 pos = cache_name_pos(fullname, fullnamelen);
1063 if (pos < 0)
1064 pos = -pos - 1;
Markus Heidelberg77e84662010-02-28 16:49:15 +01001065 if (pos < active_nr) {
1066 ce = active_cache[pos];
1067 if (ce_namelen(ce) == fullnamelen &&
1068 !memcmp(ce->name, fullname, fullnamelen))
1069 die("Path '%s' is in the index, but not '%s'.\n"
1070 "Did you mean ':%d:%s'?",
1071 fullname, filename,
1072 ce_stage(ce), fullname);
1073 }
Matthieu Moy009fee42009-12-07 11:10:50 +01001074
1075 if (!lstat(filename, &st))
1076 die("Path '%s' exists on disk, but not in the index.", filename);
1077 if (errno == ENOENT || errno == ENOTDIR)
1078 die("Path '%s' does not exist (neither on disk nor in the index).",
1079 filename);
1080
1081 free(fullname);
1082}
1083
1084
1085int 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 +02001086{
Clément Poulain573285e2010-06-09 19:02:06 +02001087 struct object_context oc;
1088 int ret;
1089 ret = get_sha1_with_context_1(name, sha1, &oc, gently, prefix);
1090 *mode = oc.mode;
1091 return ret;
1092}
1093
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001094static char *resolve_relative_path(const char *rel)
1095{
1096 if (prefixcmp(rel, "./") && prefixcmp(rel, "../"))
1097 return NULL;
1098
1099 if (!startup_info)
1100 die("BUG: startup_info struct is not initialized.");
1101
1102 if (!is_inside_work_tree())
1103 die("relative path syntax can't be used outside working tree.");
1104
1105 /* die() inside prefix_path() if resolved path is outside worktree */
1106 return prefix_path(startup_info->prefix,
1107 startup_info->prefix ? strlen(startup_info->prefix) : 0,
1108 rel);
1109}
1110
Clément Poulain573285e2010-06-09 19:02:06 +02001111int get_sha1_with_context_1(const char *name, unsigned char *sha1,
1112 struct object_context *oc,
1113 int gently, const char *prefix)
1114{
Martin Koeglera0cd87a2007-04-23 22:55:05 +02001115 int ret, bracket_depth;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001116 int namelen = strlen(name);
1117 const char *cp;
Linus Torvalds51196022006-04-18 16:45:16 -07001118
Clément Poulain573285e2010-06-09 19:02:06 +02001119 memset(oc, 0, sizeof(*oc));
1120 oc->mode = S_IFINVALID;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001121 ret = get_sha1_1(name, namelen, sha1);
1122 if (!ret)
1123 return ret;
1124 /* sha1:path --> object name of path in ent sha1
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001125 * :path -> object name of absolute path in index
1126 * :./path -> object name of path relative to cwd in index
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001127 * :[0-3]:path -> object name of path in index at stage
Matthieu Moy95ad6d22010-09-24 18:43:59 +02001128 * :/foo -> recent commit matching foo
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001129 */
1130 if (name[0] == ':') {
1131 int stage = 0;
1132 struct cache_entry *ce;
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001133 char *new_path = NULL;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001134 int pos;
Nguyễn Thái Ngọc Duy84baa312010-12-13 10:01:14 +07001135 if (namelen > 2 && name[1] == '/') {
1136 struct commit_list *list = NULL;
1137 for_each_ref(handle_one_ref, &list);
1138 return get_sha1_oneline(name + 2, sha1, list);
1139 }
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001140 if (namelen < 3 ||
1141 name[2] != ':' ||
1142 name[1] < '0' || '3' < name[1])
1143 cp = name + 1;
1144 else {
1145 stage = name[1] - '0';
1146 cp = name + 3;
Linus Torvalds51196022006-04-18 16:45:16 -07001147 }
Junio C Hamano3d6e0f72010-12-09 13:38:05 -08001148 new_path = resolve_relative_path(cp);
1149 if (!new_path) {
1150 namelen = namelen - (cp - name);
1151 } else {
1152 cp = new_path;
1153 namelen = strlen(cp);
1154 }
Clément Poulain573285e2010-06-09 19:02:06 +02001155
1156 strncpy(oc->path, cp,
1157 sizeof(oc->path));
1158 oc->path[sizeof(oc->path)-1] = '\0';
1159
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001160 if (!active_cache)
1161 read_cache();
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001162 pos = cache_name_pos(cp, namelen);
1163 if (pos < 0)
1164 pos = -pos - 1;
1165 while (pos < active_nr) {
1166 ce = active_cache[pos];
1167 if (ce_namelen(ce) != namelen ||
1168 memcmp(ce->name, cp, namelen))
1169 break;
1170 if (ce_stage(ce) == stage) {
Shawn Pearcee7024962006-08-23 02:49:00 -04001171 hashcpy(sha1, ce->sha1);
Kirill Smelkov90064712010-09-29 15:35:24 +04001172 oc->mode = ce->ce_mode;
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001173 free(new_path);
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001174 return 0;
1175 }
Junio C Hamanoe7cef452006-05-08 15:44:06 -07001176 pos++;
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001177 }
Matthieu Moy009fee42009-12-07 11:10:50 +01001178 if (!gently)
1179 diagnose_invalid_index_path(stage, prefix, cp);
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001180 free(new_path);
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001181 return -1;
1182 }
Shawn Pearcecce91a22006-05-19 03:29:43 -04001183 for (cp = name, bracket_depth = 0; *cp; cp++) {
1184 if (*cp == '{')
1185 bracket_depth++;
1186 else if (bracket_depth && *cp == '}')
1187 bracket_depth--;
1188 else if (!bracket_depth && *cp == ':')
1189 break;
1190 }
1191 if (*cp == ':') {
Junio C Hamano73b0e5a2006-04-21 17:31:04 -07001192 unsigned char tree_sha1[20];
Matthieu Moy009fee42009-12-07 11:10:50 +01001193 char *object_name = NULL;
1194 if (!gently) {
1195 object_name = xmalloc(cp-name+1);
1196 strncpy(object_name, name, cp-name);
1197 object_name[cp-name] = '\0';
1198 }
1199 if (!get_sha1_1(name, cp-name, tree_sha1)) {
1200 const char *filename = cp+1;
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001201 char *new_filename = NULL;
1202
1203 new_filename = resolve_relative_path(filename);
1204 if (new_filename)
1205 filename = new_filename;
Clément Poulain573285e2010-06-09 19:02:06 +02001206 ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode);
Matthieu Moy009fee42009-12-07 11:10:50 +01001207 if (!gently) {
1208 diagnose_invalid_sha1_path(prefix, filename,
1209 tree_sha1, object_name);
1210 free(object_name);
1211 }
Clément Poulain573285e2010-06-09 19:02:06 +02001212 hashcpy(oc->tree, tree_sha1);
1213 strncpy(oc->path, filename,
1214 sizeof(oc->path));
1215 oc->path[sizeof(oc->path)-1] = '\0';
1216
Nguyễn Thái Ngọc Duy979f79292010-11-28 10:37:32 +07001217 free(new_filename);
Matthieu Moy009fee42009-12-07 11:10:50 +01001218 return ret;
1219 } else {
1220 if (!gently)
1221 die("Invalid object name '%s'.", object_name);
1222 }
Linus Torvalds51196022006-04-18 16:45:16 -07001223 }
1224 return ret;
Junio C Hamano9938af62005-08-03 22:15:49 -07001225}