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