import subprocess def get_git_info(): try: # Get the latest tag latest_tag = subprocess.check_output(['git', 'describe', '--abbrev=0', '--tags']).decode().strip() # Get the number of commits since the latest tag commit_count = subprocess.check_output(['git', 'rev-list', f'{latest_tag}..HEAD', '--count']).decode().strip() # Get the current branch branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode().strip() # Get the short hash of the latest commit commit_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip() # Parse the version string version_parts = latest_tag.split('.') major, minor, patch = map(int, version_parts) return major, minor, patch, int(commit_count), branch, commit_hash except Exception as e: print(f"Error: {e}") return None, None, None, None, None, None def generate_c_file(filename, version_info): version_info_string = f"""\ // This file is auto-generated by a Python script // Do not modify manually #include "version_info.h" __attribute__((section(".version"))) const firmware_version_t VERSION_INFO = {{ {version_info[0]}, {version_info[1]}, {version_info[2]}, {version_info[3]}, "{version_info[4]}", "{version_info[5]}" }}; """ with open(filename, 'w') as f: f.write(version_info_string) if __name__ == "__main__": version_info = get_git_info() if version_info: generate_c_file("version_info.c", version_info) print("Version information has been written to version_info.c") else: print("Failed to retrieve Git information.")