library-distribution-mirror/git-apply-patch.sh

135 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# Applies current directory content as a patch to an existing git repository
#
# Usage: ./git-apply-patch.sh <config-file>
#
# Config file format:
# GIT_URL=https://github.com/user/repo.git
# GIT_USER=username
# GIT_TOKEN=your_token_or_password
#
# VERSION file format:
# <new_version>
# last_version: <previous_version>
# source_branch: <branch_name> (optional)
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <config-file>"
exit 1
fi
CONFIG_FILE="$1"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Error: Config file '$CONFIG_FILE' not found"
exit 1
fi
# Load config file
source "$CONFIG_FILE"
# Validate required fields
if [ -z "$GIT_URL" ]; then
echo "Error: GIT_URL not defined in config file"
exit 1
fi
if [ -z "$GIT_USER" ]; then
echo "Error: GIT_USER not defined in config file"
exit 1
fi
if [ -z "$GIT_TOKEN" ]; then
echo "Error: GIT_TOKEN not defined in config file"
exit 1
fi
# Read VERSION file
if [ ! -f "VERSION" ]; then
echo "Error: VERSION file not found in current directory"
exit 1
fi
NEW_VERSION=$(sed -n '1p' VERSION)
LAST_VERSION=$(sed -n '2p' VERSION | sed 's/last_version: //')
SOURCE_BRANCH=$(sed -n '3p' VERSION | sed 's/source_branch: //')
if [ -z "$NEW_VERSION" ]; then
echo "Error: New version not found in VERSION file (line 1)"
exit 1
fi
if [ -z "$LAST_VERSION" ]; then
echo "Error: last_version not found in VERSION file (line 2)"
exit 1
fi
echo "New version: $NEW_VERSION"
echo "Last version: $LAST_VERSION"
echo "Source branch: ${SOURCE_BRANCH:-N/A}"
# Build authenticated URL
if [[ "$GIT_URL" == https://* ]]; then
URL_WITHOUT_PROTOCOL="${GIT_URL#https://}"
AUTH_URL="https://${GIT_USER}:${GIT_TOKEN}@${URL_WITHOUT_PROTOCOL}"
elif [[ "$GIT_URL" == http://* ]]; then
URL_WITHOUT_PROTOCOL="${GIT_URL#http://}"
AUTH_URL="http://${GIT_USER}:${GIT_TOKEN}@${URL_WITHOUT_PROTOCOL}"
else
echo "Error: URL must start with http:// or https://"
exit 1
fi
# Create temp directory for cloning
TEMP_DIR=$(mktemp -d)
CURRENT_DIR=$(pwd)
echo ""
echo "Cloning repository from branch '$LAST_VERSION'..."
git clone --branch "$LAST_VERSION" --single-branch "$AUTH_URL" "$TEMP_DIR" 2>/dev/null || {
echo "Branch '$LAST_VERSION' not found, trying to clone default branch..."
git clone "$AUTH_URL" "$TEMP_DIR"
cd "$TEMP_DIR"
git checkout -b "$LAST_VERSION" 2>/dev/null || git checkout "$LAST_VERSION"
cd "$CURRENT_DIR"
}
echo "Copying current files to cloned repository..."
# Copy all files except .git, temp dir, and config file
rsync -av \
--exclude='.git' \
--exclude='test' \
--exclude="$(basename "$TEMP_DIR")" \
--exclude="$CONFIG_FILE" \
"$CURRENT_DIR/" "$TEMP_DIR/"
cd "$TEMP_DIR"
echo "Creating new branch '$NEW_VERSION'..."
git checkout -b "$NEW_VERSION" 2>/dev/null || git checkout "$NEW_VERSION"
echo "Adding changes..."
git add -A
echo "Committing changes..."
COMMIT_MSG="Release $NEW_VERSION"
if [ -n "$SOURCE_BRANCH" ]; then
COMMIT_MSG="$COMMIT_MSG (from $SOURCE_BRANCH)"
fi
git commit -m "$COMMIT_MSG" || echo "Nothing to commit"
echo "Pushing to remote..."
git push -u origin "$NEW_VERSION"
# Cleanup
cd "$CURRENT_DIR"
rm -rf "$TEMP_DIR"
echo ""
echo "Done! Patch applied and pushed to branch '$NEW_VERSION'"
echo "Based on previous version: '$LAST_VERSION'"