Skip to content

Instantly share code, notes, and snippets.

@ericthelin
Last active April 26, 2016 14:45
Show Gist options
  • Save ericthelin/07f1fadcf2eccda10d510720cc2abb04 to your computer and use it in GitHub Desktop.
Save ericthelin/07f1fadcf2eccda10d510720cc2abb04 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Protect against using git push by mistake without getting a code review through gerrit first
# install by adding the following to your .bashrc (or .zshrc) (after changing the path for the location of the script):
# alias git="$HOME/git-override.pl"
# this also relies on having a git alias for pushing to gerrit in place called "push-refs". Create that by adding the
# following to the [alias] section of your ~/.gitconfig:
# push-refs = push origin HEAD:refs/for/master
use strict;
use warnings;
my $path_to_git = `which git`;
chomp($path_to_git);
my $use_gerrit = 0;
my $git_remote = `git config --get remote.origin.url`;
if ($git_remote=~/gerrit/) {
$use_gerrit = 1;
}
if ($use_gerrit && scalar(@ARGV) == 1 && $ARGV[0] eq 'push') {
print "\n";
printf "\a";
print " Did you get code reviewed? (y/N) ";
my $line = readline(*STDIN);
if ($line=~/^[yY]/) {
system($path_to_git, @ARGV);
} else {
print " Did you mean git push-refs? (Y/n) ";
my $line = readline(*STDIN);
if ($line!~/^[nN]/) {
system($path_to_git, ("push-refs"));
} else {
print " If you are sure you want to push anyway type \"push\": ";
my $line = readline(*STDIN);
if ($line=~/^push/i) {
system($path_to_git, @ARGV);
} else {
print "\nYou don't seem sure... Aborting!\n\n";
}
}
exit;
}
} else {
system($path_to_git, @ARGV);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment