[PATCH] Make "git diff" work inside relative subdirectories
[git.git] / rev-parse.c
blob39cf63540e389683e020563f6f3a1f51f44c2f20
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];
137 const char *prefix = setup_git_directory();
139 for (i = 1; i < argc; i++) {
140 char *arg = argv[i];
141 char *dotdot;
143 if (as_is) {
144 show_norev(arg);
145 continue;
147 if (*arg == '-') {
148 if (!strcmp(arg, "--")) {
149 show_default();
150 if (revs_only)
151 break;
152 as_is = 1;
154 if (!strcmp(arg, "--default")) {
155 def = argv[i+1];
156 i++;
157 continue;
159 if (!strcmp(arg, "--revs-only")) {
160 revs_only = 1;
161 continue;
163 if (!strcmp(arg, "--no-revs")) {
164 no_revs = 1;
165 continue;
167 if (!strcmp(arg, "--flags")) {
168 flags_only = 1;
169 continue;
171 if (!strcmp(arg, "--no-flags")) {
172 no_flags = 1;
173 continue;
175 if (!strcmp(arg, "--verify")) {
176 revs_only = 1;
177 do_rev_argument = 0;
178 single_rev = 1;
179 continue;
181 if (!strcmp(arg, "--sq")) {
182 output_sq = 1;
183 continue;
185 if (!strcmp(arg, "--not")) {
186 show_type ^= REVERSED;
187 continue;
189 if (!strcmp(arg, "--all")) {
190 for_each_ref(show_reference);
191 continue;
193 if (!strcmp(arg, "--show-prefix")) {
194 puts(prefix);
195 continue;
197 show_arg(arg);
198 continue;
200 dotdot = strstr(arg, "..");
201 if (dotdot) {
202 unsigned char end[20];
203 char *n = dotdot+2;
204 *dotdot = 0;
205 if (!get_sha1(arg, sha1)) {
206 if (!*n)
207 n = "HEAD";
208 if (!get_sha1(n, end)) {
209 if (no_revs)
210 continue;
211 def = NULL;
212 show_rev(NORMAL, end);
213 show_rev(REVERSED, sha1);
214 continue;
217 *dotdot = '.';
219 if (!get_sha1(arg, sha1)) {
220 if (no_revs)
221 continue;
222 def = NULL;
223 show_rev(NORMAL, sha1);
224 continue;
226 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
227 if (no_revs)
228 continue;
229 def = NULL;
230 show_rev(REVERSED, sha1);
231 continue;
233 show_default();
234 show_norev(arg);
236 show_default();
237 if (single_rev && output_revs != 1) {
238 fprintf(stderr, "Needed a single revision\n");
239 exit(1);
241 return 0;