[PATCH v2] perl/Git.pm: add parse_rev method

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Lea Wiemann
Date: Saturday, May 31, 2008 - 6:52 am

The parse_rev method takes a revision name and returns a SHA1 hash,
like the git-rev-parse command.

Signed-off-by: Lea Wiemann <LeWiemann@gmail.com>
---
Use tabs instead of blanks to match the rest of the file.  No changes
apart from that.

 perl/Git.pm |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)

diff --git a/perl/Git.pm b/perl/Git.pm
index d05b633..249daad 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -716,6 +716,44 @@ sub ident_person {
 	return "$ident[0] <$ident[1]>";
 }
 
+=item parse_rev ( REVISION_NAME )
+
+Look up the specified revision name and return the SHA1 hash, or
+return undef if the lookup failed.  See git rev-parse --help, section
+"Specifying Revisions".
+
+=cut
+
+sub parse_rev {
+	# We could allow for a list of revisions here.
+	my ($self, $rev_name) = @_;
+
+	my $hash;
+	try {
+		# The --quiet --verify options cause git-rev-parse to fail
+		# with exit status 1 (instead of 128) if the given revision
+		# name is not found, which enables us to distinguish not-found
+		# from serious errors.  The --default option works around
+		# git-rev-parse's lack of support for getopt style "--"
+		# separators (it would fail for tags named "--foo" without
+		# it).
+		$hash = $self->command_oneline("rev-parse", "--verify", "--quiet",
+					       "--default", $rev_name);
+	} catch Git::Error::Command with {
+		my $E = shift;
+		if ($E->value() == 1) {
+			# Revision name not found.
+			$hash = undef;
+		} else {
+			throw $E;
+		}
+	};
+	# Guard against unexpected output.
+	throw Error::Simple(
+		"parse_rev: unexpected output for \"$rev_name\": $hash")
+	    if defined $hash and $hash !~ /^([0-9a-fA-F]{40})$/;
+	return $hash;
+}
 
 =item hash_object ( TYPE, FILENAME )
 
-- 
1.5.5.GIT

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH] perl/Git.pm: add rev_parse method, Lea Wiemann, (Thu May 29, 9:43 pm)
Re: [PATCH] perl/Git.pm: add rev_parse method, Lea Wiemann, (Fri May 30, 12:03 am)
Re: [PATCH] perl/Git.pm: add rev_parse method, Petr Baudis, (Fri May 30, 2:50 am)
Re: [PATCH] perl/Git.pm: add rev_parse method, Petr Baudis, (Fri May 30, 2:59 am)
[PATCH] perl/Git.pm: add parse_rev method, Lea Wiemann, (Fri May 30, 1:27 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Petr Baudis, (Fri May 30, 2:05 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Junio C Hamano, (Fri May 30, 2:25 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Randal L. Schwartz, (Fri May 30, 2:44 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Petr Baudis, (Fri May 30, 2:49 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Lea Wiemann, (Fri May 30, 2:59 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Randal L. Schwartz, (Fri May 30, 3:03 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Lea Wiemann, (Fri May 30, 3:05 pm)
Re: [PATCH] perl/Git.pm: add parse_rev method, Junio C Hamano, (Fri May 30, 3:19 pm)
Re: Merging strategy for extending Git.pm, Junio C Hamano, (Fri May 30, 4:20 pm)
Re: Merging strategy for extending Git.pm (was: [PATCH] pe ..., Johannes Schindelin, (Sat May 31, 4:38 am)
Re: [PATCH] perl/Git.pm: add parse_rev method, Johannes Schindelin, (Sat May 31, 4:50 am)
Support for old Perl versions, Petr Baudis, (Sat May 31, 5:17 am)
Re: Support for old Perl versions, Johannes Schindelin, (Sat May 31, 5:32 am)
Re: Merging strategy for extending Git.pm, Lea Wiemann, (Sat May 31, 5:42 am)
Re: Merging strategy for extending Git.pm, Johannes Schindelin, (Sat May 31, 5:52 am)
[PATCH v2] perl/Git.pm: add parse_rev method, Lea Wiemann, (Sat May 31, 6:52 am)
[PATCH v3] perl/Git.pm: add parse_rev method, Lea Wiemann, (Sat May 31, 8:17 pm)
[PATCH v3] perl/Git.pm: add parse_rev method, Lea Wiemann, (Sat May 31, 8:17 pm)
Re: [PATCH v3] perl/Git.pm: add parse_rev method, Lea Wiemann, (Sun Jun 1, 10:38 am)
Re: [PATCH v3] perl/Git.pm: add parse_rev method, Miklos Vajna, (Sun Jun 1, 2:54 pm)
Re: [PATCH v3] perl/Git.pm: add parse_rev method, Lea Wiemann, (Sun Jun 1, 3:51 pm)
[PATCH v4] perl/Git.pm: add get_hash method, Lea Wiemann, (Sun Jun 1, 4:09 pm)
[PATCH v4] perl/Git.pm: add get_hash method, Lea Wiemann, (Sun Jun 1, 4:24 pm)
Re: [PATCH v3] perl/Git.pm: add parse_rev method, Junio C Hamano, (Sun Jun 1, 9:59 pm)
Re: [PATCH v3] perl/Git.pm: add parse_rev method, Lea Wiemann, (Mon Jun 2, 6:51 am)