Skip to content

Commit

Permalink
Refactor manifest copy and comparison process for improved ID handling
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Nov 1, 2024
1 parent 8c91bbf commit 0be27f7
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions .github/workflows/ci-build-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,40 +94,39 @@ jobs:
sed -i 's#<itunes:subtitle>.*</itunes:subtitle>##' ${{ steps.set-dirs.outputs.old_dir }}/public/podcast-archives.rss
sed -i 's#<itunes:subtitle>.*</itunes:subtitle>##' ${{ steps.set-dirs.outputs.new_dir }}/public/podcast-archives.rss
- name: Handle manifest.json and Replace IDs in New Build
- name: Copy New Manifest and Retrieve Old Manifest
run: |
set -x
# Step 1: Get the old manifest.json by checking out the old commit
# Copy the generated manifest.json from the new build
cp ${{ steps.set-dirs.outputs.new_dir }}/hugo/data/manifest.json ${{ steps.set-dirs.outputs.new_dir }}/manifest.json
# Check out the old commit to get its manifest.json
git checkout ${{ steps.get-comparison-commit.outputs.compare_sha }} -- hugo/data/manifest.json
mv hugo/data/manifest.json ${{ steps.set-dirs.outputs.old_dir }}/manifest.json
# Step 2: Get the new manifest.json by checking out the latest commit
git checkout ${{ github.event.pull_request.head.sha || github.sha }} -- hugo/data/manifest.json
mv hugo/data/manifest.json ${{ steps.set-dirs.outputs.new_dir }}/manifest.json
# Step 3: Load IDs from both old and new manifest.json files
# Define paths to old and new manifests
old_manifest="${{ steps.set-dirs.outputs.old_dir }}/manifest.json"
new_manifest="${{ steps.set-dirs.outputs.new_dir }}/manifest.json"
# Read paths and hashes from old manifest
declare -A old_ids
while read -r line; do
path=$(echo "$line" | grep -oP '\"\/[^"]+')
hash=$(echo "$line" | grep -oP 'id=\K[^"]+')
old_ids[$path]=$hash
done < <(grep -oP '\"\/[^"]+\".*id=[^"]+' "$old_manifest")
# Step 3: Compare manifest.json files and handle ID replacements if they differ
if cmp -s "$old_manifest" "$new_manifest"; then
echo "No differences in manifest.json. Skipping ID replacements."
exit 0
fi
# Replace mismatches in new files based on old manifest
while read -r line; do
path=$(echo "$line" | grep -oP '\"\/[^"]+')
new_hash=$(echo "$line" | grep -oP 'id=\K[^"]+')
old_hash=${old_ids[$path]}
# Step 4: Extract and replace differing IDs
while IFS= read -r old_line && IFS= read -r new_line <&3; do
if [[ "$old_line" != "$new_line" ]]; then
# Extract ID from both lines
old_id=$(echo "$old_line" | grep -oP 'id=\K[^"]+')
new_id=$(echo "$new_line" | grep -oP 'id=\K[^"]+')
# Replace in new files only if the old and new hashes differ
if [[ -n "$old_hash" && "$old_hash" != "$new_hash" ]]; then
sed -i "s|$path?id=$new_hash|$path?id=$old_hash|g" ${{ steps.set-dirs.outputs.new_dir }}/public/**/*.html
# Perform replacement if IDs differ
if [[ -n "$old_id" && -n "$new_id" && "$old_id" != "$new_id" ]]; then
find ${{ steps.set-dirs.outputs.new_dir }}/public -name '*.html' -exec sudo sed -i "s?id=$new_id?id=$old_id?g" {} +
fi
fi
done < <(grep -oP '\"\/[^"]+\".*id=[^"]+' "$new_manifest")
done <"$old_manifest" 3<"$new_manifest"
- name: Generate diff
run: |
Expand Down

0 comments on commit 0be27f7

Please sign in to comment.