| 1 | #!/usr/bin/perl |
|---|
| 2 | # ------------------------------------------------------------------------------ |
|---|
| 3 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
|---|
| 4 | # |
|---|
| 5 | # This file is part of FCM, tools for managing and building source code. |
|---|
| 6 | # |
|---|
| 7 | # FCM is free software: you can redistribute it and/or modify |
|---|
| 8 | # it under the terms of the GNU General Public License as published by |
|---|
| 9 | # the Free Software Foundation, either version 3 of the License, or |
|---|
| 10 | # (at your option) any later version. |
|---|
| 11 | # |
|---|
| 12 | # FCM is distributed in the hope that it will be useful, |
|---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | # GNU General Public License for more details. |
|---|
| 16 | # |
|---|
| 17 | # You should have received a copy of the GNU General Public License |
|---|
| 18 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | # ------------------------------------------------------------------------------ |
|---|
| 20 | |
|---|
| 21 | use strict; |
|---|
| 22 | use warnings; |
|---|
| 23 | |
|---|
| 24 | use FindBin; |
|---|
| 25 | use lib "$FindBin::Bin/../lib"; |
|---|
| 26 | use FCM::Admin::System qw{verify_users}; |
|---|
| 27 | use FCM::System::CM::SVN; |
|---|
| 28 | use FCM::Util; |
|---|
| 29 | |
|---|
| 30 | our @IGNORES = qw{Config Rel Share}; |
|---|
| 31 | |
|---|
| 32 | my $UTIL = $FCM::Admin::System::UTIL; |
|---|
| 33 | my $CM_SYS = FCM::System::CM::SVN->new({'util' => $UTIL}); |
|---|
| 34 | |
|---|
| 35 | if (!caller()) { |
|---|
| 36 | main(@ARGV); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub main { |
|---|
| 40 | local(@ARGV) = @_; |
|---|
| 41 | my ($repos, $txn) = @ARGV; |
|---|
| 42 | |
|---|
| 43 | my %layout_config = $CM_SYS->load_layout_config('file://' . $repos); |
|---|
| 44 | if (!$layout_config{'level-owner-branch'}) { |
|---|
| 45 | return; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | # Get list of new paths |
|---|
| 49 | my %lines_of; # $lines_of{$owner} = [$path, ...] |
|---|
| 50 | LINE: |
|---|
| 51 | for my $line ($CM_SYS->stdout(qw{svnlook changed -t}, $txn, $repos)) { |
|---|
| 52 | my $status = substr($line, 0, 1); |
|---|
| 53 | if ($status eq 'A') { |
|---|
| 54 | my $path = substr($line, 4); |
|---|
| 55 | # $is_local=1 |
|---|
| 56 | my $layout = $CM_SYS->get_layout_common($repos, $txn, $path, 1); |
|---|
| 57 | my $owner = $layout->get_branch_owner(); |
|---|
| 58 | if ($owner && !grep {$_ eq $owner} @IGNORES) { |
|---|
| 59 | if (!exists($lines_of{$owner})) { |
|---|
| 60 | $lines_of{$owner} = []; |
|---|
| 61 | } |
|---|
| 62 | push(@{$lines_of{$owner}}, $line); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | # Verify branch owners, if necessary |
|---|
| 68 | my @bad_users = verify_users(keys(%lines_of)); |
|---|
| 69 | for my $bad_user (@bad_users) { |
|---|
| 70 | for my $line (@{$lines_of{$bad_user}}) { |
|---|
| 71 | warn("[INVALID BRANCH OWNER] $line\n"); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | exit(scalar(@bad_users)); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | 1; |
|---|
| 78 | __END__ |
|---|
| 79 | |
|---|
| 80 | =head1 NAME |
|---|
| 81 | |
|---|
| 82 | pre-commit-verify-branch-owner |
|---|
| 83 | |
|---|
| 84 | =head1 SYNOPSIS |
|---|
| 85 | |
|---|
| 86 | pre-commit-verify-branch-owner REPOS TXN |
|---|
| 87 | |
|---|
| 88 | =head1 ARGUMENTS |
|---|
| 89 | |
|---|
| 90 | Accept the same arguments as a Subversion pre-commit hook. |
|---|
| 91 | |
|---|
| 92 | =head1 DESCRIPTION |
|---|
| 93 | |
|---|
| 94 | This program ensures that users create a branch with its owner correctly set. |
|---|
| 95 | |
|---|
| 96 | =head1 COPYRIGHT |
|---|
| 97 | |
|---|
| 98 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
|---|
| 99 | |
|---|
| 100 | =cut |
|---|