4 * Copyright (C) Linus Torvalds, 2005
9 static int get_extended_sha1(char *name
, unsigned char *sha1
);
12 * Some arguments are relevant "revision" arguments,
13 * others are about output format or other details.
14 * This sorts it all out.
16 static int is_rev_argument(const char *arg
)
18 static const char *rev_args
[] = {
25 const char **p
= rev_args
;
28 const char *str
= *p
++;
33 if (!strncmp(arg
, str
, len
))
38 static int get_parent(char *name
, unsigned char *result
, int idx
)
40 unsigned char sha1
[20];
41 int ret
= get_extended_sha1(name
, sha1
);
42 struct commit
*commit
;
43 struct commit_list
*p
;
47 commit
= lookup_commit_reference(sha1
);
50 if (parse_commit(commit
))
55 memcpy(result
, p
->item
->object
.sha1
, 20);
64 * This is like "get_sha1()", except it allows "sha1 expressions",
65 * notably "xyz^" for "parent of xyz"
67 static int get_extended_sha1(char *name
, unsigned char *sha1
)
70 int len
= strlen(name
);
73 if (len
> 2 && name
[len
-1] >= '1' && name
[len
-1] <= '9') {
74 parent
= name
[len
-1] - '0';
77 if (len
> 1 && name
[len
-1] == '^') {
80 ret
= get_parent(name
, sha1
, parent
);
85 return get_sha1(name
, sha1
);
88 int main(int argc
, char **argv
)
90 int i
, as_is
= 0, revs_only
= 0, no_revs
= 0;
92 unsigned char sha1
[20];
94 for (i
= 1; i
< argc
; i
++) {
103 if (!strcmp(arg
, "--")) {
112 if (!strcmp(arg
, "--default")) {
119 if (!strcmp(arg
, "--revs-only")) {
123 if (!strcmp(arg
, "--no-revs")) {
127 if (revs_only
| no_revs
) {
128 if (is_rev_argument(arg
) != revs_only
)
134 dotdot
= strstr(arg
, "..");
136 unsigned char end
[20];
139 if (!get_extended_sha1(arg
, sha1
)) {
142 if (!get_extended_sha1(n
, end
)) {
146 printf("%s\n", sha1_to_hex(end
));
147 printf("^%s\n", sha1_to_hex(sha1
));
153 if (!get_extended_sha1(arg
, sha1
)) {
157 printf("%s\n", sha1_to_hex(sha1
));
160 if (*arg
== '^' && !get_extended_sha1(arg
+1, sha1
)) {
164 printf("^%s\n", sha1_to_hex(sha1
));