2009-08-25 05:14:46 +02:00
|
|
|
#!/usr/bin/perl
|
2009-08-23 18:10:03 +02:00
|
|
|
|
|
|
|
use strict;
|
2009-08-25 05:14:46 +02:00
|
|
|
use warnings;
|
2009-08-23 18:10:03 +02:00
|
|
|
|
|
|
|
# === update ===
|
2009-08-26 02:47:27 +02:00
|
|
|
# this is gitolite's update hook
|
2009-08-23 18:10:03 +02:00
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
# find the rc file, then pull the libraries
|
|
|
|
# ----------------------------------------------------------------------------
|
2009-08-23 18:10:03 +02:00
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
BEGIN {
|
2011-02-12 16:25:34 +01:00
|
|
|
# people with shell access should be allowed to bypass the update hook,
|
|
|
|
# simply by setting an env var that the ssh "front door" will never set
|
|
|
|
exit 0 if exists $ENV{GL_BYPASS_UPDATE_HOOK};
|
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
die "ENV GL_RC not set\n" unless $ENV{GL_RC};
|
|
|
|
die "ENV GL_BINDIR not set\n" unless $ENV{GL_BINDIR};
|
|
|
|
}
|
2009-08-23 18:10:03 +02:00
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
use lib $ENV{GL_BINDIR};
|
|
|
|
use gitolite_rc;
|
|
|
|
use gitolite qw(:DEFAULT %repos);
|
2009-08-23 18:10:03 +02:00
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------
|
2011-01-15 16:39:56 +01:00
|
|
|
# start...
|
2009-08-23 18:10:03 +02:00
|
|
|
# ----------------------------------------------------------------------------
|
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
my ($perm, $creator, $wild) = repo_rights($ENV{GL_REPO});
|
|
|
|
my $reported_repo = $ENV{GL_REPO} . ( $wild ? " ($wild)" : "" );
|
2010-03-14 17:37:34 +01:00
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
# arguments are as supplied to an update hook by git; man githooks
|
|
|
|
my ($ref, $oldsha, $newsha) = @ARGV;
|
2009-08-23 18:10:03 +02:00
|
|
|
my $merge_base = '0' x 40;
|
2009-08-25 03:08:05 +02:00
|
|
|
# compute a merge-base if both SHAs are non-0, else leave it as '0'x40
|
|
|
|
# (i.e., for branch create or delete, merge_base == '0'x40)
|
2009-08-23 18:10:03 +02:00
|
|
|
chomp($merge_base = `git merge-base $oldsha $newsha`)
|
2009-08-25 03:08:05 +02:00
|
|
|
unless $oldsha eq '0' x 40
|
|
|
|
or $newsha eq '0' x 40;
|
2009-08-23 18:10:03 +02:00
|
|
|
|
2010-05-10 08:16:47 +02:00
|
|
|
# att_acc == attempted access -- what are you trying to do? (is it 'W' or '+'?)
|
|
|
|
my $att_acc = 'W';
|
2009-08-25 03:08:05 +02:00
|
|
|
# rewriting a tag is considered a rewind, in terms of permissions
|
2010-05-10 08:16:47 +02:00
|
|
|
$att_acc = '+' if $ref =~ m(refs/tags/) and $oldsha ne ('0' x 40);
|
2009-08-28 17:11:21 +02:00
|
|
|
# non-ff push to ref
|
|
|
|
# notice that ref delete looks like a rewind, as it should
|
2010-05-10 08:16:47 +02:00
|
|
|
$att_acc = '+' if $oldsha ne $merge_base;
|
2009-08-23 18:10:03 +02:00
|
|
|
|
2010-03-30 16:31:49 +02:00
|
|
|
# were any 'D' perms specified? If they were, it means we have to separate
|
|
|
|
# deletes from rewinds, so if the new sha is all 0's, change the '+' to a 'D'
|
2010-05-10 08:16:47 +02:00
|
|
|
$att_acc = 'D' if ( $repos{$ENV{GL_REPO}}{DELETE_IS_D} or $repos{'@all'}{DELETE_IS_D} ) and $newsha eq '0' x 40;
|
2010-06-18 16:44:40 +02:00
|
|
|
# similarly C for create a branch
|
|
|
|
$att_acc = 'C' if ( $repos{$ENV{GL_REPO}}{CREATE_IS_C} or $repos{'@all'}{CREATE_IS_C} ) and $oldsha eq '0' x 40;
|
2010-03-30 16:31:49 +02:00
|
|
|
|
2009-08-27 09:44:47 +02:00
|
|
|
my @allowed_refs;
|
2010-03-23 17:50:34 +01:00
|
|
|
# @all repos: see comments in similar code in check_access
|
2009-09-18 14:30:14 +02:00
|
|
|
push @allowed_refs, @ { $repos{$ENV{GL_REPO}}{$ENV{GL_USER}} || [] };
|
2010-03-23 17:50:34 +01:00
|
|
|
push @allowed_refs, @ { $repos{'@all'} {$ENV{GL_USER}} || [] };
|
2009-09-18 14:30:14 +02:00
|
|
|
push @allowed_refs, @ { $repos{$ENV{GL_REPO}}{'@all'} || [] };
|
2011-10-07 05:47:38 +02:00
|
|
|
push @allowed_refs, @ { $repos{'@all'} {'@all'} || [] };
|
2009-11-16 14:55:34 +01:00
|
|
|
|
2009-11-16 17:15:55 +01:00
|
|
|
# prepare the list of refs to be checked
|
|
|
|
|
|
|
|
# previously, we just checked $ref -- the ref being updated, which is passed
|
2010-01-07 13:29:34 +01:00
|
|
|
# to us by git (see man githooks). Now we also have to treat each NAME being
|
|
|
|
# updated as a potential "ref" and check that, if NAME-based restrictions have
|
2009-11-16 17:15:55 +01:00
|
|
|
# been specified
|
|
|
|
|
|
|
|
my @refs = ($ref); # the first ref to check is the real one
|
2010-03-23 17:50:34 +01:00
|
|
|
# because making it work screws up efficiency like no tomorrow...
|
2010-01-07 13:29:34 +01:00
|
|
|
if (exists $repos{$ENV{GL_REPO}}{NAME_LIMITS}) {
|
2009-11-16 17:15:55 +01:00
|
|
|
# this is special to git -- the hash of an empty tree
|
|
|
|
my $empty='4b825dc642cb6eb9a060e54bf8d69288fbee4904';
|
|
|
|
# well they're not really "trees" but $empty is indeed the empty tree so
|
|
|
|
# we can just pretend $oldsha/$newsha are also trees, and anyway 'git
|
|
|
|
# diff' only wants trees
|
|
|
|
my $oldtree = $oldsha eq '0' x 40 ? $empty : $oldsha;
|
|
|
|
my $newtree = $newsha eq '0' x 40 ? $empty : $newsha;
|
2010-01-07 13:29:34 +01:00
|
|
|
push @refs, map { chomp; s/^/NAME\//; $_; } `git diff --name-only $oldtree $newtree`;
|
2009-11-16 17:15:55 +01:00
|
|
|
}
|
|
|
|
|
2011-01-15 16:39:56 +01:00
|
|
|
# we potentially have many "refs" to check. The one we print in the log is
|
|
|
|
# the *first* one (which is a *real* ref, like refs/heads/master), while all
|
|
|
|
# the rest (if they exist) are like NAME/something. So we do the first one
|
|
|
|
# separately to capture it, then run the rest (if any)
|
2010-05-10 08:16:47 +02:00
|
|
|
my $log_refex = check_ref(\@allowed_refs, $ENV{GL_REPO}, (shift @refs), $att_acc);
|
2011-01-15 16:39:56 +01:00
|
|
|
check_ref (\@allowed_refs, $ENV{GL_REPO}, $_ , $att_acc) for @refs;
|
2009-11-16 14:55:34 +01:00
|
|
|
|
2011-04-25 16:51:37 +02:00
|
|
|
# if we returned at all, all the checks succeeded. Check secondary hooks now
|
|
|
|
$UPDATE_CHAINS_TO ||= 'hooks/update.secondary';
|
2011-05-28 16:56:55 +02:00
|
|
|
-x $UPDATE_CHAINS_TO and system ( $UPDATE_CHAINS_TO, @ARGV ) and die "$UPDATE_CHAINS_TO died\n";
|
2009-11-16 14:55:34 +01:00
|
|
|
|
2011-04-25 16:51:37 +02:00
|
|
|
# now log it and exit 0 so git can get on with it
|
2011-01-15 16:39:56 +01:00
|
|
|
log_it("", "$att_acc\t" . substr($oldsha, 0, 14) . "\t" . substr($newsha, 0, 14) .
|
2010-06-16 03:50:12 +02:00
|
|
|
"\t$reported_repo\t$ref\t$log_refex");
|
2010-03-14 17:37:34 +01:00
|
|
|
|
2009-11-16 14:55:34 +01:00
|
|
|
exit 0;
|