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