From c7efcb724601162026733f82466e888dd8e692ea Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Sat, 18 Oct 2025 12:48:37 +0200 Subject: [PATCH] Fix /deployment/version/ endpoint to show commit_date and add deployment_date - Fix commit_date parsing from git logs (was showing 'unknown') - Add deployment_date field using manage.py modification time - Improve git log parsing to handle author names with spaces - Both dates now show in proper format: YYYY-MM-DD HH:MM:SS +timezone The endpoint now returns: - commit_date: When the commit was made (from git logs) - deployment_date: When the code was deployed (from file mtime) --- ivatar/views.py | 51 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/ivatar/views.py b/ivatar/views.py index 5831bef..bea9cbe 100644 --- a/ivatar/views.py +++ b/ivatar/views.py @@ -876,26 +876,31 @@ def _get_git_info_from_files(): f.seek(max(0, file_size - chunk_size)) chunk = f.read().decode("utf-8", errors="ignore") - # Find the last newline - last_newline = chunk.rfind("\n") - if last_newline != -1: - last_line = chunk[last_newline + 1:].strip() - else: - last_line = chunk.strip() + # Find the last non-empty line + lines = chunk.split("\n") + last_line = None + for line in reversed(lines): + if line.strip(): + last_line = line.strip() + break if last_line: # Git log format: - parts = last_line.split("\t") - if len(parts) >= 2: + # The format uses spaces, not tabs + parts = last_line.split() + if len(parts) >= 6: # Extract timestamp and convert to readable date - timestamp_part = parts[0].split()[-2] # Get timestamp - if timestamp_part.isdigit(): - import datetime + # Format: + # We need to find the timestamp which is after the author email + for i, part in enumerate(parts): + if part.isdigit() and len(part) == 10: # Unix timestamp + import datetime - timestamp = int(timestamp_part) - commit_date = datetime.datetime.fromtimestamp( - timestamp - ).strftime("%Y-%m-%d %H:%M:%S %z") + timestamp = int(part) + commit_date = datetime.datetime.fromtimestamp( + timestamp + ).strftime("%Y-%m-%d %H:%M:%S %z") + break except (ValueError, IndexError, UnicodeDecodeError): pass @@ -911,11 +916,27 @@ def _get_git_info_from_files(): except Exception: commit_date = "unknown" + # Get deployment date from file modification time + # Use manage.py as it's always updated during deployment + deployment_date = None + manage_py_path = path.join(project_root, "manage.py") + if path.exists(manage_py_path): + try: + import datetime + + mtime = path.getmtime(manage_py_path) + deployment_date = datetime.datetime.fromtimestamp(mtime).strftime( + "%Y-%m-%d %H:%M:%S %z" + ) + except Exception: + deployment_date = "unknown" + return { "commit_hash": commit_hash, "short_hash": commit_hash[:7] if len(commit_hash) >= 7 else commit_hash, "branch": branch_name, "commit_date": commit_date or "unknown", + "deployment_date": deployment_date or "unknown", "deployment_status": "active", "version": f"{branch_name}-{commit_hash[:7] if len(commit_hash) >= 7 else commit_hash}", }