Exemple #1
0
def git_root():
    """Return the root directory of the current ``git`` checkout.

    Returns:
        str: Filesystem path to ``git`` checkout root.
    """
    return _utils.check_output('git', 'rev-parse', '--show-toplevel')
Exemple #2
0
def _push_build_base(slug):
    """Get the diffbase for a Travis "push" build.

    Args:
        slug (str): The GitHub repo slug for the current build.
            Of the form ``{organization}/{repository}``.

    Returns:
        str: The commit SHA of the diff base.
    """
    start, finish = _get_commit_range()
    # Resolve the start object name into a 40-char SHA1 hash.
    start_full = _utils.check_output('git',
                                     'rev-parse',
                                     start,
                                     ignore_err=True)

    if start_full is None:
        # In this case, the start commit isn't in history so we
        # need to use the GitHub API.
        return _get_merge_base_from_github(slug, start, finish)
    else:
        # In this case, the start commit is in history so we
        # expect it to also be the merge base of the start and finish
        # commits.
        _verify_merge_base(start_full, finish)
        return start_full
    def _maybe_skip_if_no_commit(self, blob_name):
        from ci_diff_helper import _utils

        commit_file = _utils.check_output('git',
                                          'cat-file',
                                          '-t',
                                          blob_name,
                                          ignore_err=True)
        if commit_file != 'commit':  # pragma: NO COVER
            self.skipTest('Commit {!r} does not exist'.format(blob_name))
Exemple #4
0
def commit_subject(revision='HEAD'):
    """Gets the subject of a ``git`` commit.

    Args:
        revision (Optional[str]): A ``git`` revision, any of a branch
            name, tag, a commit SHA or a special reference.

    Returns:
        str: The commit subject.
    """
    return _utils.check_output(
        'git', 'log', '--pretty=%s', '-1', revision)
Exemple #5
0
def _verify_merge_base(start, finish):
    """Verifies that the merge base of a commit range **is** the start.

    Args:
        start (str): The start commit in a range.
        finish (str): The last commit in a range.

    Raises:
        ValueError: If the merge base is not the start commit.
    """
    merge_base = _utils.check_output('git',
                                     'merge-base',
                                     start,
                                     finish,
                                     ignore_err=True)
    if merge_base != start:
        raise ValueError('git merge base is not the start commit in range',
                         merge_base, start, finish)
Exemple #6
0
def get_changed_files(blob_name1, blob_name2):
    """Gets a list of changed files between two ``git`` revisions.

    A ``git`` object reference can be any of a branch name, tag,
    a commit SHA or a special reference.

    Args:
        blob_name1 (str): A ``git`` object reference.
        blob_name2 (str): A ``git`` object reference.

    Returns:
        list: List of all filenames changed.
    """
    cmd_output = _utils.check_output(
        'git', 'diff', '--name-only', blob_name1, blob_name2)

    if cmd_output:
        return cmd_output.split('\n')
    else:
        return []
Exemple #7
0
def get_checked_in_files():
    """Gets a list of files in the current ``git`` repository.

    Effectively runs:

    .. code-block:: bash

      $ git ls-files ${GIT_ROOT}

    and then finds the absolute path for each file returned.

    Returns:
        list: List of all filenames checked into the repository.
    """
    root_dir = git_root()
    cmd_output = _utils.check_output('git', 'ls-files', root_dir)

    result = []
    for filename in cmd_output.split('\n'):
        result.append(os.path.abspath(filename))

    return result
Exemple #8
0
def merge_commit(revision='HEAD'):
    """Checks if a ``git`` revision is a merge commit.

    Args:
        revision (Optional[str]):  A ``git`` revision, any of a branch
            name, tag, a commit SHA or a special reference.

    Returns:
        bool: Flag indicating if the given revision.

    Raises:
        NotImplementedError: if the number of parents is not 1 or 2.
    """
    parents = _utils.check_output(
        'git', 'log', '--pretty=%P', '-1', revision)
    num_parents = len(parents.split())
    if num_parents == 1:
        return False
    elif num_parents == 2:
        return True
    else:
        raise NotImplementedError(
            'Unexpected number of parent commits', parents)
Exemple #9
0
 def _call_function_under_test(*args, **kwargs):
     from ci_diff_helper._utils import check_output
     return check_output(*args, **kwargs)