summaryrefslogtreecommitdiffstats
path: root/lib/changelog.sh
blob: 52fab294cedbd9eaec75c98cd80871bedc23939f (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# opkbuild
# lib/changelog
# Functions for parsing changelogs.
#
# Copyright (C) 2012 Patrick "P. J." McDermott
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

[ -n "${_OB_CHANGELOG_SM}" ] && return 0
_OB_CHANGELOG_SM='true'

ob_use messages
ob_use locale
ob_use util

# Constant global parameters:
# FIXME: Remove.
OB_SOURCE_RE='^[a-z0-9][a-z0-9+.-]+$'
OB_SOURCE_VERSION_ID_RE='^'
OB_SOURCE_VERSION_ID_RE="${OB_SOURCE_VERSION_ID_RE}"'[0-9a-z.~]+'
OB_SOURCE_VERSION_ID_RE="${OB_SOURCE_VERSION_ID_RE}"'(\+sip[1-9][0-9]*)?'
OB_SOURCE_VERSION_ID_RE="${OB_SOURCE_VERSION_ID_RE}"'(-[1-9][0-9]*)?'
OB_SOURCE_VERSION_ID_RE="${OB_SOURCE_VERSION_ID_RE}"'(\+[a-z0-9]+-[1-9][0-9]*)?'
OB_SOURCE_VERSION_ID_RE="${OB_SOURCE_VERSION_ID_RE}"'$'

OB_CHANGELOG_SOURCE=
OB_CHANGELOG_VERSION=
OB_CHANGELOG_DISTRIBUTION=
OB_CHANGELOG_CHANGES=
OB_CHANGELOG_MAINTAINER=
OB_CHANGELOG_DATE=

oh_parse_changelog()
{
	_ob_local _obpch_file _obpch_entry_cb \
		_obpch_line_nr _obpch_line _obpch_line_ \
		_obpch_expect _obpch_blank_lines _obpch_entries \
		_obpch_source _obpch_distribution _obpch_version \
		_obpch_maintainer _obpch_date

	if [ ${#} -eq 2 ]; then
		_obpch_file="${1}"
		_obpch_entry_cb="${2}"
	else
		_ob_return 125
		return ${?}
	fi

	# Parsing logic based on that of dpkg.

	_obpch_line_nr=0
	_obpch_expect=first_heading
	_obpch_entries=0

	while IFS= read _obpch_line; do
		_obpch_line_nr=$(($_obpch_line_nr + 1))
		if [ -z "${_obpch_line}" ]; then
			if [ "${_obpch_expect}" = start_changes ]; then
				OB_CHANGELOG_CHANGES="${OB_CHANGELOG_CHANGES}
${_obpch_line}"
			elif [ "${_obpch_expect}" = next_or_eof ]; then
				:
			elif [ "${_obpch_expect}" != changes_or_trailer ]; then
				_ob_parse_changelog_error "${_obpch_line_nr}" \
					'changelog_found_blank_line' \
					"$(_ob_get_changelog_expect_str "${_obpch_expect}")"
			else
				_obpch_blank_lines="${_obpch_blank_lines}
${_obpch_line}"
			fi
		elif [ "${_obpch_line# }" = "${_obpch_line}" ]; then
			if [ "${_obpch_expect}" != first_heading -a \
					"${_obpch_expect}" != next_or_eof ]; then
				_ob_parse_changelog_error "${_obpch_line_nr}" \
					'changelog_found_heading' \
					"$(_ob_get_changelog_expect_str "${_obpch_expect}")"
			fi
			_obpch_source="${_obpch_line%% (*}"
			_obpch_line_="${_obpch_line#* (}"
			_obpch_distribution="${_obpch_line_##*) }"
			_obpch_line_="${_obpch_line_%) *}"
			_obpch_version="${_obpch_line_}"
			if [ -z "${_obpch_source}" -o -z "${_obpch_distribution}" -o \
					-z "${_obpch_version}" -o \
					"${_obpch_version% *}" != "${_obpch_version}" ]; then
				_ob_parse_changelog_error "${_obpch_line_nr}" \
					'changelog_bad_heading'
				OB_CHANGELOG_SOURCE=
				OB_CHANGELOG_VERSION=
				OB_CHANGELOG_DISTRIBUTION=
				OB_CHANGELOG_CHANGES=
			else
				echo "${_obpch_source}" | \
					grep -E "${OB_SOURCE_RE}" >/dev/null 2>&1
				if [ "${?}" -ne 0 ]; then
					_ob_parse_changelog_error "${_obpch_line_nr}" \
						'changelog_bad_source' "${source}"
				fi
				echo "${_obpch_version}" | \
					grep -E "${OB_SOURCE_VERSION_ID_RE}" >/dev/null 2>&1
				if [ "${?}" -ne 0 ]; then
					_ob_parse_changelog_error "${_obpch_line_nr}" \
						'changelog_bad_source_version' "${_obpch_version}"
				fi
				OB_CHANGELOG_SOURCE="${_obpch_source}"
				OB_CHANGELOG_VERSION="${_obpch_version}"
				OB_CHANGELOG_DISTRIBUTION="${_obpch_distribution}"
				OB_CHANGELOG_CHANGES="${_obpch_line}"
			fi
			_obpch_expect=start_changes
			_obpch_blank_lines=
		elif [ "${_obpch_line# -- }" != "${_obpch_line}" ]; then
			if [ "${_obpch_expect}" != changes_or_trailer ]; then
				_ob_parse_changelog_error "${_obpch_line_nr}" \
					'changelog_found_trailer' \
					"$(_ob_get_changelog_expect_str "${_obpch_expect}")"
			fi
			_obpch_line="${_obpch_line# -- }"
			_obpch_maintainer="${_obpch_line%%  *}"
			_obpch_date="${_obpch_line#*  }"
			if [ -z "${_obpch_maintainer}" -o -z "${_obpch_date}" -o \
					"${_obpch_maintainer}" = "${_obpch_date}" ]; then
				_ob_parse_changelog_error "${_obpch_line_nr}" \
					'changelog_bad_trailer'
				OB_CHANGELOG_MAINTAINER=
				OB_CHANGELOG_DATE=
			elif [ -n "${OB_CHANGELOG_SOURCE}" ]; then
				OB_CHANGELOG_MAINTAINER="${_obpch_maintainer}"
				OB_CHANGELOG_DATE="${_obpch_date}"
				_obpch_entries=$(($_obpch_entries + 1))
				"${_obpch_entry_cb}"
				if [ ${?} -ne 0 ]; then
					_ob_return ${_obpch_entries}
					return ${?}
				fi
			fi
			_obpch_expect=next_or_eof
			_obpch_blank_lines=
		elif [ "${_obpch_line# --}" != "${_obpch_line}" ]; then
			_ob_parse_changelog_error "${_obpch_line_nr}" \
				'changelog_bad_trailer'
		elif [ "${_obpch_line##  }" != "${_obpch_line}" ]; then
			if [ "${_obpch_expect}" != start_changes -a \
					"${_obpch_expect}" != changes_or_trailer ]; then
				_ob_parse_changelog_error "${_obpch_line_nr}" \
					'changelog_found_change' \
					"$(_ob_get_changelog_expect_str "${_obpch_expect}")"
			fi
			OB_CHANGELOG_CHANGES="${OB_CHANGELOG_CHANGES}
${_obpch_blank_lines}${_obpch_line}"
			_obpch_expect=changes_or_trailer
			_obpch_blank_lines=
		else
			_ob_parse_changelog_error "${_obpch_line_nr}" \
				'changelog_bad_line'
			_obpch_blank_lines=
		fi
	done <"${_obpch_file}"

	if [ "${_obpch_expect}" != next_or_eof ]; then
		_ob_parse_changelog_error "${_obpch_line_nr}" \
			'changelog_found_eof' \
			"$(_ob_get_changelog_expect_str "${_obpch_expect}")"
	fi

	_ob_return ${_obpch_entries}
	return ${?}
}

_ob_parse_changelog_error()
{
	_ob_local _obpche_file _obpche_line_nr _obpche_msg_id \
		_obpche_file_info _obpche_orig_text_domain

	_obpche_file="${1}"
	_obpche_line_nr="${2}"
	_obpche_msg_id="${3}"
	shift 3

	_obpche_file_info=$(printf '%20s(l%d):' "${_obpche_file}" \
		"${_obpche_line_nr}")

	_obpche_orig_text_domain="$(ob_get_text_domain)"
	ob_set_text_domain 'libopkbuild.1'

	oh_warn "${_obpche_file_info} $(ob_get_msg "${_obpche_msg_id}")" "${@}"

	ob_set_text_domain "${_obpche_orig_text_domain}"

	_ob_return 0
	return ${?}
}

_ob_get_changelog_expect_str()
{
	_ob_local _obgces_orig_text_domain

	_obgces_orig_text_domain="$(ob_get_text_domain)"
	ob_set_text_domain 'libopkbuild.1'

	echo "$(ob_get_msg "changelog_expect_${1}")"

	ob_set_text_domain "${_obgces_orig_text_domain}"

	_ob_return 0
	return ${?}
}