@@ -5,21 +5,24 @@ set -euo pipefail
55# Script to synchronise the nhs-notify-template-repository with this repository
66#
77# Usage:
8- # $ [options] ./check-terraform-format .sh
8+ # $ [options] ./sync-template-repo .sh
99#
1010# Options:
1111# new_only=true # Only identify new files from the template-repository
1212# changes_only=true # Only identify files which have drifted from the template-repository
1313
1414# ==============================================================================
1515
16- # Command line prameters
16+ scriptdir=$( realpath " $( dirname " $0 " ) " )
17+
18+ # Command line parameters
1719new_only=${new_only:- false}
1820changes_only=${changes_only:- false}
1921
2022# Set variables
2123TEMPLATE_REPO_DIR=" nhs-notify-repository-template"
2224IGNORE_FILE=" scripts/config/.repository-template-sync-ignore"
25+ MERGE_FILE=" scripts/config/.repository-template-sync-merge"
2326
2427# Check if the template directory exists
2528if [ ! -d " ${TEMPLATE_REPO_DIR} " ]; then
@@ -34,11 +37,21 @@ if [ ! -f "${IGNORE_FILE}" ]; then
3437 echo " # Files and Folders in the template repository to disregard" >> ${IGNORE_FILE}
3538fi
3639
40+ # Check if the .template-merge file exists, create an empty one if not
41+ if [ ! -f " ${MERGE_FILE} " ]; then
42+ echo " # Files and folders to merge when syncing ${TEMPLATE_REPO_DIR} back in to this repository" > ${MERGE_FILE}
43+ fi
44+
3745# Read the .template-ignore file into an array
3846while IFS= read -r line || [ -n " $line " ]; do
3947 IGNORED_PATHS+=(" $line " )
4048done < " $IGNORE_FILE "
4149
50+ # Read the .template-merge file into an array
51+ while IFS= read -r line || [ -n " $line " ]; do
52+ MERGED_PATHS+=(" $line " )
53+ done < " $MERGE_FILE "
54+
4255# Check if a file is ignored.
4356is_ignored () {
4457 local file=${1}
@@ -56,13 +69,24 @@ is_ignored() {
5669 return 1
5770}
5871
72+ is_merge () {
73+ local file=${1}
74+
75+ for merged in " ${MERGED_PATHS[@]} " ; do
76+ if [[ -n " $merged " && " $file " =~ $merged ]]; then
77+ return 0
78+ fi
79+ done
80+ return 1
81+ }
82+
5983# Navigate to the template directory
6084cd " ${TEMPLATE_REPO_DIR} " || exit
6185FILES_ADDED=()
6286FILES_WITH_CHANGES=()
6387
6488# Loop through all files in the template directory
65- while IFS= read -r -d ' ' file; do
89+ while IFS= read -r -d ' ' file || [[ -n $file ]] ; do
6690 relative_path=" ${file# ./ } " # Remove leading './'
6791
6892 # Check if the file is ignored
@@ -84,27 +108,38 @@ while IFS= read -r -d '' file; do
84108 # If the file exists, check if it's different
85109 if [ " $new_only " == false ]; then
86110 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 "
111+ if is_merge " $relative_path " ; then
112+ echo " Merging changes from $relative_path "
113+ cp " $target_path " " ${target_path} .bak"
114+ node " ${scriptdir} /../maintenance/merge.js" " $target_path " " $file " > " ${target_path} .merged"
115+ if ! cmp -s " ${target_path} .merged" " ${target_path} .bak" ; then
116+ FILES_WITH_CHANGES+=(" ${relative_path} " )
117+ mv " ${target_path} .merged" " $target_path "
118+ fi
119+ rm -f " ${target_path} .merged" " ${target_path} .bak"
120+ else
121+ echo " Copying changes from $relative_path "
122+ cp " $file " " $target_path "
123+ FILES_WITH_CHANGES+=(" ${relative_path} " )
124+ fi
90125 fi
91126 fi
92127 fi
93128done < <( find . -type f -print0)
94129
95- echo " ${# FILES_ADDED[@]} " files added, " ${# FILES_WITH_CHANGES[@]} " files with changes detected.
96-
97130echo ------------------------------------------
131+ echo " ${# FILES_ADDED[@]} files added, ${# FILES_WITH_CHANGES[@]} files with changes detected."
98132
99- if [ " $changes_only " == false ]; then
133+ if [[ " $changes_only " == false && ${ # FILES_ADDED[@]} -gt 0 ] ]; then
100134 echo ------------------------------------------
101135 echo " New files added:"
102136 printf ' - %s\n' " ${FILES_ADDED[@]} "
103137fi
104138
105-
106- if [ " $new_only " == false ]; then
139+ if [[ " $new_only " == false && ${# FILES_WITH_CHANGES[@]} -gt 0 ]]; then
107140 echo ------------------------------------------
108141 echo " Changed files:"
109142 printf ' - %s\n' " ${FILES_WITH_CHANGES[@]} "
110143fi
144+
145+ echo ------------------------------------------
0 commit comments