-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-historical
executable file
·38 lines (29 loc) · 1.33 KB
/
git-historical
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
#! /usr/bin/env python3
import argparse, sys, subprocess, os
import arrow
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--checkout", action="store_true", default=False)
parser.add_argument("-s", "--since", required=True)
parser.add_argument("-b", "--branch", default="HEAD")
parser.add_argument("-j", "--jump", choices=('day', 'week', 'month'), default='month')
parser.add_argument('command', nargs=argparse.REMAINDER)
args = parser.parse_args()
date = then = arrow.get(args.since, "YYYY-MM-DD")
tip = subprocess.check_output(["git", "rev-parse", args.branch]).decode("ascii").strip()
shift = { args.jump + "s": +1 }
# TODO save/restore current checkout
now = arrow.utcnow()
env = os.environ.copy()
while date < now:
print("%s..." % date.format("YYYY-MM-DD"))
env["DATE"] = date.format("YYYY-MM-DD")
env["NEXTDATE"] = date.shift(**shift).format("YYYY-MM-DD")
sha = subprocess.check_output(["git", "rev-list", tip, "--before", date.format("YYYY-MM-DD"), "--max-count=1"]).decode("ascii").strip()
env["SHA"] = sha
if args.checkout:
# TODO use worktree?
subprocess.check_output(["git", "checkout", "--quiet", sha])
output = subprocess.check_output(" ".join(args.command), shell=True, env=env).decode("utf-8")
print(output)
# TODO option to write to named file?
date = date.shift(**shift)