寻找p4perl示例代码,以便在P4存储库中添加/删除/签出/签入文件

时间:2013-05-17 15:14:44

标签: perl perforce

当我的p4perl脚本和文件属于同一个p4客户端时,我知道如何使用p4perl添加/删除/签入/签出文件。但是,当p4perl脚本和文件属于不同的p4客户端时,我不知道该怎么做。

我想我需要以某种方式调用$ p4-> FetchClient()。但我不知道该怎么做。

下面是我的实验代码,可以在同一个p4客户端下查看文件。

use strict;
use P4; # a p4perl module

**#my $p4root = "A P4 root dir" e.g. '//projects/...'
# File '$f1' is under a different p4 client root, e.g. $p4root
#my $f1 = $ENV{HOME}.'/work/aFile_ToBe_Checkedout_From_P4.pl';**

# File '$f2' is under the same p4 client root as this tool $0 is in
my $f2 = './runANI.pl';

&showFileMode($f2);

my $p4 = new P4;
$p4->Connect() or die( "Failed to connect to Perforce Server" );
**#$p4->RunEdit($f1); # To check out file '$f1'. It does not work
#&reportP4err($p4);**
$p4->RunEdit($f2); # To check out file '$f2'. It works!!
&reportP4err($p4);

&showFileMode($f2);
exit;

sub showFileMode {
  my ($file) = @_;
  my @properties = stat($file);
  my $mode = $properties[2];
  my $modeInDecimal = $mode & 07777;
  my $modeInOctal = sprintf("%04o", $modeInDecimal);
  if($modeInOctal eq '0555') {
    print "File '$file' is checked in with a mode: $modeInOctal\n";
  }
  elsif($modeInOctal eq '0755') {
    print "File '$file' is checked out with a mode: $modeInOctal\n";
  }
}

sub reportP4err {
  my ($p4) = @_;
  if ($p4->ErrorCount()) {
    print "In report_p4_errors()\n";
    foreach my $e ($p4->Errors()) {
      print "P4 Error MSG: $e\n";
    }
    die "P4 error, exiting";
  }
}

示例运行:

% ./testP4perl.pl
File './runANI.pl' is checked in with a mode: 0555
File './runANI.pl' is checked out with a mode: 0755

1 个答案:

答案 0 :(得分:2)

如果您的文件位于不同的工作区,那么就像在p4v中切换工作区以提交文件一样,您需要在使用p4perl API时切换客户端。

使用SetClient()方法切换工作区,例如:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use P4;

    my $p4 = new P4;

    ## Connect with Workspace ws_1
    $p4->SetPort( "perforce:1666" );
    $p4->SetUser( "user" );
    $p4->SetClient( "ws_1" );
    $p4->Connect() or die("Failed to connect to Server ");
    my $syncedFiles_1 = $p4->RunHave("//...");

    ## Switch to Workspace ws_2
    $p4->SetClient( "ws_2" );
    my $syncedFiles_2 = $p4->RunHave("//...");