1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7#include "commit.h"
8
9static char *def = NULL;
10static int no_revs = 0;
11static int single_rev = 0;
12static int revs_only = 0;
13static int do_rev_argument = 1;
14static int output_revs = 0;
15
16#define NORMAL 0
17#define REVERSED 1
18static int show_type = NORMAL;
19
20static int get_extended_sha1(char *name, unsigned char *sha1);
21
22/*
23 * Some arguments are relevant "revision" arguments,
24 * others are about output format or other details.
25 * This sorts it all out.
26 */
27static int is_rev_argument(const char *arg)
28{
29 static const char *rev_args[] = {
30 "--max-count=",
31 "--max-age=",
32 "--min-age=",
33 "--merge-order",
34 NULL
35 };
36 const char **p = rev_args;
37
38 for (;;) {
39 const char *str = *p++;
40 int len;
41 if (!str)
42 return 0;
43 len = strlen(str);
44 if (!strncmp(arg, str, len))
45 return 1;
46 }
47}
48
49static void show_rev(int type, unsigned char *sha1)
50{
51 if (no_revs)
52 return;
53 output_revs++;
54 printf("%s%s\n", type == show_type ? "" : "^", sha1_to_hex(sha1));
55}
56
57static void show_rev_arg(char *rev)
58{
59 if (no_revs)
60 return;
61 puts(rev);
62}
63
64static void show_norev(char *norev)
65{
66 if (revs_only)
67 return;
68 puts(norev);
69}
70
71static void show_arg(char *arg)
72{
73 if (do_rev_argument && is_rev_argument(arg))
74 show_rev_arg(arg);
75 else
76 show_norev(arg);
77}
78
79static int get_parent(char *name, unsigned char *result, int idx)
80{
81 unsigned char sha1[20];
82 int ret = get_extended_sha1(name, sha1);
83 struct commit *commit;
84 struct commit_list *p;
85
86 if (ret)
87 return ret;
88 commit = lookup_commit_reference(sha1);
89 if (!commit)
90 return -1;
91 if (parse_commit(commit))
92 return -1;
93 p = commit->parents;
94 while (p) {
95 if (!--idx) {
96 memcpy(result, p->item->object.sha1, 20);
97 return 0;
98 }
99 p = p->next;
100 }
101 return -1;
102}
103
104/*
105 * This is like "get_sha1()", except it allows "sha1 expressions",
106 * notably "xyz^" for "parent of xyz"
107 */
108static int get_extended_sha1(char *name, unsigned char *sha1)
109{
110 int parent;
111 int len = strlen(name);
112
113 parent = 1;
114 if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
115 parent = name[len-1] - '0';
116 len--;
117 }
118 if (len > 1 && name[len-1] == '^') {
119 int ret;
120 name[len-1] = 0;
121 ret = get_parent(name, sha1, parent);
122 name[len-1] = '^';
123 if (!ret)
124 return 0;
125 }
126 return get_sha1(name, sha1);
127}
128
129static void show_default(void)
130{
131 char *s = def;
132
133 if (s) {
134 unsigned char sha1[20];
135
136 def = NULL;
137 if (!get_extended_sha1(s, sha1)) {
138 show_rev(NORMAL, sha1);
139 return;
140 }
141 show_arg(s);
142 }
143}
144
145int main(int argc, char **argv)
146{
147 int i, as_is = 0;
148 unsigned char sha1[20];
149
150 for (i = 1; i < argc; i++) {
151 char *arg = argv[i];
152 char *dotdot;
153
154 if (as_is) {
155 show_norev(arg);
156 continue;
157 }
158 if (*arg == '-') {
159 if (!strcmp(arg, "--")) {
160 show_default();
161 if (revs_only)
162 break;
163 as_is = 1;
164 }
165 if (!strcmp(arg, "--default")) {
166 def = argv[i+1];
167 i++;
168 continue;
169 }
170 if (!strcmp(arg, "--revs-only")) {
171 revs_only = 1;
172 continue;
173 }
174 if (!strcmp(arg, "--no-revs")) {
175 no_revs = 1;
176 continue;
177 }
178 if (!strcmp(arg, "--verify")) {
179 revs_only = 1;
180 do_rev_argument = 0;
181 single_rev = 1;
182 continue;
183 }
184 if (!strcmp(arg, "--not")) {
185 show_type ^= REVERSED;
186 continue;
187 }
188 show_arg(arg);
189 continue;
190 }
191 dotdot = strstr(arg, "..");
192 if (dotdot) {
193 unsigned char end[20];
194 char *n = dotdot+2;
195 *dotdot = 0;
196 if (!get_extended_sha1(arg, sha1)) {
197 if (!*n)
198 n = "HEAD";
199 if (!get_extended_sha1(n, end)) {
200 if (no_revs)
201 continue;
202 def = NULL;
203 show_rev(NORMAL, end);
204 show_rev(REVERSED, sha1);
205 continue;
206 }
207 }
208 *dotdot = '.';
209 }
210 if (!get_extended_sha1(arg, sha1)) {
211 if (no_revs)
212 continue;
213 def = NULL;
214 show_rev(NORMAL, sha1);
215 continue;
216 }
217 if (*arg == '^' && !get_extended_sha1(arg+1, sha1)) {
218 if (no_revs)
219 continue;
220 def = NULL;
221 show_rev(REVERSED, sha1);
222 continue;
223 }
224 show_default();
225 show_norev(arg);
226 }
227 show_default();
228 if (single_rev && output_revs != 1) {
229 fprintf(stderr, "Needed a single revision\n");
230 exit(1);
231 }
232 return 0;
233}