This file is indexed.

/usr/bin/patchedit is in pkg-perl-tools 0.42.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/perl

=pod

=head1 NAME

B<patchedit> - Edit headers of a patch according to DEP3

=head1 SYNOPSIS

B<patchedit> [I<command>] F<patchfile>

=head1 DESCRIPTION

B<patchedit> is a helper script for managing patch headers according to
L<http://dep.debian.net/deps/dep3/>.

=head1 COMMANDS

=over

=item I<edit> (default)

Opens F<patchfile> in EDITOR (or VISUAL or sensible-editor) and

=over

=item * checks all headers

=item * marks problems

=item * adds missing required headers with proposals for their values

=back

If no command is given, I<edit> is chosen automatically.

=item I<check>

Does a non-interactive check if the headers conform to DEP3. Prints the
results (missing required headers, wrong values, ...) to stdout.

=back

=head1 ARGUMENTS

=over

=item F<patchfile> (required)

The patch to work on.

=back

=head1 OPTIONS

=over

=item B<-f|--fix>

Tries to fix problems in the headers when editing/checking patches.

=item B<-o|--optional>

Also add/print missing optional headers.

=item B<-h|--help>

Help output.

=back

=head1 ENVIRONMENT

B<patchedit> respects DEBEMAIL (or EMAIL) and DEBFULLNAME (for new Author or
Reviewed-by headers).

=head1 NOTE

This script is not pkg-perl specific. It should go into I<devscripts>
eventually.

=head1 TODO

 * preserve the extra fields
 * preserve the order of the fields

=head1 COPYRIGHT AND LICENSE

Copyright 2010, Jozef Kutej <jkutej@cpan.org>.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

use strict;
use warnings;

use 5.010;

use Getopt::Long;
use Pod::Usage;
use Test::Builder;
use DateTime;
use List::MoreUtils 'none';
use User::pwent;

use open ':std', ':encoding(UTF-8)';

our @standard_fields = qw(
    Description
    Subject
    Origin
    Bug
    Forwarded
    Author
    From
    Reviewed-by
    Acked-by
    Last-Update
    Applied-Upstream
);

exit main() unless caller();

sub main {
    my $help;
    my $do_fix        = 0;
    my $also_optional = 0;
    GetOptions(
        'help|h'     => \$help,
        'optional|o' => \$also_optional,
        'fix|f'      => \$do_fix,
    ) or pod2usage;
    pod2usage if $help or !@ARGV;

    my $cmd = shift @ARGV;

    # make the edit default
    if ( $cmd !~ m{^(edit|check)$} ) {
        unshift @ARGV, $cmd;
        $cmd = 'edit';
    }

    my @patch_files = @ARGV;

    pod2usage if ( ( not $cmd ) or ( not @patch_files ) );

    foreach my $patch_file (@patch_files) {
        die 'no such file "' . $patch_file . '"'
            if not -f $patch_file;

        if ( $cmd eq 'edit' ) {
            fix_patch( $patch_file, $also_optional ) if $do_fix;
            edit_patch($patch_file);
        }
        elsif ( $cmd eq 'check' ) {
            check_patch( $patch_file, $also_optional );
            fix_patch( $patch_file, $also_optional ) if $do_fix;
        }
    }

    return 0;
}

sub fix_patch {
    my $patch         = shift;
    my $also_optional = shift;

    my $patch_struct  = _read_patch($patch);
    my $patch_content = $patch_struct->{fields};
    $patch_content->{'Description'} = "*** FIXME ***\n"
        unless $patch_content->{'Description'} || $patch_content->{'Subject'};
    $patch_content->{'Origin'} = "vendor\n"
        unless $patch_content->{'Origin'} || $patch_content->{'Author'};

    if ($also_optional) {
        $patch_content->{'Bug'} = "*** FIXME ***\n"
            unless scalar( grep {m/Bug-?/} keys %{$patch_content} );

        $patch_content->{'Forwarded'} ||= "*** FIXME ***\n";

        my $gecosname = getpwuid($<)->gecos;
        $gecosname =~ s/,.*//;
        my $authorname ||= ( $ENV{DEBFULLNAME} || $gecosname );
        my $authoremail ||= ( $ENV{DEBEMAIL} || $ENV{EMAIL} );
        my $author = $authorname . ' <' . $authoremail . '>';

        $patch_content->{'Author'} ||= "$author\n"
            unless $patch_content->{'Author'} || $patch_content->{'From'};
        $patch_content->{'Reviewed-by'} ||= "$author\n"
            unless $patch_content->{'Reviewed-by'}
            || $patch_content->{'Acked-by'};
        # stat[9] = mtime
        $patch_content->{'Last-Update'}
            = DateTime->from_epoch( epoch => ( stat($patch) )[9] )
            ->set_time_zone('local')->strftime('%Y-%m-%d')
            . "\n";
        $patch_content->{'Applied-Upstream'} ||= "*** FIXME ***\n";
    }

    _write_patch( $patch, $patch_struct );
}

sub _write_patch {
    my $patch         = shift;
    my $patch_struct  = shift;
    my $patch_content = $patch_struct->{fields};

    open( my $patch_fh, '>', $patch )
        or die 'failed to open "' . $patch . '" - ' . $!;
    print $patch_fh $patch_struct->{header}{head} if $patch_struct->{header};
    foreach my $key (@standard_fields) {
        if ( $patch_content->{$key} ) {
            print $patch_fh '# '
                if $patch_struct->{header};
            print $patch_fh $key, ': ', $patch_content->{$key};
        }
        if ( $key eq 'Bug' ) {
            foreach my $key ( grep {m/Bug-/} sort keys %{$patch_content} ) {
                print $patch_fh '# '
                    if $patch_struct->{header};
                print $patch_fh $key, ': ', $patch_content->{$key};
            }
        }
    }
    print $patch_fh $patch_struct->{header}{tail} if $patch_struct->{header};
    print $patch_fh "\n";
    print $patch_fh $patch_struct->{body};
    close($patch_fh);
}

sub check_patch {
    my $patch         = shift;
    my $also_optional = shift;

    my $patch_struct  = _read_patch($patch);
    my $patch_content = $patch_struct->{fields};
    my $tb            = Test::Builder->new;
    $tb->plan( 'tests' => 9 );
    $tb->ok( $patch_content->{'Description'} || $patch_content->{'Subject'},
        'has Description or Subject' );
    $tb->ok( $patch_content->{'Origin'} || $patch_content->{'Author'},
        'has Origin or Author' );

    if ($also_optional) {
        if ( scalar( grep {m/Bug-?/} keys %{$patch_content} ) ) {
            $tb->ok( 1, 'has Bug or Bug-???' );
        }
        else {
            $tb->todo_skip('Bug or Bug-??? missing');
        }
        if ( $patch_content->{'Forwarded'} ) {
            $tb->ok( 1, 'has Forwarded' );
        }
        else {
            $tb->todo_skip('Forwarded missing');
        }
        if ( $patch_content->{'Author'} || $patch_content->{'From'} ) {
            $tb->ok( 1, 'has Author or From' );
        }
        else {
            $tb->todo_skip('Author or From missing');
        }
        if ( $patch_content->{'Reviewed-by'} || $patch_content->{'Acked-by'} )
        {
            $tb->ok( 1, 'has Reviewed-by or Acked-by' );
        }
        else {
            $tb->todo_skip('Reviewed-by or Acked-by missing');
        }
        if ( $patch_content->{'Last-Update'} ) {
            $tb->ok( $patch_content->{'Last-Update'}, 'has Last-Update' );
        }
        else {
            $tb->todo_skip('Last-Update missing');
        }
        if ( $patch_content->{'Applied-Upstream'} ) {
            $tb->ok( 1, 'has Applied-Upstream' );
        }
        else {
            $tb->todo_skip('Applied-Upstream missing');
        }
    }
    else {
        $tb->skip('skipping optional') foreach ( 1 .. 6 );
    }
    my @extra_fields
        = grep { $_ ne '_patch' }    # _patch is ok
        grep {
        my $key = $_;
        none { $_ eq $key } @standard_fields
        }                            # grep out standard fields
        grep { not m/^Bug-/ }        # different bugs are fine
        keys %{$patch_content};
    if ( @extra_fields == 0 ) {
        $tb->ok( 1, 'no extra fields' );
    }
    else {
        $tb->skip( 'some extra fields - ' . join( ', ', @extra_fields ) );
        $tb->note(
            join( "\n",
                map { $_ . ': ' . $patch_content->{$_} } @extra_fields )
        );
    }
}

sub edit_patch {
    my $patch = shift;

    my $editor = $ENV{'EDITOR'} || $ENV{'VISUAL'} || '/usr/bin/editor';
    system( "$editor $patch" );
}

sub _read_patch {
    my $patch = shift;
    my %patch_content;

    open( my $patch_fh, '<', $patch )
        or die 'failed to open "' . $patch . '" - ' . $!;

# Peek at the first line and see if we are dealing with a normal patch or with
# dpatch. We assume that if the file starts with a shebang (#!) that we are
# dealing with dpatch.
    my $use_classic = 1;
    my $line        = <$patch_fh>;
    if ( $line =~ /^#!/ ) {
        $use_classic = 0;
    }

    # Rewind the file handle back to the beeking.
    seek $patch_fh, 0, 0;

    my $patch_content;
    if ($use_classic) {
        $patch_content = _read_patch_classic($patch_fh);
    }
    else {
        $patch_content = _read_patch_dpatch($patch_fh);

    }
    close($patch_fh);

    return $patch_content;
}

sub _read_patch_classic {
    my ($patch_fh) = @_;

    my %patch_content = (
        header => undef,
        body   => '',
        fields => {},
    );
    my $key        = '';
    my $header_end = 0;
    while ( my $line = <$patch_fh> ) {
        if ( !$header_end and $line =~ /^--- / ) {

            # Start of the patch body
            $header_end = 1;
        }

        if ($header_end) {

            # Slurping the patch
            $patch_content{body} .= $line;
            next;
        }

        if ( $line =~ m/^ (\S+) : \s+ (.+) $/xms ) {

            # Starting a new field
            my $value;
            ( $key, $value ) = ( $1, $2 );
            $patch_content{fields}{$key} = $value;
            next;
        }

        if ( $line =~ m/^ / or $key eq 'Subject' ) {

            # Previous field not over
            $patch_content{fields}{$key} .= $line;
            next;
        }

        # End of header but not yet the start of patch (before ---)
        $header_end = 1;
        $patch_content{body} .= $line;
    }

    # remove the first empty line (will be added automaticaly)
    $patch_content{body} =~ s/\A\s+//xms;

    return \%patch_content;
}

sub _read_patch_dpatch {
    my ($patch_fh) = @_;

    my %patch_content = (
        header => {
            head => scalar <$patch_fh>,
            tail => '',
        },
        body   => '',
        fields => {},
    );

    my $key        = '';
    my $header_end = 0;
    my $spaces     = '--- ';
    while ( my $line = <$patch_fh> ) {

        if ( !$header_end and $line =~ /^--- / ) {

            # Start of the patch body
            $header_end = 1;
        }

        if ($header_end) {

            # Slurping the patch
            $patch_content{body} .= $line;
            next;
        }

        if ( $line =~ m/^ \# (\s+) (\S+) : \s+ (.+) $/xms ) {

            # Starting a new field
            my $value;
            ( $spaces, $key, $value ) = ( $1, $2, $3 );
            die $line if not $key;
            $patch_content{fields}{$key} = $value;
            next;
        }

        if ( $line =~ m/^ \# $spaces \s+ /xms or $key eq 'Subject' ) {

            # Previous field not over
            $patch_content{fields}{$key} .= $line;
            next;
        }

        if ( $line =~ m/^ \# /xms ) {

            # Still in the header
            $patch_content{header}{ $key ? 'tail' : 'head' } .= $line;
            next;
        }

        # End of header but not yet the start of patch (before ---)
        $header_end = 1;
        $patch_content{body} .= $line;
    }

    # remove the first empty line (will be added automaticaly)
    $patch_content{body} =~ s/\A\s+//xms;

    return \%patch_content;
}