blob: 7f711e75a623d274ee29ab74bbb6fbd18e7a56c2 [file] [log] [blame]
Jamie Madill42f529e2014-02-24 12:45:32 -05001import subprocess as sp
2import sys
Jamie Madill9c4b24a2014-06-12 13:41:17 -04003import os
Jamie Madill42f529e2014-02-24 12:45:32 -05004
Jamie Madill9c4b24a2014-06-12 13:41:17 -04005# Usage: commit_id.py check <angle_dir> (checks if git is present)
6# Usage: commit_id.py gen <angle_dir> <file_to_write> (generates commit id)
Jamie Madill42f529e2014-02-24 12:45:32 -05007
Jamie Madill9c4b24a2014-06-12 13:41:17 -04008def grab_output(command, cwd):
9 return sp.Popen(command, stdout=sp.PIPE, shell=True, cwd=cwd).communicate()[0].strip()
10
11operation = sys.argv[1]
12cwd = sys.argv[2]
13
14if operation == 'check':
15 index_path = os.path.join(cwd, '.git', 'index')
16 if os.path.exists(index_path):
17 print("1")
18 else:
19 print("0")
20 sys.exit(0)
21
22output_file = sys.argv[3]
Jamie Madill42f529e2014-02-24 12:45:32 -050023commit_id_size = 12
24
25try:
Jamie Madill9c4b24a2014-06-12 13:41:17 -040026 commit_id = grab_output('git rev-parse --short=%d HEAD' % commit_id_size, cwd)
27 commit_date = grab_output('git show -s --format=%ci HEAD', cwd)
Jamie Madill42f529e2014-02-24 12:45:32 -050028except:
29 commit_id = 'invalid-hash'
30 commit_date = 'invalid-date'
31
Jamie Madill9c4b24a2014-06-12 13:41:17 -040032hfile = open(output_file, 'w')
Jamie Madill42f529e2014-02-24 12:45:32 -050033
34hfile.write('#define ANGLE_COMMIT_HASH "%s"\n' % commit_id)
35hfile.write('#define ANGLE_COMMIT_HASH_SIZE %d\n' % commit_id_size)
36hfile.write('#define ANGLE_COMMIT_DATE "%s"\n' % commit_date)
37
38hfile.close()