Skip to content

Commit f7420aa

Browse files
committed
tools: add git-version to print a package version from git references
1 parent 909520e commit f7420aa

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

tools/git-version/git-version

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#! /usr/bin/env python3
2+
3+
# Daemon BSD Source Code
4+
# Copyright (c) 2024, Daemon Developers
5+
# All rights reserved.
6+
#
7+
# Redistribution and use in source and binary forms, with or without
8+
# modification, are permitted provided that the following conditions are met:
9+
# * Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
# * Neither the name of the <organization> nor the
15+
# names of its contributors may be used to endorse or promote products
16+
# derived from this software without specific prior written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
22+
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
import datetime
30+
import os
31+
import subprocess
32+
import sys
33+
import time
34+
35+
class GitVersion():
36+
def __init__(self, repository_dir):
37+
if not os.path.isdir(repository_dir):
38+
raise(ValueError, "not a directory")
39+
40+
self.git_command_list = ["git", "-C", repository_dir]
41+
42+
def runGitCommand(self, command_list):
43+
command_list = self.git_command_list + command_list
44+
return subprocess.run(command_list, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode("utf-8").rstrip()
45+
46+
def testGitCommand(self, command_list):
47+
command_list = self.git_command_list + command_list
48+
return subprocess.run(command_list, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode
49+
50+
def getDate(self, timestamp):
51+
return datetime.datetime.utcfromtimestamp(timestamp).strftime('%Y%m%d-%H%M%S')
52+
53+
def printVersion(self):
54+
tag_string="0"
55+
date_string=""
56+
ref_string=""
57+
dirt_string=""
58+
59+
git_last_commit_short = self.runGitCommand(["rev-parse", "HEAD"])[:7]
60+
61+
if git_last_commit_short:
62+
git_describe_string = self.runGitCommand(["describe", "--tags", "--match", "v[0-9].*"])[1:]
63+
64+
git_last_commit_timestamp = int(self.runGitCommand(["log", "-1", "--pretty=format:%ct"]))
65+
git_last_commit_date = self.getDate(git_last_commit_timestamp)
66+
67+
git_closest_tag = self.runGitCommand(["describe", "--tags", "--abbrev=0", "--match", "v[0-9].*"])[1:]
68+
69+
if git_closest_tag:
70+
tag_string = git_closest_tag
71+
72+
if git_closest_tag != git_describe_string:
73+
date_string = "-" + git_last_commit_date
74+
ref_string = "-" + git_last_commit_short
75+
else:
76+
date_string = "-" + git_last_commit_date
77+
ref_string = "-" + git_last_commit_short
78+
else:
79+
date_string = "-" + self.getDate(time.time())
80+
ref_string = "-0"
81+
82+
if self.testGitCommand(["diff", "--quiet"]) != 0:
83+
dirt_string = '+dirty'
84+
85+
print(tag_string + date_string + ref_string + dirt_string)
86+
87+
if __name__ == "__main__":
88+
if len(sys.argv) == 1:
89+
repository_dir = os.getcwd()
90+
elif len(sys.argv) == 2:
91+
repository_dir = sys.argv[1]
92+
else:
93+
raise(ValueError, "too many arguments")
94+
95+
gitVersion = GitVersion(repository_dir)
96+
gitVersion.printVersion()

0 commit comments

Comments
 (0)