Repository navigation
Expand file tree
/
Copy pathcommit.py
More file actions
executable file
·97 lines (74 loc) · 2.33 KB
/
Copy pathcommit.py
File metadata and controls
executable file
·97 lines (74 loc) · 2.33 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
#!/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 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 commit(author_date=None, message="", amend=False):
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(author_date)
command.append("--date='%s'" % author_date)
subprocess.run(command, env=env)
def check(prev_timestamp, timestamp):
return True
def get_timestamp(prev_timestamp):
arr = []
timestamp = math.floor(datetime.now().timestamp())
while len(arr) <= 1:
if check(prev_timestamp, timestamp):
arr.append(timestamp)
timestamp += 1
return random.choice(arr)
parser = argparse.ArgumentParser()
parser.add_argument("--date", "-d",
type=str, help="Date"
)
parser.add_argument("--message", "-m",
type=str, help="Message"
)
parser.add_argument("--amend", action="store_true", help="Amend")
args = parser.parse_args()
prev_timestamp = None
commit_info = get_commit_info()
if commit_info:
commit_dates = get_commit_dates(commit_info)
prev_timestamp = commit_dates["author_date"]
timestamp = prev_timestamp
if args.date:
timestamp = args.date
elif not args.amend:
timestamp = get_timestamp(prev_timestamp)
commit(
author_date=timestamp,
message=args.message,
amend=args.amend
)