Generate pack info file after repack.
[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 7#include "commit.h"
960bba0d 8#include "refs.h"
a8be83fe 9
023d66ed
LT
10static char *def = NULL;
11static int no_revs = 0;
12static int single_rev = 0;
13static int revs_only = 0;
14static int do_rev_argument = 1;
15static int output_revs = 0;
f79b65aa
LT
16static int flags_only = 0;
17static int no_flags = 0;
5bb2c65a 18static int output_sq = 0;
30b96fce 19static int symbolic = 0;
023d66ed 20
042a4ed7
LT
21#define NORMAL 0
22#define REVERSED 1
23static int show_type = NORMAL;
24
921d865e
LT
25/*
26 * Some arguments are relevant "revision" arguments,
27 * others are about output format or other details.
28 * This sorts it all out.
29 */
30static int is_rev_argument(const char *arg)
31{
32 static const char *rev_args[] = {
33 "--max-count=",
34 "--max-age=",
35 "--min-age=",
36 "--merge-order",
5ccfb758
JH
37 "--topo-order",
38 "--bisect",
39 "--no-merges",
921d865e
LT
40 NULL
41 };
42 const char **p = rev_args;
43
44 for (;;) {
45 const char *str = *p++;
46 int len;
47 if (!str)
48 return 0;
49 len = strlen(str);
50 if (!strncmp(arg, str, len))
51 return 1;
52 }
53}
54
5bb2c65a
JH
55static void show(const char *arg)
56{
57 if (output_sq) {
58 int sq = '\'', ch;
59
60 putchar(sq);
61 while ((ch = *arg++)) {
62 if (ch == sq)
63 fputs("'\\'", stdout);
64 putchar(ch);
65 }
66 putchar(sq);
67 putchar(' ');
68 }
69 else
70 puts(arg);
71}
72
30b96fce 73static void show_rev(int type, const unsigned char *sha1, const char *name)
023d66ed
LT
74{
75 if (no_revs)
76 return;
77 output_revs++;
5bb2c65a 78
30b96fce
JH
79 if (type != show_type)
80 putchar('^');
81 if (symbolic && name)
82 show(name);
83 else
84 show(sha1_to_hex(sha1));
023d66ed
LT
85}
86
87static void show_rev_arg(char *rev)
88{
89 if (no_revs)
90 return;
5bb2c65a 91 show(rev);
023d66ed
LT
92}
93
94static void show_norev(char *norev)
95{
f79b65aa
LT
96 if (flags_only)
97 return;
023d66ed
LT
98 if (revs_only)
99 return;
5bb2c65a 100 show(norev);
023d66ed
LT
101}
102
103static void show_arg(char *arg)
104{
f79b65aa
LT
105 if (no_flags)
106 return;
023d66ed
LT
107 if (do_rev_argument && is_rev_argument(arg))
108 show_rev_arg(arg);
109 else
0360e99d 110 show(arg);
023d66ed
LT
111}
112
023d66ed
LT
113static void show_default(void)
114{
115 char *s = def;
116
117 if (s) {
118 unsigned char sha1[20];
119
120 def = NULL;
9938af6a 121 if (!get_sha1(s, sha1)) {
30b96fce 122 show_rev(NORMAL, sha1, s);
023d66ed
LT
123 return;
124 }
0360e99d 125 show_norev(s);
023d66ed
LT
126 }
127}
128
960bba0d
LT
129static int show_reference(const char *refname, const unsigned char *sha1)
130{
30b96fce 131 show_rev(NORMAL, sha1, refname);
960bba0d
LT
132 return 0;
133}
134
178cb243
LT
135int main(int argc, char **argv)
136{
023d66ed 137 int i, as_is = 0;
178cb243 138 unsigned char sha1[20];
d288a700
LT
139 const char *prefix = setup_git_directory();
140
178cb243
LT
141 for (i = 1; i < argc; i++) {
142 char *arg = argv[i];
143 char *dotdot;
144
145 if (as_is) {
023d66ed 146 show_norev(arg);
178cb243
LT
147 continue;
148 }
149 if (*arg == '-') {
150 if (!strcmp(arg, "--")) {
023d66ed 151 show_default();
0360e99d 152 if (revs_only || flags_only)
8ebb0184 153 break;
178cb243
LT
154 as_is = 1;
155 }
156 if (!strcmp(arg, "--default")) {
178cb243
LT
157 def = argv[i+1];
158 i++;
159 continue;
160 }
8ebb0184
LT
161 if (!strcmp(arg, "--revs-only")) {
162 revs_only = 1;
163 continue;
164 }
165 if (!strcmp(arg, "--no-revs")) {
166 no_revs = 1;
167 continue;
168 }
f79b65aa
LT
169 if (!strcmp(arg, "--flags")) {
170 flags_only = 1;
171 continue;
172 }
173 if (!strcmp(arg, "--no-flags")) {
174 no_flags = 1;
175 continue;
176 }
023d66ed
LT
177 if (!strcmp(arg, "--verify")) {
178 revs_only = 1;
179 do_rev_argument = 0;
180 single_rev = 1;
181 continue;
921d865e 182 }
5bb2c65a
JH
183 if (!strcmp(arg, "--sq")) {
184 output_sq = 1;
185 continue;
186 }
042a4ed7
LT
187 if (!strcmp(arg, "--not")) {
188 show_type ^= REVERSED;
189 continue;
190 }
30b96fce
JH
191 if (!strcmp(arg, "--symbolic")) {
192 symbolic = 1;
193 continue;
194 }
960bba0d
LT
195 if (!strcmp(arg, "--all")) {
196 for_each_ref(show_reference);
197 continue;
198 }
d288a700
LT
199 if (!strcmp(arg, "--show-prefix")) {
200 puts(prefix);
201 continue;
202 }
023d66ed 203 show_arg(arg);
178cb243
LT
204 continue;
205 }
178cb243
LT
206 dotdot = strstr(arg, "..");
207 if (dotdot) {
208 unsigned char end[20];
209 char *n = dotdot+2;
210 *dotdot = 0;
9938af6a 211 if (!get_sha1(arg, sha1)) {
178cb243
LT
212 if (!*n)
213 n = "HEAD";
9938af6a 214 if (!get_sha1(n, end)) {
8ebb0184
LT
215 if (no_revs)
216 continue;
217 def = NULL;
30b96fce
JH
218 show_rev(NORMAL, end, n);
219 show_rev(REVERSED, sha1, arg);
178cb243
LT
220 continue;
221 }
222 }
223 *dotdot = '.';
224 }
9938af6a 225 if (!get_sha1(arg, sha1)) {
800644c5
LT
226 if (no_revs)
227 continue;
228 def = NULL;
30b96fce 229 show_rev(NORMAL, sha1, arg);
800644c5
LT
230 continue;
231 }
9938af6a 232 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
800644c5
LT
233 if (no_revs)
234 continue;
235 def = NULL;
30b96fce 236 show_rev(REVERSED, sha1, arg+1);
800644c5
LT
237 continue;
238 }
023d66ed
LT
239 show_default();
240 show_norev(arg);
241 }
242 show_default();
243 if (single_rev && output_revs != 1) {
244 fprintf(stderr, "Needed a single revision\n");
245 exit(1);
178cb243 246 }
178cb243
LT
247 return 0;
248}