-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmygit-cli.sh
More file actions
315 lines (284 loc) · 10.9 KB
/
mygit-cli.sh
File metadata and controls
315 lines (284 loc) · 10.9 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/bin/bash
# =============================================================
# GitHub Private Repo - Klon & Yönetim Scripti
# Önce github-setup.sh ile kurulumu tamamla.
#
# Kullanım:
# ./mygit-cli.sh <repo-url> [dizin]
#
# Örnekler:
# ./mygit-cli.sh git@github.com:kullanici/repo.git
# ./mygit-cli.sh git@github.com:kullanici/repo.git /mnt/gitrepos
#
# Repo zaten klonlanmışsa pull/push/fetch menüsü açılır.
# =============================================================
set -e
# ── Renkler ──
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${CYAN}[BİLGİ]${NC} $1"; }
ok() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[UYARI]${NC} $1"; }
err() { echo -e "${RED}[HATA]${NC} $1"; }
show_menu() {
echo ""
echo -e "${BOLD}$1${NC}"
echo "──────────────────────────────────────"
shift
local i=1
for opt in "$@"; do
echo " $i) $opt"
((i++))
done
echo ""
}
# ── Kullanım bilgisi ──
if [ -z "$1" ]; then
echo ""
echo "Kullanım: $0 <repo-ssh-url> [hedef-dizin]"
echo ""
echo "Örnekler:"
echo " $0 git@github.com:kullanici/repo.git"
echo " $0 git@github.com:kullanici/repo.git /mnt/gitrepos"
echo ""
exit 1
fi
REPO_URL="$1"
CLONE_DIR="$2"
REPO_NAME=$(basename "$REPO_URL" .git)
# ── SSH agent kontrolü ──
# systemd user servisi varsa onun soketini kullan
if [ -S "$XDG_RUNTIME_DIR/ssh-agent.socket" ]; then
export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"
fi
# Agent yoksa başlat
if [ -z "$SSH_AUTH_SOCK" ]; then
info "ssh-agent başlatılıyor..."
eval "$(ssh-agent -s)" > /dev/null 2>&1
fi
# Key önbellekte mi kontrol et
if ssh-add -l &>/dev/null; then
ok "SSH key önbellekte, parola sorulmayacak."
else
warn "SSH key önbellekte değil (süre dolmuş veya ilk kullanım)."
info "Parolanı bir kez gir, tekrar önbelleğe alınacak."
echo ""
KEY_ADDED=false
for KEY in "$HOME/.ssh/id_ed25519" "$HOME/.ssh/id_rsa" "$HOME/.ssh/id_ecdsa" \
"$HOME/.ssh/id_ed25519_github" "$HOME/.ssh/id_rsa_github" "$HOME/.ssh/id_ecdsa_github"; do
if [ -f "$KEY" ]; then
if ssh-add "$KEY"; then
ok "Key eklendi: $KEY"
KEY_ADDED=true
break
fi
fi
done
if [ "$KEY_ADDED" != true ]; then
err "Hiçbir SSH key eklenemedi. Önce github-setup.sh çalıştır."
exit 1
fi
fi
# ── Hedef dizini belirle ──
if [ -n "$CLONE_DIR" ]; then
TARGET="$CLONE_DIR/$REPO_NAME"
else
TARGET="$(pwd)/$REPO_NAME"
fi
# ═══════════════════════════════════════════════════════════════
# REPO ZATEN VARSA → YÖNETIM MENÜSÜ
# ═══════════════════════════════════════════════════════════════
if [ -d "$TARGET/.git" ]; then
warn "Bu repo zaten klonlanmış: $TARGET"
cd "$TARGET"
BRANCH=$(git branch --show-current 2>/dev/null || echo "HEAD")
REMOTE=$(git remote get-url origin 2>/dev/null || echo "bilinmiyor")
echo ""
echo -e " Dal : ${GREEN}$BRANCH${NC}"
echo -e " Remote : ${CYAN}$REMOTE${NC}"
# ── Ana menü ──
show_menu "Ne yapmak istersin?" \
"Pull - Uzak repodan değişiklikleri çek" \
"Push - Yerel değişiklikleri uzak repoya gönder" \
"Fetch - Uzak repo bilgilerini güncelle (dosyalara dokunmaz)"
read -rp "Seçimin (1/2/3): " MAIN_CHOICE
case "$MAIN_CHOICE" in
# ──────────────────────────────────────
# PULL
# ──────────────────────────────────────
1)
show_menu "Pull Yöntemi" \
"Normal Pull - Değişiklikleri çek ve birleştir (merge)" \
"Pull --rebase - Değişiklikleri çek ve yerel commitleri üste al" \
"Pull (belirli dal) - Başka bir daldan çek" \
"Force Pull - Yerel değişiklikleri SİL, uzak repoya sıfırla"
read -rp "Seçimin (1/2/3/4): " PULL_CHOICE
case "$PULL_CHOICE" in
1)
info "Normal pull yapılıyor..."
git pull origin "$BRANCH"
ok "Pull tamamlandı."
;;
2)
info "Rebase ile pull yapılıyor..."
git pull --rebase origin "$BRANCH"
ok "Pull (rebase) tamamlandı."
;;
3)
read -rp "Hangi daldan çekilsin? (örn: main, dev): " TARGET_BRANCH
if [ -z "$TARGET_BRANCH" ]; then
err "Dal adı boş olamaz."
exit 1
fi
info "$TARGET_BRANCH dalından pull yapılıyor..."
git pull origin "$TARGET_BRANCH"
ok "Pull ($TARGET_BRANCH) tamamlandı."
;;
4)
warn "DİKKAT: Tüm yerel değişiklikler silinecek!"
read -rp "Emin misin? (evet/hayır): " CONFIRM
if [ "$CONFIRM" = "evet" ]; then
info "Force pull yapılıyor..."
git fetch origin
git reset --hard "origin/$BRANCH"
git clean -fd
ok "Force pull tamamlandı. Yerel repo uzak repo ile aynı."
else
info "İptal edildi."
fi
;;
*)
err "Geçersiz seçim."
exit 1
;;
esac
;;
# ──────────────────────────────────────
# PUSH
# ──────────────────────────────────────
2)
show_menu "Push Yöntemi" \
"Normal Push - Yerel commitleri uzak repoya gönder" \
"Push (belirli dal) - Başka bir dala gönder" \
"Push --set-upstream - Yeni dalı uzak repoya ilk kez gönder" \
"Force Push - Uzak repoyu yerel ile zorla değiştir" \
"Force Push (güvenli) - Başkası push yapmadıysa zorla değiştir"
read -rp "Seçimin (1/2/3/4/5): " PUSH_CHOICE
case "$PUSH_CHOICE" in
1)
info "Normal push yapılıyor..."
git push origin "$BRANCH"
ok "Push tamamlandı."
;;
2)
read -rp "Hangi dala gönderilsin? (örn: main, dev): " TARGET_BRANCH
if [ -z "$TARGET_BRANCH" ]; then
err "Dal adı boş olamaz."
exit 1
fi
info "$TARGET_BRANCH dalına push yapılıyor..."
git push origin "$TARGET_BRANCH"
ok "Push ($TARGET_BRANCH) tamamlandı."
;;
3)
info "Yeni dal uzak repoya gönderiliyor..."
git push --set-upstream origin "$BRANCH"
ok "Dal '$BRANCH' uzak repoya bağlandı ve gönderildi."
;;
4)
warn "DİKKAT: Uzak repodaki değişiklikler kaybolabilir!"
read -rp "Emin misin? (evet/hayır): " CONFIRM
if [ "$CONFIRM" = "evet" ]; then
info "Force push yapılıyor..."
git push --force origin "$BRANCH"
ok "Force push tamamlandı."
else
info "İptal edildi."
fi
;;
5)
info "Güvenli force push yapılıyor..."
warn "Başkası push yapmışsa işlem reddedilecek."
git push --force-with-lease origin "$BRANCH"
ok "Force push (güvenli) tamamlandı."
;;
*)
err "Geçersiz seçim."
exit 1
;;
esac
;;
# ──────────────────────────────────────
# FETCH
# ──────────────────────────────────────
3)
show_menu "Fetch Yöntemi" \
"Normal Fetch - Uzak repo bilgilerini güncelle" \
"Fetch All - Tüm remote'lardan güncelle" \
"Fetch + Prune - Güncelle ve silinmiş dalları temizle" \
"Fetch (belirli dal) - Sadece belirli bir dalın bilgisini çek"
read -rp "Seçimin (1/2/3/4): " FETCH_CHOICE
case "$FETCH_CHOICE" in
1)
info "Fetch yapılıyor..."
git fetch origin
ok "Fetch tamamlandı."
;;
2)
info "Tüm remote'lardan fetch yapılıyor..."
git fetch --all
ok "Fetch (all) tamamlandı."
;;
3)
info "Fetch + prune yapılıyor..."
git fetch --prune origin
ok "Fetch + prune tamamlandı. Silinmiş uzak dallar temizlendi."
;;
4)
read -rp "Hangi dal? (örn: main, dev): " TARGET_BRANCH
if [ -z "$TARGET_BRANCH" ]; then
err "Dal adı boş olamaz."
exit 1
fi
info "$TARGET_BRANCH dalı fetch ediliyor..."
git fetch origin "$TARGET_BRANCH"
ok "Fetch ($TARGET_BRANCH) tamamlandı."
;;
*)
err "Geçersiz seçim."
exit 1
;;
esac
;;
*)
err "Geçersiz seçim."
exit 1
;;
esac
# ── Son durum özeti ──
echo ""
echo "──────────────────────────────────────"
echo -e "${BOLD} Son Durum${NC}"
echo "──────────────────────────────────────"
echo -e " Dal : ${GREEN}$(git branch --show-current)${NC}"
echo -e " Son commit : $(git log --oneline -1 2>/dev/null)"
echo "──────────────────────────────────────"
echo ""
exit 0
fi
# ═══════════════════════════════════════════════════════════════
# REPO YOK → KLONLA
# ═══════════════════════════════════════════════════════════════
if [ -n "$CLONE_DIR" ]; then
mkdir -p "$CLONE_DIR"
cd "$CLONE_DIR"
info "Hedef dizin: $CLONE_DIR"
fi
info "Klonlanıyor: $REPO_URL"
git clone "$REPO_URL"
ok "Tamamlandı! Repo: $TARGET"
echo ""