You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

105 lines
3.7 KiB

#!/bin/bash
# TimeSafari Git Hash Retrieval Script
# Author: Matthew Raymer
# Description: Retrieves the current git commit hash for the active branch
#
# This script ensures that the correct git hash is retrieved regardless of
# the current branch or git state. It handles edge cases like detached HEAD
# and provides fallbacks for when git is not available.
#
# ARCHITECTURAL BENEFITS:
# - Centralized Logic: Single source of truth for git hash retrieval across all build scripts
# - Consistent Behavior: Ensures all builds use the same git hash logic and format
# - Maintainability: Changes to git hash logic only need to be made in one place
# - Robust Error Handling: Handles edge cases that could cause build failures
# - Branch-Aware: Explicitly uses current branch, preventing default branch fallback issues
#
# USAGE PATTERNS:
# # Direct usage
# ./scripts/get-git-hash.sh
#
# # In build scripts (recommended)
# VITE_GIT_HASH=$(./scripts/get-git-hash.sh) npm run build
#
# # In shell scripts
# git_hash=$(./scripts/get-git-hash.sh)
# echo "Current commit: $git_hash"
#
# # In package.json scripts
# "build:capacitor": "VITE_GIT_HASH=$(./scripts/get-git-hash.sh) vite build --mode capacitor --config vite.config.capacitor.mts"
#
# OUTPUT:
# - Git commit hash (7 characters) if available (e.g., "bf08e57c")
# - "unknown" if git is not available or no repository found
#
# EXIT CODES:
# 0 - Success (hash retrieved or "unknown" returned)
# 1 - Error (should not occur in normal operation)
#
# EDGE CASES HANDLED:
# - Detached HEAD state: Falls back to HEAD commit
# - No git repository: Returns "unknown"
# - Git not installed: Returns "unknown"
# - No commits: Returns "unknown"
# - Branch detection failure: Falls back to HEAD commit
#
# INTEGRATION POINTS:
# - scripts/common.sh: Primary usage via get_git_hash() function
# - package.json: Direct usage in build:capacitor script
# - Build scripts: Used by build-web.sh, build-electron.sh, etc.
# - Docker builds: Ensures consistent git hashes in containerized builds
#
# VALUE PROPOSITION:
# This script was created to solve git hash inconsistencies across different
# build environments and branch states. It provides a reliable, consistent
# interface for git hash retrieval that works regardless of the current
# git state or environment. This prevents issues like:
# - Builds using wrong branch's commit hash
# - Inconsistent versioning across different build types
# - Build failures due to git state issues
# - Manual git hash management in multiple scripts
#
# MAINTENANCE:
# - Update this script if git hash retrieval logic needs to change
# - All build scripts automatically benefit from improvements
# - Test with various git states (detached HEAD, different branches, etc.)
# - Ensure compatibility with CI/CD environments
set -euo pipefail
# Function to get git hash for versioning
get_git_hash() {
# Check if git is available
if ! command -v git &> /dev/null; then
echo "unknown"
return 0
fi
# Check if we're in a git repository
if ! git rev-parse --git-dir &> /dev/null; then
echo "unknown"
return 0
fi
# Get the current branch name
local current_branch
current_branch=$(git branch --show-current 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
# If we're in a detached HEAD state or no branch, use HEAD
if [ -z "$current_branch" ] || [ "$current_branch" = "HEAD" ]; then
git log -1 --pretty=format:%h 2>/dev/null || echo "unknown"
else
# Use the current branch explicitly
git log -1 --pretty=format:%h "$current_branch" 2>/dev/null || echo "unknown"
fi
}
# Main execution
main() {
local git_hash
git_hash=$(get_git_hash)
echo "$git_hash"
}
# Run main function
main "$@"