summaryrefslogtreecommitdiffstats
path: root/bin/git-dpkg-source
blob: cc06e3ad976d63712973bacbff4b2e27aea5770c (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
#!/bin/sh

# source-tools - Manage Git repositories effectively and efficiently
# Copyright (C) 2014-2017 Daniel Baumann <daniel.baumann@open-infrastructure.net>

set -e

# FIXME: handle backports specific changelog entry.

# Assumptions:
#
# * the repository is using pristine-tar for non-native packages.
# * release commits, if any, are of the form 'Releasing debian version ...'.
# * import commits, if any, are of the form 'Adding debian version ...'.
# * the repository has at least either of a release or an import commit on the
#   target branch.
# * and finally, the target branch is including upstream sources, not just
#   debianization. but that should be clear anyway.


# Check depends
for FILE in git git-dch dpkg-parsechangelog pristine-tar
do
	if [ ! -x "$(which ${FILE} 2>/dev/null)" ]
	then
		echo "${FILE} missing, aborting."
		exit 1
	fi
done

# Arguments
if [ -z "${1}" ]
then
	echo "Usage: ${0} REPOSITORY [DISTRIBUTION] [BRANCH]"
	exit 1
fi

REPOSITORY="${1}"
DISTRIBUTION="${2:-UNRELEASED}"
BRANCH="${3}"

# Dynamic variables
PACKAGE="$(basename ${REPOSITORY} .git)"

# Static variables
TEMPDIR="$(mktemp -d -t git-dpkg-source.XXXXXXXX)"
CURDIR="${PWD}"

################################################################################

# Creating build directory
mkdir -p "${TEMPDIR}"

# Cloning sources
cd "${TEMPDIR}"
git clone ${REPOSITORY}

# Checking out branch
cd "${TEMPDIR}"/${PACKAGE}

if [ -n "${BRANCH}" ] && [ ! "$(git branch | awk '/^\* / { print $2 }')" = "${BRANCH}" ] &&
   [ -n "$(git branch -r | grep ${BRANCH}$ )" ]
then
	# user specified a branch because he wants to build what is not
	# the default branch of his repository, also:
	# we are not on that target branch, but the target branch exists,
	# so we check it out and switch to it.
	git checkout -b ${BRANCH} origin/${BRANCH} || true
fi

# Assemble version information
VERSION="$(dpkg-parsechangelog | awk '/Version:/ { print $2 }' | sed -e 's|.*:||')"
UPSTREAM_VERSION="$(echo ${VERSION} | sed -e 's|-.*||')"

if echo $(dpkg-parsechangelog | awk '/Version:/ { print $2 }')| grep -q ":"
then
	EPOCHE="$(dpkg-parsechangelog | awk '/Version:/ { print $2 }' | awk -F: '{ print $1 }'):"
else
	EPOCHE=""
fi

# let's see if we have a newer upstream version checked in
if git branch -r | grep -q pristine-tar
then
	TAR_VERSION="$(git show origin/pristine-tar | awk -F_ '/^\+\+\+(.*)tar.gz.id$/ { print $2 }' | sed -e 's|.orig.tar.gz.id||' -e 's|.tar.gz.id||')"
fi

DELIMETER=""

if [ -n "${TAR_VERSION}" ] && [ "${UPSTREAM_VERSION}" != "${TAR_VERSION}" ]
then
	# there has been newer upstream version merged
	# (means: head has newer upstream than last release commit)
	VERSION="${TAR_VERSION}-"
	UPSTREAM_VERSION="${TAR_VERSION}"
	DELIMETER="~"
else
	VERSION="${VERSION}"
fi

# let's see if the last commit was a release or import commit.
# FIXME: it's more generic to compare if the last commit matches any available tag.
if [ -z "$(git log | grep -m1 Date -A2 | grep "Releasing debian version ")" ] && \
   [ -z "$(git log | grep -m1 Date -A2 | grep "Adding debian version ")" ] && \
   [ -z "$(git log | grep -m1 Date -A2 | grep "Releasing version ")"] && \
   [ -z "$(git log | grep -m1 Date -A2 | grep "Adding version ")" ]
then
	# we are building a snapshot
	if [ -z "${DELIMETER}" ]
	then
		DELIMETER="+"
	fi

	# building version string from date of last commit
	REVISION="$(git log | grep -m1 Date | awk -FDate: '{ print $2 }' | awk '{ print $1 ",", $3, $2, $5, $4, $6 }')"
	REVISION="${DELIMETER}$(date -d "${REVISION}" +%Y%m%d.%H%M%S)"
else
	# we are building a release
	REVISION=""
fi

HASH="$(cd "${TEMPDIR}"/${PACKAGE} && git log | grep -m1 commit | awk '{ print $2 }')"

# Renaming package directory (in case we are building native,
# it would matter for cosemtics; if non-native it's ignored anyway)
mv "${TEMPDIR}"/${PACKAGE} "${TEMPDIR}"/${PACKAGE}-${VERSION}${REVISION}

# Creating changelog
cd "${TEMPDIR}"/${PACKAGE}-${VERSION}${REVISION}

if [ -z "${DEBFULLNAME}" ]
then
	if [ -n "$(git config --get user.name)" ]
	then
		# take name from git config
		DEBFULLNAME="$(git config --get user.name)"
	else
		# or from last commit in the repository
		DEBFULLNAME="$(git log | grep -m1 "^Author: " | sed -e 's|^Author: ||' -e 's| <.*>$||')"
	fi
fi

if [ -z "${DEBEMAIL}" ]
then
	if [ -n "$(git config --get user.email)" ]
	then
		# take email from git config
		DEBEMAIL="$(git config --get user.email)"
	else
		# or from last commit in the repository
		DEBEMAIL="$(git log | grep -m1 "^Author: " | sed -e 's|^.*<||' -e 's|>$||')"
	fi
fi

export DEBFULLNAME DEBEMAIL

if [ -n "${REVISION}" ]
then
	# we are building a snapshot, let's get the hash of the last release
	OLD_HASH="$(git log | grep -B4 -m1 'Releasing debian version ' | awk '/commit / { print $2 }')"
	if [ -z "${OLD_HASH}" ]
	then
		# if there wasn't a release commit, maybe just have an import commit.
		OLD_HASH="$(git log | grep -B4 -m1 'Adding debian version ' | awk '/commit / { print $2 }')"
	fi

	# Add git commit messages since the last release/import.
	echo "Running git-dch..."
	git-dch --debian-branch $(git branch | awk '/^\* / { print $2 }') --since ${OLD_HASH} --new-version ${EPOCHE}${VERSION}${REVISION}

	# Create changelog top note.
	HEADER="$(head -1 debian/changelog)"
	sed -i -e '1 d' debian/changelog

	mv debian/changelog debian/changelog.tmp

#case "${DISTRIBUTION}" in
#	lenny-backports)
#
#cat > debian/changelog << EOF
#${HEADER}
#
#  * Rebuilding for Debian GNU/Linux 5.0 "lenny".
#EOF
#
#		;;
#
#	sid-snapshots)
#
cat > debian/changelog << EOF
${HEADER}

  * Unreleased snapshot from Git repository:
    ${HASH}
EOF

#		;;
#esac

	cat debian/changelog.tmp >> debian/changelog
	rm -f debian/changelog.tmp

	dch --distribution ${DISTRIBUTION} --force-distribution --release ""
#	--append "Unreleased snapshot from Git repository: ${HASH}"
else
	# we are building a release

	case "${DISTRIBUTION}" in
		sid-snapshots)
			dch --force-distribution --force-bad-version --newversion ${EPOCHE}${VERSION}${REVISION} --distribution ${DISTRIBUTION} "Rebuild release from Git repository: ${HASH}"
			;;
	esac
fi

# Getting orig.tar.gz
if [ -n "${TAR_VERSION}" ]
then
	# we have a non-native package
	pristine-tar checkout ${PACKAGE}_${UPSTREAM_VERSION}.orig.tar.gz
	mv ${PACKAGE}_${UPSTREAM_VERSION}.orig.tar.gz ../
fi

# Building package
cd "${TEMPDIR}"/
rm -rf ${PACKAGE}-${VERSION}${REVISION}/.git
dpkg-source -b ${PACKAGE}-${VERSION}${REVISION}

# Removing sources
rm -rf ${PACKAGE}-${VERSION}${REVISION}
mv * ${CURDIR}
rm -rf "${TEMPDIR}"