-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·112 lines (89 loc) · 3.59 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·112 lines (89 loc) · 3.59 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
#!/bin/bash
# gitmeright setup script
# author: @s403o
# Prompt and validate non-empty input (and validate emails if applicable)
prompt_and_validate() {
local prompt="$1"
local varname="$2"
while true; do
read -p "$prompt" input
if [ -z "$input" ]; then
echo "❌ Error: $varname cannot be empty. Please enter a valid value."
continue
fi
# Email format validation
if [[ "$varname" == *EMAIL* && ! "$input" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
echo "❌ Error: Invalid email format. Please try again."
continue
fi
eval "$varname=\"$input\""
break
done
}
echo "🎉 Welcome to gitmeright setup!"
# Step 1: User Input
prompt_and_validate "🔧 Enter your GitHub username: " GITHUB_USER
prompt_and_validate "👤 Enter your full name for Personal (GitHub): " NAME_PERSONAL
prompt_and_validate "📧 Enter email for Personal: " EMAIL_PERSONAL
prompt_and_validate "🔧 Enter name for Project 1 (e.g., work): " PROJECT1
prompt_and_validate "👤 Enter your full name for $PROJECT1: " NAME_PROJECT1
prompt_and_validate "📧 Enter email for $PROJECT1: " EMAIL_PROJECT1
prompt_and_validate "🔧 Enter name for Project 2 (e.g., freelance): " PROJECT2
prompt_and_validate "👤 Enter your full name for $PROJECT2: " NAME_PROJECT2
prompt_and_validate "📧 Enter email for $PROJECT2: " EMAIL_PROJECT2
# Step 2: Copy main .gitconfig
echo "📁 Copying .gitconfig to ~/.gitconfig..."
cp .gitconfig ~/.gitconfig
# Replace placeholders in ~/.gitconfig
sed -i.bak "s/GITHUB_USERNAME_PLACEHOLDER/$GITHUB_USER/" ~/.gitconfig
sed -i.bak "s/.gitconfig-personal/.gitconfig-personal/" ~/.gitconfig
sed -i.bak "s/project1/$PROJECT1/" ~/.gitconfig
sed -i.bak "s/project2/$PROJECT2/" ~/.gitconfig
rm ~/.gitconfig.bak
# Step 3: Copy project-specific configs
cp .gitconfig-personal ~/.gitconfig-personal
cp .gitconfig-project1 ~/.gitconfig-$PROJECT1
cp .gitconfig-project2 ~/.gitconfig-$PROJECT2
# Step 4: Fill in user.name, user.email, sshCommand
# Personal
sed -i "s/PERSONAL_NAME_PLACEHOLDER/$NAME_PERSONAL/" ~/.gitconfig-personal
sed -i "s/you@personal.com/$EMAIL_PERSONAL/" ~/.gitconfig-personal
sed -i "s/id_rsa_personal/id_rsa_personal/" ~/.gitconfig-personal
# Project 1
sed -i "s/PROJECT1_NAME_PLACEHOLDER/$NAME_PROJECT1/" ~/.gitconfig-$PROJECT1
sed -i "s/you@project1.com/$EMAIL_PROJECT1/" ~/.gitconfig-$PROJECT1
sed -i "s/id_rsa_project1/id_rsa_$PROJECT1/" ~/.gitconfig-$PROJECT1
# Project 2
sed -i "s/PROJECT2_NAME_PLACEHOLDER/$NAME_PROJECT2/" ~/.gitconfig-$PROJECT2
sed -i "s/you@project2.com/$EMAIL_PROJECT2/" ~/.gitconfig-$PROJECT2
sed -i "s/id_rsa_project2/id_rsa_$PROJECT2/" ~/.gitconfig-$PROJECT2
# Step 5: Generate SSH keys if missing
generate_ssh_key() {
local keyname=$1
local email=$2
if [ ! -f "$HOME/.ssh/$keyname" ]; then
echo "🔐 Generating SSH key: $keyname"
ssh-keygen -t ed25519 -C "$email" -f "$HOME/.ssh/$keyname" -N ""
else
echo "✅ SSH key $keyname already exists. Skipping."
fi
}
mkdir -p ~/.ssh
generate_ssh_key "id_rsa_personal" "$EMAIL_PERSONAL"
generate_ssh_key "id_rsa_$PROJECT1" "$EMAIL_PROJECT1"
generate_ssh_key "id_rsa_$PROJECT2" "$EMAIL_PROJECT2"
# Step 6: Add SSH keys to agent
echo "🚀 Adding keys to SSH agent..."
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa_personal
ssh-add ~/.ssh/id_rsa_$PROJECT1
ssh-add ~/.ssh/id_rsa_$PROJECT2
# Step 7: Done!
echo ""
echo "✅ Setup complete! Git identities are now managed by gitmeright 🔥"
echo ""
echo "Test it with:"
echo " git config user.name"
echo " git config user.email"
echo ""
echo "💡 Tip: Use 'git config --list --show-origin' to see where Git gets its config."