forked from hetida/hetida-designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.sh
executable file
·189 lines (158 loc) · 5.22 KB
/
release.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/bin/bash -e
###########################################################################
# Constants...
###########################################################################
SOURCE_REMOTE=origin
SOURCE_BRANCH=develop
TARGET_REMOTE=origin
TARGET_BRANCH=release
TARGET_REPO='[email protected]:hetida/hetida-designer.git'
SOURCE_REMOTE_BRANCH="$SOURCE_REMOTE/$SOURCE_BRANCH"
TARGET_REMOTE_BRANCH="$TARGET_REMOTE/$TARGET_BRANCH"
###########################################################################
# Usage...
###########################################################################
print_usage() {
echo
echo "Usage:"
echo
echo " release.sh --help"
echo " Displays this help message."
echo
echo " release.sh <VERSION>"
echo " Creates (or checks out) a local '$TARGET_BRANCH' branch,tags the commit and pushes the result to github."
echo
echo " Note: If a local branch named '$TARGET_BRANCH' already exists, it will be overwritten with the content of the remote release branch WITHOUT WARNING!"
echo
}
if [ "$1" == '-h' -o "$1" == '--help' ]; then
print_usage
exit 0
fi
RELEASE_VERSION="$1"
if [ -z "$RELEASE_VERSION" ]; then
print_usage
exit 1
else
echo
echo "Releasing version '$RELEASE_VERSION' from '$SOURCE_REMOTE_BRANCH' to '$TARGET_REMOTE_BRANCH'."
fi
###########################################################################
echo
echo "Testing preconditions..."
###########################################################################
# 1. Test presence of the source repository.
echo -n "source remote '$SOURCE_REMOTE'"
REMOTE_ORIGIN=`git remote get-url "$SOURCE_REMOTE" 2>/dev/null || true`
if [ -z "$REMOTE_ORIGIN" ]; then
echo " - not ok: missing"
exit 1
else
echo " - ok"
fi
# 2. Ensure presence of the correct target repository with correct url.
echo -n "target remote '$TARGET_REMOTE'"
REMOTE_URL=`git remote get-url "$TARGET_REMOTE" 2>/dev/null || true`
if [ -z "$REMOTE_URL" ]; then
git remote add "$TARGET_REMOTE" "$TARGET_REPO"
echo " - ok: added"
elif [ "$REMOTE_URL" == "$TARGET_REPO" ]; then
echo " - ok"
else
echo " - not ok: wrong remote URL '$REMOTE_URL'"
exit 1
fi
git fetch --multiple "$SOURCE_REMOTE" "$TARGET_REMOTE" >/dev/null
# Usage: branch_exists <branch name> --extra --args
branch_exists() {
EXISTING_BRANCH=`git branch $2 '--format=%(refname)' --list "$1"`
if [ -n "$EXISTING_BRANCH" ]; then
return 0
else
return 1
fi
}
# Usage: remote_branch_exists <remote branch name>
remote_branch_exists() {
branch_exists "$1" --remote
return $?
}
# Usage: local_branch_exists <local branch name>
local_branch_exists() {
branch_exists "$1"
return $?
}
# 3. Test presence of the source branch
echo -n "remote source branch '$SOURCE_REMOTE_BRANCH'"
if remote_branch_exists "$SOURCE_REMOTE_BRANCH"; then
echo " - ok"
else
echo " - not ok: missing"
exit 1
fi
# 4. Ensure local presence of the target branch.
echo -n "local target branch '$TARGET_BRANCH'"
if remote_branch_exists "$TARGET_REMOTE_BRANCH"; then
git checkout --quiet -B "$TARGET_BRANCH" "$TARGET_REMOTE_BRANCH"
echo " - ok: created"
else
if local_branch_exists "$TARGET_BRANCH"; then
echo " - ok: checked out'"
git checkout --quiet "$TARGET_BRANCH"
else
git checkout --quiet --orphan "$TARGET_BRANCH"
echo " - ok: initialized"
fi
fi
# 5. Test whether the release version already exists.
RELEASE_TAG="v$RELEASE_VERSION"
echo -n "release tag availability '$RELEASE_TAG'"
EXISTING_TAG=`git tag --list "$RELEASE_TAG"`
if [ -n "$EXISTING_TAG" ]; then
echo " - not ok: exists"
exit 1
else
echo " - ok"
fi
###########################################################################
echo
echo "Preparing release..."
###########################################################################
# git restore --quiet --source "$SOURCE_REMOTE_BRANCH" .
echo "Get newest develop branch from remote"
git checkout --quiet "$SOURCE_BRANCH"
git pull "$SOURCE_REMOTE" "$SOURCE_BRANCH"
echo "Get newest release branch from remote"
git checkout --quiet "$TARGET_BRANCH"
git pull "$TARGET_REMOTE" "$TARGET_BRANCH"
echo "Checkout source branch and merge in target branch"
git checkout --quiet "$SOURCE_BRANCH"
git merge "$TARGET_BRANCH"
# Make sure changelog has been updated
CHANGELOG_DIFF=$(git diff "$SOURCE_BRANCH" "$TARGET_BRANCH" -- CHANGELOG.md)
if [ -z "$CHANGELOG_DIFF" ]; then
echo "no changelog entry. Aborting. Please edit changelog."
exit 1
else
echo "- ok: found changelog edits"
fi
echo "add VERSION and CHANGELOG.md"
echo "$RELEASE_VERSION" > VERSION # Version into version file
# git add --all
git add VERSION CHANGELOG.md
git commit --allow-empty --quiet --message="Release $RELEASE_TAG"
git tag "$RELEASE_TAG"
###########################################################################
echo
echo "Publishing release..."
###########################################################################
git checkout --quiet "$TARGET_BRANCH"
git merge "$SOURCE_BRANCH"
echo "Push release branch"
git push --tags --no-verify "$TARGET_REMOTE" "$TARGET_BRANCH"
git checkout --quiet "$SOURCE_BRANCH"
echo "dev-snapshot-post-$RELEASE_VERSION" > VERSION
git add VERSION
git commit --allow-empty --quiet --message="Post-Release $RELEASE_VERSION dev snapshot"
echo "Push development branch"
git push --tags --no-verify "$SOURCE_REMOTE" "$SOURCE_BRANCH"