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