from pathlib import Path
from typing import Union
import hashlib
import re


def insert_hash(path: Path, content: Union[str, bytes], *, hash_length=7, hash_algorithm=hashlib.md5):
    """
    Insert a hash based on the content into the path after the first dot.

    hash_length 7 matches git commit short references
    """
    if isinstance(content, str):
        content = content.encode()
    hash_ = hash_algorithm(content).hexdigest()[:hash_length]
    if '.' in path.name:
        new_name = re.sub(r'\.', f'.{hash_}.', path.name, count=1)
    else:
        new_name = f'{path.name}.{hash_}'
    return path.with_name(new_name)