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 abbrev
= 0;
24 static int output_sq
= 0;
26 static int revs_count
= 0;
29 * Some arguments are relevant "revision" arguments,
30 * others are about output format or other details.
31 * This sorts it all out.
33 static int is_rev_argument(const char *arg
)
35 static const char *rev_args
[] = {
54 const char **p
= rev_args
;
57 const char *str
= *p
++;
62 if (!strcmp(arg
, str
) ||
63 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
68 /* Output argument as a string, either SQ or normal */
69 static void show(const char *arg
)
75 while ((ch
= *arg
++)) {
77 fputs("'\\'", stdout
);
87 /* Output a revision, only if filter allows it */
88 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
90 if (!(filter
& DO_REVS
))
95 if (type
!= show_type
)
100 show(find_unique_abbrev(sha1
, abbrev
));
102 show(sha1_to_hex(sha1
));
105 /* Output a flag, only if filter allows it. */
106 static void show_flag(char *arg
)
108 if (!(filter
& DO_FLAGS
))
110 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
))
114 static void show_default(void)
119 unsigned char sha1
[20];
122 if (!get_sha1(s
, sha1
)) {
123 show_rev(NORMAL
, sha1
, s
);
129 static int show_reference(const char *refname
, const unsigned char *sha1
)
131 show_rev(NORMAL
, sha1
, refname
);
135 static void show_datestring(const char *flag
, const char *datestr
)
137 static char buffer
[100];
139 /* date handling requires both flags and revs */
140 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
142 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
146 static void show_file(const char *arg
)
149 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
))
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
,"-n")) {
170 die("-n requires an argument");
171 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
177 if (!strncmp(arg
,"-n",2)) {
178 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
184 if (!strcmp(arg
, "--")) {
186 /* Pass on the "--" if we show anything but files.. */
187 if (filter
& (DO_FLAGS
| DO_REVS
))
191 if (!strcmp(arg
, "--default")) {
196 if (!strcmp(arg
, "--revs-only")) {
200 if (!strcmp(arg
, "--no-revs")) {
204 if (!strcmp(arg
, "--flags")) {
205 filter
&= ~DO_NONFLAGS
;
208 if (!strcmp(arg
, "--no-flags")) {
212 if (!strcmp(arg
, "--verify")) {
213 filter
&= ~(DO_FLAGS
|DO_NOREV
);
217 if (!strcmp(arg
, "--short") ||
218 !strncmp(arg
, "--short=", 9)) {
219 filter
&= ~(DO_FLAGS
|DO_NOREV
);
221 abbrev
= DEFAULT_ABBREV
;
223 abbrev
= strtoul(arg
+ 9, NULL
, 10);
224 if (abbrev
< MINIMUM_ABBREV
)
225 abbrev
= MINIMUM_ABBREV
;
226 else if (40 <= abbrev
)
230 if (!strcmp(arg
, "--sq")) {
234 if (!strcmp(arg
, "--not")) {
235 show_type
^= REVERSED
;
238 if (!strcmp(arg
, "--symbolic")) {
242 if (!strcmp(arg
, "--all")) {
243 for_each_ref(show_reference
);
246 if (!strcmp(arg
, "--show-prefix")) {
251 if (!strcmp(arg
, "--show-cdup")) {
252 const char *pfx
= prefix
;
254 pfx
= strchr(pfx
, '/');
263 if (!strcmp(arg
, "--git-dir")) {
264 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
265 static char cwd
[PATH_MAX
];
274 if (!getcwd(cwd
, PATH_MAX
))
275 die("unable to get current working directory");
276 printf("%s/.git\n", cwd
);
279 if (!strncmp(arg
, "--since=", 8)) {
280 show_datestring("--max-age=", arg
+8);
283 if (!strncmp(arg
, "--after=", 8)) {
284 show_datestring("--max-age=", arg
+8);
287 if (!strncmp(arg
, "--before=", 9)) {
288 show_datestring("--min-age=", arg
+9);
291 if (!strncmp(arg
, "--until=", 8)) {
292 show_datestring("--min-age=", arg
+8);
296 die("Needed a single revision");
301 /* Not a flag argument */
302 dotdot
= strstr(arg
, "..");
304 unsigned char end
[20];
307 if (!get_sha1(arg
, sha1
)) {
310 if (!get_sha1(n
, end
)) {
311 show_rev(NORMAL
, end
, n
);
312 show_rev(REVERSED
, sha1
, arg
);
318 if (!get_sha1(arg
, sha1
)) {
319 show_rev(NORMAL
, sha1
, arg
);
322 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
323 show_rev(REVERSED
, sha1
, arg
+1);
327 die("Needed a single revision");
328 if ((filter
& DO_REVS
) &&
329 (filter
& DO_NONFLAGS
) && /* !def && */
331 die("'%s': %s", arg
, strerror(errno
));
336 if (verify
&& revs_count
!= 1)
337 die("Needed a single revision");