diff options
Diffstat (limited to 'meta-oe/classes/gitver.bbclass')
-rw-r--r-- | meta-oe/classes/gitver.bbclass | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/meta-oe/classes/gitver.bbclass b/meta-oe/classes/gitver.bbclass new file mode 100644 index 000000000..e7b5155ee --- /dev/null +++ b/meta-oe/classes/gitver.bbclass | |||
@@ -0,0 +1,75 @@ | |||
1 | # 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 | |||
8 | def 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 | |||
15 | GIT_TAGADJUST = "git_drop_tag_prefix(version)" | ||
16 | GITVER = "${@get_git_pv('${S}', d, tagadjust=lambda version:${GIT_TAGADJUST})}" | ||
17 | GITSHA = "${@get_git_hash('${S}', d)}" | ||
18 | |||
19 | def gitrev_run(cmd, path): | ||
20 | (output, error) = bb.process.run(cmd, cwd=path) | ||
21 | return output.rstrip() | ||
22 | |||
23 | def 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 | |||
48 | def 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 | |||
73 | python () { | ||
74 | mark_recipe_dependencies(d.getVar("S", True), d) | ||
75 | } | ||