Beispiel #1
0
def _get_git_branch(q):
    try:
        cmd = ["git", "rev-parse", "--abbrev-ref", "HEAD"]
        branch = xt.decode_bytes(_run_git_cmd(cmd))
    except (subprocess.CalledProcessError, OSError, FileNotFoundError):
        q.put(None)
    else:
        q.put(branch.splitlines()[0] if branch else None)
Beispiel #2
0
def _get_git_branch(q):
    try:
        status = subprocess.check_output(['git', 'status'],
                                         stderr=subprocess.DEVNULL)
    except (subprocess.CalledProcessError, OSError):
        q.put(None)
    else:
        info = xt.decode_bytes(status)
        branch = info.splitlines()[0].split()[-1]
        q.put(branch)
Beispiel #3
0
Datei: vc.py Projekt: xxh/xonsh
def _get_git_branch(q):
    denv = builtins.__xonsh__.env.detype()
    try:
        cmd = ["git", "rev-parse", "--abbrev-ref", "HEAD"]
        branch = xt.decode_bytes(
            subprocess.check_output(cmd, env=denv, stderr=subprocess.DEVNULL))
        branch = branch.splitlines()[0] or None
    except (subprocess.CalledProcessError, OSError, FileNotFoundError):
        q.put(None)
    else:
        q.put(branch)
Beispiel #4
0
def _get_git_branch(q):
    # from https://git-blame.blogspot.com/2013/06/checking-current-branch-programatically.html
    for cmds in [
            "git symbolic-ref --short HEAD",
            "git show-ref --head -s --abbrev",  # in detached mode return sha1
    ]:
        with contextlib.suppress(subprocess.CalledProcessError, OSError):
            branch = xt.decode_bytes(_run_git_cmd(cmds.split()))
            if branch:
                q.put(os.path.basename(branch.splitlines()[0]))
                return
    # all failed
    q.put(None)
Beispiel #5
0
def _get_git_branch(q):
    # create a safge detyped env dictionary and update with the additional git environment variables
    # when running git status commands we do not want to acquire locks running command like git status
    denv = dict(builtins.__xonsh__.env.detype())
    denv.update({"GIT_OPTIONAL_LOCKS": "0"})
    try:
        cmd = ["git", "rev-parse", "--abbrev-ref", "HEAD"]
        branch = xt.decode_bytes(
            subprocess.check_output(cmd, env=denv, stderr=subprocess.DEVNULL))
        branch = branch.splitlines()[0] or None
    except (subprocess.CalledProcessError, OSError, FileNotFoundError):
        q.put(None)
    else:
        q.put(branch)
Beispiel #6
0
def _get_git_branch(q):
    try:
        branches = xt.decode_bytes(subprocess.check_output(["git", "branch"], stderr=subprocess.DEVNULL)).splitlines()
    except (subprocess.CalledProcessError, OSError):
        q.put(None)
    else:
        for branch in branches:
            if not branch.startswith("* "):
                continue
            elif branch.endswith(")"):
                branch = branch.split()[-1][:-1]
            else:
                branch = branch.split()[-1]

            q.put(branch)
            break
        else:
            q.put(None)
Beispiel #7
0
def _get_git_branch(q):
    try:
        branches = xt.decode_bytes(
            subprocess.check_output(['git', 'branch'],
                                    stderr=subprocess.DEVNULL)).splitlines()
    except (subprocess.CalledProcessError, OSError):
        q.put(None)
    else:
        for branch in branches:
            if not branch.startswith('* '):
                continue
            elif branch.endswith(')'):
                branch = branch.split()[-1][:-1]
            else:
                branch = branch.split()[-1]

            q.put(branch)
            break
        else:
            q.put(None)
Beispiel #8
0
def get_hg_branch(root=None):
    """Try to get the mercurial branch of the current directory,
    return None if not in a repo or subprocess.TimeoutExpired if timed out.
    """
    env = builtins.__xonsh_env__
    timeout = env['VC_BRANCH_TIMEOUT']
    try:
        root = subprocess.check_output(['hg', 'root'],
                                       timeout=timeout,
                                       stderr=subprocess.DEVNULL)
    except subprocess.TimeoutExpired:
        return subprocess.TimeoutExpired(['hg'], timeout)
    except subprocess.CalledProcessError:
        # not in repo
        return None
    else:
        root = xt.decode_bytes(root).strip()
    if env.get('VC_HG_SHOW_BRANCH'):
        # get branch name
        branch_path = os.path.sep.join([root, '.hg', 'branch'])
        if os.path.exists(branch_path):
            with open(branch_path, 'r') as branch_file:
                branch = branch_file.read()
        else:
            branch = 'default'
    else:
        branch = ''
    # add bookmark, if we can
    bookmark_path = os.path.sep.join([root, '.hg', 'bookmarks.current'])
    if os.path.exists(bookmark_path):
        with open(bookmark_path, 'r') as bookmark_file:
            active_bookmark = bookmark_file.read()
        if env.get('VC_HG_SHOW_BRANCH') is True:
            branch = "{0}, {1}".format(*(b.strip(os.linesep)
                                         for b in (branch, active_bookmark)))
        else:
            branch = active_bookmark.strip(os.linesep)
    else:
        branch = branch.strip(os.linesep)
    return branch
Beispiel #9
0
def get_hg_branch(root=None):
    """Try to get the mercurial branch of the current directory,
    return None if not in a repo or subprocess.TimeoutExpired if timed out.
    """
    env = builtins.__xonsh_env__
    timeout = env['VC_BRANCH_TIMEOUT']
    try:
        root = subprocess.check_output(['hg', 'root'], timeout=timeout,
                                       stderr=subprocess.DEVNULL)
    except subprocess.TimeoutExpired:
        return subprocess.TimeoutExpired(['hg'], timeout)
    except (subprocess.CalledProcessError, FileNotFoundError):
        # not in repo or command not in PATH
        return None
    else:
        root = xt.decode_bytes(root).strip()
    if env.get('VC_HG_SHOW_BRANCH'):
        # get branch name
        branch_path = os.path.sep.join([root, '.hg', 'branch'])
        if os.path.exists(branch_path):
            with open(branch_path, 'r') as branch_file:
                branch = branch_file.read()
        else:
            branch = 'default'
    else:
        branch = ''
    # add bookmark, if we can
    bookmark_path = os.path.sep.join([root, '.hg', 'bookmarks.current'])
    if os.path.exists(bookmark_path):
        with open(bookmark_path, 'r') as bookmark_file:
            active_bookmark = bookmark_file.read()
        if env.get('VC_HG_SHOW_BRANCH') is True:
            branch = "{0}, {1}".format(*(b.strip(os.linesep) for b in
                                         (branch, active_bookmark)))
        else:
            branch = active_bookmark.strip(os.linesep)
    else:
        branch = branch.strip(os.linesep)
    return branch
Beispiel #10
0
def _get_git_branch(q):
    denv = builtins.__xonsh__.env.detype()
    try:
        branches = xt.decode_bytes(
            subprocess.check_output(["git", "branch"],
                                    env=denv,
                                    stderr=subprocess.DEVNULL)).splitlines()
    except (subprocess.CalledProcessError, OSError, FileNotFoundError):
        q.put(None)
    else:
        for branch in branches:
            if not branch.startswith("* "):
                continue
            elif branch.endswith(")"):
                branch = branch.split()[-1][:-1]
            else:
                branch = branch.split()[-1]

            q.put(branch)
            break
        else:
            q.put(None)