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"])
41 reffile = os.path.join(gitdir, ref)
42 if ref and os.path.exists(reffile):
43 mark_dependency(d, reffile)
45 # The ref might be hidden in packed-refs. Force a reparse if anything
46 # in the working copy changes.
47 mark_dependency(d, os.path.join(gitdir, "index"))
50 tagdir = os.path.join(gitdir, "refs", "tags")
51 if os.path.exists(tagdir):
52 mark_dependency(d, tagdir)
54 ver = popen(["git", "describe", "--tags"], cwd=path)
56 ver = popen(["git", "rev-parse", "--short", "HEAD"], cwd=path)