blob: 0caa3d41d68c084b9189e37abbaacaad2646eefd [file] [log] [blame]
Raymes Khoury98e7edd2011-08-24 10:43:27 -07001# Copyright 1999-2011 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/git-2.eclass,v 1.14 2011/08/22 04:46:31 vapier Exp $
4
5# @ECLASS: git-2.eclass
6# @MAINTAINER:
7# Donnie Berkholz <dberkholz@gentoo.org>
8# @BLURB: Eclass for fetching and unpacking git repositories.
9# @DESCRIPTION:
10# Eclass for easing maitenance of live ebuilds using git as remote repository.
11# Eclass support working with git submodules and branching.
12
13# This eclass support all EAPIs
14EXPORT_FUNCTIONS src_unpack
15
Mike Frysinger7880f272011-12-19 14:21:19 -050016DEPEND="dev-vcs/git"
Raymes Khoury98e7edd2011-08-24 10:43:27 -070017
18# @ECLASS-VARIABLE: EGIT_SOURCEDIR
19# @DESCRIPTION:
20# This variable specifies destination where the cloned
21# data are copied to.
22#
23# EGIT_SOURCEDIR="${S}"
24
25# @ECLASS-VARIABLE: EGIT_STORE_DIR
26# @DESCRIPTION:
27# Storage directory for git sources.
28#
29# EGIT_STORE_DIR="${DISTDIR}/egit-src"
30
31# @ECLASS-VARIABLE: EGIT_HAS_SUBMODULES
32# @DEFAULT_UNSET
33# @DESCRIPTION:
34# If non-empty this variable enables support for git submodules in our
35# checkout. Also this makes the checkout to be non-bare for now.
36
37# @ECLASS-VARIABLE: EGIT_OPTIONS
38# @DEFAULT_UNSET
39# @DESCRIPTION:
40# Variable specifying additional options for fetch command.
41
42# @ECLASS-VARIABLE: EGIT_MASTER
43# @DESCRIPTION:
44# Variable for specifying master branch.
45# Usefull when upstream don't have master branch or name it differently.
46#
47# EGIT_MASTER="master"
48
49# @ECLASS-VARIABLE: EGIT_PROJECT
50# @DESCRIPTION:
51# Variable specifying name for the folder where we check out the git
52# repository. Value of this variable should be unique in the
53# EGIT_STORE_DIR as otherwise you would override another repository.
54#
55# EGIT_PROJECT="${EGIT_REPO_URI##*/}"
56
57# @ECLASS-VARIABLE: EGIT_DIR
58# @DESCRIPTION:
59# Directory where we want to store the git data.
60# This variable should not be overriden.
61#
62# EGIT_DIR="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
63
64# @ECLASS-VARIABLE: EGIT_REPO_URI
65# @REQUIRED
66# @DEFAULT_UNSET
67# @DESCRIPTION:
68# URI for the repository
69# e.g. http://foo, git://bar
70#
71# Support multiple values:
72# EGIT_REPO_URI="git://a/b.git http://c/d.git"
73
74# @ECLASS-VARIABLE: EVCS_OFFLINE
75# @DEFAULT_UNSET
76# @DESCRIPTION:
77# If non-empty this variable prevents performance of any online
78# operations.
79
80# @ECLASS-VARIABLE: EGIT_BRANCH
81# @DESCRIPTION:
82# Variable containing branch name we want to check out.
83# It can be overriden via env using packagename_LIVE_BRANCH
84# variable.
85#
86# EGIT_BRANCH="${EGIT_MASTER}"
87
88# @ECLASS-VARIABLE: EGIT_COMMIT
89# @DESCRIPTION:
90# Variable containing commit hash/tag we want to check out.
91# It can be overriden via env using packagename_LIVE_COMMIT
92# variable.
93#
94# EGIT_COMMIT="${EGIT_BRANCH}"
95
96# @ECLASS-VARIABLE: EGIT_REPACK
97# @DEFAULT_UNSET
98# @DESCRIPTION:
99# If non-empty this variable specifies that repository will be repacked to
100# save space. However this can take a REALLY LONG time with VERY big
101# repositories.
102
103# @ECLASS-VARIABLE: EGIT_PRUNE
104# @DEFAULT_UNSET
105# @DESCRIPTION:
106# If non-empty this variable enables pruning all loose objects on each fetch.
107# This is useful if upstream rewinds and rebases branches often.
108
109# @ECLASS-VARIABLE: EGIT_NONBARE
110# @DEFAULT_UNSET
111# @DESCRIPTION:
112# If non-empty this variable specifies that all checkouts will be done using
113# non bare repositories. This is useful if you can't operate with bare
114# checkouts for some reason.
115
116# @ECLASS-VARIABLE: EGIT_NOUNPACK
117# @DEFAULT_UNSET
118# @DESCRIPTION:
119# If non-empty this variable bans unpacking of ${A} content into the srcdir.
120# Default behaviour is to unpack ${A} content.
121
122# @FUNCTION: git-2_init_variables
123# @DESCRIPTION:
124# Internal function initializing all git variables.
125# We define it in function scope so user can define
126# all the variables before and after inherit.
127git-2_init_variables() {
128 debug-print-function ${FUNCNAME} "$@"
129
130 local x
131
132 : ${EGIT_SOURCEDIR="${S}"}
133
134 : ${EGIT_STORE_DIR:="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/egit-src"}
135
136 : ${EGIT_HAS_SUBMODULES:=}
137
138 : ${EGIT_OPTIONS:=}
139
140 : ${EGIT_MASTER:=master}
141
142 eval x="\$${PN//[-+]/_}_LIVE_REPO"
143 EGIT_REPO_URI=${x:-${EGIT_REPO_URI}}
144 [[ -z ${EGIT_REPO_URI} ]] && die "EGIT_REPO_URI must have some value"
145
146 : ${EVCS_OFFLINE:=}
147
148 eval x="\$${PN//[-+]/_}_LIVE_BRANCH"
149 [[ -n ${x} ]] && ewarn "QA: using \"${PN//[-+]/_}_LIVE_BRANCH\" variable, you won't get any support"
150 EGIT_BRANCH=${x:-${EGIT_BRANCH:-${EGIT_MASTER}}}
151
152 eval x="\$${PN//[-+]/_}_LIVE_COMMIT"
153 [[ -n ${x} ]] && ewarn "QA: using \"${PN//[-+]/_}_LIVE_COMMIT\" variable, you won't get any support"
154 EGIT_COMMIT=${x:-${EGIT_COMMIT:-${EGIT_BRANCH}}}
155
156 : ${EGIT_REPACK:=}
157
158 : ${EGIT_PRUNE:=}
159}
160
161# @FUNCTION: git-2_submodules
162# @DESCRIPTION:
163# Internal function wrapping the submodule initialisation and update.
164git-2_submodules() {
165 debug-print-function ${FUNCNAME} "$@"
166 if [[ -n ${EGIT_HAS_SUBMODULES} ]]; then
167 if [[ -n ${EVCS_OFFLINE} ]]; then
168 # for submodules operations we need to be online
169 debug-print "${FUNCNAME}: not updating submodules in offline mode"
170 return 1
171 fi
172
173 debug-print "${FUNCNAME}: working in \"${1}\""
174 pushd "${EGIT_DIR}" > /dev/null
175
176 debug-print "${FUNCNAME}: git submodule init"
177 git submodule init || die
178 debug-print "${FUNCNAME}: git submodule sync"
179 git submodule sync || die
180 debug-print "${FUNCNAME}: git submodule update"
181 git submodule update || die
182
183 popd > /dev/null
184 fi
185}
186
187# @FUNCTION: git-2_branch
188# @DESCRIPTION:
189# Internal function that changes branch for the repo based on EGIT_COMMIT and
190# EGIT_BRANCH variables.
191git-2_branch() {
192 debug-print-function ${FUNCNAME} "$@"
193
194 local branchname src
195
196 debug-print "${FUNCNAME}: working in \"${EGIT_SOURCEDIR}\""
197 pushd "${EGIT_SOURCEDIR}" > /dev/null
198
199 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
200 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
201 branchname=tree-${EGIT_COMMIT}
202 src=${EGIT_COMMIT}
203 fi
204 debug-print "${FUNCNAME}: git checkout -b ${branchname} ${src}"
205 git checkout -b ${branchname} ${src} \
206 || die "${FUNCNAME}: changing the branch failed"
207
208 popd > /dev/null
209}
210
211# @FUNCTION: git-2_gc
212# @DESCRIPTION:
213# Internal function running garbage collector on checked out tree.
214git-2_gc() {
215 debug-print-function ${FUNCNAME} "$@"
216
217 local args
218
219 pushd "${EGIT_DIR}" > /dev/null
220 if [[ -n ${EGIT_REPACK} || -n ${EGIT_PRUNE} ]]; then
221 ebegin "Garbage collecting the repository"
222 [[ -n ${EGIT_PRUNE} ]] && args='--prune'
223 debug-print "${FUNCNAME}: git gc ${args}"
224 git gc ${args}
225 eend $?
226 fi
227 popd > /dev/null
228}
229
230# @FUNCTION: git-2_prepare_storedir
231# @DESCRIPTION:
232# Internal function preparing directory where we are going to store SCM
233# repository.
234git-2_prepare_storedir() {
235 debug-print-function ${FUNCNAME} "$@"
236
237 local clone_dir
238
239 # initial clone, we have to create master git storage directory and play
240 # nicely with sandbox
241 if [[ ! -d ${EGIT_STORE_DIR} ]]; then
242 debug-print "${FUNCNAME}: Creating git main storage directory"
243 addwrite /
244 mkdir -p "${EGIT_STORE_DIR}" \
245 || die "${FUNCNAME}: can't mkdir \"${EGIT_STORE_DIR}\""
246 fi
247
248 # allow writing into EGIT_STORE_DIR
249 addwrite "${EGIT_STORE_DIR}"
250 # calculate the proper store dir for data
251 # If user didn't specify the EGIT_DIR, we check if he did specify
252 # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
253 [[ -z ${EGIT_REPO_URI##*/} ]] && EGIT_REPO_URI="${EGIT_REPO_URI%/}"
254 if [[ -z ${EGIT_DIR} ]]; then
255 if [[ -n ${EGIT_PROJECT} ]]; then
256 clone_dir=${EGIT_PROJECT}
257 else
258 clone_dir=${EGIT_REPO_URI##*/}
259 fi
260 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
261 fi
262 export EGIT_DIR=${EGIT_DIR}
263 debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
264}
265
266# @FUNCTION: git-2_move_source
267# @DESCRIPTION:
268# Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
269git-2_move_source() {
270 debug-print-function ${FUNCNAME} "$@"
271
272 debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
273 pushd "${EGIT_DIR}" > /dev/null
274 mkdir -p "${EGIT_SOURCEDIR}" \
275 || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
276 ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
277 || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
278 popd > /dev/null
279}
280
281# @FUNCTION: git-2_initial_clone
282# @DESCRIPTION:
283# Internal function running initial clone on specified repo_uri.
284git-2_initial_clone() {
285 debug-print-function ${FUNCNAME} "$@"
286
287 local repo_uri
288
289 EGIT_REPO_URI_SELECTED=""
290 for repo_uri in ${EGIT_REPO_URI}; do
291 debug-print "${FUNCNAME}: git clone ${EGIT_LOCAL_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
292 git clone ${EGIT_LOCAL_OPTIONS} "${repo_uri}" "${EGIT_DIR}"
293 if [[ $? -eq 0 ]]; then
294 # global variable containing the repo_name we will be using
295 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
296 EGIT_REPO_URI_SELECTED="${repo_uri}"
297 break
298 fi
299 done
300
301 if [[ -z ${EGIT_REPO_URI_SELECTED} ]]; then
302 die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
303 fi
304}
305
306# @FUNCTION: git-2_update_repo
307# @DESCRIPTION:
308# Internal function running update command on specified repo_uri.
309git-2_update_repo() {
310 debug-print-function ${FUNCNAME} "$@"
311
312 local repo_uri
313
314 if [[ -n ${EGIT_LOCAL_NONBARE} ]]; then
315 # checkout master branch and drop all other local branches
316 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
317 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
318 debug-print "${FUNCNAME}: git branch -D ${x}"
319 git branch -D ${x} > /dev/null
320 done
321 fi
322
323 EGIT_REPO_URI_SELECTED=""
324 for repo_uri in ${EGIT_REPO_URI}; do
325 # git urls might change, so reset it
326 git config remote.origin.url "${repo_uri}"
327
328 debug-print "${EGIT_UPDATE_CMD}"
329 ${EGIT_UPDATE_CMD} > /dev/null
330 if [[ $? -eq 0 ]]; then
331 # global variable containing the repo_name we will be using
332 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
333 EGIT_REPO_URI_SELECTED="${repo_uri}"
334 break
335 fi
336 done
337
338 if [[ -z ${EGIT_REPO_URI_SELECTED} ]]; then
339 die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
340 fi
341}
342
343# @FUNCTION: git-2_fetch
344# @DESCRIPTION:
345# Internal function fetching repository from EGIT_REPO_URI and storing it in
346# specified EGIT_STORE_DIR.
347git-2_fetch() {
348 debug-print-function ${FUNCNAME} "$@"
349
350 local oldsha cursha repo_type
351
352 [[ -n ${EGIT_LOCAL_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
353
354 if [[ ! -d ${EGIT_DIR} ]]; then
355 git-2_initial_clone
356 pushd "${EGIT_DIR}" > /dev/null
357 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
358 echo "GIT NEW clone -->"
359 echo " repository: ${EGIT_REPO_URI_SELECTED}"
360 echo " at the commit: ${cursha}"
361
362 popd > /dev/null
363 elif [[ -n ${EVCS_OFFLINE} ]]; then
364 pushd "${EGIT_DIR}" > /dev/null
365 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
366 echo "GIT offline update -->"
367 echo " repository: $(git config remote.origin.url)"
368 echo " at the commit: ${cursha}"
369 popd > /dev/null
370 else
371 pushd "${EGIT_DIR}" > /dev/null
372 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
373 git-2_update_repo
374 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
375
376 # fetch updates
377 echo "GIT update -->"
378 echo " repository: ${EGIT_REPO_URI_SELECTED}"
379 # write out message based on the revisions
380 if [[ "${oldsha}" != "${cursha}" ]]; then
381 echo " updating from commit: ${oldsha}"
382 echo " to commit: ${cursha}"
383 else
384 echo " at the commit: ${cursha}"
385 fi
386
387 # print nice statistic of what was changed
388 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
389 popd > /dev/null
390 fi
391 # export the version the repository is at
392 export EGIT_VERSION="${cursha}"
393 # log the repo state
394 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
395 && echo " commit: ${EGIT_COMMIT}"
396 echo " branch: ${EGIT_BRANCH}"
397 echo " storage directory: \"${EGIT_DIR}\""
398 echo " checkout type: ${repo_type}"
399}
400
401# @FUNCTION: git_bootstrap
402# @DESCRIPTION:
403# Internal function that runs bootstrap command on unpacked source.
404git-2_bootstrap() {
405 debug-print-function ${FUNCNAME} "$@"
406
407 # @ECLASS-VARIABLE: EGIT_BOOTSTRAP
408 # @DESCRIPTION:
409 # Command to be executed after checkout and clone of the specified
410 # repository.
411 # enviroment the package will fail if there is no update, thus in
412 # combination with --keep-going it would lead in not-updating
413 # pakcages that are up-to-date.
414 if [[ -n ${EGIT_BOOTSTRAP} ]]; then
415 pushd "${EGIT_SOURCEDIR}" > /dev/null
416 einfo "Starting bootstrap"
417
418 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
419 # we have file in the repo which we should execute
420 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
421
422 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
423 eval "./${EGIT_BOOTSTRAP}" \
424 || die "${FUNCNAME}: bootstrap script failed"
425 else
426 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
427 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
428 die "\"${EGIT_BOOTSTRAP}\" is not executable"
429 fi
430 else
431 # we execute some system command
432 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
433
434 eval "${EGIT_BOOTSTRAP}" \
435 || die "${FUNCNAME}: bootstrap commands failed"
436 fi
437
438 einfo "Bootstrap finished"
439 popd > /dev/null
440 fi
441}
442
443# @FUNCTION: git-2_migrate_repository
444# @DESCRIPTION:
445# Internal function migrating between bare and normal checkout repository.
446# This is based on usage of EGIT_SUBMODULES, at least until they
447# start to work with bare checkouts sanely.
448# This function also set some global variables that differ between
449# bare and non-bare checkout.
450git-2_migrate_repository() {
451 debug-print-function ${FUNCNAME} "$@"
452
453 local target returnstate
454
455 # first find out if we have submodules
456 if [[ -z ${EGIT_HAS_SUBMODULES} ]]; then
457 target="bare"
458 else
459 target="full"
460 fi
461 # check if user didn't specify that we want non-bare repo
462 if [[ -n ${EGIT_NONBARE} ]]; then
463 target="full"
464 EGIT_LOCAL_NONBARE="true"
465 fi
466
467 # test if we already have some repo and if so find out if we have
468 # to migrate the data
469 if [[ -d ${EGIT_DIR} ]]; then
470 if [[ ${target} == bare && -d ${EGIT_DIR}/.git ]]; then
471 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
472
473 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
474 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
475 export GIT_DIR="${EGIT_DIR}.bare"
476 git config core.bare true > /dev/null
477 returnstate=$?
478 unset GIT_DIR
479 rm -rf "${EGIT_DIR}"
480 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
481 eend ${returnstate}
482 fi
483 if [[ ${target} == full && ! -d ${EGIT_DIR}/.git ]]; then
484 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
485
486 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
487 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
488 returnstate=$?
489 rm -rf "${EGIT_DIR}"
490 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
491 eend ${returnstate}
492 fi
493 fi
494 if [[ ${returnstate} -ne 0 ]]; then
495 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
496
497 # migration failed, remove the EGIT_DIR to play it safe
498 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
499 rm -rf "${EGIT_DIR}"
500 fi
501
502 # set various options to work with both targets
503 if [[ ${target} == bare ]]; then
504 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
505 EGIT_LOCAL_OPTIONS+="${EGIT_OPTIONS} --bare"
506 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
507 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
508 UPSTREAM_BRANCH="${EGIT_BRANCH}"
509 else
510 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
511 MOVE_COMMAND="cp -pPR ."
512 EGIT_LOCAL_OPTIONS="${EGIT_OPTIONS}"
513 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
514 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
515 EGIT_LOCAL_NONBARE="true"
516 fi
517}
518
519# @FUNCTION: git-2_cleanup
520# @DESCRIPTION:
521# Internal function cleaning up all the global variables
522# that are not required after the unpack has been done.
523git-2_cleanup() {
524 debug-print-function ${FUNCNAME} "$@"
525
526 # Here we can unset only variables that are GLOBAL
527 # defined by the eclass, BUT NOT subject to change
528 # by user (like EGIT_PROJECT).
529 # If ebuild writer polutes his environment it is
530 # his problem only.
531 unset EGIT_DIR
532 unset MOVE_COMMAND
533 unset EGIT_LOCAL_OPTIONS
534 unset EGIT_UPDATE_CMD
535 unset UPSTREAM_BRANCH
536 unset EGIT_LOCAL_NONBARE
537}
538
539# @FUNCTION: git-2_src_unpack
540# @DESCRIPTION:
541# Default git src_unpack function.
542git-2_src_unpack() {
543 debug-print-function ${FUNCNAME} "$@"
544
545 git-2_init_variables
546 git-2_prepare_storedir
547 git-2_migrate_repository
548 git-2_fetch "$@"
549 git-2_gc
550 git-2_submodules
551 git-2_move_source
552 git-2_branch
553 git-2_bootstrap
554 git-2_cleanup
555 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
556
557 # Users can specify some SRC_URI and we should
558 # unpack the files too.
559 if [[ -z ${EGIT_NOUNPACK} ]]; then
560 if has ${EAPI:-0} 0 1; then
561 [[ -n ${A} ]] && unpack ${A}
562 else
563 default_src_unpack
564 fi
565 fi
566}