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

Contents of /eclass/git-2.eclass

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations) (download)
Thu May 19 12:03:41 2011 UTC (2 years, 10 months ago) by scarabeus
Branch: MAIN
Changes since 1.5: +17 -3 lines
Add EGIT_PROJECT variable that allows us to specify folder where to check out the git repository.

1 scarabeus 1.1 # Copyright 1999-2011 Gentoo Foundation
2     # Distributed under the terms of the GNU General Public License v2
3 scarabeus 1.6 # $Header: /var/cvsroot/gentoo-x86/eclass/git-2.eclass,v 1.5 2011/05/02 21:15:30 mgorny Exp $
4 scarabeus 1.1
5     # @ECLASS: git-2.eclass
6     # @MAINTAINER:
7     # Tomas Chvatal <scarabeus@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 scarabeus 1.6 # @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 scarabeus 1.1 # @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 scarabeus 1.6 # EGIT_DIR="${EGIT_STORE_DIR}/${EGIT_PROJECT}"
63 scarabeus 1.1
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 scarabeus 1.4 # EGIT_COMMIT="${EGIT_BRANCH}"
95 scarabeus 1.1
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     cd -P "${EGIT_STORE_DIR}" \
242     || die "${FUNCNAME}: can't chdir to \"${EGIT_STORE_DIR}\""
243     # allow writing into EGIT_STORE_DIR
244     addwrite "${EGIT_STORE_DIR}"
245     # calculate the proper store dir for data
246 scarabeus 1.6 # If user didn't specify the EGIT_DIR, we check if he did specify
247     # the EGIT_PROJECT or get the folder name from EGIT_REPO_URI.
248 scarabeus 1.1 [[ -z ${EGIT_REPO_URI##*/} ]] && EGIT_REPO_URI="${EGIT_REPO_URI%/}"
249     if [[ -z ${EGIT_DIR} ]]; then
250 scarabeus 1.6 if [[ -n ${EGIT_PROJECT} ]]; then
251     clone_dir=${EGIT_PROJECT}
252     else
253     clone_dir=${EGIT_REPO_URI##*/}
254     fi
255 scarabeus 1.1 EGIT_DIR=${EGIT_STORE_DIR}/${clone_dir}
256     fi
257     export EGIT_DIR=${EGIT_DIR}
258     debug-print "${FUNCNAME}: Storing the repo into \"${EGIT_DIR}\"."
259     }
260    
261     # @FUNCTION: git-2_move_source
262     # @DESCRIPTION:
263     # Internal function moving sources from the EGIT_DIR to EGIT_SOURCEDIR dir.
264     git-2_move_source() {
265     debug-print-function ${FUNCNAME} "$@"
266    
267     debug-print "${FUNCNAME}: ${MOVE_COMMAND} \"${EGIT_DIR}\" \"${EGIT_SOURCEDIR}\""
268     pushd "${EGIT_DIR}" > /dev/null
269     mkdir -p "${EGIT_SOURCEDIR}" \
270     || die "${FUNCNAME}: failed to create ${EGIT_SOURCEDIR}"
271     ${MOVE_COMMAND} "${EGIT_SOURCEDIR}" \
272     || die "${FUNCNAME}: sync to \"${EGIT_SOURCEDIR}\" failed"
273     popd > /dev/null
274     }
275    
276     # @FUNCTION: git-2_initial_clone
277     # @DESCRIPTION:
278     # Internal function running initial clone on specified repo_uri.
279     git-2_initial_clone() {
280     debug-print-function ${FUNCNAME} "$@"
281    
282     local repo_uri
283    
284     EGIT_REPO_URI_SELECTED=""
285     for repo_uri in ${EGIT_REPO_URI}; do
286     debug-print "${FUNCNAME}: git clone ${EGIT_OPTIONS} \"${repo_uri}\" \"${EGIT_DIR}\""
287     git clone ${EGIT_OPTIONS} "${repo_uri}" "${EGIT_DIR}"
288     if [[ $? -eq 0 ]]; then
289     # global variable containing the repo_name we will be using
290     debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
291     EGIT_REPO_URI_SELECTED="${repo_uri}"
292     break
293     fi
294     done
295    
296     if [[ -z ${EGIT_REPO_URI_SELECTED} ]]; then
297     die "${FUNCNAME}: can't fetch from ${EGIT_REPO_URI}"
298     fi
299     }
300    
301     # @FUNCTION: git-2_update_repo
302     # @DESCRIPTION:
303     # Internal function running update command on specified repo_uri.
304     git-2_update_repo() {
305     debug-print-function ${FUNCNAME} "$@"
306    
307     local repo_uri
308    
309     if [[ -n ${EGIT_NONBARE} ]]; then
310     # checkout master branch and drop all other local branches
311     git checkout ${EGIT_MASTER} || die "${FUNCNAME}: can't checkout master branch ${EGIT_MASTER}"
312     for x in $(git branch | grep -v "* ${EGIT_MASTER}" | tr '\n' ' '); do
313     debug-print "${FUNCNAME}: git branch -D ${x}"
314     git branch -D ${x} > /dev/null
315     done
316     fi
317    
318     EGIT_REPO_URI_SELECTED=""
319     for repo_uri in ${EGIT_REPO_URI}; do
320     # git urls might change, so reset it
321     git config remote.origin.url "${repo_uri}"
322    
323 scarabeus 1.3 debug-print "${EGIT_UPDATE_CMD}"
324 scarabeus 1.1 ${EGIT_UPDATE_CMD} > /dev/null
325     if [[ $? -eq 0 ]]; then
326     # global variable containing the repo_name we will be using
327     debug-print "${FUNCNAME}: EGIT_REPO_URI_SELECTED=\"${repo_uri}\""
328     EGIT_REPO_URI_SELECTED="${repo_uri}"
329     break
330     fi
331     done
332    
333     if [[ -z ${EGIT_REPO_URI_SELECTED} ]]; then
334     die "${FUNCNAME}: can't update from ${EGIT_REPO_URI}"
335     fi
336     }
337    
338     # @FUNCTION: git-2_fetch
339     # @DESCRIPTION:
340     # Internal function fetching repository from EGIT_REPO_URI and storing it in
341     # specified EGIT_STORE_DIR.
342     git-2_fetch() {
343     debug-print-function ${FUNCNAME} "$@"
344    
345     local oldsha cursha repo_type
346    
347     [[ -n ${EGIT_NONBARE} ]] && repo_type="non-bare repository" || repo_type="bare repository"
348    
349     if [[ ! -d ${EGIT_DIR} ]]; then
350     git-2_initial_clone
351     pushd "${EGIT_DIR}" > /dev/null
352     cursha=$(git rev-parse ${UPSTREAM_BRANCH})
353     echo "GIT NEW clone -->"
354     echo " repository: ${EGIT_REPO_URI_SELECTED}"
355     echo " at the commit: ${cursha}"
356    
357     popd > /dev/null
358     elif [[ -n ${EVCS_OFFLINE} ]]; then
359     pushd "${EGIT_DIR}" > /dev/null
360     cursha=$(git rev-parse ${UPSTREAM_BRANCH})
361     echo "GIT offline update -->"
362     echo " repository: $(git config remote.origin.url)"
363     echo " at the commit: ${cursha}"
364     popd > /dev/null
365     else
366     pushd "${EGIT_DIR}" > /dev/null
367     oldsha=$(git rev-parse ${UPSTREAM_BRANCH})
368     git-2_update_repo
369     cursha=$(git rev-parse ${UPSTREAM_BRANCH})
370    
371     # fetch updates
372     echo "GIT update -->"
373     echo " repository: ${EGIT_REPO_URI_SELECTED}"
374     # write out message based on the revisions
375 mgorny 1.5 if [[ "${oldsha}" != "${cursha}" ]]; then
376 scarabeus 1.1 echo " updating from commit: ${oldsha}"
377     echo " to commit: ${cursha}"
378     else
379     echo " at the commit: ${cursha}"
380     fi
381    
382     # print nice statistic of what was changed
383     git --no-pager diff --stat ${oldsha}..${UPSTREAM_BRANCH}
384     popd > /dev/null
385     fi
386     # export the version the repository is at
387 mgorny 1.5 export EGIT_VERSION="${cursha}"
388 scarabeus 1.1 # log the repo state
389     [[ ${EGIT_COMMIT} != ${EGIT_BRANCH} ]] \
390     && echo " commit: ${EGIT_COMMIT}"
391     echo " branch: ${EGIT_BRANCH}"
392     echo " storage directory: \"${EGIT_DIR}\""
393     echo " checkout type: ${repo_type}"
394     }
395    
396     # @FUNCTION: git_bootstrap
397     # @DESCRIPTION:
398     # Internal function that runs bootstrap command on unpacked source.
399     git-2_bootstrap() {
400     debug-print-function ${FUNCNAME} "$@"
401    
402     # @ECLASS_VARIABLE: EGIT_BOOTSTRAP
403     # @DESCRIPTION:
404     # Command to be executed after checkout and clone of the specified
405     # repository.
406     # enviroment the package will fail if there is no update, thus in
407     # combination with --keep-going it would lead in not-updating
408     # pakcages that are up-to-date.
409     if [[ -n ${EGIT_BOOTSTRAP} ]]; then
410     pushd "${EGIT_SOURCEDIR}" > /dev/null
411     einfo "Starting bootstrap"
412    
413     if [[ -f ${EGIT_BOOTSTRAP} ]]; then
414     # we have file in the repo which we should execute
415     debug-print "${FUNCNAME}: bootstraping with file \"${EGIT_BOOTSTRAP}\""
416    
417     if [[ -x ${EGIT_BOOTSTRAP} ]]; then
418     eval "./${EGIT_BOOTSTRAP}" \
419     || die "${FUNCNAME}: bootstrap script failed"
420     else
421     eerror "\"${EGIT_BOOTSTRAP}\" is not executable."
422     eerror "Report upstream, or bug ebuild maintainer to remove bootstrap command."
423     die "\"${EGIT_BOOTSTRAP}\" is not executable"
424     fi
425     else
426     # we execute some system command
427     debug-print "${FUNCNAME}: bootstraping with commands \"${EGIT_BOOTSTRAP}\""
428    
429     eval "${EGIT_BOOTSTRAP}" \
430     || die "${FUNCNAME}: bootstrap commands failed"
431     fi
432    
433     einfo "Bootstrap finished"
434     popd > /dev/null
435     fi
436     }
437    
438     # @FUNCTION: git-2_migrate_repository
439     # @DESCRIPTION:
440     # Internal function migrating between bare and normal checkout repository.
441     # This is based on usage of EGIT_SUBMODULES, at least until they
442     # start to work with bare checkouts sanely.
443     git-2_migrate_repository() {
444     debug-print-function ${FUNCNAME} "$@"
445    
446     local target returnstate
447    
448     # first find out if we have submodules
449     if [[ -z ${EGIT_HAS_SUBMODULES} ]]; then
450     target="bare"
451     else
452     target="full"
453     fi
454     [[ -n ${EGIT_NONBARE} ]] && target="full"
455    
456     # test if we already have some repo and if so find out if we have
457     # to migrate the data
458     if [[ -d ${EGIT_DIR} ]]; then
459     if [[ ${target} == bare && -d ${EGIT_DIR}/.git ]]; then
460     debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to bare copy"
461    
462     ebegin "Converting \"${EGIT_DIR}\" from non-bare to bare copy"
463     mv "${EGIT_DIR}/.git" "${EGIT_DIR}.bare"
464     export GIT_DIR="${EGIT_DIR}.bare"
465     git config core.bare true > /dev/null
466     returnstate=$?
467     unset GIT_DIR
468     rm -rf "${EGIT_DIR}"
469     mv "${EGIT_DIR}.bare" "${EGIT_DIR}"
470     eend ${returnstate}
471     fi
472     if [[ ${target} == full && ! -d ${EGIT_DIR}/.git ]]; then
473     debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" to non-bare copy"
474    
475     ebegin "Converting \"${EGIT_DIR}\" from bare to non-bare copy"
476     git clone -l "${EGIT_DIR}" "${EGIT_DIR}.nonbare" > /dev/null
477     returnstate=$?
478     rm -rf "${EGIT_DIR}"
479     mv "${EGIT_DIR}.nonbare" "${EGIT_DIR}"
480     eend ${returnstate}
481     fi
482     fi
483     if [[ ${returnstate} -ne 0 ]]; then
484     debug-print "${FUNCNAME}: converting \"${EGIT_DIR}\" failed, removing to start from scratch"
485    
486     # migration failed, remove the EGIT_DIR to play it safe
487     einfo "Migration failed, removing \"${EGIT_DIR}\" to start from scratch."
488     rm -rf "${EGIT_DIR}"
489     fi
490    
491     # set various options to work with both targets
492     if [[ ${target} == bare ]]; then
493     debug-print "${FUNCNAME}: working in bare repository for \"${EGIT_DIR}\""
494     EGIT_OPTIONS+=" --bare"
495     MOVE_COMMAND="git clone -l -s -n ${EGIT_DIR// /\\ }"
496 scarabeus 1.2 EGIT_UPDATE_CMD="git fetch -t -f -u origin ${EGIT_BRANCH}:${EGIT_BRANCH}"
497 scarabeus 1.1 UPSTREAM_BRANCH="${EGIT_BRANCH}"
498     else
499     debug-print "${FUNCNAME}: working in bare repository for non-bare \"${EGIT_DIR}\""
500     MOVE_COMMAND="cp -pPR ."
501     EGIT_UPDATE_CMD="git pull -f -u ${EGIT_OPTIONS}"
502     UPSTREAM_BRANCH="origin/${EGIT_BRANCH}"
503     EGIT_NONBARE="true"
504     fi
505     }
506    
507     # @FUNCTION: git-2_src_unpack
508     # @DESCRIPTION:
509     # Default git src_upack function.
510     git-2_src_unpack() {
511     debug-print-function ${FUNCNAME} "$@"
512    
513     git-2_init_variables
514     git-2_prepare_storedir
515     git-2_migrate_repository
516     git-2_fetch "$@"
517     git-2_gc
518     git-2_submodules
519     git-2_move_source
520     git-2_branch
521     git-2_bootstrap
522     echo ">>> Unpacked to ${EGIT_SOURCEDIR}"
523     }

  ViewVC Help
Powered by ViewVC 1.1.20