1# 2# CDDL HEADER START 3# 4# The contents of this file are subject to the terms of the 5# Common Development and Distribution License (the "License"). 6# You may not use this file except in compliance with the License. 7# 8# See LICENSE.txt included in this distribution for the specific 9# language governing permissions and limitations under the License. 10# 11# When distributing Covered Code, include this CDDL HEADER in each 12# file and include the License file at LICENSE.txt. 13# If applicable, add the following below this CDDL HEADER, with the 14# fields enclosed by brackets "[]" replaced with your own identifying 15# information: Portions Copyright [yyyy] [name of copyright owner] 16# 17# CDDL HEADER END 18# 19 20# 21# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. 22# Portions Copyright (c) 2020, Krystof Tulinger <k.tulinger@seznam.cz> 23# 24 25from shutil import which 26 27from .repository import Repository, RepositoryException 28from ..utils.command import Command 29 30 31class MercurialRepository(Repository): 32 def __init__(self, name, logger, path, project, command, env, hooks, timeout): 33 super().__init__(name, logger, path, project, command, env, hooks, timeout) 34 35 self.command = self._repository_command(command, default=lambda: which('hg')) 36 37 if not self.command: 38 raise RepositoryException("Cannot get hg command") 39 40 def get_branch(self): 41 hg_command = [self.command, "branch"] 42 cmd = self.get_command(hg_command, work_dir=self.path, 43 env_vars=self.env, logger=self.logger) 44 cmd.execute() 45 self.logger.info("output of {}:".format(cmd)) 46 self.logger.info(cmd.getoutputstr()) 47 if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED: 48 cmd.log_error("failed to get branch") 49 return None 50 else: 51 if not cmd.getoutput(): 52 self.logger.error("no output from {}". 53 format(hg_command)) 54 return None 55 if len(cmd.getoutput()) == 0: 56 self.logger.error("empty output from {}". 57 format(hg_command)) 58 return None 59 return cmd.getoutput()[0].strip() 60 61 def reposync(self): 62 branch = self.get_branch() 63 if not branch: 64 # Error logged already in get_branch(). 65 return 1 66 67 hg_command = [self.command, "pull"] 68 if branch != "default": 69 hg_command.append("-b") 70 hg_command.append(branch) 71 cmd = self.get_command(hg_command, work_dir=self.path, 72 env_vars=self.env, logger=self.logger) 73 cmd.execute() 74 self.logger.info("output of {}:".format(cmd)) 75 self.logger.info(cmd.getoutputstr()) 76 if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED: 77 cmd.log_error("failed to perform pull") 78 return 1 79 80 hg_command = [self.command, "update"] 81 # Avoid remote branch lookup for default branches since 82 # some servers do not support it. 83 if branch == "default": 84 hg_command.append("--check") 85 cmd = self.get_command(hg_command, work_dir=self.path, 86 env_vars=self.env, logger=self.logger) 87 cmd.execute() 88 self.logger.info("output of {}:".format(cmd)) 89 self.logger.info(cmd.getoutputstr()) 90 if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED: 91 cmd.log_error("failed to perform pull and update") 92 return 1 93 94 return 0 95 96 def incoming_check(self): 97 branch = self.get_branch() 98 if not branch: 99 # Error logged already in get_branch(). 100 raise RepositoryException('cannot get branch for repository {}'. 101 format(self)) 102 103 hg_command = [self.command, 'incoming'] 104 if branch != "default": 105 hg_command.append("-b") 106 hg_command.append(branch) 107 cmd = self.get_command(hg_command, work_dir=self.path, 108 env_vars=self.env, logger=self.logger) 109 cmd.execute() 110 self.logger.info("output of {}:".format(cmd)) 111 self.logger.info(cmd.getoutputstr()) 112 retcode = cmd.getretcode() 113 if cmd.getstate() != Command.FINISHED or retcode not in [0, 1]: 114 cmd.log_error("failed to perform incoming") 115 raise RepositoryException('failed to perform incoming command ' 116 'for repository {}'.format(self)) 117 118 if retcode == 0: 119 return True 120 else: 121 return False 122