File: gitpkg

package info (click to toggle)
gitpkg 0.31%2Bnmu1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 232 kB
  • sloc: sh: 1,365; makefile: 34
file content (1130 lines) | stat: -rwxr-xr-x 37,648 bytes parent folder | download
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
#!/bin/bash
# Simple shell script for creating Debian source releases from a git repository
#
# Copyright(C) 2007 - 2023, Ron <ron@debian.org>
# This script is distributed according to the terms of the GNU GPL.

set -e

# This has nothing to do with the release version of the package, it is for
# hook scripts to check what guarantees we might make for them. The number
# to the right of point will get incremented every time we add some new
# variable a hook might access, or add some new knob it might tweak.
# Existing interfaces should not have changed.  If we do screw up and need
# to change some current thing, the number to the left will get bumped.
# For what you get with each revision see gitpkg(1).
GITPKG_HOOK_API="0.3"


# Parse the command line.
#
# As a general rule it is not in gitpkg's idiom to accept command line options,
# or at least that was one of the original design goals that we'd held dear to.
# The main reason for this being the importance of ensuring an export is easily
# and reliably reproducible - and if reproducing the build depends on recalling
# the precise options that gitpkg was called with beyond simply the version you
# wish to export, then you've pretty much guaranteed failure at that before you
# even begin.  The first exception we made to this was to handle the --version
# and --help options gracefully, since many people have (valid) expectations of
# those doing something other than trying to export a ref with those names ...
# That was ok though, because they have nothing to do with actually exporting a
# package, so it didn't violate the primary goal.  It turns out there are other
# options which usefully fit the same exception too though.  Things like making
# the choice of which exit hook to run, and what options it should be run with
# are quite reasonable to make on a per-invocation basis, since they don't make
# any difference to the actual package exported, only what is done with it once
# it is.  For things like that it does seem silly to stick to some false purity
# and insist the only way to tweak those is to edit the git config.  Ideally it
# still should be 'hard' to 'accidentally' make the correct export of a package
# depend on the command line options that are passed, so not every option ought
# to be able to be overridden here, but there's clearly a few which it is saner
# to allow than not to, given the way things have now evolved.
#
# This parser separates the command line arguments into three main groups based
# on how they are handled.  Options are of the form --opt or --opt=val, and are
# divided into two groups, those that are for gitpkg itself and those which are
# ignored by gitpkg but may be used by the hook scripts.  The options that will
# be handled by gitpkg directly either result in some immediate action (so that
# hook scripts are never called at all) or get exposed by the variable which is
# already responsible for the option being overridden.  All other options which
# are passed will be stored verbatim in an indexed array GITPKG_IOPTS to permit
# options which may be passed multiple times with multiple values, and they are
# also decomposed into an associative array GITPKG_AOPTS, for trivial access to
# options which should only be passed once (or for which only the last value to
# be passed will be used).  Hook scripts can example these for options relevant
# to their operation.  Options that are specific to a particular hook should be
# named something sufficiently unique to avoid unintentional collisions, unless
# the option really is generally relevant to multiple hooks.  The latter is the
# only case where such options need to be coordinated here for all gitpkg users
# otherwise hooks can define and handle whatever they please as they please.
#
# The third group of arguments is 'parameters' which typically don't start with
# '--' and provide us with the debian and (optional) orig treeish(s) to export.
# In the unlikely event that someone was ever insane enough to create a treeish
# ref that does start with '--' (which git does allow in principle), then using
# a literal ' -- ' argument can be used to separate options from parameters in
# the conventional manner. Parameters are stored in the GITPKG_PARAMS array but
# since it is only valid to pass one or two of them they should more usually be
# accessed using the GITPKG_TREEISH and GITPKG_ORIG_TREEISH variables instead.
declare -A GITPKG_AOPTS
declare -a GITPKG_IOPTS

for arg; do
    case $arg in
	--)
	    no_more_opts=1
	    ;;

	--deb-dir=*)
	    OVERRIDE_DEB_DIR=${arg#*=}
	    ;;

	--admin-config-hook=*)
	    OVERRIDE_ADMIN_CONFIG_HOOK=${arg#*=}
	    if [ -n "$OVERRIDE_ADMIN_CONFIG_HOOK" ] && [ ! -r "$OVERRIDE_ADMIN_CONFIG_HOOK" ];
	    then
		echo "ERROR: cannot read admin-config-hook '$OVERRIDE_ADMIN_CONFIG_HOOK'" 1>&2
		exit 1
	    fi
	    ;;

	--exit-hook=*)
	    OVERRIDE_EXIT_HOOK=${arg#*=}

	    if [ -n "$OVERRIDE_EXIT_HOOK" ]; then
		if [ ! -r "$OVERRIDE_EXIT_HOOK" ]; then
		    hook_dir="/usr/share/gitpkg/hooks"

		    if [ -r "$hook_dir/$OVERRIDE_EXIT_HOOK" ]; then
			OVERRIDE_EXIT_HOOK="$hook_dir/$OVERRIDE_EXIT_HOOK"
		    elif [ -r "$hook_dir/${OVERRIDE_EXIT_HOOK}-exit-hook" ]; then
			OVERRIDE_EXIT_HOOK="$hook_dir/${OVERRIDE_EXIT_HOOK}-exit-hook"
		    else
			echo "ERROR: cannot read exit-hook '$OVERRIDE_EXIT_HOOK'" 1>&2
			exit 1
		    fi
		fi
	    fi
	    ;;

	--keep-unpacked-source)
	    OVERRIDE_KEEP_UNPACKED_SOURCE="true"
	    ;;

	--keep-unpacked-source=*)
	    case ${arg#*=} in
		n* | N* | f* | F*)
		    OVERRIDE_KEEP_UNPACKED_SOURCE="false"
		    ;;

		*)
		    OVERRIDE_KEEP_UNPACKED_SOURCE="true"
		    ;;
	    esac
	    ;;

	--dpkg-source=*)
	    OVERRIDE_DPKG_SOURCE+=( "${arg#*=}" )
	    ;;

	--help)
	    [ -n "$no_more_opts" ] || exec man gitpkg
	    ;&

	--version)
	    [ -n "$no_more_opts" ] || exec dpkg-query -W -f='${Package} ${Version}\n' gitpkg
	    ;&

	--*=*)
	    if [ -z "$no_more_opts" ]; then
		_name="${arg%%=*}"
		GITPKG_AOPTS[${_name#--}]=${arg#*=}
		GITPKG_IOPTS+=( "$arg" )
		continue
	    fi
	    ;&

	--*)
	    if [ -z "$no_more_opts" ]; then
		GITPKG_AOPTS[${arg#--}]=
		GITPKG_IOPTS+=( "$arg" )
		continue
	    fi
	    ;&

	*)
	    GITPKG_PARAMS+=( "$arg" )
	    ;;
    esac
done


# If we've been run for a repo with submodules, from a working directory that
# is managed by a submodule, then all git commands we use will operate on the
# submodule, not the superproject, and all bets about what the caller really
# intended are off.  We can check if there is an immediate superproject, but
# if that is also a submodule of some larger superproject then we have no way
# of knowing for sure how far up that tree we should climb.
#
# So the only sane and safe option is to check if the user has configured the
# allow-subproject-export option, or to prompt them about whether exporting
# just the submodule was what they really wanted to do, and if it wasn't, to
# instead run gitpkg from a working dir of the superproject that they wish to
# export...  Have we mentioned submodules probably aren't the solution you are
# looking for ...  we can try to accommodate them, but they do add complexity
# and novel problems that we just can't hide.
if [ -n "$(git rev-parse --show-superproject-working-tree)" ]; then
    ALLOW_SUBPROJECT_EXPORT="$(git config --get gitpkg.allow-subproject-export)" || true

    if [ "$ALLOW_SUBPROJECT_EXPORT" = "true" ]; then
	echo "Exporting submodule package only."
    else
	cat <<-EOF

	You have run gitpkg in the working tree of a submodule.

	If you wanted to export the super-project, this will not do what you
	were expecting.  You need to run gitpkg from a working dir of the
	(super) project that you wish to export and there is no sane way to
	be sure how far up this tree we should climb automatically.

	EOF

	if [ "$ALLOW_SUBPROJECT_EXPORT" = "false" ]; then
	    echo "Aborting export because gitpkg.allow-subproject-export is 'false'."
	    exit 1
	fi

	printf "Export just the submodule source (y/N)? "
	read -e yesno
	case $yesno in
	    Y* | y*)
		cat <<-EOF

		Ok then.  If you plan to make a habit of this you may wish to set:
		 git config gitpkg.allow-subproject-export 'true'
		To avoid being asked about it again each time.

		EOF
		;;
	    *)
		cat <<-EOF
		Aborting export.

		To avoid being asked about this again you can set
		 git config gitpkg.allow-subproject-export 'false'
		and then in future this will simply print an error and fail.
		EOF
		exit 1
		;;
	esac
    fi
fi


# Everything here assumes we started in the TLD of the repo, so if we aren't
# there already, let's go there now.  Note that if we're in the working dir of
# a submodule, this will only go up to the root of the submodule, not to the
# root of its superproject (hence the checks above).
_TLD=$(git rev-parse --show-cdup)
[ -z "$_TLD" ] || cd "$_TLD"

# Remember where we are right now, in absolute terms, in case things that run
# out of the repo tree really need to do something in it. Like call git-config.
# Usually if you're out of the tree you should stay there though, so if you do
# use this for something, think carefully about what you are really doing.
REPO_DIR=$(pwd)


# You can ignore most of this first lot.  We temporarily waste a bunch of your
# startup cycles being far too friendly about migrating users to the new config.
# It could probably be packed into just a few lines of deep recursion, but we'll
# just drop it completely once people have migrated.
# I figure if it saves me two "how do I ..." email exchanges with users, it will
# have paid for every line of itself in time saved for everyone :)
# {{{

if [ -r /etc/gitpkg.conf ]; then
    . /etc/gitpkg.conf

    if [ -n "$BUILD_ROOTCMD" ]; then
	EXIT_MSG="${EXIT_MSG}# git config --system gitpkg.build-rootcmd '$BUILD_ROOTCMD'\n"
	_BUILD_ROOTCMD="$BUILD_ROOTCMD"
    fi
    if [ -n "$DEB_DIR" ]; then
	EXIT_MSG="${EXIT_MSG}# git config --system gitpkg.deb-dir '$DEB_DIR'\n"
	_DEB_DIR="$DEB_DIR"
    fi
    if [ -n "$HOOK_FILE" ]; then
	EXIT_MSG="${EXIT_MSG}  (You may want to hook only one of the next two)\n"
	EXIT_MSG="${EXIT_MSG}# git config --system gitpkg.deb-export-hook '$HOOK_FILE'\n"
	EXIT_MSG="${EXIT_MSG}# git config --system gitpkg.orig-export-hook '$HOOK_FILE'\n"
	_HOOK_FILE="$HOOK_FILE"
    fi
    if [ -n "$EXIT_MSG" ]; then
	OLD_ROOT_CONFIG="# rm /etc/gitpkg.conf\n"
    fi
fi

if [ -r ~/.gitpkg ]; then
    . ~/.gitpkg

    if [ "$_BUILD_ROOTCMD" != "$BUILD_ROOTCMD" ]; then
	EXIT_MSG="${EXIT_MSG}\$ git config --global gitpkg.build-rootcmd '$BUILD_ROOTCMD'\n"
	_BUILD_ROOTCMD="$BUILD_ROOTCMD"
	_OLD_CONFIG="~/.gitpkg"
    fi
    if [ "$_DEB_DIR" != "$DEB_DIR" ]; then
	EXIT_MSG="${EXIT_MSG}\$ git config --global gitpkg.deb-dir '$DEB_DIR'\n"
	_DEB_DIR="$DEB_DIR"
	_OLD_CONFIG="~/.gitpkg"
    fi
    if [ "$_HOOK_FILE" != "$HOOK_FILE" ]; then
	EXIT_MSG="${EXIT_MSG}  (You may want to hook only one of the next two)\n"
	EXIT_MSG="${EXIT_MSG}\$ git config --global gitpkg.deb-export-hook '$HOOK_FILE'\n"
	EXIT_MSG="${EXIT_MSG}\$ git config --global gitpkg.orig-export-hook '$HOOK_FILE'\n"
	_HOOK_FILE="$HOOK_FILE"
	_OLD_CONFIG="~/.gitpkg"
    fi
    OLD_CONFIG="$_OLD_CONFIG"
    _OLD_CONFIG=
fi

if [ -r .git/gitpkg.conf ]; then
    . .git/gitpkg.conf

    if [ "$_BUILD_ROOTCMD" != "$BUILD_ROOTCMD" ]; then
	EXIT_MSG="${EXIT_MSG}\$ git config gitpkg.build-rootcmd '$BUILD_ROOTCMD'\n"
	_OLD_CONFIG=".git/gitpkg.conf"
    fi
    if [ "$_DEB_DIR" != "$DEB_DIR" ]; then
	EXIT_MSG="${EXIT_MSG}\$ git config gitpkg.deb-dir '$DEB_DIR'\n"
	_OLD_CONFIG=".git/gitpkg.conf"
    fi
    if [ "$_HOOK_FILE" != "$HOOK_FILE" ]; then
	EXIT_MSG="${EXIT_MSG}  (You may want to hook only one of the next two)\n"
	EXIT_MSG="${EXIT_MSG}\$ git config gitpkg.deb-export-hook '$HOOK_FILE'\n"
	EXIT_MSG="${EXIT_MSG}\$ git config gitpkg.orig-export-hook '$HOOK_FILE'\n"
	_OLD_CONFIG=".git/gitpkg.conf"
    fi
    OLD_CONFIG="${OLD_CONFIG:+$OLD_CONFIG }$_OLD_CONFIG"
fi

if [ -n "$OLD_CONFIG" ]; then
    OLD_CONFIG="$ rm $OLD_CONFIG\n"
fi

EXIT_MSG="$EXIT_MSG$OLD_ROOT_CONFIG$OLD_CONFIG"


# sorry_dude OLD_VAR new-config new-value
# We could loop harder and probably resolve conflicts like this, but it really
# is better to just get rid of them asap.  This should only bite the hardcore
# tweakers, if there is some common pattern of innocent victims then we can see
# what to do about those if or as they come.
sorry_dude()
{ #{{{

    cat 1>&2 <<-EOF

	 Hmm.  You have $1 set in an old-style config file and also have
	 gitpkg.$2 set in git-config.  Mixing old and new config
	 options could go badly.  Your old-style config options can be migrated
	 into git-config now using the following:

	$(printf "$EXIT_MSG")

	 The current git-config reported value for gitpkg.$2
	 is: '$3'

	 When you have merged the desired options into git-config, simply run
	 gitpkg again and everything should be peachy.  Sorry for the hassle,
	 but it's best you check this gets untangled in the right way for you
	 before we continue in this repo.

	EOF

    exit 1

} #}}}

# git-config is where static user preferences should be now.
# In the case of a system with a partial transition between old and new config,
# we bark and stop until the user can have a look and set it right.  If it's
# purely using the old config then we just growl and point -- people who didn't
# fiddle with things deserve to have them still be working like they left them.
if _GCBR="$(git config --get gitpkg.build-rootcmd)"; then
    [ -z "$BUILD_ROOTCMD" ]	|| sorry_dude BUILD_ROOTCMD build-rootcmd "$_GCBR"
    [ -z "$DEB_DIR" ]		|| sorry_dude DEB_DIR build-rootcmd "$_GCBR"
    [ -z "$HOOK_FILE" ]		|| sorry_dude HOOK_FILE build-rootcmd "$_GCBR"
    BUILD_ROOTCMD="$_GCBR"
fi
if _GCDD="$(git config --get gitpkg.deb-dir)"; then
    [ -z "$DEB_DIR" ]		|| sorry_dude DEB_DIR deb-dir "$_GCDD"
    [ -n "$_GCBR" ]		\
    || [ -z "$BUILD_ROOTCMD" ]	|| sorry_dude BUILD_ROOTCMD deb-dir "$_GCDD"
    [ -z "$HOOK_FILE" ]		|| sorry_dude HOOK_FILE deb-dir "$_GCDD"
    DEB_DIR="$_GCDD"
fi
if DEB_EXPORT_HOOK="$(git config --get gitpkg.deb-export-hook)"; then
    [ -z "$HOOK_FILE" ]		|| sorry_dude HOOK_FILE deb-export-hook "$DEB_EXPORT_HOOK"
    [ -n "$_GCBR" ]		\
    || [ -z "$BUILD_ROOTCMD" ]	|| sorry_dude BUILD_ROOTCMD deb-export-hook "$DEB_EXPORT_HOOK"
    [ -n "$_GCDD" ]		\
    || [ -z "$DEB_DIR" ]	|| sorry_dude DEB_DIR deb-export-hook "$DEB_EXPORT_HOOK"
fi
if ORIG_EXPORT_HOOK="$(git config --get gitpkg.orig-export-hook)"; then
    [ -z "$HOOK_FILE" ]		|| sorry_dude HOOK_FILE orig-export-hook "$ORIG_EXPORT_HOOK"
    [ -n "$_GCBR" ]		\
    || [ -z "$BUILD_ROOTCMD" ]	|| sorry_dude BUILD_ROOTCMD orig-export-hook "$ORIG_EXPORT_HOOK"
    [ -n "$_GCDD" ]		\
    || [ -z "$DEB_DIR" ]	|| sorry_dude DEB_DIR orig-export-hook "$ORIG_EXPORT_HOOK"
fi

# Really this should check against the treeish being exported, but this
# first approximation should catch most affected people on the first pass.
if [ -e .gitpkg_hook ] && [ "$HOOK_FILE" != ".gitpkg_hook" ] &&
			  [ "$DEB_EXPORT_HOOK" != ".gitpkg_hook" ] &&
			  [ "$ORIG_EXPORT_HOOK" != ".gitpkg_hook" ]; then
    cat 1>&2 <<-EOF

	Uh Oh.  You're the one corner case I can't safely make life easy for.
	  You have a .gitpkg_hook in the local repo but you never explicitly
	  enabled it in your config.  To enable it now you will need to do
	  something like:

	\$ git config gitpkg.deb-export-hook '.gitpkg_hook'
	  and/or
	\$ git config gitpkg.orig-export-hook '.gitpkg_hook'

	  possibly in conjunction with a gitpkg.package-config-hook too.
	  Please see gitpkg(1) for details of the new hook and config options.

	  Since I can't tell this fine .gitpkg_hook of yours from a rogue
	  power you found drifting about the nastyweb, I'm not going to
	  give it a shell until you say that is OK with you, explicitly.
	  And since your things probably need it, I should stop right here
	  until you sort that out.  Sorry.
	$(if [ -n "$EXIT_MSG" ]; then
	    printf "\nTo migrate the rest of your existing config to use git-config(1)\n"
	    printf "the following commands may be helpful too:\n\n"
	    printf "$EXIT_MSG"
	    printf " "
	fi)
	EOF

    exit 1
fi

if [ -n "$HOOK_FILE" ]; then
    # This is just for compatibility with the original trivial hook,
    # not a recommendation of a particularly good idea in most cases.
    DEB_EXPORT_HOOK="$HOOK_FILE"
    ORIG_EXPORT_HOOK="$HOOK_FILE"
fi

# if the user is still using old config, prepare a gentle warning about that
# to nag them with after all the action is over.  It is expected this will
# only be shown if subsequent operations succeed, in the event of some other
# failure we can save the nagging until they are in a better mood for it.
if [ -n "$EXIT_MSG" ]; then
    EXIT_MSG=$(cat <<-EOF

	 OH HAI! gitpkg now uses git-config(1) for configuration data,
	 and you have some options set in the old configuration files.
	 They still worked ok this time, but you can use the following
	 to migrate your existing config:

	$EXIT_MSG
	 If you do nothing, things will continue to work as they do at
	 present, and this message will continue to report the commands
	 needed to migrate over to the standard git-config interface.\n\n

	EOF
    )
    # I guess we _could_ add a "no really, just do it button here" ...
fi

# if all else fails, fit them up with some sensible shoes for the trek.
: "${BUILD_ROOTCMD="fakeroot"}"
: "${DEB_DIR="../deb-packages"}"

# }}}



# Ok you can open your eyes again now, this is where the script really starts.
# All new config options should go below here, leave all that stuff above alone
# until we can drop support for the old config files and just remove it again.

# So...  first we see what the local user left for us in git-config
# Roughly in the order that they are used below:
PACKAGE_CONFIG_HOOK="$(git config --get gitpkg.package-config-hook)" || true
ADMIN_CONFIG_HOOK="$(git config --get gitpkg.admin-config-hook)" || true
PRE_EXPORT_HOOK="$(git config --get gitpkg.pre-export-hook)" || true
#DEB_EXPORT_HOOK="$(git config --get gitpkg.deb-export-hook)" || true
PREBUILD_TARGET="$(git config --get gitpkg.prebuild-target)" || true
EXPORT_SUBMODULES="$(git config --get --bool gitpkg.export-submodules)" || true
CREATE_FAKE_ORIG="$(git config --get --bool gitpkg.create-fake-orig)" || true
FORCE_OVERWRITE_ORIG="$(git config --get --bool gitpkg.force-overwrite-orig)" || true
#ORIG_EXPORT_HOOK="$(git config --get gitpkg.orig-export-hook)" || true
ORIG_COMPRESSOR="$(git config --get gitpkg.orig-compressor)" || ORIG_COMPRESSOR=gzip
ORIG_COMPRESS_LEVEL="$(git config --get gitpkg.orig-compress-level)" || true
DEB_COMPRESSOR="$(git config --get gitpkg.deb-compressor)" || true
DEB_COMPRESS_LEVEL="$(git config --get gitpkg.deb-compress-level)" || true

while read opt; do ORIG_GZ_OPTS+=("$opt")
		done < <(git config --get-all gitpkg.orig-gz-opts)

# If ORIG_GZ_OPTS were not explicitly set, add -n to them here, so that we
# default to creating a reproducible orig.tar.gz without a timestamp in it.
[ ${#ORIG_GZ_OPTS[@]} -gt 0 ] || ORIG_GZ_OPTS=( "-n" )

while read opt; do ORIG_BZ2_OPTS+=("$opt")
		done < <(git config --get-all gitpkg.orig-bz2-opts)

while read opt; do ORIG_XZ_OPTS+=("$opt")
		done < <(git config --get-all gitpkg.orig-xz-opts)

while read opt; do DPKG_SOURCE_OPTS+=("$opt")
		done < <(git config --get-all gitpkg.dpkg-source)

EXIT_HOOK="$(git config --get gitpkg.exit-hook)" || true
KEEP_UNPACKED_SOURCE="$(git config --get gitpkg.keep-unpacked-source)" || true

# XXX should the (an?) exit hook be run on failure too?
#     if we do that, we might also need to tell it how far we got
#     and why we croaked.  Wait until someone needs that maybe.


[ -z "$OVERRIDE_DEB_DIR" ]              || DEB_DIR=$OVERRIDE_DEB_DIR
[ -z "$OVERRIDE_ADMIN_CONFIG_HOOK" ]    || ADMIN_CONFIG_HOOK=$OVERRIDE_ADMIN_CONFIG_HOOK
[ -z "$OVERRIDE_EXIT_HOOK" ]            || EXIT_HOOK=$OVERRIDE_EXIT_HOOK
[ -z "$OVERRIDE_KEEP_UNPACKED_SOURCE" ] || KEEP_UNPACKED_SOURCE=$OVERRIDE_KEEP_UNPACKED_SOURCE


usage()
{ #{{{
    # We may not actually know the real final type yet here.
    case $ORIG_COMPRESSOR in
	gzip)	OCEXT=".gz"  ;;
	bzip2)	OCEXT=".bz2" ;;
	xz)	OCEXT=".xz"  ;;
	*)	OCEXT=".wtf" ;;
    esac

    cat 1>&2 <<EOF

gitpkg branch [origbranch]

  If gitpkg is run in a git repo with a single branch specified, then it will
  do a git-archive export of that branch to the $DEB_DIR directory.  If
  the package is Debian native it will simply create a source package from it.
  $(case $CREATE_FAKE_ORIG in
    (true)
	echo "If the package has a Debian version, then an orig.tar$OCEXT will be created"
	echo "  containing everything except the debian/ dir, if it does not already exist."
	;;
    (false)
	echo "If the package has a Debian version, then an orig.tar$OCEXT must already exist"
	echo "  for it (or it is expected to have been retrieved by one of the hooks)."
	;;
    (*)
	echo "If the package has a Debian version, and an orig.tar$OCEXT does not already"
	echo "  exist for it, then you will be prompted to either go find one, or to let"
	echo "  gitpkg fake one up for you."
	;;
  esac)

  If gitpkg is invoked with two branches specified, then the first branch will
  be checked out as the unpacked complete source, while the second branch will
  be checked out as the orig.tar$OCEXT.  This allows all local changes to the
  source to be recorded in the resulting diff.gz if a pristine upstream branch
  exists in the repository.  $(

  case $FORCE_OVERWRITE_ORIG in
    (true)
	echo "If the orig.tar$OCEXT already exists it will be"
	echo "  overwritten by the source exported from the specified branch."
	;;
    (false)
	echo "If the orig.tar$OCEXT already exists gitpkg will"
	echo "  cry foul."
	;;
    (*)
	echo "If the orig.tar$OCEXT already exists gitpkg will"
	echo "  ask you whether to overwrite it or not."
	;;
  esac)

  'branch' should always have a debian/ dir and may be any tree-ish object that
  is accepted by git-archive(1).  The 'origbranch', if supplied, should usually
  not have a debian/ dir.

EOF

    exit $1
} #}}}



[ ${#GITPKG_PARAMS[@]} -gt 0 ] && [ ${#GITPKG_PARAMS[@]} -lt 3 ] || usage 1

# Export these explicitly to the hook scripts, but few should actually need them.
GITPKG_TREEISH=${GITPKG_PARAMS[0]}
GITPKG_ORIG_TREEISH=${GITPKG_PARAMS[1]}

[ ${#OVERRIDE_DPKG_SOURCE[@]} -eq 0 ] || DPKG_SOURCE_OPTS=( "${OVERRIDE_DPKG_SOURCE[@]}" )


# Next we see what the package itself wants to force for this release
if [ -n "$PACKAGE_CONFIG_HOOK" ]; then
    mkdir -p "$DEB_DIR"

    git archive --format=tar --prefix="gitpkg-${GITPKG_TREEISH}/" \
		"$GITPKG_TREEISH" "$PACKAGE_CONFIG_HOOK" |
		tar -C "$DEB_DIR" -xf -

    # since it's valid for this hook to alter DEB_DIR we need to cache it
    # somewhere that it won't accidentally get run over, so we can clean up.
    dont_alter_this_copy_of_deb_dir_mkay="$DEB_DIR"

    # and since bash 3.2.48 (at least) apparently wants to crap out immediately
    # if sourcing a file fails under set -e, disable that so we can catch and
    # clean up before bailing out.
    set +e
    . "$DEB_DIR/gitpkg-${GITPKG_TREEISH}/$PACKAGE_CONFIG_HOOK" || omg=1

    # Don't complain at them, or waste time testing if we should.  just fix it.
    cd "$REPO_DIR"
    rm -rf "$dont_alter_this_copy_of_deb_dir_mkay/gitpkg-$GITPKG_TREEISH"
    [ -z "$omg" ] || exit 1
    set -e
fi

# Give the BOFH one last chance to look things over and dickheadproof them.
if [ -n "$ADMIN_CONFIG_HOOK" ]; then
    . "$ADMIN_CONFIG_HOOK"
    cd "$REPO_DIR"
    # even admins can screw up sometimes.  catch an easy one for them too.
fi

# Do a little post-last chance option crunching.
case $ORIG_COMPRESSOR in
    gzip)   OCEXT=".gz";  ORIG_COMPRESS_OPTS=( "${ORIG_GZ_OPTS[@]}" )  ;;
    bzip2)  OCEXT=".bz2"; ORIG_COMPRESS_OPTS=( "${ORIG_BZ2_OPTS[@]}" ) ;;
    xz)     OCEXT=".xz";  ORIG_COMPRESS_OPTS=( "${ORIG_XZ_OPTS[@]}" )  ;;
    *)
	echo "gitpkg: Invalid orig-compressor '$ORIG_COMPRESSOR' requested." 1>&2
	echo "        Are you from the ... future?" 1>&2
	# If so, please add support for new formats here.
	# lzma is explicitly not supported since .xz already killed it.
	exit 1
	;;
esac
case $DEB_COMPRESSOR in
    gzip|bzip2|xz)
	DPKG_SOURCE_OPTS+=("-Z$DEB_COMPRESSOR")
	;;
    '')	;;
    *)
	echo "gitpkg: Invalid deb-compressor '$DEB_COMPRESSOR' requested." 1>&2
	echo "        Are you from the ... future?" 1>&2    # If so, see above.
	exit 1
	;;
esac

[ -z "$DEB_COMPRESS_LEVEL" ] || DPKG_SOURCE_OPTS+=("-z$DEB_COMPRESS_LEVEL")

ORIG_COMPRESS_OPTS+=( ${ORIG_COMPRESS_LEVEL:+-$ORIG_COMPRESS_LEVEL} )


get_deb_version()
{ #{{{
    DEB_SOURCE="$(cd "$1" && dpkg-parsechangelog | awk '/Source:/ {print $2}')"
    DEB_VERSION="$(cd "$1" && dpkg-parsechangelog | sed -rne 's/^Version: ([^:]+:)?//p')"
    UPSTREAM_VERSION="${DEB_VERSION%-*}"
    DEB_PACKAGE="${DEB_SOURCE}-${UPSTREAM_VERSION}"
    DEB_DSC="${DEB_SOURCE}_${DEB_VERSION}.dsc"

    case $DEB_VERSION in
	*-*)
	    DEB_ORIG="${DEB_SOURCE}_${UPSTREAM_VERSION}.orig.tar$OCEXT"
	    ;;
    esac
} #}}}


# Fetch (just) the changelog, and loop out through another hook with the package
# name and version information available too.  It's too late to change anything
# now, but not too late to check that it's all ok before getting started.
if [ -n "$PRE_EXPORT_HOOK" ]; then
    mkdir -p "$DEB_DIR"

    git archive --format=tar --prefix="gitpkg-${GITPKG_TREEISH}/" \
		"$GITPKG_TREEISH" debian/changelog |
		tar -C "$DEB_DIR" -xf -

    get_deb_version "$DEB_DIR/gitpkg-$GITPKG_TREEISH"
    rm -rf "$DEB_DIR/gitpkg-$GITPKG_TREEISH"

    if [ -z "$DEB_SOURCE" ] || [ -z "$DEB_VERSION" ];  then
	echo "ERROR: Failed to parse debian/changelog" 1>&2
	exit 1
    fi

    if ! ( . "$PRE_EXPORT_HOOK" ); then
	echo "ERROR: pre-export-hook '$PRE_EXPORT_HOOK' failed" 1>&2
	exit 1
    fi
fi


# get_submodules name/path-array treeish gitdir
get_submodules()
{ #{{{
    local -n mods=$1
    local    treeish=$2 gitdir=$3 k
    local -a conf_keys gitcmd=( 'git' )

    if [ -n "$gitdir" ]; then
	git rev-parse --resolve-git-dir "$gitdir" > /dev/null 2>&1 || return 1
	gitcmd+=( '--git-dir' "$gitdir" )
    fi

    gitcmd+=( 'config' '--blob' "$treeish:.gitmodules" )

    IFS= readarray -d '' conf_keys < \
	<("${gitcmd[@]}" --name-only -z --get-regexp 'path$' 2>/dev/null)

    [ ${#conf_keys[@]} -gt 0 ] || return 1

    for k in "${conf_keys[@]}"; do
	local _name=${k#submodule.}
	local  name=${_name%.path}
	local  path=$("${gitcmd[@]}" --get "$k")

	mods[$name]=$path
    done
} #}}}

# Return true if we need to export submodules, false if we don't,
# and prompt if they exist but what to do with them is not already known.
want_submodules()
{ #{{{
    if [ "$EXPORT_SUBMODULES" = "false" ]; then
	echo "Ignoring submodules in '$1' because EXPORT_SUBMODULES is 'false'."
	return 1
    fi

    local -A m
    get_submodules m "$1" || return 1

    if [ "$EXPORT_SUBMODULES" = "true" ]; then
	echo "Exporting submodules of '$1' because EXPORT_SUBMODULES is 'true'."
	return 0
    fi

    cat <<-EOF

	The '$1' revision of this repo contains one or more submodules.

	Historically gitpkg has silently ignored submodules because git-archive
	does not export them by default, but this version does support including
	them in the exported source package.

	If you always want to include them in future exports of this package
	then you can set:
	 git config gitpkg.export-submodules 'true'
	in the superproject configuration.

	If you want to always ignore them (preserving the historical behaviour
	of gitpkg) then you can set:
	 git config gitpkg.export-submodules 'false'
	in the superproject configuration.

	If that option is not set, then you will be prompted for each export of
	a treeish containing submodules about whether to include them or not.

	EOF

    printf "Export all submodules for this package (y/N)? "
    read -e yesno
    case $yesno in
	Y* | y*)
	    cat <<-EOF

		Ok then.  If you plan to make a habit of this you may wish to set:
		 git config gitpkg.export-submodules 'true'
		in the superproject configuration, to avoid being asked about it
		again each time.

		EOF
	    EXPORT_SUBMODULES="true"
	    ;;
	*)
	    cat <<-EOF

		Ignoring submodules.

		To avoid being asked about this again you can set
		 git config gitpkg.export-submodules 'false'
		in the superproject configuration, and then all subprojects will
		be silently ignored as they are by git-archive.

		EOF
	    EXPORT_SUBMODULES="false"
	    return 1
	    ;;
    esac
} #}}}

export_submodules()
{ #{{{
    local root_prefix=$1 treeish=$2 gitdir=$3
    local -A m

    [ -n "$gitdir" ] || gitdir=$(git rev-parse --git-dir)

    if get_submodules m "$treeish" "$gitdir"; then
	local name

	for name in "${!m[@]}"; do
	    local sm_path=${m[$name]}
	    local sm_hash=$(git --git-dir "$gitdir" rev-parse "$treeish:$sm_path")
	    local sm_gitdir="$gitdir/modules/$name"
	    local prefix="$root_prefix$sm_path/"

	    if ! git rev-parse --resolve-git-dir "$sm_gitdir" > /dev/null 2>&1; then
		cat <<-EOF

		 Submodule '$name' git directory '$sm_gitdir'
		 is not a git repository.
		 This probably means that the submodule has not yet been initialised
		 and cloned into the superproject working directory.

		 Aborting the export so this can be resolved.

		EOF
		exit 1
	    fi

	    echo "git archive exporting submodule $sm_gitdir:$sm_hash to $prefix"

	    git --git-dir "$sm_gitdir" archive --format=tar --prefix="$prefix" "$sm_hash" |
		tar -C "$DEB_DIR" -xf -

	    export_submodules "$prefix" "$sm_hash" "$sm_gitdir"
	done
    fi
} #}}}

export_debian_tree()
{ #{{{
    # We do this before the initial export in case the user wants to bail out
    # at the prompt, so that we won't have junk from half an export to clean up.
    want_submodules "$1" || true

    echo "git archive exporting $1"
    git archive --format=tar --prefix="gitpkg-${1}/" "$1" |
    (
	mkdir -p "$DEB_DIR"
	tar -C "$DEB_DIR" -xf -

	[ "$EXPORT_SUBMODULES" = "false" ] || export_submodules "gitpkg-${1}/" "$1"
    ) || exit 1

    # We don't do this from the subshell so that it's still visible to EXIT_HOOK
    [ -n "$PRE_EXPORT_HOOK" ] || get_deb_version "$DEB_DIR/gitpkg-$1"

    if [ -z "$DEB_SOURCE" ] || [ -z "$DEB_VERSION" ];  then
	rm -rf "$DEB_DIR/gitpkg-$1"
	echo "ERROR: Failed to parse debian/changelog" 1>&2
	exit 1
    fi

    echo "preparing $DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE"
    (
	cd "$DEB_DIR"

	[ -e "$DEB_SOURCE/$DEB_PACKAGE" ] && rm -rf "$DEB_SOURCE/$DEB_PACKAGE"

	mkdir -p "$DEB_SOURCE"
	mv "gitpkg-$1" "$DEB_SOURCE/$DEB_PACKAGE"

	cd "$DEB_SOURCE/$DEB_PACKAGE"

	if [ -n "$DEB_EXPORT_HOOK" ] && ! ( . "$DEB_EXPORT_HOOK" ); then
	    echo "ERROR: deb-export-hook '$DEB_EXPORT_HOOK' failed" 1>&2
	    echo "       in: $DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE" 1>&2
	    exit 1
	fi

	if [ -n "$PREBUILD_TARGET" ]; then
	    echo "$BUILD_ROOTCMD debian/rules $PREBUILD_TARGET"
	    $BUILD_ROOTCMD debian/rules $PREBUILD_TARGET
	fi

    ) || exit 1
} #}}}

format3_lameness()
{ #{{{
    if [ "$ORIG_COMPRESSOR" != "gzip" ] ||
       [ "${DEB_COMPRESSOR:-gzip}" != "gzip" ] ||
       grep -q "^3\.0" "$DEB_PACKAGE/debian/source/format" 2>/dev/null;
    then
	echo "dpkg-source" "${DPKG_SOURCE_OPTS[@]}" "-b $DEB_PACKAGE"
	dpkg-source "${DPKG_SOURCE_OPTS[@]}" -b "$DEB_PACKAGE" || exit 1
    else
	return 1
    fi
} #}}}

build_with_existing_orig_tarball()
{ #{{{
    if ! format3_lameness; then
	echo "dpkg-source" "${DPKG_SOURCE_OPTS[@]}" "-b -sp $DEB_PACKAGE"
	dpkg-source "${DPKG_SOURCE_OPTS[@]}" -b -sp "$DEB_PACKAGE"
    fi
} #}}}

build_native_package()
{ #{{{
    if ! format3_lameness; then
	echo "dpkg-source" "${DPKG_SOURCE_OPTS[@]}" "-b -sn $DEB_PACKAGE"
	dpkg-source "${DPKG_SOURCE_OPTS[@]}" -b -sn "$DEB_PACKAGE"
    fi
} #}}}

build_with_exported_orig()
{ #{{{
    # We build this ourselves and use -ss rather than -sR because dpkg-source
    # handles making tarballs badly anyhow and will leave the .orig suffix
    # in the path inside them.

    echo "tar cf - $DEB_PACKAGE(.orig) | $ORIG_COMPRESSOR" "${ORIG_COMPRESS_OPTS[@]}" "> $DEB_ORIG"
    tar --transform "s,^$DEB_PACKAGE.orig,$DEB_PACKAGE," \
	-cf - "$DEB_PACKAGE.orig" | $ORIG_COMPRESSOR "${ORIG_COMPRESS_OPTS[@]}" > "$DEB_ORIG"

    if ! format3_lameness; then
	echo "dpkg-source" "${DPKG_SOURCE_OPTS[@]}" "-b -ss $DEB_PACKAGE"
	dpkg-source "${DPKG_SOURCE_OPTS[@]}" -b -ss "$DEB_PACKAGE"
    fi
    rm -rf "$DEB_PACKAGE.orig"
} #}}}

build_with_faked_orig()
{ #{{{
    if [ "$CREATE_FAKE_ORIG" != "true" ]; then
	cat <<-EOF

	The package you are exporting has a Debian version, but there is no
	 $DEB_ORIG
	already present and you did not provide a treeish to export one from.
	EOF

	if [ "$CREATE_FAKE_ORIG" = "false" ]; then
	    rm -rf "$DEB_PACKAGE"
	    exit 1
	fi

	cat <<-EOF
	We can fake one up from everything but the debian/ directory if you
	would like, but this may not be what you had originally intended.

	You have exported '$GITPKG_TREEISH'.
	EOF
	printf "Fake a $DEB_ORIG from that (y/N)? "
	read -e yesno
	case $yesno in
	    Y* | y*)
		cat <<-EOF

		Ok then.  If you plan to make a habit of this you may wish to set:
		 git config gitpkg.create-fake-orig 'true'
		To avoid being asked about it again each time.

		EOF
		;;
	    *)
		cat <<-EOF
		Yeah, good call.

		To avoid being asked about this again you can set
		 git config gitpkg.create-fake-orig 'false'
		then in future this will simply print an error and fail.
		EOF

		rm -rf "$DEB_PACKAGE"
		exit 1
		;;
	esac
    fi

    [ -e "$DEB_PACKAGE.orig" ] && rm -rf "$DEB_PACKAGE.orig"

    cp -al "$DEB_PACKAGE" "$DEB_PACKAGE.orig"
    rm -rf "$DEB_PACKAGE.orig/debian"

    build_with_exported_orig
} #}}}

do_exit()
{ #{{{
    if [ -n "$EXIT_HOOK" ] && ! ( cd "$DEB_DIR/$DEB_SOURCE" && . "$EXIT_HOOK" );
    then
	echo "ERROR: gitpkg.exit-hook '$EXIT_HOOK' failed" 1>&2
	exit 1
    fi

    if [ "$1" == "0" ] && [ "$KEEP_UNPACKED_SOURCE" != "true" ]; then
	if [ -e "$DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE/debian/control" ]; then
	    echo "rm -rf $DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE"
	    rm -rf "$DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE"
	fi
    fi

    printf "$EXIT_MSG"

    exit $1
} #}}}


# Ok, if we're still here, let's reap what they've sown.
if [ -z "$GITPKG_ORIG_TREEISH" ]; then

    export_debian_tree "$GITPKG_TREEISH"

    (
	cd "$DEB_DIR/$DEB_SOURCE"

	case $DEB_VERSION in
	    (*-*)
		if [ -e "$DEB_ORIG" ]; then
		    build_with_existing_orig_tarball
		else
		    build_with_faked_orig
		fi
		;;

	    (*)
		build_native_package
		;;
	esac

    ) || exit 1

else

    export_debian_tree "$GITPKG_TREEISH"

    # Check that get_deb_version really did find a debian version number
    if [ -z "$DEB_ORIG" ]; then
	cat <<-EOF

	 You have requested an orig tarball to be exported from '$GITPKG_ORIG_TREEISH',
	 but the debian source from '$GITPKG_TREEISH' does not have a debian version
	 in the changelog (version '$DEB_VERSION' was found).

	 If this is really a native package, you probably want to call gitpkg
	 with just a single treeish.  If it is not, now might be a good time
	 to fix the changelog entry to include a debian version part.

	 Stopping here until that is sorted out.
	EOF

	exit 1
    fi

    # Check for an existing orig.tar.gz and what to do if we have one ...
    if [ -e "$DEB_DIR/$DEB_SOURCE/$DEB_ORIG" ]
    then
	if [ "$FORCE_OVERWRITE_ORIG" = "true" ]; then
	    echo "Overwriting existing $DEB_DIR/$DEB_SOURCE/$DEB_ORIG"
	else
	    cat <<-EOF

		You have requested '$GITPKG_ORIG_TREEISH' be exported,
		but $DEB_ORIG is already present.
		EOF

	    if [ "$FORCE_OVERWRITE_ORIG" = "false" ]; then
		rm -rf "$DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE"
		exit 1
	    fi

	    printf "Overwrite the existing orig with this repo export (y/N)? "
	    read -e yesno
	    case $yesno in
		Y* | y*)
		    cat <<-EOF

			Ok then.  If you plan to make a habit of this you may wish to set:
			 git config gitpkg.force-overwrite-orig 'true'
			To avoid being asked about it again each time.

			EOF
		    ;;
		*)
		    printf "Using the existing $DEB_ORIG, that's probably smart.\n\n"
		    (
			cd "$DEB_DIR/$DEB_SOURCE"
			build_with_existing_orig_tarball

		    ) || exit 1

		    # All done!
		    do_exit 0
		    ;;
	    esac
	fi
    fi

    # else check out the requested pristine upstream branch now too
    if want_submodules "$GITPKG_ORIG_TREEISH" || [ -n "$ORIG_EXPORT_HOOK" ]; then

	[ -e "$DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE.orig" ] &&
	    rm -rf "$DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE.orig"

	echo "git archive exporting $GITPKG_ORIG_TREEISH to $DEB_PACKAGE.orig/"
	git archive --format=tar --prefix="$DEB_SOURCE/$DEB_PACKAGE.orig/" "$GITPKG_ORIG_TREEISH" |
	(
	    tar -C "$DEB_DIR" -xf -

	    [ "$EXPORT_SUBMODULES" = "false" ] ||
		export_submodules "$DEB_SOURCE/$DEB_PACKAGE.orig/" "$GITPKG_ORIG_TREEISH"

	    if [ -n "$ORIG_EXPORT_HOOK" ]; then
		(
		    cd "$DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE.orig"

		    if ! ( . "$ORIG_EXPORT_HOOK" ); then
			echo "ERROR: orig-export-hook '$ORIG_EXPORT_HOOK' failed" 1>&2
			echo "       in: $DEB_DIR/$DEB_SOURCE/$DEB_PACKAGE.orig" 1>&2
			exit 1
		    fi

		) || exit 1
	    fi

	    cd "$DEB_DIR/$DEB_SOURCE"
	    build_with_exported_orig

	) || exit 1
    else
	echo "git archive --prefix=$DEB_PACKAGE/ $GITPKG_ORIG_TREEISH" \
	     "| $ORIG_COMPRESSOR" "${ORIG_COMPRESS_OPTS[@]}" "> $DEB_ORIG"
	git archive --format=tar --prefix="$DEB_PACKAGE/" "$GITPKG_ORIG_TREEISH" |
		$ORIG_COMPRESSOR "${ORIG_COMPRESS_OPTS[@]}" > "$DEB_DIR/$DEB_SOURCE/$DEB_ORIG"
	(
	    cd "$DEB_DIR/$DEB_SOURCE"
	    build_with_existing_orig_tarball

	) || exit 1
    fi
fi

do_exit 0

# vi:sts=4:sw=4:noet:foldmethod=marker