inject Distar behavior at import time rather than compile time
[p5sagit/Distar.git] / helpers / add-changelog-heading
CommitLineData
e7a78651 1#!/usr/bin/env perl
2use strict;
3use warnings FATAL => 'all';
762733d9 4use Getopt::Long qw(:config gnu_getopt);
5GetOptions(
6 "git" => \my $git,
7) or die("Error in command line arguments\n");
e7a78651 8
9my $version = shift;
10my @ymd = (gmtime)[5,4,3];
11$ymd[0] += 1900;
12$ymd[1] += 1;
762733d9 13my $changes_line = sprintf "%s - %i-%02i-%02i", $version, @ymd;
e7a78651 14
15foreach my $changes (@ARGV) {
16 print "Adding $version heading to $changes.\n";
762733d9 17 open my $fh, '+<:raw', $changes
e7a78651 18 or die "Can't open $changes: $!";
762733d9 19
e7a78651 20 my $content = '';
762733d9 21 my $context;
22
e7a78651 23 while (<$fh>) {
762733d9 24 if (defined $context) {
25 if (/^\S/) {
26 local $/;
27 $content .= $_ . <$fh>;
28 last;
29 }
30 $context++;
e7a78651 31 }
32 elsif (/^v?(\d+(?:\.\d+)*)(\s+|$)/) {
33 if ($1 eq $version) {
34 die "$changes already has an entry for $version!\n";
35 }
36 else {
37 die "No changelog entries found before $1!\n";
38 }
39 }
40 elsif (/^\s+[-*+#]/) {
41 $content .= $changes_line;
762733d9 42 $content .= (/(\r?\n)/ || $content =~ /(\r?\n)/) ? $1 : "\n";
43 $context = 1;
e7a78651 44 }
45 $content .= $_;
46 }
47
48 seek $fh, 0, 0;
49 truncate $fh, 0;
50 print { $fh } $content;
51 close $fh;
762733d9 52
53 if ($git) {
54 local $ENV{GIT_DIFF_OPTS} = "-u$context";
55 system qw(git add -p), $changes;
56 }
e7a78651 57}