/[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.35
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.35 2014/03/02 11:49:05 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# The 'mirror' type clones all remote branches and tags with complete
41# history and all notes. EGIT_COMMIT can specify any commit hash.
42# Upstream-removed branches and tags are purged from the local clone
43# while fetching. This mode is suitable for cloning the local copy
44# for development or hosting a local git mirror. However, clones
45# of repositories with large diverged branches may quickly grow large.
46#
47# The 'single' type clones only the requested branch or tag. Tags
48# referencing commits throughout the branch history are fetched as well,
49# and all notes. EGIT_COMMIT can safely specify only hashes
50# in the current branch. No purging of old references is done (if you
51# often switch branches, you may need to remove stale branches
52# yourself). This mode is suitable for general use.
53#
54# The 'shallow' type clones only the newest commit on requested branch
55# or tag. EGIT_COMMIT can only specify tags, and since the history is
56# unavailable calls like 'git describe' will not reference prior tags.
57# No purging of old references is done. This mode is intended mostly for
58# embedded systems with limited disk space.
59: ${EGIT_CLONE_TYPE:=single}
60
61# @ECLASS-VARIABLE: EGIT_MIN_CLONE_TYPE
62# @DESCRIPTION:
63# 'Minimum' clone type supported by the ebuild. Takes same values
64# as EGIT_CLONE_TYPE. When user sets a type that's 'lower' (that is,
65# later on the list) than EGIT_MIN_CLONE_TYPE, the eclass uses
66# EGIT_MIN_CLONE_TYPE instead.
67#
68# A common case is to use 'single' whenever the build system requires
69# access to full branch history or the remote (Google Code) does not
70# support shallow clones. Please use sparingly, and to fix fatal errors
71# rather than 'non-pretty versions'.
72: ${EGIT_MIN_CLONE_TYPE:=shallow}
34 73
35# @ECLASS-VARIABLE: EGIT3_STORE_DIR 74# @ECLASS-VARIABLE: EGIT3_STORE_DIR
36# @DESCRIPTION: 75# @DESCRIPTION:
37# Storage directory for git sources. 76# Storage directory for git sources.
38# 77#
104# @DESCRIPTION: 143# @DESCRIPTION:
105# Set the eclass variables as necessary for operation. This can involve 144# Set the eclass variables as necessary for operation. This can involve
106# setting EGIT_* to defaults or ${PN}_LIVE_* variables. 145# setting EGIT_* to defaults or ${PN}_LIVE_* variables.
107_git-r3_env_setup() { 146_git-r3_env_setup() {
108 debug-print-function ${FUNCNAME} "$@" 147 debug-print-function ${FUNCNAME} "$@"
148
149 # check the clone type
150 case "${EGIT_CLONE_TYPE}" in
151 mirror|single|shallow)
152 ;;
153 *)
154 die "Invalid EGIT_CLONE_TYPE=${EGIT_CLONE_TYPE}"
155 esac
156 case "${EGIT_MIN_CLONE_TYPE}" in
157 shallow)
158 ;;
159 single)
160 if [[ ${EGIT_CLONE_TYPE} == shallow ]]; then
161 einfo "git-r3: ebuild needs to be cloned in 'single' mode, adjusting"
162 EGIT_CLONE_TYPE=single
163 fi
164 ;;
165 mirror)
166 if [[ ${EGIT_CLONE_TYPE} != mirror ]]; then
167 einfo "git-r3: ebuild needs to be cloned in 'mirror' mode, adjusting"
168 EGIT_CLONE_TYPE=mirror
169 fi
170 ;;
171 *)
172 die "Invalid EGIT_MIN_CLONE_TYPE=${EGIT_MIN_CLONE_TYPE}"
173 esac
109 174
110 local esc_pn livevar 175 local esc_pn livevar
111 esc_pn=${PN//[-+]/_} 176 esc_pn=${PN//[-+]/_}
112 177
113 livevar=${esc_pn}_LIVE_REPO 178 livevar=${esc_pn}_LIVE_REPO
219 mkdir -m0755 -p "${EGIT3_STORE_DIR}" || die 284 mkdir -m0755 -p "${EGIT3_STORE_DIR}" || die
220 ) || die "Unable to create ${EGIT3_STORE_DIR}" 285 ) || die "Unable to create ${EGIT3_STORE_DIR}"
221 fi 286 fi
222 287
223 addwrite "${EGIT3_STORE_DIR}" 288 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 289 if [[ ! -d ${GIT_DIR} ]]; then
229 mkdir "${GIT_DIR}" || die 290 mkdir "${GIT_DIR}" || die
230 git init --bare || die 291 git init --bare || die
231 fi 292 fi
232} 293}
282 local uri=${1} 343 local uri=${1}
283 344
284 [[ ${uri} == file://* || ${uri} == /* ]] 345 [[ ${uri} == file://* || ${uri} == /* ]]
285} 346}
286 347
287# @FUNCTION: _git-r3_update_head 348# @FUNCTION: _git-r3_find_head
288# @USAGE: <remote-head-ref> 349# @USAGE: <head-ref>
289# @INTERNAL 350# @INTERNAL
290# @DESCRIPTION: 351# @DESCRIPTION:
291# Given a ref to which remote HEAD was fetched, try to match 352# Given a ref to which remote HEAD was fetched, try to find
292# a local branch and update symbolic HEAD appropriately. 353# a branch matching the commit. Expects 'git show-ref'
354# or 'git ls-remote' output on stdin.
293_git-r3_update_head() 355_git-r3_find_head() {
294{
295 debug-print-function ${FUNCNAME} "$@" 356 debug-print-function ${FUNCNAME} "$@"
296 357
297 local head_ref=${1} 358 local head_ref=${1}
298 local head_hash=$(git rev-parse --verify "${1}" || die) 359 local head_hash=$(git rev-parse --verify "${1}" || die)
299 local matching_ref 360 local matching_ref
309 # either take the first matching ref, or master if it is there 370 # either take the first matching ref, or master if it is there
310 if [[ ! ${matching_ref} || ${ref} == refs/heads/master ]]; then 371 if [[ ! ${matching_ref} || ${ref} == refs/heads/master ]]; then
311 matching_ref=${ref} 372 matching_ref=${ref}
312 fi 373 fi
313 fi 374 fi
314 done < <(git show-ref --heads || die) 375 done
315 376
316 if [[ ! ${matching_ref} ]]; then 377 if [[ ! ${matching_ref} ]]; then
317 die "Unable to find a matching branch for remote HEAD (${head_hash})" 378 die "Unable to find a matching branch for remote HEAD (${head_hash})"
318 fi 379 fi
319 380
320 git symbolic-ref HEAD "${matching_ref}" || die 381 echo "${matching_ref}"
321} 382}
322 383
323# @FUNCTION: git-r3_fetch 384# @FUNCTION: git-r3_fetch
324# @USAGE: [<repo-uri> [<remote-ref> [<local-id>]]] 385# @USAGE: [<repo-uri> [<remote-ref> [<local-id>]]]
325# @DESCRIPTION: 386# @DESCRIPTION:
383 # try to fetch from the remote 444 # try to fetch from the remote
384 local r success 445 local r success
385 for r in "${repos[@]}"; do 446 for r in "${repos[@]}"; do
386 einfo "Fetching ${r} ..." 447 einfo "Fetching ${r} ..."
387 448
449 local fetch_command=( git fetch "${r}" )
450
451 if [[ ${EGIT_CLONE_TYPE} == mirror ]]; then
388 local fetch_command=( 452 fetch_command+=(
389 git fetch --prune "${r}" 453 --prune
390 # mirror the remote branches as local branches 454 # mirror the remote branches as local branches
391 "refs/heads/*:refs/heads/*" 455 "refs/heads/*:refs/heads/*"
392 # pull tags explicitly in order to prune them properly 456 # pull tags explicitly in order to prune them properly
393 "refs/tags/*:refs/tags/*" 457 "refs/tags/*:refs/tags/*"
394 # notes in case something needs them 458 # notes in case something needs them
395 "refs/notes/*:refs/notes/*" 459 "refs/notes/*:refs/notes/*"
396 # and HEAD in case we need the default branch 460 # and HEAD in case we need the default branch
397 # (we keep it in refs/git-r3 since otherwise --prune interferes) 461 # (we keep it in refs/git-r3 since otherwise --prune interferes)
398 HEAD:refs/git-r3/HEAD 462 HEAD:refs/git-r3/HEAD
399 ) 463 )
464 else # single or shallow
465 local fetch_l fetch_r
466
467 if [[ ${remote_ref} == HEAD ]]; then
468 # HEAD
469 fetch_l=HEAD
470 elif [[ ${remote_ref} == refs/heads/* ]]; then
471 # regular branch
472 fetch_l=${remote_ref}
473 else
474 # tag or commit...
475 # let ls-remote figure it out
476 local tagref=$(git ls-remote "${r}" "refs/tags/${remote_ref}")
477
478 # if it was a tag, ls-remote obtained a hash
479 if [[ ${tagref} ]]; then
480 # tag
481 fetch_l=refs/tags/${remote_ref}
482 else
483 # commit
484 # so we need to fetch the branch
485 if [[ ${branch} ]]; then
486 fetch_l=${branch}
487 else
488 fetch_l=HEAD
489 fi
490
491 # fetching by commit in shallow mode? can't do.
492 if [[ ${EGIT_CLONE_TYPE} == shallow ]]; then
493 local EGIT_CLONE_TYPE=single
494 fi
495 fi
496 fi
497
498 if [[ ${fetch_l} == HEAD ]]; then
499 fetch_r=refs/git-r3/HEAD
500 else
501 fetch_r=${fetch_l}
502 fi
503
504 fetch_command+=(
505 "${fetch_l}:${fetch_r}"
506 )
507 fi
508
509 if [[ ${EGIT_CLONE_TYPE} == shallow ]]; then
510 # use '--depth 1' when fetching a new branch
511 if [[ ! $(git rev-parse --quiet --verify "${fetch_r}") ]]
512 then
513 fetch_command+=( --depth 1 )
514 fi
515 else # non-shallow mode
516 if [[ -f ${GIT_DIR}/shallow ]]; then
517 fetch_command+=( --unshallow )
518 fi
519 fi
400 520
401 set -- "${fetch_command[@]}" 521 set -- "${fetch_command[@]}"
402 echo "${@}" >&2 522 echo "${@}" >&2
403 if "${@}"; then 523 if "${@}"; then
524 if [[ ${EGIT_CLONE_TYPE} == mirror ]]; then
404 # find remote HEAD and update our HEAD properly 525 # find remote HEAD and update our HEAD properly
526 git symbolic-ref HEAD \
405 _git-r3_update_head refs/git-r3/HEAD 527 "$(_git-r3_find_head refs/git-r3/HEAD \
528 < <(git show-ref --heads || die))" \
529 || die "Unable to update HEAD"
530 else # single or shallow
531 if [[ ${fetch_l} == HEAD ]]; then
532 # find out what branch we fetched as HEAD
533 local head_branch=$(_git-r3_find_head \
534 refs/git-r3/HEAD \
535 < <(git ls-remote --heads "${r}" || die))
536
537 # and move it to its regular place
538 git update-ref --no-deref "${head_branch}" \
539 refs/git-r3/HEAD \
540 || die "Unable to sync HEAD branch ${head_branch}"
541 git symbolic-ref HEAD "${head_branch}" \
542 || die "Unable to update HEAD"
543 fi
544 fi
406 545
407 # now let's see what the user wants from us 546 # now let's see what the user wants from us
408 local full_remote_ref=$( 547 local full_remote_ref=$(
409 git rev-parse --verify --symbolic-full-name "${remote_ref}" 548 git rev-parse --verify --symbolic-full-name "${remote_ref}"
410 ) 549 )
528 # [htn]* safely catches heads, tags, notes without complaining 667 # [htn]* safely catches heads, tags, notes without complaining
529 # on non-existing ones, and omits internal 'git-r3' ref 668 # on non-existing ones, and omits internal 'git-r3' ref
530 cp -R "${orig_repo}"/refs/[htn]* "${GIT_DIR}"/refs/ || die 669 cp -R "${orig_repo}"/refs/[htn]* "${GIT_DIR}"/refs/ || die
531 670
532 # (no need to copy HEAD, we will set it via checkout) 671 # (no need to copy HEAD, we will set it via checkout)
672
673 if [[ -f ${orig_repo}/shallow ]]; then
674 cp "${orig_repo}"/shallow "${GIT_DIR}"/ || die
675 fi
533 676
534 set -- git checkout --quiet 677 set -- git checkout --quiet
535 if [[ ${remote_ref} ]]; then 678 if [[ ${remote_ref} ]]; then
536 set -- "${@}" "${remote_ref#refs/heads/}" 679 set -- "${@}" "${remote_ref#refs/heads/}"
537 else 680 else

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

  ViewVC Help
Powered by ViewVC 1.1.20