blib/lib/Perl/Repository/APC2SVN.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 216 CC 27 SCAR 45.0
CC
27
Cov
0%
latest_change
210.0
dir_will_be_empty
42.0
url_latest_change
12.0
CRAP 90.4
1 # -*- cperl-indent-level: 4 -*- vi:nowrap:
2 package Perl::Repository::APC2SVN;
3  
4 use strict;
5 use warnings;
6 use File::Basename qw(dirname);
7  
8 my $Id = q$Id: APC2SVN.pm 220 2006-10-22 10:51:44Z k $;
9 our $VERSION = sprintf "%.3f", 1 + substr(q$Rev: 220 $,4)/1000;
10  
11 require Exporter;
12 our @ISA = qw(Exporter);
13 our @EXPORT_OK = qw(latest_change url_latest_change get_dirs_to_add
14 get_dirs_to_delete delete_empty_dirs);
15 our $DEBUG = 0; # apologies for the inconvenient debugging switch
16  
17 sub latest_change ();
18 sub url_latest_change ($);
19 sub get_dirs_to_add (@);
20 sub get_dirs_to_delete (@);
21 sub dir_will_be_empty ($);
22 sub delete_empty_dirs (@);
23  
24  
25 # to request the whole log is very slow for large repositories (4 secs
26 # for 10000 revisions), much slower than repeatedly requesting dozens
27 # of single log entries, but still faster than 1000s of single log
28 # requests. So we make a compromise: if requesting single log entries
29 # leads to a quick success, all is good, but after 4 tries we request
30 # the whole log
31 sub latest_change () {
32 my $lastpatch = 0;
33 my($rev);
34 warn "DEBUG: Running svn info -R" if $DEBUG;
35 open my $svninfo, "svn info -R |" or die "Can't fork 'svn info': $!\n";
36 local $/;
37 local $_ = <$svninfo>;
38 close $svninfo;
39 use List::Util qw(max);
40 $rev = max(/^Last Changed Rev: (\d+)/gm);
41 $/ = "\n";
42 my $triesleft = 4; # maximum
43 until ($lastpatch) {
44 # warn "Trying rev $rev";
45 last unless $rev;
46 warn "DEBUG: Running svn log -r $rev" if $DEBUG;
47 open my $svnlog, "svn log -r $rev |" or die "Can't fork 'svn log': $!\n";
48 while (<$svnlog>) {
49 chomp;
50 if ($. == 2) {
51 if (/^rev (\d+):/) {
52 $rev = $1;
53 } else {
54 die "Unexpected log-status-line: '$_'";
55 }
56 } elsif (/^Change (\d+) by /) {
57 $lastpatch = $1;
58 last;
59 }
60 }
61 1 while <$svnlog>; # bug in svn? it returns false if we break the pipe???
62 unless (close $svnlog) {
63 warn "Warning (probably harmless): Can't close 'svn log -r $rev': $!";
64 }
65 $rev-- unless $lastpatch;
66 last unless $rev;
67 last unless --$triesleft > 0;
68 }
69 return $lastpatch if $lastpatch;
70 # our speedup strategy didn't work out, so let's do the safe thing
71 warn "DEBUG: Running svn log" if $DEBUG;
72 open my $svnlog, 'svn log |'
73 or die "Can't fork 'svn log': $!\n";
74 while (<$svnlog>) {
75 if (/^Change (\d+) by /) {
76 $lastpatch = $1;
77 last;
78 }
79 }
80 close $svnlog;
81 $lastpatch;
82 }
83  
84 # If we don't have a repository checked out, we cannot use the trick
85 # with 'svn info' as in latest_change(), so we ask directly the
86 # repository for the whole log
87 sub url_latest_change ($) {
88 my $url = shift;
89 my $lastpatch = 0;
90 warn "DEBUG: Running svn log $url" if $DEBUG;
91 open my $svnlog, "svn log $url |"
92 or die "Can't fork 'svn log $url': $!\n";
93 local($/) = "\n";
94 while (<$svnlog>) {
95 if (/^Change (\d+) by /) {
96 $lastpatch = $1;
97 last;
98 }
99 }
100 close $svnlog;
101 $lastpatch;
102 }
103  
104 # Returns all directories to add to the repository
105 # for a given set of added files
106 sub get_dirs_to_add (@) {
107 return () if @_ == 0;
108 my %dirs = ();
109 for my $file (@_) {
110 my $dir = $file;
111 while (($dir = dirname $dir) ne ".") {
112 $dirs{$dir} = 1 unless $dirs{$dir} or -d $dir;
113 }
114 }
115 # Order is important
116 return sort { length $a <=> length $b } keys %dirs;
117 }
118  
119 # get_dirs_to_delete returns all additional directories to delete from
120 # the repository for a given set of files or directories that have
121 # already been scheduled for deletion.
122 # (After some experiments with the behaviour of svn, I decided that the
123 # safest way was to use the output of qx(svn info $dir/*) on each $dir
124 # parent of a deleted file, and to check that every file under version
125 # control in it is scheduled for deletion. This is not the fastest way
126 # but it's less error-prone that every other method I can think
127 # of right now.)
128  
129 sub get_dirs_to_delete (@) {
130 return () if @_ == 0;
131 my %dirs = ();
132 for my $file (@_) {
133 my $dir = dirname $file;
134 $dirs{$dir} = 1 if ! $dirs{$dir} and dir_will_be_empty($dir);
135 # NOT equiv: $dirs{$dir} ||= dir_will_be_empty($dir);
136 }
137 return sort { length $b <=> length $a } keys %dirs;
138 }
139  
140 sub dir_will_be_empty ($) {
141 my $dir = shift;
142 my $ret = 1;
143 my $count = 0;
144 my @glob = grep !/\/\.svn$/, glob "$dir/*";
145 return 1 unless @glob;
146 my $glob = join " ", map { "'$_'" } @glob;
147 warn "DEBUG: Running svn info $glob" if $DEBUG;
148 open my $svninfo, "svn info $glob |"
149 or die "Can't fork 'svn info': $!\n";
150 while (<$svninfo>) {
151 next if !/^Schedule: (\w+)/;
152 ++$count;
153 $ret = 0 if $1 ne 'delete';
154 }
155 close $svninfo;
156 return $count ? $ret : 0;
157 }
158  
159 sub delete_empty_dirs (@) {
160 my @files = @_;
161 my @to_delete_recursively = my @to_delete = get_dirs_to_delete(@files);
162 if (@to_delete) {
163 warn "DEBUG: Running svn rm on to_delete[@to_delete]" if $DEBUG;
164 system(svn => 'rm', @to_delete)
165 and die "Error executing svn rm : $!,$?\n";
166 push @to_delete_recursively, delete_empty_dirs(@to_delete);
167 }
168 @to_delete_recursively;
169 }
170  
171  
172  
173 1;
174  
175 __END__