1#!/usr/bin/env python3 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 get_git_dirs - prints work-tree and common git directories 16 commit_id.py unpack <ref_file> - check if <ref_file> exists, and if not 17 create it based on .git/packed-refs 18 commit_id.py position - print commit position 19 commit_id.py gen <file_to_write> - generate commit.h""" 20 21 22def grab_output(command, cwd): 23 return sp.Popen( 24 command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip().decode('utf-8') 25 26 27def get_git_dir(cwd): 28 return grab_output('git rev-parse --git-dir', cwd) 29 30 31def get_git_common_dir(cwd): 32 return grab_output('git rev-parse --git-common-dir', cwd) 33 34 35def get_commit_position(cwd): 36 return grab_output('git rev-list HEAD --count', cwd) 37 38 39def does_git_dir_exist(cwd): 40 ret = os.path.exists(os.path.join(cwd, '.git', 'HEAD')) 41 # .git may be a file with a gitdir directive pointing elsewhere. 42 if not ret and os.path.exists(os.path.join(cwd, '.git')): 43 ret = 'true' == grab_output('git rev-parse --is-inside-work-tree', cwd) 44 return ret 45 46 47def unpack_ref(ref_file, ref_file_full_path, packed_refs_full_path): 48 49 with open(packed_refs_full_path) as fin: 50 refs = fin.read().strip().split('\n') 51 52 # Strip comments 53 refs = [ref.split(' ') for ref in refs if ref.strip()[0] != '#'] 54 55 # Parse lines (which are in the format <hash> <ref_file>) and find the input file 56 refs = [git_hash for (git_hash, file_path) in refs if file_path == ref_file] 57 58 assert (len(refs) == 1) 59 git_hash = refs[0] 60 61 with open(ref_file_full_path, 'w') as fout: 62 fout.write(git_hash + '\n') 63 64 65if len(sys.argv) < 2: 66 sys.exit(usage) 67 68operation = sys.argv[1] 69 70# Set the root of ANGLE's repo as the working directory 71aosp_angle_path = os.path.join(os.path.dirname('.'), 'external', 'angle') 72aosp = os.path.exists(aosp_angle_path) 73cwd = aosp_angle_path if aosp else os.path.join(os.path.dirname(os.path.realpath(__file__)), '..') 74git_dir_exists = does_git_dir_exist(cwd) 75 76if operation == 'check': 77 if git_dir_exists: 78 print("1") 79 else: 80 print("0") 81 sys.exit(0) 82elif operation == 'get_git_dirs': 83 print(get_git_dir(cwd)) 84 print(get_git_common_dir(cwd)) 85 sys.exit(0) 86elif operation == 'unpack': 87 if len(sys.argv) < 3: 88 sys.exit(usage) 89 90 ref_file = sys.argv[2] 91 git_common_dir = get_git_common_dir(cwd) 92 ref_file_full_path = os.path.join(cwd, git_common_dir, ref_file) 93 ref_file_exists = os.path.exists(ref_file_full_path) 94 95 if not ref_file_exists: 96 packed_refs_full_path = os.path.join(cwd, git_common_dir, 'packed-refs') 97 unpack_ref(ref_file, ref_file_full_path, packed_refs_full_path) 98 99 sys.exit(0) 100elif operation == 'position': 101 if git_dir_exists: 102 print(get_commit_position(cwd)) 103 else: 104 print("0") 105 sys.exit(0) 106 107if len(sys.argv) < 3 or operation != 'gen': 108 sys.exit(usage) 109 110output_file = sys.argv[2] 111commit_id_size = 12 112commit_date = 'unknown date' 113commit_position = '0' 114 115# If the ANGLE_UPSTREAM_HASH environment variable is set, use it as 116# commit_id. commit_date will be 'unknown date' and commit_position will be 0 117# in this case. See details in roll_aosp.sh where commit_id.py is invoked. 118commit_id = os.environ.get('ANGLE_UPSTREAM_HASH') 119# If ANGLE_UPSTREAM_HASH environment variable is not set, use the git command 120# to get the git hash, when .git is available 121if git_dir_exists and not commit_id: 122 try: 123 commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd) 124 commit_date = grab_output('git show -s --format=%ci HEAD', cwd) or commit_date 125 commit_position = get_commit_position(cwd) or commit_position 126 except: 127 pass 128 129hfile = open(output_file, 'w') 130 131hfile.write('#define ANGLE_COMMIT_HASH "%s"\n' % (commit_id or "unknown hash")) 132hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size) 133hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date) 134hfile.write('#define ANGLE_COMMIT_POSITION %s\n' % commit_position) 135 136 137hfile.close() 138