1 # Copyright 1999-2018 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 # @SUPPORTED_EAPIS: 0 1 2 3 4 5
8 # @BLURB: Eclass for fetching and unpacking git repositories.
10 # Eclass for easing maintenance of live ebuilds using git as remote repository.
11 # Eclass support working with git submodules and branching.
13 # This eclass is DEPRECATED. Please use git-r3 instead.
17 *) die "${ECLASS}.eclass is banned in EAPI ${EAPI}";;
20 # This eclass support all EAPIs.
21 EXPORT_FUNCTIONS src_unpack
25 # @ECLASS-VARIABLE: EGIT_SOURCEDIR
27 # This variable specifies destination where the cloned
30 # EGIT_SOURCEDIR="${S}"
32 # @ECLASS-VARIABLE: EGIT_STORE_DIR
34 # Storage directory for git sources.
36 # EGIT_STORE_DIR="${DISTDIR}/egit-src"
38 # @ECLASS-VARIABLE: EGIT_HAS_SUBMODULES
41 # If non-empty this variable enables support for git submodules in our
42 # checkout. Also this makes the checkout to be non-bare for now.
44 # @ECLASS-VARIABLE: EGIT_OPTIONS
47 # Variable specifying additional options for fetch command.
49 # @ECLASS-VARIABLE: EGIT_MASTER
51 # Variable for specifying master branch.
52 # Useful when upstream don't have master branch or name it differently.
54 # EGIT_MASTER="master"
56 # @ECLASS-VARIABLE: EGIT_PROJECT
58 # Variable specifying name for the folder where we check out the git
59 # repository. Value of this variable should be unique in the
60 # EGIT_STORE_DIR as otherwise you would override another repository.
62 # EGIT_PROJECT="${EGIT_REPO_URI##*/}"
64 # @ECLASS-VARIABLE: EGIT_DIR
66 # Directory where we want to store the git data.
67 # This variable should not be overridden.
69 # EGIT_DIR="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
71 # @ECLASS-VARIABLE: EGIT_REPO_URI
75 # URI for the repository
76 # e.g. http://foo, git://bar
78 # It can be overridden via env using packagename_LIVE_REPO
81 # Support multiple values:
82 # EGIT_REPO_URI="git://a/b.git http://c/d.git"
84 # @ECLASS-VARIABLE: EVCS_OFFLINE
87 # If non-empty this variable prevents performance of any online
90 # @ECLASS-VARIABLE: EGIT_BRANCH
92 # Variable containing branch name we want to check out.
93 # It can be overridden via env using packagename_LIVE_BRANCH
96 # EGIT_BRANCH="${EGIT_MASTER}"
98 # @ECLASS-VARIABLE: EGIT_COMMIT
100 # Variable containing commit hash/tag we want to check out.
101 # It can be overridden via env using packagename_LIVE_COMMIT
104 # EGIT_COMMIT="${EGIT_BRANCH}"
106 # @ECLASS-VARIABLE: EGIT_REPACK
109 # If non-empty this variable specifies that repository will be repacked to
110 # save space. However this can take a REALLY LONG time with VERY big
113 # @ECLASS-VARIABLE: EGIT_PRUNE
116 # If non-empty this variable enables pruning all loose objects on each fetch.
117 # This is useful if upstream rewinds and rebases branches often.
119 # @ECLASS-VARIABLE: EGIT_NONBARE
122 # If non-empty this variable specifies that all checkouts will be done using
123 # non bare repositories. This is useful if you can't operate with bare
124 # checkouts for some reason.
126 # @ECLASS-VARIABLE: EGIT_NOUNPACK
129 # If non-empty this variable bans unpacking of ${A} content into the srcdir.
130 # Default behavior is to unpack ${A} content.
132 # @FUNCTION: git-2_init_variables
135 # Internal function initializing all git variables.
136 # We define it in function scope so user can define
137 # all the variables before and after inherit.
138 git-2_init_variables() {
139 debug-print-function ${FUNCNAME} "$@"
141 local esc_pn liverepo livebranch livecommit
144 : ${EGIT_SOURCEDIR="${S}"}
146 : ${EGIT_STORE_DIR:="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/egit-src"}
148 : ${EGIT_HAS_SUBMODULES:=}
152 : ${EGIT_MASTER:=master}
154 liverepo=${esc_pn}_LIVE_REPO
155 EGIT_REPO_URI=${!liverepo:-${EGIT_REPO_URI}}
156 [[ ${EGIT_REPO_URI} ]] || die "EGIT_REPO_URI must have some value"
160 livebranch=${esc_pn}_LIVE_BRANCH
161 [[ ${!livebranch} ]] && ewarn "QA: using \"${esc_pn}_LIVE_BRANCH\" variable, you won't get any support"
162 EGIT_BRANCH=${!livebranch:-${EGIT_BRANCH:-${EGIT_MASTER}}}
164 livecommit=${esc_pn}_LIVE_COMMIT
165 [[ ${!livecommit} ]] && ewarn "QA: using \"${esc_pn}_LIVE_COMMIT\" variable, you won't get any support"
166 EGIT_COMMIT=${!livecommit:-${EGIT_COMMIT:-${EGIT_BRANCH}}}
173 # @FUNCTION: git-2_submodules
176 # Internal function wrapping the submodule initialisation and update.
178 debug-print-function ${FUNCNAME} "$@"
179 if [[ ${EGIT_HAS_SUBMODULES} ]]; then
180 if [[ ${EVCS_OFFLINE} ]]; then
181 # for submodules operations we need to be online
182 debug-print "${FUNCNAME}: not updating submodules in offline mode"
186 debug-print "${FUNCNAME}: working in \"${1}\""
187 pushd "${EGIT_DIR}" > /dev/null || die
189 debug-print "${FUNCNAME}: git submodule init"
190 git submodule init || die
191 debug-print "${FUNCNAME}: git submodule sync"
192 git submodule sync || die
193 debug-print "${FUNCNAME}: git submodule update"
194 git submodule update || die
196 popd > /dev/null || die
200 # @FUNCTION: git-2_branch
203 # Internal function that changes branch for the repo based on EGIT_COMMIT and
204 # EGIT_BRANCH variables.
206 debug-print-function ${FUNCNAME} "$@"
210 debug-print "${FUNCNAME}: working in \"${EGIT_SOURCEDIR}\""
211 pushd "${EGIT_SOURCEDIR}" > /dev/null || die
213 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
214 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
215 branchname=tree-${EGIT_COMMIT}
218 debug-print "${FUNCNAME}: git checkout -b ${branchname} ${src}"
219 git checkout -b ${branchname} ${src} \
220 || die "${FUNCNAME}: changing the branch failed"
222 popd > /dev/null || die
225 # @FUNCTION: git-2_gc
228 # Internal function running garbage collector on checked out tree.
230 debug-print-function ${FUNCNAME} "$@"
234 if [[ ${EGIT_REPACK} || ${EGIT_PRUNE} ]]; then
235 pushd "${EGIT_DIR}" > /dev/null || die
236 ebegin "Garbage collecting the repository"
237 [[ ${EGIT_PRUNE} ]] && args='--prune'
238 debug-print "${FUNCNAME}: git gc ${args}"
241 popd > /dev/null || die
245 # @FUNCTION: git-2_prepare_storedir
248 # Internal function preparing directory where we are going to store SCM
250 git-2_prepare_storedir() {
251 debug-print-function ${FUNCNAME} "$@"
255 # initial clone, we have to create master git storage directory and play
256 # nicely with sandbox
257 if [[ ! -d ${EGIT_STORE_DIR} ]]; then
258 debug-print "${FUNCNAME}: Creating git main storage directory"
260 mkdir -m 775 -p "${EGIT_STORE_DIR}" \
261 || die "${FUNCNAME}: can't mkdir \"${EGIT_STORE_DIR}\""
264 # allow writing into EGIT_STORE_DIR
265 addwrite "${EGIT_STORE_DIR}"
267 # calculate git.eclass store dir for data
268 # We will try to clone the old repository,
269 # and we will remove it if we don't need it anymore.
271 if [[ ${EGIT_STORE_DIR} == */egit-src ]]; then
272 local old_store_dir=${EGIT_STORE_DIR/%egit-src/git-src}
273 local old_location=${old_store_dir}/${EGIT_PROJECT:-${PN}}
275 if [[ -d ${old_location} ]]; then
276 EGIT_OLD_CLONE=${old_location}
277 # required to remove the old clone
278 addwrite "${old_store_dir}"
282 # calculate the proper store dir for data
283 # If user didn't specify the EGIT_DIR, we check if he did specify
284 # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
285 EGIT_REPO_URI=${EGIT_REPO_URI%/}
286 if [[ ! ${EGIT_DIR} ]]; then
287 if [[ ${EGIT_PROJECT} ]]; then
288 clone_dir=${EGIT_PROJECT}
290 local strippeduri=${EGIT_REPO_URI%/.git}
291 clone_dir=${strippeduri##*/}
293 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
295 if [[ ${EGIT_OLD_CLONE} && ! -d ${EGIT_DIR} ]]; then
296 elog "${FUNCNAME}: ${CATEGORY}/${PF} will be cloned from old location."
297 elog "It will be necessary to rebuild the package to fetch updates."
298 EGIT_REPO_URI="${EGIT_OLD_CLONE} ${EGIT_REPO_URI}"
301 export EGIT_DIR=${EGIT_DIR}
302 debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
305 # @FUNCTION: git-2_move_source
308 # Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
309 git-2_move_source() {
310 debug-print-function ${FUNCNAME} "$@"
312 debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
313 pushd "${EGIT_DIR}" > /dev/null || die
314 mkdir -p "${EGIT_SOURCEDIR}" \
315 || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
316 ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
317 || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
318 popd > /dev/null || die
321 # @FUNCTION: git-2_initial_clone
324 # Internal function running initial clone on specified repo_uri.
325 git-2_initial_clone() {
326 debug-print-function ${FUNCNAME} "$@"
330 EGIT_REPO_URI_SELECTED=""
331 for repo_uri in ${EGIT_REPO_URI}; do
332 debug-print "${FUNCNAME}: git clone ${EGIT_LOCAL_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
333 if git clone ${EGIT_LOCAL_OPTIONS} "${repo_uri}" "${EGIT_DIR}"; then
334 # global variable containing the repo_name we will be using
335 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
336 EGIT_REPO_URI_SELECTED="${repo_uri}"
341 [[ ${EGIT_REPO_URI_SELECTED} ]] \
342 || die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
345 # @FUNCTION: git-2_update_repo
348 # Internal function running update command on specified repo_uri.
349 git-2_update_repo() {
350 debug-print-function ${FUNCNAME} "$@"
354 if [[ ${EGIT_LOCAL_NONBARE} ]]; then
355 # checkout master branch and drop all other local branches
356 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
357 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
358 debug-print "${FUNCNAME}: git branch -D ${x}"
359 git branch -D ${x} > /dev/null
363 EGIT_REPO_URI_SELECTED=""
364 for repo_uri in ${EGIT_REPO_URI}; do
365 # git urls might change, so reset it
366 git config remote.origin.url "${repo_uri}"
368 debug-print "${EGIT_UPDATE_CMD}"
369 if ${EGIT_UPDATE_CMD} > /dev/null; then
370 # global variable containing the repo_name we will be using
371 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
372 EGIT_REPO_URI_SELECTED="${repo_uri}"
377 [[ ${EGIT_REPO_URI_SELECTED} ]] \
378 || die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
381 # @FUNCTION: git-2_fetch
384 # Internal function fetching repository from EGIT_REPO_URI and storing it in
385 # specified EGIT_STORE_DIR.
387 debug-print-function ${FUNCNAME} "$@"
389 local oldsha cursha repo_type
391 [[ ${EGIT_LOCAL_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
393 if [[ ! -d ${EGIT_DIR} ]]; then
395 pushd "${EGIT_DIR}" > /dev/null || die
396 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
397 echo "GIT NEW clone -->"
398 echo " repository: ${EGIT_REPO_URI_SELECTED}"
399 echo " at the commit: ${cursha}"
401 popd > /dev/null || die
402 elif [[ ${EVCS_OFFLINE} ]]; then
403 pushd "${EGIT_DIR}" > /dev/null || die
404 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
405 echo "GIT offline update -->"
406 echo " repository: $(git config remote.origin.url)"
407 echo " at the commit: ${cursha}"
408 popd > /dev/null || die
410 pushd "${EGIT_DIR}" > /dev/null || die
411 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
413 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
416 echo "GIT update -->"
417 echo " repository: ${EGIT_REPO_URI_SELECTED}"
418 # write out message based on the revisions
419 if [[ "${oldsha}" != "${cursha}" ]]; then
420 echo " updating from commit: ${oldsha}"
421 echo " to commit: ${cursha}"
423 echo " at the commit: ${cursha}"
426 # print nice statistic of what was changed
427 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
428 popd > /dev/null || die
430 # export the version the repository is at
431 export EGIT_VERSION="${cursha}"
433 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
434 && echo " commit: ${EGIT_COMMIT}"
435 echo " branch: ${EGIT_BRANCH}"
436 echo " storage directory: \"${EGIT_DIR}\""
437 echo " checkout type: ${repo_type}"
439 # Cleanup after git.eclass
440 if [[ ${EGIT_OLD_CLONE} ]]; then
441 einfo "${FUNCNAME}: removing old clone in ${EGIT_OLD_CLONE}."
442 rm -rf "${EGIT_OLD_CLONE}"
446 # @FUNCTION: git_bootstrap
449 # Internal function that runs bootstrap command on unpacked source.
451 debug-print-function ${FUNCNAME} "$@"
453 # @ECLASS-VARIABLE: EGIT_BOOTSTRAP
455 # Command to be executed after checkout and clone of the specified
457 # enviroment the package will fail if there is no update, thus in
458 # combination with --keep-going it would lead in not-updating
459 # pakcages that are up-to-date.
460 if [[ ${EGIT_BOOTSTRAP} ]]; then
461 pushd "${EGIT_SOURCEDIR}" > /dev/null || die
462 einfo "Starting bootstrap"
464 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
465 # we have file in the repo which we should execute
466 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
468 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
469 eval "./${EGIT_BOOTSTRAP}" \
470 || die "${FUNCNAME}: bootstrap script failed"
472 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
473 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
474 die "\"${EGIT_BOOTSTRAP}\" is not executable"
477 # we execute some system command
478 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
480 eval "${EGIT_BOOTSTRAP}" \
481 || die "${FUNCNAME}: bootstrap commands failed"
484 einfo "Bootstrap finished"
485 popd > /dev/null || die
489 # @FUNCTION: git-2_migrate_repository
492 # Internal function migrating between bare and normal checkout repository.
493 # This is based on usage of EGIT_SUBMODULES, at least until they
494 # start to work with bare checkouts sanely.
495 # This function also set some global variables that differ between
496 # bare and non-bare checkout.
497 git-2_migrate_repository() {
498 debug-print-function ${FUNCNAME} "$@"
500 local bare returnstate
502 # first find out if we have submodules
503 # or user explicitly wants us to use non-bare clones
504 if ! [[ ${EGIT_HAS_SUBMODULES} || ${EGIT_NONBARE} ]]; then
508 # test if we already have some repo and if so find out if we have
509 # to migrate the data
510 if [[ -d ${EGIT_DIR} ]]; then
511 if [[ ${bare} && -d ${EGIT_DIR}/.git ]]; then
512 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
514 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
515 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
516 export GIT_DIR="${EGIT_DIR}.bare"
517 git config core.bare true > /dev/null
521 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
523 elif [[ ! ${bare} && ! -d ${EGIT_DIR}/.git ]]; then
524 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
526 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
527 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
530 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
534 if [[ ${returnstate} -ne 0 ]]; then
535 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
537 # migration failed, remove the EGIT_DIR to play it safe
538 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
542 # set various options to work with both targets
543 if [[ ${bare} ]]; then
544 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
545 EGIT_LOCAL_OPTIONS+="${EGIT_OPTIONS} --bare"
546 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
547 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
548 UPSTREAM_BRANCH="${EGIT_BRANCH}"
551 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
552 MOVE_COMMAND="cp -pPR ."
553 EGIT_LOCAL_OPTIONS="${EGIT_OPTIONS}"
554 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
555 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
556 EGIT_LOCAL_NONBARE="true"
560 # @FUNCTION: git-2_cleanup
563 # Internal function cleaning up all the global variables
564 # that are not required after the unpack has been done.
566 debug-print-function ${FUNCNAME} "$@"
568 # Here we can unset only variables that are GLOBAL
569 # defined by the eclass, BUT NOT subject to change
570 # by user (like EGIT_PROJECT).
571 # If ebuild writer polutes his environment it is
575 unset EGIT_LOCAL_OPTIONS
576 unset EGIT_UPDATE_CMD
577 unset UPSTREAM_BRANCH
578 unset EGIT_LOCAL_NONBARE
581 # @FUNCTION: git-2_src_unpack
583 # Default git src_unpack function.
585 debug-print-function ${FUNCNAME} "$@"
588 git-2_prepare_storedir
589 git-2_migrate_repository
597 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
599 # Users can specify some SRC_URI and we should
600 # unpack the files too.
601 if [[ ! ${EGIT_NOUNPACK} ]]; then
602 if has ${EAPI:-0} 0 1; then
603 [[ ${A} ]] && unpack ${A}