-
Notifications
You must be signed in to change notification settings - Fork 314
132 lines (111 loc) · 4.37 KB
/
Copy pathsync-md-files.yml
File metadata and controls
132 lines (111 loc) · 4.37 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
name: Sync MD Files to dexie-web-mui
on:
push:
branches: [gh-pages]
paths: ['**/*.md']
jobs:
sync-md-files:
if: false # Workflow disabled
runs-on: ubuntu-latest
steps:
- name: Checkout source repository
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Get changed MD files
id: changed-files
run: |
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD | grep '\.md$' || true)
if [ -z "$CHANGED_FILES" ]; then
echo "No .md files have been changed"
echo "has_changes=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "Changed .md files:"
echo "$CHANGED_FILES"
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "$CHANGED_FILES" > changed_files.txt
- name: Generate patches for changed files
if: steps.changed-files.outputs.has_changes == 'true'
run: |
mkdir -p patches
while IFS= read -r file; do
if [ -n "$file" ]; then
echo "Creating patch for: $file"
git diff HEAD~1 HEAD -- "$file" > "patches/${file//\//_}.patch"
if [ -s "patches/${file//\//_}.patch" ]; then
echo "Patch created for $file"
else
echo "Empty patch for $file"
rm -f "patches/${file//\//_}.patch"
fi
fi
done < changed_files.txt
ls -la patches/ || echo "No patches were created"
- name: Checkout target repository
if: steps.changed-files.outputs.has_changes == 'true'
uses: actions/checkout@v4
with:
repository: dexie/dexie-web-mui
token: ${{ secrets.TARGET_REPO_TOKEN }}
path: target-repo
- name: Apply patches to target repository
if: steps.changed-files.outputs.has_changes == 'true'
run: |
cd target-repo
git config user.name "GitHub Action"
git config user.email "action@github.com"
APPLIED_PATCHES=()
FAILED_PATCHES=()
for patch_file in ../patches/*.patch; do
if [ -f "$patch_file" ]; then
original_file=$(basename "$patch_file" .patch | tr '_' '/')
echo "Attempting to apply patch for: $original_file"
if [ -f "$original_file" ]; then
if git apply --check "$patch_file" 2>/dev/null; then
git apply "$patch_file"
git add "$original_file"
APPLIED_PATCHES+=("$original_file")
echo "✓ Patch applied for: $original_file"
else
echo "✗ Patch could not be applied for: $original_file"
FAILED_PATCHES+=("$original_file")
if git apply --3way "$patch_file" 2>/dev/null; then
git add "$original_file"
APPLIED_PATCHES+=("$original_file (3-way merge)")
echo "✓ 3-way merge succeeded for: $original_file"
else
echo "✗ 3-way merge also failed for: $original_file"
fi
fi
else
echo "⚠ Target file does not exist: $original_file"
mkdir -p "$(dirname "$original_file")"
if git apply "$patch_file" 2>/dev/null; then
git add "$original_file"
APPLIED_PATCHES+=("$original_file (new file)")
echo "✓ New file created: $original_file"
else
echo "✗ Could not create new file: $original_file"
FAILED_PATCHES+=("$original_file")
fi
fi
fi
done
echo "=== SUMMARY ==="
echo "Applied patches: ${#APPLIED_PATCHES[@]}"
for file in "${APPLIED_PATCHES[@]}"; do
echo " ✓ $file"
done
echo "Failed patches: ${#FAILED_PATCHES[@]}"
for file in "${FAILED_PATCHES[@]}"; do
echo " ✗ $file"
done
if [ ${#APPLIED_PATCHES[@]} -gt 0 ]; then
APPLIED_LIST=$(printf '%s, ' "${APPLIED_PATCHES[@]}" | sed 's/, $//')
git commit -m "Sync MD files from dexie-website - Applied patches for: $APPLIED_LIST - Source commit: ${{ github.sha }}"
git push
echo "Changes committed and pushed!"
else
echo "No changes to commit"
fi