Git::Repository - Perl interface to Git repositories
use Git::Repository; # start from an existing repository $r = Git::Repository->new( repository => $gitdir ); # start from an existing working copy $r = Git::Repository->new( working_copy => $dir ); # or init our own repository $r = Git::Repository->create( init => $dir, ... ); # or clone from a URL $r = Git::Repository->create( clone => $url, ... ); # run commands # - get the full output (no errput) $output = $r->run(@cmd); # - get the full output as a list of lines (no errput) @output = $r->run(@cmd); # - obtain a Git::Repository::Command object $cmd = $r->command(@cmd);
Git::Repository is a Perl interface to Git, allowing scripted interactions with one or more repositories. It's a low-level interface, allowing to call any Git command, either porcelain or plumbing, including bidirectional commands such as git commit-tree.
Git::Repository
git commit-tree
Since it is a low-level interface, it doesn't provide any fancy way to call Git commands. It is up to the programmer to setup any environment variables (except GIT_DIR and GIT_WORK_TREE) that the underlying Git command may need and use.
GIT_DIR
GIT_WORK_TREE
A Git::Repository object simply provides context to the git commands being run. Is it possible to call the command()and run() methods agains the class itself, and the context (typically current working directory) will be obtained from the options and environment.
command()
run()
Git::Repository supports the following methods:
Create a new Git::Repository object, based on an existing Git repository.
Parameters are:
The location of the git repository (.git directory or equivalent).
The location of the git working copy (for a non-bare repository).
At least one of the two parameters is required. Usually, one is enough, as Git::Repository can work out where the other directory (if any) is.
Runs a repository initializing command (like init or clone) and returns a Git::Repository object pointing to it. @cmd can contain a hashref with options (see Git::Repository::Command.
init
clone
@cmd
This method runs the command and parses the first line as Initialized empty Git repository in $dir to find the repository path.
Initialized empty Git repository in $dir
Runs the git sub-command and options, and returns a Git::Repository::Command object pointing to the sub-process running the command.
Git::Repository::Command
As described in the Git::Repository::Command documentation, @cmd can also hold a hashref containing options for the command.
Runs the command and returns the output as a string in scalar context, and as a list of lines in list context. Also accepts a hashref of options.
Lines are automatically chomped.
chomp
If the git command printed anything on stderr, it will be printed as warnings. If the git sub-process exited with status 128 (fatal error), run() will die().
128
die()
Returns the repository path.
Returns the working copy path.
my $r = Git::Repository->create( init => $dir );
my $r = Git::Repository->create( clone => $url => $dir );
$r->run( add => '.' ); $r->run( commit => '-m', 'my commit message' );
The run() command doesn't capture stderr: it only warns (or dies) if something was printed on it. To be able to actually capture error output, command() must be used.
my $cmd = $r->command( @cmd ); my @errput = $cmd->{stderr}->getlines(); $cmd->close;
run() also captures all output at once, which can lead to unecessary memory consumption when capturing the output of some really verbose commands.
my $cmd = $r->command( log => '--pretty=oneline', '--all' ); my $log = $cmd->{stdout}; while (<$log>) { ...; } $cmd->close;
Of course, as soon as one starts reading and writing to an external process' communication handles, a risk of blocking exists. Caveat emptor.
Use the input option:
input
my $commit = $r->run( 'commit-tree', $tree, '-p', $parent, { input => $message } );
Use the env option:
env
$r->run( 'commit', '-m', 'log message', { env => { GIT_COMMITTER_NAME => 'Git::Repository', GIT_COMMITTER_EMAIL => 'book@cpan.org', }, }, );
See Git::Repository::Command for other available options.
A number of Perl git wrappers already exist. Why create a new one?
I have a lot of ideas of nice things to do with Git as a tool to manipulate blobs, trees, and tags, that may or may not reprensent version history of a project. A lot of those commands can output huge amounts of data, which I need to be able to process in chunks. Some of these commands also expect to receive input.
Git.pm is not on CPAN. It is usually packaged with Git, and installed with the system Perl libraries. Not being on CPAN makes it harder to install in any Perl. It makes it harder for a CPAN library to depend on it.
It doesn't allow calling git init or git clone.
git init
git clone
The command_bidi_pipe function especially has problems: http://kerneltrap.org/mailarchive/git/2008/10/24/3789584
command_bidi_pipe
Depends on Moose, which seems an unnecessary dependency for a simple wrapper around Git.
Although it supports git init and git clone, it is mostly aimed at porcelain commands, and provides no way to control bidirectional commands (such as git commit-tree).
Doesn't support streams or bidirectional commands.
Philippe Bruhat (BooK), <book at cpan.org>
<book at cpan.org>
Please report any bugs or feature requests to bug-git-repository at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Git-Repository. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-git-repository at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Git::Repository
You can also look for information at:
RT: CPAN's request tracker
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Git-Repository
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Git-Repository
CPAN Ratings
http://cpanratings.perl.org/d/Git-Repository
Search CPAN
http://search.cpan.org/dist/Git-Repository
Copyright 2010 Philippe Bruhat (BooK), all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Git::Repository, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Git::Repository
CPAN shell
perl -MCPAN -e shell install Git::Repository
For more information on module installation, please visit the detailed CPAN module installation guide.