/
post-commit.pl
executable file
·145 lines (118 loc) · 4.14 KB
/
post-commit.pl
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
# POST-COMMIT HOOK
#
# [1] REPOS-PATH (the path to this repository)
# [2] REV (the number of the revision just committed)
#
# Must be chdired to the tools subdirectory when this is run
#
use strict;
use warnings;
my $REPOS = $ARGV[0];
my $BUGS = '/home/foswiki.org/public_html/data/Tasks';
my $SUPPORT = '/home/svn';
my $verbose = 0; # 1 to debug
my $first = 1;
if ( open( F, '<', "$SUPPORT/lastupdate" ) ) {
local $/ = "\n";
$first = <F>;
chomp($first);
close(F);
}
my $last = $ARGV[1] || `/usr/local/bin/svnlook youngest $REPOS`;
chomp($last);
#my $BRANCH = $ARGV[2]; # Not used
die unless $last;
#die unless $BRANCH; ] Not used
$first ||= ( $last - 1 );
print "F:$first L:$last\n" if $verbose;
my @changes;
for ( my $i = $first + 1 ; $i <= $last ; $i++ ) {
push(
@changes,
# No filter since change to /trunk/
# map { s/^.*?$BRANCH\///; $_ }
# grep { /branches\/$BRANCH/ }
split( /\n/, `/usr/local/bin/svnlook changed -r $i $REPOS` )
);
}
print scalar(@changes), " changes\n" if $verbose;
exit 0 unless scalar(@changes);
sub _add {
my ( $cur, $rev, $changed ) = @_;
my %curr = map { $_ => 1 } grep { /^\d+$/ }
map { s/^(TWikirev|Nextwikirev|Foswikirev|Rev)://i; $_ }
split( /\s+/, $cur );
$curr{$rev} = 1;
my @list = sort { $a <=> $b } keys %curr; # numeric sort
my $new = join( " ", map { "Foswikirev:$_" } @list );
$$changed = 1 if $cur ne $new;
return $new;
}
# Don't know where STDERR goes, so send it somewhere we can read it
open( STDERR, '>>', "$SUPPORT/logs/post-commit.log" ) || die $!;
print STDERR "Post-Commit $first..$last in $REPOS\n";
$/ = undef;
for my $rev ( $first .. $last ) {
# Update the list of checkins for referenced bugs
my $logmsg = `/usr/local/bin/svnlook log -r $rev $REPOS`;
my $committer = `/usr/local/bin/svnlook author -r $rev $REPOS`;
#SMELL: Can't use chomp - $/ is undef
$committer =~ s/\n$//;
my @list;
while ( $logmsg =~ s/\b(Item\d+)\s*:// ) {
push( @list, $1 );
}
foreach my $item (@list) {
my $fi = "$BUGS/$item.txt";
my $changed = 0;
# Extract the last revision of the item
my $lastrev = 1;
if ( -e "$BUGS/$item.txt,v" ) {
my $rlog = `rlog -h $BUGS/$item.txt`;
($lastrev) = $rlog =~ m/^head: 1\.(\d+).*?$/ms;
print STDERR "LAST REVISION $lastrev of Item$item \n" if $verbose;
}
$lastrev++;
open( F, '<', $fi ) || next;
my $text = <F>;
close(F);
# Update the TOPICINFO
$text =~ s/^(%META:TOPICINFO{.*?author=")(?:[^"]*)(".*?}%)$/$1$committer$2/m;
$text =~ s/^(%META:TOPICINFO{.*?version=")(?:[^"]*)(".*?}%)$/$1$lastrev$2/m;
$text =~ s/^(%META:TOPICINFO{.*?comment=")(?:[^"]*)(".*?}%)$/$1svn commit$2/m;
my $timestamp = time();
$text =~ s/^(%META:TOPICINFO{.*?date=")(?:[^"]*)(".*?}%)$/$1$timestamp$2/m;
print STDERR "Updated TOPICINFO with author $committer rev $lastrev timestamp $timestamp\n" if $verbose;
unless ( $text =~
s/^(%META:FIELD.*name="Checkins".*value=")(.*?)(".*%)$/$1._add($2, $rev, \$changed).$3/gem
)
{
$text .= "\n" unless $text =~ /\n$/s;
$text .=
"%META:FIELD{name=\"Checkins\" attributes=\"\" title=\"Checkins\" value=\"Foswikirev:$rev\"}%\n";
$changed = 1;
}
next unless $changed;
print STDERR `co -l -f $fi`;
die $! if $?;
open( F, '>', $fi ) || die "Failed to write $fi: $!";
print F $text;
close(F);
print STDERR `ci -mauto -u $fi`;
die $! if $?;
# 777 in case subversion user is not Apache user
chmod( 0777, $fi );
print STDERR "Updated $item with $rev\n";
}
}
# Create the flag that tells the cron job to update from the repository
open( F, '>>', "$SUPPORT/svncommit" )
|| die "Failed to write $SUPPORT/svncommit: $!";
print F join( " ", @changes );
close(F);
# Create the flag for this script
open( F, '>', "$SUPPORT/lastupdate" ) || die $!;
print F "$last\n";
close(F);
close(STDERR);