def METHOD_NAME(git_repo_dir: str) -> Optional[str]:
    try:
        sha1 = subprocess.check_output(
            'cd {} && git rev-parse HEAD'.format(shlex.quote(git_repo_dir)), shell=True
        ).decode('utf-8').strip()
        if re.match(r'^[0-9a-f]{40}$', sha1):
            return sha1
        logging.warning("Invalid git SHA1 in directory '%s': %s", git_repo_dir, sha1)
        return None
    except Exception as e:
        logging.warning("Failed to get git SHA1 in directory: %s", git_repo_dir)
        return None