Skip to content

Commit 047431d

Browse files
authored
fix(backend): determine active branch on startup (inventree#9056)
* fix(backend): Determine active branch on startup * add more warnings and document them * extend expected errors * fix typos
1 parent 4df6e98 commit 047431d

2 files changed

Lines changed: 31 additions & 9 deletions

File tree

docs/docs/settings/error_codes.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ You probably have a reference to invoke or a directory with invoke in your PATH
2525
### INVE-W (InvenTree Warning)
2626
Warnings - These are non-critical errors which should be addressed when possible.
2727

28+
#### INVE-W1
29+
**Current branch could not be detected - Backend**
30+
31+
During startup of the backend InvenTree tries to detect branch, commit hash and commit date to surface on various points in the UI, through tags and in the API.
32+
This information is not needed for operation but very helpful for debugging and support. These issues might be caused by running a deployment version that delivers without git information, not having git installed or not having dulwich installed.
33+
You can ignore this warning if you are not interested in the git information.
34+
35+
#### INVE-W2
36+
**Dulwich module not found - Backend**
37+
38+
See [INVE-W1](#inve-w1)
39+
40+
#### INVE-W3
41+
**Could not detect git information - Backend**
42+
43+
See [INVE-W1](#inve-w1)
44+
45+
2846
### INVE-I (InvenTree Information)
2947
Information — These are not errors but information messages. They might point out potential issues or just provide information.
3048

src/backend/InvenTree/InvenTree/version.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@
2626

2727
# Discover git
2828
try:
29+
from dulwich.porcelain import active_branch
2930
from dulwich.repo import Repo
3031

3132
main_repo = Repo(pathlib.Path(__file__).parent.parent.parent.parent.parent)
3233
main_commit = main_repo[main_repo.head()]
34+
try:
35+
main_branch = active_branch(main_repo)
36+
except (KeyError, IndexError):
37+
logger.warning('INVE-W1: Current branch could not be detected.')
38+
main_branch = None
3339
except ImportError:
3440
logger.warning(
35-
'Warning: Dulwich module not found, git information will not be available.'
41+
'INVE-W2: Dulwich module not found, git information will not be available.'
3642
)
3743
main_repo = None
3844
main_commit = None
39-
except Exception:
45+
main_branch = None
46+
except Exception as exc:
47+
logger.warning('INVE-W3: Could not detect git information.', exc_info=exc)
4048
main_repo = None
4149
main_commit = None
50+
main_branch = None
4251

4352

4453
def checkMinPythonVersion():
@@ -270,14 +279,9 @@ def inventreeBranch():
270279
if branch:
271280
return branch
272281

273-
if main_commit is None:
282+
if main_branch is None:
274283
return None
275-
276-
try:
277-
branch = main_repo.refs.follow(b'HEAD')[0][1].decode()
278-
return branch.removeprefix('refs/heads/')
279-
except IndexError:
280-
return None # pragma: no cover
284+
return main_branch.decode('utf-8')
281285

282286

283287
def inventreeTarget():

0 commit comments

Comments
 (0)