
Ini adalah skrip Python sederhana yang dirancang untuk memfilter pelanggaran pengkodean (kemungkinan diidentifikasi oleh penganalisa statis) hanya untuk garis yang telah diubah dalam repositori git.
Alat ini berfungsi dengan penganalisa linter atau kode statis apa pun, termasuk tetapi tidak terbatas pada:
pip install ondiviPastikan Anda berada di direktori root repositori git Anda.
Jalankan skrip:
flake8 script.py | ondivi
# with ruff:
ruff check file.py --output-format=concise | ondiviatau:
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.
Script mem -parsing output diff git untuk mengidentifikasi baris yang diubah di setiap file.
Kemudian menyaring pelanggaran pengkodean yang diberikan untuk memasukkan hanya pelanggaran yang sesuai dengan garis yang diubah.
Flakeheaven dan Flakehell tidak didukung karena mengandalkan Flake8 API internal, yang dapat menyebabkan masalah kompatibilitas seiring berkembangnya Flake8. Sebaliknya, OnDivi hanya menggunakan output teks dari pelanggaran dan keadaan repositori git, membuatnya lebih kuat dan lebih mudah untuk dipertahankan.
Flake8 pada 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 1Contoh Perubahan:
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'))Dengan Git Diff yang kita lihat, bahwa dua baris baru ditambahkan (12 dan 16):
Ondivi menyaring pelanggaran dan hanya menunjukkan satu untuk baris 12:
$ flake8 script.py | ondivi
file.py:12:80: E501 line too long (119 > 79 characters)Proyek ini dilisensikan di bawah lisensi MIT. Lihat file lisensi untuk detailnya.