]> git.0d.be Git - git-acab.git/blob - git-acab
add initial version
[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 subprocess
7 import sys
8 import time
9
10 parser = argparse.ArgumentParser()
11 parser.add_argument('--start', type=str, default='commit', help='now or commit')
12 parser.add_argument('--prefix', action='store_true')
13 parser.add_argument('-v', '--verbose', action='store_true')
14 args = parser.parse_args()
15
16 if args.start == 'commit':
17     p = subprocess.run(['git', 'show', '--format=format:%at'], capture_output=True)
18     timestamp = int(p.stdout[: p.stdout.index(b'\n')])
19 elif args.start == 'now':
20     timestamp = int(time.time())
21 else:
22     sys.stderr('unknown value for --start')
23     sys.exit(1)
24
25 t0 = time.time()
26 counter = 0
27 direction = -1
28 while True:
29     counter += 1
30     timestamp += direction
31     if counter % 3600 == 0:
32         # don't move more than one hour in the past, going back to initial time
33         # will give new commit hashes as the commit date (not author date) will
34         # be different.
35         direction = - direction
36     if args.verbose:
37         print(
38             '%5d - %s - [%s:%02d]'
39             % (
40                 counter,
41                 time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp)),
42                 int((time.time() - t0) // 60),
43                 int((time.time() - t0) % 60),
44             ),
45             end='\r',
46         )
47     p = subprocess.run(
48         [
49             'git',
50             'commit',
51             '--no-verify',
52             '--amend',
53             '--no-edit',
54             '--date=%s' % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(timestamp)),
55         ],
56         capture_output=True,
57     )
58     p = subprocess.run(['git', 'rev-parse', 'HEAD'], capture_output=True)
59     if args.prefix:
60         if b'acab' == p.stdout[:4]:
61             break
62     elif b'acab' in p.stdout:
63         break
64
65 if args.verbose:
66     print('\ngot %s' % p.stdout.decode().strip())