-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTakeover
More file actions
266 lines (218 loc) · 7.52 KB
/
Copy pathTakeover
File metadata and controls
266 lines (218 loc) · 7.52 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
#!/bin/bash
# Mass Repository Merger - Handles hundreds of repos efficiently
# Usage: ./merge-repos.sh repos-list.txt
# For private repos: GITHUB_TOKEN=ghp_xxx ./merge-repos.sh repos-list.txt
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Configuration
MONOREPO_DIR="monorepo"
LOG_DIR="merge-logs"
REPOS_LIST="$1"
PARALLEL_JOBS=10 # Adjust based on your system
BATCH_SIZE=50 # Process in batches to avoid memory issues
# Check for GitHub token (required for private repos)
if [ -z "$GITHUB_TOKEN" ]; then
echo -e "${YELLOW}Warning: GITHUB_TOKEN not set${NC}"
echo "For private repos, run:"
echo " export GITHUB_TOKEN=ghp_your_token_here"
echo " ./merge-repos.sh repos-list.txt"
echo ""
read -p "Continue without token? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
# Validate input
if [ -z "$REPOS_LIST" ]; then
echo -e "${RED}Error: Please provide a repos list file${NC}"
echo "Usage: $0 repos-list.txt"
echo ""
echo "Format of repos-list.txt (one per line):"
echo "https://github.com/user/repo-a.git:projects/repo-a"
echo "https://github.com/user/repo-b.git:services/repo-b"
exit 1
fi
if [ ! -f "$REPOS_LIST" ]; then
echo -e "${RED}Error: File $REPOS_LIST not found${NC}"
exit 1
fi
# Create directories
mkdir -p "$LOG_DIR"
mkdir -p "$MONOREPO_DIR"
# Initialize monorepo if it doesn't exist
if [ ! -d "$MONOREPO_DIR/.git" ]; then
echo -e "${GREEN}Initializing monorepo...${NC}"
cd "$MONOREPO_DIR"
git init
git config user.name "Monorepo Bot"
git config user.email "bot@monorepo.local"
# Create initial commit
echo "# Monorepo" > README.md
echo "Created: $(date)" >> README.md
git add README.md
git commit -m "chore: initialize monorepo"
cd ..
fi
# Count total repos
TOTAL_REPOS=$(wc -l < "$REPOS_LIST")
echo -e "${GREEN}Found $TOTAL_REPOS repositories to merge${NC}"
echo ""
# Function to merge a single repo
merge_repo() {
local repo_info="$1"
local index="$2"
IFS=':' read -r url dir <<< "$repo_info"
local repo_name=$(basename "$url" .git)
local remote_name="temp-${repo_name}-${RANDOM}"
local log_file="$LOG_DIR/${repo_name}.log"
# Convert HTTPS URL to authenticated URL if token exists
if [ -n "$GITHUB_TOKEN" ]; then
# Replace https://github.com with https://TOKEN@github.com
url=$(echo "$url" | sed "s|https://github.com|https://${GITHUB_TOKEN}@github.com|")
fi
echo -e "${YELLOW}[$index/$TOTAL_REPOS] Processing: $repo_name → $dir${NC}"
cd "$MONOREPO_DIR"
{
echo "=== Merging $repo_name at $(date) ==="
# Add remote
if ! git remote add "$remote_name" "$url" 2>&1; then
echo "Warning: Could not add remote, skipping..."
cd ..
return 1
fi
# Fetch with timeout
if ! timeout 300 git fetch "$remote_name" --depth=1 2>&1; then
echo "Error: Fetch timeout or failed"
git remote remove "$remote_name" 2>&1 || true
cd ..
return 1
fi
# Get the default branch name
default_branch=$(git remote show "$remote_name" | grep 'HEAD branch' | cut -d' ' -f5)
if [ -z "$default_branch" ]; then
default_branch="main"
fi
# Create subdirectory if it doesn't exist
mkdir -p "$dir"
# Merge strategy: Create subtree merge
if ! git merge -s ours --no-commit --allow-unrelated-histories "$remote_name/$default_branch" 2>&1; then
echo "Warning: Merge strategy failed, trying alternative..."
git merge --abort 2>&1 || true
fi
# Read tree into subdirectory
if ! git read-tree --prefix="$dir/" -u "$remote_name/$default_branch" 2>&1; then
echo "Error: Could not read tree"
git remote remove "$remote_name" 2>&1 || true
cd ..
return 1
fi
# Commit the merge
git commit -m "feat: merge $repo_name into $dir
Source: $url
Merged: $(date)
Index: $index/$TOTAL_REPOS" 2>&1
# Clean up remote
git remote remove "$remote_name" 2>&1 || true
echo "=== Success: $repo_name merged ==="
} > "$log_file" 2>&1
cd ..
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ $repo_name merged successfully${NC}"
return 0
else
echo -e "${RED}✗ $repo_name failed (check $log_file)${NC}"
return 1
fi
}
export -f merge_repo
export MONOREPO_DIR LOG_DIR TOTAL_REPOS RED GREEN YELLOW NC GITHUB_TOKEN
# Configure git to use token for authentication
if [ -n "$GITHUB_TOKEN" ]; then
cd "$MONOREPO_DIR"
git config credential.helper store
echo "https://${GITHUB_TOKEN}@github.com" > ~/.git-credentials
cd ..
fi
# Process repos in batches
echo -e "${GREEN}Starting merge process in batches of $BATCH_SIZE...${NC}"
echo ""
batch_num=1
success_count=0
fail_count=0
while IFS= read -r line; do
# Skip empty lines and comments
[[ -z "$line" || "$line" =~ ^# ]] && continue
repos_batch+=("$line")
# Process batch when full or at end of file
if [ ${#repos_batch[@]} -eq $BATCH_SIZE ] || [ $(wc -l < "$REPOS_LIST") -eq $((batch_num * BATCH_SIZE)) ]; then
echo -e "${YELLOW}Processing batch $batch_num (${#repos_batch[@]} repos)...${NC}"
# Process batch with parallel jobs
for i in "${!repos_batch[@]}"; do
index=$((batch_num * BATCH_SIZE - BATCH_SIZE + i + 1))
merge_repo "${repos_batch[$i]}" "$index"
if [ $? -eq 0 ]; then
((success_count++))
else
((fail_count++))
fi
# Rate limiting - small delay between repos
sleep 0.5
done
# Push batch to remote (if configured)
if [ -n "$REMOTE_URL" ]; then
echo -e "${YELLOW}Pushing batch $batch_num...${NC}"
cd "$MONOREPO_DIR"
git push origin main 2>&1 | tee -a "$LOG_DIR/push-batch-$batch_num.log"
cd ..
fi
# Clear batch
repos_batch=()
((batch_num++))
# Memory cleanup
echo -e "${YELLOW}Cleaning up...${NC}"
cd "$MONOREPO_DIR"
git gc --auto
cd ..
fi
done < "$REPOS_LIST"
# Final summary
echo ""
echo "========================================"
echo -e "${GREEN}MERGE COMPLETE${NC}"
echo "========================================"
echo "Total repositories: $TOTAL_REPOS"
echo -e "${GREEN}Successful: $success_count${NC}"
echo -e "${RED}Failed: $fail_count${NC}"
echo ""
echo "Logs available in: $LOG_DIR/"
echo "Monorepo location: $MONOREPO_DIR/"
echo ""
# Generate summary report
cat > "$LOG_DIR/summary.txt" <<EOF
Merge Summary - $(date)
======================================
Total Repositories: $TOTAL_REPOS
Successful: $success_count
Failed: $fail_count
Failed repositories (if any):
EOF
if [ $fail_count -gt 0 ]; then
grep -l "Error:" "$LOG_DIR"/*.log | while read log; do
basename "$log" .log >> "$LOG_DIR/summary.txt"
done
fi
echo -e "${GREEN}Summary report: $LOG_DIR/summary.txt${NC}"
# Offer to create GitHub repo
echo ""
echo "Next steps:"
echo "1. Review logs in $LOG_DIR/"
echo "2. Push to GitHub:"
echo " cd $MONOREPO_DIR"
echo " git remote add origin https://github.com/YOUR-ORG/monorepo.git"
echo " git push -u origin main"
echo "3. Archive old repositories"