12d57dc69SVladimir Kotal# 22d57dc69SVladimir Kotal# CDDL HEADER START 32d57dc69SVladimir Kotal# 42d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the 52d57dc69SVladimir Kotal# Common Development and Distribution License (the "License"). 62d57dc69SVladimir Kotal# You may not use this file except in compliance with the License. 72d57dc69SVladimir Kotal# 82d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific 92d57dc69SVladimir Kotal# language governing permissions and limitations under the License. 102d57dc69SVladimir Kotal# 112d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each 122d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt. 132d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the 142d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying 152d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner] 162d57dc69SVladimir Kotal# 172d57dc69SVladimir Kotal# CDDL HEADER END 182d57dc69SVladimir Kotal# 192d57dc69SVladimir Kotal 202d57dc69SVladimir Kotal# 21*ae5b3cb8SVladimir Kotal# Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. 222d57dc69SVladimir Kotal# Portions Copyright (c) 2020, Krystof Tulinger <k.tulinger@seznam.cz> 232d57dc69SVladimir Kotal# 242d57dc69SVladimir Kotal 252d57dc69SVladimir Kotalfrom shutil import which 262d57dc69SVladimir Kotal 272d57dc69SVladimir Kotalfrom .repository import Repository, RepositoryException 282d57dc69SVladimir Kotalfrom ..utils.command import Command 292d57dc69SVladimir Kotal 302d57dc69SVladimir Kotal 312d57dc69SVladimir Kotalclass MercurialRepository(Repository): 32*ae5b3cb8SVladimir Kotal def __init__(self, name, logger, path, project, command, env, hooks, timeout): 33*ae5b3cb8SVladimir Kotal super().__init__(name, logger, path, project, command, env, hooks, timeout) 342d57dc69SVladimir Kotal 352d57dc69SVladimir Kotal self.command = self._repository_command(command, default=lambda: which('hg')) 362d57dc69SVladimir Kotal 372d57dc69SVladimir Kotal if not self.command: 382d57dc69SVladimir Kotal raise RepositoryException("Cannot get hg command") 392d57dc69SVladimir Kotal 402d57dc69SVladimir Kotal def get_branch(self): 412d57dc69SVladimir Kotal hg_command = [self.command, "branch"] 42*ae5b3cb8SVladimir Kotal cmd = self.get_command(hg_command, work_dir=self.path, 432d57dc69SVladimir Kotal env_vars=self.env, logger=self.logger) 442d57dc69SVladimir Kotal cmd.execute() 452d57dc69SVladimir Kotal self.logger.info("output of {}:".format(cmd)) 462d57dc69SVladimir Kotal self.logger.info(cmd.getoutputstr()) 472d57dc69SVladimir Kotal if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED: 482d57dc69SVladimir Kotal cmd.log_error("failed to get branch") 492d57dc69SVladimir Kotal return None 502d57dc69SVladimir Kotal else: 512d57dc69SVladimir Kotal if not cmd.getoutput(): 522d57dc69SVladimir Kotal self.logger.error("no output from {}". 532d57dc69SVladimir Kotal format(hg_command)) 542d57dc69SVladimir Kotal return None 552d57dc69SVladimir Kotal if len(cmd.getoutput()) == 0: 562d57dc69SVladimir Kotal self.logger.error("empty output from {}". 572d57dc69SVladimir Kotal format(hg_command)) 582d57dc69SVladimir Kotal return None 592d57dc69SVladimir Kotal return cmd.getoutput()[0].strip() 602d57dc69SVladimir Kotal 612d57dc69SVladimir Kotal def reposync(self): 622d57dc69SVladimir Kotal branch = self.get_branch() 632d57dc69SVladimir Kotal if not branch: 642d57dc69SVladimir Kotal # Error logged already in get_branch(). 652d57dc69SVladimir Kotal return 1 662d57dc69SVladimir Kotal 672d57dc69SVladimir Kotal hg_command = [self.command, "pull"] 682d57dc69SVladimir Kotal if branch != "default": 692d57dc69SVladimir Kotal hg_command.append("-b") 702d57dc69SVladimir Kotal hg_command.append(branch) 71*ae5b3cb8SVladimir Kotal cmd = self.get_command(hg_command, work_dir=self.path, 722d57dc69SVladimir Kotal env_vars=self.env, logger=self.logger) 732d57dc69SVladimir Kotal cmd.execute() 742d57dc69SVladimir Kotal self.logger.info("output of {}:".format(cmd)) 752d57dc69SVladimir Kotal self.logger.info(cmd.getoutputstr()) 762d57dc69SVladimir Kotal if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED: 772d57dc69SVladimir Kotal cmd.log_error("failed to perform pull") 782d57dc69SVladimir Kotal return 1 792d57dc69SVladimir Kotal 802d57dc69SVladimir Kotal hg_command = [self.command, "update"] 812d57dc69SVladimir Kotal # Avoid remote branch lookup for default branches since 822d57dc69SVladimir Kotal # some servers do not support it. 832d57dc69SVladimir Kotal if branch == "default": 842d57dc69SVladimir Kotal hg_command.append("--check") 85*ae5b3cb8SVladimir Kotal cmd = self.get_command(hg_command, work_dir=self.path, 862d57dc69SVladimir Kotal env_vars=self.env, logger=self.logger) 872d57dc69SVladimir Kotal cmd.execute() 882d57dc69SVladimir Kotal self.logger.info("output of {}:".format(cmd)) 892d57dc69SVladimir Kotal self.logger.info(cmd.getoutputstr()) 902d57dc69SVladimir Kotal if cmd.getretcode() != 0 or cmd.getstate() != Command.FINISHED: 912d57dc69SVladimir Kotal cmd.log_error("failed to perform pull and update") 922d57dc69SVladimir Kotal return 1 932d57dc69SVladimir Kotal 942d57dc69SVladimir Kotal return 0 952d57dc69SVladimir Kotal 962d57dc69SVladimir Kotal def incoming_check(self): 972d57dc69SVladimir Kotal branch = self.get_branch() 982d57dc69SVladimir Kotal if not branch: 992d57dc69SVladimir Kotal # Error logged already in get_branch(). 1002d57dc69SVladimir Kotal raise RepositoryException('cannot get branch for repository {}'. 1012d57dc69SVladimir Kotal format(self)) 1022d57dc69SVladimir Kotal 1032d57dc69SVladimir Kotal hg_command = [self.command, 'incoming'] 1042d57dc69SVladimir Kotal if branch != "default": 1052d57dc69SVladimir Kotal hg_command.append("-b") 1062d57dc69SVladimir Kotal hg_command.append(branch) 107*ae5b3cb8SVladimir Kotal cmd = self.get_command(hg_command, work_dir=self.path, 1082d57dc69SVladimir Kotal env_vars=self.env, logger=self.logger) 1092d57dc69SVladimir Kotal cmd.execute() 1102d57dc69SVladimir Kotal self.logger.info("output of {}:".format(cmd)) 1112d57dc69SVladimir Kotal self.logger.info(cmd.getoutputstr()) 1122d57dc69SVladimir Kotal retcode = cmd.getretcode() 1132d57dc69SVladimir Kotal if cmd.getstate() != Command.FINISHED or retcode not in [0, 1]: 1142d57dc69SVladimir Kotal cmd.log_error("failed to perform incoming") 1152d57dc69SVladimir Kotal raise RepositoryException('failed to perform incoming command ' 1162d57dc69SVladimir Kotal 'for repository {}'.format(self)) 1172d57dc69SVladimir Kotal 1182d57dc69SVladimir Kotal if retcode == 0: 1192d57dc69SVladimir Kotal return True 1202d57dc69SVladimir Kotal else: 1212d57dc69SVladimir Kotal return False 122