#!/usr/bin/perl

=pod

=head1 NAME

git-client - Git Client Wrapper

=head1 DESCRIPTION

Wrapper around the real git for augmented functionality.

=head2 .gitconfig Override

Allows for .gitconfig descent override files up until the $HOME directory.
All descent .gitconfig files will be honored, although files closest
to the git working directory will take precedence. All these descent
.gitconfig files take precedence over --global config files,
which take precedence over --system files in case of duplicate settings.
And --local config files take precedence over all these descent files
and all other --global and --system files.

=head2 -O <OPTION>

Populates GIT_OPTION_* environment variables on server side.
These ENV settings will be available to all the server side
hooks, including the pre-* hooks.
Note that for this to work, the git ssh server must have
"AcceptEnv XMODIFIERS" enabled in its sshd_config.

=head2 -v | --version | version

Shows both git-client version and git version and exits.

=head2 DEBUG

Sets DEBUG environment on server side to match the same numeric value
as set on the client invoker.

=head1 INSTALL

Just make sure this program comes BEFORE the
real "git" program in the PATH.

For example, as super user, you could do this:

  [root@deploy-host ~]# wget -N -P /usr/local/bin https://raw.githubusercontent.com/hookbot/git-server/master/git-client
  [root@deploy-host ~]# chmod 755 /usr/local/bin/git-client
  [root@deploy-host ~]# ln -s -v git-client /usr/local/bin/git
  [root@deploy-host ~]#

Or as normal user, you could do this:

  [user@deploy-host ~]$ mkdir -p ~/bin
  [user@deploy-host ~]$ wget -N -P ~/bin https://raw.githubusercontent.com/hookbot/git-server/master/git-client
  [user@deploy-host ~]$ chmod 755 ~/bin/git-client
  [user@deploy-host ~]$ ln -s -v git-client ~/bin/git
  [user@deploy-host ~]$ grep 'PATH=$HOME/bin' ~/.bash_profile || echo 'export PATH=$HOME/bin:$PATH' | tee -a ~/.bash_profile
  [user@deploy-host ~]$

=head1 SYNOPSIS

  cd ~/src/github/project
  git config --file ../.gitconfig user.email 'hookbot@github.com'
  git config --list

=head1 PURPOSE

Allows you to use many different .gitconfig files
within each folder of git repos. If there is no
.gitconfig within the directory descent structure,
then it will behave exactly like the normal git.

=head1 AUTHOR

Rob Brown <bbb@cpan.org>

=head1 COPYRIGHT AND LICENSE

Copyright 2016-2026 by Rob Brown <bbb@cpan.org>

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

=cut

use strict;
use Cwd qw(abs_path);
use IO::Handle; # For non-blocking

our $VERSION = "0.049";

our $full = abs_path $0 or die "$0: Not real?\n";

# Locate the real "git" later in the $PATH (after myself)
our $real_git = find_real_git();

# By default, silence any error spewages for internal git usage.
our $HANDLE_STDERR = sub { open STDERR, ">", "/dev/null" };

# Special $GIT_SSH shim to emulate $GIT_SSH_COMMAND behavior
!$ENV{DEBUG} || warn "DEBUG: [$full] Faking GIT_SSH_COMMAND behavior for incompetent git/".git_ver()."\n"
    and exec sh => -c => $ENV{GIT_SSH_COMMAND}.' "$@"' => ssh => @ARGV or die "exec: $!\n"
    if $0 eq ($ENV{GIT_SSH}||"") and $ENV{GIT_SSH_COMMAND}||="ssh";

# Special Version Handling so -v works prior to 2.37.0+ (June 27, 2022) and to show git-client $VERSION
if ("@ARGV" =~ /^(-v|(?:--|)version)$/ and $real_git =~ m{^(.*/)}) {
    my $nextpath = $1;
    print "[git-client v$VERSION] ";
    my @tried = tried();
    my @prev = split /\n/, $ENV{GIT_CLIENT_VERSION_RECURSE} || "";
    if (@prev) {
        my %skip = map { $_=>1 } @prev; # Skip any previous shims already printed
        @tried = grep { $_ eq $0 or $_ eq $full or $_ eq $real_git or !$skip{$_} } @tried;
        %skip = map { $_=>1 } @tried; # Avoid duplicate stashing back into ENV
        @prev = grep { !$skip{$_} } @prev;
    }
    $ENV{GIT_CLIENT_VERSION_RECURSE} = join "\n", @prev, @tried;
    # Show final target only if $real_git is a symlink elsewhere.
    pop @tried if $tried[-1] eq $real_git;
    print "$_ => " foreach @tried;
    my $g;
    print $nextpath unless open $g, "<", $real_git and $g = getc $g and $g eq "#";
    print git_ver_string();
    exit;
}

# Handle [ -C <dir> ] first
if (eval { require Getopt::Long; 1; } and
    Getopt::Long::Configure(qw[pass_through no_ignore_case bundling]) and
    Getopt::Long::GetOptions( "chdir|C=s" => (my $chdir=[]) ),
    ) {
    foreach (@$chdir) { chdir $_ or die "fatal: cannot change to '$_': $!\n"; }
}

# Grab special options for XMODIFIERS transport
pass_options();

# Scan for .gitconfig files in descent directories
scan_descent_override();

# Having a "git-deploy" command in the path works fine to pretend like "git deploy" is a command,
# but "git-config" will be ignored since /usr/libexec/git-core/git-config will always come first in the PATH.
# So we must handle all "git config" overrides within this "git" wrapper.
handle_config(@ARGV);

exec $real_git, @ARGV;

sub git_ver_string { return $ENV{GIT_VERSION_STRING} ||= git("--version") || "unknown $real_git version 0.0.0\n"; }
sub git_ver { return git_ver_string() =~ /version (\d+(?:\.\d+)+)/ ? $1 : "0.0.0" }
sub packv { return join '', map { chr } (shift || git_ver) =~ /(\d+)/g; }

# git_min($MINIMUM_VERSION)
# Returns true only if current version is at least the version specified, i.e.,
#   warn "TOO LOW!" if !git_min("2.26.0");
#     # -or-
#   $supports_GIT_CONFIG_NOGLOBAL = git_min(1.6.1.3) && !git_min(v1.7.4);
sub git_min {
    local $_ = shift or die 'git_min($VER)';
    $_ = packv $_ if /^\d/;
    return $_ le packv;
}

# Provide special options through "SendEnv XMODIFIERS" to make available in pre-hooks prior to running the real git command on the server
sub pass_options {
    my ($op) = grep { /^(clone|fetch|pull|ls-remote|push)$/ } @ARGV;
    return if !$op;
    # Need to search for special "-o" options for XMODIFIERS transport
    my $xmod = $ENV{XMODIFIERS} || "";
    my @options = ();
    my $pull_branch = "";
    push @options, "DEBUG=$ENV{DEBUG}" if defined $ENV{DEBUG} and $xmod !~ /^DEBUG=/m;
    if (eval { require Getopt::Long; 1; } and
        Getopt::Long::GetOptions( "O=s" => \@options )) { # Strip stealth -O args from ARGV so the real git doesn't choke on it
        if (my $rest_opt = ($op ne "clone" && "o|")."server-option|push-option=s") { # Ignore -o<origin> for "git clone"
            local @ARGV = @ARGV; # Don't monkey anything. Just peek.
            Getopt::Long::GetOptions( $rest_opt => \@options, "branch|b=s" => \$pull_branch ); # Handle "git clone -b <ref> <repo>"
        }
    }
    $pull_branch = $1 if $op =~ /^(fetch|pull)$/ and $xmod !~ /^pull_branch=/m and git("branch") =~ m{^\* ([\w/\-.@]+)\s*$}m;
    push @options, "pull_branch=$pull_branch" if $pull_branch;
    push @options, "client=$0\@v$VERSION" if $xmod !~ /^client=/m;
    push @options, $xmod if $xmod;
    $ENV{XMODIFIERS} = join "\n", @options;
    $ENV{GIT_SSH_COMMAND} ||= git(qw[config core.sshCommand]) =~ /^(.+)/ ? $1 : "ssh";
    $ENV{GIT_SSH_COMMAND} .= " -o SendEnv=XMODIFIERS" if $ENV{GIT_SSH_COMMAND} !~ /=XMODIFIERS/;
    $ENV{GIT_SSH} ||= $0; # Backward compatible in case $GIT_SSH_COMMAND is ignored (git < 2.3.x)
}

# tried( [ $prog ] )
# Usage:
#  1. Mark $prog as tried:
#     tried( $prog ); # Void context
#  2. Check if $prog has already been tried:
#     if (tried($prog)) { ...} # Boolean context
#  3. List everything that has been tried:
#     my @already_tried = tried(); # No arguments in array context
sub tried {
    my $prog = shift;
    my $context = wantarray;
    die 'tried: Boolean or Void context requires $prog to check or set, and List context must not have an argument' if !$context eq !$prog;
    my @tried = split /\n/, ($ENV{GIT_CLIENT_TRIED}||="");
    return @tried if $context; # List context has no arg, so return entire list of everything tried.
    return 1 if grep { $prog eq $_ } @tried; # If already tried $prog, then do nothing and Kick out and return true (regardless of context).
    return 0 if defined $context; # If Boolean context, return false.
    # Void context, so inject $prog to mark as tried.
    push @tried, $prog;
    $ENV{GIT_CLIENT_TRIED} = join "\n", @tried;
    return; # Return value is ignored.
}

sub find_real_git {
    my $myself = (stat $0)[1] or die "$0: Can't find my inode?\n";
    tried($0);
    tried($full);
    my $found_git = "";
    require File::Spec;
    foreach my $path (File::Spec->path) {
        my $try = "$path/git";
        if (my @stat = stat $try) {
            if ($stat[1] == $myself) {
                # Ignore myself
            }
            elsif (tried($try)) {
                # Already tried
            }
            else {
                # First executable one in the path that isn't me is the winner
                tried($found_git = $try);
                tried(abs_path $try);
                last;
            }
        }
    }

    !$found_git and $found_git = "/usr/bin/git" and $ENV{GIT_CLIENT_TRIED} .= "\n$found_git";
    -x $found_git or die "$found_git: Unable to execute\n";
    die "GIT_CLIENT_TRIED:\n$1\n\n$found_git: Recursion too deep\n" if $ENV{GIT_CLIENT_TRIED} =~ /^(?=.{9999})(.{555,}?[^\n]*)/s;
    return $found_git;
}

# Find effective GIT_DIR if not set
sub git_dir { return $ENV{GIT_CLIENT_DIR} ||= $ENV{GIT_DIR} || (git(qw[rev-parse --git-dir]) =~ /^(.+)$/ ? $1 : ""); }

# Capture "git" command output
sub git {
    if (open my $fh_out, "-|") {
        # Parent: Read answer and wait for the child zombie to clear.
        local $/;
        return [scalar(<$fh_out>), close($fh_out)]->[0];
    }

    # Child runs the git command
    $HANDLE_STDERR->();
    # Secure exec @array method to avoid having to shell escape arguments containing spaces or other dangerous chars
    exec $real_git, @_ or exit 1;
}

# Jam equivalent "-c" config settings into @ARGV for any descent .gitconfig files and set GIT_CLIENT_OVERRIDE* env.
sub scan_descent_override {
    return if exists $ENV{GIT_CLIENT_OVERRIDE};
    $ENV{GIT_CLIENT_OVERRIDE} = "";
    my $descent_config_files = [];
    my $last = ".";
    $ENV{HOME} ||= (getpwuid $<)[7];
    while (1) {
        my $scan = abs_path( $last eq "." && !-d "$last/.git" ? $last : "$last/.." );
        last if $scan eq $last or $scan eq $ENV{HOME};
        $last = $scan;
        my $try = "$scan/.gitconfig";
        if (-r $try) {
            return warn "WARNING: Please remove --descent file [$try] or upgrade git from ".git_ver()." to 1.7.2+\n" if !@$descent_config_files and !git_min(1.7.2);
            warn "DEBUG: $try: Override\n" if $ENV{DEBUG};
            unshift @$descent_config_files, $try;
        }
    }
    if (@$descent_config_files) {
        $ENV{GIT_CLIENT_OVERRIDE} = join "\n", @$descent_config_files;
        my $cmd_settings = [];
        my $descent_names = {};
        my $descent_counter = 0;
        foreach my $config_file (@$descent_config_files) {
            my $settings = git(qw[ config --list --file ], $config_file);
            $descent_counter++;
            while ($settings =~ s/^([^=\r\n]+)=(.*?)\r?\n?$//m) {
                my $name = $1;
                my $value = $2;
                push @$cmd_settings, "$name=$value";
                $descent_names->{$name} = $value;
                $ENV{"GIT_CLIENT_OVERRIDE_$descent_counter"}++;
            }
        }
        my $l = git_dir;
        if ($l and -d $l and my $local_settings = git(qw[ config --list --file ], "$l/config")) {
            while ($local_settings =~ s/^([^=\r\n]+)=(.*?)\r?\n?$//m) {
                my $name = $1;
                my $value = $2;
                if (defined $descent_names->{$name}) {
                    # It seems better to double override than to completely leave out the duplicate descent setting
                    push @$cmd_settings, "$name=$value";
                    $ENV{GIT_CLIENT_OVERRIDE_0}++;
                }
            }
        }
        while (my $setting = pop @$cmd_settings) {
            unshift @ARGV, -c => $setting;
        }
    }
    else {
        # No effective .gitconfig files found in descent, so nothing to do
    }
}

# Special "config" handler to honor new --descent option
sub handle_config {
    my @args = @_;
    local @ARGV = @args;
    # Don't try to handle the "--help"-first case.
    return if join(" ", @ARGV) =~ /config --help\b/;
    if (grep {$_ eq "config"} @ARGV and
        # Smells like special "git config" command
        eval { require Getopt::Long; 1; } and
        Getopt::Long::Configure(qw[pass_through no_ignore_case require_order]) and
        Getopt::Long::GetOptions( "c=s" => (my $c=[]) ) and # XXX: Is is safe to ignore -c <name>=<value> args?
        $ARGV[0] eq "config" and
        do {
            # Handle naked "git config" case to make sure the usage can be updated before sending it
            push @_,"--usage" and push @ARGV,"--usage" if 1 == @ARGV;
            # Also handle new-fangled git >= 2.46.x "git config <command_without_dashes>" syntax:
            $ARGV[1]=~/^([a-z][a-z\-]+)$/ && ($ARGV[1]="--$1");
            1;
        } and
        Getopt::Long::Configure(qw[no_bundling no_require_order]) and
        Getopt::Long::GetOptions(
            # Scope
            "descent"      => \(my $descent),
            "global"       => \(my $global),
            "system"       => \(my $system),
            "local"        => \(my $local),
            "worktree"     => \(my $work),
            "f|file=s"     => \(my $file),

            # Action
            "l|list"       => \(my $list),
            "e|edit"       => \(my $edit),
            "get|get-all|get-regexp=s{1,2}" => \(my $get),
            "remove-section=s{1}" => \(my $action_1_arg),
            "set|get-urlmatch|rename-section=s{2}" => \(my $action_2_arg),
            "get-color|get-colorbool=s{1,2}" => \(my $action_1_2_arg),
            "replace-all=s{2,3}" => \(my $action_2_3_arg),

            # Other
            "all"          => \(my $all),
            "fixed-value"  => \(my $fixed),
            "z|null"       => \(my $null),
            "name-only"    => \(my $nameonly),
            "show-origin"  => \(my $origin),
            "show-scope"   => \(my $scope),
            "h|help|usage" => \(my $help),
        )
    ) {
        # Looks like this "config" command may need special attention
        my $descent_config_files = [ split /\n/, $ENV{GIT_CLIENT_OVERRIDE} ];
        if ($descent) {
            # Swap "--descent" for "--file <DESCENT_CONFIG_FILE>"
            # If more than one descent file, then just use the closest one
            @_ = map { $_ =~ /^--des/ ? ("--file" => ($descent_config_files->[-1] || "/dev/null")) : ($_) } @_;
        }
        # error: only one action at a time:
        map { ($_ eq "config" and ++$help) ? ($_,"--list","--edit") : ($_) } @_ if 1 < !!$list + !!$edit + !!$get + !!$action_1_arg + !!$action_2_arg + !!$action_1_2_arg + !!$action_2_3_arg;
        # error: only one config file at a time:
        map { ($_ eq "config" and ++$help) ? ($_,"--system","--global") : ($_) } @_ if 1 < !!$file + !!$descent + !!$global + !!$system + !!$local;
        # If it's not an implemented action or unrecognized args, then just let the regular git handle it:
        return @ARGV = @_ if !$help && !$list && !$get;
        if ($local and !$origin and !$scope and !git_min(1.7.4)) { # Don't spew: error: unknown option `local'
            my $gd = git_dir or die "fatal: --local can only be used inside a git repository\n";
            # Swap "--local" for "--file <$GIT_DIR/config>" to act like the "--local" option in case of git < 1.7.4
            @_ = map { $_ =~ /^--lo/ ? ("--file" => "$gd/config") : ($_) } @_;
        }
        my $exit_status;
        # Keep STDERR in case the usage spewage needs to be tweaked to mention the --descent option.
        my $err;
        my $out = do {
            pipe my $rd_err, my $wr_err;
            local $HANDLE_STDERR = sub { close $rd_err; eval { $wr_err->blocking(0) }; open STDERR, ">&", $wr_err };
            # Hopefully the kernel can hold the entire STDERR within the pipe to avoid deadlock
            my $stdout = git @_;
            $exit_status = $? >> 8;
            close $wr_err;
            $err = join "", <$rd_err>;
            $stdout;
        };
        exit $exit_status if !length $out and !length $err;

        if ($help or !length $out or $err =~ /usage: git config/) {
            # Looks like the USAGE Spewage:
            $err =~ s/^error: unknown option .usage.\s*//m;
            $err =~ s/(Config file location\n)(( +)(\-.*global)(  +)\S.*global)/"$1"."$3--descent".(" "x(length("$4$5")-9))."use descent .gitconfig files\n$2"/e;
            $err =~ s/\n?$/\n/;
            warn $err;
            print $out;
            exit $exit_status;
        }
        # No more munging STDERR so just spit it out, if any
        warn $err if $err;
        # XXX - Do we also need to more-gracefully handle --show-scope or --show-origin with --get or --get-all or --get-regexp?
        if (!$list) {
            print $out;
            exit $exit_status;
        }

        # Delim chars for --list output
        my $top = $null ? "\x00" : "\n"; # Beginning char of the entry
        my $end = $null ? "\x00" : "\t"; # Ending char for each field
        if ($descent) {
            # Special --descent --list means to list ALL descent config file settings (not just the closest one)
            # But skip all other --system and --global and --local and commandline config settings
            my $descent_left = [ @$descent_config_files ];
            my $d = pop @$descent_left; # Already did the last one
            while (my $config_file = pop @$descent_left) {
                my @run = map { $_ eq $d ? $config_file : $_ } @_;
                $out = git(@run) . $out;
            }
            $out = $top.$out;
            if ($scope) {
                my $this_scope = "descent";
                $out =~ s/($top)\w+($end)/$top$this_scope$end/g;
            }
            # Remove the first $top char
            $out =~ s/^$top//;
            print $out;
            exit $exit_status;
        }

        if ($scope || $origin and $ENV{GIT_CLIENT_OVERRIDE}) {
            # Fix up --show-scope and --show-origin output for any "descent" files.
            my $local_dir = "file:".(git_dir || ".git")."/config";
            $out = $top.$out;
            my $descent_counter = 0;
            my $descent_origins = [ map {"file:$_"} split /\n/, $ENV{GIT_CLIENT_OVERRIDE} ];
            foreach my $this_origin (@$descent_origins, $local_dir) {
                # Wrap around back to ZERO if hit the end
                $descent_counter = 0 if ++$descent_counter > @$descent_origins;
                my $s = "command";        # Old Scope
                my $o = "command line:";  # Old Origin
                my $this_scope = $descent_counter ? "descent" : "local";
                for (1..$ENV{"GIT_CLIENT_OVERRIDE_$descent_counter"}) {
                    if ($scope && $origin) {
                        $out =~ s/$top$s$end$o$end/$top$this_scope$end$this_origin$end/;
                    }
                    elsif ($scope) {
                        $out =~ s/$top$s$end/$top$this_scope$end/;
                    }
                    elsif ($origin) {
                        $out =~ s/$top$o$end/$top$this_origin$end/;
                    }
                    else {
                        # Nothing "--show"ing in order to unmunge back to its true "descent" reference
                    }
                }
            }
            # Remove the first $top char
            $out =~ s/^$top//;
        }
        print $out;
        exit $exit_status;
    }
}
