mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.0 KiB
Python
119 lines
3.0 KiB
Python
8 years ago
|
"""Wrapper around git command-line tools."""
|
||
5 years ago
|
from __future__ import (absolute_import, division, print_function)
|
||
|
__metaclass__ = type
|
||
8 years ago
|
|
||
5 years ago
|
from . import types as t
|
||
5 years ago
|
|
||
5 years ago
|
from .util import (
|
||
8 years ago
|
SubprocessError,
|
||
5 years ago
|
raw_command,
|
||
8 years ago
|
)
|
||
|
|
||
|
|
||
5 years ago
|
class Git:
|
||
8 years ago
|
"""Wrapper around git command-line tools."""
|
||
5 years ago
|
def __init__(self, root=None): # type: (t.Optional[str]) -> None
|
||
8 years ago
|
self.git = 'git'
|
||
5 years ago
|
self.root = root
|
||
8 years ago
|
|
||
7 years ago
|
def get_diff(self, args, git_options=None):
|
||
8 years ago
|
"""
|
||
|
:type args: list[str]
|
||
6 years ago
|
:type git_options: list[str] | None
|
||
8 years ago
|
:rtype: list[str]
|
||
|
"""
|
||
|
cmd = ['diff'] + args
|
||
7 years ago
|
if git_options is None:
|
||
|
git_options = ['-c', 'core.quotePath=']
|
||
|
return self.run_git_split(git_options + cmd, '\n', str_errors='replace')
|
||
8 years ago
|
|
||
8 years ago
|
def get_diff_names(self, args):
|
||
|
"""
|
||
|
:type args: list[str]
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
cmd = ['diff', '--name-only', '--no-renames', '-z'] + args
|
||
|
return self.run_git_split(cmd, '\0')
|
||
|
|
||
|
def get_file_names(self, args):
|
||
|
"""
|
||
|
:type args: list[str]
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
cmd = ['ls-files', '-z'] + args
|
||
|
return self.run_git_split(cmd, '\0')
|
||
|
|
||
|
def get_branches(self):
|
||
|
"""
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
cmd = ['for-each-ref', 'refs/heads/', '--format', '%(refname:strip=2)']
|
||
|
return self.run_git_split(cmd)
|
||
|
|
||
|
def get_branch(self):
|
||
|
"""
|
||
|
:rtype: str
|
||
|
"""
|
||
|
cmd = ['symbolic-ref', '--short', 'HEAD']
|
||
|
return self.run_git(cmd).strip()
|
||
|
|
||
6 years ago
|
def get_rev_list(self, commits=None, max_count=None):
|
||
|
"""
|
||
|
:type commits: list[str] | None
|
||
|
:type max_count: int | None
|
||
|
:rtype: list[str]
|
||
|
"""
|
||
|
cmd = ['rev-list']
|
||
|
|
||
|
if commits:
|
||
|
cmd += commits
|
||
|
else:
|
||
|
cmd += ['HEAD']
|
||
|
|
||
|
if max_count:
|
||
|
cmd += ['--max-count', '%s' % max_count]
|
||
|
|
||
|
return self.run_git_split(cmd)
|
||
|
|
||
8 years ago
|
def get_branch_fork_point(self, branch):
|
||
|
"""
|
||
|
:type branch: str
|
||
|
:rtype: str
|
||
|
"""
|
||
|
cmd = ['merge-base', '--fork-point', branch]
|
||
|
return self.run_git(cmd).strip()
|
||
|
|
||
8 years ago
|
def is_valid_ref(self, ref):
|
||
|
"""
|
||
|
:type ref: str
|
||
|
:rtype: bool
|
||
|
"""
|
||
|
cmd = ['show', ref]
|
||
|
try:
|
||
7 years ago
|
self.run_git(cmd, str_errors='replace')
|
||
8 years ago
|
return True
|
||
|
except SubprocessError:
|
||
|
return False
|
||
|
|
||
7 years ago
|
def run_git_split(self, cmd, separator=None, str_errors='strict'):
|
||
8 years ago
|
"""
|
||
|
:type cmd: list[str]
|
||
7 years ago
|
:type separator: str | None
|
||
|
:type str_errors: str
|
||
8 years ago
|
:rtype: list[str]
|
||
|
"""
|
||
7 years ago
|
output = self.run_git(cmd, str_errors=str_errors).strip(separator)
|
||
8 years ago
|
|
||
8 years ago
|
if not output:
|
||
8 years ago
|
return []
|
||
|
|
||
|
return output.split(separator)
|
||
|
|
||
7 years ago
|
def run_git(self, cmd, str_errors='strict'):
|
||
8 years ago
|
"""
|
||
|
:type cmd: list[str]
|
||
7 years ago
|
:type str_errors: str
|
||
8 years ago
|
:rtype: str
|
||
|
"""
|
||
5 years ago
|
return raw_command([self.git] + cmd, cwd=self.root, capture=True, str_errors=str_errors)[0]
|