Add "has_sha1_pack()" function to query whether the object is available in a pack
[git/git.git] / rev-parse.c
CommitLineData
178cb243
LT
1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
a8be83fe
LT
7#include "commit.h"
8
023d66ed
LT
9static char *def = NULL;
10static int no_revs = 0;
11static int single_rev = 0;
12static int revs_only = 0;
13static int do_rev_argument = 1;
14static int output_revs = 0;
15
042a4ed7
LT
16#define NORMAL 0
17#define REVERSED 1
18static int show_type = NORMAL;
19
a8be83fe 20static int get_extended_sha1(char *name, unsigned char *sha1);
178cb243 21
921d865e
LT
22/*
23 * Some arguments are relevant "revision" arguments,
24 * others are about output format or other details.
25 * This sorts it all out.
26 */
27static int is_rev_argument(const char *arg)
28{
29 static const char *rev_args[] = {
30 "--max-count=",
31 "--max-age=",
32 "--min-age=",
33 "--merge-order",
34 NULL
35 };
36 const char **p = rev_args;
37
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;
46 }
47}
48
042a4ed7 49static void show_rev(int type, unsigned char *sha1)
023d66ed
LT
50{
51 if (no_revs)
52 return;
53 output_revs++;
042a4ed7 54 printf("%s%s\n", type == show_type ? "" : "^", sha1_to_hex(sha1));
023d66ed
LT
55}
56
57static void show_rev_arg(char *rev)
58{
59 if (no_revs)
60 return;
61 puts(rev);
62}
63
64static void show_norev(char *norev)
65{
66 if (revs_only)
67 return;
68 puts(norev);
69}
70
71static void show_arg(char *arg)
72{
73 if (do_rev_argument && is_rev_argument(arg))
74 show_rev_arg(arg);
75 else
76 show_norev(arg);
77}
78
a8be83fe
LT
79static int get_parent(char *name, unsigned char *result, int idx)
80{
81 unsigned char sha1[20];
82 int ret = get_extended_sha1(name, sha1);
83 struct commit *commit;
84 struct commit_list *p;
85
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;
98 }
99 p = p->next;
100 }
101 return -1;
102}
103
104/*
105 * This is like "get_sha1()", except it allows "sha1 expressions",
218e441d 106 * notably "xyz^" for "parent of xyz"
a8be83fe
LT
107 */
108static int get_extended_sha1(char *name, unsigned char *sha1)
109{
110 int parent;
111 int len = strlen(name);
112
113 parent = 1;
218e441d 114 if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
a8be83fe
LT
115 parent = name[len-1] - '0';
116 len--;
117 }
218e441d 118 if (len > 1 && name[len-1] == '^') {
a8be83fe 119 int ret;
218e441d 120 name[len-1] = 0;
a8be83fe 121 ret = get_parent(name, sha1, parent);
218e441d 122 name[len-1] = '^';
a8be83fe
LT
123 if (!ret)
124 return 0;
125 }
126 return get_sha1(name, sha1);
127}
128
023d66ed
LT
129static void show_default(void)
130{
131 char *s = def;
132
133 if (s) {
134 unsigned char sha1[20];
135
136 def = NULL;
137 if (!get_extended_sha1(s, sha1)) {
042a4ed7 138 show_rev(NORMAL, sha1);
023d66ed
LT
139 return;
140 }
141 show_arg(s);
142 }
143}
144
178cb243
LT
145int main(int argc, char **argv)
146{
023d66ed 147 int i, as_is = 0;
178cb243
LT
148 unsigned char sha1[20];
149
150 for (i = 1; i < argc; i++) {
151 char *arg = argv[i];
152 char *dotdot;
153
154 if (as_is) {
023d66ed 155 show_norev(arg);
178cb243
LT
156 continue;
157 }
158 if (*arg == '-') {
159 if (!strcmp(arg, "--")) {
023d66ed 160 show_default();
8ebb0184
LT
161 if (revs_only)
162 break;
178cb243
LT
163 as_is = 1;
164 }
165 if (!strcmp(arg, "--default")) {
178cb243
LT
166 def = argv[i+1];
167 i++;
168 continue;
169 }
8ebb0184
LT
170 if (!strcmp(arg, "--revs-only")) {
171 revs_only = 1;
172 continue;
173 }
174 if (!strcmp(arg, "--no-revs")) {
175 no_revs = 1;
176 continue;
177 }
023d66ed
LT
178 if (!strcmp(arg, "--verify")) {
179 revs_only = 1;
180 do_rev_argument = 0;
181 single_rev = 1;
182 continue;
921d865e 183 }
042a4ed7
LT
184 if (!strcmp(arg, "--not")) {
185 show_type ^= REVERSED;
186 continue;
187 }
023d66ed 188 show_arg(arg);
178cb243
LT
189 continue;
190 }
178cb243
LT
191 dotdot = strstr(arg, "..");
192 if (dotdot) {
193 unsigned char end[20];
194 char *n = dotdot+2;
195 *dotdot = 0;
a8be83fe 196 if (!get_extended_sha1(arg, sha1)) {
178cb243
LT
197 if (!*n)
198 n = "HEAD";
a8be83fe 199 if (!get_extended_sha1(n, end)) {
8ebb0184
LT
200 if (no_revs)
201 continue;
202 def = NULL;
042a4ed7
LT
203 show_rev(NORMAL, end);
204 show_rev(REVERSED, sha1);
178cb243
LT
205 continue;
206 }
207 }
208 *dotdot = '.';
209 }
a8be83fe 210 if (!get_extended_sha1(arg, sha1)) {
800644c5
LT
211 if (no_revs)
212 continue;
213 def = NULL;
042a4ed7 214 show_rev(NORMAL, sha1);
800644c5
LT
215 continue;
216 }
a8be83fe 217 if (*arg == '^' && !get_extended_sha1(arg+1, sha1)) {
800644c5
LT
218 if (no_revs)
219 continue;
220 def = NULL;
042a4ed7 221 show_rev(REVERSED, sha1);
800644c5
LT
222 continue;
223 }
023d66ed
LT
224 show_default();
225 show_norev(arg);
226 }
227 show_default();
228 if (single_rev && output_revs != 1) {
229 fprintf(stderr, "Needed a single revision\n");
230 exit(1);
178cb243 231 }
178cb243
LT
232 return 0;
233}