コード例 #1
0
def _repo_ref(tmpdir: str, repo: str, ref: Optional[str]) -> Tuple[str, str]:
    # if `ref` is explicitly passed, use it
    if ref is not None:
        return repo, ref

    ref = git.head_rev(repo)
    # if it exists on disk, we'll try and clone it with the local changes
    if os.path.exists(repo) and git.has_diff("HEAD", repo=repo):
        logger.warning("Creating temporary repo with uncommitted changes...")

        shadow = os.path.join(tmpdir, "shadow-repo")
        cmd_output_b("git", "clone", repo, shadow)
        cmd_output_b("git", "checkout", ref, "-b", "_pc_tmp", cwd=shadow)

        idx = git.git_path("index", repo=shadow)
        objs = git.git_path("objects", repo=shadow)
        env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)

        staged_files = git.get_staged_files(cwd=repo)
        if staged_files:
            xargs(("git", "add", "--"), staged_files, cwd=repo, env=env)

        cmd_output_b("git", "add", "-u", cwd=repo, env=env)
        git.commit(repo=shadow)

        return shadow, git.head_rev(shadow)
    else:
        return repo, ref
コード例 #2
0
ファイル: run.py プロジェクト: QPanDevOps/TST_PY-pre-commit
def _run_hooks(config, hooks, args, environ):
    """Actually run the hooks."""
    skips = _get_skips(environ)
    cols = _compute_cols(hooks, args.verbose)
    filenames = _all_filenames(args)
    filenames = filter_by_include_exclude(filenames, '', config['exclude'])
    classifier = Classifier(filenames)
    retval = 0
    for hook in hooks:
        retval |= _run_single_hook(classifier, hook, args, skips, cols)
        if retval and config['fail_fast']:
            break
    if retval and args.show_diff_on_failure and git.has_diff():
        if args.all_files:
            output.write_line(
                'pre-commit hook(s) made changes.\n'
                'If you are seeing this message in CI, '
                'reproduce locally with: `pre-commit run --all-files`.\n'
                'To run `pre-commit` as part of git workflow, use '
                '`pre-commit install`.', )
        output.write_line('All changes made by hooks:')
        # args.color is a boolean.
        # See user_color function in color.py
        subprocess.call((
            'git',
            '--no-pager',
            'diff',
            '--no-ext-diff',
            '--color={}'.format({
                True: 'always',
                False: 'never'
            }[args.color]),
        ))

    return retval
コード例 #3
0
def _repo_ref(tmpdir, repo, ref):
    # if `ref` is explicitly passed, use it
    if ref:
        return repo, ref

    ref = git.head_rev(repo)
    # if it exists on disk, we'll try and clone it with the local changes
    if os.path.exists(repo) and git.has_diff('HEAD', repo=repo):
        logger.warning('Creating temporary repo with uncommitted changes...')

        shadow = os.path.join(tmpdir, 'shadow-repo')
        cmd_output_b('git', 'clone', repo, shadow)
        cmd_output_b('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)

        idx = git.git_path('index', repo=shadow)
        objs = git.git_path('objects', repo=shadow)
        env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)

        staged_files = git.get_staged_files(cwd=repo)
        if staged_files:
            xargs(('git', 'add', '--'), staged_files, cwd=repo, env=env)

        cmd_output_b('git', 'add', '-u', cwd=repo, env=env)
        git.commit(repo=shadow)

        return shadow, git.head_rev(shadow)
    else:
        return repo, ref
コード例 #4
0
ファイル: run.py プロジェクト: pre-commit/pre-commit
def _run_hooks(config, hooks, args, environ):
    """Actually run the hooks."""
    skips = _get_skips(environ)
    cols = _compute_cols(hooks, args.verbose)
    filenames = _all_filenames(args)
    filenames = filter_by_include_exclude(filenames, '', config['exclude'])
    classifier = Classifier(filenames)
    retval = 0
    for hook in hooks:
        retval |= _run_single_hook(classifier, hook, args, skips, cols)
        if retval and config['fail_fast']:
            break
    if retval and args.show_diff_on_failure and git.has_diff():
        if args.all_files:
            output.write_line(
                'pre-commit hook(s) made changes.\n'
                'If you are seeing this message in CI, '
                'reproduce locally with: `pre-commit run --all-files`.\n'
                'To run `pre-commit` as part of git workflow, use '
                '`pre-commit install`.',
            )
        output.write_line('All changes made by hooks:')
        # args.color is a boolean.
        # See user_color function in color.py
        subprocess.call((
            'git', '--no-pager', 'diff', '--no-ext-diff',
            '--color={}'.format({True: 'always', False: 'never'}[args.color]),
        ))

    return retval
コード例 #5
0
ファイル: try_repo.py プロジェクト: pre-commit/pre-commit
def _repo_ref(tmpdir, repo, ref):
    # if `ref` is explicitly passed, use it
    if ref:
        return repo, ref

    ref = git.head_rev(repo)
    # if it exists on disk, we'll try and clone it with the local changes
    if os.path.exists(repo) and git.has_diff('HEAD', repo=repo):
        logger.warning('Creating temporary repo with uncommitted changes...')

        shadow = os.path.join(tmpdir, 'shadow-repo')
        cmd_output('git', 'clone', repo, shadow)
        cmd_output('git', 'checkout', ref, '-b', '_pc_tmp', cwd=shadow)

        idx = git.git_path('index', repo=shadow)
        objs = git.git_path('objects', repo=shadow)
        env = dict(os.environ, GIT_INDEX_FILE=idx, GIT_OBJECT_DIRECTORY=objs)

        staged_files = git.get_staged_files(cwd=repo)
        if staged_files:
            xargs(('git', 'add', '--'), staged_files, cwd=repo, env=env)

        cmd_output('git', 'add', '-u', cwd=repo, env=env)
        git.commit(repo=shadow)

        return shadow, git.head_rev(shadow)
    else:
        return repo, ref
コード例 #6
0
def _run_hooks(config, hooks, args, environ):
    """Actually run the hooks."""
    skips = _get_skips(environ)
    cols = _compute_cols(hooks, args.verbose)
    filenames = _all_filenames(args)
    filenames = _filter_by_include_exclude(filenames, '', config['exclude'])
    retval = 0
    for hook in hooks:
        retval |= _run_single_hook(filenames, hook, args, skips, cols)
        if retval and config['fail_fast']:
            break
    if retval and args.show_diff_on_failure and git.has_diff():
        output.write_line('All changes made by hooks:')
        subprocess.call(('git', '--no-pager', 'diff', '--no-ext-diff'))
    return retval
コード例 #7
0
def _run_hooks(
    config: Dict[str, Any],
    hooks: Sequence[Hook],
    args: argparse.Namespace,
    environ: EnvironT,
) -> int:
    """Actually run the hooks."""
    skips = _get_skips(environ)
    cols = _compute_cols(hooks)
    filenames = filter_by_include_exclude(
        _all_filenames(args),
        config['files'],
        config['exclude'],
    )
    classifier = Classifier(filenames)
    retval = 0
    for hook in hooks:
        retval |= _run_single_hook(
            classifier,
            hook,
            skips,
            cols,
            verbose=args.verbose,
            use_color=args.color,
        )
        if retval and config['fail_fast']:
            break
    if retval and args.show_diff_on_failure and git.has_diff():
        if args.all_files:
            output.write_line(
                'pre-commit hook(s) made changes.\n'
                'If you are seeing this message in CI, '
                'reproduce locally with: `pre-commit run --all-files`.\n'
                'To run `pre-commit` as part of git workflow, use '
                '`pre-commit install`.', )
        output.write_line('All changes made by hooks:')
        # args.color is a boolean.
        # See user_color function in color.py
        git_color_opt = 'always' if args.color else 'never'
        subprocess.call((
            'git',
            '--no-pager',
            'diff',
            '--no-ext-diff',
            f'--color={git_color_opt}',
        ))

    return retval
コード例 #8
0
def _run_hooks(
    config: Dict[str, Any],
    hooks: Sequence[Hook],
    args: argparse.Namespace,
    environ: EnvironT,
) -> int:
    """Actually run the hooks."""
    skips = _get_skips(environ)
    cols = _compute_cols(hooks)
    classifier = Classifier.from_config(
        _all_filenames(args), config["files"], config["exclude"],
    )
    retval = 0
    for hook in hooks:
        retval |= _run_single_hook(
            classifier, hook, skips, cols, verbose=args.verbose, use_color=args.color,
        )
        if retval and config["fail_fast"]:
            break
    if retval and args.show_diff_on_failure and git.has_diff():
        if args.all_files:
            output.write_line(
                "pre-commit hook(s) made changes.\n"
                "If you are seeing this message in CI, "
                "reproduce locally with: `pre-commit run --all-files`.\n"
                "To run `pre-commit` as part of git workflow, use "
                "`pre-commit install`.",
            )
        output.write_line("All changes made by hooks:")
        # args.color is a boolean.
        # See user_color function in color.py
        git_color_opt = "always" if args.color else "never"
        subprocess.call(
            ("git", "--no-pager", "diff", "--no-ext-diff", f"--color={git_color_opt}",)
        )

    return retval