Merge remote-tracking branch 'kc/new/bug_6753' into kcmaster
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 strict;
25 use warnings;
26
27 use Getopt::Long;
28 use POSIX qw/strftime/;
29
30 my $buildresult;
31 my $distribution='squeeze-dev';
32 my $git_checks='all';
33 my $version='3.5-1~git';
34 my $auto_version=1;
35 my $need_help;
36 my $debug;
37
38 GetOptions(
39     'buildresult|r=s'   => \$buildresult,
40     'distribution|D=s'  => \$distribution,
41     'git-checks|g=s'    => \$git_checks,
42     'version|v=s'       => \$version,
43     'autoversion!'      => \$auto_version,
44     'help|h'            => \$need_help,
45     'debug|d'           => \$debug,
46 );
47
48 help_and_exit() if $need_help;
49
50
51 sub sys_command_output {
52     my ($command) = @_;
53
54     print "$command\n" if $debug;
55     my $command_output;
56     open($command_output, "-|", "$command ")
57       or die qq{Cannot execute "$command": $!"};
58     return map { chomp; $_ } <$command_output>;
59 }
60
61 sub everything_is_committed {
62     my $filter;
63     for ($git_checks) {
64         $_ eq "none"
65           and return 1;
66
67         $_ eq "modified"
68           and $filter = "no",
69               last;
70
71         $_ eq "all"
72           and $filter = "normal",
73               last;
74
75             help_and_exit("$0: --git-checks/-g must be one of 'all', 'modified', or 'none'");
76     }
77     my $has_changes = grep /^xxx/, sys_command_output("git status --porcelain -u${filter}");
78
79     return !$has_changes;
80 }
81
82 sub help_and_exit {
83         my $msg = shift;
84         if ($msg) {
85         print "$msg\n\n";
86     }
87     print <<EOH;
88 This builds Koha deb packages, from a git snapshot. It's not suitable for
89 making upstreamable verions, but handy for your own local packages.
90
91 Options:
92     --buildresult, -r
93         the location that the resulting .deb, .changes, etc. will be placed in.
94         Default is whatever pdebuild uses.
95     --distribution, -D
96         the distribution value to set in the changelog when editing it. Default
97         is 'squeeze-dev'.
98     --git-checks, -g
99         what level of git checks are run to determine if the working copy is
100         clean enough. One of 'all' (any changes are bad), 'modified' (only
101         tracked files with untracked changes will cause an error), and 'none'
102         (checking git status is skipped totally.) Default is 'all'.
103     --version, -v
104         the version string for the resulting package. Default is '3.5-1~git'.
105     --(no)autoversion
106         whether or not to use the date and git commit ID in the version value.
107         Default is to include it.
108     --debug, -d
109 EOH
110     exit;
111 }
112
113 sub latest_sha1 {
114     return sys_command_output("git rev-parse --short=8 HEAD");
115 }
116
117 sub adjust_debian_changelog {
118     my ($newversion) = @_;
119
120     sys_command_output( qq{dch --force-distribution -D "$distribution" -v "$newversion" "Building git snapshot."} );
121     sys_command_output( qq{dch -r "Building git snapshot."} );
122 }
123
124 sub reset_debian_changelog {
125     sys_command_output( qq{git checkout -- debian/changelog} );
126 }
127
128 sub build_package {
129     my ($newversion) = @_;
130     sys_command_output( qq{git archive --format=tar --prefix="koha-$newversion/" HEAD | gzip -9 > "../koha_$newversion.tar.gz"} );
131
132     my $pdebuildopts = $buildresult ? "--buildresult $buildresult" : "";
133     sys_command_output( "pdebuild $pdebuildopts" );
134 }
135
136 everything_is_committed() or die "cannot build: uncommited changes";
137
138 my $newversion = $auto_version
139   ? sprintf ('%s%s.%s', $version, strftime("+%Y%m%d%H%M%S", localtime), latest_sha1())
140   : $version;
141
142 adjust_debian_changelog( $newversion );
143 build_package( $newversion );
144 reset_debian_changelog();
145