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
[] = {
55 const char **p
= rev_args
;
57 /* accept -<digit>, like traditional "head" */
58 if ((*arg
== '-') && isdigit(arg
[1]))
62 const char *str
= *p
++;
67 if (!strcmp(arg
, str
) ||
68 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
73 /* Output argument as a string, either SQ or normal */
74 static void show(const char *arg
)
80 while ((ch
= *arg
++)) {
82 fputs("'\\'", stdout
);
92 /* Output a revision, only if filter allows it */
93 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
95 if (!(filter
& DO_REVS
))
100 if (type
!= show_type
)
102 if (symbolic
&& name
)
105 show(find_unique_abbrev(sha1
, abbrev
));
107 show(sha1_to_hex(sha1
));
110 /* Output a flag, only if filter allows it. */
111 static int show_flag(char *arg
)
113 if (!(filter
& DO_FLAGS
))
115 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
122 static void show_default(void)
127 unsigned char sha1
[20];
130 if (!get_sha1(s
, sha1
)) {
131 show_rev(NORMAL
, sha1
, s
);
137 static int show_reference(const char *refname
, const unsigned char *sha1
)
139 show_rev(NORMAL
, sha1
, refname
);
143 static void show_datestring(const char *flag
, const char *datestr
)
145 static char buffer
[100];
147 /* date handling requires both flags and revs */
148 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
150 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
154 static int show_file(const char *arg
)
157 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
164 int main(int argc
, char **argv
)
166 int i
, as_is
= 0, verify
= 0;
167 unsigned char sha1
[20];
168 const char *prefix
= setup_git_directory();
170 for (i
= 1; i
< argc
; i
++) {
179 if (!strcmp(arg
,"-n")) {
181 die("-n requires an argument");
182 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
188 if (!strncmp(arg
,"-n",2)) {
189 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
195 if (!strcmp(arg
, "--")) {
197 /* Pass on the "--" if we show anything but files.. */
198 if (filter
& (DO_FLAGS
| DO_REVS
))
202 if (!strcmp(arg
, "--default")) {
207 if (!strcmp(arg
, "--revs-only")) {
211 if (!strcmp(arg
, "--no-revs")) {
215 if (!strcmp(arg
, "--flags")) {
216 filter
&= ~DO_NONFLAGS
;
219 if (!strcmp(arg
, "--no-flags")) {
223 if (!strcmp(arg
, "--verify")) {
224 filter
&= ~(DO_FLAGS
|DO_NOREV
);
228 if (!strcmp(arg
, "--short") ||
229 !strncmp(arg
, "--short=", 8)) {
230 filter
&= ~(DO_FLAGS
|DO_NOREV
);
232 abbrev
= DEFAULT_ABBREV
;
234 abbrev
= strtoul(arg
+ 8, NULL
, 10);
235 if (abbrev
< MINIMUM_ABBREV
)
236 abbrev
= MINIMUM_ABBREV
;
237 else if (40 <= abbrev
)
241 if (!strcmp(arg
, "--sq")) {
245 if (!strcmp(arg
, "--not")) {
246 show_type
^= REVERSED
;
249 if (!strcmp(arg
, "--symbolic")) {
253 if (!strcmp(arg
, "--all")) {
254 for_each_ref(show_reference
);
257 if (!strcmp(arg
, "--show-prefix")) {
262 if (!strcmp(arg
, "--show-cdup")) {
263 const char *pfx
= prefix
;
265 pfx
= strchr(pfx
, '/');
274 if (!strcmp(arg
, "--git-dir")) {
275 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
276 static char cwd
[PATH_MAX
];
285 if (!getcwd(cwd
, PATH_MAX
))
286 die("unable to get current working directory");
287 printf("%s/.git\n", cwd
);
290 if (!strncmp(arg
, "--since=", 8)) {
291 show_datestring("--max-age=", arg
+8);
294 if (!strncmp(arg
, "--after=", 8)) {
295 show_datestring("--max-age=", arg
+8);
298 if (!strncmp(arg
, "--before=", 9)) {
299 show_datestring("--min-age=", arg
+9);
302 if (!strncmp(arg
, "--until=", 8)) {
303 show_datestring("--min-age=", arg
+8);
306 if (show_flag(arg
) && verify
)
307 die("Needed a single revision");
311 /* Not a flag argument */
312 dotdot
= strstr(arg
, "..");
314 unsigned char end
[20];
317 if (!get_sha1(arg
, sha1
)) {
320 if (!get_sha1(n
, end
)) {
321 show_rev(NORMAL
, end
, n
);
322 show_rev(REVERSED
, sha1
, arg
);
328 if (!get_sha1(arg
, sha1
)) {
329 show_rev(NORMAL
, sha1
, arg
);
332 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
333 show_rev(REVERSED
, sha1
, arg
+1);
340 die("Needed a single revision");
341 if (lstat(arg
, &st
) < 0)
342 die("'%s': %s", arg
, strerror(errno
));
345 if (verify
&& revs_count
!= 1)
346 die("Needed a single revision");