1 |
package Frey::SVK; |
2 |
use Moose; |
3 |
|
4 |
extends 'Frey'; |
5 |
with 'Frey::Web'; |
6 |
with 'Frey::Path'; |
7 |
|
8 |
use Moose::Util::TypeConstraints; |
9 |
|
10 |
enum 'SVK_Action' => ( 'commit', 'revert', 'postpone' ); |
11 |
|
12 |
has action => ( |
13 |
is => 'rw', |
14 |
isa => 'SVK_Action', |
15 |
); |
16 |
|
17 |
has path => ( |
18 |
documentation => 'path to work with', |
19 |
is => 'rw', |
20 |
isa => 'Str|ArrayRef', |
21 |
); |
22 |
|
23 |
has commit_message => ( |
24 |
documentation => 'commit message', |
25 |
is => 'rw', |
26 |
isa => 'Str', |
27 |
); |
28 |
|
29 |
sub svk { |
30 |
my ( $self, $exec, $coderef ) = @_; |
31 |
open(my $svk, '-|', 'svk ' . $exec) or die "svk $exec: $@"; |
32 |
while(<$svk>) { |
33 |
chomp; |
34 |
$coderef->( $_ ); |
35 |
} |
36 |
close($svk) or die "can't close svk $exec: $@"; |
37 |
} |
38 |
|
39 |
sub modified { |
40 |
my ($self) = @_; |
41 |
my @modified; |
42 |
my $svk = $self->svk('status -q', sub { |
43 |
push @modified, $1 if /^\w+\s+(.+)/; |
44 |
}); |
45 |
return @modified; |
46 |
} |
47 |
|
48 |
our $info; # cache, we use it on every hit |
49 |
sub info { |
50 |
my ($self) = @_; |
51 |
return $info if $info; |
52 |
my $svk = $self->svk('info', sub { |
53 |
my ( $label, $value ) = split(/:\s+/, $_, 2); |
54 |
$info->{$label} = $value if $label; |
55 |
}); |
56 |
warn "# svk info ",$self->dump( $info ); |
57 |
return $info; |
58 |
} |
59 |
|
60 |
sub as_data { |
61 |
my ($self) = @_; |
62 |
{ |
63 |
modified => [ $self->modified ], |
64 |
} |
65 |
} |
66 |
|
67 |
sub checkbox { |
68 |
my ($self,$name,$value) = @_; |
69 |
my $checked = ''; |
70 |
my $all_checkboxes = $self->$name; |
71 |
$all_checkboxes = [ $all_checkboxes ] unless ref($all_checkboxes) eq 'ARRAY'; # sigh, too chatty |
72 |
$checked = ' checked' if grep { $_ eq $value } @$all_checkboxes; |
73 |
warn "# checkbox $name $value $checked\t", $self->dump( $self->$name ); |
74 |
qq|<input name="$name" value="$value" type="checkbox"$checked>|; |
75 |
} |
76 |
|
77 |
sub commit_as_markup { |
78 |
my ($self) = @_; |
79 |
my $status = `svk status -q`; |
80 |
$status =~ s{^(\w+[\+\s]+)(\S+)$}{$1 . $self->checkbox('path',$2) . qq|<a href="#$2">$2</a>|}egm; |
81 |
if ( $status ) { |
82 |
$self->add_css(qq| |
83 |
pre.l a { text-decoration: none; } |
84 |
form.commit { |
85 |
background: #eee; |
86 |
padding: 1em 1em; |
87 |
position: fixed; |
88 |
top: 1em; |
89 |
right: 1em; |
90 |
z-index: 10; |
91 |
} |
92 |
| ); |
93 |
|
94 |
|
95 |
$status = qq| |
96 |
<form class="commit" method="post"> |
97 |
<pre class="l">$status</pre> |
98 |
<textarea name="commit_message" cols=40 rows=4></textarea> |
99 |
<br><input type="submit" name="action" value="commit"> |
100 |
</form> |
101 |
|; |
102 |
$self->add_status( status => $status ); |
103 |
warn "commit_as_markup ",length($status)," bytes"; |
104 |
} |
105 |
return $status; |
106 |
} |
107 |
|
108 |
sub diff_as_markup { |
109 |
my ($self) = @_; |
110 |
|
111 |
my $diff = `svk diff`; |
112 |
$self->add_status( diff => $diff ); |
113 |
|
114 |
$diff = $self->html_escape( $diff ); |
115 |
$self->add_css( qq| |
116 |
pre span.add { background: #dfd } |
117 |
pre span.del { background: #fdd } |
118 |
pre form.inline { display: inline } |
119 |
| ); |
120 |
$diff =~ s{^(\+.+?)$}{<span class="add">$1</span>}gm; |
121 |
$diff =~ s{^(\-.+?)$}{<span class="del">$1</span>}gm; |
122 |
sub form { |
123 |
my ( $path, $action ) = @_; |
124 |
qq|<form class="inline"><input type="hidden" name="path" value="$path"><input type="submit" name="action" value="$action"></form>|; |
125 |
}; |
126 |
$diff =~ s{^(===\s+)(\S+)$}{$1 . form($2,'revert') . qq| <a name="$2" target="editor" href="/editor+$2+1">$2</a> | . form($2,'postpone') }gem; |
127 |
|
128 |
$diff = qq|<pre>$diff</pre>|; |
129 |
warn "diff_as_markup ",length($diff)," bytes"; |
130 |
return $diff; |
131 |
} |
132 |
|
133 |
sub action_as_markup { |
134 |
my ($self) = @_; |
135 |
|
136 |
my $cmd; |
137 |
|
138 |
if ( $self->action eq 'postpone' ) { |
139 |
my $old = $self->path; |
140 |
my $new = $old; |
141 |
$new =~ s{/([^/]+)$}{/.postponed.$1}; |
142 |
|
143 |
die "Allready have ", $self->path_size($new) if -e $new; |
144 |
$cmd = "mv $old $new && svk revert $old"; |
145 |
} elsif ( $self->action ) { |
146 |
$cmd = 'svk ' . $self->action; |
147 |
if ( $self->action eq 'commit' ) { |
148 |
my $msg = $self->commit_message || return $self->error( "need commit message\n" ); |
149 |
$msg =~ s{"}{\\"}gs; |
150 |
$cmd .= qq{ -m "$msg"}; |
151 |
} else { |
152 |
confess "need path" unless $self->path; |
153 |
} |
154 |
$cmd .= ' ' . join(' ',$self->path); |
155 |
} |
156 |
if ( $cmd ) { |
157 |
$cmd .= ' 2>&1'; |
158 |
warn "# cmd $cmd"; |
159 |
|
160 |
my $out = `$cmd`; |
161 |
warn "# output of $cmd is: $out"; |
162 |
|
163 |
return qq| |
164 |
Command <tt>$cmd</tt> produced output: |
165 |
<pre style="background: #ff8;"> |
166 |
$out |
167 |
</pre> |
168 |
|; |
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
sub as_markup { |
174 |
my ($self) = @_; |
175 |
|
176 |
my $html = $self->action_as_markup; |
177 |
|
178 |
$self->title( 'svk' . ( $self->action ? ' - ' . $self->action : '' ) ); # XXX without this we get wrong icon and title |
179 |
|
180 |
$html .= $self->commit_as_markup . $self->diff_as_markup || |
181 |
qq|No changes in tracked files|; |
182 |
|
183 |
warn "as_markup ",length($html)," bytes"; |
184 |
|
185 |
return $html; |
186 |
} |
187 |
|
188 |
1; |