Add 'git-rev-parse' helper script
[tgit.git] / rev-parse.c
blob69749f5b783bfc35c91b8aa4a98ff7adac3714c1
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 int main(int argc, char **argv)
10 int i, as_is = 0;
11 char *def = NULL;
12 unsigned char sha1[20];
14 for (i = 1; i < argc; i++) {
15 char *arg = argv[i];
16 char *dotdot;
18 if (as_is) {
19 printf("%s\n", arg);
20 continue;
22 if (*arg == '-') {
23 if (!strcmp(arg, "--")) {
24 if (def) {
25 printf("%s\n", def);
26 def = NULL;
28 as_is = 1;
30 if (!strcmp(arg, "--default")) {
31 if (def)
32 printf("%s\n", def);
33 def = argv[i+1];
34 i++;
35 continue;
37 printf("%s\n", arg);
38 continue;
40 def = NULL;
41 if (!get_sha1(arg, sha1)) {
42 printf("%s\n", sha1_to_hex(sha1));
43 continue;
45 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
46 printf("^%s\n", sha1_to_hex(sha1));
47 continue;
49 dotdot = strstr(arg, "..");
50 if (dotdot) {
51 unsigned char end[20];
52 char *n = dotdot+2;
53 *dotdot = 0;
54 if (!get_sha1(arg, sha1)) {
55 if (!*n)
56 n = "HEAD";
57 if (!get_sha1(n, end)) {
58 printf("%s\n", sha1_to_hex(end));
59 printf("^%s\n", sha1_to_hex(sha1));
60 continue;
63 *dotdot = '.';
65 printf("%s\n", arg);
67 if (def)
68 printf("%s\n", def);
69 return 0;