summaryrefslogtreecommitdiff
blob: c4e65a4d6aefcefd5f629a4dc0dff05904428cee (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
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
# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/eclass/git.eclass,v 1.28 2009/08/24 21:48:58 scarabeus Exp $

# @ECLASS: git.eclass
# @MAINTAINER:
# Tomas Chvatal <scarabeus@gentoo.org>
# Donnie Berkholz <dberkholz@gentoo.org>
# @BLURB: This eclass provides functions for fetch and unpack git repositories
# @DESCRIPTION:
# The eclass is based on subversion eclass.
# If you use this eclass, the ${S} is ${WORKDIR}/${P}.
# It is necessary to define the EGIT_REPO_URI variable at least.
# @THANKS TO:
# Fernando J. Pereda <ferdy@gentoo.org>

inherit eutils

EGIT="git.eclass"

EXPORTED_FUNCTIONS="src_unpack"
case "${EAPI:-0}" in
	2) EXPORTED_FUNCTIONS="${EXPORTED_FUNCTIONS} src_prepare" ;;
	0|1) ;;
	*) die "Unknown EAPI, Bug eclass maintainers." ;;
esac
EXPORT_FUNCTIONS ${EXPORTED_FUNCTIONS}

# define some nice defaults but only if nothing is set already
: ${HOMEPAGE:=http://git-scm.com/}

# We DEPEND on at least a bit recent git version
DEPEND=">=dev-util/git-1.6"

# @ECLASS-VARIABLE: EGIT_QUIET
# @DESCRIPTION:
# Enables user specified verbosity for the eclass elog informations.
# The user just needs to add EGIT_QUIET="ON" to the /etc/make.conf.
: ${EGIT_QUIET:="OFF"}

# @ECLASS-VARIABLE: EGIT_STORE_DIR
# @DESCRIPTION:
# Storage directory for git sources.
# Can be redefined.
[[ -z ${EGIT_STORE_DIR} ]] && EGIT_STORE_DIR="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/git-src"

# @ECLASS-VARIABLE: EGIT_FETCH_CMD
# @DESCRIPTION:
# Command for cloning the repository.
: ${EGIT_FETCH_CMD:="git clone --bare"}

# @ECLASS-VARIABLE: EGIT_UPDATE_CMD
# @DESCRIPTION:
# Git fetch command.
EGIT_UPDATE_CMD="git fetch -f -u"

# @ECLASS-VARIABLE: EGIT_DIFFSTAT_CMD
# @DESCRIPTION:
# Git command for diffstat.
EGIT_DIFFSTAT_CMD="git --no-pager diff --stat"

# @ECLASS-VARIABLE: EGIT_OPTIONS
# @DESCRIPTION:
# This variable value is passed to clone and fetch.
: ${EGIT_OPTIONS:=}

# @ECLASS-VARIABLE: EGIT_REPO_URI
# @DESCRIPTION:
# URI for the repository
# e.g. http://foo, git://bar
# Supported protocols:
#   http://
#   https://
#   git://
#   git+ssh://
#   rsync://
#   ssh://
eval X="\$${PN//-/_}_LIVE_REPO"
if [[ ${X} = "" ]]; then
	EGIT_REPO_URI=${EGIT_REPO_URI:=}
else
	EGIT_REPO_URI="${X}"
fi
# @ECLASS-VARIABLE: EGIT_PROJECT
# @DESCRIPTION:
# Project name of your ebuild.
# Git eclass will check out the git repository like:
#	${EGIT_STORE_DIR}/${EGIT_PROJECT}/${EGIT_REPO_URI##*/}
# so if you define EGIT_REPO_URI as http://git.collab.net/repo/git or
# http://git.collab.net/repo/git. and PN is subversion-git.
# it will check out like:
#	${EGIT_STORE_DIR}/subversion
: ${EGIT_PROJECT:=${PN/-git}}

# @ECLASS-VARIABLE: EGIT_BOOSTRAP
# @DESCRIPTION:
# bootstrap script or command like autogen.sh or etc...
: ${EGIT_BOOTSTRAP:=}

# @ECLASS-VARIABLE: EGIT_OFFLINE
# @DESCRIPTION:
# Set this variable to a non-empty value to disable the automatic updating of
# an GIT source tree. This is intended to be set outside the git source
# tree by users.
EGIT_OFFLINE="${EGIT_OFFLINE:-${ESCM_OFFLINE}}"

# @ECLASS-VARIABLE: EGIT_PATCHES
# @DESCRIPTION:
# Similar to PATCHES array from base.eclass
# Only difference is that this patches are applied before bootstrap.
# Please take note that this variable should be bash array.

# @ECLASS-VARIABLE: EGIT_BRANCH
# @DESCRIPTION:
# git eclass can fetch any branch in git_fetch().
eval X="\$${PN//-/_}_LIVE_BRANCH"
if [[ ${X} = "" ]]; then
	EGIT_BRANCH=${EGIT_BRANCH:=master}
else
	EGIT_BRANCH="${X}"
fi

# @ECLASS-VARIABLE: EGIT_TREE
# @DESCRIPTION:
# git eclass can checkout any tree (commit).
eval X="\$${PN//-/_}_LIVE_TREE"
if [[ ${X} = "" ]]; then
	: ${EGIT_TREE:=${EGIT_BRANCH}}
else
	EGIT_TREE="${X}"
fi

# @ECLASS-VARIABLE: EGIT_REPACK
# @DESCRIPTION:
# git eclass will repack objects to save disk space. However this can take a
# long time with VERY big repositories.
: ${EGIT_REPACK:=false}

# @ECLASS-VARIABLE: EGIT_PRUNE
# @DESCRIPTION:
# git.eclass can prune the local clone. This is useful if upstream rewinds and
# rebases branches too often.
: ${EGIT_PRUNE:=false}

# @FUNCTION: git_fetch
# @DESCRIPTION:
# Gets repository from EGIT_REPO_URI and store it in specified EGIT_STORE_DIR
git_fetch() {
	debug-print-function ${FUNCNAME} "$@"

	local EGIT_CLONE_DIR oldsha1 cursha1

	# choose if user wants elog or just einfo.
	if [[ ${EGIT_QUIET} != OFF ]]; then
		elogcmd="einfo"
	else
		elogcmd="elog"
	fi

	# If we have same branch and the tree we can do --depth 1 clone
	# which outputs into really smaller data transfers.
	# Sadly we can do shallow copy for now because quite a few packages need .git
	# folder.
	#[[ ${EGIT_TREE} = ${EGIT_BRANCH} ]] && \
	#	EGIT_FETCH_CMD="${EGIT_FETCH_CMD} --depth 1"

	# EGIT_REPO_URI is empty.
	[[ -z ${EGIT_REPO_URI} ]] && die "${EGIT}: EGIT_REPO_URI is empty."

	# check for the protocol or pull from a local repo.
	if [[ -z ${EGIT_REPO_URI%%:*} ]] ; then
		case ${EGIT_REPO_URI%%:*} in
			git*|http|https|rsync|ssh) ;;
			*) die "${EGIT}: protocol for fetch from "${EGIT_REPO_URI%:*}" is not yet implemented in eclass." ;;
		esac
	fi

	# initial clone, we have to create master git storage directory and play
	# nicely with sandbox
	if [[ ! -d ${EGIT_STORE_DIR} ]] ; then
		debug-print "${FUNCNAME}: initial clone. creating git directory"
		addwrite /
		mkdir -p "${EGIT_STORE_DIR}" \
			|| die "${EGIT}: can't mkdir ${EGIT_STORE_DIR}."
		export SANDBOX_WRITE="${SANDBOX_WRITE%%:/}"
	fi

	cd -P "${EGIT_STORE_DIR}" || die "${EGIT}: can't chdir to ${EGIT_STORE_DIR}"
	EGIT_STORE_DIR=${PWD}

	# allow writing into EGIT_STORE_DIR
	addwrite "${EGIT_STORE_DIR}"

	[[ -z ${EGIT_REPO_URI##*/} ]] && EGIT_REPO_URI="${EGIT_REPO_URI%/}"
	EGIT_CLONE_DIR="${EGIT_PROJECT}"

	debug-print "${FUNCNAME}: EGIT_OPTIONS = \"${EGIT_OPTIONS}\""

	export GIT_DIR="${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}"

	# we also have to remove all shallow copied repositories
	# and fetch them again
	if [[ -e "${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}/shallow" ]]; then
		rm -rf "${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}"
		einfo "The ${EGIT_CLONE_DIR} was shallow copy. Refetching."
	fi

	if [[ ! -d ${EGIT_CLONE_DIR} ]] ; then
		# first clone
		${elogcmd} "GIT NEW clone -->"
		${elogcmd} "   repository: 		${EGIT_REPO_URI}"

		${EGIT_FETCH_CMD} ${EGIT_OPTIONS} "${EGIT_REPO_URI}" ${EGIT_PROJECT} \
			|| die "${EGIT}: can't fetch from ${EGIT_REPO_URI}."

		cursha1=$(git rev-parse ${EGIT_BRANCH})
		${elogcmd} "   at the commit:		${cursha1}"
		# We use --bare cloning, so git doesn't do this for us.
		git config remote.origin.url "${EGIT_REPO_URI}"
	elif [[ -n ${EGIT_OFFLINE} ]] ; then
		cursha1=$(git rev-parse ${EGIT_BRANCH})
		${elogcmd} "GIT offline update -->"
		${elogcmd} "   repository: 		${EGIT_REPO_URI}"
		${elogcmd} "   at the commit:		${cursha1}"
	else
		# Git urls might change, so unconditionally set it here
		git config remote.origin.url "${EGIT_REPO_URI}"

		# fetch updates
		${elogcmd} "GIT update -->"
		${elogcmd} "   repository: 		${EGIT_REPO_URI}"

		oldsha1=$(git rev-parse ${EGIT_BRANCH})

		${elogcmd} ${EGIT_UPDATE_CMD} ${EGIT_OPTIONS} origin ${EGIT_BRANCH}:${EGIT_BRANCH}
		${EGIT_UPDATE_CMD} ${EGIT_OPTIONS} origin ${EGIT_BRANCH}:${EGIT_BRANCH} \
			|| die "${EGIT}: can't update from ${EGIT_REPO_URI}."

		cursha1=$(git rev-parse ${EGIT_BRANCH})

		# write out message based on the revisions
		if [[ ${oldsha1} != ${cursha1} ]]; then
			${elogcmd} "   updating from commit:	${oldsha1}"
			${elogcmd} "   to commit:		${cursha1}"
		else
			${elogcmd} "   at the commit: 		${cursha1}"
			# @ECLASS_VARIABLE: LIVE_FAIL_FETCH_IF_REPO_NOT_UPDATED
			# @DESCRIPTION:
			# If this variable is set to TRUE in make.conf or somewhere in
			# enviroment the package will fail if there is no update, thus in
			# combination with --keep-going it would lead in not-updating
			# pakcages that are up-to-date.
			# TODO: this can lead to issues if more projects/packages use same repo
			[[ ${LIVE_FAIL_FETCH_IF_REPO_NOT_UPDATED} = true ]] && \
				debug-print "${FUNCNAME}: Repository \"${EGIT_REPO_URI}\" is up-to-date. Skipping." && \
				die "${EGIT}: Repository \"${EGIT_REPO_URI}\" is up-to-date. Skipping."
		fi
		${EGIT_DIFFSTAT_CMD} ${oldsha1}..${EGIT_BRANCH}
	fi

	if ${EGIT_REPACK} || ${EGIT_PRUNE} ; then
		ebegin "Garbage collecting the repository"
		git gc $(${EGIT_PRUNE} && echo '--prune')
		eend $?
	fi
	
	# export the git version
	export EGIT_VERSION="${cursha1}"

	[[ ${EGIT_TREE} != ${EGIT_BRANCH} ]] && elog "   tree:			${EGIT_TREE}"
	${elogcmd} "   branch: 			${EGIT_BRANCH}"
	${elogcmd} "   storage directory: 	\"${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}\""

	# unpack to the ${S}
	unset GIT_DIR
	debug-print "git clone -l -s -n \"${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}\" \"${S}\""
	git clone -l -s -n "${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}" "${S}"
	
	# set correct branch and the tree ebuild specified
	pushd "${S}" > /dev/null
	local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
	if [[ ${EGIT_TREE} != ${EGIT_BRANCH} ]]; then
		branchname=tree-${EGIT_TREE}
		src=${EGIT_TREE}
	fi
	debug-print "git checkout -b ${branchname} ${src}"
	git checkout -b ${branchname} ${src} 2>&1 > /dev/null
	debug-print "git submodule init"
	git submodule init 2>&1 > /dev/null
	debug-print "git submodule update"
	git submodule update 2>&1 > /dev/null
	popd > /dev/null

	unset branchname src

	echo ">>> Unpacked to ${S}"
}

# @FUNCTION: git_bootstrap
# @DESCRIPTION:
# Runs bootstrap command if EGIT_BOOTSTRAP variable contains some value
# Remember that what ever gets to the EGIT_BOOTSTRAP variable gets evaled by bash.
git_bootstrap() {
	debug-print-function ${FUNCNAME} "$@"

	if [[ -n ${EGIT_BOOTSTRAP} ]] ; then
		pushd "${S}" > /dev/null
		einfo "Starting bootstrap"

		if [[ -f ${EGIT_BOOTSTRAP} ]]; then
			# we have file in the repo which we should execute
			debug-print "$FUNCNAME: bootstraping with file \"${EGIT_BOOTSTRAP}\""

			if [[ -x ${EGIT_BOOTSTRAP} ]]; then
				eval "./${EGIT_BOOTSTRAP}" \
					|| die "${EGIT}: bootstrap script failed"
			else
				eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
				eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
				die "${EGIT}: \"${EGIT_BOOTSTRAP}\" is not executable."
			fi
		else
			# we execute some system command
			debug-print "$FUNCNAME: bootstraping with commands \"${EGIT_BOOTSTRAP}\""

			eval "${EGIT_BOOTSTRAP}" \
				|| die "${EGIT}: bootstrap commands failed."

		fi

		einfo "Bootstrap finished"
		popd > /dev/null
	fi
}

# @FUNCTION: git_apply_patches
# @DESCRIPTION:
# Apply patches from EGIT_PATCHES bash array.
# Preffered is using the variable as bash array but for now it allows to write
# it also as normal space separated string list. (This part of code should be
# removed when all ebuilds get converted on bash array).
git_apply_patches() {
	debug-print-function ${FUNCNAME} "$@"

	pushd "${S}" > /dev/null
	if [[ ${#EGIT_PATCHES[@]} -gt 1 ]] ; then
		for i in "${EGIT_PATCHES[@]}"; do
			debug-print "$FUNCNAME: git_autopatch: patching from ${i}"
			epatch "${i}"
		done
	elif [[ ${EGIT_PATCHES} != "" ]]; then
		# no need for loop if space separated string is passed.
		debug-print "$FUNCNAME: git_autopatch: patching from ${EGIT_PATCHES}"
		epatch "${EGIT_PATCHES}"
	fi

	popd > /dev/null
}

# @FUNCTION: git_src_unpack
# @DESCRIPTION:
# src_upack function, calls src_prepare one if EAPI!=2.
git_src_unpack() {
	debug-print-function ${FUNCNAME} "$@"

	git_fetch || die "${EGIT}: unknown problem in git_fetch()."

	has src_prepare ${EXPORTED_FUNCTIONS} || git_src_prepare
}

# @FUNCTION: git_src_prepare
# @DESCRIPTION:
# src_prepare function for git stuff. Patches, bootstrap...
git_src_prepare() {
	debug-print-function ${FUNCNAME} "$@"
	
	git_apply_patches
	git_bootstrap
}