gitpkgv.bbclass 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # gitpkgv.bbclass provides a GITPKGV and GITPKGVTAG variables to be
  2. # used in PKGV, as described bellow:
  3. #
  4. # - GITPKGV which is a sortable version with the format NN+GITHASH, to
  5. # be used in PKGV, where
  6. #
  7. # NN equals the total number of revs up to SRCREV
  8. # GITHASH is SRCREV's (full) hash
  9. #
  10. # - GITPKGVTAG which is the output of 'git describe --tags --exact-match'
  11. # allowing for automatic versioning
  12. #
  13. # gitpkgv.bbclass assumes the git repository has been cloned, and
  14. # contains SRCREV. So ${GITPKGV} and ${GITPKGVTAG} should never be
  15. # used in PV, only in PKGV. It can handle SRCREV = ${AUTOREV}, as
  16. # well as SRCREV = "<some fixed git hash>".
  17. #
  18. # WARNING: if upstream repository is always using consistent and
  19. # sortable tag name scheme you can get sortable version including tag
  20. # name with ${GITPKGVTAG}, but be aware that ie tag sequence "v1.0,
  21. # v1.2, xtest, v2.0" will force you to increment PE to get upgradeable
  22. # path to v2.0 revisions
  23. #
  24. # use example:
  25. #
  26. # inherit gitpkgv
  27. #
  28. # PV = "1.0+gitr${SRCPV}" # expands to something like 1.0+gitr3+4c1c21d7dbbf93b0df336994524313dfe0d4963b
  29. # PKGV = "1.0+gitr${GITPKGV}" # expands also to something like 1.0+gitr31337+4c1c21d7d
  30. #
  31. # or
  32. #
  33. # inherit gitpkgv
  34. #
  35. # PV = "1.0+gitr${SRCPV}" # expands to something like 1.0+gitr3+4c1c21d7dbbf93b0df336994524313dfe0d4963b
  36. # PKGV = "${GITPKGVTAG}" # expands to something like 1.0-31337+g4c1c21d
  37. # if there is tag v1.0 before this revision or
  38. # ver1.0-31337+g4c1c21d if there is tag ver1.0
  39. GITPKGV = "${@get_git_pkgv(d, False)}"
  40. GITPKGVTAG = "${@get_git_pkgv(d, True)}"
  41. # This regexp is used to drop unwanted parts of the found tags. Any matching
  42. # groups will be concatenated to yield the final version.
  43. GITPKGV_TAG_REGEXP ??= "v(\d.*)"
  44. def gitpkgv_drop_tag_prefix(d, version):
  45. import re
  46. m = re.match(d.getVar('GITPKGV_TAG_REGEXP'), version)
  47. if m:
  48. return ''.join(group for group in m.groups() if group)
  49. else:
  50. return version
  51. def get_git_pkgv(d, use_tags):
  52. import os
  53. import bb
  54. from pipes import quote
  55. src_uri = d.getVar('SRC_URI').split()
  56. fetcher = bb.fetch2.Fetch(src_uri, d)
  57. ud = fetcher.ud
  58. #
  59. # If SRCREV_FORMAT is set respect it for tags
  60. #
  61. format = d.getVar('SRCREV_FORMAT')
  62. if not format:
  63. names = []
  64. for url in ud.values():
  65. if url.type == 'git' or url.type == 'gitsm':
  66. names.extend(url.revisions.keys())
  67. if len(names) > 0:
  68. format = '_'.join(names)
  69. else:
  70. format = 'default'
  71. found = False
  72. for url in ud.values():
  73. if url.type == 'git' or url.type == 'gitsm':
  74. for name, rev in url.revisions.items():
  75. if not os.path.exists(url.localpath):
  76. return None
  77. found = True
  78. vars = { 'repodir' : quote(url.localpath),
  79. 'rev' : quote(rev) }
  80. rev = bb.fetch2.get_srcrev(d).split('+')[1]
  81. rev_file = os.path.join(url.localpath, "oe-gitpkgv_" + rev)
  82. if not os.path.exists(rev_file) or os.path.getsize(rev_file)==0:
  83. commits = bb.fetch2.runfetchcmd(
  84. "git --git-dir=%(repodir)s rev-list %(rev)s -- 2>/dev/null | wc -l"
  85. % vars, d, quiet=True).strip().lstrip('0')
  86. if commits != "":
  87. oe.path.remove(rev_file, recurse=False)
  88. with open(rev_file, "w") as f:
  89. f.write("%d\n" % int(commits))
  90. else:
  91. commits = "0"
  92. else:
  93. with open(rev_file, "r") as f:
  94. commits = f.readline(128).strip()
  95. if use_tags:
  96. try:
  97. output = bb.fetch2.runfetchcmd(
  98. "git --git-dir=%(repodir)s describe %(rev)s --tags --exact-match 2>/dev/null"
  99. % vars, d, quiet=True).strip()
  100. ver = gitpkgv_drop_tag_prefix(d, output)
  101. except Exception:
  102. ver = "0.0-%s-g%s" % (commits, vars['rev'][:7])
  103. else:
  104. ver = "%s+%s" % (commits, vars['rev'][:7])
  105. format = format.replace(name, ver)
  106. if found:
  107. return format
  108. return '0+0'