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
[] = {
50 const char **p
= rev_args
;
53 const char *str
= *p
++;
58 if (!strcmp(arg
, str
) ||
59 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
64 /* Output argument as a string, either SQ or normal */
65 static void show(const char *arg
)
71 while ((ch
= *arg
++)) {
73 fputs("'\\'", stdout
);
83 /* Output a revision, only if filter allows it */
84 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
86 if (!(filter
& DO_REVS
))
91 if (type
!= show_type
)
96 show(sha1_to_hex(sha1
));
99 /* Output a flag, only if filter allows it. */
100 static void show_flag(char *arg
)
102 if (!(filter
& DO_FLAGS
))
104 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
))
108 static void show_default(void)
113 unsigned char sha1
[20];
116 if (!get_sha1(s
, sha1
)) {
117 show_rev(NORMAL
, sha1
, s
);
123 static int show_reference(const char *refname
, const unsigned char *sha1
)
125 show_rev(NORMAL
, sha1
, refname
);
129 static void show_datestring(const char *flag
, const char *datestr
)
132 static char buffer
[100];
133 static char cmd
[1000];
136 /* date handling requires both flags and revs */
137 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
140 memcpy(buffer
, flag
, len
);
142 snprintf(cmd
, sizeof(cmd
), "date --date=%s +%%s", sq_quote(datestr
));
143 date
= popen(cmd
, "r");
144 if (!date
|| !fgets(buffer
+ len
, sizeof(buffer
) - len
, date
))
145 die("git-rev-list: bad date string");
147 len
= strlen(buffer
);
148 if (buffer
[len
-1] == '\n')
153 int main(int argc
, char **argv
)
155 int i
, as_is
= 0, verify
= 0;
156 unsigned char sha1
[20];
157 const char *prefix
= setup_git_directory();
159 for (i
= 1; i
< argc
; i
++) {
168 if (!strcmp(arg
, "--")) {
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
, "--git-dir")) {
220 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
221 static char cwd
[PATH_MAX
];
230 if (!getcwd(cwd
, PATH_MAX
))
231 die("unable to get current working directory");
232 printf("%s/.git\n", cwd
);
235 if (!strncmp(arg
, "--since=", 8)) {
236 show_datestring("--max-age=", arg
+8);
239 if (!strncmp(arg
, "--after=", 8)) {
240 show_datestring("--max-age=", arg
+8);
243 if (!strncmp(arg
, "--before=", 9)) {
244 show_datestring("--min-age=", arg
+9);
247 if (!strncmp(arg
, "--until=", 8)) {
248 show_datestring("--min-age=", arg
+8);
252 die("Needed a single revision");
257 /* Not a flag argument */
258 dotdot
= strstr(arg
, "..");
260 unsigned char end
[20];
263 if (!get_sha1(arg
, sha1
)) {
266 if (!get_sha1(n
, end
)) {
267 show_rev(NORMAL
, end
, n
);
268 show_rev(REVERSED
, sha1
, arg
);
274 if (!get_sha1(arg
, sha1
)) {
275 show_rev(NORMAL
, sha1
, arg
);
278 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
279 show_rev(REVERSED
, sha1
, arg
+1);
283 die("Needed a single revision");
284 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) ==
285 (DO_NONFLAGS
|DO_NOREV
))
289 if (verify
&& revs_count
!= 1)
290 die("Needed a single revision");