blob: 41b9dae429d2b423f57af4f4e091accfd9697022 [file] [log] [blame]
Linus Torvalds178cb242005-06-13 17:06:501/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Linus Torvaldsa8be83f2005-06-21 03:28:097#include "commit.h"
Linus Torvalds960bba02005-07-03 20:07:528#include "refs.h"
Linus Torvaldsc1babb12005-09-20 21:13:249#include "quote.h"
Linus Torvaldsa8be83f2005-06-21 03:28:0910
Junio C Hamano4866ccf2005-08-24 21:30:0411#define DO_REVS 1
12#define DO_NOREV 2
13#define DO_FLAGS 4
14#define DO_NONFLAGS 8
15static int filter = ~0;
16
Linus Torvalds023d66e2005-06-24 17:12:5517static char *def = NULL;
Linus Torvalds023d66e2005-06-24 17:12:5518
Linus Torvalds042a4ed2005-06-26 18:34:3019#define NORMAL 0
20#define REVERSED 1
21static int show_type = NORMAL;
Junio C Hamano4866ccf2005-08-24 21:30:0422static int symbolic = 0;
23static int output_sq = 0;
24
25static int revs_count = 0;
Linus Torvalds042a4ed2005-06-26 18:34:3026
Linus Torvalds921d8652005-06-13 18:14:2027/*
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[] = {
Junio C Hamanoe091eb92005-10-05 21:49:5435 "--all",
Junio C Hamano5ccfb752005-08-09 02:31:3736 "--bisect",
Junio C Hamano4866ccf2005-08-24 21:30:0437 "--header",
38 "--max-age=",
39 "--max-count=",
40 "--merge-order",
41 "--min-age=",
Junio C Hamano5ccfb752005-08-09 02:31:3742 "--no-merges",
Junio C Hamano4866ccf2005-08-24 21:30:0443 "--objects",
44 "--parents",
45 "--pretty",
46 "--show-breaks",
47 "--topo-order",
48 "--unpacked",
Linus Torvalds921d8652005-06-13 18:14:2049 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);
Junio C Hamano4866ccf2005-08-24 21:30:0459 if (!strcmp(arg, str) ||
60 (str[len-1] == '=' && !strncmp(arg, str, len)))
Linus Torvalds921d8652005-06-13 18:14:2061 return 1;
62 }
63}
64
Junio C Hamano4866ccf2005-08-24 21:30:0465/* Output argument as a string, either SQ or normal */
Junio C Hamano5bb2c652005-07-23 02:08:3266static 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
Junio C Hamano4866ccf2005-08-24 21:30:0484/* Output a revision, only if filter allows it */
Junio C Hamano30b96fc2005-08-16 19:36:4685static void show_rev(int type, const unsigned char *sha1, const char *name)
Linus Torvalds023d66e2005-06-24 17:12:5586{
Junio C Hamano4866ccf2005-08-24 21:30:0487 if (!(filter & DO_REVS))
Linus Torvalds023d66e2005-06-24 17:12:5588 return;
Junio C Hamano4866ccf2005-08-24 21:30:0489 def = NULL;
90 revs_count++;
Junio C Hamano5bb2c652005-07-23 02:08:3291
Junio C Hamano30b96fc2005-08-16 19:36:4692 if (type != show_type)
93 putchar('^');
94 if (symbolic && name)
95 show(name);
96 else
97 show(sha1_to_hex(sha1));
Linus Torvalds023d66e2005-06-24 17:12:5598}
99
Junio C Hamano4866ccf2005-08-24 21:30:04100/* Output a flag, only if filter allows it. */
101static void show_flag(char *arg)
Linus Torvalds023d66e2005-06-24 17:12:55102{
Junio C Hamano4866ccf2005-08-24 21:30:04103 if (!(filter & DO_FLAGS))
Linus Torvalds023d66e2005-06-24 17:12:55104 return;
Junio C Hamano4866ccf2005-08-24 21:30:04105 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
Linus Torvalds0360e992005-08-23 17:47:54106 show(arg);
Linus Torvalds023d66e2005-06-24 17:12:55107}
108
Linus Torvalds023d66e2005-06-24 17:12:55109static void show_default(void)
110{
111 char *s = def;
112
113 if (s) {
114 unsigned char sha1[20];
115
116 def = NULL;
Junio C Hamano9938af62005-08-04 05:15:49117 if (!get_sha1(s, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46118 show_rev(NORMAL, sha1, s);
Linus Torvalds023d66e2005-06-24 17:12:55119 return;
120 }
Linus Torvalds023d66e2005-06-24 17:12:55121 }
122}
123
Linus Torvalds960bba02005-07-03 20:07:52124static int show_reference(const char *refname, const unsigned char *sha1)
125{
Junio C Hamano30b96fc2005-08-16 19:36:46126 show_rev(NORMAL, sha1, refname);
Linus Torvalds960bba02005-07-03 20:07:52127 return 0;
128}
129
Linus Torvaldsc1babb12005-09-20 21:13:24130static 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
Linus Torvalds178cb242005-06-13 17:06:50154int main(int argc, char **argv)
155{
Junio C Hamano4866ccf2005-08-24 21:30:04156 int i, as_is = 0, verify = 0;
Linus Torvalds178cb242005-06-13 17:06:50157 unsigned char sha1[20];
Linus Torvaldsd288a702005-08-17 01:06:34158 const char *prefix = setup_git_directory();
159
Linus Torvalds178cb242005-06-13 17:06:50160 for (i = 1; i < argc; i++) {
161 char *arg = argv[i];
162 char *dotdot;
163
164 if (as_is) {
Junio C Hamano4866ccf2005-08-24 21:30:04165 show(arg);
Linus Torvalds178cb242005-06-13 17:06:50166 continue;
167 }
168 if (*arg == '-') {
169 if (!strcmp(arg, "--")) {
Linus Torvalds178cb242005-06-13 17:06:50170 as_is = 1;
Junio C Hamano4866ccf2005-08-24 21:30:04171 continue;
Linus Torvalds178cb242005-06-13 17:06:50172 }
173 if (!strcmp(arg, "--default")) {
Linus Torvalds178cb242005-06-13 17:06:50174 def = argv[i+1];
175 i++;
176 continue;
177 }
Linus Torvalds8ebb0182005-06-13 17:21:11178 if (!strcmp(arg, "--revs-only")) {
Junio C Hamano4866ccf2005-08-24 21:30:04179 filter &= ~DO_NOREV;
Linus Torvalds8ebb0182005-06-13 17:21:11180 continue;
181 }
182 if (!strcmp(arg, "--no-revs")) {
Junio C Hamano4866ccf2005-08-24 21:30:04183 filter &= ~DO_REVS;
Linus Torvalds8ebb0182005-06-13 17:21:11184 continue;
185 }
Linus Torvaldsf79b65a2005-07-06 17:08:08186 if (!strcmp(arg, "--flags")) {
Junio C Hamano4866ccf2005-08-24 21:30:04187 filter &= ~DO_NONFLAGS;
Linus Torvaldsf79b65a2005-07-06 17:08:08188 continue;
189 }
190 if (!strcmp(arg, "--no-flags")) {
Junio C Hamano4866ccf2005-08-24 21:30:04191 filter &= ~DO_FLAGS;
Linus Torvaldsf79b65a2005-07-06 17:08:08192 continue;
193 }
Linus Torvalds023d66e2005-06-24 17:12:55194 if (!strcmp(arg, "--verify")) {
Junio C Hamano4866ccf2005-08-24 21:30:04195 filter &= ~(DO_FLAGS|DO_NOREV);
196 verify = 1;
Linus Torvalds023d66e2005-06-24 17:12:55197 continue;
Linus Torvalds921d8652005-06-13 18:14:20198 }
Junio C Hamano5bb2c652005-07-23 02:08:32199 if (!strcmp(arg, "--sq")) {
200 output_sq = 1;
201 continue;
202 }
Linus Torvalds042a4ed2005-06-26 18:34:30203 if (!strcmp(arg, "--not")) {
204 show_type ^= REVERSED;
205 continue;
206 }
Junio C Hamano30b96fc2005-08-16 19:36:46207 if (!strcmp(arg, "--symbolic")) {
208 symbolic = 1;
209 continue;
210 }
Linus Torvalds960bba02005-07-03 20:07:52211 if (!strcmp(arg, "--all")) {
212 for_each_ref(show_reference);
213 continue;
214 }
Linus Torvaldsd288a702005-08-17 01:06:34215 if (!strcmp(arg, "--show-prefix")) {
Junio C Hamano4866ccf2005-08-24 21:30:04216 if (prefix)
217 puts(prefix);
Linus Torvaldsd288a702005-08-17 01:06:34218 continue;
219 }
Linus Torvaldsa8783ee2005-09-18 18:18:30220 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;
226 }
227 if (!prefix) {
228 puts(".git");
229 continue;
230 }
231 if (!getcwd(cwd, PATH_MAX))
232 die("unable to get current working directory");
233 printf("%s/.git\n", cwd);
234 continue;
235 }
Linus Torvaldsc1babb12005-09-20 21:13:24236 if (!strncmp(arg, "--since=", 8)) {
237 show_datestring("--max-age=", arg+8);
238 continue;
239 }
240 if (!strncmp(arg, "--after=", 8)) {
241 show_datestring("--max-age=", arg+8);
242 continue;
243 }
244 if (!strncmp(arg, "--before=", 9)) {
245 show_datestring("--min-age=", arg+9);
246 continue;
247 }
248 if (!strncmp(arg, "--until=", 8)) {
249 show_datestring("--min-age=", arg+8);
250 continue;
251 }
Junio C Hamano4866ccf2005-08-24 21:30:04252 if (verify)
253 die("Needed a single revision");
254 show_flag(arg);
Linus Torvalds178cb242005-06-13 17:06:50255 continue;
256 }
Junio C Hamano4866ccf2005-08-24 21:30:04257
258 /* Not a flag argument */
Linus Torvalds178cb242005-06-13 17:06:50259 dotdot = strstr(arg, "..");
260 if (dotdot) {
261 unsigned char end[20];
262 char *n = dotdot+2;
263 *dotdot = 0;
Junio C Hamano9938af62005-08-04 05:15:49264 if (!get_sha1(arg, sha1)) {
Linus Torvalds178cb242005-06-13 17:06:50265 if (!*n)
266 n = "HEAD";
Junio C Hamano9938af62005-08-04 05:15:49267 if (!get_sha1(n, end)) {
Junio C Hamano30b96fc2005-08-16 19:36:46268 show_rev(NORMAL, end, n);
269 show_rev(REVERSED, sha1, arg);
Linus Torvalds178cb242005-06-13 17:06:50270 continue;
271 }
272 }
273 *dotdot = '.';
274 }
Junio C Hamano9938af62005-08-04 05:15:49275 if (!get_sha1(arg, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46276 show_rev(NORMAL, sha1, arg);
Linus Torvalds800644c2005-06-20 15:29:13277 continue;
278 }
Junio C Hamano9938af62005-08-04 05:15:49279 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46280 show_rev(REVERSED, sha1, arg+1);
Linus Torvalds800644c2005-06-20 15:29:13281 continue;
282 }
Junio C Hamano4866ccf2005-08-24 21:30:04283 if (verify)
284 die("Needed a single revision");
285 if ((filter & (DO_NONFLAGS|DO_NOREV)) ==
286 (DO_NONFLAGS|DO_NOREV))
287 show(arg);
Linus Torvalds178cb242005-06-13 17:06:50288 }
Linus Torvalds023d66e2005-06-24 17:12:55289 show_default();
Junio C Hamano4866ccf2005-08-24 21:30:04290 if (verify && revs_count != 1)
291 die("Needed a single revision");
Linus Torvalds178cb242005-06-13 17:06:50292 return 0;
293}