4 * Copyright (C) Linus Torvalds, 2005
15 static int filter = ~0;
17 static char *def = NULL;
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[] = {
53 const char **p = rev_args;
56 const char *str = *p++;
61 if (!strcmp(arg, str) ||
62 (str[len-1] == '=' && !strncmp(arg, str, len)))
67 /* Output argument as a string, either SQ or normal */
68 static void show(const char *arg)
74 while ((ch = *arg++)) {
76 fputs("'\\'", stdout);
86 /* Output a revision, only if filter allows it */
87 static void show_rev(int type, const unsigned char *sha1, const char *name)
89 if (!(filter & DO_REVS))
94 if (type != show_type)
99 show(sha1_to_hex(sha1));
102 /* Output a flag, only if filter allows it. */
103 static void show_flag(char *arg)
105 if (!(filter & DO_FLAGS))
107 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
111 static void show_default(void)
116 unsigned char sha1[20];
119 if (!get_sha1(s, sha1)) {
120 show_rev(NORMAL, sha1, s);
126 static int show_reference(const char *refname, const unsigned char *sha1)
128 show_rev(NORMAL, sha1, refname);
132 static void show_datestring(const char *flag, const char *datestr)
134 static char buffer[100];
136 /* date handling requires both flags and revs */
137 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
139 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
143 static void show_file(const char *arg)
146 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
150 int main(int argc, char **argv)
152 int i, as_is = 0, verify = 0;
153 unsigned char sha1[20];
154 const char *prefix = setup_git_directory();
156 for (i = 1; i < argc; i++) {
165 if (!strcmp(arg, "--")) {
167 /* Pass on the "--" if we show anything but files.. */
168 if (filter & (DO_FLAGS | DO_REVS))
172 if (!strcmp(arg, "--default")) {
177 if (!strcmp(arg, "--revs-only")) {
181 if (!strcmp(arg, "--no-revs")) {
185 if (!strcmp(arg, "--flags")) {
186 filter &= ~DO_NONFLAGS;
189 if (!strcmp(arg, "--no-flags")) {
193 if (!strcmp(arg, "--verify")) {
194 filter &= ~(DO_FLAGS|DO_NOREV);
198 if (!strcmp(arg, "--sq")) {
202 if (!strcmp(arg, "--not")) {
203 show_type ^= REVERSED;
206 if (!strcmp(arg, "--symbolic")) {
210 if (!strcmp(arg, "--all")) {
211 for_each_ref(show_reference);
214 if (!strcmp(arg, "--show-prefix")) {
219 if (!strcmp(arg, "--show-cdup")) {
220 const char *pfx = prefix;
222 pfx = strchr(pfx, '/');
231 if (!strcmp(arg, "--git-dir")) {
232 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
233 static char cwd[PATH_MAX];
242 if (!getcwd(cwd, PATH_MAX))
243 die("unable to get current working directory");
244 printf("%s/.git\n", cwd);
247 if (!strncmp(arg, "--since=", 8)) {
248 show_datestring("--max-age=", arg+8);
251 if (!strncmp(arg, "--after=", 8)) {
252 show_datestring("--max-age=", arg+8);
255 if (!strncmp(arg, "--before=", 9)) {
256 show_datestring("--min-age=", arg+9);
259 if (!strncmp(arg, "--until=", 8)) {
260 show_datestring("--min-age=", arg+8);
264 die("Needed a single revision");
269 /* Not a flag argument */
270 dotdot = strstr(arg, "..");
272 unsigned char end[20];
275 if (!get_sha1(arg, sha1)) {
278 if (!get_sha1(n, end)) {
279 show_rev(NORMAL, end, n);
280 show_rev(REVERSED, sha1, arg);
286 if (!get_sha1(arg, sha1)) {
287 show_rev(NORMAL, sha1, arg);
290 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
291 show_rev(REVERSED, sha1, arg+1);
295 die("Needed a single revision");
300 if (verify && revs_count != 1)
301 die("Needed a single revision");