-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiff.pm
More file actions
124 lines (77 loc) · 3.33 KB
/
Diff.pm
File metadata and controls
124 lines (77 loc) · 3.33 KB
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
package Rex::Hook::File::Diff;
# ABSTRACT: show diff of changes for files managed by Rex
use 5.012;
use warnings;
use File::Basename;
use IPC::Run;
use Rex 1.013004 -base;
use Rex::Helper::Run;
use Rex::Hook;
use Text::Diff 1.44;
our $VERSION = '9999';
register_function_hooks { before_change => { file => \&show_diff, }, };
sub show_diff {
my ( $original_file, @opts ) = @_;
my $diff;
my $diff_command = can_run('diff');
state $managing_windows = is_windows();
if ( !$managing_windows && $diff_command ) {
my $command = join q( ), $diff_command, '-u', involved_files($original_file);
if ( $diff = i_run $command, fail_ok => TRUE ) {
$diff .= "\n";
}
}
elsif ( Rex::is_local() ) {
$diff = diff( involved_files($original_file) );
}
else {
Rex::Logger::debug(
'Skipping File::Diff hook due to remote operation without the diff utility');
return;
}
if ( length $diff > 0 ) {
my @highlighter = qw( delta --color-only --diff-so-fancy );
my $highlighted;
IPC::Run::run \@highlighter, \$diff, \$highlighted;
$diff = $highlighted;
Rex::Commands::say("Diff for: $original_file\n$diff");
}
return;
}
sub involved_files {
my $file = shift;
my $temp_file = Rex::Commands::File::get_tmp_file_name($file);
my $null = File::Spec->devnull();
if ( !is_file($file) ) { $file = $null } # creating file
if ( !is_file($temp_file) ) { $temp_file = $null } # removing file
return ( $file, $temp_file );
}
1;
__END__
=for :stopwords backend CPAN sed
=head1 SYNOPSIS
use Rex::Hook::File::Diff;
=head1 DESCRIPTION
This module allows L<Rex> to show a diff of changes for the files managed via its built-in L<file manipulation commands|https://metacpan.org/pod/Rex::Commands::File> which rely on the L<file|https://metacpan.org/pod/Rex::Commands::File#file> command as a backend:
=over 4
=item L<file|https://metacpan.org/pod/Rex::Commands::File#file>
=item L<delete_lines_matching|https://metacpan.org/pod/Rex::Commands::File#delete_lines_matching>
=item L<delete_lines_according_to|https://metacpan.org/pod/Rex::Commands::File#delete_lines_according_to>
=item L<append_if_no_such_line|https://metacpan.org/pod/Rex::Commands::File#append_if_no_such_line>
=item L<append_or_amend_line|https://metacpan.org/pod/Rex::Commands::File#append_or_amend_line>
=item L<sed|https://metacpan.org/pod/Rex::Commands::File#sed>
=back
It prefers to use the C<diff> utility on non-Windows managed endpoints, if available.
=head1 DIAGNOSTICS
This module does not do any error checking (yet).
=head1 CONFIGURATION AND ENVIRONMENT
This module does not require any configuration, nor does it use any environment variables.
=head1 DEPENDENCIES
See the included C<cpanfile>.
Requires the C<diff> utility to show the diff for remote file operations.
=head1 INCOMPATIBILITIES
There are no known incompatibilities with other modules.
=head1 BUGS AND LIMITATIONS
There are no known bugs. Make sure they are reported.
Upload hook support is not implemented (yet), so diff is not shown upon file uploads when using the C<source> option with the L<file|https://metacpan.org/pod/Rex::Commands::File#file> command (or the L<upload|https://metacpan.org/pod/Rex::Commands::Upload#upload> command directly).
=cut