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
|
#!/usr/bin/perl
# Copyright © 2009-2010 Raphaël Hertzog <hertzog@debian.org>
# Copyright © 2012 Guillem Jover <guillem@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/>.
use warnings;
use strict;
use Scalar::Util qw(blessed);
use Getopt::Long qw(:config posix_default bundling_values no_ignorecase);
use Dpkg ();
use Dpkg::Changelog::Debian;
use Dpkg::ErrorHandling;
use Dpkg::Gettext;
use Dpkg::Version;
use Dpkg::Vendor qw(run_vendor_hook);
textdomain('dpkg-dev');
sub merge_entries($$$);
sub merge_block($$$;&);
sub merge_entry_item($$$$);
sub merge_conflict($$);
sub get_conflict_block($$);
sub join_lines($);
BEGIN {
eval q{
pop @INC if $INC[-1] eq '.';
use Algorithm::Merge qw(merge);
};
if ($@) {
*merge = sub {
my ($o, $a, $b) = @_;
return @$a if join("\n", @$a) eq join("\n", @$b);
return get_conflict_block($a, $b);
};
}
}
sub version {
printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
printf "\n" . g_(
'This is free software; see the GNU General Public License version 2 or
later for copying conditions. There is NO warranty.
');
}
sub usage {
printf g_(
"Usage: %s [<option>...] <old> <new-a> <new-b> [<out>]
Options:
-m, --merge-prereleases merge pre-releases together, ignores everything
after the last '~' in the version.
--merge-unreleased merge UNRELEASED entries together, ignoring their
version numbers.
-?, --help show this help message.
--version show the version.
"), $Dpkg::PROGNAME;
}
my $merge_prereleases;
my $merge_unreleased;
my @options_spec = (
'help|?' => sub { usage(); exit(0) },
'version' => sub { version(); exit(0) },
'merge-prereleases|m' => \$merge_prereleases,
'merge-unreleased' => \$merge_unreleased,
);
{
local $SIG{__WARN__} = sub { usageerr($_[0]) };
GetOptions(@options_spec);
}
my $backport_version_regex = run_vendor_hook('backport-version-regex');
my ($old, $new_a, $new_b, $out_file) = @ARGV;
unless (defined $old and defined $new_a and defined $new_b)
{
usageerr(g_('needs at least three arguments'));
}
unless (-e $old and -e $new_a and -e $new_b)
{
usageerr(g_('file arguments need to exist'));
}
my ($cho, $cha, $chb);
$cho = Dpkg::Changelog::Debian->new();
$cho->load($old);
$cha = Dpkg::Changelog::Debian->new();
$cha->load($new_a);
$chb = Dpkg::Changelog::Debian->new();
$chb->load($new_b);
my @o = reverse @$cho;
my @a = reverse @$cha;
my @b = reverse @$chb;
my @result; # Lines to output
my $exitcode = 0; # 1 if conflict encountered
unless (merge_block($cho, $cha, $chb, sub {
my $changes = shift;
my $tail = $changes->get_unparsed_tail();
chomp $tail if defined $tail;
return $tail;
}))
{
merge_conflict($cha->get_unparsed_tail(), $chb->get_unparsed_tail());
}
while (1) {
my ($o, $a, $b) = get_items_to_merge();
last unless defined $o or defined $a or defined $b;
next if merge_block($o, $a, $b);
# We only have the usually conflicting cases left
if (defined $a and defined $b) {
# Same entry, merge sub-items separately for a nicer result
merge_entries($o, $a, $b);
} else {
# Non-existing on one side, changed on the other side
merge_conflict($a, $b);
}
}
if (defined($out_file) and $out_file ne '-') {
open(my $out_fh, '>', $out_file)
or syserr(g_('cannot write %s'), $out_file);
print { $out_fh } ((blessed $_) ? "$_" : "$_\n") foreach @result;
close($out_fh) or syserr(g_('cannot write %s'), $out_file);
} else {
print ((blessed $_) ? "$_" : "$_\n") foreach @result;
}
exit $exitcode;
# Returns the next items to merge, all items returned correspond to the
# same minimal version among the 3 possible next items (undef is returned
# if the next item on the given changelog is skipped)
sub get_items_to_merge {
my @items = (shift @o, shift @a, shift @b);
my @arrays = (\@o, \@a, \@b);
my $minitem;
foreach my $i (0 .. 2) {
if (defined $minitem and defined $items[$i]) {
my $cmp = compare_versions($minitem, $items[$i]);
if ($cmp > 0) {
$minitem = $items[$i];
foreach my $j (0 .. $i - 1) {
unshift @{$arrays[$j]}, $items[$j];
$items[$j] = undef;
}
} elsif ($cmp < 0) {
unshift @{$arrays[$i]}, $items[$i];
$items[$i] = undef;
}
} else {
$minitem = $items[$i] if defined $items[$i];
}
}
return @items;
}
# Compares the versions taking into account some oddities like the fact
# that we want backport versions to sort higher than the version
# on which they are based.
sub compare_versions {
my ($a, $b) = @_;
return 0 if not defined $a and not defined $b;
return 1 if not defined $b;
return -1 if not defined $a;
my ($av, $bv) = ($a, $b);
$av = $a->get_version() if ref $a and $a->isa('Dpkg::Changelog::Entry');
$bv = $b->get_version() if ref $b and $b->isa('Dpkg::Changelog::Entry');
if ($merge_unreleased) {
return 0 if $a->get_distributions() eq 'UNRELEASED' and
$b->get_distributions() eq 'UNRELEASED';
}
# Backports are not real prereleases.
if (defined $backport_version_regex) {
$a =~ s/$backport_version_regex/+$1/;
$b =~ s/$backport_version_regex/+$1/;
}
if ($merge_prereleases) {
$av =~ s/~[^~]*$//;
$bv =~ s/~[^~]*$//;
}
$av = Dpkg::Version->new($av);
$bv = Dpkg::Version->new($bv);
return $av <=> $bv;
}
# Merge changelog entries smartly by merging individually the different
# parts constituting an entry
sub merge_entries($$$) {
my ($o, $a, $b) = @_;
# NOTE: Only $o can be undef
# Merge the trailer line
unless (merge_entry_item('blank_after_trailer', $o, $a, $b)) {
unshift @result, '';
}
unless (merge_entry_item('trailer', $o, $a, $b)) {
merge_conflict($a->get_part('trailer'), $b->get_part('trailer'));
}
# Merge the changes
unless (merge_entry_item('blank_after_changes', $o, $a, $b)) {
unshift @result, '';
}
my @merged = merge(defined $o ? $o->get_part('changes') : [],
$a->get_part('changes'), $b->get_part('changes'),
{
CONFLICT => sub {
my ($ca, $cb) = @_;
$exitcode = 1;
return get_conflict_block($ca, $cb);
}
});
unshift @result, @merged;
# Merge the header line
unless (merge_entry_item('blank_after_header', $o, $a, $b)) {
unshift @result, '';
}
unless (merge_entry_item('header', $o, $a, $b)) {
merge_conflict($a->get_part('header'), $b->get_part('header'));
}
}
sub join_lines($) {
my $array = shift;
return join("\n", @$array) if ref($array) eq 'ARRAY';
return $array;
}
# Try to merge the obvious cases, return 1 on success and 0 on failure
# O A B
# - x x => x
# o o b => b
# - - b => b
# o a o => a
# - a - => a
sub merge_block($$$;&) {
my ($o, $a, $b, $preprocess) = @_;
$preprocess //= \&join_lines;
$o = $preprocess->($o) if defined $o;
$a = $preprocess->($a) if defined $a;
$b = $preprocess->($b) if defined $b;
return 1 if not defined($a) and not defined($b);
if (defined($a) and defined($b) and ($a eq $b)) {
unshift @result, $a;
} elsif ((defined($a) and defined($o) and ($a eq $o)) or
(not defined($a) and not defined($o))) {
unshift @result, $b if defined $b;
} elsif ((defined($b) and defined($o) and ($b eq $o)) or
(not defined($b) and not defined($o))) {
unshift @result, $a if defined $a;
} else {
return 0;
}
return 1;
}
sub merge_entry_item($$$$) {
my ($item, $o, $a, $b) = @_;
if (blessed($o) and $o->isa('Dpkg::Changelog::Entry')) {
$o = $o->get_part($item);
} elsif (ref $o) {
$o = $o->{$item};
}
if (blessed($a) and $a->isa('Dpkg::Changelog::Entry')) {
$a = $a->get_part($item);
} elsif (ref $a) {
$a = $a->{$item};
}
if (blessed($b) and $b->isa('Dpkg::Changelog::Entry')) {
$b = $b->get_part($item);
} elsif (ref $b) {
$b = $b->{$item};
}
return merge_block($o, $a, $b);
}
sub merge_conflict($$) {
my ($a, $b) = @_;
unshift @result, get_conflict_block($a, $b);
$exitcode = 1;
}
sub get_conflict_block($$) {
my ($a, $b) = @_;
my (@a, @b);
push @a, $a if defined $a;
push @b, $b if defined $b;
@a = @{$a} if ref($a) eq 'ARRAY';
@b = @{$b} if ref($b) eq 'ARRAY';
return ('<<<<<<<', @a, '=======', @b, '>>>>>>>');
}
|