git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 07/22] git-remote-mediawiki: Change style of some regular expressions

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jun 8, 2013, 00:12 UTC
Message-ID
<CAPig+cQ6MZpC2hGZUSf_9Xs7zFOgZG9EsNEDw1J-R48xvNQLNg@mail.gmail.com>
In-Reply-To
<1370641344-4253-8-git-send-email-celestin.matte@ensimag.fr>

On Fri, Jun 7, 2013 at 5:42 PM, Célestin Matte <celestin.matte@ensimag.fr> wrote:

Show 5 quoted lines
> - Remove m modifier when useless (m// and // was used randomly; this makes the
> code more coherent)
> - Remove stringy split (split('c', ...) instead of split(/c/, ...))
> - Use {}{} instead of /// when slashes are used inside the regexp so as not to
> escape it.

When you split up 1/22 from v1 into distinct patches, review became much easier. The same can be done here (IMHO). The above bullet points are each conceptually distinct (even if they may happen to overlap textually in some cases).

> A "split ' '" is turned into a "split / /", which changes its behaviour: the old
> method matched a run of whtespaces (/\s*/), while the new one will match a
> single whitespace, which is what we want here.

The phrase "which is what we want here" does not convey much. Taking a hint from Junio's responses explaining why he finds this subtle change acceptable, perhaps a paragraph like this might be appropriate:

  Note the special case split(' ') for which Perl splits on runs of
  whitespace, whereas split(/ /) splits on exactly one space.  In
  other contexts, changing split(' ') to split(/ /) could potentially
  be a regression, however, here, when parsing the output of
  "rev-list --parents", whose output SHA-1's are each separated by a
  single space, splitting on a single space is perfectly correct.
More comments below.
Show 25 quoted lines
> Signed-off-by: Célestin Matte <celestin.matte@ensimag.fr>
> Signed-off-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
> ---
>  contrib/mw-to-git/git-remote-mediawiki.perl |   20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/contrib/mw-to-git/git-remote-mediawiki.perl b/contrib/mw-to-git/git-remote-mediawiki.perl
> index a5c963b..482cd95 100755
> --- a/contrib/mw-to-git/git-remote-mediawiki.perl
> +++ b/contrib/mw-to-git/git-remote-mediawiki.perl
> @@ -121,7 +121,7 @@ chomp($dumb_push);
>  $dumb_push = ($dumb_push eq "true");
>
>  my $wiki_name = $url;
> -$wiki_name =~ s/[^\/]*:\/\///;
> +$wiki_name =~ s{[^/]*://}{};
>  # If URL is like http://user:password@example.com/, we clearly don't
>  # want the password in $wiki_name. While we're there, also remove user
>  # and '@' sign, to avoid author like MWUser@HTTPUser@host.com
> @@ -565,7 +565,7 @@ sub mediawiki_smudge {
>
>  sub mediawiki_clean_filename {
>         my $filename = shift;
> -       $filename =~ s/@{[SLASH_REPLACEMENT]}/\//g;
> +       $filename =~ s{$SLASH_REPLACEMENT}{/}g;

The change from SLASH_REPLACEMENT to $SLASH_REPLACEMENT should have happened in patch 2/22 where 'constant' was replaced with 'Readonly'.

Show 9 quoted lines
>         # [, ], |, {, and } are forbidden by MediaWiki, even URL-encoded.
>         # Do a variant of URL-encoding, i.e. looks like URL-encoding,
>         # but with _ added to prevent MediaWiki from thinking this is
> @@ -579,7 +579,7 @@ sub mediawiki_clean_filename {
>
>  sub mediawiki_smudge_filename {
>         my $filename = shift;
> -       $filename =~ s/\//@{[SLASH_REPLACEMENT]}/g;
> +       $filename =~ s{/}{$SLASH_REPLACEMENT}g;
Ditto regarding patch 2/22.
Show 33 quoted lines
>         $filename =~ s/ /_/g;
>         # Decode forbidden characters encoded in mediawiki_clean_filename
>         $filename =~ s/_%_([0-9a-fA-F][0-9a-fA-F])/sprintf("%c", hex($1))/ge;
> @@ -762,7 +762,7 @@ sub get_more_refs {
>         my @refs;
>         while (1) {
>                 my $line = <STDIN>;
> -               if ($line =~ m/^$cmd (.*)$/) {
> +               if ($line =~ /^$cmd (.*)$/) {
>                         push(@refs, $1);
>                 } elsif ($line eq "\n") {
>                         return @refs;
> @@ -1168,11 +1168,11 @@ sub mw_push_revision {
>                 my @local_ancestry = split(/\n/, run_git("rev-list --boundary --parents $local ^$parsed_sha1"));
>                 my %local_ancestry;
>                 foreach my $line (@local_ancestry) {
> -                       if (my ($child, $parents) = $line =~ m/^-?([a-f0-9]+) ([a-f0-9 ]+)/) {
> -                               foreach my $parent (split(' ', $parents)) {
> +                       if (my ($child, $parents) = $line =~ /^-?([a-f0-9]+) ([a-f0-9 ]+)/) {
> +                               foreach my $parent (split(/ /, $parents)) {
>                                         $local_ancestry{$parent} = $child;
>                                 }
> -                       } elsif (!$line =~ m/^([a-f0-9]+)/) {
> +                       } elsif (!$line =~ /^([a-f0-9]+)/) {
>                                 die "Unexpected output from git rev-list: $line";
>                         }
>                 }
> @@ -1190,10 +1190,10 @@ sub mw_push_revision {
>                 # history (linearized with --first-parent)
>                 print STDERR "Warning: no common ancestor, pushing complete history\n";
>                 my $history = run_git("rev-list --first-parent --children $local");
> -               my @history = split('\n', $history);
> +               my @history = split(/\n/, $history);

This looks fishy. In Perl, '\n' is not a newline, but instead a literal backslash followed by an "n". The output of "rev-list --first-parent" is line-oriented, so this looks like a bug in the original code, which you may want to fix it in a separate patch to make it clear that this is an actual bug fix, distinct from the otherwise mechanical change of split('x') to split(/x/).

Show 17 quoted lines
>                 @history = @history[1..$#history];
>                 foreach my $line (reverse @history) {
> -                       my @commit_info_split = split(/ |\n/, $line);
> +                       my @commit_info_split = split(/[ \n]/, $line);
>                         push(@commit_pairs, \@commit_info_split);
>                 }
>         }
> @@ -1272,7 +1272,7 @@ sub get_mw_namespace_id {
>                 # Look at configuration file, if the record for that namespace is
>                 # already cached. Namespaces are stored in form:
>                 # "Name_of_namespace:Id_namespace", ex.: "File:6".
> -               my @temp = split(/[\n]/, run_git("config --get-all remote."
> +               my @temp = split(/\n/, run_git("config --get-all remote."
>                                                 . $remotename .".namespaceCache"));
>                 chomp(@temp);
>                 foreach my $ns (@temp) {
> --
Previous: Célestin MatteNext: Célestin Matte
Message 14 of 41 in “git-remote-mediawiki: Follow perlcritic's recommandations”
  1. 00/22 git-remote-mediawiki: Follow perlcritic's recommandationsCélestin Matte, Jun 7, 2013
  2. 01/22 git-remote-mediawiki: Replace :utf8 by :encoding(UTF-8)Célestin Matte, Jun 7, 2013
  3. 02/22 git-remote-mediawiki: Use the Readonly module instead of the constant pragmaCélestin Matte, Jun 7, 2013
  4. Jeff KingJun 8, 2013
  5. Benoît PersonJun 8, 2013
  6. Célestin PerduJun 8, 2013
  7. Jeff KingJun 8, 2013
  8. Matthieu MoyJun 8, 2013
  9. 03/22 git-remote-mediawiki: Always end a subroutine with a returnCélestin Matte, Jun 7, 2013
  10. 04/22 git-remote-mediawiki: Move a variable declaration at the top of the codeCélestin Matte, Jun 7, 2013
  11. 05/22 git-remote-mediawiki: Change syntax of map callsCélestin Matte, Jun 7, 2013
  12. 06/22 git-remote-mediawiki: Rewrite unclear line of instructionsCélestin Matte, Jun 7, 2013
  13. 07/22 git-remote-mediawiki: Change style of some regular expressionsCélestin Matte, Jun 7, 2013
  14. Eric SunshineJun 8, 2013
  15. 08/22 git-remote-mediawiki: Add newline in the end of die() error messagesCélestin Matte, Jun 7, 2013
  16. 09/22 git-remote-mediawiki: Change the name of a variableCélestin Matte, Jun 7, 2013
  17. 10/22 git-remote-mediawiki: Turn double-negated expressions into simple expressionsCélestin Matte, Jun 7, 2013
  18. 11/22 git-remote-mediawiki: Remove unused variable $entryCélestin Matte, Jun 7, 2013
  19. 12/22 git-remote-mediawiki: Rename a variable ($last) which has the name of a keywordCélestin Matte, Jun 7, 2013
  20. 13/22 git-remote-mediawiki: Assign a variable as undef and make proper indentationCélestin Matte, Jun 7, 2013
  21. 14/22 git-remote-mediawiki: Check return value of open + remove import of unused open2Célestin Matte, Jun 7, 2013
  22. Eric SunshineJun 8, 2013
  23. Célestin MatteJun 8, 2013
  24. Matthieu MoyJun 8, 2013
  25. Célestin MatteJun 8, 2013
  26. Matthieu MoyJun 8, 2013
  27. Eric SunshineJun 9, 2013
  28. 15/22 git-remote-mediawiki: Put long code into a subroutineCélestin Matte, Jun 7, 2013
  29. Eric SunshineJun 8, 2013
  30. 16/22 git-remote-mediawiki: Modify strings for a better coding-styleCélestin Matte, Jun 7, 2013
  31. Eric SunshineJun 8, 2013
  32. Célestin MatteJun 8, 2013
  33. Eric SunshineJun 9, 2013
  34. 17/22 git-remote-mediawiki: Brace file handles for print for more clarityCélestin Matte, Jun 7, 2013
  35. Eric SunshineJun 8, 2013
  36. 18/22 git-remote-mediawiki: Replace "unless" statements with negated "if" statementsCélestin Matte, Jun 7, 2013
  37. 19/22 git-remote-mediawiki: Don't use quotes for empty stringsCélestin Matte, Jun 7, 2013
  38. 20/22 git-remote-mediawiki: Put non-trivial numeric values in constants.Célestin Matte, Jun 7, 2013
  39. 21/22 git-remote-mediawiki: Fix a typo ("mediwiki" instead of "mediawiki")Célestin Matte, Jun 7, 2013
  40. 22/22 git-remote-mediawiki: Clearly rewrite double dereferenceCélestin Matte, Jun 7, 2013
  41. Eric SunshineJun 7, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.