#! /usr/bin/perl
#
# NAME: renamebranch.pl
# DESC: This script uses original branchspec name to make a new branchspec,
# and then deletes the old branchspec.
#
# Command line equivalent
# p4 branch -o OLD | sed '/^Branch:/ c\Branch: NEW' | p4 branch -i
# p4 branch -d OLD
#
$branchold = shift ARGV; #The original branchspec name to rename.
$user = shift ARGV; #The current user.
$server = shift ARGV; #ip_address:port of your Perforce server.
$branchnew = shift ARGV; #The new branchspec name to replace original.
$p4 = "p4"; # Absolute path to the p4 cli program.
if ($branchold eq $branchnew)
{
print "\nNew branchspec name must be unique and not already exist.\n\nSelect a branchspec and try again.\n";
exit 1;
}
$cmd = "\"$p4\" -u $user -p $server branch -o \"$branchold\" | sed '/^Branch:/ c\Branch: \"$branchnew\"' | p4 branch -i |";
open RENAME, $cmd
or die "Cannot run rename branchspec command.\n\n";
$count = 0;
while (<RENAME>)
{
# Run the second command to delete the original branchspec
$cmd2 = "\"$p4\" -u $user -p $server branch -d \"$branchold\" |";
open DELORIG, $cmd2
or die "Cannot run delete for original branchspec command.\n\n";
$count++;
}
if($count ne 0) { print "\nBranchspec rename complete.\n\nBranchspec renamed from $branchold to $branchnew.\nBranchspec $branchold deleted.\n"; }
if($count eq 0) { print "\nNo branchspec rename performed.\n"; }
exit 0;