#!/usr/bin/python3
import argparse
import sys
import os.path
scriptsdir = os.path.dirname(__file__)
sys.path.append(os.path.join(scriptsdir, "../git_sort"))
from patch import Patch
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Add references to patch file")
parser.add_argument("-r", "--reference", action="append",
help="bsc# or CVE token used to tag the patch file. The option can be used more times.")
parser.add_argument("patches", help="Patch files.",
nargs=argparse.REMAINDER)
args = parser.parse_args()
added_refs = list(args.reference)
for f in args.patches:
with Patch(open(f, "r+b")) as patch:
refs = "".join(patch.get("References"))
refs = list(refs.replace(",", " ").split())
new_refs = refs + [r for r in added_refs if not r in refs]
if new_refs == refs:
continue
patch.change("References", " ".join(new_refs))