/[gentoo-x86]/eclass/git-r3.eclass
Gentoo

Diff of /eclass/git-r3.eclass

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.30 Revision 1.44
1# Copyright 1999-2014 Gentoo Foundation 1# Copyright 1999-2014 Gentoo Foundation
2# Distributed under the terms of the GNU General Public License v2 2# Distributed under the terms of the GNU General Public License v2
3# $Header: /var/cvsroot/gentoo-x86/eclass/git-r3.eclass,v 1.30 2014/03/02 11:46:42 mgorny Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/git-r3.eclass,v 1.44 2014/06/20 11:40:28 mgorny Exp $
4 4
5# @ECLASS: git-r3.eclass 5# @ECLASS: git-r3.eclass
6# @MAINTAINER: 6# @MAINTAINER:
7# Michał Górny <mgorny@gentoo.org> 7# Michał Górny <mgorny@gentoo.org>
8# @BLURB: Eclass for fetching and unpacking git repositories. 8# @BLURB: Eclass for fetching and unpacking git repositories.
27EXPORT_FUNCTIONS src_unpack 27EXPORT_FUNCTIONS src_unpack
28 28
29if [[ ! ${_GIT_R3} ]]; then 29if [[ ! ${_GIT_R3} ]]; then
30 30
31if [[ ! ${_INHERITED_BY_GIT_2} ]]; then 31if [[ ! ${_INHERITED_BY_GIT_2} ]]; then
32 DEPEND="dev-vcs/git" 32 DEPEND=">=dev-vcs/git-1.8.2.1"
33fi 33fi
34
35# @ECLASS-VARIABLE: EGIT_CLONE_TYPE
36# @DESCRIPTION:
37# Type of clone that should be used against the remote repository.
38# This can be either of: 'mirror', 'single', 'shallow'.
39#
40# This is intended to be set by user in make.conf. Ebuilds are supposed
41# to set EGIT_MIN_CLONE_TYPE if necessary instead.
42#
43# The 'mirror' type clones all remote branches and tags with complete
44# history and all notes. EGIT_COMMIT can specify any commit hash.
45# Upstream-removed branches and tags are purged from the local clone
46# while fetching. This mode is suitable for cloning the local copy
47# for development or hosting a local git mirror. However, clones
48# of repositories with large diverged branches may quickly grow large.
49#
50# The 'single+tags' type clones the requested branch and all tags
51# in the repository. All notes are fetched as well. EGIT_COMMIT
52# can safely specify hashes throughout the current branch and all tags.
53# No purging of old references is done (if you often switch branches,
54# you may need to remove stale branches yourself). This mode is intended
55# mostly for use with broken git servers such as Google Code that fail
56# to fetch tags along with the branch in 'single' mode.
57#
58# The 'single' type clones only the requested branch or tag. Tags
59# referencing commits throughout the branch history are fetched as well,
60# and all notes. EGIT_COMMIT can safely specify only hashes
61# in the current branch. No purging of old references is done (if you
62# often switch branches, you may need to remove stale branches
63# yourself). This mode is suitable for general use.
64#
65# The 'shallow' type clones only the newest commit on requested branch
66# or tag. EGIT_COMMIT can only specify tags, and since the history is
67# unavailable calls like 'git describe' will not reference prior tags.
68# No purging of old references is done. This mode is intended mostly for
69# embedded systems with limited disk space.
70: ${EGIT_CLONE_TYPE:=single}
71
72# @ECLASS-VARIABLE: EGIT_MIN_CLONE_TYPE
73# @DESCRIPTION:
74# 'Minimum' clone type supported by the ebuild. Takes same values
75# as EGIT_CLONE_TYPE. When user sets a type that's 'lower' (that is,
76# later on the list) than EGIT_MIN_CLONE_TYPE, the eclass uses
77# EGIT_MIN_CLONE_TYPE instead.
78#
79# This variable is intended to be used by ebuilds only. Users are
80# supposed to set EGIT_CLONE_TYPE instead.
81#
82# A common case is to use 'single' whenever the build system requires
83# access to full branch history, or 'single+tags' when Google Code
84# or a similar remote is used that does not support shallow clones
85# and fetching tags along with commits. Please use sparingly, and to fix
86# fatal errors rather than 'non-pretty versions'.
87: ${EGIT_MIN_CLONE_TYPE:=shallow}
34 88
35# @ECLASS-VARIABLE: EGIT3_STORE_DIR 89# @ECLASS-VARIABLE: EGIT3_STORE_DIR
36# @DESCRIPTION: 90# @DESCRIPTION:
37# Storage directory for git sources. 91# Storage directory for git sources.
38# 92#
104# @DESCRIPTION: 158# @DESCRIPTION:
105# Set the eclass variables as necessary for operation. This can involve 159# Set the eclass variables as necessary for operation. This can involve
106# setting EGIT_* to defaults or ${PN}_LIVE_* variables. 160# setting EGIT_* to defaults or ${PN}_LIVE_* variables.
107_git-r3_env_setup() { 161_git-r3_env_setup() {
108 debug-print-function ${FUNCNAME} "$@" 162 debug-print-function ${FUNCNAME} "$@"
163
164 # check the clone type
165 case "${EGIT_CLONE_TYPE}" in
166 mirror|single+tags|single|shallow)
167 ;;
168 *)
169 die "Invalid EGIT_CLONE_TYPE=${EGIT_CLONE_TYPE}"
170 esac
171 case "${EGIT_MIN_CLONE_TYPE}" in
172 shallow)
173 ;;
174 single)
175 if [[ ${EGIT_CLONE_TYPE} == shallow ]]; then
176 einfo "git-r3: ebuild needs to be cloned in 'single' mode, adjusting"
177 EGIT_CLONE_TYPE=single
178 fi
179 ;;
180 single+tags)
181 if [[ ${EGIT_CLONE_TYPE} == shallow || ${EGIT_CLONE_TYPE} == single ]]; then
182 einfo "git-r3: ebuild needs to be cloned in 'single+tags' mode, adjusting"
183 EGIT_CLONE_TYPE=single+tags
184 fi
185 ;;
186 mirror)
187 if [[ ${EGIT_CLONE_TYPE} != mirror ]]; then
188 einfo "git-r3: ebuild needs to be cloned in 'mirror' mode, adjusting"
189 EGIT_CLONE_TYPE=mirror
190 fi
191 ;;
192 *)
193 die "Invalid EGIT_MIN_CLONE_TYPE=${EGIT_MIN_CLONE_TYPE}"
194 esac
109 195
110 local esc_pn livevar 196 local esc_pn livevar
111 esc_pn=${PN//[-+]/_} 197 esc_pn=${PN//[-+]/_}
112 198
113 livevar=${esc_pn}_LIVE_REPO 199 livevar=${esc_pn}_LIVE_REPO
219 mkdir -m0755 -p "${EGIT3_STORE_DIR}" || die 305 mkdir -m0755 -p "${EGIT3_STORE_DIR}" || die
220 ) || die "Unable to create ${EGIT3_STORE_DIR}" 306 ) || die "Unable to create ${EGIT3_STORE_DIR}"
221 fi 307 fi
222 308
223 addwrite "${EGIT3_STORE_DIR}" 309 addwrite "${EGIT3_STORE_DIR}"
224 if [[ -e ${GIT_DIR}/shallow ]]; then
225 einfo "${GIT_DIR} was a shallow clone, recreating..."
226 rm -r "${GIT_DIR}" || die
227 fi
228 if [[ ! -d ${GIT_DIR} ]]; then 310 if [[ ! -d ${GIT_DIR} ]]; then
229 mkdir "${GIT_DIR}" || die 311 mkdir "${GIT_DIR}" || die
230 git init --bare || die 312 git init --bare || die
231 fi 313 fi
232} 314}
268 submodule."${subname}".path || die)" 350 submodule."${subname}".path || die)"
269 ) 351 )
270 done < <(echo "${data}" | git config -f /dev/fd/0 -l || die) 352 done < <(echo "${data}" | git config -f /dev/fd/0 -l || die)
271} 353}
272 354
355# @FUNCTION: _git-r3_set_subrepos
356# @USAGE: <submodule-uri> <parent-repo-uri>...
357# @INTERNAL
358# @DESCRIPTION:
359# Create 'subrepos' array containing absolute (canonical) submodule URIs
360# for the given <submodule-uri>. If the URI is relative, URIs will be
361# constructed using all <parent-repo-uri>s. Otherwise, this single URI
362# will be placed in the array.
363_git-r3_set_subrepos() {
364 debug-print-function ${FUNCNAME} "$@"
365
366 local suburl=${1}
367 subrepos=( "${@:2}" )
368
369 if [[ ${suburl} == ./* || ${suburl} == ../* ]]; then
370 # drop all possible trailing slashes for consistency
371 subrepos=( "${subrepos[@]%%/}" )
372
373 while true; do
374 if [[ ${suburl} == ./* ]]; then
375 suburl=${suburl:2}
376 elif [[ ${suburl} == ../* ]]; then
377 suburl=${suburl:3}
378
379 # XXX: correctness checking
380
381 # drop the last path component
382 subrepos=( "${subrepos[@]%/*}" )
383 # and then the trailing slashes, again
384 subrepos=( "${subrepos[@]%%/}" )
385 else
386 break
387 fi
388 done
389
390 # append the preprocessed path to the preprocessed URIs
391 subrepos=( "${subrepos[@]/%//${suburl}}")
392 else
393 subrepos=( "${suburl}" )
394 fi
395}
396
397
273# @FUNCTION: _git-r3_is_local_repo 398# @FUNCTION: _git-r3_is_local_repo
274# @USAGE: <repo-uri> 399# @USAGE: <repo-uri>
275# @INTERNAL 400# @INTERNAL
276# @DESCRIPTION: 401# @DESCRIPTION:
277# Determine whether the given URI specifies a local (on-disk) 402# Determine whether the given URI specifies a local (on-disk)
282 local uri=${1} 407 local uri=${1}
283 408
284 [[ ${uri} == file://* || ${uri} == /* ]] 409 [[ ${uri} == file://* || ${uri} == /* ]]
285} 410}
286 411
287# @FUNCTION: _git-r3_update_head 412# @FUNCTION: _git-r3_find_head
288# @USAGE: <remote-head-ref> 413# @USAGE: <head-ref>
289# @INTERNAL 414# @INTERNAL
290# @DESCRIPTION: 415# @DESCRIPTION:
291# Given a ref to which remote HEAD was fetched, try to match 416# Given a ref to which remote HEAD was fetched, try to find
292# a local branch and update symbolic HEAD appropriately. 417# a branch matching the commit. Expects 'git show-ref'
418# or 'git ls-remote' output on stdin.
293_git-r3_update_head() 419_git-r3_find_head() {
294{
295 debug-print-function ${FUNCNAME} "$@" 420 debug-print-function ${FUNCNAME} "$@"
296 421
297 local head_ref=${1} 422 local head_ref=${1}
298 local head_hash=$(git rev-parse --verify "${1}" || die) 423 local head_hash=$(git rev-parse --verify "${1}" || die)
299 local matching_ref 424 local matching_ref
309 # either take the first matching ref, or master if it is there 434 # either take the first matching ref, or master if it is there
310 if [[ ! ${matching_ref} || ${ref} == refs/heads/master ]]; then 435 if [[ ! ${matching_ref} || ${ref} == refs/heads/master ]]; then
311 matching_ref=${ref} 436 matching_ref=${ref}
312 fi 437 fi
313 fi 438 fi
314 done < <(git show-ref --heads || die) 439 done
315 440
316 if [[ ! ${matching_ref} ]]; then 441 if [[ ! ${matching_ref} ]]; then
317 die "Unable to find a matching branch for remote HEAD (${head_hash})" 442 die "Unable to find a matching branch for remote HEAD (${head_hash})"
318 fi 443 fi
319 444
320 git symbolic-ref HEAD "${matching_ref}" || die 445 echo "${matching_ref}"
321} 446}
322 447
323# @FUNCTION: git-r3_fetch 448# @FUNCTION: git-r3_fetch
324# @USAGE: [<repo-uri> [<remote-ref> [<local-id>]]] 449# @USAGE: [<repo-uri> [<remote-ref> [<local-id>]]]
325# @DESCRIPTION: 450# @DESCRIPTION:
383 # try to fetch from the remote 508 # try to fetch from the remote
384 local r success 509 local r success
385 for r in "${repos[@]}"; do 510 for r in "${repos[@]}"; do
386 einfo "Fetching ${r} ..." 511 einfo "Fetching ${r} ..."
387 512
513 local fetch_command=( git fetch "${r}" )
514 local clone_type=${EGIT_CLONE_TYPE}
515
516 if [[ ${r} == https://* ]] && ! has_version 'dev-vcs/git[curl]'; then
517 eerror "git-r3: fetching from https:// requested. In order to support https,"
518 eerror "dev-vcs/git needs to be built with USE=curl. Example solution:"
519 eerror
520 eerror " echo dev-vcs/git curl >> /etc/portage/package.use"
521 eerror " emerge -1v dev-vcs/git"
522 die "dev-vcs/git built with USE=curl required."
523 fi
524
525 if [[ ${r} == https://code.google.com/* ]]; then
526 # Google Code has special magic on top of git that:
527 # 1) can't handle shallow clones at all,
528 # 2) fetches duplicately when tags are pulled in with branch
529 # so automatically switch to single+tags mode.
530 if [[ ${clone_type} == shallow ]]; then
531 einfo " Google Code does not support shallow clones"
532 einfo " using EGIT_CLONE_TYPE=single+tags"
533 clone_type=single+tags
534 elif [[ ${clone_type} == single ]]; then
535 einfo " git-r3: Google Code does not send tags properly in 'single' mode"
536 einfo " using EGIT_CLONE_TYPE=single+tags"
537 clone_type=single+tags
538 fi
539 fi
540
541 if [[ ${clone_type} == mirror ]]; then
388 local fetch_command=( 542 fetch_command+=(
389 git fetch --prune "${r}" 543 --prune
390 # mirror the remote branches as local branches 544 # mirror the remote branches as local branches
391 "refs/heads/*:refs/heads/*" 545 "+refs/heads/*:refs/heads/*"
392 # pull tags explicitly in order to prune them properly 546 # pull tags explicitly in order to prune them properly
393 "refs/tags/*:refs/tags/*" 547 "+refs/tags/*:refs/tags/*"
394 # notes in case something needs them 548 # notes in case something needs them
395 "refs/notes/*:refs/notes/*" 549 "+refs/notes/*:refs/notes/*"
396 # and HEAD in case we need the default branch 550 # and HEAD in case we need the default branch
397 # (we keep it in refs/git-r3 since otherwise --prune interferes) 551 # (we keep it in refs/git-r3 since otherwise --prune interferes)
398 HEAD:refs/git-r3/HEAD 552 "+HEAD:refs/git-r3/HEAD"
399 ) 553 )
554 else # single or shallow
555 local fetch_l fetch_r
556
557 if [[ ${remote_ref} == HEAD ]]; then
558 # HEAD
559 fetch_l=HEAD
560 elif [[ ${remote_ref} == refs/heads/* ]]; then
561 # regular branch
562 fetch_l=${remote_ref}
563 else
564 # tag or commit...
565 # let ls-remote figure it out
566 local tagref=$(git ls-remote "${r}" "refs/tags/${remote_ref}")
567
568 # if it was a tag, ls-remote obtained a hash
569 if [[ ${tagref} ]]; then
570 # tag
571 fetch_l=refs/tags/${remote_ref}
572 else
573 # commit
574 # so we need to fetch the branch
575 if [[ ${branch} ]]; then
576 fetch_l=${branch}
577 else
578 fetch_l=HEAD
579 fi
580
581 # fetching by commit in shallow mode? can't do.
582 if [[ ${clone_type} == shallow ]]; then
583 clone_type=single
584 fi
585 fi
586 fi
587
588 if [[ ${fetch_l} == HEAD ]]; then
589 fetch_r=refs/git-r3/HEAD
590 else
591 fetch_r=${fetch_l}
592 fi
593
594 fetch_command+=(
595 "+${fetch_l}:${fetch_r}"
596 )
597
598 if [[ ${clone_type} == single+tags ]]; then
599 fetch_command+=(
600 # pull tags explicitly as requested
601 "+refs/tags/*:refs/tags/*"
602 )
603 fi
604 fi
605
606 if [[ ${clone_type} == shallow ]]; then
607 if _git-r3_is_local_repo; then
608 # '--depth 1' causes sandbox violations with local repos
609 # bug #491260
610 clone_type=single
611 elif [[ ! $(git rev-parse --quiet --verify "${fetch_r}") ]]
612 then
613 # use '--depth 1' when fetching a new branch
614 fetch_command+=( --depth 1 )
615 fi
616 else # non-shallow mode
617 if [[ -f ${GIT_DIR}/shallow ]]; then
618 fetch_command+=( --unshallow )
619 fi
620 fi
400 621
401 set -- "${fetch_command[@]}" 622 set -- "${fetch_command[@]}"
402 echo "${@}" >&2 623 echo "${@}" >&2
403 if "${@}"; then 624 if "${@}"; then
625 if [[ ${clone_type} == mirror ]]; then
404 # find remote HEAD and update our HEAD properly 626 # find remote HEAD and update our HEAD properly
627 git symbolic-ref HEAD \
405 _git-r3_update_head refs/git-r3/HEAD 628 "$(_git-r3_find_head refs/git-r3/HEAD \
629 < <(git show-ref --heads || die))" \
630 || die "Unable to update HEAD"
631 else # single or shallow
632 if [[ ${fetch_l} == HEAD ]]; then
633 # find out what branch we fetched as HEAD
634 local head_branch=$(_git-r3_find_head \
635 refs/git-r3/HEAD \
636 < <(git ls-remote --heads "${r}" || die))
637
638 # and move it to its regular place
639 git update-ref --no-deref "${head_branch}" \
640 refs/git-r3/HEAD \
641 || die "Unable to sync HEAD branch ${head_branch}"
642 git symbolic-ref HEAD "${head_branch}" \
643 || die "Unable to update HEAD"
644 fi
645 fi
406 646
407 # now let's see what the user wants from us 647 # now let's see what the user wants from us
408 local full_remote_ref=$( 648 local full_remote_ref=$(
409 git rev-parse --verify --symbolic-full-name "${remote_ref}" 649 git rev-parse --verify --symbolic-full-name "${remote_ref}"
410 ) 650 )
427 break 667 break
428 fi 668 fi
429 done 669 done
430 [[ ${success} ]] || die "Unable to fetch from any of EGIT_REPO_URI" 670 [[ ${success} ]] || die "Unable to fetch from any of EGIT_REPO_URI"
431 671
672 # submodules can reference commits in any branch
673 # always use the 'clone' mode to accomodate that, bug #503332
674 local EGIT_CLONE_TYPE=mirror
675
432 # recursively fetch submodules 676 # recursively fetch submodules
433 if git cat-file -e "${local_ref}":.gitmodules &>/dev/null; then 677 if git cat-file -e "${local_ref}":.gitmodules &>/dev/null; then
434 local submodules 678 local submodules
435 _git-r3_set_submodules \ 679 _git-r3_set_submodules \
436 "$(git cat-file -p "${local_ref}":.gitmodules || die)" 680 "$(git cat-file -p "${local_ref}":.gitmodules || die)"
442 local commit=$(git rev-parse "${local_ref}:${path}") 686 local commit=$(git rev-parse "${local_ref}:${path}")
443 687
444 if [[ ! ${commit} ]]; then 688 if [[ ! ${commit} ]]; then
445 die "Unable to get commit id for submodule ${subname}" 689 die "Unable to get commit id for submodule ${subname}"
446 fi 690 fi
447 if [[ ${url} == ./* || ${url} == ../* ]]; then 691
448 local subrepos=( "${repos[@]/%//${url}}" ) 692 local subrepos
449 else 693 _git-r3_set_subrepos "${url}" "${repos[@]}"
450 local subrepos=( "${url}" )
451 fi
452 694
453 git-r3_fetch "${subrepos[*]}" "${commit}" "${local_id}/${subname}" 695 git-r3_fetch "${subrepos[*]}" "${commit}" "${local_id}/${subname}"
454 696
455 submodules=( "${submodules[@]:3}" ) # shift 697 submodules=( "${submodules[@]:3}" ) # shift
456 done 698 done
528 # [htn]* safely catches heads, tags, notes without complaining 770 # [htn]* safely catches heads, tags, notes without complaining
529 # on non-existing ones, and omits internal 'git-r3' ref 771 # on non-existing ones, and omits internal 'git-r3' ref
530 cp -R "${orig_repo}"/refs/[htn]* "${GIT_DIR}"/refs/ || die 772 cp -R "${orig_repo}"/refs/[htn]* "${GIT_DIR}"/refs/ || die
531 773
532 # (no need to copy HEAD, we will set it via checkout) 774 # (no need to copy HEAD, we will set it via checkout)
775
776 if [[ -f ${orig_repo}/shallow ]]; then
777 cp "${orig_repo}"/shallow "${GIT_DIR}"/ || die
778 fi
533 779
534 set -- git checkout --quiet 780 set -- git checkout --quiet
535 if [[ ${remote_ref} ]]; then 781 if [[ ${remote_ref} ]]; then
536 set -- "${@}" "${remote_ref#refs/heads/}" 782 set -- "${@}" "${remote_ref#refs/heads/}"
537 else 783 else
574 820
575 while [[ ${submodules[@]} ]]; do 821 while [[ ${submodules[@]} ]]; do
576 local subname=${submodules[0]} 822 local subname=${submodules[0]}
577 local url=${submodules[1]} 823 local url=${submodules[1]}
578 local path=${submodules[2]} 824 local path=${submodules[2]}
825 local subrepos
826 _git-r3_set_subrepos "${url}" "${repos[@]}"
579 827
580 if [[ ${url} == ./* || ${url} == ../* ]]; then
581 url=${repos[0]%%/}/${url}
582 fi
583
584 git-r3_checkout "${url}" "${out_dir}/${path}" \ 828 git-r3_checkout "${subrepos[*]}" "${out_dir}/${path}" \
585 "${local_id}/${subname}" 829 "${local_id}/${subname}"
586 830
587 submodules=( "${submodules[@]:3}" ) # shift 831 submodules=( "${submodules[@]:3}" ) # shift
588 done 832 done
589 fi 833 fi

Legend:
Removed from v.1.30  
changed lines
  Added in v.1.44

  ViewVC Help
Powered by ViewVC 1.1.20