dpkg-mergechangelogs.pl 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/perl
  2. # Copyright © 2009-2010 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 <http://www.gnu.org/licenses/>.
  16. use warnings;
  17. use strict;
  18. use Dpkg;
  19. use Dpkg::Changelog::Debian;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Gettext;
  22. use Dpkg::Version;
  23. use Getopt::Long;
  24. use Scalar::Util qw(blessed);
  25. textdomain("dpkg-dev");
  26. sub merge_entries($$$);
  27. sub merge_block($$$;&);
  28. sub merge_entry_item($$$$);
  29. sub merge_conflict($$);
  30. sub get_conflict_block($$);
  31. sub join_lines($);
  32. BEGIN {
  33. eval 'use Algorithm::Merge qw(merge);';
  34. if ($@) {
  35. eval q{
  36. sub merge {
  37. my ($o, $a, $b) = @_;
  38. return @$a if join("\n", @$a) eq join("\n", @$b);
  39. return get_conflict_block($a, $b);
  40. }
  41. };
  42. }
  43. }
  44. sub version {
  45. printf _g("Debian %s version %s.\n"), $progname, $version;
  46. printf "\n" . _g("Copyright (C) 2009-2010 Raphael Hertzog.");
  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. --merge-prereleases, -m merge pre-releases together, ignores everything
  57. after the last '~' in the version.
  58. --help, -h, -? show this help message.
  59. --version show the version.
  60. "), $progname);
  61. }
  62. my $merge_prereleases;
  63. unless (GetOptions('help|?' => sub { usage(); exit(0) },
  64. 'merge-prereleases|m' => \$merge_prereleases,
  65. 'version' => sub { version(); exit(0) })) {
  66. usage();
  67. exit(2);
  68. }
  69. my ($old, $new_a, $new_b, $out_file) = @ARGV;
  70. unless (defined $old and defined $new_a and defined $new_b and
  71. -e $old and -e $new_a and -e $new_b)
  72. {
  73. usage();
  74. exit(2);
  75. }
  76. my ($cho, $cha, $chb, $res);
  77. $cho = Dpkg::Changelog::Debian->new();
  78. $cho->load($old);
  79. $cha = Dpkg::Changelog::Debian->new();
  80. $cha->load($new_a);
  81. $chb = Dpkg::Changelog::Debian->new();
  82. $chb->load($new_b);
  83. my @o = reverse @$cho;
  84. my @a = reverse @$cha;
  85. my @b = reverse @$chb;
  86. my @result; # Lines to output
  87. my $exitcode = 0; # 1 if conflict encountered
  88. unless (merge_block($cho, $cha, $chb, sub {
  89. my $tail = $_[0]->get_unparsed_tail();
  90. chomp $tail if defined $tail;
  91. return $tail;
  92. }))
  93. {
  94. merge_conflict($cha->get_unparsed_tail(), $chb->get_unparsed_tail());
  95. }
  96. while (1) {
  97. my ($o, $a, $b) = get_items_to_merge();
  98. last unless defined $o or defined $a or defined $b;
  99. next if merge_block($o, $a, $b);
  100. # We only have the usually conflicting cases left
  101. if (defined $a and defined $b) {
  102. # Same entry, merge sub-items separately for a nicer result
  103. merge_entries($o, $a, $b);
  104. } else {
  105. # Non-existing on one side, changed on the other side
  106. merge_conflict($a, $b);
  107. }
  108. }
  109. if (defined($out_file) and $out_file ne "-") {
  110. open(OUT, ">", $out_file) || syserr(_g("cannot write %s"), $out_file);
  111. print OUT ((blessed $_) ? "$_" : "$_\n") foreach @result;
  112. close(OUT);
  113. } else {
  114. print ((blessed $_) ? "$_" : "$_\n") foreach @result;
  115. }
  116. exit $exitcode;
  117. # Returns the next items to merge, all items returned correspond to the
  118. # same minimal version among the 3 possible next items (undef is returned
  119. # if the next item on the given changelog is skipped)
  120. sub get_items_to_merge {
  121. my @items = (shift @o, shift @a, shift @b);
  122. my @arrays = (\@o, \@a, \@b);
  123. my $minver;
  124. foreach my $i (0 .. 2) {
  125. if (defined $minver and defined $items[$i]) {
  126. my $cmp = compare_versions($minver, $items[$i]->get_version());
  127. if ($cmp > 0) {
  128. $minver = $items[$i]->get_version();
  129. foreach my $j (0 .. $i - 1) {
  130. unshift @{$arrays[$j]}, $items[$j];
  131. $items[$j] = undef;
  132. }
  133. } elsif ($cmp < 0) {
  134. unshift @{$arrays[$i]}, $items[$i];
  135. $items[$i] = undef;
  136. }
  137. } else {
  138. $minver = $items[$i]->get_version() if defined $items[$i];
  139. }
  140. }
  141. return @items;
  142. }
  143. # Compares the versions taking into account some oddities like the fact
  144. # that we want backport/volatile versions to sort higher than the version
  145. # on which they are based.
  146. sub compare_versions {
  147. my ($a, $b) = @_;
  148. return 0 if not defined $a and not defined $b;
  149. return 1 if not defined $b;
  150. return -1 if not defined $a;
  151. $a = $a->get_version() if ref($a) and $a->isa("Dpkg::Changelog::Entry");
  152. $b = $b->get_version() if ref($b) and $b->isa("Dpkg::Changelog::Entry");
  153. # Backport and volatile are not real prereleases
  154. $a =~ s/~(bpo|vola)/+$1/;
  155. $b =~ s/~(bpo|vola)/+$1/;
  156. if ($merge_prereleases) {
  157. $a =~ s/~[^~]*$//;
  158. $b =~ s/~[^~]*$//;
  159. }
  160. return version_compare($a, $b);
  161. }
  162. # Merge changelog entries smartly by merging individually the different
  163. # parts constituting an entry
  164. sub merge_entries($$$) {
  165. my ($o, $a, $b) = @_;
  166. # NOTE: Only $o can be undef
  167. # Merge the trailer line
  168. unless (merge_entry_item("blank_after_trailer", $o, $a, $b)) {
  169. unshift @result, "";
  170. }
  171. unless (merge_entry_item("trailer", $o, $a, $b)) {
  172. merge_conflict($a->get_part('trailer'), $b->get_part('trailer'));
  173. }
  174. # Merge the changes
  175. unless (merge_entry_item("blank_after_changes", $o, $a, $b)) {
  176. unshift @result, "";
  177. }
  178. my @merged = merge(defined $o ? $o->get_part('changes') : [],
  179. $a->get_part('changes'), $b->get_part('changes'),
  180. {
  181. CONFLICT => sub {
  182. $exitcode = 1;
  183. return get_conflict_block($_[0], $_[1]);
  184. }
  185. });
  186. unshift @result, @merged;
  187. # Merge the header line
  188. unless (merge_entry_item("blank_after_header", $o, $a, $b)) {
  189. unshift @result, "";
  190. }
  191. unless (merge_entry_item("header", $o, $a, $b)) {
  192. merge_conflict($a->get_part('header'), $b->get_part('header'));
  193. }
  194. }
  195. sub join_lines($) {
  196. my $array = shift;
  197. return join("\n", @$array) if ref($array) eq "ARRAY";
  198. return $array;
  199. }
  200. # Try to merge the obvious cases, return 1 on success and 0 on failure
  201. # O A B
  202. # - x x => x
  203. # o o b => b
  204. # - - b => b
  205. # o a o => a
  206. # - a - => a
  207. sub merge_block($$$;&) {
  208. my ($o, $a, $b, $preprocess) = @_;
  209. $preprocess = \&join_lines unless defined $preprocess;
  210. $o = &$preprocess($o) if defined($o);
  211. $a = &$preprocess($a) if defined($a);
  212. $b = &$preprocess($b) if defined($b);
  213. return 1 if not defined($a) and not defined($b);
  214. if (defined($a) and defined($b) and ($a eq $b)) {
  215. unshift @result, $a;
  216. } elsif ((defined($a) and defined($o) and ($a eq $o)) or
  217. (not defined($a) and not defined($o))) {
  218. unshift @result, $b if defined $b;
  219. } elsif ((defined($b) and defined($o) and ($b eq $o)) or
  220. (not defined($b) and not defined($o))) {
  221. unshift @result, $a if defined $a;
  222. } else {
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. sub merge_entry_item($$$$) {
  228. my ($item, $o, $a, $b) = @_;
  229. if (blessed($o) and $o->isa('Dpkg::Changelog::Entry')) {
  230. $o = $o->get_part($item);
  231. } elsif (ref $o) {
  232. $o = $o->{$item};
  233. }
  234. if (blessed($a) and $a->isa('Dpkg::Changelog::Entry')) {
  235. $a = $a->get_part($item);
  236. } elsif (ref $a) {
  237. $a = $a->{$item};
  238. }
  239. if (blessed($b) and $b->isa('Dpkg::Changelog::Entry')) {
  240. $b = $b->get_part($item);
  241. } elsif (ref $b) {
  242. $b = $b->{$item};
  243. }
  244. return merge_block($o, $a, $b);
  245. }
  246. sub merge_conflict($$) {
  247. my ($a, $b) = @_;
  248. unshift @result, get_conflict_block($a, $b);
  249. $exitcode = 1;
  250. }
  251. sub get_conflict_block($$) {
  252. my ($a, $b) = @_;
  253. my (@a, @b);
  254. push @a, $a if defined $a;
  255. push @b, $b if defined $b;
  256. @a = @{$a} if ref($a) eq "ARRAY";
  257. @b = @{$b} if ref($b) eq "ARRAY";
  258. return ("<<<<<<", @a, "======", @b, ">>>>>>");
  259. }