check-dco.py
1 #!/usr/bin/env python 2 # SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-or-later OR CERN-OHL-S-2.0+ OR Apache-2.0 3 # Check if commit message if signed off by author or committer. 4 # Currently only checks the last commit log 5 from typing import Dict, Optional 6 from subprocess import run, PIPE 7 8 r = run(("git", "log", "-n", "1", "--pretty=fuller"), stdout=PIPE) 9 ids: Dict[str, Optional[str]] = { 10 "Author:": None, 11 "Commit:": None, 12 "Signed-off-by:": None, 13 } 14 for line in r.stdout.decode().split("\n"): 15 for key in ids.keys(): 16 if key in line: 17 idx = line.index(key) 18 ids[key] = line[(idx + len(key)):].strip() 19 author = ids["Author:"] 20 committer = ids["Commit:"] 21 signer = ids["Signed-off-by:"] 22 23 assert author is not None 24 assert committer is not None 25 signer = ids["Signed-off-by:"] 26 if signer is None: 27 raise ValueError("'Signed-off-by:' line not found in commit message") 28 if signer not in (author, committer): 29 raise ValueError( 30 f"Signer '{signer}' differs from author '{author}' and committer '{committer}'", 31 )