-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit
More file actions
executable file
·177 lines (147 loc) · 4.6 KB
/
git
File metadata and controls
executable file
·177 lines (147 loc) · 4.6 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
set -e
usage() {
cat <<EOF
Usage: $0 [OPTIONS]
Options:
-L Setup Git user identity and credential helper (login)
-C Install Gerrit Change-Id commit-msg hook
-A Setup Git aliases
-h Show this help message
You can combine options, e.g. $0 -L -A
EOF
exit 1
}
# Parse flags
FLAG_LOGIN=0
FLAG_HOOK=0
FLAG_ALIAS=0
while getopts "LCAh" opt; do
case $opt in
L) FLAG_LOGIN=1 ;;
C) FLAG_HOOK=1 ;;
A) FLAG_ALIAS=1 ;;
h) usage ;;
*) usage ;;
esac
done
if [[ $FLAG_LOGIN -eq 0 && $FLAG_HOOK -eq 0 && $FLAG_ALIAS -eq 0 ]]; then
usage
fi
# --- FUNCTIONS ---
check_dependencies() {
for cmd in git gh jq; do
if ! command -v "$cmd" &> /dev/null; then
echo "❌ Required command '$cmd' is not installed."
case $cmd in
gh) echo "👉 Install GitHub CLI: https://cli.github.com/" ;;
jq) echo "👉 Install jq (Linux): sudo apt install jq" ;;
git) echo "👉 Install Git: https://git-scm.com/downloads" ;;
esac
exit 1
fi
done
}
setup_login() {
echo "=============================================="
echo " Git Identity & Credential Setup with GitHub CLI"
echo "=============================================="
echo -e "\n🔐 Logging into GitHub..."
if ! gh auth login; then
echo "❌ GitHub authentication failed. Exiting."
exit 1
fi
echo "✅ GitHub authentication successful."
echo -e "\n📦 Fetching GitHub user info..."
GH_USER_JSON=$(gh api user)
GH_USERNAME=$(echo "$GH_USER_JSON" | jq -r '.login')
GH_EMAIL=$(echo "$GH_USER_JSON" | jq -r '.email')
if [[ "$GH_EMAIL" == "null" || -z "$GH_EMAIL" ]]; then
echo "⚠️ Your GitHub email is not publicly visible."
read -rp "Please enter the email you want to use for Git commits: " GH_EMAIL
fi
echo -e "\nGit will be configured with the following identity:"
echo " Username: $GH_USERNAME"
echo " Email: $GH_EMAIL"
read -rp "Continue with these settings? (y/n): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "❌ Setup aborted by user."
exit 1
fi
git config --global user.name "$GH_USERNAME"
git config --global user.email "$GH_EMAIL"
echo "✅ Git user identity has been set."
echo -e "\nChoose Git credential helper:"
echo " 1) GitHub CLI (recommended)"
echo " 2) Git Credential Manager (manager-core)"
read -rp "Enter choice [1 or 2]: " HELPER_CHOICE
case "$HELPER_CHOICE" in
2)
git config --global credential.helper manager-core
echo "✅ Git credential helper set to: manager-core"
;;
*)
git config --global credential.helper '!gh auth git-credential'
echo "✅ Git credential helper set to: GitHub CLI"
;;
esac
echo -e "\n🎉 Setup complete! Here is your current Git config:"
echo " User Name: $(git config --global user.name)"
echo " User Email: $(git config --global user.email)"
echo " Credential Helper: $(git config --global credential.helper)"
}
setup_aliases() {
echo "==============================="
echo "🔧 Setting up Git Aliases"
echo "==============================="
declare -A aliases=(
[c]="commit -s"
[cam]="commit --amend"
[cm]="commit"
[csm]="commit -s -m"
[ca]="cherry-pick --abort"
[cr]="cherry-pick --signoff"
[p]="push -f"
[cc]="cherry-pick --continue"
[cs]="cherry-pick --skip"
[cp]="cherry-pick"
[r]="revert"
[rc]="revert --continue"
[ro]="remote rm origin"
[ra]="remote add origin"
[s]="switch -c"
[b]="branch"
[rh]="reset --hard"
[ch]="checkout"
[f]="fetch"
[m]="merge"
)
for key in "${!aliases[@]}"; do
git config --global alias."$key" "${aliases[$key]}"
echo " ✔ alias.$key = ${aliases[$key]}"
done
echo "✅ All Git aliases have been configured."
}
setup_commit_hook() {
HOOKS_DIR="$HOME/.githooks"
echo "🔧 Installing Gerrit Change-Id commit-msg hook globally..."
mkdir -p "$HOOKS_DIR"
if curl -fsSL -o "$HOOKS_DIR/commit-msg" https://gerrit-review.googlesource.com/tools/hooks/commit-msg; then
chmod +x "$HOOKS_DIR/commit-msg"
git config --global core.hooksPath "$HOOKS_DIR"
echo "✅ Gerrit Change-Id commit-msg hook installed and configured globally."
else
echo "❌ Failed to download Gerrit commit-msg hook."
exit 1
fi
}
# --- MAIN EXECUTION ---
# Check dependencies if login or aliases or hook needed (all require git at least)
if (( FLAG_LOGIN || FLAG_ALIAS || FLAG_HOOK )); then
check_dependencies
fi
# Run requested setups
(( FLAG_LOGIN )) && setup_login
(( FLAG_ALIAS )) && setup_aliases
(( FLAG_HOOK )) && setup_commit_hook
echo -e "\nAll requested setups are done. 🎉"