From d95bdef3f56757011cdee73d4bc4530453feb2e5 Mon Sep 17 00:00:00 2001 From: Jeffrey Hsu Date: Mon, 5 Jan 2026 21:22:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=94=9F=E6=88=90=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E4=BF=A1=E6=81=AF=E7=9A=84=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E4=BB=A5?= =?UTF-8?q?=E5=BA=94=E5=AF=B9=20git=20=E6=9C=AA=E5=AE=89=E8=A3=85=E6=88=96?= =?UTF-8?q?=E9=9D=9E=20git=20=E4=BB=93=E5=BA=93=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/hook.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/utils/hook.py b/utils/hook.py index 8230a91..76777e6 100644 --- a/utils/hook.py +++ b/utils/hook.py @@ -18,11 +18,23 @@ from datetime import datetime def gen_build_info(): - hash_str = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('utf-8').strip() + try: + hash_str = subprocess.check_output( + ['git', 'rev-parse', '--short', 'HEAD'], + stderr=subprocess.DEVNULL + ).decode('utf-8').strip() + except FileNotFoundError: + # git 未安装 + hash_str = 'unknown' + except subprocess.CalledProcessError: + # 不是 git 仓库(如从压缩包下载) + hash_str = 'unknown' + with open('build_info.py', 'w', encoding='utf-8') as f: f.write(f"# Auto-generated build info\n") f.write(f"BUILD_TIME = '{datetime.now().isoformat(sep=' ', timespec='seconds')}'\n") f.write(f"GIT_HASH = '{hash_str}'\n") -gen_build_info() +if __name__ == '__main__': + gen_build_info()