summaryrefslogtreecommitdiffstats
path: root/contrib/perl5/Porting/p4desc
blob: 0bf79da2e004b2df741c73b17cd2a382f796aca8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl -wpi.bak

#
# Munge "p4 describe ..." output to include new files.
#
# Gurusamy Sarathy <gsar@activestate.com>
#

use vars qw($thisfile $change $file $fnum $h $v $p4port @addfiles);

BEGIN {
    $0 =~ s|^.*/||;
    $p4port = $ENV{P4PORT} || 'localhost:1666';
    for (@ARGV) {
        if ($p4port =~ /^\s+$/) {
	   $p4port = $_;
	}
        elsif (/^-p(.*)$/) {
	    $p4port = $1 || ' ';
	}
	elsif (/^-v$/) {
	    $v++;
	}
	elsif (/^-h/) {
	    $h++;
	}
	else {
	    push @files, $_;
	}
    }
    unless (@files) { @files = '-'; undef $^I; }
    @ARGV = @files;
    if ($h) {
	print STDERR <<USAGE;
Usage: $0 [-p \$P4PORT] [-v] [-h] [files]

	-p host:port	p4 port (e.g. myhost:1666)
	-h		print this help
	-v		output progress messages

A smart 'cat'.  When fed the spew from "p4 describe ..." on STDIN,
spits it right out on STDOUT, followed by patches for any new files
detected in the spew.  Can also be used to edit insitu a bunch of
files containing said spew.

WARNING: Currently only emits unified diffs.

Examples:
	p4 describe -du 123 | $0 > change-123.desc
	p4 describe -du 123 | $0 | p4d2p > change-123.patch

USAGE
	exit(0);
    }
    $thisfile = "";
}


if ($ARGV ne $thisfile) {
    warn "processing patchfile [$ARGV]\n" unless $ARGV eq '-';
    $thisfile = $ARGV;
}

my $cur = m|^Affected files| ... m|^Differences|;

# while we are within range
if ($cur) {
    if (m{^\.\.\. (//depot/.+?#\d+) (add|branch)$}) {
	my $newfile = $1;
	push @addfiles, $newfile;
	warn "$newfile add, revision != 1!\n" unless $newfile =~ /#1$/;
    }
    warn "file [$file] line [$cur] file# [$fnum]\n" if $v;
}

if (/^Change (\d+) by/) {
    $_ = "\n\n" . $_ if $change;	# start of a new change list
    $change = $1;
    my $new = newfiles();
    if ($new) {
	$_ = $new . $_;
    }
}

if (eof) {
    $_ .= newfiles();
}

sub newfiles {
    my $addfile;
    my $ret = "";
    for $addfile (@addfiles) {
	my $type = `p4 -p $p4port files '$addfile'`;
	if ($?) {
	    warn "$0: `p4 -p $p4port print '$addfile'` failed, status[$?]\n";
	    next;
	}
	$type =~ m|^//.*\((.+)\)$| or next;
	$type = $1;
	unless ($type =~ /text/) {
	    $ret .= "\n==== $addfile ($type) ====\n\n";
	    next;
	}
	my @new = `p4 -p $p4port print '$addfile'`;
	if ($?) {
	    die "$0: `p4 -p $p4port print '$addfile'` failed, status[$?]\n";
	}
	my $desc = shift @new;		# discard initial description
	$ret .= "\n==== $addfile ($type) ====\n\n";
	my $lines = "," . @new;
	$lines = "" if @new < 2;
	$ret .= "\@\@ -0,0 +1$lines \@\@\n";
	$ret .= join("+","",@new);
	$ret .= "\n\\ No newline at end of file\n" if $ret !~ /\n$/;
    }
    @addfiles = ();
    return $ret;
}
OpenPOWER on IntegriCloud