4 * Copyright (C) Linus Torvalds, 2005
10 static char *def
= NULL
;
11 static int no_revs
= 0;
12 static int single_rev
= 0;
13 static int revs_only
= 0;
14 static int do_rev_argument
= 1;
15 static int output_revs
= 0;
16 static int flags_only
= 0;
17 static int no_flags
= 0;
21 static int show_type
= NORMAL
;
23 static int get_extended_sha1(char *name
, unsigned char *sha1
);
26 * Some arguments are relevant "revision" arguments,
27 * others are about output format or other details.
28 * This sorts it all out.
30 static int is_rev_argument(const char *arg
)
32 static const char *rev_args
[] = {
39 const char **p
= rev_args
;
42 const char *str
= *p
++;
47 if (!strncmp(arg
, str
, len
))
52 static void show_rev(int type
, const unsigned char *sha1
)
57 printf("%s%s\n", type
== show_type
? "" : "^", sha1_to_hex(sha1
));
60 static void show_rev_arg(char *rev
)
67 static void show_norev(char *norev
)
76 static void show_arg(char *arg
)
80 if (do_rev_argument
&& is_rev_argument(arg
))
86 static int get_parent(char *name
, unsigned char *result
, int idx
)
88 unsigned char sha1
[20];
89 int ret
= get_extended_sha1(name
, sha1
);
90 struct commit
*commit
;
91 struct commit_list
*p
;
95 commit
= lookup_commit_reference(sha1
);
98 if (parse_commit(commit
))
101 memcpy(result
, commit
->object
.sha1
, 20);
107 memcpy(result
, p
->item
->object
.sha1
, 20);
115 static int find_short_object_filename(int len
, const char *name
, unsigned char *sha1
)
117 static char dirname
[PATH_MAX
];
122 snprintf(dirname
, sizeof(dirname
), "%s/%.2s", get_object_directory(), name
);
123 dir
= opendir(dirname
);
124 sprintf(hex
, "%.2s", name
);
128 while ((de
= readdir(dir
)) != NULL
) {
129 if (strlen(de
->d_name
) != 38)
131 if (memcmp(de
->d_name
, name
+ 2, len
-2))
133 memcpy(hex
+ 2, de
->d_name
, 38);
140 return get_sha1_hex(hex
, sha1
) == 0;
144 static int match_sha(unsigned len
, const unsigned char *a
, const unsigned char *b
)
154 if ((*a
^ *b
) & 0xf0)
159 static int find_short_packed_object(int len
, const unsigned char *match
, unsigned char *sha1
)
161 struct packed_git
*p
;
163 prepare_packed_git();
164 for (p
= packed_git
; p
; p
= p
->next
) {
165 unsigned num
= num_packed_objects(p
);
166 unsigned first
= 0, last
= num
;
167 while (first
< last
) {
168 unsigned mid
= (first
+ last
) / 2;
169 unsigned char now
[20];
172 nth_packed_object_sha1(p
, mid
, now
);
173 cmp
= memcmp(match
, now
, 20);
185 unsigned char now
[20], next
[20];
186 nth_packed_object_sha1(p
, first
, now
);
187 if (match_sha(len
, match
, now
)) {
188 if (nth_packed_object_sha1(p
, first
+1, next
) || !match_sha(len
, match
, next
)) {
189 memcpy(sha1
, now
, 20);
198 static int get_short_sha1(char *name
, unsigned char *sha1
)
202 unsigned char res
[20];
205 memset(canonical
, 'x', 40);
207 unsigned char c
= name
[i
];
211 if (c
>= '0' && c
<= '9')
213 else if (c
>= 'a' && c
<= 'f')
215 else if (c
>= 'A' && c
<='F') {
228 if (find_short_object_filename(i
, canonical
, sha1
))
230 if (find_short_packed_object(i
, res
, sha1
))
236 * This is like "get_sha1()", except it allows "sha1 expressions",
237 * notably "xyz^" for "parent of xyz"
239 static int get_extended_sha1(char *name
, unsigned char *sha1
)
242 int len
= strlen(name
);
245 if (len
> 2 && name
[len
-1] >= '0' && name
[len
-1] <= '9') {
246 parent
= name
[len
-1] - '0';
249 if (len
> 1 && name
[len
-1] == '^') {
251 ret
= get_parent(name
, sha1
, parent
);
256 ret
= get_sha1(name
, sha1
);
259 return get_short_sha1(name
, sha1
);
262 static void show_default(void)
267 unsigned char sha1
[20];
270 if (!get_extended_sha1(s
, sha1
)) {
271 show_rev(NORMAL
, sha1
);
278 static int show_reference(const char *refname
, const unsigned char *sha1
)
280 show_rev(NORMAL
, sha1
);
284 int main(int argc
, char **argv
)
287 unsigned char sha1
[20];
289 for (i
= 1; i
< argc
; i
++) {
298 if (!strcmp(arg
, "--")) {
304 if (!strcmp(arg
, "--default")) {
309 if (!strcmp(arg
, "--revs-only")) {
313 if (!strcmp(arg
, "--no-revs")) {
317 if (!strcmp(arg
, "--flags")) {
321 if (!strcmp(arg
, "--no-flags")) {
325 if (!strcmp(arg
, "--verify")) {
331 if (!strcmp(arg
, "--not")) {
332 show_type
^= REVERSED
;
335 if (!strcmp(arg
, "--all")) {
336 for_each_ref(show_reference
);
342 dotdot
= strstr(arg
, "..");
344 unsigned char end
[20];
347 if (!get_extended_sha1(arg
, sha1
)) {
350 if (!get_extended_sha1(n
, end
)) {
354 show_rev(NORMAL
, end
);
355 show_rev(REVERSED
, sha1
);
361 if (!get_extended_sha1(arg
, sha1
)) {
365 show_rev(NORMAL
, sha1
);
368 if (*arg
== '^' && !get_extended_sha1(arg
+1, sha1
)) {
372 show_rev(REVERSED
, sha1
);
379 if (single_rev
&& output_revs
!= 1) {
380 fprintf(stderr
, "Needed a single revision\n");