App-Git-Autofixup
view release on metacpan or search on metacpan
git-autofixup view on Meta::CPAN
if ($hunk_count == 0) {
$rc = 3; # no hunks to assign
} elsif ($assigned_hunk_count == 0) {
$rc = 2; # hunks exist, but none assigned
} elsif ($assigned_hunk_count < $hunk_count) {
$rc = 1; # not all hunks assigned
} elsif ($hunk_count == $assigned_hunk_count) {
$rc = 0; # all hunks assigned
} else {
die "unexpected conditions when choosing exit code";
}
return $rc;
}
# Create a temporary index so we can craft commits with already-staged hunks.
# Return a File::Temp object so the caller has control over its lifetime.
sub create_temp_index {
my $old_index = shift;
my $tempfile = File::Temp->new(
TEMPLATE => 'git-autofixup_index.XXXXXX',
DIR => File::Spec->tmpdir());
# The index ought to be equivalent to HEAD. The fastest way to create it
# is to start with the current index, and subtract the changes since HEAD.
if (not defined($old_index)) {
my $gitdir = git_dir();
$old_index = "$gitdir/index";
}
close $tempfile or die "close temp index: $!";
copy($old_index, $tempfile->filename()) or die "Can't copy Git index '$old_index' to '$tempfile': $!\n";
$ENV{GIT_INDEX_FILE} = $tempfile->filename();
# Remove any staged changes from the new index - we want to turn them into fixup commits.
my $index_changes = qx(git diff-index --patch --no-prefix --no-ext-diff --ignore-submodules --cached HEAD);
die "git diff-index: " . child_error_desc($?) if $?;
open my $fh, '|-', git_cmd(qw(apply -p0 --cached --whitespace=nowarn --reverse -)) or die "run git apply: $!\n";
print $fh $index_changes;
close $fh or die "git apply: " . child_error_desc($?) . "\n";
return $tempfile;
}
# Create a grafts file for `git blame -S` that basically says the upstream
# commit doesn't have any parents, resulting in blame only searching back as
# far back as the upstream commit.
sub create_grafts_file {
my @upstreams = @_;
my $grafts_file = File::Temp->new(
TEMPLATE => 'git-autofixup_grafts.XXXXXX',
DIR => File::Spec->tmpdir());
open(my $fh, '>', $grafts_file) or die "Can't open $grafts_file: $!\n";
for (@upstreams) {
print $fh $_, "\n";
}
close($fh) or die "close grafts file: $!\n";
return $grafts_file;
}
sub rev_parse {
my $rev = shift;
my ($out, $err, $exit_code) = capture(qw(git rev-parse --verify --end-of-options), $rev);
if ($exit_code != 0) {
warn "git rev-parse: $err\n";
die "Can't resolve given revision\n";
}
chomp $out;
return $out;
}
sub main {
$VERBOSE = 0;
my $help;
my $man;
my $show_version;
my $strict = $CONTEXT;
my $num_context_lines = 3;
my $dryrun;
my $use_detailed_exit_codes;
GetOptions(
'h' => \$help,
'help' => \$man,
'version' => \$show_version,
'verbose|v+' => \$VERBOSE,
'strict|s=i' => \$strict,
'context|c=i' => \$num_context_lines,
'dryrun|n' => \$dryrun,
'gitopt|g=s' => \@GIT_OPTIONS,
'exit-code' => \$use_detailed_exit_codes,
) or return 1;
if ($help) {
print $usage;
return 0;
}
if ($show_version) {
print "$VERSION\n";
return 0;
}
if ($man) {
eval {
require Pod::Usage;
};
if ($@) {
die <<'EOF';
Pod::Usage unavailable for formatting the manual. The manual can be found at
the end of the git-autofixup script.
EOF
}
Pod::Usage::pod2usage(-exitval => 0, -verbose => 2);
}
if (@GIT_OPTIONS) {
warn <<'EOF';
--gitopt|-g is deprecated and will be removed in a future release. Please use
the GIT_CONFIG_{COUNT,KEY,VALUE} environment variables instead; see `git help
config`.
EOF
}
# "upstream" revisions as 40 byte SHA1 hex hashes.
my @upstreams = ();
if (@ARGV == 1) {
( run in 1.050 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )