NAME¶
Git::Repository - Perl interface to Git repositories
VERSION¶
version 1.312
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 );
# start from a repository reachable from the current directory
$r = Git::Repository->new();
# or init our own repository first
Git::Repository->run( init => $dir, ... );
$r = Git::Repository->new( work_tree => $dir );
# or clone from a URL first
Git::Repository->run( clone => $url, $dir, ... );
$r = Git::Repository->new( work_tree => $dir );
# provide an option hash for Git::Repository::Command
# (see Git::Repository::Command for all available options)
$r = Git::Repository->new( ..., \%options );
# run commands
# - get the full output (no errput) passing options for this command only
$output = $r->run( @cmd, \%options );
# - get the full output as a list of lines (no errput), with options
@output = $r->run( @cmd, \%options );
# - process the output with callbacks
$output = $r->run( @cmd, sub {...} );
@output = $r->run( @cmd, sub {...} );
# - obtain a Git::Repository::Command object
# (see Git::Repository::Command for details)
$cmd = $r->command( @cmd, \%options );
# 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.
It is 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 overridden 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::Repository requires at least Git 1.5.0, and is expected to support any
later version.
See Git::Repository::Tutorial for more code examples.
CONSTRUCTOR¶
new¶
Git::Repository->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).
If none of the parameter is given, Git::Repository will find the appropriate
repository just like Git itself does. Otherwise, one of the parameters is
usually 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 $r = Git::Repository->new(
# parameters
work_tree => $dir,
# options
{ git => '/path/to/some/other/git',
env => {
GIT_COMMITTER_EMAIL => 'book@cpan.org',
GIT_COMMITTER_NAME => 'Philippe Bruhat (BooK)',
},
}
);
is equivalent to explicitly passing the option hash to each "run()" or
"command()" call. The documentation for Git::Repository::Command
lists all available options.
Note that Git::Repository and Git::Repository::Command take great care in
finding the option hash wherever it may be in @_, and to merge multiple option
hashes if more than one is provided.
It probably makes no sense to set the "input" option in
"new()", 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.
To create a Git repository and obtain a Git::Repository object pointing to it,
simply do it in two steps:
# run a clone or init command without an instance,
# using options like cwd
Git::Repository->run( ... );
# obtain a Git::Repository instance
# on the resulting repository
$r = Git::Repository->new( ... );
METHODS¶
Git::Repository supports the following methods:
command¶
Git::Repository->command( @cmd );
$r->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¶
Git::Repository->run( @cmd );
$r->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 "chomp"ed.
In addition to the options hashref supported by Git::Repository::Command, the
parameter list can also contain code references, that will be applied
successively to each line of output. The line being processed is in $_, but
the coderef must still return the result string (like "map").
If the git command printed anything on stderr, it will be printed as warnings.
For convenience, if the git sub-process exited with status 128 (fatal error),
or 129 (usage message), "run()" will "die()". The exit
status values for which "run()" dies can be modified using the
"fatal" option (see Git::Repository::Command for details).
The exit status of the command that was just run is accessible as usual using
"$? >> 8". See perlvar for details about $?.
git_dir¶
Returns the repository path.
work_tree¶
Returns the working copy path. Used as current working directory by
Git::Repository::Command.
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 )
All those methods also accept an option hash, just like the others.
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 less 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.
Also, don't be too precise when requiring the minimum version of Git that
supported a given feature. The precise commit in git.git at which a given
feature was added doesn't mean as much as the release branch in which that
commit was merged.
PLUGIN SUPPORT¶
Git::Repository intentionally has only few methods. The idea is to provide a
lightweight wrapper around git, to be used to create interesting tools based
on Git.
However, people will want to add extra functionality to Git::Repository, the
obvious example being a "log()" method that returns simple objects
with useful attributes.
Taking the hypothetical "Git::Repository::Plugin::Hello" module which
source code is listed in the previous reference, the methods it provides would
be loaded and used as follows:
use Git::Repository qw( Hello );
my $r = Git::Repository->new();
print $r->hello();
print $r->hello_gitdir();
It's possible to load only a selection of methods from the plugin:
use Git::Repository [ Hello => 'hello' ];
my $r = Git::Repository->new();
print $r->hello();
# dies: Can't locate object method "hello_gitdir"
print $r->hello_gitdir();
If your plugin lives in another namespace than
"Git::Repository::Plugin::", just prefix the fully qualified class
name with a "+". For example:
use Git::Repository qw( +MyGit::Hello );
See Git::Repository::Plugin about how to create a new plugin.
ACKNOWLEDGEMENTS¶
Thanks to Todd Rinaldo, who wanted to add more methods to Git::Repository, which
made me look for a solution that would preserve the minimalism of
Git::Repository. The "::Plugin" interface is what I came up with.
OTHER PERL GIT WRAPPERS (a.k.a. SEE ALSO)¶
(This section was written in June 2010. The other Git wrappers have probably
evolved since that time.)
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.
What follows is a short list of "missing features" that I was looking
for when I looked at the existing Git wrappers on CPAN. They are the
"rational" reason for writing my own (the real reason being of
course "I thought it would be fun, and I enjoyed doing it").
Even though it works well for me and others, Git::Repository has its own
shortcomings: it
is a
low-level interface to Git commands,
anything complex requires you to deal with input/output handles, it provides
no high-level interface to generate actual Git commands or process the output
of commands (but have a look at the plugins), etc. One the following modules
may therefore be better suited for your needs, depending on what you're trying
to achieve.
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¶
Git::Class depends on Moose, which seems an unnecessary dependency for a simple
wrapper around Git. The startup penalty could become significant for
command-line tools.
Although it supports "git init" and "git clone" (and has
methods to call any Git command), it is mostly aimed at porcelain commands,
and provides no way to control bidirectional commands (such as "git
commit-tree").
Git::Wrapper¶
Git::Wrapper doesn't support streams or bidirectional commands.
Git::Sub¶
(This description was added for completeness in May 2013.)
Git::Sub appeared in 2013, as a set of Git-specific System::Sub functions. It
provide a nice set of "git::" functions, and has some limitations
(due to the way System::Sub itself works) which don't impact most Git
commands.
Git::Sub doesn't support working with streams.
BUGS¶
Since version 1.17, Git::Repository delegates the actual command execution to
System::Command, which has better support for Win32 since version 1.100.
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:
- •
- 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>
BUGS¶
Please report any bugs or feature requests on the bugtracker website
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Git-Repository or by email to
bug-git-repository@rt.cpan.org.
When submitting a bug or request, please include a test-file or a patch to an
existing test-file that illustrates the bug or desired feature.
AUTHOR¶
Philippe Bruhat (BooK) <book@cpan.org>
COPYRIGHT¶
Copyright 2010-2014 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.