-
Notifications
You must be signed in to change notification settings - Fork 191
/
merge_ref_solutions.sh
executable file
·103 lines (85 loc) · 2.3 KB
/
merge_ref_solutions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /usr/bin/env bash
# merge_ref_solutions.sh
#
# This is a helper script meant for trainers and project developers. It is designed to help you
# incorporate code from the reference solutions into your current project solutions. Beyond just
# a blind copy, it will prompt you to merge files so you can keep the parts you've written that
# you don't want to change.
#
# Node: This script uses kdiff3 for merging. You'll need to install it.
#
# Options:
# --rm-origs : Removes the *.orig files created by kdiff3 after merging
# --package PACKAGE_NAME : Only merge the given package
# --overwrite-all : Bypasses all merge prompts and copies all files. This will erase any
# changes you've made that aren't in the reference solutions.
RM_ORIG_FILES=0
PACKAGE_NAME=""
OVERWRITE_ALL=0
options=$(getopt \
--longoptions "rm-origs,package:,overwrite-all" \
--name "$(basename "$0")" \
--options "" \
-- "$@"
)
eval set --$options
while [[ $# -gt 0 ]]; do
case "$1" in
--rm-origs)
RM_ORIG_FILES=1
shift 1
;;
--package)
PACKAGE_NAME=$2
shift 2
;;
--overwrite-all)
OVERWRITE_ALL=1
shift 1
;;
--)
break
;;
*)
echo "Unknown option: $1"
break
;;
esac
done
if ! command -v kdiff3 &> /dev/null
then
echo "Could not find kdiff3, which is required. Please install it."
echo "eg. via apt: sudo apt install kdiff3"
exit 1
fi
for solution_file in $(find reference_solutions/$PACKAGE_NAME -type f -print)
do
current_file=$(echo $solution_file | sed 's/reference_solutions\///')
if [ "$current_file" = "COLCON_IGNORE" ]
then
continue
fi
if [ ! -f $current_file ]
then
echo "Copying new file: $current_file"
cp $solution_file $current_file
continue
fi
if cmp --silent $current_file $solution_file
then
echo "No changes in $current_file"
continue
fi
if [ "$OVERWRITE_ALL" ]
then
echo "Overwriting $current_file"
cp $solution_file $current_file
else
kdiff3 --merge --L1 'Current Solution' --L2 'Reference Solution' --output "$current_file" "$current_file" "$solution_file"
if [ "$RM_ORIG_FILES" ]
then
rm $current_file.orig
fi
fi
done
exit 0