Commit a0da061a authored by Ryan Jackson's avatar Ryan Jackson

Updated git-checkout-push

Made some changes to git-checkout-push:

- Should now work on older git versions
- Branch name is no longer hard-coded
- Can be configured to stash unsaved changes rather than abort
- Checks to make sure repository isn't bare and fails if it is
parent ac4be3dd
......@@ -5,6 +5,8 @@
# All rights reserved.
#
use POSIX qw(strftime);
#
# Simple script to do a checkout whenever the 'master' branch is pushed
# to. Intended for situations where you have people pushing up to a central
......@@ -14,44 +16,60 @@
# git config receive.denyCurrentBranch ignore
#
#
# TODO:
# Support for automatically determining the branch that's currently checked
# out, rather than hardcoding a single branch
# Support for stashing or merging uncommitted changes, rather than just
# failing
#
#
# Note: Does not hanle the case where the master branch is being deleted. This
# probably does not matter!
#
my $BRANCH = "master";
my $GIT = "git";
my $UNSAFE = 0;
my $STASH_CHANGES = 0;
my $GIT_STASH_OPTS = '';
my $BRANCH = `git rev-parse --symbolic-full-name HEAD`;
my $IS_BARE_REPO = `$GIT rev-parse --is-bare-repository`;
my $IN_WORK_TREE = `$GIT rev-parse --is-inside-work-tree`;
my $IN_GIT_DIR = `$GIT rev-parse --is-inside-git-dir`;
chomp $BRANCH;
chomp $IN_WORK_TREE;
chomp $IN_GIT_DIR;
chomp $IS_BARE_REPO;
#
# For reasons not clear to me, one has to tell git where the actual working
# tree is - this makes some layout assumptions that probably work most of
# the time
#
$ENV{GIT_WORK_TREE} = "$ENV{GIT_DIR}/../";
if ($IS_BARE_REPO eq 'true') {
print "*** ERROR: Repository being pushed to is bare ";
print "(has no work tree), checkout aborted\n";
exit(1);
} elsif ($IN_WORK_TREE ne 'true') {
print "*** ERROR: Repository being pushed to is not ";
print "inside work tree; checkout aborted\n";
exit(1);
}
if ($IN_GIT_DIR eq 'true') {
chdir "$ENV{GIT_DIR}/..";
delete $ENV{GIT_DIR};
}
#
# Look through all of the changed references to see if any was for the master
# branch
#
my $master_pushed = 0;
my $branch_pushed = 0;
foreach my $refline (<STDIN>) {
chomp $refline;
my ($oldrev, $newrev, $refname) = split(/\s+/, $refline);
if ($refname eq "refs/heads/$BRANCH") {
$master_pushed = 1;
if ($refname eq $BRANCH) {
$branch_pushed = 1;
}
}
if ($master_pushed) {
if ($branch_pushed) {
print "Checking out new $BRANCH\n";
#
......@@ -59,9 +77,23 @@ if ($master_pushed) {
# the checkout, so that we can give a reasonable error message
#
if (!$UNSAFE && system("$GIT diff-files --quiet -q --exit-code")) {
print "*** ERROR: Repository being pushed to has uncommited ";
print "changes, checkout aborted\n";
exit(1);
if ($STASH_CHANGES) {
my $stash_msg = "git-checkout-push: stashed changes to $BRANCH " .
"on %a %b %e %H:%M:%S %Y";
$stash_msg = strftime $stash_msg, localtime;
print "*** WARNING: Repository being pushed to has uncommitted ";
print "changes, stashing them.\n";
system("$GIT stash save $GIT_STASH_OPTS \"$stash_msg\"");
# git-stash will automatically do a hard reset, and since
# the branch has already been updated there's nothing left
# to do.
exit(0);
} else {
print "*** ERROR: Repository being pushed to has uncommitted ";
print "changes, checkout aborted\n";
exit(1);
}
}
#
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment