]> git.ipfire.org Git - thirdparty/git.git/blame - rev-parse.c
Really require tk 8.4 (RPM)
[thirdparty/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"
c1babb1d 9#include "quote.h"
a8be83fe 10
4866ccf0
JH
11#define DO_REVS 1
12#define DO_NOREV 2
13#define DO_FLAGS 4
14#define DO_NONFLAGS 8
15static int filter = ~0;
16
023d66ed 17static char *def = NULL;
023d66ed 18
042a4ed7
LT
19#define NORMAL 0
20#define REVERSED 1
21static int show_type = NORMAL;
4866ccf0
JH
22static int symbolic = 0;
23static int output_sq = 0;
24
25static int revs_count = 0;
042a4ed7 26
921d865e
LT
27/*
28 * Some arguments are relevant "revision" arguments,
29 * others are about output format or other details.
30 * This sorts it all out.
31 */
32static int is_rev_argument(const char *arg)
33{
34 static const char *rev_args[] = {
4866ccf0
JH
35 "--bisect",
36 "--header",
921d865e 37 "--max-age=",
4866ccf0 38 "--max-count=",
921d865e 39 "--merge-order",
4866ccf0 40 "--min-age=",
5ccfb758 41 "--no-merges",
4866ccf0
JH
42 "--objects",
43 "--parents",
44 "--pretty",
45 "--show-breaks",
46 "--topo-order",
47 "--unpacked",
921d865e
LT
48 NULL
49 };
50 const char **p = rev_args;
51
52 for (;;) {
53 const char *str = *p++;
54 int len;
55 if (!str)
56 return 0;
57 len = strlen(str);
4866ccf0
JH
58 if (!strcmp(arg, str) ||
59 (str[len-1] == '=' && !strncmp(arg, str, len)))
921d865e
LT
60 return 1;
61 }
62}
63
4866ccf0 64/* Output argument as a string, either SQ or normal */
5bb2c65a
JH
65static void show(const char *arg)
66{
67 if (output_sq) {
68 int sq = '\'', ch;
69
70 putchar(sq);
71 while ((ch = *arg++)) {
72 if (ch == sq)
73 fputs("'\\'", stdout);
74 putchar(ch);
75 }
76 putchar(sq);
77 putchar(' ');
78 }
79 else
80 puts(arg);
81}
82
4866ccf0 83/* Output a revision, only if filter allows it */
30b96fce 84static void show_rev(int type, const unsigned char *sha1, const char *name)
023d66ed 85{
4866ccf0 86 if (!(filter & DO_REVS))
023d66ed 87 return;
4866ccf0
JH
88 def = NULL;
89 revs_count++;
5bb2c65a 90
30b96fce
JH
91 if (type != show_type)
92 putchar('^');
93 if (symbolic && name)
94 show(name);
95 else
96 show(sha1_to_hex(sha1));
023d66ed
LT
97}
98
4866ccf0
JH
99/* Output a flag, only if filter allows it. */
100static void show_flag(char *arg)
023d66ed 101{
4866ccf0 102 if (!(filter & DO_FLAGS))
023d66ed 103 return;
4866ccf0 104 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
0360e99d 105 show(arg);
023d66ed
LT
106}
107
023d66ed
LT
108static void show_default(void)
109{
110 char *s = def;
111
112 if (s) {
113 unsigned char sha1[20];
114
115 def = NULL;
9938af6a 116 if (!get_sha1(s, sha1)) {
30b96fce 117 show_rev(NORMAL, sha1, s);
023d66ed
LT
118 return;
119 }
023d66ed
LT
120 }
121}
122
960bba0d
LT
123static int show_reference(const char *refname, const unsigned char *sha1)
124{
30b96fce 125 show_rev(NORMAL, sha1, refname);
960bba0d
LT
126 return 0;
127}
128
c1babb1d
LT
129static void show_datestring(const char *flag, const char *datestr)
130{
131 FILE *date;
132 static char buffer[100];
133 static char cmd[1000];
134 int len;
135
136 /* date handling requires both flags and revs */
137 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
138 return;
139 len = strlen(flag);
140 memcpy(buffer, flag, len);
141
142 snprintf(cmd, sizeof(cmd), "date --date=%s +%%s", sq_quote(datestr));
143 date = popen(cmd, "r");
144 if (!date || !fgets(buffer + len, sizeof(buffer) - len, date))
145 die("git-rev-list: bad date string");
146 pclose(date);
147 len = strlen(buffer);
148 if (buffer[len-1] == '\n')
149 buffer[--len] = 0;
150 show(buffer);
151}
152
178cb243
LT
153int main(int argc, char **argv)
154{
4866ccf0 155 int i, as_is = 0, verify = 0;
178cb243 156 unsigned char sha1[20];
d288a700
LT
157 const char *prefix = setup_git_directory();
158
178cb243
LT
159 for (i = 1; i < argc; i++) {
160 char *arg = argv[i];
161 char *dotdot;
162
163 if (as_is) {
4866ccf0 164 show(arg);
178cb243
LT
165 continue;
166 }
167 if (*arg == '-') {
168 if (!strcmp(arg, "--")) {
178cb243 169 as_is = 1;
4866ccf0 170 continue;
178cb243
LT
171 }
172 if (!strcmp(arg, "--default")) {
178cb243
LT
173 def = argv[i+1];
174 i++;
175 continue;
176 }
8ebb0184 177 if (!strcmp(arg, "--revs-only")) {
4866ccf0 178 filter &= ~DO_NOREV;
8ebb0184
LT
179 continue;
180 }
181 if (!strcmp(arg, "--no-revs")) {
4866ccf0 182 filter &= ~DO_REVS;
8ebb0184
LT
183 continue;
184 }
f79b65aa 185 if (!strcmp(arg, "--flags")) {
4866ccf0 186 filter &= ~DO_NONFLAGS;
f79b65aa
LT
187 continue;
188 }
189 if (!strcmp(arg, "--no-flags")) {
4866ccf0 190 filter &= ~DO_FLAGS;
f79b65aa
LT
191 continue;
192 }
023d66ed 193 if (!strcmp(arg, "--verify")) {
4866ccf0
JH
194 filter &= ~(DO_FLAGS|DO_NOREV);
195 verify = 1;
023d66ed 196 continue;
921d865e 197 }
5bb2c65a
JH
198 if (!strcmp(arg, "--sq")) {
199 output_sq = 1;
200 continue;
201 }
042a4ed7
LT
202 if (!strcmp(arg, "--not")) {
203 show_type ^= REVERSED;
204 continue;
205 }
30b96fce
JH
206 if (!strcmp(arg, "--symbolic")) {
207 symbolic = 1;
208 continue;
209 }
960bba0d
LT
210 if (!strcmp(arg, "--all")) {
211 for_each_ref(show_reference);
212 continue;
213 }
d288a700 214 if (!strcmp(arg, "--show-prefix")) {
4866ccf0
JH
215 if (prefix)
216 puts(prefix);
d288a700
LT
217 continue;
218 }
a8783eeb
LT
219 if (!strcmp(arg, "--git-dir")) {
220 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
221 static char cwd[PATH_MAX];
222 if (gitdir) {
223 puts(gitdir);
224 continue;
225 }
226 if (!prefix) {
227 puts(".git");
228 continue;
229 }
230 if (!getcwd(cwd, PATH_MAX))
231 die("unable to get current working directory");
232 printf("%s/.git\n", cwd);
233 continue;
234 }
c1babb1d
LT
235 if (!strncmp(arg, "--since=", 8)) {
236 show_datestring("--max-age=", arg+8);
237 continue;
238 }
239 if (!strncmp(arg, "--after=", 8)) {
240 show_datestring("--max-age=", arg+8);
241 continue;
242 }
243 if (!strncmp(arg, "--before=", 9)) {
244 show_datestring("--min-age=", arg+9);
245 continue;
246 }
247 if (!strncmp(arg, "--until=", 8)) {
248 show_datestring("--min-age=", arg+8);
249 continue;
250 }
4866ccf0
JH
251 if (verify)
252 die("Needed a single revision");
253 show_flag(arg);
178cb243
LT
254 continue;
255 }
4866ccf0
JH
256
257 /* Not a flag argument */
178cb243
LT
258 dotdot = strstr(arg, "..");
259 if (dotdot) {
260 unsigned char end[20];
261 char *n = dotdot+2;
262 *dotdot = 0;
9938af6a 263 if (!get_sha1(arg, sha1)) {
178cb243
LT
264 if (!*n)
265 n = "HEAD";
9938af6a 266 if (!get_sha1(n, end)) {
30b96fce
JH
267 show_rev(NORMAL, end, n);
268 show_rev(REVERSED, sha1, arg);
178cb243
LT
269 continue;
270 }
271 }
272 *dotdot = '.';
273 }
9938af6a 274 if (!get_sha1(arg, sha1)) {
30b96fce 275 show_rev(NORMAL, sha1, arg);
800644c5
LT
276 continue;
277 }
9938af6a 278 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
30b96fce 279 show_rev(REVERSED, sha1, arg+1);
800644c5
LT
280 continue;
281 }
4866ccf0
JH
282 if (verify)
283 die("Needed a single revision");
284 if ((filter & (DO_NONFLAGS|DO_NOREV)) ==
285 (DO_NONFLAGS|DO_NOREV))
286 show(arg);
023d66ed
LT
287 }
288 show_default();
4866ccf0
JH
289 if (verify && revs_count != 1)
290 die("Needed a single revision");
178cb243
LT
291 return 0;
292}