diff --git a/bin/pt-config-diff b/bin/pt-config-diff index f9fe1566..79bb4c53 100755 --- a/bin/pt-config-diff +++ b/bin/pt-config-diff @@ -844,7 +844,7 @@ sub print_usage { $desc .= ". Optional suffix s=seconds, m=minutes, h=hours, " . "d=days; if no suffix, $s is used."; } - $desc = join("\n$rpad", grep { $_ } $desc =~ m/(.{0,$rcol})(?:\s+|$)/g); + $desc = join("\n$rpad", grep { $_ } $desc =~ m/(.{0,$rcol}(?!\W))(?:\s+|(?<=\W)|$)/g); $desc =~ s/ +$//mg; if ( $short ) { $usage .= sprintf(" --%-${maxs}s -%s %s\n", $long, $short, $desc); @@ -2201,8 +2201,8 @@ sub _preprocess_varvals { my %vars; LINE: foreach my $line ( split /\n/, $to_parse ) { - next LINE if $line =~ m/^\s*$/; # no empty lines - next LINE if $line =~ m/^\s*#/; # no # comment lines + next LINE if $line =~ m/^\s*$/; # no empty lines + next LINE if $line =~ /^\s*[#;]/; # no # or ; comment lines if ( $line !~ $re ) { PTDEBUG && _d("Line <", $line, "> didn't match $re"); @@ -2213,6 +2213,8 @@ sub _preprocess_varvals { $var =~ tr/-/_/; + $var =~ s/\s*#.*$//; + if ( !defined $val ) { $val = ''; } @@ -2252,15 +2254,24 @@ sub _parse_varvals { return \%config, \%duplicates; } +my $quote_re = qr/ + \A # Start of value + (['"]) # Opening quote + (.*) # Value + \1 # Closing quote + \s*(?:\#.*)? # End of line comment + [\n\r]*\z # End of value +/x; sub _process_val { my ($val) = @_; - $val =~ s/ - \A # Start of value - (['"]) # Opening quote - (.*) # Value - \1 # Closing quote - [\n\r]*\z # End of value - /$2/x; + + if ( $val =~ $quote_re ) { + $val = $2; + } + else { + $val =~ s/\s*#.*//; + } + if ( my ($num, $factor) = $val =~ m/(\d+)([KMGT])b?$/i ) { my %factor_for = ( k => 1_024, diff --git a/lib/MySQLConfig.pm b/lib/MySQLConfig.pm index ac9cd668..20fe8ec7 100644 --- a/lib/MySQLConfig.pm +++ b/lib/MySQLConfig.pm @@ -340,8 +340,8 @@ sub _preprocess_varvals { my %vars; LINE: foreach my $line ( split /\n/, $to_parse ) { - next LINE if $line =~ m/^\s*$/; # no empty lines - next LINE if $line =~ m/^\s*#/; # no # comment lines + next LINE if $line =~ m/^\s*$/; # no empty lines + next LINE if $line =~ /^\s*[#;]/; # no # or ; comment lines if ( $line !~ $re ) { PTDEBUG && _d("Line <", $line, "> didn't match $re"); @@ -354,6 +354,9 @@ sub _preprocess_varvals { # but in SHOW VARIABLES they're all like "log_bin". $var =~ tr/-/_/; + # Remove trailing comments + $var =~ s/\s*#.*$//; + if ( !defined $val ) { $val = ''; } @@ -405,15 +408,26 @@ sub _parse_varvals { return \%config, \%duplicates; } +my $quote_re = qr/ + \A # Start of value + (['"]) # Opening quote + (.*) # Value + \1 # Closing quote + \s*(?:\#.*)? # End of line comment + [\n\r]*\z # End of value +/x; sub _process_val { my ($val) = @_; - $val =~ s/ - \A # Start of value - (['"]) # Opening quote - (.*) # Value - \1 # Closing quote - [\n\r]*\z # End of value - /$2/x; + + if ( $val =~ $quote_re ) { + # If it matches the quote re, then $2 holds the value + $val = $2; + } + else { + # Otherwise, remove possible trailing comments + $val =~ s/\s*#.*//; + } + if ( my ($num, $factor) = $val =~ m/(\d+)([KMGT])b?$/i ) { # value is a size like 1k, 16M, etc. my %factor_for = ( diff --git a/t/lib/MySQLConfig.t b/t/lib/MySQLConfig.t index 3e9b7b62..706e430e 100644 --- a/t/lib/MySQLConfig.t +++ b/t/lib/MySQLConfig.t @@ -827,6 +827,45 @@ SKIP: { ); } +$config = new MySQLConfig( + file => "$trunk/t/lib/samples/configs/mycnf-kc-001.txt", + TextResultSetParser => $trp, +); +is( + $config->value_of('user'), + 'mysql', + 'end of line comment in option file' +); + +is( + $config->value_of('password'), + 'password # still part of it!', + 'end of line comments respect quoted values' +); + +is( + $config->value_of('something'), + 'something ; or # another', + "..and removing comments doesn't leave trailing whitespace" +); + +ok( + defined $config->value_of('log_bin'), + "bools with comments in the end are found" +); + +is( + $config->value_of('log_bin'), + "ON", + "And the comment is correctly stripped out" +); + +is_deeply( + [ sort keys %{$config->variables} ], + [ sort qw( password something user log_bin )], + "start of line comments with # or ; are ignored" +); + # ############################################################################# # Use of uninitialized value in substitution (s///) at pt-config-diff line 1996 # https://bugs.launchpad.net/percona-toolkit/+bug/917770 @@ -865,5 +904,6 @@ like( '_d() works' ); ok($sb->ok(), "Sandbox servers") or BAIL_OUT(__FILE__ . " broke the sandbox"); + done_testing; exit; diff --git a/t/lib/samples/configs/mycnf-kc-001.txt b/t/lib/samples/configs/mycnf-kc-001.txt new file mode 100644 index 00000000..9b501bdf --- /dev/null +++ b/t/lib/samples/configs/mycnf-kc-001.txt @@ -0,0 +1,9 @@ +[mysqld] +user=mysql # comment +password="password # still part of it!"# comment +something='something ; or # another' # comment +;semicolon="start of line comment with ;" +#pound="start of line comment with #" + ;spacecolon + #spacepound +log_bin # bool with comment