4 * Copyright (C) Linus Torvalds, 2005
9 static char *def
= NULL
;
10 static int no_revs
= 0;
11 static int single_rev
= 0;
12 static int revs_only
= 0;
13 static int do_rev_argument
= 1;
14 static int output_revs
= 0;
16 static int get_extended_sha1(char *name
, unsigned char *sha1
);
19 * Some arguments are relevant "revision" arguments,
20 * others are about output format or other details.
21 * This sorts it all out.
23 static int is_rev_argument(const char *arg
)
25 static const char *rev_args
[] = {
32 const char **p
= rev_args
;
35 const char *str
= *p
++;
40 if (!strncmp(arg
, str
, len
))
45 static void show_rev(unsigned char *sha1
)
50 puts(sha1_to_hex(sha1
));
53 static void show_antirev(unsigned char *sha1
)
58 printf("^%s\n", sha1_to_hex(sha1
));
61 static void show_rev_arg(char *rev
)
68 static void show_norev(char *norev
)
75 static void show_arg(char *arg
)
77 if (do_rev_argument
&& is_rev_argument(arg
))
83 static int get_parent(char *name
, unsigned char *result
, int idx
)
85 unsigned char sha1
[20];
86 int ret
= get_extended_sha1(name
, sha1
);
87 struct commit
*commit
;
88 struct commit_list
*p
;
92 commit
= lookup_commit_reference(sha1
);
95 if (parse_commit(commit
))
100 memcpy(result
, p
->item
->object
.sha1
, 20);
109 * This is like "get_sha1()", except it allows "sha1 expressions",
110 * notably "xyz^" for "parent of xyz"
112 static int get_extended_sha1(char *name
, unsigned char *sha1
)
115 int len
= strlen(name
);
118 if (len
> 2 && name
[len
-1] >= '1' && name
[len
-1] <= '9') {
119 parent
= name
[len
-1] - '0';
122 if (len
> 1 && name
[len
-1] == '^') {
125 ret
= get_parent(name
, sha1
, parent
);
130 return get_sha1(name
, sha1
);
133 static void show_default(void)
138 unsigned char sha1
[20];
141 if (!get_extended_sha1(s
, sha1
)) {
149 int main(int argc
, char **argv
)
152 unsigned char sha1
[20];
154 for (i
= 1; i
< argc
; i
++) {
163 if (!strcmp(arg
, "--")) {
169 if (!strcmp(arg
, "--default")) {
174 if (!strcmp(arg
, "--revs-only")) {
178 if (!strcmp(arg
, "--no-revs")) {
182 if (!strcmp(arg
, "--verify")) {
191 dotdot
= strstr(arg
, "..");
193 unsigned char end
[20];
196 if (!get_extended_sha1(arg
, sha1
)) {
199 if (!get_extended_sha1(n
, end
)) {
210 if (!get_extended_sha1(arg
, sha1
)) {
217 if (*arg
== '^' && !get_extended_sha1(arg
+1, sha1
)) {
228 if (single_rev
&& output_revs
!= 1) {
229 fprintf(stderr
, "Needed a single revision\n");