From 23afd31dbefe7706ef3d2d73d16ed22b6719c6fb Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Tue, 24 Jan 2023 18:08:52 -0800 Subject: [PATCH] issue rt#145999, keep old mtime with -b if no diff See the note in CHANGES.md --- CHANGES.md | 9 +++++++++ lib/Perl/Tidy.pm | 46 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ab3a8d16..c21387c5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,15 @@ ## 2022 11 12.03 + - For the -b (--backup-and-modify-in-place) option, the file timestamps + are changing (issue rt#145999). First, if there are no formatting + changes to an input file, it will keep its original modification time. + Second, any backup file will keep its original modification time. This + was previously true for --backup-method=move but not for the default + --backup-method=copy. The purpose of these changes is to avoid + triggering Makefile operations when there are no actual file changes. + If this causes a problem please open an issue for discussion on github. + - A change was made to the way line breaks are made at the '.' operator when the user sets -wba='.' to requests breaks after a '.' ( this setting is not recommended because it can be hard to read ). diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 691b69c1..4a10a28c 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -467,6 +467,7 @@ BEGIN { _teefile_stream_ => $i++, _user_formatter_ => $i++, _input_copied_verbatim_ => $i++, + _input_output_difference_ => $i++, }; } @@ -1172,6 +1173,13 @@ sub backup_method_copy { $self->set_output_file_permissions( $backup_file, \@input_file_stat, $in_place_modify ); + # set the modification time of the copy to the original value (rt#145999) + my ( $read_time, $write_time ) = @input_file_stat[ 8, 9 ]; + if ( defined($write_time) ) { + utime( $read_time, $write_time, $backup_file ) + || Warn("error setting times for backup file '$backup_file'\n"); + } + # Open the original input file for writing ... opening with ">" will # truncate the existing data. open( my $fout, ">", $input_file ) @@ -1222,6 +1230,12 @@ EOM $self->set_output_file_permissions( $input_file, \@input_file_stat, $in_place_modify ); + # Keep original modification time if no change (rt#145999) + if ( !$self->[_input_output_difference_] && defined($write_time) ) { + utime( $read_time, $write_time, $input_file ) + || Warn("error setting times for '$input_file'\n"); + } + #--------------------------------------------------------- # remove the original file for in-place modify as follows: # $delete_backup=0 never @@ -1367,6 +1381,13 @@ EOM $self->set_output_file_permissions( $input_file, \@input_file_stat, $in_place_modify ); + # Keep original modification time if no change (rt#145999) + my ( $read_time, $write_time ) = @input_file_stat[ 8, 9 ]; + if ( !$self->[_input_output_difference_] && defined($write_time) ) { + utime( $read_time, $write_time, $input_file ) + || Warn("error setting times for '$input_file'\n"); + } + #--------------------------------------------------------- # remove the original file for in-place modify as follows: # $delete_backup=0 never @@ -2038,6 +2059,7 @@ EOM $self->[_output_file_] = $output_file; $self->[_teefile_stream_] = $teefile_stream; $self->[_input_copied_verbatim_] = 0; + $self->[_input_output_difference_] = 1; ## updated later if -b used #---------------------------------------------------------- # Do all formatting of this buffer. @@ -2158,7 +2180,7 @@ sub process_filter_layer { my $sink_object; # vars for checking assertions, if needed - my $digest_input = 0; + my $digest_input; my $saved_input_buf; my $ref_destination_stream = ref($destination_stream); @@ -2167,8 +2189,11 @@ sub process_filter_layer { # if needed. These are only used for 'tidy' formatting. if ( $rOpts->{'format'} eq 'tidy' ) { - # evaluate MD5 sum of input file for assert tests before any prefilter - if ( $rOpts->{'assert-tidy'} || $rOpts->{'assert-untidy'} ) { + # evaluate MD5 sum of input file, if needed, before any prefilter + if ( $rOpts->{'assert-tidy'} + || $rOpts->{'assert-untidy'} + || $rOpts->{'backup-and-modify-in-place'} ) + { $digest_input = $md5_hex->($buf); $saved_input_buf = $buf; } @@ -2186,7 +2211,8 @@ sub process_filter_layer { $postfilter || $remove_terminal_newline || $rOpts->{'assert-tidy'} - || $rOpts->{'assert-untidy'}; + || $rOpts->{'assert-untidy'} + || $rOpts->{'backup-and-modify-in-place'}; #------------------------- # Setup destination_buffer @@ -2274,10 +2300,15 @@ EOM ? $postfilter->($postfilter_buffer) : $postfilter_buffer; + if ( defined($digest_input) ) { + my $digest_output = $md5_hex->($buf_post); + $self->[_input_output_difference_] = + $digest_output ne $digest_input; + } + # Check if file changed if requested, but only after any postfilter if ( $rOpts->{'assert-tidy'} ) { - my $digest_output = $md5_hex->($buf_post); - if ( $digest_output ne $digest_input ) { + if ( $self->[_input_output_difference_] ) { my $diff_msg = compare_string_buffers( $saved_input_buf, $buf_post, $is_encoded_data ); @@ -2291,8 +2322,7 @@ EOM } if ( $rOpts->{'assert-untidy'} ) { - my $digest_output = $md5_hex->($buf_post); - if ( $digest_output eq $digest_input ) { + if ( !$self->[_input_output_difference_] ) { $logger_object->warning( "assertion failure: '--assert-untidy' is set but output equals input\n" ); -- 2.39.5