]> git.0d.be Git - git-acab.git/blob - git-acab
9645ac2ae8cf3f7c49fdda9a90d51e7a537a09f0
[git-acab.git] / git-acab
1 #! /usr/bin/python3
2 # amend HEAD commit to contain acab in its hash, it does so by repeatedly
3 # amending the commit with a new date.
4
5 import argparse
6 import hashlib
7 import os
8 import subprocess
9 import sys
10 import time
11
12 parser = argparse.ArgumentParser()
13 parser.add_argument('--start', type=str, default='commit', help='now or commit')
14 parser.add_argument('--prefix', action='store_true')
15 parser.add_argument('--suffix', action='store_true')
16 parser.add_argument('-q', '--quiet', action='store_true')
17 args = parser.parse_args()
18
19
20 def amend_commit(start_timestamp):
21     t0 = time.time()
22     counter = 0
23     cat_file = subprocess.run(['git', 'cat-file', 'commit', 'HEAD'], capture_output=True).stdout
24     cat_file_lines = cat_file.splitlines()
25     cat_file_lines[2] = cat_file_lines[2].replace(cat_file_lines[2].rsplit(b' ', 2)[1], b'$AUTHOR__$')
26     cat_file_lines[3] = cat_file_lines[3].replace(cat_file_lines[3].rsplit(b' ', 2)[1], b'$COMMITER$')
27     hashed_bytes_template = b'commit %s\0%s\n' % (
28         str(len(b'\n'.join(cat_file_lines)) + 1).encode(),
29         b'\n'.join(cat_file_lines),
30     )
31     authored_timestamp = start_timestamp
32     base_env = os.environ
33     while True:
34         authored_timestamp -= 1
35         for committed_timestamp in range(start_timestamp, authored_timestamp, -1):
36             counter += 1
37             if not args.quiet:
38                 print(
39                     '%5d - %s - %s - [%s:%02d]'
40                     % (
41                         counter,
42                         time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(authored_timestamp)),
43                         time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(committed_timestamp)),
44                         int((time.time() - t0) // 60),
45                         int((time.time() - t0) % 60),
46                     ),
47                     end='\r',
48                 )
49
50             hashed_bytes = hashed_bytes_template.replace(
51                 b'$AUTHOR__$', str(authored_timestamp).encode()
52             ).replace(b'$COMMITER$', str(committed_timestamp).encode())
53             new_hash = hashlib.sha1(hashed_bytes).hexdigest()
54             if args.prefix and not new_hash.startswith('acab'):
55                 continue
56             if args.suffix and not new_hash.endswith('acab'):
57                 continue
58             if 'acab' not in new_hash:
59                 continue
60             base_env['GIT_COMMITTER_DATE'] = time.strftime(
61                 '%Y-%m-%d %H:%M:%S', time.localtime(committed_timestamp)
62             )
63             subprocess.run(
64                 [
65                     'git',
66                     'commit',
67                     '--no-verify',
68                     '--amend',
69                     '--no-edit',
70                     '--date=%s' % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(authored_timestamp)),
71                 ],
72                 capture_output=True,
73                 env=base_env,
74             )
75             return
76
77
78 if args.start == 'commit':
79     p = subprocess.run(['git', 'show', '--format=format:%at'], capture_output=True)
80     start_timestamp = int(p.stdout[: p.stdout.index(b'\n')])
81 elif args.start == 'now':
82     start_timestamp = int(time.time())
83 else:
84     sys.stderr('unknown value for --start')
85     sys.exit(1)
86
87 amend_commit(start_timestamp)
88
89 if not args.quiet:
90     p = subprocess.run(['git', 'rev-parse', 'HEAD'], capture_output=True)
91     print('\ngot %s ðŸ”¥ðŸš“' % p.stdout.decode().strip())