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