Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 1 | /* |
| 2 | * rev-parse.c |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| 6 | #include "cache.h" |
Linus Torvalds | a8be83f | 2005-06-21 03:28:09 | [diff] [blame] | 7 | #include "commit.h" |
Linus Torvalds | 960bba0 | 2005-07-03 20:07:52 | [diff] [blame] | 8 | #include "refs.h" |
Linus Torvalds | c1babb1 | 2005-09-20 21:13:24 | [diff] [blame] | 9 | #include "quote.h" |
Linus Torvalds | a8be83f | 2005-06-21 03:28:09 | [diff] [blame] | 10 | |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 11 | #define DO_REVS 1 |
| 12 | #define DO_NOREV 2 |
| 13 | #define DO_FLAGS 4 |
| 14 | #define DO_NONFLAGS 8 |
| 15 | static int filter = ~0; |
| 16 | |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 17 | static char *def = NULL; |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 18 | |
Linus Torvalds | 042a4ed | 2005-06-26 18:34:30 | [diff] [blame] | 19 | #define NORMAL 0 |
| 20 | #define REVERSED 1 |
| 21 | static int show_type = NORMAL; |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 22 | static int symbolic = 0; |
| 23 | static int output_sq = 0; |
| 24 | |
| 25 | static int revs_count = 0; |
Linus Torvalds | 042a4ed | 2005-06-26 18:34:30 | [diff] [blame] | 26 | |
Linus Torvalds | 921d865 | 2005-06-13 18:14:20 | [diff] [blame] | 27 | /* |
| 28 | * Some arguments are relevant "revision" arguments, |
| 29 | * others are about output format or other details. |
| 30 | * This sorts it all out. |
| 31 | */ |
| 32 | static int is_rev_argument(const char *arg) |
| 33 | { |
| 34 | static const char *rev_args[] = { |
Junio C Hamano | e091eb9 | 2005-10-05 21:49:54 | [diff] [blame] | 35 | "--all", |
Junio C Hamano | 5ccfb75 | 2005-08-09 02:31:37 | [diff] [blame] | 36 | "--bisect", |
Junio C Hamano | 5a83f3b | 2005-10-30 09:08:35 | [diff] [blame] | 37 | "--dense", |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 38 | "--header", |
| 39 | "--max-age=", |
| 40 | "--max-count=", |
| 41 | "--merge-order", |
| 42 | "--min-age=", |
Junio C Hamano | 5ccfb75 | 2005-08-09 02:31:37 | [diff] [blame] | 43 | "--no-merges", |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 44 | "--objects", |
| 45 | "--parents", |
| 46 | "--pretty", |
| 47 | "--show-breaks", |
Junio C Hamano | 5a83f3b | 2005-10-30 09:08:35 | [diff] [blame] | 48 | "--sparse", |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 49 | "--topo-order", |
| 50 | "--unpacked", |
Linus Torvalds | 921d865 | 2005-06-13 18:14:20 | [diff] [blame] | 51 | NULL |
| 52 | }; |
| 53 | const char **p = rev_args; |
| 54 | |
| 55 | for (;;) { |
| 56 | const char *str = *p++; |
| 57 | int len; |
| 58 | if (!str) |
| 59 | return 0; |
| 60 | len = strlen(str); |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 61 | if (!strcmp(arg, str) || |
| 62 | (str[len-1] == '=' && !strncmp(arg, str, len))) |
Linus Torvalds | 921d865 | 2005-06-13 18:14:20 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | } |
| 66 | |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 67 | /* Output argument as a string, either SQ or normal */ |
Junio C Hamano | 5bb2c65 | 2005-07-23 02:08:32 | [diff] [blame] | 68 | static void show(const char *arg) |
| 69 | { |
| 70 | if (output_sq) { |
| 71 | int sq = '\'', ch; |
| 72 | |
| 73 | putchar(sq); |
| 74 | while ((ch = *arg++)) { |
| 75 | if (ch == sq) |
| 76 | fputs("'\\'", stdout); |
| 77 | putchar(ch); |
| 78 | } |
| 79 | putchar(sq); |
| 80 | putchar(' '); |
| 81 | } |
| 82 | else |
| 83 | puts(arg); |
| 84 | } |
| 85 | |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 86 | /* Output a revision, only if filter allows it */ |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 87 | static void show_rev(int type, const unsigned char *sha1, const char *name) |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 88 | { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 89 | if (!(filter & DO_REVS)) |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 90 | return; |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 91 | def = NULL; |
| 92 | revs_count++; |
Junio C Hamano | 5bb2c65 | 2005-07-23 02:08:32 | [diff] [blame] | 93 | |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 94 | if (type != show_type) |
| 95 | putchar('^'); |
| 96 | if (symbolic && name) |
| 97 | show(name); |
| 98 | else |
| 99 | show(sha1_to_hex(sha1)); |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 100 | } |
| 101 | |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 102 | /* Output a flag, only if filter allows it. */ |
| 103 | static void show_flag(char *arg) |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 104 | { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 105 | if (!(filter & DO_FLAGS)) |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 106 | return; |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 107 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) |
Linus Torvalds | 0360e99 | 2005-08-23 17:47:54 | [diff] [blame] | 108 | show(arg); |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 109 | } |
| 110 | |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 111 | static void show_default(void) |
| 112 | { |
| 113 | char *s = def; |
| 114 | |
| 115 | if (s) { |
| 116 | unsigned char sha1[20]; |
| 117 | |
| 118 | def = NULL; |
Junio C Hamano | 9938af6 | 2005-08-04 05:15:49 | [diff] [blame] | 119 | if (!get_sha1(s, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 120 | show_rev(NORMAL, sha1, s); |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 121 | return; |
| 122 | } |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Linus Torvalds | 960bba0 | 2005-07-03 20:07:52 | [diff] [blame] | 126 | static int show_reference(const char *refname, const unsigned char *sha1) |
| 127 | { |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 128 | show_rev(NORMAL, sha1, refname); |
Linus Torvalds | 960bba0 | 2005-07-03 20:07:52 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Linus Torvalds | c1babb1 | 2005-09-20 21:13:24 | [diff] [blame] | 132 | static void show_datestring(const char *flag, const char *datestr) |
| 133 | { |
| 134 | FILE *date; |
| 135 | static char buffer[100]; |
| 136 | static char cmd[1000]; |
| 137 | int len; |
| 138 | |
| 139 | /* date handling requires both flags and revs */ |
| 140 | if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) |
| 141 | return; |
| 142 | len = strlen(flag); |
| 143 | memcpy(buffer, flag, len); |
| 144 | |
| 145 | snprintf(cmd, sizeof(cmd), "date --date=%s +%%s", sq_quote(datestr)); |
| 146 | date = popen(cmd, "r"); |
| 147 | if (!date || !fgets(buffer + len, sizeof(buffer) - len, date)) |
| 148 | die("git-rev-list: bad date string"); |
| 149 | pclose(date); |
| 150 | len = strlen(buffer); |
| 151 | if (buffer[len-1] == '\n') |
| 152 | buffer[--len] = 0; |
| 153 | show(buffer); |
| 154 | } |
| 155 | |
Linus Torvalds | 7a3dd47 | 2005-10-18 07:16:45 | [diff] [blame] | 156 | static void show_file(const char *arg) |
| 157 | { |
Linus Torvalds | 7b34c2f | 2005-10-25 22:24:55 | [diff] [blame] | 158 | show_default(); |
Linus Torvalds | 7a3dd47 | 2005-10-18 07:16:45 | [diff] [blame] | 159 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) |
| 160 | show(arg); |
| 161 | } |
| 162 | |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 163 | int main(int argc, char **argv) |
| 164 | { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 165 | int i, as_is = 0, verify = 0; |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 166 | unsigned char sha1[20]; |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 167 | const char *prefix = setup_git_directory(); |
| 168 | |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 169 | for (i = 1; i < argc; i++) { |
| 170 | char *arg = argv[i]; |
| 171 | char *dotdot; |
| 172 | |
| 173 | if (as_is) { |
Linus Torvalds | 7a3dd47 | 2005-10-18 07:16:45 | [diff] [blame] | 174 | show_file(arg); |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 175 | continue; |
| 176 | } |
| 177 | if (*arg == '-') { |
| 178 | if (!strcmp(arg, "--")) { |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 179 | as_is = 1; |
Linus Torvalds | a08b650 | 2005-10-21 00:16:30 | [diff] [blame] | 180 | /* Pass on the "--" if we show anything but files.. */ |
| 181 | if (filter & (DO_FLAGS | DO_REVS)) |
| 182 | show_file(arg); |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 183 | continue; |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 184 | } |
| 185 | if (!strcmp(arg, "--default")) { |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 186 | def = argv[i+1]; |
| 187 | i++; |
| 188 | continue; |
| 189 | } |
Linus Torvalds | 8ebb018 | 2005-06-13 17:21:11 | [diff] [blame] | 190 | if (!strcmp(arg, "--revs-only")) { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 191 | filter &= ~DO_NOREV; |
Linus Torvalds | 8ebb018 | 2005-06-13 17:21:11 | [diff] [blame] | 192 | continue; |
| 193 | } |
| 194 | if (!strcmp(arg, "--no-revs")) { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 195 | filter &= ~DO_REVS; |
Linus Torvalds | 8ebb018 | 2005-06-13 17:21:11 | [diff] [blame] | 196 | continue; |
| 197 | } |
Linus Torvalds | f79b65a | 2005-07-06 17:08:08 | [diff] [blame] | 198 | if (!strcmp(arg, "--flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 199 | filter &= ~DO_NONFLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 17:08:08 | [diff] [blame] | 200 | continue; |
| 201 | } |
| 202 | if (!strcmp(arg, "--no-flags")) { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 203 | filter &= ~DO_FLAGS; |
Linus Torvalds | f79b65a | 2005-07-06 17:08:08 | [diff] [blame] | 204 | continue; |
| 205 | } |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 206 | if (!strcmp(arg, "--verify")) { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 207 | filter &= ~(DO_FLAGS|DO_NOREV); |
| 208 | verify = 1; |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 209 | continue; |
Linus Torvalds | 921d865 | 2005-06-13 18:14:20 | [diff] [blame] | 210 | } |
Junio C Hamano | 5bb2c65 | 2005-07-23 02:08:32 | [diff] [blame] | 211 | if (!strcmp(arg, "--sq")) { |
| 212 | output_sq = 1; |
| 213 | continue; |
| 214 | } |
Linus Torvalds | 042a4ed | 2005-06-26 18:34:30 | [diff] [blame] | 215 | if (!strcmp(arg, "--not")) { |
| 216 | show_type ^= REVERSED; |
| 217 | continue; |
| 218 | } |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 219 | if (!strcmp(arg, "--symbolic")) { |
| 220 | symbolic = 1; |
| 221 | continue; |
| 222 | } |
Linus Torvalds | 960bba0 | 2005-07-03 20:07:52 | [diff] [blame] | 223 | if (!strcmp(arg, "--all")) { |
| 224 | for_each_ref(show_reference); |
| 225 | continue; |
| 226 | } |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 227 | if (!strcmp(arg, "--show-prefix")) { |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 228 | if (prefix) |
| 229 | puts(prefix); |
Linus Torvalds | d288a70 | 2005-08-17 01:06:34 | [diff] [blame] | 230 | continue; |
| 231 | } |
Linus Torvalds | a8783ee | 2005-09-18 18:18:30 | [diff] [blame] | 232 | if (!strcmp(arg, "--git-dir")) { |
| 233 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
| 234 | static char cwd[PATH_MAX]; |
| 235 | if (gitdir) { |
| 236 | puts(gitdir); |
| 237 | continue; |
| 238 | } |
| 239 | if (!prefix) { |
| 240 | puts(".git"); |
| 241 | continue; |
| 242 | } |
| 243 | if (!getcwd(cwd, PATH_MAX)) |
| 244 | die("unable to get current working directory"); |
| 245 | printf("%s/.git\n", cwd); |
| 246 | continue; |
| 247 | } |
Linus Torvalds | c1babb1 | 2005-09-20 21:13:24 | [diff] [blame] | 248 | if (!strncmp(arg, "--since=", 8)) { |
| 249 | show_datestring("--max-age=", arg+8); |
| 250 | continue; |
| 251 | } |
| 252 | if (!strncmp(arg, "--after=", 8)) { |
| 253 | show_datestring("--max-age=", arg+8); |
| 254 | continue; |
| 255 | } |
| 256 | if (!strncmp(arg, "--before=", 9)) { |
| 257 | show_datestring("--min-age=", arg+9); |
| 258 | continue; |
| 259 | } |
| 260 | if (!strncmp(arg, "--until=", 8)) { |
| 261 | show_datestring("--min-age=", arg+8); |
| 262 | continue; |
| 263 | } |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 264 | if (verify) |
| 265 | die("Needed a single revision"); |
| 266 | show_flag(arg); |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 267 | continue; |
| 268 | } |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 269 | |
| 270 | /* Not a flag argument */ |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 271 | dotdot = strstr(arg, ".."); |
| 272 | if (dotdot) { |
| 273 | unsigned char end[20]; |
| 274 | char *n = dotdot+2; |
| 275 | *dotdot = 0; |
Junio C Hamano | 9938af6 | 2005-08-04 05:15:49 | [diff] [blame] | 276 | if (!get_sha1(arg, sha1)) { |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 277 | if (!*n) |
| 278 | n = "HEAD"; |
Junio C Hamano | 9938af6 | 2005-08-04 05:15:49 | [diff] [blame] | 279 | if (!get_sha1(n, end)) { |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 280 | show_rev(NORMAL, end, n); |
| 281 | show_rev(REVERSED, sha1, arg); |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 282 | continue; |
| 283 | } |
| 284 | } |
| 285 | *dotdot = '.'; |
| 286 | } |
Junio C Hamano | 9938af6 | 2005-08-04 05:15:49 | [diff] [blame] | 287 | if (!get_sha1(arg, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 288 | show_rev(NORMAL, sha1, arg); |
Linus Torvalds | 800644c | 2005-06-20 15:29:13 | [diff] [blame] | 289 | continue; |
| 290 | } |
Junio C Hamano | 9938af6 | 2005-08-04 05:15:49 | [diff] [blame] | 291 | if (*arg == '^' && !get_sha1(arg+1, sha1)) { |
Junio C Hamano | 30b96fc | 2005-08-16 19:36:46 | [diff] [blame] | 292 | show_rev(REVERSED, sha1, arg+1); |
Linus Torvalds | 800644c | 2005-06-20 15:29:13 | [diff] [blame] | 293 | continue; |
| 294 | } |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 295 | if (verify) |
| 296 | die("Needed a single revision"); |
Linus Torvalds | af13cdf | 2005-10-28 19:41:49 | [diff] [blame] | 297 | as_is = 1; |
Linus Torvalds | 7a3dd47 | 2005-10-18 07:16:45 | [diff] [blame] | 298 | show_file(arg); |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 299 | } |
Linus Torvalds | 023d66e | 2005-06-24 17:12:55 | [diff] [blame] | 300 | show_default(); |
Junio C Hamano | 4866ccf | 2005-08-24 21:30:04 | [diff] [blame] | 301 | if (verify && revs_count != 1) |
| 302 | die("Needed a single revision"); |
Linus Torvalds | 178cb24 | 2005-06-13 17:06:50 | [diff] [blame] | 303 | return 0; |
| 304 | } |