Skip to content

Commit 93d5a5f

Browse files
committed
Merge pull request #5 from wchristian/fix_ref_sha1/return_commit
fix for the retrieval of ref sha1s
2 parents 9b5d09e + e33b3f0 commit 93d5a5f

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

lib/Git/PurePerl.pm

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ sub ref_names {
155155
if ( -f $packed_refs ) {
156156
foreach my $line ( $packed_refs->slurp( chomp => 1 ) ) {
157157
next if $line =~ /^#/;
158+
next if $line =~ /^\^/;
158159
my ( $sha1, my $name ) = split ' ', $line;
159160
push @names, $name;
160161
}
@@ -174,7 +175,6 @@ sub refs {
174175

175176
sub ref_sha1 {
176177
my ( $self, $wantref ) = @_;
177-
my @refs;
178178
my $dir = dir( $self->gitdir, 'refs' );
179179
return unless -d $dir;
180180

@@ -183,8 +183,7 @@ sub ref_sha1 {
183183
my $sha1 = file($file)->slurp
184184
|| confess("Error reading $file: $!");
185185
chomp $sha1;
186-
return $self->ref_sha1($1) if $sha1 =~ /^ref: (.*)/;
187-
return $sha1;
186+
return _ensure_sha1_is_sha1( $self, $sha1 );
188187
}
189188

190189
foreach my $file ( File::Find::Rule->new->file->in($dir) ) {
@@ -193,25 +192,36 @@ sub ref_sha1 {
193192
my $sha1 = file($file)->slurp
194193
|| confess("Error reading $file: $!");
195194
chomp $sha1;
196-
return $self->ref_sha1($1) if $sha1 =~ /^ref: (.*)/;
197-
return $sha1;
195+
return _ensure_sha1_is_sha1( $self, $sha1 );
198196
}
199197
}
200198

201199
my $packed_refs = file( $self->gitdir, 'packed-refs' );
202200
if ( -f $packed_refs ) {
201+
my $last_name;
202+
my $last_sha1;
203203
foreach my $line ( $packed_refs->slurp( chomp => 1 ) ) {
204204
next if $line =~ /^#/;
205205
my ( $sha1, my $name ) = split ' ', $line;
206-
if ( $name eq $wantref ) {
207-
return $self->ref_sha1($1) if $sha1 =~ /^ref: (.*)/;
208-
return $sha1;
209-
}
206+
$sha1 =~ s/^\^//;
207+
$name ||= $last_name;
208+
209+
return _ensure_sha1_is_sha1( $self, $last_sha1 ) if $last_name and $last_name eq $wantref and $name ne $wantref;
210+
211+
$last_name = $name;
212+
$last_sha1 = $sha1;
210213
}
214+
return _ensure_sha1_is_sha1( $self, $last_sha1 ) if $last_name eq $wantref;
211215
}
212216
return undef;
213217
}
214218

219+
sub _ensure_sha1_is_sha1 {
220+
my ( $self, $sha1 ) = @_;
221+
return $self->ref_sha1($1) if $sha1 =~ /^ref: (.*)/;
222+
return $sha1;
223+
}
224+
215225
sub ref {
216226
my ( $self, $wantref ) = @_;
217227
return $self->get_object( $self->ref_sha1($wantref) );
@@ -556,4 +566,3 @@ This module is free software; you can redistribute it or
556566
modify it under the same terms as Perl itself.
557567
558568
=cut
559-

0 commit comments

Comments
 (0)