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))
103 memcpy(result, p->item->object.sha1, 20);
111 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
113 static char dirname[PATH_MAX];
118 snprintf(dirname, sizeof(dirname), "%s/%.2s", get_object_directory(), name);
119 dir = opendir(dirname);
120 sprintf(hex, "%.2s", name);
124 while ((de = readdir(dir)) != NULL) {
125 if (strlen(de->d_name) != 38)
127 if (memcmp(de->d_name, name + 2, len-2))
129 memcpy(hex + 2, de->d_name, 38);
136 return get_sha1_hex(hex, sha1) == 0;
140 static int match_sha(unsigned len, const unsigned char *a, const unsigned char *b)
150 if ((*a ^ *b) & 0xf0)
155 static int find_short_packed_object(int len, const unsigned char *match, unsigned char *sha1)
157 struct packed_git *p;
159 prepare_packed_git();
160 for (p = packed_git; p; p = p->next) {
161 unsigned num = num_packed_objects(p);
162 unsigned first = 0, last = num;
163 while (first < last) {
164 unsigned mid = (first + last) / 2;
165 unsigned char now[20];
168 nth_packed_object_sha1(p, mid, now);
169 cmp = memcmp(match, now, 20);
181 unsigned char now[20], next[20];
182 nth_packed_object_sha1(p, first, now);
183 if (match_sha(len, match, now)) {
184 if (nth_packed_object_sha1(p, first+1, next) || !match_sha(len, match, next)) {
185 memcpy(sha1, now, 20);
194 static int get_short_sha1(char *name, unsigned char *sha1)
198 unsigned char res[20];
201 memset(canonical, 'x', 40);
203 unsigned char c = name[i];
207 if (c >= '0' && c <= '9')
209 else if (c >= 'a' && c <= 'f')
211 else if (c >= 'A' && c <='F') {
224 if (find_short_object_filename(i, canonical, sha1))
226 if (find_short_packed_object(i, res, sha1))
232 * This is like "get_sha1()", except it allows "sha1 expressions",
233 * notably "xyz^" for "parent of xyz"
235 static int get_extended_sha1(char *name, unsigned char *sha1)
238 int len = strlen(name);
241 if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
242 parent = name[len-1] - '0';
245 if (len > 1 && name[len-1] == '^') {
247 ret = get_parent(name, sha1, parent);
252 ret = get_sha1(name, sha1);
255 return get_short_sha1(name, sha1);
258 static void show_default(void)
263 unsigned char sha1[20];
266 if (!get_extended_sha1(s, sha1)) {
267 show_rev(NORMAL, sha1);
274 static int show_reference(const char *refname, const unsigned char *sha1)
276 show_rev(NORMAL, sha1);
280 int main(int argc, char **argv)
283 unsigned char sha1[20];
285 for (i = 1; i < argc; i++) {
294 if (!strcmp(arg, "--")) {
300 if (!strcmp(arg, "--default")) {
305 if (!strcmp(arg, "--revs-only")) {
309 if (!strcmp(arg, "--no-revs")) {
313 if (!strcmp(arg, "--flags")) {
317 if (!strcmp(arg, "--no-flags")) {
321 if (!strcmp(arg, "--verify")) {
327 if (!strcmp(arg, "--not")) {
328 show_type ^= REVERSED;
331 if (!strcmp(arg, "--all")) {
332 for_each_ref(show_reference);
338 dotdot = strstr(arg, "..");
340 unsigned char end[20];
343 if (!get_extended_sha1(arg, sha1)) {
346 if (!get_extended_sha1(n, end)) {
350 show_rev(NORMAL, end);
351 show_rev(REVERSED, sha1);
357 if (!get_extended_sha1(arg, sha1)) {
361 show_rev(NORMAL, sha1);
364 if (*arg == '^' && !get_extended_sha1(arg+1, sha1)) {
368 show_rev(REVERSED, sha1);
375 if (single_rev && output_revs != 1) {
376 fprintf(stderr, "Needed a single revision\n");