App-gitwrap

 view release on metacpan or  search on metacpan

script/gitwrap  view on Meta::CPAN


then this will call C<gitwrap> instead.

 % git commit -m ...
 ...

=head1 DESCRIPTION

C<gitwrap> is a script that runs the C<git> binary. But before it does that, it
can do some things first. And after that, it can also do some other things.

It will first search for configuration file F<gitwrap.conf> in one of these
locations: F<~/.config>, F<~>, or F</etc>. It then will execute various commands
as described in the configuration, e.g. L</before_commit_cmd>. See
L</"CONFIGURATION">.

It will then run the git binary as specified in the configuration C<git_path> or
just use C<git>.

Then it will run various commands as described in the configuration, e.g.
L</after_commit_cmd>.

Lastly it will exit with the exit code of git command executed earlier.

Some examples of how I use this script:

=over

=item * Fix user and email dynamically before commit

Suppose that instead of putting the user/email manually on each C<.git/config>,
you want to set/fix user and email dynamically using a set of rules right before
commit.

Doing this in C<pre-commit> hook is too late as C<git> already decides the
user/email, so if you use C<pre-commit> hook for this the best you can do is fix
user/email then exit non-zero so the commit is aborted. Then you commit again.

With this wrapper, this extra step is unnecessary. You can set
C<before_commit_cmd> in F<gitwrap.conf> to the command necessary to munge the
user/email in C<.git/config>, for example:

 before_commit_cmd = perl -e'($user, $email) = ...; system "git", "config", "user.name", $user; system "git", "config", "user.email", $email'

or, if you already have a C<pre-commit> hook that does this, you can call the
pre-commit hook instead.

 before_commit_cmd = ".git/hooks/pre-commit; exit 0"

The difference is that even if the hook exits non-zero, gitwrap will continue
executing git (due to C<exit 0> clause). But if the hook fails again during
actual pre-commit phase in git, the commit will be aborted.

=item * Touch last commit/status timestamp

I update a record in a database whenever a repository is committed into or
status-ed. The first can be done with a C<post-commit> hook, but for the second
there is no hook provided by git. I do this to speed up or prioritize
repositories that are more recently modified/accessed in L<Git::Bunch>: When
there are hundreds/thousand+ git repositories in a gitbunch, and only a few of
them need to be synchronized, using these timestamps can dramatically shorten
the amount of time to determine which repositories need to be included, because
we avoid having to 'git status' every single repository.

To do this, in F<gitwrap.conf>:

 before_commit_cmd = reposdb touch --commit-time
 before_status_cmd = reposdb touch --status-time

Note: see also L<reposdb>.

=item * Setup hooks

Sometimes I copy repositories to other computers simply by using C<cp> or
C<rsync>. I also have scripts in C<.git/hooks/> directory inside the repository
as symlinks to the actual scripts. When copying to other computers, the paths
are not exactly alike so the symlinks get broken. We can use
C<before_commit_cmd> or other C<before*cmd> to correct these symlinks first.

=back

=head1 CONFIGURATION

=head2 git_path

=head2 before_commit_cmd

Command to run before C<git commit>. Can be specified multiple times. Will abort
execution if any of the commands fails.

=head2 before_status_cmd

Command to run before C<git status>. Can be specified multiple times. Will abort
execution if any of the commands fails.

=head2 before_pull_cmd

Command to run before C<git pull>. Can be specified multiple times. Will abort
execution if any of the commands fails.

=head2 before_push_cmd

Command to run before C<git push>. Can be specified multiple times. Will abort
execution if any of the commands fails.

=head2 after_commit_cmd

Command to run after C<git commit>. Can be specified multiple times.

=head2 after_status_cmd

Command to run after C<git status>. Can be specified multiple times.

=head2 after_pull_cmd

Command to run after C<git pull>. Can be specified multiple times.

=head2 after_push_cmd

Command to run after C<git push>. Can be specified multiple times.



( run in 0.592 second using v1.01-cache-2.11-cpan-39bf76dae61 )