Has anyone had much luck with using pants in a pre...
# general
p
Has anyone had much luck with using pants in a precommit hook? I tried to add
pants --changed-since=HEAD --changed-dependents=transitive check
and I think it is picking up files that are changed but not staged for commit
Hard to actually tell what is happening too since the precommit output is extremely terse
c
We still use Pants with pre-commit, but since pre-commit wants to operate on files and Pants considers targets their is a bit of an impediance mismatch. We have something like:
Copy code
- id: pants-green
        name: pants green
        always_run: false
        description: runs pants fmt/lint/check on python files
        entry: pants --changed-since=HEAD --no-dynamic-ui green
        language: system
        pass_filenames: false
        files: *pants_include_pattern
        exclude: *pants_exclude_pattern
w
I've never looked into the weeds of this before, but there seems to be a flag for tracked and untracked deps - maybe it's a jump off point? https://github.com/pantsbuild/pants/blob/0c9bbceaa026c73ddacc1fcb5740f284acb3ac60/src/python/pants/vcs/git.py#L95 What are you expecting to happen exactly with that command? As in, what if there are transitive deps that are both staged and unstaged?
w
pre-commit
tool doesn’t like giving you much feedback. So I have a wrapper that looks like
Copy code
$ cat pre-commit-helper.sh
#!/bin/bash
set -e -o pipefail
LOG=$1
shift
2>&1 $@ | tee $LOG
then my precommit looks like
Copy code
- id: pants-lint
    name: Pants Lint (tail -f ./pre-commit-log-pants-lint.txt for progress)
    entry: "./pre-commit-helper.sh pre-commit-log-pants-lint.txt ./pants --no-dynamic-ui --changed-since=HEAD lint"
    language: script
    types: [python]
    pass_filenames: false
    verbose: true
    require_serial: true
so while it’s running you can tail the file
pre-commit itself should stash unstaged before running, so it’s weird for pants to run with unstaged changes.
Its unlikely that pre-commit will add better hook feedback, if this is to be believed.
c
My hope is widdle things down to a single hook that is "run pants" at which point there won't really be much need to support
pre-commit
anymore.
w
oh thats interesting - and just call directly from git hook directly?
c
To just have some sort of "run this before submitting for code review" command, and not tie it to git internals
p
@wide-midnight-78598 the thing I wanted to happen was to pick the files that have been staged for commit as the changed files, and then figure out the transitive dependencies of those. Ideally it should use the HEAD files for everything that is not staged. If pre-commit is meant to stash unstaged changes as Nasron said, then I'm probably confused about the source of my errors since the output is so minimal