summaryrefslogtreecommitdiffstats
path: root/bin/pupdate.sh
blob: 1ed020f8d4c59850b61dccfe27fe890a6422c4b6 (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
#!/bin/sh

set -e

case "$(basename ${0})" in
	d*)
		MODE="debian"
		;;

	p*)
		MODE="progress-linux"
		;;

	b*)
		MODE="bfh"
		;;
esac

case "${MODE}" in
	debian)
		DOWNSTREAM_BRANCH=""
		;;

	progress-linux)
		DOWNSTREAM_BRANCH="progress-linux"
		;;

	bfh)
		DOWNSTREAM_BRANCH="bfh"
		;;
esac

RED="\033[1;33;31m"
GREEN="\033[1;33;32m"
YELLOW="\033[1;33;33m"
BLUE="\033[1;33;34m"
WHITE="\033[1;33;37m"
NORMAL="\033[0m"
PURPLE="\033[1;33;35m"

DSC="${1}"

case "${DSC}" in
	*.dsc)
		;;

	*)
		echo "Usage: ${0} DSC"
		exit 1
		;;
esac

case "${DSC}" in
	http*)
		DSC_TEMPDIR="$(mktemp -d)"

		cd "${DSC_TEMPDIR}"
		dget -d "${DSC}"
		cd "${OLDPWD}"

		DSC="${DSC_TEMPDIR}/$(basename ${DSC})"
		;;
esac

PACKAGE="$(basename ${DSC} | awk -F_ '{ print $1 }')"
DEB_VERSION="$(awk '/^Version: / { print $2 }' ${DSC})"

case "${DEB_VERSION}" in
	*-*)
		DEB_UPSTREAM_VERSION="$(echo ${DEB_VERSION} | awk -F- '{ $NF=""; print $0 }' | sed -e 's| |-|g' -e 's|-$||g')"
		;;

	*)
		DEB_UPSTREAM_VERSION="${DEB_VERSION}"
		;;
esac

Switch_upstream_branch ()
{
	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" != "upstream" ] && git branch | grep -qs upstream
	then
		echo
		echo "################################################################################"
		echo "git: switching to upstream branch"
		echo "################################################################################"

		git checkout upstream
	fi
}

Remove_everything ()
{
	echo
	echo "################################################################################"
	echo "git: removing all files"
	echo "################################################################################"

	echo -n "Removing files for new import..."

	find . -maxdepth 1 -mindepth 1 -and -not -name .git -print0 | xargs -0 -I '{}' rm -rf '{}'

	echo " done."
}

Unpack_upstream ()
{
	echo
	echo "################################################################################"
	echo "dpkg: unpacking upstream files"
	echo "################################################################################"

	TEMPDIR="$(mktemp -d -p . -u)"

	dpkg-source -x --no-copy --skip-debianization ${DSC} "${TEMPDIR}"
	rm -rf "${TEMPDIR}/debian"
	find "${TEMPDIR}" -type d -name ".git" -exec rm -rf {} \; || true

	DEBIAN_TAR="$(dirname $(readlink -f ${DSC}))/$(awk '/debian.tar.*$/ { print $3 }' ${DSC} | head -n1)"

	case "${DEBIAN_TAR}" in
		*.debian.tar.*)
			if [ -e "${DEBIAN_TAR}" ]
			then
				# this includes upstream additions outside debian/ into the upstream branch
				cd "${TEMPDIR}"
				tar xf "${DEBIAN_TAR}"
				rm -rf debian
				rm -rf .git
				cd "${OLDPWD}"
			fi
			;;
	esac

	find "${TEMPDIR}" -maxdepth 1 -mindepth 1 -print0 | xargs -0 -I '{}' mv '{}' .
	rmdir "${TEMPDIR}"
}

Add_upstream_version ()
{
	echo
	echo "################################################################################"
	echo "git: adding upstream version"
	echo "################################################################################"

	# FIXME
	git-upstream-add ${DEB_UPSTREAM_VERSION}
}

Switch_debian_branch ()
{
	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" != "debian" ] && git branch | grep -qs debian
	then
		echo
		echo "################################################################################"
		echo "git: switching to debian branch"
		echo "################################################################################"

		git checkout debian
	fi
}

Unpack_debian ()
{
	echo
	echo "################################################################################"
	echo "dpkg: unpacking debian files"
	echo "################################################################################"

	TEMPDIR="$(mktemp -d -p . -u)"

	dpkg-source -x --no-copy --skip-patches ${DSC} ${TEMPDIR}
	find "${TEMPDIR}" -type d -name ".git" -exec rm -rf {} \; || true

	find "${TEMPDIR}" -maxdepth 1 -mindepth 1 -print0 | xargs -0 -I '{}' mv '{}' .

	rmdir ${TEMPDIR}
}

Add_debian_version ()
{
	echo
	echo "################################################################################"
	echo "git: adding debian version"
	echo "################################################################################"

	# FIXME
	git-debian-add
}

Switch_downstream_branch ()
{
	CURRENT_BRANCH="$(git branch --show-current)"

	if [ "${CURRENT_BRANCH}" != "${DOWNSTREAM_BRANCH}" ] && git branch | grep -qs ${DOWNSTREAM_BRANCH}
	then
		echo
		echo "################################################################################"
		echo "git: switching to downstream branch"
		echo "################################################################################"

		git checkout ${DOWNSTREAM_BRANCH}
	fi
}

Merge_upstream_version ()
{
	git cherry-pick -n upstream
	git commit -a -s -S -m "Merging upstream version ${DEB_UPSTREAM_VERSION}."
}

Merge_debian_version ()
{
	git cherry-pick -n debian
	git commit -a -s -S -m "Merging debian version ${DEB_VERSION}."
}

echo "################################################################################"
echo "git: importing ${DSC}"
echo "################################################################################"

GIT_UPSTREAM_VERSION="$(git tag | awk -F/ '/^upstream\// { print $2 }' | sort -rV | head -n1 | sed -e 's|%|:|g' -e 's|_|~|g')"

if [ "${DEB_UPSTREAM_VERSION}" != "${GIT_UPSTREAM_VERSION}" ]
then
	Switch_upstream_branch

	Remove_everything
	Unpack_upstream
	Add_upstream_version

	MERGE_UPSTREAM="true"
fi

GIT_DEBIAN_VERSION="$(git tag | awk -F/ '/^debian\// { print $2 }' | sort -rV | head -n1 | sed -e 's|%|:|g' -e 's|_|~|g')"

if [ "${DEB_VERSION}" != "${GIT_DEBIAN_VERSION}" ]
then
	Switch_debian_branch

	case "${MERGE_UPSTREAM}" in
		true)
			Merge_upstream_version
			;;
	esac

	Remove_everything
	Unpack_debian
	Add_debian_version

	MERGE_DEBIAN="true"
fi

if [ -n "${DSC_TEMPDIR}" ]
then
	rm -rf "${DSC_TEMPDIR}"
fi

if [ "${MERGE_DEBIAN}" = "true" ] && [ -n "${DOWNSTREAM_BRANCH}" ]
then
	Switch_downstream_branch

	case "${MERGE_UPSTREAM}" in
		true)
			Merge_upstream_version
			;;
	esac

	Merge_debian_version
fi