diff options
Diffstat (limited to 'Bugzilla/PatchReader/AddCVSContext.pm')
-rw-r--r-- | Bugzilla/PatchReader/AddCVSContext.pm | 81 |
1 files changed, 51 insertions, 30 deletions
diff --git a/Bugzilla/PatchReader/AddCVSContext.pm b/Bugzilla/PatchReader/AddCVSContext.pm index 4e7da5661..094ef6ed8 100644 --- a/Bugzilla/PatchReader/AddCVSContext.pm +++ b/Bugzilla/PatchReader/AddCVSContext.pm @@ -9,7 +9,8 @@ use Bugzilla::PatchReader::CVSClient; use Cwd; use File::Temp; -@Bugzilla::PatchReader::AddCVSContext::ISA = qw(Bugzilla::PatchReader::FilterPatch); +@Bugzilla::PatchReader::AddCVSContext::ISA + = qw(Bugzilla::PatchReader::FilterPatch); # XXX If you need to, get the entire patch worth of files and do a single # cvs update of all files as soon as you find a file where you need to do a @@ -31,7 +32,8 @@ sub my_rmtree { foreach my $file (glob("$dir/*")) { if (-d $file) { $this->my_rmtree($file); - } else { + } + else { trick_taint($file); unlink $file; } @@ -43,6 +45,7 @@ sub my_rmtree { sub end_patch { my $this = shift; if (exists($this->{TMPDIR})) { + # Set as variable to get rid of taint # One would like to use rmtree here, but that is not taint-safe. $this->my_rmtree($this->{TMPDIR}); @@ -52,10 +55,10 @@ sub end_patch { sub start_file { my $this = shift; my ($file) = @_; - $this->{HAS_CVS_CONTEXT} = !$file->{is_add} && !$file->{is_remove} && - $file->{old_revision}; - $this->{REVISION} = $file->{old_revision}; - $this->{FILENAME} = $file->{filename}; + $this->{HAS_CVS_CONTEXT} + = !$file->{is_add} && !$file->{is_remove} && $file->{old_revision}; + $this->{REVISION} = $file->{old_revision}; + $this->{FILENAME} = $file->{filename}; $this->{SECTION_END} = -1; $this->{TARGET}->start_file(@_) if $this->{TARGET}; } @@ -66,7 +69,7 @@ sub end_file { if ($this->{FILE}) { close $this->{FILE}; - unlink $this->{FILE}; # If it fails, it fails ... + unlink $this->{FILE}; # If it fails, it fails ... delete $this->{FILE}; } $this->{TARGET}->end_file(@_) if $this->{TARGET}; @@ -76,10 +79,12 @@ sub next_section { my $this = shift; my ($section) = @_; $this->{NEXT_PATCH_LINE} = $section->{old_start}; - $this->{NEXT_NEW_LINE} = $section->{new_start}; + $this->{NEXT_NEW_LINE} = $section->{new_start}; foreach my $line (@{$section->{lines}}) { + # If this is a line requiring context ... if ($line =~ /^[-\+]/) { + # Determine how much context is needed for both the previous section line # and this one: # - If there is no old line, start new section @@ -89,20 +94,23 @@ sub next_section { # space and therefore we end the old section and start the new one # - Else we add (old start context line through new line) context to # existing section - if (! exists($this->{SECTION})) { + if (!exists($this->{SECTION})) { $this->_start_section(); - } elsif ($this->{CONTEXT} eq "file") { + } + elsif ($this->{CONTEXT} eq "file") { $this->push_context_lines($this->{SECTION_END} + 1, - $this->{NEXT_PATCH_LINE} - 1); - } else { + $this->{NEXT_PATCH_LINE} - 1); + } + else { my $start_context = $this->{NEXT_PATCH_LINE} - $this->{CONTEXT}; $start_context = $start_context > 0 ? $start_context : 0; if (($this->{SECTION_END} + $this->{CONTEXT} + 1) < $start_context) { $this->flush_section(); $this->_start_section(); - } else { + } + else { $this->push_context_lines($this->{SECTION_END} + 1, - $this->{NEXT_PATCH_LINE} - 1); + $this->{NEXT_PATCH_LINE} - 1); } } push @{$this->{SECTION}{lines}}, $line; @@ -110,16 +118,19 @@ sub next_section { $this->{SECTION}{plus_lines}++; $this->{SECTION}{new_lines}++; $this->{NEXT_NEW_LINE}++; - } else { + } + else { $this->{SECTION_END}++; $this->{SECTION}{minus_lines}++; $this->{SECTION}{old_lines}++; $this->{NEXT_PATCH_LINE}++; } - } else { + } + else { $this->{NEXT_PATCH_LINE}++; $this->{NEXT_NEW_LINE}++; } + # If this is context, for now lose it (later we should try and determine if # we can just use it instead of pulling the file all the time) } @@ -130,7 +141,8 @@ sub determine_start { return 0 if $line < 0; if ($this->{CONTEXT} eq "file") { return 1; - } else { + } + else { my $start = $line - $this->{CONTEXT}; $start = $start > 0 ? $start : 1; return $start; @@ -146,23 +158,26 @@ sub _start_section { $this->{SECTION}{old_lines} = 0; $this->{SECTION}{new_lines} = 0; $this->{SECTION}{minus_lines} = 0; - $this->{SECTION}{plus_lines} = 0; - $this->{SECTION_END} = $this->{SECTION}{old_start} - 1; + $this->{SECTION}{plus_lines} = 0; + $this->{SECTION_END} = $this->{SECTION}{old_start} - 1; $this->push_context_lines($this->{SECTION}{old_start}, - $this->{NEXT_PATCH_LINE} - 1); + $this->{NEXT_PATCH_LINE} - 1); } sub flush_section { my $this = shift; if ($this->{SECTION}) { + # Add the necessary context to the end if ($this->{CONTEXT} eq "file") { $this->push_context_lines($this->{SECTION_END} + 1, "file"); - } else { + } + else { $this->push_context_lines($this->{SECTION_END} + 1, - $this->{SECTION_END} + $this->{CONTEXT}); + $this->{SECTION_END} + $this->{CONTEXT}); } + # Send the section and line notifications $this->{TARGET}->next_section($this->{SECTION}) if $this->{TARGET}; delete $this->{SECTION}; @@ -172,35 +187,41 @@ sub flush_section { sub push_context_lines { my $this = shift; + # Grab from start to end my ($start, $end) = @_; return if $end ne "file" && $start > $end; # If it's an added / removed file, don't do anything - return if ! $this->{HAS_CVS_CONTEXT}; + return if !$this->{HAS_CVS_CONTEXT}; # Get and open the file if necessary if (!$this->{FILE}) { my $olddir = getcwd(); - if (! exists($this->{TMPDIR})) { + if (!exists($this->{TMPDIR})) { $this->{TMPDIR} = File::Temp::tempdir(); - if (! -d $this->{TMPDIR}) { + if (!-d $this->{TMPDIR}) { die "Could not get temporary directory"; } } chdir($this->{TMPDIR}) or die "Could not cd $this->{TMPDIR}"; - if (Bugzilla::PatchReader::CVSClient::cvs_co_rev($this->{CVSROOT}, $this->{REVISION}, $this->{FILENAME})) { - die "Could not check out $this->{FILENAME} r$this->{REVISION} from $this->{CVSROOT}"; + if (Bugzilla::PatchReader::CVSClient::cvs_co_rev( + $this->{CVSROOT}, $this->{REVISION}, $this->{FILENAME} + )) + { + die + "Could not check out $this->{FILENAME} r$this->{REVISION} from $this->{CVSROOT}"; } open(my $fh, '<', $this->{FILENAME}) or die "Could not open $this->{FILENAME}"; - $this->{FILE} = $fh; + $this->{FILE} = $fh; $this->{NEXT_FILE_LINE} = 1; - trick_taint($olddir); # $olddir comes from getcwd() + trick_taint($olddir); # $olddir comes from getcwd() chdir($olddir) or die "Could not cd back to $olddir"; } # Read through the file to reach the line we need - die "File read too far!" if $this->{NEXT_FILE_LINE} && $this->{NEXT_FILE_LINE} > $start; + die "File read too far!" + if $this->{NEXT_FILE_LINE} && $this->{NEXT_FILE_LINE} > $start; my $fh = $this->{FILE}; while ($this->{NEXT_FILE_LINE} < $start) { my $dummy = <$fh>; |