Files
dropshell-templates/detect-changes.sh
Your Name bedc68373d
Some checks failed
Test and Publish Templates / test-and-publish (push) Failing after 14s
Add 7 and update 2 files
2025-08-24 20:50:58 +12:00

78 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
set -e
SCRIPT_DIR=$(dirname "$0")
cd "$SCRIPT_DIR"
function info() {
echo "INFO: $1" >&2
}
function get_last_version_tag() {
# Get the most recent tag that modified versions.json
git tag --sort=-version:refname | while read tag; do
if git diff --name-only "$tag" HEAD 2>/dev/null | grep -q "^versions.json$"; then
echo "$tag"
return
fi
done
# If no tag found, use the initial commit
echo ""
}
function detect_changed_templates() {
local base_ref="$1"
local changed_templates=""
if [ -z "$base_ref" ]; then
info "No previous version tag found, considering all templates as changed"
# Return all template directories
find . -maxdepth 1 -type d ! -name ".*" ! -name "." | sed 's|^\./||' | grep -v ".gitea" | sort
else
info "Comparing against $base_ref"
# Get list of changed files since the base ref
local changed_files=$(git diff --name-only "$base_ref" HEAD 2>/dev/null || echo "")
if [ -z "$changed_files" ]; then
info "No changes detected since $base_ref"
return
fi
# Check which templates have changes
for template_dir in $(find . -maxdepth 1 -type d ! -name ".*" ! -name "." | sed 's|^\./||' | grep -v ".gitea"); do
if echo "$changed_files" | grep -q "^${template_dir}/"; then
echo "$template_dir"
fi
done | sort | uniq
fi
}
# Main execution
if [ "$1" == "--json" ]; then
# Output as JSON array
last_tag=$(get_last_version_tag)
changed=$(detect_changed_templates "$last_tag")
if [ -z "$changed" ]; then
echo "[]"
else
echo -n "["
first=true
for template in $changed; do
if [ "$first" = true ]; then
first=false
else
echo -n ","
fi
echo -n "\"$template\""
done
echo "]"
fi
else
# Output as newline-separated list
last_tag=$(get_last_version_tag)
detect_changed_templates "$last_tag"
fi