2

当我的 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
4

1 回答 1

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("//...");
于 2014-01-16T11:33:06.757 回答