blob: 5f8fb232933266dbc939477807e315d60ea50077 [file] [log] [blame]
David Rochberg6ab40042010-05-24 16:21:52 -04001# Copyright 1999-2009 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/git.eclass,v 1.43 2010/02/24 01:16:35 abcd Exp $
4
5# @ECLASS: git.eclass
6# @MAINTAINER:
7# Tomas Chvatal <scarabeus@gentoo.org>
8# Donnie Berkholz <dberkholz@gentoo.org>
9# @BLURB: This eclass provides functions for fetch and unpack git repositories
10# @DESCRIPTION:
11# The eclass is based on subversion eclass.
12# If you use this eclass, the ${S} is ${WORKDIR}/${P}.
13# It is necessary to define the EGIT_REPO_URI variable at least.
14# @THANKS TO:
15# Fernando J. Pereda <ferdy@gentoo.org>
16
17inherit eutils
18
19EGIT="git.eclass"
20
21# We DEPEND on at least a bit recent git version
22DEPEND=">=dev-util/git-1.6"
23
24EXPORTED_FUNCTIONS="src_unpack"
25case "${EAPI:-0}" in
26 3|2) EXPORTED_FUNCTIONS="${EXPORTED_FUNCTIONS} src_prepare" ;;
27 1|0) ;;
28 :) DEPEND="EAPI-UNSUPPORTED" ;;
29esac
30EXPORT_FUNCTIONS ${EXPORTED_FUNCTIONS}
31
32# define some nice defaults but only if nothing is set already
33: ${HOMEPAGE:=http://git-scm.com/}
34
35# @ECLASS-VARIABLE: EGIT_QUIET
36# @DESCRIPTION:
37# Enables user specified verbosity for the eclass elog informations.
38# The user just needs to add EGIT_QUIET="ON" to the /etc/make.conf.
39: ${EGIT_QUIET:="OFF"}
40
41# @ECLASS-VARIABLE: EGIT_STORE_DIR
42# @DESCRIPTION:
43# Storage directory for git sources.
44# Can be redefined.
45[[ -z ${EGIT_STORE_DIR} ]] && EGIT_STORE_DIR="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/git-src"
46
47# @ECLASS-VARIABLE: EGIT_HAS_SUBMODULES
48# @DESCRIPTION:
49# Set this to "true" to enable the (slower) submodule support.
50# This variable should be set before inheriting git.eclass
51: ${EGIT_HAS_SUBMODULES:=false}
52
53# @ECLASS-VARIABLE: EGIT_FETCH_CMD
54# @DESCRIPTION:
55# Command for cloning the repository.
56: ${EGIT_FETCH_CMD:="git clone"}
57
58# @ECLASS-VARIABLE: EGIT_UPDATE_CMD
59# @DESCRIPTION:
60# Git fetch command.
61if ${EGIT_HAS_SUBMODULES}; then
62 EGIT_UPDATE_CMD="git pull -f -u"
63else
64 EGIT_UPDATE_CMD="git fetch -f -u"
65fi
66
67# @ECLASS-VARIABLE: EGIT_DIFFSTAT_CMD
68# @DESCRIPTION:
69# Git command for diffstat.
70EGIT_DIFFSTAT_CMD="git --no-pager diff --stat"
71
72# @ECLASS-VARIABLE: EGIT_OPTIONS
73# @DESCRIPTION:
74# This variable value is passed to clone and fetch.
75: ${EGIT_OPTIONS:=}
76
77# @ECLASS-VARIABLE: EGIT_MASTER
78# @DESCRIPTION:
79# Variable for specifying master branch.
80# Usefull when upstream don't have master branch.
81: ${EGIT_MASTER:=master}
82
83# @ECLASS-VARIABLE: EGIT_REPO_URI
84# @DESCRIPTION:
85# URI for the repository
86# e.g. http://foo, git://bar
87# Supported protocols:
88# http://
89# https://
90# git://
91# git+ssh://
92# rsync://
93# ssh://
94eval X="\$${PN//[-+]/_}_LIVE_REPO"
95if [[ ${X} = "" ]]; then
96 EGIT_REPO_URI=${EGIT_REPO_URI:=}
97else
98 EGIT_REPO_URI="${X}"
99fi
100# @ECLASS-VARIABLE: EGIT_PROJECT
101# @DESCRIPTION:
102# Project name of your ebuild.
103# Git eclass will check out the git repository like:
104# ${EGIT_STORE_DIR}/${EGIT_PROJECT}/${EGIT_REPO_URI##*/}
105# so if you define EGIT_REPO_URI as http://git.collab.net/repo/git or
106# http://git.collab.net/repo/git. and PN is subversion-git.
107# it will check out like:
108# ${EGIT_STORE_DIR}/subversion
109: ${EGIT_PROJECT:=${PN/-git}}
110
111# @ECLASS-VARIABLE: EGIT_BOOTSTRAP
112# @DESCRIPTION:
113# bootstrap script or command like autogen.sh or etc...
114: ${EGIT_BOOTSTRAP:=}
115
116# @ECLASS-VARIABLE: EGIT_OFFLINE
117# @DESCRIPTION:
118# Set this variable to a non-empty value to disable the automatic updating of
119# an GIT source tree. This is intended to be set outside the git source
120# tree by users.
121EGIT_OFFLINE="${EGIT_OFFLINE:-${ESCM_OFFLINE}}"
122
123# @ECLASS-VARIABLE: EGIT_PATCHES
124# @DESCRIPTION:
125# Similar to PATCHES array from base.eclass
126# Only difference is that this patches are applied before bootstrap.
127# Please take note that this variable should be bash array.
128
129# @ECLASS-VARIABLE: EGIT_BRANCH
130# @DESCRIPTION:
131# git eclass can fetch any branch in git_fetch().
132eval X="\$${PN//[-+]/_}_LIVE_BRANCH"
133if [[ ${X} = "" ]]; then
134 EGIT_BRANCH=${EGIT_BRANCH:=master}
135else
136 EGIT_BRANCH="${X}"
137fi
138
139# @ECLASS-VARIABLE: EGIT_COMMIT
140# @DESCRIPTION:
141# git eclass can checkout any commit.
142eval X="\$${PN//[-+]/_}_LIVE_COMMIT"
143if [[ ${X} = "" ]]; then
144 : ${EGIT_COMMIT:=${EGIT_BRANCH}}
145else
146 EGIT_COMMIT="${X}"
147fi
148
149# @ECLASS-VARIABLE: EGIT_REPACK
150# @DESCRIPTION:
151# git eclass will repack objects to save disk space. However this can take a
152# long time with VERY big repositories.
153: ${EGIT_REPACK:=false}
154
155# @ECLASS-VARIABLE: EGIT_PRUNE
156# @DESCRIPTION:
157# git eclass can prune the local clone. This is useful if upstream rewinds and
158# rebases branches too often.
159: ${EGIT_PRUNE:=false}
160
161# @FUNCTION: git_submodules
162# @DESCRIPTION:
163# Internal function wrapping the submodule initialisation and update
164git_submodules() {
165 if ${EGIT_HAS_SUBMODULES}; then
166 debug-print "git submodule init"
167 git submodule init
168 debug-print "git submodule update"
169 git submodule update
170 fi
171}
172
173# @FUNCTION: git_branch
174# @DESCRIPTION:
175# Internal function that changes branch for the repo based on EGIT_TREE and
176# EGIT_BRANCH variables.
177git_branch() {
178 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
179 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
180 branchname=tree-${EGIT_COMMIT}
181 src=${EGIT_COMMIT}
182 fi
183 debug-print "git checkout -b ${branchname} ${src}"
David James60c97bd2010-09-20 09:44:17 -0700184 git checkout -b ${branchname} ${src} || \
185 die "${EGIT}: Could not run git checkout -b ${branchname} ${src}"
David Rochberg6ab40042010-05-24 16:21:52 -0400186
187 unset branchname src
188}
189
190# @FUNCTION: git_fetch
191# @DESCRIPTION:
192# Gets repository from EGIT_REPO_URI and store it in specified EGIT_STORE_DIR
193git_fetch() {
194 debug-print-function ${FUNCNAME} "$@"
195
196 local GIT_DIR EGIT_CLONE_DIR oldsha1 cursha1 extra_clone_opts upstream_branch
197 ${EGIT_HAS_SUBMODULES} || export GIT_DIR
198
199 # choose if user wants elog or just einfo.
200 if [[ ${EGIT_QUIET} != OFF ]]; then
201 elogcmd="einfo"
202 else
203 elogcmd="elog"
204 fi
205
206 # If we have same branch and the tree we can do --depth 1 clone
207 # which outputs into really smaller data transfers.
208 # Sadly we can do shallow copy for now because quite a few packages need .git
209 # folder.
210 #[[ ${EGIT_COMMIT} = ${EGIT_BRANCH} ]] && \
211 # EGIT_FETCH_CMD="${EGIT_FETCH_CMD} --depth 1"
212 if [[ ! -z ${EGIT_TREE} ]] ; then
213 EGIT_COMMIT=${EGIT_TREE}
214 ewarn "QA: Usage of deprecated EGIT_TREE variable detected."
215 ewarn "QA: Use EGIT_COMMIT variable instead."
216 fi
217
218 # EGIT_REPO_URI is empty.
219 [[ -z ${EGIT_REPO_URI} ]] && die "${EGIT}: EGIT_REPO_URI is empty."
220
221 # check for the protocol or pull from a local repo.
222 if [[ -z ${EGIT_REPO_URI%%:*} ]] ; then
223 case ${EGIT_REPO_URI%%:*} in
224 git*|http|https|rsync|ssh) ;;
225 *) die "${EGIT}: protocol for fetch from "${EGIT_REPO_URI%:*}" is not yet implemented in eclass." ;;
226 esac
227 fi
228
229 # initial clone, we have to create master git storage directory and play
230 # nicely with sandbox
231 if [[ ! -d ${EGIT_STORE_DIR} ]] ; then
232 debug-print "${FUNCNAME}: initial clone. creating git directory"
233 addwrite /
Eric Shienbroode49e4032010-06-02 19:32:05 -0400234 # TODO(ers): Remove this workaround once we figure out how to make
235 # sure the directories are owned by the user instead of by root.
236 local old_umask="`umask`"
237 umask 002
David Rochberg6ab40042010-05-24 16:21:52 -0400238 mkdir -p "${EGIT_STORE_DIR}" \
239 || die "${EGIT}: can't mkdir ${EGIT_STORE_DIR}."
Eric Shienbroode49e4032010-06-02 19:32:05 -0400240 umask ${old_umask}
David Rochberg6ab40042010-05-24 16:21:52 -0400241 export SANDBOX_WRITE="${SANDBOX_WRITE%%:/}"
David Rochberg6ab40042010-05-24 16:21:52 -0400242 fi
243
244 cd -P "${EGIT_STORE_DIR}" || die "${EGIT}: can't chdir to ${EGIT_STORE_DIR}"
245 EGIT_STORE_DIR=${PWD}
246
247 # allow writing into EGIT_STORE_DIR
248 addwrite "${EGIT_STORE_DIR}"
249
250 [[ -z ${EGIT_REPO_URI##*/} ]] && EGIT_REPO_URI="${EGIT_REPO_URI%/}"
251 EGIT_CLONE_DIR="${EGIT_PROJECT}"
252
253 debug-print "${FUNCNAME}: EGIT_OPTIONS = \"${EGIT_OPTIONS}\""
254
255 GIT_DIR="${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}"
256 # we also have to remove all shallow copied repositories
257 # and fetch them again
258 if [[ -e "${GIT_DIR}/shallow" ]]; then
259 rm -rf "${GIT_DIR}"
260 einfo "The ${EGIT_CLONE_DIR} was shallow copy. Refetching."
261 fi
262 # repack from bare copy to normal one
263 if ${EGIT_HAS_SUBMODULES} && [[ -d ${GIT_DIR} && ! -d "${GIT_DIR}/.git/" ]]; then
264 rm -rf "${GIT_DIR}"
265 einfo "The ${EGIT_CLONE_DIR} was bare copy. Refetching."
266 fi
267 if ! ${EGIT_HAS_SUBMODULES} && [[ -d ${GIT_DIR} && -d ${GIT_DIR}/.git ]]; then
268 rm -rf "${GIT_DIR}"
269 einfo "The ${EGIT_CLONE_DIR} was not a bare copy. Refetching."
270 fi
271
272 if ${EGIT_HAS_SUBMODULES}; then
273 upstream_branch=origin/${EGIT_BRANCH}
274 else
275 upstream_branch=${EGIT_BRANCH}
276 extra_clone_opts=--bare
277 fi
278
279 if [[ ! -d ${GIT_DIR} ]] ; then
280 # first clone
281 ${elogcmd} "GIT NEW clone -->"
282 ${elogcmd} " repository: ${EGIT_REPO_URI}"
283
284 debug-print "${EGIT_FETCH_CMD} ${extra_clone_opts} ${EGIT_OPTIONS} \"${EGIT_REPO_URI}\" ${GIT_DIR}"
Eric Shienbroode49e4032010-06-02 19:32:05 -0400285 # TODO(ers): Remove this workaround once we figure out how to make
286 # sure the directories are owned by the user instead of by root.
Eric Shienbrood584af892010-06-02 13:31:37 -0400287 local old_umask="`umask`"
288 umask 002
David Rochberg6ab40042010-05-24 16:21:52 -0400289 ${EGIT_FETCH_CMD} ${extra_clone_opts} ${EGIT_OPTIONS} "${EGIT_REPO_URI}" ${GIT_DIR} \
290 || die "${EGIT}: can't fetch from ${EGIT_REPO_URI}."
291
Eric Shienbrood584af892010-06-02 13:31:37 -0400292 umask ${old_umask}
David Rochberg6ab40042010-05-24 16:21:52 -0400293 pushd "${GIT_DIR}" &> /dev/null
294 cursha1=$(git rev-parse ${upstream_branch})
295 ${elogcmd} " at the commit: ${cursha1}"
296
297 git_submodules
298 popd &> /dev/null
299 elif [[ -n ${EGIT_OFFLINE} ]] ; then
300 pushd "${GIT_DIR}" &> /dev/null
301 cursha1=$(git rev-parse ${upstream_branch})
302 ${elogcmd} "GIT offline update -->"
303 ${elogcmd} " repository: ${EGIT_REPO_URI}"
304 ${elogcmd} " at the commit: ${cursha1}"
305 popd &> /dev/null
306 else
307 pushd "${GIT_DIR}" &> /dev/null
308 # Git urls might change, so unconditionally set it here
309 git config remote.origin.url "${EGIT_REPO_URI}"
310
311 # fetch updates
312 ${elogcmd} "GIT update -->"
313 ${elogcmd} " repository: ${EGIT_REPO_URI}"
314
315 oldsha1=$(git rev-parse ${upstream_branch})
316
317 if ${EGIT_HAS_SUBMODULES}; then
318 debug-print "${EGIT_UPDATE_CMD} ${EGIT_OPTIONS}"
319 # fix branching
320 git checkout ${EGIT_MASTER}
321 for x in $(git branch |grep -v "* ${EGIT_MASTER}" |tr '\n' ' '); do
322 git branch -D ${x}
323 done
324 ${EGIT_UPDATE_CMD} ${EGIT_OPTIONS} \
325 || die "${EGIT}: can't update from ${EGIT_REPO_URI}."
326 else
327 debug-print "${EGIT_UPDATE_CMD} ${EGIT_OPTIONS} origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
328 ${EGIT_UPDATE_CMD} ${EGIT_OPTIONS} origin ${EGIT_BRANCH}:${EGIT_BRANCH} \
329 || die "${EGIT}: can't update from ${EGIT_REPO_URI}."
330 fi
331
332 git_submodules
333 cursha1=$(git rev-parse ${upstream_branch})
334
335 # write out message based on the revisions
336 if [[ ${oldsha1} != ${cursha1} ]]; then
337 ${elogcmd} " updating from commit: ${oldsha1}"
338 ${elogcmd} " to commit: ${cursha1}"
339 else
340 ${elogcmd} " at the commit: ${cursha1}"
341 # @ECLASS_VARIABLE: LIVE_FAIL_FETCH_IF_REPO_NOT_UPDATED
342 # @DESCRIPTION:
343 # If this variable is set to TRUE in make.conf or somewhere in
344 # enviroment the package will fail if there is no update, thus in
345 # combination with --keep-going it would lead in not-updating
346 # pakcages that are up-to-date.
347 # TODO: this can lead to issues if more projects/packages use same repo
348 [[ ${LIVE_FAIL_FETCH_IF_REPO_NOT_UPDATED} = true ]] && \
349 debug-print "${FUNCNAME}: Repository \"${EGIT_REPO_URI}\" is up-to-date. Skipping." && \
350 die "${EGIT}: Repository \"${EGIT_REPO_URI}\" is up-to-date. Skipping."
351 fi
352 ${EGIT_DIFFSTAT_CMD} ${oldsha1}..${upstream_branch}
353 popd &> /dev/null
354 fi
355
356 pushd "${GIT_DIR}" &> /dev/null
357 if ${EGIT_REPACK} || ${EGIT_PRUNE} ; then
358 ebegin "Garbage collecting the repository"
359 git gc $(${EGIT_PRUNE} && echo '--prune')
360 eend $?
361 fi
362 popd &> /dev/null
363
364 # export the git version
365 export EGIT_VERSION="${cursha1}"
366
367 # log the repo state
368 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] && elog " commit: ${EGIT_COMMIT}"
369 ${elogcmd} " branch: ${EGIT_BRANCH}"
370 ${elogcmd} " storage directory: \"${GIT_DIR}\""
371
372 if ${EGIT_HAS_SUBMODULES}; then
373 pushd "${GIT_DIR}" &> /dev/null
374 debug-print "rsync -rlpgo . \"${S}\""
375 time rsync -rlpgo . "${S}"
376 popd &> /dev/null
377 else
378 unset GIT_DIR
379 debug-print "git clone -l -s -n \"${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}\" \"${S}\""
380 git clone -l -s -n "${EGIT_STORE_DIR}/${EGIT_CLONE_DIR}" "${S}"
381 fi
382
383 pushd "${S}" &> /dev/null
384 git_branch
385 # submodules always reqire net (thanks to branches changing)
386 [[ -n ${EGIT_OFFLINE} ]] || git_submodules
387 popd &> /dev/null
388
389 echo ">>> Unpacked to ${S}"
390}
391
392# @FUNCTION: git_bootstrap
393# @DESCRIPTION:
394# Runs bootstrap command if EGIT_BOOTSTRAP variable contains some value
395# Remember that what ever gets to the EGIT_BOOTSTRAP variable gets evaled by bash.
396git_bootstrap() {
397 debug-print-function ${FUNCNAME} "$@"
398
399 if [[ -n ${EGIT_BOOTSTRAP} ]] ; then
400 pushd "${S}" > /dev/null
401 einfo "Starting bootstrap"
402
403 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
404 # we have file in the repo which we should execute
405 debug-print "$FUNCNAME: bootstraping with file \"${EGIT_BOOTSTRAP}\""
406
407 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
408 eval "./${EGIT_BOOTSTRAP}" \
409 || die "${EGIT}: bootstrap script failed"
410 else
411 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
412 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
413 die "${EGIT}: \"${EGIT_BOOTSTRAP}\" is not executable."
414 fi
415 else
416 # we execute some system command
417 debug-print "$FUNCNAME: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
418
419 eval "${EGIT_BOOTSTRAP}" \
420 || die "${EGIT}: bootstrap commands failed."
421
422 fi
423
424 einfo "Bootstrap finished"
425 popd > /dev/null
426 fi
427}
428
429# @FUNCTION: git_apply_patches
430# @DESCRIPTION:
431# Apply patches from EGIT_PATCHES bash array.
432# Preffered is using the variable as bash array but for now it allows to write
433# it also as normal space separated string list. (This part of code should be
434# removed when all ebuilds get converted on bash array).
435git_apply_patches() {
436 debug-print-function ${FUNCNAME} "$@"
437
438 pushd "${S}" > /dev/null
439 if [[ ${#EGIT_PATCHES[@]} -gt 1 ]] ; then
440 for i in "${EGIT_PATCHES[@]}"; do
441 debug-print "$FUNCNAME: git_autopatch: patching from ${i}"
442 epatch "${i}"
443 done
444 elif [[ ${EGIT_PATCHES} != "" ]]; then
445 # no need for loop if space separated string is passed.
446 debug-print "$FUNCNAME: git_autopatch: patching from ${EGIT_PATCHES}"
447 epatch "${EGIT_PATCHES}"
448 fi
449
450 popd > /dev/null
451}
452
453# @FUNCTION: git_src_unpack
454# @DESCRIPTION:
455# src_upack function, calls src_prepare one if EAPI!=2.
456git_src_unpack() {
457 debug-print-function ${FUNCNAME} "$@"
458
459 git_fetch || die "${EGIT}: unknown problem in git_fetch()."
460
461 has src_prepare ${EXPORTED_FUNCTIONS} || git_src_prepare
462}
463
464# @FUNCTION: git_src_prepare
465# @DESCRIPTION:
466# src_prepare function for git stuff. Patches, bootstrap...
467git_src_prepare() {
468 debug-print-function ${FUNCNAME} "$@"
469
470 git_apply_patches
471 git_bootstrap
472}