Skip to contentSkip to conversation

gitweb: reset input record separator in parse_commit even on error

PATCH In review Sven Verdoolaege 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gitweb: reset input record separator in parse_commit even on error

---
This probably never happens, but just in case...

 gitweb/gitweb.perl |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
gitweb/gitweb.perl +1 -1
index 7afdf33..60dd598 100755
@@ -897,8 +897,8 @@ sub parse_commit {
 		my $fd = git_pipe("rev-list", "--header", "--parents", "--max-count=1", $commit_id)
 			or return;
 		@commit_lines = split '\n', <$fd>;
-		close $fd or return;
 		$/ = "\n";
+		close $fd or return;
 		pop @commit_lines;
 	}
 	my $header = shift @commit_lines;
Signature · 1 line
0.99.8c.g64e8-dirty
#1 lore raw
Quoted 5 lines · Sven Verdoolaege
gitweb/gitweb.perl +0 -0
index 7afdf33..60dd598 100755

gitweb/gitweb.perl sub parse_commit { · line 904

Quoted 9 lines
gitweb/gitweb.perl +1 -1
@@ -897,8 +897,8 @@ sub parse_commit {
 		my $fd = git_pipe("rev-list", "--header", "--parents", "--max-count=1", $commit_id)
 			or return;
 		@commit_lines = split '\n', <$fd>;
-		close $fd or return;
 		$/ = "\n";
+		close $fd or return;
 		pop @commit_lines;
 	}
 	my $header = shift @commit_lines;

You missed the other early return from git_pipe. However, I think the approach is wrong; this is a great opportunity to use the dynamic scoping offered by 'local':

  else {
    local $/ = "\0";
    # do stuff with $/ as "\0"
  }
  # $/ has automatically been reset at the end of the block

Looking further at this bit of code, it seems even more confusing, though. We split the output of rev-list on NUL, grab presumably the entire thing (since there shouldn't be any NULs in the output, right?) and then split it on newline into a list. Why aren't we doing this:

  else {
    open my $fd, ...;
    @commit_lines = map { chomp; $_ } <$fd>;
    ...

-Peff

#2 lore raw
Quoted 21 lines · Jeff King
Quoted 5 lines · Sven Verdoolaege
gitweb/gitweb.perl +0 -0
index 7afdf33..60dd598 100755

gitweb/gitweb.perl sub parse_commit { · line 904

Quoted 9 lines
gitweb/gitweb.perl +1 -1
@@ -897,8 +897,8 @@ sub parse_commit {
             my $fd = git_pipe("rev-list", "--header", "--parents", "--max-count=1", $commit_id)
                     or return;
             @commit_lines = split '\n', <$fd>;
-            close $fd or return;
             $/ = "\n";
+            close $fd or return;
             pop @commit_lines;
     }
     my $header = shift @commit_lines;

You missed the other early return from git_pipe. However, I think the approach is wrong; this is a great opportunity to use the dynamic scoping offered by 'local':

Quoted 5 lines
  else {
    local $/ = "\0";
    # do stuff with $/ as "\0"
  }
  # $/ has automatically been reset at the end of the block

And this should be done consistently, for all plain formats (blob_plain, blobdiff_plain, commitdiff_plain), and some other
places.

Sometimes it is
local $/ = "\0";
more often
local $/ = undef;
(or equivalently but slightly less human friendly, just 'local $/');

Quoted 8 lines

Looking further at this bit of code, it seems even more confusing, though. We split the output of rev-list on NUL, grab presumably the entire thing (since there shouldn't be any NULs in the output, right?) and then split it on newline into a list. Why aren't we doing this:

  else {
    open my $fd, ...;
    @commit_lines = map { chomp; $_ } <$fd>;
    ...

That is because by default git-rev-list separates output for different commits (when --header option is used) bu NULL... only because of "--max-count=1" there is only _one_ record... nevertheless it ends with "\0" (NULL) character.

Signature · 3 lines
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git