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

Contents of /eclass/git-2.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.17 - (show annotations) (download)
Fri Sep 23 13:57:15 2011 UTC (2 years, 10 months ago) by mgorny
Branch: MAIN
Changes since 1.16: +22 -24 lines
Drop -n & -z test operators -- they're redundant in [[ ]].

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.16 2011/09/23 13:56:29 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 pushd "${EGIT_DIR}" > /dev/null
221 if [[ ${EGIT_REPACK} || ${EGIT_PRUNE} ]]; then
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 fi
228 popd > /dev/null
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 git clone ${EGIT_LOCAL_OPTIONS} "${repo_uri}" "${EGIT_DIR}"
294 if [[ $? -eq 0 ]]; then
295 # global variable containing the repo_name we will be using
296 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
297 EGIT_REPO_URI_SELECTED="${repo_uri}"
298 break
299 fi
300 done
301
302 [[ ${EGIT_REPO_URI_SELECTED} ]] \
303 || die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
304 }
305
306 # @FUNCTION: git-2_update_repo
307 # @DESCRIPTION:
308 # Internal function running update command on specified repo_uri.
309 git-2_update_repo() {
310 debug-print-function ${FUNCNAME} "$@"
311
312 local repo_uri
313
314 if [[ ${EGIT_LOCAL_NONBARE} ]]; then
315 # checkout master branch and drop all other local branches
316 git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
317 for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
318 debug-print "${FUNCNAME}: git branch -D ${x}"
319 git branch -D ${x} > /dev/null
320 done
321 fi
322
323 EGIT_REPO_URI_SELECTED=""
324 for repo_uri in ${EGIT_REPO_URI}; do
325 # git urls might change, so reset it
326 git config remote.origin.url "${repo_uri}"
327
328 debug-print "${EGIT_UPDATE_CMD}"
329 ${EGIT_UPDATE_CMD} > /dev/null
330 if [[ $? -eq 0 ]]; then
331 # global variable containing the repo_name we will be using
332 debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
333 EGIT_REPO_URI_SELECTED="${repo_uri}"
334 break
335 fi
336 done
337
338 [[ ${EGIT_REPO_URI_SELECTED} ]] \
339 || die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
340 }
341
342 # @FUNCTION: git-2_fetch
343 # @DESCRIPTION:
344 # Internal function fetching repository from EGIT_REPO_URI and storing it in
345 # specified EGIT_STORE_DIR.
346 git-2_fetch() {
347 debug-print-function ${FUNCNAME} "$@"
348
349 local oldsha cursha repo_type
350
351 [[ ${EGIT_LOCAL_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
352
353 if [[ ! -d ${EGIT_DIR} ]]; then
354 git-2_initial_clone
355 pushd "${EGIT_DIR}" > /dev/null
356 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
357 echo "GIT NEW clone -->"
358 echo " repository: ${EGIT_REPO_URI_SELECTED}"
359 echo " at the commit: ${cursha}"
360
361 popd > /dev/null
362 elif [[ ${EVCS_OFFLINE} ]]; then
363 pushd "${EGIT_DIR}" > /dev/null
364 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
365 echo "GIT offline update -->"
366 echo " repository: $(git config remote.origin.url)"
367 echo " at the commit: ${cursha}"
368 popd > /dev/null
369 else
370 pushd "${EGIT_DIR}" > /dev/null
371 oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
372 git-2_update_repo
373 cursha=$(git rev-parse ${UPSTREAM_BRANCH})
374
375 # fetch updates
376 echo "GIT update -->"
377 echo " repository: ${EGIT_REPO_URI_SELECTED}"
378 # write out message based on the revisions
379 if [[ "${oldsha}" != "${cursha}" ]]; then
380 echo " updating from commit: ${oldsha}"
381 echo " to commit: ${cursha}"
382 else
383 echo " at the commit: ${cursha}"
384 fi
385
386 # print nice statistic of what was changed
387 git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
388 popd > /dev/null
389 fi
390 # export the version the repository is at
391 export EGIT_VERSION="${cursha}"
392 # log the repo state
393 [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
394 && echo " commit: ${EGIT_COMMIT}"
395 echo " branch: ${EGIT_BRANCH}"
396 echo " storage directory: \"${EGIT_DIR}\""
397 echo " checkout type: ${repo_type}"
398 }
399
400 # @FUNCTION: git_bootstrap
401 # @DESCRIPTION:
402 # Internal function that runs bootstrap command on unpacked source.
403 git-2_bootstrap() {
404 debug-print-function ${FUNCNAME} "$@"
405
406 # @ECLASS-VARIABLE: EGIT_BOOTSTRAP
407 # @DESCRIPTION:
408 # Command to be executed after checkout and clone of the specified
409 # repository.
410 # enviroment the package will fail if there is no update, thus in
411 # combination with --keep-going it would lead in not-updating
412 # pakcages that are up-to-date.
413 if [[ ${EGIT_BOOTSTRAP} ]]; then
414 pushd "${EGIT_SOURCEDIR}" > /dev/null
415 einfo "Starting bootstrap"
416
417 if [[ -f ${EGIT_BOOTSTRAP} ]]; then
418 # we have file in the repo which we should execute
419 debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
420
421 if [[ -x ${EGIT_BOOTSTRAP} ]]; then
422 eval "./${EGIT_BOOTSTRAP}" \
423 || die "${FUNCNAME}: bootstrap script failed"
424 else
425 eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
426 eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
427 die "\"${EGIT_BOOTSTRAP}\" is not executable"
428 fi
429 else
430 # we execute some system command
431 debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
432
433 eval "${EGIT_BOOTSTRAP}" \
434 || die "${FUNCNAME}: bootstrap commands failed"
435 fi
436
437 einfo "Bootstrap finished"
438 popd > /dev/null
439 fi
440 }
441
442 # @FUNCTION: git-2_migrate_repository
443 # @DESCRIPTION:
444 # Internal function migrating between bare and normal checkout repository.
445 # This is based on usage of EGIT_SUBMODULES, at least until they
446 # start to work with bare checkouts sanely.
447 # This function also set some global variables that differ between
448 # bare and non-bare checkout.
449 git-2_migrate_repository() {
450 debug-print-function ${FUNCNAME} "$@"
451
452 local target returnstate
453
454 # first find out if we have submodules
455 if [[ ! ${EGIT_HAS_SUBMODULES} ]]; then
456 target="bare"
457 else
458 target="full"
459 fi
460 # check if user didn't specify that we want non-bare repo
461 if [[ ${EGIT_NONBARE} ]]; then
462 target="full"
463 EGIT_LOCAL_NONBARE="true"
464 fi
465
466 # test if we already have some repo and if so find out if we have
467 # to migrate the data
468 if [[ -d ${EGIT_DIR} ]]; then
469 if [[ ${target} == bare && -d ${EGIT_DIR}/.git ]]; then
470 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
471
472 ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
473 mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
474 export GIT_DIR="${EGIT_DIR}.bare"
475 git config core.bare true > /dev/null
476 returnstate=$?
477 unset GIT_DIR
478 rm -rf "${EGIT_DIR}"
479 mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
480 eend ${returnstate}
481 fi
482 if [[ ${target} == full && ! -d ${EGIT_DIR}/.git ]]; then
483 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
484
485 ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
486 git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
487 returnstate=$?
488 rm -rf "${EGIT_DIR}"
489 mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
490 eend ${returnstate}
491 fi
492 fi
493 if [[ ${returnstate} -ne 0 ]]; then
494 debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
495
496 # migration failed, remove the EGIT_DIR to play it safe
497 einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
498 rm -rf "${EGIT_DIR}"
499 fi
500
501 # set various options to work with both targets
502 if [[ ${target} == bare ]]; then
503 debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
504 EGIT_LOCAL_OPTIONS+="${EGIT_OPTIONS} --bare"
505 MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
506 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
507 UPSTREAM_BRANCH="${EGIT_BRANCH}"
508 else
509 debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
510 MOVE_COMMAND="cp -pPR ."
511 EGIT_LOCAL_OPTIONS="${EGIT_OPTIONS}"
512 EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
513 UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
514 EGIT_LOCAL_NONBARE="true"
515 fi
516 }
517
518 # @FUNCTION: git-2_cleanup
519 # @DESCRIPTION:
520 # Internal function cleaning up all the global variables
521 # that are not required after the unpack has been done.
522 git-2_cleanup() {
523 debug-print-function ${FUNCNAME} "$@"
524
525 # Here we can unset only variables that are GLOBAL
526 # defined by the eclass, BUT NOT subject to change
527 # by user (like EGIT_PROJECT).
528 # If ebuild writer polutes his environment it is
529 # his problem only.
530 unset EGIT_DIR
531 unset MOVE_COMMAND
532 unset EGIT_LOCAL_OPTIONS
533 unset EGIT_UPDATE_CMD
534 unset UPSTREAM_BRANCH
535 unset EGIT_LOCAL_NONBARE
536 }
537
538 # @FUNCTION: git-2_src_unpack
539 # @DESCRIPTION:
540 # Default git src_unpack function.
541 git-2_src_unpack() {
542 debug-print-function ${FUNCNAME} "$@"
543
544 git-2_init_variables
545 git-2_prepare_storedir
546 git-2_migrate_repository
547 git-2_fetch "$@"
548 git-2_gc
549 git-2_submodules
550 git-2_move_source
551 git-2_branch
552 git-2_bootstrap
553 git-2_cleanup
554 echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
555
556 # Users can specify some SRC_URI and we should
557 # unpack the files too.
558 if [[ ! ${EGIT_NOUNPACK} ]]; then
559 if has ${EAPI:-0} 0 1; then
560 [[ ${A} ]] && unpack ${A}
561 else
562 default_src_unpack
563 fi
564 fi
565 }

  ViewVC Help
Powered by ViewVC 1.1.20