summaryrefslogtreecommitdiff
path: root/contrib/continuous/post-receive-cinotify
blob: b8f5a609af464f3af8b624246cc69eb335ce81d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/perl
#
# A hook that notifies its companion cidaemon through a simple
# queue file that a ref has been updated via a push (actually
# by a receive-pack running on the server).
#
# See cidaemon for per-repository configuration details.
#
# To use this hook, add it as the post-receive hook, make it
# executable, and set its configuration options.
#

local $ENV{PATH} = '/opt/git/bin';

use strict;
use warnings;
use File::Spec;
use Storable qw(retrieve nstore);
use Fcntl ':flock';

my $git_dir = File::Spec->rel2abs($ENV{GIT_DIR});
my $queue_name = `git config --get builder.queue`;chop $queue_name;
$queue_name =~ m,^([^\s]+)$,; $queue_name = $1; # untaint
unless ($queue_name) {
	1 while <STDIN>;
	print STDERR "\nerror: builder.queue not set.  Not enqueing.\n\n";
	exit;
}
my $queue_lock = "$queue_name.lock";

my @skip;
open S, "git config --get-all builder.skip|";
while (<S>) {
	chop;
	push @skip, $_;
}
close S;

my @new_branch_base;
open S, "git config --get-all builder.newBranchBase|";
while (<S>) {
	chop;
	push @new_branch_base, $_;
}
close S;

sub skip ($)
{
	local $_ = shift;
	foreach my $p (@skip) {
		return 1 if /^$p/;
	}
	0;
}

open LOCK, ">$queue_lock" or die "Can't open $queue_lock: $!";
flock LOCK, LOCK_EX;

my $queue = -f $queue_name ? retrieve $queue_name : [];
my %existing;
foreach my $r (@$queue) {
	my ($gd, $ref) = @$r;
	$existing{$gd}{$ref} = $r;
}

my @new_branch_commits;
my $loaded_new_branch_commits = 0;

while (<STDIN>) {
	chop;
	my ($old, $new, $ref) = split / /, $_, 3;

	next if $old eq $new;
	next if $new =~ /^0{40}$/;
	next if skip $ref;

	my $r = $existing{$git_dir}{$ref};
	if ($r) {
		$r->[3] = $new;
	} else {
		if ($old =~ /^0{40}$/) {
			if (!$loaded_new_branch_commits && @new_branch_base) {
				open M,'-|','git','show-ref',@new_branch_base;
				while (<M>) {
					($_) = split / /, $_;
					push @new_branch_commits, $_;
				}
				close M;
				$loaded_new_branch_commits = 1;
			}
			$old = [@new_branch_commits];
		} else {
			$old = [$old];
		}

		$r = [$git_dir, $ref, $old, $new];
		$existing{$git_dir}{$ref} = $r;
		push @$queue, $r;
	}
}
nstore $queue, $queue_name;

flock LOCK, LOCK_UN;
close LOCK;