Commits

Anonymous committed 895f10c

Builtin git-rev-parse.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>

Comments (0)

Files changed (5)

 	git-hash-object$X git-index-pack$X git-local-fetch$X \
 	git-mailinfo$X git-merge-base$X \
 	git-merge-index$X git-mktag$X git-mktree$X git-pack-objects$X git-patch-id$X \
-	git-peek-remote$X git-prune-packed$X \
-	git-receive-pack$X git-rev-parse$X \
+	git-peek-remote$X git-prune-packed$X git-receive-pack$X \
 	git-send-pack$X git-shell$X \
 	git-show-index$X git-ssh-fetch$X \
 	git-ssh-upload$X git-unpack-file$X \
 BUILT_INS = git-log$X git-whatchanged$X git-show$X \
 	git-count-objects$X git-diff$X git-push$X \
 	git-grep$X git-add$X git-rm$X git-rev-list$X \
-	git-check-ref-format$X \
+	git-check-ref-format$X git-rev-parse$X \
 	git-init-db$X git-tar-tree$X git-upload-tar$X git-format-patch$X \
 	git-ls-files$X git-ls-tree$X \
 	git-read-tree$X git-commit-tree$X \
 BUILTIN_OBJS = \
 	builtin-log.o builtin-help.o builtin-count.o builtin-diff.o builtin-push.o \
 	builtin-grep.o builtin-add.o builtin-rev-list.o builtin-check-ref-format.o \
-	builtin-rm.o builtin-init-db.o \
+	builtin-rm.o builtin-init-db.o builtin-rev-parse.o \
 	builtin-tar-tree.o builtin-upload-tar.o \
 	builtin-ls-files.o builtin-ls-tree.o \
 	builtin-read-tree.o builtin-commit-tree.o \

builtin-rev-parse.c

+/*
+ * rev-parse.c
+ *
+ * Copyright (C) Linus Torvalds, 2005
+ */
+#include "cache.h"
+#include "commit.h"
+#include "refs.h"
+#include "quote.h"
+#include "builtin.h"
+
+#define DO_REVS		1
+#define DO_NOREV	2
+#define DO_FLAGS	4
+#define DO_NONFLAGS	8
+static int filter = ~0;
+
+static char *def = NULL;
+
+#define NORMAL 0
+#define REVERSED 1
+static int show_type = NORMAL;
+static int symbolic = 0;
+static int abbrev = 0;
+static int output_sq = 0;
+
+static int revs_count = 0;
+
+/*
+ * 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[] = {
+		"--all",
+		"--bisect",
+		"--dense",
+		"--branches",
+		"--header",
+		"--max-age=",
+		"--max-count=",
+		"--min-age=",
+		"--no-merges",
+		"--objects",
+		"--objects-edge",
+		"--parents",
+		"--pretty",
+		"--remotes",
+		"--sparse",
+		"--tags",
+		"--topo-order",
+		"--date-order",
+		"--unpacked",
+		NULL
+	};
+	const char **p = rev_args;
+
+	/* accept -<digit>, like traditional "head" */
+	if ((*arg == '-') && isdigit(arg[1]))
+		return 1;
+
+	for (;;) {
+		const char *str = *p++;
+		int len;
+		if (!str)
+			return 0;
+		len = strlen(str);
+		if (!strcmp(arg, str) ||
+		    (str[len-1] == '=' && !strncmp(arg, str, len)))
+			return 1;
+	}
+}
+
+/* Output argument as a string, either SQ or normal */
+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);
+}
+
+/* Output a revision, only if filter allows it */
+static void show_rev(int type, const unsigned char *sha1, const char *name)
+{
+	if (!(filter & DO_REVS))
+		return;
+	def = NULL;
+	revs_count++;
+
+	if (type != show_type)
+		putchar('^');
+	if (symbolic && name)
+		show(name);
+	else if (abbrev)
+		show(find_unique_abbrev(sha1, abbrev));
+	else
+		show(sha1_to_hex(sha1));
+}
+
+/* Output a flag, only if filter allows it. */
+static int show_flag(char *arg)
+{
+	if (!(filter & DO_FLAGS))
+		return 0;
+	if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
+		show(arg);
+		return 1;
+	}
+	return 0;
+}
+
+static void show_default(void)
+{
+	char *s = def;
+
+	if (s) {
+		unsigned char sha1[20];
+
+		def = NULL;
+		if (!get_sha1(s, sha1)) {
+			show_rev(NORMAL, sha1, s);
+			return;
+		}
+	}
+}
+
+static int show_reference(const char *refname, const unsigned char *sha1)
+{
+	show_rev(NORMAL, sha1, refname);
+	return 0;
+}
+
+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;
+	snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
+	show(buffer);
+}
+
+static int show_file(const char *arg)
+{
+	show_default();
+	if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
+		show(arg);
+		return 1;
+	}
+	return 0;
+}
+
+int cmd_rev_parse(int argc, const char **argv, char **envp)
+{
+	int i, as_is = 0, verify = 0;
+	unsigned char sha1[20];
+	const char *prefix = setup_git_directory();
+
+	git_config(git_default_config);
+
+	for (i = 1; i < argc; i++) {
+		char *arg = argv[i];
+		char *dotdot;
+
+		if (as_is) {
+			if (show_file(arg) && as_is < 2)
+				verify_filename(prefix, arg);
+			continue;
+		}
+		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;
+		}
+
+		if (*arg == '-') {
+			if (!strcmp(arg, "--")) {
+				as_is = 2;
+				/* Pass on the "--" if we show anything but files.. */
+				if (filter & (DO_FLAGS | DO_REVS))
+					show_file(arg);
+				continue;
+			}
+			if (!strcmp(arg, "--default")) {
+				def = argv[i+1];
+				i++;
+				continue;
+			}
+			if (!strcmp(arg, "--revs-only")) {
+				filter &= ~DO_NOREV;
+				continue;
+			}
+			if (!strcmp(arg, "--no-revs")) {
+				filter &= ~DO_REVS;
+				continue;
+			}
+			if (!strcmp(arg, "--flags")) {
+				filter &= ~DO_NONFLAGS;
+				continue;
+			}
+			if (!strcmp(arg, "--no-flags")) {
+				filter &= ~DO_FLAGS;
+				continue;
+			}
+			if (!strcmp(arg, "--verify")) {
+				filter &= ~(DO_FLAGS|DO_NOREV);
+				verify = 1;
+				continue;
+			}
+			if (!strcmp(arg, "--short") ||
+			    !strncmp(arg, "--short=", 8)) {
+				filter &= ~(DO_FLAGS|DO_NOREV);
+				verify = 1;
+				abbrev = DEFAULT_ABBREV;
+				if (arg[7] == '=')
+					abbrev = strtoul(arg + 8, NULL, 10);
+				if (abbrev < MINIMUM_ABBREV)
+					abbrev = MINIMUM_ABBREV;
+				else if (40 <= abbrev)
+					abbrev = 40;
+				continue;
+			}
+			if (!strcmp(arg, "--sq")) {
+				output_sq = 1;
+				continue;
+			}
+			if (!strcmp(arg, "--not")) {
+				show_type ^= REVERSED;
+				continue;
+			}
+			if (!strcmp(arg, "--symbolic")) {
+				symbolic = 1;
+				continue;
+			}
+			if (!strcmp(arg, "--all")) {
+				for_each_ref(show_reference);
+				continue;
+			}
+			if (!strcmp(arg, "--branches")) {
+				for_each_branch_ref(show_reference);
+				continue;
+			}
+			if (!strcmp(arg, "--tags")) {
+				for_each_tag_ref(show_reference);
+				continue;
+			}
+			if (!strcmp(arg, "--remotes")) {
+				for_each_remote_ref(show_reference);
+				continue;
+			}
+			if (!strcmp(arg, "--show-prefix")) {
+				if (prefix)
+					puts(prefix);
+				continue;
+			}
+			if (!strcmp(arg, "--show-cdup")) {
+				const char *pfx = prefix;
+				while (pfx) {
+					pfx = strchr(pfx, '/');
+					if (pfx) {
+						pfx++;
+						printf("../");
+					}
+				}
+				putchar('\n');
+				continue;
+			}
+			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;
+			}
+			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;
+			}
+			if (show_flag(arg) && verify)
+				die("Needed a single revision");
+			continue;
+		}
+
+		/* Not a flag argument */
+		dotdot = strstr(arg, "..");
+		if (dotdot) {
+			unsigned char end[20];
+			char *next = dotdot + 2;
+			char *this = arg;
+			*dotdot = 0;
+			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(REVERSED, sha1, this);
+				continue;
+			}
+			*dotdot = '.';
+		}
+		if (!get_sha1(arg, sha1)) {
+			show_rev(NORMAL, sha1, arg);
+			continue;
+		}
+		if (*arg == '^' && !get_sha1(arg+1, sha1)) {
+			show_rev(REVERSED, sha1, arg+1);
+			continue;
+		}
+		as_is = 1;
+		if (!show_file(arg))
+			continue;
+		if (verify)
+			die("Needed a single revision");
+		verify_filename(prefix, arg);
+	}
+	show_default();
+	if (verify && revs_count != 1)
+		die("Needed a single revision");
+	return 0;
+}
 extern int cmd_diff_stages(int argc, const char **argv, char **envp);
 extern int cmd_diff_tree(int argc, const char **argv, char **envp);
 extern int cmd_cat_file(int argc, const char **argv, char **envp);
+extern int cmd_rev_parse(int argc, const char **argv, char **envp);
 
 #endif
 		{ "diff-index", cmd_diff_index },
 		{ "diff-stages", cmd_diff_stages },
 		{ "diff-tree", cmd_diff_tree },
-		{ "cat-file", cmd_cat_file }
+		{ "cat-file", cmd_cat_file },
+		{ "rev-parse", cmd_rev_parse }
 	};
 	int i;
 

rev-parse.c

-/*
- * rev-parse.c
- *
- * Copyright (C) Linus Torvalds, 2005
- */
-#include "cache.h"
-#include "commit.h"
-#include "refs.h"
-#include "quote.h"
-
-#define DO_REVS		1
-#define DO_NOREV	2
-#define DO_FLAGS	4
-#define DO_NONFLAGS	8
-static int filter = ~0;
-
-static char *def = NULL;
-
-#define NORMAL 0
-#define REVERSED 1
-static int show_type = NORMAL;
-static int symbolic = 0;
-static int abbrev = 0;
-static int output_sq = 0;
-
-static int revs_count = 0;
-
-/*
- * 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[] = {
-		"--all",
-		"--bisect",
-		"--dense",
-		"--branches",
-		"--header",
-		"--max-age=",
-		"--max-count=",
-		"--min-age=",
-		"--no-merges",
-		"--objects",
-		"--objects-edge",
-		"--parents",
-		"--pretty",
-		"--remotes",
-		"--sparse",
-		"--tags",
-		"--topo-order",
-		"--date-order",
-		"--unpacked",
-		NULL
-	};
-	const char **p = rev_args;
-
-	/* accept -<digit>, like traditional "head" */
-	if ((*arg == '-') && isdigit(arg[1]))
-		return 1;
-
-	for (;;) {
-		const char *str = *p++;
-		int len;
-		if (!str)
-			return 0;
-		len = strlen(str);
-		if (!strcmp(arg, str) ||
-		    (str[len-1] == '=' && !strncmp(arg, str, len)))
-			return 1;
-	}
-}
-
-/* Output argument as a string, either SQ or normal */
-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);
-}
-
-/* Output a revision, only if filter allows it */
-static void show_rev(int type, const unsigned char *sha1, const char *name)
-{
-	if (!(filter & DO_REVS))
-		return;
-	def = NULL;
-	revs_count++;
-
-	if (type != show_type)
-		putchar('^');
-	if (symbolic && name)
-		show(name);
-	else if (abbrev)
-		show(find_unique_abbrev(sha1, abbrev));
-	else
-		show(sha1_to_hex(sha1));
-}
-
-/* Output a flag, only if filter allows it. */
-static int show_flag(char *arg)
-{
-	if (!(filter & DO_FLAGS))
-		return 0;
-	if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
-		show(arg);
-		return 1;
-	}
-	return 0;
-}
-
-static void show_default(void)
-{
-	char *s = def;
-
-	if (s) {
-		unsigned char sha1[20];
-
-		def = NULL;
-		if (!get_sha1(s, sha1)) {
-			show_rev(NORMAL, sha1, s);
-			return;
-		}
-	}
-}
-
-static int show_reference(const char *refname, const unsigned char *sha1)
-{
-	show_rev(NORMAL, sha1, refname);
-	return 0;
-}
-
-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;
-	snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
-	show(buffer);
-}
-
-static int show_file(const char *arg)
-{
-	show_default();
-	if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
-		show(arg);
-		return 1;
-	}
-	return 0;
-}
-
-int main(int argc, char **argv)
-{
-	int i, as_is = 0, verify = 0;
-	unsigned char sha1[20];
-	const char *prefix = setup_git_directory();
-
-	git_config(git_default_config);
-
-	for (i = 1; i < argc; i++) {
-		char *arg = argv[i];
-		char *dotdot;
-
-		if (as_is) {
-			if (show_file(arg) && as_is < 2)
-				verify_filename(prefix, arg);
-			continue;
-		}
-		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;
-		}
-
-		if (*arg == '-') {
-			if (!strcmp(arg, "--")) {
-				as_is = 2;
-				/* Pass on the "--" if we show anything but files.. */
-				if (filter & (DO_FLAGS | DO_REVS))
-					show_file(arg);
-				continue;
-			}
-			if (!strcmp(arg, "--default")) {
-				def = argv[i+1];
-				i++;
-				continue;
-			}
-			if (!strcmp(arg, "--revs-only")) {
-				filter &= ~DO_NOREV;
-				continue;
-			}
-			if (!strcmp(arg, "--no-revs")) {
-				filter &= ~DO_REVS;
-				continue;
-			}
-			if (!strcmp(arg, "--flags")) {
-				filter &= ~DO_NONFLAGS;
-				continue;
-			}
-			if (!strcmp(arg, "--no-flags")) {
-				filter &= ~DO_FLAGS;
-				continue;
-			}
-			if (!strcmp(arg, "--verify")) {
-				filter &= ~(DO_FLAGS|DO_NOREV);
-				verify = 1;
-				continue;
-			}
-			if (!strcmp(arg, "--short") ||
-			    !strncmp(arg, "--short=", 8)) {
-				filter &= ~(DO_FLAGS|DO_NOREV);
-				verify = 1;
-				abbrev = DEFAULT_ABBREV;
-				if (arg[7] == '=')
-					abbrev = strtoul(arg + 8, NULL, 10);
-				if (abbrev < MINIMUM_ABBREV)
-					abbrev = MINIMUM_ABBREV;
-				else if (40 <= abbrev)
-					abbrev = 40;
-				continue;
-			}
-			if (!strcmp(arg, "--sq")) {
-				output_sq = 1;
-				continue;
-			}
-			if (!strcmp(arg, "--not")) {
-				show_type ^= REVERSED;
-				continue;
-			}
-			if (!strcmp(arg, "--symbolic")) {
-				symbolic = 1;
-				continue;
-			}
-			if (!strcmp(arg, "--all")) {
-				for_each_ref(show_reference);
-				continue;
-			}
-			if (!strcmp(arg, "--branches")) {
-				for_each_branch_ref(show_reference);
-				continue;
-			}
-			if (!strcmp(arg, "--tags")) {
-				for_each_tag_ref(show_reference);
-				continue;
-			}
-			if (!strcmp(arg, "--remotes")) {
-				for_each_remote_ref(show_reference);
-				continue;
-			}
-			if (!strcmp(arg, "--show-prefix")) {
-				if (prefix)
-					puts(prefix);
-				continue;
-			}
-			if (!strcmp(arg, "--show-cdup")) {
-				const char *pfx = prefix;
-				while (pfx) {
-					pfx = strchr(pfx, '/');
-					if (pfx) {
-						pfx++;
-						printf("../");
-					}
-				}
-				putchar('\n');
-				continue;
-			}
-			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;
-			}
-			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;
-			}
-			if (show_flag(arg) && verify)
-				die("Needed a single revision");
-			continue;
-		}
-
-		/* Not a flag argument */
-		dotdot = strstr(arg, "..");
-		if (dotdot) {
-			unsigned char end[20];
-			char *next = dotdot + 2;
-			char *this = arg;
-			*dotdot = 0;
-			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(REVERSED, sha1, this);
-				continue;
-			}
-			*dotdot = '.';
-		}
-		if (!get_sha1(arg, sha1)) {
-			show_rev(NORMAL, sha1, arg);
-			continue;
-		}
-		if (*arg == '^' && !get_sha1(arg+1, sha1)) {
-			show_rev(REVERSED, sha1, arg+1);
-			continue;
-		}
-		as_is = 1;
-		if (!show_file(arg))
-			continue;
-		if (verify)
-			die("Needed a single revision");
-		verify_filename(prefix, arg);
-	}
-	show_default();
-	if (verify && revs_count != 1)
-		die("Needed a single revision");
-	return 0;
-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.