1=head1 NAME 2 3Git - Perl interface to the Git version control system 4 5=cut 6 7 8package Git; 9 10use strict; 11 12 13BEGIN{ 14 15our($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=cut 43 44 45require Exporter; 46 47@ISA=qw(Exporter); 48 49@EXPORT=qw(git_cmd_try); 50 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); 55 56 57=head1 DESCRIPTION 58 59This module provides Perl scripts easy way to interface the Git version control 60system. The modules have an easy and well-tested way to call arbitrary Git 61commands; in the future, the interface will also provide specialized methods 62for doing easily operations which are not totally trivial to do over 63the generic command interface. 64 65While some commands can be executed outside of any context (e.g. 'version' 66or 'init-db'), most operations require a repository context, which in practice 67means 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 69called as methods of the object are then executed in the context of the 70repository. 71 72Part of the "repository state" is also information about path to the attached 73working copy (unless you work with a bare repository). You can also navigate 74inside of the working copy using the C<wc_chdir()> method. (Note that 75the repository object is self-contained and will not change working directory 76of your process.) 77 78TODO: In the future, we might also do 79 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(); 83 84Currently, the module merely wraps calls to external Git tools. In the future, 85it will provide a much faster way to interact with Git by linking directly 86to libgit. This should be completely opaque to the user, though (performance 87increate nonwithstanding). 88 89=cut 90 91 92use Carp qw(carp croak);# but croak is bad - throw instead 93use Error qw(:try); 94use Cwd qw(abs_path); 95 96} 97 98 99=head1 CONSTRUCTORS 100 101=over 4 102 103=item repository ( OPTIONS ) 104 105=item repository ( DIRECTORY ) 106 107=item repository () 108 109Construct a new repository object. 110C<OPTIONS> are passed in a hash like fashion, using key and value pairs. 111Possible options are: 112 113B<Repository> - Path to the Git repository. 114 115B<WorkingCopy> - Path to the associated working copy; not strictly required 116as many commands will happily crunch on a bare repository. 117 118B<WorkingSubdir> - Subdirectory in the working copy to work inside. 119Just left undefined if you do not want to limit the scope of operations. 120 121B<Directory> - Path to the Git working directory in its usual setup. 122The C<.git> directory is searched in the directory and all the parent 123directories; if found, C<WorkingCopy> is set to the directory containing 124it and C<Repository> to the C<.git> directory itself. If no C<.git> 125directory was found, the C<Directory> is assumed to be a bare repository, 126C<Repository> is set to point at it and C<WorkingCopy> is left undefined. 127If the C<$GIT_DIR> environment variable is set, things behave as expected 128as well. 129 130You should not use both C<Directory> and either of C<Repository> and 131C<WorkingCopy> - the results of that are undefined. 132 133Alternatively, a directory path may be passed as a single scalar argument 134to the constructor; it is equivalent to setting only the C<Directory> option 135field. 136 137Calling the constructor with no options whatsoever is equivalent to 138calling it with C<< Directory => '.' >>. In general, if you are building 139a standard porcelain command, simply doing C<< Git->repository() >> should 140do the right thing and setup the object to reflect exactly where the user 141is right now. 142 143=cut 144 145sub repository { 146my$class=shift; 147my@args=@_; 148my%opts= (); 149my$self; 150 151if(defined$args[0]) { 152if($#args%2!=1) { 153# Not a hash. 154$#args==0or throw Error::Simple("bad usage"); 155%opts= ( Directory =>$args[0] ); 156}else{ 157%opts=@args; 158} 159} 160 161if(not defined$opts{Repository}and not defined$opts{WorkingCopy}) { 162$opts{Directory} ||='.'; 163} 164 165if($opts{Directory}) { 166-d $opts{Directory}or throw Error::Simple("Directory not found:$!"); 167 168my$search= Git->repository(WorkingCopy =>$opts{Directory}); 169my$dir; 170try{ 171$dir=$search->command_oneline(['rev-parse','--git-dir'], 172 STDERR =>0); 173} catch Git::Error::Command with { 174$dir=undef; 175}; 176 177if($dir) { 178$dir=~ m#^/# or $dir = $opts{Directory} . '/' . $dir; 179$opts{Repository} =$dir; 180 181# If --git-dir went ok, this shouldn't die either. 182my$prefix=$search->command_oneline('rev-parse','--show-prefix'); 183$dir= abs_path($opts{Directory}) .'/'; 184if($prefix) { 185if(substr($dir, -length($prefix))ne$prefix) { 186 throw Error::Simple("rev-parse confused me -$dirdoes not have trailing$prefix"); 187} 188substr($dir, -length($prefix)) =''; 189} 190$opts{WorkingCopy} =$dir; 191$opts{WorkingSubdir} =$prefix; 192 193}else{ 194# A bare repository? Let's see... 195$dir=$opts{Directory}; 196 197unless(-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'); 200} 201my$search= Git->repository(Repository =>$dir); 202try{ 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'); 207} 208 209$opts{Repository} = abs_path($dir); 210} 211 212delete$opts{Directory}; 213} 214 215$self= { opts => \%opts}; 216bless$self,$class; 217} 218 219 220=back 221 222=head1 METHODS 223 224=over 4 225 226=item command ( COMMAND [, ARGUMENTS... ] ) 227 228=item command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 229 230Execute the given Git C<COMMAND> (specify it without the 'git-' 231prefix), optionally with the specified extra C<ARGUMENTS>. 232 233The second more elaborate form can be used if you want to further adjust 234the command execution. Currently, only one option is supported: 235 236B<STDERR> - How to deal with the command's error output. By default (C<undef>) 237it is delivered to the caller's C<STDERR>. A false value (0 or '') will cause 238it to be thrown away. If you want to process it, you can get it in a filehandle 239you specify, but you must be extremely careful; if the error output is not 240very short and you want to read it in the same process as where you called 241C<command()>, you are set up for a nice deadlock! 242 243The 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). 245 246In scalar context, it returns all the command output in a single string 247(verbatim). 248 249In array context, it returns an array containing lines printed to the 250command's stdout (without trailing newlines). 251 252In both cases, the command's stdin and stderr are the same as the caller's. 253 254=cut 255 256sub command { 257my($fh,$ctx) = command_output_pipe(@_); 258 259if(not defined wantarray) { 260# Nothing to pepper the possible exception with. 261 _cmd_close($fh,$ctx); 262 263}elsif(not wantarray) { 264local$/; 265my$text= <$fh>; 266try{ 267 _cmd_close($fh,$ctx); 268} catch Git::Error::Command with { 269# Pepper with the output: 270my$E=shift; 271$E->{'-outputref'} = \$text; 272 throw $E; 273}; 274return$text; 275 276}else{ 277my@lines= <$fh>; 278chomp@lines; 279try{ 280 _cmd_close($fh,$ctx); 281} catch Git::Error::Command with { 282my$E=shift; 283$E->{'-outputref'} = \@lines; 284 throw $E; 285}; 286return@lines; 287} 288} 289 290 291=item command_oneline ( COMMAND [, ARGUMENTS... ] ) 292 293=item command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 294 295Execute the given C<COMMAND> in the same way as command() 296does but always return a scalar string containing the first line 297of the command's standard output. 298 299=cut 300 301sub command_oneline { 302my($fh,$ctx) = command_output_pipe(@_); 303 304my$line= <$fh>; 305defined$lineand chomp$line; 306try{ 307 _cmd_close($fh,$ctx); 308} catch Git::Error::Command with { 309# Pepper with the output: 310my$E=shift; 311$E->{'-outputref'} = \$line; 312 throw $E; 313}; 314return$line; 315} 316 317 318=item command_output_pipe ( COMMAND [, ARGUMENTS... ] ) 319 320=item command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 321 322Execute the given C<COMMAND> in the same way as command() 323does but return a pipe filehandle from which the command output can be 324read. 325 326The function can return C<($pipe, $ctx)> in array context. 327See C<command_close_pipe()> for details. 328 329=cut 330 331sub command_output_pipe { 332 _command_common_pipe('-|',@_); 333} 334 335 336=item command_input_pipe ( COMMAND [, ARGUMENTS... ] ) 337 338=item command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 339 340Execute the given C<COMMAND> in the same way as command_output_pipe() 341does but return an input pipe filehandle instead; the command output 342is not captured. 343 344The function can return C<($pipe, $ctx)> in array context. 345See C<command_close_pipe()> for details. 346 347=cut 348 349sub command_input_pipe { 350 _command_common_pipe('|-',@_); 351} 352 353 354=item command_close_pipe ( PIPE [, CTX ] ) 355 356Close the C<PIPE> as returned from C<command_*_pipe()>, checking 357whether the command finished successfuly. The optional C<CTX> argument 358is required if you want to see the command name in the error message, 359and it is the second value returned by C<command_*_pipe()> when 360called in array context. The call idiom is: 361 362 my ($fh, $ctx) = $r->command_output_pipe('status'); 363 while (<$fh>) { ... } 364 $r->command_close_pipe($fh, $ctx); 365 366Note that you should not rely on whatever actually is in C<CTX>; 367currently it is simply the command name but in future the context might 368have more complicated structure. 369 370=cut 371 372sub command_close_pipe { 373my($self,$fh,$ctx) = _maybe_self(@_); 374$ctx||='<unknown>'; 375 _cmd_close($fh,$ctx); 376} 377 378 379=item command_noisy ( COMMAND [, ARGUMENTS... ] ) 380 381Execute the given C<COMMAND> in the same way as command() does but do not 382capture the command output - the standard output is not redirected and goes 383to the standard output of the caller application. 384 385While the method is called command_noisy(), you might want to as well use 386it for the most silent Git commands which you know will never pollute your 387stdout but you want to avoid the overhead of the pipe setup when calling them. 388 389The function returns only after the command has finished running. 390 391=cut 392 393sub command_noisy { 394my($self,$cmd,@args) = _maybe_self(@_); 395 _check_valid_cmd($cmd); 396 397my$pid=fork; 398if(not defined$pid) { 399 throw Error::Simple("fork failed:$!"); 400}elsif($pid==0) { 401 _cmd_exec($self,$cmd,@args); 402} 403if(waitpid($pid,0) >0and$?>>8!=0) { 404 throw Git::Error::Command(join(' ',$cmd,@args),$?>>8); 405} 406} 407 408 409=item version () 410 411Return the Git version in use. 412 413=cut 414 415sub version { 416my$verstr= command_oneline('--version'); 417$verstr=~s/^git version //; 418$verstr; 419} 420 421 422=item exec_path () 423 424Return path to the Git sub-command executables (the same as 425C<git --exec-path>). Useful mostly only internally. 426 427=cut 428 429sub exec_path { command_oneline('--exec-path') } 430 431 432=item repo_path () 433 434Return path to the git repository. Must be called on a repository instance. 435 436=cut 437 438sub repo_path {$_[0]->{opts}->{Repository} } 439 440 441=item wc_path () 442 443Return path to the working copy. Must be called on a repository instance. 444 445=cut 446 447sub wc_path {$_[0]->{opts}->{WorkingCopy} } 448 449 450=item wc_subdir () 451 452Return path to the subdirectory inside of a working copy. Must be called 453on a repository instance. 454 455=cut 456 457sub wc_subdir {$_[0]->{opts}->{WorkingSubdir} ||=''} 458 459 460=item wc_chdir ( SUBDIR ) 461 462Change the working copy subdirectory to work within. The C<SUBDIR> is 463relative to the working copy root directory (not the current subdirectory). 464Must be called on a repository instance attached to a working copy 465and the directory must exist. 466 467=cut 468 469sub wc_chdir { 470my($self,$subdir) =@_; 471$self->wc_path() 472or throw Error::Simple("bare repository"); 473 474-d $self->wc_path().'/'.$subdir 475or 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. 478 479$self->{opts}->{WorkingSubdir} =$subdir; 480} 481 482 483=item config ( VARIABLE ) 484 485Retrieve the configuration C<VARIABLE> in the same manner as C<repo-config> 486does. In scalar context requires the variable to be set only one time 487(exception is thrown otherwise), in array context returns allows the 488variable to be set multiple times and returns all the values. 489 490Must be called on a repository instance. 491 492This currently wraps command('repo-config') so it is not so fast. 493 494=cut 495 496sub config { 497my($self,$var) =@_; 498$self->repo_path() 499or throw Error::Simple("not a repository"); 500 501try{ 502if(wantarray) { 503return$self->command('repo-config','--get-all',$var); 504}else{ 505return$self->command_oneline('repo-config','--get',$var); 506} 507} catch Git::Error::Command with { 508my$E=shift; 509if($E->value() ==1) { 510# Key not found. 511returnundef; 512}else{ 513 throw $E; 514} 515}; 516} 517 518 519=item ident ( TYPE | IDENTSTR ) 520 521=item ident_person ( TYPE | IDENTSTR | IDENTARRAY ) 522 523This suite of functions retrieves and parses ident information, as stored 524in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus 525C<TYPE> can be either I<author> or I<committer>; case is insignificant). 526 527The C<ident> method retrieves the ident information from C<git-var> 528and either returns it as a scalar string or as an array with the fields parsed. 529Alternatively, it can take a prepared ident string (e.g. from the commit 530object) and just parse it. 531 532C<ident_person> returns the person part of the ident - name and email; 533it can take the same arguments as C<ident> or the array returned by C<ident>. 534 535The synopsis is like: 536 537 my ($name, $email, $time_tz) = ident('author'); 538 "$name <$email>" eq ident_person('author'); 539 "$name <$email>" eq ident_person($name); 540 $time_tz =~ /^\d+ [+-]\d{4}$/; 541 542Both methods must be called on a repository instance. 543 544=cut 545 546sub ident { 547my($self,$type) =@_; 548my$identstr; 549if(lc$typeeq lc'committer'or lc$typeeq lc'author') { 550$identstr=$self->command_oneline('var','GIT_'.uc($type).'_IDENT'); 551}else{ 552$identstr=$type; 553} 554if(wantarray) { 555return$identstr=~/^(.*) <(.*)> (\d+ [+-]\d{4})$/; 556}else{ 557return$identstr; 558} 559} 560 561sub ident_person { 562my($self,@ident) =@_; 563$#ident==0and@ident=$self->ident($ident[0]); 564return"$ident[0] <$ident[1]>"; 565} 566 567 568=item hash_object ( TYPE, FILENAME ) 569 570Compute the SHA1 object id of the given C<FILENAME> (or data waiting in 571C<FILEHANDLE>) considering it is of the C<TYPE> object type (C<blob>, 572C<commit>, C<tree>). 573 574The method can be called without any instance or on a specified Git repository, 575it makes zero difference. 576 577The function returns the SHA1 hash. 578 579=cut 580 581# TODO: Support for passing FILEHANDLE instead of FILENAME 582sub hash_object { 583my($self,$type,$file) = _maybe_self(@_); 584 command_oneline('hash-object','-t',$type,$file); 585} 586 587 588 589=back 590 591=head1 ERROR HANDLING 592 593All functions are supposed to throw Perl exceptions in case of errors. 594See the L<Error> module on how to catch those. Most exceptions are mere 595L<Error::Simple> instances. 596 597However, the C<command()>, C<command_oneline()> and C<command_noisy()> 598functions suite can throw C<Git::Error::Command> exceptions as well: those are 599thrown when the external command returns an error code and contain the error 600code as well as access to the captured command's output. The exception class 601provides the usual C<stringify> and C<value> (command's exit code) methods and 602in addition also a C<cmd_output> method that returns either an array or a 603string with the captured command output (depending on the original function 604call context; C<command_noisy()> returns C<undef>) and $<cmdline> which 605returns the command and its arguments (but without proper quoting). 606 607Note that the C<command_*_pipe()> functions cannot throw this exception since 608it has no idea whether the command failed or not. You will only find out 609at the time you C<close> the pipe; if you want to have that automated, 610use C<command_close_pipe()>, which can throw the exception. 611 612=cut 613 614{ 615package Git::Error::Command; 616 617@Git::Error::Command::ISA =qw(Error); 618 619sub new { 620my$self=shift; 621my$cmdline=''.shift; 622my$value=0+shift; 623my$outputref=shift; 624my(@args) = (); 625 626local$Error::Depth =$Error::Depth +1; 627 628push(@args,'-cmdline',$cmdline); 629push(@args,'-value',$value); 630push(@args,'-outputref',$outputref); 631 632$self->SUPER::new(-text =>'command returned error',@args); 633} 634 635sub stringify { 636my$self=shift; 637my$text=$self->SUPER::stringify; 638$self->cmdline() .': '.$text.': '.$self->value() ."\n"; 639} 640 641sub cmdline { 642my$self=shift; 643$self->{'-cmdline'}; 644} 645 646sub cmd_output { 647my$self=shift; 648my$ref=$self->{'-outputref'}; 649defined$refor undef; 650if(ref$refeq'ARRAY') { 651return@$ref; 652}else{# SCALAR 653return$$ref; 654} 655} 656} 657 658=over 4 659 660=item git_cmd_try { CODE } ERRMSG 661 662This magical statement will automatically catch any C<Git::Error::Command> 663exceptions thrown by C<CODE> and make your program die with C<ERRMSG> 664on its lips; the message will have %s substituted for the command line 665and %d for the exit status. This statement is useful mostly for producing 666more user-friendly error messages. 667 668In case of no exception caught the statement returns C<CODE>'s return value. 669 670Note that this is the only auto-exported function. 671 672=cut 673 674sub git_cmd_try(&$) { 675my($code,$errmsg) =@_; 676my@result; 677my$err; 678my$array=wantarray; 679try{ 680if($array) { 681@result= &$code; 682}else{ 683$result[0] = &$code; 684} 685} catch Git::Error::Command with { 686my$E=shift; 687$err=$errmsg; 688$err=~s/\%s/$E->cmdline()/ge; 689$err=~s/\%d/$E->value()/ge; 690# We can't croak here since Error.pm would mangle 691# that to Error::Simple. 692}; 693$errand croak $err; 694return$array?@result:$result[0]; 695} 696 697 698=back 699 700=head1 COPYRIGHT 701 702Copyright 2006 by Petr Baudis E<lt>pasky@suse.czE<gt>. 703 704This module is free software; it may be used, copied, modified 705and distributed under the terms of the GNU General Public Licence, 706either version 2, or (at your option) any later version. 707 708=cut 709 710 711# Take raw method argument list and return ($obj, @args) in case 712# the method was called upon an instance and (undef, @args) if 713# it was called directly. 714sub _maybe_self { 715# This breaks inheritance. Oh well. 716ref$_[0]eq'Git'?@_: (undef,@_); 717} 718 719# Check if the command id is something reasonable. 720sub _check_valid_cmd { 721my($cmd) =@_; 722$cmd=~/^[a-z0-9A-Z_-]+$/or throw Error::Simple("bad command:$cmd"); 723} 724 725# Common backend for the pipe creators. 726sub _command_common_pipe { 727my$direction=shift; 728my($self,@p) = _maybe_self(@_); 729my(%opts,$cmd,@args); 730if(ref$p[0]) { 731($cmd,@args) = @{shift@p}; 732%opts=ref$p[0] ? %{$p[0]} :@p; 733}else{ 734($cmd,@args) =@p; 735} 736 _check_valid_cmd($cmd); 737 738my$fh; 739if($^Oeq'##INSERT_ACTIVESTATE_STRING_HERE##') { 740# ActiveState Perl 741#defined $opts{STDERR} and 742# warn 'ignoring STDERR option - running w/ ActiveState'; 743$directioneq'-|'or 744die'input pipe for ActiveState not implemented'; 745 tie ($fh,'Git::activestate_pipe',$cmd,@args); 746 747}else{ 748my$pid=open($fh,$direction); 749if(not defined$pid) { 750 throw Error::Simple("open failed:$!"); 751}elsif($pid==0) { 752if(defined$opts{STDERR}) { 753close STDERR; 754} 755if($opts{STDERR}) { 756open(STDERR,'>&',$opts{STDERR}) 757or die"dup failed:$!"; 758} 759 _cmd_exec($self,$cmd,@args); 760} 761} 762returnwantarray? ($fh,join(' ',$cmd,@args)) :$fh; 763} 764 765# When already in the subprocess, set up the appropriate state 766# for the given repository and execute the git command. 767sub _cmd_exec { 768my($self,@args) =@_; 769if($self) { 770$self->repo_path()and$ENV{'GIT_DIR'} =$self->repo_path(); 771$self->wc_path()and chdir($self->wc_path()); 772$self->wc_subdir()and chdir($self->wc_subdir()); 773} 774 _execv_git_cmd(@args); 775die"exec failed:$!"; 776} 777 778# Execute the given Git command ($_[0]) with arguments ($_[1..]) 779# by searching for it at proper places. 780sub _execv_git_cmd {exec('git',@_); } 781 782# Close pipe to a subprocess. 783sub _cmd_close { 784my($fh,$ctx) =@_; 785if(not close$fh) { 786if($!) { 787# It's just close, no point in fatalities 788 carp "error closing pipe:$!"; 789}elsif($?>>8) { 790# The caller should pepper this. 791 throw Git::Error::Command($ctx,$?>>8); 792} 793# else we might e.g. closed a live stream; the command 794# dying of SIGPIPE would drive us here. 795} 796} 797 798 799sub DESTROY { } 800 801 802# Pipe implementation for ActiveState Perl. 803 804package Git::activestate_pipe; 805use strict; 806 807sub TIEHANDLE { 808my($class,@params) =@_; 809# FIXME: This is probably horrible idea and the thing will explode 810# at the moment you give it arguments that require some quoting, 811# but I have no ActiveState clue... --pasky 812my$cmdline=join" ",@params; 813my@data=qx{$cmdline}; 814bless{ i =>0, data => \@data},$class; 815} 816 817sub READLINE { 818my$self=shift; 819if($self->{i} >=scalar@{$self->{data}}) { 820returnundef; 821} 822return$self->{'data'}->[$self->{i}++ ]; 823} 824 825sub CLOSE { 826my$self=shift; 827delete$self->{data}; 828delete$self->{i}; 829} 830 831sub EOF { 832my$self=shift; 833return($self->{i} >=scalar@{$self->{data}}); 834} 835 836 8371;# Famous last words