/usr/share/perl5/Git/PurePerl.pm is in libgit-pureperl-perl 0.50-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | package Git::PurePerl;
use Moose;
use MooseX::StrictConstructor;
use MooseX::Types::Path::Class;
use Compress::Zlib qw(uncompress);
use Data::Stream::Bulk;
use Data::Stream::Bulk::Array;
use Data::Stream::Bulk::Path::Class;
use DateTime;
use Digest::SHA;
use File::Find::Rule;
use Git::PurePerl::Actor;
use Git::PurePerl::Config;
use Git::PurePerl::DirectoryEntry;
use Git::PurePerl::Loose;
use Git::PurePerl::Object;
use Git::PurePerl::NewDirectoryEntry;
use Git::PurePerl::NewObject;
use Git::PurePerl::NewObject::Blob;
use Git::PurePerl::NewObject::Commit;
use Git::PurePerl::NewObject::Tag;
use Git::PurePerl::NewObject::Tree;
use Git::PurePerl::Object::Tree;
use Git::PurePerl::Object::Blob;
use Git::PurePerl::Object::Commit;
use Git::PurePerl::Object::Tag;
use Git::PurePerl::Object::Tree;
use Git::PurePerl::Pack;
use Git::PurePerl::Pack::WithIndex;
use Git::PurePerl::Pack::WithoutIndex;
use Git::PurePerl::PackIndex;
use Git::PurePerl::PackIndex::Version1;
use Git::PurePerl::PackIndex::Version2;
use Git::PurePerl::Protocol;
use IO::Digest;
use IO::Socket::INET;
use Path::Class;
use namespace::autoclean;
our $VERSION = '0.50';
$VERSION = eval $VERSION;
has 'directory' => (
is => 'ro',
isa => 'Path::Class::Dir',
required => 0,
coerce => 1
);
has 'gitdir' => (
is => 'ro',
isa => 'Path::Class::Dir',
required => 1,
coerce => 1
);
has 'loose' => (
is => 'rw',
isa => 'Git::PurePerl::Loose',
required => 0,
lazy_build => 1,
);
has 'packs' => (
is => 'rw',
isa => 'ArrayRef[Git::PurePerl::Pack]',
required => 0,
auto_deref => 1,
lazy_build => 1,
);
has 'description' => (
is => 'rw',
isa => 'Str',
lazy => 1,
default => sub {
my $self = shift;
file( $self->gitdir, 'description' )->slurp( chomp => 1 );
}
);
has 'config' => (
is => 'ro',
isa => 'Git::PurePerl::Config',
lazy => 1,
default => sub {
my $self = shift;
Git::PurePerl::Config->new(git => $self);
}
);
__PACKAGE__->meta->make_immutable;
sub BUILDARGS {
my $class = shift;
my $params = $class->SUPER::BUILDARGS(@_);
$params->{'gitdir'} ||= dir( $params->{'directory'}, '.git' );
return $params;
}
sub BUILD {
my $self = shift;
unless ( -d $self->gitdir ) {
confess $self->gitdir . ' is not a directory';
}
unless ( not defined $self->directory or -d $self->directory ) {
confess $self->directory . ' is not a directory';
}
}
sub _build_loose {
my $self = shift;
my $loose_dir = dir( $self->gitdir, 'objects' );
return Git::PurePerl::Loose->new( directory => $loose_dir );
}
sub _build_packs {
my $self = shift;
my $pack_dir = dir( $self->gitdir, 'objects', 'pack' );
my @packs;
foreach my $filename ( $pack_dir->children ) {
next unless $filename =~ /\.pack$/;
push @packs,
Git::PurePerl::Pack::WithIndex->new( filename => $filename );
}
return \@packs;
}
sub _ref_names_recursive {
my ( $dir, $base, $names ) = @_;
foreach my $file ( $dir->children ) {
if ( -d $file ) {
my $reldir = $file->relative($dir);
my $subbase = $base . $reldir . "/";
_ref_names_recursive( $file, $subbase, $names );
} else {
push @$names, $base . $file->basename;
}
}
}
sub ref_names {
my $self = shift;
my @names;
foreach my $type (qw(heads remotes tags)) {
my $dir = dir( $self->gitdir, 'refs', $type );
next unless -d $dir;
my $base = "refs/$type/";
_ref_names_recursive( $dir, $base, \@names );
}
my $packed_refs = file( $self->gitdir, 'packed-refs' );
if ( -f $packed_refs ) {
foreach my $line ( $packed_refs->slurp( chomp => 1 ) ) {
next if $line =~ /^#/;
next if $line =~ /^\^/;
my ( $sha1, my $name ) = split ' ', $line;
push @names, $name;
}
}
return @names;
}
sub refs_sha1 {
my $self = shift;
return map { $self->ref_sha1($_) } $self->ref_names;
}
sub refs {
my $self = shift;
return map { $self->ref($_) } $self->ref_names;
}
sub ref_sha1 {
my ( $self, $wantref ) = @_;
my $dir = dir( $self->gitdir, 'refs' );
return unless -d $dir;
if ($wantref eq "HEAD") {
my $file = file($self->gitdir, 'HEAD');
my $sha1 = file($file)->slurp
|| confess("Error reading $file: $!");
chomp $sha1;
return _ensure_sha1_is_sha1( $self, $sha1 );
}
foreach my $file ( File::Find::Rule->new->file->in($dir) ) {
my $ref = 'refs/' . file($file)->relative($dir)->as_foreign('Unix');
if ( $ref eq $wantref ) {
my $sha1 = file($file)->slurp
|| confess("Error reading $file: $!");
chomp $sha1;
return _ensure_sha1_is_sha1( $self, $sha1 );
}
}
my $packed_refs = file( $self->gitdir, 'packed-refs' );
if ( -f $packed_refs ) {
my $last_name;
my $last_sha1;
foreach my $line ( $packed_refs->slurp( chomp => 1 ) ) {
next if $line =~ /^#/;
my ( $sha1, my $name ) = split ' ', $line;
$sha1 =~ s/^\^//;
$name ||= $last_name;
return _ensure_sha1_is_sha1( $self, $last_sha1 ) if $last_name and $last_name eq $wantref and $name ne $wantref;
$last_name = $name;
$last_sha1 = $sha1;
}
return _ensure_sha1_is_sha1( $self, $last_sha1 ) if $last_name eq $wantref;
}
return undef;
}
sub _ensure_sha1_is_sha1 {
my ( $self, $sha1 ) = @_;
return $self->ref_sha1($1) if $sha1 =~ /^ref: (.*)/;
return $sha1;
}
sub ref {
my ( $self, $wantref ) = @_;
return $self->get_object( $self->ref_sha1($wantref) );
}
sub master_sha1 {
my $self = shift;
return $self->ref_sha1('refs/heads/master');
}
sub master {
my $self = shift;
return $self->ref('refs/heads/master');
}
sub head_sha1 {
my $self = shift;
return $self->ref_sha1('HEAD');
}
sub head {
my $self = shift;
return $self->ref('HEAD');
}
sub get_object {
my ( $self, $sha1 ) = @_;
return unless $sha1;
return $self->get_object_packed($sha1) || $self->get_object_loose($sha1);
}
sub get_objects {
my ( $self, @sha1s ) = @_;
return map { $self->get_object($_) } @sha1s;
}
sub get_object_packed {
my ( $self, $sha1 ) = @_;
foreach my $pack ( $self->packs ) {
my ( $kind, $size, $content ) = $pack->get_object($sha1);
if ( defined($kind) && defined($size) && defined($content) ) {
return $self->create_object( $sha1, $kind, $size, $content );
}
}
}
sub get_object_loose {
my ( $self, $sha1 ) = @_;
my ( $kind, $size, $content ) = $self->loose->get_object($sha1);
if ( defined($kind) && defined($size) && defined($content) ) {
return $self->create_object( $sha1, $kind, $size, $content );
}
}
sub create_object {
my ( $self, $sha1, $kind, $size, $content ) = @_;
if ( $kind eq 'commit' ) {
return Git::PurePerl::Object::Commit->new(
sha1 => $sha1,
kind => $kind,
size => $size,
content => $content,
git => $self,
);
} elsif ( $kind eq 'tree' ) {
return Git::PurePerl::Object::Tree->new(
sha1 => $sha1,
kind => $kind,
size => $size,
content => $content,
git => $self,
);
} elsif ( $kind eq 'blob' ) {
return Git::PurePerl::Object::Blob->new(
sha1 => $sha1,
kind => $kind,
size => $size,
content => $content,
git => $self,
);
} elsif ( $kind eq 'tag' ) {
return Git::PurePerl::Object::Tag->new(
sha1 => $sha1,
kind => $kind,
size => $size,
content => $content,
git => $self,
);
} else {
confess "unknown kind $kind: $content";
}
}
sub all_sha1s {
my $self = shift;
my $dir = dir( $self->gitdir, 'objects' );
my @streams;
push @streams, $self->loose->all_sha1s;
foreach my $pack ( $self->packs ) {
push @streams, $pack->all_sha1s;
}
return Data::Stream::Bulk::Cat->new( streams => \@streams );
}
sub all_objects {
my $self = shift;
my $stream = $self->all_sha1s;
return Data::Stream::Bulk::Filter->new(
filter => sub { return [ $self->get_objects(@$_) ] },
stream => $stream,
);
}
sub put_object {
my ( $self, $object, $ref ) = @_;
$self->loose->put_object($object);
if ( $object->kind eq 'commit' ) {
$ref = 'master' unless $ref;
$self->update_ref( $ref, $object->sha1 );
}
}
sub update_ref {
my ( $self, $refname, $sha1 ) = @_;
my $ref = file( $self->gitdir, 'refs', 'heads', $refname );
$ref->parent->mkpath;
my $ref_fh = $ref->openw;
$ref_fh->print($sha1) || die "Error writing to $ref";
# FIXME is this always what we want?
my $head = file( $self->gitdir, 'HEAD' );
my $head_fh = $head->openw;
$head_fh->print("ref: refs/heads/$refname")
|| die "Error writing to $head";
}
sub init {
my ( $class, %arguments ) = @_;
my $directory = $arguments{directory};
my $git_dir;
unless ( defined $directory ) {
$git_dir = $arguments{gitdir}
|| confess
"init() needs either a 'directory' or a 'gitdir' argument";
} else {
if ( not defined $arguments{gitdir} ) {
$git_dir = $arguments{gitdir} = dir( $directory, '.git' );
}
dir($directory)->mkpath;
}
dir($git_dir)->mkpath;
dir( $git_dir, 'refs', 'tags' )->mkpath;
dir( $git_dir, 'objects', 'info' )->mkpath;
dir( $git_dir, 'objects', 'pack' )->mkpath;
dir( $git_dir, 'branches' )->mkpath;
dir( $git_dir, 'hooks' )->mkpath;
my $bare = defined($directory) ? 'false' : 'true';
$class->_add_file(
file( $git_dir, 'config' ),
"[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = $bare\n\tlogallrefupdates = true\n"
);
$class->_add_file( file( $git_dir, 'description' ),
"Unnamed repository; edit this file to name it for gitweb.\n" );
$class->_add_file(
file( $git_dir, 'hooks', 'applypatch-msg' ),
"# add shell script and make executable to enable\n"
);
$class->_add_file( file( $git_dir, 'hooks', 'post-commit' ),
"# add shell script and make executable to enable\n" );
$class->_add_file(
file( $git_dir, 'hooks', 'post-receive' ),
"# add shell script and make executable to enable\n"
);
$class->_add_file( file( $git_dir, 'hooks', 'post-update' ),
"# add shell script and make executable to enable\n" );
$class->_add_file(
file( $git_dir, 'hooks', 'pre-applypatch' ),
"# add shell script and make executable to enable\n"
);
$class->_add_file( file( $git_dir, 'hooks', 'pre-commit' ),
"# add shell script and make executable to enable\n" );
$class->_add_file( file( $git_dir, 'hooks', 'pre-rebase' ),
"# add shell script and make executable to enable\n" );
$class->_add_file( file( $git_dir, 'hooks', 'update' ),
"# add shell script and make executable to enable\n" );
dir( $git_dir, 'info' )->mkpath;
$class->_add_file( file( $git_dir, 'info', 'exclude' ),
"# *.[oa]\n# *~\n" );
return $class->new(%arguments);
}
sub checkout {
my ( $self, $directory, $tree ) = @_;
$directory ||= $self->directory;
$tree ||= $self->master->tree;
confess("Missing tree") unless $tree;
foreach my $directory_entry ( $tree->directory_entries ) {
my $filename = file( $directory, $directory_entry->filename );
my $sha1 = $directory_entry->sha1;
my $mode = $directory_entry->mode;
my $object = $self->get_object($sha1);
if ( $object->kind eq 'blob' ) {
$self->_add_file( $filename, $object->content );
chmod( oct( '0' . $mode ), $filename )
|| die "Error chmoding $filename to $mode: $!";
} elsif ( $object->kind eq 'tree' ) {
dir($filename)->mkpath;
$self->checkout( $filename, $object );
} else {
die $object->kind;
}
}
}
sub clone {
my $self = shift;
my $remote;
if (@_ == 2) {
# For backwards compatibility
$remote = "git://$_[0]";
$remote .= "/" unless $_[1] =~ m{^/};
$remote .= $_[1];
} else {
$remote = shift;
}
my $protocol = Git::PurePerl::Protocol->new(
remote => $remote,
);
my $sha1s = $protocol->connect;
my $head = $sha1s->{HEAD};
my $data = $protocol->fetch_pack($head);
my $filename
= file( $self->gitdir, 'objects', 'pack', 'pack-' . $head . '.pack' );
$self->_add_file( $filename, $data );
my $pack
= Git::PurePerl::Pack::WithoutIndex->new( filename => $filename );
$pack->create_index();
$self->update_ref( master => $head );
}
sub _add_file {
my ( $class, $filename, $contents ) = @_;
my $fh = $filename->openw || confess "Error opening to $filename: $!";
binmode($fh); #important for Win32
$fh->print($contents) || confess "Error writing to $filename: $!";
$fh->close || confess "Error closing $filename: $!";
}
1;
__END__
=head1 NAME
Git::PurePerl - A Pure Perl interface to Git repositories
=head1 SYNOPSIS
my $git = Git::PurePerl->new(
directory => '/path/to/git/'
);
$git->master->committer;
$git->master->comment;
$git->get_object($git->master->tree);
=head1 DESCRIPTION
This module is a Pure Perl interface to Git repositories.
It was mostly based on Grit L<http://grit.rubyforge.org/>.
=head1 METHODS
=over 4
=item master
=item get_object
=item get_object_packed
=item get_object_loose
=item create_object
=item all_sha1s
=back
=head1 MAINTAINANCE
This module is maintained in git at L<http://github.com/bobtfish/git-pureperl/>.
Patches are welcome, please come speak to one of the L<Gitalist> team
on C<< #gitalist >>.
=head1 AUTHOR
Leon Brocard <acme@astray.com>
=head1 CONTRIBUTORS
=over 4
=item Chris Reinhardt
=item Tomas (t0m) Doran
=item Dan (broquaint) Brook
=item Alex Vandiver
=item Dagfinn Ilmari MannsE<aring>ker
=back
=head1 COPYRIGHT
Copyright (C) 2008, Leon Brocard and the above mentioned contributors.
=head1 LICENSE
This module is free software; you can redistribute it or
modify it under the same terms as Perl itself.
=cut
|