summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/utils/git.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/utils/git.py')
-rw-r--r--meta/lib/oeqa/utils/git.py54
1 files changed, 48 insertions, 6 deletions
diff --git a/meta/lib/oeqa/utils/git.py b/meta/lib/oeqa/utils/git.py
index a4c6741f4e..ea35a766eb 100644
--- a/meta/lib/oeqa/utils/git.py
+++ b/meta/lib/oeqa/utils/git.py
@@ -1,9 +1,11 @@
#
# Copyright (C) 2016 Intel Corporation
#
-# Released under the MIT license (see COPYING.MIT)
+# SPDX-License-Identifier: MIT
#
"""Git repository interactions"""
+import os
+
from oeqa.utils.commands import runCmd
@@ -13,9 +15,21 @@ class GitError(Exception):
class GitRepo(object):
"""Class representing a Git repository clone"""
- def __init__(self, cwd):
- self.top_dir = self._run_git_cmd_at(['rev-parse', '--show-toplevel'],
- cwd)
+ def __init__(self, path, is_topdir=False):
+ git_dir = self._run_git_cmd_at(['rev-parse', '--git-dir'], path)
+ git_dir = git_dir if os.path.isabs(git_dir) else os.path.join(path, git_dir)
+ self.git_dir = os.path.realpath(git_dir)
+
+ if self._run_git_cmd_at(['rev-parse', '--is-bare-repository'], path) == 'true':
+ self.bare = True
+ self.top_dir = self.git_dir
+ else:
+ self.bare = 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):
@@ -30,9 +44,37 @@ class GitRepo(object):
cmd_str, ret.status, ret.output))
return ret.output.strip()
- def run_cmd(self, git_args):
+ @staticmethod
+ def init(path, bare=False):
+ """Initialize a new Git repository"""
+ cmd = ['init']
+ if bare:
+ cmd.append('--bare')
+ GitRepo._run_git_cmd_at(cmd, cwd=path)
+ return GitRepo(path, is_topdir=True)
+
+ def run_cmd(self, git_args, env_update=None):
"""Run Git command"""
- return self._run_git_cmd_at(git_args, self.top_dir)
+ 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', '--verify', 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