#!/usr/bin/env nix
#! nix shell nixpkgs#python3  --command python
import logging

def inject_diff():
    source_file = 'post-diff'
    target_file = 'pr_body.md'
    placeholder = "nix-diff-placeholder"

    logging.info(f"injecting '{source_file}' into '{target_file}' using '{placeholder}' as a placeholder")

    out = []
    with open(source_file,'r') as src:
        src_content = src.read()
        if len(src_content) > 60000:
            logging.warning(f"{source_file} is longer than 60k characters, truncating")
            src_content = src_content[:60000]
    with open(target_file,'r') as tgt:
        for line in tgt.readlines():
            if placeholder in line:
                out.append(line.replace(placeholder,src_content))
            else:
                out.append(line)

    with open(target_file,'w') as tgt:
        tgt.writelines(out)




if __name__ == "__main__":
    logging.basicConfig( level=logging.INFO)
    inject_diff()