SigmaHQ/contrib/filter-uuid-patch

33 lines
930 B
Plaintext
Raw Normal View History

2019-11-12 22:03:54 +00:00
#!/usr/bin/env python3
# Remove all hunks from a patch that don't add the id attribute to minimize the impact (removed
2020-03-31 14:29:58 +00:00
# comments etc.) of sigma_uuid script.
2019-11-12 22:03:54 +00:00
#
# Usually used as follows:
# 1. Add UUIDs to rules:
2020-03-31 14:29:58 +00:00
# tools/sigma_uuid -er rules
2019-11-12 22:03:54 +00:00
# 2. Generate and filter patch
# git diff | contrib/filter-uuid-patch > rule-uuid.diff
# 3. Reset to previous state
# git reset --hard
# 4. Apply filtered patch
# patch -p1 < rule-uuid.diff
#
# This tool requires an installed unidiff package.
from unidiff import PatchSet
from sys import argv, stdin
try:
with open(argv[1], "r") as f:
patch = PatchSet(f.readlines())
except IndexError:
patch = PatchSet(stdin.readlines())
for patched_file in patch:
for h in reversed(range(len(patched_file))):
hunk = patched_file[h]
if not any([ line.is_added and line.value.startswith("id: ") for line in hunk ]):
del patched_file[h]
print(str(patch))