m1m02530125 commited on
Commit
2d155ff
·
1 Parent(s): 0bfd489

Web版のバージョン表示にgitコミットハッシュを使用

Browse files

_version.py未生成(Web版)の場合は.git/HEADから直接コミット
ハッシュ短縮形を取得して表示する。デスクトップ版のbuild-N表示は
変更なし。

Files changed (2) hide show
  1. utils/version.py +27 -0
  2. web_app.py +2 -2
utils/version.py CHANGED
@@ -1,6 +1,8 @@
1
  """アプリバージョン管理とアップデートチェック"""
2
  from __future__ import annotations
3
 
 
 
4
  # ビルド時に CI が上書きする (_version.py を生成)
5
  try:
6
  from utils._version import APP_VERSION # type: ignore
@@ -16,6 +18,31 @@ def get_build_number() -> int:
16
  return 0
17
 
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def check_for_update() -> tuple[bool, str, str]:
20
  """
21
  GitHub Releases API で最新ビルドを確認する。
 
1
  """アプリバージョン管理とアップデートチェック"""
2
  from __future__ import annotations
3
 
4
+ from pathlib import Path
5
+
6
  # ビルド時に CI が上書きする (_version.py を生成)
7
  try:
8
  from utils._version import APP_VERSION # type: ignore
 
18
  return 0
19
 
20
 
21
+ def _get_git_commit_hash() -> str:
22
+ """`.git`ディレクトリを直接読んで現在のコミットハッシュ(短縮形)を取得する。
23
+ gitコマンドに依存しないため、Dockerコンテナ内でも動作する。取得できなければ空文字を返す。"""
24
+ try:
25
+ git_dir = Path(__file__).resolve().parent.parent / ".git"
26
+ head = (git_dir / "HEAD").read_text().strip()
27
+ if head.startswith("ref:"):
28
+ ref_path = git_dir / head.split(" ", 1)[1]
29
+ commit = ref_path.read_text().strip()
30
+ else:
31
+ commit = head
32
+ return commit[:7]
33
+ except OSError:
34
+ return ""
35
+
36
+
37
+ def get_display_version() -> str:
38
+ """設定画面などに表示するバージョン文字列。
39
+ デスクトップ版はCIで埋め込まれたAPP_VERSION(build-N)を、
40
+ Web版(_version.py未生成)はgitコミットハッシュを返す。"""
41
+ if APP_VERSION != "dev":
42
+ return APP_VERSION
43
+ return _get_git_commit_hash() or APP_VERSION
44
+
45
+
46
  def check_for_update() -> tuple[bool, str, str]:
47
  """
48
  GitHub Releases API で最新ビルドを確認する。
web_app.py CHANGED
@@ -73,8 +73,8 @@ def create_app():
73
 
74
  @app.context_processor
75
  def inject_auth():
76
- from utils.version import APP_VERSION
77
- return {"auth_enabled": bool(APP_PASSWORD), "app_version": APP_VERSION}
78
 
79
  # 認証チェック(ログイン・静的ファイルは除外)
80
  @app.before_request
 
73
 
74
  @app.context_processor
75
  def inject_auth():
76
+ from utils.version import get_display_version
77
+ return {"auth_enabled": bool(APP_PASSWORD), "app_version": get_display_version()}
78
 
79
  # 認証チェック(ログイン・静的ファイルは除外)
80
  @app.before_request