Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/src/commit_id.py
1693 views
1
#!/usr/bin/env python
2
# Copyright 2018 The ANGLE Project Authors. All rights reserved.
3
# Use of this source code is governed by a BSD-style license that can be
4
# found in the LICENSE file.
5
6
# Generate commit.h with git commit hash.
7
#
8
9
import subprocess as sp
10
import sys
11
import os
12
13
usage = """\
14
Usage: commit_id.py check - check if git is present
15
commit_id.py unpack <ref_file> - check if <ref_file> exists, and if not
16
create it based on .git/packed-refs
17
commit_id.py position - print commit position
18
commit_id.py gen <file_to_write> - generate commit.h"""
19
20
21
def grab_output(command, cwd):
22
return sp.Popen(
23
command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip().decode('utf-8')
24
25
26
def get_commit_position(cwd):
27
return grab_output('git rev-list HEAD --count', cwd)
28
29
30
def unpack_ref(ref_file, ref_file_full_path, packed_refs_full_path):
31
32
with open(packed_refs_full_path) as fin:
33
refs = fin.read().strip().split('\n')
34
35
# Strip comments
36
refs = [ref.split(' ') for ref in refs if ref.strip()[0] != '#']
37
38
# Parse lines (which are in the format <hash> <ref_file>) and find the input file
39
refs = [git_hash for (git_hash, file_path) in refs if file_path == ref_file]
40
41
assert (len(refs) == 1)
42
git_hash = refs[0]
43
44
with open(ref_file_full_path, 'w') as fout:
45
fout.write(git_hash + '\n')
46
47
48
if len(sys.argv) < 2:
49
sys.exit(usage)
50
51
operation = sys.argv[1]
52
53
# Set the root of ANGLE's repo as the working directory
54
cwd = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')
55
aosp_angle_path = os.path.join(os.path.dirname('.'), 'external', 'angle')
56
if os.path.exists(aosp_angle_path):
57
cwd = aosp_angle_path
58
59
git_dir_exists = os.path.exists(os.path.join(cwd, '.git', 'HEAD'))
60
61
if operation == 'check':
62
if git_dir_exists:
63
print("1")
64
else:
65
print("0")
66
sys.exit(0)
67
elif operation == 'unpack':
68
if len(sys.argv) < 3:
69
sys.exit(usage)
70
71
ref_file = sys.argv[2]
72
ref_file_full_path = os.path.join(cwd, '.git', ref_file)
73
ref_file_exists = os.path.exists(ref_file_full_path)
74
75
if not ref_file_exists:
76
packed_refs_full_path = os.path.join(cwd, '.git', 'packed-refs')
77
unpack_ref(ref_file, ref_file_full_path, packed_refs_full_path)
78
79
sys.exit(0)
80
elif operation == 'position':
81
if git_dir_exists:
82
print(get_commit_position(cwd))
83
else:
84
print("0")
85
sys.exit(0)
86
87
if len(sys.argv) < 3 or operation != 'gen':
88
sys.exit(usage)
89
90
output_file = sys.argv[2]
91
commit_id_size = 12
92
commit_id = 'unknown hash'
93
commit_date = 'unknown date'
94
commit_position = '0'
95
enable_binary_loading = False
96
97
if git_dir_exists:
98
try:
99
commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd)
100
commit_date = grab_output('git show -s --format=%ci HEAD', cwd)
101
commit_position = get_commit_position(cwd)
102
enable_binary_loading = True
103
except:
104
pass
105
106
hfile = open(output_file, 'w')
107
108
hfile.write('#define ANGLE_COMMIT_HASH "%s"\n' % commit_id)
109
hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size)
110
hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date)
111
hfile.write('#define ANGLE_COMMIT_POSITION %s\n' % commit_position)
112
113
if not enable_binary_loading:
114
hfile.write('#define ANGLE_DISABLE_PROGRAM_BINARY_LOAD\n')
115
116
hfile.close()
117
118