summaryrefslogtreecommitdiff
path: root/create_remote_repo.sh
blob: 389c09dc5fa6477f47c8d81cfc32b2e642069672 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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."