You can use your favorite scripting language to create git2
command, that will check if you're running it with arguments commit -a
. If that is the case, run /usr/bin/git status --porcelain
and check if there are some changes to the index (porcelain-formatted output is easier to parse, my first guess would be to run it through grep '^[AMD]'
and check whether it found something. Now, you can either print a warning and quit, or run /usr/bin/git
which all original argument to continue like there was no git2 (which is also what you do if you haven't been run with commit -a).
Here's an example in Perl (which git requires anyway):
#!/usr/bin/env perl
use strict;
use warnings;
my %aliases = map { split(/\n/, $_, 2) }
split /\0/,
`git config -z --get-regexp alias\\.`;
my %commit_aliases = (( commit => 1 ),
map { s/alias\.//; $_ => 1 }
grep $aliases{$_} =~ /^commit\b/,
keys %aliases);
my ($command, @args) = @ARGV;
if ($commit_aliases{$command} && $args[0] =~ /^-a|^--all/) {
my @staged = grep /^M/, split /\0/, `git status -z`;
if (@staged) {
print "There are staged changes, are you sure you want to commit all? (y/N) ";
chomp(my $answer = <STDIN>);
if ($answer =~ /^y/i) {
run_command()
}
} else {
run_command()
}
} else {
run_command()
}
sub run_command {
system 'git', $command, @args;
exit $? >> 8;
}
Then, create a bash alias alias git2 git
and you're all set.