SVN.pm untested

filter: Statementstmt 0.0% 0 / 268 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 389 CC 29 SCAR 27.3
CC
29
Cov
0%
svn_commit
72.0
_get_current_filename
20.0
svn_revert
20.0
CRAP 15.3
1 package Padre::Plugin::SVN;
2  
3 use 5.008;
4 use warnings;
5 use strict;
6  
7 use Padre::Config ();
8 use Padre::Wx ();
9 use Padre::Plugin ();
10 use Padre::Util ();
11  
12  
13  
14 use SVN::Class qw(svn_file);
15  
16 our $VERSION = '0.05';
17 our @ISA = 'Padre::Plugin';
18  
19 =head1 NAME
20  
21 Padre::Plugin::SVN - Simple SVN interface for Padre
22  
23 =head1 SYNOPSIS
24  
25 Requires SVN client tools to be installed.
26  
27 cpan install Padre::Plugin::SVN
28  
29 Acces it via Plugin/SVN
30  
31 =head1 REQUIREMENTS
32  
33 The plugin requires that the SVN client tools be installed and setup, this includes any cached authentication.
34  
35 For most of the unices this is a matter of using the package manager to install the svn client tools.
36  
37 For windows try: http://subversion.tigris.org/getting.html#windows.
38  
39  
40 =head2 Configuring the SVN client for cached authentication.
41  
42 Because this module uses the installed SVN client, actions that require authentication from the server will fail and leave Padre looking as though it has hung.
43  
44 The way to address this is to run the svn client from the command line when asked for the login and password details, enter as required.
45  
46 Once done you should now have your authentication details cached.
47  
48 More details can be found here: http://svnbook.red-bean.com/nightly/en/svn.serverconfig.netmodel.html#svn.serverconfig.netmodel.credcache
49  
50 =head1 AUTHOR
51  
52 Gabor Szabo, C<< <szabgab at gmail.com> >>
53  
54 Additional work:
55  
56 Peter Lavender, C<< <peter.lavender at gmail.com> >>
57  
58 =head1 BUGS
59  
60 Please report any bugs or feature requests to L<http://padre.perlide.org/>
61  
62  
63 =head1 COPYRIGHT & LICENSE
64  
65 Copyright 2008, 2009, 2010 The Padre development team as listed in Padre.pm.
66 all rights reserved.
67  
68 This program is free software; you can redistribute it and/or modify it
69 under the same terms as Perl itself.
70  
71  
72 =cut
73  
74 #####################################################################
75 # Padre::Plugin Methods
76  
77 sub plugin_enable {
78 my $self = shift;
79 }
80  
81 # clean up modules used.
82 sub plugin_disable {
83 my $self = shift;
84  
85 #require Class::Unload;
86 #Class::Unload->unload('Padre::Plugin::SVN::Wx::SVNDialog');
87 #Class::Unload->unload('Padre::Plugin::SVN');
88 }
89  
90 sub padre_interfaces {
91 'Padre::Plugin' => 0.43;
92 }
93  
94 sub plugin_name {
95 'SVN';
96 }
97  
98 sub menu_plugins_simple {
99 my $self = shift;
100 return $self->plugin_name => [
101  
102 # maybe reorganize according to File/Directory/Project ?
103 #'File' => [
104 Wx::gettext('Add') => [
105 Wx::gettext('File') => sub { $self->svn_add_file },
106 Wx::gettext('Dir') => sub { $self->svn_diff_of_dir },
107 Wx::gettext('Project') => sub { $self->svn_diff_of_project },
108 ],
109 Wx::gettext('Blame') => sub { $self->svn_blame },
110 Wx::gettext('Commit') => [
111 Wx::gettext('File') => sub { $self->svn_commit_file },
112 Wx::gettext('Project') => sub { $self->svn_commit_project },
113 ],
114 Wx::gettext('Diff') => [
115 Wx::gettext('File') => [
116 Wx::gettext('Show') => sub { $self->svn_diff_of_file },
117 Wx::gettext('Open in Padre') => sub {$self->svn_diff_in_padre },
118 ],
119 Wx::gettext('Dir') => sub { $self->svn_diff_of_dir },
120 Wx::gettext('Project') => sub { $self->svn_diff_of_project },
121  
122 ],
123 Wx::gettext('Revert') => sub { $self->svn_revert },
124 Wx::gettext('Log') => [
125 Wx::gettext('File') => sub { $self->svn_log_of_file },
126 Wx::gettext('Project') => sub { $self->svn_log_of_project },
127 ],
128 Wx::gettext('Status') => [
129 Wx::gettext('File') => sub { $self->svn_status_of_file },
130 Wx::gettext('Project') => sub { $self->svn_status_of_project },
131 ],
132 Wx::gettext('About') => sub { $self->show_about },
133 ];
134 }
135  
136 #####################################################################
137 # Custom Methods
138  
139 sub show_about {
140 my $self = shift;
141  
142 # Generate the About dialog
143 my $about = Wx::AboutDialogInfo->new;
144 $about->SetName("Padre::Plugin::SVN");
145 $about->SetDescription( <<"END_MESSAGE" );
146 Initial SVN support for Padre
147 END_MESSAGE
148 $about->SetVersion($VERSION);
149  
150 # Show the About dialog
151 Wx::AboutBox($about);
152  
153 return;
154 }
155  
156 # TODO: I see this a lot. Should something like
157 # this be on Padre::Util?
158 sub _get_current_filename {
159 my $main = Padre->ide->wx->main;
160 my $document = $main->current->document;
161 my $filename = $document->filename;
162 if ($filename) {
163  
164 if ( $document->is_modified ) {
165 my $ret = Wx::MessageBox(
166 sprintf(
167 Wx::gettext(
168 '%s has not been saved but SVN would commit the file from disk.'
169 . "\n\nDo you want to save the file first (No aborts commit)?"
170 ),
171 $filename,
172 ),
173 Wx::gettext("Commit warning"),
174 Wx::wxYES_NO | Wx::wxCENTRE,
175 $main,
176 );
177  
178 return if $ret == Wx::wxNO;
179  
180 $main->on_save;
181  
182 }
183  
184 return $filename;
185 } else {
186 $main->error('File needs to be saved first.');
187 return;
188 }
189 }
190  
191 # TODO Add in a timer so long running calls can be stopped at some point.
192  
193 # TODO: update!
194  
195 sub svn_revert {
196 my ($self) = @_;
197  
198 # firstly warn the person their actions will
199 # go back to the last version of the file
200  
201 my $main = Padre::Current->main;
202 my $layout = [
203 [ [ 'Wx::StaticText', undef,
204 "Warning!\n\nSVN Revert will revert the current file saved to the file system.\n\nIt will not change your current document if you have unsaved changes.\n\nReverting your changes means you will lose any changes made since your last SVN Commit."
205 ],
206  
207 ],
208 [ [ 'Wx::Button', 'ok', Wx::wxID_OK ],
209 [ 'Wx::Button', 'cancel', Wx::wxID_CANCEL ]
210 ]
211 ];
212 my $dialog = Wx::Perl::Dialog->new(
213 parent => $main,
214 title => 'SVN Revert',
215 layout => $layout,
216 width => [ 500, 1200 ],
217  
218 );
219 return if not $dialog->show_modal;
220 my $data = $dialog->get_data;
221 if ( $data->{cancel} ) {
222  
223 #print "Canceling the revert\n";
224 return;
225 } else {
226  
227 #
228 my $filename = _get_current_filename();
229 if ($filename) {
230 my $file = svn_file($filename);
231 $file->revert();
232 }
233 }
234 }
235  
236  
237 sub svn_blame {
238 my ($self) = @_;
239 my $filename = _get_current_filename();
240 if ($filename) {
241 my $main = Padre::Current->main;
242 $self->{_busyCursor} = Wx::BusyCursor->new();
243 my $file = svn_file($filename);
244 $file->blame();
245
246 #my $blame = join( "\n", @{ $file->stdout } );
247 my @blame = @{ $file->stdout };
248 require Padre::Plugin::SVN::Wx::SVNDialog;
249 my $dialog = Padre::Plugin::SVN::Wx::SVNDialog->new( $main, $filename, \@blame, 'Blame' );
250 $self->{_busyCursor} = undef;
251 $dialog->Show(1);
252 return 1;
253 }
254  
255 return;
256  
257 }
258  
259 sub svn_status {
260 my ( $self, $path ) = @_;
261 my $main = Padre->ide->wx->main;
262  
263 my $file = svn_file($path);
264  
265 my $info = "";
266  
267 if ( $file->info ) {
268  
269 #print $file->info->dump();
270 $info .= "Author: " . $file->info->{author} . "\n";
271 $info .= "File Name: " . $file->info->{name} . "\n";
272 $info .= "Last Revision: " . $file->info->{last_rev} . "\n";
273 $info .= "Current Revision: " . $file->info->{rev} . "\n\n";
274  
275 $info .= "File create Date: " . $file->info->{date} . "\n\n";
276  
277 $info .= "Last Updated: " . $file->info->{updated} . "\n\n";
278  
279 $info .= "File Path: " . $file->info->{path} . "\n";
280 $info .= "File URL: " . $file->info->{_url} . "\n";
281 $info .= "File Root: " . $file->info->{root} . "\n\n";
282  
283 $info .= "Check Sum: " . $file->info->{checksum} . "\n";
284 $info .= "UUID: " . $file->info->{uuid} . "\n";
285 $info .= "Schedule: " . $file->info->{schedule} . "\n";
286 $info .= "Node: " . $file->info->{node} . "\n\n";
287 } else {
288 $info .= 'File is not managed by SVN';
289 }
290  
291 #print $info;
292 $main->message( $info, "$path" );
293 return;
294 }
295  
296 sub svn_status_of_file {
297 my ($self) = @_;
298 my $filename = _get_current_filename();
299 if ($filename) {
300 $self->svn_status($filename);
301 }
302 return;
303 }
304  
305 sub svn_status_of_project {
306 my ($self) = @_;
307 my $filename = _get_current_filename();
308 if ($filename) {
309 my $main = Padre::Current->main;
310 my $dir = Padre::Util::get_project_dir($filename);
311 return $main->error( Wx::gettext('Could not find project root') ) if not $dir;
312 $self->svn_status($dir);
313 }
314 return;
315 }
316  
317  
318 sub svn_log {
319 my ( $self, $path ) = @_;
320 my $main = Padre->ide->wx->main;
321  
322 my $file = svn_file($path);
323 $self->{_busyCursor} = Wx::BusyCursor->new();
324 my $out = join( "\n", @{ $file->log() } );
325 $self->{_busyCursor} = undef;
326  
327 #$main->message( $out, "$path" );
328 require Padre::Plugin::SVN::Wx::SVNDialog;
329 my $log = Padre::Plugin::SVN::Wx::SVNDialog->new( $main, $path, $out, 'Log' );
330 $log->Show(1);
331  
332  
333 }
334  
335 sub svn_log_of_file {
336 my ($self) = @_;
337 my $filename = _get_current_filename();
338 if ($filename) {
339 $self->svn_log($filename);
340 }
341 return;
342 }
343  
344 sub svn_log_of_project {
345 my ($self) = @_;
346 my $filename = _get_current_filename();
347 if ($filename) {
348 my $main = Padre::Current->main;
349 my $dir = Padre::Util::get_project_dir($filename);
350 return $main->error( Wx::gettext('Could not find project root') ) if not $dir;
351 $self->svn_log($dir);
352 }
353 return;
354 }
355  
356  
357  
358  
359 sub svn_diff {
360 my ( $self, $path ) = @_;
361 my $main = Padre->ide->wx->main;
362  
363 my $file = svn_file($path);
364  
365 #print $file->stderr;
366 #print $file->stdout;
367  
368 $file->diff();
369 my $status = join( "\n", @{ $file->stdout } );
370  
371 #$main->message( $status, "$path" );
372 require Padre::Plugin::SVN::Wx::SVNDialog;
373 my $log = Padre::Plugin::SVN::Wx::SVNDialog->new( $main, $path, $status, 'Diff' );
374 $log->Show(1);
375  
376 return;
377  
378 }
379  
380 sub svn_diff_in_padre {
381 my ($self) = @_;
382 my $filename = _get_current_filename();
383 my $main = Padre->ide->wx->main;
384  
385  
386 if ($filename) {
387 my $file = svn_file($filename);
388 my $diff = $file->diff;
389 my $diff_str = join( "\n", @{ $file->stdout } );
390 $main->new_document_from_string( $diff_str, 'text/x-patch' );
391 return 1;
392 }
393 return;
394  
395 }
396  
397 sub svn_diff_of_file {
398 my ($self) = @_;
399 my $filename = _get_current_filename();
400 if ($filename) {
401 $self->svn_diff($filename);
402 }
403 return;
404 }
405  
406 sub svn_diff_of_project {
407 my ($self) = @_;
408 my $filename = _get_current_filename();
409 if ($filename) {
410 my $dir = Padre::Util::get_project_dir($filename);
411 $self->svn_diff($dir);
412 }
413 return;
414 }
415  
416 sub svn_commit {
417 my ( $self, $path ) = @_;
418  
419 my $main = Padre->ide->wx->main;
420 my $file = svn_file($path);
421  
422 # == 0 seems to produce false errors here
423 # if (( ! defined($file)) or ($file == 0)){
424 if ( ! defined($file)){
425 $main->error(Wx::gettext('Unable to find SVN file!'),Wx::gettext('Error - SVN Commit'));
426 return;
427 }
428  
429 my $info = "$path\n\n";
430 if ( defined( $file->info->{last_rev} ) ) {
431 $info .= "Last Revision: " . $file->info->{last_rev};
432 } else { # New files
433 $info .= "Last Revision: (none)";
434 }
435
436 require Padre::Plugin::SVN::Wx::SVNDialog;
437 my $dialog = Padre::Plugin::SVN::Wx::SVNDialog->new( $main, $info, undef, 'Commit File', 1 );
438 $dialog->ShowModal;
439  
440 # check Cancel!!!!
441 return if( $dialog->{cancelled});
442
443 my $message = $dialog->get_data;
444  
445
446
447  
448 # whoops!! This isn't going to work "Commit message" is always set in the text control.
449 if ($message && $message ne 'Commit Message') { # "Commit Message" come from SVNDialog
450 $self->{_busyCursor} = Wx::BusyCursor->new();
451  
452 my $revNo = $file->commit($message);
453  
454 $self->{_busyCursor} = undef;
455  
456 my @commit = @{ $file->stdout };
457 my @err = @{ $file->stderr };
458 if (@err) {
459 $main->error( join( "\n", @err ), Wx::gettext('Error - SVN Commit') );
460 } else {
461 $main->info( join( "\n", @commit ), "Committed Revision number $revNo." );
462 }
463  
464 }
465 else {
466 my $ret = Wx::MessageBox( Wx::gettext(
467 'You really should commit with a useful message'
468 . "\n\nDo you really want to commit with out a message?"
469 ),
470 Wx::gettext("Commit warning"),
471 Wx::wxYES_NO | Wx::wxCENTRE,
472 $main,
473 );
474 if( $ret == Wx::wxYES ) {
475 $self->{_busyCursor} = Wx::BusyCursor->new();
476  
477 my $revNo = $file->commit($message);
478  
479 $self->{_busyCursor} = undef;
480  
481 my @commit = @{ $file->stdout };
482 my @err = @{ $file->stderr };
483 if (@err) {
484 $main->error( join( "\n", @err ), 'Error - SVN Commit' );
485 } else {
486 $main->info( join( "\n", @commit ), "Committed Revision number $revNo." );
487 }
488 }
489 else {
490 $self->svn_commit($path);
491 }
492
493 }
494  
495 return;
496 }
497  
498 sub svn_commit_file {
499 my ($self) = @_;
500 my $filename = _get_current_filename();
501 if ($filename) {
502 $self->svn_commit($filename);
503 }
504 return;
505 }
506  
507 sub svn_commit_project {
508 my ($self) = @_;
509 my $filename = _get_current_filename();
510 if ($filename) {
511 my $dir = Padre::Util::get_project_dir($filename);
512 $self->svn_commit($dir);
513 }
514 return;
515 }
516  
517 sub svn_add {
518 my ( $self, $path ) = @_;
519
520 my $main = Padre->ide->wx->main;
521  
522 my $file = svn_file($path);
523 $file->add;
524 if ($file->errstr) {
525 $main->error($file->errstr);
526 } else {
527 $main->info("$path scheduled to be added to " . $file->info->{_url});
528 }
529  
530 return;
531 }
532  
533 sub svn_add_file {
534 my ($self) = @_;
535 my $filename = _get_current_filename();
536 if ($filename) {
537 $self->svn_add($filename);
538 }
539 return;
540 }
541  
542 1;
543