-
Notifications
You must be signed in to change notification settings - Fork 0
/
P4.pm
1106 lines (804 loc) · 29.9 KB
/
P4.pm
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#-------------------------------------------------------------------------------
# Copyright (c) 2001-2008, Perforce Software, Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#-------------------------------------------------------------------------------
=pod
=head1 NAME
P4Perl - Perl interface to the Perforce SCM System.
=head1 SYNOPSIS
use P4;
my $p4 = new P4;
$p4->SetClient( $clientname );
$p4->SetPort ( $p4port );
$p4->Connect() or die( "Failed to connect to Perforce Server" );
my $info = $p4->Run( "info" );
$p4->RunEdit( "file.txt" );
die( "Failed to open file for edit" ) if $p4->ErrorCount();
$p4->Disconnect();
=head1 DESCRIPTION
P4Perl is a Perl interface to the Perforce C++ API and allows Perl
users to run Perforce commands and get the responses from the Perforce
Server in Perl hashes and lists. Many methods also accept hashes as
input, so editing Perforce forms in Perl is simple.
Each P4 object represents a connection to a Perforce Server, and
multiple commands may be executed (serially) over a single connection.
Responses from the server are separated into: output, errors, and
warnings. The output is returned directly by the various 'Run' methods);
the errors and warnings are available by calling C<Errors()> and
C<Warnings()> respectively.
=cut
package P4;
use strict;
require Exporter;
require DynaLoader;
use AutoLoader;
use P4::Spec;
use P4::DepotFile;
use P4::Revision;
use P4::Integration;
use P4::Resolver;
use P4::IterateSpec;
use Scalar::Util qw( tainted );
use vars qw( @ISA @EXPORT @EXPORT_OK $AUTOLOAD );
@ISA = qw(Exporter DynaLoader);
@EXPORT_OK = qw( );
@EXPORT = qw();
#
# Generic error codes, from errornum.h in the Perforce API.
#
$P4::EV_NONE = 0; # misc
# The fault of the user
$P4::EV_USAGE = 0x01; # request not consistent with dox
$P4::EV_UNKNOWN = 0x02; # using unknown entity
$P4::EV_CONTEXT = 0x03; # using entity in wrong context
$P4::EV_ILLEGAL = 0x04; # trying to do something you can't
$P4::EV_NOTYET = 0x05; # something must be corrected first
$P4::EV_PROTECT = 0x06; # protections prevented operation
# No fault at all
$P4::EV_EMPTY = 0x11; # action returned empty results
# not the fault of the user
$P4::EV_FAULT = 0x21; # inexplicable program fault
$P4::EV_CLIENT = 0x22; # client side program errors
$P4::EV_ADMIN = 0x23; # server administrative action required
$P4::EV_CONFIG = 0x24; # client configuration inadequate
$P4::EV_UPGRADE = 0x25; # client or server too old to interact
$P4::EV_COMM = 0x26; # communications error
$P4::EV_TOOBIG = 0x27; # not ever Perforce can handle this much
#
# Error severities: taken from error.h in the Perforce API
#
$P4::E_EMPTY = 0; # nothing yet
$P4::E_INFO = 1; # something good happened
$P4::E_WARN = 2; # something not good happened
$P4::E_FAILED = 3; # user did somthing wrong
$P4::E_FATAL = 4; # system broken -- nothing can continue
bootstrap P4;
# Documentation for methods
=pod
=head1 CLASS METHODS
=over
=item new()
Constructs a new P4 object. For example:
$p4 = new P4;
=item Identify()
Returns a string containing build information including
P4Perl version and Perforce API version. For example:
print P4::Identify();
=back
=head1 CONNECTION MANAGEMENT
=over
=item Connect()
Connects to the server, returning false on failure. For example:
$p4->Connect() or die( "Failed to connect" )
=item Disconnect()
Terminate the connection and clean up. Should be called before exiting.
=item IsConnected()
Returns true if the client has been connected to the Perforce Server,
and that connection has not been dropped by the server.
=item ServerLevel()
Returns an integer specifying the server protocol level. This is not the
same as, but is closely aligned to, the server version. To find out your
server's protocol level run 'p4 -vrpc=5 info' and look for the server2
protocol variable in the output.
=item SetApiLevel( integer )
Specify the API compatibility level to use for this script.
This is useful when you want your script to continue to work on
newer server versions, even if the new server adds tagged output
to previously unsupported commands.
The additional tagged output support can change the server's
output, and confound your scripts. Setting the API level to a
specific value allows you to lock the output to an older
format, thus increasing the compatibility of your script.
B<Must be called before calling P4::Connect()>
For example:
$p4->SetApiLevel( 57 ); # Lock to 2005.1 format
$p4->Connect() or die( "Failed to connect to Perforce" );
...
=back
=head1 CLIENT SETTINGS
=over
=item GetCharset()
Return the name of the current charset in use. Applicable only when
used with Perforce servers running in unicode mode.
=item GetClient()
Returns the current Perforce client name. This may have previously
been set by SetClient(), or may be taken from the environment or
P4CONFIG file if any. If all that fails, it will be your hostname.
=item GetCwd()
Returns the current working directory as your Perforce client sees
it.
=item GetHost()
Returns the client hostname. Defaults to your hostname, but can
be overridden with SetHost()
=item GetMaxLockTime()
Returns the current maxlocktime limit set using SetMaxLockTime(),
or 0 if no limit is in place. Note that the limits set by
the administrator in group specifications are not visible through
this interface.
=item GetMaxResults()
Returns the current maxresults limit set using SetMaxResults(),
or 0 if no limit is in place. Note that the limits set by
the administrator in group specifications are not visible through
this interface.
=item GetMaxScanRows()
Returns the current maxscanrows limit set using SetMaxScanRows(),
or 0 if no limit is in place. Note that the limits set by
the administrator in group specifications are not visible through
this interface.
=item GetPassword()
Returns your Perforce password. Taken from a previous call to
SetPassword(), the environment ( $ENV{P4PASSWD} ), or a
P4CONFIG file.
=item GetPort()
Returns the current address for your Perforce server. Taken from
a previous call to SetPort(), the environment ($ENV{P4PORT}),
or a P4CONFIG file.
=item GetProg()
Get the name of your script. See L</SetProg>, below.
=item GetTicketFile()
Returns the path to the file where the user's login tickets
are stored.
=item GetIgnoreFile()
Returns the specfied path of the ignore file.
=item GetUser()
Returns the Perforce username in use on this client.
=item GetVersion()
Returns the (user-specified) version of your script. See
L<SetVersion()>.
=item IsTagged()
Returns 1 if tagged output is enabled, zero if it is disabled.
=item P4ConfigFile()
Returns the path to the current P4CONFIG file, if any, that is
in effect.
=item SetCharset( $charset )
Specify the character set to use for local files when used with a
Perforce server running in unicode mode.
Do not use UNLESS your Perforce server is in unicode mode.
Must be called before calling P4::Connect().
For example:
$p4->SetCharset( "winansi" );
$p4->SetCharset( "iso8859-1" );
$p4->SetCharset( "utf8" );
...
=item SetClient( $client )
Sets the name of your Perforce client. If you don't call this
method, then the client name will be determined according to the
usual Perforce conventions:
1. Value from file specified by P4CONFIG
2. Value from $ENV{P4CLIENT}
3. Hostname
=item SetCwd( $path )
Sets the current working directory for the client. This should
be called after calling Connect().
=item SetHost( $hostname )
Sets the name of the client host - overriding the actual hostname.
This is equivalent to 'p4 -H <hostname>', and really only useful when
you want to run commands as if you were on another machine. If you
don't know when or why you might want to do that, then don't do it.
=item SetMaxLockTime( $value )
Specifies the maximim number of milliseconds for which locks
may be held during queries. Note that once set, this limit remains
in force. You can remove the restriction by setting it to a value
of 0.
=item SetMaxResults( $value )
Limit the number of results for subsequent commands to the value
specified. Perforce will abort the command if continuing would
produce more than this number of results. Note that once set,
this limit remains in force. You can remove the restriction by
setting it to a value of 0.
=item SetMaxScanRows( $value )
Limit the number of records Perforce will scan when processing
subsequent commands to the value specified. Perforce will abort
the command once this number of records has been scanned. Note
that once set, this limit remains in force. You can remove the
restriction by setting it to a value of 0.
=item SetPassword( $password )
Specify the password to use when authenticating this user against
the Perforce Server - overrides all defaults. Not to be
confused with C<RunPassword()>.
=item SetPort( $port )
Set the port on which your Perforce server is listening. Defaults
to:
1. Value from file specified by P4CONFIG
2. Value from $ENV{P4PORT}
3. perforce:1666
=item SetProg( $program_name )
Set the name of your script. This value is displayed in the server log
on 2004.2 or later servers. Defaults to 'Unnamed P4Perl Script' if
not specified.
=item SetTicketFile( $path )
Set the path to the file in which login tickets are stored. If not
specified, the usual Perforce defaults apply.
=item SetIgnoreFile( $path )
Overrides the P4IGNORE file with the specified path.
=item IsIgnored( $path )
Tests the path to see if the file is ignored.
=item SetUser( $username )
Set the Perforce username to use. Defaults to:
1. Value from file specified by P4CONFIG
2. Value from C<$ENV{P4USER}>
3. OS username
=item SetVersion( $version )
Sets the version string of your program. This can be included
in the Perforce Server's logfile.
=item Tagged( [0|1] )
Enable or disable tagged output. Responses from commands that
support tagged output will be returned in the form of a hashref
rather than plain text. This makes parsing the responses to
Perforce commands much simpler.
By default, tagged output is B<enabled>. Tagged output may be
disabled, or re-enabled at any time.
For example:
$p4->Tagged( 0 ); # Disabled
$p4->Tagged( 1 ); # Enabled
=back
=head1 RUNNING COMMANDS
The main interface through which Perforce commands are run is
the C<Run()> method documented below. The syntax of
this method is quite verbose, so P4Perl provides several
shorthand wrappers, some explicitly, and some implicitly which
usually make code more concise and readable. Most people
will want to use those rather than call C<Run()>
directly.
See the section below on L</"RUN SHORTCUTS">.
=over
=item ErrorCount()
Returns the number of errors encountered during execution of the last
command.
=item Errors()
Returns a list of the error strings received during execution of the
last command.
foreach my $e ( $p4->Errors() )
{
print("ERROR: $e\n");
}
Each item in the list is a string, although a P4::Message object can be
retrieved using $p4->Messages().
=item Run( $cmd, [ $arg, ... ] )
Run a Perforce command returning the results. Since Perforce commands
can partially succeed and partially fail, you should check for errors
using C<ErrorCount()>, and warnings using C<WarningCount()>.
Run() returns a list in array context, and an array ref in scalar
context.
$results = $p4->Run( "files", "//depot/...@1" );
@results = $p4->Run( "changes", "-m1", "//...#have" );
Whether you get an array of strings, or an array of hashrefs
depends on whether the server supports tagged output for the
specific command; though modern servers support tagged output
for almost all commands.
You can disable tagged output, and thus get all your results
as strings, by calling C<< $p4->Tagged( 0 ); >> at any time.
=over
=head2 TIP
When developing, if you want to know what kind of output your
server will supply to any command, run it with '-ztag' on the
command line. For example:
p4 -ztag describe -s 4321
If the output resembles the format used by 'p4 fstat', then
it's tagged output.
=back
=item RunFilelog( $args ... )
Runs a C<p4 filelog> with the supplied arguments, and returns
the results as an array of P4::DepotFile objects. Tagged
output for C<p4 filelog> is not easy to read, nor is it easy
to handle programmatically, so this wrapper converts it into
an array of objects to make it easier to work with. For
example:
foreach $file ( $p4->RunFilelog( "//depot/path/..." ) )
{
printf( "%s\n", $file->DepotFile() );
foreach $rev ( $file->Revisions() )
{
printf( "... #%d %s by %s@%s %s\n",
$rev->Rev(),
$rev->Action(),
$rev->User(),
$rev->Client(),
$rev->Desc() );
foreach $integ ( $rev->Integrations() )
{
printf( "... ... %s %s#%d,%d\n",
$integ->How(),
$integ->File(),
$integ->SRev() + 1,
$integ->ERev()
);
}
}
}
=item RunPassword( $oldpass, $newpass )
Run a C<p4 password> command to change the user's password from
$oldpass to $newpass. Not to be confused with C<SetPassword()>
which specifies the password to be used to authenticate.
=item RunResolve( [$resolver] [, $arg...] )
Run a C<p4 resolve> command. Interactive resolves require the
$resolver parameter to be an object of a class derived from
P4::Resolver. In these cases, the C<Resolve()> method of this
class will be called to handle the resolve. For example:
$resolver = new MyResolver;
$p4->RunResolve( $resolver );
In non-interactive resolves, no C<P4::Resolver> object is
required. For example:
$p4->RunResolve( '-at' );
=item RunSubmit( [ $spec | $arg ] ... )
Submits a changelist. If a hashref is passed as one of the
arguments, it is taken to be the change specification form
and is passed to the server as input to a C<p4 submit -i>.
If no change spec is supplied, then the submit is executed
as it stands.
For example:
$change = $p4->FetchChange();
$change->{ "Description" } = "some text...";
$p4->RunSubmit( $change );
Or with a 2006.2, or later, server:
$p4->RunSubmit( "-d", "Some description" );
=item SetInput( $arg )
Save the supplied argument as input to be supplied to a subsequent
command. The input may be: a hashref, a scalar string or an array
of hashrefs or scalar strings. Note that if you pass an array the
array will be shifted once each time the Perforce command in
question asks for user input. A good example of this is
'p4 password' which prompts once for the old password, and then
twice for the new password.
Most people won't need to call this method as the wrappers around
C<Run()> take care of this for you.
=item WarningCount()
Returns the number of warnings issued by the last command.
$p4->WarningCount();
=item Warnings()
Returns a list of warning strings from the last command.
foreach my $w ( $p4->Warnings() )
{
print("WARN: $w\n");
}
Each item in the list is a string, although a P4::Message object can be
retrieved using $p4->Messages().
=back
=head1 RUN SHORTCUTS
For convenience, and legibility of code, this module makes use
of Perl's AutoLoader to implement several wrappers around
the C<Run()> method. These include:
=over
=item The Delete*() Methods
Explained in the section entitled L</"WORKING WITH PERFORCE FORMS">
=item The Fetch*() Methods
Explained in the section entitled L</"WORKING WITH PERFORCE FORMS">
=item The Format*() Methods
Explained in the section entitled L</"CONVERTING FORMS BETWEEN FORMATS">
=item The Parse*() Methods
Explained in the section entitled L</"CONVERTING FORMS BETWEEN FORMATS">
=item The Run*() Methods
Any method whose name starts with the prefix 'Run' is interpreted as
an instruction to run a Perforce command. The command to run is
taken from the suffix (after the 'Run'), so:
$p4->RunInfo();
is equivalent to:
$p4->Run( "info" );
but is more succinct, and easier to read. This technique can be used
to run B<any> Perforce command.
=item The Save*() Methods
Explained in the section entitled L</"WORKING WITH PERFORCE FORMS">
=head1 WORKING WITH PERFORCE FORMS
Perforce forms are collectively known as 'specs'. It's common for
scripts to want to manipulate these forms, and P4Perl has a range
of features to help you to do this.
Most of the time, P4Perl converts the Perforce forms into Perl
hashes to make accessing form fields by name easy. You can
update the hashes and pass them back to P4Perl to update the
forms in the Perforce database.
Most of the form manipulation methods interact with the Perforce
Server, but there are two that do not. They are there to support
scripts that need to convert forms from strings to hashes and
vice-versa (commonly when working with spec depot files). These
methods are C<ParseSpec()> and C<FormatSpec()>.
One could use C<< $p4->Run( "client", "-o" ) >> to fetch a
client specification from Perforce, but P4Perl provides a
shorthand to simplify script code. Similarly, to update
a Perforce client, one could use:
$p4->SetInput( $hash );
$p4->Run( "client", "-i" );
However, since this is a common requirement, P4Perl provides
a shorthand for this too. The C<Fetch*()> and C<Save*()>
methods are explained in more detail in the following
sections.
=over
=item Fetch<type>( [ $arg, ... ] )
This is a shorthand for running C<< $p4-E<gt>Run( <type>, "-o" ) >>
and returning the first element of the results. The intent
is to simplify and declutter code, making it easier to read
and write. The following examples show how fetching some
common specification types can be written simply in a script:
$label = $p4->FetchLabel( $labelname );
$change = $p4->FetchChange( $changeno );
$clientspec = $p4->FetchClient( $clientname );
=item Save<type>( [ $arg, ... ] )
Saves an object of the specified type (passed as either a
string, or a hashref) into the Perforce database. In fact,
this method is just a convenient shorthand for:
$p4->SetInput( $spec );
$p4->Run( "cmd", "-i");
For example:
$p4->SaveLabel( $label );
$p4->SaveChange( $changeno );
$p4->SaveClient( $clientspec );
=item Delete<type>( [ $flag, ... ], $name )
Deletes the named object from the Perforce repository. The
name of the object is mandatory, and may be preceeded by
zero or more flags to be passed to the Perforce command
line. For example:
$p4->DeleteClient( "foo" ); # runs 'p4 client -d foo'
$p4->DeleteChange( "-f", 1234 ); # runs 'p4 change -d -f 1234'
=back
=head3 CONVERTING FORMS BETWEEN FORMATS
Sometimes, we have a form in a hash format, and we want it in
a string; sometimes we have a string, and want a hash. In
those situations, the following methods will help.
=over
=item FormatSpec( $type, $hash )
Converts a Perforce form of the specified type (client/label etc.)
held in the supplied hash into its string representation. Note that
shortcut methods are available that obviate the need to supply the
type argument. The following two examples are equivalent:
$string = $p4->FormatSpec( "client", $hash );
$string = $p4->FormatClient( $hash );
See below for more information on the abbreviated form.
=item Format<type>( $hash )
Shorthand for C<< $p4->FormatSpec( <type>, $hash ) >>.
For example:
$change = $p4->FetchChange();
$change->{ 'Description' } = 'Some description';
$form = $p4->FormatChange( $change );
printf( "Submitting this change:\n\n%s\n", $form );
$p4->RunSubmit( $change );
=item ParseSpec( $type, $string )
Converts a Perforce form of the specified type (client/label etc.)
in the supplied string into a hash and returns a reference to
that hash. Note that shortcut methods are available to avoid the
need to supply the type argument. The following two examples are
equivalent:
$hash = $p4->ParseSpec( "client", $string );
$hash = $p4->ParseClient( $clientspec );
See below for more information on the abbreviated form.
=item Parse<type>( $string )
Shorthand for C<< $p4->ParseSpec( <type>, $string ) >>. For
example:
$hash = $p4->ParseClient( $string );
$hash = $p4->ParseLabel( $string );
$hash = $p4->ParseBranch( $string );
$hash = $p4->ParseProtect( $string );
=back
=head1 DVCS Support
=over
=item Init
The factory method Init takes a hash of paramiters and initilises
an empty DVCS server, then returns a P4 object. The new DVCS server
will be created in the specified "directory".
For example, to create a DVCS instance in a local directory 'dvcs':
my %init;
$init{"port"} = "perforce:1666";
$init{"user"} = "paul";
$init{"client"} = "paul_ws";
$init{"directory"} = "dvcs";
$init{"casesensitive"} = 1;
$init{"unicode"} = 0;
my $dvcs = P4->Init(\%init);
The returned P4 object $dvcs, can be used for all the normal Perforce
perations relavent to a DVCS instance, e.g.
my $status = $dvcs->RunStatus();
=item Clone
The factory method Clone takes a full copy of the history (a clone)
from the specified Perforce Server and populates a new DVCS instance.
For example, clone all of //depot/projA/... from a perforce:1666 server:
my %clone;
$clone{"port"} = "perforce:1666";
$clone{"user"} = "paul";
$clone{"directory"} = "dvcs";
$clone{"file"} = "//depot/projA/...";
my $dvcs = P4->Clone(\%clone);
If a Progress class is provided in the hash key "progress", then the
progress callbacks are fired during the clone operation.
package MyProgress;
{
use base qw( P4::Progress );
...
}
1;
package main;
my %clone;
...
$clone{"progress"} = new MyProgress;
my $dvcs = P4->Clone(\%clone);
See L<P4::Progress> for details.
=back
=head1 DEBUGGING
=over
=item Debug( [ level ] )
Gets and optionally sets the debug level. Without an argument, it
just returns the current debug level. With an argument, it first updates
the debug level and then returns the new value. For example:
$p4->Debug( 1 );
$client->Debug( 0 );
print( "Debug level = ", $client->Debug(), "\n" );
=back
=head1 COMPATIBILITY WITH PREVIOUS VERSIONS
This version of P4Perl is based on P4Perl from the Perforce Public
Depot, but many method names and behaviours have been changed in order
to be consistent with other Perforce Scripting interfaces. Consequently,
some effort will be required to port legacy P4Perl scripts to the
new interface.
The differences are documented in the P4Perl release notes in the file
'RELNOTES', included in the source distribution.
=head1 SEE ALSO
L<perl>, L<P4::DepotFile>, L<P4::Revision>, L<P4::Integration>,
L<P4::Resolver>, L<P4::MergeData>, L<P4::Message>, L<P4::Progress>
=head1 COPYRIGHT
Copyright (c) 2001-2008 Perforce Software. All rights reserved.
=cut
#
# Execute a command. The return value depends on the context of the call.
#
# Returns a list of results
#
sub Run {
my $self = shift;
# Check for tainted data if in taint mode
foreach my $arg (@_) {
if ( tainted($arg) ) {
die("Can't pass tainted arguments to Perforce commands!");
}
}
return $self->_Run(@_);
}
# Change the current working directory. Returns undef on failure.
sub SetCwd( $ ) {
my $self = shift;
my $cwd = shift;
# First we chdir to the dir if it exists. If successful, then we
# update the PWD environment variable (if defined) and call the
# API equivalent function, now named _SetCwd()
return undef unless chdir($cwd);
$ENV{"PWD"} = $cwd if ( defined( $ENV{"PWD"} ) );
$self->_SetCwd($cwd);
return $cwd;
}
#
# Run 'p4 login' using the password supplied by the user
#
sub RunLogin {
my $self = shift;
$self->SetInput( $self->GetPassword() );
return $self->Run( "login", @_ );
}
#
# Run 'p4 passwd' to change the password
#
sub RunPassword {
my $self = shift;
my $oldpass = shift;
my $newpass = shift;
my $args = [ $oldpass, $newpass, $newpass ];
if ( $oldpass eq "" ) {
# No old password, so set new one.
$args = [ $newpass, $newpass ];
}
$self->SetInput($args);
return $self->Run("password");
}
#
# Run 'p4 tickets' to fetch a hash of tickets
#
sub RunTickets {
my $self = shift;
my $path = $self->GetTicketFile();
if ( -e $path ) {
open( FILE, "<", $path ) or die $!;
my @tickets;
while (<FILE>) {
chomp $_;
$_ =~ /([^=]*)=(.*):([^:]*)$/;
my %part = ( 'Host' => $1, 'User' => $2, 'Ticket' => $3 );
push( @tickets, \%part );
}
close(FILE);
return \@tickets;
}
}
#*******************************************************************************
#* Useful shortcut methods to make common actions easier to code. Nothing
#* here that can't be done using the already defined methods.
#*******************************************************************************
# RunSubmit - "p4 submit -i"
#
# Submit a changelist to the server. if one of the supplied arguments is a
# hashref, then it will be assumed to contain the change form ready to be
# sent to the server.
#
# Synopsis: $p4->RunSubmit( args... );
sub RunSubmit( $@ ) {
my $self = shift;
my @args;
my $haveSpec = 0;
foreach my $arg (@_) {
if ( ref($arg) eq "HASH" || ref($arg) eq "P4::Spec" ) {
$self->SetInput(shift);
$haveSpec++;
}
else {
push( @args, $arg );
}
}
unshift( @args, "-i" ) if ($haveSpec);
$self->Run( "submit", @args );
}
#
# RunFileLog() (note capital L). This is a common error in people's scripts,
# so here we'll define it as an alias for RunFilelog, but warn them.
#
sub RunFileLog( $@ ) {
my $self = shift;
warn("Use RunFilelog() instead of RunFileLog()...");
return $self->RunFilelog(@_);
}
#
#
# RunFilelog(): Converts the hard-to-parse output of 'p4 filelog' into
# an array of P4::DepotFile objects which are easier to work with.
#
sub RunFilelog( $@ ) {
my $self = shift;
my @results;
foreach my $r ( $self->Run( "filelog", @_ ) ) {
if ( ref( $r eq "HASH" ) ) {
push( @results, $r );
next;
}
my $df = new P4::DepotFile( $r->{'depotFile'} );
push( @results, $df );
my $rcount = scalar( @{ $r->{'rev'} } );
for ( my $i = 0 ; $i < $rcount ; $i++ ) {
# Create a new revision object
my $rev = $df->NewRevision();
foreach my $key ( keys %$r ) {
next unless ( ref( $r->{$key} ) eq "ARRAY" );
next unless defined( $r->{$key}[$i] );
next if ref( $r->{$key}[$i] );
$rev->SetAttribute( $key, $r->{$key}[$i] );
}