Bug 23140: Fix typo in branchcode parameters for print slip
[koha.git] / debian / build-git-snapshot
1 #!/usr/bin/perl 
2
3 # Copyright 2010 Catalyst IT Ltd.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 # Written by Robin Sheat <robin@catalyst.net.nz> and
21 #   Srdjan Jankovic <srdjan@catalyst.net.nz>
22 # Based on an sh version by Lars Wirzenius.
23
24 use Modern::Perl;
25
26 use Getopt::Long;
27 use POSIX qw/strftime/;
28
29 my $basetgz;
30 my $buildresult;
31 my $distribution='squeeze-dev';
32 my $git_checks='all';
33 my $version='16.06~git';
34 my $auto_version=1;
35 my $need_help;
36 my $debug;
37
38 GetOptions(
39     'basetgz|b=s'      => \$basetgz,
40     'buildresult|r=s'   => \$buildresult,
41     'distribution|D=s'  => \$distribution,
42     'git-checks|g=s'    => \$git_checks,
43     'version|v=s'       => \$version,
44     'autoversion!'      => \$auto_version,
45     'help|h'            => \$need_help,
46     'debug|d'           => \$debug,
47 );
48
49 help_and_exit() if $need_help;
50
51
52 sub sys_command_output {
53     my ($command) = @_;
54
55     print "$command\n" if $debug;
56     my $command_output;
57     open($command_output, "-|", "$command ")
58       or die qq{Cannot execute "$command": $!"};
59     return map { chomp; $_ } <$command_output>;
60 }
61
62 sub sys_command_output_screen {
63     my ($command) = @_;
64
65     print "$command\n" if $debug;
66     system($command) == 0 or die "Command '$command' returns an error ($?)\n";
67 }
68
69 sub everything_is_committed {
70     my $filter;
71     for ($git_checks) {
72         $_ eq "none"
73           and return 1;
74
75         $_ eq "modified"
76           and $filter = "no",
77               last;
78
79         $_ eq "all"
80           and $filter = "normal",
81               last;
82
83             help_and_exit("$0: --git-checks/-g must be one of 'all', 'modified', or 'none'");
84     }
85     my $has_changes = grep /^xxx/, sys_command_output("git status --porcelain -u${filter}");
86
87     return !$has_changes;
88 }
89
90 sub help_and_exit {
91         my $msg = shift;
92         if ($msg) {
93         print "$msg\n\n";
94     }
95     print <<EOH;
96 This builds Koha deb packages, from a git snapshot. It's not suitable for
97 making upstreamable verions, but handy for your own local packages.
98
99 Options:
100     --buildresult, -r
101         the location that the resulting .deb, .changes, etc. will be placed in.
102         Default is whatever pdebuild uses.
103     --distribution, -D
104         the distribution value to set in the changelog when editing it. Default
105         is 'squeeze-dev'.
106     --git-checks, -g
107         what level of git checks are run to determine if the working copy is
108         clean enough. One of 'all' (any changes are bad), 'modified' (only
109         tracked files with untracked changes will cause an error), and 'none'
110         (checking git status is skipped totally.) Default is 'all'.
111     --version, -v
112         the version string for the resulting package. Default is '$version'.
113     --(no)autoversion
114         whether or not to use the date and git commit ID in the version value.
115         Default is to include it.
116     --debug, -d
117 EOH
118     exit;
119 }
120
121 sub latest_sha1 {
122     return sys_command_output("git rev-parse --short=8 HEAD");
123 }
124
125 sub adjust_debian_changelog {
126     my ($newversion) = @_;
127     # debian revision
128     $newversion .= "-1";
129
130     sys_command_output( qq{dch --force-distribution -D "$distribution" -v "$newversion" "Building git snapshot."} );
131     sys_command_output( qq{dch -r "Building git snapshot."} );
132 }
133
134 sub reset_debian_changelog {
135     sys_command_output( qq{git checkout -- debian/changelog} );
136 }
137
138 sub build_package {
139     my ($newversion) = @_;
140     sys_command_output( qq{git archive --format=tar --prefix="koha-$newversion/" HEAD | gzip -9 > "../koha_$newversion.orig.tar.gz"} );
141
142     my $pdebuildopts = $buildresult ? "--buildresult $buildresult" : "";
143     my $pdebuildbasetgz = $basetgz ? "-- --basetgz /var/cache/pbuilder/" . $basetgz . ".tgz" : "";
144     sys_command_output_screen( "pdebuild $pdebuildbasetgz $pdebuildopts" );
145 }
146
147 everything_is_committed() or die "cannot build: uncommited changes";
148
149 my $newversion = $auto_version
150   ? sprintf ('%s%s.%s', $version, strftime("+%Y%m%d%H%M%S", localtime), latest_sha1())
151   : $version;
152
153 adjust_debian_changelog( $newversion );
154 build_package( $newversion );
155 reset_debian_changelog();
156