summaryrefslogtreecommitdiffstats
path: root/lib/changelog.sh
blob: 9dd86fa2e271f9e44a37e5824827fddad3a84bcc (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
# Functions for parsing changelogs
#
# Copyright (C) 2012 Patrick McDermott
#
# This file is part of opkbuild.
#
# opkbuild 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.
#
# opkbuild 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 opkbuild.  If not, see <http://www.gnu.org/licenses/>.

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

_ob_parse_changelog_error()
{
	local file=
	local line_nr=
	local msg_id=
	local file_info=
	local orig_text_domain=

	file="${1}"
	line_nr="${2}"
	msg_id="${3}"
	shift 3

	file_info="$(printf '%20s(l%d):' "${file}" "${line_nr}")"

	orig_text_domain="$(ob_get_text_domain)"
	ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}"

	ob_warn "${file_info} $(ob_get_msg "${msg_id}")" "${@}"

	ob_set_text_domain "${orig_text_domain}"

	return 0
}

_ob_get_changelog_expect_str()
{
	local orig_text_domain=

	orig_text_domain="$(ob_get_text_domain)"
	ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}"

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

	ob_set_text_domain "${orig_text_domain}"

	return 0
}

:<<=cut
=head1 NAME
=head1 SYNOPSIS
=head1 DESCRIPTION
=head1 RETURN VALUE
=cut
## @brief Parse a changelog file
## @operand file     req The file to parse, or "-" for standard input.
## @operand entry_cb req Callback to run for each entry.
## @return Returns 0.
ob_parse_changelog()
{
	local file=
	local entry_cb=
	local line_nr=
	local line=
	local line_=
	local expect=
	local blank_lines=
	local source=
	local distribution=
	local version=
	local maintainer=
	local date=

	if [ ${#} -eq 2 ]; then
		file="${1}"
		entry_cb="${2}"
	else
		return 125
	fi

	# Parsing logic based on that of dpkg.

	line_nr=0
	expect='first_heading'

	while IFS= read line; do
		line_nr=$(($line_nr + 1))
		if [ -z "${line}" ]; then
			if [ "${expect}" = 'start_changes' ]; then
				OB_CHANGELOG_CHANGES="${OB_CHANGELOG_CHANGES}
${line}"
			elif [ "${expect}" = 'next_or_eof' ]; then
				:
			elif [ "${expect}" != 'changes_or_trailer' ]; then
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_found_blank_line' \
					"$(_ob_get_changelog_expect_str "${expect}")"
			else
				blank_lines="${blank_lines}
${line}"
			fi
		elif [ "${line# }" = "${line}" ]; then
			if [ "${expect}" != 'first_heading' -a \
					"${expect}" != 'next_or_eof' ]; then
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_found_heading' \
					"$(_ob_get_changelog_expect_str "${expect}")"
			fi
			source="${line%% (*}"
			line_="${line#* (}"
			distribution="${line_##*) }"
			line_="${line_%) *}"
			version="${line_}"
			if [ -z "${source}" -o -z "${distribution}" -o \
					-z "${version}" -o \
					"${version% *}" != "${version}" ]; then
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_bad_heading'
				OB_CHANGELOG_SOURCE=
				OB_CHANGELOG_VERSION=
				OB_CHANGELOG_DISTRIBUTION=
				OB_CHANGELOG_CHANGES=
			else
				if ! ob_validate_source_name "${source}"; then
					_ob_parse_changelog_error \
						"${file}" "${line_nr}" \
						'changelog_bad_source' "${source}"
				fi
				if ! ob_parse_version "${version}"; then
					_ob_parse_changelog_error \
						"${file}" "${line_nr}" \
						'changelog_bad_source_version' "${version}"
				fi
				OB_CHANGELOG_SOURCE="${source}"
				OB_CHANGELOG_VERSION="${version}"
				OB_CHANGELOG_DISTRIBUTION="${distribution}"
				OB_CHANGELOG_CHANGES="${line}"
			fi
			expect='start_changes'
			blank_lines=
		elif [ "${line# -- }" != "${line}" ]; then
			if [ "${expect}" != 'changes_or_trailer' ]; then
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_found_trailer' \
					"$(_ob_get_changelog_expect_str "${expect}")"
			fi
			line="${line# -- }"
			maintainer="${line%%  *}"
			date="${line#*  }"
			if [ -z "${maintainer}" -o -z "${date}" -o \
					"${maintainer}" = "${date}" ]; then
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_bad_trailer'
				OB_CHANGELOG_MAINTAINER=
				OB_CHANGELOG_DATE=
			elif [ -n "${OB_CHANGELOG_SOURCE}" ]; then
				OB_CHANGELOG_MAINTAINER="${maintainer}"
				OB_CHANGELOG_DATE="${date}"
				"${entry_cb}"
				if [ ${?} -ne 0 ]; then
					return 0
				fi
			fi
			expect='next_or_eof'
			blank_lines=
		elif [ "${line# --}" != "${line}" ]; then
			_ob_parse_changelog_error "${file}" "${line_nr}" \
				'changelog_bad_trailer'
		elif [ "${line##  }" != "${line}" ]; then
			if [ "${expect}" != 'start_changes' -a \
					"${expect}" != 'changes_or_trailer' ]; then
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_found_change' \
					"$(_ob_get_changelog_expect_str "${expect}")"
			fi
			OB_CHANGELOG_CHANGES="${OB_CHANGELOG_CHANGES}
${blank_lines}${line}"
			expect='changes_or_trailer'
			blank_lines=
		else
			_ob_parse_changelog_error "${file}" "${line_nr}" \
				'changelog_bad_line'
			blank_lines=
		fi
	done <<-EOF
		$(cat "${file}")
		EOF

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

	return 0
}