103 lines
2.4 KiB
Bash
Executable File
103 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Initializes a git repository and pushes to a remote using credentials from a config file
|
|
#
|
|
# Usage: ./git-init-from-config.sh <config-file>
|
|
#
|
|
# Config file format (one value per line):
|
|
# GIT_URL=https://github.com/user/repo.git
|
|
# GIT_USER=username
|
|
# GIT_TOKEN=your_token_or_password
|
|
#
|
|
# The branch name will be read from the VERSION file (first line)
|
|
|
|
set -e
|
|
|
|
if [ -z "$1" ]; then
|
|
echo "Usage: $0 <config-file>"
|
|
echo ""
|
|
echo "Config file format:"
|
|
echo " GIT_URL=https://github.com/user/repo.git"
|
|
echo " GIT_USER=username"
|
|
echo " GIT_TOKEN=your_token_or_password"
|
|
echo " GIT_BRANCH=main (optional)"
|
|
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 from VERSION file (first line)
|
|
if [ ! -f "VERSION" ]; then
|
|
echo "Error: VERSION file not found in current directory"
|
|
exit 1
|
|
fi
|
|
|
|
GIT_BRANCH=$(head -n 1 VERSION)
|
|
|
|
if [ -z "$GIT_BRANCH" ]; then
|
|
echo "Error: VERSION file is empty"
|
|
exit 1
|
|
fi
|
|
|
|
echo "Version detected: $GIT_BRANCH"
|
|
|
|
# Build authenticated URL
|
|
# Extract protocol and rest of 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
|
|
|
|
echo "Initializing git repository..."
|
|
git init
|
|
|
|
echo "Adding all files..."
|
|
git add .
|
|
|
|
echo "Creating initial commit..."
|
|
git commit -m "Initial commit" || echo "Nothing to commit or already committed"
|
|
|
|
echo "Setting branch to $GIT_BRANCH..."
|
|
git branch -M "$GIT_BRANCH"
|
|
|
|
echo "Adding remote origin..."
|
|
git remote remove origin 2>/dev/null || true
|
|
git remote add origin "$AUTH_URL"
|
|
|
|
echo "Pushing to remote..."
|
|
git push -u origin "$GIT_BRANCH"
|
|
|
|
echo ""
|
|
echo "Done! Repository pushed to $GIT_URL on branch $GIT_BRANCH"
|
|
|