Add 'git-rev-parse' helper script
[git.git] / rev-parse.c
1 /*
2  * rev-parse.c
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7
8 int main(int argc, char **argv)
9 {
10         int i, as_is = 0;
11         char *def = NULL;
12         unsigned char sha1[20];
13
14         for (i = 1; i < argc; i++) {
15                 char *arg = argv[i];
16                 char *dotdot;
17         
18                 if (as_is) {
19                         printf("%s\n", arg);
20                         continue;
21                 }
22                 if (*arg == '-') {
23                         if (!strcmp(arg, "--")) {
24                                 if (def) {
25                                         printf("%s\n", def);
26                                         def = NULL;
27                                 }
28                                 as_is = 1;
29                         }
30                         if (!strcmp(arg, "--default")) {
31                                 if (def)
32                                         printf("%s\n", def);
33                                 def = argv[i+1];
34                                 i++;
35                                 continue;
36                         }
37                         printf("%s\n", arg);
38                         continue;
39                 }
40                 def = NULL;
41                 if (!get_sha1(arg, sha1)) {
42                         printf("%s\n", sha1_to_hex(sha1));
43                         continue;
44                 }
45                 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
46                         printf("^%s\n", sha1_to_hex(sha1));
47                         continue;
48                 }
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;
61                                 }
62                         }
63                         *dotdot = '.';
64                 }
65                 printf("%s\n", arg);
66         }
67         if (def)
68                 printf("%s\n", def);
69         return 0;
70 }