#!/bin/bash # increment_and_push.sh - Version increment and git automation script # Usage: # ./increment_and_push.sh "meaningful git comment" # ./increment_and_push.sh --set-version vX.Y.Z "meaningful git comment" set -e # Exit on error # Color constants RED='\033[31m' GREEN='\033[32m' YELLOW='\033[33m' BLUE='\033[34m' BOLD='\033[1m' RESET='\033[0m' # Function to print output with colors print_info() { if [ "$USE_COLORS" = true ]; then echo -e "${BLUE}[INFO]${RESET} $1" else echo "[INFO] $1" fi } print_success() { if [ "$USE_COLORS" = true ]; then echo -e "${GREEN}${BOLD}[SUCCESS]${RESET} $1" else echo "[SUCCESS] $1" fi } print_warning() { if [ "$USE_COLORS" = true ]; then echo -e "${YELLOW}[WARNING]${RESET} $1" else echo "[WARNING] $1" fi } print_error() { if [ "$USE_COLORS" = true ]; then echo -e "${RED}${BOLD}[ERROR]${RESET} $1" else echo "[ERROR] $1" fi } # Check if we're in the correct directory CURRENT_DIR=$(basename "$(pwd)") if [ "$CURRENT_DIR" != "nostr_core_lib" ]; then print_error "Script must be run from the nostr_core_lib directory" echo "" echo "Current directory: $CURRENT_DIR" echo "Expected directory: nostr_core_lib" echo "" echo "Please change to the nostr_core_lib directory first." echo "" exit 1 fi # Check if git repository exists if ! git rev-parse --git-dir > /dev/null 2>&1; then print_error "Not a git repository. Please initialize git first." exit 1 fi TARGET_VERSION="" COMMIT_MESSAGE="" # Parse arguments if [ "$1" = "--set-version" ]; then if [ $# -lt 3 ]; then print_error "Usage: $0 --set-version vX.Y.Z \"meaningful git comment\"" echo "" echo "Example: $0 --set-version v0.6.0 \"Release v0.6.0 with ESP32 embedded support\"" echo "" exit 1 fi TARGET_VERSION="$2" COMMIT_MESSAGE="$3" else if [ $# -eq 0 ]; then print_error "Usage: $0 \"meaningful git comment\"" echo "" echo "Example: $0 \"Add enhanced subscription functionality\"" echo "" exit 1 fi COMMIT_MESSAGE="$1" fi # Check if nostr_core.h exists if [ ! -f "nostr_core/nostr_core.h" ]; then print_error "nostr_core/nostr_core.h not found" exit 1 fi print_info "Starting version increment and push process..." # Extract current version from nostr_core.h CURRENT_VERSION=$(grep '#define VERSION ' nostr_core/nostr_core.h | cut -d'"' -f2) if [ -z "$CURRENT_VERSION" ]; then print_error "Could not find VERSION define in nostr_core.h" exit 1 fi # Extract version components VERSION_MAJOR=$(grep '#define VERSION_MAJOR ' nostr_core/nostr_core.h | awk '{print $3}') VERSION_MINOR=$(grep '#define VERSION_MINOR ' nostr_core/nostr_core.h | awk '{print $3}') VERSION_PATCH=$(grep '#define VERSION_PATCH ' nostr_core/nostr_core.h | awk '{print $3}') if [ -z "$VERSION_MAJOR" ] || [ -z "$VERSION_MINOR" ] || [ -z "$VERSION_PATCH" ]; then print_error "Could not extract version components from nostr_core.h" exit 1 fi print_info "Current version: $CURRENT_VERSION (Major: $VERSION_MAJOR, Minor: $VERSION_MINOR, Patch: $VERSION_PATCH)" if [ -n "$TARGET_VERSION" ]; then if [[ ! "$TARGET_VERSION" =~ ^v([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then print_error "Invalid target version format: $TARGET_VERSION (expected vX.Y.Z)" exit 1 fi NEW_VERSION="$TARGET_VERSION" NEW_MAJOR="${BASH_REMATCH[1]}" NEW_MINOR="${BASH_REMATCH[2]}" NEW_PATCH="${BASH_REMATCH[3]}" else # Increment patch version NEW_MAJOR="$VERSION_MAJOR" NEW_MINOR="$VERSION_MINOR" NEW_PATCH=$((VERSION_PATCH + 1)) NEW_VERSION="v$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH" fi print_info "New version will be: $NEW_VERSION" # Update version in nostr_core.h sed -i "s/#define VERSION .*/#define VERSION \"$NEW_VERSION\"/" nostr_core/nostr_core.h sed -i "s/#define VERSION_MAJOR .*/#define VERSION_MAJOR $NEW_MAJOR/" nostr_core/nostr_core.h sed -i "s/#define VERSION_MINOR .*/#define VERSION_MINOR $NEW_MINOR/" nostr_core/nostr_core.h sed -i "s/#define VERSION_PATCH .*/#define VERSION_PATCH $NEW_PATCH/" nostr_core/nostr_core.h print_success "Updated version in nostr_core.h" # Check if VERSION file exists and update it if [ -f "VERSION" ]; then echo "$NEW_MAJOR.$NEW_MINOR.$NEW_PATCH" > VERSION print_success "Updated VERSION file" fi # Check git status if ! git diff --quiet; then print_info "Adding changes to git..." git add . print_info "Committing changes..." git commit -m "$COMMIT_MESSAGE" print_success "Changes committed" else print_warning "No changes to commit" fi # Create and push git tag print_info "Creating git tag: $NEW_VERSION" git tag "$NEW_VERSION" print_info "Pushing commits and tags..." CURRENT_BRANCH=$(git branch --show-current) git push origin "$CURRENT_BRANCH" git push origin "$NEW_VERSION" print_success "Version $NEW_VERSION successfully released!" print_info "Git commit: $COMMIT_MESSAGE" print_info "Tag: $NEW_VERSION" echo "" echo "🎉 Release complete! Version $NEW_VERSION is now live."