[p4] diff2 + added files?

Tetlow, Gordon gtetlow at soe.sony.com
Wed Dec 5 10:19:24 PST 2007


Here is a Perl script that does what you want. I've had it for years and
it's been incredibly useful. Basically, it turns diff2 into a GNU style
patch.

Sorry for any wrapping.

-gordon

#!/usr/bin/perl

# new usage:  ./p4patch
# Usage: p4 diff2 -du  base_view  your_view

use POSIX (strftime);
use File::Basename;

die "Usage: $0 [srcpath] [dstpath]\n" if ( $#ARGV ne 1 );

my $src_depot = shift;
my $dst_depot = shift;

my $src_base=dirname($src_depot);
my $dst_base=dirname($dst_depot);

$cmd = "p4 diff2 -du $src_depot $dst_depot";
open(P, "$cmd |") || die $!;

$time = localtime();
$time = strftime "%a %b %e %H:%M:%S %Y", localtime;
$time = strftime "%Y/%m/%d %T", localtime;

while (<P>) {
    ($f1, $f2, $r) = m|^==== (.+) - (.+) ====? ?(.*)| or print, next;
    next if $r eq 'identical';

    ($src, $srcrev) = ($f1 =~ m|$src_base/([^#]*)#(\d+)|);
    ($dst, $dstrev) = ($f2 =~ m|$dst_base/([^#]*)#(\d+)|);
    print "Index: $dst\n";
    print '=' x 75 . "\n";

    if ($r eq '') {
        print `p4 print -q $f2 | diff -Nc /dev/null - | sed "s#^---
-#--- $dst#"`
                if ($f1 =~ /<\s*none\s*>/);
        print `p4 print -q $f1 | diff -Nc - /dev/null | sed
"s#^[*][*][*] -#*** $src#"`
                if ($f2 =~ /<\s*none\s*>/);
        next;
    }

    print "--- $src\t$time\t#$srcrev\n";
    print "+++ $dst\t$time\n";
} 

-----Original Message-----
From: perforce-user-bounces at perforce.com
[mailto:perforce-user-bounces at perforce.com] On Behalf Of Sohail Somani
Sent: Tuesday, December 04, 2007 9:01 AM
To: perforce-user at perforce.com
Subject: Re: [p4] diff2 + added files?

On Tue, 04 Dec 2007 09:01:18 -0700, Looney, James B wrote:

> I usually do that using labels.  I'll create a label in one workspace
> (usually our mainline) and the second in a branch.  Then use p4 diff2
-q
> //... at mainline_label //... at branch_label
> 
> If you're scripting it, I'd recommend adding the -G (marshaling) as it
> makes the file organizing a LOT easier since it'll tell you which
files
> were added/deleted/modified without you having to parse each line
> yourself.  The -q just suppresses the actual diff output and the
header
> info for identical files.

Thanks for your reply.

Unfortunately, I don't think I was clear enough! What I want to do is 
take a diff of the two branches for the purposes of code review. So I 
want all the added and removed content, including *added* and *deleted* 
files. So say hello.txt was added with text "Hello, World", I'd want to 
see:

==== <none> - //.../hello.txt
+ Hello, World

Similarly, if the file was deleted, I'd want to see:

==== //.../hello.txt - <none>
- Hello, World

For all the text files. Right now, I can only see:

==== <none> - //.../hello.txt
==== //.../hello.txt - <none>

i.e., no content.

-- 
Sohail Somani
http://uint32t.blogspot.com
_______________________________________________
perforce-user mailing list  -  perforce-user at perforce.com
http://maillist.perforce.com/mailman/listinfo/perforce-user



More information about the perforce-user mailing list