-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-sync.sh
More file actions
executable file
·222 lines (201 loc) · 6.86 KB
/
git-sync.sh
File metadata and controls
executable file
·222 lines (201 loc) · 6.86 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
#!/bin/bash
# Enhanced GitHub Sync Script
# Automates Git repository synchronization with logging, error handling, and batch processing
LOG_FILE="$HOME/scripts/github-sync.log"
BACKUP_DIR="$HOME/scripts/backups"
SYNC_DIR="$HOME/scripts"
# Create necessary directories if they don't exist
mkdir -p "$BACKUP_DIR"
mkdir -p "$SYNC_DIR"
# Enable colored output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to play alert sound
beep() {
echo -ne "\a"
}
# Function to sync a single repository
sync_repo() {
local repo_dir=$1
cd "$repo_dir" || return
if ! git diff --quiet || ! git diff --cached --quiet; then
echo -e "${YELLOW}⚠️ Uncommitted changes in $repo_dir${NC}"
fi
echo -e "${BLUE}Syncing $repo_dir...${NC}" | tee -a "$LOG_FILE"
tar -czf "$BACKUP_DIR/$(basename "$repo_dir")-backup-$(date +%F).tar.gz" .
git fetch --all 2>&1 | tee -a "$LOG_FILE"
git stash 2>&1 | tee -a "$LOG_FILE"
git pull --rebase 2>&1 | tee -a "$LOG_FILE"
git stash pop 2>&1 | tee -a "$LOG_FILE"
git add . 2>&1 | tee -a "$LOG_FILE"
git commit -m "Auto-sync update" 2>&1 | tee -a "$LOG_FILE"
git push 2>&1 | tee -a "$LOG_FILE"
echo -e "${GREEN}Sync completed for $repo_dir${NC}" | tee -a "$LOG_FILE"
}
# Function to sync all repositories
sync_all_repos() {
for repo in "$SYNC_DIR"/*/.git; do
repo_dir=$(dirname "$repo")
sync_repo "$repo_dir"
done
beep
echo -e "${GREEN}All repositories synced.${NC} Press Enter to return to menu."
read
}
# Function to select repos to sync
select_and_sync_repos() {
echo -e "${YELLOW}Available repositories:${NC}"
repos=()
i=1
for repo in "$SYNC_DIR"/*/.git; do
repo_dir=$(dirname "$repo")
echo "$i) $(basename "$repo_dir")"
repos+=("$repo_dir")
((i++))
done
echo "Enter the numbers of the repos to sync (space-separated):"
read -r selected
for index in $selected; do
if [[ $index =~ ^[0-9]+$ ]] && [ $index -le ${#repos[@]} ]; then
sync_repo "${repos[$((index-1))]}"
else
echo -e "${RED}Invalid selection: $index${NC}"
fi
done
beep
echo -e "${GREEN}Selected repositories synced.${NC} Press Enter to return to menu."
read
}
# Function to clone repos manually
clone_repos() {
echo "Enter SSH clone URLs (one per line). Enter a blank line to finish:"
while true; do
read -r repo_url
[ -z "$repo_url" ] && break
repo_name=$(basename "$repo_url" .git)
git clone "$repo_url" "$SYNC_DIR/$repo_name" | tee -a "$LOG_FILE"
done
beep
echo -e "${GREEN}Manual cloning complete.${NC} Press Enter to return to menu."
read
}
# Function to clone all GitHub account repos and sync them
clone_from_github_account() {
echo "Enter your GitHub username:"
read -r github_user
if [ -f "$HOME/.github-sync-token" ]; then
github_token=$(cat "$HOME/.github-sync-token")
else
echo "Enter your GitHub personal access token (hidden):"
read -rs github_token
echo
echo "$github_token" > "$HOME/.github-sync-token"
chmod 600 "$HOME/.github-sync-token"
fi
echo "Fetching repositories for user $github_user..." | tee -a "$LOG_FILE"
repos=$(curl -s -u "$github_user:$github_token" https://api.github.com/user/repos?per_page=100 | grep ssh_url | cut -d '"' -f 4)
if [ -z "$repos" ]; then
echo -e "${RED}No repositories found or authentication failed.${NC}" | tee -a "$LOG_FILE"
read -p "Press Enter to return to menu."
return
fi
for url in $repos; do
repo_name=$(basename "$url" .git)
target_dir="$SYNC_DIR/$repo_name"
if [ -d "$target_dir/.git" ]; then
echo -e "${YELLOW}$repo_name already exists, skipping clone.${NC}" | tee -a "$LOG_FILE"
else
echo -e "${BLUE}Cloning $repo_name...${NC}" | tee -a "$LOG_FILE"
git clone "$url" "$target_dir" | tee -a "$LOG_FILE"
fi
sync_repo "$target_dir"
done
beep
echo -e "${GREEN}GitHub repositories cloned and synced.${NC} Press Enter to return to menu."
read
}
# Function to check if SSH key exists
check_ssh_key() {
if [ ! -f "$HOME/.ssh/id_rsa" ]; then
echo -e "${YELLOW}No SSH key found. Generating one...${NC}" | tee -a "$LOG_FILE"
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f "$HOME/.ssh/id_rsa" -N "" | tee -a "$LOG_FILE"
eval "$(ssh-agent -s)"
ssh-add "$HOME/.ssh/id_rsa"
echo -e "${BLUE}SSH key generated. Add the following public key to your GitHub account:${NC}" | tee -a "$LOG_FILE"
cat "$HOME/.ssh/id_rsa.pub"
read -p "Press Enter after adding the key to GitHub..."
fi
}
# Function to test SSH connection to GitHub
check_github_access() {
echo -e "${BLUE}Testing SSH connection to GitHub...${NC}" | tee -a "$LOG_FILE"
ssh -T git@github.com 2>&1 | tee -a "$LOG_FILE" | grep -q "successfully authenticated" || {
echo -e "${RED}SSH connection to GitHub failed! Check your SSH setup.${NC}" | tee -a "$LOG_FILE"
exit 1
}
}
# Run setup and checks
check_ssh_key
check_github_access
# Parse arguments
if [[ $1 == "--force" ]]; then
sync_all_repos
exit 0
elif [[ $1 == "--dry-run" ]]; then
echo "Dry run: would sync the following repos:"
for repo in "$SYNC_DIR"/*/.git; do
echo "- $(dirname "$repo")"
done
exit 0
fi
# Interactive menu
while true; do
echo -e "\n${YELLOW}Select an option:${NC}"
options=("Sync All" "Sync Selected Repos" "Sync One Repo" "Clone Repos Manually" "Clone Repos from GitHub Account" "View Log" "Exit")
select opt in "${options[@]}"; do
case $opt in
"Sync All")
sync_all_repos
break
;;
"Sync Selected Repos")
select_and_sync_repos
break
;;
"Sync One Repo")
echo "Enter repository path:"
read -r repo_path
sync_repo "$repo_path"
beep
echo -e "${GREEN}Repository synced.${NC} Press Enter to return to menu."
read
break
;;
"Clone Repos Manually")
clone_repos
break
;;
"Clone Repos from GitHub Account")
clone_from_github_account
break
;;
"View Log")
cat "$LOG_FILE"
echo -e "${BLUE}Press Enter to return to menu.${NC}"
read
break
;;
"Exit")
echo -e "${GREEN}Goodbye!${NC}"
exit 0
;;
*)
echo -e "${RED}Invalid option!${NC}"
break
;;
esac
done
done