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