Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rev-parse.c |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| 6 | #include "cache.h" |
Linus Torvalds | a8be83f | 2005-06-20 20:28:09 -0700 | [diff] [blame] | 7 | #include "commit.h" |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 8 | #include "refs.h" |
Linus Torvalds | a8be83f | 2005-06-20 20:28:09 -0700 | [diff] [blame] | 9 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 10 | #define DO_REVS 1 |
| 11 | #define DO_NOREV 2 |
| 12 | #define DO_FLAGS 4 |
| 13 | #define DO_NONFLAGS 8 |
| 14 | static int filter = ~0; |
| 15 | |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 16 | static char *def = NULL; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 18 | #define NORMAL 0 |
| 19 | #define REVERSED 1 |
| 20 | static int show_type = NORMAL; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 21 | static int symbolic = 0; |
| 22 | static int output_sq = 0; |
| 23 | |
| 24 | static int revs_count = 0; |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 26 | /* |
| 27 | * Some arguments are relevant "revision" arguments, |
| 28 | * others are about output format or other details. |
| 29 | * This sorts it all out. |
| 30 | */ |
| 31 | static int is_rev_argument(const char *arg) |
| 32 | { |
| 33 | static const char *rev_args[] = { |
Junio C Hamano | 5ccfb75 | 2005-08-08 19:31:37 -0700 | [diff] [blame] | 34 | "--bisect", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 35 | "--header", |
| 36 | "--max-age=", |
| 37 | "--max-count=", |
| 38 | "--merge-order", |
| 39 | "--min-age=", |
Junio C Hamano | 5ccfb75 | 2005-08-08 19:31:37 -0700 | [diff] [blame] | 40 | "--no-merges", |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 41 | "--objects", |
| 42 | "--parents", |
| 43 | "--pretty", |
| 44 | "--show-breaks", |
| 45 | "--topo-order", |
| 46 | "--unpacked", |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 47 | NULL |
| 48 | }; |
| 49 | const char **p = rev_args; |
| 50 | |
| 51 | for (;;) { |
| 52 | const char *str = *p++; |
| 53 | int len; |
| 54 | if (!str) |
| 55 | return 0; |
| 56 | len = strlen(str); |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 57 | if (!strcmp(arg, str) || |
| 58 | (str[len-1] == '=' && !strncmp(arg, str, len))) |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 59 | return 1; |
| 60 | } |
| 61 | } |
| 62 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 63 | /* Output argument as a string, either SQ or normal */ |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 64 | static void show(const char *arg) |
| 65 | { |
| 66 | if (output_sq) { |
| 67 | int sq = '\'', ch; |
| 68 | |
| 69 | putchar(sq); |
| 70 | while ((ch = *arg++)) { |
| 71 | if (ch == sq) |
| 72 | fputs("'\\'", stdout); |
| 73 | putchar(ch); |
| 74 | } |
| 75 | putchar(sq); |
| 76 | putchar(' '); |
| 77 | } |
| 78 | else |
| 79 | puts(arg); |
| 80 | } |
| 81 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 82 | /* Output a revision, only if filter allows it */ |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 83 | static void show_rev(int type, const unsigned char *sha1, const char *name) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 84 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 85 | if (!(filter & DO_REVS)) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 86 | return; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 87 | def = NULL; |
| 88 | revs_count++; |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 89 | |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 90 | if (type != show_type) |
| 91 | putchar('^'); |
| 92 | if (symbolic && name) |
| 93 | show(name); |
| 94 | else |
| 95 | show(sha1_to_hex(sha1)); |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 98 | /* Output a flag, only if filter allows it. */ |
| 99 | static void show_flag(char *arg) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 100 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 101 | if (!(filter & DO_FLAGS)) |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 102 | return; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 103 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) |
Linus Torvalds | 0360e99 | 2005-08-23 10:47:54 -0700 | [diff] [blame] | 104 | show(arg); |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 107 | static void show_default(void) |
| 108 | { |
| 109 | char *s = def; |
| 110 | |
| 111 | if (s) { |
| 112 | unsigned char sha1[20]; |
| 113 | |
| 114 | def = NULL; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 115 | if (!get_sha1(s, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 116 | show_rev(NORMAL, sha1, s); |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 122 | static int show_reference(const char *refname, const unsigned char *sha1) |
| 123 | { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 124 | show_rev(NORMAL, sha1, refname); |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 128 | int main(int argc, char **argv) |
| 129 | { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 130 | int i, as_is = 0, verify = 0; |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 131 | unsigned char sha1[20]; |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 132 | const char *prefix = setup_git_directory(); |
| 133 | |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 134 | for (i = 1; i < argc; i++) { |
| 135 | char *arg = argv[i]; |
| 136 | char *dotdot; |
| 137 | |
| 138 | if (as_is) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 139 | show(arg); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 140 | continue; |
| 141 | } |
| 142 | if (*arg == '-') { |
| 143 | if (!strcmp(arg, "--")) { |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 144 | as_is = 1; |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 145 | continue; |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 146 | } |
| 147 | if (!strcmp(arg, "--default")) { |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 148 | def = argv[i+1]; |
| 149 | i++; |
| 150 | continue; |
| 151 | } |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 152 | if (!strcmp(arg, "--revs-only")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 153 | filter &= ~DO_NOREV; |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 154 | continue; |
| 155 | } |
| 156 | if (!strcmp(arg, "--no-revs")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 157 | filter &= ~DO_REVS; |
Linus Torvalds | 8ebb018 | 2005-06-13 10:21:11 -0700 | [diff] [blame] | 158 | continue; |
| 159 | } |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 160 | if (!strcmp(arg, "--flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 161 | filter &= ~DO_NONFLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 162 | continue; |
| 163 | } |
| 164 | if (!strcmp(arg, "--no-flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 165 | filter &= ~DO_FLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 10:08:08 -0700 | [diff] [blame] | 166 | continue; |
| 167 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 168 | if (!strcmp(arg, "--verify")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 169 | filter &= ~(DO_FLAGS|DO_NOREV); |
| 170 | verify = 1; |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 171 | continue; |
Linus Torvalds | 921d865 | 2005-06-13 11:14:20 -0700 | [diff] [blame] | 172 | } |
Junio C Hamano | 5bb2c65 | 2005-07-22 19:08:32 -0700 | [diff] [blame] | 173 | if (!strcmp(arg, "--sq")) { |
| 174 | output_sq = 1; |
| 175 | continue; |
| 176 | } |
Linus Torvalds | 042a4ed | 2005-06-26 11:34:30 -0700 | [diff] [blame] | 177 | if (!strcmp(arg, "--not")) { |
| 178 | show_type ^= REVERSED; |
| 179 | continue; |
| 180 | } |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 181 | if (!strcmp(arg, "--symbolic")) { |
| 182 | symbolic = 1; |
| 183 | continue; |
| 184 | } |
Linus Torvalds | 960bba0 | 2005-07-03 13:07:52 -0700 | [diff] [blame] | 185 | if (!strcmp(arg, "--all")) { |
| 186 | for_each_ref(show_reference); |
| 187 | continue; |
| 188 | } |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 189 | if (!strcmp(arg, "--show-prefix")) { |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 190 | if (prefix) |
| 191 | puts(prefix); |
Linus Torvalds | d288a70 | 2005-08-16 18:06:34 -0700 | [diff] [blame] | 192 | continue; |
| 193 | } |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 194 | if (verify) |
| 195 | die("Needed a single revision"); |
| 196 | show_flag(arg); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 197 | continue; |
| 198 | } |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 199 | |
| 200 | /* Not a flag argument */ |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 201 | dotdot = strstr(arg, ".."); |
| 202 | if (dotdot) { |
| 203 | unsigned char end[20]; |
| 204 | char *n = dotdot+2; |
| 205 | *dotdot = 0; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 206 | if (!get_sha1(arg, sha1)) { |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 207 | if (!*n) |
| 208 | n = "HEAD"; |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 209 | if (!get_sha1(n, end)) { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 210 | show_rev(NORMAL, end, n); |
| 211 | show_rev(REVERSED, sha1, arg); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 212 | continue; |
| 213 | } |
| 214 | } |
| 215 | *dotdot = '.'; |
| 216 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 217 | if (!get_sha1(arg, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 218 | show_rev(NORMAL, sha1, arg); |
Linus Torvalds | 800644c | 2005-06-20 08:29:13 -0700 | [diff] [blame] | 219 | continue; |
| 220 | } |
Junio C Hamano | 9938af6 | 2005-08-03 22:15:49 -0700 | [diff] [blame] | 221 | if (*arg == '^' && !get_sha1(arg+1, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 12:36:46 -0700 | [diff] [blame] | 222 | show_rev(REVERSED, sha1, arg+1); |
Linus Torvalds | 800644c | 2005-06-20 08:29:13 -0700 | [diff] [blame] | 223 | continue; |
| 224 | } |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 225 | if (verify) |
| 226 | die("Needed a single revision"); |
| 227 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == |
| 228 | (DO_NONFLAGS|DO_NOREV)) |
| 229 | show(arg); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 230 | } |
Linus Torvalds | 023d66e | 2005-06-24 10:12:55 -0700 | [diff] [blame] | 231 | show_default(); |
Junio C Hamano | 4866ccf | 2005-08-24 14:30:04 -0700 | [diff] [blame] | 232 | if (verify && revs_count != 1) |
| 233 | die("Needed a single revision"); |
Linus Torvalds | 178cb24 | 2005-06-13 10:06:50 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |