daemon: Set SO_REUSEADDR on listening sockets.
[git.git] / rev-parse.c
blob9cec33b8c00c322d6acde3e588a5c1dfe53f948d
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9 #include "quote.h"
11 #define DO_REVS 1
12 #define DO_NOREV 2
13 #define DO_FLAGS 4
14 #define DO_NONFLAGS 8
15 static int filter = ~0;
17 static char *def = NULL;
19 #define NORMAL 0
20 #define REVERSED 1
21 static int show_type = NORMAL;
22 static int symbolic = 0;
23 static int abbrev = 0;
24 static int output_sq = 0;
26 static int revs_count = 0;
29 * Some arguments are relevant "revision" arguments,
30 * others are about output format or other details.
31 * This sorts it all out.
33 static int is_rev_argument(const char *arg)
35 static const char *rev_args[] = {
36 "--all",
37 "--bisect",
38 "--dense",
39 "--header",
40 "--max-age=",
41 "--max-count=",
42 "--merge-order",
43 "--min-age=",
44 "--no-merges",
45 "--objects",
46 "--parents",
47 "--pretty",
48 "--show-breaks",
49 "--sparse",
50 "--topo-order",
51 "--unpacked",
52 NULL
54 const char **p = rev_args;
56 /* accept -<digit>, like traditional "head" */
57 if ((*arg == '-') && isdigit(arg[1]))
58 return 1;
60 for (;;) {
61 const char *str = *p++;
62 int len;
63 if (!str)
64 return 0;
65 len = strlen(str);
66 if (!strcmp(arg, str) ||
67 (str[len-1] == '=' && !strncmp(arg, str, len)))
68 return 1;
72 /* Output argument as a string, either SQ or normal */
73 static void show(const char *arg)
75 if (output_sq) {
76 int sq = '\'', ch;
78 putchar(sq);
79 while ((ch = *arg++)) {
80 if (ch == sq)
81 fputs("'\\'", stdout);
82 putchar(ch);
84 putchar(sq);
85 putchar(' ');
87 else
88 puts(arg);
91 /* Output a revision, only if filter allows it */
92 static void show_rev(int type, const unsigned char *sha1, const char *name)
94 if (!(filter & DO_REVS))
95 return;
96 def = NULL;
97 revs_count++;
99 if (type != show_type)
100 putchar('^');
101 if (symbolic && name)
102 show(name);
103 else if (abbrev)
104 show(find_unique_abbrev(sha1, abbrev));
105 else
106 show(sha1_to_hex(sha1));
109 /* Output a flag, only if filter allows it. */
110 static int show_flag(char *arg)
112 if (!(filter & DO_FLAGS))
113 return 0;
114 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
115 show(arg);
116 return 1;
118 return 0;
121 static void show_default(void)
123 char *s = def;
125 if (s) {
126 unsigned char sha1[20];
128 def = NULL;
129 if (!get_sha1(s, sha1)) {
130 show_rev(NORMAL, sha1, s);
131 return;
136 static int show_reference(const char *refname, const unsigned char *sha1)
138 show_rev(NORMAL, sha1, refname);
139 return 0;
142 static void show_datestring(const char *flag, const char *datestr)
144 static char buffer[100];
146 /* date handling requires both flags and revs */
147 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
148 return;
149 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
150 show(buffer);
153 static void show_file(const char *arg)
155 show_default();
156 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
157 show(arg);
160 int main(int argc, char **argv)
162 int i, as_is = 0, verify = 0;
163 unsigned char sha1[20];
164 const char *prefix = setup_git_directory();
166 for (i = 1; i < argc; i++) {
167 struct stat st;
168 char *arg = argv[i];
169 char *dotdot;
171 if (as_is) {
172 show_file(arg);
173 continue;
175 if (!strcmp(arg,"-n")) {
176 if (++i >= argc)
177 die("-n requires an argument");
178 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
179 show(arg);
180 show(argv[i]);
182 continue;
184 if (!strncmp(arg,"-n",2)) {
185 if ((filter & DO_FLAGS) && (filter & DO_REVS))
186 show(arg);
187 continue;
190 if (*arg == '-') {
191 if (!strcmp(arg, "--")) {
192 as_is = 1;
193 /* Pass on the "--" if we show anything but files.. */
194 if (filter & (DO_FLAGS | DO_REVS))
195 show_file(arg);
196 continue;
198 if (!strcmp(arg, "--default")) {
199 def = argv[i+1];
200 i++;
201 continue;
203 if (!strcmp(arg, "--revs-only")) {
204 filter &= ~DO_NOREV;
205 continue;
207 if (!strcmp(arg, "--no-revs")) {
208 filter &= ~DO_REVS;
209 continue;
211 if (!strcmp(arg, "--flags")) {
212 filter &= ~DO_NONFLAGS;
213 continue;
215 if (!strcmp(arg, "--no-flags")) {
216 filter &= ~DO_FLAGS;
217 continue;
219 if (!strcmp(arg, "--verify")) {
220 filter &= ~(DO_FLAGS|DO_NOREV);
221 verify = 1;
222 continue;
224 if (!strcmp(arg, "--short") ||
225 !strncmp(arg, "--short=", 9)) {
226 filter &= ~(DO_FLAGS|DO_NOREV);
227 verify = 1;
228 abbrev = DEFAULT_ABBREV;
229 if (arg[8] == '=')
230 abbrev = strtoul(arg + 9, NULL, 10);
231 if (abbrev < MINIMUM_ABBREV)
232 abbrev = MINIMUM_ABBREV;
233 else if (40 <= abbrev)
234 abbrev = 40;
235 continue;
237 if (!strcmp(arg, "--sq")) {
238 output_sq = 1;
239 continue;
241 if (!strcmp(arg, "--not")) {
242 show_type ^= REVERSED;
243 continue;
245 if (!strcmp(arg, "--symbolic")) {
246 symbolic = 1;
247 continue;
249 if (!strcmp(arg, "--all")) {
250 for_each_ref(show_reference);
251 continue;
253 if (!strcmp(arg, "--show-prefix")) {
254 if (prefix)
255 puts(prefix);
256 continue;
258 if (!strcmp(arg, "--show-cdup")) {
259 const char *pfx = prefix;
260 while (pfx) {
261 pfx = strchr(pfx, '/');
262 if (pfx) {
263 pfx++;
264 printf("../");
267 putchar('\n');
268 continue;
270 if (!strcmp(arg, "--git-dir")) {
271 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
272 static char cwd[PATH_MAX];
273 if (gitdir) {
274 puts(gitdir);
275 continue;
277 if (!prefix) {
278 puts(".git");
279 continue;
281 if (!getcwd(cwd, PATH_MAX))
282 die("unable to get current working directory");
283 printf("%s/.git\n", cwd);
284 continue;
286 if (!strncmp(arg, "--since=", 8)) {
287 show_datestring("--max-age=", arg+8);
288 continue;
290 if (!strncmp(arg, "--after=", 8)) {
291 show_datestring("--max-age=", arg+8);
292 continue;
294 if (!strncmp(arg, "--before=", 9)) {
295 show_datestring("--min-age=", arg+9);
296 continue;
298 if (!strncmp(arg, "--until=", 8)) {
299 show_datestring("--min-age=", arg+8);
300 continue;
302 if (show_flag(arg) && verify)
303 die("Needed a single revision");
304 continue;
307 /* Not a flag argument */
308 dotdot = strstr(arg, "..");
309 if (dotdot) {
310 unsigned char end[20];
311 char *n = dotdot+2;
312 *dotdot = 0;
313 if (!get_sha1(arg, sha1)) {
314 if (!*n)
315 n = "HEAD";
316 if (!get_sha1(n, end)) {
317 show_rev(NORMAL, end, n);
318 show_rev(REVERSED, sha1, arg);
319 continue;
322 *dotdot = '.';
324 if (!get_sha1(arg, sha1)) {
325 show_rev(NORMAL, sha1, arg);
326 continue;
328 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
329 show_rev(REVERSED, sha1, arg+1);
330 continue;
332 if (verify)
333 die("Needed a single revision");
334 if ((filter & DO_REVS) &&
335 (filter & DO_NONFLAGS) && /* !def && */
336 lstat(arg, &st) < 0)
337 die("'%s': %s", arg, strerror(errno));
338 as_is = 1;
339 show_file(arg);
341 show_default();
342 if (verify && revs_count != 1)
343 die("Needed a single revision");
344 return 0;