Changeset 53076 in webkit


Ignore:
Timestamp:
Jan 11, 2010 7:52:48 AM (14 years ago)
Author:
ddkilzer@apple.com
Message:

Created a unit-tested function to parse the header block of
a Git or SVN diff -- for future refactoring of svn-apply and
svn-unapply.

Patch by Chris Jerdonek <chris.jerdonek@gmail.com> on 2010-01-11
Reviewed by David Kilzer.

https://bugs.webkit.org/show_bug.cgi?id=33447

  • Scripts/VCSUtils.pm:
    • Added parseDiffHeader().
    • Removed irrelevant comment from gitdiff2svndiff().
  • Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl: Added.
    • Added 48 unit tests for parseDiffHeader().
Location:
trunk/WebKitTools
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebKitTools/ChangeLog

    r53066 r53076  
     12010-01-11  Chris Jerdonek  <chris.jerdonek@gmail.com>
     2
     3        Reviewed by David Kilzer.
     4
     5        Created a unit-tested function to parse the header block of
     6        a Git or SVN diff -- for future refactoring of svn-apply and
     7        svn-unapply.
     8
     9        https://bugs.webkit.org/show_bug.cgi?id=33447
     10
     11        * Scripts/VCSUtils.pm:
     12          - Added parseDiffHeader().
     13          - Removed irrelevant comment from gitdiff2svndiff().
     14
     15        * Scripts/webkitperl/VCSUtils_unittest/parseDiffHeader.pl: Added.
     16          - Added 48 unit tests for parseDiffHeader().
     17
    1182010-01-10  Adam Barth  <abarth@webkit.org>
    219
  • trunk/WebKitTools/Scripts/VCSUtils.pm

    r53040 r53076  
    366366{
    367367    $_ = shift @_;
    368    
    369     # \V is any character that is not vertical white space
     368
    370369    if (m#^diff --git \w/(.+) \w/([^\r\n]+)#) {
    371370        return "Index: $1$POSTMATCH";
     
    381380    }
    382381    return $_;
     382}
     383
     384# Parse the next diff header from the given file handle, and advance
     385# the file handle so the last line read is the first line after the
     386# parsed header block.
     387#
     388# This subroutine dies if given leading junk.
     389#
     390# Args:
     391#   $fileHandle: advanced so the last line read is the first line of the
     392#                next diff header. For SVN-formatted diffs, this is the
     393#                "Index:" line.
     394#   $line: the line last read from $fileHandle
     395#
     396# Returns ($headerHashRef, $foundHeaderEnding, $lastReadLine):
     397#   $headerHashRef: a hash reference representing a diff header
     398#     copiedFromPath: the path in the "from" clause, if any.
     399#     copiedFromVersion: the revision number in the "from" clause, if any.
     400#     indexPath: the path in the "Index:" line.
     401#     svnConvertedText: the header text converted to SVN format.
     402#                       Unrecognized Git lines are left alone.
     403#     version: the revision number of the source.
     404#   $foundHeaderEnding: whether the last header block line was found.
     405#                       This is a line beginning with "+++".
     406#   $lastReadLine: the line last read from $fileHandle. If EOF has not
     407#                  been reached, this is the first line after the
     408#                  header ending.
     409sub parseDiffHeader($$)
     410{
     411    my ($fileHandle, $line) = @_;
     412
     413    my $filter;
     414    if ($line =~ m#^diff --git #) {
     415        $filter = \&gitdiff2svndiff;
     416    }
     417    $line = &$filter($line) if $filter;
     418
     419    my $indexPath;
     420    if ($line =~ /^Index: ([^\r\n]+)/) {
     421        $indexPath = $1;
     422    } else {
     423        die("Could not parse first line of diff header: \"$line\".");
     424    }
     425
     426    my %header;
     427
     428    my $foundHeaderEnding = 0;
     429    my $lastReadLine;
     430    my $svnConvertedText = $line;
     431    while (<$fileHandle>) {
     432        # Temporarily strip off any end-of-line characters to simplify
     433        # regex matching below.
     434        s/([\n\r]+)$//;
     435        my $eol = $1;
     436
     437        $_ = &$filter($_) if $filter;
     438
     439        # Fix paths on "diff", "---", and "+++" lines to match the
     440        # leading index line.
     441        s/\S+$/$indexPath/ if /^diff/; # FIXME: Can this ever occur? If so, include
     442                                       #        a unit test. Otherwise, remove it.
     443        s/^--- \S+/--- $indexPath/;
     444        if (/^--- .+\(from (\S+):(\d+)\)$/) {
     445            $header{copiedFromPath} = $1;
     446            $header{copiedFromVersion} = $2 if ($2 != 0);
     447        } elsif (/^--- .+\(revision (\d+)\)$/) {
     448            $header{version} = $1 if ($1 != 0);
     449        } elsif (s/^\+\+\+ \S+/+++ $indexPath/) {
     450            $foundHeaderEnding = 1;
     451        }
     452
     453        $svnConvertedText .= "$_$eol"; # Also restore EOL characters.
     454        if ($foundHeaderEnding) {
     455            $lastReadLine = <$fileHandle>;
     456            last;
     457        }
     458    } # $lastReadLine is undef if while loop ran out.
     459
     460    $header{indexPath} = $indexPath;
     461    $header{svnConvertedText} = $svnConvertedText;
     462
     463    return (\%header, $foundHeaderEnding, $lastReadLine);
    383464}
    384465
Note: See TracChangeset for help on using the changeset viewer.