Skip to content

Ondivi (Only diff violations) is a Python script filtering coding violations, identified by static analysis, only for changed lines in a Git repo.

License

Notifications You must be signed in to change notification settings

blablatdinov/ondivi

Repository files navigation

Ondivi (Only diff violations)

wemake-python-styleguide PyPI version CI status Lines of code Hits-of-Code

This is a simple Python script designed to filter coding violations (likely identified by a static analyzer) for only the lines that have been changed in a Git repository.

This tool works with any linter or static code analyzer, including but not limited to:

Prerequisites:

Installation

pip install ondivi

Usage

Ensure you are in the root directory of your Git repository.

Run the script:

flake8 script.py | ondivi
# with ruff:
ruff check file.py --output-format=concise | ondivi

or:

flake8 script.py > violations.txt
ondivi --fromfile=violations.txt
$ ondivi --help
Usage: ondivi [OPTIONS]

  Ondivi (Only diff violations).

  Python script filtering coding violations, identified by static analysis,
  only for changed lines in a Git repo. Usage example:

  flake8 script.py | ondivi

Options:
  --baseline TEXT    Commit or branch which will contain legacy code. Program
                     filter out violations on baseline (default: "master")
  --fromfile TEXT    Path to file with violations. Expected "utf-8" encoding
  --format TEXT      Template for parsing linter messages. The template should
                     include the following named parts:

                     {filename}   The name of the file with the error/warning
                     {line_num}   The line number with the error/warning
                     (integer)

                     Example usage:

                     --format "{filename}:{line_num:d}{other}"

                     In this example, the linter message

                     "src/app_types/listable.py:23:1: UP035 Import from
                     collections.abc instead: Sequence"

                     will be recognized and parsed into the following
                     components:

                      - filename: "src/app_types/listable.py"
                      - line_num: 23
                      - other: :1: "UP035 Import from collections.abc instead:
                      Sequence"

                     Ensure that the template matches the format of the
                     messages generated by your linter.
                     (default: "{filename}:{line_num:d}{other}")
  --only-violations  Show only violations
  --help             Show this message and exit.

How it works

The script parses the Git diff output to identify the changed lines in each file.

It then filters the given coding violations to include only those violations that correspond to the changed lines.

flakeheaven and flakehell are not supported because they rely on internal flake8 API, which can lead to compatibility issues as flake8 evolves. In contrast, ondivi uses only the text output of violations and the state of Git repository, making it more robust and easier to maintain.

Flake8 on file:

$ flake8 file.py
file.py:3:1: E302 expected 2 blank lines, found 1
file.py:9:1: E302 expected 2 blank lines, found 1
file.py:10:121: E501 line too long (123 > 120 characters)
file.py:14:1: E305 expected 2 blank lines after class or function definition, found 1

Example of changes:

 from dataclasses import dataclass

 @dataclass
 class User(object):

     name: str
     age: int

 def greet(user: User):
     print('Long string in initial commit ################################################################################')
     print(f'Hello, {user.name}!')
+    print('Long string in new commit ################################################################################')

 if __name__ == '__main__':
     greet(User(345, 23))
+    greet(User('Bob', '23'))

By git diff we see, that two new lines were appended (12 and 16):

Ondivi filters out violations and shows only one for line 12:

$ flake8 script.py | ondivi
file.py:12:80: E501 line too long (119 > 79 characters)

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Ondivi (Only diff violations) is a Python script filtering coding violations, identified by static analysis, only for changed lines in a Git repo.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published