4 * Copyright (C) Linus Torvalds, 2005
16 static int filter
= ~0;
18 static const char *def
;
22 static int show_type
= NORMAL
;
27 static int revs_count
;
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
, int flag
, void *cb_data
)
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 static int try_difference(const char *arg
)
170 unsigned char sha1
[20];
171 unsigned char end
[20];
176 if (!(dotdot
= strstr(arg
, "..")))
180 symmetric
= (*next
== '.');
189 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
190 show_rev(NORMAL
, end
, next
);
191 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
193 struct commit_list
*exclude
;
194 struct commit
*a
, *b
;
195 a
= lookup_commit_reference(sha1
);
196 b
= lookup_commit_reference(end
);
197 exclude
= get_merge_bases(a
, b
, 1);
199 struct commit_list
*n
= exclude
->next
;
201 exclude
->item
->object
.sha1
,NULL
);
212 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
214 int i
, as_is
= 0, verify
= 0;
215 unsigned char sha1
[20];
217 git_config(git_default_config
);
219 for (i
= 1; i
< argc
; i
++) {
220 const char *arg
= argv
[i
];
223 if (show_file(arg
) && as_is
< 2)
224 verify_filename(prefix
, arg
);
227 if (!strcmp(arg
,"-n")) {
229 die("-n requires an argument");
230 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
236 if (!strncmp(arg
,"-n",2)) {
237 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
243 if (!strcmp(arg
, "--")) {
245 /* Pass on the "--" if we show anything but files.. */
246 if (filter
& (DO_FLAGS
| DO_REVS
))
250 if (!strcmp(arg
, "--default")) {
255 if (!strcmp(arg
, "--revs-only")) {
259 if (!strcmp(arg
, "--no-revs")) {
263 if (!strcmp(arg
, "--flags")) {
264 filter
&= ~DO_NONFLAGS
;
267 if (!strcmp(arg
, "--no-flags")) {
271 if (!strcmp(arg
, "--verify")) {
272 filter
&= ~(DO_FLAGS
|DO_NOREV
);
276 if (!strcmp(arg
, "--short") ||
277 !prefixcmp(arg
, "--short=")) {
278 filter
&= ~(DO_FLAGS
|DO_NOREV
);
280 abbrev
= DEFAULT_ABBREV
;
282 abbrev
= strtoul(arg
+ 8, NULL
, 10);
283 if (abbrev
< MINIMUM_ABBREV
)
284 abbrev
= MINIMUM_ABBREV
;
285 else if (40 <= abbrev
)
289 if (!strcmp(arg
, "--sq")) {
293 if (!strcmp(arg
, "--not")) {
294 show_type
^= REVERSED
;
297 if (!strcmp(arg
, "--symbolic")) {
301 if (!strcmp(arg
, "--all")) {
302 for_each_ref(show_reference
, NULL
);
305 if (!strcmp(arg
, "--branches")) {
306 for_each_branch_ref(show_reference
, NULL
);
309 if (!strcmp(arg
, "--tags")) {
310 for_each_tag_ref(show_reference
, NULL
);
313 if (!strcmp(arg
, "--remotes")) {
314 for_each_remote_ref(show_reference
, NULL
);
317 if (!strcmp(arg
, "--show-prefix")) {
322 if (!strcmp(arg
, "--show-cdup")) {
323 const char *pfx
= prefix
;
325 pfx
= strchr(pfx
, '/');
334 if (!strcmp(arg
, "--git-dir")) {
335 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
336 static char cwd
[PATH_MAX
];
345 if (!getcwd(cwd
, PATH_MAX
))
346 die("unable to get current working directory");
347 printf("%s/.git\n", cwd
);
350 if (!strcmp(arg
, "--is-inside-git-dir")) {
351 printf("%s\n", is_inside_git_dir() ? "true"
355 if (!prefixcmp(arg
, "--since=")) {
356 show_datestring("--max-age=", arg
+8);
359 if (!prefixcmp(arg
, "--after=")) {
360 show_datestring("--max-age=", arg
+8);
363 if (!prefixcmp(arg
, "--before=")) {
364 show_datestring("--min-age=", arg
+9);
367 if (!prefixcmp(arg
, "--until=")) {
368 show_datestring("--min-age=", arg
+8);
371 if (show_flag(arg
) && verify
)
372 die("Needed a single revision");
376 /* Not a flag argument */
377 if (try_difference(arg
))
379 if (!get_sha1(arg
, sha1
)) {
380 show_rev(NORMAL
, sha1
, arg
);
383 if (*arg
== '^' && !get_sha1(arg
+1, sha1
)) {
384 show_rev(REVERSED
, sha1
, arg
+1);
391 die("Needed a single revision");
392 verify_filename(prefix
, arg
);
395 if (verify
&& revs_count
!= 1)
396 die("Needed a single revision");