4 * Copyright (C) Linus Torvalds, 2005
14 static int filter = ~0;
16 static char *def = NULL;
20 static int show_type = NORMAL;
21 static int symbolic = 0;
22 static int output_sq = 0;
24 static int revs_count = 0;
27 * Some arguments are relevant "revision" arguments,
28 * others are about output format or other details.
29 * This sorts it all out.
31 static int is_rev_argument(const char *arg)
33 static const char *rev_args[] = {
49 const char **p = rev_args;
52 const char *str = *p++;
57 if (!strcmp(arg, str) ||
58 (str[len-1] == '=' && !strncmp(arg, str, len)))
63 /* Output argument as a string, either SQ or normal */
64 static void show(const char *arg)
70 while ((ch = *arg++)) {
72 fputs("'\\'", stdout);
82 /* Output a revision, only if filter allows it */
83 static void show_rev(int type, const unsigned char *sha1, const char *name)
85 if (!(filter & DO_REVS))
90 if (type != show_type)
95 show(sha1_to_hex(sha1));
98 /* Output a flag, only if filter allows it. */
99 static void show_flag(char *arg)
101 if (!(filter & DO_FLAGS))
103 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
107 static void show_default(void)
112 unsigned char sha1[20];
115 if (!get_sha1(s, sha1)) {
116 show_rev(NORMAL, sha1, s);
122 static int show_reference(const char *refname, const unsigned char *sha1)
124 show_rev(NORMAL, sha1, refname);
128 int main(int argc, char **argv)
130 int i, as_is = 0, verify = 0;
131 unsigned char sha1[20];
132 const char *prefix = setup_git_directory();
134 for (i = 1; i < argc; i++) {
143 if (!strcmp(arg, "--")) {
147 if (!strcmp(arg, "--default")) {
152 if (!strcmp(arg, "--revs-only")) {
156 if (!strcmp(arg, "--no-revs")) {
160 if (!strcmp(arg, "--flags")) {
161 filter &= ~DO_NONFLAGS;
164 if (!strcmp(arg, "--no-flags")) {
168 if (!strcmp(arg, "--verify")) {
169 filter &= ~(DO_FLAGS|DO_NOREV);
173 if (!strcmp(arg, "--sq")) {
177 if (!strcmp(arg, "--not")) {
178 show_type ^= REVERSED;
181 if (!strcmp(arg, "--symbolic")) {
185 if (!strcmp(arg, "--all")) {
186 for_each_ref(show_reference);
189 if (!strcmp(arg, "--show-prefix")) {
195 die("Needed a single revision");
200 /* Not a flag argument */
201 dotdot = strstr(arg, "..");
203 unsigned char end[20];
206 if (!get_sha1(arg, sha1)) {
209 if (!get_sha1(n, end)) {
210 show_rev(NORMAL, end, n);
211 show_rev(REVERSED, sha1, arg);
217 if (!get_sha1(arg, sha1)) {
218 show_rev(NORMAL, sha1, arg);
221 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
222 show_rev(REVERSED, sha1, arg+1);
226 die("Needed a single revision");
227 if ((filter & (DO_NONFLAGS|DO_NOREV)) ==
228 (DO_NONFLAGS|DO_NOREV))
232 if (verify && revs_count != 1)
233 die("Needed a single revision");