File size: 1,723 Bytes
4c19a5a
 
 
 
ebc4336
4c19a5a
 
ebc4336
 
 
 
 
 
4c19a5a
 
ebc4336
 
4c19a5a
 
 
 
ebc4336
 
4c19a5a
 
 
 
 
 
 
 
 
 
 
 
ebc4336
 
4c19a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebc4336
 
4c19a5a
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from functools import lru_cache
import os
import subprocess


from modules.utils import constants

# 用于判断是否在hf spaces
try:
    import spaces
except:
    spaces = None

git = os.environ.get("GIT", "git")

in_hf_spaces = spaces is not None


@lru_cache()
def commit_hash():
    try:
        if in_hf_spaces:
            return "<hf>"
        return subprocess.check_output(
            [git, "-C", constants.ROOT_DIR, "rev-parse", "HEAD"],
            shell=False,
            encoding="utf8",
        ).strip()
    except Exception:
        return "<none>"


@lru_cache()
def git_tag():
    try:
        if in_hf_spaces:
            return "<hf>"
        return subprocess.check_output(
            [git, "-C", constants.ROOT_DIR, "describe", "--tags"],
            shell=False,
            encoding="utf8",
        ).strip()
    except Exception:
        try:

            changelog_md = os.path.join(
                os.path.dirname(os.path.dirname(__file__)), "CHANGELOG.md"
            )
            with open(changelog_md, "r", encoding="utf-8") as file:
                line = next((line.strip() for line in file if line.strip()), "<none>")
                line = line.replace("## ", "")
                return line
        except Exception:
            return "<none>"


@lru_cache()
def branch_name():
    try:
        if in_hf_spaces:
            return "<hf>"
        return subprocess.check_output(
            [git, "-C", constants.ROOT_DIR, "rev-parse", "--abbrev-ref", "HEAD"],
            shell=False,
            encoding="utf8",
        ).strip()
    except Exception:
        return "<none>"


if __name__ == "__main__":
    print(commit_hash())
    print(git_tag())
    print(branch_name())