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
[] = {
51 const char **p
= rev_args
;
54 const char *str
= *p
++;
59 if (!strcmp(arg
, str
) ||
60 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
65 /* Output argument as a string, either SQ or normal */
66 static void show(const char *arg
)
72 while ((ch
= *arg
++)) {
74 fputs("'\\'", stdout
);
84 /* Output a revision, only if filter allows it */
85 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
87 if (!(filter
& DO_REVS
))
92 if (type
!= show_type
)
97 show(sha1_to_hex(sha1
));
100 /* Output a flag, only if filter allows it. */
101 static void show_flag(char *arg
)
103 if (!(filter
& DO_FLAGS
))
105 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
))
109 static void show_default(void)
114 unsigned char sha1
[20];
117 if (!get_sha1(s
, sha1
)) {
118 show_rev(NORMAL
, sha1
, s
);
124 static int show_reference(const char *refname
, const unsigned char *sha1
)
126 show_rev(NORMAL
, sha1
, refname
);
130 static void show_datestring(const char *flag
, const char *datestr
)
133 static char buffer
[100];
134 static char cmd
[1000];
137 /* date handling requires both flags and revs */
138 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
141 memcpy(buffer
, flag
, len
);
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");
148 len
= strlen(buffer
);
149 if (buffer
[len
-1] == '\n')
154 static void show_file(const char *arg
)
157 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
))
161 int main(int argc
, char **argv
)
163 int i
, as_is
= 0, verify
= 0;
164 unsigned char sha1
[20];
165 const char *prefix
= setup_git_directory();
167 for (i
= 1; i
< argc
; i
++) {
176 if (!strcmp(arg
, "--")) {
178 /* Pass on the "--" if we show anything but files.. */
179 if (filter
& (DO_FLAGS
| DO_REVS
))
183 if (!strcmp(arg
, "--default")) {
188 if (!strcmp(arg
, "--revs-only")) {
192 if (!strcmp(arg
, "--no-revs")) {
196 if (!strcmp(arg
, "--flags")) {
197 filter
&= ~DO_NONFLAGS
;
200 if (!strcmp(arg
, "--no-flags")) {
204 if (!strcmp(arg
, "--verify")) {
205 filter
&= ~(DO_FLAGS
|DO_NOREV
);
209 if (!strcmp(arg
, "--sq")) {
213 if (!strcmp(arg
, "--not")) {
214 show_type
^= REVERSED
;
217 if (!strcmp(arg
, "--symbolic")) {
221 if (!strcmp(arg
, "--all")) {
222 for_each_ref(show_reference
);
225 if (!strcmp(arg
, "--show-prefix")) {
230 if (!strcmp(arg
, "--git-dir")) {
231 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
232 static char cwd
[PATH_MAX
];
241 if (!getcwd(cwd
, PATH_MAX
))
242 die("unable to get current working directory");
243 printf("%s/.git\n", cwd
);
246 if (!strncmp(arg
, "--since=", 8)) {
247 show_datestring("--max-age=", arg
+8);
250 if (!strncmp(arg
, "--after=", 8)) {
251 show_datestring("--max-age=", arg
+8);
254 if (!strncmp(arg
, "--before=", 9)) {
255 show_datestring("--min-age=", arg
+9);
258 if (!strncmp(arg
, "--until=", 8)) {
259 show_datestring("--min-age=", arg
+8);
263 die("Needed a single revision");
268 /* Not a flag argument */
269 dotdot
= strstr(arg
, "..");
271 unsigned char end
[20];
274 if (!get_sha1(arg
, sha1
)) {
277 if (!get_sha1(n
, end
)) {
278 show_rev(NORMAL
, end
, n
);
279 show_rev(REVERSED
, sha1
, arg
);
285 if (!get_sha1(arg
, sha1
)) {
286 show_rev(NORMAL
, sha1
, arg
);
289 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
290 show_rev(REVERSED
, sha1
, arg
+1);
294 die("Needed a single revision");
299 if (verify
&& revs_count
!= 1)
300 die("Needed a single revision");