Updated control files for adding changelog entries.
[dcp.git] / dcp-init
CommitLineData
9f26b93f
FT
1#!/bin/bash
2
3set -e
4
5usage() {
078110f6 6 echo "usage: dcp-init [-sh] [-C key=val] [-d REPODIR] PACKAGE [(PATCH|-m)...]"
9f26b93f
FT
7 echo " PATCH ::= [-p LEVEL] [-bB BRANCH] FILE"
8 echo " -b creates a new branch at the current patch"
9 echo " -B creates a new branch at the upstream sources"
078110f6 10 echo " -m merges the current branch into the master branch"
9f26b93f
FT
11}
12
13rungit() {
14 (
15 cd "$td"
16 git "$@"
17 ) || false
18}
19
20getaptsrc() {
21 mkdir "$td/apt-tmp"
22 (cd "$td/apt-tmp"; apt-get source "$1") || false
23
9f26b93f
FT
24 sdir=
25 for f in "$td/apt-tmp"/*; do
26 if [ -d "$f" ]; then
27 if [ -z "$sdir" ]; then
28 sdir="$f"
29 else
30 echo "dcp-init: got more than one directory from apt-get; cannot continue" >&2
31 exit 1
32 fi
33 fi
34 done
35 if [ -z "$sdir" ]; then
36 echo "dcp-init: could not find any source directory" >&2
37 exit 1
38 fi
39 mv "$sdir" "$td/src"
40 rm -rf "$td/apt-tmp"
41
42 rungit add src
43}
44
45initrepo() {
f90e1262 46 rungit init
9f26b93f
FT
47 mkdir "$td/control"
48 touch "$td/control/conf"
49 rungit add control
50 rungit commit -q -m "Initial repo"
51 rungit tag empty
52}
53
54initbase() {
55 mkdir "$td/control/build.d"
56 mkdir "$td/control/update.d"
57 cat >"$td/control/functions" <<EOF
58readconf() {
ad9aaa98 59 if [ -r "\$HOME/.dcp-build-conf" ]; then
6a6fbc9a
FT
60 while read key val; do
61 export "CONF_\$key"="\$val"
0941227c 62 done <"\$HOME/.dcp-build-conf"
6a6fbc9a 63 fi
54cb3b7c
FT
64 if [ -r "/etc/dcp-conf" ]; then
65 while read key val; do
66 export "CONF_\$key"="\$val"
67 done <"/etc/dcp-conf"
68 fi
9f26b93f
FT
69 while read key val; do
70 export "CONF_\$key"="\$val"
71 done <control/conf
72}
73
74rungit() {
75 (cd repo; git "\$@") || false
76}
54cb3b7c
FT
77
78logchange() {
79 tag="\${CONF_VERTAG:-dcp}"
80 ver="\$(dpkg-parsechangelog -lrepo/src/debian/changelog | sed -n 's/^Version: \(.*\)$/\1/p')"
81 [ -n "\$ver" ]
82 maint="\$CONF_MAINTAINER"
83 if [ -z "\$maint" ]; then
84 maint="\$(id -un) <\$(id -un)@\$(hostname -f)>"
85 fi
86 cat - repo/src/debian/changelog >repo/src/debian/changelog.new <<ENDCL
87\${CONF_APTPKG} (\${ver}+\${tag}1) unstable; urgency=low
88
89 * Remerged changes in DCP
90
91 -- \${maint} \$(date -R)
92ENDCL
93 mv -f repo/src/debian/changelog.new repo/src/debian/changelog
94}
9f26b93f
FT
95EOF
96 cat >"$td/control/build" <<EOF
97#!/bin/sh
98
99set -e
100
101. control/functions
102readconf
103
104for file in control/build.d/*; do
105 if [ -x "\$file" ]; then "\$file"; fi
106done
107EOF
108 chmod 755 "$td/control/build"
109 cat >"$td/control/update" <<EOF
110#!/bin/sh
111
112set -e
113
114. control/functions
115readconf
116
54cb3b7c 117updated=n
9f26b93f
FT
118for branch in repo/.git/refs/heads/*; do
119 branch="\${branch##*/}"
120 if [ -x "control/update.d/\$branch" ]; then
121 rungit checkout "\$branch"
122 lastrev="\$(rungit rev-parse HEAD)"
123 "control/update.d/\$branch"
124 newrev="\$(rungit rev-parse HEAD)"
125 rungit checkout master
126 if [ "\$newrev" != "\$lastrev" ]; then
127 rungit merge -n "\$branch"
54cb3b7c 128 updated=y
9f26b93f
FT
129 fi
130 fi
131done
54cb3b7c
FT
132
133if [ "\$updated" = y ]; then
134 for file in control/update.d/post-*; do
135 if [ -x "\$file" ]; then "\$file"; fi
136 done
137fi
9f26b93f
FT
138EOF
139 chmod 755 "$td/control/update"
140 rungit add control
141}
142
143initapt() {
144 cat >"$td/control/build.d/99dpkg" <<EOF
145#!/bin/bash
146
147set -e
148
664ffd32 149cmd=(dpkg-buildpackage -b -rfakeroot)
9f26b93f
FT
150if [ -n "\$CONF_MAINTAINER" ]; then
151 cmd=("\${cmd[@]}" "-m\$CONF_MAINTAINER")
152fi
153if [ -n "\$CONF_GPGKEY" ]; then
154 cmd=("\${cmd[@]}" "-k\$CONF_GPGKEY")
155fi
156(cd repo/src; "\${cmd[@]}") || false
157mv repo/*.deb output/
158EOF
159 chmod 755 "$td/control/build.d/99dpkg"
160 rungit add control/build.d/99dpkg
161 cat >"$td/control/update.d/upstream" <<EOF
162#!/bin/sh
163
164set -e
165
54cb3b7c
FT
166. control/functions
167
9f26b93f
FT
168cd repo
169dcp-update-apt "\$CONF_APTPKG"
9f26b93f
FT
170EOF
171 chmod 755 "$td/control/update.d/upstream"
172 rungit add control/update.d/upstream
54cb3b7c
FT
173 cat >"$td/control/update.d/post-logchange" <<EOF
174#!/bin/sh
175
176set -e
177. control/functions
178
179logchange
180rungit add src/debian/changelog
181rungit commit -q -m "Added changelog entry"
182
183EOF
9f26b93f
FT
184 echo "APTPKG $pkg" >>"$td/control/conf"
185 rungit add control/conf
186}
187
188defdir=/srv/dcp
189repodir=
190confopts=()
12978997 191shared=n
9f26b93f
FT
192
193while [ "${1:0:1}" = - ]; do
194 opt="${1:1}"
195 shift
196 if [ "$opt" = d ]; then
197 repodir="$1"
198 shift
199 elif [ "$opt" = h ]; then
200 usage
201 exit 0
12978997
FT
202 elif [ "$opt" = s ]; then
203 shared=y
9f26b93f
FT
204 elif [ "$opt" = C ]; then
205 confopts=("${confopts[@]}" "$1")
206 shift
207 else
208 echo "dcp-init: unknown option '$opt'" >&2
209 exit 1
210 fi
211done
212
213if [ $# -lt 1 ]; then
214 usage >&2
215 exit 1
216fi
217pkg="$1"
218shift
219if [ -z "$repodir" ]; then repodir="$pkg"; fi
220if [[ "$repodir" != */* ]]; then
221 repodir="$defdir/${repodir}.git"
222fi
223
224td="$(mktemp -d "/tmp/dcp-XXXXXX")"
225initrepo
226
227initbase
228rungit commit -q -m "Base control files"
229
230if [ "${#confopts[@]}" -gt 0 ]; then
231 for opt in "${confopts[@]}"; do
232 key="${opt%%=*}"
233 val="${opt#*=}"
234 echo "$key $val" >>"$td/control/conf"
235 done
236 rungit add control/conf
237 rungit commit -q -m "Custom configuration file"
238fi
239
240rungit checkout -q -b upstream empty
241getaptsrc "$pkg"
242rungit commit -q -m "Initial upstream import"
243rungit checkout master
244rungit merge -n upstream
245
246initapt
247rungit commit -q -m "APT control files"
248
249initvals() {
250 level=0
251}
252initvals
078110f6 253curbranch=master
9f26b93f
FT
254while [ $# -gt 0 ]; do
255 arg="$1"
256 shift
257 if [ "${arg:0:1}" = - ]; then
258 if [ "$arg" = -p ]; then
259 level="$1"
260 shift
261 elif [ "$arg" = -b ]; then
078110f6 262 curbranch="$1"
9f26b93f 263 shift
078110f6 264 rungit checkout -q -b "$curbranch"
9f26b93f 265 elif [ "$arg" = -B ]; then
078110f6
FT
266 curbranch="$1"
267 shift
268 rungit checkout -q -b "$curbranch" upstream
269 elif [ "$arg" = -m ]; then
270 rungit checkout -q master
271 rungit merge -n "$curbranch"
9f26b93f
FT
272 else
273 echo "dcp-init: unknown patch option '$arg'" >&2
274 exit 1
275 fi
276 else
277 (
278 if [[ "$arg" == *.gz ]]; then
279 zcat "$arg"
280 else
281 cat "$arg"
282 fi
283 ) | patch -d "$td/src" -p"$level"
284 rungit add src
285 rungit commit -q -m "Applied $(basename "$arg")"
286 initvals
287 fi
288done
289
290git clone -q --bare "$td" "$repodir"
12978997
FT
291if [ "$shared" = y ]; then
292 chmod -R g+w "$repodir"
293 td="$repodir" rungit config core.sharedrepository 1
294fi
9f26b93f
FT
295
296rm -rf "$td"