/[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.31 Revision 1.32
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.31 2014/03/02 11:47:10 mgorny Exp $ 3# $Header: /var/cvsroot/gentoo-x86/eclass/git-r3.eclass,v 1.32 2014/03/02 11:47:41 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.
33fi 33fi
34 34
35# @ECLASS-VARIABLE: EGIT_CLONE_TYPE 35# @ECLASS-VARIABLE: EGIT_CLONE_TYPE
36# @DESCRIPTION: 36# @DESCRIPTION:
37# Type of clone that should be used against the remote repository. 37# Type of clone that should be used against the remote repository.
38# This can be either of: 'mirror'. 38# This can be either of: 'mirror', 'single'.
39# 39#
40# The 'mirror' type clones all remote branches and tags with complete 40# The 'mirror' type clones all remote branches and tags with complete
41# history and all notes. EGIT_COMMIT can specify any commit hash. 41# history and all notes. EGIT_COMMIT can specify any commit hash.
42# Upstream-removed branches and tags are purged from the local clone 42# Upstream-removed branches and tags are purged from the local clone
43# while fetching. This mode is suitable for cloning the local copy 43# while fetching. This mode is suitable for cloning the local copy
44# for development or hosting a local git mirror. However, clones 44# for development or hosting a local git mirror. However, clones
45# of repositories with large diverged branches may quickly grow large. 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.
46: ${EGIT_CLONE_TYPE:=mirror} 53: ${EGIT_CLONE_TYPE:=single}
47 54
48# @ECLASS-VARIABLE: EGIT3_STORE_DIR 55# @ECLASS-VARIABLE: EGIT3_STORE_DIR
49# @DESCRIPTION: 56# @DESCRIPTION:
50# Storage directory for git sources. 57# Storage directory for git sources.
51# 58#
120_git-r3_env_setup() { 127_git-r3_env_setup() {
121 debug-print-function ${FUNCNAME} "$@" 128 debug-print-function ${FUNCNAME} "$@"
122 129
123 # check the clone type 130 # check the clone type
124 case "${EGIT_CLONE_TYPE}" in 131 case "${EGIT_CLONE_TYPE}" in
125 mirror) 132 mirror|single)
126 ;; 133 ;;
127 *) 134 *)
128 die "Invalid EGIT_CLONE_TYPE=${EGIT_CLONE_TYPE}" 135 die "Invalid EGIT_CLONE_TYPE=${EGIT_CLONE_TYPE}"
129 esac 136 esac
130 137
303 local uri=${1} 310 local uri=${1}
304 311
305 [[ ${uri} == file://* || ${uri} == /* ]] 312 [[ ${uri} == file://* || ${uri} == /* ]]
306} 313}
307 314
308# @FUNCTION: _git-r3_update_head 315# @FUNCTION: _git-r3_find_head
309# @USAGE: <remote-head-ref> 316# @USAGE: <head-ref>
310# @INTERNAL 317# @INTERNAL
311# @DESCRIPTION: 318# @DESCRIPTION:
312# Given a ref to which remote HEAD was fetched, try to match 319# Given a ref to which remote HEAD was fetched, try to find
313# a local branch and update symbolic HEAD appropriately. 320# a branch matching the commit. Expects 'git show-ref'
321# or 'git ls-remote' output on stdin.
314_git-r3_update_head() 322_git-r3_find_head() {
315{
316 debug-print-function ${FUNCNAME} "$@" 323 debug-print-function ${FUNCNAME} "$@"
317 324
318 local head_ref=${1} 325 local head_ref=${1}
319 local head_hash=$(git rev-parse --verify "${1}" || die) 326 local head_hash=$(git rev-parse --verify "${1}" || die)
320 local matching_ref 327 local matching_ref
330 # either take the first matching ref, or master if it is there 337 # either take the first matching ref, or master if it is there
331 if [[ ! ${matching_ref} || ${ref} == refs/heads/master ]]; then 338 if [[ ! ${matching_ref} || ${ref} == refs/heads/master ]]; then
332 matching_ref=${ref} 339 matching_ref=${ref}
333 fi 340 fi
334 fi 341 fi
335 done < <(git show-ref --heads || die) 342 done
336 343
337 if [[ ! ${matching_ref} ]]; then 344 if [[ ! ${matching_ref} ]]; then
338 die "Unable to find a matching branch for remote HEAD (${head_hash})" 345 die "Unable to find a matching branch for remote HEAD (${head_hash})"
339 fi 346 fi
340 347
341 git symbolic-ref HEAD "${matching_ref}" || die 348 echo "${matching_ref}"
342} 349}
343 350
344# @FUNCTION: git-r3_fetch 351# @FUNCTION: git-r3_fetch
345# @USAGE: [<repo-uri> [<remote-ref> [<local-id>]]] 352# @USAGE: [<repo-uri> [<remote-ref> [<local-id>]]]
346# @DESCRIPTION: 353# @DESCRIPTION:
404 # try to fetch from the remote 411 # try to fetch from the remote
405 local r success 412 local r success
406 for r in "${repos[@]}"; do 413 for r in "${repos[@]}"; do
407 einfo "Fetching ${r} ..." 414 einfo "Fetching ${r} ..."
408 415
416 local fetch_command=( git fetch "${r}" )
417
418 if [[ ${EGIT_CLONE_TYPE} == mirror ]]; then
409 local fetch_command=( 419 fetch_command+=(
410 git fetch --prune "${r}" 420 --prune
411 # mirror the remote branches as local branches 421 # mirror the remote branches as local branches
412 "refs/heads/*:refs/heads/*" 422 "refs/heads/*:refs/heads/*"
413 # pull tags explicitly in order to prune them properly 423 # pull tags explicitly in order to prune them properly
414 "refs/tags/*:refs/tags/*" 424 "refs/tags/*:refs/tags/*"
415 # notes in case something needs them 425 # notes in case something needs them
416 "refs/notes/*:refs/notes/*" 426 "refs/notes/*:refs/notes/*"
417 # and HEAD in case we need the default branch 427 # and HEAD in case we need the default branch
418 # (we keep it in refs/git-r3 since otherwise --prune interferes) 428 # (we keep it in refs/git-r3 since otherwise --prune interferes)
419 HEAD:refs/git-r3/HEAD 429 HEAD:refs/git-r3/HEAD
420 ) 430 )
431 else # single
432 local fetch_l fetch_r
433
434 if [[ ${remote_ref} == HEAD ]]; then
435 # HEAD
436 fetch_l=HEAD
437 elif [[ ${remote_ref} == refs/heads/* ]]; then
438 # regular branch
439 fetch_l=${remote_ref}
440 else
441 # tag or commit...
442 # let ls-remote figure it out
443 local tagref=$(git ls-remote "${r}" "refs/tags/${remote_ref}")
444
445 # if it was a tag, ls-remote obtained a hash
446 if [[ ${tagref} ]]; then
447 # tag
448 fetch_l=refs/tags/${remote_ref}
449 else
450 # commit, so we need to fetch the branch
451 # and guess where it takes us...
452 if [[ ${branch} ]]; then
453 fetch_l=${branch}
454 else
455 fetch_l=HEAD
456 fi
457 fi
458 fi
459
460 if [[ ${fetch_l} == HEAD ]]; then
461 fetch_r=refs/git-r3/HEAD
462 else
463 fetch_r=${fetch_l}
464 fi
465
466 fetch_command+=(
467 "${fetch_l}:${fetch_r}"
468 )
469 fi
421 470
422 set -- "${fetch_command[@]}" 471 set -- "${fetch_command[@]}"
423 echo "${@}" >&2 472 echo "${@}" >&2
424 if "${@}"; then 473 if "${@}"; then
474 if [[ ${EGIT_CLONE_TYPE} == mirror ]]; then
425 # find remote HEAD and update our HEAD properly 475 # find remote HEAD and update our HEAD properly
476 git symbolic-ref HEAD \
426 _git-r3_update_head refs/git-r3/HEAD 477 "$(_git-r3_find_head refs/git-r3/HEAD \
478 < <(git show-ref --heads || die))" \
479 || die "Unable to update HEAD"
480 else # single
481 if [[ ${fetch_l} == HEAD ]]; then
482 # find out what branch we fetched as HEAD
483 local head_branch=$(_git-r3_find_head \
484 refs/git-r3/HEAD \
485 < <(git ls-remote --heads "${r}" || die))
486
487 # and move it to its regular place
488 git update-ref --no-deref "${head_branch}" \
489 refs/git-r3/HEAD \
490 || die "Unable to sync HEAD branch ${head_branch}"
491 git symbolic-ref HEAD "${head_branch}" \
492 || die "Unable to update HEAD"
493 fi
494 fi
427 495
428 # now let's see what the user wants from us 496 # now let's see what the user wants from us
429 local full_remote_ref=$( 497 local full_remote_ref=$(
430 git rev-parse --verify --symbolic-full-name "${remote_ref}" 498 git rev-parse --verify --symbolic-full-name "${remote_ref}"
431 ) 499 )

Legend:
Removed from v.1.31  
changed lines
  Added in v.1.32

  ViewVC Help
Powered by ViewVC 1.1.20