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

Contents of /eclass/git-2.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations) (download)
Fri Sep 23 13:58:09 2011 UTC (2 years, 11 months ago) by mgorny
Branch: MAIN
Changes since 1.20: +3 -3 lines
Move pushd/popds within conditional to avoid needless exec.

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

  ViewVC Help
Powered by ViewVC 1.1.20