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)
135 static char buffer[100];
136 static char cmd[1000];
139 /* date handling requires both flags and revs */
140 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
143 memcpy(buffer, flag, len);
145 snprintf(cmd, sizeof(cmd), "date --date=%s +%%s", sq_quote(datestr));
146 date = popen(cmd, "r");
147 if (!date || !fgets(buffer + len, sizeof(buffer) - len, date))
148 die("git-rev-list: bad date string");
150 len = strlen(buffer);
151 if (buffer[len-1] == '\n')
156 static void show_file(const char *arg)
159 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
163 int main(int argc, char **argv)
165 int i, as_is = 0, verify = 0;
166 unsigned char sha1[20];
167 const char *prefix = setup_git_directory();
169 for (i = 1; i < argc; i++) {
178 if (!strcmp(arg, "--")) {
180 /* Pass on the "--" if we show anything but files.. */
181 if (filter & (DO_FLAGS | DO_REVS))
185 if (!strcmp(arg, "--default")) {
190 if (!strcmp(arg, "--revs-only")) {
194 if (!strcmp(arg, "--no-revs")) {
198 if (!strcmp(arg, "--flags")) {
199 filter &= ~DO_NONFLAGS;
202 if (!strcmp(arg, "--no-flags")) {
206 if (!strcmp(arg, "--verify")) {
207 filter &= ~(DO_FLAGS|DO_NOREV);
211 if (!strcmp(arg, "--sq")) {
215 if (!strcmp(arg, "--not")) {
216 show_type ^= REVERSED;
219 if (!strcmp(arg, "--symbolic")) {
223 if (!strcmp(arg, "--all")) {
224 for_each_ref(show_reference);
227 if (!strcmp(arg, "--show-prefix")) {
232 if (!strcmp(arg, "--git-dir")) {
233 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
234 static char cwd[PATH_MAX];
243 if (!getcwd(cwd, PATH_MAX))
244 die("unable to get current working directory");
245 printf("%s/.git\n", cwd);
248 if (!strncmp(arg, "--since=", 8)) {
249 show_datestring("--max-age=", arg+8);
252 if (!strncmp(arg, "--after=", 8)) {
253 show_datestring("--max-age=", arg+8);
256 if (!strncmp(arg, "--before=", 9)) {
257 show_datestring("--min-age=", arg+9);
260 if (!strncmp(arg, "--until=", 8)) {
261 show_datestring("--min-age=", arg+8);
265 die("Needed a single revision");
270 /* Not a flag argument */
271 dotdot = strstr(arg, "..");
273 unsigned char end[20];
276 if (!get_sha1(arg, sha1)) {
279 if (!get_sha1(n, end)) {
280 show_rev(NORMAL, end, n);
281 show_rev(REVERSED, sha1, arg);
287 if (!get_sha1(arg, sha1)) {
288 show_rev(NORMAL, sha1, arg);
291 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
292 show_rev(REVERSED, sha1, arg+1);
296 die("Needed a single revision");
301 if (verify && revs_count != 1)
302 die("Needed a single revision");