summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Check/Nmu.pm
blob: a758728d910c595d133f2a4586f6eb8a16315513 (plain)
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
# nmu -- lintian check script -*- perl -*-

# Copyright (C) 2004 Jeroen van Wolffelaar
# Copyright (C) 2017-2019 Chris Lamb <lamby@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, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::Check::Nmu;

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use List::SomeUtils qw(any);
use List::Util qw(first);

use Moo;
use namespace::clean;

with 'Lintian::Check';

const my $EMPTY => q{};

sub source {
    my ($self) = @_;

    my $processable = $self->processable;

    my $changelog_mentions_nmu = 0;
    my $changelog_mentions_local = 0;
    my $changelog_mentions_qa = 0;
    my $changelog_mentions_team_upload = 0;

    my $debian_dir = $processable->patched->resolve_path('debian/');

    my $chf;
    $chf = $debian_dir->child('changelog') if $debian_dir;

    # This isn't really an NMU check, but right now no other check
    # looks at debian/changelog in source packages.  Catch a
    # debian/changelog file that's a symlink.
    $self->pointed_hint('changelog-is-symlink', $chf->pointer)
      if $chf && $chf->is_symlink;

    return
      unless $processable->changelog;

    # Get some data from the changelog file.
    my ($entry) = @{$processable->changelog->entries};

    my $pointer = $chf->pointer($entry->position);

    my $uploader = canonicalize($entry->Maintainer // $EMPTY);

    # trim both ends
    $self->pointed_hint('extra-whitespace-around-name-in-changelog-trailer',
        $pointer)
      if $uploader =~ s/^\s+|\s+$//g;

    my $changes = $entry->Changes;
    $changes =~ s/^(\s*\n)+//;
    my $firstline = first { /^\s*\*/ } split(/\n/, $changes);

    # Check the first line for QA, NMU or team upload mentions.
    if ($firstline) {
        local $_ = $firstline;
        if (/\bnmu\b/i or /non-maintainer upload/i or m/LowThresholdNMU/i) {
            unless (
                m{
                        (?:ackno|\back\b|confir|incorporat).*
                        (?:\bnmu\b|non-maintainer)}xi
            ) {
                $changelog_mentions_nmu = 1;
            }
        }
        $changelog_mentions_local = 1 if /\blocal\s+package\b/i;
        $changelog_mentions_qa = 1 if /orphan/i or /qa (?:group )?upload/i;
        $changelog_mentions_team_upload = 1 if /team upload/i;
    }

    # If the version field is missing, assume it to be a native,
    # maintainer upload as it is probably the most likely case.
    my $version = $processable->fields->value('Version') || '0-1';
    my $maintainer= canonicalize($processable->fields->value('Maintainer'));
    my $uploaders = $processable->fields->value('Uploaders');

    my $version_nmuness = 0;
    my $version_local = 0;
    my $upload_is_backport = $version =~ m/~bpo(\d+)\+(\d+)$/;
    my $upload_is_stable_update = $version =~ m/~deb(\d+)u(\d+)$/;

    if ($version =~ /-[^.-]+(\.[^.-]+)?(\.[^.-]+)?$/) {
        $version_nmuness = 1 if defined $1;
        $version_nmuness = 2 if defined $2;
    }
    if ($version =~ /\+nmu\d+$/) {
        $version_nmuness = 1;
    }
    if ($version =~ /\+b\d+$/) {
        $version_nmuness = 2;
    }
    if ($version =~ /local/i) {
        $version_local = 1;
    }

    my $upload_is_nmu = $uploader ne $maintainer;

    my @uploaders = map { canonicalize($_) } split />\K\s*,\s*/,$uploaders;
    $upload_is_nmu = 0 if any { $_ eq $uploader } @uploaders;

    # If the changelog entry is missing a maintainer (eg. "-- <blank>")
    # assume it's an upload still work in progress.
    $upload_is_nmu = 0 if not $uploader;

    if ($maintainer =~ /packages\@qa.debian.org/) {

        $self->pointed_hint('uploaders-in-orphan', $pointer)
          if $processable->fields->declares('Uploaders');

        $self->pointed_hint('qa-upload-has-incorrect-version-number',
            $pointer, $version)
          if $version_nmuness == 1;

        $self->pointed_hint('no-qa-in-changelog', $pointer)
          unless $changelog_mentions_qa;

    } elsif ($changelog_mentions_team_upload) {

        $self->pointed_hint('team-upload-has-incorrect-version-number',
            $pointer, $version)
          if $version_nmuness == 1;

        $self->pointed_hint('unnecessary-team-upload', $pointer)
          unless $upload_is_nmu;

    } else {
        # Local packages may be either NMUs or not.
        unless ($changelog_mentions_local || $version_local) {

            $self->pointed_hint('no-nmu-in-changelog', $pointer)
              if !$changelog_mentions_nmu && $upload_is_nmu;

            $self->pointed_hint('source-nmu-has-incorrect-version-number',
                $pointer, $version)
              if $upload_is_nmu
              && $version_nmuness != 1
              && !$upload_is_stable_update
              && !$upload_is_backport;
        }

        $self->pointed_hint('nmu-in-changelog', $pointer)
          if $changelog_mentions_nmu && !$upload_is_nmu;

        $self->pointed_hint('maintainer-upload-has-incorrect-version-number',
            $pointer, $version)
          if !$upload_is_nmu && $version_nmuness;
    }

    return;
}

# Canonicalize a maintainer address with respect to case.  E-mail addresses
# are case-insensitive in the right-hand side.
sub canonicalize {
    my ($maintainer) = @_;

    $maintainer =~ s/<([^>\@]+\@)([\w.-]+)>/<$1\L$2>/;

    return $maintainer;
}

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et