summaryrefslogtreecommitdiff
path: root/bin/git-pbuilder
blob: 6bd7e0202b2a6c8741986b24bb3d61507e419429 (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
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
463
464
465
466
467
468
469
#!/bin/sh
# $Id: git-pbuilder,v 1.27 2012/01/13 04:12:35 eagle Exp $
#
# git-pbuilder -- Wrapper around pbuilder for git-buildpackage
#
# Written by Russ Allbery <rra@stanford.edu>
# Based on the example in the git-buildpackage documentation
# Copyright 2008, 2009, 2010, 2011, 2012
#     The Board of Trustees of the Leland Stanford Junior University
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of Stanford University not be used in
# advertising or publicity pertaining to distribution of the software without
# specific, written prior permission.  Stanford University makes no
# representations about the suitability of this software for any purpose.  It
# is provided "as is" without express or implied warranty.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

set -e

# The URL to the Debian backports repository to add to the chroot
# configuration when created via this script for a distribution ending in
# -backports.
BACKPORTS='http://backports.debian.org/debian-backports'

# Set default BUILDER, DIST, and ARCH based on the name we were invoked as.
# This allows people to create symlinks like git-pbuilder-squeeze and
# git-qemubuilder-squeeze-amd64 pointing to git-pbuilder and auto-detecting
# the builder, distribution, and architecture from that.
default_BUILDER=${0#*git-}
default_DIST=${default_BUILDER#*-}
default_BUILDER=${default_BUILDER%%-*}
case $default_BUILDER in
    pbuilder|cowbuilder) default_BUILDER=cowbuilder ;;
    /*)                  default_BUILDER=cowbuilder ;;
esac
case $default_BUILDER in
    *builder) ;;
    *)        default_BUILDER=cowbuilder ;;
esac
case $default_DIST in
    *builder)
        default_DIST=
        ;;
    *-*)
        default_ARCH=${default_DIST#*-}
        default_DIST=${default_DIST%%-*}
        ;;
esac
: ${default_BUILDER:=cowbuilder}

# Set BUILDER, DIST, and ARCH, allowing existing settings to override.
: ${BUILDER:=$default_BUILDER}
: ${DIST:=$default_DIST}
: ${ARCH:=$default_ARCH}

# If DIST ends in -backports, strip that out of DIST and add it to EXT.
if expr "$DIST" : '.*-backports$' >/dev/null; then
    DIST=${DIST%-backports}
    EXT="-backports"
else
    EXT=
fi

# Make sure we have the necessary tools.
if [ ! -x /usr/sbin/"$BUILDER" ]; then
    echo "$BUILDER not found; you need to install the $BUILDER package" >&2
    exit 1
fi

# Default options come from the environment.
OPTIONS="$GIT_PBUILDER_OPTIONS"

# How we handle options depends on what type of builder we're using.  Ignore
# options if $GIT_PBUILDER_AUTOCONF is set to no.
if [ no != "$GIT_PBUILDER_AUTOCONF" ] ; then
    case $BUILDER in
        pbuilder)
            # The root directory where different pbuilder --basepath
            # directories are found.  git-pbuilder expects them to be named
            # base-<dist>.tgz.
            : ${PBUILDER_BASE:=/var/cache/pbuilder}

            # If DIST is set, use base-$DIST.tgz.  If DIST is not set, the sid
            # chroot may be either base.tgz or base-sid.tgz.  Try both.  If
            # ARCH is set, use base-$DIST-$ARCH.tgz.
            : ${DIST:=sid}
            if [ -n "$ARCH" ] ; then
                BASE="$PBUILDER_BASE/base-$DIST$EXT-$ARCH.tgz"
                OPTIONS="$OPTIONS --architecture $ARCH"
            elif [ "$DIST" = 'sid' ] ; then
                if [ -f "$PBUILDER_BASE/base-sid.tgz" ] ; then
                    BASE="$PBUILDER_BASE/base-sid.tgz"
                else
                    BASE="$PBUILDER_BASE/base.tgz"
                fi
            else
                BASE="$PBUILDER_BASE/base-$DIST$EXT.tgz"
            fi
            OPTIONS="$OPTIONS --basetgz $BASE"

            # Make sure the base tarball exists.
            if [ ! -f "$BASE" ] && [ "$1" != "create" ]; then
                echo "Base tarball $BASE does not exist" >&2
                exit 1
            fi

            # Set --debian-etch-workaround if DIST is etch.  Assume that
            # everything else is new enough that it will be fine.
            if [ "$DIST" = 'etch' ] || [ "$DIST" = 'ebo' ] ; then
                OPTIONS="$OPTIONS --debian-etch-workaround"
            fi
            ;;
    
        cowbuilder)
            # The root directory where different cowbuilder --basepath
            # directories are found.  git-pbuilder expects them to be named
            # base-<dist>.cow.
            : ${COWBUILDER_BASE:=/var/cache/pbuilder}
    
            # If DIST is set, use base-$DIST.cow.  If DIST is not set, the sid
            # chroot may be either base.cow or base-sid.cow.  Try both.  If
            # ARCH is set, use base-$DIST-$ARCH.cow.
            : ${DIST:=sid}
            if [ -n "$ARCH" ] ; then
                BASE="$COWBUILDER_BASE/base-$DIST$EXT-$ARCH.cow"
                OPTIONS="$OPTIONS --architecture $ARCH"
            elif [ "$DIST" = 'sid' ] ; then
                if [ -d "$COWBUILDER_BASE/base-sid.cow" ] ; then
                    BASE="$COWBUILDER_BASE/base-sid.cow"
                else
                    BASE="$COWBUILDER_BASE/base.cow"
                fi
            else
                BASE="$COWBUILDER_BASE/base-$DIST$EXT.cow"
            fi
            OPTIONS="$OPTIONS --basepath $BASE"
    
            # Make sure the base directory exists.
            if [ ! -d "$BASE" ] && [ "$1" != "create" ]; then
                echo "Base directory $BASE does not exist" >&2
                exit 1
            fi
    
            # Set --debian-etch-workaround if DIST is etch.  Assume that
            # everything else is new enough that it will be fine.
            if [ "$DIST" = 'etch' ] || [ "$DIST" = 'ebo' ] ; then
                OPTIONS="$OPTIONS --debian-etch-workaround"
            fi
            ;;
    
        qemubuilder)
            # There always has to be an architecture for qemubuilder, and it
            # doesn't make much sense to default to the current architecture.
            # There's probably no good default, but this one at least makes
            # some sense.
            : ${DIST:=sid}
            : ${ARCH:=armel}
    
            # There has to be a configuration file matching our distribution
            # and architecture.
            QEMUCONFIG="/var/cache/pbuilder/$BUILDER-$ARCH-$DIST$EXT.conf"
            if [ ! -r "$CONFIG" ]; then
                echo "Cannot read configuration file $QEMUCONFIG" >&2
                exit 1
            fi
            OPTIONS="$OPTIONS --config $QEMUCONFIG"
            ;;
    
        *)
            echo "Unknown builder $BUILDER" >&2
            exit 1
            ;;
    esac
fi

# If the first argument to the script is update, create, or login, run the
# builder with the corresponding option under sudo rather than proceeding.
case $1 in
update|create|login)
    action="$1"
    shift

    # Since we're running the builder under sudo, $HOME will change to root's
    # home directory and the user's .pbuilderrc won't be run.  sudo -E would
    # fix this, but that requires special configuration in sudoers to allow
    # it.  Instead, check if the user has a .pbuilderrc and, if so, explicitly
    # add it as a configuration file.
    if [ -f "$HOME/.pbuilderrc" ] ; then
        OPTIONS="$OPTIONS --configfile $HOME/.pbuilderrc"
    fi

    # Run the builder.
    if [ no = "$GIT_PBUILDER_AUTOCONF" ] ; then
        sudo "$BUILDER" --"$action" $OPTIONS "$@"
    else
        if [ "$EXT" = '-backports' ] ; then
            OTHERMIRROR="deb $BACKPORTS $DIST$EXT main"
            sudo "$BUILDER" --"$action" --dist "$DIST" \
                --othermirror "$OTHERMIRROR" $OPTIONS "$@"
        else
            sudo "$BUILDER" --"$action" --dist "$DIST" $OPTIONS "$@"
        fi
    fi
    exit $?
    ;;
*)
    if [ -z "$GBP_BUILD_DIR" ]; then
        echo "Warning: git-pbuilder should be run via git-buildpackage" >&2
    fi
    ;;
esac

# Print out some information about what we're doing.
building="Building with $BUILDER"
if [ no = "$GIT_PBUILDER_AUTOCONF" ] ; then
    echo "$building"
elif [ -n "$ARCH" ] ; then
    echo "$building for distribution $DIST$EXT, architecture $ARCH"
else
    echo "$building for distribution $DIST$EXT"
fi

# Source package format 1.0 doesn't automatically exclude Git files, so we
# want to add the appropriate flags to do that.  But source package format 3.0
# does exclude by default and has many other ways of controlling those
# exclusions that we don't want to tromp on.  So we don't want to give any -i
# or -I flags unless we're using source format 1.0.
if [ ! -f debian/source/format ] || grep -qs '^1.0' debian/source/format ; then
    echo 'Source format 1.0 detected, adding exclude flags'
    DEBBUILDOPTS="-i'(?:^|/)\\.git(attributes)?(?:\$|/.*\$)' -I.git $*"
else
    DEBBUILDOPTS="$*"
fi

# Now we can finally run pdebuild.  The quoting here is tricky, but this
# seems to pass everything through properly.
if [ no = "$GIT_PBUILDER_AUTOCONF" ] ; then
    pdebuild --pbuilder "$BUILDER" --debbuildopts "$DEBBUILDOPTS" -- $OPTIONS
else
    pdebuild --buildresult .. --pbuilder "$BUILDER" \
        --debbuildopts "$DEBBUILDOPTS" -- $OPTIONS
fi
status="$?"
if [ -n "`ls ../*_source.changes`" ] ; then
    rm ../*_source.changes
fi
exit "$status"

# Documentation.  Use a hack to hide this from the shell.  Because of the
# above exit line, this should never be executed.
DOCS=<<__END_OF_DOCS__

=head1 NAME

git-pbuilder - Wrapper around cowbuilder/qemubuilder for git-buildpackage

=head1 SYNOPSIS

DIST=I<distribution> ARCH=I<architecture> [BUILDER=(pbuilder|qemubuilder)] \
    B<git-pbuilder> I<debbuild-options>

DIST=I<distribution> ARCH=I<architecture> [BUILDER=(pbuilder|qemubuilder)] \
    B<git-pbuilder> (update | create | login) I<cowbuilder-options>

=head1 DESCRIPTION

B<git-pbuilder> is a wrapper around B<pdebuild> intended for use by
B<git-buildpackage>.  It configures B<pdebuild> to use B<cowbuilder> by
default, passes appropriate options to B<debbuild>, and sets the base path
for B<cowbuilder> based on the environment variable DIST and, if set, the
environment variable ARCH.  B<qemubuilder> can be selected instead by
setting the environment variable BUILDER to C<qemubuilder>, and
B<pbuilder> can be selected by setting BUILDER to C<pbuilder>.

By default, B<git-pbuilder> assumes the target distribution is C<sid>, the
same architecture as the B<cowbuilder> default, and uses
F</var/cache/pbuilder/base-sid.cow> if it exists.  If it doesn't,
F</var/cache/pbuilder/base.cow> is tried.  If DIST is set, its value is
the target distribution and F</var/cache/pbuilder/base-I<dist>.cow> is
used instead.  If DIST is C<etch> or C<ebo>, B<--debian-etch-workaround>
is also passed to B<cowbuilder>.  If ARCH is set, its value is the target
architecture and F</var/cache/pbuilder/base-I<dist>-I<arch>.cow> is used,
with I<dist> being set to C<sid> if DIST was not set.

If B<qemubuilder> is used as the builder, no base directory is used.
Instead, B<qemubuilder> is invoked with the B<--config> option pointing to
the file F</var/cache/pbuilder/qemubuilder-I<arch>-I<dist>.conf>

If B<pbuilder> is used as the builder, B<git-pbuilder> instead looks for
F</var/cache/pbuilder/base-sid.tgz> by default and
F</var/cache/pbuilder/base.tgz> if it doesn't exist.  If DIST or ARCH are
set, they are used to form the expected name of the tgz file in the same
way as they're used to form the expected base directory for B<cowbuilder>.
Similar to B<cowbuilder>, B<--debian-etch-workaround> is passed to
B<pbuilder> if from the DIST setting it looks like the target distribution
is etch.

If B<git-pbuilder> is invoked via a name that starts with C<git-*->, the
part between the hyphens is taken to be the default name of the builder to
use.  However, C<pbuilder> is mapped to B<cowbuilder> for backward
compatibility; if you want to use B<pbuilder>, you have to explicitly set
BUILDER.  The part after the last hyphen is taken to be the default
distribution (if it contains no additional hyphen) or the default
distribution followed by the default architecture (if it contains a
hyphen).  One can therefore create symlinks like C<git-pbuilder-squeeze>
pointing to B<git-pbuilder> and use that name when wanting to use a
distribution of C<squeeze>, or C<git-qemubuilder-sid-armel> to use
B<qemubuilder> to build for the C<armel> architecture and the C<sid>
distribution.  Explicit settings of BUILDER, DIST, or ARCH always override
any guesses from the command name.

Any arguments are passed as-is to B<dpkg-buildpackage> via the
B<--debbuildopts> option to B<pdebuild>.  To pass arguments to the builder
instead, put them in the environment variable GIT_PBUILDER_OPTIONS.

To disable all attempts to discover the base path, tarball, or
configuration file and set up the pbuilder options and instead rely on the
settings in .pbuilderrc, set GIT_PBUILDER_AUTOCONF to C<no>.

Normally, one does not run this script directly.  Instead, it's used as
the builder script for B<git-buildpackage>.  To configure
B<git-buildpackage> to use it, add a stanza like:

    [DEFAULT]
    builder = /path/to/git-pbuilder

in your F<gbp.conf> file (which can be F<.gbp.conf> in your home directory
or at the top level of the repository, or F<gbp.conf> in the F<.git>
directory).  DIST and ARCH are read as an environment variable so that you
can set it before running B<git-buildpackage> without having to worry
about passing parameters through B<git-buildpackage>.

Alternately, B<git-pbuilder> may be called with an argument of C<update>,
C<create>, or C<login>.  In this case, it calls B<cowbuilder> (or the
configured builder as described above) using B<sudo> and passes the
corresponding command to the builder, using the same logic as above to
determine the base directory and distribution.  If the distribution (set
in DIST) ends in C<-backports>, the following will be added as an
B<--othermirror> parameter to the builder:

    deb http://backports.debian.org/debian-backports $DIST main

Any additional arguments to B<git-pbuilder> are passed along to the
builder.  Due to how B<sudo> works, invoking the builder with an action
will not read the user's F<.pbuilderrc> by default, so in this case
B<git-pbuilder> will add an explicit B<--configfile> option pointing to
the user's F<.pbuilderrc> if it exists.

=head1 ENVIRONMENT

=over 4

=item ARCH

Sets the target architecture.  For a B<cowbuilder> builder, this sets both
the base path and is passed as the B<--architecture> option.  With
B<qemubuilder>, this controls the path to the configuration file.  With
B<pbuilder>, this sets the tgz path and is passed as B<--architecture>.

=item BUILDER

Sets the builder to use.  The only supported settings are C<cowbuilder>
(the default), C<qemubuilder>, and C<pbuilder>.

=item COWBUILDER_BASE

Set this environment variable to change the default location for the
cowbuilder base directories (F</var/cache/pbuilder>).

=item DIST

Sets the target distribution.  This is used primarily to determine the
base path for B<cowbuilder> or B<pbuilder> or the configuration file path
for B<qemubuilder>, but it's also used to determine whether to pass
B<--debian-etch-workaround> to B<cowbuilder> or B<pbuilder>.

=item GIT_PBUILDER_AUTOCONF

If set to C<no>, disable the logic that constructs the base path, tarball,
or configuration file and all other logic to determine the options to pass
to the builder.  Instead, just run the configured builder and assume its
configuration is handled elsewhere (such as in F<.pbuilderrc>).  This also
suppresses setting C<--buildresult ..>, so the user will need to ensure
that the configuration still puts packages where B<git-buildpackage>
expects them.

=item GIT_PBUILDER_OPTIONS

Add additional options for the builder.  These options are passed as-is to
B<cowbuilder>, B<qemubuilder>, or B<pbuilder> via B<pdebuild>.  The
contents of this variable will undergo shell expansion, so any arguments
containing shell metacharacters or whitespace need to be quoted in the
value of the environment variable.

=item PBUILDER_BASE

Set this environment variable to change the defualt location for the
pbuilder tgz files (F</var/cache/pbuilder>) when BUILDER is set to
C<pbuilder>.

=back

=head1 FILES

=over 4

=item /var/cache/pbuilder/base-sid.cow

=item /var/cache/pbuilder/base.cow

The default C<cowbuilder --basepath> directories, searched for in that
order, if neither DIST nor ARCH is set.

=item /var/cache/pbuilder/base-sid-$ARCH.cow

The C<cowbuilder --basepath> directory used if ARCH is set and DIST is not
set.

=item /var/cache/pbuilder/base-$DIST.cow

The C<cowbuilder --basepath> directory used if DIST is set and ARCH is
not.

=item /var/cache/pbuilder/base-$DIST-$ARCH.cow

The C<cowbuilder --basepath> directory used if DIST and ARCH are both set.

=item /var/cache/pbuilder/base-sid.tgz

=item /var/cache/pbuilder/base.tgz

=item /var/cache/pbuilder/base-sid-$ARCH.tgz

=item /var/cache/pbuilder/base-$DIST.tgz

=item /var/cache/pbuilder/base-$DIST-$ARCH.tgz

Similar to the above, the C<pbuilder --basetgz> path used for various
settings of DIST and ARCH if BUILDER is set to C<pbuilder>.

=item /var/cache/pbuilder/qemubuilder-$ARCH-$DIST.conf

The C<qemubuilder --config> file used.  $ARCH defaults to C<armel> and
$DIST defaults to C<sid> if not set.

=back

=head1 SEE ALSO

cowbuilder(8), dpkg-buildpackage(1), git-buildpackage(1), pbuilder(8),
pdebuild(1), qemubuilder(8), sudo(8)

The latest version of this script is available from
L<http://www.eyrie.org/~eagle/software/scripts/>.

=head1 AUTHOR

Russ Allbery <rra@stanford.edu>

=cut

__END_OF_DOCS__