git-cvsimport-script: update cvsps cache instead of rebuilding it
[git/spearce.git] / rev-parse.c
blobc134ffda1913082e3b38c6bee7acd3747d633ffd
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
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 #define NORMAL 0
17 #define REVERSED 1
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[] = {
30 "--max-count=",
31 "--max-age=",
32 "--min-age=",
33 "--merge-order",
34 NULL
36 const char **p = rev_args;
38 for (;;) {
39 const char *str = *p++;
40 int len;
41 if (!str)
42 return 0;
43 len = strlen(str);
44 if (!strncmp(arg, str, len))
45 return 1;
49 static void show_rev(int type, unsigned char *sha1)
51 if (no_revs)
52 return;
53 output_revs++;
54 printf("%s%s\n", type == show_type ? "" : "^", sha1_to_hex(sha1));
57 static void show_rev_arg(char *rev)
59 if (no_revs)
60 return;
61 puts(rev);
64 static void show_norev(char *norev)
66 if (revs_only)
67 return;
68 puts(norev);
71 static void show_arg(char *arg)
73 if (do_rev_argument && is_rev_argument(arg))
74 show_rev_arg(arg);
75 else
76 show_norev(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;
86 if (ret)
87 return ret;
88 commit = lookup_commit_reference(sha1);
89 if (!commit)
90 return -1;
91 if (parse_commit(commit))
92 return -1;
93 p = commit->parents;
94 while (p) {
95 if (!--idx) {
96 memcpy(result, p->item->object.sha1, 20);
97 return 0;
99 p = p->next;
101 return -1;
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)
110 int parent;
111 int len = strlen(name);
113 parent = 1;
114 if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
115 parent = name[len-1] - '0';
116 len--;
118 if (len > 1 && name[len-1] == '^') {
119 int ret;
120 name[len-1] = 0;
121 ret = get_parent(name, sha1, parent);
122 name[len-1] = '^';
123 if (!ret)
124 return 0;
126 return get_sha1(name, sha1);
129 static void show_default(void)
131 char *s = def;
133 if (s) {
134 unsigned char sha1[20];
136 def = NULL;
137 if (!get_extended_sha1(s, sha1)) {
138 show_rev(NORMAL, sha1);
139 return;
141 show_arg(s);
145 int main(int argc, char **argv)
147 int i, as_is = 0;
148 unsigned char sha1[20];
150 for (i = 1; i < argc; i++) {
151 char *arg = argv[i];
152 char *dotdot;
154 if (as_is) {
155 show_norev(arg);
156 continue;
158 if (*arg == '-') {
159 if (!strcmp(arg, "--")) {
160 show_default();
161 if (revs_only)
162 break;
163 as_is = 1;
165 if (!strcmp(arg, "--default")) {
166 def = argv[i+1];
167 i++;
168 continue;
170 if (!strcmp(arg, "--revs-only")) {
171 revs_only = 1;
172 continue;
174 if (!strcmp(arg, "--no-revs")) {
175 no_revs = 1;
176 continue;
178 if (!strcmp(arg, "--verify")) {
179 revs_only = 1;
180 do_rev_argument = 0;
181 single_rev = 1;
182 continue;
184 if (!strcmp(arg, "--not")) {
185 show_type ^= REVERSED;
186 continue;
188 show_arg(arg);
189 continue;
191 dotdot = strstr(arg, "..");
192 if (dotdot) {
193 unsigned char end[20];
194 char *n = dotdot+2;
195 *dotdot = 0;
196 if (!get_extended_sha1(arg, sha1)) {
197 if (!*n)
198 n = "HEAD";
199 if (!get_extended_sha1(n, end)) {
200 if (no_revs)
201 continue;
202 def = NULL;
203 show_rev(NORMAL, end);
204 show_rev(REVERSED, sha1);
205 continue;
208 *dotdot = '.';
210 if (!get_extended_sha1(arg, sha1)) {
211 if (no_revs)
212 continue;
213 def = NULL;
214 show_rev(NORMAL, sha1);
215 continue;
217 if (*arg == '^' && !get_extended_sha1(arg+1, sha1)) {
218 if (no_revs)
219 continue;
220 def = NULL;
221 show_rev(REVERSED, sha1);
222 continue;
224 show_default();
225 show_norev(arg);
227 show_default();
228 if (single_rev && output_revs != 1) {
229 fprintf(stderr, "Needed a single revision\n");
230 exit(1);
232 return 0;