-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·228 lines (202 loc) · 6.65 KB
/
update.sh
File metadata and controls
executable file
·228 lines (202 loc) · 6.65 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
#!/bin/bash
set -e
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <child_project_path> <last_version_tag>"
exit 1
fi
CHILD_PATH="$1"
LAST_TAG="$2"
ROOT_PATH="$(pwd)"
if [ ! -d "$CHILD_PATH" ]; then
echo "Child project path does not exist: $CHILD_PATH"
exit 1
fi
# 1. Get changed files
CHANGED=$(git diff --name-status "$LAST_TAG" HEAD)
# 2. Prepare lists
ADDED_MODIFIED=()
DELETED=()
RENAMED=()
while IFS= read -r line; do
STATUS=$(echo "$line" | awk '{print $1}')
if [[ "$STATUS" == "R"* ]]; then
# For renamed files, the format is "R<similarity> old_file new_file"
OLD_FILE=$(echo "$line" | awk '{print $2}')
NEW_FILE=$(echo "$line" | awk '{print $3}')
if [[ "$OLD_FILE" == UPGRADE*.md || "$OLD_FILE" == "update.sh" ]]; then
continue
fi
RENAMED+=("$OLD_FILE|$NEW_FILE")
elif [[ "$STATUS" == "A" || "$STATUS" == "M" ]]; then
FILE=$(echo "$line" | awk '{print $2}')
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
ADDED_MODIFIED+=("$FILE")
elif [[ "$STATUS" == "D" ]]; then
FILE=$(echo "$line" | awk '{print $2}')
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
DELETED+=("$FILE")
fi
done <<< "$CHANGED"
# 3. Handle renamed files first
for RENAME in "${RENAMED[@]}"; do
OLD_FILE=$(echo "$RENAME" | cut -d'|' -f1)
NEW_FILE=$(echo "$RENAME" | cut -d'|' -f2)
OLD_PATH="$CHILD_PATH/$OLD_FILE"
NEW_PATH="$CHILD_PATH/$NEW_FILE"
# Always copy the new file from the main repo to the new location in the child repo
mkdir -p "$(dirname "$NEW_PATH")"
cp "$NEW_FILE" "$NEW_PATH"
echo "Copied $NEW_FILE to $NEW_PATH in child repository (rename from $OLD_FILE)"
# If the old file exists in the child repo, delete it
if [ -f "$OLD_PATH" ]; then
rm "$OLD_PATH"
echo "Deleted old file $OLD_PATH in child repository (renamed to $NEW_FILE)"
fi
done
# 4. Copy new/modified files
SAFE_FILES=()
for FILE in "${ADDED_MODIFIED[@]}"; do
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
DEST="$CHILD_PATH/$FILE"
# Check if the file is new in the child repo
if [ ! -f "$DEST" ]; then
SAFE_FILES+=("$FILE")
fi
mkdir -p "$(dirname "$DEST")"
cp "$FILE" "$DEST"
echo "Copied $FILE to $DEST"
done
# 5. Delete removed files
for FILE in "${DELETED[@]}"; do
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
TARGET="$CHILD_PATH/$FILE"
if [ -f "$TARGET" ]; then
rm "$TARGET"
echo "Deleted $TARGET"
fi
done
# 6. Compare diffs and report
# Get the latest tag for the 'to' version
TO_TAG=$(git describe --tags --abbrev=0)
REPORT_NAME="update_report_${LAST_TAG}_to_${TO_TAG}.txt"
REPORT="$CHILD_PATH/$REPORT_NAME"
> "$REPORT"
# Compare added/modified files
for FILE in "${ADDED_MODIFIED[@]}"; do
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
# Generate unified diff in main repo
git diff "$LAST_TAG" -- "$FILE" | grep -v '^index' | sed 's/[[:space:]]*$//' > /tmp/main_diff
# Generate unified diff in child repo
pushd "$CHILD_PATH" > /dev/null
git diff -- "$FILE" | grep -v '^index' | sed 's/[[:space:]]*$//' > /tmp/child_diff
popd > /dev/null
# Compare the diffs byte-for-byte
if ! diff -q /tmp/main_diff /tmp/child_diff > /dev/null; then
echo "$FILE" >> "$REPORT"
fi
rm -f /tmp/main_diff /tmp/child_diff
done
# Compare renamed files
for RENAME in "${RENAMED[@]}"; do
OLD_FILE=$(echo "$RENAME" | cut -d'|' -f1)
NEW_FILE=$(echo "$RENAME" | cut -d'|' -f2)
if [[ "$OLD_FILE" == UPGRADE*.md || "$OLD_FILE" == "update.sh" ]]; then
continue
fi
# Generate unified diff in main repo for the rename
git diff "$LAST_TAG" -- "$NEW_FILE" | grep -v '^index' | sed 's/[[:space:]]*$//' > /tmp/main_diff
# Generate unified diff in child repo for the new file
pushd "$CHILD_PATH" > /dev/null
git diff -- "$NEW_FILE" | grep -v '^index' | sed 's/[[:space:]]*$//' > /tmp/child_diff
popd > /dev/null
# Compare the diffs byte-for-byte
if ! diff -q /tmp/main_diff /tmp/child_diff > /dev/null; then
echo "$NEW_FILE (renamed from $OLD_FILE)" >> "$REPORT"
fi
rm -f /tmp/main_diff /tmp/child_diff
done
# Compare deleted files
for FILE in "${DELETED[@]}"; do
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
# Generate unified diff in main repo for deleted file
git diff "$LAST_TAG" -- "$FILE" | grep -v '^index' | sed 's/[[:space:]]*$//' > /tmp/main_diff
# Generate unified diff in child repo for deleted file
pushd "$CHILD_PATH" > /dev/null
git diff -- "$FILE" | grep -v '^index' | sed 's/[[:space:]]*$//' > /tmp/child_diff
popd > /dev/null
# Compare the diffs byte-for-byte
if ! diff -q /tmp/main_diff /tmp/child_diff > /dev/null; then
echo "$FILE" >> "$REPORT"
fi
rm -f /tmp/main_diff /tmp/child_diff
done
if [ -s "$REPORT" ]; then
echo "Update report created: $REPORT"
cat "$REPORT"
else
echo "All files updated and verified. No mismatches found."
rm "$REPORT"
fi
# Update README.md in the child project
README="$CHILD_PATH/README.md"
VERSION_LINE="This is based on the core of Athenia version number $TO_TAG"
if [ -f "$README" ]; then
if grep -q "This is based on the core of Athenia version number" "$README"; then
sed -i '' "s|This is based on the core of Athenia version number.*|$VERSION_LINE|" "$README"
else
echo "$VERSION_LINE" >> "$README"
fi
else
echo "$VERSION_LINE" > "$README"
fi
# Git add all updated files not in the report
pushd "$CHILD_PATH" > /dev/null
REPORTED=()
if [ -f "$REPORT_NAME" ]; then
while IFS= read -r line; do
REPORTED+=("$line")
done < "$REPORT_NAME"
fi
should_add() {
local file="$1"
for reported in "${REPORTED[@]}"; do
if [[ "$file" == "$reported" ]]; then
return 1
fi
done
return 0
}
for FILE in "${ADDED_MODIFIED[@]}"; do
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
should_add "$FILE" && git add "$FILE"
done
for FILE in "${DELETED[@]}"; do
if [[ "$FILE" == UPGRADE*.md || "$FILE" == "update.sh" ]]; then
continue
fi
if [ ! -f "$FILE" ]; then
should_add "$FILE" && git rm --cached "$FILE" 2>/dev/null || true
fi
done
popd > /dev/null
# Stage safe files for commit
echo "Staging safe files for commit..."
cd "$CHILD_PATH"
for FILE in "${SAFE_FILES[@]}"; do
git add "$FILE"
done
git add README.md