4 * Copyright (C) Linus Torvalds, 2005
16 static int filter
= ~0;
18 static const char *def
= NULL
;
22 static int show_type
= NORMAL
;
23 static int symbolic
= 0;
24 static int abbrev
= 0;
25 static int output_sq
= 0;
27 static int revs_count
= 0;
30 * Some arguments are relevant "revision" arguments,
31 * others are about output format or other details.
32 * This sorts it all out.
34 static int is_rev_argument(const char *arg
)
36 static const char *rev_args
[] = {
58 const char **p
= rev_args
;
60 /* accept -<digit>, like traditional "head" */
61 if ((*arg
== '-') && isdigit(arg
[1]))
65 const char *str
= *p
++;
70 if (!strcmp(arg
, str
) ||
71 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
76 /* Output argument as a string, either SQ or normal */
77 static void show(const char *arg
)
83 while ((ch
= *arg
++)) {
85 fputs("'\\'", stdout
);
95 /* Output a revision, only if filter allows it */
96 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
98 if (!(filter
& DO_REVS
))
103 if (type
!= show_type
)
105 if (symbolic
&& name
)
108 show(find_unique_abbrev(sha1
, abbrev
));
110 show(sha1_to_hex(sha1
));
113 /* Output a flag, only if filter allows it. */
114 static int show_flag(const char *arg
)
116 if (!(filter
& DO_FLAGS
))
118 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
125 static void show_default(void)
130 unsigned char sha1
[20];
133 if (!get_sha1(s
, sha1
)) {
134 show_rev(NORMAL
, sha1
, s
);
140 static int show_reference(const char *refname
, const unsigned char *sha1
)
142 show_rev(NORMAL
, sha1
, refname
);
146 static void show_datestring(const char *flag
, const char *datestr
)
148 static char buffer
[100];
150 /* date handling requires both flags and revs */
151 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
153 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
157 static int show_file(const char *arg
)
160 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
167 int cmd_rev_parse(int argc
, const char **argv
, char **envp
)
169 int i
, as_is
= 0, verify
= 0;
170 unsigned char sha1
[20];
171 const char *prefix
= setup_git_directory();
173 git_config(git_default_config
);
175 for (i
= 1; i
< argc
; i
++) {
176 const char *arg
= argv
[i
];
180 if (show_file(arg
) && as_is
< 2)
181 verify_filename(prefix
, arg
);
184 if (!strcmp(arg
,"-n")) {
186 die("-n requires an argument");
187 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
193 if (!strncmp(arg
,"-n",2)) {
194 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
200 if (!strcmp(arg
, "--")) {
202 /* Pass on the "--" if we show anything but files.. */
203 if (filter
& (DO_FLAGS
| DO_REVS
))
207 if (!strcmp(arg
, "--default")) {
212 if (!strcmp(arg
, "--revs-only")) {
216 if (!strcmp(arg
, "--no-revs")) {
220 if (!strcmp(arg
, "--flags")) {
221 filter
&= ~DO_NONFLAGS
;
224 if (!strcmp(arg
, "--no-flags")) {
228 if (!strcmp(arg
, "--verify")) {
229 filter
&= ~(DO_FLAGS
|DO_NOREV
);
233 if (!strcmp(arg
, "--short") ||
234 !strncmp(arg
, "--short=", 8)) {
235 filter
&= ~(DO_FLAGS
|DO_NOREV
);
237 abbrev
= DEFAULT_ABBREV
;
239 abbrev
= strtoul(arg
+ 8, NULL
, 10);
240 if (abbrev
< MINIMUM_ABBREV
)
241 abbrev
= MINIMUM_ABBREV
;
242 else if (40 <= abbrev
)
246 if (!strcmp(arg
, "--sq")) {
250 if (!strcmp(arg
, "--not")) {
251 show_type
^= REVERSED
;
254 if (!strcmp(arg
, "--symbolic")) {
258 if (!strcmp(arg
, "--all")) {
259 for_each_ref(show_reference
);
262 if (!strcmp(arg
, "--branches")) {
263 for_each_branch_ref(show_reference
);
266 if (!strcmp(arg
, "--tags")) {
267 for_each_tag_ref(show_reference
);
270 if (!strcmp(arg
, "--remotes")) {
271 for_each_remote_ref(show_reference
);
274 if (!strcmp(arg
, "--show-prefix")) {
279 if (!strcmp(arg
, "--show-cdup")) {
280 const char *pfx
= prefix
;
282 pfx
= strchr(pfx
, '/');
291 if (!strcmp(arg
, "--git-dir")) {
292 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
293 static char cwd
[PATH_MAX
];
302 if (!getcwd(cwd
, PATH_MAX
))
303 die("unable to get current working directory");
304 printf("%s/.git\n", cwd
);
307 if (!strncmp(arg
, "--since=", 8)) {
308 show_datestring("--max-age=", arg
+8);
311 if (!strncmp(arg
, "--after=", 8)) {
312 show_datestring("--max-age=", arg
+8);
315 if (!strncmp(arg
, "--before=", 9)) {
316 show_datestring("--min-age=", arg
+9);
319 if (!strncmp(arg
, "--until=", 8)) {
320 show_datestring("--min-age=", arg
+8);
323 if (show_flag(arg
) && verify
)
324 die("Needed a single revision");
328 /* Not a flag argument */
329 dotdot
= strstr(arg
, "..");
331 unsigned char end
[20];
332 const char *next
= dotdot
+ 2;
333 const char *this = arg
;
339 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
340 show_rev(NORMAL
, end
, next
);
341 show_rev(REVERSED
, sha1
, this);
346 if (!get_sha1(arg
, sha1
)) {
347 show_rev(NORMAL
, sha1
, arg
);
350 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
351 show_rev(REVERSED
, sha1
, arg
+1);
358 die("Needed a single revision");
359 verify_filename(prefix
, arg
);
362 if (verify
&& revs_count
!= 1)
363 die("Needed a single revision");