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 get_git_pv(path, d, tagadjust=None):
12 from subprocess import Popen, PIPE
15 from bb.parse import mark_dependency
17 gitdir = os.path.abspath(os.path.join(d.getVar("S", True), ".git"))
18 env = { "GIT_DIR": gitdir }
20 def popen(cmd, **kwargs):
21 kwargs["stderr"] = PIPE
22 kwargs["stdout"] = PIPE
25 pipe = Popen(cmd, **kwargs)
27 #error("Execution of %s failed: %s" % (cmd, e))
30 (stdout, stderr) = pipe.communicate(None)
31 if pipe.returncode != 0:
32 #error("Execution of %s failed: %s" % (cmd, stderr))
34 return stdout.rstrip()
36 # Force the recipe to be reparsed so the version gets bumped
37 # if the active branch is switched, or if the branch changes.
38 mark_dependency(d, os.path.join(gitdir, "HEAD"))
40 ref = popen(["git", "symbolic-ref", "HEAD"])
42 reffile = os.path.join(gitdir, ref)
43 if os.path.exists(reffile):
44 mark_dependency(d, reffile)
46 mark_dependency(d, os.path.join(gitdir, "index"))
48 # The ref might be hidden in packed-refs. Force a reparse if anything
49 # in the working copy changes.
50 mark_dependency(d, os.path.join(gitdir, "index"))
53 tagdir = os.path.join(gitdir, "refs", "tags")
54 if os.path.exists(tagdir):
55 mark_dependency(d, tagdir)
57 ver = popen(["git", "describe", "--tags"], cwd=path)
59 ver = popen(["git", "rev-parse", "--short", "HEAD"], cwd=path)