|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +# Script to synchronise the nhs-notify-template-repository with this repository |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# $ [options] ./check-terraform-format.sh |
| 9 | +# |
| 10 | +# Options: |
| 11 | +# new_only=true # Only identify new files from the template-repository |
| 12 | +# changes_only=true # Only identify files which have drifted from the template-repository |
| 13 | + |
| 14 | +# ============================================================================== |
| 15 | + |
| 16 | +# Command line prameters |
| 17 | +new_only=${new_only:-false} |
| 18 | +changes_only=${changes_only:-false} |
| 19 | + |
| 20 | +# Set variables |
| 21 | +TEMPLATE_REPO_DIR="nhs-notify-repository-template" |
| 22 | +IGNORE_FILE="scripts/config/.repository-template-sync-ignore" |
| 23 | + |
| 24 | +# Check if the template directory exists |
| 25 | +if [ ! -d "${TEMPLATE_REPO_DIR}" ]; then |
| 26 | + echo "Template directory ${TEMPLATE_REPO_DIR} not found!" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# Check if the .template-ignore file exists, create an empty one if not |
| 31 | +if [ ! -f "${IGNORE_FILE}" ]; then |
| 32 | + echo "# Files and folders to ignore when syncing ${TEMPLATE_REPO_DIR} back in to this repository" > ${IGNORE_FILE} |
| 33 | + echo "# Files and Folders in this repository to ignore" >> ${IGNORE_FILE} |
| 34 | + echo "# Files and Folders in the template repository to disregard" >> ${IGNORE_FILE} |
| 35 | +fi |
| 36 | + |
| 37 | +# Read the .template-ignore file into an array |
| 38 | +while IFS= read -r line || [ -n "$line" ]; do |
| 39 | + IGNORED_PATHS+=("$line") |
| 40 | +done < "$IGNORE_FILE" |
| 41 | + |
| 42 | +# Check if a file is ignored. |
| 43 | +is_ignored() { |
| 44 | + local file=${1} |
| 45 | + |
| 46 | + # Ignore .git directories and files |
| 47 | + if [[ "$file" == *.git/* ]]; then |
| 48 | + return 0 |
| 49 | + fi |
| 50 | + |
| 51 | + for ignored in "${IGNORED_PATHS[@]}"; do |
| 52 | + if [[ -n "$ignored" && "$file" =~ $ignored ]]; then |
| 53 | + return 0 |
| 54 | + fi |
| 55 | + done |
| 56 | + return 1 |
| 57 | +} |
| 58 | + |
| 59 | +# Navigate to the template directory |
| 60 | +cd "${TEMPLATE_REPO_DIR}" || exit |
| 61 | +FILES_ADDED=() |
| 62 | +FILES_WITH_CHANGES=() |
| 63 | + |
| 64 | +# Loop through all files in the template directory |
| 65 | +while IFS= read -r -d '' file; do |
| 66 | + relative_path="${file#./}" # Remove leading './' |
| 67 | + |
| 68 | + # Check if the file is ignored |
| 69 | + if is_ignored "$relative_path"; then |
| 70 | + echo "Ignoring $relative_path" |
| 71 | + continue |
| 72 | + fi |
| 73 | + |
| 74 | + target_path="../$relative_path" |
| 75 | + mkdir -p "$(dirname "$target_path")" |
| 76 | + |
| 77 | + # Copy the file to the root directory if it doesn't exist or is different |
| 78 | + if [ ! -f "$target_path" ] && [ "$changes_only" == false ]; then |
| 79 | + echo "Copying $relative_path to the repository" |
| 80 | + FILES_ADDED+=("${relative_path}") |
| 81 | + cp "$file" "$target_path" |
| 82 | + |
| 83 | + else |
| 84 | + # If the file exists, check if it's different |
| 85 | + if [ "$new_only" == false ]; then |
| 86 | + if ! diff -q "$file" "$target_path" > /dev/null 2>&1; then |
| 87 | + echo "Merging changes from $relative_path" |
| 88 | + FILES_WITH_CHANGES+=("${relative_path}") |
| 89 | + cp "$file" "$target_path" |
| 90 | + fi |
| 91 | + fi |
| 92 | + fi |
| 93 | +done < <(find . -type f -print0) |
| 94 | + |
| 95 | +echo "${#FILES_ADDED[@]}" files added, "${#FILES_WITH_CHANGES[@]}" files with changes detected. |
| 96 | + |
| 97 | +echo ------------------------------------------ |
| 98 | + |
| 99 | +if [ "$changes_only" == false ]; then |
| 100 | + echo ------------------------------------------ |
| 101 | + echo "New files added:" |
| 102 | + printf ' - %s\n' "${FILES_ADDED[@]}" |
| 103 | +fi |
| 104 | + |
| 105 | + |
| 106 | +if [ "$new_only" == false ]; then |
| 107 | + echo ------------------------------------------ |
| 108 | + echo "Changed files:" |
| 109 | + printf ' - %s\n' "${FILES_WITH_CHANGES[@]}" |
| 110 | +fi |
0 commit comments