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 9import subprocess as sp 10import sys 11import os 12 13usage = """\ 14Usage: 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 21def 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 26def get_commit_position(cwd): 27 return grab_output('git rev-list HEAD --count', cwd) 28 29 30def 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 48if len(sys.argv) < 2: 49 sys.exit(usage) 50 51operation = sys.argv[1] 52 53# Set the root of ANGLE's repo as the working directory 54cwd = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') 55aosp_angle_path = os.path.join(os.path.dirname('.'), 'external', 'angle') 56if os.path.exists(aosp_angle_path): 57 cwd = aosp_angle_path 58 59git_dir_exists = os.path.exists(os.path.join(cwd, '.git', 'HEAD')) 60 61if operation == 'check': 62 if git_dir_exists: 63 print("1") 64 else: 65 print("0") 66 sys.exit(0) 67elif 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) 80elif operation == 'position': 81 if git_dir_exists: 82 print(get_commit_position(cwd)) 83 else: 84 print("0") 85 sys.exit(0) 86 87if len(sys.argv) < 3 or operation != 'gen': 88 sys.exit(usage) 89 90output_file = sys.argv[2] 91commit_id_size = 12 92commit_id = 'unknown hash' 93commit_date = 'unknown date' 94commit_position = '0' 95enable_binary_loading = False 96 97if 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 106hfile = open(output_file, 'w') 107 108hfile.write('#define ANGLE_COMMIT_HASH "%s"\n' % commit_id) 109hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size) 110hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date) 111hfile.write('#define ANGLE_COMMIT_POSITION %s\n' % commit_position) 112 113if not enable_binary_loading: 114 hfile.write('#define ANGLE_DISABLE_PROGRAM_BINARY_LOAD\n') 115 116hfile.close() 117