From cbff4b25e4e6e08637403d76bf32dbbd4d517442 Mon Sep 17 00:00:00 2001 From: Luke Diamand Date: Sat, 21 Nov 2015 09:54:40 +0000 Subject: git-p4: add option to system() to return subshell status Add an optional parameter ignore_error to the git-p4 system() function. If used, it will return the subshell exit status rather than throwing an exception. Signed-off-by: Luke Diamand Signed-off-by: Jeff King --- git-p4.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'git-p4.py') diff --git a/git-p4.py b/git-p4.py index 0093fa3d83..9d55f9c1e9 100755 --- a/git-p4.py +++ b/git-p4.py @@ -192,14 +192,16 @@ def p4_has_move_command(): # assume it failed because @... was invalid changelist return True -def system(cmd): +def system(cmd, ignore_error=False): expand = isinstance(cmd,basestring) if verbose: sys.stderr.write("executing %s\n" % str(cmd)) retcode = subprocess.call(cmd, shell=expand) - if retcode: + if retcode and not ignore_error: raise CalledProcessError(retcode, cmd) + return retcode + def p4_system(cmd): """Specifically invoke p4 as the system command. """ real_cmd = p4_build_cmd(cmd) -- cgit v1.2.3 From 00ad6e3182bbe68a94eaedf322918e4f266d184f Mon Sep 17 00:00:00 2001 From: Luke Diamand Date: Sat, 21 Nov 2015 09:54:41 +0000 Subject: git-p4: work with a detached head When submitting, git-p4 finds the current branch in order to know if it is allowed to submit (configuration "git-p4.allowSubmit"). On a detached head, detecting the branch would fail, and git-p4 would report a cryptic error. This change teaches git-p4 to recognise a detached head and submit successfully. Signed-off-by: Luke Diamand Signed-off-by: Jeff King --- git-p4.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'git-p4.py') diff --git a/git-p4.py b/git-p4.py index 9d55f9c1e9..0cfc8668d6 100755 --- a/git-p4.py +++ b/git-p4.py @@ -544,7 +544,12 @@ def p4Where(depotPath): return clientPath def currentGitBranch(): - return read_pipe("git name-rev HEAD").split(" ")[1].strip() + retcode = system(["git", "symbolic-ref", "-q", "HEAD"], ignore_error=True) + if retcode != 0: + # on a detached head + return None + else: + return read_pipe(["git", "name-rev", "HEAD"]).split(" ")[1].strip() def isValidGitDir(path): if (os.path.exists(path + "/HEAD") @@ -1653,8 +1658,6 @@ class P4Submit(Command, P4UserMap): def run(self, args): if len(args) == 0: self.master = currentGitBranch() - if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master): - die("Detecting current git branch failed!") elif len(args) == 1: self.master = args[0] if not branchExists(self.master): @@ -1662,9 +1665,10 @@ class P4Submit(Command, P4UserMap): else: return False - allowSubmit = gitConfig("git-p4.allowSubmit") - if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","): - die("%s is not in git-p4.allowSubmit" % self.master) + if self.master: + allowSubmit = gitConfig("git-p4.allowSubmit") + if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","): + die("%s is not in git-p4.allowSubmit" % self.master) [upstream, settings] = findUpstreamBranchPoint() self.depotPath = settings['depot-paths'][0] @@ -1732,7 +1736,12 @@ class P4Submit(Command, P4UserMap): self.check() commits = [] - for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, self.master)]): + if self.master: + commitish = self.master + else: + commitish = 'HEAD' + + for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, commitish)]): commits.append(line.strip()) commits.reverse() -- cgit v1.2.3 From b7638fed427f4aab36ad8ff36fe06f0e4b5c98e4 Mon Sep 17 00:00:00 2001 From: GIRARD Etienne Date: Tue, 24 Nov 2015 07:43:59 +0000 Subject: git-p4: clean up after p4 submit failure When "p4 submit" command fails in P4Submit.applyCommit, the workspace is left with the changes. We already have code to revert the changes to the workspace when the user decides to cancel submission by aborting the editor that edits the change description, and we should treat the "p4 submit" failure the same way. Clean the workspace if p4_write_pipe raised SystemExit, so that the user don't have to do it themselves. Signed-off-by: GIRARD Etienne Signed-off-by: Junio C Hamano Signed-off-by: Luke Diamand Signed-off-by: Jeff King --- git-p4.py | 71 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 37 insertions(+), 34 deletions(-) (limited to 'git-p4.py') diff --git a/git-p4.py b/git-p4.py index 0093fa3d83..d53590435a 100755 --- a/git-p4.py +++ b/git-p4.py @@ -1538,44 +1538,47 @@ class P4Submit(Command, P4UserMap): # # Let the user edit the change description, then submit it. # - if self.edit_template(fileName): - # read the edited message and submit - ret = True - tmpFile = open(fileName, "rb") - message = tmpFile.read() - tmpFile.close() - if self.isWindows: - message = message.replace("\r\n", "\n") - submitTemplate = message[:message.index(separatorLine)] - p4_write_pipe(['submit', '-i'], submitTemplate) - - if self.preserveUser: - if p4User: - # Get last changelist number. Cannot easily get it from - # the submit command output as the output is - # unmarshalled. - changelist = self.lastP4Changelist() - self.modifyChangelistUser(changelist, p4User) - - # The rename/copy happened by applying a patch that created a - # new file. This leaves it writable, which confuses p4. - for f in pureRenameCopy: - p4_sync(f, "-f") + submitted = False - else: + try: + if self.edit_template(fileName): + # read the edited message and submit + tmpFile = open(fileName, "rb") + message = tmpFile.read() + tmpFile.close() + if self.isWindows: + message = message.replace("\r\n", "\n") + submitTemplate = message[:message.index(separatorLine)] + p4_write_pipe(['submit', '-i'], submitTemplate) + + if self.preserveUser: + if p4User: + # Get last changelist number. Cannot easily get it from + # the submit command output as the output is + # unmarshalled. + changelist = self.lastP4Changelist() + self.modifyChangelistUser(changelist, p4User) + + # The rename/copy happened by applying a patch that created a + # new file. This leaves it writable, which confuses p4. + for f in pureRenameCopy: + p4_sync(f, "-f") + submitted = True + + finally: # skip this patch - ret = False - print "Submission cancelled, undoing p4 changes." - for f in editedFiles: - p4_revert(f) - for f in filesToAdd: - p4_revert(f) - os.remove(f) - for f in filesToDelete: - p4_revert(f) + if not submitted: + print "Submission cancelled, undoing p4 changes." + for f in editedFiles: + p4_revert(f) + for f in filesToAdd: + p4_revert(f) + os.remove(f) + for f in filesToDelete: + p4_revert(f) os.remove(fileName) - return ret + return submitted # Export git tags as p4 labels. Create a p4 label and then tag # with that. -- cgit v1.2.3