]> git.0d.be Git - git-acab.git/blob - git-acab
b2f0af4d896e7171d60ccfa11d28b4ecf021a3dd
[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 os
7 import sys
8 import time
9
10 import git
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 while not os.path.exists('.git'):
20     os.chdir('..')
21     if os.getcwd() == '/':
22         sys.stderr('failed to find a git repository')
23         sys.exit(1)
24
25
26 def amend_commit(repo, initial_commit, start_timestamp):
27     t0 = time.time()
28     counter = 0
29     authored_timestamp = start_timestamp
30     while True:
31         authored_timestamp -= 1
32         for committed_timestamp in range(start_timestamp, authored_timestamp, -1):
33             counter += 1
34             if counter % 10000 == 0:
35                 repo.git.prune()
36             if not args.quiet:
37                 print(
38                     '%5d - %s - %s - [%s:%02d]'
39                     % (
40                         counter,
41                         time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(authored_timestamp)),
42                         time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(committed_timestamp)),
43                         int((time.time() - t0) // 60),
44                         int((time.time() - t0) % 60),
45                     ),
46                     end='\r',
47                 )
48
49             commit = initial_commit.replace(
50                 authored_date=authored_timestamp, committed_date=committed_timestamp
51             )
52             if args.prefix and not commit.hexsha.startswith('acab'):
53                 continue
54             if args.suffix and not commit.hexsha.endswith('acab'):
55                 continue
56             if 'acab' not in commit.hexsha:
57                 continue
58             repo.active_branch.set_commit(commit)
59             repo.git.prune()
60             return commit
61
62
63 repo = git.Repo()
64 commit = repo.commit('HEAD')
65
66 if args.start == 'commit':
67     start_timestamp = commit.authored_date
68 elif args.start == 'now':
69     start_timestamp = int(time.time())
70 else:
71     sys.stderr('unknown value for --start')
72     sys.exit(1)
73
74 new_head = amend_commit(repo, commit, start_timestamp)
75
76 if not args.quiet:
77     print('\ngot %s ðŸ”¥ðŸš“' % new_head.hexsha)