summaryrefslogtreecommitdiffstats
path: root/lib/changelog.sh
blob: e7c3f7cd866d4c453710fab02d399d38af1669de (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
# Functions for parsing changelogs
#
# Copyright (C) 2012, 2018, 2019 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_parse_changelog_error()
{
	local file="${1}"
	local line_nr="${2}"
	local msg_id="${3}"
	shift 3 || _ob_abort
	local file_info=

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

	_ob_warn_msg "${msg_id}" "${file_info}" "${@}"

	return 0
}

_ob_get_changelog_expect_str()
{
	_ob_get_msg "changelog_expect_${1}"

	return 0
}

## @brief Parse a changelog file
## @details \fBob_parse_changelog\fP() parses a \fIchangelog\fP file, formatted
##          as specified at
##          <http://specs.proteanos.com/spf-2.0/metadata.html#changelog>.  For
##          each version entry, \fBob_parse_changelog\fP calls \fIentry_cb\fP
##          with the source package name, the source package version, the list
##          of distributions into which the package should be installed, the
##          formatted heading (source package name, source package version, and
##          distributions list) and change details, the name and e-mail address
##          of the package maintainer, and the date of packaging as arguments. 
##          \fBob_parse_changelog\fP stops parsing either at the end of the
##          \fIchangelog\fP file or when \fIentry_cb\fP returns non-zero.
## @operand file     req The file to parse, or "-" for standard input.
## @operand entry_cb req Callback to run for each entry.
## @return Returns 0 after parsing.
## @stderr Prints error messages on parse errors.
## @pure maybe This function has no side effects.  Whether this function is
##             subshell-safe in practice depends on whether \fIentry_cb\fP is
##             subshell-safe.
ob_parse_changelog()
{
	local file="${1}"
	local entry_cb="${2}"
	shift 2 || _ob_abort
	local line_nr=
	local line=
	local line_=
	local expect=
	local changes=
	local blank_lines=
	local source=
	local distribution=
	local version=
	local maintainer=
	local date=

	# Parsing logic based on that of dpkg.

	line_nr=0
	expect='first_heading'

	while IFS='' read line; do
		line_nr=$(($line_nr + 1))
		case "${line}" in
			'')
				if [ x"${expect}" = x'start_changes' ]; then
					changes="${changes}${_OB_LF}${line}"
				elif [ x"${expect}" = x'next_or_eof' ]; then
					:
				elif [ x"${expect}" != x'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}${_OB_LF}${line}"
				fi
				;;
			[!\ ]*)
				if [ x"${expect}" != x'first_heading' ] && \
						[ x"${expect}" != \
						x'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}" ] || \
						[ -z "${distribution}" ] || \
						[ -z "${version}" ] || \
						[ x"${version% *}" != \
						x"${version}" ]; then
					_ob_parse_changelog_error "${file}" "${line_nr}" \
						'changelog_bad_heading'
					source=''
					version=''
					distribution=''
					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
					changes="${line}"
				fi
				expect='start_changes'
				blank_lines=''
				;;
			' -- '*)
				if [ x"${expect}" != x'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}" ] || [ -z "${date}" ] ||
						[ x"${maintainer}" = \
						x"${date}" ]; then
					_ob_parse_changelog_error "${file}" "${line_nr}" \
						'changelog_bad_trailer'
					maintainer=''
					date=''
				elif [ -n "${source}" ]; then
					"${entry_cb}" "${source}" "${version}" \
						"${distribution}" "${changes}" \
						"${maintainer}" "${date}" || \
						return 0
				fi
				expect='next_or_eof'
				blank_lines=''
				;;
			' --'*)
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_bad_trailer'
				;;
			'  '*)
				if [ x"${expect}" != x'start_changes' ] && \
						[ x"${expect}" != \
						x'changes_or_trailer' ]; then
					_ob_parse_changelog_error "${file}" "${line_nr}" \
						'changelog_found_change' \
						"$(_ob_get_changelog_expect_str "${expect}")"
				fi
				changes="${changes}${_OB_LF}${blank_lines}${line}"
				expect='changes_or_trailer'
				blank_lines=''
				;;
			*)
				_ob_parse_changelog_error "${file}" "${line_nr}" \
					'changelog_bad_line'
				blank_lines=''
				;;
		esac
	done <<-EOF
		$(cat -- "${file}")
		EOF

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

	return 0
}