Skip to content

Commit 67aca36

Browse files
committed
CCM-8881: Use custom merge script to combine changes from both repos
1 parent 707df0b commit 67aca36

File tree

2 files changed

+72
-12
lines changed

2 files changed

+72
-12
lines changed

scripts/githooks/merge.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const fs = require("fs");
2+
3+
// Read files
4+
const [file1, file2] = process.argv.slice(2);
5+
const lines1 = fs.readFileSync(file1).toString().split('\n');
6+
const lines2 = fs.readFileSync(file2).toString().split('\n');
7+
8+
// Tokenize lines in file1 for later comparison
9+
const tokenize = line => {
10+
if (line === '') {
11+
return [];
12+
}
13+
return line.split(/\s+/).filter(x => x !== '#').slice(0, 1);
14+
};
15+
const lines1Tokens = lines1.flatMap(tokenize);
16+
17+
// Step through the files
18+
let pos1 = 0, pos2 = 0;
19+
while (pos1 < lines1.length || pos2 < lines2.length) {
20+
21+
const l1 = pos1 < lines1.length && lines1[pos1] || '';
22+
const l2 = pos2 < lines2.length && lines2[pos2] || '';
23+
24+
// If the lines match, print l1 and skip l2
25+
if (l1 !== '' && l1 === l2) {
26+
process.stdout.write(`${l1}\n`);
27+
pos1++;
28+
pos2++;
29+
continue;
30+
}
31+
32+
if (pos2 < lines2.length) {
33+
// If l2 is empty, skip l2
34+
if (l2 === '') {
35+
pos2++;
36+
continue;
37+
}
38+
39+
const [l2token] = tokenize(l2);
40+
41+
// If l2 token matches l1 token, print l1
42+
if (pos1 < lines1.length && lines1Tokens[pos1] === l2token) {
43+
process.stdout.write(`${l1}\n`);
44+
pos1++;
45+
pos2++;
46+
continue;
47+
}
48+
49+
// If l2 doesn't match any lines in file1, print l2
50+
if (l2token && !lines1Tokens.includes(l2token)) {
51+
process.stdout.write(`${l2}\n`);
52+
}
53+
pos2++
54+
continue;
55+
}
56+
57+
// If we're not at the end of file1, print l1
58+
if (pos1 < lines1.length) {
59+
if (pos1 === lines1.length - 1 && l1 === '') {
60+
// Don't print tailing newline
61+
} else {
62+
process.stdout.write(`${l1}\n`);
63+
}
64+
pos1++;
65+
}
66+
}

scripts/githooks/sync-template-repo.sh

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ 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+
scriptdir=$(realpath "$(dirname "$0")")
17+
1618
# Command line parameters
1719
new_only=${new_only:-false}
1820
changes_only=${changes_only:-false}
@@ -108,17 +110,9 @@ while IFS= read -r -d '' file; do
108110
if ! diff -q "$file" "$target_path" > /dev/null 2>&1; then
109111
if is_merge "$relative_path"; then
110112
echo "Merging changes from $relative_path"
111-
cp "$target_path" "${target_path}.bak"
112-
if git merge-file "$target_path" /dev/null "$file" --union; then
113-
if ! cmp -s "$target_path" "${target_path}.bak"; then
114-
FILES_WITH_CHANGES+=("${relative_path}")
115-
fi
116-
else
117-
echo "Merge failed for $relative_path, rolling back."
118-
mv "${target_path}.bak" "$target_path"
119-
diff3 "$target_path" /dev/null "$file"
120-
fi
121-
rm -f "${target_path}.bak"
113+
echo node "$(dirname "$0")/merge.js" "$target_path" "$file"
114+
node "${scriptdir}/merge.js" "$target_path" "$file" > "$target_path.merged"
115+
mv "$target_path.merged" "$target_path"
122116
else
123117
echo "Copying changes from $relative_path"
124118
cp "$file" "$target_path"

0 commit comments

Comments
 (0)