7
7
# 2007 Trolltech ASA
8
8
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
9
9
#
10
+ # pylint: disable=invalid-name,missing-docstring,too-many-arguments,broad-except
11
+ # pylint: disable=no-self-use,wrong-import-position,consider-iterating-dictionary
12
+ # pylint: disable=wrong-import-order,unused-import,too-few-public-methods
13
+ # pylint: disable=too-many-lines,ungrouped-imports,fixme,too-many-locals
14
+ # pylint: disable=line-too-long,bad-whitespace,superfluous-parens
15
+ # pylint: disable=too-many-statements,too-many-instance-attributes
16
+ # pylint: disable=too-many-branches,too-many-nested-blocks
17
+ #
10
18
import sys
11
19
if sys .hexversion < 0x02040000 :
12
20
# The limiter is the subprocess module
@@ -161,6 +169,9 @@ def calcDiskFree():
161
169
return st .f_bavail * st .f_frsize
162
170
163
171
def die (msg ):
172
+ """ Terminate execution. Make sure that any running child processes have been wait()ed for before
173
+ calling this.
174
+ """
164
175
if verbose :
165
176
raise Exception (msg )
166
177
else :
@@ -618,6 +629,14 @@ def __init__(self, exit_code, p4_result, limit):
618
629
super (P4RequestSizeException , self ).__init__ (exit_code , p4_result )
619
630
self .limit = limit
620
631
632
+ class P4CommandException (P4Exception ):
633
+ """ Something went wrong calling p4 which means we have to give up """
634
+ def __init__ (self , msg ):
635
+ self .msg = msg
636
+
637
+ def __str__ (self ):
638
+ return self .msg
639
+
621
640
def isModeExecChanged (src_mode , dst_mode ):
622
641
return isModeExec (src_mode ) != isModeExec (dst_mode )
623
642
@@ -3539,6 +3558,74 @@ def importHeadRevision(self, revision):
3539
3558
print ("IO error details: {}" .format (err ))
3540
3559
print (self .gitError .read ())
3541
3560
3561
+
3562
+ def importRevisions (self , args , branch_arg_given ):
3563
+ changes = []
3564
+
3565
+ if len (self .changesFile ) > 0 :
3566
+ with open (self .changesFile ) as f :
3567
+ output = f .readlines ()
3568
+ changeSet = set ()
3569
+ for line in output :
3570
+ changeSet .add (int (line ))
3571
+
3572
+ for change in changeSet :
3573
+ changes .append (change )
3574
+
3575
+ changes .sort ()
3576
+ else :
3577
+ # catch "git p4 sync" with no new branches, in a repo that
3578
+ # does not have any existing p4 branches
3579
+ if len (args ) == 0 :
3580
+ if not self .p4BranchesInGit :
3581
+ raise P4CommandException ("No remote p4 branches. Perhaps you never did \" git p4 clone\" in here." )
3582
+
3583
+ # The default branch is master, unless --branch is used to
3584
+ # specify something else. Make sure it exists, or complain
3585
+ # nicely about how to use --branch.
3586
+ if not self .detectBranches :
3587
+ if not branch_exists (self .branch ):
3588
+ if branch_arg_given :
3589
+ raise P4CommandException ("Error: branch %s does not exist." % self .branch )
3590
+ else :
3591
+ raise P4CommandException ("Error: no branch %s; perhaps specify one with --branch." %
3592
+ self .branch )
3593
+
3594
+ if self .verbose :
3595
+ print ("Getting p4 changes for %s...%s" % (', ' .join (self .depotPaths ),
3596
+ self .changeRange ))
3597
+ changes = p4ChangesForPaths (self .depotPaths , self .changeRange , self .changes_block_size )
3598
+
3599
+ if len (self .maxChanges ) > 0 :
3600
+ changes = changes [:min (int (self .maxChanges ), len (changes ))]
3601
+
3602
+ if len (changes ) == 0 :
3603
+ if not self .silent :
3604
+ print ("No changes to import!" )
3605
+ else :
3606
+ if not self .silent and not self .detectBranches :
3607
+ print ("Import destination: %s" % self .branch )
3608
+
3609
+ self .updatedBranches = set ()
3610
+
3611
+ if not self .detectBranches :
3612
+ if args :
3613
+ # start a new branch
3614
+ self .initialParent = ""
3615
+ else :
3616
+ # build on a previous revision
3617
+ self .initialParent = parseRevision (self .branch )
3618
+
3619
+ self .importChanges (changes )
3620
+
3621
+ if not self .silent :
3622
+ print ("" )
3623
+ if len (self .updatedBranches ) > 0 :
3624
+ sys .stdout .write ("Updated branches: " )
3625
+ for b in self .updatedBranches :
3626
+ sys .stdout .write ("%s " % b )
3627
+ sys .stdout .write ("\n " )
3628
+
3542
3629
def openStreams (self ):
3543
3630
self .importProcess = subprocess .Popen (["git" , "fast-import" ],
3544
3631
stdin = subprocess .PIPE ,
@@ -3549,11 +3636,14 @@ def openStreams(self):
3549
3636
self .gitError = self .importProcess .stderr
3550
3637
3551
3638
def closeStreams (self ):
3639
+ if self .gitStream is None :
3640
+ return
3552
3641
self .gitStream .close ()
3553
3642
if self .importProcess .wait () != 0 :
3554
3643
die ("fast-import failed: %s" % self .gitError .read ())
3555
3644
self .gitOutput .close ()
3556
3645
self .gitError .close ()
3646
+ self .gitStream = None
3557
3647
3558
3648
def run (self , args ):
3559
3649
if self .importIntoRemotes :
@@ -3737,87 +3827,36 @@ def run(self, args):
3737
3827
b = b [len (self .projectName ):]
3738
3828
self .createdBranches .add (b )
3739
3829
3740
- self .openStreams ()
3741
-
3742
- if revision :
3743
- self .importHeadRevision (revision )
3744
- else :
3745
- changes = []
3746
-
3747
- if len (self .changesFile ) > 0 :
3748
- output = open (self .changesFile ).readlines ()
3749
- changeSet = set ()
3750
- for line in output :
3751
- changeSet .add (int (line ))
3752
-
3753
- for change in changeSet :
3754
- changes .append (change )
3755
-
3756
- changes .sort ()
3757
- else :
3758
- # catch "git p4 sync" with no new branches, in a repo that
3759
- # does not have any existing p4 branches
3760
- if len (args ) == 0 :
3761
- if not self .p4BranchesInGit :
3762
- die ("No remote p4 branches. Perhaps you never did \" git p4 clone\" in here." )
3763
-
3764
- # The default branch is master, unless --branch is used to
3765
- # specify something else. Make sure it exists, or complain
3766
- # nicely about how to use --branch.
3767
- if not self .detectBranches :
3768
- if not branch_exists (self .branch ):
3769
- if branch_arg_given :
3770
- die ("Error: branch %s does not exist." % self .branch )
3771
- else :
3772
- die ("Error: no branch %s; perhaps specify one with --branch." %
3773
- self .branch )
3830
+ p4_check_access ()
3774
3831
3775
- if self .verbose :
3776
- print ("Getting p4 changes for %s...%s" % (', ' .join (self .depotPaths ),
3777
- self .changeRange ))
3778
- changes = p4ChangesForPaths (self .depotPaths , self .changeRange , self .changes_block_size )
3832
+ self .openStreams ()
3779
3833
3780
- if len (self .maxChanges ) > 0 :
3781
- changes = changes [:min (int (self .maxChanges ), len (changes ))]
3834
+ err = None
3782
3835
3783
- if len ( changes ) == 0 :
3784
- if not self . silent :
3785
- print ( "No changes to import!" )
3836
+ try :
3837
+ if revision :
3838
+ self . importHeadRevision ( revision )
3786
3839
else :
3787
- if not self .silent and not self .detectBranches :
3788
- print ("Import destination: %s" % self .branch )
3840
+ self .importRevisions (args , branch_arg_given )
3789
3841
3790
- self .updatedBranches = set ()
3842
+ if gitConfigBool ("git-p4.importLabels" ):
3843
+ self .importLabels = True
3791
3844
3792
- if not self .detectBranches :
3793
- if args :
3794
- # start a new branch
3795
- self .initialParent = ""
3796
- else :
3797
- # build on a previous revision
3798
- self .initialParent = parseRevision (self .branch )
3845
+ if self .importLabels :
3846
+ p4Labels = getP4Labels (self .depotPaths )
3847
+ gitTags = getGitTags ()
3799
3848
3800
- self .importChanges (changes )
3849
+ missingP4Labels = p4Labels - gitTags
3850
+ self .importP4Labels (self .gitStream , missingP4Labels )
3801
3851
3802
- if not self .silent :
3803
- print ("" )
3804
- if len (self .updatedBranches ) > 0 :
3805
- sys .stdout .write ("Updated branches: " )
3806
- for b in self .updatedBranches :
3807
- sys .stdout .write ("%s " % b )
3808
- sys .stdout .write ("\n " )
3809
-
3810
- if gitConfigBool ("git-p4.importLabels" ):
3811
- self .importLabels = True
3812
-
3813
- if self .importLabels :
3814
- p4Labels = getP4Labels (self .depotPaths )
3815
- gitTags = getGitTags ()
3852
+ except P4CommandException as e :
3853
+ err = e
3816
3854
3817
- missingP4Labels = p4Labels - gitTags
3818
- self .importP4Labels ( self . gitStream , missingP4Labels )
3855
+ finally :
3856
+ self .closeStreams ( )
3819
3857
3820
- self .closeStreams ()
3858
+ if err :
3859
+ die (str (err ))
3821
3860
3822
3861
# Cleanup temporary branches created during import
3823
3862
if self .tempBranches != []:
0 commit comments