Merge branch 'maint'
[git] / perl / Git.pm
1 =head1 NAME
2
3 Git - Perl interface to the Git version control system
4
5 =cut
6
7
8 package Git;
9
10 use strict;
11
12
13 BEGIN {
14
15 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);
16
17 # Totally unstable API.
18 $VERSION = '0.01';
19
20
21 =head1 SYNOPSIS
22
23   use Git;
24
25   my $version = Git::command_oneline('version');
26
27   git_cmd_try { Git::command_noisy('update-server-info') }
28               '%s failed w/ code %d';
29
30   my $repo = Git->repository (Directory => '/srv/git/cogito.git');
31
32
33   my @revs = $repo->command('rev-list', '--since=last monday', '--all');
34
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);
38
39   my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ],
40                                         STDERR => 0 );
41
42   my $sha1 = $repo->hash_and_insert_object('file.txt');
43   my $tempfile = tempfile();
44   my $size = $repo->cat_blob($sha1, $tempfile);
45
46 =cut
47
48
49 require Exporter;
50
51 @ISA = qw(Exporter);
52
53 @EXPORT = qw(git_cmd_try);
54
55 # Methods which can be called as standalone functions as well:
56 @EXPORT_OK = qw(command command_oneline command_noisy
57                 command_output_pipe command_input_pipe command_close_pipe
58                 command_bidi_pipe command_close_bidi_pipe
59                 version exec_path hash_object git_cmd_try
60                 remote_refs);
61
62
63 =head1 DESCRIPTION
64
65 This module provides Perl scripts easy way to interface the Git version control
66 system. The modules have an easy and well-tested way to call arbitrary Git
67 commands; in the future, the interface will also provide specialized methods
68 for doing easily operations which are not totally trivial to do over
69 the generic command interface.
70
71 While some commands can be executed outside of any context (e.g. 'version'
72 or 'init'), most operations require a repository context, which in practice
73 means getting an instance of the Git object using the repository() constructor.
74 (In the future, we will also get a new_repository() constructor.) All commands
75 called as methods of the object are then executed in the context of the
76 repository.
77
78 Part of the "repository state" is also information about path to the attached
79 working copy (unless you work with a bare repository). You can also navigate
80 inside of the working copy using the C<wc_chdir()> method. (Note that
81 the repository object is self-contained and will not change working directory
82 of your process.)
83
84 TODO: In the future, we might also do
85
86         my $remoterepo = $repo->remote_repository (Name => 'cogito', Branch => 'master');
87         $remoterepo ||= Git->remote_repository ('http://git.or.cz/cogito.git/');
88         my @refs = $remoterepo->refs();
89
90 Currently, the module merely wraps calls to external Git tools. In the future,
91 it will provide a much faster way to interact with Git by linking directly
92 to libgit. This should be completely opaque to the user, though (performance
93 increate nonwithstanding).
94
95 =cut
96
97
98 use Carp qw(carp croak); # but croak is bad - throw instead
99 use Error qw(:try);
100 use Cwd qw(abs_path);
101 use IPC::Open2 qw(open2);
102
103 }
104
105
106 =head1 CONSTRUCTORS
107
108 =over 4
109
110 =item repository ( OPTIONS )
111
112 =item repository ( DIRECTORY )
113
114 =item repository ()
115
116 Construct a new repository object.
117 C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
118 Possible options are:
119
120 B<Repository> - Path to the Git repository.
121
122 B<WorkingCopy> - Path to the associated working copy; not strictly required
123 as many commands will happily crunch on a bare repository.
124
125 B<WorkingSubdir> - Subdirectory in the working copy to work inside.
126 Just left undefined if you do not want to limit the scope of operations.
127
128 B<Directory> - Path to the Git working directory in its usual setup.
129 The C<.git> directory is searched in the directory and all the parent
130 directories; if found, C<WorkingCopy> is set to the directory containing
131 it and C<Repository> to the C<.git> directory itself. If no C<.git>
132 directory was found, the C<Directory> is assumed to be a bare repository,
133 C<Repository> is set to point at it and C<WorkingCopy> is left undefined.
134 If the C<$GIT_DIR> environment variable is set, things behave as expected
135 as well.
136
137 You should not use both C<Directory> and either of C<Repository> and
138 C<WorkingCopy> - the results of that are undefined.
139
140 Alternatively, a directory path may be passed as a single scalar argument
141 to the constructor; it is equivalent to setting only the C<Directory> option
142 field.
143
144 Calling the constructor with no options whatsoever is equivalent to
145 calling it with C<< Directory => '.' >>. In general, if you are building
146 a standard porcelain command, simply doing C<< Git->repository() >> should
147 do the right thing and setup the object to reflect exactly where the user
148 is right now.
149
150 =cut
151
152 sub repository {
153         my $class = shift;
154         my @args = @_;
155         my %opts = ();
156         my $self;
157
158         if (defined $args[0]) {
159                 if ($#args % 2 != 1) {
160                         # Not a hash.
161                         $#args == 0 or throw Error::Simple("bad usage");
162                         %opts = ( Directory => $args[0] );
163                 } else {
164                         %opts = @args;
165                 }
166         }
167
168         if (not defined $opts{Repository} and not defined $opts{WorkingCopy}) {
169                 $opts{Directory} ||= '.';
170         }
171
172         if ($opts{Directory}) {
173                 -d $opts{Directory} or throw Error::Simple("Directory not found: $!");
174
175                 my $search = Git->repository(WorkingCopy => $opts{Directory});
176                 my $dir;
177                 try {
178                         $dir = $search->command_oneline(['rev-parse', '--git-dir'],
179                                                         STDERR => 0);
180                 } catch Git::Error::Command with {
181                         $dir = undef;
182                 };
183
184                 if ($dir) {
185                         $dir =~ m#^/# or $dir = $opts{Directory} . '/' . $dir;
186                         $opts{Repository} = $dir;
187
188                         # If --git-dir went ok, this shouldn't die either.
189                         my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
190                         $dir = abs_path($opts{Directory}) . '/';
191                         if ($prefix) {
192                                 if (substr($dir, -length($prefix)) ne $prefix) {
193                                         throw Error::Simple("rev-parse confused me - $dir does not have trailing $prefix");
194                                 }
195                                 substr($dir, -length($prefix)) = '';
196                         }
197                         $opts{WorkingCopy} = $dir;
198                         $opts{WorkingSubdir} = $prefix;
199
200                 } else {
201                         # A bare repository? Let's see...
202                         $dir = $opts{Directory};
203
204                         unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") {
205                                 # Mimick git-rev-parse --git-dir error message:
206                                 throw Error::Simple('fatal: Not a git repository');
207                         }
208                         my $search = Git->repository(Repository => $dir);
209                         try {
210                                 $search->command('symbolic-ref', 'HEAD');
211                         } catch Git::Error::Command with {
212                                 # Mimick git-rev-parse --git-dir error message:
213                                 throw Error::Simple('fatal: Not a git repository');
214                         }
215
216                         $opts{Repository} = abs_path($dir);
217                 }
218
219                 delete $opts{Directory};
220         }
221
222         $self = { opts => \%opts };
223         bless $self, $class;
224 }
225
226 =back
227
228 =head1 METHODS
229
230 =over 4
231
232 =item command ( COMMAND [, ARGUMENTS... ] )
233
234 =item command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
235
236 Execute the given Git C<COMMAND> (specify it without the 'git-'
237 prefix), optionally with the specified extra C<ARGUMENTS>.
238
239 The second more elaborate form can be used if you want to further adjust
240 the command execution. Currently, only one option is supported:
241
242 B<STDERR> - How to deal with the command's error output. By default (C<undef>)
243 it is delivered to the caller's C<STDERR>. A false value (0 or '') will cause
244 it to be thrown away. If you want to process it, you can get it in a filehandle
245 you specify, but you must be extremely careful; if the error output is not
246 very short and you want to read it in the same process as where you called
247 C<command()>, you are set up for a nice deadlock!
248
249 The method can be called without any instance or on a specified Git repository
250 (in that case the command will be run in the repository context).
251
252 In scalar context, it returns all the command output in a single string
253 (verbatim).
254
255 In array context, it returns an array containing lines printed to the
256 command's stdout (without trailing newlines).
257
258 In both cases, the command's stdin and stderr are the same as the caller's.
259
260 =cut
261
262 sub command {
263         my ($fh, $ctx) = command_output_pipe(@_);
264
265         if (not defined wantarray) {
266                 # Nothing to pepper the possible exception with.
267                 _cmd_close($fh, $ctx);
268
269         } elsif (not wantarray) {
270                 local $/;
271                 my $text = <$fh>;
272                 try {
273                         _cmd_close($fh, $ctx);
274                 } catch Git::Error::Command with {
275                         # Pepper with the output:
276                         my $E = shift;
277                         $E->{'-outputref'} = \$text;
278                         throw $E;
279                 };
280                 return $text;
281
282         } else {
283                 my @lines = <$fh>;
284                 defined and chomp for @lines;
285                 try {
286                         _cmd_close($fh, $ctx);
287                 } catch Git::Error::Command with {
288                         my $E = shift;
289                         $E->{'-outputref'} = \@lines;
290                         throw $E;
291                 };
292                 return @lines;
293         }
294 }
295
296
297 =item command_oneline ( COMMAND [, ARGUMENTS... ] )
298
299 =item command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
300
301 Execute the given C<COMMAND> in the same way as command()
302 does but always return a scalar string containing the first line
303 of the command's standard output.
304
305 =cut
306
307 sub command_oneline {
308         my ($fh, $ctx) = command_output_pipe(@_);
309
310         my $line = <$fh>;
311         defined $line and chomp $line;
312         try {
313                 _cmd_close($fh, $ctx);
314         } catch Git::Error::Command with {
315                 # Pepper with the output:
316                 my $E = shift;
317                 $E->{'-outputref'} = \$line;
318                 throw $E;
319         };
320         return $line;
321 }
322
323
324 =item command_output_pipe ( COMMAND [, ARGUMENTS... ] )
325
326 =item command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
327
328 Execute the given C<COMMAND> in the same way as command()
329 does but return a pipe filehandle from which the command output can be
330 read.
331
332 The function can return C<($pipe, $ctx)> in array context.
333 See C<command_close_pipe()> for details.
334
335 =cut
336
337 sub command_output_pipe {
338         _command_common_pipe('-|', @_);
339 }
340
341
342 =item command_input_pipe ( COMMAND [, ARGUMENTS... ] )
343
344 =item command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
345
346 Execute the given C<COMMAND> in the same way as command_output_pipe()
347 does but return an input pipe filehandle instead; the command output
348 is not captured.
349
350 The function can return C<($pipe, $ctx)> in array context.
351 See C<command_close_pipe()> for details.
352
353 =cut
354
355 sub command_input_pipe {
356         _command_common_pipe('|-', @_);
357 }
358
359
360 =item command_close_pipe ( PIPE [, CTX ] )
361
362 Close the C<PIPE> as returned from C<command_*_pipe()>, checking
363 whether the command finished successfully. The optional C<CTX> argument
364 is required if you want to see the command name in the error message,
365 and it is the second value returned by C<command_*_pipe()> when
366 called in array context. The call idiom is:
367
368         my ($fh, $ctx) = $r->command_output_pipe('status');
369         while (<$fh>) { ... }
370         $r->command_close_pipe($fh, $ctx);
371
372 Note that you should not rely on whatever actually is in C<CTX>;
373 currently it is simply the command name but in future the context might
374 have more complicated structure.
375
376 =cut
377
378 sub command_close_pipe {
379         my ($self, $fh, $ctx) = _maybe_self(@_);
380         $ctx ||= '<unknown>';
381         _cmd_close($fh, $ctx);
382 }
383
384 =item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] )
385
386 Execute the given C<COMMAND> in the same way as command_output_pipe()
387 does but return both an input pipe filehandle and an output pipe filehandle.
388
389 The function will return return C<($pid, $pipe_in, $pipe_out, $ctx)>.
390 See C<command_close_bidi_pipe()> for details.
391
392 =cut
393
394 sub command_bidi_pipe {
395         my ($pid, $in, $out);
396         $pid = open2($in, $out, 'git', @_);
397         return ($pid, $in, $out, join(' ', @_));
398 }
399
400 =item command_close_bidi_pipe ( PID, PIPE_IN, PIPE_OUT [, CTX] )
401
402 Close the C<PIPE_IN> and C<PIPE_OUT> as returned from C<command_bidi_pipe()>,
403 checking whether the command finished successfully. The optional C<CTX>
404 argument is required if you want to see the command name in the error message,
405 and it is the fourth value returned by C<command_bidi_pipe()>.  The call idiom
406 is:
407
408         my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check');
409         print "000000000\n" $out;
410         while (<$in>) { ... }
411         $r->command_close_bidi_pipe($pid, $in, $out, $ctx);
412
413 Note that you should not rely on whatever actually is in C<CTX>;
414 currently it is simply the command name but in future the context might
415 have more complicated structure.
416
417 =cut
418
419 sub command_close_bidi_pipe {
420         my ($pid, $in, $out, $ctx) = @_;
421         foreach my $fh ($in, $out) {
422                 unless (close $fh) {
423                         if ($!) {
424                                 carp "error closing pipe: $!";
425                         } elsif ($? >> 8) {
426                                 throw Git::Error::Command($ctx, $? >>8);
427                         }
428                 }
429         }
430
431         waitpid $pid, 0;
432
433         if ($? >> 8) {
434                 throw Git::Error::Command($ctx, $? >>8);
435         }
436 }
437
438
439 =item command_noisy ( COMMAND [, ARGUMENTS... ] )
440
441 Execute the given C<COMMAND> in the same way as command() does but do not
442 capture the command output - the standard output is not redirected and goes
443 to the standard output of the caller application.
444
445 While the method is called command_noisy(), you might want to as well use
446 it for the most silent Git commands which you know will never pollute your
447 stdout but you want to avoid the overhead of the pipe setup when calling them.
448
449 The function returns only after the command has finished running.
450
451 =cut
452
453 sub command_noisy {
454         my ($self, $cmd, @args) = _maybe_self(@_);
455         _check_valid_cmd($cmd);
456
457         my $pid = fork;
458         if (not defined $pid) {
459                 throw Error::Simple("fork failed: $!");
460         } elsif ($pid == 0) {
461                 _cmd_exec($self, $cmd, @args);
462         }
463         if (waitpid($pid, 0) > 0 and $?>>8 != 0) {
464                 throw Git::Error::Command(join(' ', $cmd, @args), $? >> 8);
465         }
466 }
467
468
469 =item version ()
470
471 Return the Git version in use.
472
473 =cut
474
475 sub version {
476         my $verstr = command_oneline('--version');
477         $verstr =~ s/^git version //;
478         $verstr;
479 }
480
481
482 =item exec_path ()
483
484 Return path to the Git sub-command executables (the same as
485 C<git --exec-path>). Useful mostly only internally.
486
487 =cut
488
489 sub exec_path { command_oneline('--exec-path') }
490
491
492 =item repo_path ()
493
494 Return path to the git repository. Must be called on a repository instance.
495
496 =cut
497
498 sub repo_path { $_[0]->{opts}->{Repository} }
499
500
501 =item wc_path ()
502
503 Return path to the working copy. Must be called on a repository instance.
504
505 =cut
506
507 sub wc_path { $_[0]->{opts}->{WorkingCopy} }
508
509
510 =item wc_subdir ()
511
512 Return path to the subdirectory inside of a working copy. Must be called
513 on a repository instance.
514
515 =cut
516
517 sub wc_subdir { $_[0]->{opts}->{WorkingSubdir} ||= '' }
518
519
520 =item wc_chdir ( SUBDIR )
521
522 Change the working copy subdirectory to work within. The C<SUBDIR> is
523 relative to the working copy root directory (not the current subdirectory).
524 Must be called on a repository instance attached to a working copy
525 and the directory must exist.
526
527 =cut
528
529 sub wc_chdir {
530         my ($self, $subdir) = @_;
531         $self->wc_path()
532                 or throw Error::Simple("bare repository");
533
534         -d $self->wc_path().'/'.$subdir
535                 or throw Error::Simple("subdir not found: $!");
536         # Of course we will not "hold" the subdirectory so anyone
537         # can delete it now and we will never know. But at least we tried.
538
539         $self->{opts}->{WorkingSubdir} = $subdir;
540 }
541
542
543 =item config ( VARIABLE )
544
545 Retrieve the configuration C<VARIABLE> in the same manner as C<config>
546 does. In scalar context requires the variable to be set only one time
547 (exception is thrown otherwise), in array context returns allows the
548 variable to be set multiple times and returns all the values.
549
550 This currently wraps command('config') so it is not so fast.
551
552 =cut
553
554 sub config {
555         my ($self, $var) = _maybe_self(@_);
556
557         try {
558                 my @cmd = ('config');
559                 unshift @cmd, $self if $self;
560                 if (wantarray) {
561                         return command(@cmd, '--get-all', $var);
562                 } else {
563                         return command_oneline(@cmd, '--get', $var);
564                 }
565         } catch Git::Error::Command with {
566                 my $E = shift;
567                 if ($E->value() == 1) {
568                         # Key not found.
569                         return;
570                 } else {
571                         throw $E;
572                 }
573         };
574 }
575
576
577 =item config_bool ( VARIABLE )
578
579 Retrieve the bool configuration C<VARIABLE>. The return value
580 is usable as a boolean in perl (and C<undef> if it's not defined,
581 of course).
582
583 This currently wraps command('config') so it is not so fast.
584
585 =cut
586
587 sub config_bool {
588         my ($self, $var) = _maybe_self(@_);
589
590         try {
591                 my @cmd = ('config', '--bool', '--get', $var);
592                 unshift @cmd, $self if $self;
593                 my $val = command_oneline(@cmd);
594                 return undef unless defined $val;
595                 return $val eq 'true';
596         } catch Git::Error::Command with {
597                 my $E = shift;
598                 if ($E->value() == 1) {
599                         # Key not found.
600                         return undef;
601                 } else {
602                         throw $E;
603                 }
604         };
605 }
606
607 =item config_int ( VARIABLE )
608
609 Retrieve the integer configuration C<VARIABLE>. The return value
610 is simple decimal number.  An optional value suffix of 'k', 'm',
611 or 'g' in the config file will cause the value to be multiplied
612 by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
613 It would return C<undef> if configuration variable is not defined,
614
615 This currently wraps command('config') so it is not so fast.
616
617 =cut
618
619 sub config_int {
620         my ($self, $var) = _maybe_self(@_);
621
622         try {
623                 my @cmd = ('config', '--int', '--get', $var);
624                 unshift @cmd, $self if $self;
625                 return command_oneline(@cmd);
626         } catch Git::Error::Command with {
627                 my $E = shift;
628                 if ($E->value() == 1) {
629                         # Key not found.
630                         return undef;
631                 } else {
632                         throw $E;
633                 }
634         };
635 }
636
637 =item get_colorbool ( NAME )
638
639 Finds if color should be used for NAMEd operation from the configuration,
640 and returns boolean (true for "use color", false for "do not use color").
641
642 =cut
643
644 sub get_colorbool {
645         my ($self, $var) = @_;
646         my $stdout_to_tty = (-t STDOUT) ? "true" : "false";
647         my $use_color = $self->command_oneline('config', '--get-colorbool',
648                                                $var, $stdout_to_tty);
649         return ($use_color eq 'true');
650 }
651
652 =item get_color ( SLOT, COLOR )
653
654 Finds color for SLOT from the configuration, while defaulting to COLOR,
655 and returns the ANSI color escape sequence:
656
657         print $repo->get_color("color.interactive.prompt", "underline blue white");
658         print "some text";
659         print $repo->get_color("", "normal");
660
661 =cut
662
663 sub get_color {
664         my ($self, $slot, $default) = @_;
665         my $color = $self->command_oneline('config', '--get-color', $slot, $default);
666         if (!defined $color) {
667                 $color = "";
668         }
669         return $color;
670 }
671
672 =item remote_refs ( REPOSITORY [, GROUPS [, REFGLOBS ] ] )
673
674 This function returns a hashref of refs stored in a given remote repository.
675 The hash is in the format C<refname =\> hash>. For tags, the C<refname> entry
676 contains the tag object while a C<refname^{}> entry gives the tagged objects.
677
678 C<REPOSITORY> has the same meaning as the appropriate C<git-ls-remote>
679 argument; either an URL or a remote name (if called on a repository instance).
680 C<GROUPS> is an optional arrayref that can contain 'tags' to return all the
681 tags and/or 'heads' to return all the heads. C<REFGLOB> is an optional array
682 of strings containing a shell-like glob to further limit the refs returned in
683 the hash; the meaning is again the same as the appropriate C<git-ls-remote>
684 argument.
685
686 This function may or may not be called on a repository instance. In the former
687 case, remote names as defined in the repository are recognized as repository
688 specifiers.
689
690 =cut
691
692 sub remote_refs {
693         my ($self, $repo, $groups, $refglobs) = _maybe_self(@_);
694         my @args;
695         if (ref $groups eq 'ARRAY') {
696                 foreach (@$groups) {
697                         if ($_ eq 'heads') {
698                                 push (@args, '--heads');
699                         } elsif ($_ eq 'tags') {
700                                 push (@args, '--tags');
701                         } else {
702                                 # Ignore unknown groups for future
703                                 # compatibility
704                         }
705                 }
706         }
707         push (@args, $repo);
708         if (ref $refglobs eq 'ARRAY') {
709                 push (@args, @$refglobs);
710         }
711
712         my @self = $self ? ($self) : (); # Ultra trickery
713         my ($fh, $ctx) = Git::command_output_pipe(@self, 'ls-remote', @args);
714         my %refs;
715         while (<$fh>) {
716                 chomp;
717                 my ($hash, $ref) = split(/\t/, $_, 2);
718                 $refs{$ref} = $hash;
719         }
720         Git::command_close_pipe(@self, $fh, $ctx);
721         return \%refs;
722 }
723
724
725 =item ident ( TYPE | IDENTSTR )
726
727 =item ident_person ( TYPE | IDENTSTR | IDENTARRAY )
728
729 This suite of functions retrieves and parses ident information, as stored
730 in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus
731 C<TYPE> can be either I<author> or I<committer>; case is insignificant).
732
733 The C<ident> method retrieves the ident information from C<git-var>
734 and either returns it as a scalar string or as an array with the fields parsed.
735 Alternatively, it can take a prepared ident string (e.g. from the commit
736 object) and just parse it.
737
738 C<ident_person> returns the person part of the ident - name and email;
739 it can take the same arguments as C<ident> or the array returned by C<ident>.
740
741 The synopsis is like:
742
743         my ($name, $email, $time_tz) = ident('author');
744         "$name <$email>" eq ident_person('author');
745         "$name <$email>" eq ident_person($name);
746         $time_tz =~ /^\d+ [+-]\d{4}$/;
747
748 =cut
749
750 sub ident {
751         my ($self, $type) = _maybe_self(@_);
752         my $identstr;
753         if (lc $type eq lc 'committer' or lc $type eq lc 'author') {
754                 my @cmd = ('var', 'GIT_'.uc($type).'_IDENT');
755                 unshift @cmd, $self if $self;
756                 $identstr = command_oneline(@cmd);
757         } else {
758                 $identstr = $type;
759         }
760         if (wantarray) {
761                 return $identstr =~ /^(.*) <(.*)> (\d+ [+-]\d{4})$/;
762         } else {
763                 return $identstr;
764         }
765 }
766
767 sub ident_person {
768         my ($self, @ident) = _maybe_self(@_);
769         $#ident == 0 and @ident = $self ? $self->ident($ident[0]) : ident($ident[0]);
770         return "$ident[0] <$ident[1]>";
771 }
772
773
774 =item hash_object ( TYPE, FILENAME )
775
776 Compute the SHA1 object id of the given C<FILENAME> considering it is
777 of the C<TYPE> object type (C<blob>, C<commit>, C<tree>).
778
779 The method can be called without any instance or on a specified Git repository,
780 it makes zero difference.
781
782 The function returns the SHA1 hash.
783
784 =cut
785
786 # TODO: Support for passing FILEHANDLE instead of FILENAME
787 sub hash_object {
788         my ($self, $type, $file) = _maybe_self(@_);
789         command_oneline('hash-object', '-t', $type, $file);
790 }
791
792
793 =item hash_and_insert_object ( FILENAME )
794
795 Compute the SHA1 object id of the given C<FILENAME> and add the object to the
796 object database.
797
798 The function returns the SHA1 hash.
799
800 =cut
801
802 # TODO: Support for passing FILEHANDLE instead of FILENAME
803 sub hash_and_insert_object {
804         my ($self, $filename) = @_;
805
806         carp "Bad filename \"$filename\"" if $filename =~ /[\r\n]/;
807
808         $self->_open_hash_and_insert_object_if_needed();
809         my ($in, $out) = ($self->{hash_object_in}, $self->{hash_object_out});
810
811         unless (print $out $filename, "\n") {
812                 $self->_close_hash_and_insert_object();
813                 throw Error::Simple("out pipe went bad");
814         }
815
816         chomp(my $hash = <$in>);
817         unless (defined($hash)) {
818                 $self->_close_hash_and_insert_object();
819                 throw Error::Simple("in pipe went bad");
820         }
821
822         return $hash;
823 }
824
825 sub _open_hash_and_insert_object_if_needed {
826         my ($self) = @_;
827
828         return if defined($self->{hash_object_pid});
829
830         ($self->{hash_object_pid}, $self->{hash_object_in},
831          $self->{hash_object_out}, $self->{hash_object_ctx}) =
832                 command_bidi_pipe(qw(hash-object -w --stdin-paths));
833 }
834
835 sub _close_hash_and_insert_object {
836         my ($self) = @_;
837
838         return unless defined($self->{hash_object_pid});
839
840         my @vars = map { 'hash_object_' . $_ } qw(pid in out ctx);
841
842         command_close_bidi_pipe($self->{@vars});
843         delete $self->{@vars};
844 }
845
846 =item cat_blob ( SHA1, FILEHANDLE )
847
848 Prints the contents of the blob identified by C<SHA1> to C<FILEHANDLE> and
849 returns the number of bytes printed.
850
851 =cut
852
853 sub cat_blob {
854         my ($self, $sha1, $fh) = @_;
855
856         $self->_open_cat_blob_if_needed();
857         my ($in, $out) = ($self->{cat_blob_in}, $self->{cat_blob_out});
858
859         unless (print $out $sha1, "\n") {
860                 $self->_close_cat_blob();
861                 throw Error::Simple("out pipe went bad");
862         }
863
864         my $description = <$in>;
865         if ($description =~ / missing$/) {
866                 carp "$sha1 doesn't exist in the repository";
867                 return -1;
868         }
869
870         if ($description !~ /^[0-9a-fA-F]{40} \S+ (\d+)$/) {
871                 carp "Unexpected result returned from git cat-file";
872                 return -1;
873         }
874
875         my $size = $1;
876
877         my $blob;
878         my $bytesRead = 0;
879
880         while (1) {
881                 my $bytesLeft = $size - $bytesRead;
882                 last unless $bytesLeft;
883
884                 my $bytesToRead = $bytesLeft < 1024 ? $bytesLeft : 1024;
885                 my $read = read($in, $blob, $bytesToRead, $bytesRead);
886                 unless (defined($read)) {
887                         $self->_close_cat_blob();
888                         throw Error::Simple("in pipe went bad");
889                 }
890
891                 $bytesRead += $read;
892         }
893
894         # Skip past the trailing newline.
895         my $newline;
896         my $read = read($in, $newline, 1);
897         unless (defined($read)) {
898                 $self->_close_cat_blob();
899                 throw Error::Simple("in pipe went bad");
900         }
901         unless ($read == 1 && $newline eq "\n") {
902                 $self->_close_cat_blob();
903                 throw Error::Simple("didn't find newline after blob");
904         }
905
906         unless (print $fh $blob) {
907                 $self->_close_cat_blob();
908                 throw Error::Simple("couldn't write to passed in filehandle");
909         }
910
911         return $size;
912 }
913
914 sub _open_cat_blob_if_needed {
915         my ($self) = @_;
916
917         return if defined($self->{cat_blob_pid});
918
919         ($self->{cat_blob_pid}, $self->{cat_blob_in},
920          $self->{cat_blob_out}, $self->{cat_blob_ctx}) =
921                 command_bidi_pipe(qw(cat-file --batch));
922 }
923
924 sub _close_cat_blob {
925         my ($self) = @_;
926
927         return unless defined($self->{cat_blob_pid});
928
929         my @vars = map { 'cat_blob_' . $_ } qw(pid in out ctx);
930
931         command_close_bidi_pipe($self->{@vars});
932         delete $self->{@vars};
933 }
934
935 =back
936
937 =head1 ERROR HANDLING
938
939 All functions are supposed to throw Perl exceptions in case of errors.
940 See the L<Error> module on how to catch those. Most exceptions are mere
941 L<Error::Simple> instances.
942
943 However, the C<command()>, C<command_oneline()> and C<command_noisy()>
944 functions suite can throw C<Git::Error::Command> exceptions as well: those are
945 thrown when the external command returns an error code and contain the error
946 code as well as access to the captured command's output. The exception class
947 provides the usual C<stringify> and C<value> (command's exit code) methods and
948 in addition also a C<cmd_output> method that returns either an array or a
949 string with the captured command output (depending on the original function
950 call context; C<command_noisy()> returns C<undef>) and $<cmdline> which
951 returns the command and its arguments (but without proper quoting).
952
953 Note that the C<command_*_pipe()> functions cannot throw this exception since
954 it has no idea whether the command failed or not. You will only find out
955 at the time you C<close> the pipe; if you want to have that automated,
956 use C<command_close_pipe()>, which can throw the exception.
957
958 =cut
959
960 {
961         package Git::Error::Command;
962
963         @Git::Error::Command::ISA = qw(Error);
964
965         sub new {
966                 my $self = shift;
967                 my $cmdline = '' . shift;
968                 my $value = 0 + shift;
969                 my $outputref = shift;
970                 my(@args) = ();
971
972                 local $Error::Depth = $Error::Depth + 1;
973
974                 push(@args, '-cmdline', $cmdline);
975                 push(@args, '-value', $value);
976                 push(@args, '-outputref', $outputref);
977
978                 $self->SUPER::new(-text => 'command returned error', @args);
979         }
980
981         sub stringify {
982                 my $self = shift;
983                 my $text = $self->SUPER::stringify;
984                 $self->cmdline() . ': ' . $text . ': ' . $self->value() . "\n";
985         }
986
987         sub cmdline {
988                 my $self = shift;
989                 $self->{'-cmdline'};
990         }
991
992         sub cmd_output {
993                 my $self = shift;
994                 my $ref = $self->{'-outputref'};
995                 defined $ref or undef;
996                 if (ref $ref eq 'ARRAY') {
997                         return @$ref;
998                 } else { # SCALAR
999                         return $$ref;
1000                 }
1001         }
1002 }
1003
1004 =over 4
1005
1006 =item git_cmd_try { CODE } ERRMSG
1007
1008 This magical statement will automatically catch any C<Git::Error::Command>
1009 exceptions thrown by C<CODE> and make your program die with C<ERRMSG>
1010 on its lips; the message will have %s substituted for the command line
1011 and %d for the exit status. This statement is useful mostly for producing
1012 more user-friendly error messages.
1013
1014 In case of no exception caught the statement returns C<CODE>'s return value.
1015
1016 Note that this is the only auto-exported function.
1017
1018 =cut
1019
1020 sub git_cmd_try(&$) {
1021         my ($code, $errmsg) = @_;
1022         my @result;
1023         my $err;
1024         my $array = wantarray;
1025         try {
1026                 if ($array) {
1027                         @result = &$code;
1028                 } else {
1029                         $result[0] = &$code;
1030                 }
1031         } catch Git::Error::Command with {
1032                 my $E = shift;
1033                 $err = $errmsg;
1034                 $err =~ s/\%s/$E->cmdline()/ge;
1035                 $err =~ s/\%d/$E->value()/ge;
1036                 # We can't croak here since Error.pm would mangle
1037                 # that to Error::Simple.
1038         };
1039         $err and croak $err;
1040         return $array ? @result : $result[0];
1041 }
1042
1043
1044 =back
1045
1046 =head1 COPYRIGHT
1047
1048 Copyright 2006 by Petr Baudis E<lt>pasky@suse.czE<gt>.
1049
1050 This module is free software; it may be used, copied, modified
1051 and distributed under the terms of the GNU General Public Licence,
1052 either version 2, or (at your option) any later version.
1053
1054 =cut
1055
1056
1057 # Take raw method argument list and return ($obj, @args) in case
1058 # the method was called upon an instance and (undef, @args) if
1059 # it was called directly.
1060 sub _maybe_self {
1061         # This breaks inheritance. Oh well.
1062         ref $_[0] eq 'Git' ? @_ : (undef, @_);
1063 }
1064
1065 # Check if the command id is something reasonable.
1066 sub _check_valid_cmd {
1067         my ($cmd) = @_;
1068         $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
1069 }
1070
1071 # Common backend for the pipe creators.
1072 sub _command_common_pipe {
1073         my $direction = shift;
1074         my ($self, @p) = _maybe_self(@_);
1075         my (%opts, $cmd, @args);
1076         if (ref $p[0]) {
1077                 ($cmd, @args) = @{shift @p};
1078                 %opts = ref $p[0] ? %{$p[0]} : @p;
1079         } else {
1080                 ($cmd, @args) = @p;
1081         }
1082         _check_valid_cmd($cmd);
1083
1084         my $fh;
1085         if ($^O eq 'MSWin32') {
1086                 # ActiveState Perl
1087                 #defined $opts{STDERR} and
1088                 #       warn 'ignoring STDERR option - running w/ ActiveState';
1089                 $direction eq '-|' or
1090                         die 'input pipe for ActiveState not implemented';
1091                 # the strange construction with *ACPIPE is just to
1092                 # explain the tie below that we want to bind to
1093                 # a handle class, not scalar. It is not known if
1094                 # it is something specific to ActiveState Perl or
1095                 # just a Perl quirk.
1096                 tie (*ACPIPE, 'Git::activestate_pipe', $cmd, @args);
1097                 $fh = *ACPIPE;
1098
1099         } else {
1100                 my $pid = open($fh, $direction);
1101                 if (not defined $pid) {
1102                         throw Error::Simple("open failed: $!");
1103                 } elsif ($pid == 0) {
1104                         if (defined $opts{STDERR}) {
1105                                 close STDERR;
1106                         }
1107                         if ($opts{STDERR}) {
1108                                 open (STDERR, '>&', $opts{STDERR})
1109                                         or die "dup failed: $!";
1110                         }
1111                         _cmd_exec($self, $cmd, @args);
1112                 }
1113         }
1114         return wantarray ? ($fh, join(' ', $cmd, @args)) : $fh;
1115 }
1116
1117 # When already in the subprocess, set up the appropriate state
1118 # for the given repository and execute the git command.
1119 sub _cmd_exec {
1120         my ($self, @args) = @_;
1121         if ($self) {
1122                 $self->repo_path() and $ENV{'GIT_DIR'} = $self->repo_path();
1123                 $self->wc_path() and chdir($self->wc_path());
1124                 $self->wc_subdir() and chdir($self->wc_subdir());
1125         }
1126         _execv_git_cmd(@args);
1127         die qq[exec "@args" failed: $!];
1128 }
1129
1130 # Execute the given Git command ($_[0]) with arguments ($_[1..])
1131 # by searching for it at proper places.
1132 sub _execv_git_cmd { exec('git', @_); }
1133
1134 # Close pipe to a subprocess.
1135 sub _cmd_close {
1136         my ($fh, $ctx) = @_;
1137         if (not close $fh) {
1138                 if ($!) {
1139                         # It's just close, no point in fatalities
1140                         carp "error closing pipe: $!";
1141                 } elsif ($? >> 8) {
1142                         # The caller should pepper this.
1143                         throw Git::Error::Command($ctx, $? >> 8);
1144                 }
1145                 # else we might e.g. closed a live stream; the command
1146                 # dying of SIGPIPE would drive us here.
1147         }
1148 }
1149
1150
1151 sub DESTROY {
1152         my ($self) = @_;
1153         $self->_close_hash_and_insert_object();
1154         $self->_close_cat_blob();
1155 }
1156
1157
1158 # Pipe implementation for ActiveState Perl.
1159
1160 package Git::activestate_pipe;
1161 use strict;
1162
1163 sub TIEHANDLE {
1164         my ($class, @params) = @_;
1165         # FIXME: This is probably horrible idea and the thing will explode
1166         # at the moment you give it arguments that require some quoting,
1167         # but I have no ActiveState clue... --pasky
1168         # Let's just hope ActiveState Perl does at least the quoting
1169         # correctly.
1170         my @data = qx{git @params};
1171         bless { i => 0, data => \@data }, $class;
1172 }
1173
1174 sub READLINE {
1175         my $self = shift;
1176         if ($self->{i} >= scalar @{$self->{data}}) {
1177                 return undef;
1178         }
1179         my $i = $self->{i};
1180         if (wantarray) {
1181                 $self->{i} = $#{$self->{'data'}} + 1;
1182                 return splice(@{$self->{'data'}}, $i);
1183         }
1184         $self->{i} = $i + 1;
1185         return $self->{'data'}->[ $i ];
1186 }
1187
1188 sub CLOSE {
1189         my $self = shift;
1190         delete $self->{data};
1191         delete $self->{i};
1192 }
1193
1194 sub EOF {
1195         my $self = shift;
1196         return ($self->{i} >= scalar @{$self->{data}});
1197 }
1198
1199
1200 1; # Famous last words