Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 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 | # |
Brad Bishop | 8410d61 | 2019-11-25 09:40:59 -0500 | [diff] [blame] | 10 | # - GITPKGVTAG which is the output of 'git describe --tags --exact-match' |
| 11 | # allowing for automatic versioning |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 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 | |
| 40 | GITPKGV = "${@get_git_pkgv(d, False)}" |
| 41 | GITPKGVTAG = "${@get_git_pkgv(d, True)}" |
| 42 | |
| 43 | def gitpkgv_drop_tag_prefix(version): |
| 44 | import re |
| 45 | if re.match("v\d", version): |
| 46 | return version[1:] |
| 47 | else: |
| 48 | return version |
| 49 | |
| 50 | def get_git_pkgv(d, use_tags): |
| 51 | import os |
| 52 | import bb |
| 53 | from pipes import quote |
| 54 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 55 | src_uri = d.getVar('SRC_URI').split() |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 56 | fetcher = bb.fetch2.Fetch(src_uri, d) |
| 57 | ud = fetcher.ud |
| 58 | |
| 59 | # |
| 60 | # If SRCREV_FORMAT is set respect it for tags |
| 61 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 62 | format = d.getVar('SRCREV_FORMAT') |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 63 | if not format: |
Patrick Williams | ddad1a1 | 2017-02-23 20:36:32 -0600 | [diff] [blame] | 64 | names = [] |
| 65 | for url in ud.values(): |
| 66 | if url.type == 'git' or url.type == 'gitsm': |
| 67 | names.extend(url.revisions.keys()) |
| 68 | if len(names) > 0: |
| 69 | format = '_'.join(names) |
| 70 | else: |
| 71 | format = 'default' |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 72 | |
| 73 | found = False |
| 74 | for url in ud.values(): |
| 75 | if url.type == 'git' or url.type == 'gitsm': |
| 76 | for name, rev in url.revisions.items(): |
| 77 | if not os.path.exists(url.localpath): |
| 78 | return None |
| 79 | |
| 80 | found = True |
| 81 | |
| 82 | vars = { 'repodir' : quote(url.localpath), |
| 83 | 'rev' : quote(rev) } |
| 84 | |
| 85 | rev = bb.fetch2.get_srcrev(d).split('+')[1] |
| 86 | rev_file = os.path.join(url.localpath, "oe-gitpkgv_" + rev) |
| 87 | |
| 88 | if not os.path.exists(rev_file) or os.path.getsize(rev_file)==0: |
| 89 | commits = bb.fetch2.runfetchcmd( |
Brad Bishop | 8410d61 | 2019-11-25 09:40:59 -0500 | [diff] [blame] | 90 | "git --git-dir=%(repodir)s rev-list %(rev)s -- 2>/dev/null | wc -l" |
| 91 | % vars, d, quiet=True).strip().lstrip('0') |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 92 | |
| 93 | if commits != "": |
| 94 | oe.path.remove(rev_file, recurse=False) |
Patrick Williams | ddad1a1 | 2017-02-23 20:36:32 -0600 | [diff] [blame] | 95 | with open(rev_file, "w") as f: |
| 96 | f.write("%d\n" % int(commits)) |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 97 | else: |
| 98 | commits = "0" |
| 99 | else: |
Patrick Williams | ddad1a1 | 2017-02-23 20:36:32 -0600 | [diff] [blame] | 100 | with open(rev_file, "r") as f: |
| 101 | commits = f.readline(128).strip() |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 102 | |
| 103 | if use_tags: |
| 104 | try: |
| 105 | output = bb.fetch2.runfetchcmd( |
Brad Bishop | 8410d61 | 2019-11-25 09:40:59 -0500 | [diff] [blame] | 106 | "git --git-dir=%(repodir)s describe %(rev)s --tags --exact-match 2>/dev/null" |
| 107 | % vars, d, quiet=True).strip() |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 108 | ver = gitpkgv_drop_tag_prefix(output) |
| 109 | except Exception: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | ver = "0.0-%s-g%s" % (commits, vars['rev'][:7]) |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 111 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | ver = "%s+%s" % (commits, vars['rev'][:7]) |
Patrick Williams | b48b7b4 | 2016-08-17 15:04:38 -0500 | [diff] [blame] | 113 | |
| 114 | format = format.replace(name, ver) |
| 115 | |
| 116 | if found: |
| 117 | return format |
| 118 | |
| 119 | return '0+0' |