dev-qt/qttest: stable 5.14.2 for ppc, bug #719732
[gentoo.git] / eclass / git-r3.eclass
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: git-r3.eclass
5 # @MAINTAINER:
6 # Michał Górny <mgorny@gentoo.org>
7 # @SUPPORTED_EAPIS: 4 5 6 7
8 # @BLURB: Eclass for fetching and unpacking git repositories.
9 # @DESCRIPTION:
10 # Third generation eclass for easing maintenance of live ebuilds using
11 # git as remote repository.
12
13 case "${EAPI:-0}" in
14         0|1|2|3)
15                 die "Unsupported EAPI=${EAPI} (obsolete) for ${ECLASS}"
16                 ;;
17         4|5|6|7)
18                 ;;
19         *)
20                 die "Unsupported EAPI=${EAPI} (unknown) for ${ECLASS}"
21                 ;;
22 esac
23
24 EXPORT_FUNCTIONS src_unpack
25
26 if [[ ! ${_GIT_R3} ]]; then
27
28 PROPERTIES+=" live"
29
30 if [[ ! ${_INHERITED_BY_GIT_2} ]]; then
31         if [[ ${EAPI:-0} != [0123456] ]]; then
32                 BDEPEND=">=dev-vcs/git-1.8.2.1[curl]"
33         else
34                 DEPEND=">=dev-vcs/git-1.8.2.1[curl]"
35         fi
36 fi
37
38 # @ECLASS-VARIABLE: EGIT_CLONE_TYPE
39 # @DESCRIPTION:
40 # Type of clone that should be used against the remote repository.
41 # This can be either of: 'mirror', 'single', 'shallow'.
42 #
43 # This is intended to be set by user in make.conf. Ebuilds are supposed
44 # to set EGIT_MIN_CLONE_TYPE if necessary instead.
45 #
46 # The 'mirror' type clones all remote branches and tags with complete
47 # history and all notes. EGIT_COMMIT can specify any commit hash.
48 # Upstream-removed branches and tags are purged from the local clone
49 # while fetching. This mode is suitable for cloning the local copy
50 # for development or hosting a local git mirror. However, clones
51 # of repositories with large diverged branches may quickly grow large.
52 #
53 # The 'single+tags' type clones the requested branch and all tags
54 # in the repository. All notes are fetched as well. EGIT_COMMIT
55 # can safely specify hashes throughout the current branch and all tags.
56 # No purging of old references is done (if you often switch branches,
57 # you may need to remove stale branches yourself). This mode is intended
58 # mostly for use with broken git servers such as Google Code that fail
59 # to fetch tags along with the branch in 'single' mode.
60 #
61 # The 'single' type clones only the requested branch or tag. Tags
62 # referencing commits throughout the branch history are fetched as well,
63 # and all notes. EGIT_COMMIT can safely specify only hashes
64 # in the current branch. No purging of old references is done (if you
65 # often switch branches, you may need to remove stale branches
66 # yourself). This mode is suitable for general use.
67 #
68 # The 'shallow' type clones only the newest commit on requested branch
69 # or tag. EGIT_COMMIT can only specify tags, and since the history is
70 # unavailable calls like 'git describe' will not reference prior tags.
71 # No purging of old references is done. This mode is intended mostly for
72 # embedded systems with limited disk space.
73 : ${EGIT_CLONE_TYPE:=single}
74
75 # @ECLASS-VARIABLE: EGIT_MIN_CLONE_TYPE
76 # @DESCRIPTION:
77 # 'Minimum' clone type supported by the ebuild. Takes same values
78 # as EGIT_CLONE_TYPE. When user sets a type that's 'lower' (that is,
79 # later on the list) than EGIT_MIN_CLONE_TYPE, the eclass uses
80 # EGIT_MIN_CLONE_TYPE instead.
81 #
82 # This variable is intended to be used by ebuilds only. Users are
83 # supposed to set EGIT_CLONE_TYPE instead.
84 #
85 # A common case is to use 'single' whenever the build system requires
86 # access to full branch history, or 'single+tags' when Google Code
87 # or a similar remote is used that does not support shallow clones
88 # and fetching tags along with commits. Please use sparingly, and to fix
89 # fatal errors rather than 'non-pretty versions'.
90 : ${EGIT_MIN_CLONE_TYPE:=shallow}
91
92 # @ECLASS-VARIABLE: EGIT3_STORE_DIR
93 # @DESCRIPTION:
94 # Storage directory for git sources.
95 #
96 # This is intended to be set by user in make.conf. Ebuilds must not set
97 # it.
98 #
99 # EGIT3_STORE_DIR=${DISTDIR}/git3-src
100
101 # @ECLASS-VARIABLE: EGIT_MIRROR_URI
102 # @DEFAULT_UNSET
103 # @DESCRIPTION:
104 # 'Top' URI to a local git mirror. If specified, the eclass will try
105 # to fetch from the local mirror instead of using the remote repository.
106 #
107 # The mirror needs to follow EGIT3_STORE_DIR structure. The directory
108 # created by eclass can be used for that purpose.
109 #
110 # Example:
111 # @CODE
112 # EGIT_MIRROR_URI="git://mirror.lan/"
113 # @CODE
114
115 # @ECLASS-VARIABLE: EGIT_REPO_URI
116 # @REQUIRED
117 # @DESCRIPTION:
118 # URIs to the repository, e.g. https://foo. If multiple URIs are
119 # provided, the eclass will consider the remaining URIs as fallbacks
120 # to try if the first URI does not work. For supported URI syntaxes,
121 # read the manpage for git-clone(1).
122 #
123 # URIs should be using https:// whenever possible. http:// and git://
124 # URIs are completely unsecured and their use (even if only as
125 # a fallback) renders the ebuild completely vulnerable to MITM attacks.
126 #
127 # Can be a whitespace-separated list or an array.
128 #
129 # Example:
130 # @CODE
131 # EGIT_REPO_URI="https://a/b.git https://c/d.git"
132 # @CODE
133
134 # @ECLASS-VARIABLE: EVCS_OFFLINE
135 # @DEFAULT_UNSET
136 # @DESCRIPTION:
137 # If non-empty, this variable prevents any online operations.
138
139 # @ECLASS-VARIABLE: EVCS_UMASK
140 # @DEFAULT_UNSET
141 # @DESCRIPTION:
142 # Set this variable to a custom umask. This is intended to be set by
143 # users. By setting this to something like 002, it can make life easier
144 # for people who do development as non-root (but are in the portage
145 # group), and then switch over to building with FEATURES=userpriv.
146 # Or vice-versa. Shouldn't be a security issue here as anyone who has
147 # portage group write access already can screw the system over in more
148 # creative ways.
149
150 # @ECLASS-VARIABLE: EGIT_BRANCH
151 # @DEFAULT_UNSET
152 # @DESCRIPTION:
153 # The branch name to check out. If unset, the upstream default (HEAD)
154 # will be used.
155
156 # @ECLASS-VARIABLE: EGIT_COMMIT
157 # @DEFAULT_UNSET
158 # @DESCRIPTION:
159 # The tag name or commit identifier to check out. If unset, newest
160 # commit from the branch will be used. Note that if set to a commit
161 # not on HEAD branch, EGIT_BRANCH needs to be set to a branch on which
162 # the commit is available.
163
164 # @ECLASS-VARIABLE: EGIT_COMMIT_DATE
165 # @DEFAULT_UNSET
166 # @DESCRIPTION:
167 # Attempt to check out the repository state for the specified timestamp.
168 # The date should be in format understood by 'git rev-list'. The commits
169 # on EGIT_BRANCH will be considered.
170 #
171 # The eclass will select the last commit with commit date preceding
172 # the specified date. When merge commits are found, only first parents
173 # will be considered in order to avoid switching into external branches
174 # (assuming that merges are done correctly). In other words, each merge
175 # will be considered alike a single commit with date corresponding
176 # to the merge commit date.
177
178 # @ECLASS-VARIABLE: EGIT_CHECKOUT_DIR
179 # @DESCRIPTION:
180 # The directory to check the git sources out to.
181 #
182 # EGIT_CHECKOUT_DIR=${WORKDIR}/${P}
183
184 # @ECLASS-VARIABLE: EGIT_SUBMODULES
185 # @DEFAULT_UNSET
186 # @DESCRIPTION:
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.
190 #
191 # If unset, all submodules are enabled. Empty list disables all
192 # submodules. In order to use an exclude-only list, start the array
193 # with '*'.
194 #
195 # Remember that wildcards need to be quoted in order to prevent filename
196 # expansion.
197 #
198 # Examples:
199 # @CODE
200 # # Disable all submodules
201 # EGIT_SUBMODULES=()
202 #
203 # # Include only foo and bar
204 # EGIT_SUBMODULES=( foo bar )
205 #
206 # # Use all submodules except for test-* but include test-lib
207 # EGIT_SUBMODULES=( '*' '-test-*' test-lib )
208 # @CODE
209
210 # @FUNCTION: _git-r3_env_setup
211 # @INTERNAL
212 # @DESCRIPTION:
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} "$@"
217
218         # check the clone type
219         case "${EGIT_CLONE_TYPE}" in
220                 mirror|single+tags|single|shallow)
221                         ;;
222                 *)
223                         die "Invalid EGIT_CLONE_TYPE=${EGIT_CLONE_TYPE}"
224         esac
225         case "${EGIT_MIN_CLONE_TYPE}" in
226                 shallow)
227                         ;;
228                 single)
229                         if [[ ${EGIT_CLONE_TYPE} == shallow ]]; then
230                                 einfo "git-r3: ebuild needs to be cloned in 'single' mode, adjusting"
231                                 EGIT_CLONE_TYPE=single
232                         fi
233                         ;;
234                 single+tags)
235                         if [[ ${EGIT_CLONE_TYPE} == shallow || ${EGIT_CLONE_TYPE} == single ]]; then
236                                 einfo "git-r3: ebuild needs to be cloned in 'single+tags' mode, adjusting"
237                                 EGIT_CLONE_TYPE=single+tags
238                         fi
239                         ;;
240                 mirror)
241                         if [[ ${EGIT_CLONE_TYPE} != mirror ]]; then
242                                 einfo "git-r3: ebuild needs to be cloned in 'mirror' mode, adjusting"
243                                 EGIT_CLONE_TYPE=mirror
244                         fi
245                         ;;
246                 *)
247                         die "Invalid EGIT_MIN_CLONE_TYPE=${EGIT_MIN_CLONE_TYPE}"
248         esac
249
250         if [[ ${EGIT_SUBMODULES[@]+1} && $(declare -p EGIT_SUBMODULES) != "declare -a"* ]]
251         then
252                 die 'EGIT_SUBMODULES must be an array.'
253         fi
254
255         local esc_pn livevar
256         esc_pn=${PN//[-+]/_}
257         [[ ${esc_pn} == [0-9]* ]] && esc_pn=_${esc_pn}
258
259         # note: deprecated, use EGIT_OVERRIDE_* instead
260         livevar=${esc_pn}_LIVE_REPO
261         EGIT_REPO_URI=${!livevar-${EGIT_REPO_URI}}
262         [[ ${!livevar} ]] \
263                 && ewarn "Using ${livevar}, no support will be provided"
264
265         livevar=${esc_pn}_LIVE_BRANCH
266         EGIT_BRANCH=${!livevar-${EGIT_BRANCH}}
267         [[ ${!livevar} ]] \
268                 && ewarn "Using ${livevar}, no support will be provided"
269
270         livevar=${esc_pn}_LIVE_COMMIT
271         EGIT_COMMIT=${!livevar-${EGIT_COMMIT}}
272         [[ ${!livevar} ]] \
273                 && ewarn "Using ${livevar}, no support will be provided"
274
275         livevar=${esc_pn}_LIVE_COMMIT_DATE
276         EGIT_COMMIT_DATE=${!livevar-${EGIT_COMMIT_DATE}}
277         [[ ${!livevar} ]] \
278                 && ewarn "Using ${livevar}, no support will be provided"
279
280         if [[ ${EGIT_COMMIT} && ${EGIT_COMMIT_DATE} ]]; then
281                 die "EGIT_COMMIT and EGIT_COMMIT_DATE can not be specified simultaneously"
282         fi
283
284         # Migration helpers. Remove them when git-2 is removed.
285
286         if [[ ${EGIT_SOURCEDIR} ]]; then
287                 eerror "EGIT_SOURCEDIR has been replaced by EGIT_CHECKOUT_DIR. While updating"
288                 eerror "your ebuild, please check whether the variable is necessary at all"
289                 eerror "since the default has been changed from \${S} to \${WORKDIR}/\${P}."
290                 eerror "Therefore, proper setting of S may be sufficient."
291                 die "EGIT_SOURCEDIR has been replaced by EGIT_CHECKOUT_DIR."
292         fi
293
294         if [[ ${EGIT_MASTER} ]]; then
295                 eerror "EGIT_MASTER has been removed. Instead, the upstream default (HEAD)"
296                 eerror "is used by the eclass. Please remove the assignment or use EGIT_BRANCH"
297                 eerror "as necessary."
298                 die "EGIT_MASTER has been removed."
299         fi
300
301         if [[ ${EGIT_HAS_SUBMODULES} ]]; then
302                 eerror "EGIT_HAS_SUBMODULES has been removed. The eclass no longer needs"
303                 eerror "to switch the clone type in order to support submodules and therefore"
304                 eerror "submodules are detected and fetched automatically. If you need to"
305                 eerror "disable or filter submodules, see EGIT_SUBMODULES."
306                 die "EGIT_HAS_SUBMODULES is no longer necessary."
307         fi
308
309         if [[ ${EGIT_PROJECT} ]]; then
310                 eerror "EGIT_PROJECT has been removed. Instead, the eclass determines"
311                 eerror "the local clone path using path in canonical EGIT_REPO_URI."
312                 eerror "If the current algorithm causes issues for you, please report a bug."
313                 die "EGIT_PROJECT is no longer necessary."
314         fi
315
316         if [[ ${EGIT_BOOTSTRAP} ]]; then
317                 eerror "EGIT_BOOTSTRAP has been removed. Please create proper src_prepare()"
318                 eerror "instead."
319                 die "EGIT_BOOTSTRAP has been removed."
320         fi
321
322         if [[ ${EGIT_NOUNPACK} ]]; then
323                 eerror "EGIT_NOUNPACK has been removed. The eclass no longer calls default"
324                 eerror "unpack function. If necessary, please declare proper src_unpack()."
325                 die "EGIT_NOUNPACK has been removed."
326         fi
327 }
328
329 # @FUNCTION: _git-r3_set_gitdir
330 # @USAGE: <repo-uri>
331 # @INTERNAL
332 # @DESCRIPTION:
333 # Obtain the local repository path and set it as GIT_DIR. Creates
334 # a new repository if necessary.
335 #
336 # <repo-uri> may be used to compose the path. It should therefore be
337 # a canonical URI to the repository.
338 _git-r3_set_gitdir() {
339         debug-print-function ${FUNCNAME} "$@"
340
341         local repo_name=${1#*://*/}
342
343         # strip the trailing slash
344         repo_name=${repo_name%/}
345
346         # strip common prefixes to make paths more likely to match
347         # e.g. git://X/Y.git vs https://X/git/Y.git
348         # (but just one of the prefixes)
349         case "${repo_name}" in
350                 # gnome.org... who else?
351                 browse/*) repo_name=${repo_name#browse/};;
352                 # cgit can proxy requests to git
353                 cgit/*) repo_name=${repo_name#cgit/};;
354                 # pretty common
355                 git/*) repo_name=${repo_name#git/};;
356                 # gentoo.org
357                 gitroot/*) repo_name=${repo_name#gitroot/};;
358                 # sourceforge
359                 p/*) repo_name=${repo_name#p/};;
360                 # kernel.org
361                 pub/scm/*) repo_name=${repo_name#pub/scm/};;
362         esac
363         # ensure a .git suffix, same reason
364         repo_name=${repo_name%.git}.git
365         # now replace all the slashes
366         repo_name=${repo_name//\//_}
367
368         local distdir=${PORTAGE_ACTUAL_DISTDIR:-${DISTDIR}}
369         : ${EGIT3_STORE_DIR:=${distdir}/git3-src}
370
371         GIT_DIR=${EGIT3_STORE_DIR}/${repo_name}
372
373         if [[ ! -d ${EGIT3_STORE_DIR} && ! ${EVCS_OFFLINE} ]]; then
374                 (
375                         addwrite /
376                         mkdir -p "${EGIT3_STORE_DIR}"
377                 ) || die "Unable to create ${EGIT3_STORE_DIR}"
378         fi
379
380         addwrite "${EGIT3_STORE_DIR}"
381         if [[ ! -d ${GIT_DIR} ]]; then
382                 if [[ ${EVCS_OFFLINE} ]]; then
383                         eerror "A clone of the following repository is required to proceed:"
384                         eerror "  ${1}"
385                         eerror "However, networking activity has been disabled using EVCS_OFFLINE and there"
386                         eerror "is no local clone available."
387                         die "No local clone of ${1}. Unable to proceed with EVCS_OFFLINE."
388                 fi
389
390                 local saved_umask
391                 if [[ ${EVCS_UMASK} ]]; then
392                         saved_umask=$(umask)
393                         umask "${EVCS_UMASK}" || die "Bad options to umask: ${EVCS_UMASK}"
394                 fi
395                 mkdir "${GIT_DIR}" || die
396                 git init --bare || die
397                 if [[ ${saved_umask} ]]; then
398                         umask "${saved_umask}" || die
399                 fi
400         fi
401 }
402
403 # @FUNCTION: _git-r3_set_submodules
404 # @USAGE: <parent-path> <file-contents>
405 # @INTERNAL
406 # @DESCRIPTION:
407 # Parse .gitmodules contents passed as <file-contents>
408 # as in "$(cat .gitmodules)"). Composes a 'submodules' array that
409 # contains in order (name, URL, path) for each submodule.
410 #
411 # <parent-path> specifies path to current submodule (empty if top repo),
412 # and is used to support recursively specifying submodules.  The path
413 # must include a trailing slash if it's not empty.
414 _git-r3_set_submodules() {
415         debug-print-function ${FUNCNAME} "$@"
416
417         local parent_path=${1}
418         local data=${2}
419         [[ -z ${parent_path} || ${parent_path} == */ ]] || die
420
421         # ( name url path ... )
422         submodules=()
423
424         local l
425         while read l; do
426                 # submodule.<path>.path=<path>
427                 # submodule.<path>.url=<url>
428                 [[ ${l} == submodule.*.url=* ]] || continue
429
430                 l=${l#submodule.}
431                 local subname=${l%%.url=*}
432
433                 # filter out on EGIT_SUBMODULES
434                 if declare -p EGIT_SUBMODULES &>/dev/null; then
435                         local p l_res res=
436                         for p in "${EGIT_SUBMODULES[@]}"; do
437                                 if [[ ${p} == -* ]]; then
438                                         p=${p#-}
439                                         l_res=
440                                 else
441                                         l_res=1
442                                 fi
443
444                                 [[ ${parent_path}${subname} == ${p} ]] && res=${l_res}
445                         done
446
447                         if [[ ! ${res} ]]; then
448                                 einfo "Skipping submodule ${parent_path}${subname}"
449                                 continue
450                         else
451                                 einfo "Using submodule ${parent_path}${subname}"
452                         fi
453                 fi
454
455                 # skip modules that have 'update = none', bug #487262.
456                 local upd=$(echo "${data}" | git config -f /dev/fd/0 \
457                         submodule."${subname}".update)
458                 [[ ${upd} == none ]] && continue
459
460                 # https://github.com/git/git/blob/master/refs.c#L31
461                 # we are more restrictive than git itself but that should not
462                 # cause any issues, #572312, #606950
463                 # TODO: check escaped names for collisions
464                 local enc_subname=${subname//[^a-zA-Z0-9-]/_}
465
466                 submodules+=(
467                         "${enc_subname}"
468                         "$(echo "${data}" | git config -f /dev/fd/0 \
469                                 submodule."${subname}".url || die)"
470                         "$(echo "${data}" | git config -f /dev/fd/0 \
471                                 submodule."${subname}".path || die)"
472                 )
473         done < <(echo "${data}" | git config -f /dev/fd/0 -l || die)
474 }
475
476 # @FUNCTION: _git-r3_set_subrepos
477 # @USAGE: <submodule-uri> <parent-repo-uri>...
478 # @INTERNAL
479 # @DESCRIPTION:
480 # Create 'subrepos' array containing absolute (canonical) submodule URIs
481 # for the given <submodule-uri>. If the URI is relative, URIs will be
482 # constructed using all <parent-repo-uri>s. Otherwise, this single URI
483 # will be placed in the array.
484 _git-r3_set_subrepos() {
485         debug-print-function ${FUNCNAME} "$@"
486
487         local suburl=${1}
488         subrepos=( "${@:2}" )
489
490         if [[ ${suburl} == ./* || ${suburl} == ../* ]]; then
491                 # drop all possible trailing slashes for consistency
492                 subrepos=( "${subrepos[@]%%/}" )
493
494                 while true; do
495                         if [[ ${suburl} == ./* ]]; then
496                                 suburl=${suburl:2}
497                         elif [[ ${suburl} == ../* ]]; then
498                                 suburl=${suburl:3}
499
500                                 # XXX: correctness checking
501
502                                 # drop the last path component
503                                 subrepos=( "${subrepos[@]%/*}" )
504                                 # and then the trailing slashes, again
505                                 subrepos=( "${subrepos[@]%%/}" )
506                         else
507                                 break
508                         fi
509                 done
510
511                 # append the preprocessed path to the preprocessed URIs
512                 subrepos=( "${subrepos[@]/%//${suburl}}")
513         else
514                 subrepos=( "${suburl}" )
515         fi
516 }
517
518
519 # @FUNCTION: _git-r3_is_local_repo
520 # @USAGE: <repo-uri>
521 # @INTERNAL
522 # @DESCRIPTION:
523 # Determine whether the given URI specifies a local (on-disk)
524 # repository.
525 _git-r3_is_local_repo() {
526         debug-print-function ${FUNCNAME} "$@"
527
528         local uri=${1}
529
530         [[ ${uri} == file://* || ${uri} == /* ]]
531 }
532
533 # @FUNCTION: git-r3_fetch
534 # @USAGE: [<repo-uri> [<remote-ref> [<local-id> [<commit-date>]]]]
535 # @DESCRIPTION:
536 # Fetch new commits to the local clone of repository.
537 #
538 # <repo-uri> specifies the repository URIs to fetch from, as a space-
539 # -separated list. The first URI will be used as repository group
540 # identifier and therefore must be used consistently. When not
541 # specified, defaults to ${EGIT_REPO_URI}.
542 #
543 # <remote-ref> specifies the remote ref or commit id to fetch.
544 # It is preferred to use 'refs/heads/<branch-name>' for branches
545 # and 'refs/tags/<tag-name>' for tags. Other options are 'HEAD'
546 # for upstream default branch and hexadecimal commit SHA1. Defaults
547 # to the first of EGIT_COMMIT, EGIT_BRANCH or literal 'HEAD' that
548 # is set to a non-null value.
549 #
550 # <local-id> specifies the local branch identifier that will be used to
551 # locally store the fetch result. It should be unique to multiple
552 # fetches within the repository that can be performed at the same time
553 # (including parallel merges). It defaults to ${CATEGORY}/${PN}/${SLOT%/*}.
554 # This default should be fine unless you are fetching multiple trees
555 # from the same repository in the same ebuild.
556 #
557 # <commit-date> requests attempting to use repository state as of specific
558 # date. For more details, see EGIT_COMMIT_DATE.
559 #
560 # The fetch operation will affect the EGIT_STORE only. It will not touch
561 # the working copy, nor export any environment variables.
562 # If the repository contains submodules, they will be fetched
563 # recursively.
564 git-r3_fetch() {
565         debug-print-function ${FUNCNAME} "$@"
566
567         # disable password prompts, https://bugs.gentoo.org/701276
568         local -x GIT_TERMINAL_PROMPT=0
569
570         # process repos first since we create repo_name from it
571         local repos
572         if [[ ${1} ]]; then
573                 repos=( ${1} )
574         elif [[ $(declare -p EGIT_REPO_URI) == "declare -a"* ]]; then
575                 repos=( "${EGIT_REPO_URI[@]}" )
576         else
577                 repos=( ${EGIT_REPO_URI} )
578         fi
579
580         [[ ${repos[@]} ]] || die "No URI provided and EGIT_REPO_URI unset"
581
582         local r
583         for r in "${repos[@]}"; do
584                 if [[ ${r} == git:* || ${r} == http:* ]]; then
585                         ewarn "git-r3: ${r%%:*} protocol is completely unsecure and may render the ebuild"
586                         ewarn "easily susceptible to MITM attacks (even if used only as fallback). Please"
587                         ewarn "use https instead."
588                         ewarn "[URI: ${r}]"
589                 fi
590         done
591
592         local -x GIT_DIR
593         _git-r3_set_gitdir "${repos[0]}"
594
595         einfo "Repository id: ${GIT_DIR##*/}"
596
597         # prepend the local mirror if applicable
598         if [[ ${EGIT_MIRROR_URI} ]]; then
599                 repos=(
600                         "${EGIT_MIRROR_URI%/}/${GIT_DIR##*/}"
601                         "${repos[@]}"
602                 )
603         fi
604
605         # get the default values for the common variables and override them
606         local branch_name=${EGIT_BRANCH}
607         local commit_id=${2:-${EGIT_COMMIT}}
608         local commit_date=${4:-${EGIT_COMMIT_DATE}}
609
610         # support new override API for EAPI 6+
611         if ! has "${EAPI:-0}" 0 1 2 3 4 5; then
612                 # get the name and do some more processing:
613                 # 1) kill .git suffix,
614                 # 2) underscore (remaining) non-variable characters,
615                 # 3) add preceding underscore if it starts with a digit,
616                 # 4) uppercase.
617                 local override_name=${GIT_DIR##*/}
618                 override_name=${override_name%.git}
619                 override_name=${override_name//[^a-zA-Z0-9_]/_}
620                 override_name=${override_name^^}
621
622                 local varmap=(
623                         REPO:repos
624                         BRANCH:branch_name
625                         COMMIT:commit_id
626                         COMMIT_DATE:commit_date
627                 )
628
629                 local localvar livevar live_warn= override_vars=()
630                 for localvar in "${varmap[@]}"; do
631                         livevar=EGIT_OVERRIDE_${localvar%:*}_${override_name}
632                         localvar=${localvar#*:}
633                         override_vars+=( "${livevar}" )
634
635                         if [[ -n ${!livevar} ]]; then
636                                 [[ ${localvar} == repos ]] && repos=()
637                                 live_warn=1
638                                 ewarn "Using ${livevar}=${!livevar}"
639                                 declare "${localvar}=${!livevar}"
640                         fi
641                 done
642
643                 if [[ ${live_warn} ]]; then
644                         ewarn "No support will be provided."
645                 else
646                         einfo "To override fetched repository properties, use:"
647                         local x
648                         for x in "${override_vars[@]}"; do
649                                 einfo "  ${x}"
650                         done
651                         einfo
652                 fi
653         fi
654
655         # set final variables after applying overrides
656         local branch=${branch_name:+refs/heads/${branch_name}}
657         local remote_ref=${commit_id:-${branch:-HEAD}}
658         local local_id=${3:-${CATEGORY}/${PN}/${SLOT%/*}}
659         local local_ref=refs/git-r3/${local_id}/__main__
660
661         # try to fetch from the remote
662         local success saved_umask
663         if [[ ${EVCS_UMASK} ]]; then
664                 saved_umask=$(umask)
665                 umask "${EVCS_UMASK}" || die "Bad options to umask: ${EVCS_UMASK}"
666         fi
667         for r in "${repos[@]}"; do
668                 if [[ ! ${EVCS_OFFLINE} ]]; then
669                         einfo "Fetching ${r} ..."
670
671                         local fetch_command=( git fetch "${r}" )
672                         local clone_type=${EGIT_CLONE_TYPE}
673
674                         if [[ ${clone_type} == mirror ]]; then
675                                 fetch_command+=(
676                                         --prune
677                                         # mirror the remote branches as local branches
678                                         "+refs/heads/*:refs/heads/*"
679                                         # pull tags explicitly in order to prune them properly
680                                         "+refs/tags/*:refs/tags/*"
681                                         # notes in case something needs them
682                                         "+refs/notes/*:refs/notes/*"
683                                         # and HEAD in case we need the default branch
684                                         # (we keep it in refs/git-r3 since otherwise --prune interferes)
685                                         "+HEAD:refs/git-r3/HEAD"
686                                 )
687                         else # single or shallow
688                                 local fetch_l fetch_r
689
690                                 if [[ ${remote_ref} == HEAD ]]; then
691                                         # HEAD
692                                         fetch_l=HEAD
693                                 elif [[ ${remote_ref} == refs/* ]]; then
694                                         # regular branch, tag or some other explicit ref
695                                         fetch_l=${remote_ref}
696                                 else
697                                         # tag or commit id...
698                                         # let ls-remote figure it out
699                                         local tagref=$(git ls-remote "${r}" "refs/tags/${remote_ref}")
700
701                                         # if it was a tag, ls-remote obtained a hash
702                                         if [[ ${tagref} ]]; then
703                                                 # tag
704                                                 fetch_l=refs/tags/${remote_ref}
705                                         else
706                                                 # commit id
707                                                 # so we need to fetch the whole branch
708                                                 if [[ ${branch} ]]; then
709                                                         fetch_l=${branch}
710                                                 else
711                                                         fetch_l=HEAD
712                                                 fi
713
714                                                 # fetching by commit in shallow mode? can't do.
715                                                 if [[ ${clone_type} == shallow ]]; then
716                                                         clone_type=single
717                                                 fi
718                                         fi
719                                 fi
720
721                                 # checkout by date does not make sense in shallow mode
722                                 if [[ ${commit_date} && ${clone_type} == shallow ]]; then
723                                         clone_type=single
724                                 fi
725
726                                 if [[ ${fetch_l} == HEAD ]]; then
727                                         fetch_r=refs/git-r3/HEAD
728                                 else
729                                         fetch_r=${fetch_l}
730                                 fi
731
732                                 fetch_command+=(
733                                         "+${fetch_l}:${fetch_r}"
734                                 )
735
736                                 if [[ ${clone_type} == single+tags ]]; then
737                                         fetch_command+=(
738                                                 # pull tags explicitly as requested
739                                                 "+refs/tags/*:refs/tags/*"
740                                         )
741                                 fi
742                         fi
743
744                         if [[ ${clone_type} == shallow ]]; then
745                                 if _git-r3_is_local_repo; then
746                                         # '--depth 1' causes sandbox violations with local repos
747                                         # bug #491260
748                                         clone_type=single
749                                 elif [[ ! $(git rev-parse --quiet --verify "${fetch_r}") ]]
750                                 then
751                                         # use '--depth 1' when fetching a new branch
752                                         fetch_command+=( --depth 1 )
753                                 fi
754                         else # non-shallow mode
755                                 if [[ -f ${GIT_DIR}/shallow ]]; then
756                                         fetch_command+=( --unshallow )
757                                 fi
758                         fi
759
760                         set -- "${fetch_command[@]}"
761                         echo "${@}" >&2
762                         "${@}" || continue
763
764                         if [[ ${clone_type} == mirror || ${fetch_l} == HEAD ]]; then
765                                 # update our HEAD to match our remote HEAD ref
766                                 git symbolic-ref HEAD refs/git-r3/HEAD \
767                                                 || die "Unable to update HEAD"
768                         fi
769                 fi
770
771                 # now let's see what the user wants from us
772                 if [[ ${commit_date} ]]; then
773                         local dated_commit_id=$(
774                                 git rev-list --first-parent --before="${commit_date}" \
775                                         -n 1 "${remote_ref}"
776                         )
777                         if [[ ${?} -ne 0 ]]; then
778                                 die "Listing ${remote_ref} failed (wrong ref?)."
779                         elif [[ ! ${dated_commit_id} ]]; then
780                                 die "Unable to find commit for date ${commit_date}."
781                         else
782                                 set -- git update-ref --no-deref "${local_ref}" "${dated_commit_id}"
783                         fi
784                 else
785                         local full_remote_ref=$(
786                                 git rev-parse --verify --symbolic-full-name "${remote_ref}"
787                         )
788
789                         if [[ ${full_remote_ref} ]]; then
790                                 # when we are given a ref, create a symbolic ref
791                                 # so that we preserve the actual argument
792                                 set -- git symbolic-ref "${local_ref}" "${full_remote_ref}"
793                         else
794                                 # otherwise, we were likely given a commit id
795                                 set -- git update-ref --no-deref "${local_ref}" "${remote_ref}"
796                         fi
797                 fi
798
799                 echo "${@}" >&2
800                 if ! "${@}"; then
801                         if [[ ${EVCS_OFFLINE} ]]; then
802                                 eerror "A clone of the following repository is required to proceed:"
803                                 eerror "  ${r}"
804                                 eerror "However, networking activity has been disabled using EVCS_OFFLINE and the local"
805                                 eerror "clone does not have requested ref:"
806                                 eerror "  ${remote_ref}"
807                                 die "Local clone of ${r} does not have requested ref: ${remote_ref}. Unable to proceed with EVCS_OFFLINE."
808                         else
809                                 die "Referencing ${remote_ref} failed (wrong ref?)."
810                         fi
811                 fi
812
813                 success=1
814                 break
815         done
816         if [[ ${saved_umask} ]]; then
817                 umask "${saved_umask}" || die
818         fi
819         [[ ${success} ]] || die "Unable to fetch from any of EGIT_REPO_URI"
820
821         # submodules can reference commits in any branch
822         # always use the 'mirror' mode to accomodate that, bug #503332
823         local EGIT_CLONE_TYPE=mirror
824
825         # recursively fetch submodules
826         if git cat-file -e "${local_ref}":.gitmodules &>/dev/null; then
827                 local submodules
828                 _git-r3_set_submodules "${_GIT_SUBMODULE_PATH}" \
829                         "$(git cat-file -p "${local_ref}":.gitmodules || die)"
830
831                 while [[ ${submodules[@]} ]]; do
832                         local subname=${submodules[0]}
833                         local url=${submodules[1]}
834                         local path=${submodules[2]}
835
836                         # use only submodules for which path does exist
837                         # (this is in par with 'git submodule'), bug #551100
838                         # note: git cat-file does not work for submodules
839                         if [[ $(git ls-tree -d "${local_ref}" "${path}") ]]
840                         then
841                                 local commit=$(git rev-parse "${local_ref}:${path}" || die)
842
843                                 if [[ ! ${commit} ]]; then
844                                         die "Unable to get commit id for submodule ${subname}"
845                                 fi
846
847                                 local subrepos
848                                 _git-r3_set_subrepos "${url}" "${repos[@]}"
849
850                                 _GIT_SUBMODULE_PATH=${_GIT_SUBMODULE_PATH}${path}/ \
851                                 git-r3_fetch "${subrepos[*]}" "${commit}" \
852                                         "${local_id}/${subname}" ""
853                         fi
854
855                         submodules=( "${submodules[@]:3}" ) # shift
856                 done
857         fi
858 }
859
860 # @FUNCTION: git-r3_checkout
861 # @USAGE: [<repo-uri> [<checkout-path> [<local-id> [<checkout-paths>...]]]]
862 # @DESCRIPTION:
863 # Check the previously fetched tree to the working copy.
864 #
865 # <repo-uri> specifies the repository URIs, as a space-separated list.
866 # The first URI will be used as repository group identifier
867 # and therefore must be used consistently with git-r3_fetch.
868 # The remaining URIs are not used and therefore may be omitted.
869 # When not specified, defaults to ${EGIT_REPO_URI}.
870 #
871 # <checkout-path> specifies the path to place the checkout. It defaults
872 # to ${EGIT_CHECKOUT_DIR} if set, otherwise to ${WORKDIR}/${P}.
873 #
874 # <local-id> needs to specify the local identifier that was used
875 # for respective git-r3_fetch.
876 #
877 # If <checkout-paths> are specified, then the specified paths are passed
878 # to 'git checkout' to effect a partial checkout. Please note that such
879 # checkout will not cause the repository to switch branches,
880 # and submodules will be skipped at the moment. The submodules matching
881 # those paths might be checked out in a future version of the eclass.
882 #
883 # The checkout operation will write to the working copy, and export
884 # the repository state into the environment. If the repository contains
885 # submodules, they will be checked out recursively.
886 git-r3_checkout() {
887         debug-print-function ${FUNCNAME} "$@"
888
889         local repos
890         if [[ ${1} ]]; then
891                 repos=( ${1} )
892         elif [[ $(declare -p EGIT_REPO_URI) == "declare -a"* ]]; then
893                 repos=( "${EGIT_REPO_URI[@]}" )
894         else
895                 repos=( ${EGIT_REPO_URI} )
896         fi
897
898         local out_dir=${2:-${EGIT_CHECKOUT_DIR:-${WORKDIR}/${P}}}
899         local local_id=${3:-${CATEGORY}/${PN}/${SLOT%/*}}
900         local checkout_paths=( "${@:4}" )
901
902         local -x GIT_DIR
903         _git-r3_set_gitdir "${repos[0]}"
904
905         einfo "Checking out ${repos[0]} to ${out_dir} ..."
906
907         if ! git cat-file -e refs/git-r3/"${local_id}"/__main__; then
908                 die "Logic error: no local clone of ${repos[0]}. git-r3_fetch not used?"
909         fi
910         local remote_ref=$(
911                 git symbolic-ref --quiet refs/git-r3/"${local_id}"/__main__
912         )
913         local new_commit_id=$(
914                 git rev-parse --verify refs/git-r3/"${local_id}"/__main__
915         )
916
917         git-r3_sub_checkout() {
918                 local orig_repo=${GIT_DIR}
919                 local -x GIT_DIR=${out_dir}/.git
920                 local -x GIT_WORK_TREE=${out_dir}
921
922                 mkdir -p "${out_dir}" || die
923
924                 # use git init+fetch instead of clone since the latter doesn't like
925                 # non-empty directories.
926
927                 git init --quiet || die
928                 # setup 'alternates' to avoid copying objects
929                 echo "${orig_repo}/objects" > "${GIT_DIR}"/objects/info/alternates || die
930                 # now copy the refs
931                 cp -R "${orig_repo}"/refs/* "${GIT_DIR}"/refs/ || die
932                 if [[ -f ${orig_repo}/packed-refs ]]; then
933                         cp "${orig_repo}"/packed-refs "${GIT_DIR}"/packed-refs || die
934                 fi
935
936                 # (no need to copy HEAD, we will set it via checkout)
937
938                 if [[ -f ${orig_repo}/shallow ]]; then
939                         cp "${orig_repo}"/shallow "${GIT_DIR}"/ || die
940                 fi
941
942                 set -- git checkout --quiet
943                 if [[ ${remote_ref} ]]; then
944                         set -- "${@}" "${remote_ref#refs/heads/}"
945                 else
946                         set -- "${@}" "${new_commit_id}"
947                 fi
948                 if [[ ${checkout_paths[@]} ]]; then
949                         set -- "${@}" -- "${checkout_paths[@]}"
950                 fi
951                 echo "${@}" >&2
952                 "${@}" || die "git checkout ${remote_ref:-${new_commit_id}} failed"
953         }
954         git-r3_sub_checkout
955         unset -f git-r3_sub_checkout
956
957         local old_commit_id=$(
958                 git rev-parse --quiet --verify refs/git-r3/"${local_id}"/__old__
959         )
960         if [[ ! ${old_commit_id} ]]; then
961                 echo "GIT NEW branch -->"
962                 echo "   repository:               ${repos[0]}"
963                 echo "   at the commit:            ${new_commit_id}"
964         else
965                 # diff against previous revision
966                 echo "GIT update -->"
967                 echo "   repository:               ${repos[0]}"
968                 # write out message based on the revisions
969                 if [[ "${old_commit_id}" != "${new_commit_id}" ]]; then
970                         echo "   updating from commit:     ${old_commit_id}"
971                         echo "   to commit:                ${new_commit_id}"
972
973                         set -- git --no-pager diff --stat \
974                                 ${old_commit_id}..${new_commit_id}
975                         if [[ ${checkout_paths[@]} ]]; then
976                                 set -- "${@}" -- "${checkout_paths[@]}"
977                         fi
978                         "${@}"
979                 else
980                         echo "   at the commit:            ${new_commit_id}"
981                 fi
982         fi
983         git update-ref --no-deref refs/git-r3/"${local_id}"/{__old__,__main__} || die
984
985         # recursively checkout submodules
986         if [[ -f ${out_dir}/.gitmodules && ! ${checkout_paths} ]]; then
987                 local submodules
988                 _git-r3_set_submodules "${_GIT_SUBMODULE_PATH}" \
989                         "$(<"${out_dir}"/.gitmodules)"
990
991                 while [[ ${submodules[@]} ]]; do
992                         local subname=${submodules[0]}
993                         local url=${submodules[1]}
994                         local path=${submodules[2]}
995
996                         # use only submodules for which path does exist
997                         # (this is in par with 'git submodule'), bug #551100
998                         if [[ -d ${out_dir}/${path} ]]; then
999                                 local subrepos
1000                                 _git-r3_set_subrepos "${url}" "${repos[@]}"
1001
1002                                 _GIT_SUBMODULE_PATH=${_GIT_SUBMODULE_PATH}${path}/ \
1003                                 git-r3_checkout "${subrepos[*]}" "${out_dir}/${path}" \
1004                                         "${local_id}/${subname}"
1005                         fi
1006
1007                         submodules=( "${submodules[@]:3}" ) # shift
1008                 done
1009         fi
1010
1011         # keep this *after* submodules
1012         export EGIT_DIR=${GIT_DIR}
1013         export EGIT_VERSION=${new_commit_id}
1014 }
1015
1016 # @FUNCTION: git-r3_peek_remote_ref
1017 # @USAGE: [<repo-uri> [<remote-ref>]]
1018 # @DESCRIPTION:
1019 # Peek the reference in the remote repository and print the matching
1020 # (newest) commit SHA1.
1021 #
1022 # <repo-uri> specifies the repository URIs to fetch from, as a space-
1023 # -separated list. When not specified, defaults to ${EGIT_REPO_URI}.
1024 #
1025 # <remote-ref> specifies the remote ref to peek.  It is preferred to use
1026 # 'refs/heads/<branch-name>' for branches and 'refs/tags/<tag-name>'
1027 # for tags. Alternatively, 'HEAD' may be used for upstream default
1028 # branch. Defaults to the first of EGIT_COMMIT, EGIT_BRANCH or literal
1029 # 'HEAD' that is set to a non-null value.
1030 #
1031 # The operation will be done purely on the remote, without using local
1032 # storage. If commit SHA1 is provided as <remote-ref>, the function will
1033 # fail due to limitations of git protocol.
1034 #
1035 # On success, the function returns 0 and writes hexadecimal commit SHA1
1036 # to stdout. On failure, the function returns 1.
1037 git-r3_peek_remote_ref() {
1038         debug-print-function ${FUNCNAME} "$@"
1039
1040         local repos
1041         if [[ ${1} ]]; then
1042                 repos=( ${1} )
1043         elif [[ $(declare -p EGIT_REPO_URI) == "declare -a"* ]]; then
1044                 repos=( "${EGIT_REPO_URI[@]}" )
1045         else
1046                 repos=( ${EGIT_REPO_URI} )
1047         fi
1048
1049         local branch=${EGIT_BRANCH:+refs/heads/${EGIT_BRANCH}}
1050         local remote_ref=${2:-${EGIT_COMMIT:-${branch:-HEAD}}}
1051
1052         [[ ${repos[@]} ]] || die "No URI provided and EGIT_REPO_URI unset"
1053
1054         local r success
1055         for r in "${repos[@]}"; do
1056                 einfo "Peeking ${remote_ref} on ${r} ..." >&2
1057
1058                 local lookup_ref
1059                 if [[ ${remote_ref} == refs/* || ${remote_ref} == HEAD ]]
1060                 then
1061                         lookup_ref=${remote_ref}
1062                 else
1063                         # ls-remote by commit is going to fail anyway,
1064                         # so we may as well pass refs/tags/ABCDEF...
1065                         lookup_ref=refs/tags/${remote_ref}
1066                 fi
1067
1068                 # split on whitespace
1069                 local ref=(
1070                         $(git ls-remote "${r}" "${lookup_ref}")
1071                 )
1072
1073                 if [[ ${ref[0]} ]]; then
1074                         echo "${ref[0]}"
1075                         return 0
1076                 fi
1077         done
1078
1079         return 1
1080 }
1081
1082 git-r3_src_fetch() {
1083         debug-print-function ${FUNCNAME} "$@"
1084
1085         if [[ ! ${EGIT3_STORE_DIR} && ${EGIT_STORE_DIR} ]]; then
1086                 ewarn "You have set EGIT_STORE_DIR but not EGIT3_STORE_DIR. Please consider"
1087                 ewarn "setting EGIT3_STORE_DIR for git-r3.eclass. It is recommended to use"
1088                 ewarn "a different directory than EGIT_STORE_DIR to ease removing old clones"
1089                 ewarn "when git-2 eclass becomes deprecated."
1090         fi
1091
1092         _git-r3_env_setup
1093         git-r3_fetch
1094 }
1095
1096 git-r3_src_unpack() {
1097         debug-print-function ${FUNCNAME} "$@"
1098
1099         _git-r3_env_setup
1100         git-r3_src_fetch
1101         git-r3_checkout
1102 }
1103
1104 # https://bugs.gentoo.org/show_bug.cgi?id=482666
1105 git-r3_pkg_needrebuild() {
1106         debug-print-function ${FUNCNAME} "$@"
1107
1108         local new_commit_id=$(git-r3_peek_remote_ref)
1109         [[ ${new_commit_id} && ${EGIT_VERSION} ]] || die "Lookup failed"
1110
1111         if [[ ${EGIT_VERSION} != ${new_commit_id} ]]; then
1112                 einfo "Update from ${EGIT_VERSION} to ${new_commit_id}"
1113         else
1114                 einfo "Local and remote at ${EGIT_VERSION}"
1115         fi
1116
1117         [[ ${EGIT_VERSION} != ${new_commit_id} ]]
1118 }
1119
1120 # 'export' locally until this gets into EAPI
1121 pkg_needrebuild() { git-r3_pkg_needrebuild; }
1122
1123 _GIT_R3=1
1124 fi