Merge branch 'org.openembedded.dev' of git@git.openembedded.org:openembedded into...
[openembedded.git] / classes / gitver.bbclass
blob92c053ae24fcba3052efaaeed7ebc01853224741
1 # Copyright (C) 2009 Chris Larson <clarson@kergoth.com>
2 # Released under the MIT license (see COPYING.MIT for the terms)
4 # gitver.bbclass provides a GITVER variable which is a (fairly) sane version,
5 # for use in ${PV}, extracted from the ${S} git checkout, assuming it is one.
6 # This is most useful in concert with srctree.bbclass.
9 GITVER = "${@get_git_pv('${S}', d)}"
11 def gitver_mark_dependency(d):
12     from bb.data import expand
13     from bb.parse import mark_dependency
14     from os.path import abspath
16     fn = abspath(expand("${S}/.git/HEAD", d))
17     mark_dependency(d, fn)
19 def get_git_pv(path, d, tagadjust=None):
20     from subprocess import Popen, PIPE
21     from os.path import join
22     from bb import error
24     env = {"GIT_DIR": join(d.getVar("S", True), ".git")}
26     def popen(cmd, **kwargs):
27         kwargs["stderr"] = PIPE
28         kwargs["stdout"] = PIPE
29         kwargs["env"] = env
30         try:
31             pipe = Popen(cmd, **kwargs)
32         except OSError, e:
33             #error("Execution of %s failed: %s" % (cmd, e))
34             return
36         (stdout, stderr) = pipe.communicate(None)
37         if pipe.returncode != 0:
38             #error("Execution of %s failed: %s" % (cmd, stderr))
39             return
40         return stdout.rstrip()
42     gitver_mark_dependency(d)
44     ver = popen(["git", "describe", "--tags"], cwd=path)
45     if not ver:
46         ver = popen(["git", "rev-parse", "--short", "HEAD"], cwd=path)
47         if ver:
48             return "0.0-%s" % ver
49         else:
50             return "0.0"
51     else:
52         if tagadjust:
53             ver = tagadjust(ver)
54         return ver