1 # Copyright 1999-2014 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: git-2.eclass
6 # maintainer-needed@gentoo.org
7 # @BLURB: Eclass for fetching and unpacking git repositories.
9 # Eclass for easing maitenance of live ebuilds using git as remote repository.
10 # Eclass support working with git submodules and branching.
12 # This eclass is DEPRECATED. Please use git-r3 instead.
14 if [[ ${EAPI} == 6 ]]; then
15 die "${ECLASS}.eclass is banned in EAPI ${EAPI}"
18 # This eclass support all EAPIs
19 EXPORT_FUNCTIONS src_unpack
23 # @ECLASS-VARIABLE: EGIT_SOURCEDIR
25 # This variable specifies destination where the cloned
28 # EGIT_SOURCEDIR="${S}"
30 # @ECLASS-VARIABLE: EGIT_STORE_DIR
32 # Storage directory for git sources.
34 # EGIT_STORE_DIR="${DISTDIR}/egit-src"
36 # @ECLASS-VARIABLE: EGIT_HAS_SUBMODULES
39 # If non-empty this variable enables support for git submodules in our
40 # checkout. Also this makes the checkout to be non-bare for now.
42 # @ECLASS-VARIABLE: EGIT_OPTIONS
45 # Variable specifying additional options for fetch command.
47 # @ECLASS-VARIABLE: EGIT_MASTER
49 # Variable for specifying master branch.
50 # Usefull when upstream don't have master branch or name it differently.
52 # EGIT_MASTER="master"
54 # @ECLASS-VARIABLE: EGIT_PROJECT
56 # Variable specifying name for the folder where we check out the git
57 # repository. Value of this variable should be unique in the
58 # EGIT_STORE_DIR as otherwise you would override another repository.
60 # EGIT_PROJECT="${EGIT_REPO_URI##*/}"
62 # @ECLASS-VARIABLE: EGIT_DIR
64 # Directory where we want to store the git data.
65 # This variable should not be overriden.
67 # EGIT_DIR="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
69 # @ECLASS-VARIABLE: EGIT_REPO_URI
73 # URI for the repository
74 # e.g. http://foo, git://bar
76 # It can be overriden via env using packagename_LIVE_REPO
79 # Support multiple values:
80 # EGIT_REPO_URI="git://a/b.git http://c/d.git"
82 # @ECLASS-VARIABLE: EVCS_OFFLINE
85 # If non-empty this variable prevents performance of any online
88 # @ECLASS-VARIABLE: EGIT_BRANCH
90 # Variable containing branch name we want to check out.
91 # It can be overriden via env using packagename_LIVE_BRANCH
94 # EGIT_BRANCH="${EGIT_MASTER}"
96 # @ECLASS-VARIABLE: EGIT_COMMIT
98 # Variable containing commit hash/tag we want to check out.
99 # It can be overriden via env using packagename_LIVE_COMMIT
102 # EGIT_COMMIT="${EGIT_BRANCH}"
104 # @ECLASS-VARIABLE: EGIT_REPACK
107 # If non-empty this variable specifies that repository will be repacked to
108 # save space. However this can take a REALLY LONG time with VERY big
111 # @ECLASS-VARIABLE: EGIT_PRUNE
114 # If non-empty this variable enables pruning all loose objects on each fetch.
115 # This is useful if upstream rewinds and rebases branches often.
117 # @ECLASS-VARIABLE: EGIT_NONBARE
120 # If non-empty this variable specifies that all checkouts will be done using
121 # non bare repositories. This is useful if you can't operate with bare
122 # checkouts for some reason.
124 # @ECLASS-VARIABLE: EGIT_NOUNPACK
127 # If non-empty this variable bans unpacking of ${A} content into the srcdir.
128 # Default behaviour is to unpack ${A} content.
130 # @FUNCTION: git-2_init_variables
133 # Internal function initializing all git variables.
134 # We define it in function scope so user can define
135 # all the variables before and after inherit.
136 git-2_init_variables() {
137 debug-print-function ${FUNCNAME} "$@"
139 local esc_pn liverepo livebranch livecommit
142 : ${EGIT_SOURCEDIR="${S}"}
144 : ${EGIT_STORE_DIR:="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/egit-src"}
146 : ${EGIT_HAS_SUBMODULES:=}
150 : ${EGIT_MASTER:=master}
152 liverepo=${esc_pn}_LIVE_REPO
153 EGIT_REPO_URI=${!liverepo:-${EGIT_REPO_URI}}
154 [[ ${EGIT_REPO_URI} ]] || die "EGIT_REPO_URI must have some value"
158 livebranch=${esc_pn}_LIVE_BRANCH
159 [[ ${!livebranch} ]] && ewarn "QA: using \"${esc_pn}_LIVE_BRANCH\" variable, you won't get any support"
160 EGIT_BRANCH=${!livebranch:-${EGIT_BRANCH:-${EGIT_MASTER}}}
162 livecommit=${esc_pn}_LIVE_COMMIT
163 [[ ${!livecommit} ]] && ewarn "QA: using \"${esc_pn}_LIVE_COMMIT\" variable, you won't get any support"
164 EGIT_COMMIT=${!livecommit:-${EGIT_COMMIT:-${EGIT_BRANCH}}}
171 # @FUNCTION: git-2_submodules
174 # Internal function wrapping the submodule initialisation and update.
176 debug-print-function ${FUNCNAME} "$@"
177 if [[ ${EGIT_HAS_SUBMODULES} ]]; then
178 if [[ ${EVCS_OFFLINE} ]]; then
179 # for submodules operations we need to be online
180 debug-print "${FUNCNAME}: not updating submodules in offline mode"
184 debug-print "${FUNCNAME}: working in \"${1}\""
185 pushd "${EGIT_DIR}" > /dev/null || die
187 debug-print "${FUNCNAME}: git submodule init"
188 git submodule init || die
189 debug-print "${FUNCNAME}: git submodule sync"
190 git submodule sync || die
191 debug-print "${FUNCNAME}: git submodule update"
192 git submodule update || die
194 popd > /dev/null || die
198 # @FUNCTION: git-2_branch
201 # Internal function that changes branch for the repo based on EGIT_COMMIT and
202 # EGIT_BRANCH variables.
204 debug-print-function ${FUNCNAME} "$@"
208 debug-print "${FUNCNAME}: working in \"${EGIT_SOURCEDIR}\""
209 pushd "${EGIT_SOURCEDIR}" > /dev/null || die
211 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
212 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
213 branchname=tree-${EGIT_COMMIT}
216 debug-print "${FUNCNAME}: git checkout -b ${branchname} ${src}"
217 git checkout -b ${branchname} ${src} \
218 || die "${FUNCNAME}: changing the branch failed"
220 popd > /dev/null || die
223 # @FUNCTION: git-2_gc
226 # Internal function running garbage collector on checked out tree.
228 debug-print-function ${FUNCNAME} "$@"
232 if [[ ${EGIT_REPACK} || ${EGIT_PRUNE} ]]; then
233 pushd "${EGIT_DIR}" > /dev/null || die
234 ebegin "Garbage collecting the repository"
235 [[ ${EGIT_PRUNE} ]] && args='--prune'
236 debug-print "${FUNCNAME}: git gc ${args}"
239 popd > /dev/null || die
243 # @FUNCTION: git-2_prepare_storedir
246 # Internal function preparing directory where we are going to store SCM
248 git-2_prepare_storedir() {
249 debug-print-function ${FUNCNAME} "$@"
253 # initial clone, we have to create master git storage directory and play
254 # nicely with sandbox
255 if [[ ! -d ${EGIT_STORE_DIR} ]]; then
256 debug-print "${FUNCNAME}: Creating git main storage directory"
258 mkdir -m 775 -p "${EGIT_STORE_DIR}" \
259 || die "${FUNCNAME}: can't mkdir \"${EGIT_STORE_DIR}\""
262 # allow writing into EGIT_STORE_DIR
263 addwrite "${EGIT_STORE_DIR}"
265 # calculate git.eclass store dir for data
266 # We will try to clone the old repository,
267 # and we will remove it if we don't need it anymore.
269 if [[ ${EGIT_STORE_DIR} == */egit-src ]]; then
270 local old_store_dir=${EGIT_STORE_DIR/%egit-src/git-src}
271 local old_location=${old_store_dir}/${EGIT_PROJECT:-${PN}}
273 if [[ -d ${old_location} ]]; then
274 EGIT_OLD_CLONE=${old_location}
275 # required to remove the old clone
276 addwrite "${old_store_dir}"
280 # calculate the proper store dir for data
281 # If user didn't specify the EGIT_DIR, we check if he did specify
282 # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
283 EGIT_REPO_URI=${EGIT_REPO_URI%/}
284 if [[ ! ${EGIT_DIR} ]]; then
285 if [[ ${EGIT_PROJECT} ]]; then
286 clone_dir=${EGIT_PROJECT}
288 local strippeduri=${EGIT_REPO_URI%/.git}
289 clone_dir=${strippeduri##*/}
291 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
293 if [[ ${EGIT_OLD_CLONE} && ! -d ${EGIT_DIR} ]]; then
294 elog "${FUNCNAME}: ${CATEGORY}/${PF} will be cloned from old location."
295 elog "It will be necessary to rebuild the package to fetch updates."
296 EGIT_REPO_URI="${EGIT_OLD_CLONE} ${EGIT_REPO_URI}"
299 export EGIT_DIR=${EGIT_DIR}
300 debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
303 # @FUNCTION: git-2_move_source
306 # Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
307 git-2_move_source() {
308 debug-print-function ${FUNCNAME} "$@"
310 debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
311 pushd "${EGIT_DIR}" > /dev/null || die
312 mkdir -p "${EGIT_SOURCEDIR}" \
313 || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
314 ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
315 || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
316 popd > /dev/null || die
319 # @FUNCTION: git-2_initial_clone
322 # Internal function running initial clone on specified repo_uri.
323 git-2_initial_clone() {
324 debug-print-function ${FUNCNAME} "$@"
328 EGIT_REPO_URI_SELECTED=""
329 for repo_uri in ${EGIT_REPO_URI}; do
330 debug-print "${FUNCNAME}: git clone ${EGIT_LOCAL_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
331 if git clone ${EGIT_LOCAL_OPTIONS} "${repo_uri}" "${EGIT_DIR}"; then
332 # global variable containing the repo_name we will be using
333 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
334 EGIT_REPO_URI_SELECTED="${repo_uri}"
339 [[ ${EGIT_REPO_URI_SELECTED} ]] \
340 || die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
343 # @FUNCTION: git-2_update_repo
346 # Internal function running update command on specified repo_uri.
347 git-2_update_repo() {
348 debug-print-function ${FUNCNAME} "$@"
352 if [[ ${EGIT_LOCAL_NONBARE} ]]; then
353 # checkout master branch and drop all other local branches
354 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
355 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
356 debug-print "${FUNCNAME}: git branch -D ${x}"
357 git branch -D ${x} > /dev/null
361 EGIT_REPO_URI_SELECTED=""
362 for repo_uri in ${EGIT_REPO_URI}; do
363 # git urls might change, so reset it
364 git config remote.origin.url "${repo_uri}"
366 debug-print "${EGIT_UPDATE_CMD}"
367 if ${EGIT_UPDATE_CMD} > /dev/null; then
368 # global variable containing the repo_name we will be using
369 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
370 EGIT_REPO_URI_SELECTED="${repo_uri}"
375 [[ ${EGIT_REPO_URI_SELECTED} ]] \
376 || die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
379 # @FUNCTION: git-2_fetch
382 # Internal function fetching repository from EGIT_REPO_URI and storing it in
383 # specified EGIT_STORE_DIR.
385 debug-print-function ${FUNCNAME} "$@"
387 local oldsha cursha repo_type
389 [[ ${EGIT_LOCAL_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
391 if [[ ! -d ${EGIT_DIR} ]]; then
393 pushd "${EGIT_DIR}" > /dev/null || die
394 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
395 echo "GIT NEW clone -->"
396 echo " repository: ${EGIT_REPO_URI_SELECTED}"
397 echo " at the commit: ${cursha}"
399 popd > /dev/null || die
400 elif [[ ${EVCS_OFFLINE} ]]; then
401 pushd "${EGIT_DIR}" > /dev/null || die
402 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
403 echo "GIT offline update -->"
404 echo " repository: $(git config remote.origin.url)"
405 echo " at the commit: ${cursha}"
406 popd > /dev/null || die
408 pushd "${EGIT_DIR}" > /dev/null || die
409 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
411 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
414 echo "GIT update -->"
415 echo " repository: ${EGIT_REPO_URI_SELECTED}"
416 # write out message based on the revisions
417 if [[ "${oldsha}" != "${cursha}" ]]; then
418 echo " updating from commit: ${oldsha}"
419 echo " to commit: ${cursha}"
421 echo " at the commit: ${cursha}"
424 # print nice statistic of what was changed
425 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
426 popd > /dev/null || die
428 # export the version the repository is at
429 export EGIT_VERSION="${cursha}"
431 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
432 && echo " commit: ${EGIT_COMMIT}"
433 echo " branch: ${EGIT_BRANCH}"
434 echo " storage directory: \"${EGIT_DIR}\""
435 echo " checkout type: ${repo_type}"
437 # Cleanup after git.eclass
438 if [[ ${EGIT_OLD_CLONE} ]]; then
439 einfo "${FUNCNAME}: removing old clone in ${EGIT_OLD_CLONE}."
440 rm -rf "${EGIT_OLD_CLONE}"
444 # @FUNCTION: git_bootstrap
447 # Internal function that runs bootstrap command on unpacked source.
449 debug-print-function ${FUNCNAME} "$@"
451 # @ECLASS-VARIABLE: EGIT_BOOTSTRAP
453 # Command to be executed after checkout and clone of the specified
455 # enviroment the package will fail if there is no update, thus in
456 # combination with --keep-going it would lead in not-updating
457 # pakcages that are up-to-date.
458 if [[ ${EGIT_BOOTSTRAP} ]]; then
459 pushd "${EGIT_SOURCEDIR}" > /dev/null || die
460 einfo "Starting bootstrap"
462 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
463 # we have file in the repo which we should execute
464 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
466 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
467 eval "./${EGIT_BOOTSTRAP}" \
468 || die "${FUNCNAME}: bootstrap script failed"
470 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
471 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
472 die "\"${EGIT_BOOTSTRAP}\" is not executable"
475 # we execute some system command
476 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
478 eval "${EGIT_BOOTSTRAP}" \
479 || die "${FUNCNAME}: bootstrap commands failed"
482 einfo "Bootstrap finished"
483 popd > /dev/null || die
487 # @FUNCTION: git-2_migrate_repository
490 # Internal function migrating between bare and normal checkout repository.
491 # This is based on usage of EGIT_SUBMODULES, at least until they
492 # start to work with bare checkouts sanely.
493 # This function also set some global variables that differ between
494 # bare and non-bare checkout.
495 git-2_migrate_repository() {
496 debug-print-function ${FUNCNAME} "$@"
498 local bare returnstate
500 # first find out if we have submodules
501 # or user explicitly wants us to use non-bare clones
502 if ! [[ ${EGIT_HAS_SUBMODULES} || ${EGIT_NONBARE} ]]; then
506 # test if we already have some repo and if so find out if we have
507 # to migrate the data
508 if [[ -d ${EGIT_DIR} ]]; then
509 if [[ ${bare} && -d ${EGIT_DIR}/.git ]]; then
510 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
512 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
513 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
514 export GIT_DIR="${EGIT_DIR}.bare"
515 git config core.bare true > /dev/null
519 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
521 elif [[ ! ${bare} && ! -d ${EGIT_DIR}/.git ]]; then
522 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
524 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
525 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
528 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
532 if [[ ${returnstate} -ne 0 ]]; then
533 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
535 # migration failed, remove the EGIT_DIR to play it safe
536 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
540 # set various options to work with both targets
541 if [[ ${bare} ]]; then
542 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
543 EGIT_LOCAL_OPTIONS+="${EGIT_OPTIONS} --bare"
544 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
545 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
546 UPSTREAM_BRANCH="${EGIT_BRANCH}"
549 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
550 MOVE_COMMAND="cp -pPR ."
551 EGIT_LOCAL_OPTIONS="${EGIT_OPTIONS}"
552 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
553 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
554 EGIT_LOCAL_NONBARE="true"
558 # @FUNCTION: git-2_cleanup
561 # Internal function cleaning up all the global variables
562 # that are not required after the unpack has been done.
564 debug-print-function ${FUNCNAME} "$@"
566 # Here we can unset only variables that are GLOBAL
567 # defined by the eclass, BUT NOT subject to change
568 # by user (like EGIT_PROJECT).
569 # If ebuild writer polutes his environment it is
573 unset EGIT_LOCAL_OPTIONS
574 unset EGIT_UPDATE_CMD
575 unset UPSTREAM_BRANCH
576 unset EGIT_LOCAL_NONBARE
579 # @FUNCTION: git-2_src_unpack
581 # Default git src_unpack function.
583 debug-print-function ${FUNCNAME} "$@"
586 git-2_prepare_storedir
587 git-2_migrate_repository
595 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
597 # Users can specify some SRC_URI and we should
598 # unpack the files too.
599 if [[ ! ${EGIT_NOUNPACK} ]]; then
600 if has ${EAPI:-0} 0 1; then
601 [[ ${A} ]] && unpack ${A}