# Path: vcstool/executor.py
# USE_COLOR = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
# 
# Path: vcstool/clients/vcs_base.py
# class VcsClientBase(object):
# 
#     type = None
# 
#     def __init__(self, path):
#         self.path = path
# 
#     def __getattribute__(self, name):
#         if name == 'import':
#             try:
#                 return self.import_
#             except AttributeError:
#                 pass
#         return super(VcsClientBase, self).__getattribute__(name)
# 
#     def _not_applicable(self, command, message=None):
#         return {
#             'cmd': '%s.%s(%s)' % (
#                 self.__class__.type, 'push', command.__class__.command),
#             'output': "Command '%s' not applicable for client '%s'%s" % (
#                 command.__class__.command, self.__class__.type,
#                 ': ' + message if message else ''),
#             'returncode': NotImplemented
#         }
# 
#     def _run_command(self, cmd, env=None, retry=0):
#         for i in range(retry + 1):
#             result = run_command(cmd, os.path.abspath(self.path), env=env)
#             if not result['returncode']:
#                 # return successful result
#                 break
#             if i >= retry:
#                 # return the failure after retries
#                 break
#             # increasing sleep before each retry
#             time.sleep(i + 1)
#         return result
# 
#     def _create_path(self):
#         if not os.path.exists(self.path):
#             try:
#                 os.makedirs(self.path)
#             except os.error as e:
#                 return {
#                     'cmd': 'os.makedirs(%s)' % self.path,
#                     'cwd': self.path,
#                     'output':
#                         "Could not create directory '%s': %s" % (self.path, e),
#                     'returncode': 1
#                 }
#         return None
# 
# Path: vcstool/util.py
# def rmtree(path):
#     kwargs = {}
#     if sys.platform == 'win32':
#         kwargs['onerror'] = _onerror_windows
#     return shutil_rmtree(path, **kwargs)


import os
from shutil import which
from threading import Lock
from vcstool.executor import USE_COLOR
from .vcs_base import VcsClientBase
from ..util import rmtree
            return result_id
        id_ = result_id['output']

        if not command.exact:
            cmd_branch = [HgClient._executable, 'identify', '--branch']
            result_branch = self._run_command(cmd_branch)
            if result_branch['returncode']:
                result_branch['output'] = \
                    'Could not determine branch: ' + result_branch['output']
                return result_branch
            branch = result_branch['output']

            cmd_branch_id = [
                HgClient._executable, 'identify', '-r', branch, '--id']
            result_branch_id = self._run_command(cmd_branch_id)
            if result_branch_id['returncode']:
                result_branch_id['output'] = \
                    'Could not determine branch id: ' + \
                    result_branch_id['output']
                return result_branch_id
            if result_branch_id['output'] == id_:
                id_ = branch
                cmd_branch = cmd_branch_id

        return {
            'cmd': '%s && %s' % (result_url['cmd'], ' '.join(cmd_id)),
            'cwd': self.path,
            'output': '\n'.join([url, id_]),
            'returncode': 0,
            'export_data': {'url': url, 'version': id_}

# Path: vcstool/executor.py
# USE_COLOR = hasattr(sys.stdout, 'isatty') and sys.stdout.isatty()
# 
# Path: vcstool/clients/vcs_base.py
# class VcsClientBase(object):
# 
#     type = None
# 
#     def __init__(self, path):
#         self.path = path
# 
#     def __getattribute__(self, name):
#         if name == 'import':
#             try:
#                 return self.import_
#             except AttributeError:
#                 pass
#         return super(VcsClientBase, self).__getattribute__(name)
# 
#     def _not_applicable(self, command, message=None):
#         return {
#             'cmd': '%s.%s(%s)' % (
#                 self.__class__.type, 'push', command.__class__.command),
#             'output': "Command '%s' not applicable for client '%s'%s" % (
#                 command.__class__.command, self.__class__.type,
#                 ': ' + message if message else ''),
#             'returncode': NotImplemented
#         }
# 
#     def _run_command(self, cmd, env=None, retry=0):
#         for i in range(retry + 1):
#             result = run_command(cmd, os.path.abspath(self.path), env=env)
#             if not result['returncode']:
#                 # return successful result
#                 break
#             if i >= retry:
#                 # return the failure after retries
#                 break
#             # increasing sleep before each retry
#             time.sleep(i + 1)
#         return result
# 
#     def _create_path(self):
#         if not os.path.exists(self.path):
#             try:
#                 os.makedirs(self.path)
#             except os.error as e:
#                 return {
#                     'cmd': 'os.makedirs(%s)' % self.path,
#                     'cwd': self.path,
#                     'output':
#                         "Could not create directory '%s': %s" % (self.path, e),
#                     'returncode': 1
#                 }
#         return None
# 
# Path: vcstool/util.py
# def rmtree(path):
#     kwargs = {}
#     if sys.platform == 'win32':
#         kwargs['onerror'] = _onerror_windows
#     return shutil_rmtree(path, **kwargs)


# Path: vcstool/clients/hg.py
import os
from shutil import which
from threading import Lock
from vcstool.executor import USE_COLOR
from .vcs_base import VcsClientBase
from ..util import rmtree

            return result_id
        id_ = result_id['output']

        if not command.exact:
            cmd_branch = [HgClient._executable, 'identify', '--branch']
            result_branch = self._run_command(cmd_branch)
            if result_branch['returncode']:
                result_branch['output'] = \
                    'Could not determine branch: ' + result_branch['output']
                return result_branch
            branch = result_branch['output']

            cmd_branch_id = [
                HgClient._executable, 'identify', '-r', branch, '--id']
            result_branch_id = self._run_command(cmd_branch_id)
            if result_branch_id['returncode']:
                result_branch_id['output'] = \
                    'Could not determine branch id: ' + \
                    result_branch_id['output']
                return result_branch_id
            if result_branch_id['output'] == id_:
                id_ = branch
                cmd_branch = cmd_branch_id

        return {
            'cmd': '%s && %s' % (result_url['cmd'], ' '.join(cmd_id)),
            'cwd': self.path,
            'output': '\n'.join([url, id_]),
            'returncode': 0,
            'export_data': {'url': url, 'version': id_}

        }