1=head1 NAME 2 3Git - Perl interface to the Git version control system 4 5=cut 6 7 8package Git; 9 10use5.008; 11use strict; 12 13 14BEGIN{ 15 16our($VERSION,@ISA,@EXPORT,@EXPORT_OK); 17 18# Totally unstable API. 19$VERSION='0.01'; 20 21 22=head1 SYNOPSIS 23 24 use Git; 25 26 my $version = Git::command_oneline('version'); 27 28 git_cmd_try { Git::command_noisy('update-server-info') } 29 '%s failed w/ code %d'; 30 31 my $repo = Git->repository (Directory => '/srv/git/cogito.git'); 32 33 34 my @revs = $repo->command('rev-list', '--since=last monday', '--all'); 35 36 my ($fh, $c) = $repo->command_output_pipe('rev-list', '--since=last monday', '--all'); 37 my $lastrev = <$fh>; chomp $lastrev; 38 $repo->command_close_pipe($fh, $c); 39 40 my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ], 41 STDERR => 0 ); 42 43 my $sha1 = $repo->hash_and_insert_object('file.txt'); 44 my $tempfile = tempfile(); 45 my $size = $repo->cat_blob($sha1, $tempfile); 46 47=cut 48 49 50require Exporter; 51 52@ISA=qw(Exporter); 53 54@EXPORT=qw(git_cmd_try); 55 56# Methods which can be called as standalone functions as well: 57@EXPORT_OK=qw(command command_oneline command_noisy 58 command_output_pipe command_input_pipe command_close_pipe 59 command_bidi_pipe command_close_bidi_pipe 60 version exec_path html_path hash_object git_cmd_try 61 remote_refs prompt 62 get_tz_offset 63 temp_acquire temp_release temp_reset temp_path); 64 65 66=head1 DESCRIPTION 67 68This module provides Perl scripts easy way to interface the Git version control 69system. The modules have an easy and well-tested way to call arbitrary Git 70commands; in the future, the interface will also provide specialized methods 71for doing easily operations which are not totally trivial to do over 72the generic command interface. 73 74While some commands can be executed outside of any context (e.g. 'version' 75or 'init'), most operations require a repository context, which in practice 76means getting an instance of the Git object using the repository() constructor. 77(In the future, we will also get a new_repository() constructor.) All commands 78called as methods of the object are then executed in the context of the 79repository. 80 81Part of the "repository state" is also information about path to the attached 82working copy (unless you work with a bare repository). You can also navigate 83inside of the working copy using the C<wc_chdir()> method. (Note that 84the repository object is self-contained and will not change working directory 85of your process.) 86 87TODO: In the future, we might also do 88 89 my $remoterepo = $repo->remote_repository (Name => 'cogito', Branch => 'master'); 90 $remoterepo ||= Git->remote_repository ('http://git.or.cz/cogito.git/'); 91 my @refs = $remoterepo->refs(); 92 93Currently, the module merely wraps calls to external Git tools. In the future, 94it will provide a much faster way to interact with Git by linking directly 95to libgit. This should be completely opaque to the user, though (performance 96increase notwithstanding). 97 98=cut 99 100 101use Carp qw(carp croak);# but croak is bad - throw instead 102use Error qw(:try); 103use Cwd qw(abs_path cwd); 104use IPC::Open2 qw(open2); 105use Fcntl qw(SEEK_SET SEEK_CUR); 106use Time::Local qw(timegm); 107} 108 109 110=head1 CONSTRUCTORS 111 112=over 4 113 114=item repository ( OPTIONS ) 115 116=item repository ( DIRECTORY ) 117 118=item repository () 119 120Construct a new repository object. 121C<OPTIONS> are passed in a hash like fashion, using key and value pairs. 122Possible options are: 123 124B<Repository> - Path to the Git repository. 125 126B<WorkingCopy> - Path to the associated working copy; not strictly required 127as many commands will happily crunch on a bare repository. 128 129B<WorkingSubdir> - Subdirectory in the working copy to work inside. 130Just left undefined if you do not want to limit the scope of operations. 131 132B<Directory> - Path to the Git working directory in its usual setup. 133The C<.git> directory is searched in the directory and all the parent 134directories; if found, C<WorkingCopy> is set to the directory containing 135it and C<Repository> to the C<.git> directory itself. If no C<.git> 136directory was found, the C<Directory> is assumed to be a bare repository, 137C<Repository> is set to point at it and C<WorkingCopy> is left undefined. 138If the C<$GIT_DIR> environment variable is set, things behave as expected 139as well. 140 141You should not use both C<Directory> and either of C<Repository> and 142C<WorkingCopy> - the results of that are undefined. 143 144Alternatively, a directory path may be passed as a single scalar argument 145to the constructor; it is equivalent to setting only the C<Directory> option 146field. 147 148Calling the constructor with no options whatsoever is equivalent to 149calling it with C<< Directory => '.' >>. In general, if you are building 150a standard porcelain command, simply doing C<< Git->repository() >> should 151do the right thing and setup the object to reflect exactly where the user 152is right now. 153 154=cut 155 156sub repository { 157my$class=shift; 158my@args=@_; 159my%opts= (); 160my$self; 161 162if(defined$args[0]) { 163if($#args%2!=1) { 164# Not a hash. 165$#args==0or throw Error::Simple("bad usage"); 166%opts= ( Directory =>$args[0] ); 167}else{ 168%opts=@args; 169} 170} 171 172if(not defined$opts{Repository}and not defined$opts{WorkingCopy} 173and not defined$opts{Directory}) { 174$opts{Directory} ='.'; 175} 176 177if(defined$opts{Directory}) { 178-d $opts{Directory}or throw Error::Simple("Directory not found:$opts{Directory}$!"); 179 180my$search= Git->repository(WorkingCopy =>$opts{Directory}); 181my$dir; 182try{ 183$dir=$search->command_oneline(['rev-parse','--git-dir'], 184 STDERR =>0); 185} catch Git::Error::Command with { 186$dir=undef; 187}; 188 189if($dir) { 190$dir=~ m#^/# or $dir = $opts{Directory} . '/' . $dir; 191$opts{Repository} = abs_path($dir); 192 193# If --git-dir went ok, this shouldn't die either. 194my$prefix=$search->command_oneline('rev-parse','--show-prefix'); 195$dir= abs_path($opts{Directory}) .'/'; 196if($prefix) { 197if(substr($dir, -length($prefix))ne$prefix) { 198 throw Error::Simple("rev-parse confused me -$dirdoes not have trailing$prefix"); 199} 200substr($dir, -length($prefix)) =''; 201} 202$opts{WorkingCopy} =$dir; 203$opts{WorkingSubdir} =$prefix; 204 205}else{ 206# A bare repository? Let's see... 207$dir=$opts{Directory}; 208 209unless(-d "$dir/refs"and-d "$dir/objects"and-e "$dir/HEAD") { 210# Mimic git-rev-parse --git-dir error message: 211 throw Error::Simple("fatal: Not a git repository:$dir"); 212} 213my$search= Git->repository(Repository =>$dir); 214try{ 215$search->command('symbolic-ref','HEAD'); 216} catch Git::Error::Command with { 217# Mimic git-rev-parse --git-dir error message: 218 throw Error::Simple("fatal: Not a git repository:$dir"); 219} 220 221$opts{Repository} = abs_path($dir); 222} 223 224delete$opts{Directory}; 225} 226 227$self= { opts => \%opts}; 228bless$self,$class; 229} 230 231=back 232 233=head1 METHODS 234 235=over 4 236 237=item command ( COMMAND [, ARGUMENTS... ] ) 238 239=item command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 240 241Execute the given Git C<COMMAND> (specify it without the 'git-' 242prefix), optionally with the specified extra C<ARGUMENTS>. 243 244The second more elaborate form can be used if you want to further adjust 245the command execution. Currently, only one option is supported: 246 247B<STDERR> - How to deal with the command's error output. By default (C<undef>) 248it is delivered to the caller's C<STDERR>. A false value (0 or '') will cause 249it to be thrown away. If you want to process it, you can get it in a filehandle 250you specify, but you must be extremely careful; if the error output is not 251very short and you want to read it in the same process as where you called 252C<command()>, you are set up for a nice deadlock! 253 254The method can be called without any instance or on a specified Git repository 255(in that case the command will be run in the repository context). 256 257In scalar context, it returns all the command output in a single string 258(verbatim). 259 260In array context, it returns an array containing lines printed to the 261command's stdout (without trailing newlines). 262 263In both cases, the command's stdin and stderr are the same as the caller's. 264 265=cut 266 267sub command { 268my($fh,$ctx) = command_output_pipe(@_); 269 270if(not defined wantarray) { 271# Nothing to pepper the possible exception with. 272 _cmd_close($fh,$ctx); 273 274}elsif(not wantarray) { 275local$/; 276my$text= <$fh>; 277try{ 278 _cmd_close($fh,$ctx); 279} catch Git::Error::Command with { 280# Pepper with the output: 281my$E=shift; 282$E->{'-outputref'} = \$text; 283 throw $E; 284}; 285return$text; 286 287}else{ 288my@lines= <$fh>; 289defined and chompfor@lines; 290try{ 291 _cmd_close($fh,$ctx); 292} catch Git::Error::Command with { 293my$E=shift; 294$E->{'-outputref'} = \@lines; 295 throw $E; 296}; 297return@lines; 298} 299} 300 301 302=item command_oneline ( COMMAND [, ARGUMENTS... ] ) 303 304=item command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 305 306Execute the given C<COMMAND> in the same way as command() 307does but always return a scalar string containing the first line 308of the command's standard output. 309 310=cut 311 312sub command_oneline { 313my($fh,$ctx) = command_output_pipe(@_); 314 315my$line= <$fh>; 316defined$lineand chomp$line; 317try{ 318 _cmd_close($fh,$ctx); 319} catch Git::Error::Command with { 320# Pepper with the output: 321my$E=shift; 322$E->{'-outputref'} = \$line; 323 throw $E; 324}; 325return$line; 326} 327 328 329=item command_output_pipe ( COMMAND [, ARGUMENTS... ] ) 330 331=item command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 332 333Execute the given C<COMMAND> in the same way as command() 334does but return a pipe filehandle from which the command output can be 335read. 336 337The function can return C<($pipe, $ctx)> in array context. 338See C<command_close_pipe()> for details. 339 340=cut 341 342sub command_output_pipe { 343 _command_common_pipe('-|',@_); 344} 345 346 347=item command_input_pipe ( COMMAND [, ARGUMENTS... ] ) 348 349=item command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } ) 350 351Execute the given C<COMMAND> in the same way as command_output_pipe() 352does but return an input pipe filehandle instead; the command output 353is not captured. 354 355The function can return C<($pipe, $ctx)> in array context. 356See C<command_close_pipe()> for details. 357 358=cut 359 360sub command_input_pipe { 361 _command_common_pipe('|-',@_); 362} 363 364 365=item command_close_pipe ( PIPE [, CTX ] ) 366 367Close the C<PIPE> as returned from C<command_*_pipe()>, checking 368whether the command finished successfully. The optional C<CTX> argument 369is required if you want to see the command name in the error message, 370and it is the second value returned by C<command_*_pipe()> when 371called in array context. The call idiom is: 372 373 my ($fh, $ctx) = $r->command_output_pipe('status'); 374 while (<$fh>) { ... } 375 $r->command_close_pipe($fh, $ctx); 376 377Note that you should not rely on whatever actually is in C<CTX>; 378currently it is simply the command name but in future the context might 379have more complicated structure. 380 381=cut 382 383sub command_close_pipe { 384my($self,$fh,$ctx) = _maybe_self(@_); 385$ctx||='<unknown>'; 386 _cmd_close($fh,$ctx); 387} 388 389=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] ) 390 391Execute the given C<COMMAND> in the same way as command_output_pipe() 392does but return both an input pipe filehandle and an output pipe filehandle. 393 394The function will return return C<($pid, $pipe_in, $pipe_out, $ctx)>. 395See C<command_close_bidi_pipe()> for details. 396 397=cut 398 399sub command_bidi_pipe { 400my($pid,$in,$out); 401my($self) = _maybe_self(@_); 402local%ENV=%ENV; 403my$cwd_save=undef; 404if($self) { 405shift; 406$cwd_save= cwd(); 407 _setup_git_cmd_env($self); 408} 409$pid= open2($in,$out,'git',@_); 410chdir($cwd_save)if$cwd_save; 411return($pid,$in,$out,join(' ',@_)); 412} 413 414=item command_close_bidi_pipe ( PID, PIPE_IN, PIPE_OUT [, CTX] ) 415 416Close the C<PIPE_IN> and C<PIPE_OUT> as returned from C<command_bidi_pipe()>, 417checking whether the command finished successfully. The optional C<CTX> 418argument is required if you want to see the command name in the error message, 419and it is the fourth value returned by C<command_bidi_pipe()>. The call idiom 420is: 421 422 my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check'); 423 print "000000000\n" $out; 424 while (<$in>) { ... } 425 $r->command_close_bidi_pipe($pid, $in, $out, $ctx); 426 427Note that you should not rely on whatever actually is in C<CTX>; 428currently it is simply the command name but in future the context might 429have more complicated structure. 430 431=cut 432 433sub command_close_bidi_pipe { 434local$?; 435my($pid,$in,$out,$ctx) =@_; 436foreachmy$fh($in,$out) { 437unless(close$fh) { 438if($!) { 439 carp "error closing pipe:$!"; 440}elsif($?>>8) { 441 throw Git::Error::Command($ctx,$?>>8); 442} 443} 444} 445 446waitpid$pid,0; 447 448if($?>>8) { 449 throw Git::Error::Command($ctx,$?>>8); 450} 451} 452 453 454=item command_noisy ( COMMAND [, ARGUMENTS... ] ) 455 456Execute the given C<COMMAND> in the same way as command() does but do not 457capture the command output - the standard output is not redirected and goes 458to the standard output of the caller application. 459 460While the method is called command_noisy(), you might want to as well use 461it for the most silent Git commands which you know will never pollute your 462stdout but you want to avoid the overhead of the pipe setup when calling them. 463 464The function returns only after the command has finished running. 465 466=cut 467 468sub command_noisy { 469my($self,$cmd,@args) = _maybe_self(@_); 470 _check_valid_cmd($cmd); 471 472my$pid=fork; 473if(not defined$pid) { 474 throw Error::Simple("fork failed:$!"); 475}elsif($pid==0) { 476 _cmd_exec($self,$cmd,@args); 477} 478if(waitpid($pid,0) >0and$?>>8!=0) { 479 throw Git::Error::Command(join(' ',$cmd,@args),$?>>8); 480} 481} 482 483 484=item version () 485 486Return the Git version in use. 487 488=cut 489 490sub version { 491my$verstr= command_oneline('--version'); 492$verstr=~s/^git version //; 493$verstr; 494} 495 496 497=item exec_path () 498 499Return path to the Git sub-command executables (the same as 500C<git --exec-path>). Useful mostly only internally. 501 502=cut 503 504sub exec_path { command_oneline('--exec-path') } 505 506 507=item html_path () 508 509Return path to the Git html documentation (the same as 510C<git --html-path>). Useful mostly only internally. 511 512=cut 513 514sub html_path { command_oneline('--html-path') } 515 516 517=item get_tz_offset ( TIME ) 518 519Return the time zone offset from GMT in the form +/-HHMM where HH is 520the number of hours from GMT and MM is the number of minutes. This is 521the equivalent of what strftime("%z", ...) would provide on a GNU 522platform. 523 524If TIME is not supplied, the current local time is used. 525 526=cut 527 528sub get_tz_offset { 529# some systmes don't handle or mishandle %z, so be creative. 530my$t=shift||time; 531my$gm= timegm(localtime($t)); 532my$sign=qw( + + - )[$gm<=>$t]; 533returnsprintf("%s%02d%02d",$sign, (gmtime(abs($t-$gm)))[2,1]); 534} 535 536 537=item prompt ( PROMPT , ISPASSWORD ) 538 539Query user C<PROMPT> and return answer from user. 540 541Honours GIT_ASKPASS and SSH_ASKPASS environment variables for querying 542the user. If no *_ASKPASS variable is set or an error occoured, 543the terminal is tried as a fallback. 544If C<ISPASSWORD> is set and true, the terminal disables echo. 545 546=cut 547 548sub prompt { 549my($prompt,$isPassword) =@_; 550my$ret; 551if(exists$ENV{'GIT_ASKPASS'}) { 552$ret= _prompt($ENV{'GIT_ASKPASS'},$prompt); 553} 554if(!defined$ret&&exists$ENV{'SSH_ASKPASS'}) { 555$ret= _prompt($ENV{'SSH_ASKPASS'},$prompt); 556} 557if(!defined$ret) { 558print STDERR $prompt; 559 STDERR->flush; 560if(defined$isPassword&&$isPassword) { 561require Term::ReadKey; 562 Term::ReadKey::ReadMode('noecho'); 563$ret=''; 564while(defined(my$key= Term::ReadKey::ReadKey(0))) { 565last if$key=~/[\012\015]/;# \n\r 566$ret.=$key; 567} 568 Term::ReadKey::ReadMode('restore'); 569print STDERR "\n"; 570 STDERR->flush; 571}else{ 572chomp($ret= <STDIN>); 573} 574} 575return$ret; 576} 577 578sub _prompt { 579my($askpass,$prompt) =@_; 580return unlesslength$askpass; 581$prompt=~s/\n/ /g; 582my$ret; 583open my$fh,"-|",$askpass,$promptorreturn; 584$ret= <$fh>; 585$ret=~s/[\015\012]//g;# strip \r\n, chomp does not work on all systems (i.e. windows) as expected 586close($fh); 587return$ret; 588} 589 590=item repo_path () 591 592Return path to the git repository. Must be called on a repository instance. 593 594=cut 595 596sub repo_path {$_[0]->{opts}->{Repository} } 597 598 599=item wc_path () 600 601Return path to the working copy. Must be called on a repository instance. 602 603=cut 604 605sub wc_path {$_[0]->{opts}->{WorkingCopy} } 606 607 608=item wc_subdir () 609 610Return path to the subdirectory inside of a working copy. Must be called 611on a repository instance. 612 613=cut 614 615sub wc_subdir {$_[0]->{opts}->{WorkingSubdir} ||=''} 616 617 618=item wc_chdir ( SUBDIR ) 619 620Change the working copy subdirectory to work within. The C<SUBDIR> is 621relative to the working copy root directory (not the current subdirectory). 622Must be called on a repository instance attached to a working copy 623and the directory must exist. 624 625=cut 626 627sub wc_chdir { 628my($self,$subdir) =@_; 629$self->wc_path() 630or throw Error::Simple("bare repository"); 631 632-d $self->wc_path().'/'.$subdir 633or throw Error::Simple("subdir not found:$subdir$!"); 634# Of course we will not "hold" the subdirectory so anyone 635# can delete it now and we will never know. But at least we tried. 636 637$self->{opts}->{WorkingSubdir} =$subdir; 638} 639 640 641=item config ( VARIABLE ) 642 643Retrieve the configuration C<VARIABLE> in the same manner as C<config> 644does. In scalar context requires the variable to be set only one time 645(exception is thrown otherwise), in array context returns allows the 646variable to be set multiple times and returns all the values. 647 648=cut 649 650sub config { 651return _config_common({},@_); 652} 653 654 655=item config_bool ( VARIABLE ) 656 657Retrieve the bool configuration C<VARIABLE>. The return value 658is usable as a boolean in perl (and C<undef> if it's not defined, 659of course). 660 661=cut 662 663sub config_bool { 664my$val=scalar _config_common({'kind'=>'--bool'},@_); 665 666# Do not rewrite this as return (defined $val && $val eq 'true') 667# as some callers do care what kind of falsehood they receive. 668if(!defined$val) { 669returnundef; 670}else{ 671return$valeq'true'; 672} 673} 674 675 676=item config_path ( VARIABLE ) 677 678Retrieve the path configuration C<VARIABLE>. The return value 679is an expanded path or C<undef> if it's not defined. 680 681=cut 682 683sub config_path { 684return _config_common({'kind'=>'--path'},@_); 685} 686 687 688=item config_int ( VARIABLE ) 689 690Retrieve the integer configuration C<VARIABLE>. The return value 691is simple decimal number. An optional value suffix of 'k', 'm', 692or 'g' in the config file will cause the value to be multiplied 693by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output. 694It would return C<undef> if configuration variable is not defined, 695 696=cut 697 698sub config_int { 699returnscalar _config_common({'kind'=>'--int'},@_); 700} 701 702# Common subroutine to implement bulk of what the config* family of methods 703# do. This curently wraps command('config') so it is not so fast. 704sub _config_common { 705my($opts) =shift@_; 706my($self,$var) = _maybe_self(@_); 707 708try{ 709my@cmd= ('config',$opts->{'kind'} ?$opts->{'kind'} : ()); 710unshift@cmd,$selfif$self; 711if(wantarray) { 712return command(@cmd,'--get-all',$var); 713}else{ 714return command_oneline(@cmd,'--get',$var); 715} 716} catch Git::Error::Command with { 717my$E=shift; 718if($E->value() ==1) { 719# Key not found. 720return; 721}else{ 722 throw $E; 723} 724}; 725} 726 727=item get_colorbool ( NAME ) 728 729Finds if color should be used for NAMEd operation from the configuration, 730and returns boolean (true for "use color", false for "do not use color"). 731 732=cut 733 734sub get_colorbool { 735my($self,$var) =@_; 736my$stdout_to_tty= (-t STDOUT) ?"true":"false"; 737my$use_color=$self->command_oneline('config','--get-colorbool', 738$var,$stdout_to_tty); 739return($use_coloreq'true'); 740} 741 742=item get_color ( SLOT, COLOR ) 743 744Finds color for SLOT from the configuration, while defaulting to COLOR, 745and returns the ANSI color escape sequence: 746 747 print $repo->get_color("color.interactive.prompt", "underline blue white"); 748 print "some text"; 749 print $repo->get_color("", "normal"); 750 751=cut 752 753sub get_color { 754my($self,$slot,$default) =@_; 755my$color=$self->command_oneline('config','--get-color',$slot,$default); 756if(!defined$color) { 757$color=""; 758} 759return$color; 760} 761 762=item remote_refs ( REPOSITORY [, GROUPS [, REFGLOBS ] ] ) 763 764This function returns a hashref of refs stored in a given remote repository. 765The hash is in the format C<refname =\> hash>. For tags, the C<refname> entry 766contains the tag object while a C<refname^{}> entry gives the tagged objects. 767 768C<REPOSITORY> has the same meaning as the appropriate C<git-ls-remote> 769argument; either a URL or a remote name (if called on a repository instance). 770C<GROUPS> is an optional arrayref that can contain 'tags' to return all the 771tags and/or 'heads' to return all the heads. C<REFGLOB> is an optional array 772of strings containing a shell-like glob to further limit the refs returned in 773the hash; the meaning is again the same as the appropriate C<git-ls-remote> 774argument. 775 776This function may or may not be called on a repository instance. In the former 777case, remote names as defined in the repository are recognized as repository 778specifiers. 779 780=cut 781 782sub remote_refs { 783my($self,$repo,$groups,$refglobs) = _maybe_self(@_); 784my@args; 785if(ref$groupseq'ARRAY') { 786foreach(@$groups) { 787if($_eq'heads') { 788push(@args,'--heads'); 789}elsif($_eq'tags') { 790push(@args,'--tags'); 791}else{ 792# Ignore unknown groups for future 793# compatibility 794} 795} 796} 797push(@args,$repo); 798if(ref$refglobseq'ARRAY') { 799push(@args,@$refglobs); 800} 801 802my@self=$self? ($self) : ();# Ultra trickery 803my($fh,$ctx) = Git::command_output_pipe(@self,'ls-remote',@args); 804my%refs; 805while(<$fh>) { 806chomp; 807my($hash,$ref) =split(/\t/,$_,2); 808$refs{$ref} =$hash; 809} 810 Git::command_close_pipe(@self,$fh,$ctx); 811return \%refs; 812} 813 814 815=item ident ( TYPE | IDENTSTR ) 816 817=item ident_person ( TYPE | IDENTSTR | IDENTARRAY ) 818 819This suite of functions retrieves and parses ident information, as stored 820in the commit and tag objects or produced by C<var GIT_type_IDENT> (thus 821C<TYPE> can be either I<author> or I<committer>; case is insignificant). 822 823The C<ident> method retrieves the ident information from C<git var> 824and either returns it as a scalar string or as an array with the fields parsed. 825Alternatively, it can take a prepared ident string (e.g. from the commit 826object) and just parse it. 827 828C<ident_person> returns the person part of the ident - name and email; 829it can take the same arguments as C<ident> or the array returned by C<ident>. 830 831The synopsis is like: 832 833 my ($name, $email, $time_tz) = ident('author'); 834 "$name <$email>" eq ident_person('author'); 835 "$name <$email>" eq ident_person($name); 836 $time_tz =~ /^\d+ [+-]\d{4}$/; 837 838=cut 839 840sub ident { 841my($self,$type) = _maybe_self(@_); 842my$identstr; 843if(lc$typeeq lc'committer'or lc$typeeq lc'author') { 844my@cmd= ('var','GIT_'.uc($type).'_IDENT'); 845unshift@cmd,$selfif$self; 846$identstr= command_oneline(@cmd); 847}else{ 848$identstr=$type; 849} 850if(wantarray) { 851return$identstr=~/^(.*) <(.*)> (\d+ [+-]\d{4})$/; 852}else{ 853return$identstr; 854} 855} 856 857sub ident_person { 858my($self,@ident) = _maybe_self(@_); 859$#ident==0and@ident=$self?$self->ident($ident[0]) : ident($ident[0]); 860return"$ident[0] <$ident[1]>"; 861} 862 863 864=item hash_object ( TYPE, FILENAME ) 865 866Compute the SHA1 object id of the given C<FILENAME> considering it is 867of the C<TYPE> object type (C<blob>, C<commit>, C<tree>). 868 869The method can be called without any instance or on a specified Git repository, 870it makes zero difference. 871 872The function returns the SHA1 hash. 873 874=cut 875 876# TODO: Support for passing FILEHANDLE instead of FILENAME 877sub hash_object { 878my($self,$type,$file) = _maybe_self(@_); 879 command_oneline('hash-object','-t',$type,$file); 880} 881 882 883=item hash_and_insert_object ( FILENAME ) 884 885Compute the SHA1 object id of the given C<FILENAME> and add the object to the 886object database. 887 888The function returns the SHA1 hash. 889 890=cut 891 892# TODO: Support for passing FILEHANDLE instead of FILENAME 893sub hash_and_insert_object { 894my($self,$filename) =@_; 895 896 carp "Bad filename\"$filename\""if$filename=~/[\r\n]/; 897 898$self->_open_hash_and_insert_object_if_needed(); 899my($in,$out) = ($self->{hash_object_in},$self->{hash_object_out}); 900 901unless(print$out $filename,"\n") { 902$self->_close_hash_and_insert_object(); 903 throw Error::Simple("out pipe went bad"); 904} 905 906chomp(my$hash= <$in>); 907unless(defined($hash)) { 908$self->_close_hash_and_insert_object(); 909 throw Error::Simple("in pipe went bad"); 910} 911 912return$hash; 913} 914 915sub _open_hash_and_insert_object_if_needed { 916my($self) =@_; 917 918return ifdefined($self->{hash_object_pid}); 919 920($self->{hash_object_pid},$self->{hash_object_in}, 921$self->{hash_object_out},$self->{hash_object_ctx}) = 922$self->command_bidi_pipe(qw(hash-object -w --stdin-paths --no-filters)); 923} 924 925sub _close_hash_and_insert_object { 926my($self) =@_; 927 928return unlessdefined($self->{hash_object_pid}); 929 930my@vars=map{'hash_object_'.$_}qw(pid in out ctx); 931 932 command_close_bidi_pipe(@$self{@vars}); 933delete@$self{@vars}; 934} 935 936=item cat_blob ( SHA1, FILEHANDLE ) 937 938Prints the contents of the blob identified by C<SHA1> to C<FILEHANDLE> and 939returns the number of bytes printed. 940 941=cut 942 943sub cat_blob { 944my($self,$sha1,$fh) =@_; 945 946$self->_open_cat_blob_if_needed(); 947my($in,$out) = ($self->{cat_blob_in},$self->{cat_blob_out}); 948 949unless(print$out $sha1,"\n") { 950$self->_close_cat_blob(); 951 throw Error::Simple("out pipe went bad"); 952} 953 954my$description= <$in>; 955if($description=~/ missing$/) { 956 carp "$sha1doesn't exist in the repository"; 957return-1; 958} 959 960if($description!~/^[0-9a-fA-F]{40} \S+ (\d+)$/) { 961 carp "Unexpected result returned from git cat-file"; 962return-1; 963} 964 965my$size=$1; 966 967my$blob; 968my$bytesLeft=$size; 969 970while(1) { 971last unless$bytesLeft; 972 973my$bytesToRead=$bytesLeft<1024?$bytesLeft:1024; 974my$read=read($in,$blob,$bytesToRead); 975unless(defined($read)) { 976$self->_close_cat_blob(); 977 throw Error::Simple("in pipe went bad"); 978} 979unless(print$fh $blob) { 980$self->_close_cat_blob(); 981 throw Error::Simple("couldn't write to passed in filehandle"); 982} 983$bytesLeft-=$read; 984} 985 986# Skip past the trailing newline. 987my$newline; 988my$read=read($in,$newline,1); 989unless(defined($read)) { 990$self->_close_cat_blob(); 991 throw Error::Simple("in pipe went bad"); 992} 993unless($read==1&&$newlineeq"\n") { 994$self->_close_cat_blob(); 995 throw Error::Simple("didn't find newline after blob"); 996} 997 998return$size; 999}10001001sub _open_cat_blob_if_needed {1002my($self) =@_;10031004return ifdefined($self->{cat_blob_pid});10051006($self->{cat_blob_pid},$self->{cat_blob_in},1007$self->{cat_blob_out},$self->{cat_blob_ctx}) =1008$self->command_bidi_pipe(qw(cat-file --batch));1009}10101011sub _close_cat_blob {1012my($self) =@_;10131014return unlessdefined($self->{cat_blob_pid});10151016my@vars=map{'cat_blob_'.$_}qw(pid in out ctx);10171018 command_close_bidi_pipe(@$self{@vars});1019delete@$self{@vars};1020}102110221023{# %TEMP_* Lexical Context10241025my(%TEMP_FILEMAP,%TEMP_FILES);10261027=item temp_acquire ( NAME )10281029Attempts to retreive the temporary file mapped to the string C<NAME>. If an1030associated temp file has not been created this session or was closed, it is1031created, cached, and set for autoflush and binmode.10321033Internally locks the file mapped to C<NAME>. This lock must be released with1034C<temp_release()> when the temp file is no longer needed. Subsequent attempts1035to retrieve temporary files mapped to the same C<NAME> while still locked will1036cause an error. This locking mechanism provides a weak guarantee and is not1037threadsafe. It does provide some error checking to help prevent temp file refs1038writing over one another.10391040In general, the L<File::Handle> returned should not be closed by consumers as1041it defeats the purpose of this caching mechanism. If you need to close the temp1042file handle, then you should use L<File::Temp> or another temp file faculty1043directly. If a handle is closed and then requested again, then a warning will1044issue.10451046=cut10471048sub temp_acquire {1049my$temp_fd= _temp_cache(@_);10501051$TEMP_FILES{$temp_fd}{locked} =1;1052$temp_fd;1053}10541055=item temp_release ( NAME )10561057=item temp_release ( FILEHANDLE )10581059Releases a lock acquired through C<temp_acquire()>. Can be called either with1060the C<NAME> mapping used when acquiring the temp file or with the C<FILEHANDLE>1061referencing a locked temp file.10621063Warns if an attempt is made to release a file that is not locked.10641065The temp file will be truncated before being released. This can help to reduce1066disk I/O where the system is smart enough to detect the truncation while data1067is in the output buffers. Beware that after the temp file is released and1068truncated, any operations on that file may fail miserably until it is1069re-acquired. All contents are lost between each release and acquire mapped to1070the same string.10711072=cut10731074sub temp_release {1075my($self,$temp_fd,$trunc) = _maybe_self(@_);10761077if(exists$TEMP_FILEMAP{$temp_fd}) {1078$temp_fd=$TEMP_FILES{$temp_fd};1079}1080unless($TEMP_FILES{$temp_fd}{locked}) {1081 carp "Attempt to release temp file '",1082$temp_fd,"' that has not been locked";1083}1084 temp_reset($temp_fd)if$truncand$temp_fd->opened;10851086$TEMP_FILES{$temp_fd}{locked} =0;1087undef;1088}10891090sub _temp_cache {1091my($self,$name) = _maybe_self(@_);10921093 _verify_require();10941095my$temp_fd= \$TEMP_FILEMAP{$name};1096if(defined$$temp_fdand$$temp_fd->opened) {1097if($TEMP_FILES{$$temp_fd}{locked}) {1098 throw Error::Simple("Temp file with moniker '".1099$name."' already in use");1100}1101}else{1102if(defined$$temp_fd) {1103# then we're here because of a closed handle.1104 carp "Temp file '",$name,1105"' was closed. Opening replacement.";1106}1107my$fname;11081109my$tmpdir;1110if(defined$self) {1111$tmpdir=$self->repo_path();1112}11131114($$temp_fd,$fname) = File::Temp->tempfile(1115'Git_XXXXXX', UNLINK =>1, DIR =>$tmpdir,1116)or throw Error::Simple("couldn't open new temp file");11171118$$temp_fd->autoflush;1119binmode$$temp_fd;1120$TEMP_FILES{$$temp_fd}{fname} =$fname;1121}1122$$temp_fd;1123}11241125sub _verify_require {1126eval{require File::Temp;require File::Spec; };1127$@and throw Error::Simple($@);1128}11291130=item temp_reset ( FILEHANDLE )11311132Truncates and resets the position of the C<FILEHANDLE>.11331134=cut11351136sub temp_reset {1137my($self,$temp_fd) = _maybe_self(@_);11381139truncate$temp_fd,01140or throw Error::Simple("couldn't truncate file");1141sysseek($temp_fd,0, SEEK_SET)and seek($temp_fd,0, SEEK_SET)1142or throw Error::Simple("couldn't seek to beginning of file");1143sysseek($temp_fd,0, SEEK_CUR) ==0and tell($temp_fd) ==01144or throw Error::Simple("expected file position to be reset");1145}11461147=item temp_path ( NAME )11481149=item temp_path ( FILEHANDLE )11501151Returns the filename associated with the given tempfile.11521153=cut11541155sub temp_path {1156my($self,$temp_fd) = _maybe_self(@_);11571158if(exists$TEMP_FILEMAP{$temp_fd}) {1159$temp_fd=$TEMP_FILEMAP{$temp_fd};1160}1161$TEMP_FILES{$temp_fd}{fname};1162}11631164sub END{1165unlink values%TEMP_FILEMAPif%TEMP_FILEMAP;1166}11671168}# %TEMP_* Lexical Context11691170=back11711172=head1 ERROR HANDLING11731174All functions are supposed to throw Perl exceptions in case of errors.1175See the L<Error> module on how to catch those. Most exceptions are mere1176L<Error::Simple> instances.11771178However, the C<command()>, C<command_oneline()> and C<command_noisy()>1179functions suite can throw C<Git::Error::Command> exceptions as well: those are1180thrown when the external command returns an error code and contain the error1181code as well as access to the captured command's output. The exception class1182provides the usual C<stringify> and C<value> (command's exit code) methods and1183in addition also a C<cmd_output> method that returns either an array or a1184string with the captured command output (depending on the original function1185call context; C<command_noisy()> returns C<undef>) and $<cmdline> which1186returns the command and its arguments (but without proper quoting).11871188Note that the C<command_*_pipe()> functions cannot throw this exception since1189it has no idea whether the command failed or not. You will only find out1190at the time you C<close> the pipe; if you want to have that automated,1191use C<command_close_pipe()>, which can throw the exception.11921193=cut11941195{1196package Git::Error::Command;11971198@Git::Error::Command::ISA =qw(Error);11991200sub new {1201my$self=shift;1202my$cmdline=''.shift;1203my$value=0+shift;1204my$outputref=shift;1205my(@args) = ();12061207local$Error::Depth =$Error::Depth +1;12081209push(@args,'-cmdline',$cmdline);1210push(@args,'-value',$value);1211push(@args,'-outputref',$outputref);12121213$self->SUPER::new(-text =>'command returned error',@args);1214}12151216sub stringify {1217my$self=shift;1218my$text=$self->SUPER::stringify;1219$self->cmdline() .': '.$text.': '.$self->value() ."\n";1220}12211222sub cmdline {1223my$self=shift;1224$self->{'-cmdline'};1225}12261227sub cmd_output {1228my$self=shift;1229my$ref=$self->{'-outputref'};1230defined$refor undef;1231if(ref$refeq'ARRAY') {1232return@$ref;1233}else{# SCALAR1234return$$ref;1235}1236}1237}12381239=over 412401241=item git_cmd_try { CODE } ERRMSG12421243This magical statement will automatically catch any C<Git::Error::Command>1244exceptions thrown by C<CODE> and make your program die with C<ERRMSG>1245on its lips; the message will have %s substituted for the command line1246and %d for the exit status. This statement is useful mostly for producing1247more user-friendly error messages.12481249In case of no exception caught the statement returns C<CODE>'s return value.12501251Note that this is the only auto-exported function.12521253=cut12541255sub git_cmd_try(&$) {1256my($code,$errmsg) =@_;1257my@result;1258my$err;1259my$array=wantarray;1260try{1261if($array) {1262@result= &$code;1263}else{1264$result[0] = &$code;1265}1266} catch Git::Error::Command with {1267my$E=shift;1268$err=$errmsg;1269$err=~s/\%s/$E->cmdline()/ge;1270$err=~s/\%d/$E->value()/ge;1271# We can't croak here since Error.pm would mangle1272# that to Error::Simple.1273};1274$errand croak $err;1275return$array?@result:$result[0];1276}127712781279=back12801281=head1 COPYRIGHT12821283Copyright 2006 by Petr Baudis E<lt>pasky@suse.czE<gt>.12841285This module is free software; it may be used, copied, modified1286and distributed under the terms of the GNU General Public Licence,1287either version 2, or (at your option) any later version.12881289=cut129012911292# Take raw method argument list and return ($obj, @args) in case1293# the method was called upon an instance and (undef, @args) if1294# it was called directly.1295sub _maybe_self {1296 UNIVERSAL::isa($_[0],'Git') ?@_: (undef,@_);1297}12981299# Check if the command id is something reasonable.1300sub _check_valid_cmd {1301my($cmd) =@_;1302$cmd=~/^[a-z0-9A-Z_-]+$/or throw Error::Simple("bad command:$cmd");1303}13041305# Common backend for the pipe creators.1306sub _command_common_pipe {1307my$direction=shift;1308my($self,@p) = _maybe_self(@_);1309my(%opts,$cmd,@args);1310if(ref$p[0]) {1311($cmd,@args) = @{shift@p};1312%opts=ref$p[0] ? %{$p[0]} :@p;1313}else{1314($cmd,@args) =@p;1315}1316 _check_valid_cmd($cmd);13171318my$fh;1319if($^Oeq'MSWin32') {1320# ActiveState Perl1321#defined $opts{STDERR} and1322# warn 'ignoring STDERR option - running w/ ActiveState';1323$directioneq'-|'or1324die'input pipe for ActiveState not implemented';1325# the strange construction with *ACPIPE is just to1326# explain the tie below that we want to bind to1327# a handle class, not scalar. It is not known if1328# it is something specific to ActiveState Perl or1329# just a Perl quirk.1330 tie (*ACPIPE,'Git::activestate_pipe',$cmd,@args);1331$fh= *ACPIPE;13321333}else{1334my$pid=open($fh,$direction);1335if(not defined$pid) {1336 throw Error::Simple("open failed:$!");1337}elsif($pid==0) {1338if(defined$opts{STDERR}) {1339close STDERR;1340}1341if($opts{STDERR}) {1342open(STDERR,'>&',$opts{STDERR})1343or die"dup failed:$!";1344}1345 _cmd_exec($self,$cmd,@args);1346}1347}1348returnwantarray? ($fh,join(' ',$cmd,@args)) :$fh;1349}13501351# When already in the subprocess, set up the appropriate state1352# for the given repository and execute the git command.1353sub _cmd_exec {1354my($self,@args) =@_;1355 _setup_git_cmd_env($self);1356 _execv_git_cmd(@args);1357dieqq[exec "@args" failed:$!];1358}13591360# set up the appropriate state for git command1361sub _setup_git_cmd_env {1362my$self=shift;1363if($self) {1364$self->repo_path()and$ENV{'GIT_DIR'} =$self->repo_path();1365$self->repo_path()and$self->wc_path()1366and$ENV{'GIT_WORK_TREE'} =$self->wc_path();1367$self->wc_path()and chdir($self->wc_path());1368$self->wc_subdir()and chdir($self->wc_subdir());1369}1370}13711372# Execute the given Git command ($_[0]) with arguments ($_[1..])1373# by searching for it at proper places.1374sub _execv_git_cmd {exec('git',@_); }13751376# Close pipe to a subprocess.1377sub _cmd_close {1378my($fh,$ctx) =@_;1379if(not close$fh) {1380if($!) {1381# It's just close, no point in fatalities1382 carp "error closing pipe:$!";1383}elsif($?>>8) {1384# The caller should pepper this.1385 throw Git::Error::Command($ctx,$?>>8);1386}1387# else we might e.g. closed a live stream; the command1388# dying of SIGPIPE would drive us here.1389}1390}139113921393sub DESTROY {1394my($self) =@_;1395$self->_close_hash_and_insert_object();1396$self->_close_cat_blob();1397}139813991400# Pipe implementation for ActiveState Perl.14011402package Git::activestate_pipe;1403use strict;14041405sub TIEHANDLE {1406my($class,@params) =@_;1407# FIXME: This is probably horrible idea and the thing will explode1408# at the moment you give it arguments that require some quoting,1409# but I have no ActiveState clue... --pasky1410# Let's just hope ActiveState Perl does at least the quoting1411# correctly.1412my@data=qx{git@params};1413bless{ i =>0, data => \@data},$class;1414}14151416sub READLINE {1417my$self=shift;1418if($self->{i} >=scalar@{$self->{data}}) {1419returnundef;1420}1421my$i=$self->{i};1422if(wantarray) {1423$self->{i} =$#{$self->{'data'}} +1;1424returnsplice(@{$self->{'data'}},$i);1425}1426$self->{i} =$i+1;1427return$self->{'data'}->[$i];1428}14291430sub CLOSE {1431my$self=shift;1432delete$self->{data};1433delete$self->{i};1434}14351436sub EOF {1437my$self=shift;1438return($self->{i} >=scalar@{$self->{data}});1439}1440144114421;# Famous last words