#!/usr/bin/perl -w
use strict;
use Git;
binmode(STDOUT, ":raw");
my $repo = Git->repository();
my $menu_use_color = $repo->get_colorbool('color.interactive');
my ($prompt_color, $header_color, $help_color) =
$menu_use_color ? (
$repo->get_color('color.interactive.prompt', 'bold blue'),
$repo->get_color('color.interactive.header', 'bold'),
$repo->get_color('color.interactive.help', 'red bold'),
) : ();
my $error_color = ();
if ($menu_use_color) {
my $help_color_spec = ($repo->config('color.interactive.help') or
'red bold');
$error_color = $repo->get_color('color.interactive.error',
$help_color_spec);
}
my $diff_use_color = $repo->get_colorbool('color.diff');
my ($fraginfo_color) =
$diff_use_color ? (
$repo->get_color('color.diff.frag', 'cyan'),
) : ();
my ($diff_plain_color) =
$diff_use_color ? (
$repo->get_color('color.diff.plain', ''),
) : ();
my ($diff_old_color) =
$diff_use_color ? (
$repo->get_color('color.diff.old', 'red'),
) : ();
my ($diff_new_color) =
$diff_use_color ? (
$repo->get_color('color.diff.new', 'green'),
) : ();
my $normal_color = $repo->get_color("", "reset");
my $use_readkey = 0;
sub ReadMode;
sub ReadKey;
if ($repo->config_bool("interactive.singlekey")) {
eval {
require Term::ReadKey;
Term::ReadKey->import;
$use_readkey = 1;
};
}
sub colored {
my $color = shift;
my $string = join("", @_);
if (defined $color) {
# Put a color code at the beginning of each line, a reset at the end
# color after newlines that are not at the end of the string
$string =~ s/(\n+)(.)/$1$color$2/g;
# reset before newlines
$string =~ s/(\n+)/$normal_color$1/g;
# codes at beginning and end (if necessary):
$string =~ s/^/$color/;
$string =~ s/$/$normal_color/ unless $string =~ /\n$/;
}
return $string;
}
# command line options
my $patch_mode;
sub run_cmd_pipe {
if ($^O eq 'MSWin32' || $^O eq 'msys') {
my @invalid = grep {m/[":*]/} @_;
die "$^O does not support: @invalid\n" if @invalid;
my @args = map { m/ /o ? "\"$_\"": $_ } @_;
return qx{@args};
} else {
my $fh = undef;
open($fh, '-|', @_) or die;
return <$fh>;
}
}
my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
if (!defined $GIT_DIR) {
exit(1); # rev-parse would have already said "not a git repo"
}
chomp($GIT_DIR);
my %cquote_map = (
"b" => chr(8),
"t" => chr(9),
"n" => chr(10),
"v" => chr(11),
"f" => chr(12),
"r" => chr(13),
"\\" => "\\",
"\042" => "\042",
);
sub unquote_path {
local ($_) = @_;
my ($retval, $remainder);
if (!/^\042(.*)\042$/) {
return $_;
}
($_, $retval) = ($1, "");
while (/^([^\\]*)\\(.*)$/) {
$remainder = $2;
$retval .= $1;
for ($remainder) {
if (/^([0-3][0-7][0-7])(.*)$/) {
$retval .= chr(oct($1));
$_ = $2;
last;
}
if (/^([\\\042btnvfr])(.*)$/) {
$retval .= $cquote_map{$1};
$_ = $2;
last;
}
# This is malformed -- just return it as-is for now.
return $_[0];
}
$_ = $remainder;
}
$retval .= $_;
return $retval;
}
sub refresh {
my $fh;
open $fh, 'git update-index --refresh |'
or die;
while (<$fh>) {
;# ignore 'needs update'
}
close $fh;
}
sub list_untracked {
map {
chomp $_;
unquote_path($_);
}
run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
}
my $status_fmt = '%12s %12s %s';
my $status_head = sprintf($status_fmt, 'staged', 'unstaged', 'path');
{
my $initial;
sub is_initial_commit {
$initial = system('git rev-parse HEAD -- >/dev/null 2>&1') != 0
unless defined $initial;
return $initial;
}
}
sub get_empty_tree {
return '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
}
# Returns list of hashes, contents of each of which are:
# VALUE: pathname
# BINARY: is a binary path
# INDEX: is index different from HEAD?
# FILE: is file different from index?
# INDEX_ADDDEL: is it add/delete between HEAD and index?
# FILE_ADDDEL: is it add/delete between index and file?
sub list_modified {
my ($only) = @_;
my (%data, @return);
my ($add, $del, $adddel, $file);
my @tracked = ();
if (@ARGV) {
@tracked = map {
chomp $_;
unquote_path($_);
} run_cmd_pipe(qw(git ls-files --exclude-standard --), @ARGV);
return if (!@tracked);
}
my $reference = is_initial_commit() ? get_empty_tree() : 'HEAD';
for (run_cmd_pipe(qw(git diff-index --cached
--numstat --summary), $reference,
'--', @tracked)) {
if (($add, $del, $file) =
/^([-\d]+) ([-\d]+) (.*)/) {
my ($change, $bin);
$file = unquote_path($file);
if ($add eq '-' && $del eq '-') {
$change = 'binary';
$bin = 1;
}
else {
$change = "+$add/-$del";
}
$data{$file} = {
INDEX => $change,
BINARY => $bin,
FILE => 'nothing',
}
}
elsif (($adddel, $file) =
/^ (create|delete) mode [0-7]+ (.*)$/) {
$file = unquote_path($file);
$data{$file}{INDEX_ADDDEL} = $adddel;
}
}
for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
if (($add, $del, $file) =
/^([-\d]+) ([-\d]+) (.*)/) {
$file = unquote_path($file);
if (!exists $data{$file}) {
$data{$file} = +{
INDEX => 'unchanged',
BINARY => 0,
};
}
my ($change, $bin);
if ($add eq '-' && $del
|