PatchReader.pm 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): John Keiser <john@johnkeiser.com>
#                 Frédéric Buclin <LpSolit@gmail.com>

use strict;

package Bugzilla::Attachment::PatchReader;

use Bugzilla::Error;
23
use Bugzilla::Attachment;
24
use Bugzilla::Util;
25 26 27 28 29

sub process_diff {
    my ($attachment, $format, $context) = @_;
    my $dbh = Bugzilla->dbh;
    my $cgi = Bugzilla->cgi;
30
    my $lc  = Bugzilla->localconfig;
31 32 33 34 35 36 37 38 39 40
    my $vars = {};

    my ($reader, $last_reader) = setup_patch_readers(undef, $context);

    if ($format eq 'raw') {
        require PatchReader::DiffPrinter::raw;
        $last_reader->sends_data_to(new PatchReader::DiffPrinter::raw());
        # Actually print out the patch.
        print $cgi->header(-type => 'text/plain',
                           -expires => '+3M');
41
        disable_utf8();
42 43 44
        $reader->iterate_string('Attachment ' . $attachment->id, $attachment->data);
    }
    else {
45
        my @other_patches = ();
46
        if ($lc->{interdiffbin} && $lc->{diffpath}) {
47 48 49 50 51 52 53 54
            # Get the list of attachments that the user can view in this bug.
            my @attachments =
                @{Bugzilla::Attachment->get_attachments_by_bug($attachment->bug_id)};
            # Extract patches only.
            @attachments = grep {$_->ispatch == 1} @attachments;
            # We want them sorted from newer to older.
            @attachments = sort { $b->id <=> $a->id } @attachments;

55 56 57
            # Ignore the current patch, but select the one right before it
            # chronologically.
            my $select_next_patch = 0;
58 59
            foreach my $attach (@attachments) {
                if ($attach->id == $attachment->id) {
60 61 62
                    $select_next_patch = 1;
                }
                else {
63 64 65 66
                    push(@other_patches, { 'id'       => $attach->id,
                                           'desc'     => $attach->description,
                                           'selected' => $select_next_patch });
                    $select_next_patch = 0;
67 68 69 70 71 72 73
                }
            }
        }

        $vars->{'bugid'} = $attachment->bug_id;
        $vars->{'attachid'} = $attachment->id;
        $vars->{'description'} = $attachment->description;
74
        $vars->{'other_patches'} = \@other_patches;
75 76

        setup_template_patch_reader($last_reader, $format, $context, $vars);
77 78 79
        # The patch is going to be displayed in a HTML page and if the utf8
        # param is enabled, we have to encode attachment data as utf8.
        if (Bugzilla->params->{'utf8'}) {
80 81
            $attachment->data; # Populate ->{data}
            utf8::decode($attachment->{data});
82
        }
83 84 85 86 87 88 89
        $reader->iterate_string('Attachment ' . $attachment->id, $attachment->data);
    }
}

sub process_interdiff {
    my ($old_attachment, $new_attachment, $format, $context) = @_;
    my $cgi = Bugzilla->cgi;
90
    my $lc  = Bugzilla->localconfig;
91 92
    my $vars = {};

93 94 95
    # Encode attachment data as utf8 if it's going to be displayed in a HTML
    # page using the UTF-8 encoding.
    if ($format ne 'raw' && Bugzilla->params->{'utf8'}) {
96 97 98 99
        $old_attachment->data; # Populate ->{data}
        utf8::decode($old_attachment->{data});
        $new_attachment->data; # Populate ->{data}
        utf8::decode($new_attachment->{data});
100 101
    }

102
    # Get old patch data.
103
    my ($old_filename, $old_file_list) = get_unified_diff($old_attachment, $format);
104
    # Get new patch data.
105
    my ($new_filename, $new_file_list) = get_unified_diff($new_attachment, $format);
106 107 108 109 110

    my $warning = warn_if_interdiff_might_fail($old_file_list, $new_file_list);

    # Send through interdiff, send output directly to template.
    # Must hack path so that interdiff will work.
111 112
    $ENV{'PATH'} = $lc->{diffpath};
    open my $interdiff_fh, "$lc->{interdiffbin} $old_filename $new_filename|";
113 114 115 116 117 118 119 120 121
    binmode $interdiff_fh;
    my ($reader, $last_reader) = setup_patch_readers("", $context);

    if ($format eq 'raw') {
        require PatchReader::DiffPrinter::raw;
        $last_reader->sends_data_to(new PatchReader::DiffPrinter::raw());
        # Actually print out the patch.
        print $cgi->header(-type => 'text/plain',
                           -expires => '+3M');
122
        disable_utf8();
123 124
    }
    else {
125 126 127
        # In case the HTML page is displayed with the UTF-8 encoding.
        binmode $interdiff_fh, ':utf8' if Bugzilla->params->{'utf8'};

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        $vars->{'warning'} = $warning if $warning;
        $vars->{'bugid'} = $new_attachment->bug_id;
        $vars->{'oldid'} = $old_attachment->id;
        $vars->{'old_desc'} = $old_attachment->description;
        $vars->{'newid'} = $new_attachment->id;
        $vars->{'new_desc'} = $new_attachment->description;

        setup_template_patch_reader($last_reader, $format, $context, $vars);
    }
    $reader->iterate_fh($interdiff_fh, 'interdiff #' . $old_attachment->id .
                        ' #' . $new_attachment->id);
    close $interdiff_fh;
    $ENV{'PATH'} = '';

    # Delete temporary files.
    unlink($old_filename) or warn "Could not unlink $old_filename: $!";
    unlink($new_filename) or warn "Could not unlink $new_filename: $!";
}

######################
#  Internal routines
######################

sub get_unified_diff {
152
    my ($attachment, $format) = @_;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    # Bring in the modules we need.
    require PatchReader::Raw;
    require PatchReader::FixPatchRoot;
    require PatchReader::DiffPrinter::raw;
    require PatchReader::PatchInfoGrabber;
    require File::Temp;

    $attachment->ispatch
      || ThrowCodeError('must_be_patch', { 'attach_id' => $attachment->id });

    # Reads in the patch, converting to unified diff in a temp file.
    my $reader = new PatchReader::Raw;
    my $last_reader = $reader;

    # Fixes patch root (makes canonical if possible).
    if (Bugzilla->params->{'cvsroot'}) {
        my $fix_patch_root =
            new PatchReader::FixPatchRoot(Bugzilla->params->{'cvsroot'});
        $last_reader->sends_data_to($fix_patch_root);
        $last_reader = $fix_patch_root;
    }

    # Grabs the patch file info.
    my $patch_info_grabber = new PatchReader::PatchInfoGrabber();
    $last_reader->sends_data_to($patch_info_grabber);
    $last_reader = $patch_info_grabber;

    # Prints out to temporary file.
    my ($fh, $filename) = File::Temp::tempfile();
183 184 185 186
    if ($format ne 'raw' && Bugzilla->params->{'utf8'}) {
        # The HTML page will be displayed with the UTF-8 encoding.
        binmode $fh, ':utf8';
    }
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    my $raw_printer = new PatchReader::DiffPrinter::raw($fh);
    $last_reader->sends_data_to($raw_printer);
    $last_reader = $raw_printer;

    # Iterate!
    $reader->iterate_string($attachment->id, $attachment->data);

    return ($filename, $patch_info_grabber->patch_info()->{files});
}

sub warn_if_interdiff_might_fail {
    my ($old_file_list, $new_file_list) = @_;

    # Verify that the list of files diffed is the same.
    my @old_files = sort keys %{$old_file_list};
    my @new_files = sort keys %{$new_file_list};
    if (@old_files != @new_files
        || join(' ', @old_files) ne join(' ', @new_files))
    {
        return 'interdiff1';
    }

    # Verify that the revisions in the files are the same.
    foreach my $file (keys %{$old_file_list}) {
        if ($old_file_list->{$file}{old_revision} ne
            $new_file_list->{$file}{old_revision})
        {
            return 'interdiff2';
        }
    }
    return undef;
}

sub setup_patch_readers {
    my ($diff_root, $context) = @_;

    # Parameters:
    # format=raw|html
    # context=patch|file|0-n
    # collapsed=0|1
    # headers=0|1

    # Define the patch readers.
    # The reader that reads the patch in (whatever its format).
    require PatchReader::Raw;
    my $reader = new PatchReader::Raw;
    my $last_reader = $reader;
    # Fix the patch root if we have a cvs root.
    if (Bugzilla->params->{'cvsroot'}) {
        require PatchReader::FixPatchRoot;
        $last_reader->sends_data_to(new PatchReader::FixPatchRoot(Bugzilla->params->{'cvsroot'}));
        $last_reader->sends_data_to->diff_root($diff_root) if defined($diff_root);
        $last_reader = $last_reader->sends_data_to;
    }

    # Add in cvs context if we have the necessary info to do it
243 244 245
    if ($context ne 'patch' && Bugzilla->localconfig->{cvsbin} 
        && Bugzilla->params->{'cvsroot_get'}) 
    {
246
        require PatchReader::AddCVSContext;
247 248 249
        # We need to set $cvsbin as global, because PatchReader::CVSClient
        # needs it in order to find 'cvs'.
        $main::cvsbin = Bugzilla->localconfig->{cvsbin};
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
        $last_reader->sends_data_to(
          new PatchReader::AddCVSContext($context, Bugzilla->params->{'cvsroot_get'}));
        $last_reader = $last_reader->sends_data_to;
    }

    return ($reader, $last_reader);
}

sub setup_template_patch_reader {
    my ($last_reader, $format, $context, $vars) = @_;
    my $cgi = Bugzilla->cgi;
    my $template = Bugzilla->template;

    require PatchReader::DiffPrinter::template;

    # Define the vars for templates.
    if (defined $cgi->param('headers')) {
        $vars->{'headers'} = $cgi->param('headers');
    }
    else {
270
        $vars->{'headers'} = 1;
271 272 273 274
    }

    $vars->{'collapsed'} = $cgi->param('collapsed');
    $vars->{'context'} = $context;
275 276
    $vars->{'do_context'} = Bugzilla->localconfig->{cvsbin} 
                            && Bugzilla->params->{'cvsroot_get'} && !$vars->{'newid'};
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

    # Print everything out.
    print $cgi->header(-type => 'text/html',
                       -expires => '+3M');

    $last_reader->sends_data_to(new PatchReader::DiffPrinter::template($template,
                                "attachment/diff-header.$format.tmpl",
                                "attachment/diff-file.$format.tmpl",
                                "attachment/diff-footer.$format.tmpl",
                                { %{$vars},
                                  bonsai_url => Bugzilla->params->{'bonsai_url'},
                                  lxr_url => Bugzilla->params->{'lxr_url'},
                                  lxr_root => Bugzilla->params->{'lxr_root'},
                                }));
}

1;

__END__