blob: 5ab507969c9f1136a7e738e92ac169ee7692ad66 [file] [log] [blame]
Patrick Williamsb48b7b42016-08-17 15:04:38 -05001# 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#
Brad Bishop8410d612019-11-25 09:40:59 -050010# - GITPKGVTAG which is the output of 'git describe --tags --exact-match'
11# allowing for automatic versioning
Patrick Williamsb48b7b42016-08-17 15:04:38 -050012#
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#
Patrick Williams73bd93f2024-02-20 08:07:48 -060028# PV = "1.0+git" # expands to 1.0+git
29# PKGV = "1.0+git${GITPKGV}" # expands also to something like 1.0+git31337+4c1c21d7d
Patrick Williamsb48b7b42016-08-17 15:04:38 -050030#
31# or
32#
33# inherit gitpkgv
34#
Patrick Williams73bd93f2024-02-20 08:07:48 -060035# PV = "1.0+git" # expands to 1.0+git
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
Patrick Williamsb48b7b42016-08-17 15:04:38 -050039
40GITPKGV = "${@get_git_pkgv(d, False)}"
41GITPKGVTAG = "${@get_git_pkgv(d, True)}"
42
Brad Bishop0e2770c2020-01-21 07:31:46 -050043# This regexp is used to drop unwanted parts of the found tags. Any matching
44# groups will be concatenated to yield the final version.
45GITPKGV_TAG_REGEXP ??= "v(\d.*)"
46
47def gitpkgv_drop_tag_prefix(d, version):
Patrick Williamsb48b7b42016-08-17 15:04:38 -050048 import re
Brad Bishop0e2770c2020-01-21 07:31:46 -050049
50 m = re.match(d.getVar('GITPKGV_TAG_REGEXP'), version)
51 if m:
52 return ''.join(group for group in m.groups() if group)
Patrick Williamsb48b7b42016-08-17 15:04:38 -050053 else:
54 return version
55
56def get_git_pkgv(d, use_tags):
57 import os
58 import bb
Andrew Geissler3eeda902023-05-19 10:14:02 -050059 from shlex import quote
Patrick Williamsb48b7b42016-08-17 15:04:38 -050060
Brad Bishop6e60e8b2018-02-01 10:27:11 -050061 src_uri = d.getVar('SRC_URI').split()
Patrick Williamsb48b7b42016-08-17 15:04:38 -050062 fetcher = bb.fetch2.Fetch(src_uri, d)
63 ud = fetcher.ud
64
65 #
66 # If SRCREV_FORMAT is set respect it for tags
67 #
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 format = d.getVar('SRCREV_FORMAT')
Patrick Williamsb48b7b42016-08-17 15:04:38 -050069 if not format:
Patrick Williamsddad1a12017-02-23 20:36:32 -060070 names = []
71 for url in ud.values():
72 if url.type == 'git' or url.type == 'gitsm':
73 names.extend(url.revisions.keys())
74 if len(names) > 0:
75 format = '_'.join(names)
76 else:
77 format = 'default'
Patrick Williamsb48b7b42016-08-17 15:04:38 -050078
79 found = False
80 for url in ud.values():
81 if url.type == 'git' or url.type == 'gitsm':
82 for name, rev in url.revisions.items():
83 if not os.path.exists(url.localpath):
84 return None
85
86 found = True
87
88 vars = { 'repodir' : quote(url.localpath),
89 'rev' : quote(rev) }
90
91 rev = bb.fetch2.get_srcrev(d).split('+')[1]
92 rev_file = os.path.join(url.localpath, "oe-gitpkgv_" + rev)
93
94 if not os.path.exists(rev_file) or os.path.getsize(rev_file)==0:
95 commits = bb.fetch2.runfetchcmd(
Brad Bishop8410d612019-11-25 09:40:59 -050096 "git --git-dir=%(repodir)s rev-list %(rev)s -- 2>/dev/null | wc -l"
97 % vars, d, quiet=True).strip().lstrip('0')
Patrick Williamsb48b7b42016-08-17 15:04:38 -050098
99 if commits != "":
100 oe.path.remove(rev_file, recurse=False)
Patrick Williamsddad1a12017-02-23 20:36:32 -0600101 with open(rev_file, "w") as f:
102 f.write("%d\n" % int(commits))
Patrick Williamsb48b7b42016-08-17 15:04:38 -0500103 else:
104 commits = "0"
105 else:
Patrick Williamsddad1a12017-02-23 20:36:32 -0600106 with open(rev_file, "r") as f:
107 commits = f.readline(128).strip()
Patrick Williamsb48b7b42016-08-17 15:04:38 -0500108
109 if use_tags:
110 try:
111 output = bb.fetch2.runfetchcmd(
Brad Bishop8410d612019-11-25 09:40:59 -0500112 "git --git-dir=%(repodir)s describe %(rev)s --tags --exact-match 2>/dev/null"
113 % vars, d, quiet=True).strip()
Brad Bishop0e2770c2020-01-21 07:31:46 -0500114 ver = gitpkgv_drop_tag_prefix(d, output)
Patrick Williamsb48b7b42016-08-17 15:04:38 -0500115 except Exception:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500116 ver = "0.0-%s-g%s" % (commits, vars['rev'][:7])
Patrick Williamsb48b7b42016-08-17 15:04:38 -0500117 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500118 ver = "%s+%s" % (commits, vars['rev'][:7])
Patrick Williamsb48b7b42016-08-17 15:04:38 -0500119
120 format = format.replace(name, ver)
121
122 if found:
123 return format
124
125 return '0+0'