Tamarin/get-updated-manifest-from-g...

78 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
function show_usage {
echo
echo "Usage: $0 <src>"
echo
echo "Paramètres: "
echo
echo " - <src> Chemin vers le répertoire des sources du projet. Le projet doit être un dépôt Git valide."
echo
}
function main {
cd "$SRC_DIR"
if [ ! -d '.git' ]; then
fatal "The directory $SRC_DIR seems not to be a valid Git repository."
fi
MANIFEST=$(cat tamarin.json 2>/dev/null)
if [ -z "$MANIFEST" ]; then
MANIFEST="{}"
fi
# Extract project info from Git
repo_name=$(basename `git rev-parse --show-toplevel`)
current_commit=$(git log -n 1 --pretty=format:"%h")
# Get commits log as changelog
logs=$(git log --pretty=format:"%an - %h : %s" | sed 's/"/\\"/g')
# Set the top commiter as the maintainer of the project
git_maintainer=$(git log --pretty=short | git shortlog -s -n -e | sed 's/^\s*[0-9]*\s*//g' | head -n 1)
# Get current version from manifest
current_name=$(echo "$MANIFEST" | jq -r ".name" | sed 's/null//')
current_version=$(echo "$MANIFEST" | jq -r ".version" | sed 's/null//')
current_changelog=$(echo "$MANIFEST" | jq -r ".changelog" | sed 's/null//')
current_maintainer=$(echo "$MANIFEST" | jq -r ".maintainer" | sed 's/null//')
# Complete manifest
# Add commit number to version
MANIFEST=$(echo "$MANIFEST" | jq -r ".version = \"${current_version:-0.0.0}~$current_commit\"")
# Set name if not defined
MANIFEST=$(echo "$MANIFEST" | jq -r ".name = \"${current_name:-$repo_name}\"")
# Set maintainer if not defined
MANIFEST=$(echo "$MANIFEST" | jq -r ".maintainer = \"${current_maintainer:-$git_maintainer}\"")
# Set changelog from git log if not defined
if [ -z "$current_changelog" ]; then
MANIFEST=$(echo "$MANIFEST" | jq -r ".changelog = []")
while read -r entry; do
MANIFEST=$(echo "$MANIFEST" | jq -r ".changelog += [\"$entry\"]")
done <<< "$logs"
fi
echo "$MANIFEST"
}
# Load util lib
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source "$DIR/lib/util.sh"
# Test for arguments
if [ -z "$1" ]; then
show_usage
exit 1
fi
SRC_DIR=$(readlink -f "$1")
main