NAME

Git::Repository - Perl interface to Git repositories

SYNOPSIS

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

DESCRIPTION

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.

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.

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 repo_path and wc_path 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 )

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

Parameters are:

repository => $gitdir

The location of the git repository (.git directory or equivalent).

working_copy => $dir

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.

create( @cmd )

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.

This method runs the command and parses the first line as Initialized empty Git repository in $dir to find the repository path.

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

run( @cmd )

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.

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

repo_path()

Returns the repository path.

wc_path()

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

wc_subdir()

Return the (relative) subdirectory path of the working copy. If defined, will be used as current working directory by Git::Repository::Command, instead of wc_path.

HOW-TO

Create a new repository

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

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

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.

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

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.