Update documentation for C-style quoting.
[git/dscho.git] / rev-parse.c
blob41b9dae429d2b423f57af4f4e091accfd9697022
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 output_sq = 0;
25 static int revs_count = 0;
28 * Some arguments are relevant "revision" arguments,
29 * others are about output format or other details.
30 * This sorts it all out.
32 static int is_rev_argument(const char *arg)
34 static const char *rev_args[] = {
35 "--all",
36 "--bisect",
37 "--header",
38 "--max-age=",
39 "--max-count=",
40 "--merge-order",
41 "--min-age=",
42 "--no-merges",
43 "--objects",
44 "--parents",
45 "--pretty",
46 "--show-breaks",
47 "--topo-order",
48 "--unpacked",
49 NULL
51 const char **p = rev_args;
53 for (;;) {
54 const char *str = *p++;
55 int len;
56 if (!str)
57 return 0;
58 len = strlen(str);
59 if (!strcmp(arg, str) ||
60 (str[len-1] == '=' && !strncmp(arg, str, len)))
61 return 1;
65 /* Output argument as a string, either SQ or normal */
66 static void show(const char *arg)
68 if (output_sq) {
69 int sq = '\'', ch;
71 putchar(sq);
72 while ((ch = *arg++)) {
73 if (ch == sq)
74 fputs("'\\'", stdout);
75 putchar(ch);
77 putchar(sq);
78 putchar(' ');
80 else
81 puts(arg);
84 /* Output a revision, only if filter allows it */
85 static void show_rev(int type, const unsigned char *sha1, const char *name)
87 if (!(filter & DO_REVS))
88 return;
89 def = NULL;
90 revs_count++;
92 if (type != show_type)
93 putchar('^');
94 if (symbolic && name)
95 show(name);
96 else
97 show(sha1_to_hex(sha1));
100 /* Output a flag, only if filter allows it. */
101 static void show_flag(char *arg)
103 if (!(filter & DO_FLAGS))
104 return;
105 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
106 show(arg);
109 static void show_default(void)
111 char *s = def;
113 if (s) {
114 unsigned char sha1[20];
116 def = NULL;
117 if (!get_sha1(s, sha1)) {
118 show_rev(NORMAL, sha1, s);
119 return;
124 static int show_reference(const char *refname, const unsigned char *sha1)
126 show_rev(NORMAL, sha1, refname);
127 return 0;
130 static void show_datestring(const char *flag, const char *datestr)
132 FILE *date;
133 static char buffer[100];
134 static char cmd[1000];
135 int len;
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);
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);
154 int main(int argc, char **argv)
156 int i, as_is = 0, verify = 0;
157 unsigned char sha1[20];
158 const char *prefix = setup_git_directory();
160 for (i = 1; i < argc; i++) {
161 char *arg = argv[i];
162 char *dotdot;
164 if (as_is) {
165 show(arg);
166 continue;
168 if (*arg == '-') {
169 if (!strcmp(arg, "--")) {
170 as_is = 1;
171 continue;
173 if (!strcmp(arg, "--default")) {
174 def = argv[i+1];
175 i++;
176 continue;
178 if (!strcmp(arg, "--revs-only")) {
179 filter &= ~DO_NOREV;
180 continue;
182 if (!strcmp(arg, "--no-revs")) {
183 filter &= ~DO_REVS;
184 continue;
186 if (!strcmp(arg, "--flags")) {
187 filter &= ~DO_NONFLAGS;
188 continue;
190 if (!strcmp(arg, "--no-flags")) {
191 filter &= ~DO_FLAGS;
192 continue;
194 if (!strcmp(arg, "--verify")) {
195 filter &= ~(DO_FLAGS|DO_NOREV);
196 verify = 1;
197 continue;
199 if (!strcmp(arg, "--sq")) {
200 output_sq = 1;
201 continue;
203 if (!strcmp(arg, "--not")) {
204 show_type ^= REVERSED;
205 continue;
207 if (!strcmp(arg, "--symbolic")) {
208 symbolic = 1;
209 continue;
211 if (!strcmp(arg, "--all")) {
212 for_each_ref(show_reference);
213 continue;
215 if (!strcmp(arg, "--show-prefix")) {
216 if (prefix)
217 puts(prefix);
218 continue;
220 if (!strcmp(arg, "--git-dir")) {
221 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
222 static char cwd[PATH_MAX];
223 if (gitdir) {
224 puts(gitdir);
225 continue;
227 if (!prefix) {
228 puts(".git");
229 continue;
231 if (!getcwd(cwd, PATH_MAX))
232 die("unable to get current working directory");
233 printf("%s/.git\n", cwd);
234 continue;
236 if (!strncmp(arg, "--since=", 8)) {
237 show_datestring("--max-age=", arg+8);
238 continue;
240 if (!strncmp(arg, "--after=", 8)) {
241 show_datestring("--max-age=", arg+8);
242 continue;
244 if (!strncmp(arg, "--before=", 9)) {
245 show_datestring("--min-age=", arg+9);
246 continue;
248 if (!strncmp(arg, "--until=", 8)) {
249 show_datestring("--min-age=", arg+8);
250 continue;
252 if (verify)
253 die("Needed a single revision");
254 show_flag(arg);
255 continue;
258 /* Not a flag argument */
259 dotdot = strstr(arg, "..");
260 if (dotdot) {
261 unsigned char end[20];
262 char *n = dotdot+2;
263 *dotdot = 0;
264 if (!get_sha1(arg, sha1)) {
265 if (!*n)
266 n = "HEAD";
267 if (!get_sha1(n, end)) {
268 show_rev(NORMAL, end, n);
269 show_rev(REVERSED, sha1, arg);
270 continue;
273 *dotdot = '.';
275 if (!get_sha1(arg, sha1)) {
276 show_rev(NORMAL, sha1, arg);
277 continue;
279 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
280 show_rev(REVERSED, sha1, arg+1);
281 continue;
283 if (verify)
284 die("Needed a single revision");
285 if ((filter & (DO_NONFLAGS|DO_NOREV)) ==
286 (DO_NONFLAGS|DO_NOREV))
287 show(arg);
289 show_default();
290 if (verify && revs_count != 1)
291 die("Needed a single revision");
292 return 0;