Skip to content

Commit 469b54d

Browse files
authored
sync-common: Rewrite sync logic in Rust with unit tests (#49)
Replace the bash script implementation with a testable Rust program that handles file synchronization using git history to track deletions. The Rust implementation: - Uses xshell for ergonomic subprocess handling - Provides proper error handling with anyhow - Is fully unit tested with 11 tests (6 unit + 5 integration) - Uses git to find deleted files instead of maintaining a manifest - Uses rsync to forcibly overwrite existing files and create directories The workflow now: 1. Builds the sync-common tool using cargo 2. Runs the tool to sync files from infra/common/ to target repositories 3. Only syncs when there are actual changes This fixes the original patch-based approach which failed when creating files in non-existent directories or overwriting existing files. Assisted-by: Claude Code (Sonnet 4.5) Signed-off-by: Colin Walters <walters@verbum.org>
1 parent ad66814 commit 469b54d

File tree

5 files changed

+643
-36
lines changed

5 files changed

+643
-36
lines changed

.github/workflows/sync-common.yml

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ on:
1212
paths:
1313
- 'common/**'
1414
- '.github/workflows/sync-common.yml'
15+
- 'scripts/sync-common/**'
1516

1617
# Prevent multiple workflow runs from racing
1718
concurrency: ${{ github.workflow }}
@@ -20,6 +21,26 @@ permissions:
2021
contents: read
2122

2223
jobs:
24+
build:
25+
name: Build sync-common tool
26+
runs-on: ubuntu-latest
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v5
30+
31+
- name: Setup Rust
32+
uses: ./.github/actions/setup-rust
33+
34+
- name: Build sync-common
35+
run: cargo build --release --manifest-path scripts/sync-common/Cargo.toml
36+
37+
- name: Upload sync-common binary
38+
uses: actions/upload-artifact@v4
39+
with:
40+
name: sync-common
41+
path: scripts/sync-common/target/release/sync-common
42+
retention-days: 1
43+
2344
init:
2445
name: Discover repositories
2546
runs-on: ubuntu-latest
@@ -73,7 +94,7 @@ jobs:
7394
7495
sync:
7596
name: Sync to ${{ matrix.repo }}
76-
needs: init
97+
needs: [build, init]
7798
if: needs.init.outputs.matrix != '[]'
7899
runs-on: ubuntu-latest
79100
strategy:
@@ -103,43 +124,16 @@ jobs:
103124
token: ${{ steps.token.outputs.token }}
104125
path: repo
105126

127+
- name: Download sync-common binary
128+
uses: actions/download-artifact@v4
129+
with:
130+
name: sync-common
131+
path: .
132+
106133
- name: Sync common files to repository
107134
run: |
108-
cd infra
109-
CURRENT_COMMIT="${{ github.sha }}"
110-
111-
# Get the last synced commit from target repo
112-
if [ -f "../repo/.bootc-dev-infra-commit.txt" ]; then
113-
PREVIOUS_COMMIT=$(cat ../repo/.bootc-dev-infra-commit.txt)
114-
echo "Previous sync: $PREVIOUS_COMMIT"
115-
echo "Current commit: $CURRENT_COMMIT"
116-
117-
# Generate patch for changes to common/ directory
118-
# Strip the 'common/' prefix so patch applies to repo root
119-
git diff "$PREVIOUS_COMMIT" "$CURRENT_COMMIT" -- common/ | \
120-
sed 's|a/common/|a/|g; s|b/common/|b/|g' > ../changes.patch
121-
122-
# Apply the patch to target repo only if there are changes
123-
if [ -s ../changes.patch ]; then
124-
echo "Applying patch:"
125-
cat ../changes.patch
126-
cd ../repo
127-
patch -p1 < ../changes.patch
128-
129-
# Update the commit marker only when we applied changes
130-
echo "$CURRENT_COMMIT" > .bootc-dev-infra-commit.txt
131-
else
132-
echo "No changes in common/ directory, skipping"
133-
fi
134-
else
135-
# First sync - copy everything
136-
echo "First sync - copying all files"
137-
cd ../repo
138-
rsync -av ../infra/common/ .
139-
140-
# Update the commit marker
141-
echo "$CURRENT_COMMIT" > .bootc-dev-infra-commit.txt
142-
fi
135+
chmod +x sync-common
136+
./sync-common infra repo "${{ github.sha }}"
143137
144138
- name: Open pull request
145139
uses: peter-evans/create-pull-request@v7

scripts/sync-common/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

scripts/sync-common/Cargo.lock

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/sync-common/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "sync-common"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0"
8+
xshell = "0.2"
9+
10+
[dev-dependencies]
11+
tempfile = "3.0"

0 commit comments

Comments
 (0)