Added -m operation to dcp-init.
[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
9f26b93f
FT
64 while read key val; do
65 export "CONF_\$key"="\$val"
66 done <control/conf
67}
68
69rungit() {
70 (cd repo; git "\$@") || false
71}
72EOF
73 cat >"$td/control/build" <<EOF
74#!/bin/sh
75
76set -e
77
78. control/functions
79readconf
80
81for file in control/build.d/*; do
82 if [ -x "\$file" ]; then "\$file"; fi
83done
84EOF
85 chmod 755 "$td/control/build"
86 cat >"$td/control/update" <<EOF
87#!/bin/sh
88
89set -e
90
91. control/functions
92readconf
93
94for branch in repo/.git/refs/heads/*; do
95 branch="\${branch##*/}"
96 if [ -x "control/update.d/\$branch" ]; then
97 rungit checkout "\$branch"
98 lastrev="\$(rungit rev-parse HEAD)"
99 "control/update.d/\$branch"
100 newrev="\$(rungit rev-parse HEAD)"
101 rungit checkout master
102 if [ "\$newrev" != "\$lastrev" ]; then
103 rungit merge -n "\$branch"
104 fi
105 fi
106done
107EOF
108 chmod 755 "$td/control/update"
109 rungit add control
110}
111
112initapt() {
113 cat >"$td/control/build.d/99dpkg" <<EOF
114#!/bin/bash
115
116set -e
117
664ffd32 118cmd=(dpkg-buildpackage -b -rfakeroot)
9f26b93f
FT
119if [ -n "\$CONF_MAINTAINER" ]; then
120 cmd=("\${cmd[@]}" "-m\$CONF_MAINTAINER")
121fi
122if [ -n "\$CONF_GPGKEY" ]; then
123 cmd=("\${cmd[@]}" "-k\$CONF_GPGKEY")
124fi
125(cd repo/src; "\${cmd[@]}") || false
126mv repo/*.deb output/
127EOF
128 chmod 755 "$td/control/build.d/99dpkg"
129 rungit add control/build.d/99dpkg
130 cat >"$td/control/update.d/upstream" <<EOF
131#!/bin/sh
132
133set -e
134
135cd repo
136dcp-update-apt "\$CONF_APTPKG"
137
138EOF
139 chmod 755 "$td/control/update.d/upstream"
140 rungit add control/update.d/upstream
141 echo "APTPKG $pkg" >>"$td/control/conf"
142 rungit add control/conf
143}
144
145defdir=/srv/dcp
146repodir=
147confopts=()
12978997 148shared=n
9f26b93f
FT
149
150while [ "${1:0:1}" = - ]; do
151 opt="${1:1}"
152 shift
153 if [ "$opt" = d ]; then
154 repodir="$1"
155 shift
156 elif [ "$opt" = h ]; then
157 usage
158 exit 0
12978997
FT
159 elif [ "$opt" = s ]; then
160 shared=y
9f26b93f
FT
161 elif [ "$opt" = C ]; then
162 confopts=("${confopts[@]}" "$1")
163 shift
164 else
165 echo "dcp-init: unknown option '$opt'" >&2
166 exit 1
167 fi
168done
169
170if [ $# -lt 1 ]; then
171 usage >&2
172 exit 1
173fi
174pkg="$1"
175shift
176if [ -z "$repodir" ]; then repodir="$pkg"; fi
177if [[ "$repodir" != */* ]]; then
178 repodir="$defdir/${repodir}.git"
179fi
180
181td="$(mktemp -d "/tmp/dcp-XXXXXX")"
182initrepo
183
184initbase
185rungit commit -q -m "Base control files"
186
187if [ "${#confopts[@]}" -gt 0 ]; then
188 for opt in "${confopts[@]}"; do
189 key="${opt%%=*}"
190 val="${opt#*=}"
191 echo "$key $val" >>"$td/control/conf"
192 done
193 rungit add control/conf
194 rungit commit -q -m "Custom configuration file"
195fi
196
197rungit checkout -q -b upstream empty
198getaptsrc "$pkg"
199rungit commit -q -m "Initial upstream import"
200rungit checkout master
201rungit merge -n upstream
202
203initapt
204rungit commit -q -m "APT control files"
205
206initvals() {
207 level=0
208}
209initvals
078110f6 210curbranch=master
9f26b93f
FT
211while [ $# -gt 0 ]; do
212 arg="$1"
213 shift
214 if [ "${arg:0:1}" = - ]; then
215 if [ "$arg" = -p ]; then
216 level="$1"
217 shift
218 elif [ "$arg" = -b ]; then
078110f6 219 curbranch="$1"
9f26b93f 220 shift
078110f6 221 rungit checkout -q -b "$curbranch"
9f26b93f 222 elif [ "$arg" = -B ]; then
078110f6
FT
223 curbranch="$1"
224 shift
225 rungit checkout -q -b "$curbranch" upstream
226 elif [ "$arg" = -m ]; then
227 rungit checkout -q master
228 rungit merge -n "$curbranch"
9f26b93f
FT
229 else
230 echo "dcp-init: unknown patch option '$arg'" >&2
231 exit 1
232 fi
233 else
234 (
235 if [[ "$arg" == *.gz ]]; then
236 zcat "$arg"
237 else
238 cat "$arg"
239 fi
240 ) | patch -d "$td/src" -p"$level"
241 rungit add src
242 rungit commit -q -m "Applied $(basename "$arg")"
243 initvals
244 fi
245done
246
247git clone -q --bare "$td" "$repodir"
12978997
FT
248if [ "$shared" = y ]; then
249 chmod -R g+w "$repodir"
250 td="$repodir" rungit config core.sharedrepository 1
251fi
9f26b93f
FT
252
253rm -rf "$td"