Add a test for rebase when a change was picked upstream
[git/mingw/j6t.git] / rev-parse.c
blob0c951af0d49da459fc6c85c32707b016bf581a57
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9 #include "quote.h"
11 #define DO_REVS 1
12 #define DO_NOREV 2
13 #define DO_FLAGS 4
14 #define DO_NONFLAGS 8
15 static int filter = ~0;
17 static char *def = NULL;
19 #define NORMAL 0
20 #define REVERSED 1
21 static int show_type = NORMAL;
22 static int symbolic = 0;
23 static int output_sq = 0;
25 static int revs_count = 0;
28 * Some arguments are relevant "revision" arguments,
29 * others are about output format or other details.
30 * This sorts it all out.
32 static int is_rev_argument(const char *arg)
34 static const char *rev_args[] = {
35 "--all",
36 "--bisect",
37 "--dense",
38 "--header",
39 "--max-age=",
40 "--max-count=",
41 "--merge-order",
42 "--min-age=",
43 "--no-merges",
44 "--objects",
45 "--parents",
46 "--pretty",
47 "--show-breaks",
48 "--sparse",
49 "--topo-order",
50 "--unpacked",
51 NULL
53 const char **p = rev_args;
55 for (;;) {
56 const char *str = *p++;
57 int len;
58 if (!str)
59 return 0;
60 len = strlen(str);
61 if (!strcmp(arg, str) ||
62 (str[len-1] == '=' && !strncmp(arg, str, len)))
63 return 1;
67 /* Output argument as a string, either SQ or normal */
68 static void show(const char *arg)
70 if (output_sq) {
71 int sq = '\'', ch;
73 putchar(sq);
74 while ((ch = *arg++)) {
75 if (ch == sq)
76 fputs("'\\'", stdout);
77 putchar(ch);
79 putchar(sq);
80 putchar(' ');
82 else
83 puts(arg);
86 /* Output a revision, only if filter allows it */
87 static void show_rev(int type, const unsigned char *sha1, const char *name)
89 if (!(filter & DO_REVS))
90 return;
91 def = NULL;
92 revs_count++;
94 if (type != show_type)
95 putchar('^');
96 if (symbolic && name)
97 show(name);
98 else
99 show(sha1_to_hex(sha1));
102 /* Output a flag, only if filter allows it. */
103 static void show_flag(char *arg)
105 if (!(filter & DO_FLAGS))
106 return;
107 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
108 show(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, s);
121 return;
126 static int show_reference(const char *refname, const unsigned char *sha1)
128 show_rev(NORMAL, sha1, refname);
129 return 0;
132 static void show_datestring(const char *flag, const char *datestr)
134 static char buffer[100];
136 /* date handling requires both flags and revs */
137 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
138 return;
139 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
140 show(buffer);
143 static void show_file(const char *arg)
145 show_default();
146 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
147 show(arg);
150 int main(int argc, char **argv)
152 int i, as_is = 0, verify = 0;
153 unsigned char sha1[20];
154 const char *prefix = setup_git_directory();
156 for (i = 1; i < argc; i++) {
157 char *arg = argv[i];
158 char *dotdot;
160 if (as_is) {
161 show_file(arg);
162 continue;
164 if (*arg == '-') {
165 if (!strcmp(arg, "--")) {
166 as_is = 1;
167 /* Pass on the "--" if we show anything but files.. */
168 if (filter & (DO_FLAGS | DO_REVS))
169 show_file(arg);
170 continue;
172 if (!strcmp(arg, "--default")) {
173 def = argv[i+1];
174 i++;
175 continue;
177 if (!strcmp(arg, "--revs-only")) {
178 filter &= ~DO_NOREV;
179 continue;
181 if (!strcmp(arg, "--no-revs")) {
182 filter &= ~DO_REVS;
183 continue;
185 if (!strcmp(arg, "--flags")) {
186 filter &= ~DO_NONFLAGS;
187 continue;
189 if (!strcmp(arg, "--no-flags")) {
190 filter &= ~DO_FLAGS;
191 continue;
193 if (!strcmp(arg, "--verify")) {
194 filter &= ~(DO_FLAGS|DO_NOREV);
195 verify = 1;
196 continue;
198 if (!strcmp(arg, "--sq")) {
199 output_sq = 1;
200 continue;
202 if (!strcmp(arg, "--not")) {
203 show_type ^= REVERSED;
204 continue;
206 if (!strcmp(arg, "--symbolic")) {
207 symbolic = 1;
208 continue;
210 if (!strcmp(arg, "--all")) {
211 for_each_ref(show_reference);
212 continue;
214 if (!strcmp(arg, "--show-prefix")) {
215 if (prefix)
216 puts(prefix);
217 continue;
219 if (!strcmp(arg, "--show-cdup")) {
220 const char *pfx = prefix;
221 while (pfx) {
222 pfx = strchr(pfx, '/');
223 if (pfx) {
224 pfx++;
225 printf("../");
228 putchar('\n');
229 continue;
231 if (!strcmp(arg, "--git-dir")) {
232 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
233 static char cwd[PATH_MAX];
234 if (gitdir) {
235 puts(gitdir);
236 continue;
238 if (!prefix) {
239 puts(".git");
240 continue;
242 if (!getcwd(cwd, PATH_MAX))
243 die("unable to get current working directory");
244 printf("%s/.git\n", cwd);
245 continue;
247 if (!strncmp(arg, "--since=", 8)) {
248 show_datestring("--max-age=", arg+8);
249 continue;
251 if (!strncmp(arg, "--after=", 8)) {
252 show_datestring("--max-age=", arg+8);
253 continue;
255 if (!strncmp(arg, "--before=", 9)) {
256 show_datestring("--min-age=", arg+9);
257 continue;
259 if (!strncmp(arg, "--until=", 8)) {
260 show_datestring("--min-age=", arg+8);
261 continue;
263 if (verify)
264 die("Needed a single revision");
265 show_flag(arg);
266 continue;
269 /* Not a flag argument */
270 dotdot = strstr(arg, "..");
271 if (dotdot) {
272 unsigned char end[20];
273 char *n = dotdot+2;
274 *dotdot = 0;
275 if (!get_sha1(arg, sha1)) {
276 if (!*n)
277 n = "HEAD";
278 if (!get_sha1(n, end)) {
279 show_rev(NORMAL, end, n);
280 show_rev(REVERSED, sha1, arg);
281 continue;
284 *dotdot = '.';
286 if (!get_sha1(arg, sha1)) {
287 show_rev(NORMAL, sha1, arg);
288 continue;
290 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
291 show_rev(REVERSED, sha1, arg+1);
292 continue;
294 if (verify)
295 die("Needed a single revision");
296 as_is = 1;
297 show_file(arg);
299 show_default();
300 if (verify && revs_count != 1)
301 die("Needed a single revision");
302 return 0;