[PATCH] Do not send "want" lines for complete objects
[git.git] / rev-parse.c
1 /*
2  * rev-parse.c
3  *
4  * Copyright (C) Linus Torvalds, 2005
5  */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9 #include "quote.h"
10
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;
16
17 static char *def = NULL;
18
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;
24
25 static int revs_count = 0;
26
27 /*
28  * Some arguments are relevant "revision" arguments,
29  * others are about output format or other details.
30  * This sorts it all out.
31  */
32 static int is_rev_argument(const char *arg)
33 {
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
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);
59                 if (!strcmp(arg, str) ||
60                     (str[len-1] == '=' && !strncmp(arg, str, len)))
61                         return 1;
62         }
63 }
64
65 /* Output argument as a string, either SQ or normal */
66 static 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
84 /* Output a revision, only if filter allows it */
85 static void show_rev(int type, const unsigned char *sha1, const char *name)
86 {
87         if (!(filter & DO_REVS))
88                 return;
89         def = NULL;
90         revs_count++;
91
92         if (type != show_type)
93                 putchar('^');
94         if (symbolic && name)
95                 show(name);
96         else
97                 show(sha1_to_hex(sha1));
98 }
99
100 /* Output a flag, only if filter allows it. */
101 static void show_flag(char *arg)
102 {
103         if (!(filter & DO_FLAGS))
104                 return;
105         if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
106                 show(arg);
107 }
108
109 static void show_default(void)
110 {
111         char *s = def;
112
113         if (s) {
114                 unsigned char sha1[20];
115
116                 def = NULL;
117                 if (!get_sha1(s, sha1)) {
118                         show_rev(NORMAL, sha1, s);
119                         return;
120                 }
121         }
122 }
123
124 static int show_reference(const char *refname, const unsigned char *sha1)
125 {
126         show_rev(NORMAL, sha1, refname);
127         return 0;
128 }
129
130 static 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
154 static void show_file(const char *arg)
155 {
156         if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
157                 show(arg);
158 }
159
160 int main(int argc, char **argv)
161 {
162         int i, as_is = 0, verify = 0;
163         unsigned char sha1[20];
164         const char *prefix = setup_git_directory();
165         
166         for (i = 1; i < argc; i++) {
167                 char *arg = argv[i];
168                 char *dotdot;
169         
170                 if (as_is) {
171                         show_file(arg);
172                         continue;
173                 }
174                 if (*arg == '-') {
175                         if (!strcmp(arg, "--")) {
176                                 as_is = 1;
177                                 continue;
178                         }
179                         if (!strcmp(arg, "--default")) {
180                                 def = argv[i+1];
181                                 i++;
182                                 continue;
183                         }
184                         if (!strcmp(arg, "--revs-only")) {
185                                 filter &= ~DO_NOREV;
186                                 continue;
187                         }
188                         if (!strcmp(arg, "--no-revs")) {
189                                 filter &= ~DO_REVS;
190                                 continue;
191                         }
192                         if (!strcmp(arg, "--flags")) {
193                                 filter &= ~DO_NONFLAGS;
194                                 continue;
195                         }
196                         if (!strcmp(arg, "--no-flags")) {
197                                 filter &= ~DO_FLAGS;
198                                 continue;
199                         }
200                         if (!strcmp(arg, "--verify")) {
201                                 filter &= ~(DO_FLAGS|DO_NOREV);
202                                 verify = 1;
203                                 continue;
204                         }
205                         if (!strcmp(arg, "--sq")) {
206                                 output_sq = 1;
207                                 continue;
208                         }
209                         if (!strcmp(arg, "--not")) {
210                                 show_type ^= REVERSED;
211                                 continue;
212                         }
213                         if (!strcmp(arg, "--symbolic")) {
214                                 symbolic = 1;
215                                 continue;
216                         }
217                         if (!strcmp(arg, "--all")) {
218                                 for_each_ref(show_reference);
219                                 continue;
220                         }
221                         if (!strcmp(arg, "--show-prefix")) {
222                                 if (prefix)
223                                         puts(prefix);
224                                 continue;
225                         }
226                         if (!strcmp(arg, "--git-dir")) {
227                                 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
228                                 static char cwd[PATH_MAX];
229                                 if (gitdir) {
230                                         puts(gitdir);
231                                         continue;
232                                 }
233                                 if (!prefix) {
234                                         puts(".git");
235                                         continue;
236                                 }
237                                 if (!getcwd(cwd, PATH_MAX))
238                                         die("unable to get current working directory");
239                                 printf("%s/.git\n", cwd);
240                                 continue;
241                         }
242                         if (!strncmp(arg, "--since=", 8)) {
243                                 show_datestring("--max-age=", arg+8);
244                                 continue;
245                         }
246                         if (!strncmp(arg, "--after=", 8)) {
247                                 show_datestring("--max-age=", arg+8);
248                                 continue;
249                         }
250                         if (!strncmp(arg, "--before=", 9)) {
251                                 show_datestring("--min-age=", arg+9);
252                                 continue;
253                         }
254                         if (!strncmp(arg, "--until=", 8)) {
255                                 show_datestring("--min-age=", arg+8);
256                                 continue;
257                         }
258                         if (verify)
259                                 die("Needed a single revision");
260                         show_flag(arg);
261                         continue;
262                 }
263
264                 /* Not a flag argument */
265                 dotdot = strstr(arg, "..");
266                 if (dotdot) {
267                         unsigned char end[20];
268                         char *n = dotdot+2;
269                         *dotdot = 0;
270                         if (!get_sha1(arg, sha1)) {
271                                 if (!*n)
272                                         n = "HEAD";
273                                 if (!get_sha1(n, end)) {
274                                         show_rev(NORMAL, end, n);
275                                         show_rev(REVERSED, sha1, arg);
276                                         continue;
277                                 }
278                         }
279                         *dotdot = '.';
280                 }
281                 if (!get_sha1(arg, sha1)) {
282                         show_rev(NORMAL, sha1, arg);
283                         continue;
284                 }
285                 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
286                         show_rev(REVERSED, sha1, arg+1);
287                         continue;
288                 }
289                 if (verify)
290                         die("Needed a single revision");
291                 show_file(arg);
292         }
293         show_default();
294         if (verify && revs_count != 1)
295                 die("Needed a single revision");
296         return 0;
297 }