Repository navigation
Expand file tree
/
Copy pathmake_git_hash.py
More file actions
executable file
·219 lines (166 loc) · 6.27 KB
/
Copy pathmake_git_hash.py
File metadata and controls
executable file
·219 lines (166 loc) · 6.27 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
import sys, os, subprocess, hashlib, argparse, math
from datetime import datetime
def get_commit_info():
return subprocess.check_output(["git", "cat-file", "commit", "HEAD"]).decode('utf8')
def get_commit_dates(commit_info):
lines = commit_info.split("\n")
author_date = ""
committer_date = ""
for line in lines:
arr = line.split(" ")
if len(arr) > 0:
if arr[0] == "author":
author_date = int(arr[-2])
if arr[0] == "committer":
committer_date = int(arr[-2])
return {
"author_date": author_date,
"committer_date": committer_date,
}
def change_commit_info(commit_info, new_values):
lines = commit_info.split("\n")
for i, line in enumerate(lines):
arr = line.split(" ")
if len(arr) > 0:
if arr[0] == "author":
arr[-2] = str(new_values["author_date"])
if arr[0] == "committer":
arr[-2] = str(new_values["committer_date"])
lines[i] = " ".join(arr)
return "\n".join(lines)
def get_commit_hash(commit):
commit_str = 'commit %i\x00%s' % (len(commit), commit)
return hashlib.sha1(commit_str.encode('utf8')).hexdigest()
def get_commit_short_hash(commit_hash):
return commit_hash[0:7]
def validate_hash(commit_hash):
result = True;
if args.prefix and commit_hash[0:len(args.prefix)] != args.prefix:
result = False
if args.number:
if not get_commit_short_hash(commit_hash).isdigit():
return False
return result
def get_text_item(item):
if item is None:
return ""
return get_commit_short_hash(item["hash"])
def print_by_columns(arr):
terminal_width = os.get_terminal_size().columns
terminal_height = os.get_terminal_size().lines
max_columns = max(terminal_width // 10, 1)
count_lines = math.ceil(len(arr) / max_columns)
count_last_row = max_columns + len(arr) - count_lines * max_columns
arr = sorted(arr, key=lambda x: x["time"])
start = 0
matrix = {}
for i in range(max_columns):
count = count_lines if i < count_last_row else count_lines - 1
matrix[i] = arr[start:start + count]
if i >= count_last_row:
matrix[i].append(None)
start = start + count
lines = []
for i in range(0, count_lines):
items = [get_text_item(matrix[k][i]) for k in range(max_columns)]
items = list(filter(lambda s: s != "", items))
lines.append(" ".join(items))
if len(lines) > terminal_height - 5:
subprocess.run(["less"], input="\n".join(lines), text=True)
else:
for line in lines:
print(line)
def set_date(values, author_date, committer_date=-1):
if committer_date == -1:
committer_date = author_date
values["author_date"] = author_date
values["committer_date"] = committer_date
def generate_items(commit_info, start=0, end=1800):
values = get_commit_dates(commit_info)
arr = []
committer_date = values["author_date"] + args.start
for i in range(start, end):
set_date(values, committer_date + i)
new_commit_info = change_commit_info(commit_info, values)
new_commit_hash = get_commit_hash(new_commit_info)
if validate_hash(new_commit_hash):
arr.append({
"index": i,
"hash": new_commit_hash,
"time": committer_date + i,
})
return arr
def commit(author_date=None, committer_date=None, apply=True, message="", amend=True):
if committer_date is None:
committer_date = author_date
env = os.environ.copy()
command = ["git", "commit"]
if amend:
command.append("--amend")
if message:
command.append("-m")
command.append(message)
else:
command.append("-C")
command.append("HEAD")
if author_date:
env["GIT_COMMITTER_DATE"] = str(committer_date)
command.append("--date='%s'" % author_date)
subprocess.run(command, env=env)
def print_select_item(short_hash, start=-3600, end=3600):
commit_info = get_commit_info()
arr = generate_items(commit_info, start, end)
index = -1
for i, item in enumerate(arr):
if get_commit_short_hash(item["hash"]) == short_hash:
index = item["index"]
break
if index == -1:
print("Wrong prefix")
return
values = get_commit_dates(commit_info)
set_date(values, values["author_date"] + args.start + index)
new_commit_info = change_commit_info(commit_info, values)
new_commit_hash = get_commit_hash(new_commit_info)
if not args.apply:
print("Hash=%s" % (get_commit_short_hash(new_commit_hash)))
print("GIT_COMMITTER_DATE='%s' git commit --amend -C HEAD --date='%s'" % \
(values["committer_date"], values["author_date"]))
else:
commit(committer_date=values["committer_date"], author_date=values["author_date"])
# Create parser
parser = argparse.ArgumentParser()
parser.add_argument("--apply", action="store_true", help="Select and apply")
parser.add_argument("--number", action="store_true", help="Show only numbers")
parser.add_argument("--info", action="store_true", help="Show info")
parser.add_argument("--prefix", help="Set start prefix")
parser.add_argument("--select", type=str, help="Select item")
parser.add_argument("--start", type=int, default=0, help="Time offset")
parser.add_argument("--commit", type=str, help="Commit")
parser.add_argument("--message", "-m", type=str, help="Message")
# Parse arguments
args = parser.parse_args()
if args.select is not None:
print_select_item(args.select)
elif args.commit is not None:
commit(
author_date=args.commit,
message=args.message,
)
elif args.message is not None:
commit_info = get_commit_info()
values = get_commit_dates(commit_info)
commit(
author_date=values["author_date"],
message=args.message,
)
elif args.info:
print(get_commit_info())
else:
arr = []
if args.prefix or args.number:
arr = generate_items(get_commit_info(), start=-3600, end=3600)
else:
arr = generate_items(get_commit_info(), start=-30, end=30)
print_by_columns(arr)