Introducing Git::Repository::Plugin::AUTOLOAD

Most git wrappers on CPAN (Git::Wrapper, Git::Class and Git::Sub offer methods (or functions) based on the existing git subcommands (usually limited to a subset of the porcelain commands). Git::Repository is different.

When writing Git::Repository, I was heavily inspired by My design goals were to be as simple as possible, while making it possible to run any git command (even those that both read from STDIN and write to STDOUT). This is why (apart from the version comparison methods), Git::Repository has only two main commands: run and command.

I recently realized it might be a good thing to offer methods named after the existing Git subcommands, just like the competition does. However, I do not want to maintain a list of existing subcommands (Git evolves rapidly, and subcommands do too) or do anything special with their output. This is what specialized plugins are for (e.g. Git::Repository::Plugin::Log, which adds a log method that parses the git log output, or Git::Repository::Plugin::Status, which adds a status method that parses the git status output).

So I went down the AUTOLOAD route: the Git::Repository::Plugin::AUTOLOAD installs an AUTOLOAD method that, whenever an unknown method is encountered, will proxy the call to $self->run( $method, ... ). It will of course not override methods added by plugins.

Given that most of the basic Git commands can be executed through run(), this can makes the code look a little bit nicer.

Before ($r is a Git::Repository object):

$r->run( add => $file );
$r->run( commit =>  '-m' => 'message' );

my $sha1 = $r->run( 'rev-parse' => 'master' );

Git::Repository->run( clone => $url );

After use Git::Repository 'AUTOLOAD';:

$r->commit( '-m' => 'message' );

my $sha1 = $r->rev_parse('master');    # replace  "-" by "_" in command names

Git::Repository->clone( $url );        # can also be used as a class method

So, if Git::Repository's interface was a little too dry for you, maybe this thin layer of syntactic sugar will make you consider using it... :-)



I'm using Git::Repository to manage the history of audio projects created using the Nama multitrack audio recorder. I find Git::Repository simple and straightforward. Taking this opportunity to say "Thanks."

Leave a comment

About BooK

user-pic Pink.