11# -*- coding: utf-8 -*-
22#
3- # 2022 Dennis Camera (dennis.camera at riiengineering.ch)
3+ # 2022,2026 Dennis Camera (dennis.camera at riiengineering.ch)
44# 2022-2023 Ander Punnar (ander at kvlt.ee)
55#
66# This file is part of skonfig.
@@ -35,6 +35,20 @@ def __silent_check_output(cmd, cwd):
3535 os .close (devnull )
3636
3737
38+ def __silent_check_status (cmd , cwd ):
39+ import os
40+ import subprocess
41+
42+ try :
43+ # NOTE: subprocess.DEVNULL was added with Python 3.3
44+ devnull = os .open (os .devnull , os .O_RDONLY )
45+ return subprocess .run (
46+ cmd , cwd = cwd , stdin = devnull , stdout = devnull , stderr = devnull ,
47+ shell = False , check = False ).returncode
48+ finally :
49+ os .close (devnull )
50+
51+
3852def __guess_git_version ():
3953 import os
4054
@@ -43,35 +57,24 @@ def __guess_git_version():
4357 # If .git exists (could be a directory or a file in case of a submodule) it
4458 # could be a Git repo, so try to generate version number from Git metadata.
4559 if os .path .exists (os .path .join (project_dir , ".git" )):
46- import re
47-
4860 # Try to use Git to generate the version
4961 try :
50- git_match = re .match (
51- r"^(.*?)(?:-([0-9]+)-g([0-9a-f]{7}))?(-dirty)?$" ,
52- __silent_check_output (
53- ["git" , "describe" , "--tags" , "--dirty" ,
54- "--abbrev=7" , "--match=[0-9]*" ], project_dir ))
55-
56- version = git_match .group (1 )
57-
58- if any (git_match .group (3 , 4 )):
59- # NOTE: this complex logic is to always produce
60- # a "+commit[.dirty]" suffix if HEAD is either not a
61- # tag/release or dirty.
62- version += "+"
62+ tag = __silent_check_output (
63+ ["git" , "tag" , "--points-at" , "HEAD" ],
64+ project_dir )
6365
64- # get commit id
65- if git_match .group (3 ):
66- version += git_match .group (3 )
67- else :
68- # git didn't produce a commit id, probably because it is a
69- # tag with dirty changes.
70- version += __silent_check_output (
71- ["git" , "rev-parse" , "--short=7" , "HEAD" ], project_dir )
66+ if tag :
67+ version = tag
68+ else :
69+ commit_date = __silent_check_output (
70+ ["git" , "show" , "-s" , "--pretty=format:%cs" , "HEAD" ],
71+ project_dir )
72+ version = "%s.dev%u" % (commit_date .replace ("-" , "" ), 0 )
7273
73- if git_match .group (4 ):
74- version += "." + git_match .group (4 )[1 :]
74+ if 0 != __silent_check_status (
75+ ["git" , "diff-index" , "--quiet" , "HEAD" , "--" ],
76+ project_dir ):
77+ version += "+dirty"
7578
7679 return version
7780 except Exception :
0 commit comments