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;
18 static int show_type = NORMAL;
20 static int get_extended_sha1(char *name, unsigned char *sha1);
23 * Some arguments are relevant "revision" arguments,
24 * others are about output format or other details.
25 * This sorts it all out.
27 static int is_rev_argument(const char *arg)
29 static const char *rev_args[] = {
36 const char **p = rev_args;
39 const char *str = *p++;
44 if (!strncmp(arg, str, len))
49 static void show_rev(int type, unsigned char *sha1)
54 printf("%s%s\n", type == show_type ? "" : "^", sha1_to_hex(sha1));
57 static void show_rev_arg(char *rev)
64 static void show_norev(char *norev)
71 static void show_arg(char *arg)
73 if (do_rev_argument && is_rev_argument(arg))
79 static int get_parent(char *name, unsigned char *result, int idx)
81 unsigned char sha1[20];
82 int ret = get_extended_sha1(name, sha1);
83 struct commit *commit;
84 struct commit_list *p;
88 commit = lookup_commit_reference(sha1);
91 if (parse_commit(commit))
96 memcpy(result, p->item->object.sha1, 20);
105 * This is like "get_sha1()", except it allows "sha1 expressions",
106 * notably "xyz^" for "parent of xyz"
108 static int get_extended_sha1(char *name, unsigned char *sha1)
111 int len = strlen(name);
114 if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
115 parent = name[len-1] - '0';
118 if (len > 1 && name[len-1] == '^') {
121 ret = get_parent(name, sha1, parent);
126 return get_sha1(name, sha1);
129 static void show_default(void)
134 unsigned char sha1[20];
137 if (!get_extended_sha1(s, sha1)) {
138 show_rev(NORMAL, sha1);
145 int main(int argc, char **argv)
148 unsigned char sha1[20];
150 for (i = 1; i < argc; i++) {
159 if (!strcmp(arg, "--")) {
165 if (!strcmp(arg, "--default")) {
170 if (!strcmp(arg, "--revs-only")) {
174 if (!strcmp(arg, "--no-revs")) {
178 if (!strcmp(arg, "--verify")) {
184 if (!strcmp(arg, "--not")) {
185 show_type ^= REVERSED;
191 dotdot = strstr(arg, "..");
193 unsigned char end[20];
196 if (!get_extended_sha1(arg, sha1)) {
199 if (!get_extended_sha1(n, end)) {
203 show_rev(NORMAL, end);
204 show_rev(REVERSED, sha1);
210 if (!get_extended_sha1(arg, sha1)) {
214 show_rev(NORMAL, sha1);
217 if (*arg == '^' && !get_extended_sha1(arg+1, sha1)) {
221 show_rev(REVERSED, sha1);
228 if (single_rev && output_revs != 1) {
229 fprintf(stderr, "Needed a single revision\n");