Git::Repository - Perl interface to Git repositories
use Git::Repository; # start from an existing repository $r = Git::Repository->new( git_dir => $gitdir ); # start from an existing working copy $r = Git::Repository->new( work_tree => $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); # obtain version information my $version = $r->version(); # compare current git version if ( $r->version_gt('1.6.5') ) { ...; }
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 that the underlying Git command may need and use.
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()
The GIT_DIR and GIT_WORK_TREE environment variables are special: if the command is run in the context of a Git::Repository object, they will be overriden by the object's git_dir and work_tree attributes, respectively. It is however still possible to override them if necessary, using the env option.
GIT_DIR
GIT_WORK_TREE
git_dir
work_tree
env
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).
For backward compatibility with versions 1.06 and before, repository is accepted in place of git_dir (but the newer takes precedence).
repository
The location of the git working copy (for a non-bare repository).
If work_tree actually points to a subdirectory of the work tree, Git::Repository will automatically recompute the proper value.
For backward compatibility with versions 1.06 and before, working_copy is accepted in place of work_tree (but the newer takes precedence).
working_copy
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.
new() also accepts a reference to an option hash, that will be automatically used by Git::Repository::Command when working with the corresponding Git::Repository instance.
new()
Git::Repository::Command
So this:
my $options = { git => '/path/to/some/other/git', env => { GIT_COMMITTER_EMAIL => 'book@cpan.org', GIT_COMMITTER_NAME => 'Philippe Bruhat (BooK)', }, }; my $r = Git::Repository->new( work_tree => $dir, $options );
will be equivalent to having any option hash that will be passed to run() or command() be pre-filled with these options.
It probably makes no sense to set the input option in new() or create(), but Git::Repository won't stop you. Note that on some systems, some git commands may close standard input on startup, which will cause a SIGPIPE. Git::Repository::Command will raise an exception.
input
create()
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 to find the repository path. Using the option -q on such commands makes no sense, as it will prevent create() to parse their output.
create() also accepts a reference to an option hash, that will be used to setup the returned Git::Repository instance.
Runs the git sub-command and options, and returns a Git::Repository::Command object pointing to the sub-process running the 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.
For backward compatibility with versions 1.06 and before, repo_path() it provided as an alias to git_dir().
repo_path()
git_dir()
Returns the working copy path. Used as current working directory by Git::Repository::Command.
For backward compatibility with versions 1.06 and before, wc_path() it provided as an alias to work_tree().
wc_path()
work_tree()
Return the option hash that was passed to Git::Repository->new().
Git::Repository->new()
Return the version of git, as given by git --version.
git --version
Git evolves very fast, and new features are constantly added to it. To facilitate the creation of programs that can properly handle the wide variety of Git versions seen in the wild, a number of version comparison "operators" are available.
They are named version_op where op is the equivalent of the Perl operators lt, gt, le, ge, eq, ne. They return a boolean value, obtained by comparing the version of the git binary and the version string passed as parameter.
version_op
lt
gt
le
ge
eq
ne
The methods are:
Note that there are a small number of cases where the version comparison operators will not compare versions correctly for very old versions of Git. Typical example is 1.0.0a gt 1.0.0 which should return true, but doesn't. It actually only concerns cases when it is needed to compare and the last significant bit of very close (and very old) version numbers.
1.0.0a gt 1.0.0
However, this only concerns Git versions older than 1.4.0-rc1 (June 2006). It wasn't worth the trouble to try and correctly compare older version numbers.
1.4.0-rc1
Other issues exist when comparing development version numbers with one another. For example, 1.7.1.1 is greater than both 1.7.1.1.gc8c07 and 1.7.1.1.g5f35a, and 1.7.1 is lower than both. Obviously, 1.7.1.1.gc8c07 will compare as greater than 1.7.1.1.g5f35a (asciibetically), but in fact these two version numbers cannot be compared, as they are two siblings children of the commit tagged v1.7.1).
1.7.1.1
1.7.1.1.gc8c07
1.7.1.1.g5f35a
1.7.1
v1.7.1
If one was to compute the set of all possible version numbers (as returned by git --version) for all git versions that can be compiled from each commit in the git.git repository, this would not be a totally ordered set. Big deal.
# git version 1.6.5 and above my $r = Git::Repository->create( init => $dir ); # any older git will need two steps chdir $dir; my $r = Git::Repository->create( 'init' );
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:
my $commit = $r->run( 'commit-tree', $tree, '-p', $parent, { input => $message } );
Use the env option:
$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.
When creating a tool that needs to process the output of git log, you should always define precisely the expected format using the --pretty option, and choose a format that is easy to parse.
Assuming git log will output the default format will eventually lead to problems, for example when the user's git configuration defines format.pretty to be something else than the default of medium.
format.pretty
medium
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.