From: Lea Wiemann <lewiemann@gmail.com>
To: git@vger.kernel.org
Cc: Lea Wiemann <LeWiemann@gmail.com>
Subject: [PATCH v4] perl/Git.pm: add get_hash method
Date: Mon, 2 Jun 2008 01:24:52 +0200 [thread overview]
Message-ID: <1212362693-22084-1-git-send-email-LeWiemann@gmail.com> (raw)
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
prev parent reply other threads:[~2008-06-01 23:25 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-30 4:43 [PATCH] perl/Git.pm: add rev_parse method Lea Wiemann
2008-05-30 7:03 ` Lea Wiemann
2008-05-30 9:59 ` Petr Baudis
2008-05-30 15:15 ` Merging strategy for extending Git.pm (was: [PATCH] perl/Git.pm: add rev_parse method) Lea Wiemann
2008-05-30 23:20 ` Merging strategy for extending Git.pm Junio C Hamano
2008-05-31 11:38 ` Merging strategy for extending Git.pm (was: [PATCH] perl/Git.pm: add rev_parse method) Johannes Schindelin
2008-05-31 12:42 ` Merging strategy for extending Git.pm Lea Wiemann
2008-05-31 12:52 ` Johannes Schindelin
2008-05-30 20:28 ` [PATCH] perl/Git.pm: add rev_parse method Junio C Hamano
2008-05-30 9:50 ` Petr Baudis
2008-05-30 20:27 ` [PATCH] perl/Git.pm: add parse_rev method Lea Wiemann
2008-05-30 21:05 ` Petr Baudis
2008-05-30 21:25 ` Junio C Hamano
2008-05-30 21:44 ` Randal L. Schwartz
2008-05-30 21:59 ` Lea Wiemann
2008-05-30 22:03 ` Randal L. Schwartz
2008-05-30 22:05 ` Lea Wiemann
2008-05-30 22:19 ` Junio C Hamano
2008-05-31 11:50 ` Johannes Schindelin
2008-05-31 12:17 ` Support for old Perl versions Petr Baudis
2008-05-31 12:32 ` Johannes Schindelin
2008-05-30 21:49 ` [PATCH] perl/Git.pm: add parse_rev method Petr Baudis
2008-05-31 13:52 ` [PATCH v2] " Lea Wiemann
2008-06-01 3:17 ` [PATCH v3] " Lea Wiemann
2008-06-01 3:17 ` Lea Wiemann
2008-06-01 17:38 ` Lea Wiemann
2008-06-01 21:54 ` Miklos Vajna
2008-06-01 22:51 ` Lea Wiemann
2008-06-02 4:59 ` Junio C Hamano
2008-06-02 13:51 ` Lea Wiemann
2008-06-01 23:09 ` [PATCH v4] perl/Git.pm: add get_hash method Lea Wiemann
2008-06-01 23:24 ` Lea Wiemann [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1212362693-22084-1-git-send-email-LeWiemann@gmail.com \
--to=lewiemann@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).