1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: git-r3.eclass
6 # Michał Górny <mgorny@gentoo.org>
7 # @BLURB: Eclass for fetching and unpacking git repositories.
9 # Third generation eclass for easing maintenance of live ebuilds using
10 # git as remote repository.
16 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
20 if [[ ! ${_GIT_R3} ]]; then
26 EXPORT_FUNCTIONS src_unpack
28 if [[ ! ${_GIT_R3} ]]; then
30 if [[ ! ${_INHERITED_BY_GIT_2} ]]; then
31 DEPEND=">=dev-vcs/git-1.8.2.1"
34 # @ECLASS-VARIABLE: EGIT_CLONE_TYPE
36 # Type of clone that should be used against the remote repository.
37 # This can be either of: 'mirror', 'single', 'shallow'.
39 # This is intended to be set by user in make.conf. Ebuilds are supposed
40 # to set EGIT_MIN_CLONE_TYPE if necessary instead.
42 # The 'mirror' type clones all remote branches and tags with complete
43 # history and all notes. EGIT_COMMIT can specify any commit hash.
44 # Upstream-removed branches and tags are purged from the local clone
45 # while fetching. This mode is suitable for cloning the local copy
46 # for development or hosting a local git mirror. However, clones
47 # of repositories with large diverged branches may quickly grow large.
49 # The 'single+tags' type clones the requested branch and all tags
50 # in the repository. All notes are fetched as well. EGIT_COMMIT
51 # can safely specify hashes throughout the current branch and all tags.
52 # No purging of old references is done (if you often switch branches,
53 # you may need to remove stale branches yourself). This mode is intended
54 # mostly for use with broken git servers such as Google Code that fail
55 # to fetch tags along with the branch in 'single' mode.
57 # The 'single' type clones only the requested branch or tag. Tags
58 # referencing commits throughout the branch history are fetched as well,
59 # and all notes. EGIT_COMMIT can safely specify only hashes
60 # in the current branch. No purging of old references is done (if you
61 # often switch branches, you may need to remove stale branches
62 # yourself). This mode is suitable for general use.
64 # The 'shallow' type clones only the newest commit on requested branch
65 # or tag. EGIT_COMMIT can only specify tags, and since the history is
66 # unavailable calls like 'git describe' will not reference prior tags.
67 # No purging of old references is done. This mode is intended mostly for
68 # embedded systems with limited disk space.
69 : ${EGIT_CLONE_TYPE:=single}
71 # @ECLASS-VARIABLE: EGIT_MIN_CLONE_TYPE
73 # 'Minimum' clone type supported by the ebuild. Takes same values
74 # as EGIT_CLONE_TYPE. When user sets a type that's 'lower' (that is,
75 # later on the list) than EGIT_MIN_CLONE_TYPE, the eclass uses
76 # EGIT_MIN_CLONE_TYPE instead.
78 # This variable is intended to be used by ebuilds only. Users are
79 # supposed to set EGIT_CLONE_TYPE instead.
81 # A common case is to use 'single' whenever the build system requires
82 # access to full branch history, or 'single+tags' when Google Code
83 # or a similar remote is used that does not support shallow clones
84 # and fetching tags along with commits. Please use sparingly, and to fix
85 # fatal errors rather than 'non-pretty versions'.
86 : ${EGIT_MIN_CLONE_TYPE:=shallow}
88 # @ECLASS-VARIABLE: EGIT3_STORE_DIR
90 # Storage directory for git sources.
92 # This is intended to be set by user in make.conf. Ebuilds must not set
95 # EGIT3_STORE_DIR=${DISTDIR}/git3-src
97 # @ECLASS-VARIABLE: EGIT_MIRROR_URI
100 # 'Top' URI to a local git mirror. If specified, the eclass will try
101 # to fetch from the local mirror instead of using the remote repository.
103 # The mirror needs to follow EGIT3_STORE_DIR structure. The directory
104 # created by eclass can be used for that purpose.
108 # EGIT_MIRROR_URI="git://mirror.lan/"
111 # @ECLASS-VARIABLE: EGIT_REPO_URI
114 # URIs to the repository, e.g. git://foo, https://foo. If multiple URIs
115 # are provided, the eclass will consider them as fallback URIs to try
116 # if the first URI does not work. For supported URI syntaxes, read up
117 # the manpage for git-clone(1).
119 # It can be overriden via env using ${PN}_LIVE_REPO variable.
121 # Can be a whitespace-separated list or an array.
125 # EGIT_REPO_URI="git://a/b.git https://c/d.git"
128 # @ECLASS-VARIABLE: EVCS_OFFLINE
131 # If non-empty, this variable prevents any online operations.
133 # @ECLASS-VARIABLE: EVCS_UMASK
136 # Set this variable to a custom umask. This is intended to be set by
137 # users. By setting this to something like 002, it can make life easier
138 # for people who do development as non-root (but are in the portage
139 # group), and then switch over to building with FEATURES=userpriv.
140 # Or vice-versa. Shouldn't be a security issue here as anyone who has
141 # portage group write access already can screw the system over in more
144 # @ECLASS-VARIABLE: EGIT_BRANCH
147 # The branch name to check out. If unset, the upstream default (HEAD)
150 # It can be overriden via env using ${PN}_LIVE_BRANCH variable.
152 # @ECLASS-VARIABLE: EGIT_COMMIT
155 # The tag name or commit identifier to check out. If unset, newest
156 # commit from the branch will be used. Note that if set to a commit
157 # not on HEAD branch, EGIT_BRANCH needs to be set to a branch on which
158 # the commit is available.
160 # It can be overriden via env using ${PN}_LIVE_COMMIT variable.
162 # @ECLASS-VARIABLE: EGIT_COMMIT_DATE
165 # Attempt to check out the repository state for the specified timestamp.
166 # The date should be in format understood by 'git rev-list'. The commits
167 # on EGIT_BRANCH will be considered.
169 # The eclass will select the last commit with commit date preceding
170 # the specified date. When merge commits are found, only first parents
171 # will be considered in order to avoid switching into external branches
172 # (assuming that merges are done correctly). In other words, each merge
173 # will be considered alike a single commit with date corresponding
174 # to the merge commit date.
176 # It can be overriden via env using ${PN}_LIVE_COMMIT_DATE variable.
178 # @ECLASS-VARIABLE: EGIT_CHECKOUT_DIR
180 # The directory to check the git sources out to.
182 # EGIT_CHECKOUT_DIR=${WORKDIR}/${P}
184 # @ECLASS-VARIABLE: EGIT_SUBMODULES
187 # An array of inclusive and exclusive wildcards on submodule names,
188 # stating which submodules are fetched and checked out. Exclusions
189 # start with '-', and exclude previously matched submodules.
191 # If unset, all submodules are enabled. Empty list disables all
192 # submodules. In order to use an exclude-only list, start the array
195 # Remember that wildcards need to be quoted in order to prevent filename
200 # # Disable all submodules
203 # # Include only foo and bar
204 # EGIT_SUBMODULES=( foo bar )
206 # # Use all submodules except for test-* but include test-lib
207 # EGIT_SUBMODULES=( '*' '-test-*' test-lib )
210 # @FUNCTION: _git-r3_env_setup
213 # Set the eclass variables as necessary for operation. This can involve
214 # setting EGIT_* to defaults or ${PN}_LIVE_* variables.
215 _git-r3_env_setup() {
216 debug-print-function ${FUNCNAME} "$@"
218 # check the clone type
219 case "${EGIT_CLONE_TYPE}" in
220 mirror|single+tags|single|shallow)
223 die "Invalid EGIT_CLONE_TYPE=${EGIT_CLONE_TYPE}"
225 case "${EGIT_MIN_CLONE_TYPE}" in
229 if [[ ${EGIT_CLONE_TYPE} == shallow ]]; then
230 einfo "git-r3: ebuild needs to be cloned in '\e[1msingle\e[22m' mode, adjusting"
231 EGIT_CLONE_TYPE=single
235 if [[ ${EGIT_CLONE_TYPE} == shallow || ${EGIT_CLONE_TYPE} == single ]]; then
236 einfo "git-r3: ebuild needs to be cloned in '\e[1msingle+tags\e[22m' mode, adjusting"
237 EGIT_CLONE_TYPE=single+tags
241 if [[ ${EGIT_CLONE_TYPE} != mirror ]]; then
242 einfo "git-r3: ebuild needs to be cloned in '\e[1mmirror\e[22m' mode, adjusting"
243 EGIT_CLONE_TYPE=mirror
247 die "Invalid EGIT_MIN_CLONE_TYPE=${EGIT_MIN_CLONE_TYPE}"
250 if [[ ${EGIT_SUBMODULES[@]+1} && $(declare -p EGIT_SUBMODULES) != "declare -a"* ]]
252 die 'EGIT_SUBMODULES must be an array.'
257 [[ ${esc_pn} == [0-9]* ]] && esc_pn=_${esc_pn}
259 livevar=${esc_pn}_LIVE_REPO
260 EGIT_REPO_URI=${!livevar-${EGIT_REPO_URI}}
262 && ewarn "Using ${livevar}, no support will be provided"
264 livevar=${esc_pn}_LIVE_BRANCH
265 EGIT_BRANCH=${!livevar-${EGIT_BRANCH}}
267 && ewarn "Using ${livevar}, no support will be provided"
269 livevar=${esc_pn}_LIVE_COMMIT
270 EGIT_COMMIT=${!livevar-${EGIT_COMMIT}}
272 && ewarn "Using ${livevar}, no support will be provided"
274 livevar=${esc_pn}_LIVE_COMMIT_DATE
275 EGIT_COMMIT_DATE=${!livevar-${EGIT_COMMIT_DATE}}
277 && ewarn "Using ${livevar}, no support will be provided"
279 if [[ ${EGIT_COMMIT} && ${EGIT_COMMIT_DATE} ]]; then
280 die "EGIT_COMMIT and EGIT_COMMIT_DATE can not be specified simultaneously"
283 # Migration helpers. Remove them when git-2 is removed.
285 if [[ ${EGIT_SOURCEDIR} ]]; then
286 eerror "EGIT_SOURCEDIR has been replaced by EGIT_CHECKOUT_DIR. While updating"
287 eerror "your ebuild, please check whether the variable is necessary at all"
288 eerror "since the default has been changed from \${S} to \${WORKDIR}/\${P}."
289 eerror "Therefore, proper setting of S may be sufficient."
290 die "EGIT_SOURCEDIR has been replaced by EGIT_CHECKOUT_DIR."
293 if [[ ${EGIT_MASTER} ]]; then
294 eerror "EGIT_MASTER has been removed. Instead, the upstream default (HEAD)"
295 eerror "is used by the eclass. Please remove the assignment or use EGIT_BRANCH"
296 eerror "as necessary."
297 die "EGIT_MASTER has been removed."
300 if [[ ${EGIT_HAS_SUBMODULES} ]]; then
301 eerror "EGIT_HAS_SUBMODULES has been removed. The eclass no longer needs"
302 eerror "to switch the clone type in order to support submodules and therefore"
303 eerror "submodules are detected and fetched automatically. If you need to"
304 eerror "disable or filter submodules, see EGIT_SUBMODULES."
305 die "EGIT_HAS_SUBMODULES is no longer necessary."
308 if [[ ${EGIT_PROJECT} ]]; then
309 eerror "EGIT_PROJECT has been removed. Instead, the eclass determines"
310 eerror "the local clone path using path in canonical EGIT_REPO_URI."
311 eerror "If the current algorithm causes issues for you, please report a bug."
312 die "EGIT_PROJECT is no longer necessary."
315 if [[ ${EGIT_BOOTSTRAP} ]]; then
316 eerror "EGIT_BOOTSTRAP has been removed. Please create proper src_prepare()"
318 die "EGIT_BOOTSTRAP has been removed."
321 if [[ ${EGIT_NOUNPACK} ]]; then
322 eerror "EGIT_NOUNPACK has been removed. The eclass no longer calls default"
323 eerror "unpack function. If necessary, please declare proper src_unpack()."
324 die "EGIT_NOUNPACK has been removed."
328 # @FUNCTION: _git-r3_set_gitdir
332 # Obtain the local repository path and set it as GIT_DIR. Creates
333 # a new repository if necessary.
335 # <repo-uri> may be used to compose the path. It should therefore be
336 # a canonical URI to the repository.
337 _git-r3_set_gitdir() {
338 debug-print-function ${FUNCNAME} "$@"
340 local repo_name=${1#*://*/}
342 # strip the trailing slash
343 repo_name=${repo_name%/}
345 # strip common prefixes to make paths more likely to match
346 # e.g. git://X/Y.git vs https://X/git/Y.git
347 # (but just one of the prefixes)
348 case "${repo_name}" in
349 # gnome.org... who else?
350 browse/*) repo_name=${repo_name#browse/};;
351 # cgit can proxy requests to git
352 cgit/*) repo_name=${repo_name#cgit/};;
354 git/*) repo_name=${repo_name#git/};;
356 gitroot/*) repo_name=${repo_name#gitroot/};;
358 p/*) repo_name=${repo_name#p/};;
360 pub/scm/*) repo_name=${repo_name#pub/scm/};;
362 # ensure a .git suffix, same reason
363 repo_name=${repo_name%.git}.git
364 # now replace all the slashes
365 repo_name=${repo_name//\//_}
367 local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
368 : ${EGIT3_STORE_DIR:=${distdir}/git3-src}
370 GIT_DIR=${EGIT3_STORE_DIR}/${repo_name}
372 if [[ ! -d ${EGIT3_STORE_DIR} && ! ${EVCS_OFFLINE} ]]; then
375 mkdir -p "${EGIT3_STORE_DIR}"
376 ) || die "Unable to create ${EGIT3_STORE_DIR}"
379 addwrite "${EGIT3_STORE_DIR}"
380 if [[ ! -d ${GIT_DIR} ]]; then
381 if [[ ${EVCS_OFFLINE} ]]; then
382 eerror "A clone of the following repository is required to proceed:"
384 eerror "However, networking activity has been disabled using EVCS_OFFLINE and there"
385 eerror "is no local clone available."
386 die "No local clone of ${1}. Unable to proceed with EVCS_OFFLINE."
390 if [[ ${EVCS_UMASK} ]]; then
392 umask "${EVCS_UMASK}" || die "Bad options to umask: ${EVCS_UMASK}"
394 mkdir "${GIT_DIR}" || die
395 git init --bare || die
396 if [[ ${saved_umask} ]]; then
397 umask "${saved_umask}" || die
402 # @FUNCTION: _git-r3_set_submodules
403 # @USAGE: <file-contents>
406 # Parse .gitmodules contents passed as <file-contents>
407 # as in "$(cat .gitmodules)"). Composes a 'submodules' array that
408 # contains in order (name, URL, path) for each submodule.
409 _git-r3_set_submodules() {
410 debug-print-function ${FUNCNAME} "$@"
414 # ( name url path ... )
419 # submodule.<path>.path=<path>
420 # submodule.<path>.url=<url>
421 [[ ${l} == submodule.*.url=* ]] || continue
424 local subname=${l%%.url=*}
426 # filter out on EGIT_SUBMODULES
427 if declare -p EGIT_SUBMODULES &>/dev/null; then
429 for p in "${EGIT_SUBMODULES[@]}"; do
430 if [[ ${p} == -* ]]; then
437 [[ ${subname} == ${p} ]] && res=${l_res}
440 if [[ ! ${res} ]]; then
441 einfo "Skipping submodule \e[1m${subname}\e[22m"
446 # skip modules that have 'update = none', bug #487262.
447 local upd=$(echo "${data}" | git config -f /dev/fd/0 \
448 submodule."${subname}".update)
449 [[ ${upd} == none ]] && continue
451 # https://github.com/git/git/blob/master/refs.c#L31
452 # we are more restrictive than git itself but that should not
453 # cause any issues, #572312, #606950
454 # TODO: check escaped names for collisions
455 local enc_subname=${subname//[^a-zA-Z0-9-]/_}
459 "$(echo "${data}" | git config -f /dev/fd/0 \
460 submodule."${subname}".url || die)"
461 "$(echo "${data}" | git config -f /dev/fd/0 \
462 submodule."${subname}".path || die)"
464 done < <(echo "${data}" | git config -f /dev/fd/0 -l || die)
467 # @FUNCTION: _git-r3_set_subrepos
468 # @USAGE: <submodule-uri> <parent-repo-uri>...
471 # Create 'subrepos' array containing absolute (canonical) submodule URIs
472 # for the given <submodule-uri>. If the URI is relative, URIs will be
473 # constructed using all <parent-repo-uri>s. Otherwise, this single URI
474 # will be placed in the array.
475 _git-r3_set_subrepos() {
476 debug-print-function ${FUNCNAME} "$@"
479 subrepos=( "${@:2}" )
481 if [[ ${suburl} == ./* || ${suburl} == ../* ]]; then
482 # drop all possible trailing slashes for consistency
483 subrepos=( "${subrepos[@]%%/}" )
486 if [[ ${suburl} == ./* ]]; then
488 elif [[ ${suburl} == ../* ]]; then
491 # XXX: correctness checking
493 # drop the last path component
494 subrepos=( "${subrepos[@]%/*}" )
495 # and then the trailing slashes, again
496 subrepos=( "${subrepos[@]%%/}" )
502 # append the preprocessed path to the preprocessed URIs
503 subrepos=( "${subrepos[@]/%//${suburl}}")
505 subrepos=( "${suburl}" )
510 # @FUNCTION: _git-r3_is_local_repo
514 # Determine whether the given URI specifies a local (on-disk)
516 _git-r3_is_local_repo() {
517 debug-print-function ${FUNCNAME} "$@"
521 [[ ${uri} == file://* || ${uri} == /* ]]
524 # @FUNCTION: git-r3_fetch
525 # @USAGE: [<repo-uri> [<remote-ref> [<local-id> [<commit-date>]]]]
527 # Fetch new commits to the local clone of repository.
529 # <repo-uri> specifies the repository URIs to fetch from, as a space-
530 # -separated list. The first URI will be used as repository group
531 # identifier and therefore must be used consistently. When not
532 # specified, defaults to ${EGIT_REPO_URI}.
534 # <remote-ref> specifies the remote ref or commit id to fetch.
535 # It is preferred to use 'refs/heads/<branch-name>' for branches
536 # and 'refs/tags/<tag-name>' for tags. Other options are 'HEAD'
537 # for upstream default branch and hexadecimal commit SHA1. Defaults
538 # to the first of EGIT_COMMIT, EGIT_BRANCH or literal 'HEAD' that
539 # is set to a non-null value.
541 # <local-id> specifies the local branch identifier that will be used to
542 # locally store the fetch result. It should be unique to multiple
543 # fetches within the repository that can be performed at the same time
544 # (including parallel merges). It defaults to ${CATEGORY}/${PN}/${SLOT%/*}.
545 # This default should be fine unless you are fetching multiple trees
546 # from the same repository in the same ebuild.
548 # <commit-id> requests attempting to use repository state as of specific
549 # date. For more details, see EGIT_COMMIT_DATE.
551 # The fetch operation will affect the EGIT_STORE only. It will not touch
552 # the working copy, nor export any environment variables.
553 # If the repository contains submodules, they will be fetched
556 debug-print-function ${FUNCNAME} "$@"
561 elif [[ $(declare -p EGIT_REPO_URI) == "declare -a"* ]]; then
562 repos=( "${EGIT_REPO_URI[@]}" )
564 repos=( ${EGIT_REPO_URI} )
567 local branch=${EGIT_BRANCH:+refs/heads/${EGIT_BRANCH}}
568 local remote_ref=${2:-${EGIT_COMMIT:-${branch:-HEAD}}}
569 local local_id=${3:-${CATEGORY}/${PN}/${SLOT%/*}}
570 local local_ref=refs/git-r3/${local_id}/__main__
571 local commit_date=${4:-${EGIT_COMMIT_DATE}}
573 [[ ${repos[@]} ]] || die "No URI provided and EGIT_REPO_URI unset"
576 _git-r3_set_gitdir "${repos[0]}"
578 # prepend the local mirror if applicable
579 if [[ ${EGIT_MIRROR_URI} ]]; then
581 "${EGIT_MIRROR_URI%/}/${GIT_DIR##*/}"
586 # try to fetch from the remote
587 local r success saved_umask
588 if [[ ${EVCS_UMASK} ]]; then
590 umask "${EVCS_UMASK}" || die "Bad options to umask: ${EVCS_UMASK}"
592 for r in "${repos[@]}"; do
593 if [[ ! ${EVCS_OFFLINE} ]]; then
594 einfo "Fetching \e[1m${r}\e[22m ..."
596 local fetch_command=( git fetch "${r}" )
597 local clone_type=${EGIT_CLONE_TYPE}
599 if [[ ${r} == http://* || ${r} == https://* ]] &&
600 [[ ! ${EGIT_CURL_WARNED} ]] &&
601 ! ROOT=/ has_version 'dev-vcs/git[curl]'
603 ewarn "git-r3: fetching from HTTP(S) requested. In order to support HTTP(S),"
604 ewarn "dev-vcs/git needs to be built with USE=curl. Example solution:"
606 ewarn " echo dev-vcs/git curl >> /etc/portage/package.use"
607 ewarn " emerge -1v dev-vcs/git"
609 ewarn "HTTP(S) URIs will be skipped."
613 if [[ ${clone_type} == mirror ]]; then
616 # mirror the remote branches as local branches
617 "+refs/heads/*:refs/heads/*"
618 # pull tags explicitly in order to prune them properly
619 "+refs/tags/*:refs/tags/*"
620 # notes in case something needs them
621 "+refs/notes/*:refs/notes/*"
622 # and HEAD in case we need the default branch
623 # (we keep it in refs/git-r3 since otherwise --prune interferes)
624 "+HEAD:refs/git-r3/HEAD"
626 else # single or shallow
627 local fetch_l fetch_r
629 if [[ ${remote_ref} == HEAD ]]; then
632 elif [[ ${remote_ref} == refs/* ]]; then
633 # regular branch, tag or some other explicit ref
634 fetch_l=${remote_ref}
636 # tag or commit id...
637 # let ls-remote figure it out
638 local tagref=$(git ls-remote "${r}" "refs/tags/${remote_ref}")
640 # if it was a tag, ls-remote obtained a hash
641 if [[ ${tagref} ]]; then
643 fetch_l=refs/tags/${remote_ref}
646 # so we need to fetch the whole branch
647 if [[ ${branch} ]]; then
653 # fetching by commit in shallow mode? can't do.
654 if [[ ${clone_type} == shallow ]]; then
660 # checkout by date does not make sense in shallow mode
661 if [[ ${commit_date} && ${clone_type} == shallow ]]; then
665 if [[ ${fetch_l} == HEAD ]]; then
666 fetch_r=refs/git-r3/HEAD
672 "+${fetch_l}:${fetch_r}"
675 if [[ ${clone_type} == single+tags ]]; then
677 # pull tags explicitly as requested
678 "+refs/tags/*:refs/tags/*"
683 if [[ ${clone_type} == shallow ]]; then
684 if _git-r3_is_local_repo; then
685 # '--depth 1' causes sandbox violations with local repos
688 elif [[ ! $(git rev-parse --quiet --verify "${fetch_r}") ]]
690 # use '--depth 1' when fetching a new branch
691 fetch_command+=( --depth 1 )
693 else # non-shallow mode
694 if [[ -f ${GIT_DIR}/shallow ]]; then
695 fetch_command+=( --unshallow )
699 set -- "${fetch_command[@]}"
703 if [[ ${clone_type} == mirror || ${fetch_l} == HEAD ]]; then
704 # update our HEAD to match our remote HEAD ref
705 git symbolic-ref HEAD refs/git-r3/HEAD \
706 || die "Unable to update HEAD"
710 # now let's see what the user wants from us
711 if [[ ${commit_date} ]]; then
712 local dated_commit_id=$(
713 git rev-list --first-parent --before="${commit_date}" \
716 if [[ ${?} -ne 0 ]]; then
717 die "Listing ${remote_ref} failed (wrong ref?)."
718 elif [[ ! ${dated_commit_id} ]]; then
719 die "Unable to find commit for date ${commit_date}."
721 set -- git update-ref --no-deref "${local_ref}" "${dated_commit_id}"
724 local full_remote_ref=$(
725 git rev-parse --verify --symbolic-full-name "${remote_ref}"
728 if [[ ${full_remote_ref} ]]; then
729 # when we are given a ref, create a symbolic ref
730 # so that we preserve the actual argument
731 set -- git symbolic-ref "${local_ref}" "${full_remote_ref}"
733 # otherwise, we were likely given a commit id
734 set -- git update-ref --no-deref "${local_ref}" "${remote_ref}"
740 if [[ ${EVCS_OFFLINE} ]]; then
741 eerror "A clone of the following repository is required to proceed:"
743 eerror "However, networking activity has been disabled using EVCS_OFFLINE and the local"
744 eerror "clone does not have requested ref:"
745 eerror " ${remote_ref}"
746 die "Local clone of ${r} does not have requested ref: ${remote_ref}. Unable to proceed with EVCS_OFFLINE."
748 die "Referencing ${remote_ref} failed (wrong ref?)."
755 if [[ ${saved_umask} ]]; then
756 umask "${saved_umask}" || die
758 [[ ${success} ]] || die "Unable to fetch from any of EGIT_REPO_URI"
760 # submodules can reference commits in any branch
761 # always use the 'mirror' mode to accomodate that, bug #503332
762 local EGIT_CLONE_TYPE=mirror
764 # recursively fetch submodules
765 if git cat-file -e "${local_ref}":.gitmodules &>/dev/null; then
767 _git-r3_set_submodules \
768 "$(git cat-file -p "${local_ref}":.gitmodules || die)"
770 while [[ ${submodules[@]} ]]; do
771 local subname=${submodules[0]}
772 local url=${submodules[1]}
773 local path=${submodules[2]}
775 # use only submodules for which path does exist
776 # (this is in par with 'git submodule'), bug #551100
777 # note: git cat-file does not work for submodules
778 if [[ $(git ls-tree -d "${local_ref}" "${path}") ]]
780 local commit=$(git rev-parse "${local_ref}:${path}" || die)
782 if [[ ! ${commit} ]]; then
783 die "Unable to get commit id for submodule ${subname}"
787 _git-r3_set_subrepos "${url}" "${repos[@]}"
789 git-r3_fetch "${subrepos[*]}" "${commit}" "${local_id}/${subname}"
792 submodules=( "${submodules[@]:3}" ) # shift
797 # @FUNCTION: git-r3_checkout
798 # @USAGE: [<repo-uri> [<checkout-path> [<local-id>]]]
800 # Check the previously fetched tree to the working copy.
802 # <repo-uri> specifies the repository URIs, as a space-separated list.
803 # The first URI will be used as repository group identifier
804 # and therefore must be used consistently with git-r3_fetch.
805 # The remaining URIs are not used and therefore may be omitted.
806 # When not specified, defaults to ${EGIT_REPO_URI}.
808 # <checkout-path> specifies the path to place the checkout. It defaults
809 # to ${EGIT_CHECKOUT_DIR} if set, otherwise to ${WORKDIR}/${P}.
811 # <local-id> needs to specify the local identifier that was used
812 # for respective git-r3_fetch.
814 # The checkout operation will write to the working copy, and export
815 # the repository state into the environment. If the repository contains
816 # submodules, they will be checked out recursively.
818 debug-print-function ${FUNCNAME} "$@"
823 elif [[ $(declare -p EGIT_REPO_URI) == "declare -a"* ]]; then
824 repos=( "${EGIT_REPO_URI[@]}" )
826 repos=( ${EGIT_REPO_URI} )
829 local out_dir=${2:-${EGIT_CHECKOUT_DIR:-${WORKDIR}/${P}}}
830 local local_id=${3:-${CATEGORY}/${PN}/${SLOT%/*}}
833 _git-r3_set_gitdir "${repos[0]}"
835 einfo "Checking out \e[1m${repos[0]}\e[22m to \e[1m${out_dir}\e[22m ..."
837 if ! git cat-file -e refs/git-r3/"${local_id}"/__main__; then
838 die "Logic error: no local clone of ${repos[0]}. git-r3_fetch not used?"
841 git symbolic-ref --quiet refs/git-r3/"${local_id}"/__main__
843 local new_commit_id=$(
844 git rev-parse --verify refs/git-r3/"${local_id}"/__main__
847 git-r3_sub_checkout() {
848 local orig_repo=${GIT_DIR}
849 local -x GIT_DIR=${out_dir}/.git
850 local -x GIT_WORK_TREE=${out_dir}
852 mkdir -p "${out_dir}" || die
854 # use git init+fetch instead of clone since the latter doesn't like
855 # non-empty directories.
857 git init --quiet || die
858 # setup 'alternates' to avoid copying objects
859 echo "${orig_repo}/objects" > "${GIT_DIR}"/objects/info/alternates || die
861 cp -R "${orig_repo}"/refs/* "${GIT_DIR}"/refs/ || die
863 # (no need to copy HEAD, we will set it via checkout)
865 if [[ -f ${orig_repo}/shallow ]]; then
866 cp "${orig_repo}"/shallow "${GIT_DIR}"/ || die
869 set -- git checkout --quiet
870 if [[ ${remote_ref} ]]; then
871 set -- "${@}" "${remote_ref#refs/heads/}"
873 set -- "${@}" "${new_commit_id}"
876 "${@}" || die "git checkout ${remote_ref:-${new_commit_id}} failed"
879 unset -f git-r3_sub_checkout
881 local old_commit_id=$(
882 git rev-parse --quiet --verify refs/git-r3/"${local_id}"/__old__
884 if [[ ! ${old_commit_id} ]]; then
885 echo "GIT NEW branch -->"
886 echo " repository: ${repos[0]}"
887 echo " at the commit: ${new_commit_id}"
889 # diff against previous revision
890 echo "GIT update -->"
891 echo " repository: ${repos[0]}"
892 # write out message based on the revisions
893 if [[ "${old_commit_id}" != "${new_commit_id}" ]]; then
894 echo " updating from commit: ${old_commit_id}"
895 echo " to commit: ${new_commit_id}"
897 git --no-pager diff --stat \
898 ${old_commit_id}..${new_commit_id}
900 echo " at the commit: ${new_commit_id}"
903 git update-ref --no-deref refs/git-r3/"${local_id}"/{__old__,__main__} || die
905 # recursively checkout submodules
906 if [[ -f ${out_dir}/.gitmodules ]]; then
908 _git-r3_set_submodules \
909 "$(<"${out_dir}"/.gitmodules)"
911 while [[ ${submodules[@]} ]]; do
912 local subname=${submodules[0]}
913 local url=${submodules[1]}
914 local path=${submodules[2]}
916 # use only submodules for which path does exist
917 # (this is in par with 'git submodule'), bug #551100
918 if [[ -d ${out_dir}/${path} ]]; then
920 _git-r3_set_subrepos "${url}" "${repos[@]}"
922 git-r3_checkout "${subrepos[*]}" "${out_dir}/${path}" \
923 "${local_id}/${subname}"
926 submodules=( "${submodules[@]:3}" ) # shift
930 # keep this *after* submodules
931 export EGIT_DIR=${GIT_DIR}
932 export EGIT_VERSION=${new_commit_id}
935 # @FUNCTION: git-r3_peek_remote_ref
936 # @USAGE: [<repo-uri> [<remote-ref>]]
938 # Peek the reference in the remote repository and print the matching
939 # (newest) commit SHA1.
941 # <repo-uri> specifies the repository URIs to fetch from, as a space-
942 # -separated list. When not specified, defaults to ${EGIT_REPO_URI}.
944 # <remote-ref> specifies the remote ref to peek. It is preferred to use
945 # 'refs/heads/<branch-name>' for branches and 'refs/tags/<tag-name>'
946 # for tags. Alternatively, 'HEAD' may be used for upstream default
947 # branch. Defaults to the first of EGIT_COMMIT, EGIT_BRANCH or literal
948 # 'HEAD' that is set to a non-null value.
950 # The operation will be done purely on the remote, without using local
951 # storage. If commit SHA1 is provided as <remote-ref>, the function will
952 # fail due to limitations of git protocol.
954 # On success, the function returns 0 and writes hexadecimal commit SHA1
955 # to stdout. On failure, the function returns 1.
956 git-r3_peek_remote_ref() {
957 debug-print-function ${FUNCNAME} "$@"
962 elif [[ $(declare -p EGIT_REPO_URI) == "declare -a"* ]]; then
963 repos=( "${EGIT_REPO_URI[@]}" )
965 repos=( ${EGIT_REPO_URI} )
968 local branch=${EGIT_BRANCH:+refs/heads/${EGIT_BRANCH}}
969 local remote_ref=${2:-${EGIT_COMMIT:-${branch:-HEAD}}}
971 [[ ${repos[@]} ]] || die "No URI provided and EGIT_REPO_URI unset"
974 for r in "${repos[@]}"; do
975 einfo "Peeking \e[1m${remote_ref}\e[22m on \e[1m${r}\e[22m ..." >&2
978 if [[ ${remote_ref} == refs/* || ${remote_ref} == HEAD ]]
980 lookup_ref=${remote_ref}
982 # ls-remote by commit is going to fail anyway,
983 # so we may as well pass refs/tags/ABCDEF...
984 lookup_ref=refs/tags/${remote_ref}
987 # split on whitespace
989 $(git ls-remote "${r}" "${lookup_ref}")
992 if [[ ${ref[0]} ]]; then
1001 git-r3_src_fetch() {
1002 debug-print-function ${FUNCNAME} "$@"
1004 if [[ ! ${EGIT3_STORE_DIR} && ${EGIT_STORE_DIR} ]]; then
1005 ewarn "You have set EGIT_STORE_DIR but not EGIT3_STORE_DIR. Please consider"
1006 ewarn "setting EGIT3_STORE_DIR for git-r3.eclass. It is recommended to use"
1007 ewarn "a different directory than EGIT_STORE_DIR to ease removing old clones"
1008 ewarn "when git-2 eclass becomes deprecated."
1015 git-r3_src_unpack() {
1016 debug-print-function ${FUNCNAME} "$@"
1023 # https://bugs.gentoo.org/show_bug.cgi?id=482666
1024 git-r3_pkg_needrebuild() {
1025 debug-print-function ${FUNCNAME} "$@"
1027 local new_commit_id=$(git-r3_peek_remote_ref)
1028 [[ ${new_commit_id} && ${EGIT_VERSION} ]] || die "Lookup failed"
1030 if [[ ${EGIT_VERSION} != ${new_commit_id} ]]; then
1031 einfo "Update from \e[1m${EGIT_VERSION}\e[22m to \e[1m${new_commit_id}\e[22m"
1033 einfo "Local and remote at \e[1m${EGIT_VERSION}\e[22m"
1036 [[ ${EGIT_VERSION} != ${new_commit_id} ]]
1039 # 'export' locally until this gets into EAPI
1040 pkg_needrebuild() { git-r3_pkg_needrebuild; }