-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-git-remote.sh
More file actions
executable file
·128 lines (113 loc) · 3.87 KB
/
Copy pathsetup-git-remote.sh
File metadata and controls
executable file
·128 lines (113 loc) · 3.87 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# Setup git remote for learning workspace repo
# Usage: ./setup-git-remote.sh [your-github-username]
set -e
# Load configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" 2>/dev/null && pwd || pwd)"
LEARNING_DIR="$(cd "$SCRIPT_DIR/.." 2>/dev/null && pwd || pwd)"
source "$SCRIPT_DIR/config-loader.sh"
# Colors
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
cd "$LEARNING_DIR"
if [ ! -d .git ]; then
echo -e "${RED}Error: Not a git repository${NC}"
echo "Run: git init"
exit 1
fi
# Get GitHub username
if [ -n "$1" ]; then
GITHUB_USER="$1"
else
# Try to get from config, git config, or GitHub CLI
GITHUB_USER="${YOUR_GITHUB_USER:-$(git config user.name 2>/dev/null || gh api user --jq .login 2>/dev/null || echo "")}"
if [ -z "$GITHUB_USER" ]; then
echo -e "${YELLOW}Enter your GitHub username:${NC}"
read -r GITHUB_USER
else
echo -e "${BLUE}Detected GitHub user: $GITHUB_USER${NC}"
echo -e "${YELLOW}Is this correct? (y/n)${NC}"
read -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${YELLOW}Enter your GitHub username:${NC}"
read -r GITHUB_USER
fi
fi
fi
REPO_NAME=$(basename "$LEARNING_DIR")
REMOTE_URL="git@github.com:$GITHUB_USER/$REPO_NAME.git"
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Setting up Git Remote${NC}"
echo -e "${GREEN}========================================${NC}\n"
echo -e "${BLUE}Repository:${NC} $REPO_NAME"
echo -e "${BLUE}GitHub User:${NC} $GITHUB_USER"
echo -e "${BLUE}Remote URL:${NC} $REMOTE_URL"
echo ""
# Check if remote already exists
if git remote | grep -q "origin"; then
CURRENT_URL=$(git remote get-url origin)
echo -e "${YELLOW}⚠️ Remote 'origin' already exists:${NC}"
echo -e " $CURRENT_URL"
echo ""
if [[ "$CURRENT_URL" == *"${UPSTREAM_ORG}/"* ]] && [[ "$CURRENT_URL" != *"${YOUR_GITHUB_USER}"* ]]; then
echo -e "${RED}✗ SAFETY: Current remote points to ${UPSTREAM_ORG} organization!${NC}"
echo -e "${YELLOW}This is blocked for safety.${NC}"
echo ""
echo -e "${YELLOW}Update remote? (y/n)${NC}"
read -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git remote set-url origin "$REMOTE_URL"
echo -e "${GREEN}✓ Remote updated${NC}"
else
echo "Cancelled."
exit 0
fi
elif [[ "$CURRENT_URL" == "$REMOTE_URL" ]]; then
echo -e "${GREEN}✓ Remote is already correctly configured${NC}"
else
echo -e "${YELLOW}Update remote to $REMOTE_URL? (y/n)${NC}"
read -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git remote set-url origin "$REMOTE_URL"
echo -e "${GREEN}✓ Remote updated${NC}"
else
echo "Keeping existing remote."
fi
fi
else
echo -e "${YELLOW}Add remote 'origin'? (y/n)${NC}"
read -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
git remote add origin "$REMOTE_URL"
echo -e "${GREEN}✓ Remote added${NC}"
else
echo "Cancelled."
exit 0
fi
fi
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Remote Configuration${NC}"
echo -e "${GREEN}========================================${NC}\n"
git remote -v
echo ""
echo -e "${BLUE}Next Steps:${NC}"
echo -e " 1. Create the repository on GitHub:"
echo -e " ${CYAN}Visit: https://github.com/new${NC}"
echo -e " ${CYAN}Name: $REPO_NAME${NC}"
echo -e " ${CYAN}Don't initialize with README (we already have one)${NC}"
echo ""
echo -e " 2. Add and commit your files:"
echo -e " ${GREEN}git add .${NC}"
echo -e " ${GREEN}git commit -m 'Initial commit: ${UPSTREAM_ORG} learning workspace'${NC}"
echo ""
echo -e " 3. Push safely with checks:"
echo -e " ${GREEN}epush${NC}"
echo ""