1 # Copyright 1999-2019 Gentoo Authors
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
27 # @ECLASS-VARIABLE: EGIT_SOURCEDIR
29 # This variable specifies destination where the cloned
32 # EGIT_SOURCEDIR="${S}"
34 # @ECLASS-VARIABLE: EGIT_STORE_DIR
36 # Storage directory for git sources.
38 # EGIT_STORE_DIR="${DISTDIR}/egit-src"
40 # @ECLASS-VARIABLE: EGIT_HAS_SUBMODULES
43 # If non-empty this variable enables support for git submodules in our
44 # checkout. Also this makes the checkout to be non-bare for now.
46 # @ECLASS-VARIABLE: EGIT_OPTIONS
49 # Variable specifying additional options for fetch command.
51 # @ECLASS-VARIABLE: EGIT_MASTER
53 # Variable for specifying master branch.
54 # Useful when upstream don't have master branch or name it differently.
56 # EGIT_MASTER="master"
58 # @ECLASS-VARIABLE: EGIT_PROJECT
60 # Variable specifying name for the folder where we check out the git
61 # repository. Value of this variable should be unique in the
62 # EGIT_STORE_DIR as otherwise you would override another repository.
64 # EGIT_PROJECT="${EGIT_REPO_URI##*/}"
66 # @ECLASS-VARIABLE: EGIT_DIR
68 # Directory where we want to store the git data.
69 # This variable should not be overridden.
71 # EGIT_DIR="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
73 # @ECLASS-VARIABLE: EGIT_REPO_URI
77 # URI for the repository
78 # e.g. http://foo, git://bar
80 # It can be overridden via env using packagename_LIVE_REPO
83 # Support multiple values:
84 # EGIT_REPO_URI="git://a/b.git http://c/d.git"
86 # @ECLASS-VARIABLE: EVCS_OFFLINE
89 # If non-empty this variable prevents performance of any online
92 # @ECLASS-VARIABLE: EGIT_BRANCH
94 # Variable containing branch name we want to check out.
95 # It can be overridden via env using packagename_LIVE_BRANCH
98 # EGIT_BRANCH="${EGIT_MASTER}"
100 # @ECLASS-VARIABLE: EGIT_COMMIT
102 # Variable containing commit hash/tag we want to check out.
103 # It can be overridden via env using packagename_LIVE_COMMIT
106 # EGIT_COMMIT="${EGIT_BRANCH}"
108 # @ECLASS-VARIABLE: EGIT_REPACK
111 # If non-empty this variable specifies that repository will be repacked to
112 # save space. However this can take a REALLY LONG time with VERY big
115 # @ECLASS-VARIABLE: EGIT_PRUNE
118 # If non-empty this variable enables pruning all loose objects on each fetch.
119 # This is useful if upstream rewinds and rebases branches often.
121 # @ECLASS-VARIABLE: EGIT_NONBARE
124 # If non-empty this variable specifies that all checkouts will be done using
125 # non bare repositories. This is useful if you can't operate with bare
126 # checkouts for some reason.
128 # @ECLASS-VARIABLE: EGIT_NOUNPACK
131 # If non-empty this variable bans unpacking of ${A} content into the srcdir.
132 # Default behavior is to unpack ${A} content.
134 # @FUNCTION: git-2_init_variables
137 # Internal function initializing all git variables.
138 # We define it in function scope so user can define
139 # all the variables before and after inherit.
140 git-2_init_variables() {
141 debug-print-function ${FUNCNAME} "$@"
143 local esc_pn liverepo livebranch livecommit
146 : ${EGIT_SOURCEDIR="${S}"}
148 : ${EGIT_STORE_DIR:="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/egit-src"}
150 : ${EGIT_HAS_SUBMODULES:=}
154 : ${EGIT_MASTER:=master}
156 liverepo=${esc_pn}_LIVE_REPO
157 EGIT_REPO_URI=${!liverepo:-${EGIT_REPO_URI}}
158 [[ ${EGIT_REPO_URI} ]] || die "EGIT_REPO_URI must have some value"
162 livebranch=${esc_pn}_LIVE_BRANCH
163 [[ ${!livebranch} ]] && ewarn "QA: using \"${esc_pn}_LIVE_BRANCH\" variable, you won't get any support"
164 EGIT_BRANCH=${!livebranch:-${EGIT_BRANCH:-${EGIT_MASTER}}}
166 livecommit=${esc_pn}_LIVE_COMMIT
167 [[ ${!livecommit} ]] && ewarn "QA: using \"${esc_pn}_LIVE_COMMIT\" variable, you won't get any support"
168 EGIT_COMMIT=${!livecommit:-${EGIT_COMMIT:-${EGIT_BRANCH}}}
175 # @FUNCTION: git-2_submodules
178 # Internal function wrapping the submodule initialisation and update.
180 debug-print-function ${FUNCNAME} "$@"
181 if [[ ${EGIT_HAS_SUBMODULES} ]]; then
182 if [[ ${EVCS_OFFLINE} ]]; then
183 # for submodules operations we need to be online
184 debug-print "${FUNCNAME}: not updating submodules in offline mode"
188 debug-print "${FUNCNAME}: working in \"${1}\""
189 pushd "${EGIT_DIR}" > /dev/null || die
191 debug-print "${FUNCNAME}: git submodule init"
192 git submodule init || die
193 debug-print "${FUNCNAME}: git submodule sync"
194 git submodule sync || die
195 debug-print "${FUNCNAME}: git submodule update"
196 git submodule update || die
198 popd > /dev/null || die
202 # @FUNCTION: git-2_branch
205 # Internal function that changes branch for the repo based on EGIT_COMMIT and
206 # EGIT_BRANCH variables.
208 debug-print-function ${FUNCNAME} "$@"
212 debug-print "${FUNCNAME}: working in \"${EGIT_SOURCEDIR}\""
213 pushd "${EGIT_SOURCEDIR}" > /dev/null || die
215 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
216 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
217 branchname=tree-${EGIT_COMMIT}
220 debug-print "${FUNCNAME}: git checkout -b ${branchname} ${src}"
221 git checkout -b ${branchname} ${src} \
222 || die "${FUNCNAME}: changing the branch failed"
224 popd > /dev/null || die
227 # @FUNCTION: git-2_gc
230 # Internal function running garbage collector on checked out tree.
232 debug-print-function ${FUNCNAME} "$@"
236 if [[ ${EGIT_REPACK} || ${EGIT_PRUNE} ]]; then
237 pushd "${EGIT_DIR}" > /dev/null || die
238 ebegin "Garbage collecting the repository"
239 [[ ${EGIT_PRUNE} ]] && args='--prune'
240 debug-print "${FUNCNAME}: git gc ${args}"
243 popd > /dev/null || die
247 # @FUNCTION: git-2_prepare_storedir
250 # Internal function preparing directory where we are going to store SCM
252 git-2_prepare_storedir() {
253 debug-print-function ${FUNCNAME} "$@"
257 # initial clone, we have to create master git storage directory and play
258 # nicely with sandbox
259 if [[ ! -d ${EGIT_STORE_DIR} ]]; then
260 debug-print "${FUNCNAME}: Creating git main storage directory"
262 mkdir -m 775 -p "${EGIT_STORE_DIR}" \
263 || die "${FUNCNAME}: can't mkdir \"${EGIT_STORE_DIR}\""
266 # allow writing into EGIT_STORE_DIR
267 addwrite "${EGIT_STORE_DIR}"
269 # calculate git.eclass store dir for data
270 # We will try to clone the old repository,
271 # and we will remove it if we don't need it anymore.
273 if [[ ${EGIT_STORE_DIR} == */egit-src ]]; then
274 local old_store_dir=${EGIT_STORE_DIR/%egit-src/git-src}
275 local old_location=${old_store_dir}/${EGIT_PROJECT:-${PN}}
277 if [[ -d ${old_location} ]]; then
278 EGIT_OLD_CLONE=${old_location}
279 # required to remove the old clone
280 addwrite "${old_store_dir}"
284 # calculate the proper store dir for data
285 # If user didn't specify the EGIT_DIR, we check if he did specify
286 # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
287 EGIT_REPO_URI=${EGIT_REPO_URI%/}
288 if [[ ! ${EGIT_DIR} ]]; then
289 if [[ ${EGIT_PROJECT} ]]; then
290 clone_dir=${EGIT_PROJECT}
292 local strippeduri=${EGIT_REPO_URI%/.git}
293 clone_dir=${strippeduri##*/}
295 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
297 if [[ ${EGIT_OLD_CLONE} && ! -d ${EGIT_DIR} ]]; then
298 elog "${FUNCNAME}: ${CATEGORY}/${PF} will be cloned from old location."
299 elog "It will be necessary to rebuild the package to fetch updates."
300 EGIT_REPO_URI="${EGIT_OLD_CLONE} ${EGIT_REPO_URI}"
303 export EGIT_DIR=${EGIT_DIR}
304 debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
307 # @FUNCTION: git-2_move_source
310 # Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
311 git-2_move_source() {
312 debug-print-function ${FUNCNAME} "$@"
314 debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
315 pushd "${EGIT_DIR}" > /dev/null || die
316 mkdir -p "${EGIT_SOURCEDIR}" \
317 || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
318 ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
319 || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
320 popd > /dev/null || die
323 # @FUNCTION: git-2_initial_clone
326 # Internal function running initial clone on specified repo_uri.
327 git-2_initial_clone() {
328 debug-print-function ${FUNCNAME} "$@"
332 EGIT_REPO_URI_SELECTED=""
333 for repo_uri in ${EGIT_REPO_URI}; do
334 debug-print "${FUNCNAME}: git clone ${EGIT_LOCAL_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
335 if git clone ${EGIT_LOCAL_OPTIONS} "${repo_uri}" "${EGIT_DIR}"; then
336 # global variable containing the repo_name we will be using
337 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
338 EGIT_REPO_URI_SELECTED="${repo_uri}"
343 [[ ${EGIT_REPO_URI_SELECTED} ]] \
344 || die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
347 # @FUNCTION: git-2_update_repo
350 # Internal function running update command on specified repo_uri.
351 git-2_update_repo() {
352 debug-print-function ${FUNCNAME} "$@"
356 if [[ ${EGIT_LOCAL_NONBARE} ]]; then
357 # checkout master branch and drop all other local branches
358 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
359 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
360 debug-print "${FUNCNAME}: git branch -D ${x}"
361 git branch -D ${x} > /dev/null
365 EGIT_REPO_URI_SELECTED=""
366 for repo_uri in ${EGIT_REPO_URI}; do
367 # git urls might change, so reset it
368 git config remote.origin.url "${repo_uri}"
370 debug-print "${EGIT_UPDATE_CMD}"
371 if ${EGIT_UPDATE_CMD} > /dev/null; then
372 # global variable containing the repo_name we will be using
373 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
374 EGIT_REPO_URI_SELECTED="${repo_uri}"
379 [[ ${EGIT_REPO_URI_SELECTED} ]] \
380 || die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
383 # @FUNCTION: git-2_fetch
386 # Internal function fetching repository from EGIT_REPO_URI and storing it in
387 # specified EGIT_STORE_DIR.
389 debug-print-function ${FUNCNAME} "$@"
391 local oldsha cursha repo_type
393 [[ ${EGIT_LOCAL_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
395 if [[ ! -d ${EGIT_DIR} ]]; then
397 pushd "${EGIT_DIR}" > /dev/null || die
398 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
399 echo "GIT NEW clone -->"
400 echo " repository: ${EGIT_REPO_URI_SELECTED}"
401 echo " at the commit: ${cursha}"
403 popd > /dev/null || die
404 elif [[ ${EVCS_OFFLINE} ]]; then
405 pushd "${EGIT_DIR}" > /dev/null || die
406 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
407 echo "GIT offline update -->"
408 echo " repository: $(git config remote.origin.url)"
409 echo " at the commit: ${cursha}"
410 popd > /dev/null || die
412 pushd "${EGIT_DIR}" > /dev/null || die
413 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
415 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
418 echo "GIT update -->"
419 echo " repository: ${EGIT_REPO_URI_SELECTED}"
420 # write out message based on the revisions
421 if [[ "${oldsha}" != "${cursha}" ]]; then
422 echo " updating from commit: ${oldsha}"
423 echo " to commit: ${cursha}"
425 echo " at the commit: ${cursha}"
428 # print nice statistic of what was changed
429 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
430 popd > /dev/null || die
432 # export the version the repository is at
433 export EGIT_VERSION="${cursha}"
435 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
436 && echo " commit: ${EGIT_COMMIT}"
437 echo " branch: ${EGIT_BRANCH}"
438 echo " storage directory: \"${EGIT_DIR}\""
439 echo " checkout type: ${repo_type}"
441 # Cleanup after git.eclass
442 if [[ ${EGIT_OLD_CLONE} ]]; then
443 einfo "${FUNCNAME}: removing old clone in ${EGIT_OLD_CLONE}."
444 rm -rf "${EGIT_OLD_CLONE}"
448 # @FUNCTION: git_bootstrap
451 # Internal function that runs bootstrap command on unpacked source.
453 debug-print-function ${FUNCNAME} "$@"
455 # @ECLASS-VARIABLE: EGIT_BOOTSTRAP
457 # Command to be executed after checkout and clone of the specified
459 # enviroment the package will fail if there is no update, thus in
460 # combination with --keep-going it would lead in not-updating
461 # pakcages that are up-to-date.
462 if [[ ${EGIT_BOOTSTRAP} ]]; then
463 pushd "${EGIT_SOURCEDIR}" > /dev/null || die
464 einfo "Starting bootstrap"
466 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
467 # we have file in the repo which we should execute
468 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
470 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
471 eval "./${EGIT_BOOTSTRAP}" \
472 || die "${FUNCNAME}: bootstrap script failed"
474 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
475 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
476 die "\"${EGIT_BOOTSTRAP}\" is not executable"
479 # we execute some system command
480 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
482 eval "${EGIT_BOOTSTRAP}" \
483 || die "${FUNCNAME}: bootstrap commands failed"
486 einfo "Bootstrap finished"
487 popd > /dev/null || die
491 # @FUNCTION: git-2_migrate_repository
494 # Internal function migrating between bare and normal checkout repository.
495 # This is based on usage of EGIT_SUBMODULES, at least until they
496 # start to work with bare checkouts sanely.
497 # This function also set some global variables that differ between
498 # bare and non-bare checkout.
499 git-2_migrate_repository() {
500 debug-print-function ${FUNCNAME} "$@"
502 local bare returnstate
504 # first find out if we have submodules
505 # or user explicitly wants us to use non-bare clones
506 if ! [[ ${EGIT_HAS_SUBMODULES} || ${EGIT_NONBARE} ]]; then
510 # test if we already have some repo and if so find out if we have
511 # to migrate the data
512 if [[ -d ${EGIT_DIR} ]]; then
513 if [[ ${bare} && -d ${EGIT_DIR}/.git ]]; then
514 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
516 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
517 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
518 export GIT_DIR="${EGIT_DIR}.bare"
519 git config core.bare true > /dev/null
523 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
525 elif [[ ! ${bare} && ! -d ${EGIT_DIR}/.git ]]; then
526 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
528 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
529 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
532 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
536 if [[ ${returnstate} -ne 0 ]]; then
537 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
539 # migration failed, remove the EGIT_DIR to play it safe
540 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
544 # set various options to work with both targets
545 if [[ ${bare} ]]; then
546 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
547 EGIT_LOCAL_OPTIONS+="${EGIT_OPTIONS} --bare"
548 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
549 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
550 UPSTREAM_BRANCH="${EGIT_BRANCH}"
553 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
554 MOVE_COMMAND="cp -pPR ."
555 EGIT_LOCAL_OPTIONS="${EGIT_OPTIONS}"
556 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
557 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
558 EGIT_LOCAL_NONBARE="true"
562 # @FUNCTION: git-2_cleanup
565 # Internal function cleaning up all the global variables
566 # that are not required after the unpack has been done.
568 debug-print-function ${FUNCNAME} "$@"
570 # Here we can unset only variables that are GLOBAL
571 # defined by the eclass, BUT NOT subject to change
572 # by user (like EGIT_PROJECT).
573 # If ebuild writer polutes his environment it is
577 unset EGIT_LOCAL_OPTIONS
578 unset EGIT_UPDATE_CMD
579 unset UPSTREAM_BRANCH
580 unset EGIT_LOCAL_NONBARE
583 # @FUNCTION: git-2_src_unpack
585 # Default git src_unpack function.
587 debug-print-function ${FUNCNAME} "$@"
590 git-2_prepare_storedir
591 git-2_migrate_repository
599 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
601 # Users can specify some SRC_URI and we should
602 # unpack the files too.
603 if [[ ! ${EGIT_NOUNPACK} ]]; then
604 if has ${EAPI:-0} 0 1; then
605 [[ ${A} ]] && unpack ${A}