@@ -203,14 +203,16 @@ def p4_has_move_command():
203203 # assume it failed because @... was invalid changelist
204204 return True
205205
206- def system (cmd ):
206+ def system (cmd , ignore_error = False ):
207207 expand = isinstance (cmd ,basestring )
208208 if verbose :
209209 sys .stderr .write ("executing %s\n " % str (cmd ))
210210 retcode = subprocess .call (cmd , shell = expand )
211- if retcode :
211+ if retcode and not ignore_error :
212212 raise CalledProcessError (retcode , cmd )
213213
214+ return retcode
215+
214216def p4_system (cmd ):
215217 """Specifically invoke p4 as the system command. """
216218 real_cmd = p4_build_cmd (cmd )
@@ -553,7 +555,12 @@ def p4Where(depotPath):
553555 return clientPath
554556
555557def currentGitBranch ():
556- return read_pipe ("git name-rev HEAD" ).split (" " )[1 ].strip ()
558+ retcode = system (["git" , "symbolic-ref" , "-q" , "HEAD" ], ignore_error = True )
559+ if retcode != 0 :
560+ # on a detached head
561+ return None
562+ else :
563+ return read_pipe (["git" , "name-rev" , "HEAD" ]).split (" " )[1 ].strip ()
557564
558565def isValidGitDir (path ):
559566 if (os .path .exists (path + "/HEAD" )
@@ -1741,44 +1748,47 @@ def applyCommit(self, id):
17411748 #
17421749 # Let the user edit the change description, then submit it.
17431750 #
1744- if self .edit_template (fileName ):
1745- # read the edited message and submit
1746- ret = True
1747- tmpFile = open (fileName , "rb" )
1748- message = tmpFile .read ()
1749- tmpFile .close ()
1750- if self .isWindows :
1751- message = message .replace ("\r \n " , "\n " )
1752- submitTemplate = message [:message .index (separatorLine )]
1753- p4_write_pipe (['submit' , '-i' ], submitTemplate )
1754-
1755- if self .preserveUser :
1756- if p4User :
1757- # Get last changelist number. Cannot easily get it from
1758- # the submit command output as the output is
1759- # unmarshalled.
1760- changelist = self .lastP4Changelist ()
1761- self .modifyChangelistUser (changelist , p4User )
1762-
1763- # The rename/copy happened by applying a patch that created a
1764- # new file. This leaves it writable, which confuses p4.
1765- for f in pureRenameCopy :
1766- p4_sync (f , "-f" )
1751+ submitted = False
17671752
1768- else :
1753+ try :
1754+ if self .edit_template (fileName ):
1755+ # read the edited message and submit
1756+ tmpFile = open (fileName , "rb" )
1757+ message = tmpFile .read ()
1758+ tmpFile .close ()
1759+ if self .isWindows :
1760+ message = message .replace ("\r \n " , "\n " )
1761+ submitTemplate = message [:message .index (separatorLine )]
1762+ p4_write_pipe (['submit' , '-i' ], submitTemplate )
1763+
1764+ if self .preserveUser :
1765+ if p4User :
1766+ # Get last changelist number. Cannot easily get it from
1767+ # the submit command output as the output is
1768+ # unmarshalled.
1769+ changelist = self .lastP4Changelist ()
1770+ self .modifyChangelistUser (changelist , p4User )
1771+
1772+ # The rename/copy happened by applying a patch that created a
1773+ # new file. This leaves it writable, which confuses p4.
1774+ for f in pureRenameCopy :
1775+ p4_sync (f , "-f" )
1776+ submitted = True
1777+
1778+ finally :
17691779 # skip this patch
1770- ret = False
1771- print "Submission cancelled, undoing p4 changes."
1772- for f in editedFiles :
1773- p4_revert (f )
1774- for f in filesToAdd :
1775- p4_revert (f )
1776- os .remove (f )
1777- for f in filesToDelete :
1778- p4_revert (f )
1780+ if not submitted :
1781+ print "Submission cancelled, undoing p4 changes."
1782+ for f in editedFiles :
1783+ p4_revert (f )
1784+ for f in filesToAdd :
1785+ p4_revert (f )
1786+ os .remove (f )
1787+ for f in filesToDelete :
1788+ p4_revert (f )
17791789
17801790 os .remove (fileName )
1781- return ret
1791+ return submitted
17821792
17831793 # Export git tags as p4 labels. Create a p4 label and then tag
17841794 # with that.
@@ -1854,18 +1864,17 @@ def exportGitTags(self, gitTags):
18541864 def run (self , args ):
18551865 if len (args ) == 0 :
18561866 self .master = currentGitBranch ()
1857- if len (self .master ) == 0 or not gitBranchExists ("refs/heads/%s" % self .master ):
1858- die ("Detecting current git branch failed!" )
18591867 elif len (args ) == 1 :
18601868 self .master = args [0 ]
18611869 if not branchExists (self .master ):
18621870 die ("Branch %s does not exist" % self .master )
18631871 else :
18641872 return False
18651873
1866- allowSubmit = gitConfig ("git-p4.allowSubmit" )
1867- if len (allowSubmit ) > 0 and not self .master in allowSubmit .split ("," ):
1868- die ("%s is not in git-p4.allowSubmit" % self .master )
1874+ if self .master :
1875+ allowSubmit = gitConfig ("git-p4.allowSubmit" )
1876+ if len (allowSubmit ) > 0 and not self .master in allowSubmit .split ("," ):
1877+ die ("%s is not in git-p4.allowSubmit" % self .master )
18691878
18701879 [upstream , settings ] = findUpstreamBranchPoint ()
18711880 self .depotPath = settings ['depot-paths' ][0 ]
@@ -1933,7 +1942,12 @@ def run(self, args):
19331942 self .check ()
19341943
19351944 commits = []
1936- for line in read_pipe_lines (["git" , "rev-list" , "--no-merges" , "%s..%s" % (self .origin , self .master )]):
1945+ if self .master :
1946+ commitish = self .master
1947+ else :
1948+ commitish = 'HEAD'
1949+
1950+ for line in read_pipe_lines (["git" , "rev-list" , "--no-merges" , "%s..%s" % (self .origin , commitish )]):
19371951 commits .append (line .strip ())
19381952 commits .reverse ()
19391953
0 commit comments