File new_version.sh of Package autogit

#!/bin/bash
set -e

# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Function to print colored messages
info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
    exit 1
}

warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

# Check if version argument is provided
if [ $# -ne 1 ]; then
    error "Usage: $0 <version>"
fi

NEW_VERSION="$1"

# Validate version format (e.g., 0.8.2)
if ! [[ "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    error "Invalid version format. Expected: X.Y.Z (e.g., 0.8.2)"
fi

info "Starting release process for version $NEW_VERSION"

# Check if we're in an OBS working directory
if [ ! -d ".osc" ]; then
    error "Not in an OBS working directory (no .osc directory found)"
fi

# Step 1: Update _service file
info "Updating _service file to point to tag v$NEW_VERSION"
if [ ! -f "_service" ]; then
    error "_service file not found"
fi

sed -i "s|<param name=\"revision\">v[0-9.]*</param>|<param name=\"revision\">v$NEW_VERSION</param>|" _service

# Step 2: Clean old files
info "Cleaning old generated files"
rm -f autogit-*.tar.zst autogit-*.tar registry.tar.zst _servicedata autogit.changes

# Step 3: Run tar_scm service manually
info "Running tar_scm service to fetch tag v$NEW_VERSION"
osc service manualrun tar_scm 2>&1 | grep -v "^ls:" || true

# Check if tar file was created
TAR_FILE=$(ls autogit-*.tar 2>/dev/null | head -1)
if [ -z "$TAR_FILE" ]; then
    error "Failed to create source tarball"
fi

EXTRACTED_VERSION=$(echo "$TAR_FILE" | sed 's/autogit-\(.*\)\.tar/\1/')
if [ "$EXTRACTED_VERSION" != "$NEW_VERSION" ]; then
    warning "Expected version $NEW_VERSION but got $EXTRACTED_VERSION from tarball"
fi

# Step 4: Compress tarball
info "Compressing tarball with zstd"
zstd -f "$TAR_FILE"
rm -f "$TAR_FILE"

# Step 5: Extract and run cargo_vendor
info "Extracting tarball for cargo vendoring"
tar -xf "autogit-$NEW_VERSION.tar.zst"

info "Running cargo_vendor service (this may take a while)"
osc service run cargo_vendor 2>&1 | tail -5

# Step 6: Rename cargo vendor output
if [ -f "_service:cargo_vendor:registry.tar.zst" ]; then
    mv _service:cargo_vendor:registry.tar.zst registry.tar.zst
fi

if [ ! -f "registry.tar.zst" ]; then
    error "Failed to create registry.tar.zst"
fi

# Step 7: Update spec file version
info "Updating .spec file version to $NEW_VERSION"
if [ ! -f "autogit.spec" ]; then
    error "autogit.spec file not found"
fi

sed -i "s/^Version:.*$/Version:        $NEW_VERSION/" autogit.spec

# Step 8: Update changelog
info "Adding changelog entry"
osc vc -m "Update to version $NEW_VERSION"

# Step 9: Handle file changes in OBS
info "Managing file changes in OBS"
OLD_TARBALL=$(osc status | grep "^!" | grep "autogit-.*\.tar\.zst" | awk '{print $2}')
if [ -n "$OLD_TARBALL" ]; then
    info "Removing old tarball: $OLD_TARBALL"
    osc remove "$OLD_TARBALL"
fi

NEW_TARBALL="autogit-$NEW_VERSION.tar.zst"
if [ -f "$NEW_TARBALL" ]; then
    info "Adding new tarball: $NEW_TARBALL"
    osc add "$NEW_TARBALL"
fi

# Step 10: Show status
info "Current OBS status:"
osc status

# Step 11: Commit changes
echo ""
read -p "$(echo -e ${YELLOW}Commit changes to OBS? [y/N]:${NC} )" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    info "Committing changes to OBS"
    osc commit -m "Update to version $NEW_VERSION"

    info "Build started! Checking results..."
    sleep 2
    osc results

    echo ""
    info "✓ Successfully released version $NEW_VERSION"
    info "Monitor build status with: osc results"
else
    warning "Commit skipped. You can commit manually with: osc commit -m 'Update to version $NEW_VERSION'"
fi

# Cleanup extracted directory
info "Cleaning up extracted source directory"
rm -rf "autogit-$NEW_VERSION"

info "Done!"
openSUSE Build Service is sponsored by