From eb71390037c113beae4149726c3bec91ebe8856c Mon Sep 17 00:00:00 2001 From: David Tomaschik Date: Tue, 29 Sep 2026 12:52:30 -0700 Subject: [PATCH] Add git-setup-remote --- bin/git-setup-remote | 79 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 bin/git-setup-remote diff --git a/bin/git-setup-remote b/bin/git-setup-remote new file mode 100755 index 0000000..51cb810 --- /dev/null +++ b/bin/git-setup-remote @@ -0,0 +1,79 @@ +#!/usr/bin/env bash + +# Exit immediately if a command exits with a non-zero status, +# or if an uninitialized variable is used. +set -euo pipefail + +# Print usage instructions +usage() { + echo "Usage: $0 " + echo "Example: $0 user@server.com /var/www/my-project production" + exit 1 +} + +# 1. Check arguments +if [ "$#" -ne 3 ]; then + echo "❌ Error: Invalid number of arguments." >&2 + usage +fi + +REMOTE_HOST="$1" +REMOTE_PATH="$2" +REMOTE_NAME="$3" + +# 2. Verify we are running inside a local Git repository +if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + echo "❌ Error: This script must be run from within a local Git repository." >&2 + exit 1 +fi + +# Get current branch name (usually 'main' or 'master') +CURRENT_BRANCH=$(git branch --show-current) +if [ -z "$CURRENT_BRANCH" ]; then + echo "❌ Error: Could not determine current branch. Is your repository empty?" >&2 + exit 1 +fi + +# 3. Check SSH connection +echo "🔍 Checking SSH connection to ${REMOTE_HOST}..." +if ! ssh -q -o BatchMode=yes -o ConnectTimeout=5 "$REMOTE_HOST" exit; then + echo "❌ Error: Cannot establish SSH connection to ${REMOTE_HOST}." >&2 + echo "Please check your SSH credentials, host address, or ssh-agent configuration." >&2 + exit 1 +fi + +# 4. Initialize and configure the remote repository over SSH +echo "🚀 Creating remote directory and initializing Git on ${REMOTE_HOST}..." +ssh "$REMOTE_HOST" bash -s <