NAME

Git::Repository - Perl interface to Git repositories

SYNOPSIS

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') ) {
    ...;
}

DESCRIPTION

Git::Repository is a Perl interface to Git, for scripted interactions with repositories. It's a low-level interface that allows calling any Git command, whether porcelain or plumbing, including bidirectional commands such as git commit-tree.

A Git::Repository object simply provides context to the git commands being run. Is it possible to call the command()and run() methods against the class itself, and the context (typically current working directory) will be obtained from the options and environment.

As a low-level interface, it provides no sugar for particular Git commands. Specifically, it will not prepare environment variables that individual Git commands may need or use.

However, 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.

METHODS

Git::Repository supports the following methods:

new( %args, $options )

Create a new Git::Repository object, based on an existing Git repository.

Parameters are:

git_dir => $gitdir

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 name takes precedence).

work_tree => $dir

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 name takes precedence).

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 which will be used as the default by Git::Repository::Command when working with the corresponding Git::Repository instance.

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
);

is equivalent to explicitly passing the option hash to each run() or command().

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.

create( @cmd )

Runs a repository initialization command (like init or clone) and returns a Git::Repository object pointing to it. @cmd may contain a hashref with options (see Git::Repository::Command.

Do not use the -q option on such commands. create() needs to parse their output to find the path to the repository.

create() also accepts a reference to an option hash which will be used to set up the returned Git::Repository instance.

command( @cmd )

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 may also contain a hashref containing options for the command.

run( @cmd )

Runs the command and returns the output as a string in scalar context, or as a list of lines in list context. Also accepts a hashref of options.

Lines are automatically chomped.

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().

git_dir()

Returns the repository path.

repo_path()

For backward compatibility with versions 1.06 and before, repo_path() it provided as an alias to git_dir().

work_tree()

Returns the working copy path. Used as current working directory by Git::Repository::Command.

wc_path()

For backward compatibility with versions 1.06 and before, wc_path() it provided as an alias to work_tree().

options()

Return the option hash that was passed to Git::Repository->new().

version()

Return the version of git, as given by git --version.

Version-comparison "operators"

Git evolves very fast, and new features are constantly added. 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.

The methods are:

version_lt( $version )
version_gt( $version )
version_le( $version )
version_ge( $version )
version_eq( $version )
version_ne( $version )

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. This only matters in comparisons, only for version numbers prior to 1.4.0-rc1 (June 2006), and only when the compared versions are very close.

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).

If one were 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, the result would not be a totally ordered set. Big deal.

HOW-TO

Create a new repository

# 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' );

Clone a repository

my $r = Git::Repository->create( clone => $url => $dir );

Run a simple command

$r->run( add => '.' );
$r->run( commit => '-m', 'my commit message' );

Process normal and error output

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 unnecessary 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.

Provide input on standard input

Use the input option:

my $commit = $r->run( 'commit-tree', $tree, '-p', $parent,
    { input => $message } );

Change the environment of a command

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.

Process the output of git log

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.

OTHER PERL GIT WRAPPERS

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 represent revision 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

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.

The command_bidi_pipe function especially has problems: http://kerneltrap.org/mailarchive/git/2008/10/24/3789584

Git::Class

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).

Git::Wrapper

Doesn't support streams or bidirectional commands.

AUTHOR

Philippe Bruhat (BooK), <book at cpan.org>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Git::Repository

You can also look for information at:

ACKNOWLEDGEMENTS

COPYRIGHT

Copyright 2010 Philippe Bruhat (BooK), all rights reserved.

LICENSE

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.