summaryrefslogtreecommitdiff
path: root/create_remote_repo.sh
diff options
context:
space:
mode:
Diffstat (limited to 'create_remote_repo.sh')
-rwxr-xr-xcreate_remote_repo.sh106
1 files changed, 106 insertions, 0 deletions
diff --git a/create_remote_repo.sh b/create_remote_repo.sh
new file mode 100755
index 0000000..389c09d
--- /dev/null
+++ b/create_remote_repo.sh
@@ -0,0 +1,106 @@
+#!/usr/bin/env bash
+#
+# create_remote_repo.sh
+#
+# Creates a bare Git repository on a remote server (under /var/git, or
+# /var/git-private if --p is passed) via SSH, then adds it as a remote for
+# the current local repo (under the name you choose) and pushes the current
+# branch to it.
+#
+# Usage:
+# ./create_remote_repo.sh [--p] <repo-name> <remote-name>
+#
+# Flags:
+# --p Create the bare repo under /var/git-private instead of /var/git
+#
+# Example:
+# ./create_remote_repo.sh my-project origin
+# -> creates /var/git/my-project.git on the "sourceobby" host
+# -> adds it locally as remote "origin"
+#
+# ./create_remote_repo.sh --p my-secret-project origin
+# -> creates /var/git-private/my-secret-project.git on the "sourceobby" host
+# -> adds it locally as remote "origin"
+#
+# Requirements:
+# - An SSH host alias called "sourceobby" already configured
+# (e.g. in ~/.ssh/config), pointing at the correct IP/user.
+# - The remote account used by "sourceobby" must be able to run
+# `sudo -u git` (passwordless sudo recommended, otherwise you'll be
+# prompted for a sudo password on the remote host).
+# - A 'git' user/account must already exist on the remote server.
+# - Run this script from inside the local git repo you want to push.
+#
+# Note on passwords:
+# This script does NOT store or pass your SSH password anywhere. It simply
+# invokes `ssh`/`git push` normally, so if your "sourceobby" alias is set
+# up for password authentication, you'll be prompted interactively by SSH
+# itself, once for the remote setup and once for the push.
+
+set -euo pipefail
+
+# ---- Argument handling ----
+PRIVATE=false
+POSITIONAL=()
+
+for arg in "$@"; do
+ case "$arg" in
+ --p)
+ PRIVATE=true
+ ;;
+ --)
+ ;;
+ *)
+ POSITIONAL+=("$arg")
+ ;;
+ esac
+done
+
+if [ "${#POSITIONAL[@]}" -ne 2 ]; then
+ echo "Usage: $0 [--p] <repo-name> <remote-name>" >&2
+ exit 1
+fi
+
+REPO_NAME="${POSITIONAL[0]}"
+REMOTE_NAME="${POSITIONAL[1]}"
+REMOTE_ALIAS="git.sourceobby"
+
+if [ "${PRIVATE}" = true ]; then
+ BASE_DIR="/var/git-private"
+else
+ BASE_DIR="/var/git"
+fi
+
+REMOTE_DIR="${BASE_DIR}/${REPO_NAME}.git"
+REMOTE_URL="${REMOTE_ALIAS}:${REMOTE_DIR}"
+
+# ---- Sanity check: are we inside a git repo? ----
+if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
+ echo "Error: this script must be run from inside a local git repository." >&2
+ exit 1
+fi
+
+# ---- Step 1: Create the bare repo on the remote server (as the 'git' user) ----
+# Running as the 'git' user ensures the new repo is owned by the right
+# account and has correct permissions, regardless of which user/account the
+# "sourceobby" SSH alias logs in as.
+echo "Creating bare repo on ${REMOTE_ALIAS}:${REMOTE_DIR} (as user 'git') ..."
+echo "(You may be prompted for your SSH password, and possibly a sudo password.)"
+ssh -t "${REMOTE_ALIAS}" "mkdir -p '${REMOTE_DIR}' && git init --bare '${REMOTE_DIR}'"
+
+# ---- Step 2: Add (or update) the local remote ----
+if git remote get-url "${REMOTE_NAME}" >/dev/null 2>&1; then
+ echo "Remote '${REMOTE_NAME}' already exists locally; updating its URL..."
+ git remote set-url "${REMOTE_NAME}" "${REMOTE_URL}"
+else
+ echo "Adding '${REMOTE_NAME}' remote -> ${REMOTE_URL}"
+ git remote add "${REMOTE_NAME}" "${REMOTE_URL}"
+fi
+
+# ---- Step 3: Push the current branch ----
+CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
+echo "Pushing branch '${CURRENT_BRANCH}' to ${REMOTE_NAME} ..."
+echo "(You may be prompted for your SSH password again.)"
+git push -u "${REMOTE_NAME}" "${CURRENT_BRANCH}"
+
+echo "Done. Remote repo '${REPO_NAME}.git' created and local repo pushed."