import argparse
import subprocess
import sys
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--base_ref", required=True, help="Older git ref")
parser.add_argument("--head_ref", required=True, help="Newer git ref")
return parser.parse_args()
def run_git_log(base_ref: str, head_ref: str):
cmd = [
"git",
"log",
"--pretty=format:%h|%an|%s",
f"{base_ref}..{head_ref}",
]
result = subprocess.run(cmd, capture_output=True, text=True, check=False)
if result.returncode != 0:
raise RuntimeError(result.stderr.strip() or "git log failed")
return [line for line in result.stdout.splitlines() if line.strip()]
def main():
args = parse_args()
try:
entries = run_git_log(args.base_ref, args.head_ref)
except Exception as exc: # noqa: BLE001
print(f"Failed to build changelog: {exc}", file=sys.stderr)
sys.exit(1)
print("Release Changelog")
print("=================")
print(f"Range: {args.base_ref}..{args.head_ref}")
print()
if not entries:
print("No commits found in the selected range.")
return
for entry in entries:
sha, author, subject = entry.split("|", 2)
print(f"- {subject} ({sha}, {author})")
if __name__ == "__main__":
main()