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
[] = {
57 const char **p
= rev_args
;
59 /* accept -<digit>, like traditional "head" */
60 if ((*arg
== '-') && isdigit(arg
[1]))
64 const char *str
= *p
++;
69 if (!strcmp(arg
, str
) ||
70 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
75 /* Output argument as a string, either SQ or normal */
76 static void show(const char *arg
)
82 while ((ch
= *arg
++)) {
84 fputs("'\\'", stdout
);
94 /* Output a revision, only if filter allows it */
95 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
97 if (!(filter
& DO_REVS
))
102 if (type
!= show_type
)
104 if (symbolic
&& name
)
107 show(find_unique_abbrev(sha1
, abbrev
));
109 show(sha1_to_hex(sha1
));
112 /* Output a flag, only if filter allows it. */
113 static int show_flag(char *arg
)
115 if (!(filter
& DO_FLAGS
))
117 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
124 static void show_default(void)
129 unsigned char sha1
[20];
132 if (!get_sha1(s
, sha1
)) {
133 show_rev(NORMAL
, sha1
, s
);
139 static int show_reference(const char *refname
, const unsigned char *sha1
)
141 show_rev(NORMAL
, sha1
, refname
);
145 static void show_datestring(const char *flag
, const char *datestr
)
147 static char buffer
[100];
149 /* date handling requires both flags and revs */
150 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
152 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
156 static int show_file(const char *arg
)
159 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
166 int main(int argc
, char **argv
)
168 int i
, as_is
= 0, verify
= 0;
169 unsigned char sha1
[20];
170 const char *prefix
= setup_git_directory();
172 git_config(git_default_config
);
174 for (i
= 1; i
< argc
; i
++) {
179 if (show_file(arg
) && as_is
< 2)
180 verify_filename(prefix
, arg
);
183 if (!strcmp(arg
,"-n")) {
185 die("-n requires an argument");
186 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
192 if (!strncmp(arg
,"-n",2)) {
193 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
199 if (!strcmp(arg
, "--")) {
201 /* Pass on the "--" if we show anything but files.. */
202 if (filter
& (DO_FLAGS
| DO_REVS
))
206 if (!strcmp(arg
, "--default")) {
211 if (!strcmp(arg
, "--revs-only")) {
215 if (!strcmp(arg
, "--no-revs")) {
219 if (!strcmp(arg
, "--flags")) {
220 filter
&= ~DO_NONFLAGS
;
223 if (!strcmp(arg
, "--no-flags")) {
227 if (!strcmp(arg
, "--verify")) {
228 filter
&= ~(DO_FLAGS
|DO_NOREV
);
232 if (!strcmp(arg
, "--short") ||
233 !strncmp(arg
, "--short=", 8)) {
234 filter
&= ~(DO_FLAGS
|DO_NOREV
);
236 abbrev
= DEFAULT_ABBREV
;
238 abbrev
= strtoul(arg
+ 8, NULL
, 10);
239 if (abbrev
< MINIMUM_ABBREV
)
240 abbrev
= MINIMUM_ABBREV
;
241 else if (40 <= abbrev
)
245 if (!strcmp(arg
, "--sq")) {
249 if (!strcmp(arg
, "--not")) {
250 show_type
^= REVERSED
;
253 if (!strcmp(arg
, "--symbolic")) {
257 if (!strcmp(arg
, "--all")) {
258 for_each_ref(show_reference
);
261 if (!strcmp(arg
, "--branches")) {
262 for_each_branch_ref(show_reference
);
265 if (!strcmp(arg
, "--tags")) {
266 for_each_tag_ref(show_reference
);
269 if (!strcmp(arg
, "--remotes")) {
270 for_each_remote_ref(show_reference
);
273 if (!strcmp(arg
, "--show-prefix")) {
278 if (!strcmp(arg
, "--show-cdup")) {
279 const char *pfx
= prefix
;
281 pfx
= strchr(pfx
, '/');
290 if (!strcmp(arg
, "--git-dir")) {
291 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
292 static char cwd
[PATH_MAX
];
301 if (!getcwd(cwd
, PATH_MAX
))
302 die("unable to get current working directory");
303 printf("%s/.git\n", cwd
);
306 if (!strncmp(arg
, "--since=", 8)) {
307 show_datestring("--max-age=", arg
+8);
310 if (!strncmp(arg
, "--after=", 8)) {
311 show_datestring("--max-age=", arg
+8);
314 if (!strncmp(arg
, "--before=", 9)) {
315 show_datestring("--min-age=", arg
+9);
318 if (!strncmp(arg
, "--until=", 8)) {
319 show_datestring("--min-age=", arg
+8);
322 if (show_flag(arg
) && verify
)
323 die("Needed a single revision");
327 /* Not a flag argument */
328 dotdot
= strstr(arg
, "..");
330 unsigned char end
[20];
331 char *next
= dotdot
+ 2;
338 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
339 show_rev(NORMAL
, end
, next
);
340 show_rev(REVERSED
, sha1
, this);
345 if (!get_sha1(arg
, sha1
)) {
346 show_rev(NORMAL
, sha1
, arg
);
349 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
350 show_rev(REVERSED
, sha1
, arg
+1);
357 die("Needed a single revision");
358 verify_filename(prefix
, arg
);
361 if (verify
&& revs_count
!= 1)
362 die("Needed a single revision");