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
#!/bin/sh
# merge-repos.sh - Merge multiple git repos, keep the latest, move others
#
# FIXME AI generated, human review not done yet, use with caution
TARGET_BRANCH="main" # Change to your branch name
DELETE_FOLDER="deleted_repos"
if [ $# -lt 2 ]; then
echo "Usage: $0 repo1 repo2 ..."
exit 1
fi
# Function to get the latest commit timestamp of a branch
get_latest_commit_time() {
repo_path="$1"
branch="$2"
(cd "$repo_path" && git log -1 --format=%ct "$branch" 2>/dev/null || echo "0")
}
# Function to get directory mtime (fallback)
get_dir_mtime() {
stat -c %Y "$1" 2>/dev/null || echo "0"
}
# Find the latest repository
LATEST_REPO=""
LATEST_TIME=0
echo "Analyzing repositories to find the latest one..."
for repo in "$@"; do
if [ ! -d "$repo/.git" ]; then
echo "Warning: $repo is not a git repository, skipping"
continue
fi
# Try commit time first, fall back to mtime
commit_time=$(get_latest_commit_time "$repo" "$TARGET_BRANCH")
if [ "$commit_time" = "0" ]; then
echo " $repo: No commits on $TARGET_BRANCH, using mtime"
current_time=$(get_dir_mtime "$repo")
else
echo " $repo: Last commit $commit_time"
current_time=$commit_time
fi
if [ $current_time -gt $LATEST_TIME ]; then
LATEST_TIME=$current_time
LATEST_REPO="$repo"
fi
done
if [ -z "$LATEST_REPO" ]; then
echo "Error: No valid repositories found"
exit 1
fi
echo "Latest repository: $LATEST_REPO (timestamp: $LATEST_TIME)"
# Create delete folder
mkdir -p "$DELETE_FOLDER"
# Process each repository
for repo in "$@"; do
if [ "$repo" = "$LATEST_REPO" ] || [ ! -d "$repo/.git" ]; then
continue
fi
echo "Processing $repo..."
# Add as remote to latest repo
repo_name=$(basename "$repo")
remote_name="temp_$repo_name"
(cd "$LATEST_REPO" &&
git remote add "$remote_name" "../$repo" 2>/dev/null &&
git fetch "$remote_name" "$TARGET_BRANCH" 2>/dev/null)
if [ $? -ne 0 ]; then
echo " Failed to fetch from $repo, skipping"
continue
fi
# Check if there are new commits
new_commits=$(cd "$LATEST_REPO" && git log "$TARGET_BRANCH..$remote_name/$TARGET_BRANCH" --oneline 2>/dev/null | wc -l)
if [ "$new_commits" -gt 0 ]; then
echo " Found $new_commits new commits"
# Create patch files
patch_dir="patches_$repo_name"
mkdir -p "$patch_dir"
(cd "$LATEST_REPO" &&
git format-patch -o "../$patch_dir" "$TARGET_BRANCH..$remote_name/$TARGET_BRANCH")
echo " Patches saved to $patch_dir"
# Apply patches
(cd "$LATEST_REPO" &&
git checkout "$TARGET_BRANCH" &&
git am "../$patch_dir"/*.patch)
if [ $? -eq 0 ]; then
echo " Successfully merged commits from $repo"
else
echo " Merge conflicts detected! Manual resolution needed"
echo " Aborting patch application"
(cd "$LATEST_REPO" && git am --abort 2>/dev/null)
fi
else
echo " No new commits found in $repo"
fi
# Move processed repo to delete folder
mv "$repo" "$DELETE_FOLDER/"
echo " Moved $repo to $DELETE_FOLDER/"
done
echo "Done! Latest repository: $LATEST_REPO"