git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4] perl/Git.pm: add get_hash method

From
LWLea Wiemann <lewiemann@gmail.com>
Date
Jun 1, 2008, 23:24 UTC
Message-ID
<1212362693-22084-1-git-send-email-LeWiemann@gmail.com>
In-Reply-To
<1212361784-20409-1-git-send-email-LeWiemann@gmail.com>

The get_hash method takes a "revision" (as described in git-rev-parse --help) and returns the SHA1 hash of the commit, tree, file, or tag object it refers to.

Signed-off-by: Lea Wiemann <LeWiemann@gmail.com>
---
Changes since v3: Renamed parse_rev to get_hash, improved
documentation to point out that the parameter can refer to file, tree
and tag object as well, and added example in "Synopsis" section.
 perl/Git.pm |   45 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 97e61ef..f2e9e29 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -43,6 +43,9 @@ $VERSION = '0.01';
   my $tempfile = tempfile();
   my $size = $repo->cat_blob($sha1, $tempfile);
 
+  my $head_commit_sha1 = $repo->get_hash('HEAD');  # see git-rev-parse --help
+  my $file_blob_sha1   = $repo->get_hash('HEAD:path/file.txt');
+
 =cut
 
 
@@ -716,6 +719,48 @@ sub ident_person {
 	return "$ident[0] <$ident[1]>";
 }
 
+=item get_hash ( REVISION )
+
+Look up the object referred to by C<REVISION> and return its SHA1
+hash, or return undef if the lookup failed.  When passed a SHA1 hash,
+always return it even if it doesn't exist in the repository.
+
+Note that C<REVISION> can refer to a commit, file, tree, or tag
+object!  See git rev-parse --help, section "Specifying Revisions", for
+valid formats of the C<REVISION> parameter.
+
+=cut
+
+sub get_hash {
+	# 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(
+		"get_hash: 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
Previous: Lea Wiemann
Message 32 of 32 in “perl/Git.pm: add rev_parse method”
  1. perl/Git.pm: add rev_parse methodLea Wiemann, May 30, 2008
  2. Lea WiemannMay 30, 2008
  3. Petr BaudisMay 30, 2008
  4. Merging strategy for extending Git.pm (was: [PATCH] perl/Git.pm: add rev_parse method)Lea Wiemann, May 30, 2008
  5. Junio C HamanoMay 30, 2008
  6. Johannes SchindelinMay 31, 2008
  7. Lea WiemannMay 31, 2008
  8. Johannes SchindelinMay 31, 2008
  9. Junio C HamanoMay 30, 2008
  10. Petr BaudisMay 30, 2008
  11. perl/Git.pm: add parse_rev methodLea Wiemann, May 30, 2008
  12. Petr BaudisMay 30, 2008
  13. Junio C HamanoMay 30, 2008
  14. Randal L. SchwartzMay 30, 2008
  15. Lea WiemannMay 30, 2008
  16. Randal L. SchwartzMay 30, 2008
  17. Lea WiemannMay 30, 2008
  18. Junio C HamanoMay 30, 2008
  19. Johannes SchindelinMay 31, 2008
  20. Support for old Perl versionsPetr Baudis, May 31, 2008
  21. Johannes SchindelinMay 31, 2008
  22. Petr BaudisMay 30, 2008
  23. perl/Git.pm: add parse_rev methodLea Wiemann, May 31, 2008
  24. perl/Git.pm: add parse_rev methodLea Wiemann, Jun 1, 2008
  25. perl/Git.pm: add parse_rev methodLea Wiemann, Jun 1, 2008
  26. Lea WiemannJun 1, 2008
  27. Miklos VajnaJun 1, 2008
  28. Lea WiemannJun 1, 2008
  29. Junio C HamanoJun 2, 2008
  30. Lea WiemannJun 2, 2008
  31. perl/Git.pm: add get_hash methodLea Wiemann, Jun 1, 2008
  32. perl/Git.pm: add get_hash methodLea Wiemann, Jun 1, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.