Gustavo Leite de Mendonça Chaves > Git-Hooks-0.049 > Git::More



Annotate this POD



Open  1
View/Report Bugs
Module Version: 0.049   Source   Latest Release: Git-Hooks-1.0.1


Git::More - A Git extension with some goodies for hook developers.


version 0.049


    use Git::More;

    my $git = Git::More->repository();

    my $config  = $git->get_config();
    my $branch  = $git->get_current_branch();
    my $commits = $git->get_commits($oldcommit, $newcommit);
    my $message = $git->get_commit_msg('HEAD');

    my $files_modified_by_commit = $git->get_diff_files('--diff-filter=AM', '--cached');
    my $files_modified_by_push   = $git->get_diff_files('--diff-filter=AM', $oldcommit, $newcommit);


This is an extension of the Git class. It's meant to implement a few extra methods commonly needed by Git hook developers.

In particular, it's used by the standard hooks implemented by the Git::Hooks framework.

Note that the Git module is distributed along with git and it's not on CPAN yet. So, it's possible that Git::More can't find it in the default directories in @INC. You have to find it and make it available to your Perl in order to install Git::Hooks and to use it. Here are a few options to do that:

(If you have trouble finding, here is a tip. There are some Perl scripts distributed along with git that use Take a look at the git-svn script which should be installed in the directory returned by the git --exec-path command. In it's second line there is a mention to a directory where it looks for


get_config [SECTION [VARIABLE]]

This method groks the configuration options for the repository by invoking git config --list. The configuration is cached during the first invokation in the object Git::More object. So, if the configuration is changed afterwards, the method won't notice it. This is usually ok for hooks, though.

With no arguments, the options are returned as a hash-ref pointing to a two-level hash. For example, if the config options are these:


Then, it'll return this hash:

        'section1' => {
            'a' => [1],
            'b' => [2, 3],
        'section2.x' => {
            'a' => ['A'],
            'b' => ['B', 'C'],

The first level keys are the part of the option names before the last dot. The second level keys are everything after the last dot in the option names. You won't get more levels than two. In the example above, you can see that the option "section2.x.a" is split in two: "section2.x" in the first level and "a" in the second.

The values are always array-refs, even it there is only one value to a specific option. For some options, it makes sense to have a list of values attached to them. But even if you expect a single value to an option you may have it defined in the global scope and redefined in the local scope. In this case, it will appear as a two-element array, the last one being the local value.

So, if you want to treat an option as single-valued, you should fetch it like this:


If the SECTION argument is passed, the method returns the second-level hash for it. So, following the example above, this call:


This call would return this hash:

        'a' => [1],
        'b' => [2, 3],

If the section don't exist an empty hash is returned. Any key/value added to the returned hash will be available in subsequent invokations of get_config.

If the VARIABLE argument is also passed, the method returns the value(s) of the configuration option SECTION.VARIABLE. In list context the method returns the list of all values or the empty list, if the variable isn't defined. In scalar context, the method returns the variable's last value or undef, if it's not defined.


This method may be used by plugin developers to cache information in the context of a Git::More object. SECTION is a string (usually a plugin name) that is associated with a hash-ref. The method simply returns the hash-ref, which can be used by the caller to store any kind of information.

clean_cache SECTION

This method deletes the cache entry for SECTION. It may be used by hooks just before returning to Git::Hooks::run_hooks in order to get rid of any value kept in the SECTION's cache.


This method returns a list of hashes representing every commit reachable from NEWCOMMIT but not from OLDCOMMIT. It obtains this information by invoking git rev-list NEWCOMMIT ^OLDCOMMIT.

There are two special cases, though:

If NEWCOMMIT is the null SHA-1, i.e., '0000000000000000000000000000000000000000', this means that a branch, pointing to OLDCOMMIT, has been removed. In this case the method returns an empty list, meaning that no new commit has been created.

If OLDCOMMIT is the null SHA-1, this means that a new branch poiting to NEWCOMMIT is being created. In this case we want all commits reachable from NEWCOMMIT but not reachable from any other branch. The syntax for this is NEWCOMMIT ^B1 ^B2 ... ^Bn", i.e., NEWCOMMIT followed by every other branch name prefixed by carets. We can get at their names using the technique described in, e.g., this discussion.

Each commit in the returned list is represented by a hash with the following structure (the codes are explained in the git help rev-list document):

        commit          => %H:  commit hash
        tree            => %T:  tree hash
        parent          => %P:  parent hashes (space separated)
        author_name     => %aN: author name
        author_email    => %aE: author email
        author_date     => %ai: author date in ISO8601 format
        commmitter_name => %cN: committer name
        committer_email => %cE: committer email
        committer_date  => %ci: committer date in ISO8601 format
        body            => %B:  raw body (aka commit message)

get_commit_msg COMMIT_ID

This method returns the commit message (a.k.a. body) of the commit identified by COMMIT_ID. The result is a string.

read_commit_msg_file FILENAME

This method returns the relevant contents of the commit message file called FILENAME. It's useful during the commit-msg and the prepare-commit-msg hooks.

The file is read using the character encoding defined by the i18n.commitencoding configuration option or utf-8 if not defined.

Some non-relevant contents are stripped off the file. Specifically:

All this cleanup is performed to make it easier for different plugins to analyse the commit message using a canonical base.

write_commit_msg_file FILENAME, MSG, ...

This method writes the list of strings MSG to FILENAME. It's useful during the commit-msg and the prepare-commit-msg hooks.

The file is written to using the character encoding defined by the i18n.commitencoding configuration option or utf-8 if not defined.

An empty line (\n\n) is inserted between every pair of MSG arguments, if there is more than one, of course.

get_diff_files DIFFARGS...

This method invokes the command git diff --name-status with extra options and arguments as passed to it. It returns a reference to a hash mapping every affected files to their affecting status. Its purpose is to make it easy to grok the names of files affected by a commit or a sequence of commits. Please, read git help diff to know everything about its options.

A common usage is to grok every file added or modified in a pre-commit hook:

    my $hash_ref = $git->get_diff_files('--diff-filter=AM', '--cached');

Another one is to grok every file added or modified in a pre-receive hook:

    my $hash_ref = $git->get_diff_files('--diff-filter=AM', $old_commit, $new_commit);


This method should be used in the beginning of an update, pre-receive, or post-receive hook in order to record the references that were affected by the push command. The information recorded will be later used by the following get_affected_ref* methods.


This method returns the list of names of references that were affected by the current push command, as they were set by calls to the set_affected_ref method.


This method returns the two-element list of commit ids representing the OLDCOMMIT and the NEWCOMMIT of the affected REF.


This method returns the list of commit ids leading from the affected REF's NEWCOMMIT to OLDCOMMIT.


This routine returns the list of commits leading from the affected REF's NEWCOMMIT to OLDCOMMIT. The commits are represented by hashes, as returned by the get_commits method.


This method returns the username of the authenticated user performing the Git action. It groks it from the githooks.userenv configuration variable specification, which is described in the Git::Hooks documentation. It's useful for most access control check plugins.

push_input_data DATA

This method gets a single value and tucks it in an internal list so that every piece of data can be gotten later with the get_input_data method below.

It's used by Git::Hooks to save arguments read from STDIN by some Git hooks like pre-receive, post-receive, pre-push, and post-rewrite.


This method returns an array-ref pointing to a list of all pieces of data saved by calls to push_input_data method above.

set_authenticated_user USERNAME

This method can be used to set the username of the authenticated user when the default heristics defined above aren't enough. The name will be cached so that subsequent invokations of authenticated_user will return this.


This method returns the repository's current branch name, as indicated by the git symbolic-ref HEAD command.

If the repository is in a dettached head state, i.e., if HEAD points to a commit instead of to a branch, the method returns undef.


This method should be used by plugins to record consistent error or warning messages. It gets two or three arguments. The PREFIX is usually the plugin's package name. The MESSAGE is a oneline string. These two arguments are combined to produce a single line like this:


DETAILS is an optional string. If present, it is appended to the line above, separated by an empty line, and with its lines prefixed by two spaces, like this:



The method simply records the formatted error message and returns. It doesn't die.


This method returns a list of all error messages recorded with the error method.


By default all errors produced by Git::Hooks use Carp::croak, so that they contain a suffix telling where the error occurred. Sometimes you may not want this. For instance, if the user is going to receive the error message produced by a server hook he/she won't be able to use that information.

This method makes error strip any such suffixes from its DETAILS argument and to produce its own message with warn instead of carp.




Gustavo L. de M. Chaves <>


This software is copyright (c) 2014 by CPqD <>.

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

syntax highlighting: