-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv-cr
executable file
·145 lines (117 loc) · 3.48 KB
/
mv-cr
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
SHELL=/bin/bash
set -e
. $(dirname "$0")/utility.sh
usage_message=$(cat <<EOF
usage: mv-cr OPTIONS...
Move a change from one project to another project.
If change is not found, no action is taken.
--change Change to remove.
--from-project NAME Project change belongs to.
--to-project NAME Project change should be moved to.
--tag TAG Move change to TAG in destination plan.
--no-update-plan Do not update Sqitch plans.
Example:
bin/mv-cr --from-project general --to-project bugs --change "20231201_XYZ" --tag @2023.12
EOF
)
function usage() {
echo "$usage_message"
}
g_change=
g_from_project=
g_to_project=
g_update_plan=YES
g_tag=
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--change|-c)
g_change="$2";
shift 2;
;;
--from-project|-f)
g_from_project="$2";
shift 2;
;;
--to-project|-t)
g_to_project="$2";
shift 2;
;;
--no-update-plan|-u)
g_update_plan="NO";
shift;
;;
--tag)
g_tag="$2";
shift;
;;
--help|-h)
usage
exit 0
;;
--*)
echo "error: invalid option $1"
usage
exit 64
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [ -z "$g_from_project" ] || [ -z "$g_to_project" ] || [ -z "$g_change" ]; then
echo "error: missing required option --from-project, --to-project or --change"
exit 64
fi
if [ ! -f "$g_from_project/sqitch.plan" ]; then
echo "Project $g_from_project does not exist"
exit 64
fi
if [ ! -f "$g_to_project/sqitch.plan" ]; then
echo "Project $g_to_project does not exist"
exit 64
fi
if [ -z "$g_tag" ]; then
g_tag=$(get_change_tag "$g_from_project" "$g_change")
fi
if [ ! -z "$g_tag" ]; then
if ! [[ $g_tag =~ ^@([0-9]{4})\.([0-9]{2})$ ]]; then
echo "Invalid tag: tags must conform to format \"@YYYY.MM\"."
exit 64
fi
if ! grep -q "^$g_tag" "$g_to_project/sqitch.plan"; then
echo "Tag \"$g_tag\" not found in $g_to_project/sqitch.plan"
exit 64
fi
fi
if [ ! -f "$g_from_project/deploy/${g_change}.sql" ]; then
echo "Change $g_from_project/deploy/${g_change}.sql does not exist"
exit 64
fi
for type in deploy revert verify; do
mv "$g_from_project/$type/${g_change}.sql" "$g_to_project/$type/${g_change}.sql"
done
if [ -d "$g_from_project/deploy/${g_change}" ]; then
mv "$g_from_project/deploy/${g_change}" "$g_to_project/deploy/${g_change}"
fi
if [ "$g_update_plan" == "YES" ]; then
if [ ! -z "$g_tag" ]; then
cr_line=$(grep -e "$g_change" "$g_from_project/sqitch.plan")
sed -i "/^$g_tag/ i\\$cr_line" "$g_to_project/sqitch.plan"
else
echo "" >> "$g_to_project/sqitch.plan"
grep -e "$g_change" "$g_from_project/sqitch.plan" >> "$g_to_project/sqitch.plan"
fi
grep -v -e "$g_change" "$g_from_project/sqitch.plan" > "$g_from_project/sqitch.plan.tmp"
mv -f "$g_from_project/sqitch.plan.tmp" "$g_from_project/sqitch.plan"
issue_id=$(get_cr_issue_id "$project" "$g_change")
if [ ! -z "$issue_id" ]; then
gh issue comment -b "Issue moved from $g_from_project to $g_to_project." $issue_id
fi
fi
echo "Change $g_change moved from $g_from_project to $g_to_project"