1+ import json
2+ import subprocess
3+ from datetime import datetime
4+ from pathlib import Path
5+
6+
7+ class GitJsonGenerator :
8+ def generate_git_json (self , package_path : str = "resources" ):
9+ target_dir = Path (package_path )
10+ target_dir .mkdir (parents = True , exist_ok = True )
11+
12+ git_json = {
13+ "git.branch" : self .run_git_command ("git rev-parse --abbrev-ref HEAD" ),
14+ "git.build.host" : subprocess .run ("hostname" , shell = True , capture_output = True , text = True ).stdout .strip (),
15+ "git.build.time" : datetime .now ().strftime ("%d.%m.%Y @ %H:%M:%S %Z" ),
16+ "git.build.user.email" : self .run_git_command ("git config user.email" ),
17+ "git.build.user.name" : self .run_git_command ("git config user.name" ),
18+ "git.build.version" : "1.0.0" ,
19+ "git.closest.tag.commit.count" : self .run_git_command ("git describe --tags --abbrev=0 --match='*' --always" ),
20+ "git.closest.tag.name" : self .run_git_command ("git describe --tags --abbrev=0 --match='*' --always" ),
21+ "git.commit.author.time" : self .run_git_command ('git log -1 --format=%ad --date=format:"%d.%m.%Y @ %H:%M:%S "' ),
22+ "git.commit.committer.time" : self .run_git_command ('git log -1 --format=%cd --date=format:"%d.%m.%Y @ %H:%M:%S "' ),
23+ "git.commit.id" : self .run_git_command ("git rev-parse HEAD" ),
24+ "git.commit.id.abbrev" : self .run_git_command ("git rev-parse --short HEAD" ),
25+ "git.commit.id.describe" : self .run_git_command ("git describe --always --dirty" ),
26+ "git.commit.id.describe-short" : self .run_git_command ("git describe --always --dirty --abbrev=10" ),
27+ "git.commit.message.full" : self .run_git_command ("git log -1 --format=%B" ).replace ("\n " , "\\ n" ),
28+ "git.commit.message.short" : self .run_git_command ("git log -1 --format=%s" ),
29+ "git.commit.time" : self .run_git_command ('git log -1 --format=%cd --date=format:"%d.%m.%Y @ %H:%M:%S "' ),
30+ "git.commit.user.email" : self .run_git_command ("git log -1 --format=%ae" ),
31+ "git.commit.user.name" : self .run_git_command ("git log -1 --format=%an" ),
32+ "git.dirty" : "true" if self .run_git_command ("git diff --quiet; echo $?" ) == "1" else "false" ,
33+ "git.local.branch.ahead" : self .run_git_command ("git rev-list --count HEAD@{upstream}..HEAD 2>/dev/null || echo 0" ),
34+ "git.local.branch.behind" : self .run_git_command ("git rev-list --count HEAD...HEAD@{upstream} 2>/dev/null || echo 0" ),
35+ "git.remote.origin.url" : self .run_git_command ("git config --get remote.origin.url" ),
36+ "git.tags" : self .run_git_command ("git tag --points-at HEAD" ),
37+ "git.total.commit.count" : self .run_git_command ("git rev-list --count HEAD" )
38+ }
39+
40+ json_file = target_dir / "git.json"
41+ with open (json_file , "w" ) as f :
42+ json .dump (git_json , f , indent = 4 )
43+
44+ print (f"Generated git info: { json_file .absolute ()} " )
45+
46+ def run_git_command (self , cmd : str ) -> str :
47+ try :
48+ result = subprocess .run (cmd , shell = True , capture_output = True , text = True , check = True )
49+ return result .stdout .strip ()
50+ except subprocess .CalledProcessError :
51+ return ""
0 commit comments