[PATCH] Allow file removal when "git commit --all" is used.
[git/jnareb-git.git] / rev-parse.c
blobf1f5163447e98f1afb65137fd7cc9db7ff793b29
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
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;
18 static int output_sq = 0;
19 static int symbolic = 0;
21 #define NORMAL 0
22 #define REVERSED 1
23 static int show_type = NORMAL;
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[] = {
33 "--max-count=",
34 "--max-age=",
35 "--min-age=",
36 "--merge-order",
37 "--topo-order",
38 "--bisect",
39 "--no-merges",
40 NULL
42 const char **p = rev_args;
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;
55 static void show(const char *arg)
57 if (output_sq) {
58 int sq = '\'', ch;
60 putchar(sq);
61 while ((ch = *arg++)) {
62 if (ch == sq)
63 fputs("'\\'", stdout);
64 putchar(ch);
66 putchar(sq);
67 putchar(' ');
69 else
70 puts(arg);
73 static void show_rev(int type, const unsigned char *sha1, const char *name)
75 if (no_revs)
76 return;
77 output_revs++;
79 if (type != show_type)
80 putchar('^');
81 if (symbolic && name)
82 show(name);
83 else
84 show(sha1_to_hex(sha1));
87 static void show_rev_arg(char *rev)
89 if (no_revs)
90 return;
91 show(rev);
94 static void show_norev(char *norev)
96 if (flags_only)
97 return;
98 if (revs_only)
99 return;
100 show(norev);
103 static void show_arg(char *arg)
105 if (no_flags)
106 return;
107 if (do_rev_argument && is_rev_argument(arg))
108 show_rev_arg(arg);
109 else
110 show_norev(arg);
113 static void show_default(void)
115 char *s = def;
117 if (s) {
118 unsigned char sha1[20];
120 def = NULL;
121 if (!get_sha1(s, sha1)) {
122 show_rev(NORMAL, sha1, s);
123 return;
125 show_arg(s);
129 static int show_reference(const char *refname, const unsigned char *sha1)
131 show_rev(NORMAL, sha1, refname);
132 return 0;
135 int main(int argc, char **argv)
137 int i, as_is = 0;
138 unsigned char sha1[20];
139 const char *prefix = setup_git_directory();
141 for (i = 1; i < argc; i++) {
142 char *arg = argv[i];
143 char *dotdot;
145 if (as_is) {
146 show_norev(arg);
147 continue;
149 if (*arg == '-') {
150 if (!strcmp(arg, "--")) {
151 show_default();
152 if (revs_only)
153 break;
154 as_is = 1;
156 if (!strcmp(arg, "--default")) {
157 def = argv[i+1];
158 i++;
159 continue;
161 if (!strcmp(arg, "--revs-only")) {
162 revs_only = 1;
163 continue;
165 if (!strcmp(arg, "--no-revs")) {
166 no_revs = 1;
167 continue;
169 if (!strcmp(arg, "--flags")) {
170 flags_only = 1;
171 continue;
173 if (!strcmp(arg, "--no-flags")) {
174 no_flags = 1;
175 continue;
177 if (!strcmp(arg, "--verify")) {
178 revs_only = 1;
179 do_rev_argument = 0;
180 single_rev = 1;
181 continue;
183 if (!strcmp(arg, "--sq")) {
184 output_sq = 1;
185 continue;
187 if (!strcmp(arg, "--not")) {
188 show_type ^= REVERSED;
189 continue;
191 if (!strcmp(arg, "--symbolic")) {
192 symbolic = 1;
193 continue;
195 if (!strcmp(arg, "--all")) {
196 for_each_ref(show_reference);
197 continue;
199 if (!strcmp(arg, "--show-prefix")) {
200 puts(prefix);
201 continue;
203 show_arg(arg);
204 continue;
206 dotdot = strstr(arg, "..");
207 if (dotdot) {
208 unsigned char end[20];
209 char *n = dotdot+2;
210 *dotdot = 0;
211 if (!get_sha1(arg, sha1)) {
212 if (!*n)
213 n = "HEAD";
214 if (!get_sha1(n, end)) {
215 if (no_revs)
216 continue;
217 def = NULL;
218 show_rev(NORMAL, end, n);
219 show_rev(REVERSED, sha1, arg);
220 continue;
223 *dotdot = '.';
225 if (!get_sha1(arg, sha1)) {
226 if (no_revs)
227 continue;
228 def = NULL;
229 show_rev(NORMAL, sha1, arg);
230 continue;
232 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
233 if (no_revs)
234 continue;
235 def = NULL;
236 show_rev(REVERSED, sha1, arg+1);
237 continue;
239 show_default();
240 show_norev(arg);
242 show_default();
243 if (single_rev && output_revs != 1) {
244 fprintf(stderr, "Needed a single revision\n");
245 exit(1);
247 return 0;