| 1 | package Rex::Hook::File::Diff; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: show diff of changes for files managed by Rex | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use 5.012; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Rex 1.013004 -base; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Rex::Hook; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Text::Diff 1.44; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | 2 | our $VERSION = 'v0.4.0'; | |||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 2 | register_function_hooks { before_change => { file => \&show_diff, }, }; | |||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | sub show_diff { | ||||||||||||||||||||||||||||||
|
Subroutine
show_diff: called CC=6 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 19 | 12 | my ( $original_file, @opts ) = @_; | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 12 | my $diff; | |||||||||||||||||||||||||||||||
| 22 | 12 | my $diff_command = can_run('diff'); | |||||||||||||||||||||||||||||||
| 23 | 12 | state $managing_windows = is_windows(); | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 12 | ▶ | if ( !$managing_windows && $diff_command ) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
not $managing_windows and $diff_command
MC/DC0%0/2
not $managing_windows and $diff_command
not $managing_windows
$diff_command
Truth table33%1/3
not(A) and B
A= not $managing_windowsB= $diff_command
| |||||||||||||||||||||||||||||||||
| 26 | 12 | my $command = join q( ), $diff_command, '-u', involved_files($original_file); | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 12 | ▶ | if ( $diff = i_run $command, fail_ok => TRUE ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 10 | $diff .= "\n"; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||
| 32 | elsif ( Rex::is_local() ) { | ||||||||||||||||||||||||||||||||
| 33 | 0 | $diff = diff( involved_files($original_file) ); | |||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||||||
| 36 | 0 | Rex::Logger::debug( | |||||||||||||||||||||||||||||||
| 37 | 'Skipping File::Diff hook due to remote operation without the diff utility'); | ||||||||||||||||||||||||||||||||
| 38 | 0 | return; | |||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | 12 | ▶ | if ( length $diff > 0 ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 10 | Rex::Commands::say("Diff for: $original_file\n$diff"); | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 12 | return; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 15 | ▶ | sub involved_files { | ||||||||||||||||||||||||||||||
|
Subroutine
involved_files: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 49 | 15 | my $file = shift; | |||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 15 | my $temp_file = Rex::Commands::File::get_tmp_file_name($file); | |||||||||||||||||||||||||||||||
| 52 | 15 | my $null = File::Spec->devnull(); | |||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 15 | ▶ | if ( !is_file($file) ) { $file = $null } # creating file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 15 | ▶ | if ( !is_file($temp_file) ) { $temp_file = $null } # removing file | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 15 | return ( $file, $temp_file ); | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||
| 60 | 2 | 1; | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||||||