[PATCH] Add "--sign" option to git-format-patch-script
[git/jnareb-git.git] / rev-parse.c
blob1c6ae76f9aa32dab77a4189a4ae6b744d39cee36
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 "--topo-order",
37 "--bisect",
38 "--no-merges",
39 NULL
41 const char **p = rev_args;
43 for (;;) {
44 const char *str = *p++;
45 int len;
46 if (!str)
47 return 0;
48 len = strlen(str);
49 if (!strncmp(arg, str, len))
50 return 1;
54 static void show(const char *arg)
56 if (output_sq) {
57 int sq = '\'', ch;
59 putchar(sq);
60 while ((ch = *arg++)) {
61 if (ch == sq)
62 fputs("'\\'", stdout);
63 putchar(ch);
65 putchar(sq);
66 putchar(' ');
68 else
69 puts(arg);
72 static void show_rev(int type, const unsigned char *sha1)
74 if (no_revs)
75 return;
76 output_revs++;
78 /* Hexadecimal string plus possibly a carret;
79 * this does not have to be quoted even under output_sq.
81 printf("%s%s%c", type == show_type ? "" : "^", sha1_to_hex(sha1),
82 output_sq ? ' ' : '\n');
85 static void show_rev_arg(char *rev)
87 if (no_revs)
88 return;
89 show(rev);
92 static void show_norev(char *norev)
94 if (flags_only)
95 return;
96 if (revs_only)
97 return;
98 show(norev);
101 static void show_arg(char *arg)
103 if (no_flags)
104 return;
105 if (do_rev_argument && is_rev_argument(arg))
106 show_rev_arg(arg);
107 else
108 show_norev(arg);
111 static void show_default(void)
113 char *s = def;
115 if (s) {
116 unsigned char sha1[20];
118 def = NULL;
119 if (!get_sha1(s, sha1)) {
120 show_rev(NORMAL, sha1);
121 return;
123 show_arg(s);
127 static int show_reference(const char *refname, const unsigned char *sha1)
129 show_rev(NORMAL, sha1);
130 return 0;
133 int main(int argc, char **argv)
135 int i, as_is = 0;
136 unsigned char sha1[20];
138 for (i = 1; i < argc; i++) {
139 char *arg = argv[i];
140 char *dotdot;
142 if (as_is) {
143 show_norev(arg);
144 continue;
146 if (*arg == '-') {
147 if (!strcmp(arg, "--")) {
148 show_default();
149 if (revs_only)
150 break;
151 as_is = 1;
153 if (!strcmp(arg, "--default")) {
154 def = argv[i+1];
155 i++;
156 continue;
158 if (!strcmp(arg, "--revs-only")) {
159 revs_only = 1;
160 continue;
162 if (!strcmp(arg, "--no-revs")) {
163 no_revs = 1;
164 continue;
166 if (!strcmp(arg, "--flags")) {
167 flags_only = 1;
168 continue;
170 if (!strcmp(arg, "--no-flags")) {
171 no_flags = 1;
172 continue;
174 if (!strcmp(arg, "--verify")) {
175 revs_only = 1;
176 do_rev_argument = 0;
177 single_rev = 1;
178 continue;
180 if (!strcmp(arg, "--sq")) {
181 output_sq = 1;
182 continue;
184 if (!strcmp(arg, "--not")) {
185 show_type ^= REVERSED;
186 continue;
188 if (!strcmp(arg, "--all")) {
189 for_each_ref(show_reference);
190 continue;
192 show_arg(arg);
193 continue;
195 dotdot = strstr(arg, "..");
196 if (dotdot) {
197 unsigned char end[20];
198 char *n = dotdot+2;
199 *dotdot = 0;
200 if (!get_sha1(arg, sha1)) {
201 if (!*n)
202 n = "HEAD";
203 if (!get_sha1(n, end)) {
204 if (no_revs)
205 continue;
206 def = NULL;
207 show_rev(NORMAL, end);
208 show_rev(REVERSED, sha1);
209 continue;
212 *dotdot = '.';
214 if (!get_sha1(arg, sha1)) {
215 if (no_revs)
216 continue;
217 def = NULL;
218 show_rev(NORMAL, sha1);
219 continue;
221 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
222 if (no_revs)
223 continue;
224 def = NULL;
225 show_rev(REVERSED, sha1);
226 continue;
228 show_default();
229 show_norev(arg);
231 show_default();
232 if (single_rev && output_revs != 1) {
233 fprintf(stderr, "Needed a single revision\n");
234 exit(1);
236 return 0;