#!/usr/bin/perl -w
# -*- perl -*-
use P4CGI ;
use strict ;
#
#################################################################
# CONFIGURATION INFORMATION
# All config info should be in P4CGI.pm
#
#################################################################
#
# P4 label diff viewer
# View diff between two labels
#
#################################################################
# Get arguments
# Labels to diff
my $LABEL1 = P4CGI::cgi()->param("LABEL1") ;
my $LABEL2 = P4CGI::cgi()->param("LABEL2") ;
&P4CGI::error("No first label specified") unless defined $LABEL1 ;
#&P4CGI::error("No second label specified") unless defined $LABEL2 ;
$LABEL1 = &P4CGI::htmlEncode($LABEL1) ;
$LABEL2 = &P4CGI::htmlEncode($LABEL2) if defined $LABEL2 ;
# Set to true if we compare with current depot
my $compWithCurr = ! defined $LABEL2 ;
# defined if files that are the same in both labels
# should be listed
my $SHOWSAME = P4CGI::cgi()->param("SHOWSAME") ;
if(defined $SHOWSAME) { undef $SHOWSAME if $SHOWSAME ne "Y" ; } ;
# defined if files that are not the same in botha labels
# should be listed
my $SHOWNOTSAME = P4CGI::cgi()->param("SHOWNOTSAME") ;
if(defined $SHOWNOTSAME) { undef $SHOWNOTSAME if $SHOWNOTSAME ne "Y" ; } ;
# defined if files that exists only in one of the labels
# shold be displayed
my $SHOWDIFF = P4CGI::cgi()->param("SHOWDIFF") ;
if(defined $SHOWDIFF) { undef $SHOWDIFF if $SHOWDIFF ne "Y" ; } ;
sub compareFiles($$) {
my ($a,$b) = @_ ;
if(!defined $a) {
return 1 ; # In this context an undef value is higher than any other
}
if(!defined $b) {
return -1 ;
}
if(&P4CGI::IGNORE_CASE() eq "Yes") {
return uc($a) cmp uc($b) ;
}
else {
return $a cmp $b ;
} ;
}
my $title = "
Diff between | | " ;
$title .= "label $LABEL1 and label $LABEL2" if !$compWithCurr ;
$title .= "label $LABEL1 and current depot" if $compWithCurr ;
$title .= " |
" ;
#
# Start page
#
print
&P4CGI::start_page($title),
"
",
&P4CGI::start_framedTable("Diff",""),
#
# Get basic data for labels
#
my %label1Data ;
my @fields = &P4CGI::p4readform("label -o '$LABEL1'",\%label1Data) ;
my %label2Data ;
if($compWithCurr) {
my $f ;
foreach $f (@fields) {
$label2Data{$f} = " " ;
} ;
$label2Data{"View"} = $label1Data{"View"} ;
}
else {
&P4CGI::p4readform("label -o '$LABEL2'",\%label2Data)
}
my $lab2hdr ;
$lab2hdr = "Label $LABEL2:" if !$compWithCurr ;
$lab2hdr = "Current depot:" if $compWithCurr ;
#
# Print basic label data
#
print "", # Start table
&P4CGI::start_table(""),
&P4CGI::table_row("",
{-class=>"\"ListHeader\"",
-text=>"Label $LABEL1:"},
{-class=>"\"ListHeader\"",
-text=>$lab2hdr}) ;
my $f ;
shift @fields ; # remove "Label" from fields (redundant)
# print label information
if(exists $label1Data{"Description"}) {
$label1Data{"Description"} =
&P4CGI::formatDescription($label1Data{"Description"}) ;
}
if(exists $label2Data{"Description"}) {
$label2Data{"Description"} =
&P4CGI::formatDescription($label2Data{"Description"}) ;
}
#
# Get files for labels
#
# Get files
my (@filesLabel1,@filesLabel2);
&P4CGI::p4call(\@filesLabel1, "files \"\@$LABEL1\"" );
if($compWithCurr) {
my @view = split("\n",$label1Data{"View"}) ;
my $v ;
my %tmph ;
foreach $v (@view) {
my @files ;
&P4CGI::p4call(\@files, "files \"$v\"" );
map { $tmph{$_} = 1 ; } @files ;
}
@filesLabel2 = keys %tmph ;
}
else {
&P4CGI::p4call(\@filesLabel2, "files \"\@$LABEL2\"" );
}
# Remove revision info and create file-to-rev maps
my (%fileRevLabel1,%fileRevLabel2) ;
map { s/\#(\d+).*// ; $fileRevLabel1{$_} = $1 } @filesLabel1 ;
map { s/\#(\d+).*// ; $fileRevLabel2{$_} = $1 } @filesLabel2 ;
# Sort files
{
my @tmp = @filesLabel1 ;
@filesLabel1 = sort { compareFiles($a,$b) ; } @tmp ;
@tmp = @filesLabel2 ;
@filesLabel2 = sort { compareFiles($a,$b) ; } @tmp ;
}
# Get some statistics
my $commonFound=0 ;
my $commonAndSameRev=0 ;
{
my $f ;
foreach $f (@filesLabel1) {
if(exists $fileRevLabel2{$f}) {
$commonFound++ ;
$commonAndSameRev++ if $fileRevLabel2{$f} == $fileRevLabel1{$f} ;
}
}
}
push @fields,"Files in label" ;
$label1Data{"Files in label"} = scalar @filesLabel1 . " files" ;
$label2Data{"Files in label"} = scalar @filesLabel2 . " files" ;
push @fields,"Common files" ;
$label1Data{"Common files"} = "$commonFound files in common" ;
$label2Data{"Common files"} = "" ;
push @fields,"Same revision" ;
$label1Data{"Same revision"} = "$commonAndSameRev files with same revision" ;
$label2Data{"Same revision"} = "" ;
# Fix View field
{
$label1Data{"View"} = "$label1Data{View}" ;
$label1Data{"View"} =~ s/\n/
/g ;
$label2Data{"View"} = "$label2Data{View}" ;
$label2Data{"View"} =~ s/\n/
/g ;
}
foreach $f (@fields) {
my $f1 = $label1Data{$f} ;
my $f2 = $label2Data{$f} ;
my %x ;
$x{class} = "Description" if $f eq "Description" ;
if($f2 ne "") {
$f1 = {%x,
-text => "$f1"} ;
$f2 = {%x,
-text => "$f2"} ;
}
else {
$f2 = {%x,
-align => "\"center\"",
-text =>"$f1"} ;
$f1 = undef ;
}
print "",&P4CGI::table_row({-class => "\"Prompt\"",
-text => "$f"},
$f1,
$f2) ;
} ;
print "",
&P4CGI::end_table(),
"
";
my ($nfiles1,$nfiles2) ;
$nfiles1 = @filesLabel1 ;
$nfiles2 = @filesLabel2 ;
my $fileslisted = "Yes" ;
sub printTableLine($$$$) {
my $file = shift @_ ;
my $rev1 = shift @_ ;
my $diff = shift @_ ;
my $rev2 = shift @_ ;
print &P4CGI::table_row($file,
{-text=>$rev1,
-align=>"center"},
$diff,
{-text=>$rev2,
-align=>"center"}) ;
} ;
if($commonFound == 0) {
print
"The two labels have no files in common," ;
}
else {
print &P4CGI::start_framedTable("Files:") ;
if(defined $SHOWSAME and defined $SHOWNOTSAME and defined $SHOWDIFF) {
print "Files:
\n" ;
}
elsif(!defined $SHOWSAME and !defined $SHOWNOTSAME and !defined $SHOWDIFF) {
print "No files listed!
\n" ;
$fileslisted = undef ;
}
else {
print "Listed files are:
\n" ;
defined $SHOWSAME and do {
print "- Files not modified\n" ; } ;
defined $SHOWNOTSAME and do {
print "
- Modified files (different rev.)\n" ; } ;
defined $SHOWDIFF and do {
if($compWithCurr) {
print "
- Files only in label $LABEL1 or current depot\n" ;
}
else {
print "
- Files only in label $LABEL1 or $LABEL2\n" ;
} ;
} ;
print "
\n" ;
} ;
#
# Start print list of files
#
if(defined $fileslisted) {
print
"",
&P4CGI::start_table(""),
&P4CGI::table_row("-type","th",
"File","$LABEL1","",$lab2hdr) ;
my ($name1,$name2) ;
while(@filesLabel1 > 0 or @filesLabel2 > 0) {
$name1 = shift @filesLabel1 unless defined $name1 ;
$name2 = shift @filesLabel2 unless defined $name2 ;
my $rev1 = $fileRevLabel1{$name1} if defined $name1 ;
my $rev2 = $fileRevLabel2{$name2} if defined $name2 ;
my $rev1Text = &P4CGI::ahref(-url=>"fileViewer.cgi",
"FSPC=$name1",
"REV=$rev1",
"HELP=View file $name1\#$rev1",
"\#$rev1") if defined $name1 ;
my $rev2Text = &P4CGI::ahref(-url=>"fileViewer.cgi",
"FSPC=$name2",
"REV=$rev2",
"HELP=View file $name2\#$rev2",
"\#$rev2") if defined $name2 ;
my $name1Text = &P4CGI::ahref(-url => "fileLogView.cgi",
"FSPC=$name1",
"HELP=View file log",
"$name1") if defined $name1 ;
my $name2Text = &P4CGI::ahref(-url => "fileLogView.cgi",
"FSPC=$name2",
"HELP=View file log",
"$name2") if defined $name2 ;
my $cmp = compareFiles($name1,$name2) ;
if($cmp == 0) {
if($rev1 == $rev2 and defined $SHOWSAME) {
printTableLine($name1Text,
$rev1Text,
"",
$rev2Text) ;
}
if($rev1 != $rev2 and defined $SHOWNOTSAME) {
printTableLine($name1Text,
$rev1Text,
&P4CGI::ahref(-url=>"fileDiffView.cgi",
"FSPC=$name1",
"REV=$rev1",
"REV2=$rev2",
"ACT=edit",
"HELP=Show Diff",
"diff"),
$rev2Text) ;
}
$name1 = undef ;
$name2 = undef ;
next ;
}
if($cmp < 0) {
if(defined $SHOWDIFF) {
printTableLine($name1Text,
$rev1Text,
"",
"--") ;
}
$name1=undef ;
next ;
}
else {
if(defined $SHOWDIFF) {
printTableLine($name2Text,
"--",
"",
$rev2Text) ;
}
$name2=undef ;
next ;
}
}
print
&P4CGI::end_table(),
&P4CGI::end_framedTable() ;
}
}
print
&P4CGI::end_framedTable(),
&P4CGI::end_page() ;
#
# That's all folks
#