dpkg-mergechangelogs.pl 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/usr/bin/perl
  2. # Copyright © 2009-2010 Raphaël Hertzog <hertzog@debian.org>
  3. # Copyright © 2012 Guillem Jover <guillem@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. use warnings;
  18. use strict;
  19. use Dpkg ();
  20. use Dpkg::Changelog::Debian;
  21. use Dpkg::ErrorHandling;
  22. use Dpkg::Gettext;
  23. use Dpkg::Version;
  24. use Getopt::Long qw(:config posix_default bundling no_ignorecase);
  25. use Scalar::Util qw(blessed);
  26. textdomain('dpkg-dev');
  27. sub merge_entries($$$);
  28. sub merge_block($$$;&);
  29. sub merge_entry_item($$$$);
  30. sub merge_conflict($$);
  31. sub get_conflict_block($$);
  32. sub join_lines($);
  33. BEGIN {
  34. eval 'use Algorithm::Merge qw(merge);';
  35. if ($@) {
  36. eval q{
  37. sub merge {
  38. my ($o, $a, $b) = @_;
  39. return @$a if join("\n", @$a) eq join("\n", @$b);
  40. return get_conflict_block($a, $b);
  41. }
  42. };
  43. }
  44. }
  45. sub version {
  46. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  47. printf "\n" . _g(
  48. 'This is free software; see the GNU General Public License version 2 or
  49. later for copying conditions. There is NO warranty.
  50. ');
  51. }
  52. sub usage {
  53. printf(_g(
  54. "Usage: %s [<option>...] <old> <new-a> <new-b> [<out>]
  55. Options:
  56. -m, --merge-prereleases merge pre-releases together, ignores everything
  57. after the last '~' in the version.
  58. -?, --help show this help message.
  59. --version show the version.
  60. "), $Dpkg::PROGNAME);
  61. }
  62. my $merge_prereleases;
  63. my @options_spec = (
  64. 'help|?' => sub { usage(); exit(0) },
  65. 'version' => sub { version(); exit(0) },
  66. 'merge-prereleases|m' => \$merge_prereleases,
  67. );
  68. {
  69. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  70. GetOptions(@options_spec);
  71. }
  72. my ($old, $new_a, $new_b, $out_file) = @ARGV;
  73. unless (defined $old and defined $new_a and defined $new_b)
  74. {
  75. usageerr(_g('needs at least three arguments'));
  76. }
  77. unless (-e $old and -e $new_a and -e $new_b)
  78. {
  79. usageerr(_g('file arguments need to exist'));
  80. }
  81. my ($cho, $cha, $chb);
  82. $cho = Dpkg::Changelog::Debian->new();
  83. $cho->load($old);
  84. $cha = Dpkg::Changelog::Debian->new();
  85. $cha->load($new_a);
  86. $chb = Dpkg::Changelog::Debian->new();
  87. $chb->load($new_b);
  88. my @o = reverse @$cho;
  89. my @a = reverse @$cha;
  90. my @b = reverse @$chb;
  91. my @result; # Lines to output
  92. my $exitcode = 0; # 1 if conflict encountered
  93. unless (merge_block($cho, $cha, $chb, sub {
  94. my $tail = $_[0]->get_unparsed_tail();
  95. chomp $tail if defined $tail;
  96. return $tail;
  97. }))
  98. {
  99. merge_conflict($cha->get_unparsed_tail(), $chb->get_unparsed_tail());
  100. }
  101. while (1) {
  102. my ($o, $a, $b) = get_items_to_merge();
  103. last unless defined $o or defined $a or defined $b;
  104. next if merge_block($o, $a, $b);
  105. # We only have the usually conflicting cases left
  106. if (defined $a and defined $b) {
  107. # Same entry, merge sub-items separately for a nicer result
  108. merge_entries($o, $a, $b);
  109. } else {
  110. # Non-existing on one side, changed on the other side
  111. merge_conflict($a, $b);
  112. }
  113. }
  114. if (defined($out_file) and $out_file ne '-') {
  115. open(my $out_fh, '>', $out_file)
  116. or syserr(_g('cannot write %s'), $out_file);
  117. print { $out_fh } ((blessed $_) ? "$_" : "$_\n") foreach @result;
  118. close($out_fh) or syserr(_g('cannot write %s'), $out_file);
  119. } else {
  120. print ((blessed $_) ? "$_" : "$_\n") foreach @result;
  121. }
  122. exit $exitcode;
  123. # Returns the next items to merge, all items returned correspond to the
  124. # same minimal version among the 3 possible next items (undef is returned
  125. # if the next item on the given changelog is skipped)
  126. sub get_items_to_merge {
  127. my @items = (shift @o, shift @a, shift @b);
  128. my @arrays = (\@o, \@a, \@b);
  129. my $minver;
  130. foreach my $i (0 .. 2) {
  131. if (defined $minver and defined $items[$i]) {
  132. my $cmp = compare_versions($minver, $items[$i]->get_version());
  133. if ($cmp > 0) {
  134. $minver = $items[$i]->get_version();
  135. foreach my $j (0 .. $i - 1) {
  136. unshift @{$arrays[$j]}, $items[$j];
  137. $items[$j] = undef;
  138. }
  139. } elsif ($cmp < 0) {
  140. unshift @{$arrays[$i]}, $items[$i];
  141. $items[$i] = undef;
  142. }
  143. } else {
  144. $minver = $items[$i]->get_version() if defined $items[$i];
  145. }
  146. }
  147. return @items;
  148. }
  149. # Compares the versions taking into account some oddities like the fact
  150. # that we want backport/volatile versions to sort higher than the version
  151. # on which they are based.
  152. sub compare_versions {
  153. my ($a, $b) = @_;
  154. return 0 if not defined $a and not defined $b;
  155. return 1 if not defined $b;
  156. return -1 if not defined $a;
  157. $a = $a->get_version() if ref($a) and $a->isa('Dpkg::Changelog::Entry');
  158. $b = $b->get_version() if ref($b) and $b->isa('Dpkg::Changelog::Entry');
  159. # Backport and volatile are not real prereleases
  160. $a =~ s/~(bpo|vola)/+$1/;
  161. $b =~ s/~(bpo|vola)/+$1/;
  162. if ($merge_prereleases) {
  163. $a =~ s/~[^~]*$//;
  164. $b =~ s/~[^~]*$//;
  165. }
  166. $a = Dpkg::Version->new($a);
  167. $b = Dpkg::Version->new($b);
  168. return $a <=> $b;
  169. }
  170. # Merge changelog entries smartly by merging individually the different
  171. # parts constituting an entry
  172. sub merge_entries($$$) {
  173. my ($o, $a, $b) = @_;
  174. # NOTE: Only $o can be undef
  175. # Merge the trailer line
  176. unless (merge_entry_item('blank_after_trailer', $o, $a, $b)) {
  177. unshift @result, '';
  178. }
  179. unless (merge_entry_item('trailer', $o, $a, $b)) {
  180. merge_conflict($a->get_part('trailer'), $b->get_part('trailer'));
  181. }
  182. # Merge the changes
  183. unless (merge_entry_item('blank_after_changes', $o, $a, $b)) {
  184. unshift @result, '';
  185. }
  186. my @merged = merge(defined $o ? $o->get_part('changes') : [],
  187. $a->get_part('changes'), $b->get_part('changes'),
  188. {
  189. CONFLICT => sub {
  190. $exitcode = 1;
  191. return get_conflict_block($_[0], $_[1]);
  192. }
  193. });
  194. unshift @result, @merged;
  195. # Merge the header line
  196. unless (merge_entry_item('blank_after_header', $o, $a, $b)) {
  197. unshift @result, '';
  198. }
  199. unless (merge_entry_item('header', $o, $a, $b)) {
  200. merge_conflict($a->get_part('header'), $b->get_part('header'));
  201. }
  202. }
  203. sub join_lines($) {
  204. my $array = shift;
  205. return join("\n", @$array) if ref($array) eq 'ARRAY';
  206. return $array;
  207. }
  208. # Try to merge the obvious cases, return 1 on success and 0 on failure
  209. # O A B
  210. # - x x => x
  211. # o o b => b
  212. # - - b => b
  213. # o a o => a
  214. # - a - => a
  215. sub merge_block($$$;&) {
  216. my ($o, $a, $b, $preprocess) = @_;
  217. $preprocess //= \&join_lines;
  218. $o = &$preprocess($o) if defined($o);
  219. $a = &$preprocess($a) if defined($a);
  220. $b = &$preprocess($b) if defined($b);
  221. return 1 if not defined($a) and not defined($b);
  222. if (defined($a) and defined($b) and ($a eq $b)) {
  223. unshift @result, $a;
  224. } elsif ((defined($a) and defined($o) and ($a eq $o)) or
  225. (not defined($a) and not defined($o))) {
  226. unshift @result, $b if defined $b;
  227. } elsif ((defined($b) and defined($o) and ($b eq $o)) or
  228. (not defined($b) and not defined($o))) {
  229. unshift @result, $a if defined $a;
  230. } else {
  231. return 0;
  232. }
  233. return 1;
  234. }
  235. sub merge_entry_item($$$$) {
  236. my ($item, $o, $a, $b) = @_;
  237. if (blessed($o) and $o->isa('Dpkg::Changelog::Entry')) {
  238. $o = $o->get_part($item);
  239. } elsif (ref $o) {
  240. $o = $o->{$item};
  241. }
  242. if (blessed($a) and $a->isa('Dpkg::Changelog::Entry')) {
  243. $a = $a->get_part($item);
  244. } elsif (ref $a) {
  245. $a = $a->{$item};
  246. }
  247. if (blessed($b) and $b->isa('Dpkg::Changelog::Entry')) {
  248. $b = $b->get_part($item);
  249. } elsif (ref $b) {
  250. $b = $b->{$item};
  251. }
  252. return merge_block($o, $a, $b);
  253. }
  254. sub merge_conflict($$) {
  255. my ($a, $b) = @_;
  256. unshift @result, get_conflict_block($a, $b);
  257. $exitcode = 1;
  258. }
  259. sub get_conflict_block($$) {
  260. my ($a, $b) = @_;
  261. my (@a, @b);
  262. push @a, $a if defined $a;
  263. push @b, $b if defined $b;
  264. @a = @{$a} if ref($a) eq 'ARRAY';
  265. @b = @{$b} if ref($b) eq 'ARRAY';
  266. return ('<<<<<<<', @a, '=======', @b, '>>>>>>>');
  267. }