#!perl

our $DATE = '2016-06-11'; # DATE
our $VERSION = '0.001'; # VERSION

use 5.010001;
use strict;
use warnings;

my $Config;
my ($Cwd, $Repo_Dir, $Repo_Name);

sub read_config {
    #require PERLANCAR::File::HomeDir;

    for my $dir ("$ENV{HOME}/.config", $ENV{HOME}, "/etc") {
        my $file = "$dir/gitwrap.conf";
        if (-f $file) {
            require Config::IOD::Reader;
            $Config = Config::IOD::Reader->new->read_file($file);
            return;
        }
    }
    $Config = {};
}

sub run_cmd_template {
    require Cwd;
    require IPC::System::Options;

    my ($cmd) = @_;

    unless ($Cwd) {
        $Cwd = Cwd::getcwd();

      FIND_REPO_TOP_LEVEL_DIR:
        {
            my $cwd = $Cwd;
            while (1) {
                if (-d ".git") {
                    $Repo_Dir = $cwd;
                    ($Repo_Name = $Repo_Dir) =~ s!.+/!!;
                    last;
                }
                chdir ".." or last;
                $cwd =~ s!(.+)/.+!$1! or last;
            }
        }
    }

    my %env;
    $env{GIT_REPO_NAME} = $Repo_Name;

    chdir $Repo_Dir if defined($Repo_Dir);
    IPC::System::Options::system({env=>\%env}, $cmd);
    chdir $Cwd if defined($Repo_Dir);
}

sub do_stuffs {
    if ($Config->{GLOBAL}{before_commit_cmd} && @ARGV && $ARGV[0] eq 'commit') {
        run_cmd_template($Config->{GLOBAL}{before_commit_cmd});
    }
    if ($Config->{GLOBAL}{before_status_cmd} && @ARGV && $ARGV[0] eq 'status') {
        run_cmd_template($Config->{GLOBAL}{before_status_cmd});
    }
}


sub exec_git {
    my @cmd = ($Config->{GLOBAL}{git_path} // 'git', @ARGV);
    exec {$cmd[0]} @cmd;
}

### main

read_config;
do_stuffs;
exec_git;

# ABSTRACT: Git wrapper script
# PODNAME: gitwrap

__END__

=pod

=encoding UTF-8

=head1 NAME

gitwrap - Git wrapper script

=head1 VERSION

This document describes version 0.001 of gitwrap (from Perl distribution App-gitwrap), released on 2016-06-11.

=head1 SYNOPSIS

Use like you would C<git>:

 % gitwrap commit -m ...
 ...

To use the name C<git>, in your Unix shell:

 alias git=gitwrap

then this will call C<gitwrap> instead.

 % git commit -m ...
 ...

=head1 DESCRIPTION

C<gitwrap> is a script that will eventually exec() the C<git> binary. But before
it does that, it can do some things first.

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. See L</"CONFIGURATION">.

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

Some examples of how I use this script:

=over

=item * Fix user and email 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.

=item * Touch last commit/status timestamp

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

=back

=head1 CONFIGURATION

=head2 git_path

=head2 before_commit_cmd_template

=head2 before_status_cmd_template

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-gitwrap>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-gitwrap>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-gitwrap>

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.

=head1 SEE ALSO

C<git>

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

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

=cut
