Merge with master.
[git/mingw.git] / rev-parse.c
blob1965e90a8a0c3fa25c2fccfcc82f5da395a093bc
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;
20 #define NORMAL 0
21 #define REVERSED 1
22 static int show_type = NORMAL;
25 * Some arguments are relevant "revision" arguments,
26 * others are about output format or other details.
27 * This sorts it all out.
29 static int is_rev_argument(const char *arg)
31 static const char *rev_args[] = {
32 "--max-count=",
33 "--max-age=",
34 "--min-age=",
35 "--merge-order",
36 NULL
38 const char **p = rev_args;
40 for (;;) {
41 const char *str = *p++;
42 int len;
43 if (!str)
44 return 0;
45 len = strlen(str);
46 if (!strncmp(arg, str, len))
47 return 1;
51 static void show(const char *arg)
53 if (output_sq) {
54 int sq = '\'', ch;
56 putchar(sq);
57 while ((ch = *arg++)) {
58 if (ch == sq)
59 fputs("'\\'", stdout);
60 putchar(ch);
62 putchar(sq);
63 putchar(' ');
65 else
66 puts(arg);
69 static void show_rev(int type, const unsigned char *sha1)
71 if (no_revs)
72 return;
73 output_revs++;
75 /* Hexadecimal string plus possibly a carret;
76 * this does not have to be quoted even under output_sq.
78 printf("%s%s%c", type == show_type ? "" : "^", sha1_to_hex(sha1),
79 output_sq ? ' ' : '\n');
82 static void show_rev_arg(char *rev)
84 if (no_revs)
85 return;
86 show(rev);
89 static void show_norev(char *norev)
91 if (flags_only)
92 return;
93 if (revs_only)
94 return;
95 show(norev);
98 static void show_arg(char *arg)
100 if (no_flags)
101 return;
102 if (do_rev_argument && is_rev_argument(arg))
103 show_rev_arg(arg);
104 else
105 show_norev(arg);
108 static void show_default(void)
110 char *s = def;
112 if (s) {
113 unsigned char sha1[20];
115 def = NULL;
116 if (!get_sha1(s, sha1)) {
117 show_rev(NORMAL, sha1);
118 return;
120 show_arg(s);
124 static int show_reference(const char *refname, const unsigned char *sha1)
126 show_rev(NORMAL, sha1);
127 return 0;
130 int main(int argc, char **argv)
132 int i, as_is = 0;
133 unsigned char sha1[20];
135 for (i = 1; i < argc; i++) {
136 char *arg = argv[i];
137 char *dotdot;
139 if (as_is) {
140 show_norev(arg);
141 continue;
143 if (*arg == '-') {
144 if (!strcmp(arg, "--")) {
145 show_default();
146 if (revs_only)
147 break;
148 as_is = 1;
150 if (!strcmp(arg, "--default")) {
151 def = argv[i+1];
152 i++;
153 continue;
155 if (!strcmp(arg, "--revs-only")) {
156 revs_only = 1;
157 continue;
159 if (!strcmp(arg, "--no-revs")) {
160 no_revs = 1;
161 continue;
163 if (!strcmp(arg, "--flags")) {
164 flags_only = 1;
165 continue;
167 if (!strcmp(arg, "--no-flags")) {
168 no_flags = 1;
169 continue;
171 if (!strcmp(arg, "--verify")) {
172 revs_only = 1;
173 do_rev_argument = 0;
174 single_rev = 1;
175 continue;
177 if (!strcmp(arg, "--sq")) {
178 output_sq = 1;
179 continue;
181 if (!strcmp(arg, "--not")) {
182 show_type ^= REVERSED;
183 continue;
185 if (!strcmp(arg, "--all")) {
186 for_each_ref(show_reference);
187 continue;
189 show_arg(arg);
190 continue;
192 dotdot = strstr(arg, "..");
193 if (dotdot) {
194 unsigned char end[20];
195 char *n = dotdot+2;
196 *dotdot = 0;
197 if (!get_sha1(arg, sha1)) {
198 if (!*n)
199 n = "HEAD";
200 if (!get_sha1(n, end)) {
201 if (no_revs)
202 continue;
203 def = NULL;
204 show_rev(NORMAL, end);
205 show_rev(REVERSED, sha1);
206 continue;
209 *dotdot = '.';
211 if (!get_sha1(arg, sha1)) {
212 if (no_revs)
213 continue;
214 def = NULL;
215 show_rev(NORMAL, sha1);
216 continue;
218 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
219 if (no_revs)
220 continue;
221 def = NULL;
222 show_rev(REVERSED, sha1);
223 continue;
225 show_default();
226 show_norev(arg);
228 show_default();
229 if (single_rev && output_revs != 1) {
230 fprintf(stderr, "Needed a single revision\n");
231 exit(1);
233 return 0;