summaryrefslogtreecommitdiffstats
path: root/scripts/Dpkg/Changelog/Entry.pm
blob: d55e5fd29985ff9fa068139e9ba917a6cb98e288 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
#
# 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 2 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 <https://www.gnu.org/licenses/>.

=encoding utf8

=head1 NAME

Dpkg::Changelog::Entry - represents a changelog entry

=head1 DESCRIPTION

This class represents a changelog entry. It is composed
of a set of lines with specific purpose: a header line, changes lines, a
trailer line. Blank lines can be between those kind of lines.

=cut

package Dpkg::Changelog::Entry 1.01;

use strict;
use warnings;

use Carp;

use Dpkg::Gettext;
use Dpkg::ErrorHandling;
use Dpkg::Control::Changelog;

use overload
    '""' => \&output,
    'eq' => sub { defined($_[1]) and "$_[0]" eq "$_[1]" },
    fallback => 1;

=head1 METHODS

=over 4

=item $entry = Dpkg::Changelog::Entry->new()

Creates a new object. It doesn't represent a real changelog entry
until one has been successfully parsed or built from scratch.

=cut

sub new {
    my $this = shift;
    my $class = ref($this) || $this;

    my $self = {
	header => undef,
	changes => [],
	trailer => undef,
	blank_after_header => [],
	blank_after_changes => [],
	blank_after_trailer => [],
    };
    bless $self, $class;
    return $self;
}

=item $str = $entry->output()

=item "$entry"

Get a string representation of the changelog entry.

=item $entry->output($fh)

Print the string representation of the changelog entry to a
filehandle.

=cut

sub _format_output_block {
    my $lines = shift;
    return join('', map { $_ . "\n" } @{$lines});
}

sub output {
    my ($self, $fh) = @_;
    my $str = '';
    $str .= $self->{header} . "\n" if defined($self->{header});
    $str .= _format_output_block($self->{blank_after_header});
    $str .= _format_output_block($self->{changes});
    $str .= _format_output_block($self->{blank_after_changes});
    $str .= $self->{trailer} . "\n" if defined($self->{trailer});
    $str .= _format_output_block($self->{blank_after_trailer});
    print { $fh } $str if defined $fh;
    return $str;
}

=item $entry->get_part($part)

Return either a string (for a single line) or an array ref (for multiple
lines) corresponding to the requested part. $part can be
"header, "changes", "trailer", "blank_after_header",
"blank_after_changes", "blank_after_trailer".

=cut

sub get_part {
    my ($self, $part) = @_;
    croak "invalid part of changelog entry: $part" unless exists $self->{$part};
    return $self->{$part};
}

=item $entry->set_part($part, $value)

Set the value of the corresponding part. $value can be a string
or an array ref.

=cut

sub set_part {
    my ($self, $part, $value) = @_;
    croak "invalid part of changelog entry: $part" unless exists $self->{$part};
    if (ref($self->{$part})) {
	if (ref($value)) {
	    $self->{$part} = $value;
	} else {
	    $self->{$part} = [ $value ];
	}
    } else {
	$self->{$part} = $value;
    }
}

=item $entry->extend_part($part, $value)

Concatenate $value at the end of the part. If the part is already a
multi-line value, $value is added as a new line otherwise it's
concatenated at the end of the current line.

=cut

sub extend_part {
    my ($self, $part, $value, @rest) = @_;
    croak "invalid part of changelog entry: $part" unless exists $self->{$part};
    if (ref($self->{$part})) {
	if (ref($value)) {
	    push @{$self->{$part}}, @$value;
	} else {
	    push @{$self->{$part}}, $value;
	}
    } elsif (defined $self->{$part}) {
        if (ref($value)) {
            $self->{$part} = [ $self->{$part}, @$value ];
        } else {
            $self->{$part} .= $value;
        }
    } else {
        $self->{$part} = $value;
    }
}

=item $is_empty = $entry->is_empty()

Returns 1 if the changelog entry doesn't contain anything at all.
Returns 0 as soon as it contains something in any of its non-blank
parts.

=cut

sub is_empty {
    my $self = shift;
    return !(defined($self->{header}) || defined($self->{trailer}) ||
	     scalar(@{$self->{changes}}));
}

=item $entry->normalize()

Normalize the content. Strip whitespaces at end of lines, use a single
empty line to separate each part.

=cut

sub normalize {
    my $self = shift;
    if (defined($self->{header})) {
	$self->{header} =~ s/\s+$//g;
	$self->{blank_after_header} = [''];
    } else {
	$self->{blank_after_header} = [];
    }
    if (scalar(@{$self->{changes}})) {
	s/\s+$//g foreach @{$self->{changes}};
	$self->{blank_after_changes} = [''];
    } else {
	$self->{blank_after_changes} = [];
    }
    if (defined($self->{trailer})) {
	$self->{trailer} =~ s/\s+$//g;
	$self->{blank_after_trailer} = [''];
    } else {
	$self->{blank_after_trailer} = [];
    }
}

=item $src = $entry->get_source()

Return the name of the source package associated to the changelog entry.

=cut

sub get_source {
    return;
}

=item $ver = $entry->get_version()

Return the version associated to the changelog entry.

=cut

sub get_version {
    return;
}

=item @dists = $entry->get_distributions()

Return a list of target distributions for this version.

=cut

sub get_distributions {
    return;
}

=item $ctrl = $entry->get_optional_fields()

Return a set of optional fields exposed by the changelog entry.
It always returns a L<Dpkg::Control> object (possibly empty though).

=cut

sub get_optional_fields {
    return Dpkg::Control::Changelog->new();
}

=item $urgency = $entry->get_urgency()

Return the urgency of the associated upload.

=cut

sub get_urgency {
    return;
}

=item $maint = $entry->get_maintainer()

Return the string identifying the person who signed this changelog entry.

=cut

sub get_maintainer {
    return;
}

=item $time = $entry->get_timestamp()

Return the timestamp of the changelog entry.

=cut

sub get_timestamp {
    return;
}

=item $time = $entry->get_timepiece()

Return the timestamp of the changelog entry as a L<Time::Piece> object.

This function might return undef if there was no timestamp.

=cut

sub get_timepiece {
    return;
}

=item $str = $entry->get_dpkg_changes()

Returns a string that is suitable for usage in a C<Changes> field
in the output format of C<dpkg-parsechangelog>.

=cut

sub get_dpkg_changes {
    my $self = shift;
    my $header = $self->get_part('header') // '';
    $header =~ s/\s+$//;
    return "\n$header\n\n" . join("\n", @{$self->get_part('changes')});
}

=back

=head1 CHANGES

=head2 Version 1.01 (dpkg 1.18.8)

New method: $entry->get_timepiece().

=head2 Version 1.00 (dpkg 1.15.6)

Mark the module as public.

=cut

1;