
นี่คือสคริปต์ Python อย่างง่ายที่ออกแบบมาเพื่อกรองการละเมิดการเข้ารหัส (น่าจะระบุโดยเครื่องวิเคราะห์แบบคงที่) สำหรับเฉพาะบรรทัดที่มีการเปลี่ยนแปลงในพื้นที่เก็บข้อมูล GIT
เครื่องมือนี้ใช้งานได้กับเครื่องวิเคราะห์รหัส linter หรือ code ใด ๆ รวมถึง แต่ไม่ จำกัด เพียง:
pip install ondiviตรวจสอบให้แน่ใจว่าคุณอยู่ในไดเรกทอรีรากของที่เก็บ Git ของคุณ
เรียกใช้สคริปต์:
flake8 script.py | ondivi
# with ruff:
ruff check file.py --output-format=concise | ondiviหรือ:
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.
สคริปต์จะวิเคราะห์เอาต์พุต Git Diff เพื่อระบุบรรทัดที่เปลี่ยนแปลงในแต่ละไฟล์
จากนั้นจะกรองการละเมิดการเข้ารหัสที่กำหนดเพื่อรวมเฉพาะการละเมิดที่สอดคล้องกับบรรทัดที่เปลี่ยนแปลง
Flakeheaven และ Flakehell ไม่ได้รับการสนับสนุนเพราะพวกเขาพึ่งพา Flake8 ภายใน API ซึ่งสามารถนำไปสู่ปัญหาความเข้ากันได้เมื่อ Flake8 วิวัฒนาการ ในทางตรงกันข้าม Ondivi ใช้เฉพาะเอาท์พุทข้อความของการละเมิดและสถานะของพื้นที่เก็บข้อมูล GIT ทำให้มีความแข็งแกร่งและง่ายต่อการบำรุงรักษามากขึ้น
Flake8 บนไฟล์:
$ 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ตัวอย่างของการเปลี่ยนแปลง:
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'))โดย Git Diff เราเห็นว่าสองบรรทัดใหม่ถูกต่อท้าย (12 และ 16):
Ondivi กรองการละเมิดและแสดงเพียงหนึ่งเดียวสำหรับบรรทัด 12:
$ flake8 script.py | ondivi
file.py:12:80: E501 line too long (119 > 79 characters)โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต MIT ดูไฟล์ใบอนุญาตสำหรับรายละเอียด