|
|
@@ -0,0 +1,110 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+
|
|
|
+from argparse import ArgumentParser,Namespace
|
|
|
+import re
|
|
|
+
|
|
|
+STMT_CREATE='''
|
|
|
+CREATE TABLE IF NOT EXISTS gourcelog (
|
|
|
+ time DATETIME NOT NULL,
|
|
|
+ user text,
|
|
|
+ action text,
|
|
|
+ path text
|
|
|
+);
|
|
|
+'''
|
|
|
+STMT_INSERT='''
|
|
|
+INSERT INTO gourcelog VALUES (@time,@user,@action,@path)
|
|
|
+'''
|
|
|
+STMT_SELECT='''
|
|
|
+SELECT time,user,action,path FROM gourcelog ORDER BY time ASC
|
|
|
+'''
|
|
|
+
|
|
|
+def main():
|
|
|
+ p = parser()
|
|
|
+ args = p.parse_args()
|
|
|
+ import sys
|
|
|
+ sys.exit(args.func(args) or 0)
|
|
|
+
|
|
|
+def parser():
|
|
|
+ p = ArgumentParser()
|
|
|
+ p.set_defaults(func=lambda x: p.print_help())
|
|
|
+ sub = p.add_subparsers()
|
|
|
+ p_render = sub.add_parser('render')
|
|
|
+ p_render.set_defaults(func=render)
|
|
|
+ p_render.add_argument('--gource', default='gource')
|
|
|
+ p_join = sub.add_parser('gitjoin')
|
|
|
+ p_join.set_defaults(func=gitjoin)
|
|
|
+ p_join.add_argument('source')
|
|
|
+ p_join.add_argument('output')
|
|
|
+ p_join.add_argument('-p','--prefix')
|
|
|
+ return p
|
|
|
+
|
|
|
+def gitjoin(args: Namespace):
|
|
|
+ print(args)
|
|
|
+ import os
|
|
|
+ import subprocess
|
|
|
+ import sqlite3
|
|
|
+ GIT_CMD = ["git", "log", "--all", '--pretty=format:user:%aN%n%ct', "--reverse", "--raw", "--encoding=UTF-8", "--no-renames"]
|
|
|
+
|
|
|
+ with sqlite3.connect(args.output) as db:
|
|
|
+ db.execute(STMT_CREATE)
|
|
|
+ db.commit()
|
|
|
+ for fp in gitwalk(args.source):
|
|
|
+ p = fp[len(args.source):]
|
|
|
+ proc = subprocess.run(GIT_CMD, cwd=fp, stdout=subprocess.PIPE)
|
|
|
+ if not proc.returncode == 0:
|
|
|
+ continue
|
|
|
+ print(fp)
|
|
|
+ try:
|
|
|
+ gitlog = proc.stdout.decode()
|
|
|
+ except Exception as ex:
|
|
|
+ print(ex)
|
|
|
+ continue
|
|
|
+ for l in gource_format(gitlog):
|
|
|
+ l[3] = os.path.join(p, l[3].lstrip('/'))
|
|
|
+ db.execute(STMT_INSERT, {
|
|
|
+ 'time': int(l[0]),
|
|
|
+ 'user': l[1],
|
|
|
+ 'action': l[2],
|
|
|
+ 'path': l[3],
|
|
|
+ })
|
|
|
+ # print('|'.join(l))
|
|
|
+ db.commit()
|
|
|
+ for it in db.execute(STMT_SELECT):
|
|
|
+ print(it)
|
|
|
+
|
|
|
+def gource_format(inp: str):
|
|
|
+ import subprocess
|
|
|
+ GOURCE_CMD = ["gource", "--log-format", "git", "--output-custom-log", "-", "-"]
|
|
|
+ with subprocess.Popen(GOURCE_CMD, stdout=subprocess.PIPE, stdin=subprocess.PIPE) as proc:
|
|
|
+ out, err = proc.communicate(input=inp.encode())
|
|
|
+ if proc.returncode != 0:
|
|
|
+ return
|
|
|
+ for l in out.decode().splitlines():
|
|
|
+ ls = l.split('|')
|
|
|
+ if len(ls) != 4:
|
|
|
+ continue
|
|
|
+ yield ls
|
|
|
+ # sed "s, \([ACDMRTU]\)\t, \1\t$REL/," | gource --log-format git --output-custom-log - -
|
|
|
+
|
|
|
+def gitwalk(root: str):
|
|
|
+ import os
|
|
|
+ if not os.path.isdir(root):
|
|
|
+ return
|
|
|
+ if root.endswith('.git'):
|
|
|
+ yield root
|
|
|
+ else:
|
|
|
+ ls = os.listdir(root)
|
|
|
+ for d in ls:
|
|
|
+ dj = os.path.join(root,d)
|
|
|
+ for p in gitwalk(dj):
|
|
|
+ yield p
|
|
|
+
|
|
|
+def tmpid(key: str):
|
|
|
+ import hashlib
|
|
|
+ return hashlib.sha256(key).hexdigest()
|
|
|
+
|
|
|
+def render(args: Namespace):
|
|
|
+ print(args)
|
|
|
+
|
|
|
+if __name__=='__main__':
|
|
|
+ main()
|