Teach applymbox to keep the Subject: line.
[git/git.git] / rev-parse.c
CommitLineData
178cb243
LT
1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
a8be83fe 7#include "commit.h"
960bba0d 8#include "refs.h"
a8be83fe 9
023d66ed
LT
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;
f79b65aa
LT
16static int flags_only = 0;
17static int no_flags = 0;
5bb2c65a 18static int output_sq = 0;
023d66ed 19
042a4ed7
LT
20#define NORMAL 0
21#define REVERSED 1
22static int show_type = NORMAL;
23
921d865e
LT
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",
5ccfb758
JH
36 "--topo-order",
37 "--bisect",
38 "--no-merges",
921d865e
LT
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
5bb2c65a
JH
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
960bba0d 72static void show_rev(int type, const unsigned char *sha1)
023d66ed
LT
73{
74 if (no_revs)
75 return;
76 output_revs++;
5bb2c65a
JH
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');
023d66ed
LT
83}
84
85static void show_rev_arg(char *rev)
86{
87 if (no_revs)
88 return;
5bb2c65a 89 show(rev);
023d66ed
LT
90}
91
92static void show_norev(char *norev)
93{
f79b65aa
LT
94 if (flags_only)
95 return;
023d66ed
LT
96 if (revs_only)
97 return;
5bb2c65a 98 show(norev);
023d66ed
LT
99}
100
101static void show_arg(char *arg)
102{
f79b65aa
LT
103 if (no_flags)
104 return;
023d66ed
LT
105 if (do_rev_argument && is_rev_argument(arg))
106 show_rev_arg(arg);
107 else
108 show_norev(arg);
109}
110
023d66ed
LT
111static void show_default(void)
112{
113 char *s = def;
114
115 if (s) {
116 unsigned char sha1[20];
117
118 def = NULL;
9938af6a 119 if (!get_sha1(s, sha1)) {
042a4ed7 120 show_rev(NORMAL, sha1);
023d66ed
LT
121 return;
122 }
123 show_arg(s);
124 }
125}
126
960bba0d
LT
127static int show_reference(const char *refname, const unsigned char *sha1)
128{
129 show_rev(NORMAL, sha1);
130 return 0;
131}
132
178cb243
LT
133int main(int argc, char **argv)
134{
023d66ed 135 int i, as_is = 0;
178cb243 136 unsigned char sha1[20];
d288a700
LT
137 const char *prefix = setup_git_directory();
138
178cb243
LT
139 for (i = 1; i < argc; i++) {
140 char *arg = argv[i];
141 char *dotdot;
142
143 if (as_is) {
023d66ed 144 show_norev(arg);
178cb243
LT
145 continue;
146 }
147 if (*arg == '-') {
148 if (!strcmp(arg, "--")) {
023d66ed 149 show_default();
8ebb0184
LT
150 if (revs_only)
151 break;
178cb243
LT
152 as_is = 1;
153 }
154 if (!strcmp(arg, "--default")) {
178cb243
LT
155 def = argv[i+1];
156 i++;
157 continue;
158 }
8ebb0184
LT
159 if (!strcmp(arg, "--revs-only")) {
160 revs_only = 1;
161 continue;
162 }
163 if (!strcmp(arg, "--no-revs")) {
164 no_revs = 1;
165 continue;
166 }
f79b65aa
LT
167 if (!strcmp(arg, "--flags")) {
168 flags_only = 1;
169 continue;
170 }
171 if (!strcmp(arg, "--no-flags")) {
172 no_flags = 1;
173 continue;
174 }
023d66ed
LT
175 if (!strcmp(arg, "--verify")) {
176 revs_only = 1;
177 do_rev_argument = 0;
178 single_rev = 1;
179 continue;
921d865e 180 }
5bb2c65a
JH
181 if (!strcmp(arg, "--sq")) {
182 output_sq = 1;
183 continue;
184 }
042a4ed7
LT
185 if (!strcmp(arg, "--not")) {
186 show_type ^= REVERSED;
187 continue;
188 }
960bba0d
LT
189 if (!strcmp(arg, "--all")) {
190 for_each_ref(show_reference);
191 continue;
192 }
d288a700
LT
193 if (!strcmp(arg, "--show-prefix")) {
194 puts(prefix);
195 continue;
196 }
023d66ed 197 show_arg(arg);
178cb243
LT
198 continue;
199 }
178cb243
LT
200 dotdot = strstr(arg, "..");
201 if (dotdot) {
202 unsigned char end[20];
203 char *n = dotdot+2;
204 *dotdot = 0;
9938af6a 205 if (!get_sha1(arg, sha1)) {
178cb243
LT
206 if (!*n)
207 n = "HEAD";
9938af6a 208 if (!get_sha1(n, end)) {
8ebb0184
LT
209 if (no_revs)
210 continue;
211 def = NULL;
042a4ed7
LT
212 show_rev(NORMAL, end);
213 show_rev(REVERSED, sha1);
178cb243
LT
214 continue;
215 }
216 }
217 *dotdot = '.';
218 }
9938af6a 219 if (!get_sha1(arg, sha1)) {
800644c5
LT
220 if (no_revs)
221 continue;
222 def = NULL;
042a4ed7 223 show_rev(NORMAL, sha1);
800644c5
LT
224 continue;
225 }
9938af6a 226 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
800644c5
LT
227 if (no_revs)
228 continue;
229 def = NULL;
042a4ed7 230 show_rev(REVERSED, sha1);
800644c5
LT
231 continue;
232 }
023d66ed
LT
233 show_default();
234 show_norev(arg);
235 }
236 show_default();
237 if (single_rev && output_revs != 1) {
238 fprintf(stderr, "Needed a single revision\n");
239 exit(1);
178cb243 240 }
178cb243
LT
241 return 0;
242}