Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 2 | #include "tag.h" |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 3 | #include "commit.h" |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 4 | #include "tree.h" |
| 5 | #include "blob.h" |
Junio C Hamano | f3ab49d | 2006-04-19 11:56:53 -0700 | [diff] [blame] | 6 | #include "tree-walk.h" |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 7 | #include "refs.h" |
Johannes Schindelin | 28fb843 | 2009-09-10 17:25:57 +0200 | [diff] [blame] | 8 | #include "remote.h" |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 9 | |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 10 | static int get_sha1_oneline(const char *, unsigned char *, struct commit_list *); |
| 11 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 12 | static int find_short_object_filename(int len, const char *name, unsigned char *sha1) |
| 13 | { |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 14 | struct alternate_object_database *alt; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 15 | char hex[40]; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 16 | int found = 0; |
| 17 | static struct alternate_object_database *fakeent; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 18 | |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 19 | if (!fakeent) { |
| 20 | const char *objdir = get_object_directory(); |
| 21 | int objdir_len = strlen(objdir); |
| 22 | int entlen = objdir_len + 43; |
| 23 | fakeent = xmalloc(sizeof(*fakeent) + entlen); |
| 24 | memcpy(fakeent->base, objdir, objdir_len); |
| 25 | fakeent->name = fakeent->base + objdir_len + 1; |
| 26 | fakeent->name[-1] = '/'; |
| 27 | } |
| 28 | fakeent->next = alt_odb_list; |
| 29 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 30 | sprintf(hex, "%.2s", name); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 31 | for (alt = fakeent; alt && found < 2; alt = alt->next) { |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 32 | struct dirent *de; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 33 | DIR *dir; |
| 34 | sprintf(alt->name, "%.2s/", name); |
| 35 | dir = opendir(alt->base); |
| 36 | if (!dir) |
| 37 | continue; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 38 | while ((de = readdir(dir)) != NULL) { |
| 39 | if (strlen(de->d_name) != 38) |
| 40 | continue; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 41 | if (memcmp(de->d_name, name + 2, len - 2)) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 42 | continue; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 43 | if (!found) { |
| 44 | memcpy(hex + 2, de->d_name, 38); |
| 45 | found++; |
| 46 | } |
| 47 | else if (memcmp(hex + 2, de->d_name, 38)) { |
| 48 | found = 2; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 49 | break; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 50 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 51 | } |
| 52 | closedir(dir); |
| 53 | } |
| 54 | if (found == 1) |
| 55 | return get_sha1_hex(hex, sha1) == 0; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 56 | return found; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b) |
| 60 | { |
| 61 | do { |
| 62 | if (*a != *b) |
| 63 | return 0; |
| 64 | a++; |
| 65 | b++; |
| 66 | len -= 2; |
| 67 | } while (len > 1); |
| 68 | if (len) |
| 69 | if ((*a ^ *b) & 0xf0) |
| 70 | return 0; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1) |
| 75 | { |
| 76 | struct packed_git *p; |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 77 | const unsigned char *found_sha1 = NULL; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 78 | int found = 0; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 79 | |
| 80 | prepare_packed_git(); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 81 | for (p = packed_git; p && found < 2; p = p->next) { |
James Bowes | 1055880 | 2007-05-29 19:29:51 -0400 | [diff] [blame] | 82 | uint32_t num, last; |
| 83 | uint32_t first = 0; |
| 84 | open_pack_index(p); |
| 85 | num = p->num_objects; |
| 86 | last = num; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 87 | while (first < last) { |
Shawn O. Pearce | 326bf39 | 2007-03-06 20:44:19 -0500 | [diff] [blame] | 88 | uint32_t mid = (first + last) / 2; |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 89 | const unsigned char *now; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 90 | int cmp; |
| 91 | |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 92 | now = nth_packed_object_sha1(p, mid); |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 93 | cmp = hashcmp(match, now); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 94 | if (!cmp) { |
| 95 | first = mid; |
| 96 | break; |
| 97 | } |
| 98 | if (cmp > 0) { |
| 99 | first = mid+1; |
| 100 | continue; |
| 101 | } |
| 102 | last = mid; |
| 103 | } |
| 104 | if (first < num) { |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 105 | const unsigned char *now, *next; |
| 106 | now = nth_packed_object_sha1(p, first); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 107 | if (match_sha(len, match, now)) { |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 108 | next = nth_packed_object_sha1(p, first+1); |
| 109 | if (!next|| !match_sha(len, match, next)) { |
Junio C Hamano | 0bc4589 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 110 | /* unique within this pack */ |
| 111 | if (!found) { |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 112 | found_sha1 = now; |
Junio C Hamano | 0bc4589 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 113 | found++; |
| 114 | } |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 115 | else if (hashcmp(found_sha1, now)) { |
Junio C Hamano | 0bc4589 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 116 | found = 2; |
| 117 | break; |
| 118 | } |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 119 | } |
Junio C Hamano | 0bc4589 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 120 | else { |
| 121 | /* not even unique within this pack */ |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 122 | found = 2; |
| 123 | break; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 128 | if (found == 1) |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 129 | hashcpy(sha1, found_sha1); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 130 | return found; |
| 131 | } |
| 132 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 133 | #define SHORT_NAME_NOT_FOUND (-1) |
| 134 | #define SHORT_NAME_AMBIGUOUS (-2) |
| 135 | |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 136 | static int find_unique_short_object(int len, char *canonical, |
| 137 | unsigned char *res, unsigned char *sha1) |
| 138 | { |
| 139 | int has_unpacked, has_packed; |
| 140 | unsigned char unpacked_sha1[20], packed_sha1[20]; |
| 141 | |
Shawn O. Pearce | 693d2bc | 2007-05-26 01:25:11 -0400 | [diff] [blame] | 142 | prepare_alt_odb(); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 143 | has_unpacked = find_short_object_filename(len, canonical, unpacked_sha1); |
| 144 | has_packed = find_short_packed_object(len, res, packed_sha1); |
| 145 | if (!has_unpacked && !has_packed) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 146 | return SHORT_NAME_NOT_FOUND; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 147 | if (1 < has_unpacked || 1 < has_packed) |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 148 | return SHORT_NAME_AMBIGUOUS; |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 149 | if (has_unpacked != has_packed) { |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 150 | hashcpy(sha1, (has_packed ? packed_sha1 : unpacked_sha1)); |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | /* Both have unique ones -- do they match? */ |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 154 | if (hashcmp(packed_sha1, unpacked_sha1)) |
Uwe Zeisberger | e974c9a | 2006-01-26 12:26:15 +0100 | [diff] [blame] | 155 | return SHORT_NAME_AMBIGUOUS; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 156 | hashcpy(sha1, packed_sha1); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 157 | return 0; |
| 158 | } |
| 159 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 160 | static int get_short_sha1(const char *name, int len, unsigned char *sha1, |
| 161 | int quietly) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 162 | { |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 163 | int i, status; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 164 | char canonical[40]; |
| 165 | unsigned char res[20]; |
| 166 | |
pclouds@gmail.com | 8a83157 | 2006-10-19 08:34:41 +0700 | [diff] [blame] | 167 | if (len < MINIMUM_ABBREV || len > 40) |
Linus Torvalds | af61c6e | 2005-09-19 15:16:03 -0700 | [diff] [blame] | 168 | return -1; |
Junio C Hamano | a8e0d16 | 2006-08-23 13:57:23 -0700 | [diff] [blame] | 169 | hashclr(res); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 170 | memset(canonical, 'x', 40); |
Linus Torvalds | af61c6e | 2005-09-19 15:16:03 -0700 | [diff] [blame] | 171 | for (i = 0; i < len ;i++) { |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 172 | unsigned char c = name[i]; |
| 173 | unsigned char val; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 174 | if (c >= '0' && c <= '9') |
| 175 | val = c - '0'; |
| 176 | else if (c >= 'a' && c <= 'f') |
| 177 | val = c - 'a' + 10; |
| 178 | else if (c >= 'A' && c <='F') { |
| 179 | val = c - 'A' + 10; |
| 180 | c -= 'A' - 'a'; |
| 181 | } |
| 182 | else |
| 183 | return -1; |
| 184 | canonical[i] = c; |
| 185 | if (!(i & 1)) |
| 186 | val <<= 4; |
| 187 | res[i >> 1] |= val; |
| 188 | } |
Junio C Hamano | 99a19b4 | 2005-10-02 21:40:51 -0700 | [diff] [blame] | 189 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 190 | status = find_unique_short_object(i, canonical, res, sha1); |
| 191 | if (!quietly && (status == SHORT_NAME_AMBIGUOUS)) |
| 192 | return error("short SHA1 %.*s is ambiguous.", len, canonical); |
| 193 | return status; |
| 194 | } |
| 195 | |
| 196 | const char *find_unique_abbrev(const unsigned char *sha1, int len) |
| 197 | { |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 198 | int status, exists; |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 199 | static char hex[41]; |
Junio C Hamano | 47dd0d5 | 2005-12-13 17:21:41 -0800 | [diff] [blame] | 200 | |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 201 | exists = has_sha1_file(sha1); |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 202 | memcpy(hex, sha1_to_hex(sha1), 40); |
Junio C Hamano | 02c5cba | 2006-08-09 13:17:04 -0700 | [diff] [blame] | 203 | if (len == 40 || !len) |
Junio C Hamano | 47dd0d5 | 2005-12-13 17:21:41 -0800 | [diff] [blame] | 204 | return hex; |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 205 | while (len < 40) { |
| 206 | unsigned char sha1_ret[20]; |
| 207 | status = get_short_sha1(hex, len, sha1_ret, 1); |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 208 | if (exists |
| 209 | ? !status |
| 210 | : status == SHORT_NAME_NOT_FOUND) { |
Junio C Hamano | ea2c69e | 2011-03-10 22:41:14 -0800 | [diff] [blame] | 211 | hex[len] = 0; |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 212 | return hex; |
| 213 | } |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 214 | len++; |
| 215 | } |
Junio C Hamano | b66fde9 | 2008-03-01 23:35:32 -0800 | [diff] [blame] | 216 | return hex; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 219 | static int ambiguous_path(const char *path, int len) |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 220 | { |
| 221 | int slash = 1; |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 222 | int cnt; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 223 | |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 224 | for (cnt = 0; cnt < len; cnt++) { |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 225 | switch (*path++) { |
| 226 | case '\0': |
| 227 | break; |
| 228 | case '/': |
| 229 | if (slash) |
| 230 | break; |
| 231 | slash = 1; |
| 232 | continue; |
| 233 | case '.': |
| 234 | continue; |
| 235 | default: |
| 236 | slash = 0; |
| 237 | continue; |
| 238 | } |
Junio C Hamano | c054d64 | 2005-12-17 00:00:50 -0800 | [diff] [blame] | 239 | break; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 240 | } |
Junio C Hamano | 6677c46 | 2005-12-15 12:54:00 -0800 | [diff] [blame] | 241 | return slash; |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 244 | /* |
| 245 | * *string and *len will only be substituted, and *string returned (for |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 246 | * later free()ing) if the string passed in is a magic short-hand form |
| 247 | * to name a branch. |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 248 | */ |
Junio C Hamano | 431b196 | 2009-03-21 12:51:34 -0700 | [diff] [blame] | 249 | static char *substitute_branch_name(const char **string, int *len) |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 250 | { |
| 251 | struct strbuf buf = STRBUF_INIT; |
Junio C Hamano | 431b196 | 2009-03-21 12:51:34 -0700 | [diff] [blame] | 252 | int ret = interpret_branch_name(*string, &buf); |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 253 | |
| 254 | if (ret == *len) { |
| 255 | size_t size; |
| 256 | *string = strbuf_detach(&buf, &size); |
| 257 | *len = size; |
| 258 | return (char *)*string; |
| 259 | } |
| 260 | |
| 261 | return NULL; |
| 262 | } |
| 263 | |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 264 | int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 265 | { |
Junio C Hamano | 431b196 | 2009-03-21 12:51:34 -0700 | [diff] [blame] | 266 | char *last_branch = substitute_branch_name(&str, &len); |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 267 | const char **p, *r; |
| 268 | int refs_found = 0; |
| 269 | |
| 270 | *ref = NULL; |
Steffen Prohaska | 7980332 | 2007-11-11 15:01:46 +0100 | [diff] [blame] | 271 | for (p = ref_rev_parse_rules; *p; p++) { |
Alex Riesen | 94cc355 | 2008-10-26 23:07:24 +0100 | [diff] [blame] | 272 | char fullref[PATH_MAX]; |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 273 | unsigned char sha1_from_ref[20]; |
| 274 | unsigned char *this_result; |
Junio C Hamano | 057e713 | 2009-02-08 23:52:01 -0800 | [diff] [blame] | 275 | int flag; |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 276 | |
| 277 | this_result = refs_found ? sha1_from_ref : sha1; |
Alex Riesen | 94cc355 | 2008-10-26 23:07:24 +0100 | [diff] [blame] | 278 | mksnpath(fullref, sizeof(fullref), *p, len, str); |
Junio C Hamano | 057e713 | 2009-02-08 23:52:01 -0800 | [diff] [blame] | 279 | r = resolve_ref(fullref, this_result, 1, &flag); |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 280 | if (r) { |
| 281 | if (!refs_found++) |
| 282 | *ref = xstrdup(r); |
| 283 | if (!warn_ambiguous_refs) |
| 284 | break; |
Jeff King | 003c6ab | 2010-02-16 02:03:16 -0500 | [diff] [blame] | 285 | } else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) |
Junio C Hamano | 057e713 | 2009-02-08 23:52:01 -0800 | [diff] [blame] | 286 | warning("ignoring dangling symref %s.", fullref); |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 287 | } |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 288 | free(last_branch); |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 289 | return refs_found; |
| 290 | } |
| 291 | |
Johannes Schindelin | eb3a482 | 2007-02-09 01:28:23 +0100 | [diff] [blame] | 292 | int dwim_log(const char *str, int len, unsigned char *sha1, char **log) |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 293 | { |
Junio C Hamano | 431b196 | 2009-03-21 12:51:34 -0700 | [diff] [blame] | 294 | char *last_branch = substitute_branch_name(&str, &len); |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 295 | const char **p; |
| 296 | int logs_found = 0; |
| 297 | |
| 298 | *log = NULL; |
Steffen Prohaska | 7980332 | 2007-11-11 15:01:46 +0100 | [diff] [blame] | 299 | for (p = ref_rev_parse_rules; *p; p++) { |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 300 | struct stat st; |
Junio C Hamano | 40facde | 2007-02-08 23:24:51 -0800 | [diff] [blame] | 301 | unsigned char hash[20]; |
| 302 | char path[PATH_MAX]; |
| 303 | const char *ref, *it; |
| 304 | |
Alex Riesen | 94cc355 | 2008-10-26 23:07:24 +0100 | [diff] [blame] | 305 | mksnpath(path, sizeof(path), *p, len, str); |
Junio C Hamano | 0c4cd7f | 2008-07-23 17:22:58 -0700 | [diff] [blame] | 306 | ref = resolve_ref(path, hash, 1, NULL); |
Junio C Hamano | 40facde | 2007-02-08 23:24:51 -0800 | [diff] [blame] | 307 | if (!ref) |
| 308 | continue; |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 309 | if (!stat(git_path("logs/%s", path), &st) && |
Junio C Hamano | 40facde | 2007-02-08 23:24:51 -0800 | [diff] [blame] | 310 | S_ISREG(st.st_mode)) |
| 311 | it = path; |
| 312 | else if (strcmp(ref, path) && |
| 313 | !stat(git_path("logs/%s", ref), &st) && |
| 314 | S_ISREG(st.st_mode)) |
| 315 | it = ref; |
| 316 | else |
| 317 | continue; |
| 318 | if (!logs_found++) { |
| 319 | *log = xstrdup(it); |
| 320 | hashcpy(sha1, hash); |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 321 | } |
Junio C Hamano | 40facde | 2007-02-08 23:24:51 -0800 | [diff] [blame] | 322 | if (!warn_ambiguous_refs) |
| 323 | break; |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 324 | } |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 325 | free(last_branch); |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 326 | return logs_found; |
| 327 | } |
| 328 | |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 329 | static inline int upstream_mark(const char *string, int len) |
| 330 | { |
| 331 | const char *suffix[] = { "@{upstream}", "@{u}" }; |
| 332 | int i; |
| 333 | |
| 334 | for (i = 0; i < ARRAY_SIZE(suffix); i++) { |
| 335 | int suffix_len = strlen(suffix[i]); |
| 336 | if (suffix_len <= len |
| 337 | && !memcmp(string, suffix[i], suffix_len)) |
| 338 | return suffix_len; |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 343 | static int get_sha1_1(const char *name, int len, unsigned char *sha1); |
| 344 | |
Junio C Hamano | e86eb66 | 2007-01-19 01:15:15 -0800 | [diff] [blame] | 345 | static int get_sha1_basic(const char *str, int len, unsigned char *sha1) |
| 346 | { |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 347 | static const char *warn_msg = "refname '%.*s' is ambiguous."; |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 348 | char *real_ref = NULL; |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 349 | int refs_found = 0; |
| 350 | int at, reflog_len; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 351 | |
Linus Torvalds | 3c3852e | 2005-08-13 11:05:25 -0700 | [diff] [blame] | 352 | if (len == 40 && !get_sha1_hex(str, sha1)) |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 353 | return 0; |
| 354 | |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 355 | /* basic@{time or number or -number} format to query ref-log */ |
Junio C Hamano | 694500e | 2006-10-23 21:15:34 -0700 | [diff] [blame] | 356 | reflog_len = at = 0; |
Johannes Schindelin | f265458 | 2009-01-28 00:07:46 +0100 | [diff] [blame] | 357 | if (len && str[len-1] == '}') { |
Johannes Schindelin | aa9c55b | 2009-01-17 19:08:12 +0100 | [diff] [blame] | 358 | for (at = len-2; at >= 0; at--) { |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 359 | if (str[at] == '@' && str[at+1] == '{') { |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 360 | if (!upstream_mark(str + at, len - at)) { |
Johannes Schindelin | 28fb843 | 2009-09-10 17:25:57 +0200 | [diff] [blame] | 361 | reflog_len = (len-1) - (at+2); |
| 362 | len = at; |
| 363 | } |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 364 | break; |
| 365 | } |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 369 | /* Accept only unambiguous ref paths. */ |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 370 | if (len && ambiguous_path(str, len)) |
Linus Torvalds | af13cdf | 2005-10-28 12:41:49 -0700 | [diff] [blame] | 371 | return -1; |
| 372 | |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 373 | if (!len && reflog_len) { |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 374 | struct strbuf buf = STRBUF_INIT; |
| 375 | int ret; |
| 376 | /* try the @{-N} syntax for n-th checkout */ |
Junio C Hamano | 431b196 | 2009-03-21 12:51:34 -0700 | [diff] [blame] | 377 | ret = interpret_branch_name(str+at, &buf); |
Thomas Rast | d18ba22 | 2009-01-17 17:09:55 +0100 | [diff] [blame] | 378 | if (ret > 0) { |
| 379 | /* substitute this branch name and restart */ |
| 380 | return get_sha1_1(buf.buf, buf.len, sha1); |
| 381 | } else if (ret == 0) { |
| 382 | return -1; |
| 383 | } |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 384 | /* allow "@{...}" to mean the current branch reflog */ |
| 385 | refs_found = dwim_ref("HEAD", 4, sha1, &real_ref); |
Nicolas Pitre | f2eba66 | 2007-02-03 21:49:16 -0500 | [diff] [blame] | 386 | } else if (reflog_len) |
| 387 | refs_found = dwim_log(str, len, sha1, &real_ref); |
| 388 | else |
Nicolas Pitre | 11cf880 | 2007-02-01 17:29:33 -0500 | [diff] [blame] | 389 | refs_found = dwim_ref(str, len, sha1, &real_ref); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 390 | |
| 391 | if (!refs_found) |
| 392 | return -1; |
| 393 | |
| 394 | if (warn_ambiguous_refs && refs_found > 1) |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 395 | warning(warn_msg, len, str); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 396 | |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 397 | if (reflog_len) { |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 398 | int nth, i; |
| 399 | unsigned long at_time; |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 400 | unsigned long co_time; |
| 401 | int co_tz, co_cnt; |
| 402 | |
Jeff King | 12a258c | 2010-01-28 04:56:43 -0500 | [diff] [blame] | 403 | /* a @{-N} placed anywhere except the start is an error */ |
| 404 | if (str[at+2] == '-') |
| 405 | return -1; |
| 406 | |
Nicolas Pitre | fe55851 | 2007-02-01 12:33:23 -0500 | [diff] [blame] | 407 | /* Is it asking for N-th entry, or approxidate? */ |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 408 | for (i = nth = 0; 0 <= nth && i < reflog_len; i++) { |
| 409 | char ch = str[at+2+i]; |
| 410 | if ('0' <= ch && ch <= '9') |
| 411 | nth = nth * 10 + ch - '0'; |
| 412 | else |
| 413 | nth = -1; |
| 414 | } |
Shawn O. Pearce | ea360dd | 2008-08-21 08:40:44 -0700 | [diff] [blame] | 415 | if (100000000 <= nth) { |
| 416 | at_time = nth; |
| 417 | nth = -1; |
| 418 | } else if (0 <= nth) |
Junio C Hamano | ab2a1a3 | 2006-10-05 23:16:15 -0700 | [diff] [blame] | 419 | at_time = 0; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 420 | else { |
Junio C Hamano | 93cfa7c | 2010-01-26 11:58:00 -0800 | [diff] [blame] | 421 | int errors = 0; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 422 | char *tmp = xstrndup(str + at + 2, reflog_len); |
Junio C Hamano | 93cfa7c | 2010-01-26 11:58:00 -0800 | [diff] [blame] | 423 | at_time = approxidate_careful(tmp, &errors); |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 424 | free(tmp); |
Junio C Hamano | a5e10ac | 2010-01-27 10:53:09 -0800 | [diff] [blame] | 425 | if (errors) |
| 426 | return -1; |
Jeff King | 861f00e | 2008-04-30 00:13:58 -0400 | [diff] [blame] | 427 | } |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 428 | if (read_ref_at(real_ref, at_time, nth, sha1, NULL, |
| 429 | &co_time, &co_tz, &co_cnt)) { |
| 430 | if (at_time) |
Jon Seymour | eedce78 | 2010-08-24 14:52:43 +1000 | [diff] [blame] | 431 | warning("Log for '%.*s' only goes " |
| 432 | "back to %s.", len, str, |
Junio C Hamano | 73013af | 2007-07-13 23:14:52 -0700 | [diff] [blame] | 433 | show_date(co_time, co_tz, DATE_RFC2822)); |
Jon Seymour | e6eedc3 | 2010-08-24 14:52:42 +1000 | [diff] [blame] | 434 | else { |
| 435 | free(real_ref); |
| 436 | die("Log for '%.*s' only has %d entries.", |
| 437 | len, str, co_cnt); |
| 438 | } |
Junio C Hamano | 16d7cc9 | 2007-01-19 01:19:05 -0800 | [diff] [blame] | 439 | } |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 442 | free(real_ref); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 443 | return 0; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 446 | static int get_parent(const char *name, int len, |
| 447 | unsigned char *result, int idx) |
| 448 | { |
| 449 | unsigned char sha1[20]; |
| 450 | int ret = get_sha1_1(name, len, sha1); |
| 451 | struct commit *commit; |
| 452 | struct commit_list *p; |
| 453 | |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | commit = lookup_commit_reference(sha1); |
| 457 | if (!commit) |
| 458 | return -1; |
| 459 | if (parse_commit(commit)) |
| 460 | return -1; |
| 461 | if (!idx) { |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 462 | hashcpy(result, commit->object.sha1); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | p = commit->parents; |
| 466 | while (p) { |
| 467 | if (!--idx) { |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 468 | hashcpy(result, p->item->object.sha1); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | p = p->next; |
| 472 | } |
| 473 | return -1; |
| 474 | } |
| 475 | |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 476 | static int get_nth_ancestor(const char *name, int len, |
| 477 | unsigned char *result, int generation) |
| 478 | { |
| 479 | unsigned char sha1[20]; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 480 | struct commit *commit; |
| 481 | int ret; |
| 482 | |
| 483 | ret = get_sha1_1(name, len, sha1); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 484 | if (ret) |
| 485 | return ret; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 486 | commit = lookup_commit_reference(sha1); |
| 487 | if (!commit) |
| 488 | return -1; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 489 | |
| 490 | while (generation--) { |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 491 | if (parse_commit(commit) || !commit->parents) |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 492 | return -1; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 493 | commit = commit->parents->item; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 494 | } |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 495 | hashcpy(result, commit->object.sha1); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 499 | struct object *peel_to_type(const char *name, int namelen, |
| 500 | struct object *o, enum object_type expected_type) |
| 501 | { |
| 502 | if (name && !namelen) |
| 503 | namelen = strlen(name); |
| 504 | if (!o) { |
| 505 | unsigned char sha1[20]; |
| 506 | if (get_sha1_1(name, namelen, sha1)) |
| 507 | return NULL; |
| 508 | o = parse_object(sha1); |
| 509 | } |
| 510 | while (1) { |
| 511 | if (!o || (!o->parsed && !parse_object(o->sha1))) |
| 512 | return NULL; |
| 513 | if (o->type == expected_type) |
| 514 | return o; |
| 515 | if (o->type == OBJ_TAG) |
| 516 | o = ((struct tag*) o)->tagged; |
| 517 | else if (o->type == OBJ_COMMIT) |
| 518 | o = &(((struct commit *) o)->tree->object); |
| 519 | else { |
| 520 | if (name) |
| 521 | error("%.*s: expected %s type, but the object " |
| 522 | "dereferences to %s type", |
| 523 | namelen, name, typename(expected_type), |
| 524 | typename(o->type)); |
| 525 | return NULL; |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 530 | static int peel_onion(const char *name, int len, unsigned char *sha1) |
| 531 | { |
| 532 | unsigned char outer[20]; |
| 533 | const char *sp; |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 534 | unsigned int expected_type = 0; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 535 | struct object *o; |
| 536 | |
| 537 | /* |
| 538 | * "ref^{type}" dereferences ref repeatedly until you cannot |
| 539 | * dereference anymore, or you get an object of given type, |
| 540 | * whichever comes first. "ref^{}" means just dereference |
| 541 | * tags until you get a non-tag. "ref^0" is a shorthand for |
| 542 | * "ref^{commit}". "commit^{tree}" could be used to find the |
| 543 | * top-level tree of the given commit. |
| 544 | */ |
| 545 | if (len < 4 || name[len-1] != '}') |
| 546 | return -1; |
| 547 | |
| 548 | for (sp = name + len - 1; name <= sp; sp--) { |
| 549 | int ch = *sp; |
| 550 | if (ch == '{' && name < sp && sp[-1] == '^') |
| 551 | break; |
| 552 | } |
| 553 | if (sp <= name) |
| 554 | return -1; |
| 555 | |
| 556 | sp++; /* beginning of type name, or closing brace for empty */ |
| 557 | if (!strncmp(commit_type, sp, 6) && sp[6] == '}') |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 558 | expected_type = OBJ_COMMIT; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 559 | else if (!strncmp(tree_type, sp, 4) && sp[4] == '}') |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 560 | expected_type = OBJ_TREE; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 561 | else if (!strncmp(blob_type, sp, 4) && sp[4] == '}') |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 562 | expected_type = OBJ_BLOB; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 563 | else if (sp[0] == '}') |
Linus Torvalds | 1974632 | 2006-07-11 20:45:31 -0700 | [diff] [blame] | 564 | expected_type = OBJ_NONE; |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 565 | else if (sp[0] == '/') |
| 566 | expected_type = OBJ_COMMIT; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 567 | else |
| 568 | return -1; |
| 569 | |
| 570 | if (get_sha1_1(name, sp - name - 2, outer)) |
| 571 | return -1; |
| 572 | |
| 573 | o = parse_object(outer); |
| 574 | if (!o) |
| 575 | return -1; |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 576 | if (!expected_type) { |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 577 | o = deref_tag(o, name, sp - name - 2); |
Junio C Hamano | 6e1c6c1 | 2005-10-19 22:48:16 -0700 | [diff] [blame] | 578 | if (!o || (!o->parsed && !parse_object(o->sha1))) |
| 579 | return -1; |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 580 | hashcpy(sha1, o->sha1); |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 581 | return 0; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 582 | } |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 583 | |
| 584 | /* |
| 585 | * At this point, the syntax look correct, so |
| 586 | * if we do not get the needed object, we should |
| 587 | * barf. |
| 588 | */ |
| 589 | o = peel_to_type(name, len, o, expected_type); |
| 590 | if (!o) |
Junio C Hamano | 8177631 | 2007-12-24 00:51:01 -0800 | [diff] [blame] | 591 | return -1; |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 592 | |
| 593 | hashcpy(sha1, o->sha1); |
| 594 | if (sp[0] == '/') { |
| 595 | /* "$commit^{/foo}" */ |
| 596 | char *prefix; |
| 597 | int ret; |
| 598 | struct commit_list *list = NULL; |
| 599 | |
Nguyễn Thái Ngọc Duy | 4322842 | 2010-12-15 16:02:54 +0700 | [diff] [blame] | 600 | /* |
| 601 | * $commit^{/}. Some regex implementation may reject. |
| 602 | * We don't need regex anyway. '' pattern always matches. |
| 603 | */ |
| 604 | if (sp[1] == '}') |
| 605 | return 0; |
| 606 | |
Nguyễn Thái Ngọc Duy | 32574b6 | 2010-12-13 10:01:15 +0700 | [diff] [blame] | 607 | prefix = xstrndup(sp + 1, name + len - 1 - (sp + 1)); |
| 608 | commit_list_insert((struct commit *)o, &list); |
| 609 | ret = get_sha1_oneline(prefix, sha1, list); |
| 610 | free(prefix); |
| 611 | return ret; |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 612 | } |
| 613 | return 0; |
| 614 | } |
| 615 | |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 616 | static int get_describe_name(const char *name, int len, unsigned char *sha1) |
| 617 | { |
| 618 | const char *cp; |
| 619 | |
| 620 | for (cp = name + len - 1; name + 2 <= cp; cp--) { |
| 621 | char ch = *cp; |
| 622 | if (hexval(ch) & ~0377) { |
| 623 | /* We must be looking at g in "SOMETHING-g" |
| 624 | * for it to be describe output. |
| 625 | */ |
| 626 | if (ch == 'g' && cp[-1] == '-') { |
| 627 | cp++; |
| 628 | len -= cp - name; |
| 629 | return get_short_sha1(cp, len, sha1, 1); |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | return -1; |
| 634 | } |
| 635 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 636 | static int get_sha1_1(const char *name, int len, unsigned char *sha1) |
| 637 | { |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 638 | int ret, has_suffix; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 639 | const char *cp; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 640 | |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 641 | /* |
| 642 | * "name~3" is "name^^^", "name~" is "name~1", and "name^" is "name^1". |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 643 | */ |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 644 | has_suffix = 0; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 645 | for (cp = name + len - 1; name <= cp; cp--) { |
| 646 | int ch = *cp; |
| 647 | if ('0' <= ch && ch <= '9') |
| 648 | continue; |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 649 | if (ch == '~' || ch == '^') |
| 650 | has_suffix = ch; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 651 | break; |
| 652 | } |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 653 | |
| 654 | if (has_suffix) { |
| 655 | int num = 0; |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 656 | int len1 = cp - name; |
| 657 | cp++; |
| 658 | while (cp < name + len) |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 659 | num = num * 10 + *cp++ - '0'; |
Linus Torvalds | 621ff67 | 2008-03-14 11:49:40 -0700 | [diff] [blame] | 660 | if (!num && len1 == len - 1) |
| 661 | num = 1; |
| 662 | if (has_suffix == '^') |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 663 | return get_parent(name, len1, sha1, num); |
Junio C Hamano | 0601dbe | 2006-02-02 23:48:36 -0800 | [diff] [blame] | 664 | /* else if (has_suffix == '~') -- goes without saying */ |
| 665 | return get_nth_ancestor(name, len1, sha1, num); |
Junio C Hamano | 4f7599a | 2005-08-21 02:43:54 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Junio C Hamano | 5385f52 | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 668 | ret = peel_onion(name, len, sha1); |
| 669 | if (!ret) |
| 670 | return 0; |
| 671 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 672 | ret = get_sha1_basic(name, len, sha1); |
| 673 | if (!ret) |
| 674 | return 0; |
Junio C Hamano | 7dd45e1 | 2006-09-20 16:11:08 -0700 | [diff] [blame] | 675 | |
| 676 | /* It could be describe output that is "SOMETHING-gXXXX" */ |
| 677 | ret = get_describe_name(name, len, sha1); |
| 678 | if (!ret) |
| 679 | return 0; |
| 680 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 681 | return get_short_sha1(name, len, sha1, 0); |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 684 | /* |
| 685 | * This interprets names like ':/Initial revision of "git"' by searching |
| 686 | * through history and returning the first commit whose message starts |
Junio C Hamano | 3d04589 | 2010-08-12 18:32:49 -0700 | [diff] [blame] | 687 | * the given regular expression. |
Junio C Hamano | f7bff00 | 2010-08-02 14:37:06 -0700 | [diff] [blame] | 688 | * |
| 689 | * For future extension, ':/!' is reserved. If you want to match a message |
| 690 | * beginning with a '!', you have to repeat the exclamation mark. |
| 691 | */ |
| 692 | #define ONELINE_SEEN (1u<<20) |
| 693 | |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 694 | static int handle_one_ref(const char *path, |
| 695 | const unsigned char *sha1, int flag, void *cb_data) |
| 696 | { |
| 697 | struct commit_list **list = cb_data; |
| 698 | struct object *object = parse_object(sha1); |
| 699 | if (!object) |
| 700 | return 0; |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 701 | if (object->type == OBJ_TAG) { |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 702 | object = deref_tag(object, path, strlen(path)); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 703 | if (!object) |
| 704 | return 0; |
| 705 | } |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 706 | if (object->type != OBJ_COMMIT) |
| 707 | return 0; |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 708 | commit_list_insert_by_date((struct commit *)object, list); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 709 | return 0; |
| 710 | } |
| 711 | |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 712 | static int get_sha1_oneline(const char *prefix, unsigned char *sha1, |
| 713 | struct commit_list *list) |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 714 | { |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 715 | struct commit_list *backup = NULL, *l; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 716 | int found = 0; |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 717 | regex_t regex; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 718 | |
| 719 | if (prefix[0] == '!') { |
| 720 | if (prefix[1] != '!') |
| 721 | die ("Invalid search pattern: %s", prefix); |
| 722 | prefix++; |
| 723 | } |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 724 | |
| 725 | if (regcomp(®ex, prefix, REG_EXTENDED)) |
| 726 | die("Invalid search pattern: %s", prefix); |
| 727 | |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 728 | for (l = list; l; l = l->next) { |
| 729 | l->item->object.flags |= ONELINE_SEEN; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 730 | commit_list_insert(l->item, &backup); |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 731 | } |
Jim Meyering | ed8ad7e | 2007-03-11 19:49:08 +0100 | [diff] [blame] | 732 | while (list) { |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 733 | char *p, *to_free = NULL; |
Linus Torvalds | 1358e7d | 2007-03-12 11:30:38 -0700 | [diff] [blame] | 734 | struct commit *commit; |
Johannes Schindelin | 364d3e6 | 2007-12-03 18:42:39 +0000 | [diff] [blame] | 735 | enum object_type type; |
| 736 | unsigned long size; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 737 | int matches; |
Jim Meyering | ed8ad7e | 2007-03-11 19:49:08 +0100 | [diff] [blame] | 738 | |
| 739 | commit = pop_most_recent_commit(&list, ONELINE_SEEN); |
Martin Koegler | 283cdbc | 2008-02-18 21:47:53 +0100 | [diff] [blame] | 740 | if (!parse_object(commit->object.sha1)) |
| 741 | continue; |
Johannes Schindelin | 364d3e6 | 2007-12-03 18:42:39 +0000 | [diff] [blame] | 742 | if (commit->buffer) |
| 743 | p = commit->buffer; |
| 744 | else { |
| 745 | p = read_sha1_file(commit->object.sha1, &type, &size); |
| 746 | if (!p) |
| 747 | continue; |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 748 | to_free = p; |
Johannes Schindelin | 364d3e6 | 2007-12-03 18:42:39 +0000 | [diff] [blame] | 749 | } |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 750 | |
| 751 | p = strstr(p, "\n\n"); |
| 752 | matches = p && !regexec(®ex, p + 2, 0, NULL, 0); |
| 753 | free(to_free); |
| 754 | |
| 755 | if (matches) { |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 756 | hashcpy(sha1, commit->object.sha1); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 757 | found = 1; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 758 | break; |
| 759 | } |
| 760 | } |
Linus Torvalds | 5789510 | 2010-04-23 08:20:20 -0700 | [diff] [blame] | 761 | regfree(®ex); |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 762 | free_commit_list(list); |
| 763 | for (l = backup; l; l = l->next) |
| 764 | clear_commit_marks(l->item, ONELINE_SEEN); |
Junio C Hamano | 28042db | 2010-12-12 22:19:00 -0800 | [diff] [blame] | 765 | free_commit_list(backup); |
| 766 | return found ? 0 : -1; |
Johannes Schindelin | 28a4d94 | 2007-02-24 03:08:20 +0100 | [diff] [blame] | 767 | } |
| 768 | |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 769 | struct grab_nth_branch_switch_cbdata { |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 770 | long cnt, alloc; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 771 | struct strbuf *buf; |
| 772 | }; |
| 773 | |
| 774 | static int grab_nth_branch_switch(unsigned char *osha1, unsigned char *nsha1, |
| 775 | const char *email, unsigned long timestamp, int tz, |
| 776 | const char *message, void *cb_data) |
| 777 | { |
| 778 | struct grab_nth_branch_switch_cbdata *cb = cb_data; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 779 | const char *match = NULL, *target = NULL; |
| 780 | size_t len; |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 781 | int nth; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 782 | |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 783 | if (!prefixcmp(message, "checkout: moving from ")) { |
| 784 | match = message + strlen("checkout: moving from "); |
Junio C Hamano | d7c03c1 | 2009-01-21 00:37:38 -0800 | [diff] [blame] | 785 | target = strstr(match, " to "); |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Junio C Hamano | c829774 | 2009-01-19 16:44:08 -0800 | [diff] [blame] | 788 | if (!match || !target) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 789 | return 0; |
| 790 | |
Junio C Hamano | d7c03c1 | 2009-01-21 00:37:38 -0800 | [diff] [blame] | 791 | len = target - match; |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 792 | nth = cb->cnt++ % cb->alloc; |
| 793 | strbuf_reset(&cb->buf[nth]); |
| 794 | strbuf_add(&cb->buf[nth], match, len); |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | /* |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 799 | * Parse @{-N} syntax, return the number of characters parsed |
| 800 | * if successful; otherwise signal an error with negative value. |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 801 | */ |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 802 | static int interpret_nth_prior_checkout(const char *name, struct strbuf *buf) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 803 | { |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 804 | long nth; |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 805 | int i, retval; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 806 | struct grab_nth_branch_switch_cbdata cb; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 807 | const char *brace; |
| 808 | char *num_end; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 809 | |
| 810 | if (name[0] != '@' || name[1] != '{' || name[2] != '-') |
| 811 | return -1; |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 812 | brace = strchr(name, '}'); |
| 813 | if (!brace) |
| 814 | return -1; |
| 815 | nth = strtol(name+3, &num_end, 10); |
| 816 | if (num_end != brace) |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 817 | return -1; |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 818 | if (nth <= 0) |
| 819 | return -1; |
| 820 | cb.alloc = nth; |
| 821 | cb.buf = xmalloc(nth * sizeof(struct strbuf)); |
| 822 | for (i = 0; i < nth; i++) |
| 823 | strbuf_init(&cb.buf[i], 20); |
| 824 | cb.cnt = 0; |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 825 | retval = 0; |
Junio C Hamano | 101d15e | 2009-01-19 22:18:29 -0800 | [diff] [blame] | 826 | for_each_recent_reflog_ent("HEAD", grab_nth_branch_switch, 40960, &cb); |
| 827 | if (cb.cnt < nth) { |
| 828 | cb.cnt = 0; |
Junio C Hamano | 101d15e | 2009-01-19 22:18:29 -0800 | [diff] [blame] | 829 | for_each_reflog_ent("HEAD", grab_nth_branch_switch, &cb); |
| 830 | } |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 831 | if (cb.cnt < nth) |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 832 | goto release_return; |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 833 | i = cb.cnt % nth; |
| 834 | strbuf_reset(buf); |
| 835 | strbuf_add(buf, cb.buf[i].buf, cb.buf[i].len); |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 836 | retval = brace-name+1; |
| 837 | |
| 838 | release_return: |
Junio C Hamano | c2883e6 | 2009-01-19 00:04:25 -0800 | [diff] [blame] | 839 | for (i = 0; i < nth; i++) |
| 840 | strbuf_release(&cb.buf[i]); |
| 841 | free(cb.buf); |
Thomas Rast | a884d0c | 2009-01-17 17:09:54 +0100 | [diff] [blame] | 842 | |
Junio C Hamano | 39765e5 | 2009-01-19 21:58:31 -0800 | [diff] [blame] | 843 | return retval; |
Junio C Hamano | ae5a6c3 | 2009-01-17 17:09:53 +0100 | [diff] [blame] | 844 | } |
| 845 | |
Junio C Hamano | 619a644 | 2009-10-18 12:34:56 -0700 | [diff] [blame] | 846 | int get_sha1_mb(const char *name, unsigned char *sha1) |
| 847 | { |
| 848 | struct commit *one, *two; |
| 849 | struct commit_list *mbs; |
| 850 | unsigned char sha1_tmp[20]; |
| 851 | const char *dots; |
| 852 | int st; |
| 853 | |
| 854 | dots = strstr(name, "..."); |
| 855 | if (!dots) |
| 856 | return get_sha1(name, sha1); |
| 857 | if (dots == name) |
| 858 | st = get_sha1("HEAD", sha1_tmp); |
| 859 | else { |
| 860 | struct strbuf sb; |
| 861 | strbuf_init(&sb, dots - name); |
| 862 | strbuf_add(&sb, name, dots - name); |
| 863 | st = get_sha1(sb.buf, sha1_tmp); |
| 864 | strbuf_release(&sb); |
| 865 | } |
| 866 | if (st) |
| 867 | return st; |
| 868 | one = lookup_commit_reference_gently(sha1_tmp, 0); |
| 869 | if (!one) |
| 870 | return -1; |
| 871 | |
| 872 | if (get_sha1(dots[3] ? (dots + 3) : "HEAD", sha1_tmp)) |
| 873 | return -1; |
| 874 | two = lookup_commit_reference_gently(sha1_tmp, 0); |
| 875 | if (!two) |
| 876 | return -1; |
| 877 | mbs = get_merge_bases(one, two, 1); |
| 878 | if (!mbs || mbs->next) |
| 879 | st = -1; |
| 880 | else { |
| 881 | st = 0; |
| 882 | hashcpy(sha1, mbs->item->object.sha1); |
| 883 | } |
| 884 | free_commit_list(mbs); |
| 885 | return st; |
| 886 | } |
| 887 | |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 888 | /* |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 889 | * This reads short-hand syntax that not only evaluates to a commit |
| 890 | * object name, but also can act as if the end user spelled the name |
| 891 | * of the branch from the command line. |
| 892 | * |
| 893 | * - "@{-N}" finds the name of the Nth previous branch we were on, and |
| 894 | * places the name of the branch in the given buf and returns the |
| 895 | * number of characters parsed if successful. |
| 896 | * |
| 897 | * - "<branch>@{upstream}" finds the name of the other ref that |
| 898 | * <branch> is configured to merge with (missing <branch> defaults |
| 899 | * to the current branch), and places the name of the branch in the |
| 900 | * given buf and returns the number of characters parsed if |
| 901 | * successful. |
| 902 | * |
| 903 | * If the input is not of the accepted format, it returns a negative |
| 904 | * number to signal an error. |
| 905 | * |
| 906 | * If the input was ok but there are not N branch switches in the |
| 907 | * reflog, it returns 0. |
| 908 | */ |
| 909 | int interpret_branch_name(const char *name, struct strbuf *buf) |
| 910 | { |
| 911 | char *cp; |
| 912 | struct branch *upstream; |
| 913 | int namelen = strlen(name); |
| 914 | int len = interpret_nth_prior_checkout(name, buf); |
| 915 | int tmp_len; |
| 916 | |
| 917 | if (!len) |
| 918 | return len; /* syntax Ok, not enough switches */ |
Jeff King | d46a830 | 2010-01-28 04:52:22 -0500 | [diff] [blame] | 919 | if (0 < len && len == namelen) |
| 920 | return len; /* consumed all */ |
| 921 | else if (0 < len) { |
| 922 | /* we have extra data, which might need further processing */ |
| 923 | struct strbuf tmp = STRBUF_INIT; |
| 924 | int used = buf->len; |
| 925 | int ret; |
| 926 | |
| 927 | strbuf_add(buf, name + len, namelen - len); |
| 928 | ret = interpret_branch_name(buf->buf, &tmp); |
| 929 | /* that data was not interpreted, remove our cruft */ |
| 930 | if (ret < 0) { |
| 931 | strbuf_setlen(buf, used); |
| 932 | return len; |
| 933 | } |
| 934 | strbuf_reset(buf); |
| 935 | strbuf_addbuf(buf, &tmp); |
| 936 | strbuf_release(&tmp); |
| 937 | /* tweak for size of {-N} versus expanded ref name */ |
| 938 | return ret - used + len; |
| 939 | } |
| 940 | |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 941 | cp = strchr(name, '@'); |
| 942 | if (!cp) |
| 943 | return -1; |
| 944 | tmp_len = upstream_mark(cp, namelen - (cp - name)); |
| 945 | if (!tmp_len) |
| 946 | return -1; |
| 947 | len = cp + tmp_len - name; |
| 948 | cp = xstrndup(name, cp - name); |
| 949 | upstream = branch_get(*cp ? cp : NULL); |
| 950 | if (!upstream |
| 951 | || !upstream->merge |
| 952 | || !upstream->merge[0]->dst) |
| 953 | return error("No upstream branch found for '%s'", cp); |
| 954 | free(cp); |
| 955 | cp = shorten_unambiguous_ref(upstream->merge[0]->dst, 0); |
| 956 | strbuf_reset(buf); |
| 957 | strbuf_addstr(buf, cp); |
| 958 | free(cp); |
| 959 | return len; |
| 960 | } |
| 961 | |
Jonathan Nieder | 6bab74e | 2010-11-06 06:46:52 -0500 | [diff] [blame] | 962 | int strbuf_branchname(struct strbuf *sb, const char *name) |
| 963 | { |
| 964 | int len = strlen(name); |
| 965 | if (interpret_branch_name(name, sb) == len) |
| 966 | return 0; |
| 967 | strbuf_add(sb, name, len); |
| 968 | return len; |
| 969 | } |
| 970 | |
| 971 | int strbuf_check_branch_ref(struct strbuf *sb, const char *name) |
| 972 | { |
| 973 | strbuf_branchname(sb, name); |
| 974 | if (name[0] == '-') |
| 975 | return CHECK_REF_FORMAT_ERROR; |
| 976 | strbuf_splice(sb, 0, 0, "refs/heads/", 11); |
| 977 | return check_ref_format(sb->buf); |
| 978 | } |
| 979 | |
Junio C Hamano | ae0ba8e | 2010-01-19 23:17:11 -0800 | [diff] [blame] | 980 | /* |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 981 | * This is like "get_sha1_basic()", except it allows "sha1 expressions", |
| 982 | * notably "xyz^" for "parent of xyz" |
| 983 | */ |
| 984 | int get_sha1(const char *name, unsigned char *sha1) |
| 985 | { |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 986 | struct object_context unused; |
| 987 | return get_sha1_with_context(name, sha1, &unused); |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 990 | /* Must be called only when object_name:filename doesn't exist. */ |
| 991 | static void diagnose_invalid_sha1_path(const char *prefix, |
| 992 | const char *filename, |
| 993 | const unsigned char *tree_sha1, |
| 994 | const char *object_name) |
| 995 | { |
| 996 | struct stat st; |
| 997 | unsigned char sha1[20]; |
| 998 | unsigned mode; |
| 999 | |
| 1000 | if (!prefix) |
| 1001 | prefix = ""; |
| 1002 | |
| 1003 | if (!lstat(filename, &st)) |
| 1004 | die("Path '%s' exists on disk, but not in '%s'.", |
| 1005 | filename, object_name); |
| 1006 | if (errno == ENOENT || errno == ENOTDIR) { |
| 1007 | char *fullname = xmalloc(strlen(filename) |
| 1008 | + strlen(prefix) + 1); |
| 1009 | strcpy(fullname, prefix); |
| 1010 | strcat(fullname, filename); |
| 1011 | |
| 1012 | if (!get_tree_entry(tree_sha1, fullname, |
| 1013 | sha1, &mode)) { |
| 1014 | die("Path '%s' exists, but not '%s'.\n" |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1015 | "Did you mean '%s:%s' aka '%s:./%s'?", |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1016 | fullname, |
| 1017 | filename, |
| 1018 | object_name, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1019 | fullname, |
| 1020 | object_name, |
| 1021 | filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 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. */ |
| 1029 | static 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 Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1047 | 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 Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1056 | |
| 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 Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1065 | 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" |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1070 | "Did you mean ':%d:%s' aka ':%d:./%s'?", |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1071 | fullname, filename, |
Michael J Gruber | e41d718 | 2011-03-31 11:17:34 +0200 | [diff] [blame] | 1072 | ce_stage(ce), fullname, |
| 1073 | ce_stage(ce), filename); |
Markus Heidelberg | 77e8466 | 2010-02-28 16:49:15 +0100 | [diff] [blame] | 1074 | } |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1075 | |
| 1076 | if (!lstat(filename, &st)) |
| 1077 | die("Path '%s' exists on disk, but not in the index.", filename); |
| 1078 | if (errno == ENOENT || errno == ENOTDIR) |
| 1079 | die("Path '%s' does not exist (neither on disk nor in the index).", |
| 1080 | filename); |
| 1081 | |
| 1082 | free(fullname); |
| 1083 | } |
| 1084 | |
| 1085 | |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1086 | int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode, |
| 1087 | int only_to_die, const char *prefix) |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1088 | { |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1089 | struct object_context oc; |
| 1090 | int ret; |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1091 | ret = get_sha1_with_context_1(name, sha1, &oc, only_to_die, prefix); |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1092 | *mode = oc.mode; |
| 1093 | return ret; |
| 1094 | } |
| 1095 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1096 | static char *resolve_relative_path(const char *rel) |
| 1097 | { |
| 1098 | if (prefixcmp(rel, "./") && prefixcmp(rel, "../")) |
| 1099 | return NULL; |
| 1100 | |
| 1101 | if (!startup_info) |
| 1102 | die("BUG: startup_info struct is not initialized."); |
| 1103 | |
| 1104 | if (!is_inside_work_tree()) |
| 1105 | die("relative path syntax can't be used outside working tree."); |
| 1106 | |
| 1107 | /* die() inside prefix_path() if resolved path is outside worktree */ |
| 1108 | return prefix_path(startup_info->prefix, |
| 1109 | startup_info->prefix ? strlen(startup_info->prefix) : 0, |
| 1110 | rel); |
| 1111 | } |
| 1112 | |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1113 | int get_sha1_with_context_1(const char *name, unsigned char *sha1, |
| 1114 | struct object_context *oc, |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1115 | int only_to_die, const char *prefix) |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1116 | { |
Martin Koegler | a0cd87a | 2007-04-23 22:55:05 +0200 | [diff] [blame] | 1117 | int ret, bracket_depth; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1118 | int namelen = strlen(name); |
| 1119 | const char *cp; |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1120 | |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1121 | memset(oc, 0, sizeof(*oc)); |
| 1122 | oc->mode = S_IFINVALID; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1123 | ret = get_sha1_1(name, namelen, sha1); |
| 1124 | if (!ret) |
| 1125 | return ret; |
| 1126 | /* sha1:path --> object name of path in ent sha1 |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1127 | * :path -> object name of absolute path in index |
| 1128 | * :./path -> object name of path relative to cwd in index |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1129 | * :[0-3]:path -> object name of path in index at stage |
Matthieu Moy | 95ad6d2 | 2010-09-24 18:43:59 +0200 | [diff] [blame] | 1130 | * :/foo -> recent commit matching foo |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1131 | */ |
| 1132 | if (name[0] == ':') { |
| 1133 | int stage = 0; |
| 1134 | struct cache_entry *ce; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1135 | char *new_path = NULL; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1136 | int pos; |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1137 | if (!only_to_die && namelen > 2 && name[1] == '/') { |
Nguyễn Thái Ngọc Duy | 84baa31 | 2010-12-13 10:01:14 +0700 | [diff] [blame] | 1138 | struct commit_list *list = NULL; |
| 1139 | for_each_ref(handle_one_ref, &list); |
| 1140 | return get_sha1_oneline(name + 2, sha1, list); |
| 1141 | } |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1142 | if (namelen < 3 || |
| 1143 | name[2] != ':' || |
| 1144 | name[1] < '0' || '3' < name[1]) |
| 1145 | cp = name + 1; |
| 1146 | else { |
| 1147 | stage = name[1] - '0'; |
| 1148 | cp = name + 3; |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1149 | } |
Junio C Hamano | 3d6e0f7 | 2010-12-09 13:38:05 -0800 | [diff] [blame] | 1150 | new_path = resolve_relative_path(cp); |
| 1151 | if (!new_path) { |
| 1152 | namelen = namelen - (cp - name); |
| 1153 | } else { |
| 1154 | cp = new_path; |
| 1155 | namelen = strlen(cp); |
| 1156 | } |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1157 | |
| 1158 | strncpy(oc->path, cp, |
| 1159 | sizeof(oc->path)); |
| 1160 | oc->path[sizeof(oc->path)-1] = '\0'; |
| 1161 | |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1162 | if (!active_cache) |
| 1163 | read_cache(); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1164 | pos = cache_name_pos(cp, namelen); |
| 1165 | if (pos < 0) |
| 1166 | pos = -pos - 1; |
| 1167 | while (pos < active_nr) { |
| 1168 | ce = active_cache[pos]; |
| 1169 | if (ce_namelen(ce) != namelen || |
| 1170 | memcmp(ce->name, cp, namelen)) |
| 1171 | break; |
| 1172 | if (ce_stage(ce) == stage) { |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 1173 | hashcpy(sha1, ce->sha1); |
Kirill Smelkov | 9006471 | 2010-09-29 15:35:24 +0400 | [diff] [blame] | 1174 | oc->mode = ce->ce_mode; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1175 | free(new_path); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
Junio C Hamano | e7cef45 | 2006-05-08 15:44:06 -0700 | [diff] [blame] | 1178 | pos++; |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1179 | } |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1180 | if (only_to_die && name[1] && name[1] != '/') |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1181 | diagnose_invalid_index_path(stage, prefix, cp); |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1182 | free(new_path); |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1183 | return -1; |
| 1184 | } |
Shawn Pearce | cce91a2 | 2006-05-19 03:29:43 -0400 | [diff] [blame] | 1185 | for (cp = name, bracket_depth = 0; *cp; cp++) { |
| 1186 | if (*cp == '{') |
| 1187 | bracket_depth++; |
| 1188 | else if (bracket_depth && *cp == '}') |
| 1189 | bracket_depth--; |
| 1190 | else if (!bracket_depth && *cp == ':') |
| 1191 | break; |
| 1192 | } |
| 1193 | if (*cp == ':') { |
Junio C Hamano | 73b0e5a | 2006-04-21 17:31:04 -0700 | [diff] [blame] | 1194 | unsigned char tree_sha1[20]; |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1195 | char *object_name = NULL; |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1196 | if (only_to_die) { |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1197 | object_name = xmalloc(cp-name+1); |
| 1198 | strncpy(object_name, name, cp-name); |
| 1199 | object_name[cp-name] = '\0'; |
| 1200 | } |
| 1201 | if (!get_sha1_1(name, cp-name, tree_sha1)) { |
| 1202 | const char *filename = cp+1; |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1203 | char *new_filename = NULL; |
| 1204 | |
| 1205 | new_filename = resolve_relative_path(filename); |
| 1206 | if (new_filename) |
| 1207 | filename = new_filename; |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1208 | ret = get_tree_entry(tree_sha1, filename, sha1, &oc->mode); |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1209 | if (only_to_die) { |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1210 | diagnose_invalid_sha1_path(prefix, filename, |
| 1211 | tree_sha1, object_name); |
| 1212 | free(object_name); |
| 1213 | } |
Clément Poulain | 573285e | 2010-06-09 19:02:06 +0200 | [diff] [blame] | 1214 | hashcpy(oc->tree, tree_sha1); |
| 1215 | strncpy(oc->path, filename, |
| 1216 | sizeof(oc->path)); |
| 1217 | oc->path[sizeof(oc->path)-1] = '\0'; |
| 1218 | |
Nguyễn Thái Ngọc Duy | 979f7929 | 2010-11-28 10:37:32 +0700 | [diff] [blame] | 1219 | free(new_filename); |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1220 | return ret; |
| 1221 | } else { |
Junio C Hamano | 2e83b66 | 2011-05-10 12:02:54 -0700 | [diff] [blame] | 1222 | if (only_to_die) |
Matthieu Moy | 009fee4 | 2009-12-07 11:10:50 +0100 | [diff] [blame] | 1223 | die("Invalid object name '%s'.", object_name); |
| 1224 | } |
Linus Torvalds | 5119602 | 2006-04-18 16:45:16 -0700 | [diff] [blame] | 1225 | } |
| 1226 | return ret; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 1227 | } |