builtin-rev-parse.c 7.68 KB
Newer Older
1 2 3 4 5 6
/*
 * rev-parse.c
 *
 * Copyright (C) Linus Torvalds, 2005
 */
#include "cache.h"
7
#include "commit.h"
8
#include "refs.h"
9
#include "quote.h"
Christian Couder's avatar
Christian Couder committed
10
#include "builtin.h"
11

12 13 14 15 16 17
#define DO_REVS		1
#define DO_NOREV	2
#define DO_FLAGS	4
#define DO_NONFLAGS	8
static int filter = ~0;

18
static const char *def;
19

20 21 22
#define NORMAL 0
#define REVERSED 1
static int show_type = NORMAL;
23 24 25
static int symbolic;
static int abbrev;
static int output_sq;
26

27
static int revs_count;
28

29 30 31 32 33 34 35 36
/*
 * Some arguments are relevant "revision" arguments,
 * others are about output format or other details.
 * This sorts it all out.
 */
static int is_rev_argument(const char *arg)
{
	static const char *rev_args[] = {
37
		"--all",
38
		"--bisect",
39
		"--dense",
40
		"--branches",
41
		"--header",
42
		"--max-age=",
43 44
		"--max-count=",
		"--min-age=",
45
		"--no-merges",
46
		"--objects",
Junio C Hamano's avatar
Junio C Hamano committed
47
		"--objects-edge",
48 49
		"--parents",
		"--pretty",
50
		"--remotes",
51
		"--sparse",
52
		"--tags",
53
		"--topo-order",
54
		"--date-order",
55
		"--unpacked",
56 57 58 59
		NULL
	};
	const char **p = rev_args;

60 61 62 63
	/* accept -<digit>, like traditional "head" */
	if ((*arg == '-') && isdigit(arg[1]))
		return 1;

64 65 66 67 68 69
	for (;;) {
		const char *str = *p++;
		int len;
		if (!str)
			return 0;
		len = strlen(str);
70 71
		if (!strcmp(arg, str) ||
		    (str[len-1] == '=' && !strncmp(arg, str, len)))
72 73 74 75
			return 1;
	}
}

76
/* Output argument as a string, either SQ or normal */
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static void show(const char *arg)
{
	if (output_sq) {
		int sq = '\'', ch;

		putchar(sq);
		while ((ch = *arg++)) {
			if (ch == sq)
				fputs("'\\'", stdout);
			putchar(ch);
		}
		putchar(sq);
		putchar(' ');
	}
	else
		puts(arg);
}

95
/* Output a revision, only if filter allows it */
96
static void show_rev(int type, const unsigned char *sha1, const char *name)
97
{
98
	if (!(filter & DO_REVS))
99
		return;
100 101
	def = NULL;
	revs_count++;
102

103 104 105 106
	if (type != show_type)
		putchar('^');
	if (symbolic && name)
		show(name);
Junio C Hamano's avatar
Junio C Hamano committed
107 108
	else if (abbrev)
		show(find_unique_abbrev(sha1, abbrev));
109 110
	else
		show(sha1_to_hex(sha1));
111 112
}

113
/* Output a flag, only if filter allows it. */
114
static int show_flag(const char *arg)
115
{
116
	if (!(filter & DO_FLAGS))
117 118
		return 0;
	if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
119
		show(arg);
120 121 122
		return 1;
	}
	return 0;
123 124 125 126
}

static void show_default(void)
{
127
	const char *s = def;
128 129 130 131 132

	if (s) {
		unsigned char sha1[20];

		def = NULL;
133
		if (!get_sha1(s, sha1)) {
134
			show_rev(NORMAL, sha1, s);
135 136 137 138 139
			return;
		}
	}
}

140
static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
141
{
142
	show_rev(NORMAL, sha1, refname);
143 144 145
	return 0;
}

146 147 148 149 150 151 152
static void show_datestring(const char *flag, const char *datestr)
{
	static char buffer[100];

	/* date handling requires both flags and revs */
	if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
		return;
153
	snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
154 155 156
	show(buffer);
}

157
static int show_file(const char *arg)
158
{
159
	show_default();
160
	if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
161
		show(arg);
162 163 164
		return 1;
	}
	return 0;
165 166
}

167
static int try_difference(const char *arg)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
{
	char *dotdot;
	unsigned char sha1[20];
	unsigned char end[20];
	const char *next;
	const char *this;
	int symmetric;

	if (!(dotdot = strstr(arg, "..")))
		return 0;
	next = dotdot + 2;
	this = arg;
	symmetric = (*next == '.');

	*dotdot = 0;
	next += symmetric;

	if (!*next)
		next = "HEAD";
	if (dotdot == arg)
		this = "HEAD";
	if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
		show_rev(NORMAL, end, next);
		show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
		if (symmetric) {
			struct commit_list *exclude;
			struct commit *a, *b;
			a = lookup_commit_reference(sha1);
			b = lookup_commit_reference(end);
			exclude = get_merge_bases(a, b, 1);
			while (exclude) {
				struct commit_list *n = exclude->next;
				show_rev(REVERSED,
					 exclude->item->object.sha1,NULL);
				free(exclude);
				exclude = n;
			}
		}
		return 1;
	}
	*dotdot = '.';
	return 0;
}

212
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
213
{
214
	int i, as_is = 0, verify = 0;
215
	unsigned char sha1[20];
216

217 218
	git_config(git_default_config);

219
	for (i = 1; i < argc; i++) {
220
		const char *arg = argv[i];
221

222
		if (as_is) {
223
			if (show_file(arg) && as_is < 2)
224
				verify_filename(prefix, arg);
225 226
			continue;
		}
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		if (!strcmp(arg,"-n")) {
			if (++i >= argc)
				die("-n requires an argument");
			if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
				show(arg);
				show(argv[i]);
			}
			continue;
		}
		if (!strncmp(arg,"-n",2)) {
			if ((filter & DO_FLAGS) && (filter & DO_REVS))
				show(arg);
			continue;
		}

242 243
		if (*arg == '-') {
			if (!strcmp(arg, "--")) {
244
				as_is = 2;
245 246 247
				/* Pass on the "--" if we show anything but files.. */
				if (filter & (DO_FLAGS | DO_REVS))
					show_file(arg);
248
				continue;
249 250 251 252 253 254
			}
			if (!strcmp(arg, "--default")) {
				def = argv[i+1];
				i++;
				continue;
			}
255
			if (!strcmp(arg, "--revs-only")) {
256
				filter &= ~DO_NOREV;
257 258 259
				continue;
			}
			if (!strcmp(arg, "--no-revs")) {
260
				filter &= ~DO_REVS;
261 262
				continue;
			}
263
			if (!strcmp(arg, "--flags")) {
264
				filter &= ~DO_NONFLAGS;
265 266 267
				continue;
			}
			if (!strcmp(arg, "--no-flags")) {
268
				filter &= ~DO_FLAGS;
269 270
				continue;
			}
271
			if (!strcmp(arg, "--verify")) {
272 273
				filter &= ~(DO_FLAGS|DO_NOREV);
				verify = 1;
274
				continue;
275
			}
276
			if (!strcmp(arg, "--short") ||
277
			    !strncmp(arg, "--short=", 8)) {
Junio C Hamano's avatar
Junio C Hamano committed
278 279 280
				filter &= ~(DO_FLAGS|DO_NOREV);
				verify = 1;
				abbrev = DEFAULT_ABBREV;
281 282
				if (arg[7] == '=')
					abbrev = strtoul(arg + 8, NULL, 10);
283 284 285 286
				if (abbrev < MINIMUM_ABBREV)
					abbrev = MINIMUM_ABBREV;
				else if (40 <= abbrev)
					abbrev = 40;
Junio C Hamano's avatar
Junio C Hamano committed
287 288
				continue;
			}
289 290 291 292
			if (!strcmp(arg, "--sq")) {
				output_sq = 1;
				continue;
			}
293 294 295 296
			if (!strcmp(arg, "--not")) {
				show_type ^= REVERSED;
				continue;
			}
297 298 299 300
			if (!strcmp(arg, "--symbolic")) {
				symbolic = 1;
				continue;
			}
301
			if (!strcmp(arg, "--all")) {
302
				for_each_ref(show_reference, NULL);
303 304
				continue;
			}
305
			if (!strcmp(arg, "--branches")) {
306
				for_each_branch_ref(show_reference, NULL);
307 308 309
				continue;
			}
			if (!strcmp(arg, "--tags")) {
310
				for_each_tag_ref(show_reference, NULL);
311 312 313
				continue;
			}
			if (!strcmp(arg, "--remotes")) {
314
				for_each_remote_ref(show_reference, NULL);
315 316
				continue;
			}
317
			if (!strcmp(arg, "--show-prefix")) {
318 319
				if (prefix)
					puts(prefix);
320 321
				continue;
			}
Junio C Hamano's avatar
Junio C Hamano committed
322 323 324 325 326 327 328 329 330 331 332 333
			if (!strcmp(arg, "--show-cdup")) {
				const char *pfx = prefix;
				while (pfx) {
					pfx = strchr(pfx, '/');
					if (pfx) {
						pfx++;
						printf("../");
					}
				}
				putchar('\n');
				continue;
			}
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
			if (!strcmp(arg, "--git-dir")) {
				const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
				static char cwd[PATH_MAX];
				if (gitdir) {
					puts(gitdir);
					continue;
				}
				if (!prefix) {
					puts(".git");
					continue;
				}
				if (!getcwd(cwd, PATH_MAX))
					die("unable to get current working directory");
				printf("%s/.git\n", cwd);
				continue;
			}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
			if (!strncmp(arg, "--since=", 8)) {
				show_datestring("--max-age=", arg+8);
				continue;
			}
			if (!strncmp(arg, "--after=", 8)) {
				show_datestring("--max-age=", arg+8);
				continue;
			}
			if (!strncmp(arg, "--before=", 9)) {
				show_datestring("--min-age=", arg+9);
				continue;
			}
			if (!strncmp(arg, "--until=", 8)) {
				show_datestring("--min-age=", arg+8);
				continue;
			}
366
			if (show_flag(arg) && verify)
367
				die("Needed a single revision");
368 369
			continue;
		}
370 371

		/* Not a flag argument */
372 373
		if (try_difference(arg))
			continue;
374
		if (!get_sha1(arg, sha1)) {
375
			show_rev(NORMAL, sha1, arg);
376 377
			continue;
		}
378
		if (*arg == '^' && !get_sha1(arg+1, sha1)) {
379
			show_rev(REVERSED, sha1, arg+1);
380 381
			continue;
		}
382 383 384
		as_is = 1;
		if (!show_file(arg))
			continue;
385 386
		if (verify)
			die("Needed a single revision");
387
		verify_filename(prefix, arg);
388 389
	}
	show_default();
390 391
	if (verify && revs_count != 1)
		die("Needed a single revision");
392 393
	return 0;
}