Exemplo n.º 1
0
def run_upstream_source_updater(repo_root: str):
    """Run the upstream source updater at the root of the repository

    :param repo_root: the root of the repository
    """
    with file_utils.workdir(repo_root):
        update_upstream_sources(commit=True)
Exemplo n.º 2
0
def test_workdir_exception(monkeypatch, tmpdir):
    newdir = tmpdir / 'newdir'
    monkeypatch.chdir(tmpdir)
    with pytest.raises(OSError) as excinfo:
        with file_utils.workdir(str(newdir)) as cwd:
            pass  # should failed here
    assert excinfo.value.errno == 2, 'expected errno to be 2 (no such file)'
Exemplo n.º 3
0
def updater_main(
    repo_url: str,
    refspec: str,
    target_branch: str,
    push_map: str,
    updater_func: Callable[[], None],
    logger: Logger,
    execute_commit: bool = False,
):
    """Run the actual logic to update the upstream source and push the changes

    :param repo_url: midstream repository URL
    :param refspec: refspec to fetch
    :param target_branch: branch to push the changes to
    :param push_map: path to pusher push map
    :param updater_func: A callable that is called with the root of the
        repository as the first argument and run the actual update.
    :param logger: logger instance that will be used to log messages
    """
    repo_name = get_name_from_repo_url(repo_url)
    repo_root = os.path.join(os.getcwd(), repo_name)
    logger.info('adding repo url: %s', repo_url)
    _, fetch_sha = prep_git_repo(repo_root, repo_url, refspec, checkout=True)
    with file_utils.workdir(repo_root):
        ret = updater_func()
        if execute_commit:
            commit_files(['.'])
        push_upstream_sources(dst_branch=target_branch,
                              push_map=push_map,
                              if_not_exists=True,
                              unless_hash=fetch_sha)

    return ret
Exemplo n.º 4
0
def commit_files(files,
                 branch=None,
                 commit_msg=None,
                 add_change_id=True,
                 add_headers=None,
                 repo_dir='.',
                 change_id_headers=None):
    """
    executes staging and committing with specified params

    :param list<str> files a list of git command line args
    :param str branch: git branch to be on
    :param str commit_msg: message body(w/o any header) for the commit msg
    :param bool add_change_id: if Change-Id should be appended to commit msg
    :param str repo_dir: dir to stay on when executing git commands
    :param list<str> change_id_headers: a list of change_id that need to be appended
    to commit msg

    raises IOError when files to be committed cannot be accessed
    :rtype: list a list of changed files
:   returns: output or error of the command
    """
    if repo_dir and repo_dir.strip():
        file_utils.workdir(repo_dir.strip())

    if change_id_headers:
        change_id_headers = set(change_id_headers)
    else:
        change_id_headers = set()

    if add_change_id:
        change_id_headers.add('Change-Id')

    if len(git('log', '--oneline').splitlines()):
        git('reset', 'HEAD')

    git('add', *filter(os.path.exists, files))
    changed_files = staged_files()
    if changed_files:
        if branch:
            git('checkout', '-B', branch)
        git(
            'commit', '-m',
            commit_message(changed_files, commit_msg, change_id_headers,
                           add_headers))

    return changed_files
Exemplo n.º 5
0
def test_workdir(monkeypatch, tmpdir):
    newdir = tmpdir / 'newdir'
    newdir.mkdir()
    monkeypatch.chdir(tmpdir)
    with file_utils.workdir(str(newdir)) as cwd:
        assert cwd == str(newdir), 'expected cwd to be newdir'
        assert os.getcwd() == cwd, 'expected to be in newdir'
    assert os.getcwd() == str(tmpdir), 'expected to be in tmpdir'
Exemplo n.º 6
0
def push_changes(repo_root: str,
                 repo_url: str,
                 push_branch: str,
                 unless_hash: str,
                 push_map: str,
                 push_func: Callable = push_upstream_sources):
    """Push the any new commits in the repository

    :param repo_root: the root of the repository
    :param push_branch: the branch to push to
    :param unless_hash: skip push if the commit sha is the same as this
        parameter
    :param push_map: path to the push map
    :param push_func: the function to use to push the changes
    """
    with file_utils.workdir(repo_root):
        push_func(dst_branch=push_branch,
                  push_map=push_map,
                  if_not_exists=True,
                  unless_hash=unless_hash)