Changelog.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. # Copyright © 2005, 2007 Frank Lichtenheld <frank@lichtenheld.de>
  2. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. =encoding utf8
  17. =head1 NAME
  18. Dpkg::Changelog - base class to implement a changelog parser
  19. =head1 DESCRIPTION
  20. Dpkg::Changelog is a class representing a changelog file
  21. as an array of changelog entries (Dpkg::Changelog::Entry).
  22. By deriving this object and implementing its parse method, you
  23. add the ability to fill this object with changelog entries.
  24. =cut
  25. package Dpkg::Changelog;
  26. use strict;
  27. use warnings;
  28. our $VERSION = '1.00';
  29. use Dpkg::Gettext;
  30. use Dpkg::ErrorHandling qw(:DEFAULT report REPORT_WARN);
  31. use Dpkg::Control;
  32. use Dpkg::Control::Changelog;
  33. use Dpkg::Control::Fields;
  34. use Dpkg::Index;
  35. use Dpkg::Version;
  36. use Dpkg::Vendor qw(run_vendor_hook);
  37. use parent qw(Dpkg::Interface::Storable);
  38. use overload
  39. '@{}' => sub { return $_[0]->{data} };
  40. =head1 METHODS
  41. =over 4
  42. =item $c = Dpkg::Changelog->new(%options)
  43. Creates a new changelog object.
  44. =cut
  45. sub new {
  46. my ($this, %opts) = @_;
  47. my $class = ref($this) || $this;
  48. my $self = {
  49. verbose => 1,
  50. parse_errors => []
  51. };
  52. bless $self, $class;
  53. $self->set_options(%opts);
  54. return $self;
  55. }
  56. =item $c->load($filename)
  57. Parse $filename as a changelog.
  58. =cut
  59. =item $c->set_options(%opts)
  60. Change the value of some options. "verbose" (defaults to 1) defines
  61. whether parse errors are displayed as warnings by default. "reportfile"
  62. is a string to use instead of the name of the file parsed, in particular
  63. in error messages. "range" defines the range of entries that we want to
  64. parse, the parser will stop as soon as it has parsed enough data to
  65. satisfy $c->get_range($opts{range}).
  66. =cut
  67. sub set_options {
  68. my ($self, %opts) = @_;
  69. $self->{$_} = $opts{$_} foreach keys %opts;
  70. }
  71. =item $c->reset_parse_errors()
  72. Can be used to delete all information about errors occurred during
  73. previous L<parse> runs.
  74. =cut
  75. sub reset_parse_errors {
  76. my $self = shift;
  77. $self->{parse_errors} = [];
  78. }
  79. =item $c->parse_error($file, $line_nr, $error, [$line])
  80. Record a new parse error in $file at line $line_nr. The error message is
  81. specified with $error and a copy of the line can be recorded in $line.
  82. =cut
  83. sub parse_error {
  84. my ($self, $file, $line_nr, $error, $line) = @_;
  85. push @{$self->{parse_errors}}, [ $file, $line_nr, $error, $line ];
  86. if ($self->{verbose}) {
  87. if ($line) {
  88. warning("%20s(l$line_nr): $error\nLINE: $line", $file);
  89. } else {
  90. warning("%20s(l$line_nr): $error", $file);
  91. }
  92. }
  93. }
  94. =item $c->get_parse_errors()
  95. Returns all error messages from the last L<parse> run.
  96. If called in scalar context returns a human readable
  97. string representation. If called in list context returns
  98. an array of arrays. Each of these arrays contains
  99. =over 4
  100. =item 1.
  101. a string describing the origin of the data (a filename usually). If the
  102. reportfile configuration option was given, its value will be used instead.
  103. =item 2.
  104. the line number where the error occurred
  105. =item 3.
  106. an error description
  107. =item 4.
  108. the original line
  109. =back
  110. =cut
  111. sub get_parse_errors {
  112. my $self = shift;
  113. if (wantarray) {
  114. return @{$self->{parse_errors}};
  115. } else {
  116. my $res = '';
  117. foreach my $e (@{$self->{parse_errors}}) {
  118. if ($e->[3]) {
  119. $res .= report(REPORT_WARN, g_("%s(l%s): %s\nLINE: %s"), @$e);
  120. } else {
  121. $res .= report(REPORT_WARN, g_('%s(l%s): %s'), @$e);
  122. }
  123. }
  124. return $res;
  125. }
  126. }
  127. =item $c->set_unparsed_tail($tail)
  128. Add a string representing unparsed lines after the changelog entries.
  129. Use undef as $tail to remove the unparsed lines currently set.
  130. =item $c->get_unparsed_tail()
  131. Return a string representing the unparsed lines after the changelog
  132. entries. Returns undef if there's no such thing.
  133. =cut
  134. sub set_unparsed_tail {
  135. my ($self, $tail) = @_;
  136. $self->{unparsed_tail} = $tail;
  137. }
  138. sub get_unparsed_tail {
  139. my $self = shift;
  140. return $self->{unparsed_tail};
  141. }
  142. =item @{$c}
  143. Returns all the Dpkg::Changelog::Entry objects contained in this changelog
  144. in the order in which they have been parsed.
  145. =item $c->get_range($range)
  146. Returns an array (if called in list context) or a reference to an array of
  147. Dpkg::Changelog::Entry objects which each represent one entry of the
  148. changelog. $range is a hash reference describing the range of entries
  149. to return. See section L<"RANGE SELECTION">.
  150. =cut
  151. sub __sanity_check_range {
  152. my ($self, $r) = @_;
  153. my $data = $self->{data};
  154. if (defined($r->{offset}) and not defined($r->{count})) {
  155. warning(g_("'offset' without 'count' has no effect")) if $self->{verbose};
  156. delete $r->{offset};
  157. }
  158. ## no critic (ControlStructures::ProhibitUntilBlocks)
  159. if ((defined($r->{count}) || defined($r->{offset})) &&
  160. (defined($r->{from}) || defined($r->{since}) ||
  161. defined($r->{to}) || defined($r->{until})))
  162. {
  163. warning(g_("you can't combine 'count' or 'offset' with any other " .
  164. 'range option')) if $self->{verbose};
  165. delete $r->{from};
  166. delete $r->{since};
  167. delete $r->{to};
  168. delete $r->{until};
  169. }
  170. if (defined($r->{from}) && defined($r->{since})) {
  171. warning(g_("you can only specify one of 'from' and 'since', using " .
  172. "'since'")) if $self->{verbose};
  173. delete $r->{from};
  174. }
  175. if (defined($r->{to}) && defined($r->{until})) {
  176. warning(g_("you can only specify one of 'to' and 'until', using " .
  177. "'until'")) if $self->{verbose};
  178. delete $r->{to};
  179. }
  180. # Handle non-existing versions
  181. my (%versions, @versions);
  182. foreach my $entry (@{$data}) {
  183. my $version = $entry->get_version();
  184. next unless defined $version;
  185. $versions{$version->as_string()} = 1;
  186. push @versions, $version->as_string();
  187. }
  188. if ((defined($r->{since}) and not exists $versions{$r->{since}})) {
  189. warning(g_("'%s' option specifies non-existing version"), 'since');
  190. warning(g_('use newest entry that is earlier than the one specified'));
  191. foreach my $v (@versions) {
  192. if (version_compare_relation($v, REL_LT, $r->{since})) {
  193. $r->{since} = $v;
  194. last;
  195. }
  196. }
  197. if (not exists $versions{$r->{since}}) {
  198. # No version was earlier, include all
  199. warning(g_('none found, starting from the oldest entry'));
  200. delete $r->{since};
  201. $r->{from} = $versions[-1];
  202. }
  203. }
  204. if ((defined($r->{from}) and not exists $versions{$r->{from}})) {
  205. warning(g_("'%s' option specifies non-existing version"), 'from');
  206. warning(g_('use oldest entry that is later than the one specified'));
  207. my $oldest;
  208. foreach my $v (@versions) {
  209. if (version_compare_relation($v, REL_GT, $r->{from})) {
  210. $oldest = $v;
  211. }
  212. }
  213. if (defined($oldest)) {
  214. $r->{from} = $oldest;
  215. } else {
  216. warning(g_("no such entry found, ignoring '%s' parameter"), 'from');
  217. delete $r->{from}; # No version was oldest
  218. }
  219. }
  220. if (defined($r->{until}) and not exists $versions{$r->{until}}) {
  221. warning(g_("'%s' option specifies non-existing version"), 'until');
  222. warning(g_('use oldest entry that is later than the one specified'));
  223. my $oldest;
  224. foreach my $v (@versions) {
  225. if (version_compare_relation($v, REL_GT, $r->{until})) {
  226. $oldest = $v;
  227. }
  228. }
  229. if (defined($oldest)) {
  230. $r->{until} = $oldest;
  231. } else {
  232. warning(g_("no such entry found, ignoring '%s' parameter"), 'until');
  233. delete $r->{until}; # No version was oldest
  234. }
  235. }
  236. if (defined($r->{to}) and not exists $versions{$r->{to}}) {
  237. warning(g_("'%s' option specifies non-existing version"), 'to');
  238. warning(g_('use newest entry that is earlier than the one specified'));
  239. foreach my $v (@versions) {
  240. if (version_compare_relation($v, REL_LT, $r->{to})) {
  241. $r->{to} = $v;
  242. last;
  243. }
  244. }
  245. if (not exists $versions{$r->{to}}) {
  246. # No version was earlier
  247. warning(g_("no such entry found, ignoring '%s' parameter"), 'to');
  248. delete $r->{to};
  249. }
  250. }
  251. if (defined($r->{since}) and $data->[0]->get_version() eq $r->{since}) {
  252. warning(g_("'since' option specifies most recent version, ignoring"));
  253. delete $r->{since};
  254. }
  255. if (defined($r->{until}) and $data->[-1]->get_version() eq $r->{until}) {
  256. warning(g_("'until' option specifies oldest version, ignoring"));
  257. delete $r->{until};
  258. }
  259. ## use critic
  260. }
  261. sub get_range {
  262. my ($self, $range) = @_;
  263. $range //= {};
  264. my $res = $self->_data_range($range);
  265. if (defined $res) {
  266. return @$res if wantarray;
  267. return $res;
  268. } else {
  269. return;
  270. }
  271. }
  272. sub _is_full_range {
  273. my ($self, $range) = @_;
  274. return 1 if $range->{all};
  275. # If no range delimiter is specified, we want everything.
  276. foreach my $delim (qw(since until from to count offset)) {
  277. return 0 if exists $range->{$delim};
  278. }
  279. return 1;
  280. }
  281. sub _data_range {
  282. my ($self, $range) = @_;
  283. my $data = $self->{data} or return;
  284. return [ @$data ] if $self->_is_full_range($range);
  285. $self->__sanity_check_range($range);
  286. my ($start, $end);
  287. if (defined($range->{count})) {
  288. my $offset = $range->{offset} // 0;
  289. my $count = $range->{count};
  290. # Convert count/offset in start/end
  291. if ($offset > 0) {
  292. $offset -= ($count < 0);
  293. } elsif ($offset < 0) {
  294. $offset = $#$data + ($count > 0) + $offset;
  295. } else {
  296. $offset = $#$data if $count < 0;
  297. }
  298. $start = $end = $offset;
  299. $start += $count+1 if $count < 0;
  300. $end += $count-1 if $count > 0;
  301. # Check limits
  302. $start = 0 if $start < 0;
  303. return if $start > $#$data;
  304. $end = $#$data if $end > $#$data;
  305. return if $end < 0;
  306. $end = $start if $end < $start;
  307. return [ @{$data}[$start .. $end] ];
  308. }
  309. ## no critic (ControlStructures::ProhibitUntilBlocks)
  310. my @result;
  311. my $include = 1;
  312. $include = 0 if defined($range->{to}) or defined($range->{until});
  313. foreach my $entry (@{$data}) {
  314. my $v = $entry->get_version();
  315. $include = 1 if defined($range->{to}) and $v eq $range->{to};
  316. last if defined($range->{since}) and $v eq $range->{since};
  317. push @result, $entry if $include;
  318. $include = 1 if defined($range->{until}) and $v eq $range->{until};
  319. last if defined($range->{from}) and $v eq $range->{from};
  320. }
  321. ## use critic
  322. return \@result if scalar(@result);
  323. return;
  324. }
  325. =item $c->abort_early()
  326. Returns true if enough data have been parsed to be able to return all
  327. entries selected by the range set at creation (or with set_options).
  328. =cut
  329. sub abort_early {
  330. my $self = shift;
  331. my $data = $self->{data} or return;
  332. my $r = $self->{range} or return;
  333. my $count = $r->{count} // 0;
  334. my $offset = $r->{offset} // 0;
  335. return if $self->_is_full_range($r);
  336. return if $offset < 0 or $count < 0;
  337. if (defined($r->{count})) {
  338. if ($offset > 0) {
  339. $offset -= ($count < 0);
  340. }
  341. my $start = my $end = $offset;
  342. $end += $count-1 if $count > 0;
  343. return ($start < @$data and $end < @$data);
  344. }
  345. return unless defined($r->{since}) or defined($r->{from});
  346. foreach my $entry (@{$data}) {
  347. my $v = $entry->get_version();
  348. return 1 if defined($r->{since}) and $v eq $r->{since};
  349. return 1 if defined($r->{from}) and $v eq $r->{from};
  350. }
  351. return;
  352. }
  353. =item $c->save($filename)
  354. Save the changelog in the given file.
  355. =item $c->output()
  356. =item "$c"
  357. Returns a string representation of the changelog (it's a concatenation of
  358. the string representation of the individual changelog entries).
  359. =item $c->output($fh)
  360. Output the changelog to the given filehandle.
  361. =cut
  362. sub output {
  363. my ($self, $fh) = @_;
  364. my $str = '';
  365. foreach my $entry (@{$self}) {
  366. my $text = $entry->output();
  367. print { $fh } $text if defined $fh;
  368. $str .= $text if defined wantarray;
  369. }
  370. my $text = $self->get_unparsed_tail();
  371. if (defined $text) {
  372. print { $fh } $text if defined $fh;
  373. $str .= $text if defined wantarray;
  374. }
  375. return $str;
  376. }
  377. =item $control = $c->dpkg($range)
  378. Returns a Dpkg::Control::Changelog object representing the entries selected
  379. by the optional range specifier (see L<"RANGE SELECTION"> for details).
  380. Returns undef in no entries are matched.
  381. The following fields are contained in the object:
  382. =over 4
  383. =item Source
  384. package name (in the first entry)
  385. =item Version
  386. packages' version (from first entry)
  387. =item Distribution
  388. target distribution (from first entry)
  389. =item Urgency
  390. urgency (highest of all printed entries)
  391. =item Maintainer
  392. person that created the (first) entry
  393. =item Date
  394. date of the (first) entry
  395. =item Closes
  396. bugs closed by the entry/entries, sorted by bug number
  397. =item Changes
  398. content of the the entry/entries
  399. =back
  400. =cut
  401. our ( @URGENCIES, %URGENCIES );
  402. BEGIN {
  403. @URGENCIES = qw(low medium high critical emergency);
  404. my $i = 1;
  405. %URGENCIES = map { $_ => $i++ } @URGENCIES;
  406. }
  407. sub dpkg {
  408. my ($self, $range) = @_;
  409. my @data = $self->get_range($range) or return;
  410. my $src = shift @data;
  411. my $f = Dpkg::Control::Changelog->new();
  412. $f->{Urgency} = $src->get_urgency() || 'unknown';
  413. $f->{Source} = $src->get_source() || 'unknown';
  414. $f->{Version} = $src->get_version() // 'unknown';
  415. $f->{Distribution} = join(' ', $src->get_distributions());
  416. $f->{Maintainer} = $src->get_maintainer() // '';
  417. $f->{Date} = $src->get_timestamp() // '';
  418. $f->{Changes} = $src->get_dpkg_changes();
  419. # handle optional fields
  420. my $opts = $src->get_optional_fields();
  421. my %closes;
  422. foreach (keys %$opts) {
  423. if (/^Urgency$/i) { # Already dealt
  424. } elsif (/^Closes$/i) {
  425. $closes{$_} = 1 foreach (split(/\s+/, $opts->{Closes}));
  426. } else {
  427. field_transfer_single($opts, $f);
  428. }
  429. }
  430. foreach my $bin (@data) {
  431. my $oldurg = $f->{Urgency} // '';
  432. my $oldurgn = $URGENCIES{$f->{Urgency}} // -1;
  433. my $newurg = $bin->get_urgency() // '';
  434. my $newurgn = $URGENCIES{$newurg} // -1;
  435. $f->{Urgency} = ($newurgn > $oldurgn) ? $newurg : $oldurg;
  436. $f->{Changes} .= "\n" . $bin->get_dpkg_changes();
  437. # handle optional fields
  438. $opts = $bin->get_optional_fields();
  439. foreach (keys %$opts) {
  440. if (/^Closes$/i) {
  441. $closes{$_} = 1 foreach (split(/\s+/, $opts->{Closes}));
  442. } elsif (not exists $f->{$_}) { # Don't overwrite an existing field
  443. field_transfer_single($opts, $f);
  444. }
  445. }
  446. }
  447. if (scalar keys %closes) {
  448. $f->{Closes} = join ' ', sort { $a <=> $b } keys %closes;
  449. }
  450. run_vendor_hook('post-process-changelog-entry', $f);
  451. return $f;
  452. }
  453. =item @controls = $c->rfc822($range)
  454. Returns a Dpkg::Index containing Dpkg::Control::Changelog objects where
  455. each object represents one entry in the changelog that is part of the
  456. range requested (see L<"RANGE SELECTION"> for details). For the format of
  457. such an object see the description of the L<"dpkg"> method (while ignoring
  458. the remarks about which values are taken from the first entry).
  459. =cut
  460. sub rfc822 {
  461. my ($self, $range) = @_;
  462. my @data = $self->get_range($range) or return;
  463. my $index = Dpkg::Index->new(type => CTRL_CHANGELOG);
  464. foreach my $entry (@data) {
  465. my $f = Dpkg::Control::Changelog->new();
  466. $f->{Urgency} = $entry->get_urgency() || 'unknown';
  467. $f->{Source} = $entry->get_source() || 'unknown';
  468. $f->{Version} = $entry->get_version() // 'unknown';
  469. $f->{Distribution} = join(' ', $entry->get_distributions());
  470. $f->{Maintainer} = $entry->get_maintainer() // '';
  471. $f->{Date} = $entry->get_timestamp() // '';
  472. $f->{Changes} = $entry->get_dpkg_changes();
  473. # handle optional fields
  474. my $opts = $entry->get_optional_fields();
  475. foreach (keys %$opts) {
  476. field_transfer_single($opts, $f) unless exists $f->{$_};
  477. }
  478. run_vendor_hook('post-process-changelog-entry', $f);
  479. $index->add($f);
  480. }
  481. return $index;
  482. }
  483. =back
  484. =head1 RANGE SELECTION
  485. A range selection is described by a hash reference where
  486. the allowed keys and values are described below.
  487. The following options take a version number as value.
  488. =over 4
  489. =item since
  490. Causes changelog information from all versions strictly
  491. later than B<version> to be used.
  492. =item until
  493. Causes changelog information from all versions strictly
  494. earlier than B<version> to be used.
  495. =item from
  496. Similar to C<since> but also includes the information for the
  497. specified B<version> itself.
  498. =item to
  499. Similar to C<until> but also includes the information for the
  500. specified B<version> itself.
  501. =back
  502. The following options don't take version numbers as values:
  503. =over 4
  504. =item all
  505. If set to a true value, all entries of the changelog are returned,
  506. this overrides all other options.
  507. =item count
  508. Expects a signed integer as value. Returns C<value> entries from the
  509. top of the changelog if set to a positive integer, and C<abs(value)>
  510. entries from the tail if set to a negative integer.
  511. =item offset
  512. Expects a signed integer as value. Changes the starting point for
  513. C<count>, either counted from the top (positive integer) or from
  514. the tail (negative integer). C<offset> has no effect if C<count>
  515. wasn't given as well.
  516. =back
  517. Some examples for the above options. Imagine an example changelog with
  518. entries for the versions 1.2, 1.3, 2.0, 2.1, 2.2, 3.0 and 3.1.
  519. Range Included entries
  520. ----- ----------------
  521. since => '2.0' 3.1, 3.0, 2.2
  522. until => '2.0' 1.3, 1.2
  523. from => '2.0' 3.1, 3.0, 2.2, 2.1, 2.0
  524. to => '2.0' 2.0, 1.3, 1.2
  525. count => 2 3.1, 3.0
  526. count => -2 1.3, 1.2
  527. count => 3, offset => 2 2.2, 2.1, 2.0
  528. count => 2, offset => -3 2.0, 1.3
  529. count => -2, offset => 3 3.0, 2.2
  530. count => -2, offset => -3 2.2, 2.1
  531. Any combination of one option of C<since> and C<from> and one of
  532. C<until> and C<to> returns the intersection of the two results
  533. with only one of the options specified.
  534. =head1 CHANGES
  535. =head2 Version 1.00 (dpkg 1.15.6)
  536. Mark the module as public.
  537. =head1 AUTHOR
  538. Frank Lichtenheld, E<lt>frank@lichtenheld.deE<gt>
  539. Raphaël Hertzog, E<lt>hertzog@debian.orgE<gt>
  540. =cut
  541. 1;