From mboxrd@z Thu Jan 1 00:00:00 1970 From: Lea Wiemann Subject: [PATCH v3] perl/Git.pm: add parse_rev method Date: Sun, 1 Jun 2008 05:17:23 +0200 Message-ID: <1212290243-19393-2-git-send-email-LeWiemann@gmail.com> References: <1212241932-28605-1-git-send-email-LeWiemann@gmail.com> <1212290243-19393-1-git-send-email-LeWiemann@gmail.com> Cc: Lea Wiemann To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Sun Jun 01 05:18:04 2008 Return-path: Envelope-to: gcvg-git-2@gmane.org Received: from vger.kernel.org ([209.132.176.167]) by lo.gmane.org with esmtp (Exim 4.50) id 1K2e55-000593-VY for gcvg-git-2@gmane.org; Sun, 01 Jun 2008 05:18:04 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755250AbYFADRL (ORCPT ); Sat, 31 May 2008 23:17:11 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1755288AbYFADRL (ORCPT ); Sat, 31 May 2008 23:17:11 -0400 Received: from fg-out-1718.google.com ([72.14.220.156]:37360 "EHLO fg-out-1718.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754225AbYFADRJ (ORCPT ); Sat, 31 May 2008 23:17:09 -0400 Received: by fg-out-1718.google.com with SMTP id 19so426593fgg.17 for ; Sat, 31 May 2008 20:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:received:to:cc:subject:date:message-id:x-mailer:in-reply-to:references:from; bh=PlDqEFxZ4zi1lcyARTqgNwrWaLi3PVIEVmZzveec8i4=; b=dDMa1wO/fgDZjnDYG7zZvY6GIq4u3VJDWnwkl5pJj4w471IjyXhXD92uK000/fKF+D9yvoqih6DLfNNSrvZEkz4Ukam4VFORVwU1neqwuBROglETe1b8CRRJF32HgsYAqDpTa68iw4BNCHBLVFP033RzWvx67O7hXIiXyPXUdK0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=to:cc:subject:date:message-id:x-mailer:in-reply-to:references:from; b=mXqDdJ5AjGHhbH90Mw8SYmImKzcgg6xb9bwgo8SwEZvdRQWUvj6k5XPENQka2c/M90AWsq7rpWan7TQC31V0LxTy7pcfGmOE0iJzf1baWljVuldLKJPjWPPPTH0nj6My2pn1XfbZFMZMBdvkYyf6J3eTwgIZWGm+wTehsuSmTLM= Received: by 10.86.49.3 with SMTP id w3mr3299441fgw.4.1212290224335; Sat, 31 May 2008 20:17:04 -0700 (PDT) Received: from fly ( [91.33.240.119]) by mx.google.com with ESMTPS id d13sm6189216fka.10.2008.05.31.20.17.02 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 31 May 2008 20:17:03 -0700 (PDT) Received: from lea by fly with local (Exim 4.69) (envelope-from ) id 1K2e4R-00054g-HJ; Sun, 01 Jun 2008 05:17:23 +0200 X-Mailer: git-send-email 1.5.5.GIT In-Reply-To: <1212290243-19393-1-git-send-email-LeWiemann@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: The parse_rev method takes a revision name and returns a SHA1 hash, like the git-rev-parse command. Signed-off-by: Lea Wiemann --- Changes since PATCH v2: Updated the documentation (found this while testing). Here's the diff excerpt from v2 to v3: =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". +return undef if the lookup failed. When passed a SHA1 hash, always +return it even if it doesn't exist in the repository. + +See git rev-parse --help, section "Specifying Revisions", for valid +revision name formats. perl/Git.pm | 41 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 deletions(-) diff --git a/perl/Git.pm b/perl/Git.pm index d05b633..80f7669 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -716,6 +716,47 @@ 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. When passed a SHA1 hash, always +return it even if it doesn't exist in the repository. + +See git rev-parse --help, section "Specifying Revisions", for valid +revision name formats. + +=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