|
| 1 | +#!/usr/bin/env bash |
| 2 | +## Copyright 2026 Apple Inc. and the Swift Homomorphic Encryption project authors |
| 3 | +## |
| 4 | +## Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +## you may not use this file except in compliance with the License. |
| 6 | +## You may obtain a copy of the License at |
| 7 | +## |
| 8 | +## http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +## |
| 10 | +## Unless required by applicable law or agreed to in writing, software |
| 11 | +## distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +## See the License for the specific language governing permissions and |
| 14 | +## limitations under the License. |
| 15 | + |
| 16 | +# ============================================================ |
| 17 | +# Pre-commit hook: filter-package-resolve.sh |
| 18 | +# |
| 19 | +# Removes unwanted pins from Package.resolved. |
| 20 | +# Triggered by pre-commit only when Package.resolved is staged. |
| 21 | +# |
| 22 | +# Requirements: jq (brew install jq) |
| 23 | +# ============================================================ |
| 24 | + |
| 25 | +set -euo pipefail |
| 26 | + |
| 27 | +if ! command -v jq &> /dev/null; then |
| 28 | + echo 2>&1 "jq not found" |
| 29 | + exit 44 |
| 30 | +fi |
| 31 | + |
| 32 | +if [[ -z $(git diff HEAD Package.resolved) ]]; then |
| 33 | + # Nothing to check |
| 34 | + exit 0 |
| 35 | +fi |
| 36 | + |
| 37 | +# ------------------------------------------------------------ |
| 38 | +# ✏️ CONFIGURE: add/remove package identities here. |
| 39 | +# ------------------------------------------------------------ |
| 40 | +UNWANTED_PINS=( |
| 41 | + "hdrhistogram-swift" |
| 42 | + "package-benchmark" |
| 43 | + "package-jemalloc" |
| 44 | + "swift-atomics" |
| 45 | + "swift-system" |
| 46 | + "texttable" |
| 47 | +) |
| 48 | + |
| 49 | +# ------------------------------------------------------------ |
| 50 | +# Build jq filter and remove pins in-place |
| 51 | +# ------------------------------------------------------------ |
| 52 | + |
| 53 | +printf -v JOINED '"%s",' "${UNWANTED_PINS[@]}" |
| 54 | +FILTER="del(.pins[] | select(.identity | IN(${JOINED%,})))" |
| 55 | + |
| 56 | +jq --indent 2 "$FILTER" Package.resolved \ |
| 57 | + | sed 's/": /\" : /g' \ |
| 58 | + > Package.resolved.tmp \ |
| 59 | + && mv Package.resolved.tmp Package.resolved |
| 60 | + |
| 61 | +# If the only remaining diff against HEAD is the originHash line, restore it |
| 62 | +CHANGED_LINES="$(git diff HEAD Package.resolved | grep '^[+-]' | grep -v '^---' | grep -v '^+++')" |
| 63 | +if [[ "$(echo "$CHANGED_LINES" | wc -l)" -eq 2 ]] \ |
| 64 | + && echo "$CHANGED_LINES" | grep -q 'originHash' \ |
| 65 | + && [[ "$(echo "$CHANGED_LINES" | grep -v 'originHash')" == "" ]]; then |
| 66 | + ORIGINAL_HASH="$(git diff HEAD Package.resolved | grep '^-.*originHash' | sed 's/^-.*"originHash" : "//' | sed 's/".*//')" |
| 67 | + sed -i '' "s/\"originHash\" : \".*\"/\"originHash\" : \"$ORIGINAL_HASH\"/" Package.resolved |
| 68 | +fi |
0 commit comments