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
;
56 /* accept -<digit>, like traditional "head" */
57 if ((*arg
== '-') && isdigit(arg
[1]))
61 const char *str
= *p
++;
66 if (!strcmp(arg
, str
) ||
67 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
72 /* Output argument as a string, either SQ or normal */
73 static void show(const char *arg
)
79 while ((ch
= *arg
++)) {
81 fputs("'\\'", stdout
);
91 /* Output a revision, only if filter allows it */
92 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
94 if (!(filter
& DO_REVS
))
99 if (type
!= show_type
)
101 if (symbolic
&& name
)
104 show(find_unique_abbrev(sha1
, abbrev
));
106 show(sha1_to_hex(sha1
));
109 /* Output a flag, only if filter allows it. */
110 static int show_flag(char *arg
)
112 if (!(filter
& DO_FLAGS
))
114 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
121 static void show_default(void)
126 unsigned char sha1
[20];
129 if (!get_sha1(s
, sha1
)) {
130 show_rev(NORMAL
, sha1
, s
);
136 static int show_reference(const char *refname
, const unsigned char *sha1
)
138 show_rev(NORMAL
, sha1
, refname
);
142 static void show_datestring(const char *flag
, const char *datestr
)
144 static char buffer
[100];
146 /* date handling requires both flags and revs */
147 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
149 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
153 static void show_file(const char *arg
)
156 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
))
160 int main(int argc
, char **argv
)
162 int i
, as_is
= 0, verify
= 0;
163 unsigned char sha1
[20];
164 const char *prefix
= setup_git_directory();
166 for (i
= 1; i
< argc
; i
++) {
175 if (!strcmp(arg
,"-n")) {
177 die("-n requires an argument");
178 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
184 if (!strncmp(arg
,"-n",2)) {
185 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
191 if (!strcmp(arg
, "--")) {
193 /* Pass on the "--" if we show anything but files.. */
194 if (filter
& (DO_FLAGS
| DO_REVS
))
198 if (!strcmp(arg
, "--default")) {
203 if (!strcmp(arg
, "--revs-only")) {
207 if (!strcmp(arg
, "--no-revs")) {
211 if (!strcmp(arg
, "--flags")) {
212 filter
&= ~DO_NONFLAGS
;
215 if (!strcmp(arg
, "--no-flags")) {
219 if (!strcmp(arg
, "--verify")) {
220 filter
&= ~(DO_FLAGS
|DO_NOREV
);
224 if (!strcmp(arg
, "--short") ||
225 !strncmp(arg
, "--short=", 9)) {
226 filter
&= ~(DO_FLAGS
|DO_NOREV
);
228 abbrev
= DEFAULT_ABBREV
;
230 abbrev
= strtoul(arg
+ 9, NULL
, 10);
231 if (abbrev
< MINIMUM_ABBREV
)
232 abbrev
= MINIMUM_ABBREV
;
233 else if (40 <= abbrev
)
237 if (!strcmp(arg
, "--sq")) {
241 if (!strcmp(arg
, "--not")) {
242 show_type
^= REVERSED
;
245 if (!strcmp(arg
, "--symbolic")) {
249 if (!strcmp(arg
, "--all")) {
250 for_each_ref(show_reference
);
253 if (!strcmp(arg
, "--show-prefix")) {
258 if (!strcmp(arg
, "--show-cdup")) {
259 const char *pfx
= prefix
;
261 pfx
= strchr(pfx
, '/');
270 if (!strcmp(arg
, "--git-dir")) {
271 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
272 static char cwd
[PATH_MAX
];
281 if (!getcwd(cwd
, PATH_MAX
))
282 die("unable to get current working directory");
283 printf("%s/.git\n", cwd
);
286 if (!strncmp(arg
, "--since=", 8)) {
287 show_datestring("--max-age=", arg
+8);
290 if (!strncmp(arg
, "--after=", 8)) {
291 show_datestring("--max-age=", arg
+8);
294 if (!strncmp(arg
, "--before=", 9)) {
295 show_datestring("--min-age=", arg
+9);
298 if (!strncmp(arg
, "--until=", 8)) {
299 show_datestring("--min-age=", arg
+8);
302 if (show_flag(arg
) && verify
)
303 die("Needed a single revision");
307 /* Not a flag argument */
308 dotdot
= strstr(arg
, "..");
310 unsigned char end
[20];
313 if (!get_sha1(arg
, sha1
)) {
316 if (!get_sha1(n
, end
)) {
317 show_rev(NORMAL
, end
, n
);
318 show_rev(REVERSED
, sha1
, arg
);
324 if (!get_sha1(arg
, sha1
)) {
325 show_rev(NORMAL
, sha1
, arg
);
328 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
329 show_rev(REVERSED
, sha1
, arg
+1);
333 die("Needed a single revision");
334 if ((filter
& DO_REVS
) &&
335 (filter
& DO_NONFLAGS
) && /* !def && */
337 die("'%s': %s", arg
, strerror(errno
));
342 if (verify
&& revs_count
!= 1)
343 die("Needed a single revision");