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

Contents of /eclass/git-2.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.23 - (show annotations) (download)
Fri Sep 23 13:58:35 2011 UTC (2 years, 6 months ago) by mgorny
Branch: MAIN
Changes since 1.22: +2 -1 lines
Ensure EGIT_LOCAL_NONBARE doesn't leak in from env.

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.22 2011/09/23 13:58:22 mgorny 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 variable should not be overriden.
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 # @ECLASS-VARIABLE: EGIT_NOUNPACK
117 # @DEFAULT_UNSET
118 # @DESCRIPTION:
119 # If non-empty this variable bans unpacking of ${A} content into the srcdir.
120 # Default behaviour is to unpack ${A} content.
121
122 # @FUNCTION: git-2_init_variables
123 # @INTERNAL
124 # @DESCRIPTION:
125 # Internal function initializing all git variables.
126 # We define it in function scope so user can define
127 # all the variables before and after inherit.
128 git-2_init_variables() {
129 debug-print-function ${FUNCNAME} "$@"
130
131 local esc_pn liverepo livebranch livecommit
132 esc_pn=${PN//[-+]/_}
133
134 : ${EGIT_SOURCEDIR="${S}"}
135
136 : ${EGIT_STORE_DIR:="${PORTAGE_ACTUAL_DISTDIR-${DISTDIR}}/egit-src"}
137
138 : ${EGIT_HAS_SUBMODULES:=}
139
140 : ${EGIT_OPTIONS:=}
141
142 : ${EGIT_MASTER:=master}
143
144 liverepo=${esc_pn}_LIVE_REPO
145 EGIT_REPO_URI=${!liverepo:-${EGIT_REPO_URI}}
146 [[ ${EGIT_REPO_URI} ]] || die "EGIT_REPO_URI must have some value"
147
148 : ${EVCS_OFFLINE:=}
149
150 livebranch=${esc_pn}_LIVE_BRANCH
151 [[ ${!livebranch} ]] && ewarn "QA: using \"${esc_pn}_LIVE_BRANCH\" variable, you won't get any support"
152 EGIT_BRANCH=${!livebranch:-${EGIT_BRANCH:-${EGIT_MASTER}}}
153
154 livecommit=${esc_pn}_LIVE_COMMIT
155 [[ ${!livecommit} ]] && ewarn "QA: using \"${esc_pn}_LIVE_COMMIT\" variable, you won't get any support"
156 EGIT_COMMIT=${!livecommit:-${EGIT_COMMIT:-${EGIT_BRANCH}}}
157
158 : ${EGIT_REPACK:=}
159
160 : ${EGIT_PRUNE:=}
161 }
162
163 # @FUNCTION: git-2_submodules
164 # @INTERNAL
165 # @DESCRIPTION:
166 # Internal function wrapping the submodule initialisation and update.
167 git-2_submodules() {
168 debug-print-function ${FUNCNAME} "$@"
169 if [[ ${EGIT_HAS_SUBMODULES} ]]; then
170 if [[ ${EVCS_OFFLINE} ]]; then
171 # for submodules operations we need to be online
172 debug-print "${FUNCNAME}: not updating submodules in offline mode"
173 return 1
174 fi
175
176 debug-print "${FUNCNAME}: working in \"${1}\""
177 pushd "${EGIT_DIR}" > /dev/null
178
179 debug-print "${FUNCNAME}: git submodule init"
180 git submodule init || die
181 debug-print "${FUNCNAME}: git submodule sync"
182 git submodule sync || die
183 debug-print "${FUNCNAME}: git submodule update"
184 git submodule update || die
185
186 popd > /dev/null
187 fi
188 }
189
190 # @FUNCTION: git-2_branch
191 # @INTERNAL
192 # @DESCRIPTION:
193 # Internal function that changes branch for the repo based on EGIT_COMMIT and
194 # EGIT_BRANCH variables.
195 git-2_branch() {
196 debug-print-function ${FUNCNAME} "$@"
197
198 local branchname src
199
200 debug-print "${FUNCNAME}: working in \"${EGIT_SOURCEDIR}\""
201 pushd "${EGIT_SOURCEDIR}" > /dev/null
202
203 local branchname=branch-${EGIT_BRANCH} src=origin/${EGIT_BRANCH}
204 if [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]]; then
205 branchname=tree-${EGIT_COMMIT}
206 src=${EGIT_COMMIT}
207 fi
208 debug-print "${FUNCNAME}: git checkout -b ${branchname} ${src}"
209 git checkout -b ${branchname} ${src} \
210 || die "${FUNCNAME}: changing the branch failed"
211
212 popd > /dev/null
213 }
214
215 # @FUNCTION: git-2_gc
216 # @INTERNAL
217 # @DESCRIPTION:
218 # Internal function running garbage collector on checked out tree.
219 git-2_gc() {
220 debug-print-function ${FUNCNAME} "$@"
221
222 local args
223
224 if [[ ${EGIT_REPACK} || ${EGIT_PRUNE} ]]; then
225 pushd "${EGIT_DIR}" > /dev/null
226 ebegin "Garbage collecting the repository"
227 [[ ${EGIT_PRUNE} ]] && args='--prune'
228 debug-print "${FUNCNAME}: git gc ${args}"
229 git gc ${args}
230 eend $?
231 popd > /dev/null
232 fi
233 }
234
235 # @FUNCTION: git-2_prepare_storedir
236 # @INTERNAL
237 # @DESCRIPTION:
238 # Internal function preparing directory where we are going to store SCM
239 # repository.
240 git-2_prepare_storedir() {
241 debug-print-function ${FUNCNAME} "$@"
242
243 local clone_dir
244
245 # initial clone, we have to create master git storage directory and play
246 # nicely with sandbox
247 if [[ ! -d ${EGIT_STORE_DIR} ]]; then
248 debug-print "${FUNCNAME}: Creating git main storage directory"
249 addwrite /
250 mkdir -p "${EGIT_STORE_DIR}" \
251 || die "${FUNCNAME}: can't mkdir \"${EGIT_STORE_DIR}\""
252 fi
253
254 # allow writing into EGIT_STORE_DIR
255 addwrite "${EGIT_STORE_DIR}"
256 # calculate the proper store dir for data
257 # If user didn't specify the EGIT_DIR, we check if he did specify
258 # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
259 EGIT_REPO_URI=${EGIT_REPO_URI%/}
260 if [[ ! ${EGIT_DIR} ]]; then
261 if [[ ${EGIT_PROJECT} ]]; then
262 clone_dir=${EGIT_PROJECT}
263 else
264 clone_dir=${EGIT_REPO_URI##*/}
265 fi
266 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
267 fi
268 export EGIT_DIR=${EGIT_DIR}
269 debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
270 }
271
272 # @FUNCTION: git-2_move_source
273 # @INTERNAL
274 # @DESCRIPTION:
275 # Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
276 git-2_move_source() {
277 debug-print-function ${FUNCNAME} "$@"
278
279 debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
280 pushd "${EGIT_DIR}" > /dev/null
281 mkdir -p "${EGIT_SOURCEDIR}" \
282 || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
283 ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
284 || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
285 popd > /dev/null
286 }
287
288 # @FUNCTION: git-2_initial_clone
289 # @INTERNAL
290 # @DESCRIPTION:
291 # Internal function running initial clone on specified repo_uri.
292 git-2_initial_clone() {
293 debug-print-function ${FUNCNAME} "$@"
294
295 local repo_uri
296
297 EGIT_REPO_URI_SELECTED=""
298 for repo_uri in ${EGIT_REPO_URI}; do
299 debug-print "${FUNCNAME}: git clone ${EGIT_LOCAL_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
300 if git clone ${EGIT_LOCAL_OPTIONS} "${repo_uri}" "${EGIT_DIR}"; then
301 # global variable containing the repo_name we will be using
302 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
303 EGIT_REPO_URI_SELECTED="${repo_uri}"
304 break
305 fi
306 done
307
308 [[ ${EGIT_REPO_URI_SELECTED} ]] \
309 || die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
310 }
311
312 # @FUNCTION: git-2_update_repo
313 # @INTERNAL
314 # @DESCRIPTION:
315 # Internal function running update command on specified repo_uri.
316 git-2_update_repo() {
317 debug-print-function ${FUNCNAME} "$@"
318
319 local repo_uri
320
321 if [[ ${EGIT_LOCAL_NONBARE} ]]; then
322 # checkout master branch and drop all other local branches
323 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
324 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
325 debug-print "${FUNCNAME}: git branch -D ${x}"
326 git branch -D ${x} > /dev/null
327 done
328 fi
329
330 EGIT_REPO_URI_SELECTED=""
331 for repo_uri in ${EGIT_REPO_URI}; do
332 # git urls might change, so reset it
333 git config remote.origin.url "${repo_uri}"
334
335 debug-print "${EGIT_UPDATE_CMD}"
336 if ${EGIT_UPDATE_CMD} > /dev/null; then
337 # global variable containing the repo_name we will be using
338 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
339 EGIT_REPO_URI_SELECTED="${repo_uri}"
340 break
341 fi
342 done
343
344 [[ ${EGIT_REPO_URI_SELECTED} ]] \
345 || die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
346 }
347
348 # @FUNCTION: git-2_fetch
349 # @INTERNAL
350 # @DESCRIPTION:
351 # Internal function fetching repository from EGIT_REPO_URI and storing it in
352 # specified EGIT_STORE_DIR.
353 git-2_fetch() {
354 debug-print-function ${FUNCNAME} "$@"
355
356 local oldsha cursha repo_type
357
358 [[ ${EGIT_LOCAL_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
359
360 if [[ ! -d ${EGIT_DIR} ]]; then
361 git-2_initial_clone
362 pushd "${EGIT_DIR}" > /dev/null
363 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
364 echo "GIT NEW clone -->"
365 echo " repository: ${EGIT_REPO_URI_SELECTED}"
366 echo " at the commit: ${cursha}"
367
368 popd > /dev/null
369 elif [[ ${EVCS_OFFLINE} ]]; then
370 pushd "${EGIT_DIR}" > /dev/null
371 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
372 echo "GIT offline update -->"
373 echo " repository: $(git config remote.origin.url)"
374 echo " at the commit: ${cursha}"
375 popd > /dev/null
376 else
377 pushd "${EGIT_DIR}" > /dev/null
378 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
379 git-2_update_repo
380 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
381
382 # fetch updates
383 echo "GIT update -->"
384 echo " repository: ${EGIT_REPO_URI_SELECTED}"
385 # write out message based on the revisions
386 if [[ "${oldsha}" != "${cursha}" ]]; then
387 echo " updating from commit: ${oldsha}"
388 echo " to commit: ${cursha}"
389 else
390 echo " at the commit: ${cursha}"
391 fi
392
393 # print nice statistic of what was changed
394 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
395 popd > /dev/null
396 fi
397 # export the version the repository is at
398 export EGIT_VERSION="${cursha}"
399 # log the repo state
400 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
401 && echo " commit: ${EGIT_COMMIT}"
402 echo " branch: ${EGIT_BRANCH}"
403 echo " storage directory: \"${EGIT_DIR}\""
404 echo " checkout type: ${repo_type}"
405 }
406
407 # @FUNCTION: git_bootstrap
408 # @INTERNAL
409 # @DESCRIPTION:
410 # Internal function that runs bootstrap command on unpacked source.
411 git-2_bootstrap() {
412 debug-print-function ${FUNCNAME} "$@"
413
414 # @ECLASS-VARIABLE: EGIT_BOOTSTRAP
415 # @DESCRIPTION:
416 # Command to be executed after checkout and clone of the specified
417 # repository.
418 # enviroment the package will fail if there is no update, thus in
419 # combination with --keep-going it would lead in not-updating
420 # pakcages that are up-to-date.
421 if [[ ${EGIT_BOOTSTRAP} ]]; then
422 pushd "${EGIT_SOURCEDIR}" > /dev/null
423 einfo "Starting bootstrap"
424
425 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
426 # we have file in the repo which we should execute
427 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
428
429 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
430 eval "./${EGIT_BOOTSTRAP}" \
431 || die "${FUNCNAME}: bootstrap script failed"
432 else
433 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
434 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
435 die "\"${EGIT_BOOTSTRAP}\" is not executable"
436 fi
437 else
438 # we execute some system command
439 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
440
441 eval "${EGIT_BOOTSTRAP}" \
442 || die "${FUNCNAME}: bootstrap commands failed"
443 fi
444
445 einfo "Bootstrap finished"
446 popd > /dev/null
447 fi
448 }
449
450 # @FUNCTION: git-2_migrate_repository
451 # @INTERNAL
452 # @DESCRIPTION:
453 # Internal function migrating between bare and normal checkout repository.
454 # This is based on usage of EGIT_SUBMODULES, at least until they
455 # start to work with bare checkouts sanely.
456 # This function also set some global variables that differ between
457 # bare and non-bare checkout.
458 git-2_migrate_repository() {
459 debug-print-function ${FUNCNAME} "$@"
460
461 local bare returnstate
462
463 # first find out if we have submodules
464 # or user explicitly wants us to use non-bare clones
465 if ! [[ ${EGIT_HAS_SUBMODULES} || ${EGIT_NONBARE} ]]; then
466 bare=1
467 fi
468
469 # test if we already have some repo and if so find out if we have
470 # to migrate the data
471 if [[ -d ${EGIT_DIR} ]]; then
472 if [[ ${bare} && -d ${EGIT_DIR}/.git ]]; then
473 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
474
475 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
476 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
477 export GIT_DIR="${EGIT_DIR}.bare"
478 git config core.bare true > /dev/null
479 returnstate=$?
480 unset GIT_DIR
481 rm -rf "${EGIT_DIR}"
482 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
483 eend ${returnstate}
484 elif [[ ! ${bare} && ! -d ${EGIT_DIR}/.git ]]; then
485 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
486
487 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
488 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
489 returnstate=$?
490 rm -rf "${EGIT_DIR}"
491 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
492 eend ${returnstate}
493 fi
494 fi
495 if [[ ${returnstate} -ne 0 ]]; then
496 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
497
498 # migration failed, remove the EGIT_DIR to play it safe
499 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
500 rm -rf "${EGIT_DIR}"
501 fi
502
503 # set various options to work with both targets
504 if [[ ${bare} ]]; then
505 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
506 EGIT_LOCAL_OPTIONS+="${EGIT_OPTIONS} --bare"
507 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
508 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
509 UPSTREAM_BRANCH="${EGIT_BRANCH}"
510 EGIT_LOCAL_NONBARE=
511 else
512 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
513 MOVE_COMMAND="cp -pPR ."
514 EGIT_LOCAL_OPTIONS="${EGIT_OPTIONS}"
515 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
516 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
517 EGIT_LOCAL_NONBARE="true"
518 fi
519 }
520
521 # @FUNCTION: git-2_cleanup
522 # @INTERNAL
523 # @DESCRIPTION:
524 # Internal function cleaning up all the global variables
525 # that are not required after the unpack has been done.
526 git-2_cleanup() {
527 debug-print-function ${FUNCNAME} "$@"
528
529 # Here we can unset only variables that are GLOBAL
530 # defined by the eclass, BUT NOT subject to change
531 # by user (like EGIT_PROJECT).
532 # If ebuild writer polutes his environment it is
533 # his problem only.
534 unset EGIT_DIR
535 unset MOVE_COMMAND
536 unset EGIT_LOCAL_OPTIONS
537 unset EGIT_UPDATE_CMD
538 unset UPSTREAM_BRANCH
539 unset EGIT_LOCAL_NONBARE
540 }
541
542 # @FUNCTION: git-2_src_unpack
543 # @DESCRIPTION:
544 # Default git src_unpack function.
545 git-2_src_unpack() {
546 debug-print-function ${FUNCNAME} "$@"
547
548 git-2_init_variables
549 git-2_prepare_storedir
550 git-2_migrate_repository
551 git-2_fetch "$@"
552 git-2_gc
553 git-2_submodules
554 git-2_move_source
555 git-2_branch
556 git-2_bootstrap
557 git-2_cleanup
558 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
559
560 # Users can specify some SRC_URI and we should
561 # unpack the files too.
562 if [[ ! ${EGIT_NOUNPACK} ]]; then
563 if has ${EAPI:-0} 0 1; then
564 [[ ${A} ]] && unpack ${A}
565 else
566 default_src_unpack
567 fi
568 fi
569 }

  ViewVC Help
Powered by ViewVC 1.1.20