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

Contents of /eclass/git-2.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations) (download)
Sat Jul 16 13:11:54 2011 UTC (3 years, 5 months ago) by mgorny
Branch: MAIN
Changes since 1.11: +6 -2 lines
Fix calling default_src_unpack in EAPI < 2. Use inlined default code instead.

1 # Copyright 1999-2011 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-x86/eclass/git-2.eclass,v 1.11 2011/06/08 15:18:13 dberkholz Exp $
4
5 # @ECLASS: git-2.eclass
6 # @MAINTAINER:
7 # Donnie Berkholz <dberkholz@gentoo.org>
8 # @BLURB: Eclass for fetching and unpacking git repositories.
9 # @DESCRIPTION:
10 # Eclass for easing maitenance of live ebuilds using git as remote repository.
11 # Eclass support working with git submodules and branching.
12
13 # This eclass support all EAPIs
14 EXPORT_FUNCTIONS src_unpack
15
16 DEPEND="dev-vcs/git"
17
18 # @ECLASS-VARIABLE: EGIT_SOURCEDIR
19 # @DESCRIPTION:
20 # This variable specifies destination where the cloned
21 # data are copied to.
22 #
23 # EGIT_SOURCEDIR="${S}"
24
25 # @ECLASS-VARIABLE: EGIT_STORE_DIR
26 # @DESCRIPTION:
27 # Storage directory for git sources.
28 #
29 # EGIT_STORE_DIR="${DISTDIR}/egit-src"
30
31 # @ECLASS-VARIABLE: EGIT_HAS_SUBMODULES
32 # @DEFAULT_UNSET
33 # @DESCRIPTION:
34 # If non-empty this variable enables support for git submodules in our
35 # checkout. Also this makes the checkout to be non-bare for now.
36
37 # @ECLASS-VARIABLE: EGIT_OPTIONS
38 # @DEFAULT_UNSET
39 # @DESCRIPTION:
40 # Variable specifying additional options for fetch command.
41
42 # @ECLASS-VARIABLE: EGIT_MASTER
43 # @DESCRIPTION:
44 # Variable for specifying master branch.
45 # Usefull when upstream don't have master branch or name it differently.
46 #
47 # EGIT_MASTER="master"
48
49 # @ECLASS-VARIABLE: EGIT_PROJECT
50 # @DESCRIPTION:
51 # Variable specifying name for the folder where we check out the git
52 # repository. Value of this variable should be unique in the
53 # EGIT_STORE_DIR as otherwise you would override another repository.
54 #
55 # EGIT_PROJECT="${EGIT_REPO_URI##*/}"
56
57 # @ECLASS-VARIABLE: EGIT_DIR
58 # @DESCRIPTION:
59 # Directory where we want to store the git data.
60 # This should not be overriden unless really required.
61 #
62 # EGIT_DIR="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
63
64 # @ECLASS-VARIABLE: EGIT_REPO_URI
65 # @REQUIRED
66 # @DEFAULT_UNSET
67 # @DESCRIPTION:
68 # URI for the repository
69 # e.g. http://foo, git://bar
70 #
71 # Support multiple values:
72 # EGIT_REPO_URI="git://a/b.git http://c/d.git"
73
74 # @ECLASS-VARIABLE: EVCS_OFFLINE
75 # @DEFAULT_UNSET
76 # @DESCRIPTION:
77 # If non-empty this variable prevents performance of any online
78 # operations.
79
80 # @ECLASS-VARIABLE: EGIT_BRANCH
81 # @DESCRIPTION:
82 # Variable containing branch name we want to check out.
83 # It can be overriden via env using packagename_LIVE_BRANCH
84 # variable.
85 #
86 # EGIT_BRANCH="${EGIT_MASTER}"
87
88 # @ECLASS-VARIABLE: EGIT_COMMIT
89 # @DESCRIPTION:
90 # Variable containing commit hash/tag we want to check out.
91 # It can be overriden via env using packagename_LIVE_COMMIT
92 # variable.
93 #
94 # EGIT_COMMIT="${EGIT_BRANCH}"
95
96 # @ECLASS-VARIABLE: EGIT_REPACK
97 # @DEFAULT_UNSET
98 # @DESCRIPTION:
99 # If non-empty this variable specifies that repository will be repacked to
100 # save space. However this can take a REALLY LONG time with VERY big
101 # repositories.
102
103 # @ECLASS-VARIABLE: EGIT_PRUNE
104 # @DEFAULT_UNSET
105 # @DESCRIPTION:
106 # If non-empty this variable enables pruning all loose objects on each fetch.
107 # This is useful if upstream rewinds and rebases branches often.
108
109 # @ECLASS-VARIABLE: EGIT_NONBARE
110 # @DEFAULT_UNSET
111 # @DESCRIPTION:
112 # If non-empty this variable specifies that all checkouts will be done using
113 # non bare repositories. This is useful if you can't operate with bare
114 # checkouts for some reason.
115
116 # @FUNCTION: git-2_init_variables
117 # @DESCRIPTION:
118 # Internal function initializing all git variables.
119 # We define it in function scope so user can define
120 # all the variables before and after inherit.
121 git-2_init_variables() {
122 debug-print-function ${FUNCNAME} "$@"
123
124 local x
125
126 : ${EGIT_SOURCEDIR="${S}"}
127
128 : ${EGIT_STORE_DIR:="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/egit-src"}
129
130 : ${EGIT_HAS_SUBMODULES:=}
131
132 : ${EGIT_OPTIONS:=}
133
134 : ${EGIT_MASTER:=master}
135
136 eval x="\$${PN//[-+]/_}_LIVE_REPO"
137 EGIT_REPO_URI=${x:-${EGIT_REPO_URI}}
138 [[ -z ${EGIT_REPO_URI} ]] && die "EGIT_REPO_URI must have some value"
139
140 : ${EVCS_OFFLINE:=}
141
142 eval x="\$${PN//[-+]/_}_LIVE_BRANCH"
143 [[ -n ${x} ]] && ewarn "QA: using \"${PN//[-+]/_}_LIVE_BRANCH\" variable, you won't get any support"
144 EGIT_BRANCH=${x:-${EGIT_BRANCH:-${EGIT_MASTER}}}
145
146 eval x="\$${PN//[-+]/_}_LIVE_COMMIT"
147 [[ -n ${x} ]] && ewarn "QA: using \"${PN//[-+]/_}_LIVE_COMMIT\" variable, you won't get any support"
148 EGIT_COMMIT=${x:-${EGIT_COMMIT:-${EGIT_BRANCH}}}
149
150 : ${EGIT_REPACK:=}
151
152 : ${EGIT_PRUNE:=}
153 }
154
155 # @FUNCTION: git-2_submodules
156 # @DESCRIPTION:
157 # Internal function wrapping the submodule initialisation and update.
158 git-2_submodules() {
159 debug-print-function ${FUNCNAME} "$@"
160 if [[ -n ${EGIT_HAS_SUBMODULES} ]]; then
161 if [[ -n ${EVCS_OFFLINE} ]]; then
162 # for submodules operations we need to be online
163 debug-print "${FUNCNAME}: not updating submodules in offline mode"
164 return 1
165 fi
166
167 debug-print "${FUNCNAME}: working in \"${1}\""
168 pushd "${EGIT_DIR}" > /dev/null
169
170 debug-print "${FUNCNAME}: git submodule init"
171 git submodule init || die
172 debug-print "${FUNCNAME}: git submodule sync"
173 git submodule sync || die
174 debug-print "${FUNCNAME}: git submodule update"
175 git submodule update || die
176
177 popd > /dev/null
178 fi
179 }
180
181 # @FUNCTION: git-2_branch
182 # @DESCRIPTION:
183 # Internal function that changes branch for the repo based on EGIT_COMMIT and
184 # EGIT_BRANCH variables.
185 git-2_branch() {
186 debug-print-function ${FUNCNAME} "$@"
187
188 debug-print "${FUNCNAME}: working in \"${EGIT_SOURCEDIR}\""
189 pushd "${EGIT_SOURCEDIR}" > /dev/null
190
191 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
192 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
193 branchname=tree-${EGIT_COMMIT}
194 src=${EGIT_COMMIT}
195 fi
196 debug-print "${FUNCNAME}: git checkout -b ${branchname} ${src}"
197 git checkout -b ${branchname} ${src} \
198 || die "${FUNCNAME}: changing the branch failed"
199
200 popd > /dev/null
201
202 unset branchname src
203 }
204
205 # @FUNCTION: git-2_gc
206 # @DESCRIPTION:
207 # Internal function running garbage collector on checked out tree.
208 git-2_gc() {
209 debug-print-function ${FUNCNAME} "$@"
210
211 pushd "${EGIT_DIR}" > /dev/null
212 if [[ -n ${EGIT_REPACK} || -n ${EGIT_PRUNE} ]]; then
213 ebegin "Garbage collecting the repository"
214 local args
215 [[ -n ${EGIT_PRUNE} ]] && args='--prune'
216 debug-print "${FUNCNAME}: git gc ${args}"
217 git gc ${args}
218 eend $?
219 fi
220 popd > /dev/null
221 }
222
223 # @FUNCTION: git-2_prepare_storedir
224 # @DESCRIPTION:
225 # Internal function preparing directory where we are going to store SCM
226 # repository.
227 git-2_prepare_storedir() {
228 debug-print-function ${FUNCNAME} "$@"
229
230 local clone_dir
231
232 # initial clone, we have to create master git storage directory and play
233 # nicely with sandbox
234 if [[ ! -d ${EGIT_STORE_DIR} ]]; then
235 debug-print "${FUNCNAME}: Creating git main storage directory"
236 addwrite /
237 mkdir -p "${EGIT_STORE_DIR}" \
238 || die "${FUNCNAME}: can't mkdir \"${EGIT_STORE_DIR}\""
239 fi
240
241 # allow writing into EGIT_STORE_DIR
242 addwrite "${EGIT_STORE_DIR}"
243 # calculate the proper store dir for data
244 # If user didn't specify the EGIT_DIR, we check if he did specify
245 # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
246 [[ -z ${EGIT_REPO_URI##*/} ]] && EGIT_REPO_URI="${EGIT_REPO_URI%/}"
247 if [[ -z ${EGIT_DIR} ]]; then
248 if [[ -n ${EGIT_PROJECT} ]]; then
249 clone_dir=${EGIT_PROJECT}
250 else
251 clone_dir=${EGIT_REPO_URI##*/}
252 fi
253 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
254 fi
255 export EGIT_DIR=${EGIT_DIR}
256 debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
257 }
258
259 # @FUNCTION: git-2_move_source
260 # @DESCRIPTION:
261 # Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
262 git-2_move_source() {
263 debug-print-function ${FUNCNAME} "$@"
264
265 debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
266 pushd "${EGIT_DIR}" > /dev/null
267 mkdir -p "${EGIT_SOURCEDIR}" \
268 || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
269 ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
270 || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
271 popd > /dev/null
272 }
273
274 # @FUNCTION: git-2_initial_clone
275 # @DESCRIPTION:
276 # Internal function running initial clone on specified repo_uri.
277 git-2_initial_clone() {
278 debug-print-function ${FUNCNAME} "$@"
279
280 local repo_uri
281
282 EGIT_REPO_URI_SELECTED=""
283 for repo_uri in ${EGIT_REPO_URI}; do
284 debug-print "${FUNCNAME}: git clone ${EGIT_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
285 git clone ${EGIT_OPTIONS} "${repo_uri}" "${EGIT_DIR}"
286 if [[ $? -eq 0 ]]; then
287 # global variable containing the repo_name we will be using
288 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
289 EGIT_REPO_URI_SELECTED="${repo_uri}"
290 break
291 fi
292 done
293
294 if [[ -z ${EGIT_REPO_URI_SELECTED} ]]; then
295 die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
296 fi
297 }
298
299 # @FUNCTION: git-2_update_repo
300 # @DESCRIPTION:
301 # Internal function running update command on specified repo_uri.
302 git-2_update_repo() {
303 debug-print-function ${FUNCNAME} "$@"
304
305 local repo_uri
306
307 if [[ -n ${EGIT_NONBARE} ]]; then
308 # checkout master branch and drop all other local branches
309 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
310 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
311 debug-print "${FUNCNAME}: git branch -D ${x}"
312 git branch -D ${x} > /dev/null
313 done
314 fi
315
316 EGIT_REPO_URI_SELECTED=""
317 for repo_uri in ${EGIT_REPO_URI}; do
318 # git urls might change, so reset it
319 git config remote.origin.url "${repo_uri}"
320
321 debug-print "${EGIT_UPDATE_CMD}"
322 ${EGIT_UPDATE_CMD} > /dev/null
323 if [[ $? -eq 0 ]]; then
324 # global variable containing the repo_name we will be using
325 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
326 EGIT_REPO_URI_SELECTED="${repo_uri}"
327 break
328 fi
329 done
330
331 if [[ -z ${EGIT_REPO_URI_SELECTED} ]]; then
332 die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
333 fi
334 }
335
336 # @FUNCTION: git-2_fetch
337 # @DESCRIPTION:
338 # Internal function fetching repository from EGIT_REPO_URI and storing it in
339 # specified EGIT_STORE_DIR.
340 git-2_fetch() {
341 debug-print-function ${FUNCNAME} "$@"
342
343 local oldsha cursha repo_type
344
345 [[ -n ${EGIT_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
346
347 if [[ ! -d ${EGIT_DIR} ]]; then
348 git-2_initial_clone
349 pushd "${EGIT_DIR}" > /dev/null
350 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
351 echo "GIT NEW clone -->"
352 echo " repository: ${EGIT_REPO_URI_SELECTED}"
353 echo " at the commit: ${cursha}"
354
355 popd > /dev/null
356 elif [[ -n ${EVCS_OFFLINE} ]]; then
357 pushd "${EGIT_DIR}" > /dev/null
358 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
359 echo "GIT offline update -->"
360 echo " repository: $(git config remote.origin.url)"
361 echo " at the commit: ${cursha}"
362 popd > /dev/null
363 else
364 pushd "${EGIT_DIR}" > /dev/null
365 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
366 git-2_update_repo
367 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
368
369 # fetch updates
370 echo "GIT update -->"
371 echo " repository: ${EGIT_REPO_URI_SELECTED}"
372 # write out message based on the revisions
373 if [[ "${oldsha}" != "${cursha}" ]]; then
374 echo " updating from commit: ${oldsha}"
375 echo " to commit: ${cursha}"
376 else
377 echo " at the commit: ${cursha}"
378 fi
379
380 # print nice statistic of what was changed
381 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
382 popd > /dev/null
383 fi
384 # export the version the repository is at
385 export EGIT_VERSION="${cursha}"
386 # log the repo state
387 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
388 && echo " commit: ${EGIT_COMMIT}"
389 echo " branch: ${EGIT_BRANCH}"
390 echo " storage directory: \"${EGIT_DIR}\""
391 echo " checkout type: ${repo_type}"
392 }
393
394 # @FUNCTION: git_bootstrap
395 # @DESCRIPTION:
396 # Internal function that runs bootstrap command on unpacked source.
397 git-2_bootstrap() {
398 debug-print-function ${FUNCNAME} "$@"
399
400 # @ECLASS_VARIABLE: EGIT_BOOTSTRAP
401 # @DESCRIPTION:
402 # Command to be executed after checkout and clone of the specified
403 # repository.
404 # enviroment the package will fail if there is no update, thus in
405 # combination with --keep-going it would lead in not-updating
406 # pakcages that are up-to-date.
407 if [[ -n ${EGIT_BOOTSTRAP} ]]; then
408 pushd "${EGIT_SOURCEDIR}" > /dev/null
409 einfo "Starting bootstrap"
410
411 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
412 # we have file in the repo which we should execute
413 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
414
415 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
416 eval "./${EGIT_BOOTSTRAP}" \
417 || die "${FUNCNAME}: bootstrap script failed"
418 else
419 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
420 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
421 die "\"${EGIT_BOOTSTRAP}\" is not executable"
422 fi
423 else
424 # we execute some system command
425 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
426
427 eval "${EGIT_BOOTSTRAP}" \
428 || die "${FUNCNAME}: bootstrap commands failed"
429 fi
430
431 einfo "Bootstrap finished"
432 popd > /dev/null
433 fi
434 }
435
436 # @FUNCTION: git-2_migrate_repository
437 # @DESCRIPTION:
438 # Internal function migrating between bare and normal checkout repository.
439 # This is based on usage of EGIT_SUBMODULES, at least until they
440 # start to work with bare checkouts sanely.
441 git-2_migrate_repository() {
442 debug-print-function ${FUNCNAME} "$@"
443
444 local target returnstate
445
446 # first find out if we have submodules
447 if [[ -z ${EGIT_HAS_SUBMODULES} ]]; then
448 target="bare"
449 else
450 target="full"
451 fi
452 [[ -n ${EGIT_NONBARE} ]] && target="full"
453
454 # test if we already have some repo and if so find out if we have
455 # to migrate the data
456 if [[ -d ${EGIT_DIR} ]]; then
457 if [[ ${target} == bare && -d ${EGIT_DIR}/.git ]]; then
458 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
459
460 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
461 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
462 export GIT_DIR="${EGIT_DIR}.bare"
463 git config core.bare true > /dev/null
464 returnstate=$?
465 unset GIT_DIR
466 rm -rf "${EGIT_DIR}"
467 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
468 eend ${returnstate}
469 fi
470 if [[ ${target} == full && ! -d ${EGIT_DIR}/.git ]]; then
471 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
472
473 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
474 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
475 returnstate=$?
476 rm -rf "${EGIT_DIR}"
477 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
478 eend ${returnstate}
479 fi
480 fi
481 if [[ ${returnstate} -ne 0 ]]; then
482 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
483
484 # migration failed, remove the EGIT_DIR to play it safe
485 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
486 rm -rf "${EGIT_DIR}"
487 fi
488
489 # set various options to work with both targets
490 if [[ ${target} == bare ]]; then
491 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
492 EGIT_OPTIONS+=" --bare"
493 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
494 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
495 UPSTREAM_BRANCH="${EGIT_BRANCH}"
496 else
497 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
498 MOVE_COMMAND="cp -pPR ."
499 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
500 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
501 EGIT_NONBARE="true"
502 fi
503 }
504
505 # @FUNCTION: git-2_src_unpack
506 # @DESCRIPTION:
507 # Default git src_unpack function.
508 git-2_src_unpack() {
509 debug-print-function ${FUNCNAME} "$@"
510
511 git-2_init_variables
512 git-2_prepare_storedir
513 git-2_migrate_repository
514 git-2_fetch "$@"
515 git-2_gc
516 git-2_submodules
517 git-2_move_source
518 git-2_branch
519 git-2_bootstrap
520 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
521
522 # Users can specify some SRC_URI and we should
523 # unpack the files too.
524 if has ${EAPI:-0} 0 1; then
525 [[ -n ${A} ]] && unpack ${A}
526 else
527 default_src_unpack
528 fi
529 }

  ViewVC Help
Powered by ViewVC 1.1.20