Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 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)" |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 16 | GITVER = "${@get_git_pv(d, tagadjust=lambda version:${GIT_TAGADJUST})}" |
| 17 | GITSHA = "${@get_git_hash(d)}" |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 18 | |
| 19 | def gitrev_run(cmd, path): |
| 20 | (output, error) = bb.process.run(cmd, cwd=path) |
| 21 | return output.rstrip() |
| 22 | |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 23 | def get_git_pv(d, tagadjust=None): |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 24 | import os |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 25 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 26 | srcdir = d.getVar("EXTERNALSRC") or d.getVar("S") |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 27 | gitdir = os.path.abspath(os.path.join(srcdir, ".git")) |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 28 | try: |
| 29 | ver = gitrev_run("git describe --tags", gitdir) |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 30 | except: |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 31 | try: |
| 32 | ver = gitrev_run("git rev-parse --short HEAD", gitdir) |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 33 | if ver: |
| 34 | return "0.0+%s" % ver |
| 35 | else: |
| 36 | return "0.0" |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 37 | |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 38 | except Exception as exc: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 39 | raise bb.parse.SkipRecipe(str(exc)) |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 40 | |
| 41 | if ver and tagadjust: |
| 42 | ver = tagadjust(ver) |
| 43 | return ver |
| 44 | |
| 45 | def get_git_hash(d): |
| 46 | import os |
| 47 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | srcdir = d.getVar("EXTERNALSRC") or d.getVar("S") |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 49 | gitdir = os.path.abspath(os.path.join(srcdir, ".git")) |
| 50 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 51 | rev = gitrev_run("git rev-list HEAD -1") |
| 52 | return rev[:7] |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 53 | except Exception as exc: |
| 54 | bb.fatal(str(exc)) |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 55 | |
| 56 | def mark_recipe_dependencies(path, d): |
| 57 | from bb.parse import mark_dependency |
| 58 | |
| 59 | gitdir = os.path.join(path, ".git") |
| 60 | |
| 61 | # Force the recipe to be reparsed so the version gets bumped |
| 62 | # if the active branch is switched, or if the branch changes. |
| 63 | mark_dependency(d, os.path.join(gitdir, "HEAD")) |
| 64 | |
| 65 | # Force a reparse if anything in the index changes. |
| 66 | mark_dependency(d, os.path.join(gitdir, "index")) |
| 67 | |
| 68 | try: |
| 69 | ref = gitrev_run("git symbolic-ref -q HEAD", gitdir) |
| 70 | except bb.process.CmdError: |
| 71 | pass |
| 72 | else: |
| 73 | if ref: |
| 74 | mark_dependency(d, os.path.join(gitdir, ref)) |
| 75 | |
| 76 | # Catch new tags. |
| 77 | tagdir = os.path.join(gitdir, "refs", "tags") |
| 78 | if os.path.exists(tagdir): |
| 79 | mark_dependency(d, tagdir) |
| 80 | |
| 81 | python () { |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 82 | srcdir = d.getVar("EXTERNALSRC") or d.getVar("S") |
Brad Bishop | 7f28bc5 | 2017-12-03 23:42:40 -0500 | [diff] [blame] | 83 | mark_recipe_dependencies(srcdir, d) |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 84 | } |