dpkg-genchanges.pl 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-genchanges
  4. #
  5. # Copyright © 1996 Ian Jackson
  6. # Copyright © 2000,2001 Wichert Akkerman
  7. # Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. use strict;
  22. use warnings;
  23. use Carp;
  24. use Encode;
  25. use POSIX qw(:errno_h);
  26. use Dpkg ();
  27. use Dpkg::Gettext;
  28. use Dpkg::Util qw(:list);
  29. use Dpkg::File;
  30. use Dpkg::Checksums;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::BuildProfiles qw(get_build_profiles);
  33. use Dpkg::Arch qw(get_host_arch debarch_eq debarch_is);
  34. use Dpkg::Compression;
  35. use Dpkg::Control::Info;
  36. use Dpkg::Control::Fields;
  37. use Dpkg::Control;
  38. use Dpkg::Substvars;
  39. use Dpkg::Vars;
  40. use Dpkg::Changelog::Parse;
  41. use Dpkg::Dist::Files;
  42. use Dpkg::Version;
  43. textdomain('dpkg-dev');
  44. my $controlfile = 'debian/control';
  45. my $changelogfile = 'debian/changelog';
  46. my $changelogformat;
  47. my $fileslistfile = 'debian/files';
  48. my $uploadfilesdir = '..';
  49. my $sourcestyle = 'i';
  50. my $quiet = 0;
  51. my $host_arch = get_host_arch();
  52. my $changes_format = '1.8';
  53. my %p2f; # - package to file map, has entries for "packagename"
  54. my %p2arch; # - package to arch map
  55. my %f2seccf; # - package to section map, from control file
  56. my %f2pricf; # - package to priority map, from control file
  57. my %sourcedefault; # - default values as taken from source (used for Section,
  58. # Priority and Maintainer)
  59. my @descriptions;
  60. my $checksums = Dpkg::Checksums->new();
  61. my %remove; # - fields to remove
  62. my %override;
  63. my %archadded;
  64. my @archvalues;
  65. my $dsc;
  66. my $changesdescription;
  67. my $forcemaint;
  68. my $forcechangedby;
  69. my $since;
  70. my $substvars_loaded = 0;
  71. my $substvars = Dpkg::Substvars->new();
  72. $substvars->set_as_auto('Format', $changes_format);
  73. use constant BUILD_SOURCE => 1;
  74. use constant BUILD_ARCH_DEP => 2;
  75. use constant BUILD_ARCH_INDEP => 4;
  76. use constant BUILD_BINARY => BUILD_ARCH_DEP | BUILD_ARCH_INDEP;
  77. use constant BUILD_SOURCE_DEP => BUILD_SOURCE | BUILD_ARCH_DEP;
  78. use constant BUILD_SOURCE_INDEP => BUILD_SOURCE | BUILD_ARCH_INDEP;
  79. use constant BUILD_ALL => BUILD_BINARY | BUILD_SOURCE;
  80. my $include = BUILD_ALL;
  81. sub build_is_default() { return ($include & BUILD_ALL) == BUILD_ALL; }
  82. sub build_opt {
  83. if ($include == BUILD_BINARY) {
  84. return '-b';
  85. } elsif ($include == BUILD_ARCH_DEP) {
  86. return '-B';
  87. } elsif ($include == BUILD_ARCH_INDEP) {
  88. return '-A';
  89. } elsif ($include == BUILD_SOURCE) {
  90. return '-S';
  91. } elsif ($include == BUILD_SOURCE_DEP) {
  92. return '-G';
  93. } elsif ($include == BUILD_SOURCE_INDEP) {
  94. return '-g';
  95. } else {
  96. croak "build_opt called with include=$include";
  97. }
  98. }
  99. sub set_build_type
  100. {
  101. my ($build_type, $build_option) = @_;
  102. usageerr(_g('cannot combine %s and %s'), build_opt(), $build_option)
  103. if not build_is_default and $include != $build_type;
  104. $include = $build_type;
  105. }
  106. sub version {
  107. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  108. printf _g('
  109. This is free software; see the GNU General Public License version 2 or
  110. later for copying conditions. There is NO warranty.
  111. ');
  112. }
  113. sub usage {
  114. printf _g(
  115. 'Usage: %s [<option>...]')
  116. . "\n\n" . _g(
  117. "Options:
  118. -g source and arch-indep build.
  119. -G source and arch-specific build.
  120. -b binary-only, no source files.
  121. -B binary-only, only arch-specific files.
  122. -A binary-only, only arch-indep files.
  123. -S source-only, no binary files.
  124. -c<control-file> get control info from this file.
  125. -l<changelog-file> get per-version info from this file.
  126. -f<files-list-file> get .deb files list from this file.
  127. -v<since-version> include all changes later than version.
  128. -C<changes-description> use change description from this file.
  129. -m<maintainer> override control's maintainer value.
  130. -e<maintainer> override changelog's maintainer value.
  131. -u<upload-files-dir> directory with files (default is '..').
  132. -si (default) source includes orig, if new upstream.
  133. -sa source includes orig, always.
  134. -sd source is diff and .dsc only.
  135. -q quiet - no informational messages on stderr.
  136. -F<changelog-format> force changelog format.
  137. -V<name>=<value> set a substitution variable.
  138. -T<substvars-file> read variables here, not debian/substvars.
  139. -D<field>=<value> override or add a field and value.
  140. -U<field> remove a field.
  141. -?, --help show this help message.
  142. --version show the version.
  143. "), $Dpkg::PROGNAME;
  144. }
  145. while (@ARGV) {
  146. $_=shift(@ARGV);
  147. if (m/^-b$/) {
  148. set_build_type(BUILD_BINARY, $_);
  149. } elsif (m/^-B$/) {
  150. set_build_type(BUILD_ARCH_DEP, $_);
  151. } elsif (m/^-A$/) {
  152. set_build_type(BUILD_ARCH_INDEP, $_);
  153. } elsif (m/^-S$/) {
  154. set_build_type(BUILD_SOURCE, $_);
  155. } elsif (m/^-G$/) {
  156. set_build_type(BUILD_SOURCE_DEP, $_);
  157. } elsif (m/^-g$/) {
  158. set_build_type(BUILD_SOURCE_INDEP, $_);
  159. } elsif (m/^-s([iad])$/) {
  160. $sourcestyle= $1;
  161. } elsif (m/^-q$/) {
  162. $quiet= 1;
  163. } elsif (m/^-c(.*)$/) {
  164. $controlfile = $1;
  165. } elsif (m/^-l(.*)$/) {
  166. $changelogfile = $1;
  167. } elsif (m/^-C(.*)$/) {
  168. $changesdescription = $1;
  169. } elsif (m/^-f(.*)$/) {
  170. $fileslistfile = $1;
  171. } elsif (m/^-v(.*)$/) {
  172. $since = $1;
  173. } elsif (m/^-T(.*)$/) {
  174. $substvars->load($1) if -e $1;
  175. $substvars_loaded = 1;
  176. } elsif (m/^-m(.*)$/s) {
  177. $forcemaint = $1;
  178. } elsif (m/^-e(.*)$/s) {
  179. $forcechangedby = $1;
  180. } elsif (m/^-F([0-9a-z]+)$/) {
  181. $changelogformat = $1;
  182. } elsif (m/^-D([^\=:]+)[=:](.*)$/s) {
  183. $override{$1} = $2;
  184. } elsif (m/^-u(.*)$/) {
  185. $uploadfilesdir = $1;
  186. } elsif (m/^-U([^\=:]+)$/) {
  187. $remove{$1} = 1;
  188. } elsif (m/^-V(\w[-:0-9A-Za-z]*)[=:](.*)$/s) {
  189. $substvars->set($1, $2);
  190. } elsif (m/^-(?:\?|-help)$/) {
  191. usage();
  192. exit(0);
  193. } elsif (m/^--version$/) {
  194. version();
  195. exit(0);
  196. } else {
  197. usageerr(_g("unknown option \`%s'"), $_);
  198. }
  199. }
  200. # Retrieve info from the current changelog entry
  201. my %options = (file => $changelogfile);
  202. $options{changelogformat} = $changelogformat if $changelogformat;
  203. $options{since} = $since if defined($since);
  204. my $changelog = changelog_parse(%options);
  205. # Change options to retrieve info of the former changelog entry
  206. delete $options{since};
  207. $options{count} = 1;
  208. $options{offset} = 1;
  209. my $prev_changelog = changelog_parse(%options);
  210. # Other initializations
  211. my $control = Dpkg::Control::Info->new($controlfile);
  212. my $fields = Dpkg::Control->new(type => CTRL_FILE_CHANGES);
  213. my $sourceversion = $changelog->{'Binary-Only'} ?
  214. $prev_changelog->{'Version'} : $changelog->{'Version'};
  215. my $binaryversion = $changelog->{'Version'};
  216. $substvars->set_version_substvars($sourceversion, $binaryversion);
  217. $substvars->set_arch_substvars();
  218. $substvars->load('debian/substvars') if -e 'debian/substvars' and not $substvars_loaded;
  219. if (defined($prev_changelog) and
  220. version_compare_relation($changelog->{'Version'}, REL_LT,
  221. $prev_changelog->{'Version'}))
  222. {
  223. warning(_g('the current version (%s) is earlier than the previous one (%s)'),
  224. $changelog->{'Version'}, $prev_changelog->{'Version'})
  225. # ~bpo and ~vola are backports and have lower version number by definition
  226. unless $changelog->{'Version'} =~ /~(?:bpo|vola)/;
  227. }
  228. my $dist = Dpkg::Dist::Files->new();
  229. if ($include & BUILD_BINARY) {
  230. my $dist_count = 0;
  231. $dist_count = $dist->load($fileslistfile) if -e $fileslistfile;
  232. error(_g('binary build with no binary artifacts found; cannot distribute'))
  233. if $dist_count == 0;
  234. foreach my $file ($dist->get_files()) {
  235. if (defined $file->{package} && $file->{package_type} =~ m/^u?deb$/) {
  236. $p2f{$file->{package}} //= [];
  237. push @{$p2f{$file->{package}}}, $file->{filename};
  238. }
  239. if (defined $file->{arch}) {
  240. push @archvalues, $file->{arch}
  241. if $file->{arch} and not $archadded{$file->{arch}}++;
  242. }
  243. }
  244. }
  245. # Scan control info of source package
  246. my $src_fields = $control->get_source();
  247. foreach (keys %{$src_fields}) {
  248. my $v = $src_fields->{$_};
  249. if (m/^Source$/) {
  250. set_source_package($v);
  251. } elsif (m/^Section$|^Priority$/i) {
  252. $sourcedefault{$_} = $v;
  253. } else {
  254. field_transfer_single($src_fields, $fields);
  255. }
  256. }
  257. # Scan control info of all binary packages
  258. foreach my $pkg ($control->get_packages()) {
  259. my $p = $pkg->{'Package'};
  260. my $a = $pkg->{'Architecture'} // '';
  261. my $d = $pkg->{'Description'} || 'no description available';
  262. $d = $1 if $d =~ /^(.*)\n/;
  263. my $pkg_type = $pkg->{'Package-Type'} ||
  264. $pkg->get_custom_field('Package-Type') || 'deb';
  265. my @f; # List of files for this binary package
  266. push @f, @{$p2f{$p}} if defined $p2f{$p};
  267. # Add description of all binary packages
  268. my $desc = encode_utf8(sprintf('%-10s - %-.65s', $p, decode_utf8($d)));
  269. $desc .= ' (udeb)' if $pkg_type eq 'udeb';
  270. push @descriptions, $desc;
  271. if (not defined($p2f{$p})) {
  272. # No files for this package... warn if it's unexpected
  273. if ((debarch_eq('all', $a) and ($include & BUILD_ARCH_INDEP)) ||
  274. ((any { debarch_is($host_arch, $_) } split /\s+/, $a)
  275. and ($include & BUILD_ARCH_DEP))) {
  276. warning(_g('package %s in control file but not in files list'),
  277. $p);
  278. }
  279. next; # and skip it
  280. }
  281. $p2arch{$p} = $a;
  282. foreach (keys %{$pkg}) {
  283. my $v = $pkg->{$_};
  284. if (m/^Section$/) {
  285. $f2seccf{$_} = $v foreach (@f);
  286. } elsif (m/^Priority$/) {
  287. $f2pricf{$_} = $v foreach (@f);
  288. } elsif (m/^Architecture$/) {
  289. if ((any { debarch_is($host_arch, $_) } split /\s+/, $v)
  290. and ($include & BUILD_ARCH_DEP)) {
  291. $v = $host_arch;
  292. } elsif (!debarch_eq('all', $v)) {
  293. $v = '';
  294. }
  295. push(@archvalues, $v) if $v and not $archadded{$v}++;
  296. } elsif (m/^Description$/) {
  297. # Description in changes is computed, do not copy this field
  298. } else {
  299. field_transfer_single($pkg, $fields);
  300. }
  301. }
  302. }
  303. # Scan fields of dpkg-parsechangelog
  304. foreach (keys %{$changelog}) {
  305. my $v = $changelog->{$_};
  306. if (m/^Source$/i) {
  307. set_source_package($v);
  308. } elsif (m/^Maintainer$/i) {
  309. $fields->{'Changed-By'} = $v;
  310. } else {
  311. field_transfer_single($changelog, $fields);
  312. }
  313. }
  314. if ($changesdescription) {
  315. open(my $changes_fh, '<', $changesdescription)
  316. or syserr(_g('read changesdescription'));
  317. $fields->{'Changes'} = "\n" . file_slurp($changes_fh);
  318. close($changes_fh);
  319. }
  320. for my $p (keys %p2f) {
  321. warning(_g('package %s listed in files list but not in control info'), $p)
  322. unless defined $control->get_pkg_by_name($p);
  323. }
  324. for my $p (keys %p2f) {
  325. my @f = @{$p2f{$p}};
  326. foreach my $f (@f) {
  327. my $file = $dist->get_file($f);
  328. my $sec = $f2seccf{$f};
  329. $sec ||= $sourcedefault{'Section'};
  330. if (!defined($sec)) {
  331. $sec = '-';
  332. warning(_g("missing Section for binary package %s; using '-'"), $p);
  333. }
  334. if ($sec ne $file->{section}) {
  335. error(_g('package %s has section %s in control file but %s in ' .
  336. 'files list'), $p, $sec, $file->{section});
  337. }
  338. my $pri = $f2pricf{$f};
  339. $pri ||= $sourcedefault{'Priority'};
  340. if (!defined($pri)) {
  341. $pri = '-';
  342. warning(_g("missing Priority for binary package %s; using '-'"), $p);
  343. }
  344. if ($pri ne $file->{priority}) {
  345. error(_g('package %s has priority %s in control file but %s in ' .
  346. 'files list'), $p, $pri, $file->{priority});
  347. }
  348. }
  349. }
  350. my $origsrcmsg;
  351. if ($include & BUILD_SOURCE) {
  352. my $sec = $sourcedefault{'Section'};
  353. if (!defined($sec)) {
  354. $sec = '-';
  355. warning(_g('missing Section for source files'));
  356. }
  357. my $pri = $sourcedefault{'Priority'};
  358. if (!defined($pri)) {
  359. $pri = '-';
  360. warning(_g('missing Priority for source files'));
  361. }
  362. my $spackage = get_source_package();
  363. (my $sversion = $substvars->get('source:Version')) =~ s/^\d+://;
  364. $dsc= "$uploadfilesdir/${spackage}_${sversion}.dsc";
  365. my $dsc_fields = Dpkg::Control->new(type => CTRL_PKG_SRC);
  366. $dsc_fields->load($dsc) or error(_g('%s is empty', $dsc));
  367. $checksums->add_from_file($dsc, key => "$spackage\_$sversion.dsc");
  368. $checksums->add_from_control($dsc_fields, use_files_for_md5 => 1);
  369. # Compare upstream version to previous upstream version to decide if
  370. # the .orig tarballs must be included
  371. my $include_tarball;
  372. if (defined($prev_changelog)) {
  373. my $cur = Dpkg::Version->new($changelog->{'Version'});
  374. my $prev = Dpkg::Version->new($prev_changelog->{'Version'});
  375. $include_tarball = ($cur->version() ne $prev->version()) ? 1 : 0;
  376. } else {
  377. # No previous entry means first upload, tarball required
  378. $include_tarball = 1;
  379. }
  380. my $ext = compression_get_file_extension_regex();
  381. if ((($sourcestyle =~ m/i/ && !$include_tarball) ||
  382. $sourcestyle =~ m/d/) &&
  383. any { m/\.(?:debian\.tar|diff)\.$ext$/ } $checksums->get_files())
  384. {
  385. $origsrcmsg= _g('not including original source code in upload');
  386. foreach my $f (grep { m/\.orig(-.+)?\.tar\.$ext$/ } $checksums->get_files()) {
  387. $checksums->remove_file($f);
  388. }
  389. } else {
  390. if ($sourcestyle =~ m/d/ &&
  391. none { m/\.(?:debian\.tar|diff)\.$ext$/ } $checksums->get_files()) {
  392. warning(_g('ignoring -sd option for native Debian package'));
  393. }
  394. $origsrcmsg= _g('including full source code in upload');
  395. }
  396. # Only add attributes for files being distributed.
  397. for my $f ($checksums->get_files()) {
  398. $dist->add_file($f, $sec, $pri);
  399. }
  400. } elsif ($include == BUILD_ARCH_DEP) {
  401. $origsrcmsg = _g('binary-only arch-specific upload ' .
  402. '(source code and arch-indep packages not included)');
  403. } elsif ($include == BUILD_ARCH_INDEP) {
  404. $origsrcmsg = _g('binary-only arch-indep upload ' .
  405. '(source code and arch-specific packages not included)');
  406. } else {
  407. $origsrcmsg = _g('binary-only upload (no source code included)');
  408. }
  409. print { *STDERR } "$Dpkg::PROGNAME: $origsrcmsg\n"
  410. or syserr(_g('write original source message')) unless $quiet;
  411. $fields->{'Format'} = $substvars->get('Format');
  412. if (!defined($fields->{'Date'})) {
  413. chomp(my $date822 = `date -R`);
  414. subprocerr('date -R') if $?;
  415. $fields->{'Date'}= $date822;
  416. }
  417. $fields->{'Binary'} = join(' ', map { $_->{'Package'} } $control->get_packages());
  418. # Avoid overly long line by splitting over multiple lines
  419. if (length($fields->{'Binary'}) > 980) {
  420. $fields->{'Binary'} =~ s/(.{0,980}) /$1\n/g;
  421. }
  422. unshift @archvalues, 'source' if $include & BUILD_SOURCE;
  423. @archvalues = ('all') if $include == BUILD_ARCH_INDEP;
  424. @archvalues = grep { !debarch_eq('all', $_) } @archvalues
  425. unless $include & BUILD_ARCH_INDEP;
  426. @archvalues = grep { !debarch_eq($host_arch, $_) } @archvalues
  427. unless $include & BUILD_ARCH_DEP;
  428. $fields->{'Architecture'} = join ' ', @archvalues;
  429. $fields->{'Built-For-Profiles'} = join ' ', get_build_profiles();
  430. $fields->{'Description'} = "\n" . join("\n", sort @descriptions);
  431. $fields->{'Files'} = '';
  432. for my $file ($dist->get_files()) {
  433. my $f = $file->{filename};
  434. if (defined $file->{package}) {
  435. my $arch_all = debarch_eq('all', $p2arch{$file->{package}});
  436. next if (not ($include & BUILD_ARCH_INDEP) and $arch_all);
  437. next if (not ($include & BUILD_ARCH_DEP) and not $arch_all);
  438. }
  439. $checksums->add_from_file("$uploadfilesdir/$f", key => $f);
  440. $fields->{'Files'} .= "\n" . $checksums->get_checksum($f, 'md5') .
  441. ' ' . $checksums->get_size($f) .
  442. " $file->{section} $file->{priority} $f";
  443. }
  444. $checksums->export_to_control($fields);
  445. # redundant with the Files field
  446. delete $fields->{'Checksums-Md5'};
  447. $fields->{'Source'} = get_source_package();
  448. if ($fields->{'Version'} ne $substvars->get('source:Version')) {
  449. $fields->{'Source'} .= ' (' . $substvars->get('source:Version') . ')';
  450. }
  451. $fields->{'Maintainer'} = $forcemaint if defined($forcemaint);
  452. $fields->{'Changed-By'} = $forcechangedby if defined($forcechangedby);
  453. for my $f (qw(Version Distribution Maintainer Changes)) {
  454. error(_g('missing information for critical output field %s'), $f)
  455. unless defined $fields->{$f};
  456. }
  457. for my $f (qw(Urgency)) {
  458. warning(_g('missing information for output field %s'), $f)
  459. unless defined $fields->{$f};
  460. }
  461. for my $f (keys %override) {
  462. $fields->{$f} = $override{$f};
  463. }
  464. for my $f (keys %remove) {
  465. delete $fields->{$f};
  466. }
  467. # Note: do not perform substitution of variables, one of the reasons is that
  468. # they could interfere with field values, for example the Changes field.
  469. $fields->output(\*STDOUT);