blob: e7b5155ee006f89a5e327d20ee052e46a8e03b71 [file] [log] [blame]
Patrick Williamsb48b7b42016-08-17 15:04:38 -05001# Copyright (C) 2009 Chris Larson <clarson@kergoth.com>
2# Released under the MIT license (see COPYING.MIT for the terms)
3#
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.
7
8def git_drop_tag_prefix(version):
9 import re
10 if re.match("v\d", version):
11 return version[1:]
12 else:
13 return version
14
15GIT_TAGADJUST = "git_drop_tag_prefix(version)"
16GITVER = "${@get_git_pv('${S}', d, tagadjust=lambda version:${GIT_TAGADJUST})}"
17GITSHA = "${@get_git_hash('${S}', d)}"
18
19def gitrev_run(cmd, path):
20 (output, error) = bb.process.run(cmd, cwd=path)
21 return output.rstrip()
22
23def get_git_pv(path, d, tagadjust=None):
24 import os
25 import bb.process
26
27 gitdir = os.path.abspath(os.path.join(d.getVar("S", True), ".git"))
28 try:
29 ver = gitrev_run("git describe --tags", gitdir)
30 except Exception, exc:
31 bb.fatal(str(exc))
32
33 if not ver:
34 try:
35 ver = gitrev_run("git rev-parse --short HEAD", gitdir)
36 except Exception, exc:
37 bb.fatal(str(exc))
38
39 if ver:
40 return "0.0+%s" % ver
41 else:
42 return "0.0"
43 else:
44 if tagadjust:
45 ver = tagadjust(ver)
46 return ver
47
48def mark_recipe_dependencies(path, d):
49 from bb.parse import mark_dependency
50
51 gitdir = os.path.join(path, ".git")
52
53 # Force the recipe to be reparsed so the version gets bumped
54 # if the active branch is switched, or if the branch changes.
55 mark_dependency(d, os.path.join(gitdir, "HEAD"))
56
57 # Force a reparse if anything in the index changes.
58 mark_dependency(d, os.path.join(gitdir, "index"))
59
60 try:
61 ref = gitrev_run("git symbolic-ref -q HEAD", gitdir)
62 except bb.process.CmdError:
63 pass
64 else:
65 if ref:
66 mark_dependency(d, os.path.join(gitdir, ref))
67
68 # Catch new tags.
69 tagdir = os.path.join(gitdir, "refs", "tags")
70 if os.path.exists(tagdir):
71 mark_dependency(d, tagdir)
72
73python () {
74 mark_recipe_dependencies(d.getVar("S", True), d)
75}