Skip to content

Commit

Permalink
Display full commit in the version displayed in the UI #88 (#91)
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <tdruez@nexb.com>
  • Loading branch information
tdruez committed May 2, 2024
1 parent d0e2b38 commit 97e3d7a
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions .VERSION
@@ -0,0 +1 @@
$Format:%(describe:tags)$
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -54,6 +54,9 @@ Release notes
- Fix the logout link of the admin app.
https://github.com/nexB/dejacode/issues/89

- Display full commit in the version displayed in the UI
https://github.com/nexB/dejacode/issues/88

### Version 5.0.1

- Improve the stability of the "Check for new Package versions" feature.
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Expand Up @@ -31,6 +31,7 @@ RUN apt-get update \
libldap2-dev \
libsasl2-dev \
libpq5 \
git \
wait-for-it \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Expand Down
57 changes: 56 additions & 1 deletion dejacode/__init__.py
Expand Up @@ -9,9 +9,64 @@
import os
import sys
import warnings
from contextlib import suppress
from pathlib import Path

import git

VERSION = "5.1.0-dev"
__version__ = VERSION

PROJECT_DIR = Path(__file__).resolve().parent
ROOT_DIR = PROJECT_DIR.parent


def get_version(version):
"""Return the version including the git describe tag when available."""
# The codebase is a git clone
if git_describe := get_git_describe_from_local_checkout():
return git_describe

# The codebase is an extracted git archive
if git_describe := get_git_describe_from_version_file():
return git_describe

return version


def get_git_describe_from_local_checkout():
"""
Return the git describe tag from the local checkout.
This will only provide a result when the codebase is a git clone.
"""
with suppress(git.GitError):
return git.Repo(".").git.describe(tags=True, always=True)


def get_git_describe_from_version_file(version_file_location=ROOT_DIR / ".VERSION"):
"""
Return the git describe tag from the ".VERSION" file.
This will only provide a result when the codebase is an extracted git archive
"""
try:
version = version_file_location.read_text().strip()
except (FileNotFoundError, UnicodeDecodeError):
return

if version and version.startswith("v"):
return version


def extract_short_commit(git_describe):
"""
Extract the short commit hash from a Git describe string while removing
any leading "g" character if present.
"""
short_commit = git_describe.split("-")[-1]
return short_commit.lstrip("g")


__version__ = get_version(VERSION)


# Turn off the warnings for the following modules.
warnings.filterwarnings("ignore", module="cyclonedx")
Expand Down
2 changes: 1 addition & 1 deletion dje/templates/includes/header.html
Expand Up @@ -17,7 +17,7 @@
<a class="dropdown-item" href="https://github.com/nexB/dejacode/releases" target="_blank" rel="noreferrer">{% trans 'Releases' %}</a>
<a class="dropdown-item" href="https://scancode-licensedb.aboutcode.org/agpl-3.0.html" target="_blank" rel="noreferrer">{% trans 'License' %}</a>
<div class="dropdown-divider"></div>
<h6 class="dropdown-header">DejaCode: v{{ DEJACODE_VERSION }}</h6>
<h6 class="dropdown-header">{{ DEJACODE_VERSION }}</h6>
</div>
</div>
{% endblock %}
2 changes: 1 addition & 1 deletion dje/templates/includes/navbar_header.html
Expand Up @@ -126,7 +126,7 @@
</a>
{% endif %}
<div class="dropdown-divider"></div>
<div class="dropdown-header">DejaCode: v{{ DEJACODE_VERSION }}</div>
<div class="dropdown-header">{{ DEJACODE_VERSION }}</div>
<div class="dropdown-divider"></div>
<form id="logout-form" method="post" action="{% url 'logout' %}">
{% csrf_token %}
Expand Down
4 changes: 4 additions & 0 deletions setup.cfg
Expand Up @@ -164,6 +164,10 @@ install_requires =
sortedcontainers==2.4.0
toml==0.10.2
py-serializable==1.0.3
# Git
gitpython==3.1.43
gitdb==4.0.11
smmap==5.0.1

[options.extras_require]
dev =
Expand Down
Binary file not shown.
Binary file added thirdparty/dist/gitdb-4.0.11-py3-none-any.whl
Binary file not shown.
Binary file added thirdparty/dist/smmap-5.0.1-py3-none-any.whl
Binary file not shown.

0 comments on commit 97e3d7a

Please sign in to comment.