summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/utils/git.py
blob: ae85d276637fd61920dc9ecc34f8939dc9a89d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#
# Copyright (C) 2016 Intel Corporation
#
# Released under the MIT license (see COPYING.MIT)
#
"""Git repository interactions"""
import os

from oeqa.utils.commands import runCmd


class GitError(Exception):
    """Git error handling"""
    pass

class GitRepo(object):
    """Class representing a Git repository clone"""
    def __init__(self, path, is_topdir=False):
        self.top_dir = self._run_git_cmd_at(['rev-parse', '--show-toplevel'],
                                            path)
        realpath = os.path.realpath(path)
        if is_topdir and realpath != self.top_dir:
            raise GitError("{} is not a Git top directory".format(realpath))

    @staticmethod
    def _run_git_cmd_at(git_args, cwd, **kwargs):
        """Run git command at a specified directory"""
        git_cmd = 'git ' if isinstance(git_args, str) else ['git']
        git_cmd += git_args
        ret = runCmd(git_cmd, ignore_status=True, cwd=cwd, **kwargs)
        if ret.status:
            cmd_str = git_cmd if isinstance(git_cmd, str) \
                                else ' '.join(git_cmd)
            raise GitError("'{}' failed with exit code {}: {}".format(
                cmd_str, ret.status, ret.output))
        return ret.output.strip()

    @staticmethod
    def init(path):
        """Initialize a new Git repository"""
        GitRepo._run_git_cmd_at('init', cwd=path)
        return GitRepo(path, is_topdir=True)

    def run_cmd(self, git_args, env_update=None):
        """Run Git command"""
        env = None
        if env_update:
            env = os.environ.copy()
            env.update(env_update)
        return self._run_git_cmd_at(git_args, self.top_dir, env=env)

    def rev_parse(self, revision):
        """Do git rev-parse"""
        try:
            return self.run_cmd(['rev-parse', revision])
        except GitError:
            # Revision does not exist
            return None

    def get_current_branch(self):
        """Get current branch"""
        try:
            # Strip 11 chars, i.e. 'refs/heads' from the beginning
            return self.run_cmd(['symbolic-ref', 'HEAD'])[11:]
        except GitError:
            return None