This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
git_filters.py
135 lines (124 loc) · 4.93 KB
/
git_filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# file to implement git smudge and clean filters
# to enable calling of this file add a section to the .git/config like this:
#
# [filter "createVersionFile"]
# clean = python git_filters.py --tag-version
# smudge = python git_filters.py --record-version
#
# this is failing as somehow the file is getting overwritten, at least on clean
# but this routine does what it is supposed to do when it is called directly
#
import os
import sys
import datetime as dt
import git
# get location of the GSAS-II files
# assumed to be the parent of location of this file
path2GSAS2 = os.path.dirname(os.path.dirname(
os.path.abspath(os.path.expanduser(__file__))))
# and the repo is in the parent of that
path2repo = os.path.dirname(path2GSAS2)
if __name__ == '__main__':
help = False
actionName = None
for arg in sys.argv[1:]:
if '--record-version' in arg: # records version info
if actionName:
print(f'previous option conflicts with {arg}')
help = True
break
actionName = 'record'
elif '--tag-version' in arg: # records version info
if actionName:
print(f'previous option conflicts with {arg}')
help = True
break
actionName = 'tag'
elif '--help' in arg:
help = True
break
else:
print(f'unknown arg {arg}')
help = True
if help or len(sys.argv) == 1:
print(f'''Options when running {sys.argv[0]}:
--record-version records version info into a file
that can be sourced to determine
GSAS-II version info, when not
not available from git
--tag-version creates version number tags
''')
sys.exit()
# for debug, redirect output to a log file
#sys.stderr = sys.stdout = open('/tmp/gitfilter.log','a')
if actionName == 'record':
# create a file with GSAS-II version infomation
try:
g2repo = git.Repo(path2repo)
except:
print('Launch of gitpython for version file failed'+
f' with path {path2repo}')
sys.exit()
commit = g2repo.head.commit
ctim = commit.committed_datetime.strftime('%d-%b-%Y %H:%M')
now = dt.datetime.now().replace(
tzinfo=commit.committed_datetime.tzinfo)
commit0 = commit.hexsha
tags0 = g2repo.git.tag('--points-at',commit).split('\n')
history = list(g2repo.iter_commits('HEAD'))
for i in history[1:]:
tags = g2repo.git.tag('--points-at',i)
if not tags: continue
commitm1 = i.hexsha
tagsm1 = tags.split('\n')
break
pyfile = os.path.join(path2GSAS2,'git_verinfo.py')
try:
fp = open(pyfile,'w')
except:
print(f'Creation of git version file {pyfile} failed')
sys.exit()
fp.write('# -*- coding: utf-8 -*-\n')
fp.write(f'# {os.path.split(pyfile)[1]} - GSAS-II version info from git\n')
fp.write(f'# Do not edit, generated by {" ".join(sys.argv)!r} command\n')
fp.write(f'# Created {now}\n\n')
fp.write(f'git_version = {commit0!r}\n')
if tags:
fp.write(f'git_tags = {tags0}\n')
else:
fp.write('git_tags = []\n')
fp.write(f'git_prevtaggedversion = {commitm1!r}\n')
fp.write(f'git_prevtags = {tagsm1}\n')
fp.close()
print(f'Created git version file {pyfile} at {now} for {commit0[:6]!r}')
sys.exit()
elif actionName == 'tag':
g2repo = git.Repo(path2repo)
if g2repo.active_branch.name != 'master':
print(f'Not on master branch {commit0[:6]!r}')
sys.exit()
if g2repo.head.is_detached:
print(f'Detached head {commit0[:6]!r}')
sys.exit()
# make a list of tags without a dash; get the largest numeric tag
numtag = [i for i in g2repo.tags if '-' not in i.name]
max_numeric = max([int(i.name) for i in numtag if i.name.isdecimal()])
# scan for the newest untagged commits, stopping at the first
# tagged one
untagged = []
for i,c in enumerate(g2repo.iter_commits('HEAD')):
if i > 50: break
tags = g2repo.git.tag('--points-at',c).split('\n')
if tags == ['']:
untagged.append(c)
else:
break
# add tags, processing the oldest untagged version first
tagnum = max_numeric
for i in sorted(untagged,key=lambda k:k.committed_datetime):
tagnum += 1
if str(tagnum) in g2repo.tags:
print(f'Error: {tagnum} would be repeated')
break
g2repo.create_tag(str(tagnum),ref=i)
print(f'created tag {tagnum} for {i.hexsha[:6]}')