gitver.bbclass 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. def git_drop_tag_prefix(version):
  8. import re
  9. if re.match("v\d", version):
  10. return version[1:]
  11. else:
  12. return version
  13. GIT_TAGADJUST = "git_drop_tag_prefix(version)"
  14. GITVER = "${@get_git_pv(d, tagadjust=lambda version:${GIT_TAGADJUST})}"
  15. GITSHA = "${@get_git_hash(d)}"
  16. def gitrev_run(cmd, path):
  17. (output, error) = bb.process.run(cmd, cwd=path)
  18. return output.rstrip()
  19. def get_git_pv(d, tagadjust=None):
  20. import os
  21. srcdir = d.getVar("EXTERNALSRC") or d.getVar("S")
  22. gitdir = os.path.abspath(os.path.join(srcdir, ".git"))
  23. try:
  24. ver = gitrev_run("git describe --tags", gitdir)
  25. except:
  26. try:
  27. ver = gitrev_run("git rev-parse --short HEAD", gitdir)
  28. if ver:
  29. return "0.0+%s" % ver
  30. else:
  31. return "0.0"
  32. except Exception as exc:
  33. raise bb.parse.SkipRecipe(str(exc))
  34. if ver and tagadjust:
  35. ver = tagadjust(ver)
  36. return ver
  37. def get_git_hash(d):
  38. import os
  39. srcdir = d.getVar("EXTERNALSRC") or d.getVar("S")
  40. gitdir = os.path.abspath(os.path.join(srcdir, ".git"))
  41. try:
  42. rev = gitrev_run("git rev-list HEAD -1", gitdir)
  43. return rev[:7]
  44. except Exception as exc:
  45. bb.fatal(str(exc))
  46. def mark_recipe_dependencies(path, d):
  47. from bb.parse import mark_dependency
  48. gitdir = os.path.join(path, ".git")
  49. # Force the recipe to be reparsed so the version gets bumped
  50. # if the active branch is switched, or if the branch changes.
  51. mark_dependency(d, os.path.join(gitdir, "HEAD"))
  52. # Force a reparse if anything in the index changes.
  53. mark_dependency(d, os.path.join(gitdir, "index"))
  54. try:
  55. ref = gitrev_run("git symbolic-ref -q HEAD", gitdir)
  56. except bb.process.CmdError:
  57. pass
  58. else:
  59. if ref:
  60. mark_dependency(d, os.path.join(gitdir, ref))
  61. # Catch new tags.
  62. tagdir = os.path.join(gitdir, "refs", "tags")
  63. if os.path.exists(tagdir):
  64. mark_dependency(d, tagdir)
  65. python () {
  66. srcdir = d.getVar("EXTERNALSRC") or d.getVar("S")
  67. mark_recipe_dependencies(srcdir, d)
  68. }