Add "--expire <time>" option to 'git prune'
[git.git] / perl / Git.pm
blob7468460f9a6d29d5c4bf14db4921bf28e23b6814
1 =head1 NAME
3 Git - Perl interface to the Git version control system
5 =cut
8 package Git;
10 use strict;
13 BEGIN {
15 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
17 # Totally unstable API.
18 $VERSION = '0.01';
21 =head1 SYNOPSIS
23 use Git;
25 my $version = Git::command_oneline('version');
27 git_cmd_try { Git::command_noisy('update-server-info') }
28 '%s failed w/ code %d';
30 my $repo = Git->repository (Directory => '/srv/git/cogito.git');
33 my @revs = $repo->command('rev-list', '--since=last monday', '--all');
35 my ($fh, $c) = $repo->command_output_pipe('rev-list', '--since=last monday', '--all');
36 my $lastrev = <$fh>; chomp $lastrev;
37 $repo->command_close_pipe($fh, $c);
39 my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ],
40 STDERR => 0 );
42 =cut
45 require Exporter;
47 @ISA = qw(Exporter);
49 @EXPORT = qw(git_cmd_try);
51 # Methods which can be called as standalone functions as well:
52 @EXPORT_OK = qw(command command_oneline command_noisy
53 command_output_pipe command_input_pipe command_close_pipe
54 version exec_path hash_object git_cmd_try);
57 =head1 DESCRIPTION
59 This module provides Perl scripts easy way to interface the Git version control
60 system. The modules have an easy and well-tested way to call arbitrary Git
61 commands; in the future, the interface will also provide specialized methods
62 for doing easily operations which are not totally trivial to do over
63 the generic command interface.
65 While some commands can be executed outside of any context (e.g. 'version'
66 or 'init'), most operations require a repository context, which in practice
67 means getting an instance of the Git object using the repository() constructor.
68 (In the future, we will also get a new_repository() constructor.) All commands
69 called as methods of the object are then executed in the context of the
70 repository.
72 Part of the "repository state" is also information about path to the attached
73 working copy (unless you work with a bare repository). You can also navigate
74 inside of the working copy using the C<wc_chdir()> method. (Note that
75 the repository object is self-contained and will not change working directory
76 of your process.)
78 TODO: In the future, we might also do
80 my $remoterepo = $repo->remote_repository (Name => 'cogito', Branch => 'master');
81 $remoterepo ||= Git->remote_repository ('http://git.or.cz/cogito.git/');
82 my @refs = $remoterepo->refs();
84 Currently, the module merely wraps calls to external Git tools. In the future,
85 it will provide a much faster way to interact with Git by linking directly
86 to libgit. This should be completely opaque to the user, though (performance
87 increate nonwithstanding).
89 =cut
92 use Carp qw(carp croak); # but croak is bad - throw instead
93 use Error qw(:try);
94 use Cwd qw(abs_path);
99 =head1 CONSTRUCTORS
101 =over 4
103 =item repository ( OPTIONS )
105 =item repository ( DIRECTORY )
107 =item repository ()
109 Construct a new repository object.
110 C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
111 Possible options are:
113 B<Repository> - Path to the Git repository.
115 B<WorkingCopy> - Path to the associated working copy; not strictly required
116 as many commands will happily crunch on a bare repository.
118 B<WorkingSubdir> - Subdirectory in the working copy to work inside.
119 Just left undefined if you do not want to limit the scope of operations.
121 B<Directory> - Path to the Git working directory in its usual setup.
122 The C<.git> directory is searched in the directory and all the parent
123 directories; if found, C<WorkingCopy> is set to the directory containing
124 it and C<Repository> to the C<.git> directory itself. If no C<.git>
125 directory was found, the C<Directory> is assumed to be a bare repository,
126 C<Repository> is set to point at it and C<WorkingCopy> is left undefined.
127 If the C<$GIT_DIR> environment variable is set, things behave as expected
128 as well.
130 You should not use both C<Directory> and either of C<Repository> and
131 C<WorkingCopy> - the results of that are undefined.
133 Alternatively, a directory path may be passed as a single scalar argument
134 to the constructor; it is equivalent to setting only the C<Directory> option
135 field.
137 Calling the constructor with no options whatsoever is equivalent to
138 calling it with C<< Directory => '.' >>. In general, if you are building
139 a standard porcelain command, simply doing C<< Git->repository() >> should
140 do the right thing and setup the object to reflect exactly where the user
141 is right now.
143 =cut
145 sub repository {
146 my $class = shift;
147 my @args = @_;
148 my %opts = ();
149 my $self;
151 if (defined $args[0]) {
152 if ($#args % 2 != 1) {
153 # Not a hash.
154 $#args == 0 or throw Error::Simple("bad usage");
155 %opts = ( Directory => $args[0] );
156 } else {
157 %opts = @args;
161 if (not defined $opts{Repository} and not defined $opts{WorkingCopy}) {
162 $opts{Directory} ||= '.';
165 if ($opts{Directory}) {
166 -d $opts{Directory} or throw Error::Simple("Directory not found: $!");
168 my $search = Git->repository(WorkingCopy => $opts{Directory});
169 my $dir;
170 try {
171 $dir = $search->command_oneline(['rev-parse', '--git-dir'],
172 STDERR => 0);
173 } catch Git::Error::Command with {
174 $dir = undef;
177 if ($dir) {
178 $dir =~ m#^/# or $dir = $opts{Directory} . '/' . $dir;
179 $opts{Repository} = $dir;
181 # If --git-dir went ok, this shouldn't die either.
182 my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
183 $dir = abs_path($opts{Directory}) . '/';
184 if ($prefix) {
185 if (substr($dir, -length($prefix)) ne $prefix) {
186 throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix");
188 substr($dir, -length($prefix)) = '';
190 $opts{WorkingCopy} = $dir;
191 $opts{WorkingSubdir} = $prefix;
193 } else {
194 # A bare repository? Let's see...
195 $dir = $opts{Directory};
197 unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") {
198 # Mimick git-rev-parse --git-dir error message:
199 throw Error::Simple('fatal: Not a git repository');
201 my $search = Git->repository(Repository => $dir);
202 try {
203 $search->command('symbolic-ref', 'HEAD');
204 } catch Git::Error::Command with {
205 # Mimick git-rev-parse --git-dir error message:
206 throw Error::Simple('fatal: Not a git repository');
209 $opts{Repository} = abs_path($dir);
212 delete $opts{Directory};
215 $self = { opts => \%opts };
216 bless $self, $class;
220 =back
222 =head1 METHODS
224 =over 4
226 =item command ( COMMAND [, ARGUMENTS... ] )
228 =item command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
230 Execute the given Git C<COMMAND> (specify it without the 'git-'
231 prefix), optionally with the specified extra C<ARGUMENTS>.
233 The second more elaborate form can be used if you want to further adjust
234 the command execution. Currently, only one option is supported:
236 B<STDERR> - How to deal with the command's error output. By default (C<undef>)
237 it is delivered to the caller's C<STDERR>. A false value (0 or '') will cause
238 it to be thrown away. If you want to process it, you can get it in a filehandle
239 you specify, but you must be extremely careful; if the error output is not
240 very short and you want to read it in the same process as where you called
241 C<command()>, you are set up for a nice deadlock!
243 The method can be called without any instance or on a specified Git repository
244 (in that case the command will be run in the repository context).
246 In scalar context, it returns all the command output in a single string
247 (verbatim).
249 In array context, it returns an array containing lines printed to the
250 command's stdout (without trailing newlines).
252 In both cases, the command's stdin and stderr are the same as the caller's.
254 =cut
256 sub command {
257 my ($fh, $ctx) = command_output_pipe(@_);
259 if (not defined wantarray) {
260 # Nothing to pepper the possible exception with.
261 _cmd_close($fh, $ctx);
263 } elsif (not wantarray) {
264 local $/;
265 my $text = <$fh>;
266 try {
267 _cmd_close($fh, $ctx);
268 } catch Git::Error::Command with {
269 # Pepper with the output:
270 my $E = shift;
271 $E->{'-outputref'} = \$text;
272 throw $E;
274 return $text;
276 } else {
277 my @lines = <$fh>;
278 defined and chomp for @lines;
279 try {
280 _cmd_close($fh, $ctx);
281 } catch Git::Error::Command with {
282 my $E = shift;
283 $E->{'-outputref'} = \@lines;
284 throw $E;
286 return @lines;
291 =item command_oneline ( COMMAND [, ARGUMENTS... ] )
293 =item command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
295 Execute the given C<COMMAND> in the same way as command()
296 does but always return a scalar string containing the first line
297 of the command's standard output.
299 =cut
301 sub command_oneline {
302 my ($fh, $ctx) = command_output_pipe(@_);
304 my $line = <$fh>;
305 defined $line and chomp $line;
306 try {
307 _cmd_close($fh, $ctx);
308 } catch Git::Error::Command with {
309 # Pepper with the output:
310 my $E = shift;
311 $E->{'-outputref'} = \$line;
312 throw $E;
314 return $line;
318 =item command_output_pipe ( COMMAND [, ARGUMENTS... ] )
320 =item command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
322 Execute the given C<COMMAND> in the same way as command()
323 does but return a pipe filehandle from which the command output can be
324 read.
326 The function can return C<($pipe, $ctx)> in array context.
327 See C<command_close_pipe()> for details.
329 =cut
331 sub command_output_pipe {
332 _command_common_pipe('-|', @_);
336 =item command_input_pipe ( COMMAND [, ARGUMENTS... ] )
338 =item command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
340 Execute the given C<COMMAND> in the same way as command_output_pipe()
341 does but return an input pipe filehandle instead; the command output
342 is not captured.
344 The function can return C<($pipe, $ctx)> in array context.
345 See C<command_close_pipe()> for details.
347 =cut
349 sub command_input_pipe {
350 _command_common_pipe('|-', @_);
354 =item command_close_pipe ( PIPE [, CTX ] )
356 Close the C<PIPE> as returned from C<command_*_pipe()>, checking
357 whether the command finished successfully. The optional C<CTX> argument
358 is required if you want to see the command name in the error message,
359 and it is the second value returned by C<command_*_pipe()> when
360 called in array context. The call idiom is:
362 my ($fh, $ctx) = $r->command_output_pipe('status');
363 while (<$fh>) { ... }
364 $r->command_close_pipe($fh, $ctx);
366 Note that you should not rely on whatever actually is in C<CTX>;
367 currently it is simply the command name but in future the context might
368 have more complicated structure.
370 =cut
372 sub command_close_pipe {
373 my ($self, $fh, $ctx) = _maybe_self(@_);
374 $ctx ||= '<unknown>';
375 _cmd_close($fh, $ctx);
379 =item command_noisy ( COMMAND [, ARGUMENTS... ] )
381 Execute the given C<COMMAND> in the same way as command() does but do not
382 capture the command output - the standard output is not redirected and goes
383 to the standard output of the caller application.
385 While the method is called command_noisy(), you might want to as well use
386 it for the most silent Git commands which you know will never pollute your
387 stdout but you want to avoid the overhead of the pipe setup when calling them.
389 The function returns only after the command has finished running.
391 =cut
393 sub command_noisy {
394 my ($self, $cmd, @args) = _maybe_self(@_);
395 _check_valid_cmd($cmd);
397 my $pid = fork;
398 if (not defined $pid) {
399 throw Error::Simple("fork failed: $!");
400 } elsif ($pid == 0) {
401 _cmd_exec($self, $cmd, @args);
403 if (waitpid($pid, 0) > 0 and $?>>8 != 0) {
404 throw Git::Error::Command(join(' ', $cmd, @args), $? >> 8);
409 =item version ()
411 Return the Git version in use.
413 =cut
415 sub version {
416 my $verstr = command_oneline('--version');
417 $verstr =~ s/^git version //;
418 $verstr;
422 =item exec_path ()
424 Return path to the Git sub-command executables (the same as
425 C<git --exec-path>). Useful mostly only internally.
427 =cut
429 sub exec_path { command_oneline('--exec-path') }
432 =item repo_path ()
434 Return path to the git repository. Must be called on a repository instance.
436 =cut
438 sub repo_path { $_[0]->{opts}->{Repository} }
441 =item wc_path ()
443 Return path to the working copy. Must be called on a repository instance.
445 =cut
447 sub wc_path { $_[0]->{opts}->{WorkingCopy} }
450 =item wc_subdir ()
452 Return path to the subdirectory inside of a working copy. Must be called
453 on a repository instance.
455 =cut
457 sub wc_subdir { $_[0]->{opts}->{WorkingSubdir} ||= '' }
460 =item wc_chdir ( SUBDIR )
462 Change the working copy subdirectory to work within. The C<SUBDIR> is
463 relative to the working copy root directory (not the current subdirectory).
464 Must be called on a repository instance attached to a working copy
465 and the directory must exist.
467 =cut
469 sub wc_chdir {
470 my ($self, $subdir) = @_;
471 $self->wc_path()
472 or throw Error::Simple("bare repository");
474 -d $self->wc_path().'/'.$subdir
475 or throw Error::Simple("subdir not found: $!");
476 # Of course we will not "hold" the subdirectory so anyone
477 # can delete it now and we will never know. But at least we tried.
479 $self->{opts}->{WorkingSubdir} = $subdir;
483 =item config ( VARIABLE )
485 Retrieve the configuration C<VARIABLE> in the same manner as C<config>
486 does. In scalar context requires the variable to be set only one time
487 (exception is thrown otherwise), in array context returns allows the
488 variable to be set multiple times and returns all the values.
490 Must be called on a repository instance.
492 This currently wraps command('config') so it is not so fast.
494 =cut
496 sub config {
497 my ($self, $var) = @_;
498 $self->repo_path()
499 or throw Error::Simple("not a repository");
501 try {
502 if (wantarray) {
503 return $self->command('config', '--get-all', $var);
504 } else {
505 return $self->command_oneline('config', '--get', $var);
507 } catch Git::Error::Command with {
508 my $E = shift;
509 if ($E->value() == 1) {
510 # Key not found.
511 return undef;
512 } else {
513 throw $E;
519 =item config_bool ( VARIABLE )
521 Retrieve the bool configuration C<VARIABLE>. The return value
522 is usable as a boolean in perl (and C<undef> if it's not defined,
523 of course).
525 Must be called on a repository instance.
527 This currently wraps command('config') so it is not so fast.
529 =cut
531 sub config_bool {
532 my ($self, $var) = @_;
533 $self->repo_path()
534 or throw Error::Simple("not a repository");
536 try {
537 my $val = $self->command_oneline('config', '--bool', '--get',
538 $var);
539 return undef unless defined $val;
540 return $val eq 'true';
541 } catch Git::Error::Command with {
542 my $E = shift;
543 if ($E->value() == 1) {
544 # Key not found.
545 return undef;
546 } else {
547 throw $E;
552 =item config_int ( VARIABLE )
554 Retrieve the integer configuration C<VARIABLE>. The return value
555 is simple decimal number. An optional value suffix of 'k', 'm',
556 or 'g' in the config file will cause the value to be multiplied
557 by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
558 It would return C<undef> if configuration variable is not defined,
560 Must be called on a repository instance.
562 This currently wraps command('config') so it is not so fast.
564 =cut
566 sub config_int {
567 my ($self, $var) = @_;
568 $self->repo_path()
569 or throw Error::Simple("not a repository");
571 try {
572 return $self->command_oneline('config', '--int', '--get', $var);
573 } catch Git::Error::Command with {
574 my $E = shift;
575 if ($E->value() == 1) {
576 # Key not found.
577 return undef;
578 } else {
579 throw $E;
584 =item ident ( TYPE | IDENTSTR )
586 =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )
588 This suite of functions retrieves and parses ident information, as stored
589 in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus
590 C<TYPE> can be either I<author> or I<committer>; case is insignificant).
592 The C<ident> method retrieves the ident information from C<git-var>
593 and either returns it as a scalar string or as an array with the fields parsed.
594 Alternatively, it can take a prepared ident string (e.g. from the commit
595 object) and just parse it.
597 C<ident_person> returns the person part of the ident - name and email;
598 it can take the same arguments as C<ident> or the array returned by C<ident>.
600 The synopsis is like:
602 my ($name, $email, $time_tz) = ident('author');
603 "$name <$email>" eq ident_person('author');
604 "$name <$email>" eq ident_person($name);
605 $time_tz =~ /^\d+ [+-]\d{4}$/;
607 Both methods must be called on a repository instance.
609 =cut
611 sub ident {
612 my ($self, $type) = @_;
613 my $identstr;
614 if (lc $type eq lc 'committer' or lc $type eq lc 'author') {
615 $identstr = $self->command_oneline('var', 'GIT_'.uc($type).'_IDENT');
616 } else {
617 $identstr = $type;
619 if (wantarray) {
620 return $identstr =~ /^(.*) <(.*)> (\d+ [+-]\d{4})$/;
621 } else {
622 return $identstr;
626 sub ident_person {
627 my ($self, @ident) = @_;
628 $#ident == 0 and @ident = $self->ident($ident[0]);
629 return "$ident[0] <$ident[1]>";
633 =item hash_object ( TYPE, FILENAME )
635 Compute the SHA1 object id of the given C<FILENAME> (or data waiting in
636 C<FILEHANDLE>) considering it is of the C<TYPE> object type (C<blob>,
637 C<commit>, C<tree>).
639 The method can be called without any instance or on a specified Git repository,
640 it makes zero difference.
642 The function returns the SHA1 hash.
644 =cut
646 # TODO: Support for passing FILEHANDLE instead of FILENAME
647 sub hash_object {
648 my ($self, $type, $file) = _maybe_self(@_);
649 command_oneline('hash-object', '-t', $type, $file);
654 =back
656 =head1 ERROR HANDLING
658 All functions are supposed to throw Perl exceptions in case of errors.
659 See the L<Error> module on how to catch those. Most exceptions are mere
660 L<Error::Simple> instances.
662 However, the C<command()>, C<command_oneline()> and C<command_noisy()>
663 functions suite can throw C<Git::Error::Command> exceptions as well: those are
664 thrown when the external command returns an error code and contain the error
665 code as well as access to the captured command's output. The exception class
666 provides the usual C<stringify> and C<value> (command's exit code) methods and
667 in addition also a C<cmd_output> method that returns either an array or a
668 string with the captured command output (depending on the original function
669 call context; C<command_noisy()> returns C<undef>) and $<cmdline> which
670 returns the command and its arguments (but without proper quoting).
672 Note that the C<command_*_pipe()> functions cannot throw this exception since
673 it has no idea whether the command failed or not. You will only find out
674 at the time you C<close> the pipe; if you want to have that automated,
675 use C<command_close_pipe()>, which can throw the exception.
677 =cut
680 package Git::Error::Command;
682 @Git::Error::Command::ISA = qw(Error);
684 sub new {
685 my $self = shift;
686 my $cmdline = '' . shift;
687 my $value = 0 + shift;
688 my $outputref = shift;
689 my(@args) = ();
691 local $Error::Depth = $Error::Depth + 1;
693 push(@args, '-cmdline', $cmdline);
694 push(@args, '-value', $value);
695 push(@args, '-outputref', $outputref);
697 $self->SUPER::new(-text => 'command returned error', @args);
700 sub stringify {
701 my $self = shift;
702 my $text = $self->SUPER::stringify;
703 $self->cmdline() . ': ' . $text . ': ' . $self->value() . "\n";
706 sub cmdline {
707 my $self = shift;
708 $self->{'-cmdline'};
711 sub cmd_output {
712 my $self = shift;
713 my $ref = $self->{'-outputref'};
714 defined $ref or undef;
715 if (ref $ref eq 'ARRAY') {
716 return @$ref;
717 } else { # SCALAR
718 return $$ref;
723 =over 4
725 =item git_cmd_try { CODE } ERRMSG
727 This magical statement will automatically catch any C<Git::Error::Command>
728 exceptions thrown by C<CODE> and make your program die with C<ERRMSG>
729 on its lips; the message will have %s substituted for the command line
730 and %d for the exit status. This statement is useful mostly for producing
731 more user-friendly error messages.
733 In case of no exception caught the statement returns C<CODE>'s return value.
735 Note that this is the only auto-exported function.
737 =cut
739 sub git_cmd_try(&$) {
740 my ($code, $errmsg) = @_;
741 my @result;
742 my $err;
743 my $array = wantarray;
744 try {
745 if ($array) {
746 @result = &$code;
747 } else {
748 $result[0] = &$code;
750 } catch Git::Error::Command with {
751 my $E = shift;
752 $err = $errmsg;
753 $err =~ s/\%s/$E->cmdline()/ge;
754 $err =~ s/\%d/$E->value()/ge;
755 # We can't croak here since Error.pm would mangle
756 # that to Error::Simple.
758 $err and croak $err;
759 return $array ? @result : $result[0];
763 =back
765 =head1 COPYRIGHT
767 Copyright 2006 by Petr Baudis E<lt>pasky@suse.czE<gt>.
769 This module is free software; it may be used, copied, modified
770 and distributed under the terms of the GNU General Public Licence,
771 either version 2, or (at your option) any later version.
773 =cut
776 # Take raw method argument list and return ($obj, @args) in case
777 # the method was called upon an instance and (undef, @args) if
778 # it was called directly.
779 sub _maybe_self {
780 # This breaks inheritance. Oh well.
781 ref $_[0] eq 'Git' ? @_ : (undef, @_);
784 # Check if the command id is something reasonable.
785 sub _check_valid_cmd {
786 my ($cmd) = @_;
787 $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
790 # Common backend for the pipe creators.
791 sub _command_common_pipe {
792 my $direction = shift;
793 my ($self, @p) = _maybe_self(@_);
794 my (%opts, $cmd, @args);
795 if (ref $p[0]) {
796 ($cmd, @args) = @{shift @p};
797 %opts = ref $p[0] ? %{$p[0]} : @p;
798 } else {
799 ($cmd, @args) = @p;
801 _check_valid_cmd($cmd);
803 my $fh;
804 if ($^O eq 'MSWin32') {
805 # ActiveState Perl
806 #defined $opts{STDERR} and
807 # warn 'ignoring STDERR option - running w/ ActiveState';
808 $direction eq '-|' or
809 die 'input pipe for ActiveState not implemented';
810 # the strange construction with *ACPIPE is just to
811 # explain the tie below that we want to bind to
812 # a handle class, not scalar. It is not known if
813 # it is something specific to ActiveState Perl or
814 # just a Perl quirk.
815 tie (*ACPIPE, 'Git::activestate_pipe', $cmd, @args);
816 $fh = *ACPIPE;
818 } else {
819 my $pid = open($fh, $direction);
820 if (not defined $pid) {
821 throw Error::Simple("open failed: $!");
822 } elsif ($pid == 0) {
823 if (defined $opts{STDERR}) {
824 close STDERR;
826 if ($opts{STDERR}) {
827 open (STDERR, '>&', $opts{STDERR})
828 or die "dup failed: $!";
830 _cmd_exec($self, $cmd, @args);
833 return wantarray ? ($fh, join(' ', $cmd, @args)) : $fh;
836 # When already in the subprocess, set up the appropriate state
837 # for the given repository and execute the git command.
838 sub _cmd_exec {
839 my ($self, @args) = @_;
840 if ($self) {
841 $self->repo_path() and $ENV{'GIT_DIR'} = $self->repo_path();
842 $self->wc_path() and chdir($self->wc_path());
843 $self->wc_subdir() and chdir($self->wc_subdir());
845 _execv_git_cmd(@args);
846 die qq[exec "@args" failed: $!];
849 # Execute the given Git command ($_[0]) with arguments ($_[1..])
850 # by searching for it at proper places.
851 sub _execv_git_cmd { exec('git', @_); }
853 # Close pipe to a subprocess.
854 sub _cmd_close {
855 my ($fh, $ctx) = @_;
856 if (not close $fh) {
857 if ($!) {
858 # It's just close, no point in fatalities
859 carp "error closing pipe: $!";
860 } elsif ($? >> 8) {
861 # The caller should pepper this.
862 throw Git::Error::Command($ctx, $? >> 8);
864 # else we might e.g. closed a live stream; the command
865 # dying of SIGPIPE would drive us here.
870 sub DESTROY { }
873 # Pipe implementation for ActiveState Perl.
875 package Git::activestate_pipe;
876 use strict;
878 sub TIEHANDLE {
879 my ($class, @params) = @_;
880 # FIXME: This is probably horrible idea and the thing will explode
881 # at the moment you give it arguments that require some quoting,
882 # but I have no ActiveState clue... --pasky
883 # Let's just hope ActiveState Perl does at least the quoting
884 # correctly.
885 my @data = qx{git @params};
886 bless { i => 0, data => \@data }, $class;
889 sub READLINE {
890 my $self = shift;
891 if ($self->{i} >= scalar @{$self->{data}}) {
892 return undef;
894 my $i = $self->{i};
895 if (wantarray) {
896 $self->{i} = $#{$self->{'data'}} + 1;
897 return splice(@{$self->{'data'}}, $i);
899 $self->{i} = $i + 1;
900 return $self->{'data'}->[ $i ];
903 sub CLOSE {
904 my $self = shift;
905 delete $self->{data};
906 delete $self->{i};
909 sub EOF {
910 my $self = shift;
911 return ($self->{i} >= scalar @{$self->{data}});
915 1; # Famous last words