Peter Hagervall | baffc0e | 2007-07-15 01:14:45 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 2 | #include "cache.h" |
| 3 | #include "refs.h" |
| 4 | #include "object.h" |
| 5 | #include "tag.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 6 | #include "string-list.h" |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 7 | |
Junio C Hamano | d8285af | 2006-12-18 13:33:47 -0800 | [diff] [blame] | 8 | static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] < ref-list"; |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 9 | |
Christian Couder | c40abef | 2006-09-17 06:20:24 +0200 | [diff] [blame] | 10 | static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0, |
Junio C Hamano | 2eaf222 | 2006-10-01 00:27:27 -0700 | [diff] [blame] | 11 | found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0; |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 12 | static const char **pattern; |
| 13 | |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 14 | static void show_one(const char *refname, const unsigned char *sha1) |
| 15 | { |
| 16 | const char *hex = find_unique_abbrev(sha1, abbrev); |
| 17 | if (hash_only) |
| 18 | printf("%s\n", hex); |
| 19 | else |
| 20 | printf("%s %s\n", hex, refname); |
| 21 | } |
| 22 | |
Junio C Hamano | eaf12a8 | 2006-09-21 00:40:28 -0700 | [diff] [blame] | 23 | static int show_ref(const char *refname, const unsigned char *sha1, int flag, void *cbdata) |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 24 | { |
| 25 | struct object *obj; |
Junio C Hamano | 2eaf222 | 2006-10-01 00:27:27 -0700 | [diff] [blame] | 26 | const char *hex; |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 27 | unsigned char peeled[20]; |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 28 | |
| 29 | if (tags_only || heads_only) { |
| 30 | int match; |
| 31 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 32 | match = heads_only && !prefixcmp(refname, "refs/heads/"); |
| 33 | match |= tags_only && !prefixcmp(refname, "refs/tags/"); |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 34 | if (!match) |
| 35 | return 0; |
| 36 | } |
| 37 | if (pattern) { |
| 38 | int reflen = strlen(refname); |
| 39 | const char **p = pattern, *m; |
| 40 | while ((m = *p++) != NULL) { |
| 41 | int len = strlen(m); |
| 42 | if (len > reflen) |
| 43 | continue; |
| 44 | if (memcmp(m, refname + reflen - len, len)) |
| 45 | continue; |
| 46 | if (len == reflen) |
| 47 | goto match; |
| 48 | /* "--verify" requires an exact match */ |
| 49 | if (verify) |
| 50 | continue; |
| 51 | if (refname[reflen - len - 1] == '/') |
| 52 | goto match; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | match: |
| 58 | found_match++; |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 59 | |
| 60 | /* This changes the semantics slightly that even under quiet we |
| 61 | * detect and return error if the repository is corrupt and |
| 62 | * ref points at a nonexistent object. |
| 63 | */ |
| 64 | if (!has_sha1_file(sha1)) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 65 | die("git show-ref: bad ref %s (%s)", refname, |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 66 | sha1_to_hex(sha1)); |
| 67 | |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 68 | if (quiet) |
| 69 | return 0; |
Junio C Hamano | 2eaf222 | 2006-10-01 00:27:27 -0700 | [diff] [blame] | 70 | |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 71 | show_one(refname, sha1); |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 72 | |
| 73 | if (!deref_tags) |
| 74 | return 0; |
| 75 | |
| 76 | if ((flag & REF_ISPACKED) && !peel_ref(refname, peeled)) { |
Junio C Hamano | f4204ab | 2006-11-21 23:36:35 -0800 | [diff] [blame] | 77 | if (!is_null_sha1(peeled)) { |
| 78 | hex = find_unique_abbrev(peeled, abbrev); |
| 79 | printf("%s %s^{}\n", hex, refname); |
| 80 | } |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 81 | } |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 82 | else { |
| 83 | obj = parse_object(sha1); |
| 84 | if (!obj) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 85 | die("git show-ref: bad ref %s (%s)", refname, |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 86 | sha1_to_hex(sha1)); |
| 87 | if (obj->type == OBJ_TAG) { |
| 88 | obj = deref_tag(obj, refname, 0); |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 89 | if (!obj) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 90 | die("git show-ref: bad tag at ref %s (%s)", refname, |
Martin Koegler | affeef1 | 2008-02-18 08:31:54 +0100 | [diff] [blame] | 91 | sha1_to_hex(sha1)); |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 92 | hex = find_unique_abbrev(obj->sha1, abbrev); |
| 93 | printf("%s %s^{}\n", hex, refname); |
| 94 | } |
| 95 | } |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 99 | static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata) |
| 100 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 101 | struct string_list *list = (struct string_list *)cbdata; |
| 102 | string_list_insert(refname, list); |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input, |
| 108 | * and |
| 109 | * (1) strip "^{}" at the end of line if any; |
| 110 | * (2) ignore if match is provided and does not head-match refname; |
| 111 | * (3) warn if refname is not a well-formed refname and skip; |
| 112 | * (4) ignore if refname is a ref that exists in the local repository; |
| 113 | * (5) otherwise output the line. |
| 114 | */ |
| 115 | static int exclude_existing(const char *match) |
| 116 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 117 | static struct string_list existing_refs = { NULL, 0, 0, 0 }; |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 118 | char buf[1024]; |
| 119 | int matchlen = match ? strlen(match) : 0; |
| 120 | |
| 121 | for_each_ref(add_existing, &existing_refs); |
| 122 | while (fgets(buf, sizeof(buf), stdin)) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 123 | char *ref; |
Junio C Hamano | d8285af | 2006-12-18 13:33:47 -0800 | [diff] [blame] | 124 | int len = strlen(buf); |
| 125 | |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 126 | if (len > 0 && buf[len - 1] == '\n') |
| 127 | buf[--len] = '\0'; |
Junio C Hamano | d8285af | 2006-12-18 13:33:47 -0800 | [diff] [blame] | 128 | if (3 <= len && !strcmp(buf + len - 3, "^{}")) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 129 | len -= 3; |
| 130 | buf[len] = '\0'; |
| 131 | } |
| 132 | for (ref = buf + len; buf < ref; ref--) |
| 133 | if (isspace(ref[-1])) |
| 134 | break; |
| 135 | if (match) { |
| 136 | int reflen = buf + len - ref; |
| 137 | if (reflen < matchlen) |
| 138 | continue; |
| 139 | if (strncmp(ref, match, matchlen)) |
| 140 | continue; |
| 141 | } |
| 142 | if (check_ref_format(ref)) { |
| 143 | fprintf(stderr, "warning: ref '%s' ignored\n", ref); |
| 144 | continue; |
| 145 | } |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 146 | if (!string_list_has_string(&existing_refs, ref)) { |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 147 | printf("%s\n", buf); |
| 148 | } |
| 149 | } |
| 150 | return 0; |
| 151 | } |
| 152 | |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 153 | int cmd_show_ref(int argc, const char **argv, const char *prefix) |
| 154 | { |
| 155 | int i; |
| 156 | |
| 157 | for (i = 1; i < argc; i++) { |
| 158 | const char *arg = argv[i]; |
| 159 | if (*arg != '-') { |
| 160 | pattern = argv + i; |
| 161 | break; |
| 162 | } |
| 163 | if (!strcmp(arg, "--")) { |
| 164 | pattern = argv + i + 1; |
| 165 | if (!*pattern) |
| 166 | pattern = NULL; |
| 167 | break; |
| 168 | } |
| 169 | if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) { |
| 170 | quiet = 1; |
| 171 | continue; |
| 172 | } |
| 173 | if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) { |
| 174 | show_head = 1; |
| 175 | continue; |
| 176 | } |
| 177 | if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) { |
| 178 | deref_tags = 1; |
| 179 | continue; |
| 180 | } |
Christian Couder | c40abef | 2006-09-17 06:20:24 +0200 | [diff] [blame] | 181 | if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) { |
| 182 | hash_only = 1; |
| 183 | continue; |
| 184 | } |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 185 | if (!prefixcmp(arg, "--hash=") || |
| 186 | (!prefixcmp(arg, "--abbrev") && |
Junio C Hamano | 2eaf222 | 2006-10-01 00:27:27 -0700 | [diff] [blame] | 187 | (arg[8] == '=' || arg[8] == '\0'))) { |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 188 | if (arg[2] != 'h' && !arg[8]) |
Junio C Hamano | 2eaf222 | 2006-10-01 00:27:27 -0700 | [diff] [blame] | 189 | /* --abbrev only */ |
| 190 | abbrev = DEFAULT_ABBREV; |
| 191 | else { |
| 192 | /* --hash= or --abbrev= */ |
| 193 | char *end; |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 194 | if (arg[2] == 'h') { |
Junio C Hamano | 2eaf222 | 2006-10-01 00:27:27 -0700 | [diff] [blame] | 195 | hash_only = 1; |
| 196 | arg += 7; |
| 197 | } |
| 198 | else |
| 199 | arg += 9; |
| 200 | abbrev = strtoul(arg, &end, 10); |
| 201 | if (*end || abbrev > 40) |
| 202 | usage(show_ref_usage); |
| 203 | if (abbrev < MINIMUM_ABBREV) |
| 204 | abbrev = MINIMUM_ABBREV; |
| 205 | } |
| 206 | continue; |
| 207 | } |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 208 | if (!strcmp(arg, "--verify")) { |
| 209 | verify = 1; |
| 210 | continue; |
| 211 | } |
| 212 | if (!strcmp(arg, "--tags")) { |
| 213 | tags_only = 1; |
| 214 | continue; |
| 215 | } |
| 216 | if (!strcmp(arg, "--heads")) { |
| 217 | heads_only = 1; |
| 218 | continue; |
| 219 | } |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 220 | if (!strcmp(arg, "--exclude-existing")) |
| 221 | return exclude_existing(NULL); |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 222 | if (!prefixcmp(arg, "--exclude-existing=")) |
Junio C Hamano | ed9f7c9 | 2006-12-17 17:57:19 -0800 | [diff] [blame] | 223 | return exclude_existing(arg + 19); |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 224 | usage(show_ref_usage); |
| 225 | } |
Junio C Hamano | 26cdd1e | 2006-12-17 18:08:52 -0800 | [diff] [blame] | 226 | |
| 227 | if (verify) { |
Dmitry V. Levin | 8ab40a2 | 2007-02-23 20:12:33 +0300 | [diff] [blame] | 228 | if (!pattern) |
| 229 | die("--verify requires a reference"); |
Junio C Hamano | 26cdd1e | 2006-12-17 18:08:52 -0800 | [diff] [blame] | 230 | while (*pattern) { |
Dmitry V. Levin | 8ab40a2 | 2007-02-23 20:12:33 +0300 | [diff] [blame] | 231 | unsigned char sha1[20]; |
| 232 | |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 233 | if (!prefixcmp(*pattern, "refs/") && |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 234 | resolve_ref(*pattern, sha1, 1, NULL)) { |
Junio C Hamano | dd91429 | 2006-12-17 18:53:24 -0800 | [diff] [blame] | 235 | if (!quiet) |
Junio C Hamano | 64fe031 | 2006-12-17 19:27:49 -0800 | [diff] [blame] | 236 | show_one(*pattern, sha1); |
Junio C Hamano | dd91429 | 2006-12-17 18:53:24 -0800 | [diff] [blame] | 237 | } |
Junio C Hamano | 26cdd1e | 2006-12-17 18:08:52 -0800 | [diff] [blame] | 238 | else if (!quiet) |
| 239 | die("'%s' - not a valid ref", *pattern); |
| 240 | else |
| 241 | return 1; |
| 242 | pattern++; |
| 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 247 | if (show_head) |
Junio C Hamano | eaf12a8 | 2006-09-21 00:40:28 -0700 | [diff] [blame] | 248 | head_ref(show_ref, NULL); |
| 249 | for_each_ref(show_ref, NULL); |
Linus Torvalds | 358ddb6 | 2006-09-15 11:19:32 -0700 | [diff] [blame] | 250 | if (!found_match) { |
| 251 | if (verify && !quiet) |
| 252 | die("No match"); |
| 253 | return 1; |
| 254 | } |
| 255 | return 0; |
| 256 | } |